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