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