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