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