2 * Driver for Quatech Inc USB2.0 to serial adaptors. Largely unrelated to the
3 * serqt_usb driver, based on a re-write of the vendor supplied serqt_usb2 code,
4 * which is unrelated to the serqt_usb2 in the staging kernel
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.00"
23 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
24 #define DRIVER_DESC "Quatech USB 2.0 to Serial Driver"
26 /* vendor and device IDs */
27 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
28 #define QUATECH_SSU2_100 0xC120 /* RS232 single port */
29 #define QUATECH_DSU2_100 0xC140 /* RS232 dual port */
30 #define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */
31 #define QUATECH_QSU2_100 0xC160 /* RS232 four port */
32 #define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */
33 #define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */
34 #define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */
36 /* magic numbers go here, when we find out which ones are needed */
38 #define QU2BOXPWRON 0x8000 /* magic number to turn FPGA power on */
39 #define QU2BOX232 0x40 /* RS232 mode on MEI devices */
40 #define QU2BOXSPD9600 0x60 /* set speed to 9600 baud */
41 #define QT2_FIFO_DEPTH 1024 /* size of hardware fifos */
42 #define QT2_TX_HEADER_LENGTH 5
43 /* length of the header sent to the box with each write URB */
45 /* directions for USB transfers */
46 #define USBD_TRANSFER_DIRECTION_IN 0xc0
47 #define USBD_TRANSFER_DIRECTION_OUT 0x40
49 /* special Quatech command IDs. These are pushed down the
50 USB control pipe to get the box on the end to do things */
51 #define QT_SET_GET_DEVICE 0xc2
52 #define QT_OPEN_CLOSE_CHANNEL 0xca
53 /*#define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
54 #define QT_SET_ATF 0xcd*/
55 #define QT2_GET_SET_REGISTER 0xc0
56 #define QT2_GET_SET_UART 0xc1
57 #define QT2_HW_FLOW_CONTROL_MASK 0xc5
58 #define QT2_SW_FLOW_CONTROL_MASK 0xc6
59 #define QT2_SW_FLOW_CONTROL_DISABLE 0xc7
60 #define QT2_BREAK_CONTROL 0xc8
61 #define QT2_STOP_RECEIVE 0xe0
62 #define QT2_FLUSH_DEVICE 0xc4
63 #define QT2_GET_SET_QMCR 0xe1
65 /* sorts of flush we can do on */
66 #define QT2_FLUSH_RX 0x00
67 #define QT2_FLUSH_TX 0x01
69 /* port setting constants, used to set up serial port speeds, flow
70 * control and so on */
71 #define QT2_SERIAL_MCR_DTR 0x01
72 #define QT2_SERIAL_MCR_RTS 0x02
73 #define QT2_SERIAL_MCR_LOOP 0x10
75 #define QT2_SERIAL_MSR_CTS 0x10
76 #define QT2_SERIAL_MSR_CD 0x80
77 #define QT2_SERIAL_MSR_RI 0x40
78 #define QT2_SERIAL_MSR_DSR 0x20
79 #define QT2_SERIAL_MSR_MASK 0xf0
81 #define QT2_SERIAL_8_DATA 0x03
82 #define QT2_SERIAL_7_DATA 0x02
83 #define QT2_SERIAL_6_DATA 0x01
84 #define QT2_SERIAL_5_DATA 0x00
86 #define QT2_SERIAL_ODD_PARITY 0x08
87 #define QT2_SERIAL_EVEN_PARITY 0x18
88 #define QT2_SERIAL_TWO_STOPB 0x04
89 #define QT2_SERIAL_ONE_STOPB 0x00
91 #define QT2_MAX_BAUD_RATE 921600
92 #define QT2_MAX_BAUD_REMAINDER 4608
94 #define QT2_SERIAL_LSR_OE 0x02
95 #define QT2_SERIAL_LSR_PE 0x04
96 #define QT2_SERIAL_LSR_FE 0x08
97 #define QT2_SERIAL_LSR_BI 0x10
99 /* value of Line Status Register when UART has completed
100 * emptying data out on the line */
101 #define QT2_LSR_TEMT 0x40
103 /* register numbers on each UART, for use with qt2_box_[get|set]_register*/
104 #define QT2_XMT_HOLD_REGISTER 0x00
105 #define QT2_XVR_BUFFER_REGISTER 0x00
106 #define QT2_FIFO_CONTROL_REGISTER 0x02
107 #define QT2_LINE_CONTROL_REGISTER 0x03
108 #define QT2_MODEM_CONTROL_REGISTER 0x04
109 #define QT2_LINE_STATUS_REGISTER 0x05
110 #define QT2_MODEM_STATUS_REGISTER 0x06
112 /* handy macros for doing escape sequence parsing on data reads */
113 #define THISCHAR ((unsigned char *)(urb->transfer_buffer))[i]
114 #define NEXTCHAR ((unsigned char *)(urb->transfer_buffer))[i + 1]
115 #define THIRDCHAR ((unsigned char *)(urb->transfer_buffer))[i + 2]
116 #define FOURTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 3]
117 #define FIFTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 4]
119 static const struct usb_device_id quausb2_id_table[] = {
120 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
121 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
127 {} /* Terminating entry */
130 MODULE_DEVICE_TABLE(usb, quausb2_id_table);
132 /* custom structures we need go here */
133 static struct usb_driver quausb2_usb_driver = {
134 .name = "quatech-usb2-serial",
135 .probe = usb_serial_probe,
136 .disconnect = usb_serial_disconnect,
137 .id_table = quausb2_id_table,
142 * quatech2_port: Structure in which to keep all the messy stuff that this
143 * driver needs alongside the usb_serial_port structure
144 * @read_urb_busy: Flag indicating that port->read_urb is in use
145 * @close_pending: flag indicating that this port is in the process of
146 * being closed (and so no new reads / writes should be started).
147 * @shadowLSR: Last received state of the line status register, holds the
148 * value of the line status flags from the port
149 * @shadowMSR: Last received state of the modem status register, holds
150 * the value of the modem status received from the port
151 * @rcv_flush: Flag indicating that a receive flush has occured on
153 * @xmit_flush: Flag indicating that a transmit flush has been processed by
155 * @tx_pending_bytes: Number of bytes waiting to be sent. This total
156 * includes the size (excluding header) of URBs that have been submitted but
157 * have not yet been sent to to the device, and bytes that have been sent out
158 * of the port but not yet reported sent by the "xmit_empty" messages (which
159 * indicate the number of bytes sent each time they are recieved, despite the
161 * - Starts at zero when port is initialised.
162 * - is incremented by the size of the data to be written (no headers)
163 * each time a write urb is dispatched.
164 * - is decremented each time a "transmit empty" message is received
165 * by the driver in the data stream.
166 * @lock: Mutex to lock access to this structure when we need to ensure that
167 * races don't occur to access bits of it.
168 * @open_count: The number of uses of the port currently having
169 * it open, i.e. the reference count.
171 struct quatech2_port {
179 int tx_pending_bytes;
180 struct mutex modelock;
183 char active; /* someone has this device open */
184 unsigned char *xfer_to_tty_buffer;
185 wait_queue_head_t wait;
186 __u8 shadowLCR; /* last LCR value received */
187 __u8 shadowMCR; /* last MCR value received */
189 struct semaphore pend_xmit_sem; /* locks this structure */
194 * Structure to hold device-wide internal status information
195 * @param ReadBulkStopped The last bulk read attempt ended in tears
196 * @param open_ports The number of serial ports currently in use on the box
197 * @param current_port Pointer to the serial port structure of the port which
198 * the read stream is currently directed to. Escape sequences in the read
199 * stream will change this around as data arrives from different ports on the
201 * @buffer_size: The max size buffer each URB can take, used to set the size of
202 * the buffers allocated for writing to each port on the device (we need to
203 * store this because it is known only to the endpoint, but used each time a
204 * port is opened and a new buffer is allocated.
206 struct quatech2_dev {
207 bool ReadBulkStopped;
209 struct usb_serial_port *current_port;
213 /* structure which holds line and modem status flags */
214 struct qt2_status_data {
219 /* Function prototypes */
220 static int qt2_boxpoweron(struct usb_serial *serial);
221 static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
223 static int port_paranoia_check(struct usb_serial_port *port,
224 const char *function);
225 static int serial_paranoia_check(struct usb_serial *serial,
226 const char *function);
227 static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
229 static inline void qt2_set_port_private(struct usb_serial_port *port,
230 struct quatech2_port *data);
231 static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
233 static inline void qt2_set_dev_private(struct usb_serial *serial,
234 struct quatech2_dev *data);
235 static int qt2_openboxchannel(struct usb_serial *serial, __u16
236 Uart_Number, struct qt2_status_data *pDeviceData);
237 static int qt2_closeboxchannel(struct usb_serial *serial, __u16
239 static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number,
240 unsigned short divisor, unsigned char LCR);
241 static void qt2_read_bulk_callback(struct urb *urb);
242 static void qt2_write_bulk_callback(struct urb *urb);
243 static void qt2_process_line_status(struct usb_serial_port *port,
244 unsigned char LineStatus);
245 static void qt2_process_modem_status(struct usb_serial_port *port,
246 unsigned char ModemStatus);
247 static void qt2_process_xmit_empty(struct usb_serial_port *port,
248 unsigned char fourth_char, unsigned char fifth_char);
249 static void qt2_process_port_change(struct usb_serial_port *port,
250 unsigned char New_Current_Port);
251 static void qt2_process_rcv_flush(struct usb_serial_port *port);
252 static void qt2_process_xmit_flush(struct usb_serial_port *port);
253 static void qt2_process_rx_char(struct usb_serial_port *port,
255 static int qt2_box_get_register(struct usb_serial *serial,
256 unsigned char uart_number, unsigned short register_num,
258 static int qt2_box_set_register(struct usb_serial *serial,
259 unsigned short Uart_Number, unsigned short Register_Num,
260 unsigned short Value);
261 static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
262 unsigned short default_divisor, unsigned char default_LCR);
263 static int qt2_boxsethw_flowctl(struct usb_serial *serial,
264 unsigned int UartNumber, bool bSet);
265 static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
266 unsigned char stop_char, unsigned char start_char);
267 static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber);
268 static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
269 unsigned short stop);
271 /* implementation functions, roughly in order of use, are here */
272 static int qt2_calc_num_ports(struct usb_serial *serial)
276 switch (serial->dev->descriptor.idProduct) {
277 case QUATECH_SSU2_100:
281 case QUATECH_DSU2_400:
283 case QUATECH_DSU2_100:
287 case QUATECH_QSU2_400:
289 case QUATECH_QSU2_100:
293 case QUATECH_ESU2_400:
295 case QUATECH_ESU2_100:
305 static int qt2_attach(struct usb_serial *serial)
307 struct usb_serial_port *port;
308 struct quatech2_port *qt2_port; /* port-specific private data pointer */
309 struct quatech2_dev *qt2_dev; /* dev-specific private data pointer */
311 /* stuff for storing endpoint addresses now */
312 struct usb_endpoint_descriptor *endpoint;
313 struct usb_host_interface *iface_desc;
314 struct usb_serial_port *port0; /* first port structure on device */
316 /* check how many endpoints there are on the device, for
318 dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in",
319 __func__, serial->num_bulk_in,
320 serial->num_bulk_out, serial->num_interrupt_in);
321 if ((serial->num_bulk_in != 1) || (serial->num_bulk_out != 1)) {
322 dbg("Device has wrong number of bulk endpoints!");
325 iface_desc = serial->interface->cur_altsetting;
327 /* Set up per-device private data, storing extra data alongside
328 * struct usb_serial */
329 qt2_dev = kzalloc(sizeof(*qt2_dev), GFP_KERNEL);
331 dbg("%s: kmalloc for quatech2_dev failed!",
335 qt2_dev->open_ports = 0; /* no ports open */
336 qt2_set_dev_private(serial, qt2_dev); /* store private data */
338 /* Now setup per port private data, which replaces all the things
339 * that quatech added to standard kernel structures in their driver */
340 for (i = 0; i < serial->num_ports; i++) {
341 port = serial->port[i];
342 qt2_port = kzalloc(sizeof(*qt2_port), GFP_KERNEL);
344 dbg("%s: kmalloc for quatech2_port (%d) failed!.",
348 /* initialise stuff in the structure */
349 qt2_port->open_count = 0; /* port is not open */
350 spin_lock_init(&qt2_port->lock);
351 mutex_init(&qt2_port->modelock);
352 qt2_set_port_private(port, qt2_port);
355 /* gain access to port[0]'s structure because we want to store
356 * device-level stuff in it */
357 if (serial_paranoia_check(serial, __func__))
359 port0 = serial->port[0]; /* get the first port's device structure */
361 /* print endpoint addresses so we can check them later
363 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
364 endpoint = &iface_desc->endpoint[i].desc;
365 if ((endpoint->bEndpointAddress & 0x80) &&
366 ((endpoint->bmAttributes & 3) == 0x02)) {
367 /* we found a bulk in endpoint */
368 dbg("found bulk in at %#.2x",
369 endpoint->bEndpointAddress);
372 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
373 ((endpoint->bmAttributes & 3) == 0x02)) {
374 /* we found a bulk out endpoint */
375 dbg("found bulk out at %#.2x",
376 endpoint->bEndpointAddress);
377 qt2_dev->buffer_size = endpoint->wMaxPacketSize;
378 /* max size of URB needs recording for the device */
380 } /* end printing endpoint addresses */
382 /* switch on power to the hardware */
383 if (qt2_boxpoweron(serial) < 0) {
384 dbg("qt2_boxpoweron() failed");
387 /* set all ports to RS232 mode */
388 for (i = 0; i < serial->num_ports; ++i) {
389 if (qt2_boxsetQMCR(serial, i, QU2BOX232) < 0) {
390 dbg("qt2_boxsetQMCR() on port %d failed",
399 for (i = 0; i < serial->num_ports; i++) {
400 port = serial->port[i];
401 qt2_port = qt2_get_port_private(port);
403 qt2_set_port_private(port, NULL);
405 qt2_dev = qt2_get_dev_private(serial);
407 qt2_set_dev_private(serial, NULL);
409 dbg("Exit fail %s\n", __func__);
413 static void qt2_release(struct usb_serial *serial)
415 struct usb_serial_port *port;
416 struct quatech2_port *qt_port;
419 dbg("enterting %s", __func__);
421 for (i = 0; i < serial->num_ports; i++) {
422 port = serial->port[i];
426 qt_port = usb_get_serial_port_data(port);
428 usb_set_serial_port_data(port, NULL);
431 /* This function is called once per serial port on the device, when
432 * that port is opened by a userspace application.
433 * The tty_struct and the usb_serial_port belong to this port,
434 * i.e. there are multiple ones for a multi-port device.
435 * However the usb_serial_port structure has a back-pointer
436 * to the parent usb_serial structure which belongs to the device,
437 * so we can access either the device-wide information or
438 * any other port's information (because there are also forward
439 * pointers) via that pointer.
440 * This is most helpful if the device shares resources (e.g. end
441 * points) between different ports
443 int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
445 struct usb_serial *serial; /* device structure */
446 struct usb_serial_port *port0; /* first port structure on device */
447 struct quatech2_port *port_extra; /* extra data for this port */
448 struct quatech2_port *port0_extra; /* extra data for first port */
449 struct quatech2_dev *dev_extra; /* extra data for the device */
450 struct qt2_status_data ChannelData;
451 unsigned short default_divisor = QU2BOXSPD9600;
452 unsigned char default_LCR = QT2_SERIAL_8_DATA;
456 if (port_paranoia_check(port, __func__))
459 dbg("%s(): port %d", __func__, port->number);
461 serial = port->serial; /* get the parent device structure */
462 if (serial_paranoia_check(serial, __func__)) {
463 dbg("usb_serial struct failed sanity check");
466 dev_extra = qt2_get_dev_private(serial);
467 /* get the device private data */
468 if (dev_extra == NULL) {
469 dbg("device extra data pointer is null");
472 port0 = serial->port[0]; /* get the first port's device structure */
473 if (port_paranoia_check(port0, __func__)) {
474 dbg("port0 usb_serial_port struct failed sanity check");
478 port_extra = qt2_get_port_private(port);
479 port0_extra = qt2_get_port_private(port0);
480 if (port_extra == NULL || port0_extra == NULL) {
481 dbg("failed to get private data for port or port0");
485 /* FIXME: are these needed? Does it even do anything useful? */
486 /* get the modem and line status values from the UART */
487 status = qt2_openboxchannel(serial, port->number,
490 dbg("qt2_openboxchannel on channel %d failed",
494 port_extra->shadowLSR = ChannelData.line_status &
495 (QT2_SERIAL_LSR_OE | QT2_SERIAL_LSR_PE |
496 QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
497 port_extra->shadowMSR = ChannelData.modem_status &
498 (QT2_SERIAL_MSR_CTS | QT2_SERIAL_MSR_DSR |
499 QT2_SERIAL_MSR_RI | QT2_SERIAL_MSR_CD);
501 /* port_extra->fifo_empty_flag = true;*/
502 dbg("qt2_openboxchannel on channel %d completed.",
505 /* Set Baud rate to default and turn off flow control here */
506 status = qt2_conf_uart(serial, port->number, default_divisor,
509 dbg("qt2_conf_uart() failed on channel %d",
513 dbg("qt2_conf_uart() completed on channel %d",
517 * At this point we will need some end points to make further progress.
518 * Handlily, the correct endpoint addresses have been filled out into
519 * the usb_serial_port structure for us by the driver core, so we
520 * already have access to them.
521 * As there is only one bulk in and one bulk out end-point, these are in
522 * port[0]'s structure, and the rest are uninitialised. Handily,
523 * when we do a write to a port, we will use the same endpoint
524 * regardless of the port, with a 5-byte header added on to
525 * tell the box which port it should eventually come out of, so we only
526 * need the one set of endpoints. We will have one URB per port for
527 * writing, so that multiple ports can be writing at once.
528 * Finally we need a bulk in URB to use for background reads from the
529 * device, which will deal with uplink data from the box to host.
531 dbg("port0 bulk in endpoint is %#.2x", port0->bulk_in_endpointAddress);
532 dbg("port0 bulk out endpoint is %#.2x",
533 port0->bulk_out_endpointAddress);
535 /* set up write_urb for bulk out transfers on this port. The USB
536 * serial framework will have allocated a blank URB, buffer etc for
537 * port0 when it put the endpoints there, but not for any of the other
538 * ports on the device because there are no more endpoints. Thus we
539 * have to allocate our own URBs for ports 1-7
541 if (port->write_urb == NULL) {
542 dbg("port->write_urb == NULL, allocating one");
543 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
544 if (!port->write_urb) {
545 err("Allocating write URB failed");
548 /* buffer same size as port0 */
549 port->bulk_out_size = dev_extra->buffer_size;
550 port->bulk_out_buffer = kmalloc(port->bulk_out_size,
552 if (!port->bulk_out_buffer) {
553 err("Couldn't allocate bulk_out_buffer");
557 if (serial->dev == NULL)
558 dbg("serial->dev == NULL");
559 dbg("port->bulk_out_size is %d", port->bulk_out_size);
561 usb_fill_bulk_urb(port->write_urb, serial->dev,
562 usb_sndbulkpipe(serial->dev,
563 port0->bulk_out_endpointAddress),
564 port->bulk_out_buffer,
566 qt2_write_bulk_callback,
568 port_extra->tx_pending_bytes = 0;
570 if (dev_extra->open_ports == 0) {
571 /* this is first port to be opened, so need the read URB
572 * initialised for bulk in transfers (this is shared amongst
573 * all the ports on the device) */
574 usb_fill_bulk_urb(port0->read_urb, serial->dev,
575 usb_rcvbulkpipe(serial->dev,
576 port0->bulk_in_endpointAddress),
577 port0->bulk_in_buffer,
579 qt2_read_bulk_callback, serial);
580 dbg("port0 bulk in URB initialised");
582 /* submit URB, i.e. start reading from device (async) */
583 dev_extra->ReadBulkStopped = false;
584 port_extra->read_urb_busy = true;
585 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
588 "%s(): Error %d submitting bulk in urb",
590 port_extra->read_urb_busy = false;
591 dev_extra->ReadBulkStopped = true;
594 /* When the first port is opened, initialise the value of
595 * current_port in dev_extra to this port, so it is set
596 * to something. Once the box sends data it will send the
597 * relevant escape sequences to get it to the right port anyway
599 dev_extra->current_port = port;
602 /* initialize our wait queues */
603 init_waitqueue_head(&port_extra->wait);
604 /* increment the count of openings of this port by one */
605 port_extra->open_count++;
607 /* remember to store dev_extra, port_extra and port0_extra back again at
609 qt2_set_port_private(port, port_extra);
610 qt2_set_port_private(serial->port[0], port0_extra);
611 qt2_set_dev_private(serial, dev_extra);
613 dev_extra->open_ports++; /* one more port opened */
618 /* called when a port is closed by userspace. It won't be called, however,
619 * until calls to chars_in_buffer() reveal that the port has completed
620 * sending buffered data, and there is nothing else to do. Thus we don't have
621 * to rely on forcing data through in this function. */
622 /* Setting close_pending should keep new data from being written out,
623 * once all the data in the enpoint buffers is moved out we won't get
625 /* BoxStopReceive would keep any more data from coming from a given
626 * port, but isn't called by the vendor driver, although their comments
627 * mention it. Should it be used here to stop the inbound data
630 static void qt2_close(struct usb_serial_port *port)
632 /* time out value for flush loops */
634 struct quatech2_port *port_extra; /* extra data for this port */
635 struct usb_serial *serial; /* device structure */
636 struct quatech2_dev *dev_extra; /* extra data for the device */
637 __u8 lsr_value = 0; /* value of Line Status Register */
638 int status; /* result of last USB comms function */
640 dbg("%s(): port %d", __func__, port->number);
641 serial = port->serial; /* get the parent device structure */
642 dev_extra = qt2_get_dev_private(serial);
643 /* get the device private data */
644 port_extra = qt2_get_port_private(port); /* port private data */
646 /* we can now (and only now) stop reading data */
647 port_extra->close_pending = true;
648 dbg("%s(): port_extra->close_pending = true", __func__);
649 /* although the USB side is now empty, the UART itself may
650 * still be pushing characters out over the line, so we have to
651 * wait testing the actual line status until the lines change
652 * indicating that the data is done transfering. */
653 /* FIXME: slow this polling down so it doesn't run the USB bus flat out
654 * if it actually has to spend any time in this loop (which it normally
655 * doesn't because the buffer is nearly empty) */
656 jift = jiffies + (10 * HZ); /* 10 sec timeout */
658 status = qt2_box_get_register(serial, port->number,
659 QT2_LINE_STATUS_REGISTER, &lsr_value);
661 dbg("%s(): qt2_box_get_register failed", __func__);
664 if ((lsr_value & QT2_LSR_TEMT)) {
665 dbg("UART done sending");
669 } while (jiffies <= jift);
671 status = qt2_closeboxchannel(serial, port->number);
673 dbg("%s(): port %d qt2_box_open_close_channel failed",
674 __func__, port->number);
675 /* to avoid leaking URBs, we should now free the write_urb for this
676 * port and set the pointer to null so that next time the port is opened
677 * a new URB is allocated. This avoids leaking URBs when the device is
679 usb_free_urb(port->write_urb);
680 kfree(port->bulk_out_buffer);
681 port->bulk_out_buffer = NULL;
682 port->bulk_out_size = 0;
684 /* decrement the count of openings of this port by one */
685 port_extra->open_count--;
686 /* one less overall open as well */
687 dev_extra->open_ports--;
688 dbg("%s(): Exit, dev_extra->open_ports = %d", __func__,
689 dev_extra->open_ports);
693 * qt2_write - write bytes from the tty layer out to the USB device.
694 * @buf: The data to be written, size at least count.
695 * @count: The number of bytes requested for transmission.
696 * @return The number of bytes actually accepted for transmission to the device.
698 static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port,
699 const unsigned char *buf, int count)
701 struct usb_serial *serial; /* parent device struct */
702 __u8 header_array[5]; /* header used to direct writes to the correct
703 port on the device */
704 struct quatech2_port *port_extra; /* extra data for this port */
707 serial = port->serial; /* get the parent device of the port */
708 port_extra = qt2_get_port_private(port); /* port extra info */
711 dbg("%s(): port %d, requested to write %d bytes, %d already pending",
712 __func__, port->number, count, port_extra->tx_pending_bytes);
715 dbg("%s(): write request of <= 0 bytes", __func__);
716 return 0; /* no bytes written */
719 /* check if the write urb is already in use, i.e. data already being
720 * sent to this port */
721 if ((port->write_urb->status == -EINPROGRESS)) {
722 /* Fifo hasn't been emptied since last write to this port */
723 dbg("%s(): already writing, port->write_urb->status == "
724 "-EINPROGRESS", __func__);
725 /* schedule_work(&port->work); commented in vendor driver */
727 } else if (port_extra->tx_pending_bytes >= QT2_FIFO_DEPTH) {
728 /* buffer is full (==). > should not occur, but would indicate
729 * that an overflow had occured */
730 dbg("%s(): port transmit buffer is full!", __func__);
731 /* schedule_work(&port->work); commented in vendor driver */
735 /* We must fill the first 5 bytes of anything we sent with a transmit
736 * header which directes the data to the correct port. The maximum
737 * size we can send out in one URB is port->bulk_out_size, which caps
738 * the number of bytes of real data we can send in each write. As the
739 * semantics of write allow us to write less than we were give, we cap
740 * the maximum we will ever write to the device as 5 bytes less than
741 * one URB's worth, by reducing the value of the count argument
743 if (count > port->bulk_out_size - QT2_TX_HEADER_LENGTH) {
744 count = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
745 dbg("%s(): write request bigger than urb, only accepting "
746 "%d bytes", __func__, count);
748 /* we must also ensure that the FIFO at the other end can cope with the
749 * URB we send it, otherwise it will have problems. As above, we can
750 * restrict the write size by just shrinking count.*/
751 if (count > (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes)) {
752 count = QT2_FIFO_DEPTH - port_extra->tx_pending_bytes;
753 dbg("%s(): not enough room in buffer, only accepting %d bytes",
756 /* now build the header for transmission */
757 header_array[0] = 0x1b;
758 header_array[1] = 0x1b;
759 header_array[2] = (__u8)port->number;
760 header_array[3] = (__u8)count;
761 header_array[4] = (__u8)count >> 8;
762 /* copy header into URB */
763 memcpy(port->write_urb->transfer_buffer, header_array,
764 QT2_TX_HEADER_LENGTH);
765 /* and actual data to write */
766 memcpy(port->write_urb->transfer_buffer + 5, buf, count);
768 dbg("%s(): first data byte to send = %#.2x", __func__, *buf);
771 usb_fill_bulk_urb(port->write_urb, serial->dev,
772 usb_sndbulkpipe(serial->dev,
773 port->bulk_out_endpointAddress),
774 port->write_urb->transfer_buffer, count + 5,
775 (qt2_write_bulk_callback), port);
776 /* send the data out the bulk port */
777 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
779 /* error couldn't submit urb */
780 result = 0; /* return 0 as nothing got written */
781 dbg("%s(): failed submitting write urb, error %d",
784 port_extra->tx_pending_bytes += count;
785 result = count; /* return number of bytes written, i.e. count */
786 dbg("%s(): submitted write urb, wrote %d bytes, "
787 "total pending bytes %d",
788 __func__, result, port_extra->tx_pending_bytes);
793 /* This is used by the next layer up to know how much space is available
794 * in the buffer on the device. It is used on a device closure to avoid
795 * calling close() until the buffer is reported to be empty.
796 * The returned value must never go down by more than the number of bytes
797 * written for correct behaviour further up the driver stack, i.e. if I call
798 * it, then write 6 bytes, then call again I should get 6 less, or possibly
799 * only 5 less if one was written in the meantime, etc. I should never get 7
800 * less (or any bigger number) because I only wrote 6 bytes.
802 static int qt2_write_room(struct tty_struct *tty)
804 struct usb_serial_port *port = tty->driver_data;
805 /* parent usb_serial_port pointer */
806 struct quatech2_port *port_extra; /* extra data for this port */
808 port_extra = qt2_get_port_private(port);
810 if (port_extra->close_pending == true) {
811 dbg("%s(): port_extra->close_pending == true", __func__);
814 /* Q: how many bytes would a write() call actually succeed in writing
815 * if it happened now?
816 * A: one QT2_FIFO_DEPTH, less the number of bytes waiting to be sent
817 * out of the port, unless this is more than the size of the
818 * write_urb output buffer less the header, which is the maximum
819 * size write we can do.
821 * Most of the implementation of this is done when writes to the device
822 * are started or terminate. When we send a write to the device, we
823 * reduce the free space count by the size of the dispatched write.
824 * When a "transmit empty" message comes back up the USB read stream,
825 * we decrement the count by the number of bytes reported sent, thus
826 * keeping track of the difference between sent and recieved bytes.
829 room = (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes);
831 if (room > port->bulk_out_size - QT2_TX_HEADER_LENGTH)
832 room = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
833 /* if more than the URB can hold, then cap to that limit */
835 dbg("%s(): port %d: write room is %d", __func__, port->number, room);
839 static int qt2_chars_in_buffer(struct tty_struct *tty)
841 struct usb_serial_port *port = tty->driver_data;
842 /* parent usb_serial_port pointer */
843 struct quatech2_port *port_extra; /* extra data for this port */
844 port_extra = qt2_get_port_private(port);
846 dbg("%s(): port %d: chars_in_buffer = %d", __func__,
847 port->number, port_extra->tx_pending_bytes);
848 return port_extra->tx_pending_bytes;
851 /* called when userspace does an ioctl() on the device. Note that
852 * TIOCMGET and TIOCMSET are filtered off to their own methods before they get
853 * here, so we don't have to handle them.
855 static int qt2_ioctl(struct tty_struct *tty,
856 unsigned int cmd, unsigned long arg)
858 struct usb_serial_port *port = tty->driver_data;
859 struct usb_serial *serial = port->serial;
860 __u8 mcr_value; /* Modem Control Register value */
861 __u8 msr_value; /* Modem Status Register value */
862 unsigned short prev_msr_value; /* Previous value of Modem Status
863 * Register used to implement waiting for a line status change to
865 struct quatech2_port *port_extra; /* extra data for this port */
866 DECLARE_WAITQUEUE(wait, current);
867 /* Declare a wait queue named "wait" */
870 unsigned int UartNumber;
874 UartNumber = tty->index - serial->minor;
875 port_extra = qt2_get_port_private(port);
877 dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__,
878 port->number, UartNumber, tty);
880 if (cmd == TIOCMBIS || cmd == TIOCMBIC) {
881 if (qt2_box_get_register(port->serial, UartNumber,
882 QT2_MODEM_CONTROL_REGISTER, &mcr_value) < 0)
884 if (copy_from_user(&value, (unsigned int *)arg,
890 if (value & TIOCM_RTS)
891 mcr_value |= QT2_SERIAL_MCR_RTS;
892 if (value & TIOCM_DTR)
893 mcr_value |= QT2_SERIAL_MCR_DTR;
894 if (value & TIOCM_LOOP)
895 mcr_value |= QT2_SERIAL_MCR_LOOP;
898 if (value & TIOCM_RTS)
899 mcr_value &= ~QT2_SERIAL_MCR_RTS;
900 if (value & TIOCM_DTR)
901 mcr_value &= ~QT2_SERIAL_MCR_DTR;
902 if (value & TIOCM_LOOP)
903 mcr_value &= ~QT2_SERIAL_MCR_LOOP;
907 } /* end of local switch on cmd */
908 if (qt2_box_set_register(port->serial, UartNumber,
909 QT2_MODEM_CONTROL_REGISTER, mcr_value) < 0) {
912 port_extra->shadowMCR = mcr_value;
915 } else if (cmd == TIOCMIWAIT) {
916 dbg("%s() port %d, cmd == TIOCMIWAIT enter",
917 __func__, port->number);
918 prev_msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
920 add_wait_queue(&port_extra->wait, &wait);
921 set_current_state(TASK_INTERRUPTIBLE);
923 dbg("%s(): port %d, cmd == TIOCMIWAIT here\n",
924 __func__, port->number);
925 remove_wait_queue(&port_extra->wait, &wait);
926 /* see if a signal woke us up */
927 if (signal_pending(current))
929 msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
930 if (msr_value == prev_msr_value)
931 return -EIO; /* no change - error */
932 if ((arg & TIOCM_RNG &&
933 ((prev_msr_value & QT2_SERIAL_MSR_RI) ==
934 (msr_value & QT2_SERIAL_MSR_RI))) ||
936 ((prev_msr_value & QT2_SERIAL_MSR_DSR) ==
937 (msr_value & QT2_SERIAL_MSR_DSR))) ||
939 ((prev_msr_value & QT2_SERIAL_MSR_CD) ==
940 (msr_value & QT2_SERIAL_MSR_CD))) ||
942 ((prev_msr_value & QT2_SERIAL_MSR_CTS) ==
943 (msr_value & QT2_SERIAL_MSR_CTS)))) {
946 } /* end inifinite while */
947 /* FIXME: This while loop needs a way to break out if the device
948 * is disconnected while a process is waiting for the MSR to
949 * change, because once it's disconnected, it isn't going to
950 * change state ... */
952 /* any other ioctls we don't know about come here */
953 dbg("%s(): No ioctl for that one. port = %d", __func__,
959 /* Called when the user wishes to change the port settings using the termios
960 * userspace interface */
961 static void qt2_set_termios(struct tty_struct *tty,
962 struct usb_serial_port *port, struct ktermios *old_termios)
964 struct usb_serial *serial; /* parent serial device */
965 int baud, divisor, remainder;
966 unsigned char LCR_change_to = 0;
970 dbg("%s(): port %d", __func__, port->number);
972 serial = port->serial;
974 UartNumber = port->number;
976 if (old_termios && !tty_termios_hw_change(old_termios, tty->termios))
979 switch (tty->termios->c_cflag) {
981 LCR_change_to |= QT2_SERIAL_5_DATA;
984 LCR_change_to |= QT2_SERIAL_6_DATA;
987 LCR_change_to |= QT2_SERIAL_7_DATA;
991 LCR_change_to |= QT2_SERIAL_8_DATA;
996 if (tty->termios->c_cflag & PARENB) {
997 if (tty->termios->c_cflag & PARODD)
998 LCR_change_to |= QT2_SERIAL_ODD_PARITY;
1000 LCR_change_to |= QT2_SERIAL_EVEN_PARITY;
1002 /* Because LCR_change_to is initialised to zero, we don't have to worry
1003 * about the case where PARENB is not set or clearing bits, because by
1004 * default all of them are cleared, turning parity off.
1005 * as we don't support mark/space parity, we should clear the
1006 * mark/space parity bit in c_cflag, so the caller can tell we have
1007 * ignored the request */
1008 tty->termios->c_cflag &= ~CMSPAR;
1010 if (tty->termios->c_cflag & CSTOPB)
1011 LCR_change_to |= QT2_SERIAL_TWO_STOPB;
1013 LCR_change_to |= QT2_SERIAL_ONE_STOPB;
1015 /* Thats the LCR stuff, next we need to work out the divisor as the
1016 * LCR and the divisor are set together */
1017 baud = tty_get_baud_rate(tty);
1019 /* pick a default, any default... */
1022 dbg("%s(): got baud = %d", __func__, baud);
1024 divisor = QT2_MAX_BAUD_RATE / baud;
1025 remainder = QT2_MAX_BAUD_RATE % baud;
1026 /* Round to nearest divisor */
1027 if (((remainder * 2) >= baud) && (baud != 110))
1029 dbg("%s(): setting divisor = %d, QT2_MAX_BAUD_RATE = %d , LCR = %#.2x",
1030 __func__, divisor, QT2_MAX_BAUD_RATE, LCR_change_to);
1032 status = qt2_boxsetuart(serial, UartNumber, (unsigned short) divisor,
1035 dbg("qt2_boxsetuart() failed");
1038 /* now encode the baud rate we actually set, which may be
1039 * different to the request */
1040 baud = QT2_MAX_BAUD_RATE / divisor;
1041 tty_encode_baud_rate(tty, baud, baud);
1044 /* Now determine flow control */
1045 if (tty->termios->c_cflag & CRTSCTS) {
1046 dbg("%s(): Enabling HW flow control port %d", __func__,
1048 /* Enable RTS/CTS flow control */
1049 status = qt2_boxsethw_flowctl(serial, UartNumber, true);
1051 dbg("qt2_boxsethw_flowctl() failed");
1055 /* Disable RTS/CTS flow control */
1056 dbg("%s(): disabling HW flow control port %d", __func__,
1058 status = qt2_boxsethw_flowctl(serial, UartNumber, false);
1060 dbg("qt2_boxsethw_flowctl failed");
1064 /* if we are implementing XON/XOFF, set the start and stop character
1066 if (I_IXOFF(tty) || I_IXON(tty)) {
1067 unsigned char stop_char = STOP_CHAR(tty);
1068 unsigned char start_char = START_CHAR(tty);
1069 status = qt2_boxsetsw_flowctl(serial, UartNumber, stop_char,
1072 dbg("qt2_boxsetsw_flowctl (enabled) failed");
1074 /* disable SW flow control */
1075 status = qt2_boxunsetsw_flowctl(serial, UartNumber);
1077 dbg("qt2_boxunsetsw_flowctl (disabling) failed");
1081 static int qt2_tiocmget(struct tty_struct *tty)
1083 struct usb_serial_port *port = tty->driver_data;
1084 struct usb_serial *serial = port->serial;
1086 __u8 mcr_value; /* Modem Control Register value */
1087 __u8 msr_value; /* Modem Status Register value */
1088 unsigned int result = 0;
1090 unsigned int UartNumber;
1095 dbg("%s(): port %d, tty =0x%p", __func__, port->number, tty);
1096 UartNumber = tty->index - serial->minor;
1097 dbg("UartNumber is %d", UartNumber);
1099 status = qt2_box_get_register(port->serial, UartNumber,
1100 QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1102 status = qt2_box_get_register(port->serial, UartNumber,
1103 QT2_MODEM_STATUS_REGISTER, &msr_value);
1106 result = ((mcr_value & QT2_SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1108 | ((mcr_value & QT2_SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1110 | ((msr_value & QT2_SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1112 | ((msr_value & QT2_SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1113 /*Carrier detect set */
1114 | ((msr_value & QT2_SERIAL_MSR_RI) ? TIOCM_RI : 0)
1115 /* Ring indicator set */
1116 | ((msr_value & QT2_SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1124 static int qt2_tiocmset(struct tty_struct *tty,
1125 unsigned int set, unsigned int clear)
1127 struct usb_serial_port *port = tty->driver_data;
1128 struct usb_serial *serial = port->serial;
1129 __u8 mcr_value; /* Modem Control Register value */
1131 unsigned int UartNumber;
1136 UartNumber = tty->index - serial->minor;
1137 dbg("%s(): port %d, UartNumber %d", __func__, port->number, UartNumber);
1139 status = qt2_box_get_register(port->serial, UartNumber,
1140 QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1144 /* Turn off RTS, DTR and loopback, then only turn on what was asked
1146 mcr_value &= ~(QT2_SERIAL_MCR_RTS | QT2_SERIAL_MCR_DTR |
1147 QT2_SERIAL_MCR_LOOP);
1148 if (set & TIOCM_RTS)
1149 mcr_value |= QT2_SERIAL_MCR_RTS;
1150 if (set & TIOCM_DTR)
1151 mcr_value |= QT2_SERIAL_MCR_DTR;
1152 if (set & TIOCM_LOOP)
1153 mcr_value |= QT2_SERIAL_MCR_LOOP;
1155 status = qt2_box_set_register(port->serial, UartNumber,
1156 QT2_MODEM_CONTROL_REGISTER, mcr_value);
1163 /** qt2_break - Turn BREAK on and off on the UARTs
1165 static void qt2_break(struct tty_struct *tty, int break_state)
1167 struct usb_serial_port *port = tty->driver_data; /* parent port */
1168 struct usb_serial *serial = port->serial; /* parent device */
1169 struct quatech2_port *port_extra; /* extra data for this port */
1171 unsigned int result;
1173 port_extra = qt2_get_port_private(port);
1175 dbg("%s(): port %d: no serial object", __func__, port->number);
1179 if (break_state == -1)
1183 dbg("%s(): port %d, break_value %d", __func__, port->number,
1186 mutex_lock(&port_extra->modelock);
1187 if (!port_extra->open_count) {
1188 dbg("%s(): port not open", __func__);
1192 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1193 QT2_BREAK_CONTROL, 0x40, break_value,
1194 port->number, NULL, 0, 300);
1196 mutex_unlock(&port_extra->modelock);
1197 dbg("%s(): exit port %d", __func__, port->number);
1201 * qt2_throttle: - stop reading new data from the port
1203 static void qt2_throttle(struct tty_struct *tty)
1205 struct usb_serial_port *port = tty->driver_data;
1206 struct usb_serial *serial = port->serial;
1207 struct quatech2_port *port_extra; /* extra data for this port */
1208 dbg("%s(): port %d", __func__, port->number);
1210 port_extra = qt2_get_port_private(port);
1212 dbg("%s(): enter port %d no serial object", __func__,
1217 mutex_lock(&port_extra->modelock); /* lock structure */
1218 if (!port_extra->open_count) {
1219 dbg("%s(): port not open", __func__);
1222 /* Send command to box to stop receiving stuff. This will stop this
1223 * particular UART from filling the endpoint - in the multiport case the
1224 * FPGA UART will handle any flow control implemented, but for the single
1225 * port it's handed differently and we just quit submitting urbs
1227 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100)
1228 qt2_boxstoprx(serial, port->number, 1);
1230 port->throttled = 1;
1232 mutex_unlock(&port_extra->modelock);
1233 dbg("%s(): port %d: setting port->throttled", __func__, port->number);
1238 * qt2_unthrottle: - start receiving data through the port again after being
1241 static void qt2_unthrottle(struct tty_struct *tty)
1243 struct usb_serial_port *port = tty->driver_data;
1244 struct usb_serial *serial = port->serial;
1245 struct quatech2_port *port_extra; /* extra data for this port */
1246 struct usb_serial_port *port0; /* first port structure on device */
1247 struct quatech2_dev *dev_extra; /* extra data for the device */
1250 dbg("%s() enter port %d no serial object!", __func__,
1254 dbg("%s(): enter port %d", __func__, port->number);
1255 dev_extra = qt2_get_dev_private(serial);
1256 port_extra = qt2_get_port_private(port);
1257 port0 = serial->port[0]; /* get the first port's device structure */
1259 mutex_lock(&port_extra->modelock);
1260 if (!port_extra->open_count) {
1261 dbg("%s(): port %d not open", __func__, port->number);
1265 if (port->throttled != 0) {
1266 dbg("%s(): port %d: unsetting port->throttled", __func__,
1268 port->throttled = 0;
1269 /* Send command to box to start receiving stuff */
1270 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) {
1271 qt2_boxstoprx(serial, port->number, 0);
1272 } else if (dev_extra->ReadBulkStopped == true) {
1273 usb_fill_bulk_urb(port0->read_urb, serial->dev,
1274 usb_rcvbulkpipe(serial->dev,
1275 port0->bulk_in_endpointAddress),
1276 port0->bulk_in_buffer,
1277 port0->bulk_in_size,
1278 qt2_read_bulk_callback,
1283 mutex_unlock(&port_extra->modelock);
1284 dbg("%s(): exit port %d", __func__, port->number);
1288 /* internal, private helper functions for the driver */
1290 /* Power up the FPGA in the box to get it working */
1291 static int qt2_boxpoweron(struct usb_serial *serial)
1296 Direcion = USBD_TRANSFER_DIRECTION_OUT;
1297 pipe = usb_rcvctrlpipe(serial->dev, 0);
1298 result = usb_control_msg(serial->dev, pipe, QT_SET_GET_DEVICE,
1299 Direcion, QU2BOXPWRON, 0x00, NULL, 0x00,
1305 * qt2_boxsetQMCR Issue a QT2_GET_SET_QMCR vendor-spcific request on the
1306 * default control pipe. If successful return the number of bytes written,
1307 * otherwise return a negative error number of the problem.
1309 static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
1315 PortSettings = (__u16)(QMCR_Value);
1317 dbg("%s(): Port = %d, PortSettings = 0x%x", __func__,
1318 Uart_Number, PortSettings);
1320 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1321 QT2_GET_SET_QMCR, 0x40, PortSettings,
1322 (__u16)Uart_Number, NULL, 0, 5000);
1326 static int port_paranoia_check(struct usb_serial_port *port,
1327 const char *function)
1330 dbg("%s - port == NULL", function);
1333 if (!port->serial) {
1334 dbg("%s - port->serial == NULL\n", function);
1340 static int serial_paranoia_check(struct usb_serial *serial,
1341 const char *function)
1344 dbg("%s - serial == NULL\n", function);
1348 if (!serial->type) {
1349 dbg("%s - serial->type == NULL!", function);
1356 static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
1359 return (struct quatech2_port *)usb_get_serial_port_data(port);
1362 static inline void qt2_set_port_private(struct usb_serial_port *port,
1363 struct quatech2_port *data)
1365 usb_set_serial_port_data(port, (void *)data);
1368 static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
1371 return (struct quatech2_dev *)usb_get_serial_data(serial);
1373 static inline void qt2_set_dev_private(struct usb_serial *serial,
1374 struct quatech2_dev *data)
1376 usb_set_serial_data(serial, (void *)data);
1379 static int qt2_openboxchannel(struct usb_serial *serial, __u16
1380 Uart_Number, struct qt2_status_data *status)
1386 length = sizeof(struct qt2_status_data);
1387 Direcion = USBD_TRANSFER_DIRECTION_IN;
1388 pipe = usb_rcvctrlpipe(serial->dev, 0);
1389 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1390 Direcion, 0x00, Uart_Number, status, length, 5000);
1393 static int qt2_closeboxchannel(struct usb_serial *serial, __u16 Uart_Number)
1398 direcion = USBD_TRANSFER_DIRECTION_OUT;
1399 pipe = usb_sndctrlpipe(serial->dev, 0);
1400 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1401 direcion, 0, Uart_Number, NULL, 0, 5000);
1405 /* qt2_conf_uart Issue a SET_UART vendor-spcific request on the default
1406 * control pipe. If successful sets baud rate divisor and LCR value
1408 static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number,
1409 unsigned short divisor, unsigned char LCR)
1412 unsigned short UartNumandLCR;
1414 UartNumandLCR = (LCR << 8) + Uart_Number;
1416 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1417 QT2_GET_SET_UART, 0x40, divisor, UartNumandLCR,
1422 /** @brief Callback for asynchronous submission of read URBs on bulk in
1425 * Registered in qt2_open_port(), used to deal with incomming data
1428 static void qt2_read_bulk_callback(struct urb *urb)
1430 /* Get the device pointer (struct usb_serial) back out of the URB */
1431 struct usb_serial *serial = urb->context;
1432 /* get the extra struct for the device */
1433 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1434 /* Get first port structure from the device */
1435 struct usb_serial_port *port0 = serial->port[0];
1436 /* Get the currently active port structure from serial struct */
1437 struct usb_serial_port *active = dev_extra->current_port;
1438 /* get the extra struct for port 0 */
1439 struct quatech2_port *port0_extra = qt2_get_port_private(port0);
1440 /* and for the currently active port */
1441 struct quatech2_port *active_extra = qt2_get_port_private(active);
1442 /* When we finally get to doing some tty stuff, we will need this */
1443 struct tty_struct *tty_st;
1444 unsigned int RxCount; /* the length of the data to process */
1445 unsigned int i; /* loop counter over the data to process */
1446 int result; /* return value cache variable */
1447 bool escapeflag; /* flag set to true if this loop iteration is
1448 * parsing an escape sequence, rather than
1450 dbg("%s(): callback running, active port is %d", __func__,
1454 /* read didn't go well */
1455 dev_extra->ReadBulkStopped = true;
1456 dbg("%s(): nonzero bulk read status received: %d",
1457 __func__, urb->status);
1461 /* inline port_sofrint() here */
1462 if (port_paranoia_check(port0, __func__) != 0) {
1463 dbg("%s - port_paranoia_check on port0 failed, exiting\n",
1467 if (port_paranoia_check(active, __func__) != 0) {
1468 dbg("%s - port_paranoia_check on current_port "
1469 "failed, exiting", __func__);
1473 /* This single callback function has to do for all the ports on
1474 * the device. Data being read up the USB can contain certain
1475 * escape sequences which are used to communicate out-of-band
1476 * information from the serial port in-band over the USB.
1477 * These escapes include sending modem and flow control line
1478 * status, and switching the port. The concept of a "Current Port"
1479 * is used, which is where data is going until a port change
1480 * escape seqence is received. This Current Port is kept between
1481 * callbacks so that when this function enters we know which the
1482 * currently active port is and can get to work right away without
1483 * the box having to send repeat escape sequences (anyway, how
1484 * would it know to do so?).
1487 if (active_extra->close_pending == true) {
1488 /* We are closing , stop reading */
1489 dbg("%s - (active->close_pending == true", __func__);
1490 if (dev_extra->open_ports <= 0) {
1491 /* If this is the only port left open - stop the
1493 dev_extra->ReadBulkStopped = true;
1494 dbg("%s - (ReadBulkStopped == true;", __func__);
1500 * RxHolding is asserted by throttle, if we assert it, we're not
1501 * receiving any more characters and let the box handle the flow
1504 if ((port0_extra->RxHolding == true) &&
1505 (serial->dev->descriptor.idProduct == QUATECH_SSU2_100)) {
1506 /* single port device, input is already stopped, so we don't
1507 * need any more input data */
1508 dev_extra->ReadBulkStopped = true;
1511 /* finally, we are in a situation where we might consider the data
1512 * that is contained within the URB, and what to do about it.
1513 * This is likely to involved communicating up to the TTY layer, so
1514 * we will need to get hold of the tty for the port we are currently
1517 /* active is a usb_serial_port. It has a member port which is a
1518 * tty_port. From this we get a tty_struct pointer which is what we
1519 * actually wanted, and keep it on tty_st */
1520 tty_st = tty_port_tty_get(&active->port);
1522 dbg("%s - bad tty pointer - exiting", __func__);
1525 RxCount = urb->actual_length; /* grab length of data handy */
1528 /* skip all this if no data to process */
1529 for (i = 0; i < RxCount ; ++i) {
1530 /* Look ahead code here -works on several bytes at onc*/
1531 if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b)
1532 && (NEXTCHAR == 0x1b)) {
1533 /* we are in an escape sequence, type
1534 * determined by the 3rd char */
1536 switch (THIRDCHAR) {
1538 /* Line status change 4th byte must
1540 if (i > (RxCount - 4)) {
1541 dbg("Illegal escape sequences "
1542 "in received data");
1545 qt2_process_line_status(active,
1551 /* Modem status status change 4th byte
1553 if (i > (RxCount - 4)) {
1554 dbg("Illegal escape sequences "
1555 "in received data");
1558 qt2_process_modem_status(active,
1564 /* xmit hold empty 4th byte
1566 if (i > (RxCount - 4)) {
1567 dbg("Illegal escape sequences "
1568 "in received data");
1571 qt2_process_xmit_empty(active,
1572 FOURTHCHAR, FIFTHCHAR);
1577 /* Port number change 4th byte
1579 if (i > (RxCount - 4)) {
1580 dbg("Illegal escape sequences "
1581 "in received data");
1584 /* Port change. If port open push
1585 * current data up to tty layer */
1586 if (active_extra->open_count > 0)
1587 tty_flip_buffer_push(tty_st);
1589 dbg("Port Change: new port = %d",
1591 qt2_process_port_change(active,
1595 /* having changed port, the pointers for
1596 * the currently active port are all out
1597 * of date and need updating */
1598 active = dev_extra->current_port;
1600 qt2_get_port_private(active);
1601 tty_st = tty_port_tty_get(
1605 /* Recv flush 3rd byte must
1607 if (i > (RxCount - 3)) {
1608 dbg("Illegal escape sequences "
1609 "in received data");
1612 qt2_process_rcv_flush(active);
1617 /* xmit flush 3rd byte must follow */
1618 if (i > (RxCount - 3)) {
1619 dbg("Illegal escape sequences "
1620 "in received data");
1623 qt2_process_xmit_flush(active);
1628 dbg("No status sequence");
1629 qt2_process_rx_char(active, THISCHAR);
1630 qt2_process_rx_char(active, NEXTCHAR);
1634 qt2_process_rx_char(active, THISCHAR);
1638 if (escapeflag == true)
1640 /* if we did an escape char, we don't need
1641 * to mess around pushing data through the
1642 * tty layer, and can go round again */
1644 if (tty_st && urb->actual_length) {
1645 tty_buffer_request_room(tty_st, 1);
1646 tty_insert_flip_string(tty_st, &(
1648 (urb->transfer_buffer)
1652 tty_flip_buffer_push(tty_st);
1655 /* at this point we have complete dealing with the data for this
1656 * callback. All we have to do now is to start the async read process
1657 * back off again. */
1659 usb_fill_bulk_urb(port0->read_urb, serial->dev,
1660 usb_rcvbulkpipe(serial->dev, port0->bulk_in_endpointAddress),
1661 port0->bulk_in_buffer, port0->bulk_in_size,
1662 qt2_read_bulk_callback, serial);
1663 result = usb_submit_urb(port0->read_urb, GFP_ATOMIC);
1665 dbg("%s(): failed resubmitting read urb, error %d",
1668 dbg("%s() successfully resubmitted read urb", __func__);
1669 if (tty_st && RxCount) {
1670 /* if some inbound data was processed, then
1671 * we need to push that through the tty layer
1673 tty_flip_buffer_push(tty_st);
1674 tty_schedule_flip(tty_st);
1678 /* cribbed from serqt_usb2 driver, but not sure which work needs
1679 * scheduling - port0 or currently active port? */
1680 /* schedule_work(&port->work); */
1681 dbg("%s() completed", __func__);
1685 /** @brief Callback for asynchronous submission of write URBs on bulk in
1688 * Registered in qt2_write(), used to deal with outgoing data
1691 static void qt2_write_bulk_callback(struct urb *urb)
1693 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1694 struct usb_serial *serial = port->serial;
1695 dbg("%s(): port %d", __func__, port->number);
1697 dbg("%s(): bad serial pointer, exiting", __func__);
1701 dbg("%s(): nonzero write bulk status received: %d",
1702 __func__, urb->status);
1705 /* FIXME What is supposed to be going on here?
1706 * does this actually do anything useful, and should it?
1708 /*port_softint((void *) serial); commented in vendor driver */
1709 schedule_work(&port->work);
1710 dbg("%s(): port %d exit", __func__, port->number);
1714 static void qt2_process_line_status(struct usb_serial_port *port,
1715 unsigned char LineStatus)
1717 /* obtain the private structure for the port */
1718 struct quatech2_port *port_extra = qt2_get_port_private(port);
1719 port_extra->shadowLSR = LineStatus & (QT2_SERIAL_LSR_OE |
1720 QT2_SERIAL_LSR_PE | QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
1722 static void qt2_process_modem_status(struct usb_serial_port *port,
1723 unsigned char ModemStatus)
1725 /* obtain the private structure for the port */
1726 struct quatech2_port *port_extra = qt2_get_port_private(port);
1727 port_extra->shadowMSR = ModemStatus;
1728 wake_up_interruptible(&port_extra->wait);
1729 /* this wakes up the otherwise indefinitely waiting code for
1730 * the TIOCMIWAIT ioctl, so that it can notice that
1731 * port_extra->shadowMSR has changed and the ioctl needs to return.
1735 static void qt2_process_xmit_empty(struct usb_serial_port *port,
1736 unsigned char fourth_char, unsigned char fifth_char)
1739 /* obtain the private structure for the port */
1740 struct quatech2_port *port_extra = qt2_get_port_private(port);
1742 byte_count = (int)(fifth_char * 16);
1743 byte_count += (int)fourth_char;
1744 /* byte_count indicates how many bytes the device has written out. This
1745 * message appears to occur regularly, and is used in the vendor driver
1746 * to keep track of the fill state of the port transmit buffer */
1747 port_extra->tx_pending_bytes -= byte_count;
1748 /* reduce the stored data queue length by the known number of bytes
1750 dbg("port %d: %d bytes reported sent, %d still pending", port->number,
1751 byte_count, port_extra->tx_pending_bytes);
1753 /*port_extra->xmit_fifo_room_bytes = FIFO_DEPTH; ???*/
1756 static void qt2_process_port_change(struct usb_serial_port *port,
1757 unsigned char New_Current_Port)
1759 /* obtain the parent usb serial device structure */
1760 struct usb_serial *serial = port->serial;
1761 /* obtain the private structure for the device */
1762 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1763 dev_extra->current_port = serial->port[New_Current_Port];
1764 /* what should I do with this? commented out in upstream
1766 /*schedule_work(&port->work);*/
1769 static void qt2_process_rcv_flush(struct usb_serial_port *port)
1771 /* obtain the private structure for the port */
1772 struct quatech2_port *port_extra = qt2_get_port_private(port);
1773 port_extra->rcv_flush = true;
1775 static void qt2_process_xmit_flush(struct usb_serial_port *port)
1777 /* obtain the private structure for the port */
1778 struct quatech2_port *port_extra = qt2_get_port_private(port);
1779 port_extra->xmit_flush = true;
1782 static void qt2_process_rx_char(struct usb_serial_port *port,
1785 /* get the tty_struct for this port */
1786 struct tty_struct *tty = tty_port_tty_get(&(port->port));
1787 /* get the URB with the data in to push */
1788 struct urb *urb = port->serial->port[0]->read_urb;
1790 if (tty && urb->actual_length) {
1791 tty_buffer_request_room(tty, 1);
1792 tty_insert_flip_string(tty, &data, 1);
1793 /* should this be commented out here? */
1794 /*tty_flip_buffer_push(tty);*/
1798 /** @brief Retreive the value of a register from the device
1800 * Issues a GET_REGISTER vendor-spcific request over the USB control
1801 * pipe to obtain a value back from a specific register on a specific
1803 * @param serial Serial device handle to access the device through
1804 * @param uart_number Which UART the value is wanted from
1805 * @param register_num Which register to read the value from
1806 * @param pValue Pointer to somewhere to put the retrieved value
1808 static int qt2_box_get_register(struct usb_serial *serial,
1809 unsigned char uart_number, unsigned short register_num,
1813 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1814 QT2_GET_SET_REGISTER, 0xC0, register_num,
1815 uart_number, (void *)pValue, sizeof(*pValue), 300);
1819 /** qt2_box_set_register
1820 * Issue a SET_REGISTER vendor-specific request on the default control pipe
1822 static int qt2_box_set_register(struct usb_serial *serial,
1823 unsigned short Uart_Number, unsigned short Register_Num,
1824 unsigned short Value)
1827 unsigned short reg_and_byte;
1829 reg_and_byte = Value;
1830 reg_and_byte = reg_and_byte << 8;
1831 reg_and_byte = reg_and_byte + Register_Num;
1833 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1834 QT2_GET_SET_REGISTER, 0x40, reg_and_byte,
1835 Uart_Number, NULL, 0, 300);
1839 /** qt2_boxsetuart - Issue a SET_UART vendor-spcific request on the default
1840 * control pipe. If successful sets baud rate divisor and LCR value.
1842 static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
1843 unsigned short default_divisor, unsigned char default_LCR)
1845 unsigned short UartNumandLCR;
1847 UartNumandLCR = (default_LCR << 8) + Uart_Number;
1849 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1850 QT2_GET_SET_UART, 0x40, default_divisor, UartNumandLCR,
1854 /** qt2_boxsethw_flowctl - Turn hardware (RTS/CTS) flow control on and off for
1857 static int qt2_boxsethw_flowctl(struct usb_serial *serial,
1858 unsigned int UartNumber, bool bSet)
1862 __u16 MOUT_Value = 0;
1865 MCR_Value = QT2_SERIAL_MCR_RTS;
1866 /* flow control, box will clear RTS line to prevent remote
1867 * device from transmitting more chars */
1869 /* no flow control to remote device */
1872 MOUT_Value = MCR_Value << 8;
1875 MSR_Value = QT2_SERIAL_MSR_CTS;
1876 /* flow control on, box will inhibit tx data if CTS line is
1879 /* Box will not inhibit tx data due to CTS line */
1882 MOUT_Value |= MSR_Value;
1883 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1884 QT2_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, UartNumber,
1888 /** qt2_boxsetsw_flowctl - Turn software (XON/XOFF) flow control on for
1889 * a hardware UART, and set the XON and XOFF characters.
1891 static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
1892 unsigned char stop_char, unsigned char start_char)
1896 nSWflowout = start_char << 8;
1897 nSWflowout = (unsigned short)stop_char;
1898 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1899 QT2_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, UartNumber,
1903 /** qt2_boxunsetsw_flowctl - Turn software (XON/XOFF) flow control off for
1906 static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber)
1908 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1909 QT2_SW_FLOW_CONTROL_DISABLE, 0x40, 0, UartNumber, NULL,
1914 * qt2_boxstoprx - Start and stop reception of data by the FPGA UART in
1915 * response to requests from the tty layer
1916 * @serial: pointer to the usb_serial structure for the parent device
1917 * @uart_number: which UART on the device we are addressing
1918 * @stop: Whether to start or stop data reception. Set to 1 to stop data being
1919 * received, and to 0 to start it being received.
1921 static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
1922 unsigned short stop)
1924 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1925 QT2_STOP_RECEIVE, 0x40, stop, uart_number, NULL, 0, 300);
1930 * last things in file: stuff to register this driver into the generic
1931 * USB serial framework.
1934 static struct usb_serial_driver quatech2_device = {
1936 .owner = THIS_MODULE,
1937 .name = "quatech_usb2",
1939 .description = DRIVER_DESC,
1940 .usb_driver = &quausb2_usb_driver,
1941 .id_table = quausb2_id_table,
1946 .write_room = qt2_write_room,
1947 .chars_in_buffer = qt2_chars_in_buffer,
1948 .throttle = qt2_throttle,
1949 .unthrottle = qt2_unthrottle,
1950 .calc_num_ports = qt2_calc_num_ports,
1952 .set_termios = qt2_set_termios,
1953 .break_ctl = qt2_break,
1954 .tiocmget = qt2_tiocmget,
1955 .tiocmset = qt2_tiocmset,
1956 .attach = qt2_attach,
1957 .release = qt2_release,
1958 .read_bulk_callback = qt2_read_bulk_callback,
1959 .write_bulk_callback = qt2_write_bulk_callback,
1962 static int __init quausb2_usb_init(void)
1966 dbg("%s\n", __func__);
1968 /* register with usb-serial */
1969 retval = usb_serial_register(&quatech2_device);
1972 goto failed_usb_serial_register;
1974 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1977 /* register with usb */
1979 retval = usb_register(&quausb2_usb_driver);
1983 /* if we're here, usb_register() failed */
1984 usb_serial_deregister(&quatech2_device);
1985 failed_usb_serial_register:
1989 static void __exit quausb2_usb_exit(void)
1991 usb_deregister(&quausb2_usb_driver);
1992 usb_serial_deregister(&quatech2_device);
1995 module_init(quausb2_usb_init);
1996 module_exit(quausb2_usb_exit);
1998 MODULE_AUTHOR(DRIVER_AUTHOR);
1999 MODULE_DESCRIPTION(DRIVER_DESC);
2000 MODULE_LICENSE("GPL");
2002 module_param(debug, bool, S_IRUGO | S_IWUSR);
2003 MODULE_PARM_DESC(debug, "Debug enabled or not");