a4880d6198cc9543c689df6a17e5d3129aa8c85f
[firefly-linux-kernel-4.4.55.git] / drivers / usb / serial / mos7840.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Clean ups from Moschip version and a few ioctl implementations by:
17  *      Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18  *
19  * Originally based on drivers/usb/serial/io_edgeport.c which is:
20  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
21  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22  *
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
37
38 /*
39  * Version Information
40  */
41 #define DRIVER_VERSION "1.3.2"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
43
44 /*
45  * 16C50 UART register defines
46  */
47
48 #define LCR_BITS_5             0x00     /* 5 bits/char */
49 #define LCR_BITS_6             0x01     /* 6 bits/char */
50 #define LCR_BITS_7             0x02     /* 7 bits/char */
51 #define LCR_BITS_8             0x03     /* 8 bits/char */
52 #define LCR_BITS_MASK          0x03     /* Mask for bits/char field */
53
54 #define LCR_STOP_1             0x00     /* 1 stop bit */
55 #define LCR_STOP_1_5           0x04     /* 1.5 stop bits (if 5   bits/char) */
56 #define LCR_STOP_2             0x04     /* 2 stop bits   (if 6-8 bits/char) */
57 #define LCR_STOP_MASK          0x04     /* Mask for stop bits field */
58
59 #define LCR_PAR_NONE           0x00     /* No parity */
60 #define LCR_PAR_ODD            0x08     /* Odd parity */
61 #define LCR_PAR_EVEN           0x18     /* Even parity */
62 #define LCR_PAR_MARK           0x28     /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE          0x38     /* Force parity bit to 0 */
64 #define LCR_PAR_MASK           0x38     /* Mask for parity field */
65
66 #define LCR_SET_BREAK          0x40     /* Set Break condition */
67 #define LCR_DL_ENABLE          0x80     /* Enable access to divisor latch */
68
69 #define MCR_DTR                0x01     /* Assert DTR */
70 #define MCR_RTS                0x02     /* Assert RTS */
71 #define MCR_OUT1               0x04     /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE          0x08     /* Enable interrupt outputs */
73 #define MCR_LOOPBACK           0x10     /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY            0x20     /* Enable any char to exit XOFF mode */
75
76 #define MOS7840_MSR_CTS        0x10     /* Current state of CTS */
77 #define MOS7840_MSR_DSR        0x20     /* Current state of DSR */
78 #define MOS7840_MSR_RI         0x40     /* Current state of RI */
79 #define MOS7840_MSR_CD         0x80     /* Current state of CD */
80
81 /*
82  * Defines used for sending commands to port
83  */
84
85 #define MOS_WDR_TIMEOUT         5000    /* default urb timeout */
86
87 #define MOS_PORT1       0x0200
88 #define MOS_PORT2       0x0300
89 #define MOS_VENREG      0x0000
90 #define MOS_MAX_PORT    0x02
91 #define MOS_WRITE       0x0E
92 #define MOS_READ        0x0D
93
94 /* Requests */
95 #define MCS_RD_RTYPE    0xC0
96 #define MCS_WR_RTYPE    0x40
97 #define MCS_RDREQ       0x0D
98 #define MCS_WRREQ       0x0E
99 #define MCS_CTRL_TIMEOUT        500
100 #define VENDOR_READ_LENGTH      (0x01)
101
102 #define MAX_NAME_LEN    64
103
104 #define ZLP_REG1  0x3A          /* Zero_Flag_Reg1    58 */
105 #define ZLP_REG5  0x3E          /* Zero_Flag_Reg5    62 */
106
107 /* For higher baud Rates use TIOCEXBAUD */
108 #define TIOCEXBAUD     0x5462
109
110 /* vendor id and device id defines */
111
112 /* The native mos7840/7820 component */
113 #define USB_VENDOR_ID_MOSCHIP           0x9710
114 #define MOSCHIP_DEVICE_ID_7840          0x7840
115 #define MOSCHIP_DEVICE_ID_7820          0x7820
116 #define MOSCHIP_DEVICE_ID_7810          0x7810
117 /* The native component can have its vendor/device id's overridden
118  * in vendor-specific implementations.  Such devices can be handled
119  * by making a change here, in id_table.
120  */
121 #define USB_VENDOR_ID_BANDB              0x0856
122 #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
123 #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
124 #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
125 #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
126 #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
127 #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
128 #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
129 #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
130 #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
131 #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
132 #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
133 #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
134 #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
135
136 /* This driver also supports
137  * ATEN UC2324 device using Moschip MCS7840
138  * ATEN UC2322 device using Moschip MCS7820
139  */
140 #define USB_VENDOR_ID_ATENINTL          0x0557
141 #define ATENINTL_DEVICE_ID_UC2324       0x2011
142 #define ATENINTL_DEVICE_ID_UC2322       0x7820
143
144 /* Interrupt Routine Defines    */
145
146 #define SERIAL_IIR_RLS      0x06
147 #define SERIAL_IIR_MS       0x00
148
149 /*
150  *  Emulation of the bit mask on the LINE STATUS REGISTER.
151  */
152 #define SERIAL_LSR_DR       0x0001
153 #define SERIAL_LSR_OE       0x0002
154 #define SERIAL_LSR_PE       0x0004
155 #define SERIAL_LSR_FE       0x0008
156 #define SERIAL_LSR_BI       0x0010
157
158 #define MOS_MSR_DELTA_CTS   0x10
159 #define MOS_MSR_DELTA_DSR   0x20
160 #define MOS_MSR_DELTA_RI    0x40
161 #define MOS_MSR_DELTA_CD    0x80
162
163 /* Serial Port register Address */
164 #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
165 #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
166 #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
167 #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
168 #define LINE_STATUS_REGISTER       ((__u16)(0x05))
169 #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
170 #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
171 #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
172 #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
173
174 #define CLK_MULTI_REGISTER         ((__u16)(0x02))
175 #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
176 #define GPIO_REGISTER              ((__u16)(0x07))
177
178 #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
179
180 /*
181  * URB POOL related defines
182  */
183 #define NUM_URBS                        16      /* URB Count */
184 #define URB_TRANSFER_BUFFER_SIZE        32      /* URB Size  */
185
186 /* LED on/off milliseconds*/
187 #define LED_ON_MS       500
188 #define LED_OFF_MS      500
189
190 static int device_type;
191
192 static const struct usb_device_id id_table[] = {
193         {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
194         {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
195         {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
196         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
197         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
198         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
199         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
200         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
204         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
205         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
206         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
207         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
208         {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
209         {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
210         {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
211         {}                      /* terminating entry */
212 };
213 MODULE_DEVICE_TABLE(usb, id_table);
214
215 /* This structure holds all of the local port information */
216
217 struct moschip_port {
218         int port_num;           /*Actual port number in the device(1,2,etc) */
219         struct urb *write_urb;  /* write URB for this port */
220         struct urb *read_urb;   /* read URB for this port */
221         struct urb *int_urb;
222         __u8 shadowLCR;         /* last LCR value received */
223         __u8 shadowMCR;         /* last MCR value received */
224         char open;
225         char open_ports;
226         char zombie;
227         wait_queue_head_t wait_chase;   /* for handling sleeping while waiting for chase to finish */
228         wait_queue_head_t delta_msr_wait;       /* for handling sleeping while waiting for msr change to happen */
229         int delta_msr_cond;
230         struct async_icount icount;
231         struct usb_serial_port *port;   /* loop back to the owner of this object */
232
233         /* Offsets */
234         __u8 SpRegOffset;
235         __u8 ControlRegOffset;
236         __u8 DcrRegOffset;
237         /* for processing control URBS in interrupt context */
238         struct urb *control_urb;
239         struct usb_ctrlrequest *dr;
240         char *ctrl_buf;
241         int MsrLsr;
242
243         spinlock_t pool_lock;
244         struct urb *write_urb_pool[NUM_URBS];
245         char busy[NUM_URBS];
246         bool read_urb_busy;
247
248         /* For device(s) with LED indicator */
249         bool has_led;
250         bool led_flag;
251         struct timer_list led_timer1;   /* Timer for LED on */
252         struct timer_list led_timer2;   /* Timer for LED off */
253 };
254
255 static bool debug;
256
257 /*
258  * mos7840_set_reg_sync
259  *      To set the Control register by calling usb_fill_control_urb function
260  *      by passing usb_sndctrlpipe function as parameter.
261  */
262
263 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
264                                 __u16 val)
265 {
266         struct usb_device *dev = port->serial->dev;
267         val = val & 0x00ff;
268         dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
269
270         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
271                                MCS_WR_RTYPE, val, reg, NULL, 0,
272                                MOS_WDR_TIMEOUT);
273 }
274
275 /*
276  * mos7840_get_reg_sync
277  *      To set the Uart register by calling usb_fill_control_urb function by
278  *      passing usb_rcvctrlpipe function as parameter.
279  */
280
281 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
282                                 __u16 *val)
283 {
284         struct usb_device *dev = port->serial->dev;
285         int ret = 0;
286         u8 *buf;
287
288         buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
289         if (!buf)
290                 return -ENOMEM;
291
292         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
293                               MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
294                               MOS_WDR_TIMEOUT);
295         *val = buf[0];
296         dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
297
298         kfree(buf);
299         return ret;
300 }
301
302 /*
303  * mos7840_set_uart_reg
304  *      To set the Uart register by calling usb_fill_control_urb function by
305  *      passing usb_sndctrlpipe function as parameter.
306  */
307
308 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
309                                 __u16 val)
310 {
311
312         struct usb_device *dev = port->serial->dev;
313         val = val & 0x00ff;
314         /* For the UART control registers, the application number need
315            to be Or'ed */
316         if (port->serial->num_ports == 4) {
317                 val |= (((__u16) port->number -
318                                 (__u16) (port->serial->minor)) + 1) << 8;
319         } else {
320                 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
321                         val |= (((__u16) port->number -
322                               (__u16) (port->serial->minor)) + 1) << 8;
323                 } else {
324                         val |= (((__u16) port->number -
325                               (__u16) (port->serial->minor)) + 2) << 8;
326                 }
327         }
328         dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
329         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
330                                MCS_WR_RTYPE, val, reg, NULL, 0,
331                                MOS_WDR_TIMEOUT);
332
333 }
334
335 /*
336  * mos7840_get_uart_reg
337  *      To set the Control register by calling usb_fill_control_urb function
338  *      by passing usb_rcvctrlpipe function as parameter.
339  */
340 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
341                                 __u16 *val)
342 {
343         struct usb_device *dev = port->serial->dev;
344         int ret = 0;
345         __u16 Wval;
346         u8 *buf;
347
348         buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
349         if (!buf)
350                 return -ENOMEM;
351
352         /* Wval  is same as application number */
353         if (port->serial->num_ports == 4) {
354                 Wval =
355                     (((__u16) port->number - (__u16) (port->serial->minor)) +
356                      1) << 8;
357         } else {
358                 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
359                         Wval = (((__u16) port->number -
360                               (__u16) (port->serial->minor)) + 1) << 8;
361                 } else {
362                         Wval = (((__u16) port->number -
363                               (__u16) (port->serial->minor)) + 2) << 8;
364                 }
365         }
366         dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
367         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
368                               MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
369                               MOS_WDR_TIMEOUT);
370         *val = buf[0];
371
372         kfree(buf);
373         return ret;
374 }
375
376 static void mos7840_dump_serial_port(struct usb_serial_port *port,
377                                      struct moschip_port *mos7840_port)
378 {
379
380         dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
381         dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
382         dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
383
384 }
385
386 /************************************************************************/
387 /************************************************************************/
388 /*             I N T E R F A C E   F U N C T I O N S                    */
389 /*             I N T E R F A C E   F U N C T I O N S                    */
390 /************************************************************************/
391 /************************************************************************/
392
393 static inline void mos7840_set_port_private(struct usb_serial_port *port,
394                                             struct moschip_port *data)
395 {
396         usb_set_serial_port_data(port, (void *)data);
397 }
398
399 static inline struct moschip_port *mos7840_get_port_private(struct
400                                                             usb_serial_port
401                                                             *port)
402 {
403         return (struct moschip_port *)usb_get_serial_port_data(port);
404 }
405
406 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
407 {
408         struct moschip_port *mos7840_port;
409         struct async_icount *icount;
410         mos7840_port = port;
411         icount = &mos7840_port->icount;
412         if (new_msr &
413             (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
414              MOS_MSR_DELTA_CD)) {
415                 icount = &mos7840_port->icount;
416
417                 /* update input line counters */
418                 if (new_msr & MOS_MSR_DELTA_CTS) {
419                         icount->cts++;
420                         smp_wmb();
421                 }
422                 if (new_msr & MOS_MSR_DELTA_DSR) {
423                         icount->dsr++;
424                         smp_wmb();
425                 }
426                 if (new_msr & MOS_MSR_DELTA_CD) {
427                         icount->dcd++;
428                         smp_wmb();
429                 }
430                 if (new_msr & MOS_MSR_DELTA_RI) {
431                         icount->rng++;
432                         smp_wmb();
433                 }
434         }
435 }
436
437 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
438 {
439         struct async_icount *icount;
440
441         if (new_lsr & SERIAL_LSR_BI) {
442                 /*
443                  * Parity and Framing errors only count if they
444                  * occur exclusive of a break being
445                  * received.
446                  */
447                 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
448         }
449
450         /* update input line counters */
451         icount = &port->icount;
452         if (new_lsr & SERIAL_LSR_BI) {
453                 icount->brk++;
454                 smp_wmb();
455         }
456         if (new_lsr & SERIAL_LSR_OE) {
457                 icount->overrun++;
458                 smp_wmb();
459         }
460         if (new_lsr & SERIAL_LSR_PE) {
461                 icount->parity++;
462                 smp_wmb();
463         }
464         if (new_lsr & SERIAL_LSR_FE) {
465                 icount->frame++;
466                 smp_wmb();
467         }
468 }
469
470 /************************************************************************/
471 /************************************************************************/
472 /*            U S B  C A L L B A C K   F U N C T I O N S                */
473 /*            U S B  C A L L B A C K   F U N C T I O N S                */
474 /************************************************************************/
475 /************************************************************************/
476
477 static void mos7840_control_callback(struct urb *urb)
478 {
479         unsigned char *data;
480         struct moschip_port *mos7840_port;
481         struct device *dev = &urb->dev->dev;
482         __u8 regval = 0x0;
483         int result = 0;
484         int status = urb->status;
485
486         mos7840_port = urb->context;
487
488         switch (status) {
489         case 0:
490                 /* success */
491                 break;
492         case -ECONNRESET:
493         case -ENOENT:
494         case -ESHUTDOWN:
495                 /* this urb is terminated, clean up */
496                 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
497                 return;
498         default:
499                 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
500                 goto exit;
501         }
502
503         dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
504         dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
505                 mos7840_port->MsrLsr, mos7840_port->port_num);
506         data = urb->transfer_buffer;
507         regval = (__u8) data[0];
508         dev_dbg(dev, "%s data is %x\n", __func__, regval);
509         if (mos7840_port->MsrLsr == 0)
510                 mos7840_handle_new_msr(mos7840_port, regval);
511         else if (mos7840_port->MsrLsr == 1)
512                 mos7840_handle_new_lsr(mos7840_port, regval);
513
514 exit:
515         spin_lock(&mos7840_port->pool_lock);
516         if (!mos7840_port->zombie)
517                 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
518         spin_unlock(&mos7840_port->pool_lock);
519         if (result) {
520                 dev_err(dev, "%s - Error %d submitting interrupt urb\n",
521                         __func__, result);
522         }
523 }
524
525 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
526                            __u16 *val)
527 {
528         struct usb_device *dev = mcs->port->serial->dev;
529         struct usb_ctrlrequest *dr = mcs->dr;
530         unsigned char *buffer = mcs->ctrl_buf;
531         int ret;
532
533         dr->bRequestType = MCS_RD_RTYPE;
534         dr->bRequest = MCS_RDREQ;
535         dr->wValue = cpu_to_le16(Wval); /* 0 */
536         dr->wIndex = cpu_to_le16(reg);
537         dr->wLength = cpu_to_le16(2);
538
539         usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
540                              (unsigned char *)dr, buffer, 2,
541                              mos7840_control_callback, mcs);
542         mcs->control_urb->transfer_buffer_length = 2;
543         ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
544         return ret;
545 }
546
547 static void mos7840_set_led_callback(struct urb *urb)
548 {
549         switch (urb->status) {
550         case 0:
551                 /* Success */
552                 break;
553         case -ECONNRESET:
554         case -ENOENT:
555         case -ESHUTDOWN:
556                 /* This urb is terminated, clean up */
557                 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
558                         __func__, urb->status);
559                 break;
560         default:
561                 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
562                         __func__, urb->status);
563         }
564 }
565
566 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
567                                 __u16 reg)
568 {
569         struct usb_device *dev = mcs->port->serial->dev;
570         struct usb_ctrlrequest *dr = mcs->dr;
571
572         dr->bRequestType = MCS_WR_RTYPE;
573         dr->bRequest = MCS_WRREQ;
574         dr->wValue = cpu_to_le16(wval);
575         dr->wIndex = cpu_to_le16(reg);
576         dr->wLength = cpu_to_le16(0);
577
578         usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
579                 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
580
581         usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
582 }
583
584 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
585                                 __u16 val)
586 {
587         struct usb_device *dev = port->serial->dev;
588
589         usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
590                         val, reg, NULL, 0, MOS_WDR_TIMEOUT);
591 }
592
593 static void mos7840_led_off(unsigned long arg)
594 {
595         struct moschip_port *mcs = (struct moschip_port *) arg;
596
597         /* Turn off LED */
598         mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
599         mod_timer(&mcs->led_timer2,
600                                 jiffies + msecs_to_jiffies(LED_OFF_MS));
601 }
602
603 static void mos7840_led_flag_off(unsigned long arg)
604 {
605         struct moschip_port *mcs = (struct moschip_port *) arg;
606
607         mcs->led_flag = false;
608 }
609
610 /*****************************************************************************
611  * mos7840_interrupt_callback
612  *      this is the callback function for when we have received data on the
613  *      interrupt endpoint.
614  *****************************************************************************/
615
616 static void mos7840_interrupt_callback(struct urb *urb)
617 {
618         int result;
619         int length;
620         struct moschip_port *mos7840_port;
621         struct usb_serial *serial;
622         __u16 Data;
623         unsigned char *data;
624         __u8 sp[5], st;
625         int i, rv = 0;
626         __u16 wval, wreg = 0;
627         int status = urb->status;
628
629         switch (status) {
630         case 0:
631                 /* success */
632                 break;
633         case -ECONNRESET:
634         case -ENOENT:
635         case -ESHUTDOWN:
636                 /* this urb is terminated, clean up */
637                 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
638                         __func__, status);
639                 return;
640         default:
641                 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
642                         __func__, status);
643                 goto exit;
644         }
645
646         length = urb->actual_length;
647         data = urb->transfer_buffer;
648
649         serial = urb->context;
650
651         /* Moschip get 5 bytes
652          * Byte 1 IIR Port 1 (port.number is 0)
653          * Byte 2 IIR Port 2 (port.number is 1)
654          * Byte 3 IIR Port 3 (port.number is 2)
655          * Byte 4 IIR Port 4 (port.number is 3)
656          * Byte 5 FIFO status for both */
657
658         if (length && length > 5) {
659                 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
660                 return;
661         }
662
663         sp[0] = (__u8) data[0];
664         sp[1] = (__u8) data[1];
665         sp[2] = (__u8) data[2];
666         sp[3] = (__u8) data[3];
667         st = (__u8) data[4];
668
669         for (i = 0; i < serial->num_ports; i++) {
670                 mos7840_port = mos7840_get_port_private(serial->port[i]);
671                 wval =
672                     (((__u16) serial->port[i]->number -
673                       (__u16) (serial->minor)) + 1) << 8;
674                 if (mos7840_port->open) {
675                         if (sp[i] & 0x01) {
676                                 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
677                         } else {
678                                 switch (sp[i] & 0x0f) {
679                                 case SERIAL_IIR_RLS:
680                                         dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
681                                         dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
682                                         mos7840_port->MsrLsr = 1;
683                                         wreg = LINE_STATUS_REGISTER;
684                                         break;
685                                 case SERIAL_IIR_MS:
686                                         dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
687                                         mos7840_port->MsrLsr = 0;
688                                         wreg = MODEM_STATUS_REGISTER;
689                                         break;
690                                 }
691                                 spin_lock(&mos7840_port->pool_lock);
692                                 if (!mos7840_port->zombie) {
693                                         rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
694                                 } else {
695                                         spin_unlock(&mos7840_port->pool_lock);
696                                         return;
697                                 }
698                                 spin_unlock(&mos7840_port->pool_lock);
699                         }
700                 }
701         }
702         if (!(rv < 0))
703                 /* the completion handler for the control urb will resubmit */
704                 return;
705 exit:
706         result = usb_submit_urb(urb, GFP_ATOMIC);
707         if (result) {
708                 dev_err(&urb->dev->dev,
709                         "%s - Error %d submitting interrupt urb\n",
710                         __func__, result);
711         }
712 }
713
714 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
715                                        const char *function)
716 {
717         if (!port) {
718                 pr_debug("%s - port == NULL\n", function);
719                 return -1;
720         }
721         if (!port->serial) {
722                 pr_debug("%s - port->serial == NULL\n", function);
723                 return -1;
724         }
725
726         return 0;
727 }
728
729 /* Inline functions to check the sanity of a pointer that is passed to us */
730 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
731                                          const char *function)
732 {
733         if (!serial) {
734                 pr_debug("%s - serial == NULL\n", function);
735                 return -1;
736         }
737         if (!serial->type) {
738                 pr_debug("%s - serial->type == NULL!\n", function);
739                 return -1;
740         }
741
742         return 0;
743 }
744
745 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
746                                                  const char *function)
747 {
748         /* if no port was specified, or it fails a paranoia check */
749         if (!port ||
750             mos7840_port_paranoia_check(port, function) ||
751             mos7840_serial_paranoia_check(port->serial, function)) {
752                 /* then say that we don't have a valid usb_serial thing,
753                  * which will end up genrating -ENODEV return values */
754                 return NULL;
755         }
756
757         return port->serial;
758 }
759
760 /*****************************************************************************
761  * mos7840_bulk_in_callback
762  *      this is the callback function for when we have received data on the
763  *      bulk in endpoint.
764  *****************************************************************************/
765
766 static void mos7840_bulk_in_callback(struct urb *urb)
767 {
768         int retval;
769         unsigned char *data;
770         struct usb_serial *serial;
771         struct usb_serial_port *port;
772         struct moschip_port *mos7840_port;
773         struct tty_struct *tty;
774         int status = urb->status;
775
776         mos7840_port = urb->context;
777         if (!mos7840_port)
778                 return;
779
780         if (status) {
781                 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
782                 mos7840_port->read_urb_busy = false;
783                 return;
784         }
785
786         port = mos7840_port->port;
787         if (mos7840_port_paranoia_check(port, __func__)) {
788                 mos7840_port->read_urb_busy = false;
789                 return;
790         }
791
792         serial = mos7840_get_usb_serial(port, __func__);
793         if (!serial) {
794                 mos7840_port->read_urb_busy = false;
795                 return;
796         }
797
798         data = urb->transfer_buffer;
799         usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
800
801         if (urb->actual_length) {
802                 tty = tty_port_tty_get(&mos7840_port->port->port);
803                 if (tty) {
804                         tty_insert_flip_string(tty, data, urb->actual_length);
805                         tty_flip_buffer_push(tty);
806                         tty_kref_put(tty);
807                 }
808                 mos7840_port->icount.rx += urb->actual_length;
809                 smp_wmb();
810                 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
811         }
812
813         if (!mos7840_port->read_urb) {
814                 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
815                 mos7840_port->read_urb_busy = false;
816                 return;
817         }
818
819         /* Turn on LED */
820         if (mos7840_port->has_led && !mos7840_port->led_flag) {
821                 mos7840_port->led_flag = true;
822                 mos7840_set_led_async(mos7840_port, 0x0301,
823                                         MODEM_CONTROL_REGISTER);
824                 mod_timer(&mos7840_port->led_timer1,
825                                 jiffies + msecs_to_jiffies(LED_ON_MS));
826         }
827
828         mos7840_port->read_urb_busy = true;
829         retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
830
831         if (retval) {
832                 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
833                 mos7840_port->read_urb_busy = false;
834         }
835 }
836
837 /*****************************************************************************
838  * mos7840_bulk_out_data_callback
839  *      this is the callback function for when we have finished sending
840  *      serial data on the bulk out endpoint.
841  *****************************************************************************/
842
843 static void mos7840_bulk_out_data_callback(struct urb *urb)
844 {
845         struct moschip_port *mos7840_port;
846         struct usb_serial_port *port;
847         struct tty_struct *tty;
848         int status = urb->status;
849         int i;
850
851         mos7840_port = urb->context;
852         port = mos7840_port->port;
853         spin_lock(&mos7840_port->pool_lock);
854         for (i = 0; i < NUM_URBS; i++) {
855                 if (urb == mos7840_port->write_urb_pool[i]) {
856                         mos7840_port->busy[i] = 0;
857                         break;
858                 }
859         }
860         spin_unlock(&mos7840_port->pool_lock);
861
862         if (status) {
863                 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
864                 return;
865         }
866
867         if (mos7840_port_paranoia_check(port, __func__))
868                 return;
869
870         tty = tty_port_tty_get(&port->port);
871         if (tty && mos7840_port->open)
872                 tty_wakeup(tty);
873         tty_kref_put(tty);
874
875 }
876
877 /************************************************************************/
878 /*       D R I V E R  T T Y  I N T E R F A C E  F U N C T I O N S       */
879 /************************************************************************/
880 #ifdef MCSSerialProbe
881 static int mos7840_serial_probe(struct usb_serial *serial,
882                                 const struct usb_device_id *id)
883 {
884
885         /*need to implement the mode_reg reading and updating\
886            structures usb_serial_ device_type\
887            (i.e num_ports, num_bulkin,bulkout etc) */
888         /* Also we can update the changes  attach */
889         return 1;
890 }
891 #endif
892
893 /*****************************************************************************
894  * mos7840_open
895  *      this function is called by the tty driver when a port is opened
896  *      If successful, we return 0
897  *      Otherwise we return a negative error number.
898  *****************************************************************************/
899
900 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
901 {
902         int response;
903         int j;
904         struct usb_serial *serial;
905         struct urb *urb;
906         __u16 Data;
907         int status;
908         struct moschip_port *mos7840_port;
909         struct moschip_port *port0;
910
911         if (mos7840_port_paranoia_check(port, __func__))
912                 return -ENODEV;
913
914         serial = port->serial;
915
916         if (mos7840_serial_paranoia_check(serial, __func__))
917                 return -ENODEV;
918
919         mos7840_port = mos7840_get_port_private(port);
920         port0 = mos7840_get_port_private(serial->port[0]);
921
922         if (mos7840_port == NULL || port0 == NULL)
923                 return -ENODEV;
924
925         usb_clear_halt(serial->dev, port->write_urb->pipe);
926         usb_clear_halt(serial->dev, port->read_urb->pipe);
927         port0->open_ports++;
928
929         /* Initialising the write urb pool */
930         for (j = 0; j < NUM_URBS; ++j) {
931                 urb = usb_alloc_urb(0, GFP_KERNEL);
932                 mos7840_port->write_urb_pool[j] = urb;
933
934                 if (urb == NULL) {
935                         dev_err(&port->dev, "No more urbs???\n");
936                         continue;
937                 }
938
939                 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
940                                                                 GFP_KERNEL);
941                 if (!urb->transfer_buffer) {
942                         usb_free_urb(urb);
943                         mos7840_port->write_urb_pool[j] = NULL;
944                         dev_err(&port->dev,
945                                 "%s-out of memory for urb buffers.\n",
946                                 __func__);
947                         continue;
948                 }
949         }
950
951 /*****************************************************************************
952  * Initialize MCS7840 -- Write Init values to corresponding Registers
953  *
954  * Register Index
955  * 1 : IER
956  * 2 : FCR
957  * 3 : LCR
958  * 4 : MCR
959  *
960  * 0x08 : SP1/2 Control Reg
961  *****************************************************************************/
962
963         /* NEED to check the following Block */
964
965         Data = 0x0;
966         status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
967         if (status < 0) {
968                 dev_dbg(&port->dev, "Reading Spreg failed\n");
969                 return -1;
970         }
971         Data |= 0x80;
972         status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
973         if (status < 0) {
974                 dev_dbg(&port->dev, "writing Spreg failed\n");
975                 return -1;
976         }
977
978         Data &= ~0x80;
979         status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
980         if (status < 0) {
981                 dev_dbg(&port->dev, "writing Spreg failed\n");
982                 return -1;
983         }
984         /* End of block to be checked */
985
986         Data = 0x0;
987         status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
988                                                                         &Data);
989         if (status < 0) {
990                 dev_dbg(&port->dev, "Reading Controlreg failed\n");
991                 return -1;
992         }
993         Data |= 0x08;           /* Driver done bit */
994         Data |= 0x20;           /* rx_disable */
995         status = mos7840_set_reg_sync(port,
996                                 mos7840_port->ControlRegOffset, Data);
997         if (status < 0) {
998                 dev_dbg(&port->dev, "writing Controlreg failed\n");
999                 return -1;
1000         }
1001         /* do register settings here */
1002         /* Set all regs to the device default values. */
1003         /***********************************
1004          * First Disable all interrupts.
1005          ***********************************/
1006         Data = 0x00;
1007         status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1008         if (status < 0) {
1009                 dev_dbg(&port->dev, "disabling interrupts failed\n");
1010                 return -1;
1011         }
1012         /* Set FIFO_CONTROL_REGISTER to the default value */
1013         Data = 0x00;
1014         status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1015         if (status < 0) {
1016                 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
1017                 return -1;
1018         }
1019
1020         Data = 0xcf;
1021         status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1022         if (status < 0) {
1023                 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
1024                 return -1;
1025         }
1026
1027         Data = 0x03;
1028         status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1029         mos7840_port->shadowLCR = Data;
1030
1031         Data = 0x0b;
1032         status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1033         mos7840_port->shadowMCR = Data;
1034
1035         Data = 0x00;
1036         status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1037         mos7840_port->shadowLCR = Data;
1038
1039         Data |= SERIAL_LCR_DLAB;        /* data latch enable in LCR 0x80 */
1040         status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1041
1042         Data = 0x0c;
1043         status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1044
1045         Data = 0x0;
1046         status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1047
1048         Data = 0x00;
1049         status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1050
1051         Data = Data & ~SERIAL_LCR_DLAB;
1052         status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1053         mos7840_port->shadowLCR = Data;
1054
1055         /* clearing Bulkin and Bulkout Fifo */
1056         Data = 0x0;
1057         status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1058
1059         Data = Data | 0x0c;
1060         status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1061
1062         Data = Data & ~0x0c;
1063         status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1064         /* Finally enable all interrupts */
1065         Data = 0x0c;
1066         status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1067
1068         /* clearing rx_disable */
1069         Data = 0x0;
1070         status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1071                                                                         &Data);
1072         Data = Data & ~0x20;
1073         status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1074                                                                         Data);
1075
1076         /* rx_negate */
1077         Data = 0x0;
1078         status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1079                                                                         &Data);
1080         Data = Data | 0x10;
1081         status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1082                                                                         Data);
1083
1084         /* Check to see if we've set up our endpoint info yet    *
1085          * (can't set it up in mos7840_startup as the structures *
1086          * were not set up at that time.)                        */
1087         if (port0->open_ports == 1) {
1088                 if (serial->port[0]->interrupt_in_buffer == NULL) {
1089                         /* set up interrupt urb */
1090                         usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1091                                 serial->dev,
1092                                 usb_rcvintpipe(serial->dev,
1093                                 serial->port[0]->interrupt_in_endpointAddress),
1094                                 serial->port[0]->interrupt_in_buffer,
1095                                 serial->port[0]->interrupt_in_urb->
1096                                 transfer_buffer_length,
1097                                 mos7840_interrupt_callback,
1098                                 serial,
1099                                 serial->port[0]->interrupt_in_urb->interval);
1100
1101                         /* start interrupt read for mos7840               *
1102                          * will continue as long as mos7840 is connected  */
1103
1104                         response =
1105                             usb_submit_urb(serial->port[0]->interrupt_in_urb,
1106                                            GFP_KERNEL);
1107                         if (response) {
1108                                 dev_err(&port->dev, "%s - Error %d submitting "
1109                                         "interrupt urb\n", __func__, response);
1110                         }
1111
1112                 }
1113
1114         }
1115
1116         /* see if we've set up our endpoint info yet   *
1117          * (can't set it up in mos7840_startup as the  *
1118          * structures were not set up at that time.)   */
1119
1120         dev_dbg(&port->dev, "port number is %d\n", port->number);
1121         dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1122         dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1123         dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1124         dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1125         dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1126         mos7840_port->read_urb = port->read_urb;
1127
1128         /* set up our bulk in urb */
1129         if ((serial->num_ports == 2)
1130                 && ((((__u16)port->number -
1131                         (__u16)(port->serial->minor)) % 2) != 0)) {
1132                 usb_fill_bulk_urb(mos7840_port->read_urb,
1133                         serial->dev,
1134                         usb_rcvbulkpipe(serial->dev,
1135                                 (port->bulk_in_endpointAddress) + 2),
1136                         port->bulk_in_buffer,
1137                         mos7840_port->read_urb->transfer_buffer_length,
1138                         mos7840_bulk_in_callback, mos7840_port);
1139         } else {
1140                 usb_fill_bulk_urb(mos7840_port->read_urb,
1141                         serial->dev,
1142                         usb_rcvbulkpipe(serial->dev,
1143                                 port->bulk_in_endpointAddress),
1144                         port->bulk_in_buffer,
1145                         mos7840_port->read_urb->transfer_buffer_length,
1146                         mos7840_bulk_in_callback, mos7840_port);
1147         }
1148
1149         dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1150         mos7840_port->read_urb_busy = true;
1151         response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1152         if (response) {
1153                 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1154                         __func__, response);
1155                 mos7840_port->read_urb_busy = false;
1156         }
1157
1158         /* initialize our wait queues */
1159         init_waitqueue_head(&mos7840_port->wait_chase);
1160         init_waitqueue_head(&mos7840_port->delta_msr_wait);
1161
1162         /* initialize our icount structure */
1163         memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1164
1165         /* initialize our port settings */
1166         /* Must set to enable ints! */
1167         mos7840_port->shadowMCR = MCR_MASTER_IE;
1168         /* send a open port command */
1169         mos7840_port->open = 1;
1170         /* mos7840_change_port_settings(mos7840_port,old_termios); */
1171         mos7840_port->icount.tx = 0;
1172         mos7840_port->icount.rx = 0;
1173
1174         return 0;
1175 }
1176
1177 /*****************************************************************************
1178  * mos7840_chars_in_buffer
1179  *      this function is called by the tty driver when it wants to know how many
1180  *      bytes of data we currently have outstanding in the port (data that has
1181  *      been written, but hasn't made it out the port yet)
1182  *      If successful, we return the number of bytes left to be written in the
1183  *      system,
1184  *      Otherwise we return zero.
1185  *****************************************************************************/
1186
1187 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1188 {
1189         struct usb_serial_port *port = tty->driver_data;
1190         int i;
1191         int chars = 0;
1192         unsigned long flags;
1193         struct moschip_port *mos7840_port;
1194
1195         if (mos7840_port_paranoia_check(port, __func__))
1196                 return 0;
1197
1198         mos7840_port = mos7840_get_port_private(port);
1199         if (mos7840_port == NULL)
1200                 return 0;
1201
1202         spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1203         for (i = 0; i < NUM_URBS; ++i) {
1204                 if (mos7840_port->busy[i]) {
1205                         struct urb *urb = mos7840_port->write_urb_pool[i];
1206                         chars += urb->transfer_buffer_length;
1207                 }
1208         }
1209         spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1210         dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1211         return chars;
1212
1213 }
1214
1215 /*****************************************************************************
1216  * mos7840_close
1217  *      this function is called by the tty driver when a port is closed
1218  *****************************************************************************/
1219
1220 static void mos7840_close(struct usb_serial_port *port)
1221 {
1222         struct usb_serial *serial;
1223         struct moschip_port *mos7840_port;
1224         struct moschip_port *port0;
1225         int j;
1226         __u16 Data;
1227
1228         if (mos7840_port_paranoia_check(port, __func__))
1229                 return;
1230
1231         serial = mos7840_get_usb_serial(port, __func__);
1232         if (!serial)
1233                 return;
1234
1235         mos7840_port = mos7840_get_port_private(port);
1236         port0 = mos7840_get_port_private(serial->port[0]);
1237
1238         if (mos7840_port == NULL || port0 == NULL)
1239                 return;
1240
1241         for (j = 0; j < NUM_URBS; ++j)
1242                 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1243
1244         /* Freeing Write URBs */
1245         for (j = 0; j < NUM_URBS; ++j) {
1246                 if (mos7840_port->write_urb_pool[j]) {
1247                         if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1248                                 kfree(mos7840_port->write_urb_pool[j]->
1249                                       transfer_buffer);
1250
1251                         usb_free_urb(mos7840_port->write_urb_pool[j]);
1252                 }
1253         }
1254
1255         /* While closing port, shutdown all bulk read, write  *
1256          * and interrupt read if they exists                  */
1257         if (serial->dev) {
1258                 if (mos7840_port->write_urb) {
1259                         dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1260                         usb_kill_urb(mos7840_port->write_urb);
1261                 }
1262                 if (mos7840_port->read_urb) {
1263                         dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1264                         usb_kill_urb(mos7840_port->read_urb);
1265                         mos7840_port->read_urb_busy = false;
1266                 }
1267                 if ((&mos7840_port->control_urb)) {
1268                         dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1269                         /*/      usb_kill_urb (mos7840_port->control_urb); */
1270                 }
1271         }
1272 /*      if(mos7840_port->ctrl_buf != NULL) */
1273 /*              kfree(mos7840_port->ctrl_buf); */
1274         port0->open_ports--;
1275         dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1276         if (port0->open_ports == 0) {
1277                 if (serial->port[0]->interrupt_in_urb) {
1278                         dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1279                         usb_kill_urb(serial->port[0]->interrupt_in_urb);
1280                 }
1281         }
1282
1283         if (mos7840_port->write_urb) {
1284                 /* if this urb had a transfer buffer already (old tx) free it */
1285                 if (mos7840_port->write_urb->transfer_buffer != NULL)
1286                         kfree(mos7840_port->write_urb->transfer_buffer);
1287                 usb_free_urb(mos7840_port->write_urb);
1288         }
1289
1290         Data = 0x0;
1291         mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1292
1293         Data = 0x00;
1294         mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1295
1296         mos7840_port->open = 0;
1297 }
1298
1299 /************************************************************************
1300  *
1301  * mos7840_block_until_chase_response
1302  *
1303  *      This function will block the close until one of the following:
1304  *              1. Response to our Chase comes from mos7840
1305  *              2. A timeout of 10 seconds without activity has expired
1306  *                 (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1307  *
1308  ************************************************************************/
1309
1310 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1311                                         struct moschip_port *mos7840_port)
1312 {
1313         int timeout = msecs_to_jiffies(1000);
1314         int wait = 10;
1315         int count;
1316
1317         while (1) {
1318                 count = mos7840_chars_in_buffer(tty);
1319
1320                 /* Check for Buffer status */
1321                 if (count <= 0)
1322                         return;
1323
1324                 /* Block the thread for a while */
1325                 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1326                                                timeout);
1327                 /* No activity.. count down section */
1328                 wait--;
1329                 if (wait == 0) {
1330                         dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1331                         return;
1332                 } else {
1333                         /* Reset timeout value back to seconds */
1334                         wait = 10;
1335                 }
1336         }
1337
1338 }
1339
1340 /*****************************************************************************
1341  * mos7840_break
1342  *      this function sends a break to the port
1343  *****************************************************************************/
1344 static void mos7840_break(struct tty_struct *tty, int break_state)
1345 {
1346         struct usb_serial_port *port = tty->driver_data;
1347         unsigned char data;
1348         struct usb_serial *serial;
1349         struct moschip_port *mos7840_port;
1350
1351         if (mos7840_port_paranoia_check(port, __func__))
1352                 return;
1353
1354         serial = mos7840_get_usb_serial(port, __func__);
1355         if (!serial)
1356                 return;
1357
1358         mos7840_port = mos7840_get_port_private(port);
1359
1360         if (mos7840_port == NULL)
1361                 return;
1362
1363         if (serial->dev)
1364                 /* flush and block until tx is empty */
1365                 mos7840_block_until_chase_response(tty, mos7840_port);
1366
1367         if (break_state == -1)
1368                 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1369         else
1370                 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1371
1372         /* FIXME: no locking on shadowLCR anywhere in driver */
1373         mos7840_port->shadowLCR = data;
1374         dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1375         mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1376                              mos7840_port->shadowLCR);
1377 }
1378
1379 /*****************************************************************************
1380  * mos7840_write_room
1381  *      this function is called by the tty driver when it wants to know how many
1382  *      bytes of data we can accept for a specific port.
1383  *      If successful, we return the amount of room that we have for this port
1384  *      Otherwise we return a negative error number.
1385  *****************************************************************************/
1386
1387 static int mos7840_write_room(struct tty_struct *tty)
1388 {
1389         struct usb_serial_port *port = tty->driver_data;
1390         int i;
1391         int room = 0;
1392         unsigned long flags;
1393         struct moschip_port *mos7840_port;
1394
1395         if (mos7840_port_paranoia_check(port, __func__))
1396                 return -1;
1397
1398         mos7840_port = mos7840_get_port_private(port);
1399         if (mos7840_port == NULL)
1400                 return -1;
1401
1402         spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1403         for (i = 0; i < NUM_URBS; ++i) {
1404                 if (!mos7840_port->busy[i])
1405                         room += URB_TRANSFER_BUFFER_SIZE;
1406         }
1407         spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1408
1409         room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1410         dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1411         return room;
1412
1413 }
1414
1415 /*****************************************************************************
1416  * mos7840_write
1417  *      this function is called by the tty driver when data should be written to
1418  *      the port.
1419  *      If successful, we return the number of bytes written, otherwise we
1420  *      return a negative error number.
1421  *****************************************************************************/
1422
1423 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1424                          const unsigned char *data, int count)
1425 {
1426         int status;
1427         int i;
1428         int bytes_sent = 0;
1429         int transfer_size;
1430         unsigned long flags;
1431
1432         struct moschip_port *mos7840_port;
1433         struct usb_serial *serial;
1434         struct urb *urb;
1435         /* __u16 Data; */
1436         const unsigned char *current_position = data;
1437         unsigned char *data1;
1438
1439 #ifdef NOTMOS7840
1440         Data = 0x00;
1441         status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1442         mos7840_port->shadowLCR = Data;
1443         dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1444         dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1445
1446         /* Data = 0x03; */
1447         /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1448         /* mos7840_port->shadowLCR=Data;//Need to add later */
1449
1450         Data |= SERIAL_LCR_DLAB;        /* data latch enable in LCR 0x80 */
1451         status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1452
1453         /* Data = 0x0c; */
1454         /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1455         Data = 0x00;
1456         status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1457         dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1458
1459         Data = 0x0;
1460         status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1461         dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1462
1463         Data = Data & ~SERIAL_LCR_DLAB;
1464         dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1465         status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1466 #endif
1467
1468         if (mos7840_port_paranoia_check(port, __func__))
1469                 return -1;
1470
1471         serial = port->serial;
1472         if (mos7840_serial_paranoia_check(serial, __func__))
1473                 return -1;
1474
1475         mos7840_port = mos7840_get_port_private(port);
1476         if (mos7840_port == NULL)
1477                 return -1;
1478
1479         /* try to find a free urb in the list */
1480         urb = NULL;
1481
1482         spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1483         for (i = 0; i < NUM_URBS; ++i) {
1484                 if (!mos7840_port->busy[i]) {
1485                         mos7840_port->busy[i] = 1;
1486                         urb = mos7840_port->write_urb_pool[i];
1487                         dev_dbg(&port->dev, "URB:%d\n", i);
1488                         break;
1489                 }
1490         }
1491         spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1492
1493         if (urb == NULL) {
1494                 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1495                 goto exit;
1496         }
1497
1498         if (urb->transfer_buffer == NULL) {
1499                 urb->transfer_buffer =
1500                     kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1501
1502                 if (urb->transfer_buffer == NULL) {
1503                         dev_err_console(port, "%s no more kernel memory...\n",
1504                                 __func__);
1505                         goto exit;
1506                 }
1507         }
1508         transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1509
1510         memcpy(urb->transfer_buffer, current_position, transfer_size);
1511
1512         /* fill urb with data and submit  */
1513         if ((serial->num_ports == 2)
1514                 && ((((__u16)port->number -
1515                         (__u16)(port->serial->minor)) % 2) != 0)) {
1516                 usb_fill_bulk_urb(urb,
1517                         serial->dev,
1518                         usb_sndbulkpipe(serial->dev,
1519                                 (port->bulk_out_endpointAddress) + 2),
1520                         urb->transfer_buffer,
1521                         transfer_size,
1522                         mos7840_bulk_out_data_callback, mos7840_port);
1523         } else {
1524                 usb_fill_bulk_urb(urb,
1525                         serial->dev,
1526                         usb_sndbulkpipe(serial->dev,
1527                                 port->bulk_out_endpointAddress),
1528                         urb->transfer_buffer,
1529                         transfer_size,
1530                         mos7840_bulk_out_data_callback, mos7840_port);
1531         }
1532
1533         data1 = urb->transfer_buffer;
1534         dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1535
1536         /* Turn on LED */
1537         if (mos7840_port->has_led && !mos7840_port->led_flag) {
1538                 mos7840_port->led_flag = true;
1539                 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1540                 mod_timer(&mos7840_port->led_timer1,
1541                                 jiffies + msecs_to_jiffies(LED_ON_MS));
1542         }
1543
1544         /* send it down the pipe */
1545         status = usb_submit_urb(urb, GFP_ATOMIC);
1546
1547         if (status) {
1548                 mos7840_port->busy[i] = 0;
1549                 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1550                         "with status = %d\n", __func__, status);
1551                 bytes_sent = status;
1552                 goto exit;
1553         }
1554         bytes_sent = transfer_size;
1555         mos7840_port->icount.tx += transfer_size;
1556         smp_wmb();
1557         dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1558 exit:
1559         return bytes_sent;
1560
1561 }
1562
1563 /*****************************************************************************
1564  * mos7840_throttle
1565  *      this function is called by the tty driver when it wants to stop the data
1566  *      being read from the port.
1567  *****************************************************************************/
1568
1569 static void mos7840_throttle(struct tty_struct *tty)
1570 {
1571         struct usb_serial_port *port = tty->driver_data;
1572         struct moschip_port *mos7840_port;
1573         int status;
1574
1575         if (mos7840_port_paranoia_check(port, __func__))
1576                 return;
1577
1578         mos7840_port = mos7840_get_port_private(port);
1579
1580         if (mos7840_port == NULL)
1581                 return;
1582
1583         if (!mos7840_port->open) {
1584                 dev_dbg(&port->dev, "%s", "port not opened\n");
1585                 return;
1586         }
1587
1588         /* if we are implementing XON/XOFF, send the stop character */
1589         if (I_IXOFF(tty)) {
1590                 unsigned char stop_char = STOP_CHAR(tty);
1591                 status = mos7840_write(tty, port, &stop_char, 1);
1592                 if (status <= 0)
1593                         return;
1594         }
1595         /* if we are implementing RTS/CTS, toggle that line */
1596         if (tty->termios->c_cflag & CRTSCTS) {
1597                 mos7840_port->shadowMCR &= ~MCR_RTS;
1598                 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1599                                          mos7840_port->shadowMCR);
1600                 if (status < 0)
1601                         return;
1602         }
1603 }
1604
1605 /*****************************************************************************
1606  * mos7840_unthrottle
1607  *      this function is called by the tty driver when it wants to resume
1608  *      the data being read from the port (called after mos7840_throttle is
1609  *      called)
1610  *****************************************************************************/
1611 static void mos7840_unthrottle(struct tty_struct *tty)
1612 {
1613         struct usb_serial_port *port = tty->driver_data;
1614         int status;
1615         struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1616
1617         if (mos7840_port_paranoia_check(port, __func__))
1618                 return;
1619
1620         if (mos7840_port == NULL)
1621                 return;
1622
1623         if (!mos7840_port->open) {
1624                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1625                 return;
1626         }
1627
1628         /* if we are implementing XON/XOFF, send the start character */
1629         if (I_IXOFF(tty)) {
1630                 unsigned char start_char = START_CHAR(tty);
1631                 status = mos7840_write(tty, port, &start_char, 1);
1632                 if (status <= 0)
1633                         return;
1634         }
1635
1636         /* if we are implementing RTS/CTS, toggle that line */
1637         if (tty->termios->c_cflag & CRTSCTS) {
1638                 mos7840_port->shadowMCR |= MCR_RTS;
1639                 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1640                                          mos7840_port->shadowMCR);
1641                 if (status < 0)
1642                         return;
1643         }
1644 }
1645
1646 static int mos7840_tiocmget(struct tty_struct *tty)
1647 {
1648         struct usb_serial_port *port = tty->driver_data;
1649         struct moschip_port *mos7840_port;
1650         unsigned int result;
1651         __u16 msr;
1652         __u16 mcr;
1653         int status;
1654         mos7840_port = mos7840_get_port_private(port);
1655
1656         if (mos7840_port == NULL)
1657                 return -ENODEV;
1658
1659         status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1660         status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1661         result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1662             | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1663             | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1664             | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1665             | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1666             | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1667             | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1668
1669         dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1670
1671         return result;
1672 }
1673
1674 static int mos7840_tiocmset(struct tty_struct *tty,
1675                             unsigned int set, unsigned int clear)
1676 {
1677         struct usb_serial_port *port = tty->driver_data;
1678         struct moschip_port *mos7840_port;
1679         unsigned int mcr;
1680         int status;
1681
1682         mos7840_port = mos7840_get_port_private(port);
1683
1684         if (mos7840_port == NULL)
1685                 return -ENODEV;
1686
1687         /* FIXME: What locks the port registers ? */
1688         mcr = mos7840_port->shadowMCR;
1689         if (clear & TIOCM_RTS)
1690                 mcr &= ~MCR_RTS;
1691         if (clear & TIOCM_DTR)
1692                 mcr &= ~MCR_DTR;
1693         if (clear & TIOCM_LOOP)
1694                 mcr &= ~MCR_LOOPBACK;
1695
1696         if (set & TIOCM_RTS)
1697                 mcr |= MCR_RTS;
1698         if (set & TIOCM_DTR)
1699                 mcr |= MCR_DTR;
1700         if (set & TIOCM_LOOP)
1701                 mcr |= MCR_LOOPBACK;
1702
1703         mos7840_port->shadowMCR = mcr;
1704
1705         status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1706         if (status < 0) {
1707                 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1708                 return status;
1709         }
1710
1711         return 0;
1712 }
1713
1714 /*****************************************************************************
1715  * mos7840_calc_baud_rate_divisor
1716  *      this function calculates the proper baud rate divisor for the specified
1717  *      baud rate.
1718  *****************************************************************************/
1719 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1720                                           int baudRate, int *divisor,
1721                                           __u16 *clk_sel_val)
1722 {
1723         dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1724
1725         if (baudRate <= 115200) {
1726                 *divisor = 115200 / baudRate;
1727                 *clk_sel_val = 0x0;
1728         }
1729         if ((baudRate > 115200) && (baudRate <= 230400)) {
1730                 *divisor = 230400 / baudRate;
1731                 *clk_sel_val = 0x10;
1732         } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1733                 *divisor = 403200 / baudRate;
1734                 *clk_sel_val = 0x20;
1735         } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1736                 *divisor = 460800 / baudRate;
1737                 *clk_sel_val = 0x30;
1738         } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1739                 *divisor = 806400 / baudRate;
1740                 *clk_sel_val = 0x40;
1741         } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1742                 *divisor = 921600 / baudRate;
1743                 *clk_sel_val = 0x50;
1744         } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1745                 *divisor = 1572864 / baudRate;
1746                 *clk_sel_val = 0x60;
1747         } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1748                 *divisor = 3145728 / baudRate;
1749                 *clk_sel_val = 0x70;
1750         }
1751         return 0;
1752
1753 #ifdef NOTMCS7840
1754
1755         for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1756                 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1757                         *divisor = mos7840_divisor_table[i].Divisor;
1758                         return 0;
1759                 }
1760         }
1761
1762         /* After trying for all the standard baud rates    *
1763          * Try calculating the divisor for this baud rate  */
1764
1765         if (baudrate > 75 && baudrate < 230400) {
1766                 /* get the divisor */
1767                 custom = (__u16) (230400L / baudrate);
1768
1769                 /* Check for round off */
1770                 round1 = (__u16) (2304000L / baudrate);
1771                 round = (__u16) (round1 - (custom * 10));
1772                 if (round > 4)
1773                         custom++;
1774                 *divisor = custom;
1775
1776                 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1777                 return 0;
1778         }
1779
1780         dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1781         return -1;
1782 #endif
1783 }
1784
1785 /*****************************************************************************
1786  * mos7840_send_cmd_write_baud_rate
1787  *      this function sends the proper command to change the baud rate of the
1788  *      specified port.
1789  *****************************************************************************/
1790
1791 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1792                                             int baudRate)
1793 {
1794         int divisor = 0;
1795         int status;
1796         __u16 Data;
1797         unsigned char number;
1798         __u16 clk_sel_val;
1799         struct usb_serial_port *port;
1800
1801         if (mos7840_port == NULL)
1802                 return -1;
1803
1804         port = mos7840_port->port;
1805         if (mos7840_port_paranoia_check(port, __func__))
1806                 return -1;
1807
1808         if (mos7840_serial_paranoia_check(port->serial, __func__))
1809                 return -1;
1810
1811         number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1812
1813         dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1814                 mos7840_port->port->number, baudRate);
1815         /* reset clk_uart_sel in spregOffset */
1816         if (baudRate > 115200) {
1817 #ifdef HW_flow_control
1818                 /* NOTE: need to see the pther register to modify */
1819                 /* setting h/w flow control bit to 1 */
1820                 Data = 0x2b;
1821                 mos7840_port->shadowMCR = Data;
1822                 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1823                                                                         Data);
1824                 if (status < 0) {
1825                         dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1826                         return -1;
1827                 }
1828 #endif
1829
1830         } else {
1831 #ifdef HW_flow_control
1832                 /* setting h/w flow control bit to 0 */
1833                 Data = 0xb;
1834                 mos7840_port->shadowMCR = Data;
1835                 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1836                                                                         Data);
1837                 if (status < 0) {
1838                         dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1839                         return -1;
1840                 }
1841 #endif
1842
1843         }
1844
1845         if (1) {                /* baudRate <= 115200) */
1846                 clk_sel_val = 0x0;
1847                 Data = 0x0;
1848                 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1849                                                    &clk_sel_val);
1850                 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1851                                                                  &Data);
1852                 if (status < 0) {
1853                         dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1854                         return -1;
1855                 }
1856                 Data = (Data & 0x8f) | clk_sel_val;
1857                 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1858                                                                 Data);
1859                 if (status < 0) {
1860                         dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1861                         return -1;
1862                 }
1863                 /* Calculate the Divisor */
1864
1865                 if (status) {
1866                         dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1867                         return status;
1868                 }
1869                 /* Enable access to divisor latch */
1870                 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1871                 mos7840_port->shadowLCR = Data;
1872                 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1873
1874                 /* Write the divisor */
1875                 Data = (unsigned char)(divisor & 0xff);
1876                 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1877                 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1878
1879                 Data = (unsigned char)((divisor & 0xff00) >> 8);
1880                 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1881                 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1882
1883                 /* Disable access to divisor latch */
1884                 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1885                 mos7840_port->shadowLCR = Data;
1886                 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1887
1888         }
1889         return status;
1890 }
1891
1892 /*****************************************************************************
1893  * mos7840_change_port_settings
1894  *      This routine is called to set the UART on the device to match
1895  *      the specified new settings.
1896  *****************************************************************************/
1897
1898 static void mos7840_change_port_settings(struct tty_struct *tty,
1899         struct moschip_port *mos7840_port, struct ktermios *old_termios)
1900 {
1901         int baud;
1902         unsigned cflag;
1903         unsigned iflag;
1904         __u8 lData;
1905         __u8 lParity;
1906         __u8 lStop;
1907         int status;
1908         __u16 Data;
1909         struct usb_serial_port *port;
1910         struct usb_serial *serial;
1911
1912         if (mos7840_port == NULL)
1913                 return;
1914
1915         port = mos7840_port->port;
1916
1917         if (mos7840_port_paranoia_check(port, __func__))
1918                 return;
1919
1920         if (mos7840_serial_paranoia_check(port->serial, __func__))
1921                 return;
1922
1923         serial = port->serial;
1924
1925         if (!mos7840_port->open) {
1926                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1927                 return;
1928         }
1929
1930         lData = LCR_BITS_8;
1931         lStop = LCR_STOP_1;
1932         lParity = LCR_PAR_NONE;
1933
1934         cflag = tty->termios->c_cflag;
1935         iflag = tty->termios->c_iflag;
1936
1937         /* Change the number of bits */
1938         if (cflag & CSIZE) {
1939                 switch (cflag & CSIZE) {
1940                 case CS5:
1941                         lData = LCR_BITS_5;
1942                         break;
1943
1944                 case CS6:
1945                         lData = LCR_BITS_6;
1946                         break;
1947
1948                 case CS7:
1949                         lData = LCR_BITS_7;
1950                         break;
1951                 default:
1952                 case CS8:
1953                         lData = LCR_BITS_8;
1954                         break;
1955                 }
1956         }
1957         /* Change the Parity bit */
1958         if (cflag & PARENB) {
1959                 if (cflag & PARODD) {
1960                         lParity = LCR_PAR_ODD;
1961                         dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1962                 } else {
1963                         lParity = LCR_PAR_EVEN;
1964                         dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1965                 }
1966
1967         } else {
1968                 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1969         }
1970
1971         if (cflag & CMSPAR)
1972                 lParity = lParity | 0x20;
1973
1974         /* Change the Stop bit */
1975         if (cflag & CSTOPB) {
1976                 lStop = LCR_STOP_2;
1977                 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1978         } else {
1979                 lStop = LCR_STOP_1;
1980                 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1981         }
1982
1983         /* Update the LCR with the correct value */
1984         mos7840_port->shadowLCR &=
1985             ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1986         mos7840_port->shadowLCR |= (lData | lParity | lStop);
1987
1988         dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1989                 mos7840_port->shadowLCR);
1990         /* Disable Interrupts */
1991         Data = 0x00;
1992         mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1993
1994         Data = 0x00;
1995         mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1996
1997         Data = 0xcf;
1998         mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1999
2000         /* Send the updated LCR value to the mos7840 */
2001         Data = mos7840_port->shadowLCR;
2002
2003         mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2004
2005         Data = 0x00b;
2006         mos7840_port->shadowMCR = Data;
2007         mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2008         Data = 0x00b;
2009         mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2010
2011         /* set up the MCR register and send it to the mos7840 */
2012
2013         mos7840_port->shadowMCR = MCR_MASTER_IE;
2014         if (cflag & CBAUD)
2015                 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2016
2017         if (cflag & CRTSCTS)
2018                 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2019         else
2020                 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2021
2022         Data = mos7840_port->shadowMCR;
2023         mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2024
2025         /* Determine divisor based on baud rate */
2026         baud = tty_get_baud_rate(tty);
2027
2028         if (!baud) {
2029                 /* pick a default, any default... */
2030                 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2031                 baud = 9600;
2032         }
2033
2034         dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2035         status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2036
2037         /* Enable Interrupts */
2038         Data = 0x0c;
2039         mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2040
2041         if (mos7840_port->read_urb_busy == false) {
2042                 mos7840_port->read_urb_busy = true;
2043                 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2044                 if (status) {
2045                         dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2046                             status);
2047                         mos7840_port->read_urb_busy = false;
2048                 }
2049         }
2050         wake_up(&mos7840_port->delta_msr_wait);
2051         mos7840_port->delta_msr_cond = 1;
2052         dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2053                 mos7840_port->shadowLCR);
2054 }
2055
2056 /*****************************************************************************
2057  * mos7840_set_termios
2058  *      this function is called by the tty driver when it wants to change
2059  *      the termios structure
2060  *****************************************************************************/
2061
2062 static void mos7840_set_termios(struct tty_struct *tty,
2063                                 struct usb_serial_port *port,
2064                                 struct ktermios *old_termios)
2065 {
2066         int status;
2067         unsigned int cflag;
2068         struct usb_serial *serial;
2069         struct moschip_port *mos7840_port;
2070
2071         if (mos7840_port_paranoia_check(port, __func__))
2072                 return;
2073
2074         serial = port->serial;
2075
2076         if (mos7840_serial_paranoia_check(serial, __func__))
2077                 return;
2078
2079         mos7840_port = mos7840_get_port_private(port);
2080
2081         if (mos7840_port == NULL)
2082                 return;
2083
2084         if (!mos7840_port->open) {
2085                 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2086                 return;
2087         }
2088
2089         dev_dbg(&port->dev, "%s", "setting termios - \n");
2090
2091         cflag = tty->termios->c_cflag;
2092
2093         dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2094                 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2095         dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2096                 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2097         dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2098
2099         /* change the port settings to the new ones specified */
2100
2101         mos7840_change_port_settings(tty, mos7840_port, old_termios);
2102
2103         if (!mos7840_port->read_urb) {
2104                 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2105                 return;
2106         }
2107
2108         if (mos7840_port->read_urb_busy == false) {
2109                 mos7840_port->read_urb_busy = true;
2110                 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2111                 if (status) {
2112                         dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2113                             status);
2114                         mos7840_port->read_urb_busy = false;
2115                 }
2116         }
2117 }
2118
2119 /*****************************************************************************
2120  * mos7840_get_lsr_info - get line status register info
2121  *
2122  * Purpose: Let user call ioctl() to get info when the UART physically
2123  *          is emptied.  On bus types like RS485, the transmitter must
2124  *          release the bus after transmitting. This must be done when
2125  *          the transmit shift register is empty, not be done when the
2126  *          transmit holding register is empty.  This functionality
2127  *          allows an RS485 driver to be written in user space.
2128  *****************************************************************************/
2129
2130 static int mos7840_get_lsr_info(struct tty_struct *tty,
2131                                 unsigned int __user *value)
2132 {
2133         int count;
2134         unsigned int result = 0;
2135
2136         count = mos7840_chars_in_buffer(tty);
2137         if (count == 0)
2138                 result = TIOCSER_TEMT;
2139
2140         if (copy_to_user(value, &result, sizeof(int)))
2141                 return -EFAULT;
2142         return 0;
2143 }
2144
2145 /*****************************************************************************
2146  * mos7840_get_serial_info
2147  *      function to get information about serial port
2148  *****************************************************************************/
2149
2150 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2151                                    struct serial_struct __user *retinfo)
2152 {
2153         struct serial_struct tmp;
2154
2155         if (mos7840_port == NULL)
2156                 return -1;
2157
2158         if (!retinfo)
2159                 return -EFAULT;
2160
2161         memset(&tmp, 0, sizeof(tmp));
2162
2163         tmp.type = PORT_16550A;
2164         tmp.line = mos7840_port->port->serial->minor;
2165         tmp.port = mos7840_port->port->number;
2166         tmp.irq = 0;
2167         tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2168         tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2169         tmp.baud_base = 9600;
2170         tmp.close_delay = 5 * HZ;
2171         tmp.closing_wait = 30 * HZ;
2172
2173         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2174                 return -EFAULT;
2175         return 0;
2176 }
2177
2178 static int mos7840_get_icount(struct tty_struct *tty,
2179                         struct serial_icounter_struct *icount)
2180 {
2181         struct usb_serial_port *port = tty->driver_data;
2182         struct moschip_port *mos7840_port;
2183         struct async_icount cnow;
2184
2185         mos7840_port = mos7840_get_port_private(port);
2186         cnow = mos7840_port->icount;
2187
2188         smp_rmb();
2189         icount->cts = cnow.cts;
2190         icount->dsr = cnow.dsr;
2191         icount->rng = cnow.rng;
2192         icount->dcd = cnow.dcd;
2193         icount->rx = cnow.rx;
2194         icount->tx = cnow.tx;
2195         icount->frame = cnow.frame;
2196         icount->overrun = cnow.overrun;
2197         icount->parity = cnow.parity;
2198         icount->brk = cnow.brk;
2199         icount->buf_overrun = cnow.buf_overrun;
2200
2201         dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2202                 icount->rx, icount->tx);
2203         return 0;
2204 }
2205
2206 /*****************************************************************************
2207  * SerialIoctl
2208  *      this function handles any ioctl calls to the driver
2209  *****************************************************************************/
2210
2211 static int mos7840_ioctl(struct tty_struct *tty,
2212                          unsigned int cmd, unsigned long arg)
2213 {
2214         struct usb_serial_port *port = tty->driver_data;
2215         void __user *argp = (void __user *)arg;
2216         struct moschip_port *mos7840_port;
2217
2218         struct async_icount cnow;
2219         struct async_icount cprev;
2220
2221         if (mos7840_port_paranoia_check(port, __func__))
2222                 return -1;
2223
2224         mos7840_port = mos7840_get_port_private(port);
2225
2226         if (mos7840_port == NULL)
2227                 return -1;
2228
2229         dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2230
2231         switch (cmd) {
2232                 /* return number of bytes available */
2233
2234         case TIOCSERGETLSR:
2235                 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2236                 return mos7840_get_lsr_info(tty, argp);
2237
2238         case TIOCGSERIAL:
2239                 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2240                 return mos7840_get_serial_info(mos7840_port, argp);
2241
2242         case TIOCSSERIAL:
2243                 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2244                 break;
2245
2246         case TIOCMIWAIT:
2247                 dev_dbg(&port->dev, "%s  TIOCMIWAIT\n", __func__);
2248                 cprev = mos7840_port->icount;
2249                 while (1) {
2250                         /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2251                         mos7840_port->delta_msr_cond = 0;
2252                         wait_event_interruptible(mos7840_port->delta_msr_wait,
2253                                                  (mos7840_port->
2254                                                   delta_msr_cond == 1));
2255
2256                         /* see if a signal did it */
2257                         if (signal_pending(current))
2258                                 return -ERESTARTSYS;
2259                         cnow = mos7840_port->icount;
2260                         smp_rmb();
2261                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2262                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2263                                 return -EIO;    /* no change => error */
2264                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2265                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2266                             ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2267                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2268                                 return 0;
2269                         }
2270                         cprev = cnow;
2271                 }
2272                 /* NOTREACHED */
2273                 break;
2274
2275         default:
2276                 break;
2277         }
2278         return -ENOIOCTLCMD;
2279 }
2280
2281 static int mos7810_check(struct usb_serial *serial)
2282 {
2283         int i, pass_count = 0;
2284         __u16 data = 0, mcr_data = 0;
2285         __u16 test_pattern = 0x55AA;
2286
2287         /* Store MCR setting */
2288         usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2289                 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2290                 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2291
2292         for (i = 0; i < 16; i++) {
2293                 /* Send the 1-bit test pattern out to MCS7810 test pin */
2294                 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2295                         MCS_WRREQ, MCS_WR_RTYPE,
2296                         (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2297                         MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2298
2299                 /* Read the test pattern back */
2300                 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2301                         MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2302                         VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2303
2304                 /* If this is a MCS7810 device, both test patterns must match */
2305                 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2306                         break;
2307
2308                 pass_count++;
2309         }
2310
2311         /* Restore MCR setting */
2312         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2313                 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2314                 0, MOS_WDR_TIMEOUT);
2315
2316         if (pass_count == 16)
2317                 return 1;
2318
2319         return 0;
2320 }
2321
2322 static int mos7840_calc_num_ports(struct usb_serial *serial)
2323 {
2324         __u16 data = 0x00;
2325         int mos7840_num_ports;
2326
2327         usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2328                 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2329                 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2330
2331         if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2332                 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2333                 device_type = serial->dev->descriptor.idProduct;
2334         } else {
2335                 /* For a MCS7840 device GPIO0 must be set to 1 */
2336                 if ((data & 0x01) == 1)
2337                         device_type = MOSCHIP_DEVICE_ID_7840;
2338                 else if (mos7810_check(serial))
2339                         device_type = MOSCHIP_DEVICE_ID_7810;
2340                 else
2341                         device_type = MOSCHIP_DEVICE_ID_7820;
2342         }
2343
2344         mos7840_num_ports = (device_type >> 4) & 0x000F;
2345         serial->num_bulk_in = mos7840_num_ports;
2346         serial->num_bulk_out = mos7840_num_ports;
2347         serial->num_ports = mos7840_num_ports;
2348
2349         return mos7840_num_ports;
2350 }
2351
2352 /****************************************************************************
2353  * mos7840_startup
2354  ****************************************************************************/
2355
2356 static int mos7840_startup(struct usb_serial *serial)
2357 {
2358         struct moschip_port *mos7840_port;
2359         struct usb_device *dev;
2360         int i, status;
2361         __u16 Data;
2362
2363         dev = serial->dev;
2364
2365         /* we set up the pointers to the endpoints in the mos7840_open *
2366          * function, as the structures aren't created yet.             */
2367
2368         /* set up port private structures */
2369         for (i = 0; i < serial->num_ports; ++i) {
2370                 dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i);
2371                 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2372                 if (mos7840_port == NULL) {
2373                         dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2374                         status = -ENOMEM;
2375                         i--; /* don't follow NULL pointer cleaning up */
2376                         goto error;
2377                 }
2378
2379                 /* Initialize all port interrupt end point to port 0 int
2380                  * endpoint. Our device has only one interrupt end point
2381                  * common to all port */
2382
2383                 mos7840_port->port = serial->port[i];
2384                 mos7840_set_port_private(serial->port[i], mos7840_port);
2385                 spin_lock_init(&mos7840_port->pool_lock);
2386
2387                 /* minor is not initialised until later by
2388                  * usb-serial.c:get_free_serial() and cannot therefore be used
2389                  * to index device instances */
2390                 mos7840_port->port_num = i + 1;
2391                 dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number);
2392                 dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor);
2393                 dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2394                 dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor);
2395
2396                 if (mos7840_port->port_num == 1) {
2397                         mos7840_port->SpRegOffset = 0x0;
2398                         mos7840_port->ControlRegOffset = 0x1;
2399                         mos7840_port->DcrRegOffset = 0x4;
2400                 } else if ((mos7840_port->port_num == 2)
2401                            && (serial->num_ports == 4)) {
2402                         mos7840_port->SpRegOffset = 0x8;
2403                         mos7840_port->ControlRegOffset = 0x9;
2404                         mos7840_port->DcrRegOffset = 0x16;
2405                 } else if ((mos7840_port->port_num == 2)
2406                            && (serial->num_ports == 2)) {
2407                         mos7840_port->SpRegOffset = 0xa;
2408                         mos7840_port->ControlRegOffset = 0xb;
2409                         mos7840_port->DcrRegOffset = 0x19;
2410                 } else if ((mos7840_port->port_num == 3)
2411                            && (serial->num_ports == 4)) {
2412                         mos7840_port->SpRegOffset = 0xa;
2413                         mos7840_port->ControlRegOffset = 0xb;
2414                         mos7840_port->DcrRegOffset = 0x19;
2415                 } else if ((mos7840_port->port_num == 4)
2416                            && (serial->num_ports == 4)) {
2417                         mos7840_port->SpRegOffset = 0xc;
2418                         mos7840_port->ControlRegOffset = 0xd;
2419                         mos7840_port->DcrRegOffset = 0x1c;
2420                 }
2421                 mos7840_dump_serial_port(serial->port[i], mos7840_port);
2422                 mos7840_set_port_private(serial->port[i], mos7840_port);
2423
2424                 /* enable rx_disable bit in control register */
2425                 status = mos7840_get_reg_sync(serial->port[i],
2426                                  mos7840_port->ControlRegOffset, &Data);
2427                 if (status < 0) {
2428                         dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status);
2429                         break;
2430                 } else
2431                         dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2432                 Data |= 0x08;   /* setting driver done bit */
2433                 Data |= 0x04;   /* sp1_bit to have cts change reflect in
2434                                    modem status reg */
2435
2436                 /* Data |= 0x20; //rx_disable bit */
2437                 status = mos7840_set_reg_sync(serial->port[i],
2438                                          mos7840_port->ControlRegOffset, Data);
2439                 if (status < 0) {
2440                         dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2441                         break;
2442                 } else
2443                         dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2444
2445                 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2446                    and 0x24 in DCR3 */
2447                 Data = 0x01;
2448                 status = mos7840_set_reg_sync(serial->port[i],
2449                          (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2450                 if (status < 0) {
2451                         dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status);
2452                         break;
2453                 } else
2454                         dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status);
2455
2456                 Data = 0x05;
2457                 status = mos7840_set_reg_sync(serial->port[i],
2458                          (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2459                 if (status < 0) {
2460                         dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status);
2461                         break;
2462                 } else
2463                         dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status);
2464
2465                 Data = 0x24;
2466                 status = mos7840_set_reg_sync(serial->port[i],
2467                          (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2468                 if (status < 0) {
2469                         dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status);
2470                         break;
2471                 } else
2472                         dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status);
2473
2474                 /* write values in clkstart0x0 and clkmulti 0x20 */
2475                 Data = 0x0;
2476                 status = mos7840_set_reg_sync(serial->port[i],
2477                                          CLK_START_VALUE_REGISTER, Data);
2478                 if (status < 0) {
2479                         dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2480                         break;
2481                 } else
2482                         dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2483
2484                 Data = 0x20;
2485                 status = mos7840_set_reg_sync(serial->port[i],
2486                                         CLK_MULTI_REGISTER, Data);
2487                 if (status < 0) {
2488                         dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2489                         goto error;
2490                 } else
2491                         dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2492
2493                 /* write value 0x0 to scratchpad register */
2494                 Data = 0x00;
2495                 status = mos7840_set_uart_reg(serial->port[i],
2496                                                 SCRATCH_PAD_REGISTER, Data);
2497                 if (status < 0) {
2498                         dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2499                         break;
2500                 } else
2501                         dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2502
2503                 /* Zero Length flag register */
2504                 if ((mos7840_port->port_num != 1)
2505                     && (serial->num_ports == 2)) {
2506
2507                         Data = 0xff;
2508                         status = mos7840_set_reg_sync(serial->port[i],
2509                                       (__u16) (ZLP_REG1 +
2510                                       ((__u16)mos7840_port->port_num)), Data);
2511                         dev_dbg(&dev->dev, "ZLIP offset %x\n",
2512                                 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2513                         if (status < 0) {
2514                                 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status);
2515                                 break;
2516                         } else
2517                                 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status);
2518                 } else {
2519                         Data = 0xff;
2520                         status = mos7840_set_reg_sync(serial->port[i],
2521                               (__u16) (ZLP_REG1 +
2522                               ((__u16)mos7840_port->port_num) - 0x1), Data);
2523                         dev_dbg(&dev->dev, "ZLIP offset %x\n",
2524                                 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2525                         if (status < 0) {
2526                                 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status);
2527                                 break;
2528                         } else
2529                                 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status);
2530
2531                 }
2532                 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2533                 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2534                 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2535                                                                 GFP_KERNEL);
2536                 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2537                                                         !mos7840_port->dr) {
2538                         status = -ENOMEM;
2539                         goto error;
2540                 }
2541
2542                 mos7840_port->has_led = false;
2543
2544                 /* Initialize LED timers */
2545                 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2546                         mos7840_port->has_led = true;
2547
2548                         init_timer(&mos7840_port->led_timer1);
2549                         mos7840_port->led_timer1.function = mos7840_led_off;
2550                         mos7840_port->led_timer1.expires =
2551                                         jiffies + msecs_to_jiffies(LED_ON_MS);
2552                         mos7840_port->led_timer1.data =
2553                                                 (unsigned long)mos7840_port;
2554
2555                         init_timer(&mos7840_port->led_timer2);
2556                         mos7840_port->led_timer2.function =
2557                                                 mos7840_led_flag_off;
2558                         mos7840_port->led_timer2.expires =
2559                                         jiffies + msecs_to_jiffies(LED_OFF_MS);
2560                         mos7840_port->led_timer2.data =
2561                                                 (unsigned long)mos7840_port;
2562
2563                         mos7840_port->led_flag = false;
2564
2565                         /* Turn off LED */
2566                         mos7840_set_led_sync(serial->port[i],
2567                                                 MODEM_CONTROL_REGISTER, 0x0300);
2568                 }
2569         }
2570
2571         /* Zero Length flag enable */
2572         Data = 0x0f;
2573         status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2574         if (status < 0) {
2575                 dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2576                 goto error;
2577         } else
2578                 dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status);
2579
2580         /* setting configuration feature to one */
2581         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2582                         (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2583         return 0;
2584 error:
2585         for (/* nothing */; i >= 0; i--) {
2586                 mos7840_port = mos7840_get_port_private(serial->port[i]);
2587
2588                 kfree(mos7840_port->dr);
2589                 kfree(mos7840_port->ctrl_buf);
2590                 usb_free_urb(mos7840_port->control_urb);
2591                 kfree(mos7840_port);
2592                 serial->port[i] = NULL;
2593         }
2594         return status;
2595 }
2596
2597 /****************************************************************************
2598  * mos7840_disconnect
2599  *      This function is called whenever the device is removed from the usb bus.
2600  ****************************************************************************/
2601
2602 static void mos7840_disconnect(struct usb_serial *serial)
2603 {
2604         int i;
2605         unsigned long flags;
2606         struct moschip_port *mos7840_port;
2607
2608         /* check for the ports to be closed,close the ports and disconnect */
2609
2610         /* free private structure allocated for serial port  *
2611          * stop reads and writes on all ports                */
2612
2613         for (i = 0; i < serial->num_ports; ++i) {
2614                 mos7840_port = mos7840_get_port_private(serial->port[i]);
2615                 if (mos7840_port) {
2616                         spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2617                         mos7840_port->zombie = 1;
2618                         spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2619                         usb_kill_urb(mos7840_port->control_urb);
2620                 }
2621         }
2622 }
2623
2624 /****************************************************************************
2625  * mos7840_release
2626  *      This function is called when the usb_serial structure is freed.
2627  ****************************************************************************/
2628
2629 static void mos7840_release(struct usb_serial *serial)
2630 {
2631         int i;
2632         struct moschip_port *mos7840_port;
2633
2634         /* check for the ports to be closed,close the ports and disconnect */
2635
2636         /* free private structure allocated for serial port  *
2637          * stop reads and writes on all ports                */
2638
2639         for (i = 0; i < serial->num_ports; ++i) {
2640                 mos7840_port = mos7840_get_port_private(serial->port[i]);
2641                 if (mos7840_port) {
2642                         if (mos7840_port->has_led) {
2643                                 /* Turn off LED */
2644                                 mos7840_set_led_sync(mos7840_port->port,
2645                                                 MODEM_CONTROL_REGISTER, 0x0300);
2646
2647                                 del_timer_sync(&mos7840_port->led_timer1);
2648                                 del_timer_sync(&mos7840_port->led_timer2);
2649                         }
2650                         kfree(mos7840_port->ctrl_buf);
2651                         kfree(mos7840_port->dr);
2652                         kfree(mos7840_port);
2653                 }
2654         }
2655 }
2656
2657 static struct usb_serial_driver moschip7840_4port_device = {
2658         .driver = {
2659                    .owner = THIS_MODULE,
2660                    .name = "mos7840",
2661                    },
2662         .description = DRIVER_DESC,
2663         .id_table = id_table,
2664         .num_ports = 4,
2665         .open = mos7840_open,
2666         .close = mos7840_close,
2667         .write = mos7840_write,
2668         .write_room = mos7840_write_room,
2669         .chars_in_buffer = mos7840_chars_in_buffer,
2670         .throttle = mos7840_throttle,
2671         .unthrottle = mos7840_unthrottle,
2672         .calc_num_ports = mos7840_calc_num_ports,
2673 #ifdef MCSSerialProbe
2674         .probe = mos7840_serial_probe,
2675 #endif
2676         .ioctl = mos7840_ioctl,
2677         .set_termios = mos7840_set_termios,
2678         .break_ctl = mos7840_break,
2679         .tiocmget = mos7840_tiocmget,
2680         .tiocmset = mos7840_tiocmset,
2681         .get_icount = mos7840_get_icount,
2682         .attach = mos7840_startup,
2683         .disconnect = mos7840_disconnect,
2684         .release = mos7840_release,
2685         .read_bulk_callback = mos7840_bulk_in_callback,
2686         .read_int_callback = mos7840_interrupt_callback,
2687 };
2688
2689 static struct usb_serial_driver * const serial_drivers[] = {
2690         &moschip7840_4port_device, NULL
2691 };
2692
2693 module_usb_serial_driver(serial_drivers, id_table);
2694
2695 MODULE_DESCRIPTION(DRIVER_DESC);
2696 MODULE_LICENSE("GPL");
2697
2698 module_param(debug, bool, S_IRUGO | S_IWUSR);
2699 MODULE_PARM_DESC(debug, "Debug enabled or not");