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