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