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