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