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