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