Merge tag 'staging-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / serqt_usb2 / serqt_usb2.c
1 /*
2 * This code was developed for the Quatech USB line for linux, it used
3 * much of the code developed by Greg Kroah-Hartman for USB serial devices
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
19 static bool debug;
20
21 /* Version Information */
22 #define DRIVER_VERSION "v2.14"
23 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
24 #define DRIVER_DESC "Quatech USB to Serial Driver"
25
26 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
27 #define QUATECH_SSU200 0xC030 /* SSU200 */
28 #define QUATECH_DSU100 0xC040 /* DSU100 */
29 #define QUATECH_DSU200 0xC050 /* DSU200 */
30 #define QUATECH_QSU100 0xC060 /* QSU100 */
31 #define QUATECH_QSU200 0xC070 /* QSU200 */
32 #define QUATECH_ESU100A 0xC080 /* ESU100A */
33 #define QUATECH_ESU100B 0xC081 /* ESU100B */
34 #define QUATECH_ESU200A 0xC0A0 /* ESU200A */
35 #define QUATECH_ESU200B 0xC0A1 /* ESU200B */
36 #define QUATECH_HSU100A 0xC090 /* HSU100A */
37 #define QUATECH_HSU100B 0xC091 /* HSU100B */
38 #define QUATECH_HSU100C 0xC092 /* HSU100C */
39 #define QUATECH_HSU100D 0xC093 /* HSU100D */
40 #define QUATECH_HSU200A 0xC0B0 /* HSU200A */
41 #define QUATECH_HSU200B 0xC0B1 /* HSU200B */
42 #define QUATECH_HSU200C 0xC0B2 /* HSU200C */
43 #define QUATECH_HSU200D 0xC0B3 /* HSU200D */
44
45 #define QT_SET_GET_DEVICE 0xc2
46 #define QT_OPEN_CLOSE_CHANNEL 0xca
47 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
48 #define QT_SET_ATF 0xcd
49 #define QT_GET_SET_REGISTER 0xc0
50 #define QT_GET_SET_UART 0xc1
51 #define QT_HW_FLOW_CONTROL_MASK 0xc5
52 #define QT_SW_FLOW_CONTROL_MASK 0xc6
53 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
54 #define QT_BREAK_CONTROL 0xc8
55
56 #define USBD_TRANSFER_DIRECTION_IN 0xc0
57 #define USBD_TRANSFER_DIRECTION_OUT 0x40
58
59 #define MAX_BAUD_RATE 460800
60 #define MAX_BAUD_REMAINDER 4608
61
62 #define DIV_LATCH_LS 0x00
63 #define XMT_HOLD_REGISTER 0x00
64 #define XVR_BUFFER_REGISTER 0x00
65 #define DIV_LATCH_MS 0x01
66 #define FIFO_CONTROL_REGISTER 0x02
67 #define LINE_CONTROL_REGISTER 0x03
68 #define MODEM_CONTROL_REGISTER 0x04
69 #define LINE_STATUS_REGISTER 0x05
70 #define MODEM_STATUS_REGISTER 0x06
71
72 #define SERIAL_MCR_DTR 0x01
73 #define SERIAL_MCR_RTS 0x02
74 #define SERIAL_MCR_LOOP 0x10
75
76 #define SERIAL_MSR_CTS 0x10
77 #define SERIAL_MSR_CD 0x80
78 #define SERIAL_MSR_RI 0x40
79 #define SERIAL_MSR_DSR 0x20
80 #define SERIAL_MSR_MASK 0xf0
81
82 #define SERIAL_8_DATA 0x03
83 #define SERIAL_7_DATA 0x02
84 #define SERIAL_6_DATA 0x01
85 #define SERIAL_5_DATA 0x00
86
87 #define SERIAL_ODD_PARITY 0X08
88 #define SERIAL_EVEN_PARITY 0X18
89 #define SERIAL_TWO_STOPB 0x04
90 #define SERIAL_ONE_STOPB 0x00
91
92 #define DEFAULT_DIVISOR 0x30 /* gives 9600 baud rate */
93 #define DEFAULT_LCR SERIAL_8_DATA /* 8, none , 1 */
94
95 #define FULLPWRBIT 0x00000080
96 #define NEXT_BOARD_POWER_BIT 0x00000004
97
98 #define SERIAL_LSR_OE 0x02
99 #define SERIAL_LSR_PE 0x04
100 #define SERIAL_LSR_FE 0x08
101 #define SERIAL_LSR_BI 0x10
102
103 #define SERIAL_MSR_CTS 0x10
104 #define SERIAL_MSR_CD 0x80
105 #define SERIAL_MSR_RI 0x40
106 #define SERIAL_MSR_DSR 0x20
107 #define SERIAL_MSR_MASK 0xf0
108
109 #define PREFUFF_LEVEL_CONSERVATIVE 128
110 #define ATC_DISABLED 0x0
111
112 #define RR_BITS 0x03 /* for clearing clock bits */
113 #define DUPMODE_BITS 0xc0
114 #define CLKS_X4 0x02
115
116 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
117 #define ALL_LOOPBACK 0x01
118 #define MODEM_CTRL 0x40
119 #define RS232_MODE 0x00
120
121 static const struct usb_device_id id_table[] = {
122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
127 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
128 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
129 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
130 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
131 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
132 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
133 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
134 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
135 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
136 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
137 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
138 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
139 {} /* Terminating entry */
140 };
141 MODULE_DEVICE_TABLE(usb, id_table);
142
143 struct qt_get_device_data {
144 __u8 porta;
145 __u8 portb;
146 __u8 portc;
147 };
148
149 struct qt_open_channel_data {
150 __u8 line_status;
151 __u8 modem_status;
152 };
153
154 struct quatech_port {
155 int port_num; /* number of the port */
156 struct urb *write_urb; /* write URB for this port */
157 struct urb *read_urb; /* read URB for this port */
158 struct urb *int_urb;
159
160 __u8 shadowLCR; /* last LCR value received */
161 __u8 shadowMCR; /* last MCR value received */
162 __u8 shadowMSR; /* last MSR value received */
163 __u8 shadowLSR; /* last LSR value received */
164 char open_ports;
165
166 /* Used for TIOCMIWAIT */
167 wait_queue_head_t msr_wait;
168 char prev_status, diff_status;
169
170 wait_queue_head_t wait;
171
172 struct async_icount icount;
173
174 struct usb_serial_port *port; /* owner of this object */
175 struct qt_get_device_data DeviceData;
176 struct mutex lock;
177 bool read_urb_busy;
178 int RxHolding;
179 int ReadBulkStopped;
180 char closePending;
181 };
182
183 static int port_paranoia_check(struct usb_serial_port *port,
184 const char *function)
185 {
186 if (!port) {
187 dbg("%s - port == NULL", function);
188 return -1;
189 }
190 if (!port->serial) {
191 dbg("%s - port->serial == NULL\n", function);
192 return -1;
193 }
194
195 return 0;
196 }
197
198 static int serial_paranoia_check(struct usb_serial *serial,
199 const char *function)
200 {
201 if (!serial) {
202 dbg("%s - serial == NULL\n", function);
203 return -1;
204 }
205
206 if (!serial->type) {
207 dbg("%s - serial->type == NULL!", function);
208 return -1;
209 }
210
211 return 0;
212 }
213
214 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
215 *port)
216 {
217 return (struct quatech_port *)usb_get_serial_port_data(port);
218 }
219
220 static inline void qt_set_port_private(struct usb_serial_port *port,
221 struct quatech_port *data)
222 {
223 usb_set_serial_port_data(port, (void *)data);
224 }
225
226 static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
227 const char *function)
228 {
229 /* if no port was specified, or it fails a paranoia check */
230 if (!port ||
231 port_paranoia_check(port, function) ||
232 serial_paranoia_check(port->serial, function)) {
233 /*
234 * then say that we dont have a valid usb_serial thing,
235 * which will end up genrating -ENODEV return values
236 */
237 return NULL;
238 }
239
240 return port->serial;
241 }
242
243 static void ProcessLineStatus(struct quatech_port *qt_port,
244 unsigned char line_status)
245 {
246
247 qt_port->shadowLSR =
248 line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
249 SERIAL_LSR_BI);
250 }
251
252 static void ProcessModemStatus(struct quatech_port *qt_port,
253 unsigned char modem_status)
254 {
255
256 qt_port->shadowMSR = modem_status;
257 wake_up_interruptible(&qt_port->wait);
258 }
259
260 static void ProcessRxChar(struct tty_struct *tty, struct usb_serial_port *port,
261 unsigned char data)
262 {
263 struct urb *urb = port->read_urb;
264 if (urb->actual_length)
265 tty_insert_flip_char(tty, data, TTY_NORMAL);
266 }
267
268 static void qt_write_bulk_callback(struct urb *urb)
269 {
270 struct tty_struct *tty;
271 int status;
272 struct quatech_port *quatech_port;
273
274 status = urb->status;
275
276 if (status) {
277 dbg("nonzero write bulk status received:%d\n", status);
278 return;
279 }
280
281 quatech_port = urb->context;
282
283 tty = tty_port_tty_get(&quatech_port->port->port);
284
285 if (tty)
286 tty_wakeup(tty);
287 tty_kref_put(tty);
288 }
289
290 static void qt_interrupt_callback(struct urb *urb)
291 {
292 /* FIXME */
293 }
294
295 static void qt_read_bulk_callback(struct urb *urb)
296 {
297
298 struct usb_serial_port *port = urb->context;
299 struct usb_serial *serial = get_usb_serial(port, __func__);
300 struct quatech_port *qt_port = qt_get_port_private(port);
301 unsigned char *data;
302 struct tty_struct *tty;
303 unsigned int index;
304 unsigned int RxCount;
305 int i, result;
306 int flag, flag_data;
307
308 if (urb->status) {
309 qt_port->ReadBulkStopped = 1;
310 dbg("%s - nonzero write bulk status received: %d\n",
311 __func__, urb->status);
312 return;
313 }
314
315 tty = tty_port_tty_get(&port->port);
316 if (!tty) {
317 dbg("%s - bad tty pointer - exiting", __func__);
318 return;
319 }
320
321 data = urb->transfer_buffer;
322
323 RxCount = urb->actual_length;
324
325 /* index = MINOR(port->tty->device) - serial->minor; */
326 index = tty->index - serial->minor;
327
328 dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
329
330 if (port_paranoia_check(port, __func__) != 0) {
331 dbg("%s - port_paranoia_check, exiting\n", __func__);
332 qt_port->ReadBulkStopped = 1;
333 goto exit;
334 }
335
336 if (!serial) {
337 dbg("%s - bad serial pointer, exiting\n", __func__);
338 goto exit;
339 }
340 if (qt_port->closePending == 1) {
341 /* Were closing , stop reading */
342 dbg("%s - (qt_port->closepending == 1\n", __func__);
343 qt_port->ReadBulkStopped = 1;
344 goto exit;
345 }
346
347 /*
348 * RxHolding is asserted by throttle, if we assert it, we're not
349 * receiving any more characters and let the box handle the flow
350 * control
351 */
352 if (qt_port->RxHolding == 1) {
353 qt_port->ReadBulkStopped = 1;
354 goto exit;
355 }
356
357 if (urb->status) {
358 qt_port->ReadBulkStopped = 1;
359
360 dbg("%s - nonzero read bulk status received: %d\n",
361 __func__, urb->status);
362 goto exit;
363 }
364
365 if (tty && RxCount) {
366 flag_data = 0;
367 for (i = 0; i < RxCount; ++i) {
368 /* Look ahead code here */
369 if ((i <= (RxCount - 3)) && (data[i] == 0x1b)
370 && (data[i + 1] == 0x1b)) {
371 flag = 0;
372 switch (data[i + 2]) {
373 case 0x00:
374 /* line status change 4th byte must follow */
375 if (i > (RxCount - 4)) {
376 dbg("Illegal escape seuences in received data\n");
377 break;
378 }
379 ProcessLineStatus(qt_port, data[i + 3]);
380 i += 3;
381 flag = 1;
382 break;
383
384 case 0x01:
385 /* Modem status status change 4th byte must follow */
386 dbg("Modem status status.\n");
387 if (i > (RxCount - 4)) {
388 dbg("Illegal escape sequences in received data\n");
389 break;
390 }
391 ProcessModemStatus(qt_port,
392 data[i + 3]);
393 i += 3;
394 flag = 1;
395 break;
396 case 0xff:
397 dbg("No status sequence.\n");
398
399 if (tty) {
400 ProcessRxChar(tty, port, data[i]);
401 ProcessRxChar(tty, port, data[i + 1]);
402 }
403 i += 2;
404 break;
405 }
406 if (flag == 1)
407 continue;
408 }
409
410 if (tty && urb->actual_length)
411 tty_insert_flip_char(tty, data[i], TTY_NORMAL);
412
413 }
414 tty_flip_buffer_push(tty);
415 }
416
417 /* Continue trying to always read */
418 usb_fill_bulk_urb(port->read_urb, serial->dev,
419 usb_rcvbulkpipe(serial->dev,
420 port->bulk_in_endpointAddress),
421 port->read_urb->transfer_buffer,
422 port->read_urb->transfer_buffer_length,
423 qt_read_bulk_callback, port);
424 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
425 if (result)
426 dbg("%s - failed resubmitting read urb, error %d",
427 __func__, result);
428 else {
429 if (tty && RxCount) {
430 tty_flip_buffer_push(tty);
431 tty_schedule_flip(tty);
432 }
433 }
434
435 schedule_work(&port->work);
436 exit:
437 tty_kref_put(tty);
438 }
439
440 /*
441 * qt_get_device
442 * Issue a GET_DEVICE vendor-specific request on the default control pipe If
443 * successful, fills in the qt_get_device_data structure pointed to by
444 * device_data, otherwise return a negative error number of the problem.
445 */
446
447 static int qt_get_device(struct usb_serial *serial,
448 struct qt_get_device_data *device_data)
449 {
450 int result;
451 unsigned char *transfer_buffer;
452
453 transfer_buffer =
454 kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL);
455 if (!transfer_buffer)
456 return -ENOMEM;
457
458 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
459 QT_SET_GET_DEVICE, 0xc0, 0, 0,
460 transfer_buffer,
461 sizeof(struct qt_get_device_data), 300);
462 if (result > 0)
463 memcpy(device_data, transfer_buffer,
464 sizeof(struct qt_get_device_data));
465 kfree(transfer_buffer);
466
467 return result;
468 }
469
470 /****************************************************************************
471 * BoxSetPrebufferLevel
472 TELLS BOX WHEN TO ASSERT FLOW CONTROL
473 ****************************************************************************/
474 static int BoxSetPrebufferLevel(struct usb_serial *serial)
475 {
476 int result;
477 __u16 buffer_length;
478
479 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
480 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
481 QT_GET_SET_PREBUF_TRIG_LVL, 0x40,
482 buffer_length, 0, NULL, 0, 300);
483 return result;
484 }
485
486 /****************************************************************************
487 * BoxSetATC
488 TELLS BOX WHEN TO ASSERT automatic transmitter control
489 ****************************************************************************/
490 static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode)
491 {
492 int result;
493 __u16 buffer_length;
494
495 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
496
497 result =
498 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
499 QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300);
500
501 return result;
502 }
503
504 /**
505 * qt_set_device
506 * Issue a SET_DEVICE vendor-specific request on the default control pipe If
507 * successful returns the number of bytes written, otherwise it returns a
508 * negative error number of the problem.
509 */
510 static int qt_set_device(struct usb_serial *serial,
511 struct qt_get_device_data *device_data)
512 {
513 int result;
514 __u16 length;
515 __u16 PortSettings;
516
517 PortSettings = ((__u16) (device_data->portb));
518 PortSettings = (PortSettings << 8);
519 PortSettings += ((__u16) (device_data->porta));
520
521 length = sizeof(struct qt_get_device_data);
522 dbg("%s - PortSettings = 0x%x\n", __func__, PortSettings);
523
524 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
525 QT_SET_GET_DEVICE, 0x40, PortSettings,
526 0, NULL, 0, 300);
527 return result;
528 }
529
530 static int qt_open_channel(struct usb_serial *serial, __u16 Uart_Number,
531 struct qt_open_channel_data *pDeviceData)
532 {
533 int result;
534
535 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
536 QT_OPEN_CLOSE_CHANNEL,
537 USBD_TRANSFER_DIRECTION_IN, 1, Uart_Number,
538 pDeviceData,
539 sizeof(struct qt_open_channel_data), 300);
540
541 return result;
542
543 }
544
545 static int qt_close_channel(struct usb_serial *serial, __u16 Uart_Number)
546 {
547 int result;
548
549 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
550 QT_OPEN_CLOSE_CHANNEL,
551 USBD_TRANSFER_DIRECTION_OUT, 0, Uart_Number,
552 NULL, 0, 300);
553
554 return result;
555
556 }
557
558 /****************************************************************************
559 * BoxGetRegister
560 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
561 * If successful, fills in the pValue with the register value asked for
562 ****************************************************************************/
563 static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number,
564 unsigned short Register_Num, __u8 *pValue)
565 {
566 int result;
567 __u16 current_length;
568
569 current_length = sizeof(struct qt_get_device_data);
570
571 result =
572 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
573 QT_GET_SET_REGISTER, 0xC0, Register_Num,
574 Uart_Number, (void *)pValue, sizeof(*pValue), 300);
575
576 return result;
577 }
578
579 /****************************************************************************
580 * BoxSetRegister
581 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
582 * If successful, fills in the pValue with the register value asked for
583 ****************************************************************************/
584 static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number,
585 unsigned short Register_Num, unsigned short Value)
586 {
587 int result;
588 unsigned short RegAndByte;
589
590 RegAndByte = Value;
591 RegAndByte = RegAndByte << 8;
592 RegAndByte = RegAndByte + Register_Num;
593
594 /*
595 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
596 QT_GET_SET_REGISTER, 0xC0, Register_Num,
597 Uart_Number, NULL, 0, 300);
598 */
599
600 result =
601 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
602 QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number,
603 NULL, 0, 300);
604
605 return result;
606 }
607
608 /*
609 * qt_setuart
610 * issues a SET_UART vendor-specific request on the default control pipe
611 * If successful sets baud rate divisor and LCR value
612 */
613 static int qt_setuart(struct usb_serial *serial, unsigned short Uart_Number,
614 unsigned short default_divisor, unsigned char default_LCR)
615 {
616 int result;
617 unsigned short UartNumandLCR;
618
619 UartNumandLCR = (default_LCR << 8) + Uart_Number;
620
621 result =
622 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
623 QT_GET_SET_UART, 0x40, default_divisor,
624 UartNumandLCR, NULL, 0, 300);
625
626 return result;
627 }
628
629 static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index,
630 int bSet)
631 {
632 __u8 mcr = 0;
633 __u8 msr = 0, MOUT_Value = 0;
634 unsigned int status;
635
636 if (bSet == 1) {
637 /* flow control, box will clear RTS line to prevent remote */
638 mcr = SERIAL_MCR_RTS;
639 } /* device from xmitting more chars */
640 else {
641 /* no flow control to remote device */
642 mcr = 0;
643
644 }
645 MOUT_Value = mcr << 8;
646
647 if (bSet == 1) {
648 /* flow control, box will inhibit xmit data if CTS line is
649 * asserted */
650 msr = SERIAL_MSR_CTS;
651 } else {
652 /* Box will not inhimbe xmit data due to CTS line */
653 msr = 0;
654 }
655 MOUT_Value |= msr;
656
657 status =
658 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
659 QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value,
660 index, NULL, 0, 300);
661 return status;
662
663 }
664
665 static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index,
666 unsigned char stop_char, unsigned char start_char)
667 {
668 __u16 nSWflowout;
669 int result;
670
671 nSWflowout = start_char << 8;
672 nSWflowout = (unsigned short)stop_char;
673
674 result =
675 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
676 QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout,
677 index, NULL, 0, 300);
678 return result;
679
680 }
681
682 static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index)
683 {
684 int result;
685
686 result =
687 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
688 QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index,
689 NULL, 0, 300);
690 return result;
691
692 }
693
694 static int qt_startup(struct usb_serial *serial)
695 {
696 struct usb_serial_port *port;
697 struct quatech_port *qt_port;
698 struct qt_get_device_data DeviceData;
699 int i;
700 int status;
701
702 /* Now setup per port private data */
703 for (i = 0; i < serial->num_ports; i++) {
704 port = serial->port[i];
705 qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL);
706 if (!qt_port) {
707 dbg("%s: kzalloc for quatech_port (%d) failed!.",
708 __func__, i);
709 for (--i; i >= 0; i--) {
710 port = serial->port[i];
711 kfree(usb_get_serial_port_data(port));
712 usb_set_serial_port_data(port, NULL);
713 }
714 return -ENOMEM;
715 }
716 mutex_init(&qt_port->lock);
717
718 usb_set_serial_port_data(port, qt_port);
719
720 }
721
722 status = qt_get_device(serial, &DeviceData);
723 if (status < 0) {
724 dbg(__FILE__ "box_get_device failed");
725 goto startup_error;
726 }
727
728 dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
729
730 DeviceData.portb &= ~FULLPWRBIT;
731 dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
732
733 status = qt_set_device(serial, &DeviceData);
734 if (status < 0) {
735 dbg(__FILE__ "qt_set_device failed\n");
736 goto startup_error;
737 }
738
739 status = qt_get_device(serial, &DeviceData);
740 if (status < 0) {
741 dbg(__FILE__ "qt_get_device failed");
742 goto startup_error;
743 }
744
745 switch (serial->dev->descriptor.idProduct) {
746 case QUATECH_DSU100:
747 case QUATECH_QSU100:
748 case QUATECH_ESU100A:
749 case QUATECH_ESU100B:
750 case QUATECH_HSU100A:
751 case QUATECH_HSU100B:
752 case QUATECH_HSU100C:
753 case QUATECH_HSU100D:
754 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
755 DeviceData.porta |= CLKS_X4;
756 DeviceData.portb &= ~(LOOPMODE_BITS);
757 DeviceData.portb |= RS232_MODE;
758 break;
759
760 case QUATECH_SSU200:
761 case QUATECH_DSU200:
762 case QUATECH_QSU200:
763 case QUATECH_ESU200A:
764 case QUATECH_ESU200B:
765 case QUATECH_HSU200A:
766 case QUATECH_HSU200B:
767 case QUATECH_HSU200C:
768 case QUATECH_HSU200D:
769 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
770 DeviceData.porta |= CLKS_X4;
771 DeviceData.portb &= ~(LOOPMODE_BITS);
772 DeviceData.portb |= ALL_LOOPBACK;
773 break;
774 default:
775 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
776 DeviceData.porta |= CLKS_X4;
777 DeviceData.portb &= ~(LOOPMODE_BITS);
778 DeviceData.portb |= RS232_MODE;
779 break;
780
781 }
782
783 status = BoxSetPrebufferLevel(serial); /* sets to default value */
784 if (status < 0) {
785 dbg(__FILE__ "BoxSetPrebufferLevel failed\n");
786 goto startup_error;
787 }
788
789 status = BoxSetATC(serial, ATC_DISABLED);
790 if (status < 0) {
791 dbg(__FILE__ "BoxSetATC failed\n");
792 goto startup_error;
793 }
794
795 dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
796
797 DeviceData.portb |= NEXT_BOARD_POWER_BIT;
798 dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
799
800 status = qt_set_device(serial, &DeviceData);
801 if (status < 0) {
802 dbg(__FILE__ "qt_set_device failed\n");
803 goto startup_error;
804 }
805
806 dbg("Exit Success %s\n", __func__);
807
808 return 0;
809
810 startup_error:
811 for (i = 0; i < serial->num_ports; i++) {
812 port = serial->port[i];
813 qt_port = qt_get_port_private(port);
814 kfree(qt_port);
815 usb_set_serial_port_data(port, NULL);
816 }
817
818 dbg("Exit fail %s\n", __func__);
819
820 return -EIO;
821 }
822
823 static void qt_release(struct usb_serial *serial)
824 {
825 struct usb_serial_port *port;
826 struct quatech_port *qt_port;
827 int i;
828
829 for (i = 0; i < serial->num_ports; i++) {
830 port = serial->port[i];
831 if (!port)
832 continue;
833
834 qt_port = usb_get_serial_port_data(port);
835 kfree(qt_port);
836 usb_set_serial_port_data(port, NULL);
837 }
838
839 }
840
841 static int qt_open(struct tty_struct *tty,
842 struct usb_serial_port *port)
843 {
844 struct usb_serial *serial;
845 struct quatech_port *quatech_port;
846 struct quatech_port *port0;
847 struct qt_open_channel_data ChannelData;
848
849 int result;
850
851 if (port_paranoia_check(port, __func__))
852 return -ENODEV;
853
854 serial = port->serial;
855
856 if (serial_paranoia_check(serial, __func__))
857 return -ENODEV;
858
859 quatech_port = qt_get_port_private(port);
860 port0 = qt_get_port_private(serial->port[0]);
861
862 if (quatech_port == NULL || port0 == NULL)
863 return -ENODEV;
864
865 usb_clear_halt(serial->dev, port->write_urb->pipe);
866 usb_clear_halt(serial->dev, port->read_urb->pipe);
867 port0->open_ports++;
868
869 result = qt_get_device(serial, &port0->DeviceData);
870
871 /* Port specific setups */
872 result = qt_open_channel(serial, port->number, &ChannelData);
873 if (result < 0) {
874 dbg(__FILE__ "qt_open_channel failed\n");
875 return result;
876 }
877 dbg(__FILE__ "qt_open_channel completed.\n");
878
879 /* FIXME: are these needed? Does it even do anything useful? */
880 quatech_port->shadowLSR = ChannelData.line_status &
881 (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
882
883 quatech_port->shadowMSR = ChannelData.modem_status &
884 (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
885
886 /* Set Baud rate to default and turn off (default)flow control here */
887 result = qt_setuart(serial, port->number, DEFAULT_DIVISOR, DEFAULT_LCR);
888 if (result < 0) {
889 dbg(__FILE__ "qt_setuart failed\n");
890 return result;
891 }
892 dbg(__FILE__ "qt_setuart completed.\n");
893
894 /*
895 * Put this here to make it responsive to stty and defaults set by
896 * the tty layer
897 */
898 /* FIXME: is this needed? */
899 /* qt_set_termios(tty, port, NULL); */
900
901 /* Check to see if we've set up our endpoint info yet */
902 if (port0->open_ports == 1) {
903 if (serial->port[0]->interrupt_in_buffer == NULL) {
904 /* set up interrupt urb */
905 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
906 serial->dev,
907 usb_rcvintpipe(serial->dev,
908 serial->port[0]->interrupt_in_endpointAddress),
909 serial->port[0]->interrupt_in_buffer,
910 serial->port[0]->
911 interrupt_in_urb->transfer_buffer_length,
912 qt_interrupt_callback, serial,
913 serial->port[0]->
914 interrupt_in_urb->interval);
915
916 result =
917 usb_submit_urb(serial->port[0]->interrupt_in_urb,
918 GFP_KERNEL);
919 if (result) {
920 dev_err(&port->dev,
921 "%s - Error %d submitting "
922 "interrupt urb\n", __func__, result);
923 }
924
925 }
926
927 }
928
929 dbg("port number is %d\n", port->number);
930 dbg("serial number is %d\n", port->serial->minor);
931 dbg("Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
932 dbg("BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
933 dbg("Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
934 dbg("port's number in the device is %d\n", quatech_port->port_num);
935 quatech_port->read_urb = port->read_urb;
936
937 /* set up our bulk in urb */
938
939 usb_fill_bulk_urb(quatech_port->read_urb,
940 serial->dev,
941 usb_rcvbulkpipe(serial->dev,
942 port->bulk_in_endpointAddress),
943 port->bulk_in_buffer,
944 quatech_port->read_urb->transfer_buffer_length,
945 qt_read_bulk_callback, quatech_port);
946
947 dbg("qt_open: bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
948 quatech_port->read_urb_busy = true;
949 result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
950 if (result) {
951 dev_err(&port->dev,
952 "%s - Error %d submitting control urb\n",
953 __func__, result);
954 quatech_port->read_urb_busy = false;
955 }
956
957 /* initialize our wait queues */
958 init_waitqueue_head(&quatech_port->wait);
959 init_waitqueue_head(&quatech_port->msr_wait);
960
961 /* initialize our icount structure */
962 memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
963
964 return 0;
965
966 }
967
968 static int qt_chars_in_buffer(struct tty_struct *tty)
969 {
970 struct usb_serial_port *port = tty->driver_data;
971 struct usb_serial *serial;
972 int chars = 0;
973
974 serial = get_usb_serial(port, __func__);
975
976 if (serial->num_bulk_out) {
977 if (port->write_urb->status == -EINPROGRESS)
978 chars = port->write_urb->transfer_buffer_length;
979 }
980
981 dbg("%s - returns %d\n", __func__, chars);
982
983 return chars;
984 }
985
986 static void qt_block_until_empty(struct tty_struct *tty,
987 struct quatech_port *qt_port)
988 {
989 int timeout = HZ / 10;
990 int wait = 30;
991 int count;
992
993 while (1) {
994
995 count = qt_chars_in_buffer(tty);
996
997 if (count <= 0)
998 return;
999
1000 interruptible_sleep_on_timeout(&qt_port->wait, timeout);
1001
1002 wait--;
1003 if (wait == 0) {
1004 dbg("%s - TIMEOUT", __func__);
1005 return;
1006 } else {
1007 wait = 30;
1008 }
1009 }
1010 }
1011
1012 static void qt_close(struct usb_serial_port *port)
1013 {
1014 struct usb_serial *serial = port->serial;
1015 struct quatech_port *qt_port;
1016 struct quatech_port *port0;
1017 struct tty_struct *tty;
1018 int status;
1019 unsigned int index;
1020 status = 0;
1021
1022 tty = tty_port_tty_get(&port->port);
1023 index = tty->index - serial->minor;
1024
1025 qt_port = qt_get_port_private(port);
1026 port0 = qt_get_port_private(serial->port[0]);
1027
1028 /* shutdown any bulk reads that might be going on */
1029 if (serial->num_bulk_out)
1030 usb_unlink_urb(port->write_urb);
1031 if (serial->num_bulk_in)
1032 usb_unlink_urb(port->read_urb);
1033
1034 /* wait up to for transmitter to empty */
1035 if (serial->dev)
1036 qt_block_until_empty(tty, qt_port);
1037 tty_kref_put(tty);
1038
1039 /* Close uart channel */
1040 status = qt_close_channel(serial, index);
1041 if (status < 0)
1042 dbg("%s - port %d qt_close_channel failed.\n",
1043 __func__, port->number);
1044
1045 port0->open_ports--;
1046
1047 dbg("qt_num_open_ports in close%d:in port%d\n",
1048 port0->open_ports, port->number);
1049
1050 if (port0->open_ports == 0) {
1051 if (serial->port[0]->interrupt_in_urb) {
1052 dbg("%s", "Shutdown interrupt_in_urb\n");
1053 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1054 }
1055
1056 }
1057
1058 if (qt_port->write_urb) {
1059 /* if this urb had a transfer buffer already (old tx) free it */
1060 kfree(qt_port->write_urb->transfer_buffer);
1061 usb_free_urb(qt_port->write_urb);
1062 }
1063
1064 }
1065
1066 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1067 const unsigned char *buf, int count)
1068 {
1069 int result;
1070 struct usb_serial *serial = get_usb_serial(port, __func__);
1071
1072 if (serial == NULL)
1073 return -ENODEV;
1074
1075 if (count == 0) {
1076 dbg("%s - write request of 0 bytes\n", __func__);
1077 return 0;
1078 }
1079
1080 /* only do something if we have a bulk out endpoint */
1081 if (serial->num_bulk_out) {
1082 if (port->write_urb->status == -EINPROGRESS) {
1083 dbg("%s - already writing\n", __func__);
1084 return 0;
1085 }
1086
1087 count =
1088 (count > port->bulk_out_size) ? port->bulk_out_size : count;
1089 memcpy(port->write_urb->transfer_buffer, buf, count);
1090
1091 /* set up our urb */
1092
1093 usb_fill_bulk_urb(port->write_urb, serial->dev,
1094 usb_sndbulkpipe(serial->dev,
1095 port->
1096 bulk_out_endpointAddress),
1097 port->write_urb->transfer_buffer, count,
1098 qt_write_bulk_callback, port);
1099
1100 /* send the data out the bulk port */
1101 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1102 if (result)
1103 dbg("%s - failed submitting write urb, error %d\n",
1104 __func__, result);
1105 else
1106 result = count;
1107
1108 return result;
1109 }
1110
1111 /* no bulk out, so return 0 bytes written */
1112 return 0;
1113 }
1114
1115 static int qt_write_room(struct tty_struct *tty)
1116 {
1117 struct usb_serial_port *port = tty->driver_data;
1118 struct usb_serial *serial;
1119 struct quatech_port *qt_port;
1120
1121 int retval = -EINVAL;
1122
1123 if (port_paranoia_check(port, __func__)) {
1124 dbg("%s", "Invalid port\n");
1125 return -1;
1126 }
1127
1128 serial = get_usb_serial(port, __func__);
1129
1130 if (!serial)
1131 return -ENODEV;
1132
1133 qt_port = qt_get_port_private(port);
1134
1135 mutex_lock(&qt_port->lock);
1136
1137 if (serial->num_bulk_out) {
1138 if (port->write_urb->status != -EINPROGRESS)
1139 retval = port->bulk_out_size;
1140 }
1141
1142 mutex_unlock(&qt_port->lock);
1143 return retval;
1144
1145 }
1146
1147 static int qt_ioctl(struct tty_struct *tty,
1148 unsigned int cmd, unsigned long arg)
1149 {
1150 struct usb_serial_port *port = tty->driver_data;
1151 struct quatech_port *qt_port = qt_get_port_private(port);
1152 struct usb_serial *serial = get_usb_serial(port, __func__);
1153 unsigned int index;
1154
1155 dbg("%s cmd 0x%04x", __func__, cmd);
1156
1157 index = tty->index - serial->minor;
1158
1159 if (cmd == TIOCMIWAIT) {
1160 while (qt_port != NULL) {
1161 interruptible_sleep_on(&qt_port->msr_wait);
1162 if (signal_pending(current))
1163 return -ERESTARTSYS;
1164 else {
1165 char diff = qt_port->diff_status;
1166
1167 if (diff == 0)
1168 return -EIO; /* no change => error */
1169
1170 /* Consume all events */
1171 qt_port->diff_status = 0;
1172
1173 if (((arg & TIOCM_RNG)
1174 && (diff & SERIAL_MSR_RI))
1175 || ((arg & TIOCM_DSR)
1176 && (diff & SERIAL_MSR_DSR))
1177 || ((arg & TIOCM_CD)
1178 && (diff & SERIAL_MSR_CD))
1179 || ((arg & TIOCM_CTS)
1180 && (diff & SERIAL_MSR_CTS))) {
1181 return 0;
1182 }
1183 }
1184 }
1185 return 0;
1186 }
1187
1188 dbg("%s -No ioctl for that one. port = %d\n", __func__, port->number);
1189 return -ENOIOCTLCMD;
1190 }
1191
1192 static void qt_set_termios(struct tty_struct *tty,
1193 struct usb_serial_port *port,
1194 struct ktermios *old_termios)
1195 {
1196 struct ktermios *termios = tty->termios;
1197 unsigned char new_LCR = 0;
1198 unsigned int cflag = termios->c_cflag;
1199 unsigned int index;
1200 int baud, divisor, remainder;
1201 int status;
1202
1203 index = tty->index - port->serial->minor;
1204
1205 switch (cflag) {
1206 case CS5:
1207 new_LCR |= SERIAL_5_DATA;
1208 break;
1209 case CS6:
1210 new_LCR |= SERIAL_6_DATA;
1211 break;
1212 case CS7:
1213 new_LCR |= SERIAL_7_DATA;
1214 break;
1215 default:
1216 case CS8:
1217 new_LCR |= SERIAL_8_DATA;
1218 break;
1219 }
1220
1221 /* Parity stuff */
1222 if (cflag & PARENB) {
1223 if (cflag & PARODD)
1224 new_LCR |= SERIAL_ODD_PARITY;
1225 else
1226 new_LCR |= SERIAL_EVEN_PARITY;
1227 }
1228 if (cflag & CSTOPB)
1229 new_LCR |= SERIAL_TWO_STOPB;
1230 else
1231 new_LCR |= SERIAL_ONE_STOPB;
1232
1233 dbg("%s - 4\n", __func__);
1234
1235 /* Thats the LCR stuff, go ahead and set it */
1236 baud = tty_get_baud_rate(tty);
1237 if (!baud)
1238 /* pick a default, any default... */
1239 baud = 9600;
1240
1241 dbg("%s - got baud = %d\n", __func__, baud);
1242
1243 divisor = MAX_BAUD_RATE / baud;
1244 remainder = MAX_BAUD_RATE % baud;
1245 /* Round to nearest divisor */
1246 if (((remainder * 2) >= baud) && (baud != 110))
1247 divisor++;
1248
1249 /*
1250 * Set Baud rate to default and turn off (default)flow control here
1251 */
1252 status =
1253 qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR);
1254 if (status < 0) {
1255 dbg(__FILE__ "qt_setuart failed\n");
1256 return;
1257 }
1258
1259 /* Now determine flow control */
1260 if (cflag & CRTSCTS) {
1261 dbg("%s - Enabling HW flow control port %d\n", __func__,
1262 port->number);
1263
1264 /* Enable RTS/CTS flow control */
1265 status = BoxSetHW_FlowCtrl(port->serial, index, 1);
1266
1267 if (status < 0) {
1268 dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1269 return;
1270 }
1271 } else {
1272 /* Disable RTS/CTS flow control */
1273 dbg("%s - disabling HW flow control port %d\n", __func__,
1274 port->number);
1275
1276 status = BoxSetHW_FlowCtrl(port->serial, index, 0);
1277 if (status < 0) {
1278 dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1279 return;
1280 }
1281
1282 }
1283
1284 /* if we are implementing XON/XOFF, set the start and stop character in
1285 * the device */
1286 if (I_IXOFF(tty) || I_IXON(tty)) {
1287 unsigned char stop_char = STOP_CHAR(tty);
1288 unsigned char start_char = START_CHAR(tty);
1289 status =
1290 BoxSetSW_FlowCtrl(port->serial, index, stop_char,
1291 start_char);
1292 if (status < 0)
1293 dbg(__FILE__ "BoxSetSW_FlowCtrl (enabled) failed\n");
1294
1295 } else {
1296 /* disable SW flow control */
1297 status = BoxDisable_SW_FlowCtrl(port->serial, index);
1298 if (status < 0)
1299 dbg(__FILE__ "BoxSetSW_FlowCtrl (diabling) failed\n");
1300
1301 }
1302 tty->termios->c_cflag &= ~CMSPAR;
1303 /* FIXME: Error cases should be returning the actual bits changed only */
1304 }
1305
1306 static void qt_break(struct tty_struct *tty, int break_state)
1307 {
1308 struct usb_serial_port *port = tty->driver_data;
1309 struct usb_serial *serial = get_usb_serial(port, __func__);
1310 struct quatech_port *qt_port;
1311 u16 index, onoff;
1312 unsigned int result;
1313
1314 index = tty->index - serial->minor;
1315
1316 qt_port = qt_get_port_private(port);
1317
1318 if (break_state == -1)
1319 onoff = 1;
1320 else
1321 onoff = 0;
1322
1323 mutex_lock(&qt_port->lock);
1324
1325 result =
1326 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1327 QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1328
1329 mutex_unlock(&qt_port->lock);
1330 }
1331
1332 static inline int qt_real_tiocmget(struct tty_struct *tty,
1333 struct usb_serial_port *port,
1334 struct usb_serial *serial)
1335 {
1336
1337 u8 mcr;
1338 u8 msr;
1339 unsigned int result = 0;
1340 int status;
1341 unsigned int index;
1342
1343 index = tty->index - serial->minor;
1344 status =
1345 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1346 if (status >= 0) {
1347 status =
1348 BoxGetRegister(port->serial, index,
1349 MODEM_STATUS_REGISTER, &msr);
1350
1351 }
1352
1353 if (status >= 0) {
1354 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1355 /* DTR IS SET */
1356 | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1357 /* RTS IS SET */
1358 | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1359 /* CTS is set */
1360 | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1361 /* Carrier detect is set */
1362 | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1363 /* Ring indicator set */
1364 | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1365 /* DSR is set */
1366 return result;
1367
1368 } else
1369 return -ESPIPE;
1370 }
1371
1372 static inline int qt_real_tiocmset(struct tty_struct *tty,
1373 struct usb_serial_port *port,
1374 struct usb_serial *serial,
1375 unsigned int value)
1376 {
1377
1378 u8 mcr;
1379 int status;
1380 unsigned int index;
1381
1382 index = tty->index - serial->minor;
1383 status =
1384 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1385 if (status < 0)
1386 return -ESPIPE;
1387
1388 /*
1389 * Turn off the RTS and DTR and loopback and then only turn on what was
1390 * asked for
1391 */
1392 mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1393 if (value & TIOCM_RTS)
1394 mcr |= SERIAL_MCR_RTS;
1395 if (value & TIOCM_DTR)
1396 mcr |= SERIAL_MCR_DTR;
1397 if (value & TIOCM_LOOP)
1398 mcr |= SERIAL_MCR_LOOP;
1399
1400 status =
1401 BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1402 if (status < 0)
1403 return -ESPIPE;
1404 else
1405 return 0;
1406 }
1407
1408 static int qt_tiocmget(struct tty_struct *tty)
1409 {
1410 struct usb_serial_port *port = tty->driver_data;
1411 struct usb_serial *serial = get_usb_serial(port, __func__);
1412 struct quatech_port *qt_port = qt_get_port_private(port);
1413 int retval;
1414
1415 if (!serial)
1416 return -ENODEV;
1417
1418 mutex_lock(&qt_port->lock);
1419 retval = qt_real_tiocmget(tty, port, serial);
1420 mutex_unlock(&qt_port->lock);
1421 return retval;
1422 }
1423
1424 static int qt_tiocmset(struct tty_struct *tty,
1425 unsigned int set, unsigned int clear)
1426 {
1427
1428 struct usb_serial_port *port = tty->driver_data;
1429 struct usb_serial *serial = get_usb_serial(port, __func__);
1430 struct quatech_port *qt_port = qt_get_port_private(port);
1431 int retval;
1432
1433 if (!serial)
1434 return -ENODEV;
1435
1436 mutex_lock(&qt_port->lock);
1437 retval = qt_real_tiocmset(tty, port, serial, set);
1438 mutex_unlock(&qt_port->lock);
1439 return retval;
1440 }
1441
1442 static void qt_throttle(struct tty_struct *tty)
1443 {
1444 struct usb_serial_port *port = tty->driver_data;
1445 struct usb_serial *serial = get_usb_serial(port, __func__);
1446 struct quatech_port *qt_port;
1447
1448 if (!serial)
1449 return;
1450
1451 qt_port = qt_get_port_private(port);
1452
1453 mutex_lock(&qt_port->lock);
1454
1455 /* pass on to the driver specific version of this function */
1456 qt_port->RxHolding = 1;
1457
1458 mutex_unlock(&qt_port->lock);
1459 }
1460
1461 static void qt_unthrottle(struct tty_struct *tty)
1462 {
1463 struct usb_serial_port *port = tty->driver_data;
1464 struct usb_serial *serial = get_usb_serial(port, __func__);
1465 struct quatech_port *qt_port;
1466 unsigned int result;
1467
1468 if (!serial)
1469 return;
1470
1471 qt_port = qt_get_port_private(port);
1472
1473 mutex_lock(&qt_port->lock);
1474
1475 if (qt_port->RxHolding == 1) {
1476 dbg("%s -qt_port->RxHolding == 1\n", __func__);
1477
1478 qt_port->RxHolding = 0;
1479 dbg("%s - qt_port->RxHolding = 0\n", __func__);
1480
1481 /* if we have a bulk endpoint, start it up */
1482 if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1)) {
1483 /* Start reading from the device */
1484 usb_fill_bulk_urb(port->read_urb, serial->dev,
1485 usb_rcvbulkpipe(serial->dev,
1486 port->bulk_in_endpointAddress),
1487 port->read_urb->transfer_buffer,
1488 port->read_urb->
1489 transfer_buffer_length,
1490 qt_read_bulk_callback, port);
1491 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1492 if (result)
1493 dev_err(&port->dev,
1494 "%s - failed restarting read urb, error %d\n",
1495 __func__, result);
1496 }
1497 }
1498 mutex_unlock(&qt_port->lock);
1499 }
1500
1501 static int qt_calc_num_ports(struct usb_serial *serial)
1502 {
1503 int num_ports;
1504
1505 dbg("numberofendpoints: %d\n",
1506 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
1507 dbg("numberofendpoints: %d\n",
1508 (int)serial->interface->altsetting->desc.bNumEndpoints);
1509
1510 num_ports =
1511 (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1512
1513 return num_ports;
1514 }
1515
1516 static struct usb_serial_driver quatech_device = {
1517 .driver = {
1518 .owner = THIS_MODULE,
1519 .name = "serqt",
1520 },
1521 .description = DRIVER_DESC,
1522 .id_table = id_table,
1523 .num_ports = 8,
1524 .open = qt_open,
1525 .close = qt_close,
1526 .write = qt_write,
1527 .write_room = qt_write_room,
1528 .chars_in_buffer = qt_chars_in_buffer,
1529 .throttle = qt_throttle,
1530 .unthrottle = qt_unthrottle,
1531 .calc_num_ports = qt_calc_num_ports,
1532 .ioctl = qt_ioctl,
1533 .set_termios = qt_set_termios,
1534 .break_ctl = qt_break,
1535 .tiocmget = qt_tiocmget,
1536 .tiocmset = qt_tiocmset,
1537 .attach = qt_startup,
1538 .release = qt_release,
1539 };
1540
1541 static struct usb_serial_driver * const serial_drivers[] = {
1542 &quatech_device, NULL
1543 };
1544
1545 module_usb_serial_driver(serial_drivers, id_table);
1546
1547 MODULE_AUTHOR(DRIVER_AUTHOR);
1548 MODULE_DESCRIPTION(DRIVER_DESC);
1549 MODULE_LICENSE("GPL");
1550
1551 module_param(debug, bool, S_IRUGO | S_IWUSR);
1552 MODULE_PARM_DESC(debug, "Debug enabled or not");