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