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