2 * Aten 2011 USB serial driver for 4 port devices
4 * Copyright (C) 2000 Inside Out Networks
5 * Copyright (C) 2001-2002, 2009 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2009 Novell Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/tty.h>
20 #include <linux/tty_driver.h>
21 #include <linux/tty_flip.h>
22 #include <linux/module.h>
23 #include <linux/serial.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
26 #include <asm/uaccess.h>
29 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
30 #define ZLP_REG2 0x3B /* Zero_Flag_Reg2 59 */
31 #define ZLP_REG3 0x3C /* Zero_Flag_Reg3 60 */
32 #define ZLP_REG4 0x3D /* Zero_Flag_Reg4 61 */
33 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
35 /* Interrupt Rotinue Defines */
36 #define SERIAL_IIR_RLS 0x06
37 #define SERIAL_IIR_RDA 0x04
38 #define SERIAL_IIR_CTI 0x0c
39 #define SERIAL_IIR_THR 0x02
40 #define SERIAL_IIR_MS 0x00
42 /* Emulation of the bit mask on the LINE STATUS REGISTER. */
43 #define SERIAL_LSR_DR 0x0001
44 #define SERIAL_LSR_OE 0x0002
45 #define SERIAL_LSR_PE 0x0004
46 #define SERIAL_LSR_FE 0x0008
47 #define SERIAL_LSR_BI 0x0010
48 #define SERIAL_LSR_THRE 0x0020
49 #define SERIAL_LSR_TEMT 0x0040
50 #define SERIAL_LSR_FIFOERR 0x0080
52 /* MSR bit defines(place holders) */
53 #define ATEN_MSR_DELTA_CTS 0x10
54 #define ATEN_MSR_DELTA_DSR 0x20
55 #define ATEN_MSR_DELTA_RI 0x40
56 #define ATEN_MSR_DELTA_CD 0x80
58 /* Serial Port register Address */
59 #define RECEIVE_BUFFER_REGISTER ((__u16)(0x00))
60 #define TRANSMIT_HOLDING_REGISTER ((__u16)(0x00))
61 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
62 #define INTERRUPT_IDENT_REGISTER ((__u16)(0x02))
63 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
64 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
65 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
66 #define LINE_STATUS_REGISTER ((__u16)(0x05))
67 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
68 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
69 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
70 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
72 #define SP1_REGISTER ((__u16)(0x00))
73 #define CONTROL1_REGISTER ((__u16)(0x01))
74 #define CLK_MULTI_REGISTER ((__u16)(0x02))
75 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
76 #define DCR1_REGISTER ((__u16)(0x04))
77 #define GPIO_REGISTER ((__u16)(0x07))
79 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
82 * URB POOL related defines
84 #define NUM_URBS 16 /* URB Count */
85 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
87 #define USB_VENDOR_ID_ATENINTL 0x0557
88 #define ATENINTL_DEVICE_ID_2011 0x2011
89 #define ATENINTL_DEVICE_ID_7820 0x7820
91 static struct usb_device_id id_table [] = {
92 { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_2011) },
93 { USB_DEVICE(USB_VENDOR_ID_ATENINTL,ATENINTL_DEVICE_ID_7820) },
94 { } /* terminating entry */
96 MODULE_DEVICE_TABLE (usb, id_table);
98 /* This structure holds all of the local port information */
101 int port_num; /*Actual port number in the device(1,2,etc)*/
102 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
103 unsigned char *bulk_out_buffer; /* buffer used for the bulk out endpoint */
104 struct urb *write_urb; /* write URB for this port */
105 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
106 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
107 struct urb *read_urb; /* read URB for this port */
108 __s16 rxBytesAvail;/*the number of bytes that we need to read from this device */
109 __s16 rxBytesRemaining; /* the number of port bytes left to read */
110 char write_in_progress; /* TRUE while a write URB is outstanding */
111 __u8 shadowLCR; /* last LCR value received */
112 __u8 shadowMCR; /* last MCR value received */
113 __u8 shadowMSR; /* last MSR value received */
114 __u8 shadowLSR; /* last LSR value received */
115 __u8 shadowXonChar; /* last value set as XON char in ATENINTL */
116 __u8 shadowXoffChar; /* last value set as XOFF char in ATENINTL */
123 char chaseResponsePending;
124 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
125 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
126 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
127 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
129 struct async_icount icount;
130 struct usb_serial_port *port; /* loop back to the owner of this object */
134 __u8 ControlRegOffset;
136 //for processing control URBS in interrupt context
137 struct urb *control_urb;
142 struct urb *write_urb_pool[NUM_URBS];
143 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
144 struct ktermios tmp_termios; /* stores the old termios settings */
145 spinlock_t lock; /* private lock */
149 /* This structure holds all of the individual serial device information */
150 struct ATENINTL_serial
152 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
153 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
154 struct urb * interrupt_read_urb; /* our interrupt urb */
155 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
156 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
157 struct urb *read_urb; /* our bulk read urb */
158 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
159 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
160 __u8 rxPort; /* the port that we are currently receiving data for */
161 __u8 rxStatusCode; /* the receive status code */
162 __u8 rxStatusParam; /* the receive status paramater */
163 __s16 rxBytesRemaining; /* the number of port bytes left to read */
164 struct usb_serial *serial; /* loop back to the owner of this object */
165 int ATEN2011_spectrum_2or4ports; //this says the number of ports in the device
166 // Indicates about the no.of opened ports of an individual USB-serial adapater.
167 unsigned int NoOfOpenPorts;
168 // a flag for Status endpoint polling
169 unsigned char status_polling_started;
172 static void ATEN2011_set_termios(struct tty_struct *tty,
173 struct usb_serial_port *port,
174 struct ktermios *old_termios);
175 static void ATEN2011_change_port_settings(struct tty_struct *tty,
176 struct ATENINTL_port *ATEN2011_port,
177 struct ktermios *old_termios);
179 /*************************************
180 * Bit definitions for each register *
181 *************************************/
182 #define LCR_BITS_5 0x00 /* 5 bits/char */
183 #define LCR_BITS_6 0x01 /* 6 bits/char */
184 #define LCR_BITS_7 0x02 /* 7 bits/char */
185 #define LCR_BITS_8 0x03 /* 8 bits/char */
186 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
188 #define LCR_STOP_1 0x00 /* 1 stop bit */
189 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
190 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
191 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
193 #define LCR_PAR_NONE 0x00 /* No parity */
194 #define LCR_PAR_ODD 0x08 /* Odd parity */
195 #define LCR_PAR_EVEN 0x18 /* Even parity */
196 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
197 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
198 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
200 #define LCR_SET_BREAK 0x40 /* Set Break condition */
201 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
203 #define MCR_DTR 0x01 /* Assert DTR */
204 #define MCR_RTS 0x02 /* Assert RTS */
205 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
206 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
207 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
208 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
210 #define ATEN2011_MSR_CTS 0x10 /* Current state of CTS */
211 #define ATEN2011_MSR_DSR 0x20 /* Current state of DSR */
212 #define ATEN2011_MSR_RI 0x40 /* Current state of RI */
213 #define ATEN2011_MSR_CD 0x80 /* Current state of CD */
216 /* 1: Enables the debugging -- 0: Disable the debugging */
220 static int debug = 0;
221 #define DPRINTK(fmt, args...) printk( "%s: " fmt, __FUNCTION__ , ## args)
224 static int debug = 0;
225 #define DPRINTK(fmt, args...)
230 * Version Information
232 #define DRIVER_VERSION "2.0"
233 #define DRIVER_DESC "ATENINTL 2011 USB Serial Adapter"
236 * Defines used for sending commands to port
239 #define ATEN_WDR_TIMEOUT (50) /* default urb timeout */
242 #define ATEN_RD_RTYPE 0xC0
243 #define ATEN_WR_RTYPE 0x40
244 #define ATEN_RDREQ 0x0D
245 #define ATEN_WRREQ 0x0E
246 #define ATEN_CTRL_TIMEOUT 500
247 #define VENDOR_READ_LENGTH (0x01)
249 /* set to 1 for RS485 mode and 0 for RS232 mode */
250 /* FIXME make this somehow dynamic and not build time specific */
251 static int RS485mode = 0;
253 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port, const
255 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial, const char
257 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port, const char
260 /* setting and get register values */
261 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
263 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
265 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
267 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
270 static void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port);
273 static inline void ATEN2011_set_serial_private(struct usb_serial *serial,
274 struct ATENINTL_serial *data)
276 usb_set_serial_data(serial, (void *)data);
279 static inline struct ATENINTL_serial *ATEN2011_get_serial_private(struct
283 return (struct ATENINTL_serial *)usb_get_serial_data(serial);
286 static inline void ATEN2011_set_port_private(struct usb_serial_port *port,
287 struct ATENINTL_port *data)
289 usb_set_serial_port_data(port, (void *)data);
292 static inline struct ATENINTL_port *ATEN2011_get_port_private(struct
296 return (struct ATENINTL_port *)usb_get_serial_port_data(port);
299 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
302 struct usb_device *dev = port->serial->dev;
304 DPRINTK("ATEN2011_set_reg_sync offset is %x, value %x\n", reg, val);
306 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
307 ATEN_WR_RTYPE, val, reg, NULL, 0,
311 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
314 struct usb_device *dev = port->serial->dev;
317 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
318 ATEN_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
320 DPRINTK("ATEN2011_get_reg_sync offset is %x, return val %x\n", reg,
322 *val = (*val) & 0x00ff;
326 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
330 struct usb_device *dev = port->serial->dev;
331 struct ATENINTL_serial *ATEN2011_serial;
333 ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
334 minor = port->serial->minor;
335 if (minor == SERIAL_TTY_NO_MINOR)
338 // For the UART control registers, the application number need to be Or'ed
340 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
341 val |= (((__u16) port->number - (__u16) (minor)) + 1) << 8;
342 DPRINTK("ATEN2011_set_Uart_Reg application number is %x\n",
345 if (((__u16) port->number - (__u16) (minor)) == 0) {
348 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
350 ("ATEN2011_set_Uart_Reg application number is %x\n",
355 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
357 ("ATEN2011_set_Uart_Reg application number is %x\n",
361 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
362 ATEN_WR_RTYPE, val, reg, NULL, 0,
367 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
370 struct usb_device *dev = port->serial->dev;
373 struct ATENINTL_serial *ATEN2011_serial;
374 int minor = port->serial->minor;
375 ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
376 if (minor == SERIAL_TTY_NO_MINOR)
379 //DPRINTK("application number is %4x \n",(((__u16)port->number - (__u16)(minor))+1)<<8);
380 /*Wval is same as application number */
381 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
382 Wval = (((__u16) port->number - (__u16) (minor)) + 1) << 8;
383 DPRINTK("ATEN2011_get_Uart_Reg application number is %x\n",
386 if (((__u16) port->number - (__u16) (minor)) == 0) {
389 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
391 ("ATEN2011_get_Uart_Reg application number is %x\n",
396 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
398 ("ATEN2011_get_Uart_Reg application number is %x\n",
402 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
403 ATEN_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
405 *val = (*val) & 0x00ff;
409 static void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port)
412 DPRINTK("***************************************\n");
413 DPRINTK("Application number is %4x\n", ATEN2011_port->AppNum);
414 DPRINTK("SpRegOffset is %2x\n", ATEN2011_port->SpRegOffset);
415 DPRINTK("ControlRegOffset is %2x \n", ATEN2011_port->ControlRegOffset);
416 DPRINTK("DCRRegOffset is %2x \n", ATEN2011_port->DcrRegOffset);
417 DPRINTK("***************************************\n");
421 static int handle_newMsr(struct ATENINTL_port *port, __u8 newMsr)
423 struct ATENINTL_port *ATEN2011_port;
424 struct async_icount *icount;
425 ATEN2011_port = port;
426 icount = &ATEN2011_port->icount;
428 (ATEN_MSR_DELTA_CTS | ATEN_MSR_DELTA_DSR | ATEN_MSR_DELTA_RI |
429 ATEN_MSR_DELTA_CD)) {
430 icount = &ATEN2011_port->icount;
432 /* update input line counters */
433 if (newMsr & ATEN_MSR_DELTA_CTS) {
436 if (newMsr & ATEN_MSR_DELTA_DSR) {
439 if (newMsr & ATEN_MSR_DELTA_CD) {
442 if (newMsr & ATEN_MSR_DELTA_RI) {
450 static int handle_newLsr(struct ATENINTL_port *port, __u8 newLsr)
452 struct async_icount *icount;
454 dbg("%s - %02x", __FUNCTION__, newLsr);
456 if (newLsr & SERIAL_LSR_BI) {
458 // Parity and Framing errors only count if they
459 // occur exclusive of a break being
462 newLsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
465 /* update input line counters */
466 icount = &port->icount;
467 if (newLsr & SERIAL_LSR_BI) {
470 if (newLsr & SERIAL_LSR_OE) {
473 if (newLsr & SERIAL_LSR_PE) {
476 if (newLsr & SERIAL_LSR_FE) {
483 static void ATEN2011_control_callback(struct urb *urb)
486 struct ATENINTL_port *ATEN2011_port;
490 DPRINTK("%s", "Invalid Pointer !!!!:\n");
494 switch (urb->status) {
501 /* this urb is terminated, clean up */
502 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
506 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
511 ATEN2011_port = (struct ATENINTL_port *)urb->context;
513 DPRINTK("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
514 DPRINTK("%s ATEN2011_port->MsrLsr is %d port %d\n", __FUNCTION__,
515 ATEN2011_port->MsrLsr, ATEN2011_port->port_num);
516 data = urb->transfer_buffer;
517 regval = (__u8) data[0];
518 DPRINTK("%s data is %x\n", __FUNCTION__, regval);
519 if (ATEN2011_port->MsrLsr == 0)
520 handle_newMsr(ATEN2011_port, regval);
521 else if (ATEN2011_port->MsrLsr == 1)
522 handle_newLsr(ATEN2011_port, regval);
528 static int ATEN2011_get_reg(struct ATENINTL_port *ATEN, __u16 Wval, __u16 reg,
531 struct usb_device *dev = ATEN->port->serial->dev;
532 struct usb_ctrlrequest *dr = NULL;
533 unsigned char *buffer = NULL;
535 buffer = (__u8 *) ATEN->ctrl_buf;
537 // dr=(struct usb_ctrlrequest *)(buffer);
538 dr = (void *)(buffer + 2);
539 dr->bRequestType = ATEN_RD_RTYPE;
540 dr->bRequest = ATEN_RDREQ;
541 dr->wValue = cpu_to_le16(Wval); //0;
542 dr->wIndex = cpu_to_le16(reg);
543 dr->wLength = cpu_to_le16(2);
545 usb_fill_control_urb(ATEN->control_urb, dev, usb_rcvctrlpipe(dev, 0),
546 (unsigned char *)dr, buffer, 2,
547 ATEN2011_control_callback, ATEN);
548 ATEN->control_urb->transfer_buffer_length = 2;
549 ret = usb_submit_urb(ATEN->control_urb, GFP_ATOMIC);
553 static void ATEN2011_interrupt_callback(struct urb *urb)
557 struct ATENINTL_port *ATEN2011_port;
558 struct ATENINTL_serial *ATEN2011_serial;
559 struct usb_serial *serial;
566 //printk("in the function ATEN2011_interrupt_callback Length %d, Data %x \n",urb->actual_length,(unsigned int)urb->transfer_buffer);
567 DPRINTK("%s", " : Entering\n");
569 ATEN2011_serial = (struct ATENINTL_serial *)urb->context;
570 if (!urb) // || ATEN2011_serial->status_polling_started == 0 )
572 DPRINTK("%s", "Invalid Pointer !!!!:\n");
576 switch (urb->status) {
583 /* this urb is terminated, clean up */
584 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
588 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
592 length = urb->actual_length;
593 data = urb->transfer_buffer;
595 //ATEN2011_serial= (struct ATENINTL_serial *)urb->context;
596 //serial = ATEN2011_get_usb_serial(port,__FUNCTION__);
597 serial = ATEN2011_serial->serial;
599 /* ATENINTL get 5 bytes
600 * Byte 1 IIR Port 1 (port.number is 0)
601 * Byte 2 IIR Port 2 (port.number is 1)
602 * Byte 3 IIR Port 3 (port.number is 2)
603 * Byte 4 IIR Port 4 (port.number is 3)
604 * Byte 5 FIFO status for both */
606 if (length && length > 5) {
607 DPRINTK("%s \n", "Wrong data !!!");
612 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
613 sp[0] = (__u8) data[0];
614 sp[1] = (__u8) data[1];
615 sp[2] = (__u8) data[2];
616 sp[3] = (__u8) data[3];
619 sp[0] = (__u8) data[0];
620 sp[1] = (__u8) data[2];
621 //sp[2]=(__u8)data[2];
622 //sp[3]=(__u8)data[3];
626 // printk("%s data is sp1:%x sp2:%x sp3:%x sp4:%x status:%x\n",__FUNCTION__,sp1,sp2,sp3,sp4,st);
627 for (i = 0; i < serial->num_ports; i++) {
628 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
629 minor = serial->minor;
630 if (minor == SERIAL_TTY_NO_MINOR)
632 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
635 (((__u16) serial->port[i]->number -
636 (__u16) (minor)) + 2) << 8;
639 (((__u16) serial->port[i]->number -
640 (__u16) (minor)) + 1) << 8;
641 if (ATEN2011_port->open != 0) {
642 //printk("%s wval is:(for 2011) %x\n",__FUNCTION__,wval);
645 DPRINTK("SP%d No Interrupt !!!\n", i);
647 switch (sp[i] & 0x0f) {
650 ("Serial Port %d: Receiver status error or ",
653 ("address bit detected in 9-bit mode\n");
654 ATEN2011_port->MsrLsr = 1;
655 ATEN2011_get_reg(ATEN2011_port, wval,
656 LINE_STATUS_REGISTER,
661 ("Serial Port %d: Modem status change\n",
663 ATEN2011_port->MsrLsr = 0;
664 ATEN2011_get_reg(ATEN2011_port, wval,
665 MODEM_STATUS_REGISTER,
674 if (ATEN2011_serial->status_polling_started == 0)
677 result = usb_submit_urb(urb, GFP_ATOMIC);
679 dev_err(&urb->dev->dev,
680 "%s - Error %d submitting interrupt urb\n",
681 __FUNCTION__, result);
688 static void ATEN2011_bulk_in_callback(struct urb *urb)
692 struct usb_serial *serial;
693 struct usb_serial_port *port;
694 struct ATENINTL_serial *ATEN2011_serial;
695 struct ATENINTL_port *ATEN2011_port;
696 struct tty_struct *tty;
698 DPRINTK("%s", "Invalid Pointer !!!!:\n");
703 DPRINTK("nonzero read bulk status received: %d", urb->status);
704 // if(urb->status==84)
709 ATEN2011_port = (struct ATENINTL_port *)urb->context;
710 if (!ATEN2011_port) {
711 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
715 port = (struct usb_serial_port *)ATEN2011_port->port;
716 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
717 DPRINTK("%s", "Port Paranoia failed \n");
721 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
723 DPRINTK("%s\n", "Bad serial pointer ");
727 DPRINTK("%s\n", "Entering... \n");
729 data = urb->transfer_buffer;
730 ATEN2011_serial = ATEN2011_get_serial_private(serial);
732 DPRINTK("%s", "Entering ........... \n");
734 if (urb->actual_length) {
735 tty = tty_port_tty_get(&ATEN2011_port->port->port);
737 tty_buffer_request_room(tty, urb->actual_length);
738 tty_insert_flip_string(tty, data, urb->actual_length);
739 DPRINTK(" %s \n", data);
740 tty_flip_buffer_push(tty);
744 ATEN2011_port->icount.rx += urb->actual_length;
745 DPRINTK("ATEN2011_port->icount.rx is %d:\n",
746 ATEN2011_port->icount.rx);
750 if (!ATEN2011_port->read_urb) {
751 DPRINTK("%s", "URB KILLED !!!\n");
755 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
756 ATEN2011_port->read_urb->dev = serial->dev;
758 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
762 (" usb_submit_urb(read bulk) failed, status = %d",
768 static void ATEN2011_bulk_out_data_callback(struct urb *urb)
770 struct ATENINTL_port *ATEN2011_port;
771 struct tty_struct *tty;
773 DPRINTK("%s", "Invalid Pointer !!!!:\n");
778 DPRINTK("nonzero write bulk status received:%d\n", urb->status);
782 ATEN2011_port = (struct ATENINTL_port *)urb->context;
783 if (!ATEN2011_port) {
784 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
788 if (ATEN2011_port_paranoia_check(ATEN2011_port->port, __FUNCTION__)) {
789 DPRINTK("%s", "Port Paranoia failed \n");
793 DPRINTK("%s \n", "Entering .........");
795 tty = tty_port_tty_get(&ATEN2011_port->port->port);
797 if (tty && ATEN2011_port->open) {
798 /* tell the tty driver that something has changed */
799 wake_up_interruptible(&tty->write_wait);
802 /* Release the Write URB */
803 ATEN2011_port->write_in_progress = 0;
805 //schedule_work(&ATEN2011_port->port->work);
810 #ifdef ATENSerialProbe
811 static int ATEN2011_serial_probe(struct usb_serial *serial,
812 const struct usb_device_id *id)
815 /*need to implement the mode_reg reading and updating\
816 structures usb_serial_ device_type\
817 (i.e num_ports, num_bulkin,bulkout etc) */
818 /* Also we can update the changes attach */
823 static int ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port,
828 struct usb_serial *serial;
829 // struct usb_serial_port *port0;
833 struct ATENINTL_serial *ATEN2011_serial;
834 struct ATENINTL_port *ATEN2011_port;
835 struct ktermios tmp_termios;
838 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
839 DPRINTK("%s", "Port Paranoia failed \n");
842 //ATEN2011_serial->NoOfOpenPorts++;
843 serial = port->serial;
845 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
846 DPRINTK("%s", "Serial Paranoia failed \n");
850 ATEN2011_port = ATEN2011_get_port_private(port);
852 if (ATEN2011_port == NULL)
855 if (ATEN2011_port->ctrl_buf==NULL)
857 ATEN2011_port->ctrl_buf = kmalloc(16,GFP_KERNEL);
858 if (ATEN2011_port->ctrl_buf == NULL) {
859 printk(", Can't allocate ctrl buff\n");
865 if(!ATEN2011_port->control_urb)
867 ATEN2011_port->control_urb=kmalloc(sizeof(struct urb),GFP_KERNEL);
870 // port0 = serial->port[0];
872 ATEN2011_serial = ATEN2011_get_serial_private(serial);
874 if (ATEN2011_serial == NULL) //|| port0 == NULL)
878 // increment the number of opened ports counter here
879 ATEN2011_serial->NoOfOpenPorts++;
880 //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
882 usb_clear_halt(serial->dev, port->write_urb->pipe);
883 usb_clear_halt(serial->dev, port->read_urb->pipe);
885 /* Initialising the write urb pool */
886 for (j = 0; j < NUM_URBS; ++j) {
887 urb = usb_alloc_urb(0, GFP_ATOMIC);
888 ATEN2011_port->write_urb_pool[j] = urb;
891 err("No more urbs???");
895 urb->transfer_buffer = NULL;
896 urb->transfer_buffer =
897 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
898 if (!urb->transfer_buffer) {
899 err("%s-out of memory for urb buffers.", __FUNCTION__);
904 /*****************************************************************************
905 * Initialize ATEN2011 -- Write Init values to corresponding Registers
913 * 0x08 : SP1/2 Control Reg
914 *****************************************************************************/
916 //NEED to check the fallowing Block
920 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
922 DPRINTK("Reading Spreg failed\n");
926 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
928 DPRINTK("writing Spreg failed\n");
933 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
935 DPRINTK("writing Spreg failed\n");
939 //End of block to be checked
940 //**************************CHECK***************************//
947 status = ATEN2011_set_Uart_Reg(port, SCRATCH_PAD_REGISTER, Data);
949 DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
953 DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",
956 //**************************CHECK***************************//
961 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
963 DPRINTK("Reading Controlreg failed\n");
966 Data |= 0x08; //Driver done bit
968 status = ATEN2011_set_reg_sync(port,ATEN2011_port->ControlRegOffset,Data);
970 DPRINTK("writing Controlreg failed\n");
974 Data |= 0x20; //rx_disable
977 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
979 DPRINTK("writing Controlreg failed\n");
982 //do register settings here
983 // Set all regs to the device default values.
984 ////////////////////////////////////
985 // First Disable all interrupts.
986 ////////////////////////////////////
990 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
992 DPRINTK("disableing interrupts failed\n");
995 // Set FIFO_CONTROL_REGISTER to the default value
998 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1000 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
1006 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
1008 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
1012 Data = 0x03; //LCR_BITS_8
1014 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1015 ATEN2011_port->shadowLCR = Data;
1017 Data = 0x0b; // MCR_DTR|MCR_RTS|MCR_MASTER_IE
1019 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1020 ATEN2011_port->shadowMCR = Data;
1025 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1026 ATEN2011_port->shadowLCR = Data;
1028 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1030 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1034 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
1038 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
1042 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1044 // Data = ATEN2011_port->shadowLCR; //data latch disable
1045 Data = Data & ~SERIAL_LCR_DLAB;
1047 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1048 ATEN2011_port->shadowLCR = Data;
1050 //clearing Bulkin and Bulkout Fifo
1053 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1057 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1059 Data = Data & ~0x0c;
1061 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1062 //Finally enable all interrupts
1066 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1068 //clearing rx_disable
1072 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1073 Data = Data & ~0x20;
1076 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1082 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1086 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1088 /* force low_latency on so that our tty_push actually forces *
1089 * the data through,otherwise it is scheduled, and with *
1090 * high data rates (like with OHCI) data can get lost. */
1093 tty->low_latency = 1;
1095 printk("port number is %d \n",port->number);
1096 printk("serial number is %d \n",port->serial->minor);
1097 printk("Bulkin endpoint is %d \n",port->bulk_in_endpointAddress);
1098 printk("BulkOut endpoint is %d \n",port->bulk_out_endpointAddress);
1099 printk("Interrupt endpoint is %d \n",port->interrupt_in_endpointAddress);
1100 printk("port's number in the device is %d\n",ATEN2011_port->port_num);
1102 ////////////////////////
1103 //#ifdef CheckStatusPipe
1104 /* Check to see if we've set up our endpoint info yet *
1105 * (can't set it up in ATEN2011_startup as the structures *
1106 * were not set up at that time.) */
1107 if (ATEN2011_serial->NoOfOpenPorts == 1) {
1108 // start the status polling here
1109 ATEN2011_serial->status_polling_started = 1;
1110 //if (ATEN2011_serial->interrupt_in_buffer == NULL)
1112 /* If not yet set, Set here */
1113 ATEN2011_serial->interrupt_in_buffer =
1114 serial->port[0]->interrupt_in_buffer;
1115 ATEN2011_serial->interrupt_in_endpoint =
1116 serial->port[0]->interrupt_in_endpointAddress;
1117 //printk(" interrupt endpoint:%d \n",ATEN2011_serial->interrupt_in_endpoint);
1118 ATEN2011_serial->interrupt_read_urb =
1119 serial->port[0]->interrupt_in_urb;
1121 /* set up interrupt urb */
1122 usb_fill_int_urb(ATEN2011_serial->interrupt_read_urb,
1124 usb_rcvintpipe(serial->dev,
1126 interrupt_in_endpoint),
1127 ATEN2011_serial->interrupt_in_buffer,
1128 ATEN2011_serial->interrupt_read_urb->
1129 transfer_buffer_length,
1130 ATEN2011_interrupt_callback, ATEN2011_serial,
1131 ATEN2011_serial->interrupt_read_urb->interval);
1133 /* start interrupt read for ATEN2011 *
1134 * will continue as long as ATEN2011 is connected */
1137 usb_submit_urb(ATEN2011_serial->interrupt_read_urb,
1140 DPRINTK("%s - Error %d submitting interrupt urb",
1141 __FUNCTION__, response);
1144 // printk(" interrupt URB submitted\n");
1151 ///////////////////////
1152 /* see if we've set up our endpoint info yet *
1153 * (can't set it up in ATEN2011_startup as the *
1154 * structures were not set up at that time.) */
1156 DPRINTK("port number is %d \n", port->number);
1157 DPRINTK("serial number is %d \n", port->serial->minor);
1158 DPRINTK("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1159 DPRINTK("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1160 DPRINTK("Interrupt endpoint is %d \n",
1161 port->interrupt_in_endpointAddress);
1162 DPRINTK("port's number in the device is %d\n", ATEN2011_port->port_num);
1163 ATEN2011_port->bulk_in_buffer = port->bulk_in_buffer;
1164 ATEN2011_port->bulk_in_endpoint = port->bulk_in_endpointAddress;
1165 ATEN2011_port->read_urb = port->read_urb;
1166 ATEN2011_port->bulk_out_endpoint = port->bulk_out_endpointAddress;
1168 minor = port->serial->minor;
1169 if (minor == SERIAL_TTY_NO_MINOR)
1172 /* set up our bulk in urb */
1173 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1174 && (((__u16) port->number - (__u16) (minor)) != 0)) {
1175 usb_fill_bulk_urb(ATEN2011_port->read_urb, serial->dev,
1176 usb_rcvbulkpipe(serial->dev,
1178 bulk_in_endpointAddress +
1179 2)), port->bulk_in_buffer,
1180 ATEN2011_port->read_urb->
1181 transfer_buffer_length,
1182 ATEN2011_bulk_in_callback, ATEN2011_port);
1184 usb_fill_bulk_urb(ATEN2011_port->read_urb,
1186 usb_rcvbulkpipe(serial->dev,
1188 bulk_in_endpointAddress),
1189 port->bulk_in_buffer,
1190 ATEN2011_port->read_urb->
1191 transfer_buffer_length,
1192 ATEN2011_bulk_in_callback, ATEN2011_port);
1194 DPRINTK("ATEN2011_open: bulkin endpoint is %d\n",
1195 port->bulk_in_endpointAddress);
1196 response = usb_submit_urb(ATEN2011_port->read_urb, GFP_KERNEL);
1198 err("%s - Error %d submitting control urb", __FUNCTION__,
1202 /* initialize our wait queues */
1203 init_waitqueue_head(&ATEN2011_port->wait_open);
1204 init_waitqueue_head(&ATEN2011_port->wait_chase);
1205 init_waitqueue_head(&ATEN2011_port->delta_msr_wait);
1206 init_waitqueue_head(&ATEN2011_port->wait_command);
1208 /* initialize our icount structure */
1209 memset(&(ATEN2011_port->icount), 0x00, sizeof(ATEN2011_port->icount));
1211 /* initialize our port settings */
1212 ATEN2011_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1213 ATEN2011_port->chaseResponsePending = 0;
1214 /* send a open port command */
1215 ATEN2011_port->openPending = 0;
1216 ATEN2011_port->open = 1;
1217 //ATEN2011_change_port_settings(ATEN2011_port,old_termios);
1219 ATEN2011_set_termios(tty, port, &tmp_termios);
1220 ATEN2011_port->rxBytesAvail = 0x0;
1221 ATEN2011_port->icount.tx = 0;
1222 ATEN2011_port->icount.rx = 0;
1225 ("\n\nusb_serial serial:%x ATEN2011_port:%x\nATEN2011_serial:%x usb_serial_port port:%x\n\n",
1226 (unsigned int)serial, (unsigned int)ATEN2011_port,
1227 (unsigned int)ATEN2011_serial, (unsigned int)port);
1233 static int ATEN2011_chars_in_buffer(struct tty_struct *tty)
1235 struct usb_serial_port *port = tty->driver_data;
1238 struct ATENINTL_port *ATEN2011_port;
1240 //DPRINTK("%s \n"," ATEN2011_chars_in_buffer:entering ...........");
1242 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1243 DPRINTK("%s", "Invalid port \n");
1247 ATEN2011_port = ATEN2011_get_port_private(port);
1248 if (ATEN2011_port == NULL) {
1249 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1253 for (i = 0; i < NUM_URBS; ++i) {
1254 if (ATEN2011_port->write_urb_pool[i]->status == -EINPROGRESS) {
1255 chars += URB_TRANSFER_BUFFER_SIZE;
1258 dbg("%s - returns %d", __FUNCTION__, chars);
1263 static void ATEN2011_block_until_tx_empty(struct tty_struct *tty,
1264 struct ATENINTL_port *ATEN2011_port)
1266 int timeout = HZ / 10;
1272 count = ATEN2011_chars_in_buffer(tty);
1274 /* Check for Buffer status */
1279 /* Block the thread for a while */
1280 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1283 /* No activity.. count down section */
1286 dbg("%s - TIMEOUT", __FUNCTION__);
1289 /* Reset timout value back to seconds */
1295 static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port,
1298 struct usb_serial *serial;
1299 struct ATENINTL_serial *ATEN2011_serial;
1300 struct ATENINTL_port *ATEN2011_port;
1305 DPRINTK("%s\n", "ATEN2011_close:entering...");
1309 //printk("Entering... :ATEN2011_close\n");
1310 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1311 DPRINTK("%s", "Port Paranoia failed \n");
1314 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1316 DPRINTK("%s", "Serial Paranoia failed \n");
1319 // take the Adpater and port's private data
1320 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1321 ATEN2011_port = ATEN2011_get_port_private(port);
1322 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1326 /* flush and block(wait) until tx is empty */
1327 ATEN2011_block_until_tx_empty(tty, ATEN2011_port);
1329 // kill the ports URB's
1330 for (no_urbs = 0; no_urbs < NUM_URBS; no_urbs++)
1331 usb_kill_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1332 /* Freeing Write URBs */
1333 for (no_urbs = 0; no_urbs < NUM_URBS; ++no_urbs) {
1334 if (ATEN2011_port->write_urb_pool[no_urbs]) {
1335 if (ATEN2011_port->write_urb_pool[no_urbs]->
1337 kfree(ATEN2011_port->write_urb_pool[no_urbs]->
1339 usb_free_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1342 /* While closing port, shutdown all bulk read, write *
1343 * and interrupt read if they exists */
1345 if (ATEN2011_port->write_urb) {
1346 DPRINTK("%s", "Shutdown bulk write\n");
1347 usb_kill_urb(ATEN2011_port->write_urb);
1349 if (ATEN2011_port->read_urb) {
1350 DPRINTK("%s", "Shutdown bulk read\n");
1351 usb_kill_urb(ATEN2011_port->read_urb);
1353 if ((&ATEN2011_port->control_urb)) {
1354 DPRINTK("%s", "Shutdown control read\n");
1355 // usb_kill_urb (ATEN2011_port->control_urb);
1359 //if(ATEN2011_port->ctrl_buf != NULL)
1360 //kfree(ATEN2011_port->ctrl_buf);
1361 // decrement the no.of open ports counter of an individual USB-serial adapter.
1362 ATEN2011_serial->NoOfOpenPorts--;
1363 DPRINTK("NoOfOpenPorts in close%d:in port%d\n",
1364 ATEN2011_serial->NoOfOpenPorts, port->number);
1365 //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
1366 if (ATEN2011_serial->NoOfOpenPorts == 0) {
1367 //stop the stus polling here
1368 //printk("disabling the status polling flag to 0 :\n");
1369 ATEN2011_serial->status_polling_started = 0;
1370 if (ATEN2011_serial->interrupt_read_urb) {
1371 DPRINTK("%s", "Shutdown interrupt_read_urb\n");
1372 //ATEN2011_serial->interrupt_in_buffer=NULL;
1373 //usb_kill_urb (ATEN2011_serial->interrupt_read_urb);
1376 if (ATEN2011_port->write_urb) {
1377 /* if this urb had a transfer buffer already (old tx) free it */
1378 if (ATEN2011_port->write_urb->transfer_buffer != NULL) {
1379 kfree(ATEN2011_port->write_urb->transfer_buffer);
1381 usb_free_urb(ATEN2011_port->write_urb);
1383 // clear the MCR & IER
1385 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1387 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1389 //ATEN2011_get_Uart_Reg(port,MODEM_CONTROL_REGISTER,&Data1);
1390 //printk("value of MCR after closing the port is : 0x%x\n",Data1);
1392 ATEN2011_port->open = 0;
1393 ATEN2011_port->closePending = 0;
1394 ATEN2011_port->openPending = 0;
1395 DPRINTK("%s \n", "Leaving ............");
1399 static void ATEN2011_block_until_chase_response(struct tty_struct *tty,
1400 struct ATENINTL_port
1403 int timeout = 1 * HZ;
1408 count = ATEN2011_chars_in_buffer(tty);
1410 /* Check for Buffer status */
1412 ATEN2011_port->chaseResponsePending = 0;
1416 /* Block the thread for a while */
1417 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1419 /* No activity.. count down section */
1422 dbg("%s - TIMEOUT", __FUNCTION__);
1425 /* Reset timout value back to seconds */
1432 static void ATEN2011_break(struct tty_struct *tty, int break_state)
1434 struct usb_serial_port *port = tty->driver_data;
1436 struct usb_serial *serial;
1437 struct ATENINTL_serial *ATEN2011_serial;
1438 struct ATENINTL_port *ATEN2011_port;
1440 DPRINTK("%s \n", "Entering ...........");
1441 DPRINTK("ATEN2011_break: Start\n");
1443 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1444 DPRINTK("%s", "Port Paranoia failed \n");
1448 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1450 DPRINTK("%s", "Serial Paranoia failed \n");
1454 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1455 ATEN2011_port = ATEN2011_get_port_private(port);
1457 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1461 /* flush and chase */
1462 ATEN2011_port->chaseResponsePending = 1;
1466 /* flush and block until tx is empty */
1467 ATEN2011_block_until_chase_response(tty, ATEN2011_port);
1470 if (break_state == -1) {
1471 data = ATEN2011_port->shadowLCR | LCR_SET_BREAK;
1473 data = ATEN2011_port->shadowLCR & ~LCR_SET_BREAK;
1476 ATEN2011_port->shadowLCR = data;
1477 DPRINTK("ATEN2011_break ATEN2011_port->shadowLCR is %x\n",
1478 ATEN2011_port->shadowLCR);
1479 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER,
1480 ATEN2011_port->shadowLCR);
1485 static int ATEN2011_write_room(struct tty_struct *tty)
1487 struct usb_serial_port *port = tty->driver_data;
1490 struct ATENINTL_port *ATEN2011_port;
1492 // DPRINTK("%s \n"," ATEN2011_write_room:entering ...........");
1494 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1495 DPRINTK("%s", "Invalid port \n");
1496 DPRINTK("%s \n", " ATEN2011_write_room:leaving ...........");
1500 ATEN2011_port = ATEN2011_get_port_private(port);
1501 if (ATEN2011_port == NULL) {
1502 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1506 for (i = 0; i < NUM_URBS; ++i) {
1507 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1508 room += URB_TRANSFER_BUFFER_SIZE;
1512 dbg("%s - returns %d", __FUNCTION__, room);
1517 static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port,
1518 const unsigned char *data, int count)
1526 struct ATENINTL_port *ATEN2011_port;
1527 struct usb_serial *serial;
1528 struct ATENINTL_serial *ATEN2011_serial;
1531 const unsigned char *current_position = data;
1532 unsigned char *data1;
1533 DPRINTK("%s \n", "entering ...........");
1535 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1536 DPRINTK("%s", "Port Paranoia failed \n");
1540 serial = port->serial;
1541 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1542 DPRINTK("%s", "Serial Paranoia failed \n");
1546 ATEN2011_port = ATEN2011_get_port_private(port);
1547 if (ATEN2011_port == NULL) {
1548 DPRINTK("%s", "ATEN2011_port is NULL\n");
1552 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1553 if (ATEN2011_serial == NULL) {
1554 DPRINTK("%s", "ATEN2011_serial is NULL \n");
1558 /* try to find a free urb in the list */
1561 for (i = 0; i < NUM_URBS; ++i) {
1562 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1563 urb = ATEN2011_port->write_urb_pool[i];
1564 DPRINTK("\nURB:%d", i);
1570 dbg("%s - no more free urbs", __FUNCTION__);
1574 if (urb->transfer_buffer == NULL) {
1575 urb->transfer_buffer =
1576 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1578 if (urb->transfer_buffer == NULL) {
1579 err("%s no more kernel memory...", __FUNCTION__);
1583 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1585 memcpy(urb->transfer_buffer, current_position, transfer_size);
1586 //usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
1588 /* fill urb with data and submit */
1589 minor = port->serial->minor;
1590 if (minor == SERIAL_TTY_NO_MINOR) ;
1592 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1593 && (((__u16) port->number - (__u16) (minor)) != 0)) {
1594 usb_fill_bulk_urb(urb, ATEN2011_serial->serial->dev,
1595 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1597 bulk_out_endpointAddress) +
1598 2), urb->transfer_buffer,
1600 ATEN2011_bulk_out_data_callback,
1604 usb_fill_bulk_urb(urb,
1605 ATEN2011_serial->serial->dev,
1606 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1608 bulk_out_endpointAddress),
1609 urb->transfer_buffer, transfer_size,
1610 ATEN2011_bulk_out_data_callback,
1613 data1 = urb->transfer_buffer;
1614 DPRINTK("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1615 //for(i=0;i < urb->actual_length;i++)
1616 // DPRINTK("Data is %c\n ",data1[i]);
1618 /* send it down the pipe */
1619 status = usb_submit_urb(urb, GFP_ATOMIC);
1622 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1623 __FUNCTION__, status);
1624 bytes_sent = status;
1627 bytes_sent = transfer_size;
1628 ATEN2011_port->icount.tx += transfer_size;
1629 DPRINTK("ATEN2011_port->icount.tx is %d:\n", ATEN2011_port->icount.tx);
1636 static void ATEN2011_throttle(struct tty_struct *tty)
1638 struct usb_serial_port *port = tty->driver_data;
1639 struct ATENINTL_port *ATEN2011_port;
1642 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1643 DPRINTK("%s", "Invalid port \n");
1647 DPRINTK("- port %d\n", port->number);
1649 ATEN2011_port = ATEN2011_get_port_private(port);
1651 if (ATEN2011_port == NULL)
1654 if (!ATEN2011_port->open) {
1655 DPRINTK("%s\n", "port not opened");
1659 DPRINTK("%s", "Entering .......... \n");
1662 dbg("%s - no tty available", __FUNCTION__);
1666 /* if we are implementing XON/XOFF, send the stop character */
1668 unsigned char stop_char = STOP_CHAR(tty);
1669 status = ATEN2011_write(tty, port, &stop_char, 1); //FC4
1675 /* if we are implementing RTS/CTS, toggle that line */
1676 if (tty->termios->c_cflag & CRTSCTS) {
1677 ATEN2011_port->shadowMCR &= ~MCR_RTS;
1680 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
1681 ATEN2011_port->shadowMCR);
1691 static void ATEN2011_unthrottle(struct tty_struct *tty)
1693 struct usb_serial_port *port = tty->driver_data;
1695 struct ATENINTL_port *ATEN2011_port = ATEN2011_get_port_private(port);
1697 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1698 DPRINTK("%s", "Invalid port \n");
1702 if (ATEN2011_port == NULL)
1705 if (!ATEN2011_port->open) {
1706 dbg("%s - port not opened", __FUNCTION__);
1710 DPRINTK("%s", "Entering .......... \n");
1713 dbg("%s - no tty available", __FUNCTION__);
1717 /* if we are implementing XON/XOFF, send the start character */
1719 unsigned char start_char = START_CHAR(tty);
1720 status = ATEN2011_write(tty, port, &start_char, 1); //FC4
1726 /* if we are implementing RTS/CTS, toggle that line */
1727 if (tty->termios->c_cflag & CRTSCTS) {
1728 ATEN2011_port->shadowMCR |= MCR_RTS;
1731 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
1732 ATEN2011_port->shadowMCR);
1741 static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file)
1743 struct usb_serial_port *port = tty->driver_data;
1744 //struct ti_port *tport = usb_get_serial_port_data(port);
1745 struct ATENINTL_port *ATEN2011_port;
1746 unsigned int result;
1751 ATEN2011_port = ATEN2011_get_port_private(port);
1753 DPRINTK("%s - port %d", __FUNCTION__, port->number);
1755 if (ATEN2011_port == NULL)
1758 status = ATEN2011_get_Uart_Reg(port, MODEM_STATUS_REGISTER, &msr);
1759 status = ATEN2011_get_Uart_Reg(port, MODEM_CONTROL_REGISTER, &mcr);
1760 // mcr = ATEN2011_port->shadowMCR;
1761 // COMMENT2: the Fallowing three line are commented for updating only MSR values
1762 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1763 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1764 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1765 | ((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0)
1766 | ((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0)
1767 | ((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0)
1768 | ((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0);
1770 DPRINTK("%s - 0x%04X", __FUNCTION__, result);
1775 static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
1776 unsigned int set, unsigned int clear)
1778 struct usb_serial_port *port = tty->driver_data;
1779 struct ATENINTL_port *ATEN2011_port;
1780 //struct ti_port *tport = usb_get_serial_port_data(port);
1782 unsigned int status;
1784 DPRINTK("%s - port %d", __FUNCTION__, port->number);
1786 ATEN2011_port = ATEN2011_get_port_private(port);
1788 if (ATEN2011_port == NULL)
1791 mcr = ATEN2011_port->shadowMCR;
1792 if (clear & TIOCM_RTS)
1794 if (clear & TIOCM_DTR)
1796 if (clear & TIOCM_LOOP)
1797 mcr &= ~MCR_LOOPBACK;
1799 if (set & TIOCM_RTS)
1801 if (set & TIOCM_DTR)
1803 if (set & TIOCM_LOOP)
1804 mcr |= MCR_LOOPBACK;
1806 ATEN2011_port->shadowMCR = mcr;
1809 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, mcr);
1811 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
1818 static void ATEN2011_set_termios(struct tty_struct *tty,
1819 struct usb_serial_port *port,
1820 struct ktermios *old_termios)
1824 struct usb_serial *serial;
1825 struct ATENINTL_port *ATEN2011_port;
1827 DPRINTK("ATEN2011_set_termios: START\n");
1828 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1829 DPRINTK("%s", "Invalid port \n");
1833 serial = port->serial;
1835 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1836 DPRINTK("%s", "Invalid Serial \n");
1840 ATEN2011_port = ATEN2011_get_port_private(port);
1842 if (ATEN2011_port == NULL)
1845 if (!ATEN2011_port->open) {
1846 dbg("%s - port not opened", __FUNCTION__);
1850 DPRINTK("%s\n", "setting termios - ");
1852 cflag = tty->termios->c_cflag;
1855 DPRINTK("%s %s\n", __FUNCTION__, "cflag is NULL");
1859 /* check that they really want us to change something */
1861 if ((cflag == old_termios->c_cflag) &&
1862 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1863 RELEVANT_IFLAG(old_termios->c_iflag))) {
1864 DPRINTK("%s\n", "Nothing to change");
1869 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1870 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
1873 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1874 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1877 dbg("%s - port %d", __FUNCTION__, port->number);
1879 /* change the port settings to the new ones specified */
1881 ATEN2011_change_port_settings(tty, ATEN2011_port, old_termios);
1883 if (!ATEN2011_port->read_urb) {
1884 DPRINTK("%s", "URB KILLED !!!!!\n");
1888 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
1889 ATEN2011_port->read_urb->dev = serial->dev;
1890 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
1893 (" usb_submit_urb(read bulk) failed, status = %d",
1900 static int get_lsr_info(struct tty_struct *tty,
1901 struct ATENINTL_port *ATEN2011_port,
1902 unsigned int __user *value)
1905 unsigned int result = 0;
1907 count = ATEN2011_chars_in_buffer(tty);
1909 dbg("%s -- Empty", __FUNCTION__);
1910 result = TIOCSER_TEMT;
1913 if (copy_to_user(value, &result, sizeof(int)))
1918 static int get_number_bytes_avail(struct tty_struct *tty,
1919 struct ATENINTL_port *ATEN2011_port,
1920 unsigned int __user *value)
1922 unsigned int result = 0;
1925 return -ENOIOCTLCMD;
1927 result = tty->read_cnt;
1929 dbg("%s(%d) = %d", __FUNCTION__, ATEN2011_port->port->number, result);
1930 if (copy_to_user(value, &result, sizeof(int)))
1933 return -ENOIOCTLCMD;
1936 static int set_modem_info(struct ATENINTL_port *ATEN2011_port, unsigned int cmd,
1937 unsigned int __user *value)
1943 struct usb_serial_port *port;
1945 if (ATEN2011_port == NULL)
1948 port = (struct usb_serial_port *)ATEN2011_port->port;
1949 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1950 DPRINTK("%s", "Invalid port \n");
1954 mcr = ATEN2011_port->shadowMCR;
1956 if (copy_from_user(&arg, value, sizeof(int)))
1961 if (arg & TIOCM_RTS)
1963 if (arg & TIOCM_DTR)
1965 if (arg & TIOCM_LOOP)
1966 mcr |= MCR_LOOPBACK;
1970 if (arg & TIOCM_RTS)
1972 if (arg & TIOCM_DTR)
1974 if (arg & TIOCM_LOOP)
1975 mcr &= ~MCR_LOOPBACK;
1979 /* turn off the RTS and DTR and LOOPBACK
1980 * and then only turn on what was asked to */
1981 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
1982 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
1983 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
1984 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
1988 ATEN2011_port->shadowMCR = mcr;
1990 Data = ATEN2011_port->shadowMCR;
1992 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1994 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
2001 static int get_modem_info(struct ATENINTL_port *ATEN2011_port,
2002 unsigned int __user *value)
2004 unsigned int result = 0;
2006 unsigned int mcr = ATEN2011_port->shadowMCR;
2009 ATEN2011_get_Uart_Reg(ATEN2011_port->port, MODEM_STATUS_REGISTER,
2011 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2012 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2013 |((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2014 |((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2015 |((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2016 |((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2018 dbg("%s -- %x", __FUNCTION__, result);
2020 if (copy_to_user(value, &result, sizeof(int)))
2025 static int get_serial_info(struct ATENINTL_port *ATEN2011_port,
2026 struct serial_struct __user *retinfo)
2028 struct serial_struct tmp;
2030 if (ATEN2011_port == NULL)
2036 memset(&tmp, 0, sizeof(tmp));
2038 tmp.type = PORT_16550A;
2039 tmp.line = ATEN2011_port->port->serial->minor;
2040 if (tmp.line == SERIAL_TTY_NO_MINOR)
2042 tmp.port = ATEN2011_port->port->number;
2044 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2045 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2046 tmp.baud_base = 9600;
2047 tmp.close_delay = 5 * HZ;
2048 tmp.closing_wait = 30 * HZ;
2050 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2055 static int ATEN2011_ioctl(struct tty_struct *tty, struct file *file,
2056 unsigned int cmd, unsigned long arg)
2058 struct usb_serial_port *port = tty->driver_data;
2059 struct ATENINTL_port *ATEN2011_port;
2060 struct async_icount cnow;
2061 struct async_icount cprev;
2062 struct serial_icounter_struct icount;
2064 unsigned int __user *user_arg = (unsigned int __user *)arg;
2066 //printk("%s - port %d, cmd = 0x%x\n", __FUNCTION__, port->number, cmd);
2067 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2068 DPRINTK("%s", "Invalid port \n");
2072 ATEN2011_port = ATEN2011_get_port_private(port);
2074 if (ATEN2011_port == NULL)
2077 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2080 /* return number of bytes available */
2083 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2084 return get_number_bytes_avail(tty, ATEN2011_port, user_arg);
2088 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2089 return put_user(ATEN2011_chars_in_buffer(tty), user_arg);
2093 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2094 return get_lsr_info(tty, ATEN2011_port, user_arg);
2100 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2102 // printk("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__, port->number);
2104 set_modem_info(ATEN2011_port, cmd, user_arg);
2105 // printk(" %s: ret:%d\n",__FUNCTION__,ATENret);
2109 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2110 return get_modem_info(ATEN2011_port, user_arg);
2113 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2114 return get_serial_info(ATEN2011_port,
2115 (struct serial_struct __user *)arg);
2118 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2122 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2123 cprev = ATEN2011_port->icount;
2125 //interruptible_sleep_on(&ATEN2011_port->delta_msr_wait);
2126 // ATEN2011_port->delta_msr_cond=0;
2127 //wait_event_interruptible(ATEN2011_port->delta_msr_wait,(ATEN2011_port->delta_msr_cond==1));
2129 /* see if a signal did it */
2130 if (signal_pending(current))
2131 return -ERESTARTSYS;
2132 cnow = ATEN2011_port->icount;
2133 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2134 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2135 return -EIO; /* no change => error */
2136 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2137 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2138 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2139 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2148 cnow = ATEN2011_port->icount;
2149 icount.cts = cnow.cts;
2150 icount.dsr = cnow.dsr;
2151 icount.rng = cnow.rng;
2152 icount.dcd = cnow.dcd;
2153 icount.rx = cnow.rx;
2154 icount.tx = cnow.tx;
2155 icount.frame = cnow.frame;
2156 icount.overrun = cnow.overrun;
2157 icount.parity = cnow.parity;
2158 icount.brk = cnow.brk;
2159 icount.buf_overrun = cnow.buf_overrun;
2161 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2162 port->number, icount.rx, icount.tx);
2163 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
2171 return -ENOIOCTLCMD;
2174 static int ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,
2175 __u16 * clk_sel_val)
2178 //__u16 custom,round1, round;
2180 dbg("%s - %d", __FUNCTION__, baudRate);
2182 if (baudRate <= 115200) {
2183 *divisor = 115200 / baudRate;
2186 if ((baudRate > 115200) && (baudRate <= 230400)) {
2187 *divisor = 230400 / baudRate;
2188 *clk_sel_val = 0x10;
2189 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
2190 *divisor = 403200 / baudRate;
2191 *clk_sel_val = 0x20;
2192 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
2193 *divisor = 460800 / baudRate;
2194 *clk_sel_val = 0x30;
2195 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
2196 *divisor = 806400 / baudRate;
2197 *clk_sel_val = 0x40;
2198 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
2199 *divisor = 921600 / baudRate;
2200 *clk_sel_val = 0x50;
2201 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
2202 *divisor = 1572864 / baudRate;
2203 *clk_sel_val = 0x60;
2204 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
2205 *divisor = 3145728 / baudRate;
2206 *clk_sel_val = 0x70;
2211 static int ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port
2212 *ATEN2011_port, int baudRate)
2217 unsigned char number;
2219 struct usb_serial_port *port;
2222 if (ATEN2011_port == NULL)
2225 port = (struct usb_serial_port *)ATEN2011_port->port;
2226 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2227 DPRINTK("%s", "Invalid port \n");
2231 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2232 DPRINTK("%s", "Invalid Serial \n");
2236 DPRINTK("%s", "Entering .......... \n");
2238 minor = ATEN2011_port->port->serial->minor;
2239 if (minor == SERIAL_TTY_NO_MINOR)
2241 number = ATEN2011_port->port->number - minor;
2243 dbg("%s - port = %d, baud = %d", __FUNCTION__,
2244 ATEN2011_port->port->number, baudRate);
2245 //reset clk_uart_sel in spregOffset
2246 if (baudRate > 115200) {
2247 #ifdef HW_flow_control
2248 //NOTE: need to see the pther register to modify
2249 //setting h/w flow control bit to 1;
2251 //Data = ATEN2011_port->shadowMCR ;
2253 ATEN2011_port->shadowMCR = Data;
2255 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2257 DPRINTK("Writing spreg failed in set_serial_baud\n");
2263 #ifdef HW_flow_control
2264 //setting h/w flow control bit to 0;
2266 //Data = ATEN2011_port->shadowMCR ;
2268 ATEN2011_port->shadowMCR = Data;
2270 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2272 DPRINTK("Writing spreg failed in set_serial_baud\n");
2279 if (1) //baudRate <= 115200)
2285 ATEN2011_calc_baud_rate_divisor(baudRate, &divisor,
2288 ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset,
2291 DPRINTK("reading spreg failed in set_serial_baud\n");
2294 Data = (Data & 0x8f) | clk_sel_val;
2297 ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset,
2300 DPRINTK("Writing spreg failed in set_serial_baud\n");
2303 /* Calculate the Divisor */
2306 err("%s - bad baud rate", __FUNCTION__);
2307 DPRINTK("%s\n", "bad baud rate");
2310 /* Enable access to divisor latch */
2311 Data = ATEN2011_port->shadowLCR | SERIAL_LCR_DLAB;
2312 ATEN2011_port->shadowLCR = Data;
2313 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2315 /* Write the divisor */
2316 Data = (unsigned char)(divisor & 0xff);
2317 DPRINTK("set_serial_baud Value to write DLL is %x\n", Data);
2318 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
2320 Data = (unsigned char)((divisor & 0xff00) >> 8);
2321 DPRINTK("set_serial_baud Value to write DLM is %x\n", Data);
2322 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
2324 /* Disable access to divisor latch */
2325 Data = ATEN2011_port->shadowLCR & ~SERIAL_LCR_DLAB;
2326 ATEN2011_port->shadowLCR = Data;
2327 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2334 static void ATEN2011_change_port_settings(struct tty_struct *tty,
2335 struct ATENINTL_port *ATEN2011_port,
2336 struct ktermios *old_termios)
2347 struct usb_serial_port *port;
2348 struct usb_serial *serial;
2350 if (ATEN2011_port == NULL)
2353 port = (struct usb_serial_port *)ATEN2011_port->port;
2355 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2356 DPRINTK("%s", "Invalid port \n");
2360 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2361 DPRINTK("%s", "Invalid Serial \n");
2365 serial = port->serial;
2367 dbg("%s - port %d", __FUNCTION__, ATEN2011_port->port->number);
2369 if ((!ATEN2011_port->open) && (!ATEN2011_port->openPending)) {
2370 dbg("%s - port not opened", __FUNCTION__);
2374 if ((!tty) || (!tty->termios)) {
2375 dbg("%s - no tty structures", __FUNCTION__);
2379 DPRINTK("%s", "Entering .......... \n");
2383 lParity = LCR_PAR_NONE;
2385 cflag = tty->termios->c_cflag;
2386 iflag = tty->termios->c_iflag;
2388 /* Change the number of bits */
2390 //COMMENT1: the below Line"if(cflag & CSIZE)" is added for the errors we get for serial loop data test i.e serial_loopback.pl -v
2393 switch (cflag & CSIZE) {
2414 /* Change the Parity bit */
2415 if (cflag & PARENB) {
2416 if (cflag & PARODD) {
2417 lParity = LCR_PAR_ODD;
2418 dbg("%s - parity = odd", __FUNCTION__);
2420 lParity = LCR_PAR_EVEN;
2421 dbg("%s - parity = even", __FUNCTION__);
2425 dbg("%s - parity = none", __FUNCTION__);
2428 if (cflag & CMSPAR) {
2429 lParity = lParity | 0x20;
2432 /* Change the Stop bit */
2433 if (cflag & CSTOPB) {
2435 dbg("%s - stop bits = 2", __FUNCTION__);
2438 dbg("%s - stop bits = 1", __FUNCTION__);
2441 /* Update the LCR with the correct value */
2442 ATEN2011_port->shadowLCR &=
2443 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2444 ATEN2011_port->shadowLCR |= (lData | lParity | lStop);
2446 ATEN2011_port->validDataMask = mask;
2448 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is %x\n",
2449 ATEN2011_port->shadowLCR);
2450 /* Disable Interrupts */
2452 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2455 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
2458 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
2460 /* Send the updated LCR value to the ATEN2011 */
2461 Data = ATEN2011_port->shadowLCR;
2463 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2466 ATEN2011_port->shadowMCR = Data;
2467 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2469 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2471 /* set up the MCR register and send it to the ATEN2011 */
2473 ATEN2011_port->shadowMCR = MCR_MASTER_IE;
2474 if (cflag & CBAUD) {
2475 ATEN2011_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2478 if (cflag & CRTSCTS) {
2479 ATEN2011_port->shadowMCR |= (MCR_XON_ANY);
2482 ATEN2011_port->shadowMCR &= ~(MCR_XON_ANY);
2485 Data = ATEN2011_port->shadowMCR;
2486 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2488 /* Determine divisor based on baud rate */
2489 baud = tty_get_baud_rate(tty);
2492 /* pick a default, any default... */
2493 DPRINTK("%s\n", "Picked default baud...");
2497 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2498 status = ATEN2011_send_cmd_write_baud_rate(ATEN2011_port, baud);
2500 /* Enable Interrupts */
2502 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2504 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
2505 ATEN2011_port->read_urb->dev = serial->dev;
2507 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
2511 (" usb_submit_urb(read bulk) failed, status = %d",
2515 //wake_up(&ATEN2011_port->delta_msr_wait);
2516 //ATEN2011_port->delta_msr_cond=1;
2518 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is End %x\n",
2519 ATEN2011_port->shadowLCR);
2524 static int ATEN2011_calc_num_ports(struct usb_serial *serial)
2529 int ATEN2011_2or4ports;
2530 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2531 ATEN_RDREQ, ATEN_RD_RTYPE, 0, GPIO_REGISTER,
2532 &Data, VENDOR_READ_LENGTH, ATEN_WDR_TIMEOUT);
2534 //printk("ATEN2011_calc_num_ports GPIO is %x\n",Data);
2536 /* ghostgum: here is where the problem appears to bet */
2537 /* Which of the following are needed? */
2538 /* Greg used the serial->type->num_ports=2 */
2539 /* But the code in the ATEN2011_open relies on serial->num_ports=2 */
2540 if ((Data & 0x01) == 0) {
2541 ATEN2011_2or4ports = 2;
2542 serial->type->num_ports = 2;
2543 serial->num_ports = 2;
2545 //else if(serial->interface->cur_altsetting->desc.bNumEndpoints == 9)
2547 ATEN2011_2or4ports = 4;
2548 serial->type->num_ports = 4;
2549 serial->num_ports = 4;
2553 return ATEN2011_2or4ports;
2556 static int ATEN2011_startup(struct usb_serial *serial)
2558 struct ATENINTL_serial *ATEN2011_serial;
2559 struct ATENINTL_port *ATEN2011_port;
2560 struct usb_device *dev;
2565 DPRINTK("%s \n", " ATEN2011_startup :entering..........");
2568 DPRINTK("%s\n", "Invalid Handler");
2574 DPRINTK("%s\n", "Entering...");
2576 /* create our private serial structure */
2577 ATEN2011_serial = kzalloc(sizeof(struct ATENINTL_serial), GFP_KERNEL);
2578 if (ATEN2011_serial == NULL) {
2579 err("%s - Out of memory", __FUNCTION__);
2583 /* resetting the private structure field values to zero */
2584 memset(ATEN2011_serial, 0, sizeof(struct ATENINTL_serial));
2586 ATEN2011_serial->serial = serial;
2587 //initilize status polling flag to 0
2588 ATEN2011_serial->status_polling_started = 0;
2590 ATEN2011_set_serial_private(serial, ATEN2011_serial);
2591 ATEN2011_serial->ATEN2011_spectrum_2or4ports =
2592 ATEN2011_calc_num_ports(serial);
2593 /* we set up the pointers to the endpoints in the ATEN2011_open *
2594 * function, as the structures aren't created yet. */
2596 /* set up port private structures */
2597 for (i = 0; i < serial->num_ports; ++i) {
2599 kmalloc(sizeof(struct ATENINTL_port), GFP_KERNEL);
2600 if (ATEN2011_port == NULL) {
2601 err("%s - Out of memory", __FUNCTION__);
2602 ATEN2011_set_serial_private(serial, NULL);
2603 kfree(ATEN2011_serial);
2606 memset(ATEN2011_port, 0, sizeof(struct ATENINTL_port));
2608 /* Initialize all port interrupt end point to port 0 int endpoint *
2609 * Our device has only one interrupt end point comman to all port */
2611 // serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
2613 ATEN2011_port->port = serial->port[i];
2615 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
2617 minor = serial->port[i]->serial->minor;
2618 if (minor == SERIAL_TTY_NO_MINOR)
2620 ATEN2011_port->port_num =
2621 ((serial->port[i]->number - minor) + 1);
2623 ATEN2011_port->AppNum = (((__u16) serial->port[i]->number -
2624 (__u16) (minor)) + 1) << 8;
2626 if (ATEN2011_port->port_num == 1) {
2627 ATEN2011_port->SpRegOffset = 0x0;
2628 ATEN2011_port->ControlRegOffset = 0x1;
2629 ATEN2011_port->DcrRegOffset = 0x4;
2630 } else if ((ATEN2011_port->port_num == 2)
2631 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2633 ATEN2011_port->SpRegOffset = 0x8;
2634 ATEN2011_port->ControlRegOffset = 0x9;
2635 ATEN2011_port->DcrRegOffset = 0x16;
2636 } else if ((ATEN2011_port->port_num == 2)
2637 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2639 ATEN2011_port->SpRegOffset = 0xa;
2640 ATEN2011_port->ControlRegOffset = 0xb;
2641 ATEN2011_port->DcrRegOffset = 0x19;
2642 } else if ((ATEN2011_port->port_num == 3)
2643 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2645 ATEN2011_port->SpRegOffset = 0xa;
2646 ATEN2011_port->ControlRegOffset = 0xb;
2647 ATEN2011_port->DcrRegOffset = 0x19;
2648 } else if ((ATEN2011_port->port_num == 4)
2649 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2651 ATEN2011_port->SpRegOffset = 0xc;
2652 ATEN2011_port->ControlRegOffset = 0xd;
2653 ATEN2011_port->DcrRegOffset = 0x1c;
2655 ATEN2011_Dump_serial_port(ATEN2011_port);
2657 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
2659 //enable rx_disable bit in control register
2662 ATEN2011_get_reg_sync(serial->port[i],
2663 ATEN2011_port->ControlRegOffset,
2666 DPRINTK("Reading ControlReg failed status-0x%x\n",
2671 ("ControlReg Reading success val is %x, status%d\n",
2673 Data |= 0x08; //setting driver done bit
2674 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2676 //Data |= 0x20; //rx_disable bit
2679 ATEN2011_set_reg_sync(serial->port[i],
2680 ATEN2011_port->ControlRegOffset,
2684 ("Writing ControlReg failed(rx_disable) status-0x%x\n",
2689 ("ControlReg Writing success(rx_disable) status%d\n",
2692 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2696 ATEN2011_set_reg_sync(serial->port[i],
2697 (__u16) (ATEN2011_port->DcrRegOffset +
2700 DPRINTK("Writing DCR0 failed status-0x%x\n", status);
2703 DPRINTK("DCR0 Writing success status%d\n", status);
2708 ATEN2011_set_reg_sync(serial->port[i],
2709 (__u16) (ATEN2011_port->DcrRegOffset +
2712 DPRINTK("Writing DCR1 failed status-0x%x\n", status);
2715 DPRINTK("DCR1 Writing success status%d\n", status);
2720 ATEN2011_set_reg_sync(serial->port[i],
2721 (__u16) (ATEN2011_port->DcrRegOffset +
2724 DPRINTK("Writing DCR2 failed status-0x%x\n", status);
2727 DPRINTK("DCR2 Writing success status%d\n", status);
2729 // write values in clkstart0x0 and clkmulti 0x20
2733 ATEN2011_set_reg_sync(serial->port[i],
2734 CLK_START_VALUE_REGISTER, Data);
2737 ("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n",
2742 ("CLK_START_VALUE_REGISTER Writing success status%d\n",
2748 ATEN2011_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2752 ("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2756 DPRINTK("CLK_MULTI_REGISTER Writing success status%d\n",
2759 //Zero Length flag register
2760 if ((ATEN2011_port->port_num != 1)
2761 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)) {
2765 status = ATEN2011_set_reg_sync(serial->port[i],
2771 DPRINTK("ZLIP offset%x\n",
2773 ((__u16) ATEN2011_port->port_num)));
2776 ("Writing ZLP_REG%d failed status-0x%x\n",
2780 DPRINTK("ZLP_REG%d Writing success status%d\n",
2785 status = ATEN2011_set_reg_sync(serial->port[i],
2791 DPRINTK("ZLIP offset%x\n",
2793 ((__u16) ATEN2011_port->port_num) -
2797 ("Writing ZLP_REG%d failed status-0x%x\n",
2801 DPRINTK("ZLP_REG%d Writing success status%d\n",
2805 ATEN2011_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC);
2806 ATEN2011_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2810 //Zero Length flag enable
2813 status = ATEN2011_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2815 DPRINTK("Writing ZLP_REG5 failed status-0x%x\n", status);
2818 DPRINTK("ZLP_REG5 Writing success status%d\n", status);
2820 /* setting configuration feature to one */
2821 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2822 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2826 static void ATEN2011_shutdown(struct usb_serial *serial)
2829 struct ATENINTL_port *ATEN2011_port;
2830 DPRINTK("%s \n", " shutdown :entering..........");
2837 DPRINTK("%s", "Invalid Handler \n");
2841 /* check for the ports to be closed,close the ports and disconnect */
2843 /* free private structure allocated for serial port *
2844 * stop reads and writes on all ports */
2846 for (i = 0; i < serial->num_ports; ++i) {
2847 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
2848 kfree(ATEN2011_port->ctrl_buf);
2849 usb_kill_urb(ATEN2011_port->control_urb);
2850 kfree(ATEN2011_port);
2851 ATEN2011_set_port_private(serial->port[i], NULL);
2854 /* free private structure allocated for serial device */
2856 kfree(ATEN2011_get_serial_private(serial));
2857 ATEN2011_set_serial_private(serial, NULL);
2859 DPRINTK("%s\n", "Thank u :: ");
2863 /* Inline functions to check the sanity of a pointer that is passed to us */
2864 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial,
2865 const char *function)
2868 dbg("%s - serial == NULL", function);
2871 if (!serial->type) {
2872 dbg("%s - serial->type == NULL!", function);
2878 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port,
2879 const char *function)
2882 dbg("%s - port == NULL", function);
2885 if (!port->serial) {
2886 dbg("%s - port->serial == NULL", function);
2892 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port,
2893 const char *function)
2895 /* if no port was specified, or it fails a paranoia check */
2897 ATEN2011_port_paranoia_check(port, function) ||
2898 ATEN2011_serial_paranoia_check(port->serial, function)) {
2899 /* then say that we don't have a valid usb_serial thing, which will * end up genrating -ENODEV return values */
2903 return port->serial;
2906 static struct usb_serial_driver ATENINTL2011_4port_device = {
2908 .owner = THIS_MODULE,
2911 .description = DRIVER_DESC,
2912 .id_table = id_table,
2913 .open = ATEN2011_open,
2914 .close = ATEN2011_close,
2915 .write = ATEN2011_write,
2916 .write_room = ATEN2011_write_room,
2917 .chars_in_buffer = ATEN2011_chars_in_buffer,
2918 .throttle = ATEN2011_throttle,
2919 .unthrottle = ATEN2011_unthrottle,
2920 .calc_num_ports = ATEN2011_calc_num_ports,
2922 .ioctl = ATEN2011_ioctl,
2923 .set_termios = ATEN2011_set_termios,
2924 .break_ctl = ATEN2011_break,
2925 .tiocmget = ATEN2011_tiocmget,
2926 .tiocmset = ATEN2011_tiocmset,
2927 .attach = ATEN2011_startup,
2928 .shutdown = ATEN2011_shutdown,
2929 .read_bulk_callback = ATEN2011_bulk_in_callback,
2930 .read_int_callback = ATEN2011_interrupt_callback,
2933 static struct usb_driver aten_driver = {
2935 .probe = usb_serial_probe,
2936 .disconnect = usb_serial_disconnect,
2937 .id_table = id_table,
2940 static int __init ATENINTL2011_init(void)
2944 DPRINTK("%s \n", " ATEN2011_init :entering..........");
2946 /* Register with the usb serial */
2947 retval = usb_serial_register(&ATENINTL2011_4port_device);
2950 goto failed_port_device_register;
2952 /* info(DRIVER_DESC " " DRIVER_VERSION); */
2953 printk(KERN_INFO KBUILD_MODNAME ":"
2954 DRIVER_DESC " " DRIVER_VERSION "\n");
2956 /* Register with the usb */
2957 retval = usb_register(&aten_driver);
2960 goto failed_usb_register;
2963 DPRINTK("%s\n", "Leaving...");
2967 failed_usb_register:
2968 usb_serial_deregister(&ATENINTL2011_4port_device);
2970 failed_port_device_register:
2975 static void __exit ATENINTL2011_exit(void)
2978 DPRINTK("%s \n", " ATEN2011_exit :entering..........");
2980 usb_deregister(&aten_driver);
2982 usb_serial_deregister(&ATENINTL2011_4port_device);
2984 DPRINTK("%s\n", "End...");
2987 module_init(ATENINTL2011_init);
2988 module_exit(ATENINTL2011_exit);
2990 /* Module information */
2991 MODULE_DESCRIPTION(DRIVER_DESC);
2992 MODULE_LICENSE("GPL");
2994 MODULE_PARM_DESC(debug, "Debug enabled or not");