1
2
3
4
5
6
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>
18
19static int debug;
20
21
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"
25
26
27#define USB_VENDOR_ID_QUATECH 0x061d
28#define QUATECH_SSU2_100 0xC120
29#define QUATECH_DSU2_100 0xC140
30#define QUATECH_DSU2_400 0xC150
31#define QUATECH_QSU2_100 0xC160
32#define QUATECH_QSU2_400 0xC170
33#define QUATECH_ESU2_100 0xC1A0
34#define QUATECH_ESU2_400 0xC180
35
36
37
38#define QU2BOXPWRON 0x8000
39#define QU2BOX232 0x40
40#define QU2BOXSPD9600 0x60
41#define QT2_FIFO_DEPTH 1024
42#define QT2_TX_HEADER_LENGTH 5
43
44
45
46#define USBD_TRANSFER_DIRECTION_IN 0xc0
47#define USBD_TRANSFER_DIRECTION_OUT 0x40
48
49
50
51#define QT_SET_GET_DEVICE 0xc2
52#define QT_OPEN_CLOSE_CHANNEL 0xca
53
54
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
64
65
66#define QT2_FLUSH_RX 0x00
67#define QT2_FLUSH_TX 0x01
68
69
70
71#define QT2_SERIAL_MCR_DTR 0x01
72#define QT2_SERIAL_MCR_RTS 0x02
73#define QT2_SERIAL_MCR_LOOP 0x10
74
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
80
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
85
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
90
91#define QT2_MAX_BAUD_RATE 921600
92#define QT2_MAX_BAUD_REMAINDER 4608
93
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
98
99
100
101#define QT2_LSR_TEMT 0x40
102
103
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
111
112
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]
118
119static 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 {}
128};
129
130MODULE_DEVICE_TABLE(usb, quausb2_id_table);
131
132
133static 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,
138 .no_dynamic_id = 1,
139};
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171struct quatech2_port {
172 int magic;
173 bool read_urb_busy;
174 bool close_pending;
175 __u8 shadowLSR;
176 __u8 shadowMSR;
177 bool rcv_flush;
178 bool xmit_flush;
179 int tx_pending_bytes;
180 struct mutex modelock;
181 int open_count;
182
183 char active;
184 unsigned char *xfer_to_tty_buffer;
185 wait_queue_head_t wait;
186 __u8 shadowLCR;
187 __u8 shadowMCR;
188 char RxHolding;
189 struct semaphore pend_xmit_sem;
190 spinlock_t lock;
191};
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206struct quatech2_dev {
207 bool ReadBulkStopped;
208 char open_ports;
209 struct usb_serial_port *current_port;
210 int buffer_size;
211};
212
213
214struct qt2_status_data {
215 __u8 line_status;
216 __u8 modem_status;
217};
218
219
220static int qt2_boxpoweron(struct usb_serial *serial);
221static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
222 __u8 QMCR_Value);
223static int port_paranoia_check(struct usb_serial_port *port,
224 const char *function);
225static int serial_paranoia_check(struct usb_serial *serial,
226 const char *function);
227static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
228 *port);
229static inline void qt2_set_port_private(struct usb_serial_port *port,
230 struct quatech2_port *data);
231static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
232 *serial);
233static inline void qt2_set_dev_private(struct usb_serial *serial,
234 struct quatech2_dev *data);
235static int qt2_openboxchannel(struct usb_serial *serial, __u16
236 Uart_Number, struct qt2_status_data *pDeviceData);
237static int qt2_closeboxchannel(struct usb_serial *serial, __u16
238 Uart_Number);
239static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number,
240 unsigned short divisor, unsigned char LCR);
241static void qt2_read_bulk_callback(struct urb *urb);
242static void qt2_write_bulk_callback(struct urb *urb);
243static void qt2_process_line_status(struct usb_serial_port *port,
244 unsigned char LineStatus);
245static void qt2_process_modem_status(struct usb_serial_port *port,
246 unsigned char ModemStatus);
247static void qt2_process_xmit_empty(struct usb_serial_port *port,
248 unsigned char fourth_char, unsigned char fifth_char);
249static void qt2_process_port_change(struct usb_serial_port *port,
250 unsigned char New_Current_Port);
251static void qt2_process_rcv_flush(struct usb_serial_port *port);
252static void qt2_process_xmit_flush(struct usb_serial_port *port);
253static void qt2_process_rx_char(struct usb_serial_port *port,
254 unsigned char data);
255static int qt2_box_get_register(struct usb_serial *serial,
256 unsigned char uart_number, unsigned short register_num,
257 __u8 *pValue);
258static int qt2_box_set_register(struct usb_serial *serial,
259 unsigned short Uart_Number, unsigned short Register_Num,
260 unsigned short Value);
261static int qt2_box_flush(struct usb_serial *serial, unsigned char uart_number,
262 unsigned short rcv_or_xmit);
263static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
264 unsigned short default_divisor, unsigned char default_LCR);
265static int qt2_boxsethw_flowctl(struct usb_serial *serial,
266 unsigned int UartNumber, bool bSet);
267static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
268 unsigned char stop_char, unsigned char start_char);
269static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber);
270static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
271 unsigned short stop);
272
273
274static int qt2_calc_num_ports(struct usb_serial *serial)
275{
276 int num_ports;
277 int flag_as_400;
278 switch (serial->dev->descriptor.idProduct) {
279 case QUATECH_SSU2_100:
280 num_ports = 1;
281 break;
282
283 case QUATECH_DSU2_400:
284 flag_as_400 = true;
285 case QUATECH_DSU2_100:
286 num_ports = 2;
287 break;
288
289 case QUATECH_QSU2_400:
290 flag_as_400 = true;
291 case QUATECH_QSU2_100:
292 num_ports = 4;
293 break;
294
295 case QUATECH_ESU2_400:
296 flag_as_400 = true;
297 case QUATECH_ESU2_100:
298 num_ports = 8;
299 break;
300 default:
301 num_ports = 1;
302 break;
303 }
304 return num_ports;
305}
306
307static int qt2_attach(struct usb_serial *serial)
308{
309 struct usb_serial_port *port;
310 struct quatech2_port *qt2_port;
311 struct quatech2_dev *qt2_dev;
312 int i;
313
314 struct usb_endpoint_descriptor *endpoint;
315 struct usb_host_interface *iface_desc;
316 struct usb_serial_port *port0;
317
318
319
320 dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in",
321 __func__, serial->num_bulk_in,
322 serial->num_bulk_out, serial->num_interrupt_in);
323 if ((serial->num_bulk_in != 1) || (serial->num_bulk_out != 1)) {
324 dbg("Device has wrong number of bulk endpoints!");
325 return -ENODEV;
326 }
327 iface_desc = serial->interface->cur_altsetting;
328
329
330
331 qt2_dev = kzalloc(sizeof(*qt2_dev), GFP_KERNEL);
332 if (!qt2_dev) {
333 dbg("%s: kmalloc for quatech2_dev failed!",
334 __func__);
335 return -ENOMEM;
336 }
337 qt2_dev->open_ports = 0;
338 qt2_set_dev_private(serial, qt2_dev);
339
340
341
342 for (i = 0; i < serial->num_ports; i++) {
343 port = serial->port[i];
344 qt2_port = kzalloc(sizeof(*qt2_port), GFP_KERNEL);
345 if (!qt2_port) {
346 dbg("%s: kmalloc for quatech2_port (%d) failed!.",
347 __func__, i);
348 return -ENOMEM;
349 }
350
351 qt2_port->open_count = 0;
352 spin_lock_init(&qt2_port->lock);
353 mutex_init(&qt2_port->modelock);
354 qt2_set_port_private(port, qt2_port);
355 }
356
357
358
359 if (serial_paranoia_check(serial, __func__))
360 return -ENODEV;
361 port0 = serial->port[0];
362
363
364
365 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
366 endpoint = &iface_desc->endpoint[i].desc;
367 if ((endpoint->bEndpointAddress & 0x80) &&
368 ((endpoint->bmAttributes & 3) == 0x02)) {
369
370 dbg("found bulk in at %#.2x",
371 endpoint->bEndpointAddress);
372 }
373
374 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
375 ((endpoint->bmAttributes & 3) == 0x02)) {
376
377 dbg("found bulk out at %#.2x",
378 endpoint->bEndpointAddress);
379 qt2_dev->buffer_size = endpoint->wMaxPacketSize;
380
381 }
382 }
383
384
385 if (qt2_boxpoweron(serial) < 0) {
386 dbg("qt2_boxpoweron() failed");
387 goto startup_error;
388 }
389
390 for (i = 0; i < serial->num_ports; ++i) {
391 if (qt2_boxsetQMCR(serial, i, QU2BOX232) < 0) {
392 dbg("qt2_boxsetQMCR() on port %d failed",
393 i);
394 goto startup_error;
395 }
396 }
397
398 return 0;
399
400startup_error:
401 for (i = 0; i < serial->num_ports; i++) {
402 port = serial->port[i];
403 qt2_port = qt2_get_port_private(port);
404 kfree(qt2_port);
405 qt2_set_port_private(port, NULL);
406 }
407 qt2_dev = qt2_get_dev_private(serial);
408 kfree(qt2_dev);
409 qt2_set_dev_private(serial, NULL);
410
411 dbg("Exit fail %s\n", __func__);
412 return -EIO;
413}
414
415static void qt2_release(struct usb_serial *serial)
416{
417 struct usb_serial_port *port;
418 struct quatech2_port *qt_port;
419 int i;
420
421 dbg("enterting %s", __func__);
422
423 for (i = 0; i < serial->num_ports; i++) {
424 port = serial->port[i];
425 if (!port)
426 continue;
427
428 qt_port = usb_get_serial_port_data(port);
429 kfree(qt_port);
430 usb_set_serial_port_data(port, NULL);
431 }
432}
433
434
435
436
437
438
439
440
441
442
443
444
445int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
446{
447 struct usb_serial *serial;
448 struct usb_serial_port *port0;
449 struct quatech2_port *port_extra;
450 struct quatech2_port *port0_extra;
451 struct quatech2_dev *dev_extra;
452 struct qt2_status_data ChannelData;
453 unsigned short default_divisor = QU2BOXSPD9600;
454 unsigned char default_LCR = QT2_SERIAL_8_DATA;
455 int status;
456 int result;
457
458 if (port_paranoia_check(port, __func__))
459 return -ENODEV;
460
461 dbg("%s(): port %d", __func__, port->number);
462
463 serial = port->serial;
464 if (serial_paranoia_check(serial, __func__)) {
465 dbg("usb_serial struct failed sanity check");
466 return -ENODEV;
467 }
468 dev_extra = qt2_get_dev_private(serial);
469
470 if (dev_extra == NULL) {
471 dbg("device extra data pointer is null");
472 return -ENODEV;
473 }
474 port0 = serial->port[0];
475 if (port_paranoia_check(port0, __func__)) {
476 dbg("port0 usb_serial_port struct failed sanity check");
477 return -ENODEV;
478 }
479
480 port_extra = qt2_get_port_private(port);
481 port0_extra = qt2_get_port_private(port0);
482 if (port_extra == NULL || port0_extra == NULL) {
483 dbg("failed to get private data for port or port0");
484 return -ENODEV;
485 }
486
487
488
489 status = qt2_openboxchannel(serial, port->number,
490 &ChannelData);
491 if (status < 0) {
492 dbg("qt2_openboxchannel on channel %d failed",
493 port->number);
494 return status;
495 }
496 port_extra->shadowLSR = ChannelData.line_status &
497 (QT2_SERIAL_LSR_OE | QT2_SERIAL_LSR_PE |
498 QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
499 port_extra->shadowMSR = ChannelData.modem_status &
500 (QT2_SERIAL_MSR_CTS | QT2_SERIAL_MSR_DSR |
501 QT2_SERIAL_MSR_RI | QT2_SERIAL_MSR_CD);
502
503
504 dbg("qt2_openboxchannel on channel %d completed.",
505 port->number);
506
507
508 status = qt2_conf_uart(serial, port->number, default_divisor,
509 default_LCR);
510 if (status < 0) {
511 dbg("qt2_conf_uart() failed on channel %d",
512 port->number);
513 return status;
514 }
515 dbg("qt2_conf_uart() completed on channel %d",
516 port->number);
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533 dbg("port0 bulk in endpoint is %#.2x", port0->bulk_in_endpointAddress);
534 dbg("port0 bulk out endpoint is %#.2x",
535 port0->bulk_out_endpointAddress);
536
537
538
539
540
541
542
543 if (port->write_urb == NULL) {
544 dbg("port->write_urb == NULL, allocating one");
545 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
546 if (!port->write_urb) {
547 err("Allocating write URB failed");
548 return -ENOMEM;
549 }
550
551 port->bulk_out_size = dev_extra->buffer_size;
552 port->bulk_out_buffer = kmalloc(port->bulk_out_size,
553 GFP_KERNEL);
554 if (!port->bulk_out_buffer) {
555 err("Couldn't allocate bulk_out_buffer");
556 return -ENOMEM;
557 }
558 }
559 if (serial->dev == NULL)
560 dbg("serial->dev == NULL");
561 dbg("port->bulk_out_size is %d", port->bulk_out_size);
562
563 usb_fill_bulk_urb(port->write_urb, serial->dev,
564 usb_sndbulkpipe(serial->dev,
565 port0->bulk_out_endpointAddress),
566 port->bulk_out_buffer,
567 port->bulk_out_size,
568 qt2_write_bulk_callback,
569 port);
570 port_extra->tx_pending_bytes = 0;
571
572 if (dev_extra->open_ports == 0) {
573
574
575
576 usb_fill_bulk_urb(port0->read_urb, serial->dev,
577 usb_rcvbulkpipe(serial->dev,
578 port0->bulk_in_endpointAddress),
579 port0->bulk_in_buffer,
580 port0->bulk_in_size,
581 qt2_read_bulk_callback, serial);
582 dbg("port0 bulk in URB intialised");
583
584
585 dev_extra->ReadBulkStopped = false;
586 port_extra->read_urb_busy = true;
587 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
588 if (result) {
589 dev_err(&port->dev,
590 "%s(): Error %d submitting bulk in urb",
591 __func__, result);
592 port_extra->read_urb_busy = false;
593 dev_extra->ReadBulkStopped = true;
594 }
595
596
597
598
599
600
601 dev_extra->current_port = port;
602 }
603
604
605 init_waitqueue_head(&port_extra->wait);
606
607 port_extra->open_count++;
608
609
610
611 qt2_set_port_private(port, port_extra);
612 qt2_set_port_private(serial->port[0], port0_extra);
613 qt2_set_dev_private(serial, dev_extra);
614
615 dev_extra->open_ports++;
616
617 return 0;
618}
619
620
621
622
623
624
625
626
627
628
629
630
631
632static void qt2_close(struct usb_serial_port *port)
633{
634
635 unsigned long jift;
636 struct quatech2_port *port_extra;
637 struct usb_serial *serial;
638 struct quatech2_dev *dev_extra;
639 __u8 lsr_value = 0;
640 int status;
641
642 dbg("%s(): port %d", __func__, port->number);
643 serial = port->serial;
644 dev_extra = qt2_get_dev_private(serial);
645
646 port_extra = qt2_get_port_private(port);
647
648
649
650
651
652 port_extra->close_pending = true;
653 dbg("%s(): port_extra->close_pending = true", __func__);
654
655
656
657
658
659
660
661 jift = jiffies + (10 * HZ);
662 do {
663 status = qt2_box_get_register(serial, port->number,
664 QT2_LINE_STATUS_REGISTER, &lsr_value);
665 if (status < 0) {
666 dbg("%s(): qt2_box_get_register failed", __func__);
667 break;
668 }
669 if ((lsr_value & QT2_LSR_TEMT)) {
670 dbg("UART done sending");
671 break;
672 }
673 schedule();
674 } while (jiffies <= jift);
675
676 status = qt2_closeboxchannel(serial, port->number);
677 if (status < 0)
678 dbg("%s(): port %d qt2_box_open_close_channel failed",
679 __func__, port->number);
680
681
682
683
684 usb_free_urb(port->write_urb);
685 kfree(port->bulk_out_buffer);
686 port->bulk_out_buffer = NULL;
687 port->bulk_out_size = 0;
688
689
690 port_extra->open_count--;
691
692 dev_extra->open_ports--;
693 dbg("%s(): Exit, dev_extra->open_ports = %d", __func__,
694 dev_extra->open_ports);
695}
696
697
698
699
700
701
702
703static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port,
704 const unsigned char *buf, int count)
705{
706 struct usb_serial *serial;
707 __u8 header_array[5];
708
709 struct quatech2_port *port_extra;
710 int result;
711
712 serial = port->serial;
713 port_extra = qt2_get_port_private(port);
714 if (serial == NULL)
715 return -ENODEV;
716 dbg("%s(): port %d, requested to write %d bytes, %d already pending",
717 __func__, port->number, count, port_extra->tx_pending_bytes);
718
719 if (count <= 0) {
720 dbg("%s(): write request of <= 0 bytes", __func__);
721 return 0;
722 }
723
724
725
726 if ((port->write_urb->status == -EINPROGRESS)) {
727
728 dbg("%s(): already writing, port->write_urb->status == "
729 "-EINPROGRESS", __func__);
730
731 return 0;
732 } else if (port_extra->tx_pending_bytes >= QT2_FIFO_DEPTH) {
733
734
735 dbg("%s(): port transmit buffer is full!", __func__);
736
737 return 0;
738 }
739
740
741
742
743
744
745
746
747
748 if (count > port->bulk_out_size - QT2_TX_HEADER_LENGTH) {
749 count = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
750 dbg("%s(): write request bigger than urb, only accepting "
751 "%d bytes", __func__, count);
752 }
753
754
755
756 if (count > (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes)) {
757 count = QT2_FIFO_DEPTH - port_extra->tx_pending_bytes;
758 dbg("%s(): not enough room in buffer, only accepting %d bytes",
759 __func__, count);
760 }
761
762 header_array[0] = 0x1b;
763 header_array[1] = 0x1b;
764 header_array[2] = (__u8)port->number;
765 header_array[3] = (__u8)count;
766 header_array[4] = (__u8)count >> 8;
767
768 memcpy(port->write_urb->transfer_buffer, header_array,
769 QT2_TX_HEADER_LENGTH);
770
771 memcpy(port->write_urb->transfer_buffer + 5, buf, count);
772
773 dbg("%s(): first data byte to send = %#.2x", __func__, *buf);
774
775
776 usb_fill_bulk_urb(port->write_urb, serial->dev,
777 usb_sndbulkpipe(serial->dev,
778 port->bulk_out_endpointAddress),
779 port->write_urb->transfer_buffer, count + 5,
780 (qt2_write_bulk_callback), port);
781
782 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
783 if (result) {
784
785 result = 0;
786 dbg("%s(): failed submitting write urb, error %d",
787 __func__, result);
788 } else {
789 port_extra->tx_pending_bytes += count;
790 result = count;
791 dbg("%s(): submitted write urb, wrote %d bytes, "
792 "total pending bytes %d",
793 __func__, result, port_extra->tx_pending_bytes);
794 }
795 return result;
796}
797
798
799
800
801
802
803
804
805
806
807static int qt2_write_room(struct tty_struct *tty)
808{
809 struct usb_serial_port *port = tty->driver_data;
810
811 struct quatech2_port *port_extra;
812 int room = 0;
813 port_extra = qt2_get_port_private(port);
814
815 if (port_extra->close_pending == true) {
816 dbg("%s(): port_extra->close_pending == true", __func__);
817 return -ENODEV;
818 }
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834 room = (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes);
835
836 if (room > port->bulk_out_size - QT2_TX_HEADER_LENGTH)
837 room = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
838
839
840 dbg("%s(): port %d: write room is %d", __func__, port->number, room);
841 return room;
842}
843
844static int qt2_chars_in_buffer(struct tty_struct *tty)
845{
846 struct usb_serial_port *port = tty->driver_data;
847
848 struct quatech2_port *port_extra;
849 port_extra = qt2_get_port_private(port);
850
851 dbg("%s(): port %d: chars_in_buffer = %d", __func__,
852 port->number, port_extra->tx_pending_bytes);
853 return port_extra->tx_pending_bytes;
854}
855
856
857
858
859
860static int qt2_ioctl(struct tty_struct *tty, struct file *file,
861 unsigned int cmd, unsigned long arg)
862{
863 struct usb_serial_port *port = tty->driver_data;
864 struct usb_serial *serial = port->serial;
865 __u8 mcr_value;
866 __u8 msr_value;
867 unsigned short prev_msr_value;
868
869
870 struct quatech2_port *port_extra;
871 DECLARE_WAITQUEUE(wait, current);
872
873
874 unsigned int value;
875 unsigned int UartNumber;
876
877 if (serial == NULL)
878 return -ENODEV;
879 UartNumber = tty->index - serial->minor;
880 port_extra = qt2_get_port_private(port);
881
882 dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__,
883 port->number, UartNumber, tty);
884
885 if (cmd == TIOCMBIS || cmd == TIOCMBIC) {
886 if (qt2_box_get_register(port->serial, UartNumber,
887 QT2_MODEM_CONTROL_REGISTER, &mcr_value) < 0)
888 return -ESPIPE;
889 if (copy_from_user(&value, (unsigned int *)arg,
890 sizeof(value)))
891 return -EFAULT;
892
893 switch (cmd) {
894 case TIOCMBIS:
895 if (value & TIOCM_RTS)
896 mcr_value |= QT2_SERIAL_MCR_RTS;
897 if (value & TIOCM_DTR)
898 mcr_value |= QT2_SERIAL_MCR_DTR;
899 if (value & TIOCM_LOOP)
900 mcr_value |= QT2_SERIAL_MCR_LOOP;
901 break;
902 case TIOCMBIC:
903 if (value & TIOCM_RTS)
904 mcr_value &= ~QT2_SERIAL_MCR_RTS;
905 if (value & TIOCM_DTR)
906 mcr_value &= ~QT2_SERIAL_MCR_DTR;
907 if (value & TIOCM_LOOP)
908 mcr_value &= ~QT2_SERIAL_MCR_LOOP;
909 break;
910 default:
911 break;
912 }
913 if (qt2_box_set_register(port->serial, UartNumber,
914 QT2_MODEM_CONTROL_REGISTER, mcr_value) < 0) {
915 return -ESPIPE;
916 } else {
917 port_extra->shadowMCR = mcr_value;
918 return 0;
919 }
920 } else if (cmd == TIOCMIWAIT) {
921 dbg("%s() port %d, cmd == TIOCMIWAIT enter",
922 __func__, port->number);
923 prev_msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
924 while (1) {
925 add_wait_queue(&port_extra->wait, &wait);
926 set_current_state(TASK_INTERRUPTIBLE);
927 schedule();
928 dbg("%s(): port %d, cmd == TIOCMIWAIT here\n",
929 __func__, port->number);
930 remove_wait_queue(&port_extra->wait, &wait);
931
932 if (signal_pending(current))
933 return -ERESTARTSYS;
934 msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
935 if (msr_value == prev_msr_value)
936 return -EIO;
937 if ((arg & TIOCM_RNG &&
938 ((prev_msr_value & QT2_SERIAL_MSR_RI) ==
939 (msr_value & QT2_SERIAL_MSR_RI))) ||
940 (arg & TIOCM_DSR &&
941 ((prev_msr_value & QT2_SERIAL_MSR_DSR) ==
942 (msr_value & QT2_SERIAL_MSR_DSR))) ||
943 (arg & TIOCM_CD &&
944 ((prev_msr_value & QT2_SERIAL_MSR_CD) ==
945 (msr_value & QT2_SERIAL_MSR_CD))) ||
946 (arg & TIOCM_CTS &&
947 ((prev_msr_value & QT2_SERIAL_MSR_CTS) ==
948 (msr_value & QT2_SERIAL_MSR_CTS)))) {
949 return 0;
950 }
951 }
952
953
954
955
956 } else {
957
958 dbg("%s(): No ioctl for that one. port = %d", __func__,
959 port->number);
960 return -ENOIOCTLCMD;
961 }
962}
963
964
965
966static void qt2_set_termios(struct tty_struct *tty,
967 struct usb_serial_port *port, struct ktermios *old_termios)
968{
969 struct usb_serial *serial;
970 int baud, divisor, remainder;
971 unsigned char LCR_change_to = 0;
972 int status;
973 __u16 UartNumber;
974
975 dbg("%s(): port %d", __func__, port->number);
976
977 serial = port->serial;
978
979 UartNumber = port->number;
980
981 if (old_termios && !tty_termios_hw_change(old_termios, tty->termios))
982 return;
983
984 switch (tty->termios->c_cflag) {
985 case CS5:
986 LCR_change_to |= QT2_SERIAL_5_DATA;
987 break;
988 case CS6:
989 LCR_change_to |= QT2_SERIAL_6_DATA;
990 break;
991 case CS7:
992 LCR_change_to |= QT2_SERIAL_7_DATA;
993 break;
994 default:
995 case CS8:
996 LCR_change_to |= QT2_SERIAL_8_DATA;
997 break;
998 }
999
1000
1001 if (tty->termios->c_cflag & PARENB) {
1002 if (tty->termios->c_cflag & PARODD)
1003 LCR_change_to |= QT2_SERIAL_ODD_PARITY;
1004 else
1005 LCR_change_to |= QT2_SERIAL_EVEN_PARITY;
1006 }
1007
1008
1009
1010
1011
1012
1013 tty->termios->c_cflag &= ~CMSPAR;
1014
1015 if (tty->termios->c_cflag & CSTOPB)
1016 LCR_change_to |= QT2_SERIAL_TWO_STOPB;
1017 else
1018 LCR_change_to |= QT2_SERIAL_ONE_STOPB;
1019
1020
1021
1022 baud = tty_get_baud_rate(tty);
1023 if (!baud) {
1024
1025 baud = 9600;
1026 }
1027 dbg("%s(): got baud = %d", __func__, baud);
1028
1029 divisor = QT2_MAX_BAUD_RATE / baud;
1030 remainder = QT2_MAX_BAUD_RATE % baud;
1031
1032 if (((remainder * 2) >= baud) && (baud != 110))
1033 divisor++;
1034 dbg("%s(): setting divisor = %d, QT2_MAX_BAUD_RATE = %d , LCR = %#.2x",
1035 __func__, divisor, QT2_MAX_BAUD_RATE, LCR_change_to);
1036
1037 status = qt2_boxsetuart(serial, UartNumber, (unsigned short) divisor,
1038 LCR_change_to);
1039 if (status < 0) {
1040 dbg("qt2_boxsetuart() failed");
1041 return;
1042 } else {
1043
1044
1045 baud = QT2_MAX_BAUD_RATE / divisor;
1046 tty_encode_baud_rate(tty, baud, baud);
1047 }
1048
1049
1050 if (tty->termios->c_cflag & CRTSCTS) {
1051 dbg("%s(): Enabling HW flow control port %d", __func__,
1052 port->number);
1053
1054 status = qt2_boxsethw_flowctl(serial, UartNumber, true);
1055 if (status < 0) {
1056 dbg("qt2_boxsethw_flowctl() failed");
1057 return;
1058 }
1059 } else {
1060
1061 dbg("%s(): disabling HW flow control port %d", __func__,
1062 port->number);
1063 status = qt2_boxsethw_flowctl(serial, UartNumber, false);
1064 if (status < 0) {
1065 dbg("qt2_boxsethw_flowctl failed");
1066 return;
1067 }
1068 }
1069
1070
1071 if (I_IXOFF(tty) || I_IXON(tty)) {
1072 unsigned char stop_char = STOP_CHAR(tty);
1073 unsigned char start_char = START_CHAR(tty);
1074 status = qt2_boxsetsw_flowctl(serial, UartNumber, stop_char,
1075 start_char);
1076 if (status < 0)
1077 dbg("qt2_boxsetsw_flowctl (enabled) failed");
1078 } else {
1079
1080 status = qt2_boxunsetsw_flowctl(serial, UartNumber);
1081 if (status < 0)
1082 dbg("qt2_boxunsetsw_flowctl (disabling) failed");
1083 }
1084}
1085
1086static int qt2_tiocmget(struct tty_struct *tty, struct file *file)
1087{
1088 struct usb_serial_port *port = tty->driver_data;
1089 struct usb_serial *serial = port->serial;
1090
1091 __u8 mcr_value;
1092 __u8 msr_value;
1093 unsigned int result = 0;
1094 int status;
1095 unsigned int UartNumber;
1096
1097 if (serial == NULL)
1098 return -ENODEV;
1099
1100 dbg("%s(): port %d, tty =0x%p", __func__, port->number, tty);
1101 UartNumber = tty->index - serial->minor;
1102 dbg("UartNumber is %d", UartNumber);
1103
1104 status = qt2_box_get_register(port->serial, UartNumber,
1105 QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1106 if (status >= 0) {
1107 status = qt2_box_get_register(port->serial, UartNumber,
1108 QT2_MODEM_STATUS_REGISTER, &msr_value);
1109 }
1110 if (status >= 0) {
1111 result = ((mcr_value & QT2_SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1112
1113 | ((mcr_value & QT2_SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1114
1115 | ((msr_value & QT2_SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1116
1117 | ((msr_value & QT2_SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1118
1119 | ((msr_value & QT2_SERIAL_MSR_RI) ? TIOCM_RI : 0)
1120
1121 | ((msr_value & QT2_SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1122
1123 return result;
1124 } else {
1125 return -ESPIPE;
1126 }
1127}
1128
1129static int qt2_tiocmset(struct tty_struct *tty, struct file *file,
1130 unsigned int set, unsigned int clear)
1131{
1132 struct usb_serial_port *port = tty->driver_data;
1133 struct usb_serial *serial = port->serial;
1134 __u8 mcr_value;
1135 int status;
1136 unsigned int UartNumber;
1137
1138 if (serial == NULL)
1139 return -ENODEV;
1140
1141 UartNumber = tty->index - serial->minor;
1142 dbg("%s(): port %d, UartNumber %d", __func__, port->number, UartNumber);
1143
1144 status = qt2_box_get_register(port->serial, UartNumber,
1145 QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1146 if (status < 0)
1147 return -ESPIPE;
1148
1149
1150
1151 mcr_value &= ~(QT2_SERIAL_MCR_RTS | QT2_SERIAL_MCR_DTR |
1152 QT2_SERIAL_MCR_LOOP);
1153 if (set & TIOCM_RTS)
1154 mcr_value |= QT2_SERIAL_MCR_RTS;
1155 if (set & TIOCM_DTR)
1156 mcr_value |= QT2_SERIAL_MCR_DTR;
1157 if (set & TIOCM_LOOP)
1158 mcr_value |= QT2_SERIAL_MCR_LOOP;
1159
1160 status = qt2_box_set_register(port->serial, UartNumber,
1161 QT2_MODEM_CONTROL_REGISTER, mcr_value);
1162 if (status < 0)
1163 return -ESPIPE;
1164 else
1165 return 0;
1166}
1167
1168
1169
1170static void qt2_break(struct tty_struct *tty, int break_state)
1171{
1172 struct usb_serial_port *port = tty->driver_data;
1173 struct usb_serial *serial = port->serial;
1174 struct quatech2_port *port_extra;
1175 __u16 break_value;
1176 unsigned int result;
1177
1178 port_extra = qt2_get_port_private(port);
1179 if (!serial) {
1180 dbg("%s(): port %d: no serial object", __func__, port->number);
1181 return;
1182 }
1183
1184 if (break_state == -1)
1185 break_value = 1;
1186 else
1187 break_value = 0;
1188 dbg("%s(): port %d, break_value %d", __func__, port->number,
1189 break_value);
1190
1191 mutex_lock(&port_extra->modelock);
1192 if (!port_extra->open_count) {
1193 dbg("%s(): port not open", __func__);
1194 goto exit;
1195 }
1196
1197 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1198 QT2_BREAK_CONTROL, 0x40, break_value,
1199 port->number, NULL, 0, 300);
1200exit:
1201 mutex_unlock(&port_extra->modelock);
1202 dbg("%s(): exit port %d", __func__, port->number);
1203
1204}
1205
1206
1207
1208static void qt2_throttle(struct tty_struct *tty)
1209{
1210 struct usb_serial_port *port = tty->driver_data;
1211 struct usb_serial *serial = port->serial;
1212 struct quatech2_port *port_extra;
1213 dbg("%s(): port %d", __func__, port->number);
1214
1215 port_extra = qt2_get_port_private(port);
1216 if (!serial) {
1217 dbg("%s(): enter port %d no serial object", __func__,
1218 port->number);
1219 return;
1220 }
1221
1222 mutex_lock(&port_extra->modelock);
1223 if (!port_extra->open_count) {
1224 dbg("%s(): port not open", __func__);
1225 goto exit;
1226 }
1227
1228
1229
1230
1231
1232 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100)
1233 qt2_boxstoprx(serial, port->number, 1);
1234
1235 port->throttled = 1;
1236exit:
1237 mutex_unlock(&port_extra->modelock);
1238 dbg("%s(): port %d: setting port->throttled", __func__, port->number);
1239 return;
1240}
1241
1242
1243
1244
1245
1246static void qt2_unthrottle(struct tty_struct *tty)
1247{
1248 struct usb_serial_port *port = tty->driver_data;
1249 struct usb_serial *serial = port->serial;
1250 struct quatech2_port *port_extra;
1251 struct usb_serial_port *port0;
1252 struct quatech2_dev *dev_extra;
1253
1254 if (!serial) {
1255 dbg("%s() enter port %d no serial object!", __func__,
1256 port->number);
1257 return;
1258 }
1259 dbg("%s(): enter port %d", __func__, port->number);
1260 dev_extra = qt2_get_dev_private(serial);
1261 port_extra = qt2_get_port_private(port);
1262 port0 = serial->port[0];
1263
1264 mutex_lock(&port_extra->modelock);
1265 if (!port_extra->open_count) {
1266 dbg("%s(): port %d not open", __func__, port->number);
1267 goto exit;
1268 }
1269
1270 if (port->throttled != 0) {
1271 dbg("%s(): port %d: unsetting port->throttled", __func__,
1272 port->number);
1273 port->throttled = 0;
1274
1275 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) {
1276 qt2_boxstoprx(serial, port->number, 0);
1277 } else if (dev_extra->ReadBulkStopped == true) {
1278 usb_fill_bulk_urb(port0->read_urb, serial->dev,
1279 usb_rcvbulkpipe(serial->dev,
1280 port0->bulk_in_endpointAddress),
1281 port0->bulk_in_buffer,
1282 port0->bulk_in_size,
1283 qt2_read_bulk_callback,
1284 serial);
1285 }
1286 }
1287exit:
1288 mutex_unlock(&port_extra->modelock);
1289 dbg("%s(): exit port %d", __func__, port->number);
1290 return;
1291}
1292
1293
1294
1295
1296static int qt2_boxpoweron(struct usb_serial *serial)
1297{
1298 int result;
1299 __u8 Direcion;
1300 unsigned int pipe;
1301 Direcion = USBD_TRANSFER_DIRECTION_OUT;
1302 pipe = usb_rcvctrlpipe(serial->dev, 0);
1303 result = usb_control_msg(serial->dev, pipe, QT_SET_GET_DEVICE,
1304 Direcion, QU2BOXPWRON, 0x00, NULL, 0x00,
1305 5000);
1306 return result;
1307}
1308
1309
1310
1311
1312
1313
1314static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
1315 __u8 QMCR_Value)
1316{
1317 int result;
1318 __u16 PortSettings;
1319
1320 PortSettings = (__u16)(QMCR_Value);
1321
1322 dbg("%s(): Port = %d, PortSettings = 0x%x", __func__,
1323 Uart_Number, PortSettings);
1324
1325 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1326 QT2_GET_SET_QMCR, 0x40, PortSettings,
1327 (__u16)Uart_Number, NULL, 0, 5000);
1328 return result;
1329}
1330
1331static int port_paranoia_check(struct usb_serial_port *port,
1332 const char *function)
1333{
1334 if (!port) {
1335 dbg("%s - port == NULL", function);
1336 return -1;
1337 }
1338 if (!port->serial) {
1339 dbg("%s - port->serial == NULL\n", function);
1340 return -1;
1341 }
1342 return 0;
1343}
1344
1345static int serial_paranoia_check(struct usb_serial *serial,
1346 const char *function)
1347{
1348 if (!serial) {
1349 dbg("%s - serial == NULL\n", function);
1350 return -1;
1351 }
1352
1353 if (!serial->type) {
1354 dbg("%s - serial->type == NULL!", function);
1355 return -1;
1356 }
1357
1358 return 0;
1359}
1360
1361static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
1362 *port)
1363{
1364 return (struct quatech2_port *)usb_get_serial_port_data(port);
1365}
1366
1367static inline void qt2_set_port_private(struct usb_serial_port *port,
1368 struct quatech2_port *data)
1369{
1370 usb_set_serial_port_data(port, (void *)data);
1371}
1372
1373static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
1374 *serial)
1375{
1376 return (struct quatech2_dev *)usb_get_serial_data(serial);
1377}
1378static inline void qt2_set_dev_private(struct usb_serial *serial,
1379 struct quatech2_dev *data)
1380{
1381 usb_set_serial_data(serial, (void *)data);
1382}
1383
1384static int qt2_openboxchannel(struct usb_serial *serial, __u16
1385 Uart_Number, struct qt2_status_data *status)
1386{
1387 int result;
1388 __u16 length;
1389 __u8 Direcion;
1390 unsigned int pipe;
1391 length = sizeof(struct qt2_status_data);
1392 Direcion = USBD_TRANSFER_DIRECTION_IN;
1393 pipe = usb_rcvctrlpipe(serial->dev, 0);
1394 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1395 Direcion, 0x00, Uart_Number, status, length, 5000);
1396 return result;
1397}
1398static int qt2_closeboxchannel(struct usb_serial *serial, __u16 Uart_Number)
1399{
1400 int result;
1401 __u8 direcion;
1402 unsigned int pipe;
1403 direcion = USBD_TRANSFER_DIRECTION_OUT;
1404 pipe = usb_sndctrlpipe(serial->dev, 0);
1405 result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1406 direcion, 0, Uart_Number, NULL, 0, 5000);
1407 return result;
1408}
1409
1410
1411
1412
1413static int qt2_conf_uart(struct usb_serial *serial, unsigned short Uart_Number,
1414 unsigned short divisor, unsigned char LCR)
1415{
1416 int result;
1417 unsigned short UartNumandLCR;
1418
1419 UartNumandLCR = (LCR << 8) + Uart_Number;
1420
1421 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1422 QT2_GET_SET_UART, 0x40, divisor, UartNumandLCR,
1423 NULL, 0, 300);
1424 return result;
1425}
1426
1427
1428
1429
1430
1431
1432
1433static void qt2_read_bulk_callback(struct urb *urb)
1434{
1435
1436 struct usb_serial *serial = urb->context;
1437
1438 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1439
1440 struct usb_serial_port *port0 = serial->port[0];
1441
1442 struct usb_serial_port *active = dev_extra->current_port;
1443
1444 struct quatech2_port *port0_extra = qt2_get_port_private(port0);
1445
1446 struct quatech2_port *active_extra = qt2_get_port_private(active);
1447
1448 struct tty_struct *tty_st;
1449 unsigned int RxCount;
1450 unsigned int i;
1451 int result;
1452 bool escapeflag;
1453
1454
1455 dbg("%s(): callback running, active port is %d", __func__,
1456 active->number);
1457
1458 if (urb->status) {
1459
1460 dev_extra->ReadBulkStopped = true;
1461 dbg("%s(): nonzero bulk read status received: %d",
1462 __func__, urb->status);
1463 return;
1464 }
1465
1466
1467 if (port_paranoia_check(port0, __func__) != 0) {
1468 dbg("%s - port_paranoia_check on port0 failed, exiting\n",
1469__func__);
1470 return;
1471 }
1472 if (port_paranoia_check(active, __func__) != 0) {
1473 dbg("%s - port_paranoia_check on current_port "
1474 "failed, exiting", __func__);
1475 return;
1476 }
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492 if (active_extra->close_pending == true) {
1493
1494 dbg("%s - (active->close_pending == true", __func__);
1495 if (dev_extra->open_ports <= 0) {
1496
1497
1498 dev_extra->ReadBulkStopped = true;
1499 dbg("%s - (ReadBulkStopped == true;", __func__);
1500 return;
1501 }
1502 }
1503
1504
1505
1506
1507
1508
1509 if ((port0_extra->RxHolding == true) &&
1510 (serial->dev->descriptor.idProduct == QUATECH_SSU2_100)) {
1511
1512
1513 dev_extra->ReadBulkStopped = true;
1514 return;
1515 }
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525 tty_st = tty_port_tty_get(&active->port);
1526 if (!tty_st) {
1527 dbg("%s - bad tty pointer - exiting", __func__);
1528 return;
1529 }
1530 RxCount = urb->actual_length;
1531
1532 if (RxCount) {
1533
1534 for (i = 0; i < RxCount ; ++i) {
1535
1536 if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b)
1537 && (NEXTCHAR == 0x1b)) {
1538
1539
1540 escapeflag = false;
1541 switch (THIRDCHAR) {
1542 case 0x00:
1543
1544
1545 if (i > (RxCount - 4)) {
1546 dbg("Illegal escape sequences "
1547 "in received data");
1548 break;
1549 }
1550 qt2_process_line_status(active,
1551 FOURTHCHAR);
1552 i += 3;
1553 escapeflag = true;
1554 break;
1555 case 0x01:
1556
1557
1558 if (i > (RxCount - 4)) {
1559 dbg("Illegal escape sequences "
1560 "in received data");
1561 break;
1562 }
1563 qt2_process_modem_status(active,
1564 FOURTHCHAR);
1565 i += 3;
1566 escapeflag = true;
1567 break;
1568 case 0x02:
1569
1570
1571 if (i > (RxCount - 4)) {
1572 dbg("Illegal escape sequences "
1573 "in received data");
1574 break;
1575 }
1576 qt2_process_xmit_empty(active,
1577 FOURTHCHAR, FIFTHCHAR);
1578 i += 4;
1579 escapeflag = true;
1580 break;
1581 case 0x03:
1582
1583
1584 if (i > (RxCount - 4)) {
1585 dbg("Illegal escape sequences "
1586 "in received data");
1587 break;
1588 }
1589
1590
1591 if (active_extra->open_count > 0)
1592 tty_flip_buffer_push(tty_st);
1593
1594 dbg("Port Change: new port = %d",
1595 FOURTHCHAR);
1596 qt2_process_port_change(active,
1597 FOURTHCHAR);
1598 i += 3;
1599 escapeflag = true;
1600
1601
1602
1603 active = dev_extra->current_port;
1604 active_extra =
1605 qt2_get_port_private(active);
1606 tty_st = tty_port_tty_get(
1607 &active->port);
1608 break;
1609 case 0x04:
1610
1611
1612 if (i > (RxCount - 3)) {
1613 dbg("Illegal escape sequences "
1614 "in received data");
1615 break;
1616 }
1617 qt2_process_rcv_flush(active);
1618 i += 2;
1619 escapeflag = true;
1620 break;
1621 case 0x05:
1622
1623 if (i > (RxCount - 3)) {
1624 dbg("Illegal escape sequences "
1625 "in received data");
1626 break;
1627 }
1628 qt2_process_xmit_flush(active);
1629 i += 2;
1630 escapeflag = true;
1631 break;
1632 case 0xff:
1633 dbg("No status sequence");
1634 qt2_process_rx_char(active, THISCHAR);
1635 qt2_process_rx_char(active, NEXTCHAR);
1636 i += 2;
1637 break;
1638 default:
1639 qt2_process_rx_char(active, THISCHAR);
1640 i += 1;
1641 break;
1642 }
1643 if (escapeflag == true)
1644 continue;
1645
1646
1647
1648 }
1649 if (tty_st && urb->actual_length) {
1650 tty_buffer_request_room(tty_st, 1);
1651 tty_insert_flip_string(tty_st,
1652 &((unsigned char *)(urb->transfer_buffer)
1653 )[i],
1654 1);
1655 }
1656 }
1657 tty_flip_buffer_push(tty_st);
1658 }
1659
1660
1661
1662
1663
1664 usb_fill_bulk_urb(port0->read_urb, serial->dev,
1665 usb_rcvbulkpipe(serial->dev, port0->bulk_in_endpointAddress),
1666 port0->bulk_in_buffer, port0->bulk_in_size,
1667 qt2_read_bulk_callback, serial);
1668 result = usb_submit_urb(port0->read_urb, GFP_ATOMIC);
1669 if (result) {
1670 dbg("%s(): failed resubmitting read urb, error %d",
1671 __func__, result);
1672 } else {
1673 dbg("%s() sucessfully resumitted read urb", __func__);
1674 if (tty_st && RxCount) {
1675
1676
1677
1678 tty_flip_buffer_push(tty_st);
1679 tty_schedule_flip(tty_st);
1680 }
1681 }
1682
1683
1684
1685
1686 dbg("%s() completed", __func__);
1687 return;
1688}
1689
1690
1691
1692
1693
1694
1695
1696static void qt2_write_bulk_callback(struct urb *urb)
1697{
1698 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1699 struct usb_serial *serial = port->serial;
1700 dbg("%s(): port %d", __func__, port->number);
1701 if (!serial) {
1702 dbg("%s(): bad serial pointer, exiting", __func__);
1703 return;
1704 }
1705 if (urb->status) {
1706 dbg("%s(): nonzero write bulk status received: %d",
1707 __func__, urb->status);
1708 return;
1709 }
1710
1711
1712
1713
1714 schedule_work(&port->work);
1715 dbg("%s(): port %d exit", __func__, port->number);
1716 return;
1717}
1718
1719static void qt2_process_line_status(struct usb_serial_port *port,
1720 unsigned char LineStatus)
1721{
1722
1723 struct quatech2_port *port_extra = qt2_get_port_private(port);
1724 port_extra->shadowLSR = LineStatus & (QT2_SERIAL_LSR_OE |
1725 QT2_SERIAL_LSR_PE | QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
1726}
1727static void qt2_process_modem_status(struct usb_serial_port *port,
1728 unsigned char ModemStatus)
1729{
1730
1731 struct quatech2_port *port_extra = qt2_get_port_private(port);
1732 port_extra->shadowMSR = ModemStatus;
1733 wake_up_interruptible(&port_extra->wait);
1734
1735
1736
1737
1738}
1739
1740static void qt2_process_xmit_empty(struct usb_serial_port *port,
1741 unsigned char fourth_char, unsigned char fifth_char)
1742{
1743 int byte_count;
1744
1745 struct quatech2_port *port_extra = qt2_get_port_private(port);
1746
1747 byte_count = (int)(fifth_char * 16);
1748 byte_count += (int)fourth_char;
1749
1750
1751
1752 port_extra->tx_pending_bytes -= byte_count;
1753
1754
1755 dbg("port %d: %d bytes reported sent, %d still pending", port->number,
1756 byte_count, port_extra->tx_pending_bytes);
1757
1758
1759}
1760
1761static void qt2_process_port_change(struct usb_serial_port *port,
1762 unsigned char New_Current_Port)
1763{
1764
1765 struct usb_serial *serial = port->serial;
1766
1767 struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1768 dev_extra->current_port = serial->port[New_Current_Port];
1769
1770
1771
1772}
1773
1774static void qt2_process_rcv_flush(struct usb_serial_port *port)
1775{
1776
1777 struct quatech2_port *port_extra = qt2_get_port_private(port);
1778 port_extra->rcv_flush = true;
1779}
1780static void qt2_process_xmit_flush(struct usb_serial_port *port)
1781{
1782
1783 struct quatech2_port *port_extra = qt2_get_port_private(port);
1784 port_extra->xmit_flush = true;
1785}
1786
1787static void qt2_process_rx_char(struct usb_serial_port *port,
1788 unsigned char data)
1789{
1790
1791 struct tty_struct *tty = tty_port_tty_get(&(port->port));
1792
1793 struct urb *urb = port->serial->port[0]->read_urb;
1794
1795 if (tty && urb->actual_length) {
1796 tty_buffer_request_room(tty, 1);
1797 tty_insert_flip_string(tty, &data, 1);
1798
1799
1800 }
1801}
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813static int qt2_box_get_register(struct usb_serial *serial,
1814 unsigned char uart_number, unsigned short register_num,
1815 __u8 *pValue)
1816{
1817 int result;
1818 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1819 QT2_GET_SET_REGISTER, 0xC0, register_num,
1820 uart_number, (void *)pValue, sizeof(*pValue), 300);
1821 return result;
1822}
1823
1824
1825
1826
1827static int qt2_box_set_register(struct usb_serial *serial,
1828 unsigned short Uart_Number, unsigned short Register_Num,
1829 unsigned short Value)
1830{
1831 int result;
1832 unsigned short reg_and_byte;
1833
1834 reg_and_byte = Value;
1835 reg_and_byte = reg_and_byte << 8;
1836 reg_and_byte = reg_and_byte + Register_Num;
1837
1838 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1839 QT2_GET_SET_REGISTER, 0x40, reg_and_byte,
1840 Uart_Number, NULL, 0, 300);
1841 return result;
1842}
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852static int qt2_box_flush(struct usb_serial *serial, unsigned char uart_number,
1853 unsigned short rcv_or_xmit)
1854{
1855 int result;
1856 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1857 QT2_FLUSH_DEVICE, 0x40, rcv_or_xmit, uart_number, NULL, 0,
1858 300);
1859 return result;
1860}
1861
1862
1863
1864
1865static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
1866 unsigned short default_divisor, unsigned char default_LCR)
1867{
1868 unsigned short UartNumandLCR;
1869
1870 UartNumandLCR = (default_LCR << 8) + Uart_Number;
1871
1872 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1873 QT2_GET_SET_UART, 0x40, default_divisor, UartNumandLCR,
1874 NULL, 0, 300);
1875}
1876
1877
1878
1879static int qt2_boxsethw_flowctl(struct usb_serial *serial,
1880 unsigned int UartNumber, bool bSet)
1881{
1882 __u8 MCR_Value = 0;
1883 __u8 MSR_Value = 0;
1884 __u16 MOUT_Value = 0;
1885
1886 if (bSet == true) {
1887 MCR_Value = QT2_SERIAL_MCR_RTS;
1888
1889
1890 } else {
1891
1892 MCR_Value = 0;
1893 }
1894 MOUT_Value = MCR_Value << 8;
1895
1896 if (bSet == true) {
1897 MSR_Value = QT2_SERIAL_MSR_CTS;
1898
1899
1900 } else {
1901
1902 MSR_Value = 0;
1903 }
1904 MOUT_Value |= MSR_Value;
1905 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1906 QT2_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, UartNumber,
1907 NULL, 0, 300);
1908}
1909
1910
1911
1912
1913static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
1914 unsigned char stop_char, unsigned char start_char)
1915{
1916 __u16 nSWflowout;
1917
1918 nSWflowout = start_char << 8;
1919 nSWflowout = (unsigned short)stop_char;
1920 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1921 QT2_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, UartNumber,
1922 NULL, 0, 300);
1923}
1924
1925
1926
1927
1928static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber)
1929{
1930 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1931 QT2_SW_FLOW_CONTROL_DISABLE, 0x40, 0, UartNumber, NULL,
1932 0, 300);
1933}
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
1944 unsigned short stop)
1945{
1946 return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1947 QT2_STOP_RECEIVE, 0x40, stop, uart_number, NULL, 0, 300);
1948}
1949
1950
1951
1952
1953
1954
1955
1956static struct usb_serial_driver quatech2_device = {
1957 .driver = {
1958 .owner = THIS_MODULE,
1959 .name = "quatech_usb2",
1960 },
1961 .description = DRIVER_DESC,
1962 .usb_driver = &quausb2_usb_driver,
1963 .id_table = quausb2_id_table,
1964 .num_ports = 8,
1965 .open = qt2_open,
1966 .close = qt2_close,
1967 .write = qt2_write,
1968 .write_room = qt2_write_room,
1969 .chars_in_buffer = qt2_chars_in_buffer,
1970 .throttle = qt2_throttle,
1971 .unthrottle = qt2_unthrottle,
1972 .calc_num_ports = qt2_calc_num_ports,
1973 .ioctl = qt2_ioctl,
1974 .set_termios = qt2_set_termios,
1975 .break_ctl = qt2_break,
1976 .tiocmget = qt2_tiocmget,
1977 .tiocmset = qt2_tiocmset,
1978 .attach = qt2_attach,
1979 .release = qt2_release,
1980 .read_bulk_callback = qt2_read_bulk_callback,
1981 .write_bulk_callback = qt2_write_bulk_callback,
1982};
1983
1984static int __init quausb2_usb_init(void)
1985{
1986 int retval;
1987
1988 dbg("%s\n", __func__);
1989
1990
1991 retval = usb_serial_register(&quatech2_device);
1992
1993 if (retval)
1994 goto failed_usb_serial_register;
1995
1996 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1997 DRIVER_DESC "\n");
1998
1999
2000
2001 retval = usb_register(&quausb2_usb_driver);
2002 if (retval == 0)
2003 return 0;
2004
2005
2006 usb_serial_deregister(&quatech2_device);
2007failed_usb_serial_register:
2008 return retval;
2009}
2010
2011static void __exit quausb2_usb_exit(void)
2012{
2013 usb_deregister(&quausb2_usb_driver);
2014 usb_serial_deregister(&quatech2_device);
2015}
2016
2017module_init(quausb2_usb_init);
2018module_exit(quausb2_usb_exit);
2019
2020MODULE_AUTHOR(DRIVER_AUTHOR);
2021MODULE_DESCRIPTION(DRIVER_DESC);
2022MODULE_LICENSE("GPL");
2023
2024module_param(debug, bool, S_IRUGO | S_IWUSR);
2025MODULE_PARM_DESC(debug, "Debug enabled or not");
2026