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.
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.
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
17 /*************************************************************************
18 *** --------------------------------------------------------------------
20 *** Project Name: ATENINTL
22 *** Module Name: ATEN2011
27 *** File Revision: 1.2
29 *** Revision Date: 2009-01-16
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.
37 *** Modified from ATEN revision 1.2 for Linux kernel 2.6.26 or later
42 *** DBG - Code inserted due to as part of debugging
43 *** DPRINTK - Debug Print statement
45 *************************************************************************/
47 /* all file inclusion goes here */
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>
65 #include <linux/usb/serial.h>
67 #define MAX_RS232_PORTS 2 /* Max # of RS-232 ports per device */
69 #include <linux/version.h>
72 * All typedef goes here
76 /* typedefs that the insideout headers need */
87 #define LOW8(val) ((unsigned char)(val & 0xff))
91 #define HIGH8(val) ((unsigned char)((val & 0xff00) >> 8))
95 #define NUM_ENTRIES(x) (sizeof(x)/sizeof((x)[0]))
98 #define MAX_SERIALNUMBER_LEN 12
100 /* The following table is used to map the USBx port number to
101 * the device serial number (or physical USB path), */
103 #define MAX_ATENPORTS 2
104 #define MAX_NAME_LEN 64
106 #define RAID_REG1 0x30
107 #define RAID_REG2 0x31
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
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
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
126 /* For higher baud Rates use TIOCEXBAUD */
127 #define TIOCEXBAUD 0x5462
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 */
136 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
137 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
138 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
141 #define SERIAL_MAGIC 0x6702
144 #define PORT_MAGIC 0x7301
148 /* vendor id and device id defines */
150 #define USB_VENDOR_ID_ATENINTL 0x0557
151 #define ATENINTL_DEVICE_ID_2011 0x2011
152 #define ATENINTL_DEVICE_ID_7820 0x7820
154 /* Product information read from the ATENINTL. Provided for later upgrade */
156 /* Interrupt Rotinue Defines */
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
165 * Emulation of the bit mask on the LINE STATUS REGISTER.
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
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
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))
200 #define SP_REGISTER_BASE ((__u16)(0x08))
201 #define CONTROL_REGISTER_BASE ((__u16)(0x09))
202 #define DCR_REGISTER_BASE ((__u16)(0x16))
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))
211 #define CLOCK_SELECT_REG1 ((__u16)(0x13))
212 #define CLOCK_SELECT_REG2 ((__u16)(0x14))
214 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
217 * URB POOL related defines
219 #define NUM_URBS 16 /* URB Count */
220 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
222 struct ATENINTL_product_info
224 __u16 ProductId; /* Product Identifier */
225 __u8 NumPorts; /* Number of ports on ATENINTL */
226 __u8 ProdInfoVer; /* What version of structure is this? */
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 */
234 __u8 CpuRev; /* CPU revision level (chg only if s/w visible) */
235 __u8 BoardRev; /* PCB revision level (chg only if s/w visible) */
237 __u8 ManufactureDescDate[3]; /* MM/DD/YY when descriptor template was compiled */
238 __u8 Unused1[1]; /* Available */
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 */
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 */
254 MODULE_DEVICE_TABLE (usb, id_table_combined);
256 /* This structure holds all of the local port information */
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 */
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 */
287 struct async_icount icount;
288 struct usb_serial_port *port; /* loop back to the owner of this object */
292 __u8 ControlRegOffset;
294 __u8 ClkSelectRegOffset;
295 //for processing control URBS in interrupt context
296 struct urb *control_urb;
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 */
308 /* This structure holds all of the individual serial device information */
309 struct ATENINTL_serial
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;
333 /* baud rate information */
334 struct ATEN2011_divisor_table_entry
340 /* Define table of divisors for ATENINTL 2011 hardware *
341 * These assume a 3.6864MHz crystal, the standard /16, and *
344 static struct ATEN2011_divisor_table_entry ATEN2011_divisor_table[] = {
346 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
347 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
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 */
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);
389 //static void ATEN2011_break_ctl(struct usb_serial_port *port, int break_state );
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);
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);
403 int __init ATENINTL2011_init(void);
404 void __exit ATENINTL2011_exit(void);
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 */
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 */
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 */
428 #define LCR_SET_BREAK 0x40 /* Set Break condition */
429 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
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 */
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 */
444 /* all defines goes here */
447 * Debug related defines
450 /* 1: Enables the debugging -- 0: Disable the debugging */
457 static int debug = 0;
458 #define DPRINTK(fmt, args...) printk( "%s: " fmt, __FUNCTION__ , ## args)
461 static int debug = 0;
462 #define DPRINTK(fmt, args...)
466 // #define DPRINTK(fmt, args...)
469 * Version Information
471 #define DRIVER_VERSION "1.3.1"
472 #define DRIVER_DESC "ATENINTL 2011 USB Serial Adapter"
475 * Defines used for sending commands to port
478 #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
479 #define ATEN_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
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
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)
496 int ATEN2011_Thr_cnt;
497 //int ATEN2011_spectrum_2or4ports; //this says the number of ports in the device
500 int RS485mode = 0; //set to 1 for RS485 mode and 0 for RS232 mode
502 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port, const
504 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial, const char
506 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port, const char
509 /* setting and get register values */
510 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
512 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
514 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
516 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
519 void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port);
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 /************************************************************************/
528 static inline void ATEN2011_set_serial_private(struct usb_serial *serial,
529 struct ATENINTL_serial *data)
531 usb_set_serial_data(serial, (void *)data);
534 static inline struct ATENINTL_serial *ATEN2011_get_serial_private(struct
538 return (struct ATENINTL_serial *)usb_get_serial_data(serial);
541 static inline void ATEN2011_set_port_private(struct usb_serial_port *port,
542 struct ATENINTL_port *data)
544 usb_set_serial_port_data(port, (void *)data);
547 static inline struct ATENINTL_port *ATEN2011_get_port_private(struct
551 return (struct ATENINTL_port *)usb_get_serial_port_data(port);
555 Description:- To set the Control register by calling usb_fill_control_urb function by passing usb_sndctrlpipe function as parameter.
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.
563 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
566 struct usb_device *dev = port->serial->dev;
568 DPRINTK("ATEN2011_set_reg_sync offset is %x, value %x\n", reg, val);
570 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
571 ATEN_WR_RTYPE, val, reg, NULL, 0,
576 Description:- To set the Uart register by calling usb_fill_control_urb function by passing usb_rcvctrlpipe function as parameter.
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.
584 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
587 struct usb_device *dev = port->serial->dev;
590 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
591 ATEN_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
593 DPRINTK("ATEN2011_get_reg_sync offset is %x, return val %x\n", reg,
595 *val = (*val) & 0x00ff;
600 Description:- To set the Uart register by calling usb_fill_control_urb function by passing usb_sndctrlpipe function as parameter.
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.
608 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
612 struct usb_device *dev = port->serial->dev;
613 struct ATENINTL_serial *ATEN2011_serial;
615 ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
616 minor = port->serial->minor;
617 if (minor == SERIAL_TTY_NO_MINOR)
620 // For the UART control registers, the application number need to be Or'ed
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",
627 if (((__u16) port->number - (__u16) (minor)) == 0) {
630 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
632 ("ATEN2011_set_Uart_Reg application number is %x\n",
637 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
639 ("ATEN2011_set_Uart_Reg application number is %x\n",
643 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
644 ATEN_WR_RTYPE, val, reg, NULL, 0,
650 Description:- To set the Control register by calling usb_fill_control_urb function by passing usb_rcvctrlpipe function as parameter.
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.
657 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
660 struct usb_device *dev = port->serial->dev;
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)
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",
676 if (((__u16) port->number - (__u16) (minor)) == 0) {
679 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
681 ("ATEN2011_get_Uart_Reg application number is %x\n",
686 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
688 ("ATEN2011_get_Uart_Reg application number is %x\n",
692 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
693 ATEN_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
695 *val = (*val) & 0x00ff;
699 void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port)
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");
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 = {
719 .owner = THIS_MODULE,
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,
733 #ifdef ATENSerialProbe
734 .probe = ATEN2011_serial_probe,
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,
748 static struct usb_driver io_driver = {
750 .probe = usb_serial_probe,
751 .disconnect = usb_serial_disconnect,
752 .id_table = id_table_combined,
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 /************************************************************************/
762 /*****************************************************************************
763 * ATEN2011_interrupt_callback
764 * this is the callback function for when we have received data on the
765 * interrupt endpoint.
767 * pointer to the URB packet,
769 *****************************************************************************/
771 static void ATEN2011_interrupt_callback(struct urb *urb)
775 struct ATENINTL_port *ATEN2011_port;
776 struct ATENINTL_serial *ATEN2011_serial;
777 struct usb_serial *serial;
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");
787 ATEN2011_serial = (struct ATENINTL_serial *)urb->context;
788 if (!urb) // || ATEN2011_serial->status_polling_started == FALSE )
790 DPRINTK("%s", "Invalid Pointer !!!!:\n");
794 switch (urb->status) {
801 /* this urb is terminated, clean up */
802 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
806 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
810 length = urb->actual_length;
811 data = urb->transfer_buffer;
813 //ATEN2011_serial= (struct ATENINTL_serial *)urb->context;
814 //serial = ATEN2011_get_usb_serial(port,__FUNCTION__);
815 serial = ATEN2011_serial->serial;
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 */
824 if (length && length > 5) {
825 DPRINTK("%s \n", "Wrong data !!!");
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];
837 sp[0] = (__u8) data[0];
838 sp[1] = (__u8) data[2];
839 //sp[2]=(__u8)data[2];
840 //sp[3]=(__u8)data[3];
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)
850 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
853 (((__u16) serial->port[i]->number -
854 (__u16) (minor)) + 2) << 8;
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);
863 DPRINTK("SP%d No Interrupt !!!\n", i);
865 switch (sp[i] & 0x0f) {
868 ("Serial Port %d: Receiver status error or ",
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,
879 ("Serial Port %d: Modem status change\n",
881 ATEN2011_port->MsrLsr = 0;
882 ATEN2011_get_reg(ATEN2011_port, wval,
883 MODEM_STATUS_REGISTER,
892 if (ATEN2011_serial->status_polling_started == FALSE)
895 result = usb_submit_urb(urb, GFP_ATOMIC);
897 dev_err(&urb->dev->dev,
898 "%s - Error %d submitting interrupt urb\n",
899 __FUNCTION__, result);
907 static void ATEN2011_control_callback(struct urb *urb)
910 struct ATENINTL_port *ATEN2011_port;
914 DPRINTK("%s", "Invalid Pointer !!!!:\n");
918 switch (urb->status) {
925 /* this urb is terminated, clean up */
926 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
930 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
935 ATEN2011_port = (struct ATENINTL_port *)urb->context;
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);
951 int handle_newMsr(struct ATENINTL_port *port, __u8 newMsr)
953 struct ATENINTL_port *ATEN2011_port;
954 struct async_icount *icount;
955 ATEN2011_port = port;
956 icount = &ATEN2011_port->icount;
958 (ATEN_MSR_DELTA_CTS | ATEN_MSR_DELTA_DSR | ATEN_MSR_DELTA_RI |
959 ATEN_MSR_DELTA_CD)) {
960 icount = &ATEN2011_port->icount;
962 /* update input line counters */
963 if (newMsr & ATEN_MSR_DELTA_CTS) {
966 if (newMsr & ATEN_MSR_DELTA_DSR) {
969 if (newMsr & ATEN_MSR_DELTA_CD) {
972 if (newMsr & ATEN_MSR_DELTA_RI) {
979 int handle_newLsr(struct ATENINTL_port *port, __u8 newLsr)
981 struct async_icount *icount;
983 dbg("%s - %02x", __FUNCTION__, newLsr);
985 if (newLsr & SERIAL_LSR_BI) {
987 // Parity and Framing errors only count if they
988 // occur exclusive of a break being
991 newLsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
994 /* update input line counters */
995 icount = &port->icount;
996 if (newLsr & SERIAL_LSR_BI) {
999 if (newLsr & SERIAL_LSR_OE) {
1002 if (newLsr & SERIAL_LSR_PE) {
1005 if (newLsr & SERIAL_LSR_FE) {
1011 static int ATEN2011_get_reg(struct ATENINTL_port *ATEN, __u16 Wval, __u16 reg,
1014 struct usb_device *dev = ATEN->port->serial->dev;
1015 struct usb_ctrlrequest *dr = NULL;
1016 unsigned char *buffer = NULL;
1018 buffer = (__u8 *) ATEN->ctrl_buf;
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);
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);
1036 /*****************************************************************************
1037 * ATEN2011_bulk_in_callback
1038 * this is the callback function for when we have received data on the
1041 * pointer to the URB packet,
1043 *****************************************************************************/
1044 static void ATEN2011_bulk_in_callback(struct urb *urb)
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;
1054 DPRINTK("%s", "Invalid Pointer !!!!:\n");
1059 DPRINTK("nonzero read bulk status received: %d", urb->status);
1060 // if(urb->status==84)
1065 ATEN2011_port = (struct ATENINTL_port *)urb->context;
1066 if (!ATEN2011_port) {
1067 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
1071 port = (struct usb_serial_port *)ATEN2011_port->port;
1072 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1073 DPRINTK("%s", "Port Paranoia failed \n");
1077 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1079 DPRINTK("%s\n", "Bad serial pointer ");
1083 DPRINTK("%s\n", "Entering... \n");
1085 data = urb->transfer_buffer;
1086 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1088 DPRINTK("%s", "Entering ........... \n");
1090 if (urb->actual_length) {
1091 tty = tty_port_tty_get(&ATEN2011_port->port->port);
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);
1100 ATEN2011_port->icount.rx += urb->actual_length;
1101 DPRINTK("ATEN2011_port->icount.rx is %d:\n",
1102 ATEN2011_port->icount.rx);
1106 if (!ATEN2011_port->read_urb) {
1107 DPRINTK("%s", "URB KILLED !!!\n");
1111 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
1112 ATEN2011_port->read_urb->dev = serial->dev;
1114 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
1118 (" usb_submit_urb(read bulk) failed, status = %d",
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.
1129 * pointer to the URB packet,
1131 *****************************************************************************/
1132 static void ATEN2011_bulk_out_data_callback(struct urb *urb)
1134 struct ATENINTL_port *ATEN2011_port;
1135 struct tty_struct *tty;
1137 DPRINTK("%s", "Invalid Pointer !!!!:\n");
1142 DPRINTK("nonzero write bulk status received:%d\n", urb->status);
1146 ATEN2011_port = (struct ATENINTL_port *)urb->context;
1147 if (!ATEN2011_port) {
1148 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
1152 if (ATEN2011_port_paranoia_check(ATEN2011_port->port, __FUNCTION__)) {
1153 DPRINTK("%s", "Port Paranoia failed \n");
1157 DPRINTK("%s \n", "Entering .........");
1159 tty = tty_port_tty_get(&ATEN2011_port->port->port);
1161 if (tty && ATEN2011_port->open) {
1162 /* tell the tty driver that something has changed */
1163 wake_up_interruptible(&tty->write_wait);
1166 /* Release the Write URB */
1167 ATEN2011_port->write_in_progress = FALSE;
1169 //schedule_work(&ATEN2011_port->port->work);
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)
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 */
1190 /*****************************************************************************
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 *****************************************************************************/
1197 static int ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port,
1202 struct usb_serial *serial;
1203 // struct usb_serial_port *port0;
1207 struct ATENINTL_serial *ATEN2011_serial;
1208 struct ATENINTL_port *ATEN2011_port;
1209 struct ktermios tmp_termios;
1212 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1213 DPRINTK("%s", "Port Paranoia failed \n");
1216 //ATEN2011_serial->NoOfOpenPorts++;
1217 serial = port->serial;
1219 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1220 DPRINTK("%s", "Serial Paranoia failed \n");
1224 ATEN2011_port = ATEN2011_get_port_private(port);
1226 if (ATEN2011_port == NULL)
1229 if (ATEN2011_port->ctrl_buf==NULL)
1231 ATEN2011_port->ctrl_buf = kmalloc(16,GFP_KERNEL);
1232 if (ATEN2011_port->ctrl_buf == NULL) {
1233 printk(", Can't allocate ctrl buff\n");
1239 if(!ATEN2011_port->control_urb)
1241 ATEN2011_port->control_urb=kmalloc(sizeof(struct urb),GFP_KERNEL);
1244 // port0 = serial->port[0];
1246 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1248 if (ATEN2011_serial == NULL) //|| port0 == NULL)
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);
1256 usb_clear_halt(serial->dev, port->write_urb->pipe);
1257 usb_clear_halt(serial->dev, port->read_urb->pipe);
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;
1265 err("No more urbs???");
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__);
1278 /*****************************************************************************
1279 * Initialize ATEN2011 -- Write Init values to corresponding Registers
1287 * 0x08 : SP1/2 Control Reg
1288 *****************************************************************************/
1290 //NEED to check the fallowing Block
1294 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1296 DPRINTK("Reading Spreg failed\n");
1300 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1302 DPRINTK("writing Spreg failed\n");
1307 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1309 DPRINTK("writing Spreg failed\n");
1313 //End of block to be checked
1314 //**************************CHECK***************************//
1321 status = ATEN2011_set_Uart_Reg(port, SCRATCH_PAD_REGISTER, Data);
1323 DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
1327 DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",
1330 //**************************CHECK***************************//
1335 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1337 DPRINTK("Reading Controlreg failed\n");
1340 Data |= 0x08; //Driver done bit
1342 status = ATEN2011_set_reg_sync(port,ATEN2011_port->ControlRegOffset,Data);
1344 DPRINTK("writing Controlreg failed\n");
1348 Data |= 0x20; //rx_disable
1351 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1353 DPRINTK("writing Controlreg failed\n");
1356 //do register settings here
1357 // Set all regs to the device default values.
1358 ////////////////////////////////////
1359 // First Disable all interrupts.
1360 ////////////////////////////////////
1364 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1366 DPRINTK("disableing interrupts failed\n");
1369 // Set FIFO_CONTROL_REGISTER to the default value
1372 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1374 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
1380 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1382 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
1386 Data = 0x03; //LCR_BITS_8
1388 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1389 ATEN2011_port->shadowLCR = Data;
1391 Data = 0x0b; // MCR_DTR|MCR_RTS|MCR_MASTER_IE
1393 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1394 ATEN2011_port->shadowMCR = Data;
1399 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1400 ATEN2011_port->shadowLCR = Data;
1402 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1404 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1408 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
1412 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
1416 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1418 // Data = ATEN2011_port->shadowLCR; //data latch disable
1419 Data = Data & ~SERIAL_LCR_DLAB;
1421 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1422 ATEN2011_port->shadowLCR = Data;
1424 //clearing Bulkin and Bulkout Fifo
1427 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1431 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1433 Data = Data & ~0x0c;
1435 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1436 //Finally enable all interrupts
1440 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1442 //clearing rx_disable
1446 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1447 Data = Data & ~0x20;
1450 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1456 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1460 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
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. */
1467 tty->low_latency = 1;
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);
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)
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;
1495 /* set up interrupt urb */
1496 usb_fill_int_urb(ATEN2011_serial->interrupt_read_urb,
1498 usb_rcvintpipe(serial->dev,
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);
1507 /* start interrupt read for ATEN2011 *
1508 * will continue as long as ATEN2011 is connected */
1511 usb_submit_urb(ATEN2011_serial->interrupt_read_urb,
1514 DPRINTK("%s - Error %d submitting interrupt urb",
1515 __FUNCTION__, response);
1518 // printk(" interrupt URB submitted\n");
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.) */
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;
1542 minor = port->serial->minor;
1543 if (minor == SERIAL_TTY_NO_MINOR)
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,
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);
1558 usb_fill_bulk_urb(ATEN2011_port->read_urb,
1560 usb_rcvbulkpipe(serial->dev,
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);
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);
1572 err("%s - Error %d submitting control urb", __FUNCTION__,
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);
1582 /* initialize our icount structure */
1583 memset(&(ATEN2011_port->icount), 0x00, sizeof(ATEN2011_port->icount));
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);
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;
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);
1607 /*****************************************************************************
1609 * this function is called by the tty driver when a port is closed
1610 *****************************************************************************/
1612 static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port,
1615 struct usb_serial *serial;
1616 struct ATENINTL_serial *ATEN2011_serial;
1617 struct ATENINTL_port *ATEN2011_port;
1622 DPRINTK("%s\n", "ATEN2011_close:entering...");
1626 //printk("Entering... :ATEN2011_close\n");
1627 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1628 DPRINTK("%s", "Port Paranoia failed \n");
1631 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1633 DPRINTK("%s", "Serial Paranoia failed \n");
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)) {
1643 /* flush and block(wait) until tx is empty */
1644 ATEN2011_block_until_tx_empty(tty, ATEN2011_port);
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]->
1654 kfree(ATEN2011_port->write_urb_pool[no_urbs]->
1656 usb_free_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1659 /* While closing port, shutdown all bulk read, write *
1660 * and interrupt read if they exists */
1662 if (ATEN2011_port->write_urb) {
1663 DPRINTK("%s", "Shutdown bulk write\n");
1664 usb_kill_urb(ATEN2011_port->write_urb);
1666 if (ATEN2011_port->read_urb) {
1667 DPRINTK("%s", "Shutdown bulk read\n");
1668 usb_kill_urb(ATEN2011_port->read_urb);
1670 if ((&ATEN2011_port->control_urb)) {
1671 DPRINTK("%s", "Shutdown control read\n");
1672 // usb_kill_urb (ATEN2011_port->control_urb);
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);
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);
1698 usb_free_urb(ATEN2011_port->write_urb);
1700 // clear the MCR & IER
1702 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1704 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1706 //ATEN2011_get_Uart_Reg(port,MODEM_CONTROL_REGISTER,&Data1);
1707 //printk("value of MCR after closing the port is : 0x%x\n",Data1);
1709 ATEN2011_port->open = FALSE;
1710 ATEN2011_port->closePending = FALSE;
1711 ATEN2011_port->openPending = FALSE;
1712 DPRINTK("%s \n", "Leaving ............");
1716 /*****************************************************************************
1718 * this function sends a break to the port
1719 *****************************************************************************/
1720 static void ATEN2011_break(struct tty_struct *tty, int break_state)
1722 struct usb_serial_port *port = tty->driver_data;
1724 struct usb_serial *serial;
1725 struct ATENINTL_serial *ATEN2011_serial;
1726 struct ATENINTL_port *ATEN2011_port;
1728 DPRINTK("%s \n", "Entering ...........");
1729 DPRINTK("ATEN2011_break: Start\n");
1731 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1732 DPRINTK("%s", "Port Paranoia failed \n");
1736 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1738 DPRINTK("%s", "Serial Paranoia failed \n");
1742 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1743 ATEN2011_port = ATEN2011_get_port_private(port);
1745 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1749 /* flush and chase */
1750 ATEN2011_port->chaseResponsePending = TRUE;
1754 /* flush and block until tx is empty */
1755 ATEN2011_block_until_chase_response(tty, ATEN2011_port);
1758 if (break_state == -1) {
1759 data = ATEN2011_port->shadowLCR | LCR_SET_BREAK;
1761 data = ATEN2011_port->shadowLCR & ~LCR_SET_BREAK;
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);
1773 /************************************************************************
1775 * ATEN2011_block_until_chase_response
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)
1782 ************************************************************************/
1784 static void ATEN2011_block_until_chase_response(struct tty_struct *tty,
1785 struct ATENINTL_port
1788 int timeout = 1 * HZ;
1793 count = ATEN2011_chars_in_buffer(tty);
1795 /* Check for Buffer status */
1797 ATEN2011_port->chaseResponsePending = FALSE;
1801 /* Block the thread for a while */
1802 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1804 /* No activity.. count down section */
1807 dbg("%s - TIMEOUT", __FUNCTION__);
1810 /* Reset timout value back to seconds */
1817 /************************************************************************
1819 * ATEN2011_block_until_tx_empty
1821 * This function will block the close until one of the following:
1823 * 2. The ATEN2011 has stopped
1824 * 3. A timout of 3 seconds without activity has expired
1826 ************************************************************************/
1827 static void ATEN2011_block_until_tx_empty(struct tty_struct *tty,
1828 struct ATENINTL_port *ATEN2011_port)
1830 int timeout = HZ / 10;
1836 count = ATEN2011_chars_in_buffer(tty);
1838 /* Check for Buffer status */
1843 /* Block the thread for a while */
1844 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1847 /* No activity.. count down section */
1850 dbg("%s - TIMEOUT", __FUNCTION__);
1853 /* Reset timout value back to seconds */
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 *****************************************************************************/
1867 static int ATEN2011_write_room(struct tty_struct *tty)
1869 struct usb_serial_port *port = tty->driver_data;
1872 struct ATENINTL_port *ATEN2011_port;
1874 // DPRINTK("%s \n"," ATEN2011_write_room:entering ...........");
1876 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1877 DPRINTK("%s", "Invalid port \n");
1878 DPRINTK("%s \n", " ATEN2011_write_room:leaving ...........");
1882 ATEN2011_port = ATEN2011_get_port_private(port);
1883 if (ATEN2011_port == NULL) {
1884 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1888 for (i = 0; i < NUM_URBS; ++i) {
1889 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1890 room += URB_TRANSFER_BUFFER_SIZE;
1894 dbg("%s - returns %d", __FUNCTION__, room);
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
1906 * Otherwise we return a negative error number.
1907 *****************************************************************************/
1909 static int ATEN2011_chars_in_buffer(struct tty_struct *tty)
1911 struct usb_serial_port *port = tty->driver_data;
1914 struct ATENINTL_port *ATEN2011_port;
1916 //DPRINTK("%s \n"," ATEN2011_chars_in_buffer:entering ...........");
1918 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1919 DPRINTK("%s", "Invalid port \n");
1923 ATEN2011_port = ATEN2011_get_port_private(port);
1924 if (ATEN2011_port == NULL) {
1925 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1929 for (i = 0; i < NUM_URBS; ++i) {
1930 if (ATEN2011_port->write_urb_pool[i]->status == -EINPROGRESS) {
1931 chars += URB_TRANSFER_BUFFER_SIZE;
1934 dbg("%s - returns %d", __FUNCTION__, chars);
1939 /*****************************************************************************
1941 * this function is called by the tty driver when data should be written to
1943 * If successful, we return the number of bytes written, otherwise we
1944 * return a negative error number.
1945 *****************************************************************************/
1947 static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port,
1948 const unsigned char *data, int count)
1957 struct ATENINTL_port *ATEN2011_port;
1958 struct usb_serial *serial;
1959 struct ATENINTL_serial *ATEN2011_serial;
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);
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);
1977 //status = ATEN2011_set_Uart_Reg(port,LINE_CONTROL_REGISTER,Data);
1978 //ATEN2011_port->shadowLCR=Data;//Need to add later
1980 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1982 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1985 //status = ATEN2011_set_Uart_Reg(port,DIVISOR_LATCH_LSB,Data);
1988 status = ATEN2011_get_Uart_Reg(port, DIVISOR_LATCH_LSB, &Data);
1989 DPRINTK("ATEN2011_write:DLL value is %x\n", Data);
1993 status = ATEN2011_get_Uart_Reg(port, DIVISOR_LATCH_MSB, &Data);
1994 DPRINTK("ATEN2011_write:DLM value is %x\n", Data);
1996 Data = Data & ~SERIAL_LCR_DLAB;
1997 DPRINTK("ATEN2011_write: ATEN2011_port->shadowLCR is %x\n",
1998 ATEN2011_port->shadowLCR);
2000 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2003 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2004 DPRINTK("%s", "Port Paranoia failed \n");
2008 serial = port->serial;
2009 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
2010 DPRINTK("%s", "Serial Paranoia failed \n");
2014 ATEN2011_port = ATEN2011_get_port_private(port);
2015 if (ATEN2011_port == NULL) {
2016 DPRINTK("%s", "ATEN2011_port is NULL\n");
2020 ATEN2011_serial = ATEN2011_get_serial_private(serial);
2021 if (ATEN2011_serial == NULL) {
2022 DPRINTK("%s", "ATEN2011_serial is NULL \n");
2026 /* try to find a free urb in the list */
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);
2038 dbg("%s - no more free urbs", __FUNCTION__);
2042 if (urb->transfer_buffer == NULL) {
2043 urb->transfer_buffer =
2044 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
2046 if (urb->transfer_buffer == NULL) {
2047 err("%s no more kernel memory...", __FUNCTION__);
2051 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
2055 (urb->transfer_buffer, current_position, transfer_size)) {
2056 bytes_sent = -EFAULT;
2060 memcpy(urb->transfer_buffer, current_position, transfer_size);
2062 //usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
2064 /* fill urb with data and submit */
2065 minor = port->serial->minor;
2066 if (minor == SERIAL_TTY_NO_MINOR) ;
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,
2073 bulk_out_endpointAddress) +
2074 2), urb->transfer_buffer,
2076 ATEN2011_bulk_out_data_callback,
2080 usb_fill_bulk_urb(urb,
2081 ATEN2011_serial->serial->dev,
2082 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
2084 bulk_out_endpointAddress),
2085 urb->transfer_buffer, transfer_size,
2086 ATEN2011_bulk_out_data_callback,
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]);
2094 /* send it down the pipe */
2095 status = usb_submit_urb(urb, GFP_ATOMIC);
2098 err("%s - usb_submit_urb(write bulk) failed with status = %d",
2099 __FUNCTION__, status);
2100 bytes_sent = status;
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);
2112 /*****************************************************************************
2114 * this function is called by the tty driver when it wants to stop the data
2115 * being read from the port.
2116 *****************************************************************************/
2118 static void ATEN2011_throttle(struct tty_struct *tty)
2120 struct usb_serial_port *port = tty->driver_data;
2121 struct ATENINTL_port *ATEN2011_port;
2124 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2125 DPRINTK("%s", "Invalid port \n");
2129 DPRINTK("- port %d\n", port->number);
2131 ATEN2011_port = ATEN2011_get_port_private(port);
2133 if (ATEN2011_port == NULL)
2136 if (!ATEN2011_port->open) {
2137 DPRINTK("%s\n", "port not opened");
2141 DPRINTK("%s", "Entering .......... \n");
2144 dbg("%s - no tty available", __FUNCTION__);
2148 /* if we are implementing XON/XOFF, send the stop character */
2150 unsigned char stop_char = STOP_CHAR(tty);
2151 status = ATEN2011_write(tty, port, &stop_char, 1); //FC4
2157 /* if we are implementing RTS/CTS, toggle that line */
2158 if (tty->termios->c_cflag & CRTSCTS) {
2159 ATEN2011_port->shadowMCR &= ~MCR_RTS;
2162 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
2163 ATEN2011_port->shadowMCR);
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)
2180 struct usb_serial_port *port = tty->driver_data;
2182 struct ATENINTL_port *ATEN2011_port = ATEN2011_get_port_private(port);
2184 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2185 DPRINTK("%s", "Invalid port \n");
2189 if (ATEN2011_port == NULL)
2192 if (!ATEN2011_port->open) {
2193 dbg("%s - port not opened", __FUNCTION__);
2197 DPRINTK("%s", "Entering .......... \n");
2200 dbg("%s - no tty available", __FUNCTION__);
2204 /* if we are implementing XON/XOFF, send the start character */
2206 unsigned char start_char = START_CHAR(tty);
2207 status = ATEN2011_write(tty, port, &start_char, 1); //FC4
2213 /* if we are implementing RTS/CTS, toggle that line */
2214 if (tty->termios->c_cflag & CRTSCTS) {
2215 ATEN2011_port->shadowMCR |= MCR_RTS;
2218 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
2219 ATEN2011_port->shadowMCR);
2228 static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file)
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;
2238 ATEN2011_port = ATEN2011_get_port_private(port);
2240 DPRINTK("%s - port %d", __FUNCTION__, port->number);
2242 if (ATEN2011_port == NULL)
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);
2257 DPRINTK("%s - 0x%04X", __FUNCTION__, result);
2262 static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
2263 unsigned int set, unsigned int clear)
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);
2269 unsigned int status;
2271 DPRINTK("%s - port %d", __FUNCTION__, port->number);
2273 ATEN2011_port = ATEN2011_get_port_private(port);
2275 if (ATEN2011_port == NULL)
2278 mcr = ATEN2011_port->shadowMCR;
2279 if (clear & TIOCM_RTS)
2281 if (clear & TIOCM_DTR)
2283 if (clear & TIOCM_LOOP)
2284 mcr &= ~MCR_LOOPBACK;
2286 if (set & TIOCM_RTS)
2288 if (set & TIOCM_DTR)
2290 if (set & TIOCM_LOOP)
2291 mcr |= MCR_LOOPBACK;
2293 ATEN2011_port->shadowMCR = mcr;
2296 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, mcr);
2298 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
2305 /*****************************************************************************
2307 * this function is called by the tty driver when it wants to change the termios structure
2308 *****************************************************************************/
2310 static void ATEN2011_set_termios(struct tty_struct *tty,
2311 struct usb_serial_port *port,
2312 struct ktermios *old_termios)
2316 struct usb_serial *serial;
2317 struct ATENINTL_port *ATEN2011_port;
2319 DPRINTK("ATEN2011_set_termios: START\n");
2320 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2321 DPRINTK("%s", "Invalid port \n");
2325 serial = port->serial;
2327 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
2328 DPRINTK("%s", "Invalid Serial \n");
2332 ATEN2011_port = ATEN2011_get_port_private(port);
2334 if (ATEN2011_port == NULL)
2337 if (!ATEN2011_port->open) {
2338 dbg("%s - port not opened", __FUNCTION__);
2342 DPRINTK("%s\n", "setting termios - ");
2344 cflag = tty->termios->c_cflag;
2347 DPRINTK("%s %s\n", __FUNCTION__, "cflag is NULL");
2351 /* check that they really want us to change something */
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");
2361 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2362 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2365 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2366 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2369 dbg("%s - port %d", __FUNCTION__, port->number);
2371 /* change the port settings to the new ones specified */
2373 ATEN2011_change_port_settings(tty, ATEN2011_port, old_termios);
2375 if (!ATEN2011_port->read_urb) {
2376 DPRINTK("%s", "URB KILLED !!!!!\n");
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);
2385 (" usb_submit_urb(read bulk) failed, status = %d",
2393 static void ATEN2011_break_ctl( struct usb_serial_port *port, int break_state )
2395 //struct usb_serial *serial = port->serial;
2397 // if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
2398 // err("Set break_ctl %d", break_state);
2402 /*****************************************************************************
2403 * get_lsr_info - get line status register info
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 *****************************************************************************/
2413 static int get_lsr_info(struct tty_struct *tty,
2414 struct ATENINTL_port *ATEN2011_port,
2415 unsigned int *value)
2418 unsigned int result = 0;
2420 count = ATEN2011_chars_in_buffer(tty);
2422 dbg("%s -- Empty", __FUNCTION__);
2423 result = TIOCSER_TEMT;
2426 if (copy_to_user(value, &result, sizeof(int)))
2431 /*****************************************************************************
2432 * get_number_bytes_avail - get number of bytes available
2434 * Purpose: Let user call ioctl to get the count of number of bytes available.
2435 *****************************************************************************/
2437 static int get_number_bytes_avail(struct tty_struct *tty,
2438 struct ATENINTL_port *ATEN2011_port,
2439 unsigned int *value)
2441 unsigned int result = 0;
2444 return -ENOIOCTLCMD;
2446 result = tty->read_cnt;
2448 dbg("%s(%d) = %d", __FUNCTION__, ATEN2011_port->port->number, result);
2449 if (copy_to_user(value, &result, sizeof(int)))
2452 return -ENOIOCTLCMD;
2455 /*****************************************************************************
2457 * function to set modem info
2458 *****************************************************************************/
2460 static int set_modem_info(struct ATENINTL_port *ATEN2011_port, unsigned int cmd,
2461 unsigned int *value)
2467 struct usb_serial_port *port;
2469 if (ATEN2011_port == NULL)
2472 port = (struct usb_serial_port *)ATEN2011_port->port;
2473 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2474 DPRINTK("%s", "Invalid port \n");
2478 mcr = ATEN2011_port->shadowMCR;
2480 if (copy_from_user(&arg, value, sizeof(int)))
2485 if (arg & TIOCM_RTS)
2487 if (arg & TIOCM_DTR)
2489 if (arg & TIOCM_LOOP)
2490 mcr |= MCR_LOOPBACK;
2494 if (arg & TIOCM_RTS)
2496 if (arg & TIOCM_DTR)
2498 if (arg & TIOCM_LOOP)
2499 mcr &= ~MCR_LOOPBACK;
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);
2512 ATEN2011_port->shadowMCR = mcr;
2514 Data = ATEN2011_port->shadowMCR;
2516 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2518 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
2525 /*****************************************************************************
2527 * function to get modem info
2528 *****************************************************************************/
2530 static int get_modem_info(struct ATENINTL_port *ATEN2011_port,
2531 unsigned int *value)
2533 unsigned int result = 0;
2535 unsigned int mcr = ATEN2011_port->shadowMCR;
2538 ATEN2011_get_Uart_Reg(ATEN2011_port->port, MODEM_STATUS_REGISTER,
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 */
2547 dbg("%s -- %x", __FUNCTION__, result);
2549 if (copy_to_user(value, &result, sizeof(int)))
2554 /*****************************************************************************
2556 * function to get information about serial port
2557 *****************************************************************************/
2559 static int get_serial_info(struct ATENINTL_port *ATEN2011_port,
2560 struct serial_struct *retinfo)
2562 struct serial_struct tmp;
2564 if (ATEN2011_port == NULL)
2570 memset(&tmp, 0, sizeof(tmp));
2572 tmp.type = PORT_16550A;
2573 tmp.line = ATEN2011_port->port->serial->minor;
2574 if (tmp.line == SERIAL_TTY_NO_MINOR)
2576 tmp.port = ATEN2011_port->port->number;
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;
2584 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2589 /*****************************************************************************
2591 * this function handles any ioctl calls to the driver
2592 *****************************************************************************/
2594 static int ATEN2011_ioctl(struct tty_struct *tty, struct file *file,
2595 unsigned int cmd, unsigned long arg)
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;
2604 //struct tty_ldisc *ld;
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");
2612 ATEN2011_port = ATEN2011_get_port_private(port);
2614 if (ATEN2011_port == NULL)
2617 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2620 /* return number of bytes available */
2623 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2624 return get_number_bytes_avail(tty, ATEN2011_port,
2625 (unsigned int *)arg);
2629 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2630 return put_user(ATEN2011_chars_in_buffer(tty),
2636 retval = tty_check_change(tty);
2640 ld = tty_ldisc_ref(tty);
2643 if (ld && ld->flush_buffer)
2644 ld->flush_buffer(tty);
2647 if (ld && ld->flush_buffer)
2648 ld->flush_buffer(tty);
2651 if (tty->driver->flush_buffer)
2652 tty->driver->flush_buffer(tty);
2655 tty_ldisc_deref(ld);
2658 tty_ldisc_deref(ld);
2662 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2663 return get_lsr_info(tty, ATEN2011_port, (unsigned int *)arg);
2669 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2671 // printk("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__, port->number);
2673 set_modem_info(ATEN2011_port, cmd, (unsigned int *)arg);
2674 // printk(" %s: ret:%d\n",__FUNCTION__,ATENret);
2678 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2679 return get_modem_info(ATEN2011_port, (unsigned int *)arg);
2682 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2683 return get_serial_info(ATEN2011_port,
2684 (struct serial_struct *)arg);
2687 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2691 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2692 cprev = ATEN2011_port->icount;
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));
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))) {
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;
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)))
2742 return -ENOIOCTLCMD;
2745 /*****************************************************************************
2746 * ATEN2011_send_cmd_write_baud_rate
2747 * this function sends the proper command to change the baud rate of the
2749 *****************************************************************************/
2751 static int ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port
2752 *ATEN2011_port, int baudRate)
2757 unsigned char number;
2759 struct usb_serial_port *port;
2762 if (ATEN2011_port == NULL)
2765 port = (struct usb_serial_port *)ATEN2011_port->port;
2766 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2767 DPRINTK("%s", "Invalid port \n");
2771 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2772 DPRINTK("%s", "Invalid Serial \n");
2776 DPRINTK("%s", "Entering .......... \n");
2778 minor = ATEN2011_port->port->serial->minor;
2779 if (minor == SERIAL_TTY_NO_MINOR)
2781 number = ATEN2011_port->port->number - minor;
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;
2791 //Data = ATEN2011_port->shadowMCR ;
2793 ATEN2011_port->shadowMCR = Data;
2795 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2797 DPRINTK("Writing spreg failed in set_serial_baud\n");
2803 #ifdef HW_flow_control
2804 //setting h/w flow control bit to 0;
2806 //Data = ATEN2011_port->shadowMCR ;
2808 ATEN2011_port->shadowMCR = Data;
2810 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2812 DPRINTK("Writing spreg failed in set_serial_baud\n");
2819 if (1) //baudRate <= 115200)
2825 ATEN2011_calc_baud_rate_divisor(baudRate, &divisor,
2828 ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset,
2831 DPRINTK("reading spreg failed in set_serial_baud\n");
2834 Data = (Data & 0x8f) | clk_sel_val;
2837 ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset,
2840 DPRINTK("Writing spreg failed in set_serial_baud\n");
2843 /* Calculate the Divisor */
2846 err("%s - bad baud rate", __FUNCTION__);
2847 DPRINTK("%s\n", "bad baud rate");
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);
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);
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);
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);
2874 /*****************************************************************************
2875 * ATEN2011_calc_baud_rate_divisor
2876 * this function calculates the proper baud rate divisor for the specified
2878 *****************************************************************************/
2879 static int ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,
2880 __u16 * clk_sel_val)
2883 //__u16 custom,round1, round;
2885 dbg("%s - %d", __FUNCTION__, baudRate);
2887 if (baudRate <= 115200) {
2888 *divisor = 115200 / baudRate;
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;
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;
2924 /* After trying for all the standard baud rates *
2925 * Try calculating the divisor for this baud rate */
2927 if (baudrate > 75 && baudrate < 230400) {
2928 /* get the divisor */
2929 custom = (__u16) (230400L / baudrate);
2931 /* Check for round off */
2932 round1 = (__u16) (2304000L / baudrate);
2933 round = (__u16) (round1 - (custom * 10));
2939 DPRINTK(" Baud %d = %d\n", baudrate, custom);
2943 DPRINTK("%s\n", " Baud calculation Failed...");
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 *****************************************************************************/
2954 static void ATEN2011_change_port_settings(struct tty_struct *tty,
2955 struct ATENINTL_port *ATEN2011_port,
2956 struct ktermios *old_termios)
2967 struct usb_serial_port *port;
2968 struct usb_serial *serial;
2970 if (ATEN2011_port == NULL)
2973 port = (struct usb_serial_port *)ATEN2011_port->port;
2975 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2976 DPRINTK("%s", "Invalid port \n");
2980 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2981 DPRINTK("%s", "Invalid Serial \n");
2985 serial = port->serial;
2987 dbg("%s - port %d", __FUNCTION__, ATEN2011_port->port->number);
2989 if ((!ATEN2011_port->open) && (!ATEN2011_port->openPending)) {
2990 dbg("%s - port not opened", __FUNCTION__);
2994 if ((!tty) || (!tty->termios)) {
2995 dbg("%s - no tty structures", __FUNCTION__);
2999 DPRINTK("%s", "Entering .......... \n");
3003 lParity = LCR_PAR_NONE;
3005 cflag = tty->termios->c_cflag;
3006 iflag = tty->termios->c_iflag;
3008 /* Change the number of bits */
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
3013 switch (cflag & CSIZE) {
3034 /* Change the Parity bit */
3035 if (cflag & PARENB) {
3036 if (cflag & PARODD) {
3037 lParity = LCR_PAR_ODD;
3038 dbg("%s - parity = odd", __FUNCTION__);
3040 lParity = LCR_PAR_EVEN;
3041 dbg("%s - parity = even", __FUNCTION__);
3045 dbg("%s - parity = none", __FUNCTION__);
3048 if (cflag & CMSPAR) {
3049 lParity = lParity | 0x20;
3052 /* Change the Stop bit */
3053 if (cflag & CSTOPB) {
3055 dbg("%s - stop bits = 2", __FUNCTION__);
3058 dbg("%s - stop bits = 1", __FUNCTION__);
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);
3066 ATEN2011_port->validDataMask = mask;
3068 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is %x\n",
3069 ATEN2011_port->shadowLCR);
3070 /* Disable Interrupts */
3072 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
3075 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
3078 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
3080 /* Send the updated LCR value to the ATEN2011 */
3081 Data = ATEN2011_port->shadowLCR;
3083 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
3086 ATEN2011_port->shadowMCR = Data;
3087 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3089 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3091 /* set up the MCR register and send it to the ATEN2011 */
3093 ATEN2011_port->shadowMCR = MCR_MASTER_IE;
3094 if (cflag & CBAUD) {
3095 ATEN2011_port->shadowMCR |= (MCR_DTR | MCR_RTS);
3098 if (cflag & CRTSCTS) {
3099 ATEN2011_port->shadowMCR |= (MCR_XON_ANY);
3102 ATEN2011_port->shadowMCR &= ~(MCR_XON_ANY);
3105 Data = ATEN2011_port->shadowMCR;
3106 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
3108 /* Determine divisor based on baud rate */
3109 baud = tty_get_baud_rate(tty);
3112 /* pick a default, any default... */
3113 DPRINTK("%s\n", "Picked default baud...");
3117 dbg("%s - baud rate = %d", __FUNCTION__, baud);
3118 status = ATEN2011_send_cmd_write_baud_rate(ATEN2011_port, baud);
3120 /* Enable Interrupts */
3122 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
3124 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
3125 ATEN2011_port->read_urb->dev = serial->dev;
3127 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
3131 (" usb_submit_urb(read bulk) failed, status = %d",
3135 //wake_up(&ATEN2011_port->delta_msr_wait);
3136 //ATEN2011_port->delta_msr_cond=1;
3138 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is End %x\n",
3139 ATEN2011_port->shadowLCR);
3144 static int ATEN2011_calc_num_ports(struct usb_serial *serial)
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);
3154 //printk("ATEN2011_calc_num_ports GPIO is %x\n",Data);
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;
3165 //else if(serial->interface->cur_altsetting->desc.bNumEndpoints == 9)
3167 ATEN2011_2or4ports = 4;
3168 serial->type->num_ports = 4;
3169 serial->num_ports = 4;
3173 return ATEN2011_2or4ports;
3176 /****************************************************************************
3178 ****************************************************************************/
3180 static int ATEN2011_startup(struct usb_serial *serial)
3182 struct ATENINTL_serial *ATEN2011_serial;
3183 struct ATENINTL_port *ATEN2011_port;
3184 struct usb_device *dev;
3189 DPRINTK("%s \n", " ATEN2011_startup :entering..........");
3192 DPRINTK("%s\n", "Invalid Handler");
3198 DPRINTK("%s\n", "Entering...");
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__);
3207 /* resetting the private structure field values to zero */
3208 memset(ATEN2011_serial, 0, sizeof(struct ATENINTL_serial));
3210 ATEN2011_serial->serial = serial;
3211 //initilize status polling flag to FALSE
3212 ATEN2011_serial->status_polling_started = FALSE;
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. */
3220 /* set up port private structures */
3221 for (i = 0; i < serial->num_ports; ++i) {
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);
3230 memset(ATEN2011_port, 0, sizeof(struct ATENINTL_port));
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 */
3235 // serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
3237 ATEN2011_port->port = serial->port[i];
3239 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
3241 minor = serial->port[i]->serial->minor;
3242 if (minor == SERIAL_TTY_NO_MINOR)
3244 ATEN2011_port->port_num =
3245 ((serial->port[i]->number - minor) + 1);
3247 ATEN2011_port->AppNum = (((__u16) serial->port[i]->number -
3248 (__u16) (minor)) + 1) << 8;
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 ==
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 ==
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 ==
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 ==
3279 ATEN2011_port->SpRegOffset = 0xc;
3280 ATEN2011_port->ControlRegOffset = 0xd;
3281 ATEN2011_port->DcrRegOffset = 0x1c;
3282 //ATEN2011_port->ClkSelectRegOffset = ;
3284 ATEN2011_Dump_serial_port(ATEN2011_port);
3286 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
3288 //enable rx_disable bit in control register
3291 ATEN2011_get_reg_sync(serial->port[i],
3292 ATEN2011_port->ControlRegOffset,
3295 DPRINTK("Reading ControlReg failed status-0x%x\n",
3300 ("ControlReg Reading success val is %x, status%d\n",
3302 Data |= 0x08; //setting driver done bit
3303 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
3305 //Data |= 0x20; //rx_disable bit
3308 ATEN2011_set_reg_sync(serial->port[i],
3309 ATEN2011_port->ControlRegOffset,
3313 ("Writing ControlReg failed(rx_disable) status-0x%x\n",
3318 ("ControlReg Writing success(rx_disable) status%d\n",
3321 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
3325 ATEN2011_set_reg_sync(serial->port[i],
3326 (__u16) (ATEN2011_port->DcrRegOffset +
3329 DPRINTK("Writing DCR0 failed status-0x%x\n", status);
3332 DPRINTK("DCR0 Writing success status%d\n", status);
3337 ATEN2011_set_reg_sync(serial->port[i],
3338 (__u16) (ATEN2011_port->DcrRegOffset +
3341 DPRINTK("Writing DCR1 failed status-0x%x\n", status);
3344 DPRINTK("DCR1 Writing success status%d\n", status);
3349 ATEN2011_set_reg_sync(serial->port[i],
3350 (__u16) (ATEN2011_port->DcrRegOffset +
3353 DPRINTK("Writing DCR2 failed status-0x%x\n", status);
3356 DPRINTK("DCR2 Writing success status%d\n", status);
3358 // write values in clkstart0x0 and clkmulti 0x20
3362 ATEN2011_set_reg_sync(serial->port[i],
3363 CLK_START_VALUE_REGISTER, Data);
3366 ("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n",
3371 ("CLK_START_VALUE_REGISTER Writing success status%d\n",
3377 ATEN2011_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
3381 ("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
3385 DPRINTK("CLK_MULTI_REGISTER Writing success status%d\n",
3388 //write value 0x0 to scratchpad register
3395 status=ATEN2011_set_Uart_Reg(serial->port[i],SCRATCH_PAD_REGISTER,Data);
3397 DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
3400 else DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",status);
3404 //Threshold Registers
3405 if(ATEN2011_serial->ATEN2011_spectrum_2or4ports==4)
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));
3413 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3416 else DPRINTK("THRESHOLD_VAL Writing success status%d\n",status);
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));
3425 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3428 else DPRINTK("THRESHOLD_VAL Writing success status%d\n",status);
3435 if(ATEN2011_port->port_num==1)
3439 status=ATEN2011_set_reg_sync(serial->port[i],\
3441 DPRINTK("THRESHOLD_VAL offset is 0x3f\n");
3443 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3448 status=ATEN2011_set_reg_sync(serial->port[i],\
3450 DPRINTK("THRESHOLD_VAL offset is 0x40\n");
3452 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3461 status=ATEN2011_set_reg_sync(serial->port[i],\
3463 DPRINTK("THRESHOLD_VAL offset is 0x43\n");
3465 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3470 status=ATEN2011_set_reg_sync(serial->port[i],\
3472 DPRINTK("THRESHOLD_VAL offset is 0x44\n");
3474 DPRINTK("Writing THRESHOLD_VAL failed status-0x%x\n",status);
3483 //Zero Length flag register
3484 if ((ATEN2011_port->port_num != 1)
3485 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)) {
3489 status = ATEN2011_set_reg_sync(serial->port[i],
3495 DPRINTK("ZLIP offset%x\n",
3497 ((__u16) ATEN2011_port->port_num)));
3500 ("Writing ZLP_REG%d failed status-0x%x\n",
3504 DPRINTK("ZLP_REG%d Writing success status%d\n",
3509 status = ATEN2011_set_reg_sync(serial->port[i],
3515 DPRINTK("ZLIP offset%x\n",
3517 ((__u16) ATEN2011_port->port_num) -
3521 ("Writing ZLP_REG%d failed status-0x%x\n",
3525 DPRINTK("ZLP_REG%d Writing success status%d\n",
3529 ATEN2011_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC);
3530 ATEN2011_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
3534 ATEN2011_Thr_cnt = 0;
3535 //Zero Length flag enable
3538 status = ATEN2011_set_reg_sync(serial->port[0], ZLP_REG5, Data);
3540 DPRINTK("Writing ZLP_REG5 failed status-0x%x\n", status);
3543 DPRINTK("ZLP_REG5 Writing success status%d\n", status);
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;
3552 /****************************************************************************
3554 * This function is called whenever the device is removed from the usb bus.
3555 ****************************************************************************/
3557 static void ATEN2011_shutdown(struct usb_serial *serial)
3560 struct ATENINTL_port *ATEN2011_port;
3561 DPRINTK("%s \n", " shutdown :entering..........");
3568 DPRINTK("%s", "Invalid Handler \n");
3572 /* check for the ports to be closed,close the ports and disconnect */
3574 /* free private structure allocated for serial port *
3575 * stop reads and writes on all ports */
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);
3585 /* free private structure allocated for serial device */
3587 kfree(ATEN2011_get_serial_private(serial));
3588 ATEN2011_set_serial_private(serial, NULL);
3590 DPRINTK("%s\n", "Thank u :: ");
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)
3599 dbg("%s - serial == NULL", function);
3602 // if (serial->magic != USB_SERIAL_MAGIC) {
3603 // dbg("%s - bad magic number for serial", function);
3606 if (!serial->type) {
3607 dbg("%s - serial->type == NULL!", function);
3613 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port,
3614 const char *function)
3617 dbg("%s - port == NULL", function);
3620 // if (port->magic != USB_SERIAL_PORT_MAGIC) {
3621 // dbg("%s - bad magic number for port", function);
3624 if (!port->serial) {
3625 dbg("%s - port->serial == NULL", function);
3631 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port,
3632 const char *function)
3634 /* if no port was specified, or it fails a paranoia check */
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 */
3642 return port->serial;
3645 /****************************************************************************
3647 * This is called by the module subsystem, or on startup to initialize us
3648 ****************************************************************************/
3649 int __init ATENINTL2011_init(void)
3653 DPRINTK("%s \n", " ATEN2011_init :entering..........");
3655 /* Register with the usb serial */
3656 retval = usb_serial_register(&ATENINTL2011_4port_device);
3659 goto failed_port_device_register;
3661 /* info(DRIVER_DESC " " DRIVER_VERSION); */
3662 printk(KERN_INFO KBUILD_MODNAME ":"
3663 DRIVER_DESC " " DRIVER_VERSION "\n");
3665 /* Register with the usb */
3666 retval = usb_register(&io_driver);
3669 goto failed_usb_register;
3672 DPRINTK("%s\n", "Leaving...");
3676 failed_usb_register:
3677 usb_serial_deregister(&ATENINTL2011_4port_device);
3679 failed_port_device_register:
3684 /****************************************************************************
3686 * Called when the driver is about to be unloaded.
3687 ****************************************************************************/
3688 void __exit ATENINTL2011_exit(void)
3691 DPRINTK("%s \n", " ATEN2011_exit :entering..........");
3693 usb_deregister(&io_driver);
3695 usb_serial_deregister(&ATENINTL2011_4port_device);
3697 DPRINTK("%s\n", "End...");
3700 module_init(ATENINTL2011_init);
3701 module_exit(ATENINTL2011_exit);
3703 /* Module information */
3704 MODULE_DESCRIPTION(DRIVER_DESC);
3705 MODULE_LICENSE("GPL");
3707 MODULE_PARM_DESC(debug, "Debug enabled or not");