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