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 __u8 shadowLCR; /* last LCR value received */
109 __u8 shadowMCR; /* last MCR value received */
111 char chaseResponsePending;
112 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
113 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
114 struct async_icount icount;
115 struct usb_serial_port *port; /* loop back to the owner of this object */
118 __u8 ControlRegOffset;
120 //for processing control URBS in interrupt context
121 struct urb *control_urb;
126 struct urb *write_urb_pool[NUM_URBS];
127 /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
128 struct ktermios tmp_termios; /* stores the old termios settings */
129 spinlock_t lock; /* private lock */
133 /* This structure holds all of the individual serial device information */
134 struct ATENINTL_serial
136 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
137 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
138 struct urb * interrupt_read_urb; /* our interrupt urb */
139 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
140 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
141 struct urb *read_urb; /* our bulk read urb */
142 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
143 struct usb_serial *serial; /* loop back to the owner of this object */
144 int ATEN2011_spectrum_2or4ports; //this says the number of ports in the device
145 // Indicates about the no.of opened ports of an individual USB-serial adapater.
146 unsigned int NoOfOpenPorts;
147 // a flag for Status endpoint polling
148 unsigned char status_polling_started;
151 static void ATEN2011_set_termios(struct tty_struct *tty,
152 struct usb_serial_port *port,
153 struct ktermios *old_termios);
154 static void ATEN2011_change_port_settings(struct tty_struct *tty,
155 struct ATENINTL_port *ATEN2011_port,
156 struct ktermios *old_termios);
158 /*************************************
159 * Bit definitions for each register *
160 *************************************/
161 #define LCR_BITS_5 0x00 /* 5 bits/char */
162 #define LCR_BITS_6 0x01 /* 6 bits/char */
163 #define LCR_BITS_7 0x02 /* 7 bits/char */
164 #define LCR_BITS_8 0x03 /* 8 bits/char */
165 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
167 #define LCR_STOP_1 0x00 /* 1 stop bit */
168 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
169 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
170 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
172 #define LCR_PAR_NONE 0x00 /* No parity */
173 #define LCR_PAR_ODD 0x08 /* Odd parity */
174 #define LCR_PAR_EVEN 0x18 /* Even parity */
175 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
176 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
177 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
179 #define LCR_SET_BREAK 0x40 /* Set Break condition */
180 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
182 #define MCR_DTR 0x01 /* Assert DTR */
183 #define MCR_RTS 0x02 /* Assert RTS */
184 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
185 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
186 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
187 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
189 #define ATEN2011_MSR_CTS 0x10 /* Current state of CTS */
190 #define ATEN2011_MSR_DSR 0x20 /* Current state of DSR */
191 #define ATEN2011_MSR_RI 0x40 /* Current state of RI */
192 #define ATEN2011_MSR_CD 0x80 /* Current state of CD */
195 /* 1: Enables the debugging -- 0: Disable the debugging */
199 static int debug = 0;
200 #define DPRINTK(fmt, args...) printk( "%s: " fmt, __FUNCTION__ , ## args)
203 static int debug = 0;
204 #define DPRINTK(fmt, args...)
209 * Version Information
211 #define DRIVER_VERSION "2.0"
212 #define DRIVER_DESC "ATENINTL 2011 USB Serial Adapter"
215 * Defines used for sending commands to port
218 #define ATEN_WDR_TIMEOUT (50) /* default urb timeout */
221 #define ATEN_RD_RTYPE 0xC0
222 #define ATEN_WR_RTYPE 0x40
223 #define ATEN_RDREQ 0x0D
224 #define ATEN_WRREQ 0x0E
225 #define ATEN_CTRL_TIMEOUT 500
226 #define VENDOR_READ_LENGTH (0x01)
228 /* set to 1 for RS485 mode and 0 for RS232 mode */
229 /* FIXME make this somehow dynamic and not build time specific */
230 static int RS485mode = 0;
232 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port, const
234 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial, const char
236 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port, const char
239 /* setting and get register values */
240 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
242 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
244 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
246 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
249 static void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port);
252 static inline void ATEN2011_set_serial_private(struct usb_serial *serial,
253 struct ATENINTL_serial *data)
255 usb_set_serial_data(serial, (void *)data);
258 static inline struct ATENINTL_serial *ATEN2011_get_serial_private(struct
262 return (struct ATENINTL_serial *)usb_get_serial_data(serial);
265 static inline void ATEN2011_set_port_private(struct usb_serial_port *port,
266 struct ATENINTL_port *data)
268 usb_set_serial_port_data(port, (void *)data);
271 static inline struct ATENINTL_port *ATEN2011_get_port_private(struct
275 return (struct ATENINTL_port *)usb_get_serial_port_data(port);
278 static int ATEN2011_set_reg_sync(struct usb_serial_port *port, __u16 reg,
281 struct usb_device *dev = port->serial->dev;
283 DPRINTK("ATEN2011_set_reg_sync offset is %x, value %x\n", reg, val);
285 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
286 ATEN_WR_RTYPE, val, reg, NULL, 0,
290 static int ATEN2011_get_reg_sync(struct usb_serial_port *port, __u16 reg,
293 struct usb_device *dev = port->serial->dev;
296 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
297 ATEN_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
299 DPRINTK("ATEN2011_get_reg_sync offset is %x, return val %x\n", reg,
301 *val = (*val) & 0x00ff;
305 static int ATEN2011_set_Uart_Reg(struct usb_serial_port *port, __u16 reg,
309 struct usb_device *dev = port->serial->dev;
310 struct ATENINTL_serial *ATEN2011_serial;
312 ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
313 minor = port->serial->minor;
314 if (minor == SERIAL_TTY_NO_MINOR)
317 // For the UART control registers, the application number need to be Or'ed
319 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
320 val |= (((__u16) port->number - (__u16) (minor)) + 1) << 8;
321 DPRINTK("ATEN2011_set_Uart_Reg application number is %x\n",
324 if (((__u16) port->number - (__u16) (minor)) == 0) {
327 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
329 ("ATEN2011_set_Uart_Reg application number is %x\n",
334 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
336 ("ATEN2011_set_Uart_Reg application number is %x\n",
340 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ATEN_WRREQ,
341 ATEN_WR_RTYPE, val, reg, NULL, 0,
346 static int ATEN2011_get_Uart_Reg(struct usb_serial_port *port, __u16 reg,
349 struct usb_device *dev = port->serial->dev;
352 struct ATENINTL_serial *ATEN2011_serial;
353 int minor = port->serial->minor;
354 ATEN2011_serial = ATEN2011_get_serial_private(port->serial);
355 if (minor == SERIAL_TTY_NO_MINOR)
358 //DPRINTK("application number is %4x \n",(((__u16)port->number - (__u16)(minor))+1)<<8);
359 /*Wval is same as application number */
360 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
361 Wval = (((__u16) port->number - (__u16) (minor)) + 1) << 8;
362 DPRINTK("ATEN2011_get_Uart_Reg application number is %x\n",
365 if (((__u16) port->number - (__u16) (minor)) == 0) {
368 (((__u16) port->number - (__u16) (minor)) + 1) << 8;
370 ("ATEN2011_get_Uart_Reg application number is %x\n",
375 (((__u16) port->number - (__u16) (minor)) + 2) << 8;
377 ("ATEN2011_get_Uart_Reg application number is %x\n",
381 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ATEN_RDREQ,
382 ATEN_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
384 *val = (*val) & 0x00ff;
388 static void ATEN2011_Dump_serial_port(struct ATENINTL_port *ATEN2011_port)
391 DPRINTK("***************************************\n");
392 DPRINTK("SpRegOffset is %2x\n", ATEN2011_port->SpRegOffset);
393 DPRINTK("ControlRegOffset is %2x \n", ATEN2011_port->ControlRegOffset);
394 DPRINTK("DCRRegOffset is %2x \n", ATEN2011_port->DcrRegOffset);
395 DPRINTK("***************************************\n");
399 static int handle_newMsr(struct ATENINTL_port *port, __u8 newMsr)
401 struct ATENINTL_port *ATEN2011_port;
402 struct async_icount *icount;
403 ATEN2011_port = port;
404 icount = &ATEN2011_port->icount;
406 (ATEN_MSR_DELTA_CTS | ATEN_MSR_DELTA_DSR | ATEN_MSR_DELTA_RI |
407 ATEN_MSR_DELTA_CD)) {
408 icount = &ATEN2011_port->icount;
410 /* update input line counters */
411 if (newMsr & ATEN_MSR_DELTA_CTS) {
414 if (newMsr & ATEN_MSR_DELTA_DSR) {
417 if (newMsr & ATEN_MSR_DELTA_CD) {
420 if (newMsr & ATEN_MSR_DELTA_RI) {
428 static int handle_newLsr(struct ATENINTL_port *port, __u8 newLsr)
430 struct async_icount *icount;
432 dbg("%s - %02x", __FUNCTION__, newLsr);
434 if (newLsr & SERIAL_LSR_BI) {
436 // Parity and Framing errors only count if they
437 // occur exclusive of a break being
440 newLsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
443 /* update input line counters */
444 icount = &port->icount;
445 if (newLsr & SERIAL_LSR_BI) {
448 if (newLsr & SERIAL_LSR_OE) {
451 if (newLsr & SERIAL_LSR_PE) {
454 if (newLsr & SERIAL_LSR_FE) {
461 static void ATEN2011_control_callback(struct urb *urb)
464 struct ATENINTL_port *ATEN2011_port;
468 DPRINTK("%s", "Invalid Pointer !!!!:\n");
472 switch (urb->status) {
479 /* this urb is terminated, clean up */
480 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
484 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
489 ATEN2011_port = (struct ATENINTL_port *)urb->context;
491 DPRINTK("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
492 DPRINTK("%s ATEN2011_port->MsrLsr is %d port %d\n", __FUNCTION__,
493 ATEN2011_port->MsrLsr, ATEN2011_port->port_num);
494 data = urb->transfer_buffer;
495 regval = (__u8) data[0];
496 DPRINTK("%s data is %x\n", __FUNCTION__, regval);
497 if (ATEN2011_port->MsrLsr == 0)
498 handle_newMsr(ATEN2011_port, regval);
499 else if (ATEN2011_port->MsrLsr == 1)
500 handle_newLsr(ATEN2011_port, regval);
506 static int ATEN2011_get_reg(struct ATENINTL_port *ATEN, __u16 Wval, __u16 reg,
509 struct usb_device *dev = ATEN->port->serial->dev;
510 struct usb_ctrlrequest *dr = NULL;
511 unsigned char *buffer = NULL;
513 buffer = (__u8 *) ATEN->ctrl_buf;
515 // dr=(struct usb_ctrlrequest *)(buffer);
516 dr = (void *)(buffer + 2);
517 dr->bRequestType = ATEN_RD_RTYPE;
518 dr->bRequest = ATEN_RDREQ;
519 dr->wValue = cpu_to_le16(Wval); //0;
520 dr->wIndex = cpu_to_le16(reg);
521 dr->wLength = cpu_to_le16(2);
523 usb_fill_control_urb(ATEN->control_urb, dev, usb_rcvctrlpipe(dev, 0),
524 (unsigned char *)dr, buffer, 2,
525 ATEN2011_control_callback, ATEN);
526 ATEN->control_urb->transfer_buffer_length = 2;
527 ret = usb_submit_urb(ATEN->control_urb, GFP_ATOMIC);
531 static void ATEN2011_interrupt_callback(struct urb *urb)
535 struct ATENINTL_port *ATEN2011_port;
536 struct ATENINTL_serial *ATEN2011_serial;
537 struct usb_serial *serial;
544 //printk("in the function ATEN2011_interrupt_callback Length %d, Data %x \n",urb->actual_length,(unsigned int)urb->transfer_buffer);
545 DPRINTK("%s", " : Entering\n");
547 ATEN2011_serial = (struct ATENINTL_serial *)urb->context;
548 if (!urb) // || ATEN2011_serial->status_polling_started == 0 )
550 DPRINTK("%s", "Invalid Pointer !!!!:\n");
554 switch (urb->status) {
561 /* this urb is terminated, clean up */
562 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
566 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
570 length = urb->actual_length;
571 data = urb->transfer_buffer;
573 //ATEN2011_serial= (struct ATENINTL_serial *)urb->context;
574 //serial = ATEN2011_get_usb_serial(port,__FUNCTION__);
575 serial = ATEN2011_serial->serial;
577 /* ATENINTL get 5 bytes
578 * Byte 1 IIR Port 1 (port.number is 0)
579 * Byte 2 IIR Port 2 (port.number is 1)
580 * Byte 3 IIR Port 3 (port.number is 2)
581 * Byte 4 IIR Port 4 (port.number is 3)
582 * Byte 5 FIFO status for both */
584 if (length && length > 5) {
585 DPRINTK("%s \n", "Wrong data !!!");
590 if (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 4) {
591 sp[0] = (__u8) data[0];
592 sp[1] = (__u8) data[1];
593 sp[2] = (__u8) data[2];
594 sp[3] = (__u8) data[3];
597 sp[0] = (__u8) data[0];
598 sp[1] = (__u8) data[2];
599 //sp[2]=(__u8)data[2];
600 //sp[3]=(__u8)data[3];
604 // printk("%s data is sp1:%x sp2:%x sp3:%x sp4:%x status:%x\n",__FUNCTION__,sp1,sp2,sp3,sp4,st);
605 for (i = 0; i < serial->num_ports; i++) {
606 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
607 minor = serial->minor;
608 if (minor == SERIAL_TTY_NO_MINOR)
610 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
613 (((__u16) serial->port[i]->number -
614 (__u16) (minor)) + 2) << 8;
617 (((__u16) serial->port[i]->number -
618 (__u16) (minor)) + 1) << 8;
619 if (ATEN2011_port->open != 0) {
620 //printk("%s wval is:(for 2011) %x\n",__FUNCTION__,wval);
623 DPRINTK("SP%d No Interrupt !!!\n", i);
625 switch (sp[i] & 0x0f) {
628 ("Serial Port %d: Receiver status error or ",
631 ("address bit detected in 9-bit mode\n");
632 ATEN2011_port->MsrLsr = 1;
633 ATEN2011_get_reg(ATEN2011_port, wval,
634 LINE_STATUS_REGISTER,
639 ("Serial Port %d: Modem status change\n",
641 ATEN2011_port->MsrLsr = 0;
642 ATEN2011_get_reg(ATEN2011_port, wval,
643 MODEM_STATUS_REGISTER,
652 if (ATEN2011_serial->status_polling_started == 0)
655 result = usb_submit_urb(urb, GFP_ATOMIC);
657 dev_err(&urb->dev->dev,
658 "%s - Error %d submitting interrupt urb\n",
659 __FUNCTION__, result);
666 static void ATEN2011_bulk_in_callback(struct urb *urb)
670 struct usb_serial *serial;
671 struct usb_serial_port *port;
672 struct ATENINTL_serial *ATEN2011_serial;
673 struct ATENINTL_port *ATEN2011_port;
674 struct tty_struct *tty;
676 DPRINTK("%s", "Invalid Pointer !!!!:\n");
681 DPRINTK("nonzero read bulk status received: %d", urb->status);
682 // if(urb->status==84)
687 ATEN2011_port = (struct ATENINTL_port *)urb->context;
688 if (!ATEN2011_port) {
689 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
693 port = (struct usb_serial_port *)ATEN2011_port->port;
694 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
695 DPRINTK("%s", "Port Paranoia failed \n");
699 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
701 DPRINTK("%s\n", "Bad serial pointer ");
705 DPRINTK("%s\n", "Entering... \n");
707 data = urb->transfer_buffer;
708 ATEN2011_serial = ATEN2011_get_serial_private(serial);
710 DPRINTK("%s", "Entering ........... \n");
712 if (urb->actual_length) {
713 tty = tty_port_tty_get(&ATEN2011_port->port->port);
715 tty_buffer_request_room(tty, urb->actual_length);
716 tty_insert_flip_string(tty, data, urb->actual_length);
717 DPRINTK(" %s \n", data);
718 tty_flip_buffer_push(tty);
722 ATEN2011_port->icount.rx += urb->actual_length;
723 DPRINTK("ATEN2011_port->icount.rx is %d:\n",
724 ATEN2011_port->icount.rx);
728 if (!ATEN2011_port->read_urb) {
729 DPRINTK("%s", "URB KILLED !!!\n");
733 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
734 ATEN2011_port->read_urb->dev = serial->dev;
736 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
740 (" usb_submit_urb(read bulk) failed, status = %d",
746 static void ATEN2011_bulk_out_data_callback(struct urb *urb)
748 struct ATENINTL_port *ATEN2011_port;
749 struct tty_struct *tty;
751 DPRINTK("%s", "Invalid Pointer !!!!:\n");
756 DPRINTK("nonzero write bulk status received:%d\n", urb->status);
760 ATEN2011_port = (struct ATENINTL_port *)urb->context;
761 if (!ATEN2011_port) {
762 DPRINTK("%s", "NULL ATEN2011_port pointer \n");
766 if (ATEN2011_port_paranoia_check(ATEN2011_port->port, __FUNCTION__)) {
767 DPRINTK("%s", "Port Paranoia failed \n");
771 DPRINTK("%s \n", "Entering .........");
773 tty = tty_port_tty_get(&ATEN2011_port->port->port);
775 if (tty && ATEN2011_port->open) {
776 /* tell the tty driver that something has changed */
777 wake_up_interruptible(&tty->write_wait);
780 //schedule_work(&ATEN2011_port->port->work);
785 #ifdef ATENSerialProbe
786 static int ATEN2011_serial_probe(struct usb_serial *serial,
787 const struct usb_device_id *id)
790 /*need to implement the mode_reg reading and updating\
791 structures usb_serial_ device_type\
792 (i.e num_ports, num_bulkin,bulkout etc) */
793 /* Also we can update the changes attach */
798 static int ATEN2011_open(struct tty_struct *tty, struct usb_serial_port *port,
803 struct usb_serial *serial;
804 // struct usb_serial_port *port0;
808 struct ATENINTL_serial *ATEN2011_serial;
809 struct ATENINTL_port *ATEN2011_port;
810 struct ktermios tmp_termios;
813 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
814 DPRINTK("%s", "Port Paranoia failed \n");
817 //ATEN2011_serial->NoOfOpenPorts++;
818 serial = port->serial;
820 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
821 DPRINTK("%s", "Serial Paranoia failed \n");
825 ATEN2011_port = ATEN2011_get_port_private(port);
827 if (ATEN2011_port == NULL)
830 if (ATEN2011_port->ctrl_buf==NULL)
832 ATEN2011_port->ctrl_buf = kmalloc(16,GFP_KERNEL);
833 if (ATEN2011_port->ctrl_buf == NULL) {
834 printk(", Can't allocate ctrl buff\n");
840 if(!ATEN2011_port->control_urb)
842 ATEN2011_port->control_urb=kmalloc(sizeof(struct urb),GFP_KERNEL);
845 // port0 = serial->port[0];
847 ATEN2011_serial = ATEN2011_get_serial_private(serial);
849 if (ATEN2011_serial == NULL) //|| port0 == NULL)
853 // increment the number of opened ports counter here
854 ATEN2011_serial->NoOfOpenPorts++;
855 //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
857 usb_clear_halt(serial->dev, port->write_urb->pipe);
858 usb_clear_halt(serial->dev, port->read_urb->pipe);
860 /* Initialising the write urb pool */
861 for (j = 0; j < NUM_URBS; ++j) {
862 urb = usb_alloc_urb(0, GFP_ATOMIC);
863 ATEN2011_port->write_urb_pool[j] = urb;
866 err("No more urbs???");
870 urb->transfer_buffer = NULL;
871 urb->transfer_buffer =
872 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
873 if (!urb->transfer_buffer) {
874 err("%s-out of memory for urb buffers.", __FUNCTION__);
879 /*****************************************************************************
880 * Initialize ATEN2011 -- Write Init values to corresponding Registers
888 * 0x08 : SP1/2 Control Reg
889 *****************************************************************************/
891 //NEED to check the fallowing Block
895 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
897 DPRINTK("Reading Spreg failed\n");
901 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
903 DPRINTK("writing Spreg failed\n");
908 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
910 DPRINTK("writing Spreg failed\n");
914 //End of block to be checked
915 //**************************CHECK***************************//
922 status = ATEN2011_set_Uart_Reg(port, SCRATCH_PAD_REGISTER, Data);
924 DPRINTK("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
928 DPRINTK("SCRATCH_PAD_REGISTER Writing success status%d\n",
931 //**************************CHECK***************************//
936 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
938 DPRINTK("Reading Controlreg failed\n");
941 Data |= 0x08; //Driver done bit
943 status = ATEN2011_set_reg_sync(port,ATEN2011_port->ControlRegOffset,Data);
945 DPRINTK("writing Controlreg failed\n");
949 Data |= 0x20; //rx_disable
952 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
954 DPRINTK("writing Controlreg failed\n");
957 //do register settings here
958 // Set all regs to the device default values.
959 ////////////////////////////////////
960 // First Disable all interrupts.
961 ////////////////////////////////////
965 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
967 DPRINTK("disableing interrupts failed\n");
970 // Set FIFO_CONTROL_REGISTER to the default value
973 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
975 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
981 status = ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
983 DPRINTK("Writing FIFO_CONTROL_REGISTER failed\n");
987 Data = 0x03; //LCR_BITS_8
989 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
990 ATEN2011_port->shadowLCR = Data;
992 Data = 0x0b; // MCR_DTR|MCR_RTS|MCR_MASTER_IE
994 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
995 ATEN2011_port->shadowMCR = Data;
1000 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1001 ATEN2011_port->shadowLCR = Data;
1003 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1005 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1009 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
1013 status = ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
1017 status = ATEN2011_get_Uart_Reg(port, LINE_CONTROL_REGISTER, &Data);
1019 // Data = ATEN2011_port->shadowLCR; //data latch disable
1020 Data = Data & ~SERIAL_LCR_DLAB;
1022 status = ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
1023 ATEN2011_port->shadowLCR = Data;
1025 //clearing Bulkin and Bulkout Fifo
1028 status = ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset, &Data);
1032 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1034 Data = Data & ~0x0c;
1036 status = ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset, Data);
1037 //Finally enable all interrupts
1041 status = ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1043 //clearing rx_disable
1047 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1048 Data = Data & ~0x20;
1051 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1057 ATEN2011_get_reg_sync(port, ATEN2011_port->ControlRegOffset, &Data);
1061 ATEN2011_set_reg_sync(port, ATEN2011_port->ControlRegOffset, Data);
1063 /* force low_latency on so that our tty_push actually forces *
1064 * the data through,otherwise it is scheduled, and with *
1065 * high data rates (like with OHCI) data can get lost. */
1068 tty->low_latency = 1;
1070 printk("port number is %d \n",port->number);
1071 printk("serial number is %d \n",port->serial->minor);
1072 printk("Bulkin endpoint is %d \n",port->bulk_in_endpointAddress);
1073 printk("BulkOut endpoint is %d \n",port->bulk_out_endpointAddress);
1074 printk("Interrupt endpoint is %d \n",port->interrupt_in_endpointAddress);
1075 printk("port's number in the device is %d\n",ATEN2011_port->port_num);
1077 ////////////////////////
1078 //#ifdef CheckStatusPipe
1079 /* Check to see if we've set up our endpoint info yet *
1080 * (can't set it up in ATEN2011_startup as the structures *
1081 * were not set up at that time.) */
1082 if (ATEN2011_serial->NoOfOpenPorts == 1) {
1083 // start the status polling here
1084 ATEN2011_serial->status_polling_started = 1;
1085 //if (ATEN2011_serial->interrupt_in_buffer == NULL)
1087 /* If not yet set, Set here */
1088 ATEN2011_serial->interrupt_in_buffer =
1089 serial->port[0]->interrupt_in_buffer;
1090 ATEN2011_serial->interrupt_in_endpoint =
1091 serial->port[0]->interrupt_in_endpointAddress;
1092 //printk(" interrupt endpoint:%d \n",ATEN2011_serial->interrupt_in_endpoint);
1093 ATEN2011_serial->interrupt_read_urb =
1094 serial->port[0]->interrupt_in_urb;
1096 /* set up interrupt urb */
1097 usb_fill_int_urb(ATEN2011_serial->interrupt_read_urb,
1099 usb_rcvintpipe(serial->dev,
1101 interrupt_in_endpoint),
1102 ATEN2011_serial->interrupt_in_buffer,
1103 ATEN2011_serial->interrupt_read_urb->
1104 transfer_buffer_length,
1105 ATEN2011_interrupt_callback, ATEN2011_serial,
1106 ATEN2011_serial->interrupt_read_urb->interval);
1108 /* start interrupt read for ATEN2011 *
1109 * will continue as long as ATEN2011 is connected */
1112 usb_submit_urb(ATEN2011_serial->interrupt_read_urb,
1115 DPRINTK("%s - Error %d submitting interrupt urb",
1116 __FUNCTION__, response);
1119 // printk(" interrupt URB submitted\n");
1126 ///////////////////////
1127 /* see if we've set up our endpoint info yet *
1128 * (can't set it up in ATEN2011_startup as the *
1129 * structures were not set up at that time.) */
1131 DPRINTK("port number is %d \n", port->number);
1132 DPRINTK("serial number is %d \n", port->serial->minor);
1133 DPRINTK("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1134 DPRINTK("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1135 DPRINTK("Interrupt endpoint is %d \n",
1136 port->interrupt_in_endpointAddress);
1137 DPRINTK("port's number in the device is %d\n", ATEN2011_port->port_num);
1138 ATEN2011_port->bulk_in_buffer = port->bulk_in_buffer;
1139 ATEN2011_port->bulk_in_endpoint = port->bulk_in_endpointAddress;
1140 ATEN2011_port->read_urb = port->read_urb;
1141 ATEN2011_port->bulk_out_endpoint = port->bulk_out_endpointAddress;
1143 minor = port->serial->minor;
1144 if (minor == SERIAL_TTY_NO_MINOR)
1147 /* set up our bulk in urb */
1148 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1149 && (((__u16) port->number - (__u16) (minor)) != 0)) {
1150 usb_fill_bulk_urb(ATEN2011_port->read_urb, serial->dev,
1151 usb_rcvbulkpipe(serial->dev,
1153 bulk_in_endpointAddress +
1154 2)), port->bulk_in_buffer,
1155 ATEN2011_port->read_urb->
1156 transfer_buffer_length,
1157 ATEN2011_bulk_in_callback, ATEN2011_port);
1159 usb_fill_bulk_urb(ATEN2011_port->read_urb,
1161 usb_rcvbulkpipe(serial->dev,
1163 bulk_in_endpointAddress),
1164 port->bulk_in_buffer,
1165 ATEN2011_port->read_urb->
1166 transfer_buffer_length,
1167 ATEN2011_bulk_in_callback, ATEN2011_port);
1169 DPRINTK("ATEN2011_open: bulkin endpoint is %d\n",
1170 port->bulk_in_endpointAddress);
1171 response = usb_submit_urb(ATEN2011_port->read_urb, GFP_KERNEL);
1173 err("%s - Error %d submitting control urb", __FUNCTION__,
1177 /* initialize our wait queues */
1178 init_waitqueue_head(&ATEN2011_port->wait_chase);
1179 init_waitqueue_head(&ATEN2011_port->wait_command);
1181 /* initialize our icount structure */
1182 memset(&(ATEN2011_port->icount), 0x00, sizeof(ATEN2011_port->icount));
1184 /* initialize our port settings */
1185 ATEN2011_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1186 ATEN2011_port->chaseResponsePending = 0;
1187 /* send a open port command */
1188 ATEN2011_port->open = 1;
1189 //ATEN2011_change_port_settings(ATEN2011_port,old_termios);
1191 ATEN2011_set_termios(tty, port, &tmp_termios);
1192 ATEN2011_port->icount.tx = 0;
1193 ATEN2011_port->icount.rx = 0;
1196 ("\n\nusb_serial serial:%x ATEN2011_port:%x\nATEN2011_serial:%x usb_serial_port port:%x\n\n",
1197 (unsigned int)serial, (unsigned int)ATEN2011_port,
1198 (unsigned int)ATEN2011_serial, (unsigned int)port);
1204 static int ATEN2011_chars_in_buffer(struct tty_struct *tty)
1206 struct usb_serial_port *port = tty->driver_data;
1209 struct ATENINTL_port *ATEN2011_port;
1211 //DPRINTK("%s \n"," ATEN2011_chars_in_buffer:entering ...........");
1213 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1214 DPRINTK("%s", "Invalid port \n");
1218 ATEN2011_port = ATEN2011_get_port_private(port);
1219 if (ATEN2011_port == NULL) {
1220 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1224 for (i = 0; i < NUM_URBS; ++i) {
1225 if (ATEN2011_port->write_urb_pool[i]->status == -EINPROGRESS) {
1226 chars += URB_TRANSFER_BUFFER_SIZE;
1229 dbg("%s - returns %d", __FUNCTION__, chars);
1234 static void ATEN2011_block_until_tx_empty(struct tty_struct *tty,
1235 struct ATENINTL_port *ATEN2011_port)
1237 int timeout = HZ / 10;
1243 count = ATEN2011_chars_in_buffer(tty);
1245 /* Check for Buffer status */
1250 /* Block the thread for a while */
1251 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1254 /* No activity.. count down section */
1257 dbg("%s - TIMEOUT", __FUNCTION__);
1260 /* Reset timout value back to seconds */
1266 static void ATEN2011_close(struct tty_struct *tty, struct usb_serial_port *port,
1269 struct usb_serial *serial;
1270 struct ATENINTL_serial *ATEN2011_serial;
1271 struct ATENINTL_port *ATEN2011_port;
1276 DPRINTK("%s\n", "ATEN2011_close:entering...");
1280 //printk("Entering... :ATEN2011_close\n");
1281 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1282 DPRINTK("%s", "Port Paranoia failed \n");
1285 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1287 DPRINTK("%s", "Serial Paranoia failed \n");
1290 // take the Adpater and port's private data
1291 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1292 ATEN2011_port = ATEN2011_get_port_private(port);
1293 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1297 /* flush and block(wait) until tx is empty */
1298 ATEN2011_block_until_tx_empty(tty, ATEN2011_port);
1300 // kill the ports URB's
1301 for (no_urbs = 0; no_urbs < NUM_URBS; no_urbs++)
1302 usb_kill_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1303 /* Freeing Write URBs */
1304 for (no_urbs = 0; no_urbs < NUM_URBS; ++no_urbs) {
1305 if (ATEN2011_port->write_urb_pool[no_urbs]) {
1306 if (ATEN2011_port->write_urb_pool[no_urbs]->
1308 kfree(ATEN2011_port->write_urb_pool[no_urbs]->
1310 usb_free_urb(ATEN2011_port->write_urb_pool[no_urbs]);
1313 /* While closing port, shutdown all bulk read, write *
1314 * and interrupt read if they exists */
1316 if (ATEN2011_port->write_urb) {
1317 DPRINTK("%s", "Shutdown bulk write\n");
1318 usb_kill_urb(ATEN2011_port->write_urb);
1320 if (ATEN2011_port->read_urb) {
1321 DPRINTK("%s", "Shutdown bulk read\n");
1322 usb_kill_urb(ATEN2011_port->read_urb);
1324 if ((&ATEN2011_port->control_urb)) {
1325 DPRINTK("%s", "Shutdown control read\n");
1326 // usb_kill_urb (ATEN2011_port->control_urb);
1330 //if(ATEN2011_port->ctrl_buf != NULL)
1331 //kfree(ATEN2011_port->ctrl_buf);
1332 // decrement the no.of open ports counter of an individual USB-serial adapter.
1333 ATEN2011_serial->NoOfOpenPorts--;
1334 DPRINTK("NoOfOpenPorts in close%d:in port%d\n",
1335 ATEN2011_serial->NoOfOpenPorts, port->number);
1336 //printk("the num of ports opend is:%d\n",ATEN2011_serial->NoOfOpenPorts);
1337 if (ATEN2011_serial->NoOfOpenPorts == 0) {
1338 //stop the stus polling here
1339 //printk("disabling the status polling flag to 0 :\n");
1340 ATEN2011_serial->status_polling_started = 0;
1341 if (ATEN2011_serial->interrupt_read_urb) {
1342 DPRINTK("%s", "Shutdown interrupt_read_urb\n");
1343 //ATEN2011_serial->interrupt_in_buffer=NULL;
1344 //usb_kill_urb (ATEN2011_serial->interrupt_read_urb);
1347 if (ATEN2011_port->write_urb) {
1348 /* if this urb had a transfer buffer already (old tx) free it */
1349 if (ATEN2011_port->write_urb->transfer_buffer != NULL) {
1350 kfree(ATEN2011_port->write_urb->transfer_buffer);
1352 usb_free_urb(ATEN2011_port->write_urb);
1354 // clear the MCR & IER
1356 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1358 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1360 //ATEN2011_get_Uart_Reg(port,MODEM_CONTROL_REGISTER,&Data1);
1361 //printk("value of MCR after closing the port is : 0x%x\n",Data1);
1363 ATEN2011_port->open = 0;
1364 DPRINTK("%s \n", "Leaving ............");
1368 static void ATEN2011_block_until_chase_response(struct tty_struct *tty,
1369 struct ATENINTL_port
1372 int timeout = 1 * HZ;
1377 count = ATEN2011_chars_in_buffer(tty);
1379 /* Check for Buffer status */
1381 ATEN2011_port->chaseResponsePending = 0;
1385 /* Block the thread for a while */
1386 interruptible_sleep_on_timeout(&ATEN2011_port->wait_chase,
1388 /* No activity.. count down section */
1391 dbg("%s - TIMEOUT", __FUNCTION__);
1394 /* Reset timout value back to seconds */
1401 static void ATEN2011_break(struct tty_struct *tty, int break_state)
1403 struct usb_serial_port *port = tty->driver_data;
1405 struct usb_serial *serial;
1406 struct ATENINTL_serial *ATEN2011_serial;
1407 struct ATENINTL_port *ATEN2011_port;
1409 DPRINTK("%s \n", "Entering ...........");
1410 DPRINTK("ATEN2011_break: Start\n");
1412 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1413 DPRINTK("%s", "Port Paranoia failed \n");
1417 serial = ATEN2011_get_usb_serial(port, __FUNCTION__);
1419 DPRINTK("%s", "Serial Paranoia failed \n");
1423 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1424 ATEN2011_port = ATEN2011_get_port_private(port);
1426 if ((ATEN2011_serial == NULL) || (ATEN2011_port == NULL)) {
1430 /* flush and chase */
1431 ATEN2011_port->chaseResponsePending = 1;
1435 /* flush and block until tx is empty */
1436 ATEN2011_block_until_chase_response(tty, ATEN2011_port);
1439 if (break_state == -1) {
1440 data = ATEN2011_port->shadowLCR | LCR_SET_BREAK;
1442 data = ATEN2011_port->shadowLCR & ~LCR_SET_BREAK;
1445 ATEN2011_port->shadowLCR = data;
1446 DPRINTK("ATEN2011_break ATEN2011_port->shadowLCR is %x\n",
1447 ATEN2011_port->shadowLCR);
1448 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER,
1449 ATEN2011_port->shadowLCR);
1454 static int ATEN2011_write_room(struct tty_struct *tty)
1456 struct usb_serial_port *port = tty->driver_data;
1459 struct ATENINTL_port *ATEN2011_port;
1461 // DPRINTK("%s \n"," ATEN2011_write_room:entering ...........");
1463 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1464 DPRINTK("%s", "Invalid port \n");
1465 DPRINTK("%s \n", " ATEN2011_write_room:leaving ...........");
1469 ATEN2011_port = ATEN2011_get_port_private(port);
1470 if (ATEN2011_port == NULL) {
1471 DPRINTK("%s \n", "ATEN2011_break:leaving ...........");
1475 for (i = 0; i < NUM_URBS; ++i) {
1476 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1477 room += URB_TRANSFER_BUFFER_SIZE;
1481 dbg("%s - returns %d", __FUNCTION__, room);
1486 static int ATEN2011_write(struct tty_struct *tty, struct usb_serial_port *port,
1487 const unsigned char *data, int count)
1495 struct ATENINTL_port *ATEN2011_port;
1496 struct usb_serial *serial;
1497 struct ATENINTL_serial *ATEN2011_serial;
1500 const unsigned char *current_position = data;
1501 unsigned char *data1;
1502 DPRINTK("%s \n", "entering ...........");
1504 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1505 DPRINTK("%s", "Port Paranoia failed \n");
1509 serial = port->serial;
1510 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1511 DPRINTK("%s", "Serial Paranoia failed \n");
1515 ATEN2011_port = ATEN2011_get_port_private(port);
1516 if (ATEN2011_port == NULL) {
1517 DPRINTK("%s", "ATEN2011_port is NULL\n");
1521 ATEN2011_serial = ATEN2011_get_serial_private(serial);
1522 if (ATEN2011_serial == NULL) {
1523 DPRINTK("%s", "ATEN2011_serial is NULL \n");
1527 /* try to find a free urb in the list */
1530 for (i = 0; i < NUM_URBS; ++i) {
1531 if (ATEN2011_port->write_urb_pool[i]->status != -EINPROGRESS) {
1532 urb = ATEN2011_port->write_urb_pool[i];
1533 DPRINTK("\nURB:%d", i);
1539 dbg("%s - no more free urbs", __FUNCTION__);
1543 if (urb->transfer_buffer == NULL) {
1544 urb->transfer_buffer =
1545 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1547 if (urb->transfer_buffer == NULL) {
1548 err("%s no more kernel memory...", __FUNCTION__);
1552 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1554 memcpy(urb->transfer_buffer, current_position, transfer_size);
1555 //usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
1557 /* fill urb with data and submit */
1558 minor = port->serial->minor;
1559 if (minor == SERIAL_TTY_NO_MINOR) ;
1561 if ((ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)
1562 && (((__u16) port->number - (__u16) (minor)) != 0)) {
1563 usb_fill_bulk_urb(urb, ATEN2011_serial->serial->dev,
1564 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1566 bulk_out_endpointAddress) +
1567 2), urb->transfer_buffer,
1569 ATEN2011_bulk_out_data_callback,
1573 usb_fill_bulk_urb(urb,
1574 ATEN2011_serial->serial->dev,
1575 usb_sndbulkpipe(ATEN2011_serial->serial->dev,
1577 bulk_out_endpointAddress),
1578 urb->transfer_buffer, transfer_size,
1579 ATEN2011_bulk_out_data_callback,
1582 data1 = urb->transfer_buffer;
1583 DPRINTK("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1584 //for(i=0;i < urb->actual_length;i++)
1585 // DPRINTK("Data is %c\n ",data1[i]);
1587 /* send it down the pipe */
1588 status = usb_submit_urb(urb, GFP_ATOMIC);
1591 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1592 __FUNCTION__, status);
1593 bytes_sent = status;
1596 bytes_sent = transfer_size;
1597 ATEN2011_port->icount.tx += transfer_size;
1598 DPRINTK("ATEN2011_port->icount.tx is %d:\n", ATEN2011_port->icount.tx);
1605 static void ATEN2011_throttle(struct tty_struct *tty)
1607 struct usb_serial_port *port = tty->driver_data;
1608 struct ATENINTL_port *ATEN2011_port;
1611 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1612 DPRINTK("%s", "Invalid port \n");
1616 DPRINTK("- port %d\n", port->number);
1618 ATEN2011_port = ATEN2011_get_port_private(port);
1620 if (ATEN2011_port == NULL)
1623 if (!ATEN2011_port->open) {
1624 DPRINTK("%s\n", "port not opened");
1628 DPRINTK("%s", "Entering .......... \n");
1631 dbg("%s - no tty available", __FUNCTION__);
1635 /* if we are implementing XON/XOFF, send the stop character */
1637 unsigned char stop_char = STOP_CHAR(tty);
1638 status = ATEN2011_write(tty, port, &stop_char, 1); //FC4
1644 /* if we are implementing RTS/CTS, toggle that line */
1645 if (tty->termios->c_cflag & CRTSCTS) {
1646 ATEN2011_port->shadowMCR &= ~MCR_RTS;
1649 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
1650 ATEN2011_port->shadowMCR);
1660 static void ATEN2011_unthrottle(struct tty_struct *tty)
1662 struct usb_serial_port *port = tty->driver_data;
1664 struct ATENINTL_port *ATEN2011_port = ATEN2011_get_port_private(port);
1666 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1667 DPRINTK("%s", "Invalid port \n");
1671 if (ATEN2011_port == NULL)
1674 if (!ATEN2011_port->open) {
1675 dbg("%s - port not opened", __FUNCTION__);
1679 DPRINTK("%s", "Entering .......... \n");
1682 dbg("%s - no tty available", __FUNCTION__);
1686 /* if we are implementing XON/XOFF, send the start character */
1688 unsigned char start_char = START_CHAR(tty);
1689 status = ATEN2011_write(tty, port, &start_char, 1); //FC4
1695 /* if we are implementing RTS/CTS, toggle that line */
1696 if (tty->termios->c_cflag & CRTSCTS) {
1697 ATEN2011_port->shadowMCR |= MCR_RTS;
1700 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER,
1701 ATEN2011_port->shadowMCR);
1710 static int ATEN2011_tiocmget(struct tty_struct *tty, struct file *file)
1712 struct usb_serial_port *port = tty->driver_data;
1713 //struct ti_port *tport = usb_get_serial_port_data(port);
1714 struct ATENINTL_port *ATEN2011_port;
1715 unsigned int result;
1720 ATEN2011_port = ATEN2011_get_port_private(port);
1722 DPRINTK("%s - port %d", __FUNCTION__, port->number);
1724 if (ATEN2011_port == NULL)
1727 status = ATEN2011_get_Uart_Reg(port, MODEM_STATUS_REGISTER, &msr);
1728 status = ATEN2011_get_Uart_Reg(port, MODEM_CONTROL_REGISTER, &mcr);
1729 // mcr = ATEN2011_port->shadowMCR;
1730 // COMMENT2: the Fallowing three line are commented for updating only MSR values
1731 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1732 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1733 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1734 | ((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0)
1735 | ((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0)
1736 | ((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0)
1737 | ((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0);
1739 DPRINTK("%s - 0x%04X", __FUNCTION__, result);
1744 static int ATEN2011_tiocmset(struct tty_struct *tty, struct file *file,
1745 unsigned int set, unsigned int clear)
1747 struct usb_serial_port *port = tty->driver_data;
1748 struct ATENINTL_port *ATEN2011_port;
1749 //struct ti_port *tport = usb_get_serial_port_data(port);
1751 unsigned int status;
1753 DPRINTK("%s - port %d", __FUNCTION__, port->number);
1755 ATEN2011_port = ATEN2011_get_port_private(port);
1757 if (ATEN2011_port == NULL)
1760 mcr = ATEN2011_port->shadowMCR;
1761 if (clear & TIOCM_RTS)
1763 if (clear & TIOCM_DTR)
1765 if (clear & TIOCM_LOOP)
1766 mcr &= ~MCR_LOOPBACK;
1768 if (set & TIOCM_RTS)
1770 if (set & TIOCM_DTR)
1772 if (set & TIOCM_LOOP)
1773 mcr |= MCR_LOOPBACK;
1775 ATEN2011_port->shadowMCR = mcr;
1778 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, mcr);
1780 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
1787 static void ATEN2011_set_termios(struct tty_struct *tty,
1788 struct usb_serial_port *port,
1789 struct ktermios *old_termios)
1793 struct usb_serial *serial;
1794 struct ATENINTL_port *ATEN2011_port;
1796 DPRINTK("ATEN2011_set_termios: START\n");
1797 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1798 DPRINTK("%s", "Invalid port \n");
1802 serial = port->serial;
1804 if (ATEN2011_serial_paranoia_check(serial, __FUNCTION__)) {
1805 DPRINTK("%s", "Invalid Serial \n");
1809 ATEN2011_port = ATEN2011_get_port_private(port);
1811 if (ATEN2011_port == NULL)
1814 if (!ATEN2011_port->open) {
1815 dbg("%s - port not opened", __FUNCTION__);
1819 DPRINTK("%s\n", "setting termios - ");
1821 cflag = tty->termios->c_cflag;
1824 DPRINTK("%s %s\n", __FUNCTION__, "cflag is NULL");
1828 /* check that they really want us to change something */
1830 if ((cflag == old_termios->c_cflag) &&
1831 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1832 RELEVANT_IFLAG(old_termios->c_iflag))) {
1833 DPRINTK("%s\n", "Nothing to change");
1838 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1839 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
1842 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1843 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1846 dbg("%s - port %d", __FUNCTION__, port->number);
1848 /* change the port settings to the new ones specified */
1850 ATEN2011_change_port_settings(tty, ATEN2011_port, old_termios);
1852 if (!ATEN2011_port->read_urb) {
1853 DPRINTK("%s", "URB KILLED !!!!!\n");
1857 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
1858 ATEN2011_port->read_urb->dev = serial->dev;
1859 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
1862 (" usb_submit_urb(read bulk) failed, status = %d",
1869 static int get_lsr_info(struct tty_struct *tty,
1870 struct ATENINTL_port *ATEN2011_port,
1871 unsigned int __user *value)
1874 unsigned int result = 0;
1876 count = ATEN2011_chars_in_buffer(tty);
1878 dbg("%s -- Empty", __FUNCTION__);
1879 result = TIOCSER_TEMT;
1882 if (copy_to_user(value, &result, sizeof(int)))
1887 static int get_number_bytes_avail(struct tty_struct *tty,
1888 struct ATENINTL_port *ATEN2011_port,
1889 unsigned int __user *value)
1891 unsigned int result = 0;
1894 return -ENOIOCTLCMD;
1896 result = tty->read_cnt;
1898 dbg("%s(%d) = %d", __FUNCTION__, ATEN2011_port->port->number, result);
1899 if (copy_to_user(value, &result, sizeof(int)))
1902 return -ENOIOCTLCMD;
1905 static int set_modem_info(struct ATENINTL_port *ATEN2011_port, unsigned int cmd,
1906 unsigned int __user *value)
1912 struct usb_serial_port *port;
1914 if (ATEN2011_port == NULL)
1917 port = (struct usb_serial_port *)ATEN2011_port->port;
1918 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
1919 DPRINTK("%s", "Invalid port \n");
1923 mcr = ATEN2011_port->shadowMCR;
1925 if (copy_from_user(&arg, value, sizeof(int)))
1930 if (arg & TIOCM_RTS)
1932 if (arg & TIOCM_DTR)
1934 if (arg & TIOCM_LOOP)
1935 mcr |= MCR_LOOPBACK;
1939 if (arg & TIOCM_RTS)
1941 if (arg & TIOCM_DTR)
1943 if (arg & TIOCM_LOOP)
1944 mcr &= ~MCR_LOOPBACK;
1948 /* turn off the RTS and DTR and LOOPBACK
1949 * and then only turn on what was asked to */
1950 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
1951 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
1952 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
1953 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
1957 ATEN2011_port->shadowMCR = mcr;
1959 Data = ATEN2011_port->shadowMCR;
1961 status = ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
1963 DPRINTK("setting MODEM_CONTROL_REGISTER Failed\n");
1970 static int get_modem_info(struct ATENINTL_port *ATEN2011_port,
1971 unsigned int __user *value)
1973 unsigned int result = 0;
1975 unsigned int mcr = ATEN2011_port->shadowMCR;
1978 ATEN2011_get_Uart_Reg(ATEN2011_port->port, MODEM_STATUS_REGISTER,
1980 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1981 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1982 |((msr & ATEN2011_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1983 |((msr & ATEN2011_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
1984 |((msr & ATEN2011_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1985 |((msr & ATEN2011_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1987 dbg("%s -- %x", __FUNCTION__, result);
1989 if (copy_to_user(value, &result, sizeof(int)))
1994 static int get_serial_info(struct ATENINTL_port *ATEN2011_port,
1995 struct serial_struct __user *retinfo)
1997 struct serial_struct tmp;
1999 if (ATEN2011_port == NULL)
2005 memset(&tmp, 0, sizeof(tmp));
2007 tmp.type = PORT_16550A;
2008 tmp.line = ATEN2011_port->port->serial->minor;
2009 if (tmp.line == SERIAL_TTY_NO_MINOR)
2011 tmp.port = ATEN2011_port->port->number;
2013 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2014 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2015 tmp.baud_base = 9600;
2016 tmp.close_delay = 5 * HZ;
2017 tmp.closing_wait = 30 * HZ;
2019 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2024 static int ATEN2011_ioctl(struct tty_struct *tty, struct file *file,
2025 unsigned int cmd, unsigned long arg)
2027 struct usb_serial_port *port = tty->driver_data;
2028 struct ATENINTL_port *ATEN2011_port;
2029 struct async_icount cnow;
2030 struct async_icount cprev;
2031 struct serial_icounter_struct icount;
2033 unsigned int __user *user_arg = (unsigned int __user *)arg;
2035 //printk("%s - port %d, cmd = 0x%x\n", __FUNCTION__, port->number, cmd);
2036 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2037 DPRINTK("%s", "Invalid port \n");
2041 ATEN2011_port = ATEN2011_get_port_private(port);
2043 if (ATEN2011_port == NULL)
2046 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2049 /* return number of bytes available */
2052 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2053 return get_number_bytes_avail(tty, ATEN2011_port, user_arg);
2057 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2058 return put_user(ATEN2011_chars_in_buffer(tty), user_arg);
2062 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2063 return get_lsr_info(tty, ATEN2011_port, user_arg);
2069 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2071 // printk("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__, port->number);
2073 set_modem_info(ATEN2011_port, cmd, user_arg);
2074 // printk(" %s: ret:%d\n",__FUNCTION__,ATENret);
2078 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2079 return get_modem_info(ATEN2011_port, user_arg);
2082 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2083 return get_serial_info(ATEN2011_port,
2084 (struct serial_struct __user *)arg);
2087 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2091 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2092 cprev = ATEN2011_port->icount;
2094 /* see if a signal did it */
2095 if (signal_pending(current))
2096 return -ERESTARTSYS;
2097 cnow = ATEN2011_port->icount;
2098 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2099 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2100 return -EIO; /* no change => error */
2101 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2102 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2103 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2104 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2113 cnow = ATEN2011_port->icount;
2114 icount.cts = cnow.cts;
2115 icount.dsr = cnow.dsr;
2116 icount.rng = cnow.rng;
2117 icount.dcd = cnow.dcd;
2118 icount.rx = cnow.rx;
2119 icount.tx = cnow.tx;
2120 icount.frame = cnow.frame;
2121 icount.overrun = cnow.overrun;
2122 icount.parity = cnow.parity;
2123 icount.brk = cnow.brk;
2124 icount.buf_overrun = cnow.buf_overrun;
2126 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2127 port->number, icount.rx, icount.tx);
2128 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
2136 return -ENOIOCTLCMD;
2139 static int ATEN2011_calc_baud_rate_divisor(int baudRate, int *divisor,
2140 __u16 * clk_sel_val)
2143 //__u16 custom,round1, round;
2145 dbg("%s - %d", __FUNCTION__, baudRate);
2147 if (baudRate <= 115200) {
2148 *divisor = 115200 / baudRate;
2151 if ((baudRate > 115200) && (baudRate <= 230400)) {
2152 *divisor = 230400 / baudRate;
2153 *clk_sel_val = 0x10;
2154 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
2155 *divisor = 403200 / baudRate;
2156 *clk_sel_val = 0x20;
2157 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
2158 *divisor = 460800 / baudRate;
2159 *clk_sel_val = 0x30;
2160 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
2161 *divisor = 806400 / baudRate;
2162 *clk_sel_val = 0x40;
2163 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
2164 *divisor = 921600 / baudRate;
2165 *clk_sel_val = 0x50;
2166 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
2167 *divisor = 1572864 / baudRate;
2168 *clk_sel_val = 0x60;
2169 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
2170 *divisor = 3145728 / baudRate;
2171 *clk_sel_val = 0x70;
2176 static int ATEN2011_send_cmd_write_baud_rate(struct ATENINTL_port
2177 *ATEN2011_port, int baudRate)
2182 unsigned char number;
2184 struct usb_serial_port *port;
2187 if (ATEN2011_port == NULL)
2190 port = (struct usb_serial_port *)ATEN2011_port->port;
2191 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2192 DPRINTK("%s", "Invalid port \n");
2196 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2197 DPRINTK("%s", "Invalid Serial \n");
2201 DPRINTK("%s", "Entering .......... \n");
2203 minor = ATEN2011_port->port->serial->minor;
2204 if (minor == SERIAL_TTY_NO_MINOR)
2206 number = ATEN2011_port->port->number - minor;
2208 dbg("%s - port = %d, baud = %d", __FUNCTION__,
2209 ATEN2011_port->port->number, baudRate);
2210 //reset clk_uart_sel in spregOffset
2211 if (baudRate > 115200) {
2212 #ifdef HW_flow_control
2213 //NOTE: need to see the pther register to modify
2214 //setting h/w flow control bit to 1;
2216 //Data = ATEN2011_port->shadowMCR ;
2218 ATEN2011_port->shadowMCR = Data;
2220 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2222 DPRINTK("Writing spreg failed in set_serial_baud\n");
2228 #ifdef HW_flow_control
2229 //setting h/w flow control bit to 0;
2231 //Data = ATEN2011_port->shadowMCR ;
2233 ATEN2011_port->shadowMCR = Data;
2235 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2237 DPRINTK("Writing spreg failed in set_serial_baud\n");
2244 if (1) //baudRate <= 115200)
2250 ATEN2011_calc_baud_rate_divisor(baudRate, &divisor,
2253 ATEN2011_get_reg_sync(port, ATEN2011_port->SpRegOffset,
2256 DPRINTK("reading spreg failed in set_serial_baud\n");
2259 Data = (Data & 0x8f) | clk_sel_val;
2262 ATEN2011_set_reg_sync(port, ATEN2011_port->SpRegOffset,
2265 DPRINTK("Writing spreg failed in set_serial_baud\n");
2268 /* Calculate the Divisor */
2271 err("%s - bad baud rate", __FUNCTION__);
2272 DPRINTK("%s\n", "bad baud rate");
2275 /* Enable access to divisor latch */
2276 Data = ATEN2011_port->shadowLCR | SERIAL_LCR_DLAB;
2277 ATEN2011_port->shadowLCR = Data;
2278 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2280 /* Write the divisor */
2281 Data = (unsigned char)(divisor & 0xff);
2282 DPRINTK("set_serial_baud Value to write DLL is %x\n", Data);
2283 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_LSB, Data);
2285 Data = (unsigned char)((divisor & 0xff00) >> 8);
2286 DPRINTK("set_serial_baud Value to write DLM is %x\n", Data);
2287 ATEN2011_set_Uart_Reg(port, DIVISOR_LATCH_MSB, Data);
2289 /* Disable access to divisor latch */
2290 Data = ATEN2011_port->shadowLCR & ~SERIAL_LCR_DLAB;
2291 ATEN2011_port->shadowLCR = Data;
2292 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2299 static void ATEN2011_change_port_settings(struct tty_struct *tty,
2300 struct ATENINTL_port *ATEN2011_port,
2301 struct ktermios *old_termios)
2311 struct usb_serial_port *port;
2312 struct usb_serial *serial;
2314 if (ATEN2011_port == NULL)
2317 port = (struct usb_serial_port *)ATEN2011_port->port;
2319 if (ATEN2011_port_paranoia_check(port, __FUNCTION__)) {
2320 DPRINTK("%s", "Invalid port \n");
2324 if (ATEN2011_serial_paranoia_check(port->serial, __FUNCTION__)) {
2325 DPRINTK("%s", "Invalid Serial \n");
2329 serial = port->serial;
2331 dbg("%s - port %d", __FUNCTION__, ATEN2011_port->port->number);
2333 if (!ATEN2011_port->open) {
2334 dbg("%s - port not opened", __FUNCTION__);
2338 if ((!tty) || (!tty->termios)) {
2339 dbg("%s - no tty structures", __FUNCTION__);
2343 DPRINTK("%s", "Entering .......... \n");
2347 lParity = LCR_PAR_NONE;
2349 cflag = tty->termios->c_cflag;
2350 iflag = tty->termios->c_iflag;
2352 /* Change the number of bits */
2354 //COMMENT1: the below Line"if(cflag & CSIZE)" is added for the errors we get for serial loop data test i.e serial_loopback.pl -v
2357 switch (cflag & CSIZE) {
2375 /* Change the Parity bit */
2376 if (cflag & PARENB) {
2377 if (cflag & PARODD) {
2378 lParity = LCR_PAR_ODD;
2379 dbg("%s - parity = odd", __FUNCTION__);
2381 lParity = LCR_PAR_EVEN;
2382 dbg("%s - parity = even", __FUNCTION__);
2386 dbg("%s - parity = none", __FUNCTION__);
2389 if (cflag & CMSPAR) {
2390 lParity = lParity | 0x20;
2393 /* Change the Stop bit */
2394 if (cflag & CSTOPB) {
2396 dbg("%s - stop bits = 2", __FUNCTION__);
2399 dbg("%s - stop bits = 1", __FUNCTION__);
2402 /* Update the LCR with the correct value */
2403 ATEN2011_port->shadowLCR &=
2404 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2405 ATEN2011_port->shadowLCR |= (lData | lParity | lStop);
2408 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is %x\n",
2409 ATEN2011_port->shadowLCR);
2410 /* Disable Interrupts */
2412 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2415 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
2418 ATEN2011_set_Uart_Reg(port, FIFO_CONTROL_REGISTER, Data);
2420 /* Send the updated LCR value to the ATEN2011 */
2421 Data = ATEN2011_port->shadowLCR;
2423 ATEN2011_set_Uart_Reg(port, LINE_CONTROL_REGISTER, Data);
2426 ATEN2011_port->shadowMCR = Data;
2427 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2429 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2431 /* set up the MCR register and send it to the ATEN2011 */
2433 ATEN2011_port->shadowMCR = MCR_MASTER_IE;
2434 if (cflag & CBAUD) {
2435 ATEN2011_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2438 if (cflag & CRTSCTS) {
2439 ATEN2011_port->shadowMCR |= (MCR_XON_ANY);
2442 ATEN2011_port->shadowMCR &= ~(MCR_XON_ANY);
2445 Data = ATEN2011_port->shadowMCR;
2446 ATEN2011_set_Uart_Reg(port, MODEM_CONTROL_REGISTER, Data);
2448 /* Determine divisor based on baud rate */
2449 baud = tty_get_baud_rate(tty);
2452 /* pick a default, any default... */
2453 DPRINTK("%s\n", "Picked default baud...");
2457 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2458 status = ATEN2011_send_cmd_write_baud_rate(ATEN2011_port, baud);
2460 /* Enable Interrupts */
2462 ATEN2011_set_Uart_Reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2464 if (ATEN2011_port->read_urb->status != -EINPROGRESS) {
2465 ATEN2011_port->read_urb->dev = serial->dev;
2467 status = usb_submit_urb(ATEN2011_port->read_urb, GFP_ATOMIC);
2471 (" usb_submit_urb(read bulk) failed, status = %d",
2476 ("ATEN2011_change_port_settings ATEN2011_port->shadowLCR is End %x\n",
2477 ATEN2011_port->shadowLCR);
2482 static int ATEN2011_calc_num_ports(struct usb_serial *serial)
2487 int ATEN2011_2or4ports;
2488 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2489 ATEN_RDREQ, ATEN_RD_RTYPE, 0, GPIO_REGISTER,
2490 &Data, VENDOR_READ_LENGTH, ATEN_WDR_TIMEOUT);
2492 //printk("ATEN2011_calc_num_ports GPIO is %x\n",Data);
2494 /* ghostgum: here is where the problem appears to bet */
2495 /* Which of the following are needed? */
2496 /* Greg used the serial->type->num_ports=2 */
2497 /* But the code in the ATEN2011_open relies on serial->num_ports=2 */
2498 if ((Data & 0x01) == 0) {
2499 ATEN2011_2or4ports = 2;
2500 serial->type->num_ports = 2;
2501 serial->num_ports = 2;
2503 //else if(serial->interface->cur_altsetting->desc.bNumEndpoints == 9)
2505 ATEN2011_2or4ports = 4;
2506 serial->type->num_ports = 4;
2507 serial->num_ports = 4;
2511 return ATEN2011_2or4ports;
2514 static int ATEN2011_startup(struct usb_serial *serial)
2516 struct ATENINTL_serial *ATEN2011_serial;
2517 struct ATENINTL_port *ATEN2011_port;
2518 struct usb_device *dev;
2523 DPRINTK("%s \n", " ATEN2011_startup :entering..........");
2526 DPRINTK("%s\n", "Invalid Handler");
2532 DPRINTK("%s\n", "Entering...");
2534 /* create our private serial structure */
2535 ATEN2011_serial = kzalloc(sizeof(struct ATENINTL_serial), GFP_KERNEL);
2536 if (ATEN2011_serial == NULL) {
2537 err("%s - Out of memory", __FUNCTION__);
2541 /* resetting the private structure field values to zero */
2542 memset(ATEN2011_serial, 0, sizeof(struct ATENINTL_serial));
2544 ATEN2011_serial->serial = serial;
2545 //initilize status polling flag to 0
2546 ATEN2011_serial->status_polling_started = 0;
2548 ATEN2011_set_serial_private(serial, ATEN2011_serial);
2549 ATEN2011_serial->ATEN2011_spectrum_2or4ports =
2550 ATEN2011_calc_num_ports(serial);
2551 /* we set up the pointers to the endpoints in the ATEN2011_open *
2552 * function, as the structures aren't created yet. */
2554 /* set up port private structures */
2555 for (i = 0; i < serial->num_ports; ++i) {
2557 kmalloc(sizeof(struct ATENINTL_port), GFP_KERNEL);
2558 if (ATEN2011_port == NULL) {
2559 err("%s - Out of memory", __FUNCTION__);
2560 ATEN2011_set_serial_private(serial, NULL);
2561 kfree(ATEN2011_serial);
2564 memset(ATEN2011_port, 0, sizeof(struct ATENINTL_port));
2566 /* Initialize all port interrupt end point to port 0 int endpoint *
2567 * Our device has only one interrupt end point comman to all port */
2569 // serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
2571 ATEN2011_port->port = serial->port[i];
2573 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
2575 minor = serial->port[i]->serial->minor;
2576 if (minor == SERIAL_TTY_NO_MINOR)
2578 ATEN2011_port->port_num =
2579 ((serial->port[i]->number - minor) + 1);
2581 if (ATEN2011_port->port_num == 1) {
2582 ATEN2011_port->SpRegOffset = 0x0;
2583 ATEN2011_port->ControlRegOffset = 0x1;
2584 ATEN2011_port->DcrRegOffset = 0x4;
2585 } else if ((ATEN2011_port->port_num == 2)
2586 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2588 ATEN2011_port->SpRegOffset = 0x8;
2589 ATEN2011_port->ControlRegOffset = 0x9;
2590 ATEN2011_port->DcrRegOffset = 0x16;
2591 } else if ((ATEN2011_port->port_num == 2)
2592 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2594 ATEN2011_port->SpRegOffset = 0xa;
2595 ATEN2011_port->ControlRegOffset = 0xb;
2596 ATEN2011_port->DcrRegOffset = 0x19;
2597 } else if ((ATEN2011_port->port_num == 3)
2598 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2600 ATEN2011_port->SpRegOffset = 0xa;
2601 ATEN2011_port->ControlRegOffset = 0xb;
2602 ATEN2011_port->DcrRegOffset = 0x19;
2603 } else if ((ATEN2011_port->port_num == 4)
2604 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports ==
2606 ATEN2011_port->SpRegOffset = 0xc;
2607 ATEN2011_port->ControlRegOffset = 0xd;
2608 ATEN2011_port->DcrRegOffset = 0x1c;
2610 ATEN2011_Dump_serial_port(ATEN2011_port);
2612 ATEN2011_set_port_private(serial->port[i], ATEN2011_port);
2614 //enable rx_disable bit in control register
2617 ATEN2011_get_reg_sync(serial->port[i],
2618 ATEN2011_port->ControlRegOffset,
2621 DPRINTK("Reading ControlReg failed status-0x%x\n",
2626 ("ControlReg Reading success val is %x, status%d\n",
2628 Data |= 0x08; //setting driver done bit
2629 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2631 //Data |= 0x20; //rx_disable bit
2634 ATEN2011_set_reg_sync(serial->port[i],
2635 ATEN2011_port->ControlRegOffset,
2639 ("Writing ControlReg failed(rx_disable) status-0x%x\n",
2644 ("ControlReg Writing success(rx_disable) status%d\n",
2647 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2651 ATEN2011_set_reg_sync(serial->port[i],
2652 (__u16) (ATEN2011_port->DcrRegOffset +
2655 DPRINTK("Writing DCR0 failed status-0x%x\n", status);
2658 DPRINTK("DCR0 Writing success status%d\n", status);
2663 ATEN2011_set_reg_sync(serial->port[i],
2664 (__u16) (ATEN2011_port->DcrRegOffset +
2667 DPRINTK("Writing DCR1 failed status-0x%x\n", status);
2670 DPRINTK("DCR1 Writing success status%d\n", status);
2675 ATEN2011_set_reg_sync(serial->port[i],
2676 (__u16) (ATEN2011_port->DcrRegOffset +
2679 DPRINTK("Writing DCR2 failed status-0x%x\n", status);
2682 DPRINTK("DCR2 Writing success status%d\n", status);
2684 // write values in clkstart0x0 and clkmulti 0x20
2688 ATEN2011_set_reg_sync(serial->port[i],
2689 CLK_START_VALUE_REGISTER, Data);
2692 ("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n",
2697 ("CLK_START_VALUE_REGISTER Writing success status%d\n",
2703 ATEN2011_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2707 ("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2711 DPRINTK("CLK_MULTI_REGISTER Writing success status%d\n",
2714 //Zero Length flag register
2715 if ((ATEN2011_port->port_num != 1)
2716 && (ATEN2011_serial->ATEN2011_spectrum_2or4ports == 2)) {
2720 status = ATEN2011_set_reg_sync(serial->port[i],
2726 DPRINTK("ZLIP offset%x\n",
2728 ((__u16) ATEN2011_port->port_num)));
2731 ("Writing ZLP_REG%d failed status-0x%x\n",
2735 DPRINTK("ZLP_REG%d Writing success status%d\n",
2740 status = ATEN2011_set_reg_sync(serial->port[i],
2746 DPRINTK("ZLIP offset%x\n",
2748 ((__u16) ATEN2011_port->port_num) -
2752 ("Writing ZLP_REG%d failed status-0x%x\n",
2756 DPRINTK("ZLP_REG%d Writing success status%d\n",
2760 ATEN2011_port->control_urb = usb_alloc_urb(0, GFP_ATOMIC);
2761 ATEN2011_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2765 //Zero Length flag enable
2768 status = ATEN2011_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2770 DPRINTK("Writing ZLP_REG5 failed status-0x%x\n", status);
2773 DPRINTK("ZLP_REG5 Writing success status%d\n", status);
2775 /* setting configuration feature to one */
2776 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2777 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2781 static void ATEN2011_shutdown(struct usb_serial *serial)
2784 struct ATENINTL_port *ATEN2011_port;
2785 DPRINTK("%s \n", " shutdown :entering..........");
2792 DPRINTK("%s", "Invalid Handler \n");
2796 /* check for the ports to be closed,close the ports and disconnect */
2798 /* free private structure allocated for serial port *
2799 * stop reads and writes on all ports */
2801 for (i = 0; i < serial->num_ports; ++i) {
2802 ATEN2011_port = ATEN2011_get_port_private(serial->port[i]);
2803 kfree(ATEN2011_port->ctrl_buf);
2804 usb_kill_urb(ATEN2011_port->control_urb);
2805 kfree(ATEN2011_port);
2806 ATEN2011_set_port_private(serial->port[i], NULL);
2809 /* free private structure allocated for serial device */
2811 kfree(ATEN2011_get_serial_private(serial));
2812 ATEN2011_set_serial_private(serial, NULL);
2814 DPRINTK("%s\n", "Thank u :: ");
2818 /* Inline functions to check the sanity of a pointer that is passed to us */
2819 static int ATEN2011_serial_paranoia_check(struct usb_serial *serial,
2820 const char *function)
2823 dbg("%s - serial == NULL", function);
2826 if (!serial->type) {
2827 dbg("%s - serial->type == NULL!", function);
2833 static int ATEN2011_port_paranoia_check(struct usb_serial_port *port,
2834 const char *function)
2837 dbg("%s - port == NULL", function);
2840 if (!port->serial) {
2841 dbg("%s - port->serial == NULL", function);
2847 static struct usb_serial *ATEN2011_get_usb_serial(struct usb_serial_port *port,
2848 const char *function)
2850 /* if no port was specified, or it fails a paranoia check */
2852 ATEN2011_port_paranoia_check(port, function) ||
2853 ATEN2011_serial_paranoia_check(port->serial, function)) {
2854 /* then say that we don't have a valid usb_serial thing, which will * end up genrating -ENODEV return values */
2858 return port->serial;
2861 static struct usb_serial_driver ATENINTL2011_4port_device = {
2863 .owner = THIS_MODULE,
2866 .description = DRIVER_DESC,
2867 .id_table = id_table,
2868 .open = ATEN2011_open,
2869 .close = ATEN2011_close,
2870 .write = ATEN2011_write,
2871 .write_room = ATEN2011_write_room,
2872 .chars_in_buffer = ATEN2011_chars_in_buffer,
2873 .throttle = ATEN2011_throttle,
2874 .unthrottle = ATEN2011_unthrottle,
2875 .calc_num_ports = ATEN2011_calc_num_ports,
2877 .ioctl = ATEN2011_ioctl,
2878 .set_termios = ATEN2011_set_termios,
2879 .break_ctl = ATEN2011_break,
2880 .tiocmget = ATEN2011_tiocmget,
2881 .tiocmset = ATEN2011_tiocmset,
2882 .attach = ATEN2011_startup,
2883 .shutdown = ATEN2011_shutdown,
2884 .read_bulk_callback = ATEN2011_bulk_in_callback,
2885 .read_int_callback = ATEN2011_interrupt_callback,
2888 static struct usb_driver aten_driver = {
2890 .probe = usb_serial_probe,
2891 .disconnect = usb_serial_disconnect,
2892 .id_table = id_table,
2895 static int __init ATENINTL2011_init(void)
2899 DPRINTK("%s \n", " ATEN2011_init :entering..........");
2901 /* Register with the usb serial */
2902 retval = usb_serial_register(&ATENINTL2011_4port_device);
2905 goto failed_port_device_register;
2907 /* info(DRIVER_DESC " " DRIVER_VERSION); */
2908 printk(KERN_INFO KBUILD_MODNAME ":"
2909 DRIVER_DESC " " DRIVER_VERSION "\n");
2911 /* Register with the usb */
2912 retval = usb_register(&aten_driver);
2915 goto failed_usb_register;
2918 DPRINTK("%s\n", "Leaving...");
2922 failed_usb_register:
2923 usb_serial_deregister(&ATENINTL2011_4port_device);
2925 failed_port_device_register:
2930 static void __exit ATENINTL2011_exit(void)
2933 DPRINTK("%s \n", " ATEN2011_exit :entering..........");
2935 usb_deregister(&aten_driver);
2937 usb_serial_deregister(&ATENINTL2011_4port_device);
2939 DPRINTK("%s\n", "End...");
2942 module_init(ATENINTL2011_init);
2943 module_exit(ATENINTL2011_exit);
2945 /* Module information */
2946 MODULE_DESCRIPTION(DRIVER_DESC);
2947 MODULE_LICENSE("GPL");
2949 MODULE_PARM_DESC(debug, "Debug enabled or not");