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