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