2 * This code was developed for the Quatech USB line for linux, it used
3 * much of the code developed by Greg Kroah-Hartman for USB serial devices
7 #include <linux/errno.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/tty.h>
11 #include <linux/tty_driver.h>
12 #include <linux/tty_flip.h>
13 #include <linux/module.h>
14 #include <linux/serial.h>
15 #include <linux/usb.h>
16 #include <linux/usb/serial.h>
17 #include <linux/uaccess.h>
19 /* Version Information */
20 #define DRIVER_VERSION "v2.14"
21 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
22 #define DRIVER_DESC "Quatech USB to Serial Driver"
24 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
25 #define QUATECH_SSU200 0xC030 /* SSU200 */
26 #define QUATECH_DSU100 0xC040 /* DSU100 */
27 #define QUATECH_DSU200 0xC050 /* DSU200 */
28 #define QUATECH_QSU100 0xC060 /* QSU100 */
29 #define QUATECH_QSU200 0xC070 /* QSU200 */
30 #define QUATECH_ESU100A 0xC080 /* ESU100A */
31 #define QUATECH_ESU100B 0xC081 /* ESU100B */
32 #define QUATECH_ESU200A 0xC0A0 /* ESU200A */
33 #define QUATECH_ESU200B 0xC0A1 /* ESU200B */
34 #define QUATECH_HSU100A 0xC090 /* HSU100A */
35 #define QUATECH_HSU100B 0xC091 /* HSU100B */
36 #define QUATECH_HSU100C 0xC092 /* HSU100C */
37 #define QUATECH_HSU100D 0xC093 /* HSU100D */
38 #define QUATECH_HSU200A 0xC0B0 /* HSU200A */
39 #define QUATECH_HSU200B 0xC0B1 /* HSU200B */
40 #define QUATECH_HSU200C 0xC0B2 /* HSU200C */
41 #define QUATECH_HSU200D 0xC0B3 /* HSU200D */
43 #define QT_SET_GET_DEVICE 0xc2
44 #define QT_OPEN_CLOSE_CHANNEL 0xca
45 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
46 #define QT_SET_ATF 0xcd
47 #define QT_GET_SET_REGISTER 0xc0
48 #define QT_GET_SET_UART 0xc1
49 #define QT_HW_FLOW_CONTROL_MASK 0xc5
50 #define QT_SW_FLOW_CONTROL_MASK 0xc6
51 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
52 #define QT_BREAK_CONTROL 0xc8
54 #define USBD_TRANSFER_DIRECTION_IN 0xc0
55 #define USBD_TRANSFER_DIRECTION_OUT 0x40
57 #define MAX_BAUD_RATE 460800
58 #define MAX_BAUD_REMAINDER 4608
60 #define DIV_LATCH_LS 0x00
61 #define XMT_HOLD_REGISTER 0x00
62 #define XVR_BUFFER_REGISTER 0x00
63 #define DIV_LATCH_MS 0x01
64 #define FIFO_CONTROL_REGISTER 0x02
65 #define LINE_CONTROL_REGISTER 0x03
66 #define MODEM_CONTROL_REGISTER 0x04
67 #define LINE_STATUS_REGISTER 0x05
68 #define MODEM_STATUS_REGISTER 0x06
70 #define SERIAL_MCR_DTR 0x01
71 #define SERIAL_MCR_RTS 0x02
72 #define SERIAL_MCR_LOOP 0x10
74 #define SERIAL_MSR_CTS 0x10
75 #define SERIAL_MSR_CD 0x80
76 #define SERIAL_MSR_RI 0x40
77 #define SERIAL_MSR_DSR 0x20
78 #define SERIAL_MSR_MASK 0xf0
80 #define SERIAL_8_DATA 0x03
81 #define SERIAL_7_DATA 0x02
82 #define SERIAL_6_DATA 0x01
83 #define SERIAL_5_DATA 0x00
85 #define SERIAL_ODD_PARITY 0X08
86 #define SERIAL_EVEN_PARITY 0X18
87 #define SERIAL_TWO_STOPB 0x04
88 #define SERIAL_ONE_STOPB 0x00
90 #define DEFAULT_DIVISOR 0x30 /* gives 9600 baud rate */
91 #define DEFAULT_LCR SERIAL_8_DATA /* 8, none , 1 */
93 #define FULLPWRBIT 0x00000080
94 #define NEXT_BOARD_POWER_BIT 0x00000004
96 #define SERIAL_LSR_OE 0x02
97 #define SERIAL_LSR_PE 0x04
98 #define SERIAL_LSR_FE 0x08
99 #define SERIAL_LSR_BI 0x10
101 #define SERIAL_MSR_CTS 0x10
102 #define SERIAL_MSR_CD 0x80
103 #define SERIAL_MSR_RI 0x40
104 #define SERIAL_MSR_DSR 0x20
105 #define SERIAL_MSR_MASK 0xf0
107 #define PREFUFF_LEVEL_CONSERVATIVE 128
108 #define ATC_DISABLED 0x0
110 #define RR_BITS 0x03 /* for clearing clock bits */
111 #define DUPMODE_BITS 0xc0
114 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
115 #define ALL_LOOPBACK 0x01
116 #define MODEM_CTRL 0x40
117 #define RS232_MODE 0x00
119 static const struct usb_device_id id_table[] = {
120 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
121 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
127 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
128 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
129 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
130 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
131 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
132 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
133 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
134 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
135 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
136 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
137 {} /* Terminating entry */
139 MODULE_DEVICE_TABLE(usb, id_table);
141 struct qt_get_device_data {
147 struct qt_open_channel_data {
152 struct quatech_port {
153 int port_num; /* number of the port */
154 struct urb *write_urb; /* write URB for this port */
155 struct urb *read_urb; /* read URB for this port */
158 __u8 shadow_lcr; /* last LCR value received */
159 __u8 shadow_mcr; /* last MCR value received */
160 __u8 shadow_msr; /* last MSR value received */
161 __u8 shadow_lsr; /* last LSR value received */
164 /* Used for TIOCMIWAIT */
165 wait_queue_head_t msr_wait;
166 char prev_status, diff_status;
168 wait_queue_head_t wait;
170 struct async_icount icount;
172 struct usb_serial_port *port; /* owner of this object */
173 struct qt_get_device_data device_data;
177 int read_bulk_stopped;
181 static int port_paranoia_check(struct usb_serial_port *port,
182 const char *function)
185 pr_debug("%s - port == NULL", function);
189 pr_debug("%s - port->serial == NULL\n", function);
196 static int serial_paranoia_check(struct usb_serial *serial,
197 const char *function)
200 pr_debug("%s - serial == NULL\n", function);
205 pr_debug("%s - serial->type == NULL!", function);
212 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
215 return (struct quatech_port *)usb_get_serial_port_data(port);
218 static inline void qt_set_port_private(struct usb_serial_port *port,
219 struct quatech_port *data)
221 usb_set_serial_port_data(port, (void *)data);
224 static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
225 const char *function)
227 /* if no port was specified, or it fails a paranoia check */
229 port_paranoia_check(port, function) ||
230 serial_paranoia_check(port->serial, function)) {
232 * then say that we dont have a valid usb_serial thing,
233 * which will end up genrating -ENODEV return values
241 static void process_line_status(struct quatech_port *qt_port,
242 unsigned char line_status)
245 qt_port->shadow_lsr =
246 line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
250 static void process_modem_status(struct quatech_port *qt_port,
251 unsigned char modem_status)
254 qt_port->shadow_msr = modem_status;
255 wake_up_interruptible(&qt_port->wait);
258 static void process_rx_char(struct usb_serial_port *port, unsigned char data)
260 struct urb *urb = port->read_urb;
261 if (urb->actual_length)
262 tty_insert_flip_char(&port->port, data, TTY_NORMAL);
265 static void qt_write_bulk_callback(struct urb *urb)
268 struct quatech_port *quatech_port;
270 status = urb->status;
273 dev_dbg(&urb->dev->dev,
274 "nonzero write bulk status received:%d\n", status);
278 quatech_port = urb->context;
280 tty_port_tty_wakeup(&quatech_port->port->port);
283 static void qt_interrupt_callback(struct urb *urb)
288 static void qt_status_change_check(struct urb *urb,
289 struct quatech_port *qt_port,
290 struct usb_serial_port *port)
293 unsigned char *data = urb->transfer_buffer;
294 unsigned int rx_count = urb->actual_length;
296 for (i = 0; i < rx_count; ++i) {
297 /* Look ahead code here */
298 if ((i <= (rx_count - 3)) && (data[i] == 0x1b)
299 && (data[i + 1] == 0x1b)) {
301 switch (data[i + 2]) {
303 if (i > (rx_count - 4)) {
305 "Illegal escape seuences in received data\n");
309 process_line_status(qt_port, data[i + 3]);
316 if (i > (rx_count - 4)) {
318 "Illegal escape seuences in received data\n");
322 process_modem_status(qt_port, data[i + 3]);
329 dev_dbg(&port->dev, "No status sequence.\n");
331 process_rx_char(port, data[i]);
332 process_rx_char(port, data[i + 1]);
341 if (urb->actual_length)
342 tty_insert_flip_char(&port->port, data[i], TTY_NORMAL);
345 tty_flip_buffer_push(&port->port);
348 static void qt_read_bulk_callback(struct urb *urb)
351 struct usb_serial_port *port = urb->context;
352 struct usb_serial *serial = get_usb_serial(port, __func__);
353 struct quatech_port *qt_port = qt_get_port_private(port);
357 qt_port->read_bulk_stopped = 1;
358 dev_dbg(&urb->dev->dev,
359 "%s - nonzero write bulk status received: %d\n",
360 __func__, urb->status);
365 "%s - port->rx_holding = %d\n", __func__, qt_port->rx_holding);
367 if (port_paranoia_check(port, __func__) != 0) {
368 qt_port->read_bulk_stopped = 1;
375 if (qt_port->close_pending == 1) {
376 /* Were closing , stop reading */
378 "%s - (qt_port->close_pending == 1\n", __func__);
379 qt_port->read_bulk_stopped = 1;
384 * rx_holding is asserted by throttle, if we assert it, we're not
385 * receiving any more characters and let the box handle the flow
388 if (qt_port->rx_holding == 1) {
389 qt_port->read_bulk_stopped = 1;
394 qt_port->read_bulk_stopped = 1;
397 "%s - nonzero read bulk status received: %d\n",
398 __func__, urb->status);
402 if (urb->actual_length)
403 qt_status_change_check(urb, qt_port, port);
405 /* Continue trying to always read */
406 usb_fill_bulk_urb(port->read_urb, serial->dev,
407 usb_rcvbulkpipe(serial->dev,
408 port->bulk_in_endpointAddress),
409 port->read_urb->transfer_buffer,
410 port->read_urb->transfer_buffer_length,
411 qt_read_bulk_callback, port);
412 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
415 "%s - failed resubmitting read urb, error %d",
418 if (urb->actual_length) {
419 tty_flip_buffer_push(&port->port);
420 tty_schedule_flip(&port->port);
424 schedule_work(&port->work);
429 * Issue a GET_DEVICE vendor-specific request on the default control pipe If
430 * successful, fills in the qt_get_device_data structure pointed to by
431 * device_data, otherwise return a negative error number of the problem.
434 static int qt_get_device(struct usb_serial *serial,
435 struct qt_get_device_data *device_data)
438 unsigned char *transfer_buffer;
441 kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL);
442 if (!transfer_buffer)
445 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
446 QT_SET_GET_DEVICE, 0xc0, 0, 0,
448 sizeof(struct qt_get_device_data), 300);
450 memcpy(device_data, transfer_buffer,
451 sizeof(struct qt_get_device_data));
452 kfree(transfer_buffer);
457 /****************************************************************************
458 * box_set_prebuffer_level
459 TELLS BOX WHEN TO ASSERT FLOW CONTROL
460 ****************************************************************************/
461 static int box_set_prebuffer_level(struct usb_serial *serial)
466 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
467 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
468 QT_GET_SET_PREBUF_TRIG_LVL, 0x40,
469 buffer_length, 0, NULL, 0, 300);
473 /****************************************************************************
475 TELLS BOX WHEN TO ASSERT automatic transmitter control
476 ****************************************************************************/
477 static int box_set_atc(struct usb_serial *serial, __u16 n_mode)
482 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
485 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
486 QT_SET_ATF, 0x40, n_mode, 0, NULL, 0, 300);
493 * Issue a SET_DEVICE vendor-specific request on the default control pipe If
494 * successful returns the number of bytes written, otherwise it returns a
495 * negative error number of the problem.
497 static int qt_set_device(struct usb_serial *serial,
498 struct qt_get_device_data *device_data)
504 port_settings = ((__u16) (device_data->portb));
505 port_settings = (port_settings << 8);
506 port_settings += ((__u16) (device_data->porta));
508 length = sizeof(struct qt_get_device_data);
510 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
511 QT_SET_GET_DEVICE, 0x40, port_settings,
516 static int qt_open_channel(struct usb_serial *serial, __u16 uart_num,
517 struct qt_open_channel_data *pdevice_data)
521 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
522 QT_OPEN_CLOSE_CHANNEL,
523 USBD_TRANSFER_DIRECTION_IN, 1, uart_num,
525 sizeof(struct qt_open_channel_data), 300);
531 static int qt_close_channel(struct usb_serial *serial, __u16 uart_num)
535 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
536 QT_OPEN_CLOSE_CHANNEL,
537 USBD_TRANSFER_DIRECTION_OUT, 0, uart_num,
544 /****************************************************************************
546 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
547 * If successful, fills in the p_value with the register value asked for
548 ****************************************************************************/
549 static int box_get_register(struct usb_serial *serial, unsigned short uart_num,
550 unsigned short register_num, __u8 *p_value)
553 __u16 current_length;
555 current_length = sizeof(struct qt_get_device_data);
558 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
559 QT_GET_SET_REGISTER, 0xC0, register_num,
560 uart_num, (void *)p_value, sizeof(*p_value), 300);
565 /****************************************************************************
567 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
568 * If successful, fills in the p_value with the register value asked for
569 ****************************************************************************/
570 static int box_set_register(struct usb_serial *serial, unsigned short uart_num,
571 unsigned short register_num, unsigned short value)
574 unsigned short reg_and_byte;
576 reg_and_byte = value;
577 reg_and_byte = reg_and_byte << 8;
578 reg_and_byte = reg_and_byte + register_num;
581 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
582 QT_GET_SET_REGISTER, 0xC0, register_num,
583 uart_num, NULL, 0, 300);
587 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
588 QT_GET_SET_REGISTER, 0x40, reg_and_byte, uart_num,
596 * issues a SET_UART vendor-specific request on the default control pipe
597 * If successful sets baud rate divisor and LCR value
599 static int qt_setuart(struct usb_serial *serial, unsigned short uart_num,
600 unsigned short default_divisor, unsigned char default_lcr)
603 unsigned short uart_num_and_lcr;
605 uart_num_and_lcr = (default_lcr << 8) + uart_num;
608 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
609 QT_GET_SET_UART, 0x40, default_divisor,
610 uart_num_and_lcr, NULL, 0, 300);
615 static int box_set_hw_flow_ctrl(struct usb_serial *serial, unsigned int index,
619 __u8 msr = 0, mout_value = 0;
623 /* flow control, box will clear RTS line to prevent remote */
624 mcr = SERIAL_MCR_RTS;
625 } /* device from xmitting more chars */
627 /* no flow control to remote device */
631 mout_value = mcr << 8;
634 /* flow control, box will inhibit xmit data if CTS line is
636 msr = SERIAL_MSR_CTS;
638 /* Box will not inhimbe xmit data due to CTS line */
644 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
645 QT_HW_FLOW_CONTROL_MASK, 0x40, mout_value,
646 index, NULL, 0, 300);
651 static int box_set_sw_flow_ctrl(struct usb_serial *serial, __u16 index,
652 unsigned char stop_char, unsigned char start_char)
657 n_sw_flow_out = start_char << 8;
658 n_sw_flow_out = (unsigned short)stop_char;
661 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
662 QT_SW_FLOW_CONTROL_MASK, 0x40, n_sw_flow_out,
663 index, NULL, 0, 300);
668 static int box_disable_sw_flow_ctrl(struct usb_serial *serial, __u16 index)
673 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
674 QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index,
680 static int qt_startup(struct usb_serial *serial)
682 struct device *dev = &serial->dev->dev;
683 struct usb_serial_port *port;
684 struct quatech_port *qt_port;
685 struct qt_get_device_data device_data;
689 /* Now setup per port private data */
690 for (i = 0; i < serial->num_ports; i++) {
691 port = serial->port[i];
692 qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL);
694 for (--i; i >= 0; i--) {
695 port = serial->port[i];
696 kfree(usb_get_serial_port_data(port));
697 usb_set_serial_port_data(port, NULL);
701 mutex_init(&qt_port->lock);
703 usb_set_serial_port_data(port, qt_port);
707 status = qt_get_device(serial, &device_data);
711 dev_dbg(dev, "device_data.portb = 0x%x\n", device_data.portb);
713 device_data.portb &= ~FULLPWRBIT;
714 dev_dbg(dev, "Changing device_data.portb to 0x%x\n", device_data.portb);
716 status = qt_set_device(serial, &device_data);
718 dev_dbg(dev, "qt_set_device failed\n");
722 status = qt_get_device(serial, &device_data);
724 dev_dbg(dev, "qt_get_device failed\n");
728 switch (serial->dev->descriptor.idProduct) {
731 case QUATECH_ESU100A:
732 case QUATECH_ESU100B:
733 case QUATECH_HSU100A:
734 case QUATECH_HSU100B:
735 case QUATECH_HSU100C:
736 case QUATECH_HSU100D:
737 device_data.porta &= ~(RR_BITS | DUPMODE_BITS);
738 device_data.porta |= CLKS_X4;
739 device_data.portb &= ~(LOOPMODE_BITS);
740 device_data.portb |= RS232_MODE;
746 case QUATECH_ESU200A:
747 case QUATECH_ESU200B:
748 case QUATECH_HSU200A:
749 case QUATECH_HSU200B:
750 case QUATECH_HSU200C:
751 case QUATECH_HSU200D:
752 device_data.porta &= ~(RR_BITS | DUPMODE_BITS);
753 device_data.porta |= CLKS_X4;
754 device_data.portb &= ~(LOOPMODE_BITS);
755 device_data.portb |= ALL_LOOPBACK;
758 device_data.porta &= ~(RR_BITS | DUPMODE_BITS);
759 device_data.porta |= CLKS_X4;
760 device_data.portb &= ~(LOOPMODE_BITS);
761 device_data.portb |= RS232_MODE;
766 status = box_set_prebuffer_level(serial); /* sets to default value */
768 dev_dbg(dev, "box_set_prebuffer_level failed\n");
772 status = box_set_atc(serial, ATC_DISABLED);
774 dev_dbg(dev, "box_set_atc failed\n");
778 dev_dbg(dev, "device_data.portb = 0x%x\n", device_data.portb);
780 device_data.portb |= NEXT_BOARD_POWER_BIT;
781 dev_dbg(dev, "Changing device_data.portb to 0x%x\n", device_data.portb);
783 status = qt_set_device(serial, &device_data);
785 dev_dbg(dev, "qt_set_device failed\n");
792 for (i = 0; i < serial->num_ports; i++) {
793 port = serial->port[i];
794 qt_port = qt_get_port_private(port);
796 usb_set_serial_port_data(port, NULL);
802 static void qt_release(struct usb_serial *serial)
804 struct usb_serial_port *port;
805 struct quatech_port *qt_port;
808 for (i = 0; i < serial->num_ports; i++) {
809 port = serial->port[i];
813 qt_port = usb_get_serial_port_data(port);
815 usb_set_serial_port_data(port, NULL);
820 static void qt_submit_urb_from_open(struct usb_serial *serial,
821 struct usb_serial_port *port)
824 struct usb_serial_port *port0 = serial->port[0];
826 /* set up interrupt urb */
827 usb_fill_int_urb(port0->interrupt_in_urb,
829 usb_rcvintpipe(serial->dev,
830 port0->interrupt_in_endpointAddress),
831 port0->interrupt_in_buffer,
832 port0->interrupt_in_urb->transfer_buffer_length,
833 qt_interrupt_callback, serial,
834 port0->interrupt_in_urb->interval);
836 result = usb_submit_urb(port0->interrupt_in_urb,
840 "%s - Error %d submitting interrupt urb\n",
845 static int qt_open(struct tty_struct *tty,
846 struct usb_serial_port *port)
848 struct usb_serial *serial;
849 struct quatech_port *quatech_port;
850 struct quatech_port *port0;
851 struct qt_open_channel_data channel_data;
855 if (port_paranoia_check(port, __func__))
858 serial = port->serial;
860 if (serial_paranoia_check(serial, __func__))
863 quatech_port = qt_get_port_private(port);
864 port0 = qt_get_port_private(serial->port[0]);
866 if (quatech_port == NULL || port0 == NULL)
869 usb_clear_halt(serial->dev, port->write_urb->pipe);
870 usb_clear_halt(serial->dev, port->read_urb->pipe);
873 result = qt_get_device(serial, &port0->device_data);
875 /* Port specific setups */
876 result = qt_open_channel(serial, port->port_number, &channel_data);
878 dev_dbg(&port->dev, "qt_open_channel failed\n");
881 dev_dbg(&port->dev, "qt_open_channel completed.\n");
883 /* FIXME: are these needed? Does it even do anything useful? */
884 quatech_port->shadow_lsr = channel_data.line_status &
885 (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
887 quatech_port->shadow_msr = channel_data.modem_status &
888 (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
890 /* Set Baud rate to default and turn off (default)flow control here */
891 result = qt_setuart(serial, port->port_number, DEFAULT_DIVISOR, DEFAULT_LCR);
893 dev_dbg(&port->dev, "qt_setuart failed\n");
896 dev_dbg(&port->dev, "qt_setuart completed.\n");
899 * Put this here to make it responsive to stty and defaults set by
903 /* Check to see if we've set up our endpoint info yet */
904 if (port0->open_ports == 1) {
905 if (serial->port[0]->interrupt_in_buffer == NULL)
906 qt_submit_urb_from_open(serial, port);
909 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
911 "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
913 "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
914 dev_dbg(&port->dev, "Interrupt endpoint is %d\n",
915 port->interrupt_in_endpointAddress);
916 dev_dbg(&port->dev, "port's number in the device is %d\n",
917 quatech_port->port_num);
918 quatech_port->read_urb = port->read_urb;
920 /* set up our bulk in urb */
922 usb_fill_bulk_urb(quatech_port->read_urb,
924 usb_rcvbulkpipe(serial->dev,
925 port->bulk_in_endpointAddress),
926 port->bulk_in_buffer,
927 quatech_port->read_urb->transfer_buffer_length,
928 qt_read_bulk_callback, quatech_port);
930 dev_dbg(&port->dev, "qt_open: bulkin endpoint is %d\n",
931 port->bulk_in_endpointAddress);
932 quatech_port->read_urb_busy = true;
933 result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
936 "%s - Error %d submitting control urb\n",
938 quatech_port->read_urb_busy = false;
941 /* initialize our wait queues */
942 init_waitqueue_head(&quatech_port->wait);
943 init_waitqueue_head(&quatech_port->msr_wait);
945 /* initialize our icount structure */
946 memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
952 static int qt_chars_in_buffer(struct tty_struct *tty)
954 struct usb_serial_port *port = tty->driver_data;
955 struct usb_serial *serial;
958 serial = get_usb_serial(port, __func__);
960 if (serial->num_bulk_out) {
961 if (port->write_urb->status == -EINPROGRESS)
962 chars = port->write_urb->transfer_buffer_length;
968 static void qt_block_until_empty(struct tty_struct *tty,
969 struct quatech_port *qt_port)
971 int timeout = HZ / 10;
977 count = qt_chars_in_buffer(tty);
982 interruptible_sleep_on_timeout(&qt_port->wait, timeout);
986 dev_dbg(&qt_port->port->dev, "%s - TIMEOUT", __func__);
994 static void qt_close(struct usb_serial_port *port)
996 struct usb_serial *serial = port->serial;
997 struct quatech_port *qt_port;
998 struct quatech_port *port0;
999 struct tty_struct *tty;
1004 tty = tty_port_tty_get(&port->port);
1005 index = port->port_number;
1007 qt_port = qt_get_port_private(port);
1008 port0 = qt_get_port_private(serial->port[0]);
1010 /* shutdown any bulk reads that might be going on */
1011 if (serial->num_bulk_out)
1012 usb_unlink_urb(port->write_urb);
1013 if (serial->num_bulk_in)
1014 usb_unlink_urb(port->read_urb);
1016 /* wait up to for transmitter to empty */
1018 qt_block_until_empty(tty, qt_port);
1021 /* Close uart channel */
1022 status = qt_close_channel(serial, index);
1024 dev_dbg(&port->dev, "%s - qt_close_channel failed.\n", __func__);
1026 port0->open_ports--;
1028 dev_dbg(&port->dev, "qt_num_open_ports in close%d\n", port0->open_ports);
1030 if (port0->open_ports == 0) {
1031 if (serial->port[0]->interrupt_in_urb) {
1032 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1033 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1038 if (qt_port->write_urb) {
1039 /* if this urb had a transfer buffer already (old tx) free it */
1040 kfree(qt_port->write_urb->transfer_buffer);
1041 usb_free_urb(qt_port->write_urb);
1046 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1047 const unsigned char *buf, int count)
1050 struct usb_serial *serial = get_usb_serial(port, __func__);
1057 "%s - write request of 0 bytes\n", __func__);
1061 /* only do something if we have a bulk out endpoint */
1062 if (serial->num_bulk_out) {
1063 if (port->write_urb->status == -EINPROGRESS) {
1064 dev_dbg(&port->dev, "%s - already writing\n", __func__);
1069 (count > port->bulk_out_size) ? port->bulk_out_size : count;
1070 memcpy(port->write_urb->transfer_buffer, buf, count);
1072 /* set up our urb */
1074 usb_fill_bulk_urb(port->write_urb, serial->dev,
1075 usb_sndbulkpipe(serial->dev,
1077 bulk_out_endpointAddress),
1078 port->write_urb->transfer_buffer, count,
1079 qt_write_bulk_callback, port);
1081 /* send the data out the bulk port */
1082 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1085 "%s - failed submitting write urb, error %d\n",
1093 /* no bulk out, so return 0 bytes written */
1097 static int qt_write_room(struct tty_struct *tty)
1099 struct usb_serial_port *port = tty->driver_data;
1100 struct usb_serial *serial;
1101 struct quatech_port *qt_port;
1103 int retval = -EINVAL;
1105 if (port_paranoia_check(port, __func__))
1108 serial = get_usb_serial(port, __func__);
1113 qt_port = qt_get_port_private(port);
1115 mutex_lock(&qt_port->lock);
1117 if (serial->num_bulk_out) {
1118 if (port->write_urb->status != -EINPROGRESS)
1119 retval = port->bulk_out_size;
1122 mutex_unlock(&qt_port->lock);
1127 static int qt_ioctl(struct tty_struct *tty,
1128 unsigned int cmd, unsigned long arg)
1130 struct usb_serial_port *port = tty->driver_data;
1131 struct quatech_port *qt_port = qt_get_port_private(port);
1134 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
1136 index = port->port_number;
1138 if (cmd == TIOCMIWAIT) {
1139 while (qt_port != NULL) {
1140 interruptible_sleep_on(&qt_port->msr_wait);
1141 if (signal_pending(current))
1142 return -ERESTARTSYS;
1144 char diff = qt_port->diff_status;
1147 return -EIO; /* no change => error */
1149 /* Consume all events */
1150 qt_port->diff_status = 0;
1152 if (((arg & TIOCM_RNG)
1153 && (diff & SERIAL_MSR_RI))
1154 || ((arg & TIOCM_DSR)
1155 && (diff & SERIAL_MSR_DSR))
1156 || ((arg & TIOCM_CD)
1157 && (diff & SERIAL_MSR_CD))
1158 || ((arg & TIOCM_CTS)
1159 && (diff & SERIAL_MSR_CTS))) {
1167 dev_dbg(&port->dev, "%s -No ioctl for that one.\n", __func__);
1168 return -ENOIOCTLCMD;
1171 static void qt_set_termios(struct tty_struct *tty,
1172 struct usb_serial_port *port,
1173 struct ktermios *old_termios)
1175 struct ktermios *termios = &tty->termios;
1176 unsigned char new_lcr = 0;
1177 unsigned int cflag = termios->c_cflag;
1179 int baud, divisor, remainder;
1182 index = port->port_number;
1184 switch (cflag & CSIZE) {
1186 new_lcr |= SERIAL_5_DATA;
1189 new_lcr |= SERIAL_6_DATA;
1192 new_lcr |= SERIAL_7_DATA;
1195 termios->c_cflag &= ~CSIZE;
1196 termios->c_cflag |= CS8;
1198 new_lcr |= SERIAL_8_DATA;
1203 if (cflag & PARENB) {
1205 new_lcr |= SERIAL_ODD_PARITY;
1207 new_lcr |= SERIAL_EVEN_PARITY;
1210 new_lcr |= SERIAL_TWO_STOPB;
1212 new_lcr |= SERIAL_ONE_STOPB;
1214 dev_dbg(&port->dev, "%s - 4\n", __func__);
1216 /* Thats the LCR stuff, go ahead and set it */
1217 baud = tty_get_baud_rate(tty);
1219 /* pick a default, any default... */
1222 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
1224 divisor = MAX_BAUD_RATE / baud;
1225 remainder = MAX_BAUD_RATE % baud;
1226 /* Round to nearest divisor */
1227 if (((remainder * 2) >= baud) && (baud != 110))
1231 * Set Baud rate to default and turn off (default)flow control here
1234 qt_setuart(port->serial, index, (unsigned short)divisor, new_lcr);
1236 dev_dbg(&port->dev, "qt_setuart failed\n");
1240 /* Now determine flow control */
1241 if (cflag & CRTSCTS) {
1242 dev_dbg(&port->dev, "%s - Enabling HW flow control\n", __func__);
1244 /* Enable RTS/CTS flow control */
1245 status = box_set_hw_flow_ctrl(port->serial, index, 1);
1248 dev_dbg(&port->dev, "box_set_hw_flow_ctrl failed\n");
1252 /* Disable RTS/CTS flow control */
1254 "%s - disabling HW flow control\n", __func__);
1256 status = box_set_hw_flow_ctrl(port->serial, index, 0);
1258 dev_dbg(&port->dev, "box_set_hw_flow_ctrl failed\n");
1264 /* if we are implementing XON/XOFF, set the start and stop character in
1266 if (I_IXOFF(tty) || I_IXON(tty)) {
1267 unsigned char stop_char = STOP_CHAR(tty);
1268 unsigned char start_char = START_CHAR(tty);
1270 box_set_sw_flow_ctrl(port->serial, index, stop_char,
1274 "box_set_sw_flow_ctrl (enabled) failed\n");
1277 /* disable SW flow control */
1278 status = box_disable_sw_flow_ctrl(port->serial, index);
1281 "box_set_sw_flow_ctrl (diabling) failed\n");
1284 termios->c_cflag &= ~CMSPAR;
1286 Error cases should be returning the actual bits changed only
1290 static void qt_break(struct tty_struct *tty, int break_state)
1292 struct usb_serial_port *port = tty->driver_data;
1293 struct usb_serial *serial = get_usb_serial(port, __func__);
1294 struct quatech_port *qt_port;
1296 unsigned int result;
1298 index = port->port_number;
1300 qt_port = qt_get_port_private(port);
1302 if (break_state == -1)
1307 mutex_lock(&qt_port->lock);
1310 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1311 QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1313 mutex_unlock(&qt_port->lock);
1316 static inline int qt_real_tiocmget(struct tty_struct *tty,
1317 struct usb_serial_port *port,
1318 struct usb_serial *serial)
1323 unsigned int result = 0;
1327 index = port->port_number;
1329 box_get_register(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1332 box_get_register(port->serial, index,
1333 MODEM_STATUS_REGISTER, &msr);
1338 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1340 | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1342 | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1344 | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1345 /* Carrier detect is set */
1346 | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1347 /* Ring indicator set */
1348 | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1356 static inline int qt_real_tiocmset(struct tty_struct *tty,
1357 struct usb_serial_port *port,
1358 struct usb_serial *serial,
1366 index = port->port_number;
1368 box_get_register(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1373 * Turn off the RTS and DTR and loopback and then only turn on what was
1376 mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1377 if (value & TIOCM_RTS)
1378 mcr |= SERIAL_MCR_RTS;
1379 if (value & TIOCM_DTR)
1380 mcr |= SERIAL_MCR_DTR;
1381 if (value & TIOCM_LOOP)
1382 mcr |= SERIAL_MCR_LOOP;
1385 box_set_register(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1392 static int qt_tiocmget(struct tty_struct *tty)
1394 struct usb_serial_port *port = tty->driver_data;
1395 struct usb_serial *serial = get_usb_serial(port, __func__);
1396 struct quatech_port *qt_port = qt_get_port_private(port);
1402 mutex_lock(&qt_port->lock);
1403 retval = qt_real_tiocmget(tty, port, serial);
1404 mutex_unlock(&qt_port->lock);
1408 static int qt_tiocmset(struct tty_struct *tty,
1409 unsigned int set, unsigned int clear)
1412 struct usb_serial_port *port = tty->driver_data;
1413 struct usb_serial *serial = get_usb_serial(port, __func__);
1414 struct quatech_port *qt_port = qt_get_port_private(port);
1420 mutex_lock(&qt_port->lock);
1421 retval = qt_real_tiocmset(tty, port, serial, set);
1422 mutex_unlock(&qt_port->lock);
1426 static void qt_throttle(struct tty_struct *tty)
1428 struct usb_serial_port *port = tty->driver_data;
1429 struct usb_serial *serial = get_usb_serial(port, __func__);
1430 struct quatech_port *qt_port;
1435 qt_port = qt_get_port_private(port);
1437 mutex_lock(&qt_port->lock);
1439 /* pass on to the driver specific version of this function */
1440 qt_port->rx_holding = 1;
1442 mutex_unlock(&qt_port->lock);
1445 static void qt_submit_urb_from_unthrottle(struct usb_serial_port *port,
1446 struct usb_serial *serial)
1450 /* Start reading from the device */
1451 usb_fill_bulk_urb(port->read_urb, serial->dev,
1452 usb_rcvbulkpipe(serial->dev,
1453 port->bulk_in_endpointAddress),
1454 port->read_urb->transfer_buffer,
1455 port->read_urb->transfer_buffer_length,
1456 qt_read_bulk_callback, port);
1458 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1462 "%s - failed restarting read urb, error %d\n",
1466 static void qt_unthrottle(struct tty_struct *tty)
1468 struct usb_serial_port *port = tty->driver_data;
1469 struct usb_serial *serial = get_usb_serial(port, __func__);
1470 struct quatech_port *qt_port;
1475 qt_port = qt_get_port_private(port);
1477 mutex_lock(&qt_port->lock);
1479 if (qt_port->rx_holding == 1) {
1480 dev_dbg(&port->dev, "%s -qt_port->rx_holding == 1\n", __func__);
1482 qt_port->rx_holding = 0;
1483 dev_dbg(&port->dev, "%s - qt_port->rx_holding = 0\n", __func__);
1485 /* if we have a bulk endpoint, start it up */
1486 if ((serial->num_bulk_in) && (qt_port->read_bulk_stopped == 1))
1487 qt_submit_urb_from_unthrottle(port, serial);
1489 mutex_unlock(&qt_port->lock);
1492 static int qt_calc_num_ports(struct usb_serial *serial)
1497 (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1502 static struct usb_serial_driver quatech_device = {
1504 .owner = THIS_MODULE,
1507 .description = DRIVER_DESC,
1508 .id_table = id_table,
1513 .write_room = qt_write_room,
1514 .chars_in_buffer = qt_chars_in_buffer,
1515 .throttle = qt_throttle,
1516 .unthrottle = qt_unthrottle,
1517 .calc_num_ports = qt_calc_num_ports,
1519 .set_termios = qt_set_termios,
1520 .break_ctl = qt_break,
1521 .tiocmget = qt_tiocmget,
1522 .tiocmset = qt_tiocmset,
1523 .attach = qt_startup,
1524 .release = qt_release,
1527 static struct usb_serial_driver * const serial_drivers[] = {
1528 &quatech_device, NULL
1531 module_usb_serial_driver(serial_drivers, id_table);
1533 MODULE_AUTHOR(DRIVER_AUTHOR);
1534 MODULE_DESCRIPTION(DRIVER_DESC);
1535 MODULE_LICENSE("GPL");