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