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