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