16 #include <asm/unaligned.h>
17 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
23 #include <linux/module.h>
24 #include <linux/serial.h>
31 #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
33 #define QT_OPEN_CLOSE_CHANNEL 0xca
34 #define QT_SET_GET_DEVICE 0xc2
35 #define QT_SET_GET_REGISTER 0xc0
36 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
37 #define QT_SET_ATF 0xcd
38 #define QT_TRANSFER_IN 0xc0
39 #define QT_HW_FLOW_CONTROL_MASK 0xc5
40 #define QT_SW_FLOW_CONTROL_MASK 0xc6
41 #define QT2_BREAK_CONTROL 0xc8
42 #define QT2_GET_SET_UART 0xc1
43 #define QT2_FLUSH_DEVICE 0xc4
44 #define QT2_GET_SET_QMCR 0xe1
45 #define QT2_QMCR_RS232 0x40
46 #define QT2_QMCR_RS422 0x10
48 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
50 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
53 #define QT2_CONTROL_BYTE 0x1b
54 #define QT2_LINE_STATUS 0x00
55 #define QT2_MODEM_STATUS 0x01
56 #define QT2_XMIT_HOLD 0x02
57 #define QT2_CHANGE_PORT 0x03
58 #define QT2_REC_FLUSH 0x04
59 #define QT2_XMIT_FLUSH 0x05
60 #define QT2_CONTROL_ESCAPE 0xff
62 #define MAX_BAUD_RATE 921600
63 #define DEFAULT_BAUD_RATE 9600
65 #define QT2_WRITE_BUFFER_SIZE 512
66 #define QT2_WRITE_CONTROL_SIZE 5
69 #define DRIVER_VERSION "v0.1"
70 #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
72 #define USB_VENDOR_ID_QUATECH 0x061d
73 #define QUATECH_SSU2_100 0xC120
74 #define QUATECH_DSU2_100 0xC140
75 #define QUATECH_DSU2_400 0xC150
76 #define QUATECH_QSU2_100 0xC160
77 #define QUATECH_QSU2_400 0xC170
78 #define QUATECH_ESU2_100 0xC1A0
79 #define QUATECH_ESU2_400 0xC180
86 #define QT_DETAILS(prod, ports) \
87 .product_id = (prod), \
141 static void qt2_write_bulk_callback(
struct urb *
urb);
142 static void qt2_read_bulk_callback(
struct urb *
urb);
148 serial_priv = usb_get_serial_data(serial);
154 static inline int calc_baud_divisor(
int baudrate)
161 if (((rem * 2) >= baudrate) && (baudrate != 110))
167 static inline int qt2_set_port_config(
struct usb_device *
dev,
171 int divisor = calc_baud_divisor(baudrate);
179 static inline int qt2_control_msg(
struct usb_device *dev,
183 request, 0x40, data, index,
187 static inline int qt2_setdevice(
struct usb_device *dev,
u8 *data)
189 u16 x = ((
u16) (data[1] << 8) | (
u16) (data[0]));
195 static inline int qt2_getdevice(
struct usb_device *dev,
u8 *data)
202 static inline int qt2_getregister(
struct usb_device *dev,
213 static inline int qt2_setregister(
struct usb_device *dev,
224 unsigned int set,
unsigned int clear)
227 struct usb_device *dev = port->
serial->dev;
233 "update_mctrl - DTR|RTS not being set|cleared\n");
248 "update_mctrl - Error from MODEM_CTRL urb: %i\n",
258 for (i = 0;
d = qt2_device_details[
i],
d.product_id != 0; i++) {
265 "don't know the number of ports, assuming 1\n");
270 static void qt2_set_termios(
struct tty_struct *tty,
274 struct usb_device *dev = port->
serial->dev;
282 port_priv = usb_get_serial_port_data(port);
291 switch (cflag &
CSIZE) {
311 status = qt2_set_port_config(dev, port_priv->
device_port, baud,
314 dev_err(&port->
dev,
"%s - qt2_set_port_config failed: %i\n",
325 dev_err(&port->
dev,
"%s - set HW flow control failed: %i\n",
338 dev_err(&port->
dev,
"%s - set SW flow control failed: %i\n",
356 port_priv = usb_get_serial_port_data(port);
363 "%s failed to set RS232 mode for port %i error %i\n",
364 __func__, device_port, status);
374 usb_rcvctrlpipe(serial->
dev, 0),
380 dev_err(&port->
dev,
"%s - open port failed %i", __func__,
389 spin_unlock_irqrestore(&port_priv->
lock, flags);
394 status = qt2_set_port_config(serial->
dev, device_port,
398 "%s - initial setup failed for port %i (%i)\n",
399 __func__, port->
number, device_port);
407 qt2_set_termios(tty, port, &tty->termios);
421 port_priv = usb_get_serial_port_data(port);
428 spin_unlock_irqrestore(&port_priv->
urb_lock, flags);
431 if (port->
serial->disconnected) {
438 usb_rcvctrlpipe(serial->
dev, 0),
443 dev_err(&port->
dev,
"%s - transmit buffer flush failed: %i\n",
448 usb_rcvctrlpipe(serial->
dev, 0),
453 dev_err(&port->
dev,
"%s - receive buffer flush failed: %i\n",
458 usb_sndctrlpipe(serial->
dev, 0),
464 dev_err(&port->
dev,
"%s - close port failed %i\n",
470 static void qt2_disconnect(
struct usb_serial *serial)
491 tmp.baud_base = 9600;
492 tmp.close_delay = 5*
HZ;
493 tmp.closing_wait = 30*
HZ;
508 spin_unlock_irqrestore(&priv->
lock, flags);
522 spin_unlock_irqrestore(&priv->
lock, flags);
539 static int qt2_get_icount(
struct tty_struct *tty,
550 icount->
rx = cnow.
rx;
551 icount->
tx = cnow.
tx;
562 unsigned int cmd,
unsigned long arg)
568 return get_serial_info(port,
572 return wait_modem_info(port, arg);
581 static void qt2_process_status(
struct usb_serial_port *port,
unsigned char *ch)
585 qt2_update_lsr(port, ch + 1);
588 qt2_update_msr(port, ch + 1);
599 bytes_written = (
int)(*ch) + (
int)(*(ch + 1) << 4);
603 static void qt2_process_flush(
struct usb_serial_port *port,
unsigned char *ch)
618 unsigned char newport;
619 int len = urb->actual_length;
624 ch = urb->transfer_buffer;
626 serial = urb->context;
627 serial_priv = usb_get_serial_data(serial);
629 port_priv = usb_get_serial_port_data(port);
634 for (i = 0; i < urb->actual_length; i++) {
635 ch = (
unsigned char *)urb->transfer_buffer + i;
636 if ((i <= (len - 3)) &&
645 "%s - status message too short\n",
649 qt2_process_status(port, ch + 2);
656 "%s - xmit_empty message too short\n",
660 qt2_process_xmit_empty(port, ch + 3);
667 "%s - change_port message too short\n",
680 "%s - port change to invalid port: %i\n",
687 port_priv = usb_get_serial_port_data(port);
697 qt2_process_flush(port, ch + 2);
703 tty_insert_flip_string(tty, ch, 2);
709 "%s - unsupported command %i\n",
710 __func__, *(ch + 2));
719 tty_insert_flip_string(tty, ch, 1);
729 static void qt2_write_bulk_callback(
struct urb *
urb)
735 port_priv = usb_get_serial_port_data(port);
746 static void qt2_read_bulk_callback(
struct urb *
urb)
753 "%s - non-zero urb status: %i\n", __func__,
763 "%s - resubmit read urb failed: %i\n",
767 static int qt2_setup_urbs(
struct usb_serial *serial)
773 port0 = serial->
port[0];
775 serial_priv = usb_get_serial_data(serial);
778 dev_err(&serial->
dev->dev,
"No free urbs available\n");
782 usb_fill_bulk_urb(serial_priv->
read_urb, serial->
dev,
783 usb_rcvbulkpipe(serial->
dev,
787 qt2_read_bulk_callback, serial);
792 "%s - submit read urb failed %i\n", __func__, status);
800 static int qt2_attach(
struct usb_serial *serial)
807 0xc2, 0x40, 0x8000, 0,
NULL, 0,
811 "%s - failed to power on unit: %i\n", __func__, status);
815 serial_priv = kzalloc(
sizeof(*serial_priv),
GFP_KERNEL);
817 dev_err(&serial->
dev->dev,
"%s - Out of memory\n", __func__);
821 usb_set_serial_data(serial, serial_priv);
823 status = qt2_setup_urbs(serial);
840 port_priv = kzalloc(
sizeof(*port_priv),
GFP_KERNEL);
854 bEndpointAddress = serial->
port[0]->bulk_out_endpointAddress;
856 usb_sndbulkpipe(serial->
dev, bEndpointAddress),
859 qt2_write_bulk_callback, port);
861 usb_set_serial_port_data(port, port_priv);
870 port_priv = usb_get_serial_port_data(port);
877 static int qt2_tiocmget(
struct tty_struct *tty)
880 struct usb_device *dev = port->
serial->dev;
909 static int qt2_tiocmset(
struct tty_struct *tty,
910 unsigned int set,
unsigned int clear)
914 port_priv = usb_get_serial_port_data(tty->
driver_data);
915 return update_mctrl(port_priv,
set, clear);
918 static void qt2_break_ctl(
struct tty_struct *tty,
int break_state)
925 port_priv = usb_get_serial_port_data(port);
929 "%s - port is not open\n", __func__);
933 val = (break_state == -1) ? 1 : 0;
939 "%s - failed to send control message: %i\n", __func__,
947 struct usb_device *dev = port->
serial->dev;
951 if (!port->
serial->disconnected) {
953 if (!on && qt2_setregister(dev, port_priv->
device_port,
955 dev_warn(&port->
dev,
"error from flowcontrol urb\n");
958 update_mctrl(port_priv,
TIOCM_DTR | TIOCM_RTS, 0);
960 update_mctrl(port_priv, 0,
TIOCM_DTR | TIOCM_RTS);
965 static void qt2_update_msr(
struct usb_serial_port *port,
unsigned char *ch)
968 u8 newMSR = (
u8) *ch;
971 port_priv = usb_get_serial_port_data(port);
975 spin_unlock_irqrestore(&port_priv->
lock, flags);
995 static void qt2_update_lsr(
struct usb_serial_port *port,
unsigned char *ch)
1000 u8 newLSR = (
u8) *ch;
1002 port_priv = usb_get_serial_port_data(port);
1009 spin_unlock_irqrestore(&port_priv->
lock, flags);
1011 icount = &port_priv->
icount;
1015 if (newLSR & UART_LSR_BI)
1030 static int qt2_write_room(
struct tty_struct *tty)
1034 unsigned long flags = 0;
1037 port_priv = usb_get_serial_port_data(port);
1046 spin_unlock_irqrestore(&port_priv->
urb_lock, flags);
1053 const unsigned char *
buf,
int count)
1056 struct urb *write_urb;
1057 unsigned char *
data;
1058 unsigned long flags;
1062 port_priv = usb_get_serial_port_data(port);
1065 dev_err(&port->
dev,
"%s - no output urb\n", __func__);
1072 data = write_urb->transfer_buffer;
1075 dev_err(&port->
dev,
"qt2_write - urb is in use\n");
1082 put_unaligned_le16(count, data);
1084 memcpy(data, buf, count);
1095 spin_unlock_irqrestore(&port_priv->
urb_lock, flags);
1103 .name =
"quatech-serial",
1110 .write_room = qt2_write_room,
1111 .calc_num_ports = qt2_calc_num_ports,
1112 .attach = qt2_attach,
1113 .release = qt2_release,
1114 .disconnect = qt2_disconnect,
1115 .port_probe = qt2_port_probe,
1116 .port_remove = qt2_port_remove,
1117 .dtr_rts = qt2_dtr_rts,
1118 .break_ctl = qt2_break_ctl,
1119 .tiocmget = qt2_tiocmget,
1120 .tiocmset = qt2_tiocmset,
1121 .get_icount = qt2_get_icount,
1123 .set_termios = qt2_set_termios,