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