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