]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/serqt_usb2/serqt_usb2.c
staging: wlags49_h2(5): remove driver
[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/slab.h>
9 #include <linux/tty.h>
10 #include <linux/tty_driver.h>
11 #include <linux/tty_flip.h>
12 #include <linux/module.h>
13 #include <linux/serial.h>
14 #include <linux/usb.h>
15 #include <linux/usb/serial.h>
16 #include <linux/uaccess.h>
17
18 /* Version Information */
19 #define DRIVER_VERSION "v2.14"
20 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
21 #define DRIVER_DESC "Quatech USB to Serial Driver"
22
23 #define USB_VENDOR_ID_QUATECH                   0x061d  /* Quatech VID */
24 #define QUATECH_SSU200  0xC030  /* SSU200 */
25 #define QUATECH_DSU100  0xC040  /* DSU100 */
26 #define QUATECH_DSU200  0xC050  /* DSU200 */
27 #define QUATECH_QSU100  0xC060  /* QSU100 */
28 #define QUATECH_QSU200  0xC070  /* QSU200 */
29 #define QUATECH_ESU100A 0xC080  /* ESU100A */
30 #define QUATECH_ESU100B 0xC081  /* ESU100B */
31 #define QUATECH_ESU200A 0xC0A0  /* ESU200A */
32 #define QUATECH_ESU200B 0xC0A1  /* ESU200B */
33 #define QUATECH_HSU100A 0xC090  /* HSU100A */
34 #define QUATECH_HSU100B 0xC091  /* HSU100B */
35 #define QUATECH_HSU100C 0xC092  /* HSU100C */
36 #define QUATECH_HSU100D 0xC093  /* HSU100D */
37 #define QUATECH_HSU200A 0xC0B0  /* HSU200A */
38 #define QUATECH_HSU200B 0xC0B1  /* HSU200B */
39 #define QUATECH_HSU200C 0xC0B2  /* HSU200C */
40 #define QUATECH_HSU200D 0xC0B3  /* HSU200D */
41
42 #define QT_SET_GET_DEVICE           0xc2
43 #define QT_OPEN_CLOSE_CHANNEL       0xca
44 #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
45 #define QT_SET_ATF                  0xcd
46 #define QT_GET_SET_REGISTER         0xc0
47 #define QT_GET_SET_UART             0xc1
48 #define QT_HW_FLOW_CONTROL_MASK     0xc5
49 #define QT_SW_FLOW_CONTROL_MASK     0xc6
50 #define QT_SW_FLOW_CONTROL_DISABLE  0xc7
51 #define QT_BREAK_CONTROL            0xc8
52
53 #define USBD_TRANSFER_DIRECTION_IN    0xc0
54 #define USBD_TRANSFER_DIRECTION_OUT   0x40
55
56 #define  MAX_BAUD_RATE              460800
57 #define  MAX_BAUD_REMAINDER         4608
58
59 #define  DIV_LATCH_LS               0x00
60 #define  XMT_HOLD_REGISTER          0x00
61 #define  XVR_BUFFER_REGISTER        0x00
62 #define  DIV_LATCH_MS               0x01
63 #define  FIFO_CONTROL_REGISTER      0x02
64 #define  LINE_CONTROL_REGISTER      0x03
65 #define  MODEM_CONTROL_REGISTER     0x04
66 #define  LINE_STATUS_REGISTER       0x05
67 #define  MODEM_STATUS_REGISTER      0x06
68
69 #define  SERIAL_MCR_DTR             0x01
70 #define  SERIAL_MCR_RTS             0x02
71 #define  SERIAL_MCR_LOOP            0x10
72
73 #define  SERIAL_MSR_CTS             0x10
74 #define  SERIAL_MSR_CD              0x80
75 #define  SERIAL_MSR_RI              0x40
76 #define  SERIAL_MSR_DSR             0x20
77 #define  SERIAL_MSR_MASK            0xf0
78
79 #define  SERIAL_8_DATA              0x03
80 #define  SERIAL_7_DATA              0x02
81 #define  SERIAL_6_DATA              0x01
82 #define  SERIAL_5_DATA              0x00
83
84 #define  SERIAL_ODD_PARITY          0X08
85 #define  SERIAL_EVEN_PARITY         0X18
86 #define  SERIAL_TWO_STOPB           0x04
87 #define  SERIAL_ONE_STOPB           0x00
88
89 #define DEFAULT_DIVISOR  0x30   /* gives 9600 baud rate */
90 #define DEFAULT_LCR SERIAL_8_DATA       /* 8, none , 1 */
91
92 #define FULLPWRBIT          0x00000080
93 #define NEXT_BOARD_POWER_BIT        0x00000004
94
95 #define SERIAL_LSR_OE       0x02
96 #define SERIAL_LSR_PE       0x04
97 #define SERIAL_LSR_FE       0x08
98 #define SERIAL_LSR_BI       0x10
99
100 #define  SERIAL_MSR_CTS             0x10
101 #define  SERIAL_MSR_CD              0x80
102 #define  SERIAL_MSR_RI              0x40
103 #define  SERIAL_MSR_DSR             0x20
104 #define  SERIAL_MSR_MASK            0xf0
105
106 #define PREFUFF_LEVEL_CONSERVATIVE  128
107 #define ATC_DISABLED                0x0
108
109 #define RR_BITS             0x03        /* for clearing clock bits */
110 #define DUPMODE_BITS        0xc0
111 #define CLKS_X4             0x02
112
113 #define LOOPMODE_BITS       0x41        /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
114 #define ALL_LOOPBACK        0x01
115 #define MODEM_CTRL          0x40
116 #define RS232_MODE          0x00
117
118 static const struct usb_device_id id_table[] = {
119         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
120         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
121         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
122         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
123         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
124         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
125         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
126         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
127         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
128         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
129         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
130         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
131         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
132         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
133         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
134         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
135         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
136         {}                      /* Terminating entry */
137 };
138 MODULE_DEVICE_TABLE(usb, id_table);
139
140 struct qt_get_device_data {
141         __u8 porta;
142         __u8 portb;
143         __u8 portc;
144 };
145
146 struct qt_open_channel_data {
147         __u8 line_status;
148         __u8 modem_status;
149 };
150
151 struct quatech_port {
152         int port_num;           /* number of the port */
153         struct urb *write_urb;  /* write URB for this port */
154         struct urb *read_urb;   /* read URB for this port */
155         struct urb *int_urb;
156
157         __u8 shadow_lcr;                /* last LCR value received */
158         __u8 shadow_mcr;                /* last MCR value received */
159         __u8 shadow_msr;                /* last MSR value received */
160         __u8 shadow_lsr;                /* last LSR value received */
161         char open_ports;
162
163         /* Used for TIOCMIWAIT */
164         wait_queue_head_t msr_wait;
165         char prev_status, diff_status;
166
167         wait_queue_head_t wait;
168
169         struct async_icount icount;
170
171         struct usb_serial_port *port;   /* owner of this object */
172         struct qt_get_device_data device_data;
173         struct mutex lock;
174         bool read_urb_busy;
175         int rx_holding;
176         int read_bulk_stopped;
177         char close_pending;
178 };
179
180 static int port_paranoia_check(struct usb_serial_port *port,
181                                const char *function)
182 {
183         if (!port) {
184                 pr_debug("%s - port == NULL", function);
185                 return -1;
186         }
187         if (!port->serial) {
188                 pr_debug("%s - port->serial == NULL\n", function);
189                 return -1;
190         }
191
192         return 0;
193 }
194
195 static int serial_paranoia_check(struct usb_serial *serial,
196                                  const char *function)
197 {
198         if (!serial) {
199                 pr_debug("%s - serial == NULL\n", function);
200                 return -1;
201         }
202
203         if (!serial->type) {
204                 pr_debug("%s - serial->type == NULL!", function);
205                 return -1;
206         }
207
208         return 0;
209 }
210
211 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
212                                                        *port)
213 {
214         return (struct quatech_port *)usb_get_serial_port_data(port);
215 }
216
217 static inline void qt_set_port_private(struct usb_serial_port *port,
218                                        struct quatech_port *data)
219 {
220         usb_set_serial_port_data(port, (void *)data);
221 }
222
223 static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
224                                          const char *function)
225 {
226         /* if no port was specified, or it fails a paranoia check */
227         if (!port ||
228             port_paranoia_check(port, function) ||
229             serial_paranoia_check(port->serial, function)) {
230                 /*
231                  * then say that we dont have a valid usb_serial thing,
232                  * which will end up genrating -ENODEV return values
233                  */
234                 return NULL;
235         }
236
237         return port->serial;
238 }
239
240 static void process_line_status(struct quatech_port *qt_port,
241                               unsigned char line_status)
242 {
243
244         qt_port->shadow_lsr =
245             line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
246                            SERIAL_LSR_BI);
247 }
248
249 static void process_modem_status(struct quatech_port *qt_port,
250                                unsigned char modem_status)
251 {
252
253         qt_port->shadow_msr = modem_status;
254         wake_up_interruptible(&qt_port->wait);
255 }
256
257 static void process_rx_char(struct usb_serial_port *port, unsigned char data)
258 {
259         struct urb *urb = port->read_urb;
260
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 (le16_to_cpu(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
767                                                          * default value
768                                                          */
769         if (status < 0) {
770                 dev_dbg(dev, "box_set_prebuffer_level failed\n");
771                 goto startup_error;
772         }
773
774         status = box_set_atc(serial, ATC_DISABLED);
775         if (status < 0) {
776                 dev_dbg(dev, "box_set_atc failed\n");
777                 goto startup_error;
778         }
779
780         dev_dbg(dev, "device_data.portb = 0x%x\n", device_data.portb);
781
782         device_data.portb |= NEXT_BOARD_POWER_BIT;
783         dev_dbg(dev, "Changing device_data.portb to 0x%x\n", device_data.portb);
784
785         status = qt_set_device(serial, &device_data);
786         if (status < 0) {
787                 dev_dbg(dev, "qt_set_device failed\n");
788                 goto startup_error;
789         }
790
791         return 0;
792
793 startup_error:
794         for (i = 0; i < serial->num_ports; i++) {
795                 port = serial->port[i];
796                 qt_port = qt_get_port_private(port);
797                 kfree(qt_port);
798                 usb_set_serial_port_data(port, NULL);
799         }
800
801         return -EIO;
802 }
803
804 static void qt_release(struct usb_serial *serial)
805 {
806         struct usb_serial_port *port;
807         struct quatech_port *qt_port;
808         int i;
809
810         for (i = 0; i < serial->num_ports; i++) {
811                 port = serial->port[i];
812                 if (!port)
813                         continue;
814
815                 qt_port = usb_get_serial_port_data(port);
816                 kfree(qt_port);
817                 usb_set_serial_port_data(port, NULL);
818         }
819
820 }
821
822 static void qt_submit_urb_from_open(struct usb_serial *serial,
823                                     struct usb_serial_port *port)
824 {
825         int result;
826         struct usb_serial_port *port0 = serial->port[0];
827
828         /* set up interrupt urb */
829         usb_fill_int_urb(port0->interrupt_in_urb,
830                          serial->dev,
831                          usb_rcvintpipe(serial->dev,
832                                         port0->interrupt_in_endpointAddress),
833                          port0->interrupt_in_buffer,
834                          port0->interrupt_in_urb->transfer_buffer_length,
835                          qt_interrupt_callback, serial,
836                          port0->interrupt_in_urb->interval);
837
838         result = usb_submit_urb(port0->interrupt_in_urb,
839                                 GFP_KERNEL);
840         if (result) {
841                 dev_err(&port->dev,
842                         "%s - Error %d submitting interrupt urb\n",
843                         __func__, result);
844         }
845 }
846
847 static int qt_open(struct tty_struct *tty,
848                    struct usb_serial_port *port)
849 {
850         struct usb_serial *serial;
851         struct quatech_port *quatech_port;
852         struct quatech_port *port0;
853         struct qt_open_channel_data channel_data;
854
855         int result;
856
857         if (port_paranoia_check(port, __func__))
858                 return -ENODEV;
859
860         serial = port->serial;
861
862         if (serial_paranoia_check(serial, __func__))
863                 return -ENODEV;
864
865         quatech_port = qt_get_port_private(port);
866         port0 = qt_get_port_private(serial->port[0]);
867
868         if (quatech_port == NULL || port0 == NULL)
869                 return -ENODEV;
870
871         usb_clear_halt(serial->dev, port->write_urb->pipe);
872         usb_clear_halt(serial->dev, port->read_urb->pipe);
873         port0->open_ports++;
874
875         result = qt_get_device(serial, &port0->device_data);
876
877         /* Port specific setups */
878         result = qt_open_channel(serial, port->port_number, &channel_data);
879         if (result < 0) {
880                 dev_dbg(&port->dev, "qt_open_channel failed\n");
881                 return result;
882         }
883         dev_dbg(&port->dev, "qt_open_channel completed.\n");
884
885 /* FIXME: are these needed?  Does it even do anything useful? */
886         quatech_port->shadow_lsr = channel_data.line_status &
887             (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
888
889         quatech_port->shadow_msr = channel_data.modem_status &
890             (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
891
892         /* Set Baud rate to default and turn off (default)flow control here */
893         result = qt_setuart(serial, port->port_number, DEFAULT_DIVISOR,
894                         DEFAULT_LCR);
895         if (result < 0) {
896                 dev_dbg(&port->dev, "qt_setuart failed\n");
897                 return result;
898         }
899         dev_dbg(&port->dev, "qt_setuart completed.\n");
900
901         /*
902          * Put this here to make it responsive to stty and defaults set by
903          * the tty layer
904          */
905
906         /*  Check to see if we've set up our endpoint info yet */
907         if (port0->open_ports == 1) {
908                 if (serial->port[0]->interrupt_in_buffer == NULL)
909                         qt_submit_urb_from_open(serial, port);
910         }
911
912         dev_dbg(&port->dev, "minor number is %d\n", port->minor);
913         dev_dbg(&port->dev,
914                 "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
915         dev_dbg(&port->dev,
916                 "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
917         dev_dbg(&port->dev, "Interrupt endpoint is %d\n",
918                 port->interrupt_in_endpointAddress);
919         dev_dbg(&port->dev, "port's number in the device is %d\n",
920                 quatech_port->port_num);
921         quatech_port->read_urb = port->read_urb;
922
923         /* set up our bulk in urb */
924
925         usb_fill_bulk_urb(quatech_port->read_urb,
926                           serial->dev,
927                           usb_rcvbulkpipe(serial->dev,
928                                           port->bulk_in_endpointAddress),
929                           port->bulk_in_buffer,
930                           quatech_port->read_urb->transfer_buffer_length,
931                           qt_read_bulk_callback, quatech_port);
932
933         dev_dbg(&port->dev, "qt_open: bulkin endpoint is %d\n",
934                 port->bulk_in_endpointAddress);
935         quatech_port->read_urb_busy = true;
936         result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
937         if (result) {
938                 dev_err(&port->dev,
939                         "%s - Error %d submitting control urb\n",
940                         __func__, result);
941                 quatech_port->read_urb_busy = false;
942         }
943
944         /* initialize our wait queues */
945         init_waitqueue_head(&quatech_port->wait);
946         init_waitqueue_head(&quatech_port->msr_wait);
947
948         /* initialize our icount structure */
949         memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
950
951         return 0;
952
953 }
954
955 static int qt_chars_in_buffer(struct tty_struct *tty)
956 {
957         struct usb_serial_port *port = tty->driver_data;
958         struct usb_serial *serial;
959         int chars = 0;
960
961         serial = get_usb_serial(port, __func__);
962
963         if (serial->num_bulk_out) {
964                 if (port->write_urb->status == -EINPROGRESS)
965                         chars = port->write_urb->transfer_buffer_length;
966         }
967
968         return chars;
969 }
970
971 static void qt_block_until_empty(struct tty_struct *tty,
972                                  struct quatech_port *qt_port)
973 {
974         int timeout = HZ / 10;
975         int wait = 30;
976
977         /* returns if we get a signal, an error, or the buffer is empty */
978         while (wait_event_interruptible_timeout(qt_port->wait,
979                                         qt_chars_in_buffer(tty) <= 0,
980                                         timeout) == 0) {
981                 wait--;
982                 if (wait == 0) {
983                         dev_dbg(&qt_port->port->dev, "%s - TIMEOUT", __func__);
984                         return;
985                 }
986                 wait = 30;
987         }
988 }
989
990 static void qt_close(struct usb_serial_port *port)
991 {
992         struct usb_serial *serial = port->serial;
993         struct tty_struct *tty = tty_port_tty_get(&port->port);
994         unsigned int index = port->port_number;
995         struct quatech_port *qt_port = qt_get_port_private(port);
996         struct quatech_port *port0 = qt_get_port_private(serial->port[0]);
997
998         /* shutdown any bulk reads that might be going on */
999         if (serial->num_bulk_out)
1000                 usb_unlink_urb(port->write_urb);
1001         if (serial->num_bulk_in)
1002                 usb_unlink_urb(port->read_urb);
1003
1004         /* wait up to for transmitter to empty */
1005         if (serial->dev)
1006                 qt_block_until_empty(tty, qt_port);
1007         tty_kref_put(tty);
1008
1009         /* Close uart channel */
1010         if (qt_close_channel(serial, index) < 0)
1011                 dev_dbg(&port->dev, "%s - qt_close_channel failed.\n",
1012                         __func__);
1013
1014         port0->open_ports--;
1015
1016         dev_dbg(&port->dev, "qt_num_open_ports in close%d\n",
1017                 port0->open_ports);
1018
1019         if (port0->open_ports == 0) {
1020                 if (serial->port[0]->interrupt_in_urb) {
1021                         dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1022                         usb_kill_urb(serial->port[0]->interrupt_in_urb);
1023                 }
1024
1025         }
1026
1027         if (qt_port->write_urb) {
1028                 /* if this urb had a transfer buffer already (old tx) free it */
1029                 kfree(qt_port->write_urb->transfer_buffer);
1030                 usb_free_urb(qt_port->write_urb);
1031         }
1032
1033 }
1034
1035 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1036                     const unsigned char *buf, int count)
1037 {
1038         int result;
1039         struct usb_serial *serial = get_usb_serial(port, __func__);
1040
1041         if (serial == NULL)
1042                 return -ENODEV;
1043
1044         if (count == 0) {
1045                 dev_dbg(&port->dev,
1046                         "%s - write request of 0 bytes\n", __func__);
1047                 return 0;
1048         }
1049
1050         /* only do something if we have a bulk out endpoint */
1051         if (serial->num_bulk_out) {
1052                 if (port->write_urb->status == -EINPROGRESS) {
1053                         dev_dbg(&port->dev, "%s - already writing\n", __func__);
1054                         return 0;
1055                 }
1056
1057                 count =
1058                     (count > port->bulk_out_size) ? port->bulk_out_size : count;
1059                 memcpy(port->write_urb->transfer_buffer, buf, count);
1060
1061                 /* set up our urb */
1062
1063                 usb_fill_bulk_urb(port->write_urb, serial->dev,
1064                                   usb_sndbulkpipe(serial->dev,
1065                                                   port->
1066                                                   bulk_out_endpointAddress),
1067                                   port->write_urb->transfer_buffer, count,
1068                                   qt_write_bulk_callback, port);
1069
1070                 /* send the data out the bulk port */
1071                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1072                 if (result)
1073                         dev_dbg(&port->dev,
1074                                 "%s - failed submitting write urb, error %d\n",
1075                                 __func__, result);
1076                 else
1077                         result = count;
1078
1079                 return result;
1080         }
1081
1082         /* no bulk out, so return 0 bytes written */
1083         return 0;
1084 }
1085
1086 static int qt_write_room(struct tty_struct *tty)
1087 {
1088         struct usb_serial_port *port = tty->driver_data;
1089         struct usb_serial *serial;
1090         struct quatech_port *qt_port;
1091
1092         int retval = -EINVAL;
1093
1094         if (port_paranoia_check(port, __func__))
1095                 return -1;
1096
1097         serial = get_usb_serial(port, __func__);
1098
1099         if (!serial)
1100                 return -ENODEV;
1101
1102         qt_port = qt_get_port_private(port);
1103
1104         mutex_lock(&qt_port->lock);
1105
1106         if (serial->num_bulk_out) {
1107                 if (port->write_urb->status != -EINPROGRESS)
1108                         retval = port->bulk_out_size;
1109         }
1110
1111         mutex_unlock(&qt_port->lock);
1112         return retval;
1113
1114 }
1115
1116 static int qt_ioctl(struct tty_struct *tty,
1117                     unsigned int cmd, unsigned long arg)
1118 {
1119         struct usb_serial_port *port = tty->driver_data;
1120         struct quatech_port *qt_port = qt_get_port_private(port);
1121         unsigned int index;
1122         char diff;
1123
1124         dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
1125
1126         index = port->port_number;
1127
1128         if (cmd == TIOCMIWAIT) {
1129                 while (qt_port != NULL) {
1130 #if 0
1131                         /* this never wakes up */
1132                         interruptible_sleep_on(&qt_port->msr_wait);
1133 #endif
1134                         if (signal_pending(current))
1135                                 return -ERESTARTSYS;
1136
1137                         diff = qt_port->diff_status;
1138
1139                         if (diff == 0)
1140                                 return -EIO;    /* no change => error */
1141
1142                         /* Consume all events */
1143                         qt_port->diff_status = 0;
1144
1145                         if (((arg & TIOCM_RNG)
1146                              && (diff & SERIAL_MSR_RI))
1147                             || ((arg & TIOCM_DSR)
1148                                 && (diff & SERIAL_MSR_DSR))
1149                             || ((arg & TIOCM_CD)
1150                                 && (diff & SERIAL_MSR_CD))
1151                             || ((arg & TIOCM_CTS)
1152                                 && (diff & SERIAL_MSR_CTS))) {
1153                                 return 0;
1154                         }
1155                 }
1156                 return 0;
1157         }
1158
1159         dev_dbg(&port->dev, "%s -No ioctl for that one.\n", __func__);
1160         return -ENOIOCTLCMD;
1161 }
1162
1163 static void qt_set_termios(struct tty_struct *tty,
1164                            struct usb_serial_port *port,
1165                            struct ktermios *old_termios)
1166 {
1167         struct ktermios *termios = &tty->termios;
1168         unsigned char new_lcr = 0;
1169         unsigned int cflag = termios->c_cflag;
1170         unsigned int index;
1171         int baud, divisor, remainder;
1172         int status;
1173
1174         index = port->port_number;
1175
1176         switch (cflag & CSIZE) {
1177         case CS5:
1178                 new_lcr |= SERIAL_5_DATA;
1179                 break;
1180         case CS6:
1181                 new_lcr |= SERIAL_6_DATA;
1182                 break;
1183         case CS7:
1184                 new_lcr |= SERIAL_7_DATA;
1185                 break;
1186         default:
1187                 termios->c_cflag &= ~CSIZE;
1188                 termios->c_cflag |= CS8;
1189         case CS8:
1190                 new_lcr |= SERIAL_8_DATA;
1191                 break;
1192         }
1193
1194         /* Parity stuff */
1195         if (cflag & PARENB) {
1196                 if (cflag & PARODD)
1197                         new_lcr |= SERIAL_ODD_PARITY;
1198                 else
1199                         new_lcr |= SERIAL_EVEN_PARITY;
1200         }
1201         if (cflag & CSTOPB)
1202                 new_lcr |= SERIAL_TWO_STOPB;
1203         else
1204                 new_lcr |= SERIAL_ONE_STOPB;
1205
1206         dev_dbg(&port->dev, "%s - 4\n", __func__);
1207
1208         /* Thats the LCR stuff, go ahead and set it */
1209         baud = tty_get_baud_rate(tty);
1210         if (!baud)
1211                 /* pick a default, any default... */
1212                 baud = 9600;
1213
1214         dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
1215
1216         divisor = MAX_BAUD_RATE / baud;
1217         remainder = MAX_BAUD_RATE % baud;
1218         /* Round to nearest divisor */
1219         if (((remainder * 2) >= baud) && (baud != 110))
1220                 divisor++;
1221
1222         /*
1223          * Set Baud rate to default and turn off (default)flow control here
1224          */
1225         status =
1226             qt_setuart(port->serial, index, (unsigned short)divisor, new_lcr);
1227         if (status < 0) {
1228                 dev_dbg(&port->dev, "qt_setuart failed\n");
1229                 return;
1230         }
1231
1232         /* Now determine flow control */
1233         if (cflag & CRTSCTS) {
1234                 dev_dbg(&port->dev, "%s - Enabling HW flow control\n",
1235                         __func__);
1236
1237                 /* Enable RTS/CTS flow control */
1238                 status = box_set_hw_flow_ctrl(port->serial, index, 1);
1239
1240                 if (status < 0) {
1241                         dev_dbg(&port->dev, "box_set_hw_flow_ctrl failed\n");
1242                         return;
1243                 }
1244         } else {
1245                 /* Disable RTS/CTS flow control */
1246                 dev_dbg(&port->dev,
1247                         "%s - disabling HW flow control\n", __func__);
1248
1249                 status = box_set_hw_flow_ctrl(port->serial, index, 0);
1250                 if (status < 0) {
1251                         dev_dbg(&port->dev, "box_set_hw_flow_ctrl failed\n");
1252                         return;
1253                 }
1254
1255         }
1256
1257         /* if we are implementing XON/XOFF, set the start and stop character in
1258          * the device */
1259         if (I_IXOFF(tty) || I_IXON(tty)) {
1260                 unsigned char stop_char = STOP_CHAR(tty);
1261                 unsigned char start_char = START_CHAR(tty);
1262
1263                 status = box_set_sw_flow_ctrl(port->serial, index, stop_char,
1264                                               start_char);
1265                 if (status < 0)
1266                         dev_dbg(&port->dev,
1267                                 "box_set_sw_flow_ctrl (enabled) failed\n");
1268
1269         } else {
1270                 /* disable SW flow control */
1271                 status = box_disable_sw_flow_ctrl(port->serial, index);
1272                 if (status < 0)
1273                         dev_dbg(&port->dev,
1274                                 "box_set_sw_flow_ctrl (diabling) failed\n");
1275
1276         }
1277         termios->c_cflag &= ~CMSPAR;
1278         /* FIXME:
1279            Error cases should be returning the actual bits changed only
1280         */
1281 }
1282
1283 static void qt_break(struct tty_struct *tty, int break_state)
1284 {
1285         struct usb_serial_port *port = tty->driver_data;
1286         struct usb_serial *serial = get_usb_serial(port, __func__);
1287         struct quatech_port *qt_port;
1288         u16 index, onoff;
1289         unsigned int result;
1290
1291         index = port->port_number;
1292
1293         qt_port = qt_get_port_private(port);
1294
1295         if (break_state == -1)
1296                 onoff = 1;
1297         else
1298                 onoff = 0;
1299
1300         mutex_lock(&qt_port->lock);
1301
1302         result =
1303             usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1304                             QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1305
1306         mutex_unlock(&qt_port->lock);
1307 }
1308
1309 static inline int qt_real_tiocmget(struct tty_struct *tty,
1310                                    struct usb_serial_port *port,
1311                                    struct usb_serial *serial)
1312 {
1313
1314         u8 mcr;
1315         u8 msr;
1316         unsigned int result = 0;
1317         int status;
1318         unsigned int index;
1319
1320         index = port->port_number;
1321         status =
1322             box_get_register(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1323         if (status >= 0) {
1324                 status =
1325                     box_get_register(port->serial, index,
1326                                    MODEM_STATUS_REGISTER, &msr);
1327
1328         }
1329
1330         if (status >= 0) {
1331                 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1332                     /* DTR IS SET */
1333                     | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1334                     /* RTS IS SET */
1335                     | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1336                     /* CTS is set */
1337                     | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1338                     /* Carrier detect is set */
1339                     | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1340                     /* Ring indicator set */
1341                     | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1342                 /* DSR is set */
1343                 return result;
1344
1345         } else
1346                 return -ESPIPE;
1347 }
1348
1349 static inline int qt_real_tiocmset(struct tty_struct *tty,
1350                                    struct usb_serial_port *port,
1351                                    struct usb_serial *serial,
1352                                    unsigned int value)
1353 {
1354
1355         u8 mcr;
1356         int status;
1357         unsigned int index;
1358
1359         index = port->port_number;
1360         status =
1361             box_get_register(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1362         if (status < 0)
1363                 return -ESPIPE;
1364
1365         /*
1366          * Turn off the RTS and DTR and loopback and then only turn on what was
1367          * asked for
1368          */
1369         mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1370         if (value & TIOCM_RTS)
1371                 mcr |= SERIAL_MCR_RTS;
1372         if (value & TIOCM_DTR)
1373                 mcr |= SERIAL_MCR_DTR;
1374         if (value & TIOCM_LOOP)
1375                 mcr |= SERIAL_MCR_LOOP;
1376
1377         status =
1378             box_set_register(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1379         if (status < 0)
1380                 return -ESPIPE;
1381
1382         return 0;
1383 }
1384
1385 static int qt_tiocmget(struct tty_struct *tty)
1386 {
1387         struct usb_serial_port *port = tty->driver_data;
1388         struct usb_serial *serial = get_usb_serial(port, __func__);
1389         struct quatech_port *qt_port = qt_get_port_private(port);
1390         int retval;
1391
1392         if (!serial)
1393                 return -ENODEV;
1394
1395         mutex_lock(&qt_port->lock);
1396         retval = qt_real_tiocmget(tty, port, serial);
1397         mutex_unlock(&qt_port->lock);
1398         return retval;
1399 }
1400
1401 static int qt_tiocmset(struct tty_struct *tty,
1402                        unsigned int set, unsigned int clear)
1403 {
1404
1405         struct usb_serial_port *port = tty->driver_data;
1406         struct usb_serial *serial = get_usb_serial(port, __func__);
1407         struct quatech_port *qt_port = qt_get_port_private(port);
1408         int retval;
1409
1410         if (!serial)
1411                 return -ENODEV;
1412
1413         mutex_lock(&qt_port->lock);
1414         retval = qt_real_tiocmset(tty, port, serial, set);
1415         mutex_unlock(&qt_port->lock);
1416         return retval;
1417 }
1418
1419 static void qt_throttle(struct tty_struct *tty)
1420 {
1421         struct usb_serial_port *port = tty->driver_data;
1422         struct usb_serial *serial = get_usb_serial(port, __func__);
1423         struct quatech_port *qt_port;
1424
1425         if (!serial)
1426                 return;
1427
1428         qt_port = qt_get_port_private(port);
1429
1430         mutex_lock(&qt_port->lock);
1431
1432         /* pass on to the driver specific version of this function */
1433         qt_port->rx_holding = 1;
1434
1435         mutex_unlock(&qt_port->lock);
1436 }
1437
1438 static void qt_submit_urb_from_unthrottle(struct usb_serial_port *port,
1439                                           struct usb_serial *serial)
1440 {
1441         int result;
1442
1443         /* Start reading from the device */
1444         usb_fill_bulk_urb(port->read_urb, serial->dev,
1445                           usb_rcvbulkpipe(serial->dev,
1446                                           port->bulk_in_endpointAddress),
1447                           port->read_urb->transfer_buffer,
1448                           port->read_urb->transfer_buffer_length,
1449                           qt_read_bulk_callback, port);
1450
1451         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1452
1453         if (result)
1454                 dev_err(&port->dev,
1455                         "%s - failed restarting read urb, error %d\n",
1456                         __func__, result);
1457 }
1458
1459 static void qt_unthrottle(struct tty_struct *tty)
1460 {
1461         struct usb_serial_port *port = tty->driver_data;
1462         struct usb_serial *serial = get_usb_serial(port, __func__);
1463         struct quatech_port *qt_port;
1464
1465         if (!serial)
1466                 return;
1467
1468         qt_port = qt_get_port_private(port);
1469
1470         mutex_lock(&qt_port->lock);
1471
1472         if (qt_port->rx_holding == 1) {
1473                 dev_dbg(&port->dev, "%s -qt_port->rx_holding == 1\n", __func__);
1474
1475                 qt_port->rx_holding = 0;
1476                 dev_dbg(&port->dev, "%s - qt_port->rx_holding = 0\n", __func__);
1477
1478                 /* if we have a bulk endpoint, start it up */
1479                 if ((serial->num_bulk_in) && (qt_port->read_bulk_stopped == 1))
1480                         qt_submit_urb_from_unthrottle(port, serial);
1481         }
1482         mutex_unlock(&qt_port->lock);
1483 }
1484
1485 static int qt_calc_num_ports(struct usb_serial *serial)
1486 {
1487         int num_ports;
1488
1489         num_ports =
1490             (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1491
1492         return num_ports;
1493 }
1494
1495 static struct usb_serial_driver quatech_device = {
1496         .driver = {
1497                    .owner = THIS_MODULE,
1498                    .name = "serqt",
1499                    },
1500         .description = DRIVER_DESC,
1501         .id_table = id_table,
1502         .num_ports = 8,
1503         .open = qt_open,
1504         .close = qt_close,
1505         .write = qt_write,
1506         .write_room = qt_write_room,
1507         .chars_in_buffer = qt_chars_in_buffer,
1508         .throttle = qt_throttle,
1509         .unthrottle = qt_unthrottle,
1510         .calc_num_ports = qt_calc_num_ports,
1511         .ioctl = qt_ioctl,
1512         .set_termios = qt_set_termios,
1513         .break_ctl = qt_break,
1514         .tiocmget = qt_tiocmget,
1515         .tiocmset = qt_tiocmset,
1516         .attach = qt_startup,
1517         .release = qt_release,
1518 };
1519
1520 static struct usb_serial_driver * const serial_drivers[] = {
1521         &quatech_device, NULL
1522 };
1523
1524 module_usb_serial_driver(serial_drivers, id_table);
1525
1526 MODULE_AUTHOR(DRIVER_AUTHOR);
1527 MODULE_DESCRIPTION(DRIVER_DESC);
1528 MODULE_LICENSE("GPL");