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