Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us