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