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 QT_GET_SET_UART 0xc1
57 /*#define QT_HW_FLOW_CONTROL_MASK 0xc5
58 #define QT_SW_FLOW_CONTROL_MASK 0xc6
59 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
60 #define QT_BREAK_CONTROL 0xc8
61 #define QT_STOP_RECEIVE 0xe0*/
62 #define QT2_FLUSH_DEVICE 0xc4
63 #define QT_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 */
70 #define SERIAL_MCR_DTR 0x01
71 #define SERIAL_MCR_RTS 0x02
72 #define SERIAL_MCR_LOOP 0x10
74 #define SERIAL_MSR_CTS 0x10
75 #define SERIAL_MSR_CD 0x80
76 #define SERIAL_MSR_RI 0x40
77 #define SERIAL_MSR_DSR 0x20
78 #define SERIAL_MSR_MASK 0xf0
80 #define SERIAL_8_DATA 0x03
81 #define SERIAL_7_DATA 0x02
82 #define SERIAL_6_DATA 0x01
83 #define SERIAL_5_DATA 0x00
85 #define SERIAL_ODD_PARITY 0X08
86 #define SERIAL_EVEN_PARITY 0X18
87 #define SERIAL_TWO_STOPB 0x04
88 #define SERIAL_ONE_STOPB 0x00
90 #define MAX_BAUD_RATE 921600
91 #define MAX_BAUD_REMAINDER 4608
93 #define SERIAL_LSR_OE 0x02
94 #define SERIAL_LSR_PE 0x04
95 #define SERIAL_LSR_FE 0x08
96 #define SERIAL_LSR_BI 0x10
98 /* value of Line Status Register when UART has completed
99 * emptying data out on the line */
100 #define QT2_LSR_TEMT 0x40
102 /* register numbers on each UART, for use with qt2_box_[get|set]_register*/
103 #define QT2_XMT_HOLD_REGISTER 0x00
104 #define QT2_XVR_BUFFER_REGISTER 0x00
105 #define QT2_FIFO_CONTROL_REGISTER 0x02
106 #define QT2_LINE_CONTROL_REGISTER 0x03
107 #define QT2_MODEM_CONTROL_REGISTER 0x04
108 #define QT2_LINE_STATUS_REGISTER 0x05
109 #define QT2_MODEM_STATUS_REGISTER 0x06
111 /* handy macros for doing escape sequence parsing on data reads */
112 #define THISCHAR ((unsigned char *)(urb->transfer_buffer))[i]
113 #define NEXTCHAR ((unsigned char *)(urb->transfer_buffer))[i + 1]
114 #define THIRDCHAR ((unsigned char *)(urb->transfer_buffer))[i + 2]
115 #define FOURTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 3]
116 #define FIFTHCHAR ((unsigned char *)(urb->transfer_buffer))[i + 4]
118 static struct usb_device_id quausb2_id_table
[] = {
119 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_SSU2_100
)},
120 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_DSU2_100
)},
121 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_DSU2_400
)},
122 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_QSU2_100
)},
123 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_QSU2_400
)},
124 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_ESU2_100
)},
125 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_ESU2_400
)},
126 {} /* Terminating entry */
129 MODULE_DEVICE_TABLE(usb
, quausb2_id_table
);
131 /* custom structures we need go here */
132 static struct usb_driver quausb2_usb_driver
= {
133 .name
= "quatech-usb2-serial",
134 .probe
= usb_serial_probe
,
135 .disconnect
= usb_serial_disconnect
,
136 .id_table
= quausb2_id_table
,
140 /** structure in which to keep all the messy stuff that this driver needs
141 * alongside the usb_serial_port structure
142 * @param read_urb_busy Flag indicating that port->read_urb is in use
143 * @param close_pending flag indicating that this port is in the process of
144 * being closed (and so no new reads / writes should be started).
145 * @param shadowLSR Last received state of the line status register, holds the
146 * value of the line status flags from the port
147 * @param shadowMSR Last received state of the modem status register, holds
148 * the value of the modem status received from the port
149 * @param rcv_flush Flag indicating that a receive flush has occured on
151 * @param xmit_flush Flag indicating that a transmit flush has been processed by
153 * @param fifo_empty_flag
154 * - Starts off true when port opened
155 * - set false when a write is submitted to the driver
156 * - as far as I can see not true again until device is re-opened.
157 * - read in a number of places.
158 * @param tx_fifo_room
159 * - set to FIFO_DEPTH when port opened
160 * - decremented by tc_pending_bytes when a new write is submitted.
161 * - set to FIFO_DEPTH when "xmit_empty" is received from the device,
162 * regardless of how many bytes were reported to have been sent (?)
164 * @param tx_pending_bytes Number of bytes waiting to be sent. This total
165 * includes the size (excluding header) of URBs that have been submitted but
166 * have not yet been sent to to the device, and bytes that have been sent out
167 * of the port but not yet reported sent by the "xmit_empty" messages (which
168 * indicate the number of bytes sent each time they are recieved, despite the
170 * - Starts at zero when port is initialised.
171 * - is incremented by the size of the data to be written (no headers)
172 * each time a write urb is dispatched.
173 * - is decremented each time a "transmit empty" message is received
174 * by the driver in the data stream.
176 struct quatech2_port
{
182 /*int xmit_fifo_room_bytes;*/
185 /* bool fifo_empty_flag;
187 int tx_pending_bytes
;
189 char active
; /* someone has this device open */
190 unsigned char *xfer_to_tty_buffer
;
191 wait_queue_head_t wait
;
192 int open_count
; /* number of times this port has been opened */
193 struct semaphore sem
; /* locks this structure */
194 __u8 shadowLCR
; /* last LCR value received */
195 __u8 shadowMCR
; /* last MCR value received */
197 struct semaphore pend_xmit_sem
; /* locks this structure */
202 * Structure to hold device-wide internal status information
203 * @param ReadBulkStopped The last bulk read attempt ended in tears
204 * @param open_ports The number of serial ports currently in use on the box
205 * @param current_port Pointer to the serial port structure of the port which
206 * the read stream is currently directed to. Escape sequences in the read
207 * stream will change this around as data arrives from different ports on the
209 * @buffer_size: The max size buffer each URB can take, used to set the size of
210 * the buffers allocated for writing to each port on the device (we need to
211 * store this because it is known only to the endpoint, but used each time a
212 * port is opened and a new buffer is allocated.
214 struct quatech2_dev
{
215 bool ReadBulkStopped
;
217 struct usb_serial_port
*current_port
;
221 /* structure which holds line and modem status flags */
222 struct qt2_status_data
{
227 /* Function prototypes */
228 static int qt2_boxpoweron(struct usb_serial
*serial
);
229 static int qt2_boxsetQMCR(struct usb_serial
*serial
, __u16 Uart_Number
,
231 static int port_paranoia_check(struct usb_serial_port
*port
,
232 const char *function
);
233 static int serial_paranoia_check(struct usb_serial
*serial
,
234 const char *function
);
235 static inline struct quatech2_port
*qt2_get_port_private(struct usb_serial_port
237 static inline void qt2_set_port_private(struct usb_serial_port
*port
,
238 struct quatech2_port
*data
);
239 static inline struct quatech2_dev
*qt2_get_dev_private(struct usb_serial
241 static inline void qt2_set_dev_private(struct usb_serial
*serial
,
242 struct quatech2_dev
*data
);
243 static int qt2_openboxchannel(struct usb_serial
*serial
, __u16
244 Uart_Number
, struct qt2_status_data
*pDeviceData
);
245 static int qt2_closeboxchannel(struct usb_serial
*serial
, __u16
247 static int qt2_conf_uart(struct usb_serial
*serial
, unsigned short Uart_Number
,
248 unsigned short divisor
, unsigned char LCR
);
249 static void qt2_read_bulk_callback(struct urb
*urb
);
250 static void qt2_write_bulk_callback(struct urb
*urb
);
251 static void qt2_process_line_status(struct usb_serial_port
*port
,
252 unsigned char LineStatus
);
253 static void qt2_process_modem_status(struct usb_serial_port
*port
,
254 unsigned char ModemStatus
);
255 static void qt2_process_xmit_empty(struct usb_serial_port
*port
,
256 unsigned char fourth_char
, unsigned char fifth_char
);
257 static void qt2_process_port_change(struct usb_serial_port
*port
,
258 unsigned char New_Current_Port
);
259 static void qt2_process_rcv_flush(struct usb_serial_port
*port
);
260 static void qt2_process_xmit_flush(struct usb_serial_port
*port
);
261 static void qt2_process_rx_char(struct usb_serial_port
*port
,
263 static int qt2_box_get_register(struct usb_serial
*serial
,
264 unsigned char uart_number
, unsigned short register_num
,
266 static int qt2_box_set_register(struct usb_serial
*serial
,
267 unsigned short Uart_Number
, unsigned short Register_Num
,
268 unsigned short Value
);
269 static int qt2_box_flush(struct usb_serial
*serial
, unsigned char uart_number
,
270 unsigned short rcv_or_xmit
);
271 static int qt2_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
272 const unsigned char *buf
, int count
);
273 static int qt2_tiocmget(struct tty_struct
*tty
, struct file
*file
);
274 static int qt2_tiocmset(struct tty_struct
*tty
, struct file
*file
,
275 unsigned int set
, unsigned int clear
);
277 /* implementation functions, roughly in order of use, are here */
278 static int qt2_calc_num_ports(struct usb_serial
*serial
)
282 switch (serial
->dev
->descriptor
.idProduct
) {
283 case QUATECH_SSU2_100
:
287 case QUATECH_DSU2_400
:
289 case QUATECH_DSU2_100
:
293 case QUATECH_QSU2_400
:
295 case QUATECH_QSU2_100
:
299 case QUATECH_ESU2_400
:
301 case QUATECH_ESU2_100
:
311 static int qt2_attach(struct usb_serial
*serial
)
313 struct usb_serial_port
*port
;
314 struct quatech2_port
*qt2_port
; /* port-specific private data pointer */
315 struct quatech2_dev
*qt2_dev
; /* dev-specific private data pointer */
317 /* stuff for storing endpoint addresses now */
318 struct usb_endpoint_descriptor
*endpoint
;
319 struct usb_host_interface
*iface_desc
;
320 struct usb_serial_port
*port0
; /* first port structure on device */
322 /* check how many endpoints there are on the device, for
324 dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in",
325 __func__
, serial
->num_bulk_in
,
326 serial
->num_bulk_out
, serial
->num_interrupt_in
);
327 if ((serial
->num_bulk_in
!= 1) || (serial
->num_bulk_out
!= 1)) {
328 dbg("Device has wrong number of bulk endpoints!");
331 iface_desc
= serial
->interface
->cur_altsetting
;
333 /* Set up per-device private data, storing extra data alongside
334 * struct usb_serial */
335 qt2_dev
= kzalloc(sizeof(*qt2_dev
), GFP_KERNEL
);
337 dbg("%s: kmalloc for quatech2_dev failed!",
341 qt2_dev
->open_ports
= 0; /* no ports open */
342 qt2_set_dev_private(serial
, qt2_dev
); /* store private data */
344 /* Now setup per port private data, which replaces all the things
345 * that quatech added to standard kernel structures in their driver */
346 for (i
= 0; i
< serial
->num_ports
; i
++) {
347 port
= serial
->port
[i
];
348 qt2_port
= kzalloc(sizeof(*qt2_port
), GFP_KERNEL
);
350 dbg("%s: kmalloc for quatech2_port (%d) failed!.",
354 spin_lock_init(&qt2_port
->lock
);
355 qt2_set_port_private(port
, qt2_port
);
358 /* gain access to port[0]'s structure because we want to store
359 * device-level stuff in it */
360 if (serial_paranoia_check(serial
, __func__
))
362 port0
= serial
->port
[0]; /* get the first port's device structure */
364 /* print endpoint addresses so we can check them later
366 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
367 endpoint
= &iface_desc
->endpoint
[i
].desc
;
368 if ((endpoint
->bEndpointAddress
& 0x80) &&
369 ((endpoint
->bmAttributes
& 3) == 0x02)) {
370 /* we found a bulk in endpoint */
371 dbg("found bulk in at %#.2x",
372 endpoint
->bEndpointAddress
);
375 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
376 ((endpoint
->bmAttributes
& 3) == 0x02)) {
377 /* we found a bulk out endpoint */
378 dbg("found bulk out at %#.2x",
379 endpoint
->bEndpointAddress
);
380 qt2_dev
->buffer_size
= endpoint
->wMaxPacketSize
;
381 /* max size of URB needs recording for the device */
383 } /* end printing endpoint addresses */
385 /* switch on power to the hardware */
386 if (qt2_boxpoweron(serial
) < 0) {
387 dbg("qt2_boxpoweron() failed");
390 /* set all ports to RS232 mode */
391 for (i
= 0; i
< serial
->num_ports
; ++i
) {
392 if (qt2_boxsetQMCR(serial
, i
, QU2BOX232
) < 0) {
393 dbg("qt2_boxsetQMCR() on port %d failed",
402 for (i
= 0; i
< serial
->num_ports
; i
++) {
403 port
= serial
->port
[i
];
404 qt2_port
= qt2_get_port_private(port
);
406 qt2_set_port_private(port
, NULL
);
408 qt2_dev
= qt2_get_dev_private(serial
);
410 qt2_set_dev_private(serial
, NULL
);
412 dbg("Exit fail %s\n", __func__
);
416 static void qt2_release(struct usb_serial
*serial
)
418 struct usb_serial_port
*port
;
419 struct quatech2_port
*qt_port
;
422 dbg("enterting %s", __func__
);
424 for (i
= 0; i
< serial
->num_ports
; i
++) {
425 port
= serial
->port
[i
];
429 qt_port
= usb_get_serial_port_data(port
);
431 usb_set_serial_port_data(port
, NULL
);
434 /* This function is called once per serial port on the device, when
435 * that port is opened by a userspace application.
436 * The tty_struct and the usb_serial_port belong to this port,
437 * i.e. there are multiple ones for a multi-port device.
438 * However the usb_serial_port structure has a back-pointer
439 * to the parent usb_serial structure which belongs to the device,
440 * so we can access either the device-wide information or
441 * any other port's information (because there are also forward
442 * pointers) via that pointer.
443 * This is most helpful if the device shares resources (e.g. end
444 * points) between different ports
446 int qt2_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
448 struct usb_serial
*serial
; /* device structure */
449 struct usb_serial_port
*port0
; /* first port structure on device */
450 struct quatech2_port
*port_extra
; /* extra data for this port */
451 struct quatech2_port
*port0_extra
; /* extra data for first port */
452 struct quatech2_dev
*dev_extra
; /* extra data for the device */
453 struct qt2_status_data ChannelData
;
454 unsigned short default_divisor
= QU2BOXSPD9600
;
455 unsigned char default_LCR
= SERIAL_8_DATA
;
459 if (port_paranoia_check(port
, __func__
))
462 dbg("%s(): port %d", __func__
, port
->number
);
464 serial
= port
->serial
; /* get the parent device structure */
465 if (serial_paranoia_check(serial
, __func__
)) {
466 dbg("usb_serial struct failed sanity check");
469 dev_extra
= qt2_get_dev_private(serial
);
470 /* get the device private data */
471 if (dev_extra
== NULL
) {
472 dbg("device extra data pointer is null");
475 port0
= serial
->port
[0]; /* get the first port's device structure */
476 if (port_paranoia_check(port0
, __func__
)) {
477 dbg("port0 usb_serial_port struct failed sanity check");
481 port_extra
= qt2_get_port_private(port
);
482 port0_extra
= qt2_get_port_private(port0
);
483 if (port_extra
== NULL
|| port0_extra
== NULL
) {
484 dbg("failed to get private data for port or port0");
488 /* FIXME: are these needed? Does it even do anything useful? */
489 /* get the modem and line status values from the UART */
490 status
= qt2_openboxchannel(serial
, port
->number
,
493 dbg("qt2_openboxchannel on channel %d failed",
497 port_extra
->shadowLSR
= ChannelData
.line_status
&
498 (SERIAL_LSR_OE
| SERIAL_LSR_PE
| SERIAL_LSR_FE
|
500 port_extra
->shadowMSR
= ChannelData
.modem_status
&
501 (SERIAL_MSR_CTS
| SERIAL_MSR_DSR
| SERIAL_MSR_RI
|
504 /* port_extra->fifo_empty_flag = true;*/
505 dbg("qt2_openboxchannel on channel %d completed.",
508 /* Set Baud rate to default and turn off flow control here */
509 status
= qt2_conf_uart(serial
, port
->number
, default_divisor
,
512 dbg("qt2_conf_uart() failed on channel %d",
516 dbg("qt2_conf_uart() completed on channel %d",
520 * At this point we will need some end points to make further progress.
521 * Handlily, the correct endpoint addresses have been filled out into
522 * the usb_serial_port structure for us by the driver core, so we
523 * already have access to them.
524 * As there is only one bulk in and one bulk out end-point, these are in
525 * port[0]'s structure, and the rest are uninitialised. Handily,
526 * when we do a write to a port, we will use the same endpoint
527 * regardless of the port, with a 5-byte header added on to
528 * tell the box which port it should eventually come out of, so we only
529 * need the one set of endpoints. We will have one URB per port for
530 * writing, so that multiple ports can be writing at once.
531 * Finally we need a bulk in URB to use for background reads from the
532 * device, which will deal with uplink data from the box to host.
534 dbg("serial number is %d", port
->serial
->minor
);
535 dbg("port0 bulk in endpoint is %#.2x", port0
->bulk_in_endpointAddress
);
536 dbg("port0 bulk out endpoint is %#.2x",
537 port0
->bulk_out_endpointAddress
);
539 /* set up write_urb for bulk out transfers on this port. The USB
540 * serial framework will have allocated a blank URB, buffer etc for
541 * port0 when it put the endpoints there, but not for any of the other
542 * ports on the device because there are no more endpoints. Thus we
543 * have to allocate our own URBs for ports 1-7
545 if (port
->write_urb
== NULL
) {
546 dbg("port->write_urb == NULL, allocating one");
547 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
548 if (!port
->write_urb
) {
549 err("Allocating write URB failed");
552 /* buffer same size as port0 */
553 port
->bulk_out_size
= dev_extra
->buffer_size
;
554 port
->bulk_out_buffer
= kmalloc(port
->bulk_out_size
,
556 if (!port
->bulk_out_buffer
) {
557 err("Couldn't allocate bulk_out_buffer");
561 if (serial
->dev
== NULL
)
562 dbg("serial->dev == NULL");
563 dbg("port->bulk_out_size is %d", port
->bulk_out_size
);
565 usb_fill_bulk_urb(port
->write_urb
, serial
->dev
,
566 usb_sndbulkpipe(serial
->dev
,
567 port0
->bulk_out_endpointAddress
),
568 port
->bulk_out_buffer
,
570 qt2_write_bulk_callback
,
572 /*port_extra->fifo_empty_flag = true;
573 port_extra->tx_fifo_room = FIFO_DEPTH;*/
574 port_extra
->tx_pending_bytes
= 0;
576 if (dev_extra
->open_ports
== 0) {
577 /* this is first port to be opened, so need the read URB
578 * initialised for bulk in transfers (this is shared amongst
579 * all the ports on the device) */
580 usb_fill_bulk_urb(port0
->read_urb
, serial
->dev
,
581 usb_rcvbulkpipe(serial
->dev
,
582 port0
->bulk_in_endpointAddress
),
583 port0
->bulk_in_buffer
,
585 qt2_read_bulk_callback
, serial
);
586 dbg("port0 bulk in URB intialised");
588 /* submit URB, i.e. start reading from device (async) */
589 dev_extra
->ReadBulkStopped
= false;
590 port_extra
->read_urb_busy
= true;
591 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
594 "%s(): Error %d submitting bulk in urb",
596 port_extra
->read_urb_busy
= false;
597 dev_extra
->ReadBulkStopped
= true;
600 /* When the first port is opened, initialise the value of
601 * current_port in dev_extra to this port, so it is set
602 * to something. Once the box sends data it will send the
603 * relevant escape sequences to get it to the right port anyway
605 dev_extra
->current_port
= port
;
608 /* initialize our wait queues */
609 init_waitqueue_head(&port_extra
->wait
);
611 /* remember to store dev_extra, port_extra and port0_extra back again at
613 qt2_set_port_private(port
, port_extra
);
614 qt2_set_port_private(serial
->port
[0], port0_extra
);
615 qt2_set_dev_private(serial
, dev_extra
);
617 dev_extra
->open_ports
++; /* one more port opened */
622 /* called when a port is closed by userspace */
623 /* Setting close_pending should keep new data from being written out,
624 * once all the data in the enpoint buffers is moved out we won't get
626 /* BoxStopReceive would keep any more data from coming from a given
627 * port, but isn't called by the vendor driver, although their comments
628 * mention it. Should it be used here to stop the inbound data
631 static void qt2_close(struct usb_serial_port
*port
)
633 /* time out value for flush loops */
635 struct quatech2_port
*port_extra
; /* extra data for this port */
636 struct usb_serial
*serial
; /* device structure */
637 struct quatech2_dev
*dev_extra
; /* extra data for the device */
638 __u8 lsr_value
= 0; /* value of Line Status Register */
639 int status
; /* result of last USB comms function */
641 dbg("%s(): port %d", __func__
, port
->number
);
642 serial
= port
->serial
; /* get the parent device structure */
643 dev_extra
= qt2_get_dev_private(serial
);
644 /* get the device private data */
645 port_extra
= qt2_get_port_private(port
); /* port private data */
647 /* to check we have successfully flushed the buffers on the hardware,
648 * we set the flags indicating flushes have occured to false, then ask
649 * for flushes to occur, then sit in a timed loop until either we
650 * get notified back that the flushes have happened (good) or we get
651 * tired of waiting for the flush to happen and give up (bad).
653 port_extra
->rcv_flush
= false;
654 port_extra
->xmit_flush
= false;
655 qt2_box_flush(serial
, port
->number
, QT2_FLUSH_TX
); /* flush tx buffer */
656 qt2_box_flush(serial
, port
->number
, QT2_FLUSH_RX
); /* flush rx buffer */
657 /* now wait for the flags to magically go back to being true */
658 jift
= jiffies
+ (10 * HZ
);
660 if ((port_extra
->rcv_flush
== true) &&
661 (port_extra
->xmit_flush
== true)) {
662 dbg("Flush completed");
666 } while (jiffies
<= jift
);
668 /* we can now (and only now) stop reading data */
669 port_extra
->close_pending
= true;
670 dbg("%s(): port_extra->close_pending = true", __func__
);
671 /* although the USB side is now empty, the UART itself may
672 * still be pushing characters out over the line, so we have to
673 * wait testing the actual line status until the lines change
674 * indicating that the data is done transfering. */
675 jift
= jiffies
+ (10 * HZ
); /* 10 sec timeout */
677 status
= qt2_box_get_register(serial
, port
->number
,
678 QT2_LINE_STATUS_REGISTER
, &lsr_value
);
680 dbg("%s(): qt2_box_get_register failed", __func__
);
683 if ((lsr_value
& QT2_LSR_TEMT
)) {
684 dbg("UART done sending");
688 } while (jiffies
<= jift
);
690 status
= qt2_closeboxchannel(serial
, port
->number
);
692 dbg("%s(): port %d qt2_box_open_close_channel failed",
693 __func__
, port
->number
);
694 /* to avoid leaking URBs, we should now free the write_urb for this
695 * port and set the pointer to null so that next time the port is opened
696 * a new URB is allocated. This avoids leaking URBs when the device is
698 usb_free_urb(port
->write_urb
);
699 kfree(port
->bulk_out_buffer
);
700 port
->bulk_out_buffer
= NULL
;
701 port
->bulk_out_size
= 0;
703 dev_extra
->open_ports
--;
704 dbg("%s(): Exit, dev_extra->open_ports = %d", __func__
,
705 dev_extra
->open_ports
);
708 /* called to deliver writes from the next layer up to the device */
709 static int qt2_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
710 const unsigned char *buf
, int count
)
712 struct usb_serial
*serial
; /* parent device struct */
713 __u8 header_array
[5]; /* header used to direct writes to the correct
714 port on the device */
715 struct quatech2_port
*port_extra
; /* extra data for this port */
719 /* get the parent device of the port */
720 serial
= port
->serial
;
723 dbg("%s(): port %d", __func__
, port
->number
);
726 dbg("%s(): write request of <= 0 bytes", __func__
);
727 return 0; /* no bytes written */
729 port_extra
= qt2_get_port_private(port
);
731 /* check if the write urb is already in use, i.e. data already being
732 * sent to this port */
733 if ((port
->write_urb
->status
== -EINPROGRESS
)) {
734 /* Fifo hasn't been emptied since last write to this port */
735 dbg("%s(): already writing, port->write_urb->status == "
736 "-EINPROGRESS", __func__
);
737 /* schedule_work(&port->work); commented in vendor driver */
739 } else if (port_extra
->tx_pending_bytes
>= QT2_FIFO_DEPTH
) {
740 /* buffer is full (==). > should not occur, but would indicate
741 * that an overflow had occured */
742 dbg("%s(): port transmit buffer is full!", __func__
);
743 /* schedule_work(&port->work); commented in vendor driver */
747 /* We must fill the first 5 bytes of anything we sent with a transmit
748 * header which directes the data to the correct port. The maximum
749 * size we can send out in one URB is port->bulk_out_size, which caps
750 * the number of bytes of real data we can send in each write. As the
751 * semantics of write allow us to write less than we were give, we cap
752 * the maximum we will ever write to the device as 5 bytes less than
753 * one URB's worth, by reducing the value of the count argument
755 if (count
> port
->bulk_out_size
- QT2_TX_HEADER_LENGTH
)
756 count
= port
->bulk_out_size
- QT2_TX_HEADER_LENGTH
;
757 /* we must also ensure that the FIFO at the other end can cope with the
758 * URB we send it, otherwise it will have problems. As above, we can
759 * restrict the write size by just shrinking count.*/
760 if (count
> (QT2_FIFO_DEPTH
- port_extra
->tx_pending_bytes
))
761 count
= QT2_FIFO_DEPTH
- port_extra
->tx_pending_bytes
;
762 /* now build the header for transmission */
763 header_array
[0] = 0x1b;
764 header_array
[1] = 0x1b;
765 header_array
[2] = (__u8
)port
->number
;
766 header_array
[3] = (__u8
)count
;
767 header_array
[4] = (__u8
)count
>> 8;
768 /* copy header into URB */
769 memcpy(port
->write_urb
->transfer_buffer
, header_array
,
770 QT2_TX_HEADER_LENGTH
);
771 /* and actual data to write */
772 memcpy(port
->write_urb
->transfer_buffer
+ 5, buf
, count
);
774 dbg("%s(): first data byte to send = %#.2x", __func__
, *buf
);
777 usb_fill_bulk_urb(port
->write_urb
, serial
->dev
,
778 usb_sndbulkpipe(serial
->dev
,
779 port
->bulk_out_endpointAddress
),
780 port
->write_urb
->transfer_buffer
, count
+ 5,
781 (qt2_write_bulk_callback
), port
);
782 /* send the data out the bulk port */
783 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
785 /* error couldn't submit urb */
787 dbg("%s(): failed submitting write urb, error %d",
790 port_extra
->tx_pending_bytes
+= (count
- QT2_TX_HEADER_LENGTH
);
791 /*port->fifo_empty_flag = false;
792 port->xmit_fifo_room_bytes = FIFO_DEPTH -
793 port->xmit_pending_bytes;*/
795 dbg("%s(): submitted write urb, returning %d",
801 /* This is used by the next layer up to know how much space is available
802 * in the buffer on the device. It is used on a device closure to avoid
803 * calling close() until the buffer is reported to be empty.
804 * The returned value must never go down by more than the number of bytes
805 * written for correct behaviour further up the driver stack, i.e. if I call
806 * it, then write 6 bytes, then call again I should get 6 less, or possibly
807 * only 5 less if one was written in the meantime, etc. I should never get 7
808 * less (or any bigger number) because I only wrote 6 bytes.
810 static int qt2_write_room(struct tty_struct
*tty
)
812 struct usb_serial_port
*port
= tty
->driver_data
;
813 /* parent usb_serial_port pointer */
814 struct quatech2_port
*port_extra
; /* extra data for this port */
816 port_extra
= qt2_get_port_private(port
);
818 if (port_extra
->close_pending
== true) {
819 dbg("%s(): port_extra->close_pending == true", __func__
);
822 /* Q: how many bytes would a write() call actually succeed in writing
823 * if it happened now?
824 * A: one QT2_FIFO_DEPTH, less the number of bytes waiting to be sent
825 * out of the port, unless this is more than the size of the
826 * write_urb output buffer less the header, which is the maximum
827 * size write we can do.
829 * Most of the implementation of this is done when writes to the device
830 * are started or terminate. When we send a write to the device, we
831 * reduce the free space count by the size of the dispatched write.
832 * When a "transmit empty" message comes back up the USB read stream,
833 * we decrement the count by the number of bytes reported sent, thus
834 * keeping track of the difference between sent and recieved bytes.
837 room
= (QT2_FIFO_DEPTH
- port_extra
->tx_pending_bytes
);
839 if (room
> port
->bulk_out_size
- QT2_TX_HEADER_LENGTH
)
840 room
= port
->bulk_out_size
- QT2_TX_HEADER_LENGTH
;
841 /* if more than the URB can hold, then cap to that limit */
843 dbg("%s(): port %d: write room is %d", __func__
, port
->number
, room
);
847 static int qt2_chars_in_buffer(struct tty_struct
*tty
)
849 struct usb_serial_port
*port
= tty
->driver_data
;
850 /* parent usb_serial_port pointer */
852 struct quatech2_port
*port_extra
; /* extra data for this port */
853 port_extra
= qt2_get_port_private(port
);
855 dbg("%s(): port %d", __func__
, port
->number
);
856 if ((port
->write_urb
->status
== -EINPROGRESS
) &&
857 (port_extra
->tx_pending_bytes
!= 0))
858 chars
= port
->write_urb
->transfer_buffer_length
;
859 dbg("%s(): returns %d", __func__
, chars
);
863 /* called when userspace does an ioctl() on the device. Note that
864 * TIOCMGET and TIOCMSET are filtered off to their own methods before they get
865 * here, so we don't have to handle them.
867 static int qt2_ioctl(struct tty_struct
*tty
, struct file
*file
,
868 unsigned int cmd
, unsigned long arg
)
870 struct usb_serial_port
*port
= tty
->driver_data
;
871 struct usb_serial
*serial
= port
->serial
;
872 __u8 mcr_value
; /* Modem Control Register value */
873 __u8 msr_value
; /* Modem Status Register value */
874 unsigned short prev_msr_value
; /* Previous value of Modem Status
875 * Register used to implement waiting for a line status change to
877 struct quatech2_port
*port_extra
; /* extra data for this port */
878 DECLARE_WAITQUEUE(wait
, current
);
879 /* Declare a wait queue named "wait" */
883 unsigned int UartNumber
;
887 UartNumber
= tty
->index
- serial
->minor
;
888 port_extra
= qt2_get_port_private(port
);
890 dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__
,
891 port
->number
, UartNumber
, tty
);
893 if (cmd
== TIOCMBIS
|| cmd
== TIOCMBIC
) {
894 if (qt2_box_get_register(port
->serial
, UartNumber
,
895 QT2_MODEM_CONTROL_REGISTER
, &mcr_value
) < 0)
897 if (copy_from_user(&value
, (unsigned int *)arg
,
903 if (value
& TIOCM_RTS
)
904 mcr_value
|= SERIAL_MCR_RTS
;
905 if (value
& TIOCM_DTR
)
906 mcr_value
|= SERIAL_MCR_DTR
;
907 if (value
& TIOCM_LOOP
)
908 mcr_value
|= SERIAL_MCR_LOOP
;
911 if (value
& TIOCM_RTS
)
912 mcr_value
&= ~SERIAL_MCR_RTS
;
913 if (value
& TIOCM_DTR
)
914 mcr_value
&= ~SERIAL_MCR_DTR
;
915 if (value
& TIOCM_LOOP
)
916 mcr_value
&= ~SERIAL_MCR_LOOP
;
920 } /* end of local switch on cmd */
921 if (qt2_box_set_register(port
->serial
, UartNumber
,
922 QT2_MODEM_CONTROL_REGISTER
, mcr_value
) < 0) {
925 port_extra
->shadowMCR
= mcr_value
;
928 } else if (cmd
== TIOCMIWAIT
) {
929 dbg("%s() port %d, cmd == TIOCMIWAIT enter",
930 __func__
, port
->number
);
931 prev_msr_value
= port_extra
->shadowMSR
& SERIAL_MSR_MASK
;
933 add_wait_queue(&port_extra
->wait
, &wait
);
934 set_current_state(TASK_INTERRUPTIBLE
);
936 dbg("%s(): port %d, cmd == TIOCMIWAIT here\n",
937 __func__
, port
->number
);
938 remove_wait_queue(&port_extra
->wait
, &wait
);
939 /* see if a signal woke us up */
940 if (signal_pending(current
))
942 msr_value
= port_extra
->shadowMSR
& SERIAL_MSR_MASK
;
943 if (msr_value
== prev_msr_value
)
944 return -EIO
; /* no change - error */
945 if ((arg
& TIOCM_RNG
&&
946 ((prev_msr_value
& SERIAL_MSR_RI
) ==
947 (msr_value
& SERIAL_MSR_RI
))) ||
949 ((prev_msr_value
& SERIAL_MSR_DSR
) ==
950 (msr_value
& SERIAL_MSR_DSR
))) ||
952 ((prev_msr_value
& SERIAL_MSR_CD
) ==
953 (msr_value
& SERIAL_MSR_CD
))) ||
955 ((prev_msr_value
& SERIAL_MSR_CTS
) ==
956 (msr_value
& SERIAL_MSR_CTS
)))) {
959 } /* end inifinite while */
960 /* FIXME: This while loop needs a way to break out if the device
961 * is disconnected while a process is waiting for the MSR to
962 * change, because once it's disconnected, it isn't going to
963 * change state ... */
965 /* any other ioctls we don't know about come here */
966 dbg("%s(): No ioctl for that one. port = %d", __func__
,
972 static int qt2_tiocmget(struct tty_struct
*tty
, struct file
*file
)
974 struct usb_serial_port
*port
= tty
->driver_data
;
975 struct usb_serial
*serial
= port
->serial
;
977 __u8 mcr_value
; /* Modem Control Register value */
978 __u8 msr_value
; /* Modem Status Register value */
979 unsigned int result
= 0;
981 unsigned int UartNumber
;
986 dbg("%s(): port %d, tty =0x%p", __func__
, port
->number
, tty
);
987 UartNumber
= tty
->index
- serial
->minor
;
988 dbg("UartNumber is %d", UartNumber
);
990 status
= qt2_box_get_register(port
->serial
, UartNumber
,
991 QT2_MODEM_CONTROL_REGISTER
, &mcr_value
);
993 status
= qt2_box_get_register(port
->serial
, UartNumber
,
994 QT2_MODEM_STATUS_REGISTER
, &msr_value
);
997 result
= ((mcr_value
& SERIAL_MCR_DTR
) ? TIOCM_DTR
: 0)
999 | ((mcr_value
& SERIAL_MCR_RTS
) ? TIOCM_RTS
: 0)
1001 | ((msr_value
& SERIAL_MSR_CTS
) ? TIOCM_CTS
: 0)
1003 | ((msr_value
& SERIAL_MSR_CD
) ? TIOCM_CAR
: 0)
1004 /*Carrier detect set */
1005 | ((msr_value
& SERIAL_MSR_RI
) ? TIOCM_RI
: 0)
1006 /* Ring indicator set */
1007 | ((msr_value
& SERIAL_MSR_DSR
) ? TIOCM_DSR
: 0);
1015 static int qt2_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1016 unsigned int set
, unsigned int clear
)
1018 struct usb_serial_port
*port
= tty
->driver_data
;
1019 struct usb_serial
*serial
= port
->serial
;
1020 __u8 mcr_value
; /* Modem Control Register value */
1022 unsigned int UartNumber
;
1027 UartNumber
= tty
->index
- serial
->minor
;
1028 dbg("%s(): port %d, UartNumber %d", __func__
, port
->number
, UartNumber
);
1030 status
= qt2_box_get_register(port
->serial
, UartNumber
,
1031 QT2_MODEM_CONTROL_REGISTER
, &mcr_value
);
1035 /* Turn off RTS, DTR and loopback, then only turn on what was asked
1037 mcr_value
&= ~(SERIAL_MCR_RTS
| SERIAL_MCR_DTR
| SERIAL_MCR_LOOP
);
1038 if (set
& TIOCM_RTS
)
1039 mcr_value
|= SERIAL_MCR_RTS
;
1040 if (set
& TIOCM_DTR
)
1041 mcr_value
|= SERIAL_MCR_DTR
;
1042 if (set
& TIOCM_LOOP
)
1043 mcr_value
|= SERIAL_MCR_LOOP
;
1045 status
= qt2_box_set_register(port
->serial
, UartNumber
,
1046 QT2_MODEM_CONTROL_REGISTER
, mcr_value
);
1053 /* internal, private helper functions for the driver */
1055 /* Power up the FPGA in the box to get it working */
1056 static int qt2_boxpoweron(struct usb_serial
*serial
)
1061 Direcion
= USBD_TRANSFER_DIRECTION_OUT
;
1062 pipe
= usb_rcvctrlpipe(serial
->dev
, 0);
1063 result
= usb_control_msg(serial
->dev
, pipe
, QT_SET_GET_DEVICE
,
1064 Direcion
, QU2BOXPWRON
, 0x00, NULL
, 0x00,
1070 * qt2_boxsetQMCR Issue a QT_GET_SET_QMCR vendor-spcific request on the
1071 * default control pipe. If successful return the number of bytes written,
1072 * otherwise return a negative error number of the problem.
1074 static int qt2_boxsetQMCR(struct usb_serial
*serial
, __u16 Uart_Number
,
1080 PortSettings
= (__u16
)(QMCR_Value
);
1082 dbg("%s(): Port = %d, PortSettings = 0x%x", __func__
,
1083 Uart_Number
, PortSettings
);
1085 result
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1086 QT_GET_SET_QMCR
, 0x40, PortSettings
,
1087 (__u16
)Uart_Number
, NULL
, 0, 5000);
1091 static int port_paranoia_check(struct usb_serial_port
*port
,
1092 const char *function
)
1095 dbg("%s - port == NULL", function
);
1098 if (!port
->serial
) {
1099 dbg("%s - port->serial == NULL\n", function
);
1105 static int serial_paranoia_check(struct usb_serial
*serial
,
1106 const char *function
)
1109 dbg("%s - serial == NULL\n", function
);
1113 if (!serial
->type
) {
1114 dbg("%s - serial->type == NULL!", function
);
1121 static inline struct quatech2_port
*qt2_get_port_private(struct usb_serial_port
1124 return (struct quatech2_port
*)usb_get_serial_port_data(port
);
1127 static inline void qt2_set_port_private(struct usb_serial_port
*port
,
1128 struct quatech2_port
*data
)
1130 usb_set_serial_port_data(port
, (void *)data
);
1133 static inline struct quatech2_dev
*qt2_get_dev_private(struct usb_serial
1136 return (struct quatech2_dev
*)usb_get_serial_data(serial
);
1138 static inline void qt2_set_dev_private(struct usb_serial
*serial
,
1139 struct quatech2_dev
*data
)
1141 usb_set_serial_data(serial
, (void *)data
);
1144 static int qt2_openboxchannel(struct usb_serial
*serial
, __u16
1145 Uart_Number
, struct qt2_status_data
*status
)
1151 length
= sizeof(struct qt2_status_data
);
1152 Direcion
= USBD_TRANSFER_DIRECTION_IN
;
1153 pipe
= usb_rcvctrlpipe(serial
->dev
, 0);
1154 result
= usb_control_msg(serial
->dev
, pipe
, QT_OPEN_CLOSE_CHANNEL
,
1155 Direcion
, 0x00, Uart_Number
, status
, length
, 5000);
1158 static int qt2_closeboxchannel(struct usb_serial
*serial
, __u16 Uart_Number
)
1163 direcion
= USBD_TRANSFER_DIRECTION_OUT
;
1164 pipe
= usb_sndctrlpipe(serial
->dev
, 0);
1165 result
= usb_control_msg(serial
->dev
, pipe
, QT_OPEN_CLOSE_CHANNEL
,
1166 direcion
, 0, Uart_Number
, NULL
, 0, 5000);
1170 /* qt2_conf_uart Issue a SET_UART vendor-spcific request on the default
1171 * control pipe. If successful sets baud rate divisor and LCR value
1173 static int qt2_conf_uart(struct usb_serial
*serial
, unsigned short Uart_Number
,
1174 unsigned short divisor
, unsigned char LCR
)
1177 unsigned short UartNumandLCR
;
1179 UartNumandLCR
= (LCR
<< 8) + Uart_Number
;
1181 result
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1182 QT_GET_SET_UART
, 0x40, divisor
, UartNumandLCR
,
1187 /** @brief Callback for asynchronous submission of read URBs on bulk in
1190 * Registered in qt2_open_port(), used to deal with incomming data
1193 static void qt2_read_bulk_callback(struct urb
*urb
)
1195 /* Get the device pointer (struct usb_serial) back out of the URB */
1196 struct usb_serial
*serial
= urb
->context
;
1197 /* get the extra struct for the device */
1198 struct quatech2_dev
*dev_extra
= qt2_get_dev_private(serial
);
1199 /* Get first port structure from the device */
1200 struct usb_serial_port
*port0
= serial
->port
[0];
1201 /* Get the currently active port structure from serial struct */
1202 struct usb_serial_port
*active
= dev_extra
->current_port
;
1203 /* get the extra struct for port 0 */
1204 struct quatech2_port
*port0_extra
= qt2_get_port_private(port0
);
1205 /* and for the currently active port */
1206 struct quatech2_port
*active_extra
= qt2_get_port_private(active
);
1207 /* When we finally get to doing some tty stuff, we will need this */
1208 struct tty_struct
*tty_st
;
1209 unsigned int RxCount
; /* the length of the data to process */
1210 unsigned int i
; /* loop counter over the data to process */
1211 int result
; /* return value cache variable */
1212 bool escapeflag
; /* flag set to true if this loop iteration is
1213 * parsing an escape sequence, rather than
1217 dbg("%s(): callback running", __func__
);
1220 /* read didn't go well */
1221 dev_extra
->ReadBulkStopped
= true;
1222 dbg("%s(): nonzero bulk read status received: %d",
1223 __func__
, urb
->status
);
1227 /* inline port_sofrint() here */
1228 if (port_paranoia_check(port0
, __func__
) != 0) {
1229 dbg("%s - port_paranoia_check on port0 failed, exiting\n",
1233 if (port_paranoia_check(active
, __func__
) != 0) {
1234 dbg("%s - port_paranoia_check on current_port "
1235 "failed, exiting", __func__
);
1239 /* This single callback function has to do for all the ports on
1240 * the device. Data being read up the USB can contain certain
1241 * escape sequences which are used to communicate out-of-band
1242 * information from the serial port in-band over the USB.
1243 * These escapes include sending modem and flow control line
1244 * status, and switching the port. The concept of a "Current Port"
1245 * is used, which is where data is going until a port change
1246 * escape seqence is received. This Current Port is kept between
1247 * callbacks so that when this function enters we know which the
1248 * currently active port is and can get to work right away without
1249 * the box having to send repeat escape sequences (anyway, how
1250 * would it know to do so?).
1253 if (active_extra
->close_pending
== true) {
1254 /* We are closing , stop reading */
1255 dbg("%s - (active->close_pending == true", __func__
);
1256 if (dev_extra
->open_ports
<= 0) {
1257 /* If this is the only port left open - stop the
1259 dev_extra
->ReadBulkStopped
= true;
1260 dbg("%s - (ReadBulkStopped == true;", __func__
);
1266 * RxHolding is asserted by throttle, if we assert it, we're not
1267 * receiving any more characters and let the box handle the flow
1270 if ((port0_extra
->RxHolding
== true) &&
1271 (serial
->dev
->descriptor
.idProduct
== QUATECH_SSU2_100
)) {
1272 /* single port device, input is already stopped, so we don't
1273 * need any more input data */
1274 dev_extra
->ReadBulkStopped
= true;
1277 /* finally, we are in a situation where we might consider the data
1278 * that is contained within the URB, and what to do about it.
1279 * This is likely to involved communicating up to the TTY layer, so
1280 * we will need to get hold of the tty for the port we are currently
1283 /* active is a usb_serial_port. It has a member port which is a
1284 * tty_port. From this we get a tty_struct pointer which is what we
1285 * actually wanted, and keep it on tty_st */
1286 tty_st
= tty_port_tty_get(&active
->port
);
1288 dbg("%s - bad tty pointer - exiting", __func__
);
1291 dbg("%s(): active port %d, tty_st =0x%p\n", __func__
, active
->number
,
1293 RxCount
= urb
->actual_length
; /* grab length of data handy */
1296 /* skip all this if no data to process */
1297 for (i
= 0; i
< RxCount
; ++i
) {
1298 /* Look ahead code here -works on several bytes at onc*/
1299 if ((i
<= (RxCount
- 3)) && (THISCHAR
== 0x1b)
1300 && (NEXTCHAR
== 0x1b)) {
1301 /* we are in an escape sequence, type
1302 * determined by the 3rd char */
1304 switch (THIRDCHAR
) {
1306 /* Line status change 4th byte must
1308 if (i
> (RxCount
- 4)) {
1309 dbg("Illegal escape sequences "
1310 "in received data");
1313 qt2_process_line_status(active
,
1319 /* Modem status status change 4th byte
1321 if (i
> (RxCount
- 4)) {
1322 dbg("Illegal escape sequences "
1323 "in received data");
1326 qt2_process_modem_status(active
,
1332 /* xmit hold empty 4th byte
1334 if (i
> (RxCount
- 4)) {
1335 dbg("Illegal escape sequences "
1336 "in received data");
1339 qt2_process_xmit_empty(active
,
1340 FOURTHCHAR
, FIFTHCHAR
);
1345 /* Port number change 4th byte
1347 if (i
> (RxCount
- 4)) {
1348 dbg("Illegal escape sequences "
1349 "in received data");
1352 /* Port change. If port open push
1353 * current data up to tty layer */
1354 if (dev_extra
->open_ports
> 0)
1355 tty_flip_buffer_push(tty_st
);
1357 dbg("Port Change: new port = %d",
1359 qt2_process_port_change(active
,
1363 /* having changed port, the pointers for
1364 * the currently active port are all out
1365 * of date and need updating */
1366 active
= dev_extra
->current_port
;
1368 qt2_get_port_private(active
);
1369 tty_st
= tty_port_tty_get(
1373 /* Recv flush 3rd byte must
1375 if (i
> (RxCount
- 3)) {
1376 dbg("Illegal escape sequences "
1377 "in received data");
1380 qt2_process_rcv_flush(active
);
1385 /* xmit flush 3rd byte must follow */
1386 if (i
> (RxCount
- 3)) {
1387 dbg("Illegal escape sequences "
1388 "in received data");
1391 qt2_process_xmit_flush(active
);
1396 dbg("No status sequence");
1397 qt2_process_rx_char(active
, THISCHAR
);
1398 qt2_process_rx_char(active
, NEXTCHAR
);
1402 qt2_process_rx_char(active
, THISCHAR
);
1406 if (escapeflag
== true)
1408 /* if we did an escape char, we don't need
1409 * to mess around pushing data through the
1410 * tty layer, and can go round again */
1412 if (tty_st
&& urb
->actual_length
) {
1413 tty_buffer_request_room(tty_st
, 1);
1414 tty_insert_flip_string(tty_st
,
1415 &((unsigned char *)(urb
->transfer_buffer
)
1420 tty_flip_buffer_push(tty_st
);
1423 /* at this point we have complete dealing with the data for this
1424 * callback. All we have to do now is to start the async read process
1425 * back off again. */
1427 usb_fill_bulk_urb(port0
->read_urb
, serial
->dev
,
1428 usb_rcvbulkpipe(serial
->dev
, port0
->bulk_in_endpointAddress
),
1429 port0
->bulk_in_buffer
, port0
->bulk_in_size
,
1430 qt2_read_bulk_callback
, serial
);
1431 result
= usb_submit_urb(port0
->read_urb
, GFP_ATOMIC
);
1433 dbg("%s(): failed resubmitting read urb, error %d",
1436 if (tty_st
&& RxCount
) {
1437 /* if some inbound data was processed, then
1438 * we need to push that through the tty layer
1440 tty_flip_buffer_push(tty_st
);
1441 tty_schedule_flip(tty_st
);
1445 /* cribbed from serqt_usb2 driver, but not sure which work needs
1446 * scheduling - port0 or currently active port? */
1447 /* schedule_work(&port->work); */
1452 /** @brief Callback for asynchronous submission of write URBs on bulk in
1455 * Registered in qt2_write(), used to deal with outgoing data
1458 static void qt2_write_bulk_callback(struct urb
*urb
)
1460 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1461 struct usb_serial
*serial
= port
->serial
;
1462 dbg("%s(): port %d", __func__
, port
->number
);
1464 dbg("%s(): bad serial pointer, exiting", __func__
);
1468 dbg("%s(): nonzero write bulk status received: %d",
1469 __func__
, urb
->status
);
1473 /*port_softint((void *) serial); commented in vendor driver */
1474 schedule_work(&port
->work
);
1475 dbg("%s(): port %d exit", __func__
, port
->number
);
1479 static void qt2_process_line_status(struct usb_serial_port
*port
,
1480 unsigned char LineStatus
)
1482 /* obtain the private structure for the port */
1483 struct quatech2_port
*port_extra
= qt2_get_port_private(port
);
1484 port_extra
->shadowLSR
= LineStatus
& (SERIAL_LSR_OE
| SERIAL_LSR_PE
|
1485 SERIAL_LSR_FE
| SERIAL_LSR_BI
);
1487 static void qt2_process_modem_status(struct usb_serial_port
*port
,
1488 unsigned char ModemStatus
)
1490 /* obtain the private structure for the port */
1491 struct quatech2_port
*port_extra
= qt2_get_port_private(port
);
1492 port_extra
->shadowMSR
= ModemStatus
;
1493 wake_up_interruptible(&port_extra
->wait
);
1494 /* this wakes up the otherwise indefinitely waiting code for
1495 * the TIOCMIWAIT ioctl, so that it can notice that
1496 * port_extra->shadowMSR has changed and the ioctl needs to return.
1500 static void qt2_process_xmit_empty(struct usb_serial_port
*port
,
1501 unsigned char fourth_char
, unsigned char fifth_char
)
1504 /* obtain the private structure for the port */
1505 struct quatech2_port
*port_extra
= qt2_get_port_private(port
);
1507 byte_count
= (int)(fifth_char
* 16);
1508 byte_count
+= (int)fourth_char
;
1509 /* byte_count indicates how many bytes the device has written out. This
1510 * message appears to occur regularly, and is used in the vendor driver
1511 * to keep track of the fill state of the port transmit buffer */
1512 port_extra
->tx_pending_bytes
-= byte_count
;
1513 /* reduce the stored data queue length by the known number of bytes
1515 dbg("port %d: %d bytes reported sent, %d still pending", port
->number
,
1516 byte_count
, port_extra
->tx_pending_bytes
);
1518 /*port_extra->xmit_fifo_room_bytes = FIFO_DEPTH; ???*/
1521 static void qt2_process_port_change(struct usb_serial_port
*port
,
1522 unsigned char New_Current_Port
)
1524 /* obtain the parent usb serial device structure */
1525 struct usb_serial
*serial
= port
->serial
;
1526 /* obtain the private structure for the device */
1527 struct quatech2_dev
*dev_extra
= qt2_get_dev_private(serial
);
1528 dev_extra
->current_port
= serial
->port
[New_Current_Port
];
1529 /* what should I do with this? commented out in upstream
1531 /*schedule_work(&port->work);*/
1534 static void qt2_process_rcv_flush(struct usb_serial_port
*port
)
1536 /* obtain the private structure for the port */
1537 struct quatech2_port
*port_extra
= qt2_get_port_private(port
);
1538 port_extra
->rcv_flush
= true;
1540 static void qt2_process_xmit_flush(struct usb_serial_port
*port
)
1542 /* obtain the private structure for the port */
1543 struct quatech2_port
*port_extra
= qt2_get_port_private(port
);
1544 port_extra
->xmit_flush
= true;
1547 static void qt2_process_rx_char(struct usb_serial_port
*port
,
1550 /* get the tty_struct for this port */
1551 struct tty_struct
*tty
= tty_port_tty_get(&(port
->port
));
1552 /* get the URB with the data in to push */
1553 struct urb
*urb
= port
->serial
->port
[0]->read_urb
;
1555 if (tty
&& urb
->actual_length
) {
1556 tty_buffer_request_room(tty
, 1);
1557 tty_insert_flip_string(tty
, &data
, 1);
1558 /* should this be commented out here? */
1559 /*tty_flip_buffer_push(tty);*/
1563 /** @brief Retreive the value of a register from the device
1565 * Issues a GET_REGISTER vendor-spcific request over the USB control
1566 * pipe to obtain a value back from a specific register on a specific
1568 * @param serial Serial device handle to access the device through
1569 * @param uart_number Which UART the value is wanted from
1570 * @param register_num Which register to read the value from
1571 * @param pValue Pointer to somewhere to put the retrieved value
1573 static int qt2_box_get_register(struct usb_serial
*serial
,
1574 unsigned char uart_number
, unsigned short register_num
,
1578 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
1579 QT2_GET_SET_REGISTER
, 0xC0, register_num
,
1580 uart_number
, (void *)pValue
, sizeof(*pValue
), 300);
1584 /** qt2_box_set_register
1585 * Issue a SET_REGISTER vendor-specific request on the default control pipe
1587 static int qt2_box_set_register(struct usb_serial
*serial
,
1588 unsigned short Uart_Number
, unsigned short Register_Num
,
1589 unsigned short Value
)
1592 unsigned short reg_and_byte
;
1594 reg_and_byte
= Value
;
1595 reg_and_byte
= reg_and_byte
<< 8;
1596 reg_and_byte
= reg_and_byte
+ Register_Num
;
1598 result
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1599 QT2_GET_SET_REGISTER
, 0x40, reg_and_byte
,
1600 Uart_Number
, NULL
, 0, 300);
1605 /** @brief Request the Tx or Rx buffers on the USB side be flushed
1607 * Tx flush: When all the currently buffered data has been sent, send an escape
1608 * sequence back up the data stream to us
1609 * Rx flush: add a flag in the data stream now so we know when it's made it's
1612 static int qt2_box_flush(struct usb_serial
*serial
, unsigned char uart_number
,
1613 unsigned short rcv_or_xmit
)
1616 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
1617 QT2_FLUSH_DEVICE
, 0x40, rcv_or_xmit
, uart_number
, NULL
, 0,
1623 * last things in file: stuff to register this driver into the generic
1624 * USB serial framework.
1627 static struct usb_serial_driver quatech2_device
= {
1629 .owner
= THIS_MODULE
,
1630 .name
= "quatech_usb2",
1632 .description
= DRIVER_DESC
,
1633 .usb_driver
= &quausb2_usb_driver
,
1634 .id_table
= quausb2_id_table
,
1639 .write_room
= qt2_write_room
,
1640 .chars_in_buffer
= qt2_chars_in_buffer
,
1641 /*.throttle = qt_throttle,
1642 .unthrottle = qt_unthrottle,*/
1643 .calc_num_ports
= qt2_calc_num_ports
,
1645 /*.set_termios = qt_set_termios,
1646 .break_ctl = qt_break,*/
1647 .tiocmget
= qt2_tiocmget
,
1648 .tiocmset
= qt2_tiocmset
,
1649 .attach
= qt2_attach
,
1650 .release
= qt2_release
,
1651 .read_bulk_callback
= qt2_read_bulk_callback
,
1652 .write_bulk_callback
= qt2_write_bulk_callback
,
1655 static int __init
quausb2_usb_init(void)
1659 dbg("%s\n", __func__
);
1661 /* register with usb-serial */
1662 retval
= usb_serial_register(&quatech2_device
);
1665 goto failed_usb_serial_register
;
1667 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
1670 /* register with usb */
1672 retval
= usb_register(&quausb2_usb_driver
);
1676 /* if we're here, usb_register() failed */
1677 usb_serial_deregister(&quatech2_device
);
1678 failed_usb_serial_register
:
1682 static void __exit
quausb2_usb_exit(void)
1684 usb_deregister(&quausb2_usb_driver
);
1685 usb_serial_deregister(&quatech2_device
);
1688 module_init(quausb2_usb_init
);
1689 module_exit(quausb2_usb_exit
);
1691 MODULE_AUTHOR(DRIVER_AUTHOR
);
1692 MODULE_DESCRIPTION(DRIVER_DESC
);
1693 MODULE_LICENSE("GPL");
1695 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1696 MODULE_PARM_DESC(debug
, "Debug enabled or not");