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