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