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>
21 /* Version Information */
22 #define DRIVER_VERSION "v2.14"
23 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
24 #define DRIVER_DESC "Quatech USB to Serial Driver"
26 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
27 #define QUATECH_SSU100 0xC020 /* SSU100 */
28 #define QUATECH_SSU200 0xC030 /* SSU200 */
29 #define QUATECH_DSU100 0xC040 /* DSU100 */
30 #define QUATECH_DSU200 0xC050 /* DSU200 */
31 #define QUATECH_QSU100 0xC060 /* QSU100 */
32 #define QUATECH_QSU200 0xC070 /* QSU200 */
33 #define QUATECH_ESU100A 0xC080 /* ESU100A */
34 #define QUATECH_ESU100B 0xC081 /* ESU100B */
35 #define QUATECH_ESU200A 0xC0A0 /* ESU200A */
36 #define QUATECH_ESU200B 0xC0A1 /* ESU200B */
37 #define QUATECH_HSU100A 0xC090 /* HSU100A */
38 #define QUATECH_HSU100B 0xC091 /* HSU100B */
39 #define QUATECH_HSU100C 0xC092 /* HSU100C */
40 #define QUATECH_HSU100D 0xC093 /* HSU100D */
41 #define QUATECH_HSU200A 0xC0B0 /* HSU200A */
42 #define QUATECH_HSU200B 0xC0B1 /* HSU200B */
43 #define QUATECH_HSU200C 0xC0B2 /* HSU200C */
44 #define QUATECH_HSU200D 0xC0B3 /* HSU200D */
45 #define QUATECH_SSU100_2 0xC120 /* SSU100_2 */
46 #define QUATECH_DSU100_2 0xC140 /* DSU100_2 */
47 #define QUATECH_DSU400_2 0xC150 /* DSU400_2 */
48 #define QUATECH_QSU100_2 0xC160 /* QSU100_2 */
49 #define QUATECH_QSU400_2 0xC170 /* QSU400_2 */
50 #define QUATECH_ESU400_2 0xC180 /* ESU400_2 */
51 #define QUATECH_ESU100_2 0xC1A0 /* ESU100_2 */
53 #define QT_SET_GET_DEVICE 0xc2
54 #define QT_OPEN_CLOSE_CHANNEL 0xca
55 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
56 #define QT_SET_ATF 0xcd
57 #define QT_GET_SET_REGISTER 0xc0
58 #define QT_GET_SET_UART 0xc1
59 #define QT_HW_FLOW_CONTROL_MASK 0xc5
60 #define QT_SW_FLOW_CONTROL_MASK 0xc6
61 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
62 #define QT_BREAK_CONTROL 0xc8
64 #define USBD_TRANSFER_DIRECTION_IN 0xc0
65 #define USBD_TRANSFER_DIRECTION_OUT 0x40
67 #define MAX_BAUD_RATE 460800
68 #define MAX_BAUD_REMAINDER 4608
70 #define DIV_LATCH_LS 0x00
71 #define XMT_HOLD_REGISTER 0x00
72 #define XVR_BUFFER_REGISTER 0x00
73 #define DIV_LATCH_MS 0x01
74 #define FIFO_CONTROL_REGISTER 0x02
75 #define LINE_CONTROL_REGISTER 0x03
76 #define MODEM_CONTROL_REGISTER 0x04
77 #define LINE_STATUS_REGISTER 0x05
78 #define MODEM_STATUS_REGISTER 0x06
80 #define SERIAL_MCR_DTR 0x01
81 #define SERIAL_MCR_RTS 0x02
82 #define SERIAL_MCR_LOOP 0x10
84 #define SERIAL_MSR_CTS 0x10
85 #define SERIAL_MSR_CD 0x80
86 #define SERIAL_MSR_RI 0x40
87 #define SERIAL_MSR_DSR 0x20
88 #define SERIAL_MSR_MASK 0xf0
90 #define SERIAL_8_DATA 0x03
91 #define SERIAL_7_DATA 0x02
92 #define SERIAL_6_DATA 0x01
93 #define SERIAL_5_DATA 0x00
95 #define SERIAL_ODD_PARITY 0X08
96 #define SERIAL_EVEN_PARITY 0X18
97 #define SERIAL_TWO_STOPB 0x04
98 #define SERIAL_ONE_STOPB 0x00
100 #define DEFAULT_DIVISOR 0x30 /* gives 9600 baud rate */
101 #define DEFAULT_LCR SERIAL_8_DATA /* 8, none , 1 */
103 #define FULLPWRBIT 0x00000080
104 #define NEXT_BOARD_POWER_BIT 0x00000004
106 #define SERIAL_LSR_OE 0x02
107 #define SERIAL_LSR_PE 0x04
108 #define SERIAL_LSR_FE 0x08
109 #define SERIAL_LSR_BI 0x10
111 #define SERIAL_MSR_CTS 0x10
112 #define SERIAL_MSR_CD 0x80
113 #define SERIAL_MSR_RI 0x40
114 #define SERIAL_MSR_DSR 0x20
115 #define SERIAL_MSR_MASK 0xf0
117 #define PREFUFF_LEVEL_CONSERVATIVE 128
118 #define ATC_DISABLED 0x0
120 #define RR_BITS 0x03 /* for clearing clock bits */
121 #define DUPMODE_BITS 0xc0
124 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
125 #define ALL_LOOPBACK 0x01
126 #define MODEM_CTRL 0x40
127 #define RS232_MODE 0x00
129 static struct usb_device_id serqt_id_table[] = {
130 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
131 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
132 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
133 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
134 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
135 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
136 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
137 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
138 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
139 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
140 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
141 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
142 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
143 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
144 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
145 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
146 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
147 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
148 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100_2)},
149 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100_2)},
150 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU400_2)},
151 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100_2)},
152 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU400_2)},
153 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU400_2)},
154 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100_2)},
155 {} /* Terminating entry */
158 MODULE_DEVICE_TABLE(usb, serqt_id_table);
160 struct qt_get_device_data {
166 struct qt_open_channel_data {
171 struct quatech_port {
172 int port_num; /* number of the port */
173 struct urb *write_urb; /* write URB for this port */
174 struct urb *read_urb; /* read URB for this port */
177 __u8 shadowLCR; /* last LCR value received */
178 __u8 shadowMCR; /* last MCR value received */
179 __u8 shadowMSR; /* last MSR value received */
180 __u8 shadowLSR; /* last LSR value received */
183 /* Used for TIOCMIWAIT */
184 wait_queue_head_t msr_wait;
185 char prev_status, diff_status;
187 wait_queue_head_t wait;
189 struct async_icount icount;
191 struct usb_serial_port *port; /* owner of this object */
192 struct qt_get_device_data DeviceData;
200 static struct usb_driver serqt_usb_driver = {
201 .name = "quatech-usb-serial",
202 .probe = usb_serial_probe,
203 .disconnect = usb_serial_disconnect,
204 .id_table = serqt_id_table,
208 static int port_paranoia_check(struct usb_serial_port *port,
209 const char *function)
212 dbg("%s - port == NULL", function);
216 dbg("%s - port->serial == NULL\n", function);
223 static int serial_paranoia_check(struct usb_serial *serial,
224 const char *function)
227 dbg("%s - serial == NULL\n", function);
232 dbg("%s - serial->type == NULL!", function);
239 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
242 return (struct quatech_port *)usb_get_serial_port_data(port);
245 static inline void qt_set_port_private(struct usb_serial_port *port,
246 struct quatech_port *data)
248 usb_set_serial_port_data(port, (void *)data);
251 static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
252 const char *function)
254 /* if no port was specified, or it fails a paranoia check */
256 port_paranoia_check(port, function) ||
257 serial_paranoia_check(port->serial, function)) {
259 * then say that we dont have a valid usb_serial thing,
260 * which will end up genrating -ENODEV return values
268 static void ProcessLineStatus(struct quatech_port *qt_port,
269 unsigned char line_status)
273 line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
278 static void ProcessModemStatus(struct quatech_port *qt_port,
279 unsigned char modem_status)
282 qt_port->shadowMSR = modem_status;
283 wake_up_interruptible(&qt_port->wait);
287 static void ProcessRxChar(struct usb_serial_port *port, unsigned char Data)
289 struct tty_struct *tty;
290 struct urb *urb = port->read_urb;
291 tty = tty_port_tty_get(&port->port);
293 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
295 if (tty && urb->actual_length) {
296 tty_buffer_request_room(tty, 1);
297 tty_insert_flip_string(tty, &Data, 1);
298 /* tty_flip_buffer_push(tty); */
304 static void qt_write_bulk_callback(struct urb *urb)
306 struct tty_struct *tty;
308 struct quatech_port *quatech_port;
310 status = urb->status;
313 dbg("nonzero write bulk status received:%d\n", status);
317 quatech_port = urb->context;
319 dbg("%s - port %d\n", __func__, quatech_port->port_num);
321 tty = tty_port_tty_get(&quatech_port->port->port);
328 static void qt_interrupt_callback(struct urb *urb)
333 static void qt_read_bulk_callback(struct urb *urb)
336 struct usb_serial_port *port = urb->context;
337 struct usb_serial *serial = get_usb_serial(port, __func__);
338 struct quatech_port *qt_port = qt_get_port_private(port);
340 struct tty_struct *tty;
342 unsigned int RxCount;
347 qt_port->ReadBulkStopped = 1;
348 dbg("%s - nonzero write bulk status received: %d\n",
349 __func__, urb->status);
353 tty = tty_port_tty_get(&port->port);
355 dbg("%s - bad tty pointer - exiting", __func__);
359 data = urb->transfer_buffer;
361 RxCount = urb->actual_length;
363 /* index = MINOR(port->tty->device) - serial->minor; */
364 index = tty->index - serial->minor;
366 dbg("%s - port %d\n", __func__, port->number);
367 dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
369 if (port_paranoia_check(port, __func__) != 0) {
370 dbg("%s - port_paranoia_check, exiting\n", __func__);
371 qt_port->ReadBulkStopped = 1;
376 dbg("%s - bad serial pointer, exiting\n", __func__);
379 if (qt_port->closePending == 1) {
380 /* Were closing , stop reading */
381 dbg("%s - (qt_port->closepending == 1\n", __func__);
382 qt_port->ReadBulkStopped = 1;
387 * RxHolding is asserted by throttle, if we assert it, we're not
388 * receiving any more characters and let the box handle the flow
391 if (qt_port->RxHolding == 1) {
392 qt_port->ReadBulkStopped = 1;
397 qt_port->ReadBulkStopped = 1;
399 dbg("%s - nonzero read bulk status received: %d\n",
400 __func__, urb->status);
404 if (tty && RxCount) {
406 for (i = 0; i < RxCount; ++i) {
407 /* Look ahead code here */
408 if ((i <= (RxCount - 3)) && (data[i] == 0x1b)
409 && (data[i + 1] == 0x1b)) {
411 switch (data[i + 2]) {
413 /* line status change 4th byte must follow */
414 if (i > (RxCount - 4)) {
415 dbg("Illegal escape seuences in received data\n");
418 ProcessLineStatus(qt_port, data[i + 3]);
424 /* Modem status status change 4th byte must follow */
425 dbg("Modem status status. \n");
426 if (i > (RxCount - 4)) {
427 dbg("Illegal escape sequences in received data\n");
430 ProcessModemStatus(qt_port,
436 dbg("No status sequence. \n");
438 ProcessRxChar(port, data[i]);
439 ProcessRxChar(port, data[i + 1]);
447 if (tty && urb->actual_length) {
448 tty_buffer_request_room(tty, 1);
449 tty_insert_flip_string(tty, (data + i), 1);
453 tty_flip_buffer_push(tty);
456 /* Continue trying to always read */
457 usb_fill_bulk_urb(port->read_urb, serial->dev,
458 usb_rcvbulkpipe(serial->dev,
459 port->bulk_in_endpointAddress),
460 port->read_urb->transfer_buffer,
461 port->read_urb->transfer_buffer_length,
462 qt_read_bulk_callback, port);
463 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
465 dbg("%s - failed resubmitting read urb, error %d",
468 if (tty && RxCount) {
469 tty_flip_buffer_push(tty);
470 tty_schedule_flip(tty);
474 schedule_work(&port->work);
479 * Issue a GET_DEVICE vendor-specific request on the default control pipe If
480 * successful, fills in the qt_get_device_data structure pointed to by
481 * device_data, otherwise return a negative error number of the problem.
484 static int qt_get_device(struct usb_serial *serial,
485 struct qt_get_device_data *device_data)
488 unsigned char *transfer_buffer;
491 kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL);
492 if (!transfer_buffer)
495 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
496 QT_SET_GET_DEVICE, 0xc0, 0, 0,
498 sizeof(struct qt_get_device_data), 300);
500 memcpy(device_data, transfer_buffer,
501 sizeof(struct qt_get_device_data));
502 kfree(transfer_buffer);
507 /****************************************************************************
508 * BoxSetPrebufferLevel
509 TELLS BOX WHEN TO ASSERT FLOW CONTROL
510 ****************************************************************************/
511 static int BoxSetPrebufferLevel(struct usb_serial *serial)
516 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
517 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
518 QT_GET_SET_PREBUF_TRIG_LVL, 0x40,
519 buffer_length, 0, NULL, 0, 300);
523 /****************************************************************************
525 TELLS BOX WHEN TO ASSERT automatic transmitter control
526 ****************************************************************************/
527 static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode)
532 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
535 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
536 QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300);
543 * Issue a SET_DEVICE vendor-specific request on the default control pipe If
544 * successful returns the number of bytes written, otherwise it returns a
545 * negative error number of the problem.
547 static int qt_set_device(struct usb_serial *serial,
548 struct qt_get_device_data *device_data)
554 PortSettings = ((__u16) (device_data->portb));
555 PortSettings = (PortSettings << 8);
556 PortSettings += ((__u16) (device_data->porta));
558 length = sizeof(struct qt_get_device_data);
559 dbg("%s - PortSettings = 0x%x\n", __func__, PortSettings);
561 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
562 QT_SET_GET_DEVICE, 0x40, PortSettings,
567 static int qt_open_channel(struct usb_serial *serial, __u16 Uart_Number,
568 struct qt_open_channel_data *pDeviceData)
572 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
573 QT_OPEN_CLOSE_CHANNEL,
574 USBD_TRANSFER_DIRECTION_IN, 1, Uart_Number,
576 sizeof(struct qt_open_channel_data), 300);
582 static int qt_close_channel(struct usb_serial *serial, __u16 Uart_Number)
586 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
587 QT_OPEN_CLOSE_CHANNEL,
588 USBD_TRANSFER_DIRECTION_OUT, 0, Uart_Number,
595 /****************************************************************************
597 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
598 * If successful, fills in the pValue with the register value asked for
599 ****************************************************************************/
600 static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number,
601 unsigned short Register_Num, __u8 *pValue)
604 __u16 current_length;
606 current_length = sizeof(struct qt_get_device_data);
609 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
610 QT_GET_SET_REGISTER, 0xC0, Register_Num,
611 Uart_Number, (void *)pValue, sizeof(*pValue), 300);
616 /****************************************************************************
618 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
619 * If successful, fills in the pValue with the register value asked for
620 ****************************************************************************/
621 static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number,
622 unsigned short Register_Num, unsigned short Value)
625 unsigned short RegAndByte;
628 RegAndByte = RegAndByte << 8;
629 RegAndByte = RegAndByte + Register_Num;
632 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
633 QT_GET_SET_REGISTER, 0xC0, Register_Num,
634 Uart_Number, NULL, 0, 300);
638 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
639 QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number,
647 * issuse a SET_UART vendor-spcific request on the default control pipe
648 * If successful sets baud rate divisor and LCR value
650 static int qt_setuart(struct usb_serial *serial, unsigned short Uart_Number,
651 unsigned short default_divisor, unsigned char default_LCR)
654 unsigned short UartNumandLCR;
656 UartNumandLCR = (default_LCR << 8) + Uart_Number;
659 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
660 QT_GET_SET_UART, 0x40, default_divisor,
661 UartNumandLCR, NULL, 0, 300);
666 static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index,
670 __u8 msr = 0, MOUT_Value = 0;
674 /* flow control, box will clear RTS line to prevent remote */
675 mcr = SERIAL_MCR_RTS;
676 } /* device from xmitting more chars */
678 /* no flow control to remote device */
682 MOUT_Value = mcr << 8;
685 /* flow control, box will inhibit xmit data if CTS line is
687 msr = SERIAL_MSR_CTS;
689 /* Box will not inhimbe xmit data due to CTS line */
695 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
696 QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value,
697 index, NULL, 0, 300);
702 static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index,
703 unsigned char stop_char, unsigned char start_char)
708 nSWflowout = start_char << 8;
709 nSWflowout = (unsigned short)stop_char;
712 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
713 QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout,
714 index, NULL, 0, 300);
719 static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index)
724 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
725 QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index,
731 static int qt_startup(struct usb_serial *serial)
733 struct usb_serial_port *port;
734 struct quatech_port *qt_port;
735 struct qt_get_device_data DeviceData;
739 dbg("enterting %s", __func__);
741 /* Now setup per port private data */
742 for (i = 0; i < serial->num_ports; i++) {
743 port = serial->port[i];
744 qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL);
746 dbg("%s: kmalloc for quatech_port (%d) failed!.",
750 spin_lock_init(&qt_port->lock);
752 usb_set_serial_port_data(port, qt_port);
756 status = qt_get_device(serial, &DeviceData);
758 dbg(__FILE__ "box_get_device failed");
762 dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
764 DeviceData.portb &= ~FULLPWRBIT;
765 dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
767 status = qt_set_device(serial, &DeviceData);
769 dbg(__FILE__ "qt_set_device failed\n");
773 status = qt_get_device(serial, &DeviceData);
775 dbg(__FILE__ "qt_get_device failed");
779 switch (serial->dev->descriptor.idProduct) {
783 case QUATECH_ESU100A:
784 case QUATECH_ESU100B:
785 case QUATECH_HSU100A:
786 case QUATECH_HSU100B:
787 case QUATECH_HSU100C:
788 case QUATECH_HSU100D:
789 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
790 DeviceData.porta |= CLKS_X4;
791 DeviceData.portb &= ~(LOOPMODE_BITS);
792 DeviceData.portb |= RS232_MODE;
798 case QUATECH_ESU200A:
799 case QUATECH_ESU200B:
800 case QUATECH_HSU200A:
801 case QUATECH_HSU200B:
802 case QUATECH_HSU200C:
803 case QUATECH_HSU200D:
804 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
805 DeviceData.porta |= CLKS_X4;
806 DeviceData.portb &= ~(LOOPMODE_BITS);
807 DeviceData.portb |= ALL_LOOPBACK;
810 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
811 DeviceData.porta |= CLKS_X4;
812 DeviceData.portb &= ~(LOOPMODE_BITS);
813 DeviceData.portb |= RS232_MODE;
818 status = BoxSetPrebufferLevel(serial); /* sets to default value */
820 dbg(__FILE__ "BoxSetPrebufferLevel failed\n");
824 status = BoxSetATC(serial, ATC_DISABLED);
826 dbg(__FILE__ "BoxSetATC failed\n");
830 dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
832 DeviceData.portb |= NEXT_BOARD_POWER_BIT;
833 dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
835 status = qt_set_device(serial, &DeviceData);
837 dbg(__FILE__ "qt_set_device failed\n");
841 dbg("Exit Success %s\n", __func__);
846 for (i = 0; i < serial->num_ports; i++) {
847 port = serial->port[i];
848 qt_port = qt_get_port_private(port);
850 usb_set_serial_port_data(port, NULL);
853 dbg("Exit fail %s\n", __func__);
858 static void qt_release(struct usb_serial *serial)
860 struct usb_serial_port *port;
861 struct quatech_port *qt_port;
864 dbg("enterting %s", __func__);
866 for (i = 0; i < serial->num_ports; i++) {
867 port = serial->port[i];
871 qt_port = usb_get_serial_port_data(port);
873 usb_set_serial_port_data(port, NULL);
878 int qt_open(struct tty_struct *tty,
879 struct usb_serial_port *port, struct file *filp)
881 struct usb_serial *serial;
882 struct quatech_port *quatech_port;
883 struct quatech_port *port0;
884 struct qt_open_channel_data ChannelData;
888 if (port_paranoia_check(port, __func__))
891 dbg("%s - port %d\n", __func__, port->number);
893 serial = port->serial;
895 if (serial_paranoia_check(serial, __func__))
898 quatech_port = qt_get_port_private(port);
899 port0 = qt_get_port_private(serial->port[0]);
901 if (quatech_port == NULL || port0 == NULL)
904 usb_clear_halt(serial->dev, port->write_urb->pipe);
905 usb_clear_halt(serial->dev, port->read_urb->pipe);
908 result = qt_get_device(serial, &port0->DeviceData);
910 /* Port specific setups */
911 result = qt_open_channel(serial, port->number, &ChannelData);
913 dbg(__FILE__ "qt_open_channel failed\n");
916 dbg(__FILE__ "qt_open_channel completed.\n");
918 /* FIXME: are these needed? Does it even do anything useful? */
919 quatech_port->shadowLSR = ChannelData.line_status &
920 (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
922 quatech_port->shadowMSR = ChannelData.modem_status &
923 (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
925 /* Set Baud rate to default and turn off (default)flow control here */
926 result = qt_setuart(serial, port->number, DEFAULT_DIVISOR, DEFAULT_LCR);
928 dbg(__FILE__ "qt_setuart failed\n");
931 dbg(__FILE__ "qt_setuart completed.\n");
934 * Put this here to make it responsive to stty and defauls set by
937 /* FIXME: is this needed? */
938 /* qt_set_termios(tty, port, NULL); */
940 /* Check to see if we've set up our endpoint info yet */
941 if (port0->open_ports == 1) {
942 if (serial->port[0]->interrupt_in_buffer == NULL) {
943 /* set up interrupt urb */
944 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
946 usb_rcvintpipe(serial->dev,
947 serial->port[0]->interrupt_in_endpointAddress),
948 serial->port[0]->interrupt_in_buffer,
950 interrupt_in_urb->transfer_buffer_length,
951 qt_interrupt_callback, serial,
953 interrupt_in_urb->interval);
956 usb_submit_urb(serial->port[0]->interrupt_in_urb,
960 "%s - Error %d submitting "
961 "interrupt urb\n", __func__, result);
968 dbg("port number is %d \n", port->number);
969 dbg("serial number is %d \n", port->serial->minor);
970 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
971 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
972 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
973 dbg("port's number in the device is %d\n", quatech_port->port_num);
974 quatech_port->read_urb = port->read_urb;
976 /* set up our bulk in urb */
978 usb_fill_bulk_urb(quatech_port->read_urb,
980 usb_rcvbulkpipe(serial->dev,
981 port->bulk_in_endpointAddress),
982 port->bulk_in_buffer,
983 quatech_port->read_urb->transfer_buffer_length,
984 qt_read_bulk_callback, quatech_port);
986 dbg("qt_open: bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
987 quatech_port->read_urb_busy = true;
988 result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
991 "%s - Error %d submitting control urb\n",
993 quatech_port->read_urb_busy = false;
996 /* initialize our wait queues */
997 init_waitqueue_head(&quatech_port->wait);
998 init_waitqueue_head(&quatech_port->msr_wait);
1000 /* initialize our icount structure */
1001 memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
1007 static int qt_chars_in_buffer(struct tty_struct *tty)
1009 struct usb_serial_port *port = tty->driver_data;
1010 struct usb_serial *serial;
1013 serial = get_usb_serial(port, __func__);
1015 dbg("%s - port %d\n", __func__, port->number);
1017 if (serial->num_bulk_out) {
1018 if (port->write_urb->status == -EINPROGRESS)
1019 chars = port->write_urb->transfer_buffer_length;
1022 dbg("%s - returns %d\n", __func__, chars);
1027 static void qt_block_until_empty(struct tty_struct *tty,
1028 struct quatech_port *qt_port)
1030 int timeout = HZ / 10;
1036 count = qt_chars_in_buffer(tty);
1041 interruptible_sleep_on_timeout(&qt_port->wait, timeout);
1045 dbg("%s - TIMEOUT", __func__);
1053 static void qt_close(struct tty_struct *tty, struct usb_serial_port *port,
1056 struct usb_serial *serial = port->serial;
1057 struct quatech_port *qt_port;
1058 struct quatech_port *port0;
1063 dbg("%s - port %d\n", __func__, port->number);
1064 index = tty->index - serial->minor;
1066 qt_port = qt_get_port_private(port);
1067 port0 = qt_get_port_private(serial->port[0]);
1069 /* shutdown any bulk reads that might be going on */
1070 if (serial->num_bulk_out)
1071 usb_unlink_urb(port->write_urb);
1072 if (serial->num_bulk_in)
1073 usb_unlink_urb(port->read_urb);
1075 /* wait up to for transmitter to empty */
1077 qt_block_until_empty(tty, qt_port);
1079 /* Close uart channel */
1080 status = qt_close_channel(serial, index);
1082 dbg("%s - port %d qt_close_channel failed.\n",
1083 __func__, port->number);
1085 port0->open_ports--;
1087 dbg("qt_num_open_ports in close%d:in port%d\n",
1088 port0->open_ports, port->number);
1090 if (port0->open_ports == 0) {
1091 if (serial->port[0]->interrupt_in_urb) {
1092 dbg("%s", "Shutdown interrupt_in_urb\n");
1093 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1098 if (qt_port->write_urb) {
1099 /* if this urb had a transfer buffer already (old tx) free it */
1100 if (qt_port->write_urb->transfer_buffer != NULL)
1101 kfree(qt_port->write_urb->transfer_buffer);
1102 usb_free_urb(qt_port->write_urb);
1107 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1108 const unsigned char *buf, int count)
1111 struct usb_serial *serial = get_usb_serial(port, __func__);
1116 dbg("%s - port %d\n", __func__, port->number);
1119 dbg("%s - write request of 0 bytes\n", __func__);
1123 /* only do something if we have a bulk out endpoint */
1124 if (serial->num_bulk_out) {
1125 if (port->write_urb->status == -EINPROGRESS) {
1126 dbg("%s - already writing\n", __func__);
1131 (count > port->bulk_out_size) ? port->bulk_out_size : count;
1132 memcpy(port->write_urb->transfer_buffer, buf, count);
1134 /* set up our urb */
1136 usb_fill_bulk_urb(port->write_urb, serial->dev,
1137 usb_sndbulkpipe(serial->dev,
1139 bulk_out_endpointAddress),
1140 port->write_urb->transfer_buffer, count,
1141 qt_write_bulk_callback, port);
1143 /* send the data out the bulk port */
1144 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1146 dbg("%s - failed submitting write urb, error %d\n",
1154 /* no bulk out, so return 0 bytes written */
1158 static int qt_write_room(struct tty_struct *tty)
1160 struct usb_serial_port *port = tty->driver_data;
1161 struct usb_serial *serial;
1162 struct quatech_port *qt_port;
1163 unsigned long flags;
1165 int retval = -EINVAL;
1167 if (port_paranoia_check(port, __func__)) {
1168 dbg("%s", "Invalid port\n");
1172 serial = get_usb_serial(port, __func__);
1177 qt_port = qt_get_port_private(port);
1179 spin_lock_irqsave(&qt_port->lock, flags);
1181 dbg("%s - port %d\n", __func__, port->number);
1183 if (serial->num_bulk_out) {
1184 if (port->write_urb->status != -EINPROGRESS)
1185 retval = port->bulk_out_size;
1188 spin_unlock_irqrestore(&qt_port->lock, flags);
1193 static int qt_ioctl(struct tty_struct *tty, struct file *file,
1194 unsigned int cmd, unsigned long arg)
1196 struct usb_serial_port *port = tty->driver_data;
1197 struct quatech_port *qt_port = qt_get_port_private(port);
1198 struct usb_serial *serial = get_usb_serial(port, __func__);
1201 dbg("%s cmd 0x%04x", __func__, cmd);
1203 index = tty->index - serial->minor;
1205 if (cmd == TIOCMIWAIT) {
1206 while (qt_port != NULL) {
1207 interruptible_sleep_on(&qt_port->msr_wait);
1208 if (signal_pending(current))
1209 return -ERESTARTSYS;
1211 char diff = qt_port->diff_status;
1214 return -EIO; /* no change => error */
1216 /* Consume all events */
1217 qt_port->diff_status = 0;
1219 if (((arg & TIOCM_RNG)
1220 && (diff & SERIAL_MSR_RI))
1221 || ((arg & TIOCM_DSR)
1222 && (diff & SERIAL_MSR_DSR))
1223 || ((arg & TIOCM_CD)
1224 && (diff & SERIAL_MSR_CD))
1225 || ((arg & TIOCM_CTS)
1226 && (diff & SERIAL_MSR_CTS))) {
1234 dbg("%s -No ioctl for that one. port = %d\n", __func__, port->number);
1235 return -ENOIOCTLCMD;
1238 static void qt_set_termios(struct tty_struct *tty,
1239 struct usb_serial_port *port,
1240 struct ktermios *old_termios)
1242 struct ktermios *termios = tty->termios;
1243 unsigned char new_LCR = 0;
1244 unsigned int cflag = termios->c_cflag;
1246 int baud, divisor, remainder;
1249 dbg("%s", __func__);
1251 index = tty->index - port->serial->minor;
1255 new_LCR |= SERIAL_5_DATA;
1258 new_LCR |= SERIAL_6_DATA;
1261 new_LCR |= SERIAL_7_DATA;
1265 new_LCR |= SERIAL_8_DATA;
1270 if (cflag & PARENB) {
1272 new_LCR |= SERIAL_ODD_PARITY;
1274 new_LCR |= SERIAL_EVEN_PARITY;
1277 new_LCR |= SERIAL_TWO_STOPB;
1279 new_LCR |= SERIAL_TWO_STOPB;
1281 dbg("%s - 4\n", __func__);
1283 /* Thats the LCR stuff, go ahead and set it */
1284 baud = tty_get_baud_rate(tty);
1286 /* pick a default, any default... */
1289 dbg("%s - got baud = %d\n", __func__, baud);
1291 divisor = MAX_BAUD_RATE / baud;
1292 remainder = MAX_BAUD_RATE % baud;
1293 /* Round to nearest divisor */
1294 if (((remainder * 2) >= baud) && (baud != 110))
1298 * Set Baud rate to default and turn off (default)flow control here
1301 qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR);
1303 dbg(__FILE__ "qt_setuart failed\n");
1307 /* Now determine flow control */
1308 if (cflag & CRTSCTS) {
1309 dbg("%s - Enabling HW flow control port %d\n", __func__,
1312 /* Enable RTS/CTS flow control */
1313 status = BoxSetHW_FlowCtrl(port->serial, index, 1);
1316 dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1320 /* Disable RTS/CTS flow control */
1321 dbg("%s - disabling HW flow control port %d\n", __func__,
1324 status = BoxSetHW_FlowCtrl(port->serial, index, 0);
1326 dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1332 /* if we are implementing XON/XOFF, set the start and stop character in
1334 if (I_IXOFF(tty) || I_IXON(tty)) {
1335 unsigned char stop_char = STOP_CHAR(tty);
1336 unsigned char start_char = START_CHAR(tty);
1338 BoxSetSW_FlowCtrl(port->serial, index, stop_char,
1341 dbg(__FILE__ "BoxSetSW_FlowCtrl (enabled) failed\n");
1344 /* disable SW flow control */
1345 status = BoxDisable_SW_FlowCtrl(port->serial, index);
1347 dbg(__FILE__ "BoxSetSW_FlowCtrl (diabling) failed\n");
1350 tty->termios->c_cflag &= ~CMSPAR;
1351 /* FIXME: Error cases should be returning the actual bits changed only */
1354 static void qt_break(struct tty_struct *tty, int break_state)
1356 struct usb_serial_port *port = tty->driver_data;
1357 struct usb_serial *serial = get_usb_serial(port, __func__);
1358 struct quatech_port *qt_port;
1360 unsigned int result;
1361 unsigned long flags;
1363 index = tty->index - serial->minor;
1365 qt_port = qt_get_port_private(port);
1367 if (break_state == -1)
1372 spin_lock_irqsave(&qt_port->lock, flags);
1374 dbg("%s - port %d\n", __func__, port->number);
1377 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1378 QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1380 spin_unlock_irqrestore(&qt_port->lock, flags);
1383 static inline int qt_real_tiocmget(struct tty_struct *tty,
1384 struct usb_serial_port *port,
1385 struct file *file, struct usb_serial *serial)
1390 unsigned int result = 0;
1394 dbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty);
1396 index = tty->index - serial->minor;
1398 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1401 BoxGetRegister(port->serial, index,
1402 MODEM_STATUS_REGISTER, &msr);
1407 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1409 | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1411 | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1413 | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1414 /* Carrier detect is set */
1415 | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1416 /* Ring indicator set */
1417 | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1425 static inline int qt_real_tiocmset(struct tty_struct *tty,
1426 struct usb_serial_port *port,
1428 struct usb_serial *serial,
1436 dbg("%s - port %d\n", __func__, port->number);
1438 index = tty->index - serial->minor;
1440 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1445 * Turn off the RTS and DTR and loopbcck and then only turn on what was
1448 mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1449 if (value & TIOCM_RTS)
1450 mcr |= SERIAL_MCR_RTS;
1451 if (value & TIOCM_DTR)
1452 mcr |= SERIAL_MCR_DTR;
1453 if (value & TIOCM_LOOP)
1454 mcr |= SERIAL_MCR_LOOP;
1457 BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1464 static int qt_tiocmget(struct tty_struct *tty, struct file *file)
1466 struct usb_serial_port *port = tty->driver_data;
1467 struct usb_serial *serial = get_usb_serial(port, __func__);
1468 struct quatech_port *qt_port = qt_get_port_private(port);
1469 int retval = -ENODEV;
1470 unsigned long flags;
1472 dbg("In %s \n", __func__);
1477 spin_lock_irqsave(&qt_port->lock, flags);
1479 dbg("%s - port %d\n", __func__, port->number);
1480 dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1482 retval = qt_real_tiocmget(tty, port, file, serial);
1484 spin_unlock_irqrestore(&qt_port->lock, flags);
1488 static int qt_tiocmset(struct tty_struct *tty, struct file *file,
1489 unsigned int set, unsigned int clear)
1492 struct usb_serial_port *port = tty->driver_data;
1493 struct usb_serial *serial = get_usb_serial(port, __func__);
1494 struct quatech_port *qt_port = qt_get_port_private(port);
1495 unsigned long flags;
1496 int retval = -ENODEV;
1498 dbg("In %s \n", __func__);
1503 spin_lock_irqsave(&qt_port->lock, flags);
1505 dbg("%s - port %d \n", __func__, port->number);
1506 dbg("%s - qt_port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1508 retval = qt_real_tiocmset(tty, port, file, serial, set);
1510 spin_unlock_irqrestore(&qt_port->lock, flags);
1514 static void qt_throttle(struct tty_struct *tty)
1516 struct usb_serial_port *port = tty->driver_data;
1517 struct usb_serial *serial = get_usb_serial(port, __func__);
1518 struct quatech_port *qt_port;
1519 unsigned long flags;
1521 dbg("%s - port %d\n", __func__, port->number);
1526 qt_port = qt_get_port_private(port);
1528 spin_lock_irqsave(&qt_port->lock, flags);
1530 /* pass on to the driver specific version of this function */
1531 qt_port->RxHolding = 1;
1532 dbg("%s - port->RxHolding = 1\n", __func__);
1534 spin_unlock_irqrestore(&qt_port->lock, flags);
1538 static void qt_unthrottle(struct tty_struct *tty)
1540 struct usb_serial_port *port = tty->driver_data;
1541 struct usb_serial *serial = get_usb_serial(port, __func__);
1542 struct quatech_port *qt_port;
1543 unsigned long flags;
1544 unsigned int result;
1549 qt_port = qt_get_port_private(port);
1551 spin_lock_irqsave(&qt_port->lock, flags);
1553 dbg("%s - port %d\n", __func__, port->number);
1555 if (qt_port->RxHolding == 1) {
1556 dbg("%s -qt_port->RxHolding == 1\n", __func__);
1558 qt_port->RxHolding = 0;
1559 dbg("%s - qt_port->RxHolding = 0\n", __func__);
1561 /* if we have a bulk endpoint, start it up */
1562 if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1)) {
1563 /* Start reading from the device */
1564 usb_fill_bulk_urb(port->read_urb, serial->dev,
1565 usb_rcvbulkpipe(serial->dev,
1566 port->bulk_in_endpointAddress),
1567 port->read_urb->transfer_buffer,
1569 transfer_buffer_length,
1570 qt_read_bulk_callback, port);
1571 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1573 err("%s - failed restarting read urb, error %d",
1577 spin_unlock_irqrestore(&qt_port->lock, flags);
1582 static int qt_calc_num_ports(struct usb_serial *serial)
1586 dbg("numberofendpoints: %d \n",
1587 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
1588 dbg("numberofendpoints: %d \n",
1589 (int)serial->interface->altsetting->desc.bNumEndpoints);
1592 (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1597 static struct usb_serial_driver quatech_device = {
1599 .owner = THIS_MODULE,
1602 .description = DRIVER_DESC,
1603 .usb_driver = &serqt_usb_driver,
1604 .id_table = serqt_id_table,
1609 .write_room = qt_write_room,
1610 .chars_in_buffer = qt_chars_in_buffer,
1611 .throttle = qt_throttle,
1612 .unthrottle = qt_unthrottle,
1613 .calc_num_ports = qt_calc_num_ports,
1615 .set_termios = qt_set_termios,
1616 .break_ctl = qt_break,
1617 .tiocmget = qt_tiocmget,
1618 .tiocmset = qt_tiocmset,
1619 .attach = qt_startup,
1620 .release = qt_release,
1623 static int __init serqt_usb_init(void)
1627 dbg("%s\n", __func__);
1629 /* register with usb-serial */
1630 retval = usb_serial_register(&quatech_device);
1633 goto failed_usb_serial_register;
1635 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1638 /* register with usb */
1640 retval = usb_register(&serqt_usb_driver);
1644 /* if we're here, usb_register() failed */
1645 usb_serial_deregister(&quatech_device);
1646 failed_usb_serial_register:
1650 static void __exit serqt_usb_exit(void)
1652 usb_deregister(&serqt_usb_driver);
1653 usb_serial_deregister(&quatech_device);
1656 module_init(serqt_usb_init);
1657 module_exit(serqt_usb_exit);
1659 MODULE_AUTHOR(DRIVER_AUTHOR);
1660 MODULE_DESCRIPTION(DRIVER_DESC);
1661 MODULE_LICENSE("GPL");
1663 module_param(debug, bool, S_IRUGO | S_IWUSR);
1664 MODULE_PARM_DESC(debug, "Debug enabled or not");