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