b06e90d3cefc6b06d7999fd39842e591d659cdd9
[safe/jmp/linux-2.6] / drivers / staging / quatech_usb2 / quatech_usb2.c
1 /*
2  * Driver for Quatech Inc USB2.0 to serial adaptors. Largely unrelated to the
3  * serqt_usb driver, based on a re-write of the vendor supplied serqt_usb2 code,
4  * which is unrelated to the serqt_usb2 in the staging kernel
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.00"
23 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
24 #define DRIVER_DESC "Quatech USB 2.0 to Serial Driver"
25
26 /* vendor and device IDs */
27 #define USB_VENDOR_ID_QUATECH 0x061d    /* Quatech VID */
28 #define QUATECH_SSU2_100 0xC120         /* RS232 single port */
29 #define QUATECH_DSU2_100 0xC140         /* RS232 dual port */
30 #define QUATECH_DSU2_400 0xC150         /* RS232/422/485 dual port */
31 #define QUATECH_QSU2_100 0xC160         /* RS232 four port */
32 #define QUATECH_QSU2_400 0xC170         /* RS232/422/485 four port */
33 #define QUATECH_ESU2_100 0xC1A0         /* RS232 eight port */
34 #define QUATECH_ESU2_400 0xC180         /* RS232/422/485 eight port */
35
36 /* magic numbers go here, when we find out which ones are needed */
37
38 #define QU2BOXPWRON 0x8000              /* magic number to turn FPGA power on */
39 #define QU2BOX232 0x40                  /* RS232 mode on MEI devices */
40 #define QU2BOXSPD9600 0x60              /* set speed to 9600 baud */
41 #define QT2_FIFO_DEPTH 1024                     /* size of hardware fifos */
42 #define QT2_TX_HEADER_LENGTH    5
43 /* length of the header sent to the box with each write URB */
44
45 /* directions for USB transfers */
46 #define USBD_TRANSFER_DIRECTION_IN    0xc0
47 #define USBD_TRANSFER_DIRECTION_OUT   0x40
48
49 /* special Quatech command IDs. These are pushed down the
50  USB control pipe to get the box on the end to do things */
51 #define QT_SET_GET_DEVICE               0xc2
52 #define QT_OPEN_CLOSE_CHANNEL           0xca
53 /*#define QT_GET_SET_PREBUF_TRIG_LVL    0xcc
54 #define QT_SET_ATF                      0xcd*/
55 #define QT2_GET_SET_REGISTER                    0xc0
56 #define QT2_GET_SET_UART                        0xc1
57 #define QT2_HW_FLOW_CONTROL_MASK                0xc5
58 #define QT2_SW_FLOW_CONTROL_MASK                0xc6
59 #define QT2_SW_FLOW_CONTROL_DISABLE             0xc7
60 #define QT2_BREAK_CONTROL                       0xc8
61 #define QT2_STOP_RECEIVE                        0xe0
62 #define QT2_FLUSH_DEVICE                        0xc4
63 #define QT2_GET_SET_QMCR                        0xe1
64
65 /* sorts of flush we can do on */
66 #define QT2_FLUSH_RX                    0x00
67 #define QT2_FLUSH_TX                    0x01
68
69 /* port setting constants, used to set up serial port speeds, flow
70  * control and so on */
71 #define QT2_SERIAL_MCR_DTR      0x01
72 #define QT2_SERIAL_MCR_RTS      0x02
73 #define QT2_SERIAL_MCR_LOOP     0x10
74
75 #define QT2_SERIAL_MSR_CTS      0x10
76 #define QT2_SERIAL_MSR_CD       0x80
77 #define QT2_SERIAL_MSR_RI       0x40
78 #define QT2_SERIAL_MSR_DSR      0x20
79 #define QT2_SERIAL_MSR_MASK     0xf0
80
81 #define QT2_SERIAL_8_DATA       0x03
82 #define QT2_SERIAL_7_DATA       0x02
83 #define QT2_SERIAL_6_DATA       0x01
84 #define QT2_SERIAL_5_DATA       0x00
85
86 #define QT2_SERIAL_ODD_PARITY   0X08
87 #define QT2_SERIAL_EVEN_PARITY  0X18
88 #define QT2_SERIAL_TWO_STOPB    0x04
89 #define QT2_SERIAL_ONE_STOPB    0x00
90
91 #define QT2_MAX_BAUD_RATE       921600
92 #define QT2_MAX_BAUD_REMAINDER  4608
93
94 #define QT2_SERIAL_LSR_OE       0x02
95 #define QT2_SERIAL_LSR_PE       0x04
96 #define QT2_SERIAL_LSR_FE       0x08
97 #define QT2_SERIAL_LSR_BI       0x10
98
99 /* value of Line Status Register when UART has completed
100  * emptying data out on the line */
101 #define QT2_LSR_TEMT     0x40
102
103 /* register numbers on each UART, for use with  qt2_box_[get|set]_register*/
104 #define  QT2_XMT_HOLD_REGISTER          0x00
105 #define  QT2_XVR_BUFFER_REGISTER        0x00
106 #define  QT2_FIFO_CONTROL_REGISTER      0x02
107 #define  QT2_LINE_CONTROL_REGISTER      0x03
108 #define  QT2_MODEM_CONTROL_REGISTER     0x04
109 #define  QT2_LINE_STATUS_REGISTER       0x05
110 #define  QT2_MODEM_STATUS_REGISTER      0x06
111
112 /* handy macros for doing escape sequence parsing on data reads */
113 #define THISCHAR        ((unsigned char *)(urb->transfer_buffer))[i]
114 #define NEXTCHAR        ((unsigned char *)(urb->transfer_buffer))[i + 1]
115 #define THIRDCHAR       ((unsigned char *)(urb->transfer_buffer))[i + 2]
116 #define FOURTHCHAR      ((unsigned char *)(urb->transfer_buffer))[i + 3]
117 #define FIFTHCHAR       ((unsigned char *)(urb->transfer_buffer))[i + 4]
118
119 static struct usb_device_id quausb2_id_table[] = {
120         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
121         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
122         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
123         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
124         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
125         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
126         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
127         {}      /* Terminating entry */
128 };
129
130 MODULE_DEVICE_TABLE(usb, quausb2_id_table);
131
132 /* custom structures we need go here */
133 static struct usb_driver quausb2_usb_driver = {
134         .name = "quatech-usb2-serial",
135         .probe = usb_serial_probe,
136         .disconnect = usb_serial_disconnect,
137         .id_table = quausb2_id_table,
138         .no_dynamic_id = 1,
139 };
140
141 /**
142  * quatech2_port: Structure in which to keep all the messy stuff that this
143  * driver needs alongside the usb_serial_port structure
144  * @read_urb_busy: Flag indicating that port->read_urb is in use
145  * @close_pending: flag indicating that this port is in the process of
146  * being closed (and so no new reads / writes should be started).
147  * @shadowLSR: Last received state of the line status register, holds the
148  * value of the line status flags from the port
149  * @shadowMSR: Last received state of the modem status register, holds
150  * the value of the modem status received from the port
151  * @rcv_flush: Flag indicating that a receive flush has occured on
152  * the hardware.
153  * @xmit_flush: Flag indicating that a transmit flush has been processed by
154  * the hardware.
155  * @tx_pending_bytes: Number of bytes waiting to be sent. This total
156  * includes the size (excluding header) of URBs that have been submitted but
157  * have not yet been sent to to the device, and bytes that have been sent out
158  * of the port but not yet reported sent by the "xmit_empty" messages (which
159  * indicate the number of bytes sent each time they are recieved, despite the
160  * misleading name).
161  * - Starts at zero when port is initialised.
162  * - is incremented by the size of the data to be written (no headers)
163  * each time a write urb is dispatched.
164  * - is decremented each time a "transmit empty" message is received
165  * by the driver in the data stream.
166  * @sem: Semaphore to lock access to this structure when we need to ensure that
167  * races don't occur to access bits of it.
168  * @open_count: The number of uses of the port currently having
169  * it open, i.e. the reference count.
170  */
171 struct quatech2_port {
172         int     magic;
173         bool    read_urb_busy;
174         bool    close_pending;
175         __u8    shadowLSR;
176         __u8    shadowMSR;
177         bool    rcv_flush;
178         bool    xmit_flush;
179         int     tx_pending_bytes;
180         struct semaphore        sem;
181         int     open_count;
182
183         char    active;         /* someone has this device open */
184         unsigned char           *xfer_to_tty_buffer;
185         wait_queue_head_t       wait;
186
187         __u8    shadowLCR;      /* last LCR value received */
188         __u8    shadowMCR;      /* last MCR value received */
189         char    RxHolding;
190         struct semaphore        pend_xmit_sem;  /* locks this structure */
191         spinlock_t lock;
192 };
193
194 /**
195  * Structure to hold device-wide internal status information
196  * @param ReadBulkStopped The last bulk read attempt ended in tears
197  * @param open_ports The number of serial ports currently in use on the box
198  * @param current_port Pointer to the serial port structure of the port which
199  * the read stream is currently directed to. Escape sequences in the read
200  * stream will change this around as data arrives from different ports on the
201  * box
202  * @buffer_size: The max size buffer each URB can take, used to set the size of
203  * the buffers allocated for writing to each port on the device (we need to
204  * store this because it is known only to the endpoint, but used each time a
205  * port is opened and a new buffer is allocated.
206  */
207 struct quatech2_dev {
208         bool    ReadBulkStopped;
209         char    open_ports;
210         struct usb_serial_port *current_port;
211         int     buffer_size;
212 };
213
214 /* structure which holds line and modem status flags */
215 struct qt2_status_data {
216         __u8 line_status;
217         __u8 modem_status;
218 };
219
220 /* Function prototypes */
221 static int qt2_boxpoweron(struct usb_serial *serial);
222 static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
223                         __u8 QMCR_Value);
224 static int port_paranoia_check(struct usb_serial_port *port,
225                         const char *function);
226 static int serial_paranoia_check(struct usb_serial *serial,
227                          const char *function);
228 static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
229                         *port);
230 static inline void qt2_set_port_private(struct usb_serial_port *port,
231                         struct quatech2_port *data);
232 static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
233                         *serial);
234 static inline void qt2_set_dev_private(struct usb_serial *serial,
235                         struct quatech2_dev *data);
236 static int qt2_openboxchannel(struct usb_serial *serial, __u16
237                         Uart_Number, struct qt2_status_data *pDeviceData);
238 static int qt2_closeboxchannel(struct usb_serial *serial, __u16
239                         Uart_Number);
240 static int qt2_conf_uart(struct usb_serial *serial,  unsigned short Uart_Number,
241                          unsigned short divisor, unsigned char LCR);
242 static void qt2_read_bulk_callback(struct urb *urb);
243 static void qt2_write_bulk_callback(struct urb *urb);
244 static void qt2_process_line_status(struct usb_serial_port *port,
245                               unsigned char LineStatus);
246 static void qt2_process_modem_status(struct usb_serial_port *port,
247                                unsigned char ModemStatus);
248 static void qt2_process_xmit_empty(struct usb_serial_port *port,
249         unsigned char fourth_char, unsigned char fifth_char);
250 static void qt2_process_port_change(struct usb_serial_port *port,
251                               unsigned char New_Current_Port);
252 static void qt2_process_rcv_flush(struct usb_serial_port *port);
253 static void qt2_process_xmit_flush(struct usb_serial_port *port);
254 static void qt2_process_rx_char(struct usb_serial_port *port,
255                                 unsigned char data);
256 static int qt2_box_get_register(struct usb_serial *serial,
257                 unsigned char uart_number, unsigned short register_num,
258                 __u8 *pValue);
259 static int qt2_box_set_register(struct usb_serial *serial,
260                 unsigned short Uart_Number, unsigned short Register_Num,
261                 unsigned short Value);
262 static int qt2_box_flush(struct usb_serial *serial,  unsigned char uart_number,
263                 unsigned short rcv_or_xmit);
264 static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
265                 unsigned short default_divisor, unsigned char default_LCR);
266 /*static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port,
267                       const unsigned char *buf, int count);
268 static int qt2_tiocmget(struct tty_struct *tty, struct file *file);
269 static int qt2_tiocmset(struct tty_struct *tty, struct file *file,
270                         unsigned int set, unsigned int clear);*/
271 static int qt2_boxsethw_flowctl(struct usb_serial *serial,
272                 unsigned int UartNumber, bool bSet);
273 static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
274                 unsigned char stop_char,  unsigned char start_char);
275 static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber);
276 static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
277                          unsigned short stop);
278
279 /* implementation functions, roughly in order of use, are here */
280 static int qt2_calc_num_ports(struct usb_serial *serial)
281 {
282         int num_ports;
283         int flag_as_400;
284         switch (serial->dev->descriptor.idProduct) {
285         case QUATECH_SSU2_100:
286                 num_ports = 1;
287                 break;
288
289         case QUATECH_DSU2_400:
290                 flag_as_400 = true;
291         case QUATECH_DSU2_100:
292                 num_ports = 2;
293         break;
294
295         case QUATECH_QSU2_400:
296                 flag_as_400 = true;
297         case QUATECH_QSU2_100:
298                 num_ports = 4;
299         break;
300
301         case QUATECH_ESU2_400:
302                 flag_as_400 = true;
303         case QUATECH_ESU2_100:
304                 num_ports = 8;
305         break;
306         default:
307         num_ports = 1;
308         break;
309         }
310         return num_ports;
311 }
312
313 static int qt2_attach(struct usb_serial *serial)
314 {
315         struct usb_serial_port *port;
316         struct quatech2_port *qt2_port; /* port-specific private data pointer */
317         struct quatech2_dev  *qt2_dev;  /* dev-specific private data pointer */
318         int i;
319         /* stuff for storing endpoint addresses now */
320         struct usb_endpoint_descriptor *endpoint;
321         struct usb_host_interface *iface_desc;
322         struct usb_serial_port *port0;  /* first port structure on device */
323
324         /* check how many endpoints there are on the device, for
325          * sanity's sake */
326         dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in",
327                         __func__, serial->num_bulk_in,
328                         serial->num_bulk_out, serial->num_interrupt_in);
329         if ((serial->num_bulk_in != 1) || (serial->num_bulk_out != 1)) {
330                 dbg("Device has wrong number of bulk endpoints!");
331                 return -ENODEV;
332         }
333         iface_desc = serial->interface->cur_altsetting;
334
335         /* Set up per-device private data, storing extra data alongside
336          * struct usb_serial */
337         qt2_dev = kzalloc(sizeof(*qt2_dev), GFP_KERNEL);
338         if (!qt2_dev) {
339                 dbg("%s: kmalloc for quatech2_dev failed!",
340                     __func__);
341                 return -ENOMEM;
342         }
343         qt2_dev->open_ports = 0;        /* no ports open */
344         qt2_set_dev_private(serial, qt2_dev);   /* store private data */
345
346         /* Now setup per port private data, which replaces all the things
347          * that quatech added to standard kernel structures in their driver */
348         for (i = 0; i < serial->num_ports; i++) {
349                 port = serial->port[i];
350                 qt2_port = kzalloc(sizeof(*qt2_port), GFP_KERNEL);
351                 if (!qt2_port) {
352                         dbg("%s: kmalloc for quatech2_port (%d) failed!.",
353                             __func__, i);
354                         return -ENOMEM;
355                 }
356                 /* initialise stuff in the structure */
357                 qt2_port->open_count = 0;       /* port is not open */
358                 spin_lock_init(&qt2_port->lock);
359                 qt2_set_port_private(port, qt2_port);
360         }
361
362         /* gain access to port[0]'s structure because we want to store
363          * device-level stuff in it */
364         if (serial_paranoia_check(serial, __func__))
365                 return -ENODEV;
366         port0 = serial->port[0]; /* get the first port's device structure */
367
368         /* print endpoint addresses so we can check them later
369          * by hand */
370         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
371                 endpoint = &iface_desc->endpoint[i].desc;
372                 if ((endpoint->bEndpointAddress & 0x80) &&
373                         ((endpoint->bmAttributes & 3) == 0x02)) {
374                         /* we found a bulk in endpoint */
375                         dbg("found bulk in at %#.2x",
376                                 endpoint->bEndpointAddress);
377                 }
378
379                 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
380                         ((endpoint->bmAttributes & 3) == 0x02)) {
381                         /* we found a bulk out endpoint */
382                         dbg("found bulk out at %#.2x",
383                                 endpoint->bEndpointAddress);
384                         qt2_dev->buffer_size = endpoint->wMaxPacketSize;
385                         /* max size of URB needs recording for the device */
386                 }
387         }       /* end printing endpoint addresses */
388
389         /* switch on power to the hardware */
390         if (qt2_boxpoweron(serial) < 0) {
391                 dbg("qt2_boxpoweron() failed");
392                 goto startup_error;
393         }
394         /* set all ports to RS232 mode */
395         for (i = 0; i < serial->num_ports; ++i) {
396                 if (qt2_boxsetQMCR(serial, i, QU2BOX232) < 0) {
397                         dbg("qt2_boxsetQMCR() on port %d failed",
398                                 i);
399                         goto startup_error;
400                 }
401         }
402
403         return 0;
404
405 startup_error:
406         for (i = 0; i < serial->num_ports; i++) {
407                 port = serial->port[i];
408                 qt2_port = qt2_get_port_private(port);
409                 kfree(qt2_port);
410                 qt2_set_port_private(port, NULL);
411         }
412         qt2_dev = qt2_get_dev_private(serial);
413         kfree(qt2_dev);
414         qt2_set_dev_private(serial, NULL);
415
416         dbg("Exit fail %s\n", __func__);
417         return -EIO;
418 }
419
420 static void qt2_release(struct usb_serial *serial)
421 {
422         struct usb_serial_port *port;
423         struct quatech2_port *qt_port;
424         int i;
425
426         dbg("enterting %s", __func__);
427
428         for (i = 0; i < serial->num_ports; i++) {
429                 port = serial->port[i];
430                 if (!port)
431                         continue;
432
433                 qt_port = usb_get_serial_port_data(port);
434                 kfree(qt_port);
435                 usb_set_serial_port_data(port, NULL);
436         }
437 }
438 /* This function is called once per serial port on the device, when
439  * that port is opened by a userspace application.
440  * The tty_struct and the usb_serial_port belong to this port,
441  * i.e. there are multiple ones for a multi-port device.
442  * However the usb_serial_port structure has a back-pointer
443  * to the parent usb_serial structure which belongs to the device,
444  * so we can access either the device-wide information or
445  * any other port's information (because there are also forward
446  * pointers) via that pointer.
447  * This is most helpful if the device shares resources (e.g. end
448  * points) between different ports
449  */
450 int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
451 {
452         struct usb_serial *serial;      /* device structure */
453         struct usb_serial_port *port0;  /* first port structure on device */
454         struct quatech2_port *port_extra;       /* extra data for this port */
455         struct quatech2_port *port0_extra;      /* extra data for first port */
456         struct quatech2_dev *dev_extra;         /* extra data for the device */
457         struct qt2_status_data ChannelData;
458         unsigned short default_divisor = QU2BOXSPD9600;
459         unsigned char  default_LCR = QT2_SERIAL_8_DATA;
460         int status;
461         int result;
462
463         if (port_paranoia_check(port, __func__))
464                 return -ENODEV;
465
466         dbg("%s(): port %d", __func__, port->number);
467
468         serial = port->serial;  /* get the parent device structure */
469         if (serial_paranoia_check(serial, __func__)) {
470                 dbg("usb_serial struct failed sanity check");
471                 return -ENODEV;
472         }
473         dev_extra = qt2_get_dev_private(serial);
474         /* get the device private data */
475         if (dev_extra == NULL) {
476                 dbg("device extra data pointer is null");
477                 return -ENODEV;
478         }
479         port0 = serial->port[0]; /* get the first port's device structure */
480         if (port_paranoia_check(port0, __func__)) {
481                 dbg("port0 usb_serial_port struct failed sanity check");
482                 return -ENODEV;
483         }
484
485         port_extra = qt2_get_port_private(port);
486         port0_extra = qt2_get_port_private(port0);
487         if (port_extra == NULL || port0_extra == NULL) {
488                 dbg("failed to get private data for port or port0");
489                 return -ENODEV;
490         }
491
492         /* FIXME: are these needed?  Does it even do anything useful? */
493         /* get the modem and line status values from the UART */
494         status = qt2_openboxchannel(serial, port->number,
495                         &ChannelData);
496         if (status < 0) {
497                 dbg("qt2_openboxchannel on channel %d failed",
498                     port->number);
499                 return status;
500         }
501         port_extra->shadowLSR = ChannelData.line_status &
502                         (QT2_SERIAL_LSR_OE | QT2_SERIAL_LSR_PE |
503                         QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
504         port_extra->shadowMSR = ChannelData.modem_status &
505                         (QT2_SERIAL_MSR_CTS | QT2_SERIAL_MSR_DSR |
506                         QT2_SERIAL_MSR_RI | QT2_SERIAL_MSR_CD);
507
508 /*      port_extra->fifo_empty_flag = true;*/
509         dbg("qt2_openboxchannel on channel %d completed.",
510             port->number);
511
512         /* Set Baud rate to default and turn off flow control here */
513         status = qt2_conf_uart(serial, port->number, default_divisor,
514                                 default_LCR);
515         if (status < 0) {
516                 dbg("qt2_conf_uart() failed on channel %d",
517                     port->number);
518                 return status;
519         }
520         dbg("qt2_conf_uart() completed on channel %d",
521                 port->number);
522
523         /*
524          * At this point we will need some end points to make further progress.
525          * Handlily, the correct endpoint addresses have been filled out into
526          * the usb_serial_port structure for us by the driver core, so we
527          * already have access to them.
528          * As there is only one bulk in and one bulk out end-point, these are in
529          * port[0]'s structure, and the rest are uninitialised. Handily,
530          * when we do a write to a port, we will use the same endpoint
531          * regardless of the port, with a 5-byte header added on to
532          * tell the box which port it should eventually come out of, so we only
533          * need the one set of endpoints. We will have one URB per port for
534          * writing, so that multiple ports can be writing at once.
535          * Finally we need a bulk in URB to use for background reads from the
536          * device, which will deal with uplink data from the box to host.
537          */
538         dbg("serial number is %d", port->serial->minor);
539         dbg("port0 bulk in endpoint is %#.2x", port0->bulk_in_endpointAddress);
540         dbg("port0 bulk out endpoint is %#.2x",
541                 port0->bulk_out_endpointAddress);
542
543         /* set up write_urb for bulk out transfers on this port. The USB
544          * serial framework will have allocated a blank URB, buffer etc for
545          * port0 when it put the endpoints there, but not for any of the other
546          * ports on the device because there are no more endpoints. Thus we
547          * have to allocate our own URBs for ports 1-7
548          */
549         if (port->write_urb == NULL) {
550                 dbg("port->write_urb == NULL, allocating one");
551                 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
552                 if (!port->write_urb) {
553                         err("Allocating write URB failed");
554                         return -ENOMEM;
555                 }
556                 /* buffer same size as port0 */
557                 port->bulk_out_size = dev_extra->buffer_size;
558                 port->bulk_out_buffer = kmalloc(port->bulk_out_size,
559                                                 GFP_KERNEL);
560                 if (!port->bulk_out_buffer) {
561                         err("Couldn't allocate bulk_out_buffer");
562                         return -ENOMEM;
563                 }
564         }
565         if (serial->dev == NULL)
566                 dbg("serial->dev == NULL");
567         dbg("port->bulk_out_size is %d", port->bulk_out_size);
568
569         usb_fill_bulk_urb(port->write_urb, serial->dev,
570                         usb_sndbulkpipe(serial->dev,
571                         port0->bulk_out_endpointAddress),
572                         port->bulk_out_buffer,
573                         port->bulk_out_size,
574                         qt2_write_bulk_callback,
575                         port);
576         port_extra->tx_pending_bytes = 0;
577
578         if (dev_extra->open_ports == 0) {
579                 /* this is first port to be opened, so need the read URB
580                  * initialised for bulk in transfers (this is shared amongst
581                  * all the ports on the device) */
582                 usb_fill_bulk_urb(port0->read_urb, serial->dev,
583                         usb_rcvbulkpipe(serial->dev,
584                         port0->bulk_in_endpointAddress),
585                         port0->bulk_in_buffer,
586                         port0->bulk_in_size,
587                         qt2_read_bulk_callback, serial);
588                 dbg("port0 bulk in URB intialised");
589
590                 /* submit URB, i.e. start reading from device (async) */
591                 dev_extra->ReadBulkStopped = false;
592                 port_extra->read_urb_busy = true;
593                 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
594                 if (result) {
595                         dev_err(&port->dev,
596                                  "%s(): Error %d submitting bulk in urb",
597                                 __func__, result);
598                         port_extra->read_urb_busy = false;
599                         dev_extra->ReadBulkStopped = true;
600                 }
601
602                 /* When the first port is opened, initialise the value of
603                  * current_port in dev_extra to this port, so it is set
604                  * to something. Once the box sends data it will send the
605                  * relevant escape sequences to get it to the right port anyway
606                  */
607                 dev_extra->current_port = port;
608         }
609
610         /* initialize our wait queues */
611         init_waitqueue_head(&port_extra->wait);
612         /* increment the count of openings of this port by one */
613         port_extra->open_count++;
614
615         /* remember to store dev_extra, port_extra and port0_extra back again at
616          * end !*/
617         qt2_set_port_private(port, port_extra);
618         qt2_set_port_private(serial->port[0], port0_extra);
619         qt2_set_dev_private(serial, dev_extra);
620
621         dev_extra->open_ports++; /* one more port opened */
622
623         return 0;
624 }
625
626 /* called when a port is closed by userspace. It won't be called, however,
627  * until calls to chars_in_buffer() reveal that the port has completed
628  * sending buffered data, and there is nothing else to do. Thus we don't have
629  * to rely on forcing data through in this function. */
630 /* Setting close_pending should keep new data from being written out,
631  * once all the data in the enpoint buffers is moved out we won't get
632  * any more. */
633 /* BoxStopReceive would keep any more data from coming from a given
634  * port, but isn't called by the vendor driver, although their comments
635  * mention it. Should it be used here to stop the inbound data
636  * flow?
637  */
638 static void qt2_close(struct usb_serial_port *port)
639 {
640         /* time out value for flush loops */
641         unsigned long jift;
642         struct quatech2_port *port_extra;       /* extra data for this port */
643         struct usb_serial *serial;      /* device structure */
644         struct quatech2_dev *dev_extra; /* extra data for the device */
645         __u8  lsr_value = 0;    /* value of Line Status Register */
646         int status;     /* result of last USB comms function */
647
648         dbg("%s(): port %d", __func__, port->number);
649         serial = port->serial;  /* get the parent device structure */
650         dev_extra = qt2_get_dev_private(serial);
651         /* get the device private data */
652         port_extra = qt2_get_port_private(port); /* port private data */
653
654         /* we don't need to force flush though the hardware, so we skip using
655          * qt2_box_flush() here */
656
657         /* we can now (and only now) stop reading data */
658         port_extra->close_pending = true;
659         dbg("%s(): port_extra->close_pending = true", __func__);
660         /* although the USB side is now empty, the UART itself may
661          * still be pushing characters out over the line, so we have to
662          * wait testing the actual line status until the lines change
663          * indicating that the data is done transfering. */
664         /* FIXME: slow this polling down so it doesn't run the USB bus flat out
665          * if it actually has to spend any time in this loop (which it normally
666          * doesn't because the buffer is nearly empty) */
667         jift = jiffies + (10 * HZ);     /* 10 sec timeout */
668         do {
669                 status = qt2_box_get_register(serial, port->number,
670                         QT2_LINE_STATUS_REGISTER, &lsr_value);
671                 if (status < 0) {
672                         dbg("%s(): qt2_box_get_register failed", __func__);
673                         break;
674                 }
675                 if ((lsr_value & QT2_LSR_TEMT)) {
676                         dbg("UART done sending");
677                         break;
678                 }
679                 schedule();
680         } while (jiffies <= jift);
681
682         status = qt2_closeboxchannel(serial, port->number);
683         if (status < 0)
684                 dbg("%s(): port %d qt2_box_open_close_channel failed",
685                         __func__, port->number);
686         /* to avoid leaking URBs, we should now free the write_urb for this
687          * port and set the pointer to null so that next time the port is opened
688          * a new URB is allocated. This avoids leaking URBs when the device is
689          * removed */
690         usb_free_urb(port->write_urb);
691         kfree(port->bulk_out_buffer);
692         port->bulk_out_buffer = NULL;
693         port->bulk_out_size = 0;
694
695         /* decrement the count of openings of this port by one */
696         port_extra->open_count--;
697         /* one less overall open as well */
698         dev_extra->open_ports--;
699         dbg("%s(): Exit, dev_extra->open_ports  = %d", __func__,
700                 dev_extra->open_ports);
701 }
702
703 /* called to deliver writes from the next layer up to the device */
704 static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port,
705                 const unsigned char *buf, int count)
706 {
707         struct usb_serial *serial;      /* parent device struct */
708         __u8 header_array[5];   /* header used to direct writes to the correct
709         port on the device */
710         struct quatech2_port *port_extra;       /* extra data for this port */
711
712         int result;
713
714         /* get the parent device of the port */
715         serial = port->serial;
716         if (serial == NULL)
717                 return -ENODEV;
718         dbg("%s(): port %d", __func__, port->number);
719
720         if (count <= 0) {
721                 dbg("%s(): write request of <= 0 bytes", __func__);
722                 return 0;       /* no bytes written */
723         }
724         port_extra = qt2_get_port_private(port);
725
726         /* check if the write urb is already in use, i.e. data already being
727          * sent to this port */
728         if ((port->write_urb->status == -EINPROGRESS)) {
729                 /* Fifo hasn't been emptied since last write to this port */
730                 dbg("%s(): already writing, port->write_urb->status == "
731                         "-EINPROGRESS", __func__);
732                 /* schedule_work(&port->work); commented in vendor driver */
733                 return 0;
734         } else if (port_extra->tx_pending_bytes >= QT2_FIFO_DEPTH) {
735                 /* buffer is full (==). > should not occur, but would indicate
736                  * that an overflow had occured */
737                 dbg("%s(): port transmit buffer is full!", __func__);
738                 /* schedule_work(&port->work); commented in vendor driver */
739                 return 0;
740         }
741
742         /* We must fill the first 5 bytes of anything we sent with a transmit
743          * header which directes the data to the correct port. The maximum
744          * size we can send out in one URB is port->bulk_out_size, which caps
745          * the number of bytes of real data we can send in each write. As the
746          * semantics of write allow us to write less than we were give, we cap
747          * the maximum we will ever write to the device as 5 bytes less than
748          * one URB's worth, by reducing the value of the count argument
749          * appropriately*/
750         if (count > port->bulk_out_size - QT2_TX_HEADER_LENGTH)
751                 count = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
752         /* we must also ensure that the FIFO at the other end can cope with the
753          * URB we send it, otherwise it will have problems. As above, we can
754          * restrict the write size by just shrinking count.*/
755         if (count > (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes))
756                 count = QT2_FIFO_DEPTH - port_extra->tx_pending_bytes;
757         /* now build the header for transmission */
758         header_array[0] = 0x1b;
759         header_array[1] = 0x1b;
760         header_array[2] = (__u8)port->number;
761         header_array[3] = (__u8)count;
762         header_array[4] = (__u8)count >> 8;
763         /* copy header into URB */
764         memcpy(port->write_urb->transfer_buffer, header_array,
765                 QT2_TX_HEADER_LENGTH);
766         /* and actual data to write */
767         memcpy(port->write_urb->transfer_buffer + 5, buf, count);
768
769         dbg("%s(): first data byte to send = %#.2x", __func__, *buf);
770
771         /* set up our urb */
772         usb_fill_bulk_urb(port->write_urb, serial->dev,
773                         usb_sndbulkpipe(serial->dev,
774                         port->bulk_out_endpointAddress),
775                         port->write_urb->transfer_buffer, count + 5,
776                         (qt2_write_bulk_callback), port);
777         /* send the data out the bulk port */
778         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
779         if (result) {
780                 /* error couldn't submit urb */
781                 result = 0;
782                 dbg("%s(): failed submitting write urb, error %d",
783                         __func__, result);
784         } else {
785                 port_extra->tx_pending_bytes += (count - QT2_TX_HEADER_LENGTH);
786                 /*port->fifo_empty_flag = false;
787                 port->xmit_fifo_room_bytes = FIFO_DEPTH -
788                 port->xmit_pending_bytes;*/
789                 result = count;
790                 dbg("%s(): submitted write urb, returning %d",
791                         __func__, result);
792         }
793         return result;
794 }
795
796 /* This is used by the next layer up to know how much space is available
797  * in the buffer on the device. It is used on a device closure to avoid
798  * calling close() until the buffer is reported to be empty.
799  * The returned value must never go down by more than the number of bytes
800  * written for correct behaviour further up the driver stack, i.e. if I call
801  * it, then write 6 bytes, then call again I should get 6 less, or possibly
802  * only 5 less if one was written in the meantime, etc. I should never get 7
803  * less (or any bigger number) because I only wrote 6 bytes.
804  */
805 static int qt2_write_room(struct tty_struct *tty)
806 {
807         struct usb_serial_port *port = tty->driver_data;
808                 /* parent usb_serial_port pointer */
809         struct quatech2_port *port_extra;       /* extra data for this port */
810         int room = 0;
811         port_extra = qt2_get_port_private(port);
812
813         if (port_extra->close_pending == true) {
814                 dbg("%s(): port_extra->close_pending == true", __func__);
815                 return -ENODEV;
816         }
817         /* Q: how many bytes would a write() call actually succeed in writing
818          * if it happened now?
819          * A: one QT2_FIFO_DEPTH, less the number of bytes waiting to be sent
820          * out of the port, unless this is more than the size of the
821          * write_urb output buffer less the header, which is the maximum
822          * size write we can do.
823
824          * Most of the implementation of this is done when writes to the device
825          * are started or terminate. When we send a write to the device, we
826          * reduce the free space count by the size of the dispatched write.
827          * When a "transmit empty" message comes back up the USB read stream,
828          * we decrement the count by the number of bytes reported sent, thus
829          * keeping track of the difference between sent and recieved bytes.
830          */
831
832         room = (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes);
833         /* space in FIFO */
834         if (room > port->bulk_out_size - QT2_TX_HEADER_LENGTH)
835                 room = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
836         /* if more than the URB can hold, then cap to that limit */
837
838         dbg("%s(): port %d: write room is %d", __func__, port->number, room);
839         return room;
840 }
841
842 static int qt2_chars_in_buffer(struct tty_struct *tty)
843 {
844         struct usb_serial_port *port = tty->driver_data;
845         /* parent usb_serial_port pointer */
846         struct quatech2_port *port_extra;       /* extra data for this port */
847         port_extra = qt2_get_port_private(port);
848
849         dbg("%s(): port %d: chars_in_buffer = %d", __func__,
850                 port->number, port_extra->tx_pending_bytes);
851         return port_extra->tx_pending_bytes;
852 }
853
854 /* called when userspace does an ioctl() on the device. Note that
855  * TIOCMGET and TIOCMSET are filtered off to their own methods before they get
856  * here, so we don't have to handle them.
857  */
858 static int qt2_ioctl(struct tty_struct *tty, struct file *file,
859                      unsigned int cmd, unsigned long arg)
860 {
861         struct usb_serial_port *port = tty->driver_data;
862         struct usb_serial *serial = port->serial;
863         __u8 mcr_value; /* Modem Control Register value */
864         __u8 msr_value; /* Modem Status Register value */
865         unsigned short prev_msr_value; /* Previous value of Modem Status
866          * Register used to implement waiting for a line status change to
867          * occur */
868         struct quatech2_port *port_extra;       /* extra data for this port */
869         DECLARE_WAITQUEUE(wait, current);
870         /* Declare a wait queue named "wait" */
871
872         unsigned int value;
873         unsigned int UartNumber;
874
875         if (serial == NULL)
876                 return -ENODEV;
877         UartNumber = tty->index - serial->minor;
878         port_extra = qt2_get_port_private(port);
879
880         dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__,
881             port->number, UartNumber, tty);
882
883         if (cmd == TIOCMBIS || cmd == TIOCMBIC) {
884                 if (qt2_box_get_register(port->serial, UartNumber,
885                         QT2_MODEM_CONTROL_REGISTER, &mcr_value) < 0)
886                         return -ESPIPE;
887                 if (copy_from_user(&value, (unsigned int *)arg,
888                         sizeof(value)))
889                         return -EFAULT;
890
891                 switch (cmd) {
892                 case TIOCMBIS:
893                         if (value & TIOCM_RTS)
894                                 mcr_value |= QT2_SERIAL_MCR_RTS;
895                         if (value & TIOCM_DTR)
896                                 mcr_value |= QT2_SERIAL_MCR_DTR;
897                         if (value & TIOCM_LOOP)
898                                 mcr_value |= QT2_SERIAL_MCR_LOOP;
899                 break;
900                 case TIOCMBIC:
901                         if (value & TIOCM_RTS)
902                                 mcr_value &= ~QT2_SERIAL_MCR_RTS;
903                         if (value & TIOCM_DTR)
904                                 mcr_value &= ~QT2_SERIAL_MCR_DTR;
905                         if (value & TIOCM_LOOP)
906                                 mcr_value &= ~QT2_SERIAL_MCR_LOOP;
907                 break;
908                 default:
909                 break;
910                 }       /* end of local switch on cmd */
911                 if (qt2_box_set_register(port->serial,  UartNumber,
912                     QT2_MODEM_CONTROL_REGISTER, mcr_value) < 0) {
913                         return -ESPIPE;
914                 } else {
915                         port_extra->shadowMCR = mcr_value;
916                         return 0;
917                 }
918         } else if (cmd == TIOCMIWAIT) {
919                 dbg("%s() port %d, cmd == TIOCMIWAIT enter",
920                         __func__, port->number);
921                 prev_msr_value = port_extra->shadowMSR  & QT2_SERIAL_MSR_MASK;
922                 while (1) {
923                         add_wait_queue(&port_extra->wait, &wait);
924                         set_current_state(TASK_INTERRUPTIBLE);
925                         schedule();
926                         dbg("%s(): port %d, cmd == TIOCMIWAIT here\n",
927                                 __func__, port->number);
928                         remove_wait_queue(&port_extra->wait, &wait);
929                         /* see if a signal woke us up */
930                         if (signal_pending(current))
931                                 return -ERESTARTSYS;
932                         msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
933                         if (msr_value == prev_msr_value)
934                                 return -EIO;  /* no change - error */
935                         if ((arg & TIOCM_RNG &&
936                                 ((prev_msr_value & QT2_SERIAL_MSR_RI) ==
937                                         (msr_value & QT2_SERIAL_MSR_RI))) ||
938                                 (arg & TIOCM_DSR &&
939                                 ((prev_msr_value & QT2_SERIAL_MSR_DSR) ==
940                                         (msr_value & QT2_SERIAL_MSR_DSR))) ||
941                                 (arg & TIOCM_CD &&
942                                 ((prev_msr_value & QT2_SERIAL_MSR_CD) ==
943                                         (msr_value & QT2_SERIAL_MSR_CD))) ||
944                                 (arg & TIOCM_CTS &&
945                                 ((prev_msr_value & QT2_SERIAL_MSR_CTS) ==
946                                         (msr_value & QT2_SERIAL_MSR_CTS)))) {
947                                 return 0;
948                         }
949                 } /* end inifinite while */
950                 /* FIXME: This while loop needs a way to break out if the device
951                  * is disconnected while a process is waiting for the MSR to
952                  * change, because once it's disconnected, it isn't going to
953                  * change state ... */
954         } else {
955                 /* any other ioctls we don't know about come here */
956                 dbg("%s(): No ioctl for that one. port = %d", __func__,
957                         port->number);
958                 return -ENOIOCTLCMD;
959         }
960 }
961
962 /* Called when the user wishes to change the port settings using the termios
963  * userspace interface */
964 static void qt2_set_termios(struct tty_struct *tty,
965         struct usb_serial_port *port, struct ktermios *old_termios)
966 {
967         struct usb_serial *serial; /* parent serial device */
968         int baud, divisor, remainder;
969         unsigned char LCR_change_to = 0;
970         int status;
971         __u16 UartNumber;
972
973         dbg("%s(): port %d", __func__, port->number);
974
975         serial = port->serial;
976
977         UartNumber = port->number;
978
979         if (old_termios) {
980                 if ((tty->termios->c_cflag == old_termios->c_cflag) &&
981                         (RELEVANT_IFLAG(tty->termios->c_iflag) ==
982                         RELEVANT_IFLAG(old_termios->c_iflag))) {
983                         dbg("%s(): Nothing to change", __func__);
984                         return;
985                 }
986         }
987
988         switch (tty->termios->c_cflag) {
989         case CS5:
990                 LCR_change_to |= QT2_SERIAL_5_DATA;
991                 break;
992         case CS6:
993                 LCR_change_to |= QT2_SERIAL_6_DATA;
994                 break;
995         case CS7:
996                 LCR_change_to |= QT2_SERIAL_7_DATA;
997                 break;
998         default:
999         case CS8:
1000                 LCR_change_to |= QT2_SERIAL_8_DATA;
1001                 break;
1002         }
1003
1004         /* Parity stuff */
1005         if (tty->termios->c_cflag & PARENB) {
1006                 if (tty->termios->c_cflag & PARODD)
1007                         LCR_change_to |= QT2_SERIAL_ODD_PARITY;
1008                 else
1009                         LCR_change_to |= QT2_SERIAL_EVEN_PARITY;
1010         }
1011         if (tty->termios->c_cflag & CSTOPB)
1012                 LCR_change_to |= QT2_SERIAL_TWO_STOPB;
1013         else
1014                 LCR_change_to |= QT2_SERIAL_ONE_STOPB;
1015
1016         /* Thats the LCR stuff, go ahead and set it */
1017         baud = tty_get_baud_rate(tty);
1018         if (!baud) {
1019                 /* pick a default, any default... */
1020                 baud = 9600;
1021         }
1022         dbg("%s(): got baud = %d", __func__, baud);
1023
1024         divisor = QT2_MAX_BAUD_RATE / baud;
1025         remainder = QT2_MAX_BAUD_RATE % baud;
1026         /* Round to nearest divisor */
1027         if (((remainder * 2) >= baud) && (baud != 110))
1028                 divisor++;
1029         dbg("%s(): setting divisor = %d, QT2_MAX_BAUD_RATE = %d , LCR = %#.2x",
1030               __func__, divisor, QT2_MAX_BAUD_RATE, LCR_change_to);
1031
1032         status = qt2_boxsetuart(serial, UartNumber, (unsigned short) divisor,
1033                             LCR_change_to);
1034         if (status < 0) {
1035                 dbg("qt2_boxsetuart() failed");
1036                 return;
1037         }
1038
1039         /* Now determine flow control */
1040         if (tty->termios->c_cflag & CRTSCTS) {
1041                 dbg("%s(): Enabling HW flow control port %d", __func__,
1042                       port->number);
1043                 /* Enable  RTS/CTS flow control */
1044                 status = qt2_boxsethw_flowctl(serial, UartNumber, true);
1045                 if (status < 0) {
1046                         dbg("qt2_boxsethw_flowctl() failed");
1047                         return;
1048                 }
1049         } else {
1050                 /* Disable RTS/CTS flow control */
1051                 dbg("%s(): disabling HW flow control port %d", __func__,
1052                         port->number);
1053                 status = qt2_boxsethw_flowctl(serial, UartNumber, false);
1054                 if (status < 0) {
1055                         dbg("qt2_boxsethw_flowctl failed");
1056                         return;
1057                 }
1058         }
1059         /* if we are implementing XON/XOFF, set the start and stop character
1060          * in the device */
1061         if (I_IXOFF(tty) || I_IXON(tty)) {
1062                 unsigned char stop_char  = STOP_CHAR(tty);
1063                 unsigned char start_char = START_CHAR(tty);
1064                 status = qt2_boxsetsw_flowctl(serial, UartNumber, stop_char,
1065                                 start_char);
1066                 if (status < 0)
1067                         dbg("qt2_boxsetsw_flowctl (enabled) failed");
1068         } else {
1069                 /* disable SW flow control */
1070                 status = qt2_boxunsetsw_flowctl(serial, UartNumber);
1071                 if (status < 0)
1072                         dbg("qt2_boxunsetsw_flowctl (disabling) failed");
1073         }
1074 }
1075
1076 static int qt2_tiocmget(struct tty_struct *tty, struct file *file)
1077 {
1078         struct usb_serial_port *port = tty->driver_data;
1079         struct usb_serial *serial = port->serial;
1080
1081         __u8 mcr_value; /* Modem Control Register value */
1082         __u8 msr_value; /* Modem Status Register value */
1083         unsigned int result = 0;
1084         int status;
1085         unsigned int UartNumber;
1086
1087         if (serial == NULL)
1088                 return -ENODEV;
1089
1090         dbg("%s(): port %d, tty =0x%p", __func__, port->number, tty);
1091         UartNumber = tty->index - serial->minor;
1092         dbg("UartNumber is %d", UartNumber);
1093
1094         status = qt2_box_get_register(port->serial, UartNumber,
1095                         QT2_MODEM_CONTROL_REGISTER,     &mcr_value);
1096         if (status >= 0) {
1097                 status = qt2_box_get_register(port->serial,  UartNumber,
1098                                 QT2_MODEM_STATUS_REGISTER, &msr_value);
1099         }
1100         if (status >= 0) {
1101                 result = ((mcr_value & QT2_SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1102                                 /*DTR set */
1103                         | ((mcr_value & QT2_SERIAL_MCR_RTS)  ? TIOCM_RTS : 0)
1104                                 /*RTS set */
1105                         | ((msr_value & QT2_SERIAL_MSR_CTS)  ? TIOCM_CTS : 0)
1106                                 /* CTS set */
1107                         | ((msr_value & QT2_SERIAL_MSR_CD)  ? TIOCM_CAR : 0)
1108                                 /*Carrier detect set */
1109                         | ((msr_value & QT2_SERIAL_MSR_RI)  ? TIOCM_RI : 0)
1110                                 /* Ring indicator set */
1111                         | ((msr_value & QT2_SERIAL_MSR_DSR)  ? TIOCM_DSR : 0);
1112                                 /* DSR set */
1113                 return result;
1114         } else {
1115                 return -ESPIPE;
1116         }
1117 }
1118
1119 static int qt2_tiocmset(struct tty_struct *tty, struct file *file,
1120                        unsigned int set, unsigned int clear)
1121 {
1122         struct usb_serial_port *port = tty->driver_data;
1123         struct usb_serial *serial = port->serial;
1124         __u8 mcr_value; /* Modem Control Register value */
1125         int status;
1126         unsigned int UartNumber;
1127
1128         if (serial == NULL)
1129                 return -ENODEV;
1130
1131         UartNumber = tty->index - serial->minor;
1132         dbg("%s(): port %d, UartNumber %d", __func__, port->number, UartNumber);
1133
1134         status = qt2_box_get_register(port->serial, UartNumber,
1135                         QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1136         if (status < 0)
1137                 return -ESPIPE;
1138
1139         /* Turn off RTS, DTR and loopback, then only turn on what was asked
1140          * for */
1141         mcr_value &= ~(QT2_SERIAL_MCR_RTS | QT2_SERIAL_MCR_DTR |
1142                         QT2_SERIAL_MCR_LOOP);
1143         if (set & TIOCM_RTS)
1144                 mcr_value |= QT2_SERIAL_MCR_RTS;
1145         if (set & TIOCM_DTR)
1146                 mcr_value |= QT2_SERIAL_MCR_DTR;
1147         if (set & TIOCM_LOOP)
1148                 mcr_value |= QT2_SERIAL_MCR_LOOP;
1149
1150         status = qt2_box_set_register(port->serial, UartNumber,
1151                         QT2_MODEM_CONTROL_REGISTER, mcr_value);
1152         if (status < 0)
1153                 return -ESPIPE;
1154         else
1155                 return 0;
1156 }
1157
1158 /** qt2_break - Turn BREAK on and off on the UARTs
1159  */
1160 static void qt2_break(struct tty_struct *tty, int break_state)
1161 {
1162         struct usb_serial_port *port = tty->driver_data; /* parent port */
1163         struct usb_serial *serial = port->serial;       /* parent device */
1164         struct quatech2_port *port_extra;       /* extra data for this port */
1165         __u16 break_value;
1166         unsigned int result;
1167
1168         port_extra = qt2_get_port_private(port);
1169         if (!serial) {
1170                 dbg("%s(): port %d: no serial object", __func__, port->number);
1171                 return;
1172         }
1173
1174         if (break_state == -1)
1175                 break_value = 1;
1176         else
1177                 break_value = 0;
1178         dbg("%s(): port %d, break_value %d", __func__, port->number,
1179                 break_value);
1180
1181         down(&port_extra->sem);
1182         if (!port_extra->open_count) {
1183                 dbg("%s(): port not open", __func__);
1184                 goto exit;
1185         }
1186
1187         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1188                                 QT2_BREAK_CONTROL, 0x40, break_value,
1189                                 port->number, NULL, 0, 300);
1190 exit:
1191         up(&port_extra->sem);
1192         dbg("%s(): exit port %d", __func__, port->number);
1193
1194 }
1195 /**
1196  * qt2_throttle: - stop reading new data from the port
1197  */
1198 static void qt2_throttle(struct tty_struct *tty)
1199 {
1200         struct usb_serial_port *port = tty->driver_data;
1201         struct usb_serial *serial = port->serial;
1202         struct quatech2_port *port_extra;       /* extra data for this port */
1203         dbg("%s(): port %d", __func__, port->number);
1204
1205         port_extra = qt2_get_port_private(port);
1206         if (!serial) {
1207                 dbg("%s(): enter port %d no serial object", __func__,
1208                       port->number);
1209                 return;
1210         }
1211
1212         down(&port_extra->sem); /* lock structure */
1213         if (!port_extra->open_count) {
1214                 dbg("%s(): port not open", __func__);
1215                 goto exit;
1216         }
1217         /* Send command to box to stop receiving stuff. This will stop this
1218          * particular UART from filling the endpoint - in the multiport case the
1219          * FPGA UART will handle any flow control implmented, but for the single
1220          * port it's handed differently and we just quit submitting urbs
1221          */
1222         if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100)
1223                 qt2_boxstoprx(serial, port->number, 1);
1224
1225         port->throttled = 1;
1226 exit:
1227         up(&port_extra->sem);
1228         dbg("%s(): port %d: setting port->throttled", __func__, port->number);
1229         return;
1230 }
1231
1232 /**
1233  * qt2_unthrottle: - start receiving data through the port again after being
1234  * throttled
1235  */
1236 static void qt2_unthrottle(struct tty_struct *tty)
1237 {
1238         struct usb_serial_port *port = tty->driver_data;
1239         struct usb_serial *serial = port->serial;
1240         struct quatech2_port *port_extra;       /* extra data for this port */
1241         struct usb_serial_port *port0;  /* first port structure on device */
1242         struct quatech2_dev *dev_extra;         /* extra data for the device */
1243
1244         if (!serial) {
1245                 dbg("%s() enter port %d no serial object!", __func__,
1246                         port->number);
1247                 return;
1248         }
1249         dbg("%s(): enter port %d", __func__, port->number);
1250         dev_extra = qt2_get_dev_private(serial);
1251         port_extra = qt2_get_port_private(port);
1252         port0 = serial->port[0]; /* get the first port's device structure */
1253
1254         down(&port_extra->sem);
1255         if (!port_extra->open_count) {
1256                 dbg("%s(): port %d not open", __func__, port->number);
1257                 goto exit;
1258         }
1259
1260         if (port->throttled != 0) {
1261                 dbg("%s(): port %d: unsetting port->throttled", __func__,
1262                     port->number);
1263                 port->throttled = 0;
1264                 /* Send command to box to start receiving stuff */
1265                 if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) {
1266                         qt2_boxstoprx(serial,  port->number, 0);
1267                 } else if (dev_extra->ReadBulkStopped == true) {
1268                         usb_fill_bulk_urb(port0->read_urb, serial->dev,
1269                                 usb_rcvbulkpipe(serial->dev,
1270                                 port0->bulk_in_endpointAddress),
1271                                 port0->bulk_in_buffer,
1272                                 port0->bulk_in_size,
1273                                 qt2_read_bulk_callback,
1274                                 serial);
1275                 }
1276         }
1277 exit:
1278         up(&port_extra->sem);
1279         dbg("%s(): exit port %d", __func__, port->number);
1280         return;
1281 }
1282
1283 /* internal, private helper functions for the driver */
1284
1285 /* Power up the FPGA in the box to get it working */
1286 static int qt2_boxpoweron(struct usb_serial *serial)
1287 {
1288         int result;
1289         __u8  Direcion;
1290         unsigned int pipe;
1291         Direcion = USBD_TRANSFER_DIRECTION_OUT;
1292         pipe = usb_rcvctrlpipe(serial->dev, 0);
1293         result = usb_control_msg(serial->dev, pipe, QT_SET_GET_DEVICE,
1294                                 Direcion, QU2BOXPWRON, 0x00, NULL, 0x00,
1295                                 5000);
1296         return result;
1297 }
1298
1299 /*
1300  * qt2_boxsetQMCR Issue a QT2_GET_SET_QMCR vendor-spcific request on the
1301  * default control pipe. If successful return the number of bytes written,
1302  * otherwise return a negative error number of the problem.
1303  */
1304 static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
1305                           __u8 QMCR_Value)
1306 {
1307         int result;
1308         __u16 PortSettings;
1309
1310         PortSettings = (__u16)(QMCR_Value);
1311
1312         dbg("%s(): Port = %d, PortSettings = 0x%x", __func__,
1313                         Uart_Number, PortSettings);
1314
1315         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1316                                 QT2_GET_SET_QMCR, 0x40, PortSettings,
1317                                 (__u16)Uart_Number, NULL, 0, 5000);
1318         return result;
1319 }
1320
1321 static int port_paranoia_check(struct usb_serial_port *port,
1322                                const char *function)
1323 {
1324         if (!port) {
1325                 dbg("%s - port == NULL", function);
1326                 return -1;
1327         }
1328         if (!port->serial) {
1329                 dbg("%s - port->serial == NULL\n", function);
1330                 return -1;
1331         }
1332         return 0;
1333 }
1334
1335 static int serial_paranoia_check(struct usb_serial *serial,
1336                                  const char *function)
1337 {
1338         if (!serial) {
1339                 dbg("%s - serial == NULL\n", function);
1340                 return -1;
1341         }
1342
1343         if (!serial->type) {
1344                 dbg("%s - serial->type == NULL!", function);
1345                 return -1;
1346         }
1347
1348         return 0;
1349 }
1350
1351 static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
1352                 *port)
1353 {
1354         return (struct quatech2_port *)usb_get_serial_port_data(port);
1355 }
1356
1357 static inline void qt2_set_port_private(struct usb_serial_port *port,
1358                 struct quatech2_port *data)
1359 {
1360         usb_set_serial_port_data(port, (void *)data);
1361 }
1362
1363 static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
1364                 *serial)
1365 {
1366         return (struct quatech2_dev *)usb_get_serial_data(serial);
1367 }
1368 static inline void qt2_set_dev_private(struct usb_serial *serial,
1369                 struct quatech2_dev *data)
1370 {
1371         usb_set_serial_data(serial, (void *)data);
1372 }
1373
1374 static int qt2_openboxchannel(struct usb_serial *serial, __u16
1375                 Uart_Number, struct qt2_status_data *status)
1376 {
1377         int result;
1378         __u16 length;
1379         __u8  Direcion;
1380         unsigned int pipe;
1381         length = sizeof(struct qt2_status_data);
1382         Direcion = USBD_TRANSFER_DIRECTION_IN;
1383         pipe = usb_rcvctrlpipe(serial->dev, 0);
1384         result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1385                         Direcion, 0x00, Uart_Number, status, length, 5000);
1386         return result;
1387 }
1388 static int qt2_closeboxchannel(struct usb_serial *serial, __u16 Uart_Number)
1389 {
1390         int result;
1391         __u8  direcion;
1392         unsigned int pipe;
1393         direcion = USBD_TRANSFER_DIRECTION_OUT;
1394         pipe = usb_sndctrlpipe(serial->dev, 0);
1395         result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1396                   direcion, 0, Uart_Number, NULL, 0, 5000);
1397         return result;
1398 }
1399
1400 /* qt2_conf_uart Issue a SET_UART vendor-spcific request on the default
1401  * control pipe. If successful sets baud rate divisor and LCR value
1402  */
1403 static int qt2_conf_uart(struct usb_serial *serial,  unsigned short Uart_Number,
1404                       unsigned short divisor, unsigned char LCR)
1405 {
1406         int result;
1407         unsigned short UartNumandLCR;
1408
1409         UartNumandLCR = (LCR << 8) + Uart_Number;
1410
1411         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1412                                 QT2_GET_SET_UART, 0x40, divisor, UartNumandLCR,
1413                                 NULL, 0, 300);
1414         return result;
1415 }
1416
1417 /** @brief Callback for asynchronous submission of read URBs on bulk in
1418  * endpoints
1419  *
1420  * Registered in qt2_open_port(), used to deal with incomming data
1421  * from the box.
1422  */
1423 static void qt2_read_bulk_callback(struct urb *urb)
1424 {
1425         /* Get the device pointer (struct usb_serial) back out of the URB */
1426         struct usb_serial *serial = urb->context;
1427         /* get the extra struct for the device */
1428         struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1429         /* Get first port structure from the device */
1430         struct usb_serial_port *port0 = serial->port[0];
1431         /* Get the currently active port structure from serial struct */
1432         struct usb_serial_port *active = dev_extra->current_port;
1433         /* get the extra struct for port 0 */
1434         struct quatech2_port *port0_extra = qt2_get_port_private(port0);
1435         /* and for the currently active port */
1436         struct quatech2_port *active_extra = qt2_get_port_private(active);
1437         /* When we finally get to doing some tty stuff, we will need this */
1438         struct tty_struct *tty_st;
1439         unsigned int RxCount;   /* the length of the data to process */
1440         unsigned int i; /* loop counter over the data to process */
1441         int result;     /* return value cache variable */
1442         bool escapeflag;        /* flag set to true if this loop iteration is
1443                                  * parsing an escape sequence, rather than
1444                                  * ordinary data */
1445
1446
1447         dbg("%s(): callback running", __func__);
1448
1449         if (urb->status) {
1450                 /* read didn't go well */
1451                 dev_extra->ReadBulkStopped = true;
1452                 dbg("%s(): nonzero bulk read status received: %d",
1453                         __func__, urb->status);
1454                 return;
1455         }
1456
1457         /* inline port_sofrint() here */
1458         if (port_paranoia_check(port0, __func__) != 0) {
1459                 dbg("%s - port_paranoia_check on port0 failed, exiting\n",
1460 __func__);
1461                 return;
1462         }
1463         if (port_paranoia_check(active, __func__) != 0) {
1464                 dbg("%s - port_paranoia_check on current_port "
1465                         "failed, exiting", __func__);
1466                 return;
1467         }
1468
1469 /* This single callback function has to do for all the ports on
1470  * the device. Data being read up the USB can contain certain
1471  * escape sequences which are used to communicate out-of-band
1472  * information from the serial port in-band over the USB.
1473  * These escapes include sending modem and flow control line
1474  * status, and switching the port. The concept of a "Current Port"
1475  * is used, which is where data is going until a port change
1476  * escape seqence is received. This Current Port is kept between
1477  * callbacks so that when this function enters we know which the
1478  * currently active port is and can get to work right away without
1479  * the box having to send repeat escape sequences (anyway, how
1480  * would it know to do so?).
1481  */
1482
1483         if (active_extra->close_pending == true) {
1484                 /* We are closing , stop reading */
1485                 dbg("%s - (active->close_pending == true", __func__);
1486                 if (dev_extra->open_ports <= 0) {
1487                         /* If this is the only port left open - stop the
1488                          * bulk read */
1489                         dev_extra->ReadBulkStopped = true;
1490                         dbg("%s - (ReadBulkStopped == true;", __func__);
1491                         return;
1492                 }
1493         }
1494
1495         /*
1496          * RxHolding is asserted by throttle, if we assert it, we're not
1497          * receiving any more characters and let the box handle the flow
1498          * control
1499          */
1500         if ((port0_extra->RxHolding == true) &&
1501                     (serial->dev->descriptor.idProduct == QUATECH_SSU2_100)) {
1502                 /* single port device, input is already stopped, so we don't
1503                  * need any more input data */
1504                 dev_extra->ReadBulkStopped = true;
1505                         return;
1506         }
1507         /* finally, we are in a situation where we might consider the data
1508          * that is contained within the URB, and what to do about it.
1509          * This is likely to involved communicating up to the TTY layer, so
1510          * we will need to get hold of the tty for the port we are currently
1511          * dealing with */
1512
1513         /* active is a usb_serial_port. It has a member port which is a
1514          * tty_port. From this we get a tty_struct pointer which is what we
1515         * actually wanted, and keep it on         tty_st */
1516         tty_st = tty_port_tty_get(&active->port);
1517         if (!tty_st) {
1518                 dbg("%s - bad tty pointer - exiting", __func__);
1519                 return;
1520         }
1521         dbg("%s(): active port %d, tty_st =0x%p\n", __func__, active->number,
1522                 tty_st);
1523         RxCount = urb->actual_length;   /* grab length of data handy */
1524
1525         if (RxCount) {
1526                 /* skip all this if no data to process */
1527                 for (i = 0; i < RxCount ; ++i) {
1528                         /* Look ahead code here -works on several bytes at onc*/
1529                         if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b)
1530                                 && (NEXTCHAR == 0x1b)) {
1531                                 /* we are in an escape sequence, type
1532                                  * determined by the 3rd char */
1533                                 escapeflag = false;
1534                                 switch (THIRDCHAR) {
1535                                 case 0x00:
1536                                         /* Line status change 4th byte must
1537                                          * follow */
1538                                         if (i > (RxCount - 4)) {
1539                                                 dbg("Illegal escape sequences "
1540                                                 "in received data");
1541                                                 break;
1542                                         }
1543                                         qt2_process_line_status(active,
1544                                                 FOURTHCHAR);
1545                                         i += 3;
1546                                         escapeflag = true;
1547                                         break;
1548                                 case 0x01:
1549                                         /* Modem status status change 4th byte
1550                                          * must follow */
1551                                         if (i > (RxCount - 4)) {
1552                                                 dbg("Illegal escape sequences "
1553                                                 "in received data");
1554                                                 break;
1555                                         }
1556                                         qt2_process_modem_status(active,
1557                                                 FOURTHCHAR);
1558                                         i += 3;
1559                                         escapeflag = true;
1560                                         break;
1561                                 case 0x02:
1562                                         /* xmit hold empty 4th byte
1563                                          * must follow */
1564                                         if (i > (RxCount - 4)) {
1565                                                 dbg("Illegal escape sequences "
1566                                                 "in received data");
1567                                                 break;
1568                                         }
1569                                         qt2_process_xmit_empty(active,
1570                                                 FOURTHCHAR, FIFTHCHAR);
1571                                         i += 4;
1572                                         escapeflag = true;
1573                                         break;
1574                                 case 0x03:
1575                                         /* Port number change 4th byte
1576                                          * must follow */
1577                                         if (i > (RxCount - 4)) {
1578                                                 dbg("Illegal escape sequences "
1579                                                 "in received data");
1580                                                 break;
1581                                         }
1582                                         /* Port change. If port open push
1583                                          * current data up to tty layer */
1584                                         if (active_extra->open_count > 0)
1585                                                 tty_flip_buffer_push(tty_st);
1586
1587                                         dbg("Port Change: new port = %d",
1588                                                 FOURTHCHAR);
1589                                         qt2_process_port_change(active,
1590                                                 FOURTHCHAR);
1591                                         i += 3;
1592                                         escapeflag = true;
1593                                         /* having changed port, the pointers for
1594                                          * the currently active port are all out
1595                                          * of date and need updating */
1596                                         active = dev_extra->current_port;
1597                                         active_extra =
1598                                                 qt2_get_port_private(active);
1599                                         tty_st = tty_port_tty_get(
1600                                                 &active->port);
1601                                         break;
1602                                 case 0x04:
1603                                         /* Recv flush 3rd byte must
1604                                          * follow */
1605                                         if (i > (RxCount - 3)) {
1606                                                 dbg("Illegal escape sequences "
1607                                                         "in received data");
1608                                                 break;
1609                                         }
1610                                         qt2_process_rcv_flush(active);
1611                                         i += 2;
1612                                         escapeflag = true;
1613                                         break;
1614                                 case 0x05:
1615                                         /* xmit flush 3rd byte must follow */
1616                                         if (i > (RxCount - 3)) {
1617                                                 dbg("Illegal escape sequences "
1618                                                 "in received data");
1619                                                 break;
1620                                         }
1621                                         qt2_process_xmit_flush(active);
1622                                         i += 2;
1623                                         escapeflag = true;
1624                                         break;
1625                                 case 0xff:
1626                                         dbg("No status sequence");
1627                                         qt2_process_rx_char(active, THISCHAR);
1628                                         qt2_process_rx_char(active, NEXTCHAR);
1629                                         i += 2;
1630                                         break;
1631                                 default:
1632                                         qt2_process_rx_char(active, THISCHAR);
1633                                         i += 1;
1634                                         break;
1635                                 } /*end switch*/
1636                                 if (escapeflag == true)
1637                                         continue;
1638                                 /* if we did an escape char, we don't need
1639                                  * to mess around pushing data through the
1640                                  * tty layer, and can go round again */
1641                         } /*endif*/
1642                         if (tty_st && urb->actual_length) {
1643                                 tty_buffer_request_room(tty_st, 1);
1644                                 tty_insert_flip_string(tty_st,
1645                                         &((unsigned char *)(urb->transfer_buffer)
1646                                                 )[i],
1647                                         1);
1648                         }
1649                 } /*endfor*/
1650                 tty_flip_buffer_push(tty_st);
1651         } /*endif*/
1652
1653         /* at this point we have complete dealing with the data for this
1654          * callback. All we have to do now is to start the async read process
1655          * back off again. */
1656
1657         usb_fill_bulk_urb(port0->read_urb, serial->dev,
1658                 usb_rcvbulkpipe(serial->dev, port0->bulk_in_endpointAddress),
1659                 port0->bulk_in_buffer, port0->bulk_in_size,
1660                 qt2_read_bulk_callback, serial);
1661         result = usb_submit_urb(port0->read_urb, GFP_ATOMIC);
1662         if (result) {
1663                 dbg("%s(): failed resubmitting read urb, error %d",
1664                         __func__, result);
1665         } else {
1666                 if (tty_st && RxCount) {
1667                         /* if some inbound data was processed, then
1668                          * we need to push that through the tty layer
1669                          */
1670                         tty_flip_buffer_push(tty_st);
1671                         tty_schedule_flip(tty_st);
1672                 }
1673         }
1674
1675         /* cribbed from serqt_usb2 driver, but not sure which work needs
1676          * scheduling - port0 or currently active port? */
1677         /* schedule_work(&port->work); */
1678
1679         return;
1680 }
1681
1682 /** @brief Callback for asynchronous submission of write URBs on bulk in
1683  * endpoints
1684  *
1685  * Registered in qt2_write(), used to deal with outgoing data
1686  * to the box.
1687  */
1688 static void qt2_write_bulk_callback(struct urb *urb)
1689 {
1690         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1691         struct usb_serial *serial = port->serial;
1692         dbg("%s(): port %d", __func__, port->number);
1693         if (!serial) {
1694                 dbg("%s(): bad serial pointer, exiting", __func__);
1695                 return;
1696         }
1697         if (urb->status) {
1698                 dbg("%s(): nonzero write bulk status received: %d",
1699                         __func__, urb->status);
1700                 return;
1701         }
1702
1703         /*port_softint((void *) serial); commented in vendor driver */
1704         schedule_work(&port->work);
1705         dbg("%s(): port %d exit", __func__, port->number);
1706         return;
1707 }
1708
1709 static void qt2_process_line_status(struct usb_serial_port *port,
1710         unsigned char LineStatus)
1711 {
1712         /* obtain the private structure for the port */
1713         struct quatech2_port *port_extra = qt2_get_port_private(port);
1714         port_extra->shadowLSR = LineStatus & (QT2_SERIAL_LSR_OE |
1715                 QT2_SERIAL_LSR_PE | QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
1716 }
1717 static void qt2_process_modem_status(struct usb_serial_port *port,
1718         unsigned char ModemStatus)
1719 {
1720         /* obtain the private structure for the port */
1721         struct quatech2_port *port_extra = qt2_get_port_private(port);
1722         port_extra->shadowMSR = ModemStatus;
1723         wake_up_interruptible(&port_extra->wait);
1724         /* this wakes up the otherwise indefinitely waiting code for
1725          * the TIOCMIWAIT ioctl, so that it can notice that
1726          * port_extra->shadowMSR has changed and the ioctl needs to return.
1727          */
1728 }
1729
1730 static void qt2_process_xmit_empty(struct usb_serial_port *port,
1731         unsigned char fourth_char, unsigned char fifth_char)
1732 {
1733         int byte_count;
1734         /* obtain the private structure for the port */
1735         struct quatech2_port *port_extra = qt2_get_port_private(port);
1736
1737         byte_count = (int)(fifth_char * 16);
1738         byte_count +=  (int)fourth_char;
1739         /* byte_count indicates how many bytes the device has written out. This
1740          * message appears to occur regularly, and is used in the vendor driver
1741          * to keep track of the fill state of the port transmit buffer */
1742         port_extra->tx_pending_bytes -= byte_count;
1743         /* reduce the stored data queue length by the known number of bytes
1744          * sent */
1745         dbg("port %d: %d bytes reported sent, %d still pending", port->number,
1746                         byte_count, port_extra->tx_pending_bytes);
1747
1748         /*port_extra->xmit_fifo_room_bytes = FIFO_DEPTH; ???*/
1749 }
1750
1751 static void qt2_process_port_change(struct usb_serial_port *port,
1752         unsigned char New_Current_Port)
1753 {
1754         /* obtain the parent usb serial device structure */
1755         struct usb_serial *serial = port->serial;
1756         /* obtain the private structure for the device */
1757         struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1758         dev_extra->current_port = serial->port[New_Current_Port];
1759         /* what should I do with this? commented out in upstream
1760          * driver */
1761         /*schedule_work(&port->work);*/
1762 }
1763
1764 static void qt2_process_rcv_flush(struct usb_serial_port *port)
1765 {
1766         /* obtain the private structure for the port */
1767         struct quatech2_port *port_extra = qt2_get_port_private(port);
1768         port_extra->rcv_flush = true;
1769 }
1770 static void qt2_process_xmit_flush(struct usb_serial_port *port)
1771 {
1772         /* obtain the private structure for the port */
1773         struct quatech2_port *port_extra = qt2_get_port_private(port);
1774         port_extra->xmit_flush = true;
1775 }
1776
1777 static void qt2_process_rx_char(struct usb_serial_port *port,
1778         unsigned char data)
1779 {
1780         /* get the tty_struct for this port */
1781         struct tty_struct *tty = tty_port_tty_get(&(port->port));
1782         /* get the URB with the data in to push */
1783         struct urb *urb = port->serial->port[0]->read_urb;
1784
1785         if (tty && urb->actual_length) {
1786                 tty_buffer_request_room(tty, 1);
1787                 tty_insert_flip_string(tty, &data, 1);
1788                 /* should this be commented out here? */
1789                 /*tty_flip_buffer_push(tty);*/
1790         }
1791 }
1792
1793 /** @brief Retreive the value of a register from the device
1794  *
1795  * Issues a GET_REGISTER vendor-spcific request over the USB control
1796  * pipe to obtain a value back from a specific register on a specific
1797  * UART
1798  * @param serial Serial device handle to access the device through
1799  * @param uart_number Which UART the value is wanted from
1800  * @param register_num Which register to read the value from
1801  * @param pValue Pointer to somewhere to put the retrieved value
1802  */
1803 static int qt2_box_get_register(struct usb_serial *serial,
1804                 unsigned char uart_number, unsigned short register_num,
1805                 __u8 *pValue)
1806 {
1807         int result;
1808         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1809                         QT2_GET_SET_REGISTER, 0xC0, register_num,
1810                         uart_number, (void *)pValue, sizeof(*pValue), 300);
1811         return result;
1812 }
1813
1814 /** qt2_box_set_register
1815  * Issue a SET_REGISTER vendor-specific request on the default control pipe
1816  */
1817 static int qt2_box_set_register(struct usb_serial *serial,
1818                 unsigned short Uart_Number, unsigned short Register_Num,
1819                 unsigned short Value)
1820 {
1821         int result;
1822         unsigned short reg_and_byte;
1823
1824         reg_and_byte = Value;
1825         reg_and_byte = reg_and_byte << 8;
1826         reg_and_byte = reg_and_byte + Register_Num;
1827
1828         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1829                         QT2_GET_SET_REGISTER, 0x40, reg_and_byte,
1830                         Uart_Number, NULL, 0, 300);
1831         return result;
1832 }
1833
1834
1835 /** @brief Request the Tx or Rx buffers on the USB side be flushed
1836  *
1837  * Tx flush: When all the currently buffered data has been sent, send an escape
1838  * sequence back up the data stream to us
1839  * Rx flush: add a flag in the data stream now so we know when it's made it's
1840  * way up to us.
1841  */
1842 static int qt2_box_flush(struct usb_serial *serial,  unsigned char uart_number,
1843                     unsigned short rcv_or_xmit)
1844 {
1845         int result;
1846         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1847                 QT2_FLUSH_DEVICE, 0x40, rcv_or_xmit, uart_number, NULL, 0,
1848                 300);
1849         return result;
1850 }
1851
1852 /** qt2_boxsetuart - Issue a SET_UART vendor-spcific request on the default
1853  * control pipe. If successful sets baud rate divisor and LCR value.
1854  */
1855 static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
1856                 unsigned short default_divisor, unsigned char default_LCR)
1857 {
1858         unsigned short UartNumandLCR;
1859
1860         UartNumandLCR = (default_LCR << 8) + Uart_Number;
1861
1862         return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1863                         QT2_GET_SET_UART, 0x40, default_divisor, UartNumandLCR,
1864                         NULL, 0, 300);
1865 }
1866 /** qt2_boxsethw_flowctl - Turn hardware (RTS/CTS) flow control on and off for
1867  * a hardware UART.
1868  */
1869 static int qt2_boxsethw_flowctl(struct usb_serial *serial,
1870                 unsigned int UartNumber, bool bSet)
1871 {
1872         __u8 MCR_Value = 0;
1873         __u8 MSR_Value = 0;
1874         __u16 MOUT_Value = 0;
1875
1876         if (bSet == true) {
1877                 MCR_Value =  QT2_SERIAL_MCR_RTS;
1878                 /* flow control, box will clear RTS line to prevent remote
1879                  * device from transmitting more chars */
1880         } else {
1881                 /* no flow control to remote device */
1882                 MCR_Value =  0;
1883         }
1884         MOUT_Value = MCR_Value << 8;
1885
1886         if (bSet == true) {
1887                 MSR_Value = QT2_SERIAL_MSR_CTS;
1888                 /* flow control on, box will inhibit tx data if CTS line is
1889                  * asserted */
1890         } else {
1891                 /* Box will not inhibit tx data due to CTS line */
1892                 MSR_Value = 0;
1893         }
1894         MOUT_Value |= MSR_Value;
1895         return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1896                         QT2_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, UartNumber,
1897                         NULL, 0, 300);
1898 }
1899
1900 /** qt2_boxsetsw_flowctl - Turn software (XON/XOFF) flow control on for
1901  * a hardware UART, and set the XON and XOFF characters.
1902  */
1903 static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
1904                         unsigned char stop_char,  unsigned char start_char)
1905 {
1906         __u16 nSWflowout;
1907
1908         nSWflowout = start_char << 8;
1909         nSWflowout = (unsigned short)stop_char;
1910         return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1911                         QT2_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, UartNumber,
1912                         NULL, 0, 300);
1913 }
1914
1915 /** qt2_boxunsetsw_flowctl - Turn software (XON/XOFF) flow control off for
1916  * a hardware UART.
1917  */
1918 static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber)
1919 {
1920         return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1921                         QT2_SW_FLOW_CONTROL_DISABLE, 0x40, 0, UartNumber, NULL,
1922                         0, 300);
1923 }
1924
1925 /**
1926  * qt2_boxstoprx - Start and stop reception of data by the FPGA UART in
1927  * response to requests from the tty layer
1928  * @serial: pointer to the usb_serial structure for the parent device
1929  * @uart_number: which UART on the device we are addressing
1930  * @stop: Whether to start or stop data reception. Set to 1 to stop data being
1931  * received, and to 0 to start it being received.
1932  */
1933 static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
1934                 unsigned short stop)
1935 {
1936         return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1937                 QT2_STOP_RECEIVE, 0x40, stop, uart_number, NULL, 0, 300);
1938 }
1939
1940
1941 /*
1942  * last things in file: stuff to register this driver into the generic
1943  * USB serial framework.
1944  */
1945
1946 static struct usb_serial_driver quatech2_device = {
1947         .driver = {
1948                 .owner = THIS_MODULE,
1949                 .name = "quatech_usb2",
1950         },
1951         .description = DRIVER_DESC,
1952         .usb_driver = &quausb2_usb_driver,
1953         .id_table = quausb2_id_table,
1954         .num_ports = 8,
1955         .open = qt2_open,
1956         .close = qt2_close,
1957         .write = qt2_write,
1958         .write_room = qt2_write_room,
1959         .chars_in_buffer = qt2_chars_in_buffer,
1960         .throttle = qt2_throttle,
1961         .unthrottle = qt2_unthrottle,
1962         .calc_num_ports = qt2_calc_num_ports,
1963         .ioctl = qt2_ioctl,
1964         .set_termios = qt2_set_termios,
1965         .break_ctl = qt2_break,
1966         .tiocmget = qt2_tiocmget,
1967         .tiocmset = qt2_tiocmset,
1968         .attach = qt2_attach,
1969         .release = qt2_release,
1970         .read_bulk_callback = qt2_read_bulk_callback,
1971         .write_bulk_callback = qt2_write_bulk_callback,
1972 };
1973
1974 static int __init quausb2_usb_init(void)
1975 {
1976         int retval;
1977
1978         dbg("%s\n", __func__);
1979
1980         /* register with usb-serial */
1981         retval = usb_serial_register(&quatech2_device);
1982
1983         if (retval)
1984                 goto failed_usb_serial_register;
1985
1986         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1987                         DRIVER_DESC "\n");
1988
1989         /* register with usb */
1990
1991         retval = usb_register(&quausb2_usb_driver);
1992         if (retval == 0)
1993                 return 0;
1994
1995         /* if we're here, usb_register() failed */
1996         usb_serial_deregister(&quatech2_device);
1997 failed_usb_serial_register:
1998                 return retval;
1999 }
2000
2001 static void __exit quausb2_usb_exit(void)
2002 {
2003         usb_deregister(&quausb2_usb_driver);
2004         usb_serial_deregister(&quatech2_device);
2005 }
2006
2007 module_init(quausb2_usb_init);
2008 module_exit(quausb2_usb_exit);
2009
2010 MODULE_AUTHOR(DRIVER_AUTHOR);
2011 MODULE_DESCRIPTION(DRIVER_DESC);
2012 MODULE_LICENSE("GPL");
2013
2014 module_param(debug, bool, S_IRUGO | S_IWUSR);
2015 MODULE_PARM_DESC(debug, "Debug enabled or not");