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