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