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