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