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