]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/uc2322/aten2011.c
Staging: aten2011: move .h files into the driver
[linux-2.6-omap-h63xx.git] / drivers / staging / uc2322 / aten2011.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*************************************************************************
18  *** --------------------------------------------------------------------
19  ***
20  *** Project Name: ATENINTL
21  ***
22  *** Module Name: ATEN2011
23  ***
24  *** File: aten2011.c
25  ***
26  ***
27  *** File Revision: 1.2
28  ***
29  *** Revision Date:  2009-01-16
30  ***
31  ***
32  *** Purpose      : It gives an interface between USB to  4 Serial
33  ***                and serves as a Serial Driver for the high
34  ***                level layers /applications.
35  ***
36  *** Change History:
37  ***    Modified from ATEN revision 1.2 for Linux kernel 2.6.26 or later
38  ***
39  *** LEGEND       :
40  ***
41  ***
42  *** DBG - Code inserted due to as part of debugging
43  *** DPRINTK - Debug Print statement
44  ***
45  *************************************************************************/
46
47 /* all file inclusion goes here */
48
49 #include <linux/kernel.h>
50 #include <linux/errno.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/tty.h>
54 #include <linux/tty_driver.h>
55 #include <linux/tty_flip.h>
56 #include <linux/module.h>
57 //#include <linux/spinlock.h>
58 #include <linux/serial.h>
59 //#include <linux/ioctl.h>
60 #include <linux/usb.h>
61 #include <asm/uaccess.h>
62
63 #define KERNEL_2_6              1
64
65 #include <linux/usb/serial.h>
66
67 #define MAX_RS232_PORTS         2       /* Max # of RS-232 ports per device */
68
69 #include <linux/version.h>
70
71 /*
72  *  All typedef goes here
73  */
74
75
76 /* typedefs that the insideout headers need */
77
78 #ifndef TRUE
79         #define TRUE            (1)
80 #endif
81
82 #ifndef FALSE
83         #define FALSE           (0)
84 #endif
85
86 #ifndef LOW8
87         #define LOW8(val)       ((unsigned char)(val & 0xff))
88 #endif
89
90 #ifndef HIGH8
91         #define HIGH8(val)      ((unsigned char)((val & 0xff00) >> 8))
92 #endif
93
94 #ifndef NUM_ENTRIES
95         #define NUM_ENTRIES(x)  (sizeof(x)/sizeof((x)[0]))
96 #endif
97
98 #define MAX_SERIALNUMBER_LEN    12
99
100 /* The following table is used to map the USBx port number to
101  * the device serial number (or physical USB path), */
102
103 #define MAX_ATENPORTS   2
104 #define MAX_NAME_LEN    64
105
106 #define RAID_REG1       0x30
107 #define RAID_REG2       0x31
108
109 #define ZLP_REG1        0x3A      //Zero_Flag_Reg1    58
110 #define ZLP_REG2        0x3B      //Zero_Flag_Reg2    59
111 #define ZLP_REG3        0x3C      //Zero_Flag_Reg3    60
112 #define ZLP_REG4        0x3D      //Zero_Flag_Reg4    61
113 #define ZLP_REG5        0x3E      //Zero_Flag_Reg5    62
114
115 #define THRESHOLD_VAL_SP1_1     0x3F
116 #define THRESHOLD_VAL_SP1_2     0x40
117 #define THRESHOLD_VAL_SP2_1     0x41
118 #define THRESHOLD_VAL_SP2_2     0x42
119
120 #define THRESHOLD_VAL_SP3_1     0x43
121 #define THRESHOLD_VAL_SP3_2     0x44
122 #define THRESHOLD_VAL_SP4_1     0x45
123 #define THRESHOLD_VAL_SP4_2     0x46
124
125
126 /* For higher baud Rates use TIOCEXBAUD */
127 #define TIOCEXBAUD      0x5462
128
129 #define BAUD_1152       0       /* 115200bps  * 1       */
130 #define BAUD_2304       1       /* 230400bps  * 2       */
131 #define BAUD_4032       2       /* 403200bps  * 3.5     */
132 #define BAUD_4608       3       /* 460800bps  * 4       */
133 #define BAUD_8064       4       /* 806400bps  * 7       */
134 #define BAUD_9216       5       /* 921600bps  * 8       */
135
136 #define CHASE_TIMEOUT           (5*HZ)          /* 5 seconds */
137 #define OPEN_TIMEOUT            (5*HZ)          /* 5 seconds */
138 #define COMMAND_TIMEOUT         (5*HZ)          /* 5 seconds */
139
140 #ifndef SERIAL_MAGIC
141         #define SERIAL_MAGIC    0x6702
142 #endif
143
144 #define PORT_MAGIC              0x7301
145
146
147
148 /* vendor id and device id defines */
149
150 #define USB_VENDOR_ID_ATENINTL          0x0557
151 #define ATENINTL_DEVICE_ID_2011         0x2011
152 #define ATENINTL_DEVICE_ID_7820         0x7820
153
154 /* Product information read from the ATENINTL. Provided for later upgrade */
155
156 /* Interrupt Rotinue Defines    */
157
158 #define SERIAL_IIR_RLS      0x06
159 #define SERIAL_IIR_RDA      0x04
160 #define SERIAL_IIR_CTI      0x0c
161 #define SERIAL_IIR_THR      0x02
162 #define SERIAL_IIR_MS       0x00
163
164 /*
165  *  Emulation of the bit mask on the LINE STATUS REGISTER.
166  */
167 #define SERIAL_LSR_DR       0x0001
168 #define SERIAL_LSR_OE       0x0002
169 #define SERIAL_LSR_PE       0x0004
170 #define SERIAL_LSR_FE       0x0008
171 #define SERIAL_LSR_BI       0x0010
172 #define SERIAL_LSR_THRE     0x0020
173 #define SERIAL_LSR_TEMT     0x0040
174 #define SERIAL_LSR_FIFOERR  0x0080
175
176 //MSR bit defines(place holders)
177 #define ATEN_MSR_CTS         0x01
178 #define ATEN_MSR_DSR         0x02
179 #define ATEN_MSR_RI          0x04
180 #define ATEN_MSR_CD          0x08
181 #define ATEN_MSR_DELTA_CTS   0x10
182 #define ATEN_MSR_DELTA_DSR   0x20
183 #define ATEN_MSR_DELTA_RI    0x40
184 #define ATEN_MSR_DELTA_CD    0x80
185
186 // Serial Port register Address
187 #define RECEIVE_BUFFER_REGISTER    ((__u16)(0x00))
188 #define TRANSMIT_HOLDING_REGISTER  ((__u16)(0x00))
189 #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
190 #define INTERRUPT_IDENT_REGISTER   ((__u16)(0x02))
191 #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
192 #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
193 #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
194 #define LINE_STATUS_REGISTER       ((__u16)(0x05))
195 #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
196 #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
197 #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
198 #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
199
200 #define SP_REGISTER_BASE           ((__u16)(0x08))
201 #define CONTROL_REGISTER_BASE      ((__u16)(0x09))
202 #define DCR_REGISTER_BASE          ((__u16)(0x16))
203
204 #define SP1_REGISTER               ((__u16)(0x00))
205 #define CONTROL1_REGISTER          ((__u16)(0x01))
206 #define CLK_MULTI_REGISTER         ((__u16)(0x02))
207 #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
208 #define DCR1_REGISTER              ((__u16)(0x04))
209 #define GPIO_REGISTER              ((__u16)(0x07))
210
211 #define CLOCK_SELECT_REG1          ((__u16)(0x13))
212 #define CLOCK_SELECT_REG2          ((__u16)(0x14))
213
214 #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
215
216 /*
217  * URB POOL related defines
218  */
219 #define NUM_URBS                        16     /* URB Count */
220 #define URB_TRANSFER_BUFFER_SIZE        32     /* URB Size  */
221
222 struct ATENINTL_product_info
223 {
224         __u16   ProductId;              /* Product Identifier */
225         __u8    NumPorts;               /* Number of ports on ATENINTL */
226         __u8    ProdInfoVer;            /* What version of structure is this? */
227
228         __u32   IsServer        :1;     /* Set if Server */
229         __u32   IsRS232         :1;     /* Set if RS-232 ports exist */
230         __u32   IsRS422         :1;     /* Set if RS-422 ports exist */
231         __u32   IsRS485         :1;     /* Set if RS-485 ports exist */
232         __u32   IsReserved      :28;    /* Reserved for later expansion */
233
234         __u8    CpuRev;                 /* CPU revision level (chg only if s/w visible) */
235         __u8    BoardRev;               /* PCB revision level (chg only if s/w visible) */
236
237         __u8    ManufactureDescDate[3]; /* MM/DD/YY when descriptor template was compiled */
238         __u8    Unused1[1];             /* Available */
239 };
240
241 // different USB-serial Adapter's ID's table
242 static struct usb_device_id ATENINTL_port_id_table [] = {
243         { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_2011) },
244         { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_7820) },
245         { } /* terminating entry */
246 };
247
248 static __devinitdata struct usb_device_id id_table_combined [] = {
249         { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_2011) },
250         { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_7820) },
251         { } /* terminating entry */
252 };
253
254 MODULE_DEVICE_TABLE (usb, id_table_combined);
255
256 /* This structure holds all of the local port information */
257 struct ATENINTL_port
258 {
259         int             port_num;          /*Actual port number in the device(1,2,etc)*/
260         __u8            bulk_out_endpoint;      /* the bulk out endpoint handle */
261         unsigned char   *bulk_out_buffer;       /* buffer used for the bulk out endpoint */
262         struct urb      *write_urb;             /* write URB for this port */
263         __u8            bulk_in_endpoint;       /* the bulk in endpoint handle */
264         unsigned char   *bulk_in_buffer;        /* the buffer we use for the bulk in endpoint */
265         struct urb      *read_urb;              /* read URB for this port */
266         __s16        rxBytesAvail;/*the number of bytes that we need to read from this device */
267         __s16           rxBytesRemaining;       /* the number of port bytes left to read */
268         char            write_in_progress;      /* TRUE while a write URB is outstanding */
269         __u8            shadowLCR;              /* last LCR value received */
270         __u8            shadowMCR;              /* last MCR value received */
271         __u8            shadowMSR;              /* last MSR value received */
272         __u8            shadowLSR;              /* last LSR value received */
273         __u8            shadowXonChar;          /* last value set as XON char in ATENINTL */
274         __u8            shadowXoffChar;         /* last value set as XOFF char in ATENINTL */
275         __u8            validDataMask;
276         __u32           baudRate;
277         char            open;
278         char            openPending;
279         char            commandPending;
280         char            closePending;
281         char            chaseResponsePending;
282         wait_queue_head_t       wait_chase;             /* for handling sleeping while waiting for chase to finish */
283         wait_queue_head_t       wait_open;              /* for handling sleeping while waiting for open to finish */
284         wait_queue_head_t       wait_command;           /* for handling sleeping while waiting for command to finish */
285         wait_queue_head_t       delta_msr_wait;         /* for handling sleeping while waiting for msr change to happen */
286          int                     delta_msr_cond;
287         struct async_icount     icount;
288         struct usb_serial_port  *port;                  /* loop back to the owner of this object */
289         /*Offsets*/
290         __u16           AppNum;
291         __u8            SpRegOffset;
292         __u8            ControlRegOffset;
293         __u8            DcrRegOffset;
294         __u8            ClkSelectRegOffset;
295         //for processing control URBS in interrupt context
296         struct urb      *control_urb;
297        // __le16 rx_creg;
298         char            *ctrl_buf;
299         int             MsrLsr;
300
301         struct urb      *write_urb_pool[NUM_URBS];
302         /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
303         struct ktermios tmp_termios;        /* stores the old termios settings */
304         spinlock_t      lock;                   /* private lock */
305 };
306
307
308 /* This structure holds all of the individual serial device information */
309 struct ATENINTL_serial
310 {
311         char            name[MAX_NAME_LEN+1];           /* string name of this device */
312         struct  ATENINTL_product_info   product_info;   /* Product Info */
313         __u8            interrupt_in_endpoint;          /* the interrupt endpoint handle */
314         unsigned char  *interrupt_in_buffer;            /* the buffer we use for the interrupt endpoint */
315         struct urb *    interrupt_read_urb;     /* our interrupt urb */
316         __u8            bulk_in_endpoint;       /* the bulk in endpoint handle */
317         unsigned char  *bulk_in_buffer;         /* the buffer we use for the bulk in endpoint */
318         struct urb      *read_urb;              /* our bulk read urb */
319         __u8            bulk_out_endpoint;      /* the bulk out endpoint handle */
320         __s16    rxBytesAvail;  /* the number of bytes that we need to read from this device */
321         __u8            rxPort;         /* the port that we are currently receiving data for */
322         __u8            rxStatusCode;           /* the receive status code */
323         __u8            rxStatusParam;          /* the receive status paramater */
324         __s16           rxBytesRemaining;       /* the number of port bytes left to read */
325         struct usb_serial       *serial;        /* loop back to the owner of this object */
326         int     ATEN2011_spectrum_2or4ports;    //this says the number of ports in the device
327         // Indicates about the no.of opened ports of an individual USB-serial adapater.
328         unsigned int    NoOfOpenPorts;
329         // a flag for Status endpoint polling
330         unsigned char   status_polling_started;
331 };
332
333 /* baud rate information */
334 struct ATEN2011_divisor_table_entry
335 {
336         __u32  BaudRate;
337         __u16  Divisor;
338 };
339
340 /* Define table of divisors for ATENINTL 2011 hardware     *
341  * These assume a 3.6864MHz crystal, the standard /16, and *
342  * MCR.7 = 0.                                              */
343 #ifdef NOTATEN2011
344 static struct ATEN2011_divisor_table_entry ATEN2011_divisor_table[] = {
345         {   50,         2304},
346         {   110,        1047},  /* 2094.545455 => 230450   => .0217 % over */
347         {   134,        857},   /* 1713.011152 => 230398.5 => .00065% under */
348         {   150,        768},
349         {   300,        384},
350         {   600,        192},
351         {   1200,       96},
352         {   1800,       64},
353         {   2400,       48},
354         {   4800,       24},
355         {   7200,       16},
356         {   9600,       12},
357         {   19200,      6},
358         {   38400,      3},
359         {   57600,      2},
360         {   115200,     1},
361 };
362 #endif
363
364 /* local function prototypes */
365 /* function prototypes for all URB callbacks */
366 static void ATEN2011_interrupt_callback(struct urb *urb);
367 static void ATEN2011_bulk_in_callback(struct urb *urb);
368 static void ATEN2011_bulk_out_data_callback(struct urb *urb);
369 static void ATEN2011_control_callback(struct urb *urb);
370 static int ATEN2011_get_reg(struct ATENINTL_port *ATEN,__u16 Wval, __u16 reg, __u16 * val);
371 int handle_newMsr(struct ATENINTL_port *port,__u8 newMsr);
372 int handle_newLsr(struct ATENINTL_port *port,__u8 newLsr);
373 /* function prototypes for the usbserial callbacks */
374
375 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
376 static int  ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
377 static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
378 static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *data, int count);
379 static int  ATEN2011_write_room(struct tty_struct *tty);
380 static int  ATEN2011_chars_in_buffer(struct tty_struct *tty);
381 static void ATEN2011_throttle(struct tty_struct *tty);
382 static void ATEN2011_unthrottle(struct tty_struct *tty);
383 static void ATEN2011_set_termios(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios);
384 static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
385                                 unsigned int set, unsigned int clear);
386 static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file);
387 static int  ATEN2011_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
388 static void ATEN2011_break(struct tty_struct *tty, int break_state);
389 #else
390 static int  ATEN2011_open(struct usb_serial_port *port, struct file *filp);
391 static void ATEN2011_close(struct usb_serial_port *port, struct file *filp);
392 static int ATEN2011_write(struct usb_serial_port *port, const unsigned char *data, int count);
393 static int  ATEN2011_write_room(struct usb_serial_port *port);
394 static int  ATEN2011_chars_in_buffer(struct usb_serial_port *port);
395 static void ATEN2011_throttle(struct usb_serial_port *port);
396 static void ATEN2011_unthrottle(struct usb_serial_port *port);
397 static void ATEN2011_set_termios        (struct usb_serial_port *port, struct ktermios *old_termios);
398 static int ATEN2011_tiocmset(struct usb_serial_port *port, struct file *file,
399                                 unsigned int set, unsigned int clear);
400 static int ATEN2011_tiocmget(struct usb_serial_port *port, struct file *file);
401 static int  ATEN2011_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
402 static void ATEN2011_break(struct usb_serial_port *port, int break_state);
403 #endif
404
405 //static void ATEN2011_break_ctl(struct usb_serial_port *port, int break_state );
406
407 static int  ATEN2011_startup(struct usb_serial *serial);
408 static void ATEN2011_shutdown(struct usb_serial *serial);
409 //static int ATEN2011_serial_probe(struct usb_serial *serial, const struct usb_device_id *id);
410 static int ATEN2011_calc_num_ports(struct usb_serial *serial);
411
412 /* function prototypes for all of our local functions */
413 static int  ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,__u16 *clk_sel_val);
414 static int  ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port *ATEN2011_port, int baudRate);
415 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
416 static void ATEN2011_change_port_settings(struct tty_struct *tty, struct ATENINTL_port *ATEN2011_port, struct ktermios *old_termios);
417 static void ATEN2011_block_until_chase_response(struct tty_struct *tty, struct ATENINTL_port *ATEN2011_port);
418 static void ATEN2011_block_until_tx_empty(struct tty_struct *tty, struct ATENINTL_port *ATEN2011_port);
419 #else
420 static void ATEN2011_change_port_settings(struct ATENINTL_port *ATEN2011_port, struct ktermios *old_termios);
421 static void ATEN2011_block_until_chase_response(struct ATENINTL_port *ATEN2011_port);
422 static void ATEN2011_block_until_tx_empty(struct ATENINTL_port *ATEN2011_port);
423 #endif
424
425 int __init ATENINTL2011_init(void);
426 void __exit ATENINTL2011_exit(void);
427
428
429 /*************************************
430  * Bit definitions for each register *
431  *************************************/
432 #define LCR_BITS_5              0x00    /* 5 bits/char */
433 #define LCR_BITS_6              0x01    /* 6 bits/char */
434 #define LCR_BITS_7              0x02    /* 7 bits/char */
435 #define LCR_BITS_8              0x03    /* 8 bits/char */
436 #define LCR_BITS_MASK           0x03    /* Mask for bits/char field */
437
438 #define LCR_STOP_1              0x00    /* 1 stop bit */
439 #define LCR_STOP_1_5            0x04    /* 1.5 stop bits (if 5   bits/char) */
440 #define LCR_STOP_2              0x04    /* 2 stop bits   (if 6-8 bits/char) */
441 #define LCR_STOP_MASK           0x04    /* Mask for stop bits field */
442
443 #define LCR_PAR_NONE            0x00    /* No parity */
444 #define LCR_PAR_ODD             0x08    /* Odd parity */
445 #define LCR_PAR_EVEN            0x18    /* Even parity */
446 #define LCR_PAR_MARK            0x28    /* Force parity bit to 1 */
447 #define LCR_PAR_SPACE           0x38    /* Force parity bit to 0 */
448 #define LCR_PAR_MASK            0x38    /* Mask for parity field */
449
450 #define LCR_SET_BREAK           0x40    /* Set Break condition */
451 #define LCR_DL_ENABLE           0x80    /* Enable access to divisor latch */
452
453 #define MCR_DTR                 0x01    /* Assert DTR */
454 #define MCR_RTS                 0x02    /* Assert RTS */
455 #define MCR_OUT1                0x04    /* Loopback only: Sets state of RI */
456 #define MCR_MASTER_IE           0x08    /* Enable interrupt outputs */
457 #define MCR_LOOPBACK            0x10    /* Set internal (digital) loopback mode */
458 #define MCR_XON_ANY             0x20    /* Enable any char to exit XOFF mode */
459
460 #define ATEN2011_MSR_CTS        0x10    /* Current state of CTS */
461 #define ATEN2011_MSR_DSR        0x20    /* Current state of DSR */
462 #define ATEN2011_MSR_RI         0x40    /* Current state of RI */
463 #define ATEN2011_MSR_CD         0x80    /* Current state of CD */
464
465
466 /* all defines goes here */
467
468 /*
469  * Debug related defines
470  */
471
472 /* 1: Enables the debugging -- 0: Disable the debugging */
473
474 //#define printk //
475
476 #define ATEN_DEBUG      0
477
478 #ifdef ATEN_DEBUG
479 static int debug = 0;
480 #define DPRINTK(fmt, args...) printk( "%s: " fmt, __FUNCTION__ , ## args)
481
482 #else
483 static int debug = 0;
484 #define DPRINTK(fmt, args...)
485
486 #endif
487 //#undef DPRINTK
488 //      #define DPRINTK(fmt, args...)
489
490 /*
491  * Version Information
492  */
493 #define DRIVER_VERSION "1.3.1"
494 #define DRIVER_DESC "ATENINTL 2011 USB Serial Adapter"
495
496 /*
497  * Defines used for sending commands to port
498  */
499
500 #define WAIT_FOR_EVER   (HZ * 0 )       /* timeout urb is wait for ever */
501 #define ATEN_WDR_TIMEOUT (HZ * 5 )      /* default urb timeout */
502
503 #define ATEN_PORT1       0x0200
504 #define ATEN_PORT2       0x0300
505 #define ATEN_VENREG      0x0000
506 #define ATEN_MAX_PORT    0x02
507 #define ATEN_WRITE       0x0E
508 #define ATEN_READ        0x0D
509
510 /* Requests */
511 #define ATEN_RD_RTYPE 0xC0
512 #define ATEN_WR_RTYPE 0x40
513 #define ATEN_RDREQ    0x0D
514 #define ATEN_WRREQ    0x0E
515 #define ATEN_CTRL_TIMEOUT        500
516 #define VENDOR_READ_LENGTH                      (0x01)
517
518 int ATEN2011_Thr_cnt;
519 //int ATEN2011_spectrum_2or4ports; //this says the number of ports in the device
520 //int NoOfOpenPorts;
521
522 int RS485mode = 0;              //set to 1 for RS485 mode and 0 for RS232 mode
523
524 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port, const
525                                                   char *function);
526 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial, const char
527                                           *function);
528 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port, const char
529                                         *function);
530
531 /* setting and get register values */
532 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
533                                  __u16 val);
534 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
535                                  __u16 * val);
536 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
537                                  __u16 val);
538 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
539                                  __u16 * val);
540
541 void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port);
542
543 /************************************************************************/
544 /************************************************************************/
545 /*             I N T E R F A C E   F U N C T I O N S                    */
546 /*             I N T E R F A C E   F U N C T I O N S                    */
547 /************************************************************************/
548 /************************************************************************/
549
550 static inline void ATEN2011_set_serial_private(struct usb_serial *serial,
551                                                struct ATENINTL_serial *data)
552 {
553         usb_set_serial_data(serial, (void *)data);
554 }
555
556 static inline struct ATENINTL_serial *ATEN2011_get_serial_private(struct
557                                                                   usb_serial
558                                                                   *serial)
559 {
560         return (struct ATENINTL_serial *)usb_get_serial_data(serial);
561 }
562
563 static inline void ATEN2011_set_port_private(struct usb_serial_port *port,
564                                              struct ATENINTL_port *data)
565 {
566         usb_set_serial_port_data(port, (void *)data);
567 }
568
569 static inline struct ATENINTL_port *ATEN2011_get_port_private(struct
570                                                               usb_serial_port
571                                                               *port)
572 {
573         return (struct ATENINTL_port *)usb_get_serial_port_data(port);
574 }
575
576 /*
577 Description:- To set the Control register by calling usb_fill_control_urb function by passing usb_sndctrlpipe function as parameter.
578
579 Input Parameters:
580 usb_serial_port:  Data Structure usb_serialport correponding to that seril port.
581 Reg: Register Address
582 Val:  Value to set in the Register.
583  */
584
585 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
586                                  __u16 val)
587 {
588         struct usb_device *dev = port->serial->dev;
589         val = val & 0x00ff;
590         DPRINTK("ATEN2011_set_reg_sync offset is %x, value %x\n", reg, val);
591
592         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
593                                ATEN_WR_RTYPE, val, reg, NULL, 0,
594                                ATEN_WDR_TIMEOUT);
595 }
596
597 /*
598 Description:- To set the Uart register by calling usb_fill_control_urb function by passing usb_rcvctrlpipe function as parameter.
599
600 Input Parameters:
601 usb_serial_port:  Data Structure usb_serialport correponding to that seril port.
602 Reg: Register Address
603 Val:  Value to receive from the Register.
604  */
605
606 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
607                                  __u16 * val)
608 {
609         struct usb_device *dev = port->serial->dev;
610         int ret = 0;
611
612         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
613                               ATEN_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
614                               ATEN_WDR_TIMEOUT);
615         DPRINTK("ATEN2011_get_reg_sync offset is %x, return val %x\n", reg,
616                 *val);
617         *val = (*val) & 0x00ff;
618         return ret;
619 }
620
621 /*
622 Description:- To set the Uart register by calling usb_fill_control_urb function by passing usb_sndctrlpipe function as parameter.
623
624 Input Parameters:
625 usb_serial_port:  Data Structure usb_serialport correponding to that seril port.
626 Reg: Register Address
627 Val:  Value to set in the Register.
628  */
629
630 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
631                                  __u16 val)
632 {
633
634         struct usb_device *dev = port->serial->dev;
635         struct ATENINTL_serial *ATEN2011_serial;
636         int minor;
637         ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
638         minor = port->serial->minor;
639         if (minor == SERIAL_TTY_NO_MINOR)
640                 minor = 0;
641         val = val & 0x00ff;
642         // For the UART control registers, the application number need to be Or'ed
643
644         if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
645                 val |= (((__u16) port->number - (__u16) (minor)) + 1) << 8;
646                 DPRINTK("ATEN2011_set_Uart_Reg application number is %x\n",
647                         val);
648         } else {
649                 if (((__u16) port->number - (__u16) (minor)) == 0) {
650                         //      val= 0x100;
651                         val |=
652                             (((__u16) port->number - (__u16) (minor)) + 1) << 8;
653                         DPRINTK
654                             ("ATEN2011_set_Uart_Reg application number is %x\n",
655                              val);
656                 } else {
657                         //      val=0x300;
658                         val |=
659                             (((__u16) port->number - (__u16) (minor)) + 2) << 8;
660                         DPRINTK
661                             ("ATEN2011_set_Uart_Reg application number is %x\n",
662                              val);
663                 }
664         }
665         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
666                                ATEN_WR_RTYPE, val, reg, NULL, 0,
667                                ATEN_WDR_TIMEOUT);
668
669 }
670
671 /*
672 Description:- To set the Control register by calling usb_fill_control_urb function by passing usb_rcvctrlpipe function as parameter.
673
674 Input Parameters:
675 usb_serial_port:  Data Structure usb_serialport correponding to that seril port.
676 Reg: Register Address
677 Val:  Value to receive from the Register.
678  */
679 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
680                                  __u16 * val)
681 {
682         struct usb_device *dev = port->serial->dev;
683         int ret = 0;
684         __u16 Wval;
685         struct ATENINTL_serial *ATEN2011_serial;
686         int minor = port->serial->minor;
687         ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
688         if (minor == SERIAL_TTY_NO_MINOR)
689                 minor = 0;
690
691         //DPRINTK("application number is %4x \n",(((__u16)port->number - (__u16)(minor))+1)<<8);
692         /*Wval  is same as application number */
693         if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
694                 Wval = (((__u16) port->number - (__u16) (minor)) + 1) << 8;
695                 DPRINTK("ATEN2011_get_Uart_Reg application number is %x\n",
696                         Wval);
697         } else {
698                 if (((__u16) port->number - (__u16) (minor)) == 0) {
699                         //      Wval= 0x100;
700                         Wval =
701                             (((__u16) port->number - (__u16) (minor)) + 1) << 8;
702                         DPRINTK
703                             ("ATEN2011_get_Uart_Reg application number is %x\n",
704                              Wval);
705                 } else {
706                         //      Wval=0x300;
707                         Wval =
708                             (((__u16) port->number - (__u16) (minor)) + 2) << 8;
709                         DPRINTK
710                             ("ATEN2011_get_Uart_Reg application number is %x\n",
711                              Wval);
712                 }
713         }
714         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
715                               ATEN_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
716                               ATEN_WDR_TIMEOUT);
717         *val = (*val) & 0x00ff;
718         return ret;
719 }
720
721 void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port)
722 {
723
724         DPRINTK("***************************************\n");
725         DPRINTK("Application number is %4x\n", ATEN2011_port->AppNum);
726         DPRINTK("SpRegOffset is %2x\n", ATEN2011_port->SpRegOffset);
727         DPRINTK("ControlRegOffset is %2x \n", ATEN2011_port->ControlRegOffset);
728         DPRINTK("DCRRegOffset is %2x \n", ATEN2011_port->DcrRegOffset);
729         //DPRINTK("ClkSelectRegOffset is %2x \n",ATEN2011_port->ClkSelectRegOffset);
730         DPRINTK("***************************************\n");
731
732 }
733
734 /* all structre defination goes here */
735 /****************************************************************************
736  * ATENINTL2011_4port_device
737  *              Structure defining ATEN2011, usb serial device
738  ****************************************************************************/
739 static struct usb_serial_driver ATENINTL2011_4port_device = {
740         .driver = {
741                    .owner = THIS_MODULE,
742                    .name = "ATEN2011",
743                    },
744         .description = DRIVER_DESC,
745         .id_table = ATENINTL_port_id_table,
746         .open = ATEN2011_open,
747         .close = ATEN2011_close,
748         .write = ATEN2011_write,
749         .write_room = ATEN2011_write_room,
750         .chars_in_buffer = ATEN2011_chars_in_buffer,
751         .throttle = ATEN2011_throttle,
752         .unthrottle = ATEN2011_unthrottle,
753         .calc_num_ports = ATEN2011_calc_num_ports,
754
755 #ifdef ATENSerialProbe
756         .probe = ATEN2011_serial_probe,
757 #endif
758         .ioctl = ATEN2011_ioctl,
759         .set_termios = ATEN2011_set_termios,
760         .break_ctl = ATEN2011_break,
761 //      .break_ctl              = ATEN2011_break_ctl,
762         .tiocmget = ATEN2011_tiocmget,
763         .tiocmset = ATEN2011_tiocmset,
764         .attach = ATEN2011_startup,
765         .shutdown = ATEN2011_shutdown,
766         .read_bulk_callback = ATEN2011_bulk_in_callback,
767         .read_int_callback = ATEN2011_interrupt_callback,
768 };
769
770 static struct usb_driver io_driver = {
771         .name = "ATEN2011",
772         .probe = usb_serial_probe,
773         .disconnect = usb_serial_disconnect,
774         .id_table = id_table_combined,
775 };
776
777 /************************************************************************/
778 /************************************************************************/
779 /*            U S B  C A L L B A C K   F U N C T I O N S                */
780 /*            U S B  C A L L B A C K   F U N C T I O N S                */
781 /************************************************************************/
782 /************************************************************************/
783
784 /*****************************************************************************
785  * ATEN2011_interrupt_callback
786  *      this is the callback function for when we have received data on the
787  *      interrupt endpoint.
788  * Input : 1 Input
789  *                      pointer to the URB packet,
790  *
791  *****************************************************************************/
792 //#ifdef ATEN2011
793 static void ATEN2011_interrupt_callback(struct urb *urb)
794 {
795         int result;
796         int length;
797         struct ATENINTL_port *ATEN2011_port;
798         struct ATENINTL_serial *ATEN2011_serial;
799         struct usb_serial *serial;
800         __u16 Data;
801         unsigned char *data;
802         __u8 sp[5], st;
803         int i;
804         __u16 wval;
805         int minor;
806         //printk("in the function ATEN2011_interrupt_callback Length %d, Data %x \n",urb->actual_length,(unsigned int)urb->transfer_buffer);
807         DPRINTK("%s", " : Entering\n");
808
809         ATEN2011_serial = (struct ATENINTL_serial *)urb->context;
810         if (!urb)               // || ATEN2011_serial->status_polling_started == FALSE )
811         {
812                 DPRINTK("%s", "Invalid Pointer !!!!:\n");
813                 return;
814         }
815
816         switch (urb->status) {
817         case 0:
818                 /* success */
819                 break;
820         case -ECONNRESET:
821         case -ENOENT:
822         case -ESHUTDOWN:
823                 /* this urb is terminated, clean up */
824                 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
825                     urb->status);
826                 return;
827         default:
828                 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
829                     urb->status);
830                 goto exit;
831         }
832         length = urb->actual_length;
833         data = urb->transfer_buffer;
834
835         //ATEN2011_serial= (struct ATENINTL_serial *)urb->context;
836         //serial  = ATEN2011_get_usb_serial(port,__FUNCTION__);
837         serial = ATEN2011_serial->serial;
838
839         /* ATENINTL get 5 bytes
840          * Byte 1 IIR Port 1 (port.number is 0)
841          * Byte 2 IIR Port 2 (port.number is 1)
842          * Byte 3 IIR Port 3 (port.number is 2)
843          * Byte 4 IIR Port 4 (port.number is 3)
844          * Byte 5 FIFO status for both */
845
846         if (length && length > 5) {
847                 DPRINTK("%s \n", "Wrong data !!!");
848                 return;
849         }
850
851         /* MATRIX */
852         if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
853                 sp[0] = (__u8) data[0];
854                 sp[1] = (__u8) data[1];
855                 sp[2] = (__u8) data[2];
856                 sp[3] = (__u8) data[3];
857                 st = (__u8) data[4];
858         } else {
859                 sp[0] = (__u8) data[0];
860                 sp[1] = (__u8) data[2];
861                 //sp[2]=(__u8)data[2];
862                 //sp[3]=(__u8)data[3];
863                 st = (__u8) data[4];
864
865         }
866         //      printk("%s data is sp1:%x sp2:%x sp3:%x sp4:%x status:%x\n",__FUNCTION__,sp1,sp2,sp3,sp4,st);
867         for (i = 0; i < serial->num_ports; i++) {
868                 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
869                 minor = serial->minor;
870                 if (minor == SERIAL_TTY_NO_MINOR)
871                         minor = 0;
872                 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
873                     && (i != 0))
874                         wval =
875                             (((__u16) serial->port[i]->number -
876                               (__u16) (minor)) + 2) << 8;
877                 else
878                         wval =
879                             (((__u16) serial->port[i]->number -
880                               (__u16) (minor)) + 1) << 8;
881                 if (ATEN2011_port->open != FALSE) {
882                         //printk("%s wval is:(for 2011) %x\n",__FUNCTION__,wval);
883
884                         if (sp[i] & 0x01) {
885                                 DPRINTK("SP%d No Interrupt !!!\n", i);
886                         } else {
887                                 switch (sp[i] & 0x0f) {
888                                 case SERIAL_IIR_RLS:
889                                         DPRINTK
890                                             ("Serial Port %d: Receiver status error or ",
891                                              i);
892                                         DPRINTK
893                                             ("address bit detected in 9-bit mode\n");
894                                         ATEN2011_port->MsrLsr = 1;
895                                         ATEN2011_get_reg(ATEN2011_port, wval,
896                                                          LINE_STATUS_REGISTER,
897                                                          &Data);
898                                         break;
899                                 case SERIAL_IIR_MS:
900                                         DPRINTK
901                                             ("Serial Port %d: Modem status change\n",
902                                              i);
903                                         ATEN2011_port->MsrLsr = 0;
904                                         ATEN2011_get_reg(ATEN2011_port, wval,
905                                                          MODEM_STATUS_REGISTER,
906                                                          &Data);
907                                         break;
908                                 }
909                         }
910                 }
911
912         }
913       exit:
914         if (ATEN2011_serial->status_polling_started == FALSE)
915                 return;
916
917         result = usb_submit_urb(urb, GFP_ATOMIC);
918         if (result) {
919                 dev_err(&urb->dev->dev,
920                         "%s - Error %d submitting interrupt urb\n",
921                         __FUNCTION__, result);
922         }
923
924         return;
925
926 }
927
928 //#endif
929 static void ATEN2011_control_callback(struct urb *urb)
930 {
931         unsigned char *data;
932         struct ATENINTL_port *ATEN2011_port;
933         __u8 regval = 0x0;
934
935         if (!urb) {
936                 DPRINTK("%s", "Invalid Pointer !!!!:\n");
937                 return;
938         }
939
940         switch (urb->status) {
941         case 0:
942                 /* success */
943                 break;
944         case -ECONNRESET:
945         case -ENOENT:
946         case -ESHUTDOWN:
947                 /* this urb is terminated, clean up */
948                 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
949                     urb->status);
950                 return;
951         default:
952                 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
953                     urb->status);
954                 goto exit;
955         }
956
957         ATEN2011_port = (struct ATENINTL_port *)urb->context;
958
959         DPRINTK("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
960         DPRINTK("%s ATEN2011_port->MsrLsr is %d port %d\n", __FUNCTION__,
961                 ATEN2011_port->MsrLsr, ATEN2011_port->port_num);
962         data = urb->transfer_buffer;
963         regval = (__u8) data[0];
964         DPRINTK("%s data is %x\n", __FUNCTION__, regval);
965         if (ATEN2011_port->MsrLsr == 0)
966                 handle_newMsr(ATEN2011_port, regval);
967         else if (ATEN2011_port->MsrLsr == 1)
968                 handle_newLsr(ATEN2011_port, regval);
969
970       exit:
971         return;
972 }
973 int handle_newMsr(struct ATENINTL_port *port, __u8 newMsr)
974 {
975         struct ATENINTL_port *ATEN2011_port;
976         struct async_icount *icount;
977         ATEN2011_port = port;
978         icount = &ATEN2011_port->icount;
979         if (newMsr &
980             (ATEN_MSR_DELTA_CTS | ATEN_MSR_DELTA_DSR | ATEN_MSR_DELTA_RI |
981              ATEN_MSR_DELTA_CD)) {
982                 icount = &ATEN2011_port->icount;
983
984                 /* update input line counters */
985                 if (newMsr & ATEN_MSR_DELTA_CTS) {
986                         icount->cts++;
987                 }
988                 if (newMsr & ATEN_MSR_DELTA_DSR) {
989                         icount->dsr++;
990                 }
991                 if (newMsr & ATEN_MSR_DELTA_CD) {
992                         icount->dcd++;
993                 }
994                 if (newMsr & ATEN_MSR_DELTA_RI) {
995                         icount->rng++;
996                 }
997         }
998
999         return 0;
1000 }
1001 int handle_newLsr(struct ATENINTL_port *port, __u8 newLsr)
1002 {
1003         struct async_icount *icount;
1004
1005         dbg("%s - %02x", __FUNCTION__, newLsr);
1006
1007         if (newLsr & SERIAL_LSR_BI) {
1008                 //
1009                 // Parity and Framing errors only count if they
1010                 // occur exclusive of a break being
1011                 // received.
1012                 //
1013                 newLsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
1014         }
1015
1016         /* update input line counters */
1017         icount = &port->icount;
1018         if (newLsr & SERIAL_LSR_BI) {
1019                 icount->brk++;
1020         }
1021         if (newLsr & SERIAL_LSR_OE) {
1022                 icount->overrun++;
1023         }
1024         if (newLsr & SERIAL_LSR_PE) {
1025                 icount->parity++;
1026         }
1027         if (newLsr & SERIAL_LSR_FE) {
1028                 icount->frame++;
1029         }
1030
1031         return 0;
1032 }
1033 static int ATEN2011_get_reg(struct ATENINTL_port *ATEN, __u16 Wval, __u16 reg,
1034                             __u16 * val)
1035 {
1036         struct usb_device *dev = ATEN->port->serial->dev;
1037         struct usb_ctrlrequest *dr = NULL;
1038         unsigned char *buffer = NULL;
1039         int ret = 0;
1040         buffer = (__u8 *) ATEN->ctrl_buf;
1041
1042 //      dr=(struct usb_ctrlrequest *)(buffer);
1043         dr = (void *)(buffer + 2);
1044         dr->bRequestType = ATEN_RD_RTYPE;
1045         dr->bRequest = ATEN_RDREQ;
1046         dr->wValue = cpu_to_le16(Wval); //0;
1047         dr->wIndex = cpu_to_le16(reg);
1048         dr->wLength = cpu_to_le16(2);
1049
1050         usb_fill_control_urb(ATEN->control_urb, dev, usb_rcvctrlpipe(dev, 0),
1051                              (unsigned char *)dr, buffer, 2,
1052                              ATEN2011_control_callback, ATEN);
1053         ATEN->control_urb->transfer_buffer_length = 2;
1054         ret = usb_submit_urb(ATEN->control_urb, GFP_ATOMIC);
1055         return ret;
1056 }
1057
1058 /*****************************************************************************
1059  * ATEN2011_bulk_in_callback
1060  *      this is the callback function for when we have received data on the
1061  *      bulk in endpoint.
1062  * Input : 1 Input
1063  *                      pointer to the URB packet,
1064  *
1065  *****************************************************************************/
1066 static void ATEN2011_bulk_in_callback(struct urb *urb)
1067 {
1068         int status;
1069         unsigned char *data;
1070         struct usb_serial *serial;
1071         struct usb_serial_port *port;
1072         struct ATENINTL_serial *ATEN2011_serial;
1073         struct ATENINTL_port *ATEN2011_port;
1074         struct tty_struct *tty;
1075         if (!urb) {
1076                 DPRINTK("%s", "Invalid Pointer !!!!:\n");
1077                 return;
1078         }
1079
1080         if (urb->status) {
1081                 DPRINTK("nonzero read bulk status received: %d", urb->status);
1082 //              if(urb->status==84)
1083                 //ThreadState=1;
1084                 return;
1085         }
1086
1087         ATEN2011_port = (struct ATENINTL_port *)urb->context;
1088         if (!ATEN2011_port) {
1089                 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
1090                 return;
1091         }
1092
1093         port = (struct usb_serial_port *)ATEN2011_port->port;
1094         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1095                 DPRINTK("%s", "Port Paranoia failed \n");
1096                 return;
1097         }
1098
1099         serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1100         if (!serial) {
1101                 DPRINTK("%s\n", "Bad serial pointer ");
1102                 return;
1103         }
1104
1105         DPRINTK("%s\n", "Entering... \n");
1106
1107         data = urb->transfer_buffer;
1108         ATEN2011_serial = ATEN2011_get_serial_private(serial);
1109
1110         DPRINTK("%s", "Entering ........... \n");
1111
1112         if (urb->actual_length) {
1113 //MATRIX
1114 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
1115                 tty = tty_port_tty_get(&ATEN2011_port->port->port);
1116 #elif LINUX_VERSION_CODE == KERNEL_VERSION(2,6,27)
1117                 tty = ATEN2011_port->port->port.tty;
1118 #else
1119                 tty = ATEN2011_port->port->tty;
1120 #endif
1121                 if (tty) {
1122                         tty_buffer_request_room(tty, urb->actual_length);
1123                         tty_insert_flip_string(tty, data, urb->actual_length);
1124                         DPRINTK(" %s \n", data);
1125                         tty_flip_buffer_push(tty);
1126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
1127                         tty_kref_put(tty);
1128 #endif
1129                 }
1130
1131                 ATEN2011_port->icount.rx += urb->actual_length;
1132                 DPRINTK("ATEN2011_port->icount.rx is %d:\n",
1133                         ATEN2011_port->icount.rx);
1134 //MATRIX
1135         }
1136
1137         if (!ATEN2011_port->read_urb) {
1138                 DPRINTK("%s", "URB KILLED !!!\n");
1139                 return;
1140         }
1141
1142         if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
1143                 ATEN2011_port->read_urb->dev = serial->dev;
1144
1145                 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
1146
1147                 if (status) {
1148                         DPRINTK
1149                             (" usb_submit_urb(read bulk) failed, status = %d",
1150                              status);
1151                 }
1152         }
1153 }
1154
1155 /*****************************************************************************
1156  * ATEN2011_bulk_out_data_callback
1157  *      this is the callback function for when we have finished sending serial data
1158  *      on the bulk out endpoint.
1159  * Input : 1 Input
1160  *                      pointer to the URB packet,
1161  *
1162  *****************************************************************************/
1163 static void ATEN2011_bulk_out_data_callback(struct urb *urb)
1164 {
1165         struct ATENINTL_port *ATEN2011_port;
1166         struct tty_struct *tty;
1167         if (!urb) {
1168                 DPRINTK("%s", "Invalid Pointer !!!!:\n");
1169                 return;
1170         }
1171
1172         if (urb->status) {
1173                 DPRINTK("nonzero write bulk status received:%d\n", urb->status);
1174                 return;
1175         }
1176
1177         ATEN2011_port = (struct ATENINTL_port *)urb->context;
1178         if (!ATEN2011_port) {
1179                 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
1180                 return;
1181         }
1182
1183         if (ATEN2011_port_paranoia_check(ATEN2011_port->port, __FUNCTION__)) {
1184                 DPRINTK("%s", "Port Paranoia failed \n");
1185                 return;
1186         }
1187
1188         DPRINTK("%s \n", "Entering .........");
1189
1190 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
1191         tty = tty_port_tty_get(&ATEN2011_port->port->port);
1192 #elif LINUX_VERSION_CODE == KERNEL_VERSION(2,6,27)
1193         tty = ATEN2011_port->port->port.tty;
1194 #else
1195         tty = ATEN2011_port->port->tty;
1196 #endif
1197
1198         if (tty && ATEN2011_port->open) {
1199                 /* let the tty driver wakeup if it has a special *
1200                  * write_wakeup function                         */
1201
1202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
1203                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1204                     && tty->ldisc.write_wakeup) {
1205                         (tty->ldisc.write_wakeup) (tty);
1206                 }
1207 #endif
1208
1209                 /* tell the tty driver that something has changed */
1210                 wake_up_interruptible(&tty->write_wait);
1211         }
1212
1213         /* Release the Write URB */
1214         ATEN2011_port->write_in_progress = FALSE;
1215
1216 //schedule_work(&ATEN2011_port->port->work);
1217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
1218         tty_kref_put(tty);
1219 #endif
1220
1221 }
1222
1223 /************************************************************************/
1224 /*       D R I V E R  T T Y  I N T E R F A C E  F U N C T I O N S       */
1225 /************************************************************************/
1226 #ifdef ATENSerialProbe
1227 static int ATEN2011_serial_probe(struct usb_serial *serial,
1228                                  const struct usb_device_id *id)
1229 {
1230
1231         /*need to implement the mode_reg reading and updating\
1232            structures usb_serial_ device_type\
1233            (i.e num_ports, num_bulkin,bulkout etc) */
1234         /* Also we can update the changes  attach */
1235         return 1;
1236 }
1237 #endif
1238
1239 /*****************************************************************************
1240  * SerialOpen
1241  *      this function is called by the tty driver when a port is opened
1242  *      If successful, we return 0
1243  *      Otherwise we return a negative error number.
1244  *****************************************************************************/
1245
1246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1247 static int ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port,
1248                          struct file *filp)
1249 #else
1250 static int ATEN2011_open(struct usb_serial_port *port, struct file *filp)
1251 #endif
1252 {
1253         int response;
1254         int j;
1255         struct usb_serial *serial;
1256 //    struct usb_serial_port *port0;
1257         struct urb *urb;
1258         __u16 Data;
1259         int status;
1260         struct ATENINTL_serial *ATEN2011_serial;
1261         struct ATENINTL_port *ATEN2011_port;
1262         struct ktermios tmp_termios;
1263         int minor;
1264 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
1265         struct tty_struct *tty = NULL;
1266 #endif
1267         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1268                 DPRINTK("%s", "Port Paranoia failed \n");
1269                 return -ENODEV;
1270         }
1271         //ATEN2011_serial->NoOfOpenPorts++;
1272         serial = port->serial;
1273
1274         if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1275                 DPRINTK("%s", "Serial Paranoia failed \n");
1276                 return -ENODEV;
1277         }
1278
1279         ATEN2011_port = ATEN2011_get_port_private(port);
1280
1281         if (ATEN2011_port == NULL)
1282                 return -ENODEV;
1283 /*
1284         if (ATEN2011_port->ctrl_buf==NULL)
1285         {
1286                 ATEN2011_port->ctrl_buf = kmalloc(16,GFP_KERNEL);
1287                 if (ATEN2011_port->ctrl_buf == NULL) {
1288                         printk(", Can't allocate ctrl buff\n");
1289                         return -ENOMEM;
1290                 }
1291
1292         }
1293
1294         if(!ATEN2011_port->control_urb)
1295         {
1296         ATEN2011_port->control_urb=kmalloc(sizeof(struct urb),GFP_KERNEL);
1297         }
1298 */
1299 //      port0 = serial->port[0];
1300
1301         ATEN2011_serial = ATEN2011_get_serial_private(serial);
1302
1303         if (ATEN2011_serial == NULL)    //|| port0 == NULL)
1304         {
1305                 return -ENODEV;
1306         }
1307         // increment the number of opened ports counter here
1308         ATEN2011_serial->NoOfOpenPorts++;
1309         //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
1310
1311         usb_clear_halt(serial->dev, port->write_urb->pipe);
1312         usb_clear_halt(serial->dev, port->read_urb->pipe);
1313
1314         /* Initialising the write urb pool */
1315         for (j = 0; j < NUM_URBS; ++j) {
1316                 urb = usb_alloc_urb(0, GFP_ATOMIC);
1317                 ATEN2011_port->write_urb_pool[j] = urb;
1318
1319                 if (urb == NULL) {
1320                         err("No more urbs???");
1321                         continue;
1322                 }
1323
1324                 urb->transfer_buffer = NULL;
1325                 urb->transfer_buffer =
1326                     kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1327                 if (!urb->transfer_buffer) {
1328                         err("%s-out of memory for urb buffers.", __FUNCTION__);
1329                         continue;
1330                 }
1331         }
1332
1333 /*****************************************************************************
1334  * Initialize ATEN2011 -- Write Init values to corresponding Registers
1335  *
1336  * Register Index
1337  * 1 : IER
1338  * 2 : FCR
1339  * 3 : LCR
1340  * 4 : MCR
1341  *
1342  * 0x08 : SP1/2 Control Reg
1343  *****************************************************************************/
1344
1345 //NEED to check the fallowing Block
1346
1347         status = 0;
1348         Data = 0x0;
1349         status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1350         if (status < 0) {
1351                 DPRINTK("Reading Spreg failed\n");
1352                 return -1;
1353         }
1354         Data |= 0x80;
1355         status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1356         if (status < 0) {
1357                 DPRINTK("writing Spreg failed\n");
1358                 return -1;
1359         }
1360
1361         Data &= ~0x80;
1362         status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1363         if (status < 0) {
1364                 DPRINTK("writing Spreg failed\n");
1365                 return -1;
1366         }
1367
1368 //End of block to be checked
1369 //**************************CHECK***************************//
1370
1371         if (RS485mode == 0)
1372                 Data = 0xC0;
1373         else
1374                 Data = 0x00;
1375         status = 0;
1376         status = ATEN2011_set_Uart_Reg(port, SCRATCH_PAD_REGISTER, Data);
1377         if (status < 0) {
1378                 DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
1379                         status);
1380                 return -1;
1381         } else
1382                 DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",
1383                         status);
1384
1385 //**************************CHECK***************************//
1386
1387         status = 0;
1388         Data = 0x0;
1389         status =
1390             ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1391         if (status < 0) {
1392                 DPRINTK("Reading Controlreg failed\n");
1393                 return -1;
1394         }
1395         Data |= 0x08;           //Driver done bit
1396         /*
1397            status = ATEN2011_set_reg_sync(port,ATEN2011_port->ControlRegOffset,Data);
1398            if(status<0){
1399            DPRINTK("writing Controlreg failed\n");
1400            return -1;
1401            }
1402          */
1403         Data |= 0x20;           //rx_disable
1404         status = 0;
1405         status =
1406             ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1407         if (status < 0) {
1408                 DPRINTK("writing Controlreg failed\n");
1409                 return -1;
1410         }
1411         //do register settings here
1412         // Set all regs to the device default values.
1413         ////////////////////////////////////
1414         // First Disable all interrupts.
1415         ////////////////////////////////////
1416
1417         Data = 0x00;
1418         status = 0;
1419         status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1420         if (status < 0) {
1421                 DPRINTK("disableing interrupts failed\n");
1422                 return -1;
1423         }
1424         // Set FIFO_CONTROL_REGISTER to the default value
1425         Data = 0x00;
1426         status = 0;
1427         status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1428         if (status < 0) {
1429                 DPRINTK("Writing FIFO_CONTROL_REGISTER  failed\n");
1430                 return -1;
1431         }
1432
1433         Data = 0xcf;            //chk
1434         status = 0;
1435         status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1436         if (status < 0) {
1437                 DPRINTK("Writing FIFO_CONTROL_REGISTER  failed\n");
1438                 return -1;
1439         }
1440
1441         Data = 0x03;            //LCR_BITS_8
1442         status = 0;
1443         status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1444         ATEN2011_port->shadowLCR = Data;
1445
1446         Data = 0x0b;            // MCR_DTR|MCR_RTS|MCR_MASTER_IE
1447         status = 0;
1448         status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1449         ATEN2011_port->shadowMCR = Data;
1450
1451 #ifdef Check
1452         Data = 0x00;
1453         status = 0;
1454         status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1455         ATEN2011_port->shadowLCR = Data;
1456
1457         Data |= SERIAL_LCR_DLAB;        //data latch enable in LCR 0x80
1458         status = 0;
1459         status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1460
1461         Data = 0x0c;
1462         status = 0;
1463         status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
1464
1465         Data = 0x0;
1466         status = 0;
1467         status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
1468
1469         Data = 0x00;
1470         status = 0;
1471         status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1472
1473 //      Data = ATEN2011_port->shadowLCR; //data latch disable
1474         Data = Data & ~SERIAL_LCR_DLAB;
1475         status = 0;
1476         status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1477         ATEN2011_port->shadowLCR = Data;
1478 #endif
1479         //clearing Bulkin and Bulkout Fifo
1480         Data = 0x0;
1481         status = 0;
1482         status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1483
1484         Data = Data | 0x0c;
1485         status = 0;
1486         status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1487
1488         Data = Data & ~0x0c;
1489         status = 0;
1490         status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1491         //Finally enable all interrupts
1492         Data = 0x0;
1493         Data = 0x0c;
1494         status = 0;
1495         status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1496
1497         //clearing rx_disable
1498         Data = 0x0;
1499         status = 0;
1500         status =
1501             ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1502         Data = Data & ~0x20;
1503         status = 0;
1504         status =
1505             ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1506
1507         // rx_negate
1508         Data = 0x0;
1509         status = 0;
1510         status =
1511             ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1512         Data = Data | 0x10;
1513         status = 0;
1514         status =
1515             ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1516
1517         /* force low_latency on so that our tty_push actually forces *
1518          * the data through,otherwise it is scheduled, and with      *
1519          * high data rates (like with OHCI) data can get lost.       */
1520
1521 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
1522         tty = port->tty;
1523 #endif
1524         if (tty)
1525                 tty->low_latency = 1;
1526 /*
1527         printk("port number is %d \n",port->number);
1528         printk("serial number is %d \n",port->serial->minor);
1529         printk("Bulkin endpoint is %d \n",port->bulk_in_endpointAddress);
1530         printk("BulkOut endpoint is %d \n",port->bulk_out_endpointAddress);
1531         printk("Interrupt endpoint is %d \n",port->interrupt_in_endpointAddress);
1532         printk("port's number in the device is %d\n",ATEN2011_port->port_num);
1533 */
1534 ////////////////////////
1535 //#ifdef CheckStatusPipe
1536 /* Check to see if we've set up our endpoint info yet    *
1537      * (can't set it up in ATEN2011_startup as the structures *
1538      * were not set up at that time.)                        */
1539         if (ATEN2011_serial->NoOfOpenPorts == 1) {
1540                 // start the status polling here
1541                 ATEN2011_serial->status_polling_started = TRUE;
1542                 //if (ATEN2011_serial->interrupt_in_buffer == NULL)
1543                 // {
1544                 /* If not yet set, Set here */
1545                 ATEN2011_serial->interrupt_in_buffer =
1546                     serial->port[0]->interrupt_in_buffer;
1547                 ATEN2011_serial->interrupt_in_endpoint =
1548                     serial->port[0]->interrupt_in_endpointAddress;
1549                 //printk(" interrupt endpoint:%d \n",ATEN2011_serial->interrupt_in_endpoint);
1550                 ATEN2011_serial->interrupt_read_urb =
1551                     serial->port[0]->interrupt_in_urb;
1552
1553                 /* set up interrupt urb */
1554                 usb_fill_int_urb(ATEN2011_serial->interrupt_read_urb,
1555                                  serial->dev,
1556                                  usb_rcvintpipe(serial->dev,
1557                                                 ATEN2011_serial->
1558                                                 interrupt_in_endpoint),
1559                                  ATEN2011_serial->interrupt_in_buffer,
1560                                  ATEN2011_serial->interrupt_read_urb->
1561                                  transfer_buffer_length,
1562                                  ATEN2011_interrupt_callback, ATEN2011_serial,
1563                                  ATEN2011_serial->interrupt_read_urb->interval);
1564
1565                 /* start interrupt read for ATEN2011               *
1566                  * will continue as long as ATEN2011 is connected  */
1567
1568                 response =
1569                     usb_submit_urb(ATEN2011_serial->interrupt_read_urb,
1570                                    GFP_KERNEL);
1571                 if (response) {
1572                         DPRINTK("%s - Error %d submitting interrupt urb",
1573                                 __FUNCTION__, response);
1574                 }
1575                 //      else
1576                 // printk(" interrupt URB submitted\n");
1577
1578                 //}
1579
1580         }
1581 //#endif
1582
1583 ///////////////////////
1584         /* see if we've set up our endpoint info yet   *
1585          * (can't set it up in ATEN2011_startup as the  *
1586          * structures were not set up at that time.)   */
1587
1588         DPRINTK("port number is %d \n", port->number);
1589         DPRINTK("serial number is %d \n", port->serial->minor);
1590         DPRINTK("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1591         DPRINTK("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1592         DPRINTK("Interrupt endpoint is %d \n",
1593                 port->interrupt_in_endpointAddress);
1594         DPRINTK("port's number in the device is %d\n", ATEN2011_port->port_num);
1595         ATEN2011_port->bulk_in_buffer = port->bulk_in_buffer;
1596         ATEN2011_port->bulk_in_endpoint = port->bulk_in_endpointAddress;
1597         ATEN2011_port->read_urb = port->read_urb;
1598         ATEN2011_port->bulk_out_endpoint = port->bulk_out_endpointAddress;
1599
1600         minor = port->serial->minor;
1601         if (minor == SERIAL_TTY_NO_MINOR)
1602                 minor = 0;
1603
1604         /* set up our bulk in urb */
1605         if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1606             && (((__u16) port->number - (__u16) (minor)) != 0)) {
1607                 usb_fill_bulk_urb(ATEN2011_port->read_urb, serial->dev,
1608                                   usb_rcvbulkpipe(serial->dev,
1609                                                   (port->
1610                                                    bulk_in_endpointAddress +
1611                                                    2)), port->bulk_in_buffer,
1612                                   ATEN2011_port->read_urb->
1613                                   transfer_buffer_length,
1614                                   ATEN2011_bulk_in_callback, ATEN2011_port);
1615         } else
1616                 usb_fill_bulk_urb(ATEN2011_port->read_urb,
1617                                   serial->dev,
1618                                   usb_rcvbulkpipe(serial->dev,
1619                                                   port->
1620                                                   bulk_in_endpointAddress),
1621                                   port->bulk_in_buffer,
1622                                   ATEN2011_port->read_urb->
1623                                   transfer_buffer_length,
1624                                   ATEN2011_bulk_in_callback, ATEN2011_port);
1625
1626         DPRINTK("ATEN2011_open: bulkin endpoint is %d\n",
1627                 port->bulk_in_endpointAddress);
1628         response = usb_submit_urb(ATEN2011_port->read_urb, GFP_KERNEL);
1629         if (response) {
1630                 err("%s - Error %d submitting control urb", __FUNCTION__,
1631                     response);
1632         }
1633
1634         /* initialize our wait queues */
1635         init_waitqueue_head(&ATEN2011_port->wait_open);
1636         init_waitqueue_head(&ATEN2011_port->wait_chase);
1637         init_waitqueue_head(&ATEN2011_port->delta_msr_wait);
1638         init_waitqueue_head(&ATEN2011_port->wait_command);
1639
1640         /* initialize our icount structure */
1641         memset(&(ATEN2011_port->icount), 0x00, sizeof(ATEN2011_port->icount));
1642
1643         /* initialize our port settings */
1644         ATEN2011_port->shadowMCR = MCR_MASTER_IE;       /* Must set to enable ints! */
1645         ATEN2011_port->chaseResponsePending = FALSE;
1646         /* send a open port command */
1647         ATEN2011_port->openPending = FALSE;
1648         ATEN2011_port->open = TRUE;
1649         //ATEN2011_change_port_settings(ATEN2011_port,old_termios);
1650         /* Setup termios */
1651 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1652         ATEN2011_set_termios(tty, port, &tmp_termios);
1653 #else
1654         ATEN2011_set_termios(port, &tmp_termios);
1655 #endif
1656         ATEN2011_port->rxBytesAvail = 0x0;
1657         ATEN2011_port->icount.tx = 0;
1658         ATEN2011_port->icount.rx = 0;
1659
1660         DPRINTK
1661             ("\n\nusb_serial serial:%x       ATEN2011_port:%x\nATEN2011_serial:%x      usb_serial_port port:%x\n\n",
1662              (unsigned int)serial, (unsigned int)ATEN2011_port,
1663              (unsigned int)ATEN2011_serial, (unsigned int)port);
1664
1665         return 0;
1666
1667 }
1668
1669 /*****************************************************************************
1670  * ATEN2011_close
1671  *      this function is called by the tty driver when a port is closed
1672  *****************************************************************************/
1673
1674 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1675 static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port,
1676                            struct file *filp)
1677 #else
1678 static void ATEN2011_close(struct usb_serial_port *port, struct file *filp)
1679 #endif
1680 {
1681         struct usb_serial *serial;
1682         struct ATENINTL_serial *ATEN2011_serial;
1683         struct ATENINTL_port *ATEN2011_port;
1684         int no_urbs;
1685         __u16 Data;
1686         //__u16   Data1= 20;
1687
1688         DPRINTK("%s\n", "ATEN2011_close:entering...");
1689         /* MATRIX  */
1690         //ThreadState = 1;
1691         /* MATRIX  */
1692         //printk("Entering... :ATEN2011_close\n");
1693         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1694                 DPRINTK("%s", "Port Paranoia failed \n");
1695                 return;
1696         }
1697         serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1698         if (!serial) {
1699                 DPRINTK("%s", "Serial Paranoia failed \n");
1700                 return;
1701         }
1702         // take the Adpater and port's private data
1703         ATEN2011_serial = ATEN2011_get_serial_private(serial);
1704         ATEN2011_port = ATEN2011_get_port_private(port);
1705         if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1706                 return;
1707         }
1708         if (serial->dev) {
1709                 /* flush and block(wait) until tx is empty */
1710 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1711                 ATEN2011_block_until_tx_empty(tty, ATEN2011_port);
1712 #else
1713                 ATEN2011_block_until_tx_empty(ATEN2011_port);
1714 #endif
1715         }
1716         // kill the ports URB's
1717         for (no_urbs = 0; no_urbs < NUM_URBS; no_urbs++)
1718                 usb_kill_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1719         /* Freeing Write URBs */
1720         for (no_urbs = 0; no_urbs < NUM_URBS; ++no_urbs) {
1721                 if (ATEN2011_port->write_urb_pool[no_urbs]) {
1722                         if (ATEN2011_port->write_urb_pool[no_urbs]->
1723                             transfer_buffer)
1724                                 kfree(ATEN2011_port->write_urb_pool[no_urbs]->
1725                                       transfer_buffer);
1726                         usb_free_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1727                 }
1728         }
1729         /* While closing port, shutdown all bulk read, write  *
1730          * and interrupt read if they exists                  */
1731         if (serial->dev) {
1732                 if (ATEN2011_port->write_urb) {
1733                         DPRINTK("%s", "Shutdown bulk write\n");
1734                         usb_kill_urb(ATEN2011_port->write_urb);
1735                 }
1736                 if (ATEN2011_port->read_urb) {
1737                         DPRINTK("%s", "Shutdown bulk read\n");
1738                         usb_kill_urb(ATEN2011_port->read_urb);
1739                 }
1740                 if ((&ATEN2011_port->control_urb)) {
1741                         DPRINTK("%s", "Shutdown control read\n");
1742                         //      usb_kill_urb (ATEN2011_port->control_urb);
1743
1744                 }
1745         }
1746         //if(ATEN2011_port->ctrl_buf != NULL)
1747         //kfree(ATEN2011_port->ctrl_buf);
1748         // decrement the no.of open ports counter of an individual USB-serial adapter.
1749         ATEN2011_serial->NoOfOpenPorts--;
1750         DPRINTK("NoOfOpenPorts in close%d:in port%d\n",
1751                 ATEN2011_serial->NoOfOpenPorts, port->number);
1752         //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
1753         if (ATEN2011_serial->NoOfOpenPorts == 0) {
1754                 //stop the stus polling here
1755                 //printk("disabling the status polling flag to FALSE :\n");
1756                 ATEN2011_serial->status_polling_started = FALSE;
1757                 if (ATEN2011_serial->interrupt_read_urb) {
1758                         DPRINTK("%s", "Shutdown interrupt_read_urb\n");
1759                         //ATEN2011_serial->interrupt_in_buffer=NULL;
1760                         //usb_kill_urb (ATEN2011_serial->interrupt_read_urb);
1761                 }
1762         }
1763         if (ATEN2011_port->write_urb) {
1764                 /* if this urb had a transfer buffer already (old tx) free it */
1765                 if (ATEN2011_port->write_urb->transfer_buffer != NULL) {
1766                         kfree(ATEN2011_port->write_urb->transfer_buffer);
1767                 }
1768                 usb_free_urb(ATEN2011_port->write_urb);
1769         }
1770         // clear the MCR & IER
1771         Data = 0x00;
1772         ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1773         Data = 0x00;
1774         ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1775
1776         //ATEN2011_get_Uart_Reg(port,MODEM_CONTROL_REGISTER,&Data1);
1777         //printk("value of MCR after closing the port is : 0x%x\n",Data1);
1778
1779         ATEN2011_port->open = FALSE;
1780         ATEN2011_port->closePending = FALSE;
1781         ATEN2011_port->openPending = FALSE;
1782         DPRINTK("%s \n", "Leaving ............");
1783
1784 }
1785
1786 /*****************************************************************************
1787  * SerialBreak
1788  *      this function sends a break to the port
1789  *****************************************************************************/
1790 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1791 static void ATEN2011_break(struct tty_struct *tty, int break_state)
1792 #else
1793 static void ATEN2011_break(struct usb_serial_port *port, int break_state)
1794 #endif
1795 {
1796 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1797         struct usb_serial_port *port = tty->driver_data;
1798 #endif
1799         unsigned char data;
1800         struct usb_serial *serial;
1801         struct ATENINTL_serial *ATEN2011_serial;
1802         struct ATENINTL_port *ATEN2011_port;
1803
1804         DPRINTK("%s \n", "Entering ...........");
1805         DPRINTK("ATEN2011_break: Start\n");
1806
1807         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1808                 DPRINTK("%s", "Port Paranoia failed \n");
1809                 return;
1810         }
1811
1812         serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1813         if (!serial) {
1814                 DPRINTK("%s", "Serial Paranoia failed \n");
1815                 return;
1816         }
1817
1818         ATEN2011_serial = ATEN2011_get_serial_private(serial);
1819         ATEN2011_port = ATEN2011_get_port_private(port);
1820
1821         if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1822                 return;
1823         }
1824
1825         /* flush and chase */
1826         ATEN2011_port->chaseResponsePending = TRUE;
1827
1828         if (serial->dev) {
1829
1830                 /* flush and block until tx is empty */
1831 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1832                 ATEN2011_block_until_chase_response(tty, ATEN2011_port);
1833 #else
1834                 ATEN2011_block_until_chase_response(ATEN2011_port);
1835 #endif
1836         }
1837
1838         if (break_state == -1) {
1839                 data = ATEN2011_port->shadowLCR | LCR_SET_BREAK;
1840         } else {
1841                 data = ATEN2011_port->shadowLCR & ~LCR_SET_BREAK;
1842         }
1843
1844         ATEN2011_port->shadowLCR = data;
1845         DPRINTK("ATEN2011_break ATEN2011_port->shadowLCR is %x\n",
1846                 ATEN2011_port->shadowLCR);
1847         ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER,
1848                               ATEN2011_port->shadowLCR);
1849
1850         return;
1851 }
1852
1853 /************************************************************************
1854  *
1855  * ATEN2011_block_until_chase_response
1856  *
1857  *      This function will block the close until one of the following:
1858  *              1. Response to our Chase comes from ATEN2011
1859  *              2. A timout of 10 seconds without activity has expired
1860  *                 (1K of ATEN2011 data @ 2400 baud ==> 4 sec to empty)
1861  *
1862  ************************************************************************/
1863
1864 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1865 static void ATEN2011_block_until_chase_response(struct tty_struct *tty,
1866                                                 struct ATENINTL_port
1867                                                 *ATEN2011_port)
1868 #else
1869 static void ATEN2011_block_until_chase_response(struct ATENINTL_port
1870                                                 *ATEN2011_port)
1871 #endif
1872 {
1873         int timeout = 1 * HZ;
1874         int wait = 10;
1875         int count;
1876
1877         while (1) {
1878 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1879                 count = ATEN2011_chars_in_buffer(tty);
1880 #else
1881                 count = ATEN2011_chars_in_buffer(ATEN2011_port->port);
1882 #endif
1883
1884                 /* Check for Buffer status */
1885                 if (count <= 0) {
1886                         ATEN2011_port->chaseResponsePending = FALSE;
1887                         return;
1888                 }
1889
1890                 /* Block the thread for a while */
1891                 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1892                                                timeout);
1893                 /* No activity.. count down section */
1894                 wait--;
1895                 if (wait == 0) {
1896                         dbg("%s - TIMEOUT", __FUNCTION__);
1897                         return;
1898                 } else {
1899                         /* Reset timout value back to seconds */
1900                         wait = 10;
1901                 }
1902         }
1903
1904 }
1905
1906 /************************************************************************
1907  *
1908  * ATEN2011_block_until_tx_empty
1909  *
1910  *      This function will block the close until one of the following:
1911  *              1. TX count are 0
1912  *              2. The ATEN2011 has stopped
1913  *              3. A timout of 3 seconds without activity has expired
1914  *
1915  ************************************************************************/
1916 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1917 static void ATEN2011_block_until_tx_empty(struct tty_struct *tty,
1918                                           struct ATENINTL_port *ATEN2011_port)
1919 #else
1920 static void ATEN2011_block_until_tx_empty(struct ATENINTL_port *ATEN2011_port)
1921 #endif
1922 {
1923         int timeout = HZ / 10;
1924         int wait = 30;
1925         int count;
1926
1927         while (1) {
1928
1929 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1930                 count = ATEN2011_chars_in_buffer(tty);
1931 #else
1932                 count = ATEN2011_chars_in_buffer(ATEN2011_port->port);
1933 #endif
1934
1935                 /* Check for Buffer status */
1936                 if (count <= 0) {
1937                         return;
1938                 }
1939
1940                 /* Block the thread for a while */
1941                 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1942                                                timeout);
1943
1944                 /* No activity.. count down section */
1945                 wait--;
1946                 if (wait == 0) {
1947                         dbg("%s - TIMEOUT", __FUNCTION__);
1948                         return;
1949                 } else {
1950                         /* Reset timout value back to seconds */
1951                         wait = 30;
1952                 }
1953         }
1954 }
1955
1956 /*****************************************************************************
1957  * ATEN2011_write_room
1958  *      this function is called by the tty driver when it wants to know how many
1959  *      bytes of data we can accept for a specific port.
1960  *      If successful, we return the amount of room that we have for this port
1961  *      Otherwise we return a negative error number.
1962  *****************************************************************************/
1963
1964 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1965 static int ATEN2011_write_room(struct tty_struct *tty)
1966 #else
1967 static int ATEN2011_write_room(struct usb_serial_port *port)
1968 #endif
1969 {
1970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
1971         struct usb_serial_port *port = tty->driver_data;
1972 #endif
1973         int i;
1974         int room = 0;
1975         struct ATENINTL_port *ATEN2011_port;
1976
1977 //      DPRINTK("%s \n"," ATEN2011_write_room:entering ...........");
1978
1979         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1980                 DPRINTK("%s", "Invalid port \n");
1981                 DPRINTK("%s \n", " ATEN2011_write_room:leaving ...........");
1982                 return -1;
1983         }
1984
1985         ATEN2011_port = ATEN2011_get_port_private(port);
1986         if (ATEN2011_port == NULL) {
1987                 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1988                 return -1;
1989         }
1990
1991         for (i = 0; i < NUM_URBS; ++i) {
1992                 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1993                         room += URB_TRANSFER_BUFFER_SIZE;
1994                 }
1995         }
1996
1997         dbg("%s - returns %d", __FUNCTION__, room);
1998         return (room);
1999
2000 }
2001
2002 /*****************************************************************************
2003  * ATEN2011_chars_in_buffer
2004  *      this function is called by the tty driver when it wants to know how many
2005  *      bytes of data we currently have outstanding in the port (data that has
2006  *      been written, but hasn't made it out the port yet)
2007  *      If successful, we return the number of bytes left to be written in the
2008  *      system,
2009  *      Otherwise we return a negative error number.
2010  *****************************************************************************/
2011
2012 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2013 static int ATEN2011_chars_in_buffer(struct tty_struct *tty)
2014 #else
2015 static int ATEN2011_chars_in_buffer(struct usb_serial_port *port)
2016 #endif
2017 {
2018 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2019         struct usb_serial_port *port = tty->driver_data;
2020 #endif
2021         int i;
2022         int chars = 0;
2023         struct ATENINTL_port *ATEN2011_port;
2024
2025         //DPRINTK("%s \n"," ATEN2011_chars_in_buffer:entering ...........");
2026
2027         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2028                 DPRINTK("%s", "Invalid port \n");
2029                 return -1;
2030         }
2031
2032         ATEN2011_port = ATEN2011_get_port_private(port);
2033         if (ATEN2011_port == NULL) {
2034                 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
2035                 return -1;
2036         }
2037
2038         for (i = 0; i < NUM_URBS; ++i) {
2039                 if (ATEN2011_port->write_urb_pool[i]->status == -EINPROGRESS) {
2040                         chars += URB_TRANSFER_BUFFER_SIZE;
2041                 }
2042         }
2043         dbg("%s - returns %d", __FUNCTION__, chars);
2044         return (chars);
2045
2046 }
2047
2048 /*****************************************************************************
2049  * SerialWrite
2050  *      this function is called by the tty driver when data should be written to
2051  *      the port.
2052  *      If successful, we return the number of bytes written, otherwise we
2053  *      return a negative error number.
2054  *****************************************************************************/
2055
2056 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2057 static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port,
2058                           const unsigned char *data, int count)
2059 #else
2060 static int ATEN2011_write(struct usb_serial_port *port,
2061                           const unsigned char *data, int count)
2062 #endif
2063 {
2064         int status;
2065         int i;
2066         int bytes_sent = 0;
2067         int transfer_size;
2068         int from_user = 0;
2069         int minor;
2070
2071         struct ATENINTL_port *ATEN2011_port;
2072         struct usb_serial *serial;
2073         struct ATENINTL_serial *ATEN2011_serial;
2074         struct urb *urb;
2075         //__u16 Data;
2076         const unsigned char *current_position = data;
2077         unsigned char *data1;
2078         DPRINTK("%s \n", "entering ...........");
2079         //DPRINTK("ATEN2011_write: ATEN2011_port->shadowLCR is %x\n",ATEN2011_port->shadowLCR);
2080
2081 #ifdef NOTATEN2011
2082         Data = 0x00;
2083         status = 0;
2084         status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
2085         ATEN2011_port->shadowLCR = Data;
2086         DPRINTK("ATEN2011_write: LINE_CONTROL_REGISTER is %x\n", Data);
2087         DPRINTK("ATEN2011_write: ATEN2011_port->shadowLCR is %x\n",
2088                 ATEN2011_port->shadowLCR);
2089
2090         //Data = 0x03;
2091         //status = ATEN2011_set_Uart_Reg(port,LINE_CONTROL_REGISTER,Data);
2092         //ATEN2011_port->shadowLCR=Data;//Need to add later
2093
2094         Data |= SERIAL_LCR_DLAB;        //data latch enable in LCR 0x80
2095         status = 0;
2096         status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2097
2098         //Data = 0x0c;
2099         //status = ATEN2011_set_Uart_Reg(port,DIVISOR_LATCH_LSB,Data);
2100         Data = 0x00;
2101         status = 0;
2102         status = ATEN2011_get_Uart_Reg(port, DIVISOR_LATCH_LSB, &Data);
2103         DPRINTK("ATEN2011_write:DLL value is %x\n", Data);
2104
2105         Data = 0x0;
2106         status = 0;
2107         status = ATEN2011_get_Uart_Reg(port, DIVISOR_LATCH_MSB, &Data);
2108         DPRINTK("ATEN2011_write:DLM value is %x\n", Data);
2109
2110         Data = Data & ~SERIAL_LCR_DLAB;
2111         DPRINTK("ATEN2011_write: ATEN2011_port->shadowLCR is %x\n",
2112                 ATEN2011_port->shadowLCR);
2113         status = 0;
2114         status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2115 #endif
2116
2117         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2118                 DPRINTK("%s", "Port Paranoia failed \n");
2119                 return -1;
2120         }
2121
2122         serial = port->serial;
2123         if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
2124                 DPRINTK("%s", "Serial Paranoia failed \n");
2125                 return -1;
2126         }
2127
2128         ATEN2011_port = ATEN2011_get_port_private(port);
2129         if (ATEN2011_port == NULL) {
2130                 DPRINTK("%s", "ATEN2011_port is NULL\n");
2131                 return -1;
2132         }
2133
2134         ATEN2011_serial = ATEN2011_get_serial_private(serial);
2135         if (ATEN2011_serial == NULL) {
2136                 DPRINTK("%s", "ATEN2011_serial is NULL \n");
2137                 return -1;
2138         }
2139
2140         /* try to find a free urb in the list */
2141         urb = NULL;
2142
2143         for (i = 0; i < NUM_URBS; ++i) {
2144                 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
2145                         urb = ATEN2011_port->write_urb_pool[i];
2146                         DPRINTK("\nURB:%d", i);
2147                         break;
2148                 }
2149         }
2150
2151         if (urb == NULL) {
2152                 dbg("%s - no more free urbs", __FUNCTION__);
2153                 goto exit;
2154         }
2155
2156         if (urb->transfer_buffer == NULL) {
2157                 urb->transfer_buffer =
2158                     kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
2159
2160                 if (urb->transfer_buffer == NULL) {
2161                         err("%s no more kernel memory...", __FUNCTION__);
2162                         goto exit;
2163                 }
2164         }
2165         transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
2166
2167         if (from_user) {
2168                 if (copy_from_user
2169                     (urb->transfer_buffer, current_position, transfer_size)) {
2170                         bytes_sent = -EFAULT;
2171                         goto exit;
2172                 }
2173         } else {
2174                 memcpy(urb->transfer_buffer, current_position, transfer_size);
2175         }
2176         //usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
2177
2178         /* fill urb with data and submit  */
2179         minor = port->serial->minor;
2180         if (minor == SERIAL_TTY_NO_MINOR) ;
2181         minor = 0;
2182         if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
2183             && (((__u16) port->number - (__u16) (minor)) != 0)) {
2184                 usb_fill_bulk_urb(urb, ATEN2011_serial->serial->dev,
2185                                   usb_sndbulkpipe(ATEN2011_serial->serial->dev,
2186                                                   (port->
2187                                                    bulk_out_endpointAddress) +
2188                                                   2), urb->transfer_buffer,
2189                                   transfer_size,
2190                                   ATEN2011_bulk_out_data_callback,
2191                                   ATEN2011_port);
2192         } else
2193
2194                 usb_fill_bulk_urb(urb,
2195                                   ATEN2011_serial->serial->dev,
2196                                   usb_sndbulkpipe(ATEN2011_serial->serial->dev,
2197                                                   port->
2198                                                   bulk_out_endpointAddress),
2199                                   urb->transfer_buffer, transfer_size,
2200                                   ATEN2011_bulk_out_data_callback,
2201                                   ATEN2011_port);
2202
2203         data1 = urb->transfer_buffer;
2204         DPRINTK("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
2205         //for(i=0;i < urb->actual_length;i++)
2206         //      DPRINTK("Data is %c\n ",data1[i]);
2207
2208         /* send it down the pipe */
2209         status = usb_submit_urb(urb, GFP_ATOMIC);
2210
2211         if (status) {
2212                 err("%s - usb_submit_urb(write bulk) failed with status = %d",
2213                     __FUNCTION__, status);
2214                 bytes_sent = status;
2215                 goto exit;
2216         }
2217         bytes_sent = transfer_size;
2218         ATEN2011_port->icount.tx += transfer_size;
2219         DPRINTK("ATEN2011_port->icount.tx is %d:\n", ATEN2011_port->icount.tx);
2220       exit:
2221
2222         return bytes_sent;
2223
2224 }
2225
2226 /*****************************************************************************
2227  * SerialThrottle
2228  *      this function is called by the tty driver when it wants to stop the data
2229  *      being read from the port.
2230  *****************************************************************************/
2231
2232 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2233 static void ATEN2011_throttle(struct tty_struct *tty)
2234 #else
2235 static void ATEN2011_throttle(struct usb_serial_port *port)
2236 #endif
2237 {
2238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2239         struct usb_serial_port *port = tty->driver_data;
2240 #else
2241         struct tty_struct *tty;
2242 #endif
2243         struct ATENINTL_port *ATEN2011_port;
2244         int status;
2245
2246         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2247                 DPRINTK("%s", "Invalid port \n");
2248                 return;
2249         }
2250
2251         DPRINTK("- port %d\n", port->number);
2252
2253         ATEN2011_port = ATEN2011_get_port_private(port);
2254
2255         if (ATEN2011_port == NULL)
2256                 return;
2257
2258         if (!ATEN2011_port->open) {
2259                 DPRINTK("%s\n", "port not opened");
2260                 return;
2261         }
2262
2263         DPRINTK("%s", "Entering .......... \n");
2264
2265 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2266         tty = port->tty;
2267 #endif
2268         if (!tty) {
2269                 dbg("%s - no tty available", __FUNCTION__);
2270                 return;
2271         }
2272
2273         /* if we are implementing XON/XOFF, send the stop character */
2274         if (I_IXOFF(tty)) {
2275                 unsigned char stop_char = STOP_CHAR(tty);
2276 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2277                 status = ATEN2011_write(tty, port, &stop_char, 1);      //FC4
2278 #else
2279                 status = ATEN2011_write(port, &stop_char, 1);   //FC4
2280 #endif
2281                 if (status <= 0) {
2282                         return;
2283                 }
2284         }
2285
2286         /* if we are implementing RTS/CTS, toggle that line */
2287         if (tty->termios->c_cflag & CRTSCTS) {
2288                 ATEN2011_port->shadowMCR &= ~MCR_RTS;
2289                 status = 0;
2290                 status =
2291                     ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
2292                                           ATEN2011_port->shadowMCR);
2293
2294                 if (status < 0) {
2295                         return;
2296                 }
2297         }
2298
2299         return;
2300 }
2301
2302 /*****************************************************************************
2303  * ATEN2011_unthrottle
2304  *      this function is called by the tty driver when it wants to resume the data
2305  *      being read from the port (called after SerialThrottle is called)
2306  *****************************************************************************/
2307 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2308 static void ATEN2011_unthrottle(struct tty_struct *tty)
2309 #else
2310 static void ATEN2011_unthrottle(struct usb_serial_port *port)
2311 #endif
2312 {
2313 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2314         struct usb_serial_port *port = tty->driver_data;
2315 #else
2316         struct tty_struct *tty;
2317 #endif
2318         int status;
2319         struct ATENINTL_port *ATEN2011_port = ATEN2011_get_port_private(port);
2320
2321         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2322                 DPRINTK("%s", "Invalid port \n");
2323                 return;
2324         }
2325
2326         if (ATEN2011_port == NULL)
2327                 return;
2328
2329         if (!ATEN2011_port->open) {
2330                 dbg("%s - port not opened", __FUNCTION__);
2331                 return;
2332         }
2333
2334         DPRINTK("%s", "Entering .......... \n");
2335
2336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2337         tty = port->tty;
2338 #endif
2339         if (!tty) {
2340                 dbg("%s - no tty available", __FUNCTION__);
2341                 return;
2342         }
2343
2344         /* if we are implementing XON/XOFF, send the start character */
2345         if (I_IXOFF(tty)) {
2346                 unsigned char start_char = START_CHAR(tty);
2347 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2348                 status = ATEN2011_write(tty, port, &start_char, 1);     //FC4
2349 #else
2350                 status = ATEN2011_write(port, &start_char, 1);  //FC4
2351 #endif
2352                 if (status <= 0) {
2353                         return;
2354                 }
2355         }
2356
2357         /* if we are implementing RTS/CTS, toggle that line */
2358         if (tty->termios->c_cflag & CRTSCTS) {
2359                 ATEN2011_port->shadowMCR |= MCR_RTS;
2360                 status = 0;
2361                 status =
2362                     ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
2363                                           ATEN2011_port->shadowMCR);
2364                 if (status < 0) {
2365                         return;
2366                 }
2367         }
2368
2369         return;
2370 }
2371
2372 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2373 static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file)
2374 #else
2375 static int ATEN2011_tiocmget(struct usb_serial_port *port, struct file *file)
2376 #endif
2377 {
2378 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2379         struct usb_serial_port *port = tty->driver_data;
2380 #endif
2381         //struct ti_port *tport = usb_get_serial_port_data(port);
2382         struct ATENINTL_port *ATEN2011_port;
2383         unsigned int result;
2384         __u16 msr;
2385         __u16 mcr;
2386         //unsigned int mcr;
2387         int status = 0;
2388         ATEN2011_port = ATEN2011_get_port_private(port);
2389
2390         DPRINTK("%s - port %d", __FUNCTION__, port->number);
2391
2392         if (ATEN2011_port == NULL)
2393                 return -ENODEV;
2394
2395         status = ATEN2011_get_Uart_Reg(port, MODEM_STATUS_REGISTER, &msr);
2396         status = ATEN2011_get_Uart_Reg(port, MODEM_CONTROL_REGISTER, &mcr);
2397 //        mcr = ATEN2011_port->shadowMCR;
2398 // COMMENT2: the Fallowing three line are commented for updating only MSR values
2399         result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
2400             | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
2401             | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
2402             | ((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0)
2403             | ((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0)
2404             | ((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0)
2405             | ((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0);
2406
2407         DPRINTK("%s - 0x%04X", __FUNCTION__, result);
2408
2409         return result;
2410 }
2411
2412 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2413 static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
2414                              unsigned int set, unsigned int clear)
2415 #else
2416 static int ATEN2011_tiocmset(struct usb_serial_port *port, struct file *file,
2417                              unsigned int set, unsigned int clear)
2418 #endif
2419 {
2420 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2421         struct usb_serial_port *port = tty->driver_data;
2422 #endif
2423         struct ATENINTL_port *ATEN2011_port;
2424         //struct ti_port *tport = usb_get_serial_port_data(port);
2425         unsigned int mcr;
2426         unsigned int status;
2427
2428         DPRINTK("%s - port %d", __FUNCTION__, port->number);
2429
2430         ATEN2011_port = ATEN2011_get_port_private(port);
2431
2432         if (ATEN2011_port == NULL)
2433                 return -ENODEV;
2434
2435         mcr = ATEN2011_port->shadowMCR;
2436         if (clear & TIOCM_RTS)
2437                 mcr &= ~MCR_RTS;
2438         if (clear & TIOCM_DTR)
2439                 mcr &= ~MCR_DTR;
2440         if (clear & TIOCM_LOOP)
2441                 mcr &= ~MCR_LOOPBACK;
2442
2443         if (set & TIOCM_RTS)
2444                 mcr |= MCR_RTS;
2445         if (set & TIOCM_DTR)
2446                 mcr |= MCR_DTR;
2447         if (set & TIOCM_LOOP)
2448                 mcr |= MCR_LOOPBACK;
2449
2450         ATEN2011_port->shadowMCR = mcr;
2451
2452         status = 0;
2453         status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, mcr);
2454         if (status < 0) {
2455                 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
2456                 return -1;
2457         }
2458
2459         return 0;
2460 }
2461
2462 /*****************************************************************************
2463  * SerialSetTermios
2464  *      this function is called by the tty driver when it wants to change the termios structure
2465  *****************************************************************************/
2466
2467 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2468 static void ATEN2011_set_termios(struct tty_struct *tty,
2469                                  struct usb_serial_port *port,
2470                                  struct ktermios *old_termios)
2471 #else
2472 static void ATEN2011_set_termios(struct usb_serial_port *port,
2473                                  struct ktermios *old_termios)
2474 #endif
2475 {
2476         int status;
2477         unsigned int cflag;
2478         struct usb_serial *serial;
2479         struct ATENINTL_port *ATEN2011_port;
2480 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2481         struct tty_struct *tty;
2482 #endif
2483         DPRINTK("ATEN2011_set_termios: START\n");
2484         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2485                 DPRINTK("%s", "Invalid port \n");
2486                 return;
2487         }
2488
2489         serial = port->serial;
2490
2491         if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
2492                 DPRINTK("%s", "Invalid Serial \n");
2493                 return;
2494         }
2495
2496         ATEN2011_port = ATEN2011_get_port_private(port);
2497
2498         if (ATEN2011_port == NULL)
2499                 return;
2500
2501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2502         tty = port->tty;
2503
2504         if (!port->tty || !port->tty->termios) {
2505                 dbg("%s - no tty or termios", __FUNCTION__);
2506                 return;
2507         }
2508 #endif
2509
2510         if (!ATEN2011_port->open) {
2511                 dbg("%s - port not opened", __FUNCTION__);
2512                 return;
2513         }
2514
2515         DPRINTK("%s\n", "setting termios - ");
2516
2517         cflag = tty->termios->c_cflag;
2518
2519         if (!cflag) {
2520                 DPRINTK("%s %s\n", __FUNCTION__, "cflag is NULL");
2521                 return;
2522         }
2523
2524         /* check that they really want us to change something */
2525         if (old_termios) {
2526                 if ((cflag == old_termios->c_cflag) &&
2527                     (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2528                      RELEVANT_IFLAG(old_termios->c_iflag))) {
2529                         DPRINTK("%s\n", "Nothing to change");
2530                         return;
2531                 }
2532         }
2533
2534         dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2535             tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2536
2537         if (old_termios) {
2538                 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2539                     old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2540         }
2541
2542         dbg("%s - port %d", __FUNCTION__, port->number);
2543
2544         /* change the port settings to the new ones specified */
2545
2546 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2547         ATEN2011_change_port_settings(tty, ATEN2011_port, old_termios);
2548 #else
2549         ATEN2011_change_port_settings(ATEN2011_port, old_termios);
2550 #endif
2551
2552         if (!ATEN2011_port->read_urb) {
2553                 DPRINTK("%s", "URB KILLED !!!!!\n");
2554                 return;
2555         }
2556
2557         if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
2558                 ATEN2011_port->read_urb->dev = serial->dev;
2559                 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
2560                 if (status) {
2561                         DPRINTK
2562                             (" usb_submit_urb(read bulk) failed, status = %d",
2563                              status);
2564                 }
2565         }
2566         return;
2567 }
2568
2569 /*
2570 static void ATEN2011_break_ctl( struct usb_serial_port *port, int break_state )
2571 {
2572         //struct usb_serial *serial = port->serial;
2573
2574 //        if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
2575   //              err("Set break_ctl %d", break_state);
2576 }
2577 */
2578
2579 /*****************************************************************************
2580  * get_lsr_info - get line status register info
2581  *
2582  * Purpose: Let user call ioctl() to get info when the UART physically
2583  *          is emptied.  On bus types like RS485, the transmitter must
2584  *          release the bus after transmitting. This must be done when
2585  *          the transmit shift register is empty, not be done when the
2586  *          transmit holding register is empty.  This functionality
2587  *          allows an RS485 driver to be written in user space.
2588  *****************************************************************************/
2589
2590 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2591 static int get_lsr_info(struct tty_struct *tty,
2592                         struct ATENINTL_port *ATEN2011_port,
2593                         unsigned int *value)
2594 #else
2595 static int get_lsr_info(struct ATENINTL_port *ATEN2011_port,
2596                         unsigned int *value)
2597 #endif
2598 {
2599         int count;
2600         unsigned int result = 0;
2601
2602 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2603         count = ATEN2011_chars_in_buffer(tty);
2604 #else
2605         count = ATEN2011_chars_in_buffer(ATEN2011_port->port);
2606 #endif
2607         if (count == 0) {
2608                 dbg("%s -- Empty", __FUNCTION__);
2609                 result = TIOCSER_TEMT;
2610         }
2611
2612         if (copy_to_user(value, &result, sizeof(int)))
2613                 return -EFAULT;
2614         return 0;
2615 }
2616
2617 /*****************************************************************************
2618  * get_number_bytes_avail - get number of bytes available
2619  *
2620  * Purpose: Let user call ioctl to get the count of number of bytes available.
2621  *****************************************************************************/
2622
2623 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2624 static int get_number_bytes_avail(struct tty_struct *tty,
2625                                   struct ATENINTL_port *ATEN2011_port,
2626                                   unsigned int *value)
2627 #else
2628 static int get_number_bytes_avail(struct ATENINTL_port *ATEN2011_port,
2629                                   unsigned int *value)
2630 #endif
2631 {
2632         unsigned int result = 0;
2633 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2634         struct tty_struct *tty = ATEN2011_port->port->tty;
2635 #endif
2636
2637         if (!tty)
2638                 return -ENOIOCTLCMD;
2639
2640         result = tty->read_cnt;
2641
2642         dbg("%s(%d) = %d", __FUNCTION__, ATEN2011_port->port->number, result);
2643         if (copy_to_user(value, &result, sizeof(int)))
2644                 return -EFAULT;
2645
2646         return -ENOIOCTLCMD;
2647 }
2648
2649 /*****************************************************************************
2650  * set_modem_info
2651  *      function to set modem info
2652  *****************************************************************************/
2653
2654 static int set_modem_info(struct ATENINTL_port *ATEN2011_port, unsigned int cmd,
2655                           unsigned int *value)
2656 {
2657         unsigned int mcr;
2658         unsigned int arg;
2659         __u16 Data;
2660         int status;
2661         struct usb_serial_port *port;
2662
2663         if (ATEN2011_port == NULL)
2664                 return -1;
2665
2666         port = (struct usb_serial_port *)ATEN2011_port->port;
2667         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2668                 DPRINTK("%s", "Invalid port \n");
2669                 return -1;
2670         }
2671
2672         mcr = ATEN2011_port->shadowMCR;
2673
2674         if (copy_from_user(&arg, value, sizeof(int)))
2675                 return -EFAULT;
2676
2677         switch (cmd) {
2678         case TIOCMBIS:
2679                 if (arg & TIOCM_RTS)
2680                         mcr |= MCR_RTS;
2681                 if (arg & TIOCM_DTR)
2682                         mcr |= MCR_RTS;
2683                 if (arg & TIOCM_LOOP)
2684                         mcr |= MCR_LOOPBACK;
2685                 break;
2686
2687         case TIOCMBIC:
2688                 if (arg & TIOCM_RTS)
2689                         mcr &= ~MCR_RTS;
2690                 if (arg & TIOCM_DTR)
2691                         mcr &= ~MCR_RTS;
2692                 if (arg & TIOCM_LOOP)
2693                         mcr &= ~MCR_LOOPBACK;
2694                 break;
2695
2696         case TIOCMSET:
2697                 /* turn off the RTS and DTR and LOOPBACK
2698                  * and then only turn on what was asked to */
2699                 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2700                 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2701                 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2702                 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2703                 break;
2704         }
2705
2706         ATEN2011_port->shadowMCR = mcr;
2707
2708         Data = ATEN2011_port->shadowMCR;
2709         status = 0;
2710         status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2711         if (status < 0) {
2712                 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
2713                 return -1;
2714         }
2715
2716         return 0;
2717 }
2718
2719 /*****************************************************************************
2720  * get_modem_info
2721  *      function to get modem info
2722  *****************************************************************************/
2723
2724 static int get_modem_info(struct ATENINTL_port *ATEN2011_port,
2725                           unsigned int *value)
2726 {
2727         unsigned int result = 0;
2728         __u16 msr;
2729         unsigned int mcr = ATEN2011_port->shadowMCR;
2730         int status = 0;
2731         status =
2732             ATEN2011_get_Uart_Reg(ATEN2011_port->port, MODEM_STATUS_REGISTER,
2733                                   &msr);
2734         result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)      /* 0x002 */
2735             |((mcr & MCR_RTS) ? TIOCM_RTS : 0)  /* 0x004 */
2736             |((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2737             |((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0)  /* 0x040 */
2738             |((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0)   /* 0x080 */
2739             |((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0);        /* 0x100 */
2740
2741         dbg("%s -- %x", __FUNCTION__, result);
2742
2743         if (copy_to_user(value, &result, sizeof(int)))
2744                 return -EFAULT;
2745         return 0;
2746 }
2747
2748 /*****************************************************************************
2749  * get_serial_info
2750  *      function to get information about serial port
2751  *****************************************************************************/
2752
2753 static int get_serial_info(struct ATENINTL_port *ATEN2011_port,
2754                            struct serial_struct *retinfo)
2755 {
2756         struct serial_struct tmp;
2757
2758         if (ATEN2011_port == NULL)
2759                 return -1;
2760
2761         if (!retinfo)
2762                 return -EFAULT;
2763
2764         memset(&tmp, 0, sizeof(tmp));
2765
2766         tmp.type = PORT_16550A;
2767         tmp.line = ATEN2011_port->port->serial->minor;
2768         if (tmp.line == SERIAL_TTY_NO_MINOR)
2769                 tmp.line = 0;
2770         tmp.port = ATEN2011_port->port->number;
2771         tmp.irq = 0;
2772         tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2773         tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2774         tmp.baud_base = 9600;
2775         tmp.close_delay = 5 * HZ;
2776         tmp.closing_wait = 30 * HZ;
2777
2778         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2779                 return -EFAULT;
2780         return 0;
2781 }
2782
2783 /*****************************************************************************
2784  * SerialIoctl
2785  *      this function handles any ioctl calls to the driver
2786  *****************************************************************************/
2787
2788 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2789 static int ATEN2011_ioctl(struct tty_struct *tty, struct file *file,
2790                           unsigned int cmd, unsigned long arg)
2791 #else
2792 static int ATEN2011_ioctl(struct usb_serial_port *port, struct file *file,
2793                           unsigned int cmd, unsigned long arg)
2794 #endif
2795 {
2796 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2797         struct usb_serial_port *port = tty->driver_data;
2798 #else
2799         struct tty_struct *tty;
2800 #endif
2801         struct ATENINTL_port *ATEN2011_port;
2802         struct async_icount cnow;
2803         struct async_icount cprev;
2804         struct serial_icounter_struct icount;
2805         int ATENret = 0;
2806         //int retval;
2807         //struct tty_ldisc *ld;
2808
2809         //printk("%s - port %d, cmd = 0x%x\n", __FUNCTION__, port->number, cmd);
2810         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2811                 DPRINTK("%s", "Invalid port \n");
2812                 return -1;
2813         }
2814
2815         ATEN2011_port = ATEN2011_get_port_private(port);
2816 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2817         tty = ATEN2011_port->port->tty;
2818 #endif
2819
2820         if (ATEN2011_port == NULL)
2821                 return -1;
2822
2823         dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2824
2825         switch (cmd) {
2826                 /* return number of bytes available */
2827
2828         case TIOCINQ:
2829                 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2830 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2831                 return get_number_bytes_avail(tty, ATEN2011_port,
2832                                               (unsigned int *)arg);
2833 #else
2834                 return get_number_bytes_avail(ATEN2011_port,
2835                                               (unsigned int *)arg);
2836 #endif
2837                 break;
2838
2839         case TIOCOUTQ:
2840                 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2841 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2842                 return put_user(ATEN2011_chars_in_buffer(tty),
2843                                 (int __user *)arg);
2844 #else
2845                 return put_user(tty->driver->ops->chars_in_buffer ?
2846                                 tty->driver->ops->chars_in_buffer(tty) : 0,
2847                                 (int __user *)arg);
2848 #endif
2849                 break;
2850
2851                 /*  //2.6.17 block
2852                    case TCFLSH:
2853                    retval = tty_check_change(tty);
2854                    if (retval)
2855                    return retval;
2856
2857                    ld = tty_ldisc_ref(tty);
2858                    switch (arg) {
2859                    case TCIFLUSH:
2860                    if (ld && ld->flush_buffer)
2861                    ld->flush_buffer(tty);
2862                    break;
2863                    case TCIOFLUSH:
2864                    if (ld && ld->flush_buffer)
2865                    ld->flush_buffer(tty);
2866                    // fall through
2867                    case TCOFLUSH:
2868                    if (tty->driver->flush_buffer)
2869                    tty->driver->flush_buffer(tty);
2870                    break;
2871                    default:
2872                    tty_ldisc_deref(ld);
2873                    return -EINVAL;
2874                    }
2875                    tty_ldisc_deref(ld);
2876                    return 0;
2877                  */
2878         case TIOCSERGETLSR:
2879                 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
2881                 return get_lsr_info(tty, ATEN2011_port, (unsigned int *)arg);
2882 #else
2883                 return get_lsr_info(ATEN2011_port, (unsigned int *)arg);
2884 #endif
2885                 return 0;
2886
2887         case TIOCMBIS:
2888         case TIOCMBIC:
2889         case TIOCMSET:
2890                 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2891                     port->number);
2892                 //      printk("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,  port->number);
2893                 ATENret =
2894                     set_modem_info(ATEN2011_port, cmd, (unsigned int *)arg);
2895                 //              printk(" %s: ret:%d\n",__FUNCTION__,ATENret);
2896                 return ATENret;
2897
2898         case TIOCMGET:
2899                 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2900                 return get_modem_info(ATEN2011_port, (unsigned int *)arg);
2901
2902         case TIOCGSERIAL:
2903                 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2904                 return get_serial_info(ATEN2011_port,
2905                                        (struct serial_struct *)arg);
2906
2907         case TIOCSSERIAL:
2908                 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2909                 break;
2910
2911         case TIOCMIWAIT:
2912                 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2913                 cprev = ATEN2011_port->icount;
2914                 while (1) {
2915                         //interruptible_sleep_on(&ATEN2011_port->delta_msr_wait);
2916                         // ATEN2011_port->delta_msr_cond=0;
2917                         //wait_event_interruptible(ATEN2011_port->delta_msr_wait,(ATEN2011_port->delta_msr_cond==1));
2918
2919                         /* see if a signal did it */
2920                         if (signal_pending(current))
2921                                 return -ERESTARTSYS;
2922                         cnow = ATEN2011_port->icount;
2923                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2924                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2925                                 return -EIO;    /* no change => error */
2926                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2927                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2928                             ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2929                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2930                                 return 0;
2931                         }
2932                         cprev = cnow;
2933                 }
2934                 /* NOTREACHED */
2935                 break;
2936
2937         case TIOCGICOUNT:
2938                 cnow = ATEN2011_port->icount;
2939                 icount.cts = cnow.cts;
2940                 icount.dsr = cnow.dsr;
2941                 icount.rng = cnow.rng;
2942                 icount.dcd = cnow.dcd;
2943                 icount.rx = cnow.rx;
2944                 icount.tx = cnow.tx;
2945                 icount.frame = cnow.frame;
2946                 icount.overrun = cnow.overrun;
2947                 icount.parity = cnow.parity;
2948                 icount.brk = cnow.brk;
2949                 icount.buf_overrun = cnow.buf_overrun;
2950
2951                 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2952                     port->number, icount.rx, icount.tx);
2953                 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2954                         return -EFAULT;
2955                 return 0;
2956
2957         case TIOCEXBAUD:
2958                 return 0;
2959         default:
2960                 break;
2961         }
2962
2963         return -ENOIOCTLCMD;
2964 }
2965
2966 /*****************************************************************************
2967  * ATEN2011_send_cmd_write_baud_rate
2968  *      this function sends the proper command to change the baud rate of the
2969  *      specified port.
2970  *****************************************************************************/
2971
2972 static int ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port
2973                                              *ATEN2011_port, int baudRate)
2974 {
2975         int divisor = 0;
2976         int status;
2977         __u16 Data;
2978         unsigned char number;
2979         __u16 clk_sel_val;
2980         struct usb_serial_port *port;
2981         int minor;
2982
2983         if (ATEN2011_port == NULL)
2984                 return -1;
2985
2986         port = (struct usb_serial_port *)ATEN2011_port->port;
2987         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2988                 DPRINTK("%s", "Invalid port \n");
2989                 return -1;
2990         }
2991
2992         if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2993                 DPRINTK("%s", "Invalid Serial \n");
2994                 return -1;
2995         }
2996
2997         DPRINTK("%s", "Entering .......... \n");
2998
2999         minor = ATEN2011_port->port->serial->minor;
3000         if (minor == SERIAL_TTY_NO_MINOR)
3001                 minor = 0;
3002         number = ATEN2011_port->port->number - minor;
3003
3004         dbg("%s - port = %d, baud = %d", __FUNCTION__,
3005             ATEN2011_port->port->number, baudRate);
3006         //reset clk_uart_sel in spregOffset
3007         if (baudRate > 115200) {
3008 #ifdef HW_flow_control
3009                 //NOTE: need to see the pther register to modify
3010                 //setting h/w flow control bit to 1;
3011                 status = 0;
3012                 //Data = ATEN2011_port->shadowMCR ;
3013                 Data = 0x2b;
3014                 ATEN2011_port->shadowMCR = Data;
3015                 status =
3016                     ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3017                 if (status < 0) {
3018                         DPRINTK("Writing spreg failed in set_serial_baud\n");
3019                         return -1;
3020                 }
3021 #endif
3022
3023         } else {
3024 #ifdef HW_flow_control
3025                 //setting h/w flow control bit to 0;
3026                 status = 0;
3027                 //Data = ATEN2011_port->shadowMCR ;
3028                 Data = 0xb;
3029                 ATEN2011_port->shadowMCR = Data;
3030                 status =
3031                     ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3032                 if (status < 0) {
3033                         DPRINTK("Writing spreg failed in set_serial_baud\n");
3034                         return -1;
3035                 }
3036 #endif
3037
3038         }
3039
3040         if (1)                  //baudRate <= 115200)
3041         {
3042                 clk_sel_val = 0x0;
3043                 Data = 0x0;
3044                 status = 0;
3045                 status =
3046                     ATEN2011_calc_baud_rate_divisor(baudRate, &divisor,
3047                                                     &clk_sel_val);
3048                 status =
3049                     ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset,
3050                                           &Data);
3051                 if (status < 0) {
3052                         DPRINTK("reading spreg failed in set_serial_baud\n");
3053                         return -1;
3054                 }
3055                 Data = (Data & 0x8f) | clk_sel_val;
3056                 status = 0;
3057                 status =
3058                     ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset,
3059                                           Data);
3060                 if (status < 0) {
3061                         DPRINTK("Writing spreg failed in set_serial_baud\n");
3062                         return -1;
3063                 }
3064                 /* Calculate the Divisor */
3065
3066                 if (status) {
3067                         err("%s - bad baud rate", __FUNCTION__);
3068                         DPRINTK("%s\n", "bad baud rate");
3069                         return status;
3070                 }
3071                 /* Enable access to divisor latch */
3072                 Data = ATEN2011_port->shadowLCR | SERIAL_LCR_DLAB;
3073                 ATEN2011_port->shadowLCR = Data;
3074                 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
3075
3076                 /* Write the divisor */
3077                 Data = LOW8(divisor);   //:  commented to test
3078                 DPRINTK("set_serial_baud Value to write DLL is %x\n", Data);
3079                 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
3080
3081                 Data = HIGH8(divisor);  //:  commented to test
3082                 DPRINTK("set_serial_baud Value to write DLM is %x\n", Data);
3083                 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
3084
3085                 /* Disable access to divisor latch */
3086                 Data = ATEN2011_port->shadowLCR & ~SERIAL_LCR_DLAB;
3087                 ATEN2011_port->shadowLCR = Data;
3088                 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
3089
3090         }
3091
3092         return status;
3093 }
3094
3095 /*****************************************************************************
3096  * ATEN2011_calc_baud_rate_divisor
3097  *      this function calculates the proper baud rate divisor for the specified
3098  *      baud rate.
3099  *****************************************************************************/
3100 static int ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,
3101                                            __u16 * clk_sel_val)
3102 {
3103         //int i;
3104         //__u16 custom,round1, round;
3105
3106         dbg("%s - %d", __FUNCTION__, baudRate);
3107
3108         if (baudRate <= 115200) {
3109                 *divisor = 115200 / baudRate;
3110                 *clk_sel_val = 0x0;
3111         }
3112         if ((baudRate > 115200) && (baudRate <= 230400)) {
3113                 *divisor = 230400 / baudRate;
3114                 *clk_sel_val = 0x10;
3115         } else if ((baudRate > 230400) && (baudRate <= 403200)) {
3116                 *divisor = 403200 / baudRate;
3117                 *clk_sel_val = 0x20;
3118         } else if ((baudRate > 403200) && (baudRate <= 460800)) {
3119                 *divisor = 460800 / baudRate;
3120                 *clk_sel_val = 0x30;
3121         } else if ((baudRate > 460800) && (baudRate <= 806400)) {
3122                 *divisor = 806400 / baudRate;
3123                 *clk_sel_val = 0x40;
3124         } else if ((baudRate > 806400) && (baudRate <= 921600)) {
3125                 *divisor = 921600 / baudRate;
3126                 *clk_sel_val = 0x50;
3127         } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
3128                 *divisor = 1572864 / baudRate;
3129                 *clk_sel_val = 0x60;
3130         } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
3131                 *divisor = 3145728 / baudRate;
3132                 *clk_sel_val = 0x70;
3133         }
3134         return 0;
3135
3136 #ifdef NOTATEN2011
3137
3138         for (i = 0; i < NUM_ENTRIES(ATEN2011_divisor_table); i++) {
3139                 if (ATEN2011_divisor_table[i].BaudRate == baudrate) {
3140                         *divisor = ATEN2011_divisor_table[i].Divisor;
3141                         return 0;
3142                 }
3143         }
3144
3145         /* After trying for all the standard baud rates    *
3146          * Try calculating the divisor for this baud rate  */
3147
3148         if (baudrate > 75 && baudrate < 230400) {
3149                 /* get the divisor */
3150                 custom = (__u16) (230400L / baudrate);
3151
3152                 /* Check for round off */
3153                 round1 = (__u16) (2304000L / baudrate);
3154                 round = (__u16) (round1 - (custom * 10));
3155                 if (round > 4) {
3156                         custom++;
3157                 }
3158                 *divisor = custom;
3159
3160                 DPRINTK(" Baud %d = %d\n", baudrate, custom);
3161                 return 0;
3162         }
3163
3164         DPRINTK("%s\n", " Baud calculation Failed...");
3165         return -1;
3166 #endif
3167 }
3168
3169 /*****************************************************************************
3170  * ATEN2011_change_port_settings
3171  *      This routine is called to set the UART on the device to match
3172  *      the specified new settings.
3173  *****************************************************************************/
3174
3175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
3176 static void ATEN2011_change_port_settings(struct tty_struct *tty,
3177                                           struct ATENINTL_port *ATEN2011_port,
3178                                           struct ktermios *old_termios)
3179 #else
3180 static void ATEN2011_change_port_settings(struct ATENINTL_port *ATEN2011_port,
3181                                           struct ktermios *old_termios)
3182 #endif
3183 {
3184 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
3185         struct tty_struct *tty;
3186 #endif
3187         int baud;
3188         unsigned cflag;
3189         unsigned iflag;
3190         __u8 mask = 0xff;
3191         __u8 lData;
3192         __u8 lParity;
3193         __u8 lStop;
3194         int status;
3195         __u16 Data;
3196         struct usb_serial_port *port;
3197         struct usb_serial *serial;
3198
3199         if (ATEN2011_port == NULL)
3200                 return;
3201
3202         port = (struct usb_serial_port *)ATEN2011_port->port;
3203
3204         if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
3205                 DPRINTK("%s", "Invalid port \n");
3206                 return;
3207         }
3208
3209         if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
3210                 DPRINTK("%s", "Invalid Serial \n");
3211                 return;
3212         }
3213
3214         serial = port->serial;
3215
3216         dbg("%s - port %d", __FUNCTION__, ATEN2011_port->port->number);
3217
3218         if ((!ATEN2011_port->open) && (!ATEN2011_port->openPending)) {
3219                 dbg("%s - port not opened", __FUNCTION__);
3220                 return;
3221         }
3222 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
3223         tty = ATEN2011_port->port->tty;
3224 #endif
3225
3226         if ((!tty) || (!tty->termios)) {
3227                 dbg("%s - no tty structures", __FUNCTION__);
3228                 return;
3229         }
3230
3231         DPRINTK("%s", "Entering .......... \n");
3232
3233         lData = LCR_BITS_8;
3234         lStop = LCR_STOP_1;
3235         lParity = LCR_PAR_NONE;
3236
3237         cflag = tty->termios->c_cflag;
3238         iflag = tty->termios->c_iflag;
3239
3240         /* Change the number of bits */
3241
3242 //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
3243         //if(cflag & CSIZE)
3244         {
3245                 switch (cflag & CSIZE) {
3246                 case CS5:
3247                         lData = LCR_BITS_5;
3248                         mask = 0x1f;
3249                         break;
3250
3251                 case CS6:
3252                         lData = LCR_BITS_6;
3253                         mask = 0x3f;
3254                         break;
3255
3256                 case CS7:
3257                         lData = LCR_BITS_7;
3258                         mask = 0x7f;
3259                         break;
3260                 default:
3261                 case CS8:
3262                         lData = LCR_BITS_8;
3263                         break;
3264                 }
3265         }
3266         /* Change the Parity bit */
3267         if (cflag & PARENB) {
3268                 if (cflag & PARODD) {
3269                         lParity = LCR_PAR_ODD;
3270                         dbg("%s - parity = odd", __FUNCTION__);
3271                 } else {
3272                         lParity = LCR_PAR_EVEN;
3273                         dbg("%s - parity = even", __FUNCTION__);
3274                 }
3275
3276         } else {
3277                 dbg("%s - parity = none", __FUNCTION__);
3278         }
3279
3280         if (cflag & CMSPAR) {
3281                 lParity = lParity | 0x20;
3282         }
3283
3284         /* Change the Stop bit */
3285         if (cflag & CSTOPB) {
3286                 lStop = LCR_STOP_2;
3287                 dbg("%s - stop bits = 2", __FUNCTION__);
3288         } else {
3289                 lStop = LCR_STOP_1;
3290                 dbg("%s - stop bits = 1", __FUNCTION__);
3291         }
3292
3293         /* Update the LCR with the correct value */
3294         ATEN2011_port->shadowLCR &=
3295             ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
3296         ATEN2011_port->shadowLCR |= (lData | lParity | lStop);
3297
3298         ATEN2011_port->validDataMask = mask;
3299         DPRINTK
3300             ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is %x\n",
3301              ATEN2011_port->shadowLCR);
3302         /* Disable Interrupts */
3303         Data = 0x00;
3304         ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
3305
3306         Data = 0x00;
3307         ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
3308
3309         Data = 0xcf;
3310         ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
3311
3312         /* Send the updated LCR value to the ATEN2011 */
3313         Data = ATEN2011_port->shadowLCR;
3314
3315         ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
3316
3317         Data = 0x00b;
3318         ATEN2011_port->shadowMCR = Data;
3319         ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3320         Data = 0x00b;
3321         ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3322
3323         /* set up the MCR register and send it to the ATEN2011 */
3324
3325         ATEN2011_port->shadowMCR = MCR_MASTER_IE;
3326         if (cflag & CBAUD) {
3327                 ATEN2011_port->shadowMCR |= (MCR_DTR | MCR_RTS);
3328         }
3329
3330         if (cflag & CRTSCTS) {
3331                 ATEN2011_port->shadowMCR |= (MCR_XON_ANY);
3332
3333         } else {
3334                 ATEN2011_port->shadowMCR &= ~(MCR_XON_ANY);
3335         }
3336
3337         Data = ATEN2011_port->shadowMCR;
3338         ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3339
3340         /* Determine divisor based on baud rate */
3341         baud = tty_get_baud_rate(tty);
3342
3343         if (!baud) {
3344                 /* pick a default, any default... */
3345                 DPRINTK("%s\n", "Picked default baud...");
3346                 baud = 9600;
3347         }
3348
3349         dbg("%s - baud rate = %d", __FUNCTION__, baud);
3350         status = ATEN2011_send_cmd_write_baud_rate(ATEN2011_port, baud);
3351
3352         /* Enable Interrupts */
3353         Data = 0x0c;
3354         ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
3355
3356         if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
3357                 ATEN2011_port->read_urb->dev = serial->dev;
3358
3359                 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
3360
3361                 if (status) {
3362                         DPRINTK
3363                             (" usb_submit_urb(read bulk) failed, status = %d",
3364                              status);
3365                 }
3366         }
3367         //wake_up(&ATEN2011_port->delta_msr_wait);
3368         //ATEN2011_port->delta_msr_cond=1;
3369         DPRINTK
3370             ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is End %x\n",
3371              ATEN2011_port->shadowLCR);
3372
3373         return;
3374 }
3375
3376 static int ATEN2011_calc_num_ports(struct usb_serial *serial)
3377 {
3378
3379         __u16 Data = 0x00;
3380         int ret = 0;
3381         int ATEN2011_2or4ports;
3382         ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
3383                               ATEN_RDREQ, ATEN_RD_RTYPE, 0, GPIO_REGISTER,
3384                               &Data, VENDOR_READ_LENGTH, ATEN_WDR_TIMEOUT);
3385
3386         //printk("ATEN2011_calc_num_ports GPIO is %x\n",Data);
3387
3388 /* ghostgum: here is where the problem appears to bet */
3389 /* Which of the following are needed? */
3390 /* Greg used the serial->type->num_ports=2 */
3391 /* But the code in the ATEN2011_open relies on serial->num_ports=2 */
3392         if ((Data & 0x01) == 0) {
3393                 ATEN2011_2or4ports = 2;
3394                 serial->type->num_ports = 2;
3395                 serial->num_ports = 2;
3396         }
3397         //else if(serial->interface->cur_altsetting->desc.bNumEndpoints == 9)
3398         else {
3399                 ATEN2011_2or4ports = 4;
3400                 serial->type->num_ports = 4;
3401                 serial->num_ports = 4;
3402
3403         }
3404
3405         return ATEN2011_2or4ports;
3406 }
3407
3408 /****************************************************************************
3409  * ATEN2011_startup
3410  ****************************************************************************/
3411
3412 static int ATEN2011_startup(struct usb_serial *serial)
3413 {
3414         struct ATENINTL_serial *ATEN2011_serial;
3415         struct ATENINTL_port *ATEN2011_port;
3416         struct usb_device *dev;
3417         int i, status;
3418         int minor;
3419
3420         __u16 Data;
3421         DPRINTK("%s \n", " ATEN2011_startup :entering..........");
3422
3423         if (!serial) {
3424                 DPRINTK("%s\n", "Invalid Handler");
3425                 return -1;
3426         }
3427
3428         dev = serial->dev;
3429
3430         DPRINTK("%s\n", "Entering...");
3431
3432         /* create our private serial structure */
3433         ATEN2011_serial = kzalloc(sizeof(struct ATENINTL_serial), GFP_KERNEL);
3434         if (ATEN2011_serial == NULL) {
3435                 err("%s - Out of memory", __FUNCTION__);
3436                 return -ENOMEM;
3437         }
3438
3439         /* resetting the private structure field values to zero */
3440         memset(ATEN2011_serial, 0, sizeof(struct ATENINTL_serial));
3441
3442         ATEN2011_serial->serial = serial;
3443         //initilize status polling flag to FALSE
3444         ATEN2011_serial->status_polling_started = FALSE;
3445
3446         ATEN2011_set_serial_private(serial, ATEN2011_serial);
3447         ATEN2011_serial->ATEN2011_spectrum_2or4ports =
3448             ATEN2011_calc_num_ports(serial);
3449         /* we set up the pointers to the endpoints in the ATEN2011_open *
3450          * function, as the structures aren't created yet.             */
3451
3452         /* set up port private structures */
3453         for (i = 0; i < serial->num_ports; ++i) {
3454                 ATEN2011_port =
3455                     kmalloc(sizeof(struct ATENINTL_port), GFP_KERNEL);
3456                 if (ATEN2011_port == NULL) {
3457                         err("%s - Out of memory", __FUNCTION__);
3458                         ATEN2011_set_serial_private(serial, NULL);
3459                         kfree(ATEN2011_serial);
3460                         return -ENOMEM;
3461                 }
3462                 memset(ATEN2011_port, 0, sizeof(struct ATENINTL_port));
3463
3464                 /* Initialize all port interrupt end point to port 0 int endpoint *
3465                  * Our device has only one interrupt end point comman to all port */
3466
3467                 //      serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
3468
3469                 ATEN2011_port->port = serial->port[i];
3470 //
3471                 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
3472
3473                 minor = serial->port[i]->serial->minor;
3474                 if (minor == SERIAL_TTY_NO_MINOR)
3475                         minor = 0;
3476                 ATEN2011_port->port_num =
3477                     ((serial->port[i]->number - minor) + 1);
3478
3479                 ATEN2011_port->AppNum = (((__u16) serial->port[i]->number -
3480                                           (__u16) (minor)) + 1) << 8;
3481
3482                 if (ATEN2011_port->port_num == 1) {
3483                         ATEN2011_port->SpRegOffset = 0x0;
3484                         ATEN2011_port->ControlRegOffset = 0x1;
3485                         ATEN2011_port->DcrRegOffset = 0x4;
3486                         //ATEN2011_port->ClkSelectRegOffset =  ;
3487                 } else if ((ATEN2011_port->port_num == 2)
3488                            && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
3489                                4)) {
3490                         ATEN2011_port->SpRegOffset = 0x8;
3491                         ATEN2011_port->ControlRegOffset = 0x9;
3492                         ATEN2011_port->DcrRegOffset = 0x16;
3493                         //ATEN2011_port->ClkSelectRegOffset =  ;
3494                 } else if ((ATEN2011_port->port_num == 2)
3495                            && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
3496                                2)) {
3497                         ATEN2011_port->SpRegOffset = 0xa;
3498                         ATEN2011_port->ControlRegOffset = 0xb;
3499                         ATEN2011_port->DcrRegOffset = 0x19;
3500                         //ATEN2011_port->ClkSelectRegOffset =  ;
3501                 } else if ((ATEN2011_port->port_num == 3)
3502                            && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
3503                                4)) {
3504                         ATEN2011_port->SpRegOffset = 0xa;
3505                         ATEN2011_port->ControlRegOffset = 0xb;
3506                         ATEN2011_port->DcrRegOffset = 0x19;
3507                         //ATEN2011_port->ClkSelectRegOffset =  ;
3508                 } else if ((ATEN2011_port->port_num == 4)
3509                            && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
3510                                4)) {
3511                         ATEN2011_port->SpRegOffset = 0xc;
3512                         ATEN2011_port->ControlRegOffset = 0xd;
3513                         ATEN2011_port->DcrRegOffset = 0x1c;
3514                         //ATEN2011_port->ClkSelectRegOffset =  ;
3515                 }
3516                 ATEN2011_Dump_serial_port(ATEN2011_port);
3517
3518                 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
3519
3520                 //enable rx_disable bit in control register
3521
3522                 status =
3523                     ATEN2011_get_reg_sync(serial->port[i],
3524                                           ATEN2011_port->ControlRegOffset,
3525                                           &Data);
3526                 if (status < 0) {
3527                         DPRINTK("Reading ControlReg failed status-0x%x\n",
3528                                 status);
3529                         break;
3530                 } else
3531                         DPRINTK
3532                             ("ControlReg Reading success val is %x, status%d\n",
3533                              Data, status);
3534                 Data |= 0x08;   //setting driver done bit
3535                 Data |= 0x04;   //sp1_bit to have cts change reflect in modem status reg
3536
3537                 //Data |= 0x20; //rx_disable bit
3538                 status = 0;
3539                 status =
3540                     ATEN2011_set_reg_sync(serial->port[i],
3541                                           ATEN2011_port->ControlRegOffset,
3542                                           Data);
3543                 if (status < 0) {
3544                         DPRINTK
3545                             ("Writing ControlReg failed(rx_disable) status-0x%x\n",
3546                              status);
3547                         break;
3548                 } else
3549                         DPRINTK
3550                             ("ControlReg Writing success(rx_disable) status%d\n",
3551                              status);
3552
3553                 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
3554                 Data = 0x01;
3555                 status = 0;
3556                 status =
3557                     ATEN2011_set_reg_sync(serial->port[i],
3558                                           (__u16) (ATEN2011_port->DcrRegOffset +
3559                                                    0), Data);
3560                 if (status < 0) {
3561                         DPRINTK("Writing DCR0 failed status-0x%x\n", status);
3562                         break;
3563                 } else
3564                         DPRINTK("DCR0 Writing success status%d\n", status);
3565
3566                 Data = 0x05;
3567                 status = 0;
3568                 status =
3569                     ATEN2011_set_reg_sync(serial->port[i],
3570                                           (__u16) (ATEN2011_port->DcrRegOffset +
3571                                                    1), Data);
3572                 if (status < 0) {
3573                         DPRINTK("Writing DCR1 failed status-0x%x\n", status);
3574                         break;
3575                 } else
3576                         DPRINTK("DCR1 Writing success status%d\n", status);
3577
3578                 Data = 0x24;
3579                 status = 0;
3580                 status =
3581                     ATEN2011_set_reg_sync(serial->port[i],
3582                                           (__u16) (ATEN2011_port->DcrRegOffset +
3583                                                    2), Data);
3584                 if (status < 0) {
3585                         DPRINTK("Writing DCR2 failed status-0x%x\n", status);
3586                         break;
3587                 } else
3588                         DPRINTK("DCR2 Writing success status%d\n", status);
3589
3590                 // write values in clkstart0x0 and clkmulti 0x20
3591                 Data = 0x0;
3592                 status = 0;
3593                 status =
3594                     ATEN2011_set_reg_sync(serial->port[i],
3595                                           CLK_START_VALUE_REGISTER, Data);
3596                 if (status < 0) {
3597                         DPRINTK
3598                             ("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n",
3599                              status);
3600                         break;
3601                 } else
3602                         DPRINTK
3603                             ("CLK_START_VALUE_REGISTER Writing success status%d\n",
3604                              status);
3605
3606                 Data = 0x20;
3607                 status = 0;
3608                 status =
3609                     ATEN2011_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
3610                                           Data);
3611                 if (status < 0) {
3612                         DPRINTK
3613                             ("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
3614                              status);
3615                         break;
3616                 } else
3617                         DPRINTK("CLK_MULTI_REGISTER Writing success status%d\n",
3618                                 status);
3619
3620                 //write value 0x0 to scratchpad register
3621                 /*
3622                    if(RS485mode==0)
3623                    Data = 0xC0;
3624                    else
3625                    Data = 0x00;
3626                    status=0;
3627                    status=ATEN2011_set_Uart_Reg(serial->port[i],SCRATCH_PAD_REGISTER,Data);
3628                    if(status<0) {
3629                    DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
3630                    break;
3631                    }
3632                    else DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",status);
3633                  */
3634
3635                 /*
3636                    //Threshold Registers
3637                    if(ATEN2011_serial->ATEN2011_spectrum_2or4ports==4)
3638                    {
3639                    Data = 0x00;
3640                    status=0;
3641                    status=ATEN2011_set_reg_sync(serial->port[i],\
3642                    (__u16)(THRESHOLD_VAL_SP1_1+(__u16)ATEN2011_Thr_cnt),Data);
3643                    DPRINTK("THRESHOLD_VAL offset is%x\n", (__u16)(THRESHOLD_VAL_SP1_1+(__u16)ATEN2011_Thr_cnt));
3644                    if(status<0) {
3645                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3646                    break;
3647                    }
3648                    else DPRINTK("THRESHOLD_VAL Writing success status%d\n",status);
3649                    ATEN2011_Thr_cnt++;
3650
3651                    Data = 0x01;
3652                    status=0;
3653                    status=ATEN2011_set_reg_sync(serial->port[i],\
3654                    (__u16)(THRESHOLD_VAL_SP1_1+(__u16)ATEN2011_Thr_cnt),Data);
3655                    DPRINTK("THRESHOLD_VAL offsetis%x\n",(__u16)(THRESHOLD_VAL_SP1_1+(__u16)ATEN2011_Thr_cnt));
3656                    if(status<0) {
3657                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3658                    break;
3659                    }
3660                    else DPRINTK("THRESHOLD_VAL Writing success status%d\n",status);
3661                    ATEN2011_Thr_cnt++;
3662                    }
3663
3664                    else
3665                    {
3666
3667                    if(ATEN2011_port->port_num==1)
3668                    {
3669                    Data = 0x00;
3670                    status=0;
3671                    status=ATEN2011_set_reg_sync(serial->port[i],\
3672                    0x3f,Data);
3673                    DPRINTK("THRESHOLD_VAL offset is 0x3f\n");
3674                    if(status<0) {
3675                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3676                    break;
3677                    }
3678                    Data = 0x01;
3679                    status=0;
3680                    status=ATEN2011_set_reg_sync(serial->port[i],\
3681                    0x40,Data);
3682                    DPRINTK("THRESHOLD_VAL offset is 0x40\n");
3683                    if(status<0) {
3684                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3685                    break;
3686
3687                    }
3688                    }
3689                    else
3690                    {
3691                    Data = 0x00;
3692                    status=0;
3693                    status=ATEN2011_set_reg_sync(serial->port[i],\
3694                    0x43,Data);
3695                    DPRINTK("THRESHOLD_VAL offset is 0x43\n");
3696                    if(status<0) {
3697                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3698                    break;
3699                    }
3700                    Data = 0x01;
3701                    status=0;
3702                    status=ATEN2011_set_reg_sync(serial->port[i],\
3703                    0x44,Data);
3704                    DPRINTK("THRESHOLD_VAL offset is 0x44\n");
3705                    if(status<0) {
3706                    DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3707                    break;
3708
3709                    }
3710
3711                    }
3712
3713                    }
3714                  */
3715                 //Zero Length flag register
3716                 if ((ATEN2011_port->port_num != 1)
3717                     && (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)) {
3718
3719                         Data = 0xff;
3720                         status = 0;
3721                         status = ATEN2011_set_reg_sync(serial->port[i],
3722                                                        (__u16) (ZLP_REG1 +
3723                                                                 ((__u16)
3724                                                                  ATEN2011_port->
3725                                                                  port_num)),
3726                                                        Data);
3727                         DPRINTK("ZLIP offset%x\n",
3728                                 (__u16) (ZLP_REG1 +
3729                                          ((__u16) ATEN2011_port->port_num)));
3730                         if (status < 0) {
3731                                 DPRINTK
3732                                     ("Writing ZLP_REG%d failed status-0x%x\n",
3733                                      i + 2, status);
3734                                 break;
3735                         } else
3736                                 DPRINTK("ZLP_REG%d Writing success status%d\n",
3737                                         i + 2, status);
3738                 } else {
3739                         Data = 0xff;
3740                         status = 0;
3741                         status = ATEN2011_set_reg_sync(serial->port[i],
3742                                                        (__u16) (ZLP_REG1 +
3743                                                                 ((__u16)
3744                                                                  ATEN2011_port->
3745                                                                  port_num) -
3746                                                                 0x1), Data);
3747                         DPRINTK("ZLIP offset%x\n",
3748                                 (__u16) (ZLP_REG1 +
3749                                          ((__u16) ATEN2011_port->port_num) -
3750                                          0x1));
3751                         if (status < 0) {
3752                                 DPRINTK
3753                                     ("Writing ZLP_REG%d failed status-0x%x\n",
3754                                      i + 1, status);
3755                                 break;
3756                         } else
3757                                 DPRINTK("ZLP_REG%d Writing success status%d\n",
3758                                         i + 1, status);
3759
3760                 }
3761                 ATEN2011_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC);
3762                 ATEN2011_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
3763
3764         }
3765
3766         ATEN2011_Thr_cnt = 0;
3767         //Zero Length flag enable
3768         Data = 0x0f;
3769         status = 0;
3770         status = ATEN2011_set_reg_sync(serial->port[0], ZLP_REG5, Data);
3771         if (status < 0) {
3772                 DPRINTK("Writing ZLP_REG5 failed status-0x%x\n", status);
3773                 return -1;
3774         } else
3775                 DPRINTK("ZLP_REG5 Writing success status%d\n", status);
3776
3777         /* setting configuration feature to one */
3778         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
3779                         (__u8) 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 5 * HZ);
3780         ATEN2011_Thr_cnt = 0;
3781         return 0;
3782 }
3783
3784 /****************************************************************************
3785  * ATEN2011_shutdown
3786  *      This function is called whenever the device is removed from the usb bus.
3787  ****************************************************************************/
3788
3789 static void ATEN2011_shutdown(struct usb_serial *serial)
3790 {
3791         int i;
3792         struct ATENINTL_port *ATEN2011_port;
3793         DPRINTK("%s \n", " shutdown :entering..........");
3794
3795 /* MATRIX  */
3796         //ThreadState = 1;
3797 /* MATRIX  */
3798
3799         if (!serial) {
3800                 DPRINTK("%s", "Invalid Handler \n");
3801                 return;
3802         }
3803
3804         /*      check for the ports to be closed,close the ports and disconnect         */
3805
3806         /* free private structure allocated for serial port  *
3807          * stop reads and writes on all ports                */
3808
3809         for (i = 0; i < serial->num_ports; ++i) {
3810                 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
3811                 kfree(ATEN2011_port->ctrl_buf);
3812                 usb_kill_urb(ATEN2011_port->control_urb);
3813                 kfree(ATEN2011_port);
3814                 ATEN2011_set_port_private(serial->port[i], NULL);
3815         }
3816
3817         /* free private structure allocated for serial device */
3818
3819         kfree(ATEN2011_get_serial_private(serial));
3820         ATEN2011_set_serial_private(serial, NULL);
3821
3822         DPRINTK("%s\n", "Thank u :: ");
3823
3824 }
3825
3826 /* Inline functions to check the sanity of a pointer that is passed to us */
3827 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial,
3828                                           const char *function)
3829 {
3830         if (!serial) {
3831                 dbg("%s - serial == NULL", function);
3832                 return -1;
3833         }
3834 //      if (serial->magic != USB_SERIAL_MAGIC) {
3835 //              dbg("%s - bad magic number for serial", function);
3836 //              return -1;
3837 //      }
3838         if (!serial->type) {
3839                 dbg("%s - serial->type == NULL!", function);
3840                 return -1;
3841         }
3842
3843         return 0;
3844 }
3845 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port,
3846                                         const char *function)
3847 {
3848         if (!port) {
3849                 dbg("%s - port == NULL", function);
3850                 return -1;
3851         }
3852 //      if (port->magic != USB_SERIAL_PORT_MAGIC) {
3853 //              dbg("%s - bad magic number for port", function);
3854 //              return -1;
3855 //      }
3856         if (!port->serial) {
3857                 dbg("%s - port->serial == NULL", function);
3858                 return -1;
3859         }
3860
3861         return 0;
3862 }
3863 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port,
3864                                                   const char *function)
3865 {
3866         /* if no port was specified, or it fails a paranoia check */
3867         if (!port ||
3868             ATEN2011_port_paranoia_check(port, function) ||
3869             ATEN2011_serial_paranoia_check(port->serial, function)) {
3870                 /* then say that we don't have a valid usb_serial thing, which will                  * end up genrating -ENODEV return values */
3871                 return NULL;
3872         }
3873
3874         return port->serial;
3875 }
3876
3877 /****************************************************************************
3878  * ATENINTL2011_init
3879  *      This is called by the module subsystem, or on startup to initialize us
3880  ****************************************************************************/
3881 int __init ATENINTL2011_init(void)
3882 {
3883         int retval;
3884
3885         DPRINTK("%s \n", " ATEN2011_init :entering..........");
3886
3887         /* Register with the usb serial */
3888         retval = usb_serial_register(&ATENINTL2011_4port_device);
3889
3890         if (retval)
3891                 goto failed_port_device_register;
3892
3893 /*      info(DRIVER_DESC " " DRIVER_VERSION); */
3894         printk(KERN_INFO KBUILD_MODNAME ":"
3895                DRIVER_DESC " " DRIVER_VERSION "\n");
3896
3897         /* Register with the usb */
3898         retval = usb_register(&io_driver);
3899
3900         if (retval)
3901                 goto failed_usb_register;
3902
3903         if (retval == 0) {
3904                 DPRINTK("%s\n", "Leaving...");
3905                 return 0;
3906         }
3907
3908       failed_usb_register:
3909         usb_serial_deregister(&ATENINTL2011_4port_device);
3910
3911       failed_port_device_register:
3912
3913         return retval;
3914 }
3915
3916 /****************************************************************************
3917  * ATENINTL2011_exit
3918  *      Called when the driver is about to be unloaded.
3919  ****************************************************************************/
3920 void __exit ATENINTL2011_exit(void)
3921 {
3922
3923         DPRINTK("%s \n", " ATEN2011_exit :entering..........");
3924
3925         usb_deregister(&io_driver);
3926
3927         usb_serial_deregister(&ATENINTL2011_4port_device);
3928
3929         DPRINTK("%s\n", "End...");
3930 }
3931
3932 module_init(ATENINTL2011_init);
3933 module_exit(ATENINTL2011_exit);
3934
3935 /* Module information */
3936 MODULE_DESCRIPTION(DRIVER_DESC);
3937 MODULE_LICENSE("GPL");
3938
3939 MODULE_PARM_DESC(debug, "Debug enabled or not");