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