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