x86: Fix warning in pvclock.c
[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 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 tty_struct *tty, struct usb_serial_port *port,
1045                      struct file *filp)
1046 {
1047         struct usb_serial *serial = port->serial;
1048         struct quatech_port *qt_port;
1049         struct quatech_port *port0;
1050         int status;
1051         unsigned int index;
1052         status = 0;
1053
1054         dbg("%s - port %d\n", __func__, port->number);
1055         index = tty->index - serial->minor;
1056
1057         qt_port = qt_get_port_private(port);
1058         port0 = qt_get_port_private(serial->port[0]);
1059
1060         /* shutdown any bulk reads that might be going on */
1061         if (serial->num_bulk_out)
1062                 usb_unlink_urb(port->write_urb);
1063         if (serial->num_bulk_in)
1064                 usb_unlink_urb(port->read_urb);
1065
1066         /* wait up to for transmitter to empty */
1067         if (serial->dev)
1068                 qt_block_until_empty(tty, qt_port);
1069
1070         /* Close uart channel */
1071         status = qt_close_channel(serial, index);
1072         if (status < 0)
1073                 dbg("%s - port %d qt_close_channel failed.\n",
1074                     __func__, port->number);
1075
1076         port0->open_ports--;
1077
1078         dbg("qt_num_open_ports in close%d:in port%d\n",
1079             port0->open_ports, port->number);
1080
1081         if (port0->open_ports == 0) {
1082                 if (serial->port[0]->interrupt_in_urb) {
1083                         dbg("%s", "Shutdown interrupt_in_urb\n");
1084                         usb_kill_urb(serial->port[0]->interrupt_in_urb);
1085                 }
1086
1087         }
1088
1089         if (qt_port->write_urb) {
1090                 /* if this urb had a transfer buffer already (old tx) free it */
1091                 if (qt_port->write_urb->transfer_buffer != NULL)
1092                         kfree(qt_port->write_urb->transfer_buffer);
1093                 usb_free_urb(qt_port->write_urb);
1094         }
1095
1096 }
1097
1098 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1099                     const unsigned char *buf, int count)
1100 {
1101         int result;
1102         struct usb_serial *serial = get_usb_serial(port, __func__);
1103
1104         if (serial == NULL)
1105                 return -ENODEV;
1106
1107         dbg("%s - port %d\n", __func__, port->number);
1108
1109         if (count == 0) {
1110                 dbg("%s - write request of 0 bytes\n", __func__);
1111                 return 0;
1112         }
1113
1114         /* only do something if we have a bulk out endpoint */
1115         if (serial->num_bulk_out) {
1116                 if (port->write_urb->status == -EINPROGRESS) {
1117                         dbg("%s - already writing\n", __func__);
1118                         return 0;
1119                 }
1120
1121                 count =
1122                     (count > port->bulk_out_size) ? port->bulk_out_size : count;
1123                 memcpy(port->write_urb->transfer_buffer, buf, count);
1124
1125                 /* set up our urb */
1126
1127                 usb_fill_bulk_urb(port->write_urb, serial->dev,
1128                                   usb_sndbulkpipe(serial->dev,
1129                                                   port->
1130                                                   bulk_out_endpointAddress),
1131                                   port->write_urb->transfer_buffer, count,
1132                                   qt_write_bulk_callback, port);
1133
1134                 /* send the data out the bulk port */
1135                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1136                 if (result)
1137                         dbg("%s - failed submitting write urb, error %d\n",
1138                             __func__, result);
1139                 else
1140                         result = count;
1141
1142                 return result;
1143         }
1144
1145         /* no bulk out, so return 0 bytes written */
1146         return 0;
1147 }
1148
1149 static int qt_write_room(struct tty_struct *tty)
1150 {
1151         struct usb_serial_port *port = tty->driver_data;
1152         struct usb_serial *serial;
1153         struct quatech_port *qt_port;
1154         unsigned long flags;
1155
1156         int retval = -EINVAL;
1157
1158         if (port_paranoia_check(port, __func__)) {
1159                 dbg("%s", "Invalid port\n");
1160                 return -1;
1161         }
1162
1163         serial = get_usb_serial(port, __func__);
1164
1165         if (!serial)
1166                 return -ENODEV;
1167
1168         qt_port = qt_get_port_private(port);
1169
1170         spin_lock_irqsave(&qt_port->lock, flags);
1171
1172         dbg("%s - port %d\n", __func__, port->number);
1173
1174         if (serial->num_bulk_out) {
1175                 if (port->write_urb->status != -EINPROGRESS)
1176                         retval = port->bulk_out_size;
1177         }
1178
1179         spin_unlock_irqrestore(&qt_port->lock, flags);
1180         return retval;
1181
1182 }
1183
1184 static int qt_ioctl(struct tty_struct *tty, struct file *file,
1185                     unsigned int cmd, unsigned long arg)
1186 {
1187         struct usb_serial_port *port = tty->driver_data;
1188         struct quatech_port *qt_port = qt_get_port_private(port);
1189         struct usb_serial *serial = get_usb_serial(port, __func__);
1190         unsigned int index;
1191
1192         dbg("%s cmd 0x%04x", __func__, cmd);
1193
1194         index = tty->index - serial->minor;
1195
1196         if (cmd == TIOCMIWAIT) {
1197                 while (qt_port != NULL) {
1198                         interruptible_sleep_on(&qt_port->msr_wait);
1199                         if (signal_pending(current))
1200                                 return -ERESTARTSYS;
1201                         else {
1202                                 char diff = qt_port->diff_status;
1203
1204                                 if (diff == 0)
1205                                         return -EIO;    /* no change => error */
1206
1207                                 /* Consume all events */
1208                                 qt_port->diff_status = 0;
1209
1210                                 if (((arg & TIOCM_RNG)
1211                                      && (diff & SERIAL_MSR_RI))
1212                                     || ((arg & TIOCM_DSR)
1213                                         && (diff & SERIAL_MSR_DSR))
1214                                     || ((arg & TIOCM_CD)
1215                                         && (diff & SERIAL_MSR_CD))
1216                                     || ((arg & TIOCM_CTS)
1217                                         && (diff & SERIAL_MSR_CTS))) {
1218                                         return 0;
1219                                 }
1220                         }
1221                 }
1222                 return 0;
1223         }
1224
1225         dbg("%s -No ioctl for that one.  port = %d\n", __func__, port->number);
1226         return -ENOIOCTLCMD;
1227 }
1228
1229 static void qt_set_termios(struct tty_struct *tty,
1230                            struct usb_serial_port *port,
1231                            struct ktermios *old_termios)
1232 {
1233         struct ktermios *termios = tty->termios;
1234         unsigned char new_LCR = 0;
1235         unsigned int cflag = termios->c_cflag;
1236         unsigned int index;
1237         int baud, divisor, remainder;
1238         int status;
1239
1240         dbg("%s", __func__);
1241
1242         index = tty->index - port->serial->minor;
1243
1244         switch (cflag) {
1245         case CS5:
1246                 new_LCR |= SERIAL_5_DATA;
1247                 break;
1248         case CS6:
1249                 new_LCR |= SERIAL_6_DATA;
1250                 break;
1251         case CS7:
1252                 new_LCR |= SERIAL_7_DATA;
1253                 break;
1254         default:
1255         case CS8:
1256                 new_LCR |= SERIAL_8_DATA;
1257                 break;
1258         }
1259
1260         /* Parity stuff */
1261         if (cflag & PARENB) {
1262                 if (cflag & PARODD)
1263                         new_LCR |= SERIAL_ODD_PARITY;
1264                 else
1265                         new_LCR |= SERIAL_EVEN_PARITY;
1266         }
1267         if (cflag & CSTOPB)
1268                 new_LCR |= SERIAL_TWO_STOPB;
1269         else
1270                 new_LCR |= SERIAL_TWO_STOPB;
1271
1272         dbg("%s - 4\n", __func__);
1273
1274         /* Thats the LCR stuff, go ahead and set it */
1275         baud = tty_get_baud_rate(tty);
1276         if (!baud)
1277                 /* pick a default, any default... */
1278                 baud = 9600;
1279
1280         dbg("%s - got baud = %d\n", __func__, baud);
1281
1282         divisor = MAX_BAUD_RATE / baud;
1283         remainder = MAX_BAUD_RATE % baud;
1284         /* Round to nearest divisor */
1285         if (((remainder * 2) >= baud) && (baud != 110))
1286                 divisor++;
1287
1288         /*
1289          * Set Baud rate to default and turn off (default)flow control here
1290          */
1291         status =
1292             qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR);
1293         if (status < 0) {
1294                 dbg(__FILE__ "qt_setuart failed\n");
1295                 return;
1296         }
1297
1298         /* Now determine flow control */
1299         if (cflag & CRTSCTS) {
1300                 dbg("%s - Enabling HW flow control port %d\n", __func__,
1301                     port->number);
1302
1303                 /* Enable RTS/CTS flow control */
1304                 status = BoxSetHW_FlowCtrl(port->serial, index, 1);
1305
1306                 if (status < 0) {
1307                         dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1308                         return;
1309                 }
1310         } else {
1311                 /* Disable RTS/CTS flow control */
1312                 dbg("%s - disabling HW flow control port %d\n", __func__,
1313                     port->number);
1314
1315                 status = BoxSetHW_FlowCtrl(port->serial, index, 0);
1316                 if (status < 0) {
1317                         dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1318                         return;
1319                 }
1320
1321         }
1322
1323         /* if we are implementing XON/XOFF, set the start and stop character in
1324          * the device */
1325         if (I_IXOFF(tty) || I_IXON(tty)) {
1326                 unsigned char stop_char = STOP_CHAR(tty);
1327                 unsigned char start_char = START_CHAR(tty);
1328                 status =
1329                     BoxSetSW_FlowCtrl(port->serial, index, stop_char,
1330                                       start_char);
1331                 if (status < 0)
1332                         dbg(__FILE__ "BoxSetSW_FlowCtrl (enabled) failed\n");
1333
1334         } else {
1335                 /* disable SW flow control */
1336                 status = BoxDisable_SW_FlowCtrl(port->serial, index);
1337                 if (status < 0)
1338                         dbg(__FILE__ "BoxSetSW_FlowCtrl (diabling) failed\n");
1339
1340         }
1341         tty->termios->c_cflag &= ~CMSPAR;
1342         /* FIXME: Error cases should be returning the actual bits changed only */
1343 }
1344
1345 static void qt_break(struct tty_struct *tty, int break_state)
1346 {
1347         struct usb_serial_port *port = tty->driver_data;
1348         struct usb_serial *serial = get_usb_serial(port, __func__);
1349         struct quatech_port *qt_port;
1350         u16 index, onoff;
1351         unsigned int result;
1352         unsigned long flags;
1353
1354         index = tty->index - serial->minor;
1355
1356         qt_port = qt_get_port_private(port);
1357
1358         if (break_state == -1)
1359                 onoff = 1;
1360         else
1361                 onoff = 0;
1362
1363         spin_lock_irqsave(&qt_port->lock, flags);
1364
1365         dbg("%s - port %d\n", __func__, port->number);
1366
1367         result =
1368             usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1369                             QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1370
1371         spin_unlock_irqrestore(&qt_port->lock, flags);
1372 }
1373
1374 static inline int qt_real_tiocmget(struct tty_struct *tty,
1375                                    struct usb_serial_port *port,
1376                                    struct file *file, struct usb_serial *serial)
1377 {
1378
1379         u8 mcr;
1380         u8 msr;
1381         unsigned int result = 0;
1382         int status;
1383         unsigned int index;
1384
1385         dbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty);
1386
1387         index = tty->index - serial->minor;
1388         status =
1389             BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1390         if (status >= 0) {
1391                 status =
1392                     BoxGetRegister(port->serial, index,
1393                                    MODEM_STATUS_REGISTER, &msr);
1394
1395         }
1396
1397         if (status >= 0) {
1398                 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1399                     /* DTR IS SET */
1400                     | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1401                     /* RTS IS SET */
1402                     | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1403                     /* CTS is set */
1404                     | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1405                     /* Carrier detect is set */
1406                     | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1407                     /* Ring indicator set */
1408                     | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1409                 /* DSR is set */
1410                 return result;
1411
1412         } else
1413                 return -ESPIPE;
1414 }
1415
1416 static inline int qt_real_tiocmset(struct tty_struct *tty,
1417                                    struct usb_serial_port *port,
1418                                    struct file *file,
1419                                    struct usb_serial *serial,
1420                                    unsigned int value)
1421 {
1422
1423         u8 mcr;
1424         int status;
1425         unsigned int index;
1426
1427         dbg("%s - port %d\n", __func__, port->number);
1428
1429         index = tty->index - serial->minor;
1430         status =
1431             BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1432         if (status < 0)
1433                 return -ESPIPE;
1434
1435         /*
1436          * Turn off the RTS and DTR and loopbcck and then only turn on what was
1437          * asked for
1438          */
1439         mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1440         if (value & TIOCM_RTS)
1441                 mcr |= SERIAL_MCR_RTS;
1442         if (value & TIOCM_DTR)
1443                 mcr |= SERIAL_MCR_DTR;
1444         if (value & TIOCM_LOOP)
1445                 mcr |= SERIAL_MCR_LOOP;
1446
1447         status =
1448             BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1449         if (status < 0)
1450                 return -ESPIPE;
1451         else
1452                 return 0;
1453 }
1454
1455 static int qt_tiocmget(struct tty_struct *tty, struct file *file)
1456 {
1457         struct usb_serial_port *port = tty->driver_data;
1458         struct usb_serial *serial = get_usb_serial(port, __func__);
1459         struct quatech_port *qt_port = qt_get_port_private(port);
1460         int retval = -ENODEV;
1461         unsigned long flags;
1462
1463         dbg("In %s \n", __func__);
1464
1465         if (!serial)
1466                 return -ENODEV;
1467
1468         spin_lock_irqsave(&qt_port->lock, flags);
1469
1470         dbg("%s - port %d\n", __func__, port->number);
1471         dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1472
1473         retval = qt_real_tiocmget(tty, port, file, serial);
1474
1475         spin_unlock_irqrestore(&qt_port->lock, flags);
1476         return retval;
1477 }
1478
1479 static int qt_tiocmset(struct tty_struct *tty, struct file *file,
1480                        unsigned int set, unsigned int clear)
1481 {
1482
1483         struct usb_serial_port *port = tty->driver_data;
1484         struct usb_serial *serial = get_usb_serial(port, __func__);
1485         struct quatech_port *qt_port = qt_get_port_private(port);
1486         unsigned long flags;
1487         int retval = -ENODEV;
1488
1489         dbg("In %s \n", __func__);
1490
1491         if (!serial)
1492                 return -ENODEV;
1493
1494         spin_lock_irqsave(&qt_port->lock, flags);
1495
1496         dbg("%s - port %d \n", __func__, port->number);
1497         dbg("%s - qt_port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1498
1499         retval = qt_real_tiocmset(tty, port, file, serial, set);
1500
1501         spin_unlock_irqrestore(&qt_port->lock, flags);
1502         return retval;
1503 }
1504
1505 static void qt_throttle(struct tty_struct *tty)
1506 {
1507         struct usb_serial_port *port = tty->driver_data;
1508         struct usb_serial *serial = get_usb_serial(port, __func__);
1509         struct quatech_port *qt_port;
1510         unsigned long flags;
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         spin_lock_irqsave(&qt_port->lock, flags);
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         spin_unlock_irqrestore(&qt_port->lock, flags);
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 long flags;
1535         unsigned int result;
1536
1537         if (!serial)
1538                 return;
1539
1540         qt_port = qt_get_port_private(port);
1541
1542         spin_lock_irqsave(&qt_port->lock, flags);
1543
1544         dbg("%s - port %d\n", __func__, port->number);
1545
1546         if (qt_port->RxHolding == 1) {
1547                 dbg("%s -qt_port->RxHolding == 1\n", __func__);
1548
1549                 qt_port->RxHolding = 0;
1550                 dbg("%s - qt_port->RxHolding = 0\n", __func__);
1551
1552                 /* if we have a bulk endpoint, start it up */
1553                 if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1)) {
1554                         /* Start reading from the device */
1555                         usb_fill_bulk_urb(port->read_urb, serial->dev,
1556                                           usb_rcvbulkpipe(serial->dev,
1557                                                           port->bulk_in_endpointAddress),
1558                                           port->read_urb->transfer_buffer,
1559                                           port->read_urb->
1560                                           transfer_buffer_length,
1561                                           qt_read_bulk_callback, port);
1562                         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1563                         if (result)
1564                                 err("%s - failed restarting read urb, error %d",
1565                                     __func__, result);
1566                 }
1567         }
1568         spin_unlock_irqrestore(&qt_port->lock, flags);
1569         return;
1570
1571 }
1572
1573 static int qt_calc_num_ports(struct usb_serial *serial)
1574 {
1575         int num_ports;
1576
1577         dbg("numberofendpoints: %d \n",
1578             (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
1579         dbg("numberofendpoints: %d \n",
1580             (int)serial->interface->altsetting->desc.bNumEndpoints);
1581
1582         num_ports =
1583             (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1584
1585         return num_ports;
1586 }
1587
1588 static struct usb_serial_driver quatech_device = {
1589         .driver = {
1590                    .owner = THIS_MODULE,
1591                    .name = "serqt",
1592                    },
1593         .description = DRIVER_DESC,
1594         .usb_driver = &serqt_usb_driver,
1595         .id_table = serqt_id_table,
1596         .num_ports = 8,
1597         .open = qt_open,
1598         .close = qt_close,
1599         .write = qt_write,
1600         .write_room = qt_write_room,
1601         .chars_in_buffer = qt_chars_in_buffer,
1602         .throttle = qt_throttle,
1603         .unthrottle = qt_unthrottle,
1604         .calc_num_ports = qt_calc_num_ports,
1605         .ioctl = qt_ioctl,
1606         .set_termios = qt_set_termios,
1607         .break_ctl = qt_break,
1608         .tiocmget = qt_tiocmget,
1609         .tiocmset = qt_tiocmset,
1610         .attach = qt_startup,
1611         .release = qt_release,
1612 };
1613
1614 static int __init serqt_usb_init(void)
1615 {
1616         int retval;
1617
1618         dbg("%s\n", __func__);
1619
1620         /* register with usb-serial */
1621         retval = usb_serial_register(&quatech_device);
1622
1623         if (retval)
1624                 goto failed_usb_serial_register;
1625
1626         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1627                DRIVER_DESC "\n");
1628
1629         /* register with usb */
1630
1631         retval = usb_register(&serqt_usb_driver);
1632         if (retval == 0)
1633                 return 0;
1634
1635         /* if we're here, usb_register() failed */
1636         usb_serial_deregister(&quatech_device);
1637 failed_usb_serial_register:
1638         return retval;
1639 }
1640
1641 static void __exit serqt_usb_exit(void)
1642 {
1643         usb_deregister(&serqt_usb_driver);
1644         usb_serial_deregister(&quatech_device);
1645 }
1646
1647 module_init(serqt_usb_init);
1648 module_exit(serqt_usb_exit);
1649
1650 MODULE_AUTHOR(DRIVER_AUTHOR);
1651 MODULE_DESCRIPTION(DRIVER_DESC);
1652 MODULE_LICENSE("GPL");
1653
1654 module_param(debug, bool, S_IRUGO | S_IWUSR);
1655 MODULE_PARM_DESC(debug, "Debug enabled or not");