xref: /linux/drivers/usb/serial/quatech2.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * usb-serial driver for Quatech USB 2 devices
4  *
5  * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu)
6  *
7  *  These devices all have only 1 bulk in and 1 bulk out that is shared
8  *  for all serial ports.
9  *
10  */
11 
12 #include <asm/unaligned.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/serial.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/serial_reg.h>
23 #include <linux/uaccess.h>
24 
25 /* default urb timeout for usb operations */
26 #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
27 
28 #define QT_OPEN_CLOSE_CHANNEL       0xca
29 #define QT_SET_GET_DEVICE           0xc2
30 #define QT_SET_GET_REGISTER         0xc0
31 #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
32 #define QT_SET_ATF                  0xcd
33 #define QT_TRANSFER_IN              0xc0
34 #define QT_HW_FLOW_CONTROL_MASK     0xc5
35 #define QT_SW_FLOW_CONTROL_MASK     0xc6
36 #define QT2_BREAK_CONTROL	    0xc8
37 #define QT2_GET_SET_UART            0xc1
38 #define QT2_FLUSH_DEVICE	    0xc4
39 #define QT2_GET_SET_QMCR            0xe1
40 #define QT2_QMCR_RS232              0x40
41 #define QT2_QMCR_RS422              0x10
42 
43 #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
44 
45 #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
46 
47 /* status bytes for the device */
48 #define QT2_CONTROL_BYTE    0x1b
49 #define QT2_LINE_STATUS     0x00  /* following 1 byte is line status */
50 #define QT2_MODEM_STATUS    0x01  /* following 1 byte is modem status */
51 #define QT2_XMIT_HOLD       0x02  /* following 2 bytes are ?? */
52 #define QT2_CHANGE_PORT     0x03  /* following 1 byte is port to change to */
53 #define QT2_REC_FLUSH       0x04  /* no following info */
54 #define QT2_XMIT_FLUSH      0x05  /* no following info */
55 #define QT2_CONTROL_ESCAPE  0xff  /* pass through previous 2 control bytes */
56 
57 #define  MAX_BAUD_RATE              921600
58 #define  DEFAULT_BAUD_RATE          9600
59 
60 #define QT2_READ_BUFFER_SIZE    512  /* size of read buffer */
61 #define QT2_WRITE_BUFFER_SIZE   512  /* size of write buffer */
62 #define QT2_WRITE_CONTROL_SIZE  5    /* control bytes used for a write */
63 
64 #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
65 
66 #define	USB_VENDOR_ID_QUATECH	0x061d
67 #define QUATECH_SSU2_100	0xC120	/* RS232 single port */
68 #define QUATECH_DSU2_100	0xC140	/* RS232 dual port */
69 #define QUATECH_DSU2_400	0xC150	/* RS232/422/485 dual port */
70 #define QUATECH_QSU2_100	0xC160	/* RS232 four port */
71 #define QUATECH_QSU2_400	0xC170	/* RS232/422/485 four port */
72 #define QUATECH_ESU2_100	0xC1A0	/* RS232 eight port */
73 #define QUATECH_ESU2_400	0xC180	/* RS232/422/485 eight port */
74 
75 struct qt2_device_detail {
76 	int product_id;
77 	int num_ports;
78 };
79 
80 #define QT_DETAILS(prod, ports)	\
81 	.product_id = (prod),   \
82 	.num_ports = (ports)
83 
84 static const struct qt2_device_detail qt2_device_details[] = {
85 	{QT_DETAILS(QUATECH_SSU2_100, 1)},
86 	{QT_DETAILS(QUATECH_DSU2_400, 2)},
87 	{QT_DETAILS(QUATECH_DSU2_100, 2)},
88 	{QT_DETAILS(QUATECH_QSU2_400, 4)},
89 	{QT_DETAILS(QUATECH_QSU2_100, 4)},
90 	{QT_DETAILS(QUATECH_ESU2_400, 8)},
91 	{QT_DETAILS(QUATECH_ESU2_100, 8)},
92 	{QT_DETAILS(0, 0)}	/* Terminating entry */
93 };
94 
95 static const struct usb_device_id id_table[] = {
96 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
97 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
98 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
99 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
100 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
101 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
102 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
103 	{}			/* Terminating entry */
104 };
105 MODULE_DEVICE_TABLE(usb, id_table);
106 
107 struct qt2_serial_private {
108 	unsigned char current_port;  /* current port for incoming data */
109 
110 	struct urb	*read_urb;   /* shared among all ports */
111 	char		*read_buffer;
112 };
113 
114 struct qt2_port_private {
115 	u8   device_port;
116 
117 	spinlock_t urb_lock;
118 	bool       urb_in_use;
119 	struct urb *write_urb;
120 	char       *write_buffer;
121 
122 	spinlock_t  lock;
123 	u8          shadowLSR;
124 	u8          shadowMSR;
125 
126 	struct usb_serial_port *port;
127 };
128 
129 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
130 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
131 static void qt2_write_bulk_callback(struct urb *urb);
132 static void qt2_read_bulk_callback(struct urb *urb);
133 
134 static void qt2_release(struct usb_serial *serial)
135 {
136 	struct qt2_serial_private *serial_priv;
137 
138 	serial_priv = usb_get_serial_data(serial);
139 
140 	usb_kill_urb(serial_priv->read_urb);
141 	usb_free_urb(serial_priv->read_urb);
142 	kfree(serial_priv->read_buffer);
143 	kfree(serial_priv);
144 }
145 
146 static inline int calc_baud_divisor(int baudrate)
147 {
148 	int divisor, rem;
149 
150 	divisor = MAX_BAUD_RATE / baudrate;
151 	rem = MAX_BAUD_RATE % baudrate;
152 	/* Round to nearest divisor */
153 	if (((rem * 2) >= baudrate) && (baudrate != 110))
154 		divisor++;
155 
156 	return divisor;
157 }
158 
159 static inline int qt2_set_port_config(struct usb_device *dev,
160 				      unsigned char port_number,
161 				      u16 baudrate, u16 lcr)
162 {
163 	int divisor = calc_baud_divisor(baudrate);
164 	u16 index = ((u16) (lcr << 8) | (u16) (port_number));
165 
166 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
167 			       QT2_GET_SET_UART, 0x40,
168 			       divisor, index, NULL, 0, QT2_USB_TIMEOUT);
169 }
170 
171 static inline int qt2_control_msg(struct usb_device *dev,
172 				  u8 request, u16 data, u16 index)
173 {
174 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
175 			       request, 0x40, data, index,
176 			       NULL, 0, QT2_USB_TIMEOUT);
177 }
178 
179 static inline int qt2_getregister(struct usb_device *dev,
180 				  u8 uart,
181 				  u8 reg,
182 				  u8 *data)
183 {
184 	int ret;
185 
186 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
187 			      QT_SET_GET_REGISTER, 0xc0, reg,
188 			      uart, data, sizeof(*data), QT2_USB_TIMEOUT);
189 	if (ret < (int)sizeof(*data)) {
190 		if (ret >= 0)
191 			ret = -EIO;
192 	}
193 
194 	return ret;
195 }
196 
197 static inline int qt2_setregister(struct usb_device *dev,
198 				  u8 uart, u8 reg, u16 data)
199 {
200 	u16 value = (data << 8) | reg;
201 
202 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
203 			       QT_SET_GET_REGISTER, 0x40, value, uart,
204 			       NULL, 0, QT2_USB_TIMEOUT);
205 }
206 
207 static inline int update_mctrl(struct qt2_port_private *port_priv,
208 			       unsigned int set, unsigned int clear)
209 {
210 	struct usb_serial_port *port = port_priv->port;
211 	struct usb_device *dev = port->serial->dev;
212 	unsigned urb_value;
213 	int status;
214 
215 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
216 		dev_dbg(&port->dev,
217 			"update_mctrl - DTR|RTS not being set|cleared\n");
218 		return 0;	/* no change */
219 	}
220 
221 	clear &= ~set;	/* 'set' takes precedence over 'clear' */
222 	urb_value = 0;
223 	if (set & TIOCM_DTR)
224 		urb_value |= UART_MCR_DTR;
225 	if (set & TIOCM_RTS)
226 		urb_value |= UART_MCR_RTS;
227 
228 	status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
229 				 urb_value);
230 	if (status < 0)
231 		dev_err(&port->dev,
232 			"update_mctrl - Error from MODEM_CTRL urb: %i\n",
233 			status);
234 	return status;
235 }
236 
237 static int qt2_calc_num_ports(struct usb_serial *serial,
238 					struct usb_serial_endpoints *epds)
239 {
240 	struct qt2_device_detail d;
241 	int i;
242 
243 	for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
244 		if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
245 			return d.num_ports;
246 	}
247 
248 	/* we didn't recognize the device */
249 	dev_err(&serial->dev->dev,
250 		 "don't know the number of ports, assuming 1\n");
251 
252 	return 1;
253 }
254 
255 static void qt2_set_termios(struct tty_struct *tty,
256 		            struct usb_serial_port *port,
257 		            const struct ktermios *old_termios)
258 {
259 	struct usb_device *dev = port->serial->dev;
260 	struct qt2_port_private *port_priv;
261 	struct ktermios *termios = &tty->termios;
262 	u16 baud;
263 	unsigned int cflag = termios->c_cflag;
264 	u16 new_lcr = 0;
265 	int status;
266 
267 	port_priv = usb_get_serial_port_data(port);
268 
269 	if (cflag & PARENB) {
270 		if (cflag & PARODD)
271 			new_lcr |= UART_LCR_PARITY;
272 		else
273 			new_lcr |= SERIAL_EVEN_PARITY;
274 	}
275 
276 	new_lcr |= UART_LCR_WLEN(tty_get_char_size(cflag));
277 
278 	baud = tty_get_baud_rate(tty);
279 	if (!baud)
280 		baud = 9600;
281 
282 	status = qt2_set_port_config(dev, port_priv->device_port, baud,
283 				     new_lcr);
284 	if (status < 0)
285 		dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
286 			__func__, status);
287 
288 	if (cflag & CRTSCTS)
289 		status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
290 					 SERIAL_CRTSCTS,
291 					 port_priv->device_port);
292 	else
293 		status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
294 					 0, port_priv->device_port);
295 	if (status < 0)
296 		dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
297 			__func__, status);
298 
299 	if (I_IXOFF(tty) || I_IXON(tty)) {
300 		u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
301 
302 		status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
303 					 x, port_priv->device_port);
304 	} else
305 		status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
306 					 0, port_priv->device_port);
307 
308 	if (status < 0)
309 		dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
310 			__func__, status);
311 
312 }
313 
314 static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
315 {
316 	struct usb_serial *serial;
317 	struct qt2_port_private *port_priv;
318 	u8 *data;
319 	u16 device_port;
320 	int status;
321 	unsigned long flags;
322 
323 	device_port = port->port_number;
324 
325 	serial = port->serial;
326 
327 	port_priv = usb_get_serial_port_data(port);
328 
329 	/* set the port to RS232 mode */
330 	status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
331 				 QT2_QMCR_RS232, device_port);
332 	if (status < 0) {
333 		dev_err(&port->dev,
334 			"%s failed to set RS232 mode for port %i error %i\n",
335 			__func__, device_port, status);
336 		return status;
337 	}
338 
339 	data = kzalloc(2, GFP_KERNEL);
340 	if (!data)
341 		return -ENOMEM;
342 
343 	/* open the port */
344 	status = usb_control_msg(serial->dev,
345 				 usb_rcvctrlpipe(serial->dev, 0),
346 				 QT_OPEN_CLOSE_CHANNEL,
347 				 0xc0, 0,
348 				 device_port, data, 2, QT2_USB_TIMEOUT);
349 
350 	if (status < 2) {
351 		dev_err(&port->dev, "%s - open port failed %i\n", __func__,
352 			status);
353 		if (status >= 0)
354 			status = -EIO;
355 		kfree(data);
356 		return status;
357 	}
358 
359 	spin_lock_irqsave(&port_priv->lock, flags);
360 	port_priv->shadowLSR = data[0];
361 	port_priv->shadowMSR = data[1];
362 	spin_unlock_irqrestore(&port_priv->lock, flags);
363 
364 	kfree(data);
365 
366 	/* set to default speed and 8bit word size */
367 	status = qt2_set_port_config(serial->dev, device_port,
368 				     DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
369 	if (status < 0) {
370 		dev_err(&port->dev, "%s - initial setup failed (%i)\n",
371 			__func__, device_port);
372 		return status;
373 	}
374 
375 	port_priv->device_port = (u8) device_port;
376 
377 	if (tty)
378 		qt2_set_termios(tty, port, &tty->termios);
379 
380 	return 0;
381 
382 }
383 
384 static void qt2_close(struct usb_serial_port *port)
385 {
386 	struct usb_serial *serial;
387 	struct qt2_port_private *port_priv;
388 	int i;
389 
390 	serial = port->serial;
391 	port_priv = usb_get_serial_port_data(port);
392 
393 	usb_kill_urb(port_priv->write_urb);
394 
395 	/* flush the port transmit buffer */
396 	i = usb_control_msg(serial->dev,
397 			    usb_sndctrlpipe(serial->dev, 0),
398 			    QT2_FLUSH_DEVICE, 0x40, 1,
399 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
400 
401 	if (i < 0)
402 		dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
403 			__func__, i);
404 
405 	/* flush the port receive buffer */
406 	i = usb_control_msg(serial->dev,
407 			    usb_sndctrlpipe(serial->dev, 0),
408 			    QT2_FLUSH_DEVICE, 0x40, 0,
409 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
410 
411 	if (i < 0)
412 		dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
413 			__func__, i);
414 
415 	/* close the port */
416 	i = usb_control_msg(serial->dev,
417 			    usb_sndctrlpipe(serial->dev, 0),
418 			    QT_OPEN_CLOSE_CHANNEL,
419 			    0x40, 0,
420 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
421 
422 	if (i < 0)
423 		dev_err(&port->dev, "%s - close port failed %i\n",
424 			__func__, i);
425 }
426 
427 static void qt2_disconnect(struct usb_serial *serial)
428 {
429 	struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
430 
431 	usb_kill_urb(serial_priv->read_urb);
432 }
433 
434 static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
435 {
436 	switch (*ch) {
437 	case QT2_LINE_STATUS:
438 		qt2_update_lsr(port, ch + 1);
439 		break;
440 	case QT2_MODEM_STATUS:
441 		qt2_update_msr(port, ch + 1);
442 		break;
443 	}
444 }
445 
446 static void qt2_process_read_urb(struct urb *urb)
447 {
448 	struct usb_serial *serial;
449 	struct qt2_serial_private *serial_priv;
450 	struct usb_serial_port *port;
451 	bool escapeflag;
452 	unsigned char *ch;
453 	int i;
454 	unsigned char newport;
455 	int len = urb->actual_length;
456 
457 	if (!len)
458 		return;
459 
460 	ch = urb->transfer_buffer;
461 	serial = urb->context;
462 	serial_priv = usb_get_serial_data(serial);
463 	port = serial->port[serial_priv->current_port];
464 
465 	for (i = 0; i < urb->actual_length; i++) {
466 		ch = (unsigned char *)urb->transfer_buffer + i;
467 		if ((i <= (len - 3)) &&
468 		    (*ch == QT2_CONTROL_BYTE) &&
469 		    (*(ch + 1) == QT2_CONTROL_BYTE)) {
470 			escapeflag = false;
471 			switch (*(ch + 2)) {
472 			case QT2_LINE_STATUS:
473 			case QT2_MODEM_STATUS:
474 				if (i > (len - 4)) {
475 					dev_warn(&port->dev,
476 						 "%s - status message too short\n",
477 						__func__);
478 					break;
479 				}
480 				qt2_process_status(port, ch + 2);
481 				i += 3;
482 				escapeflag = true;
483 				break;
484 			case QT2_XMIT_HOLD:
485 				if (i > (len - 5)) {
486 					dev_warn(&port->dev,
487 						 "%s - xmit_empty message too short\n",
488 						 __func__);
489 					break;
490 				}
491 				/* bytes_written = (ch[1] << 4) + ch[0]; */
492 				i += 4;
493 				escapeflag = true;
494 				break;
495 			case QT2_CHANGE_PORT:
496 				if (i > (len - 4)) {
497 					dev_warn(&port->dev,
498 						 "%s - change_port message too short\n",
499 						 __func__);
500 					break;
501 				}
502 				tty_flip_buffer_push(&port->port);
503 
504 				newport = *(ch + 3);
505 
506 				if (newport > serial->num_ports) {
507 					dev_err(&port->dev,
508 						"%s - port change to invalid port: %i\n",
509 						__func__, newport);
510 					break;
511 				}
512 
513 				serial_priv->current_port = newport;
514 				port = serial->port[serial_priv->current_port];
515 				i += 3;
516 				escapeflag = true;
517 				break;
518 			case QT2_REC_FLUSH:
519 			case QT2_XMIT_FLUSH:
520 				i += 2;
521 				escapeflag = true;
522 				break;
523 			case QT2_CONTROL_ESCAPE:
524 				tty_insert_flip_string(&port->port, ch, 2);
525 				i += 2;
526 				escapeflag = true;
527 				break;
528 			default:
529 				dev_warn(&port->dev,
530 					 "%s - unsupported command %i\n",
531 					 __func__, *(ch + 2));
532 				break;
533 			}
534 			if (escapeflag)
535 				continue;
536 		}
537 
538 		tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
539 	}
540 
541 	tty_flip_buffer_push(&port->port);
542 }
543 
544 static void qt2_write_bulk_callback(struct urb *urb)
545 {
546 	struct usb_serial_port *port;
547 	struct qt2_port_private *port_priv;
548 	unsigned long flags;
549 
550 	port = urb->context;
551 	port_priv = usb_get_serial_port_data(port);
552 
553 	spin_lock_irqsave(&port_priv->urb_lock, flags);
554 
555 	port_priv->urb_in_use = false;
556 	usb_serial_port_softint(port);
557 
558 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
559 
560 }
561 
562 static void qt2_read_bulk_callback(struct urb *urb)
563 {
564 	struct usb_serial *serial = urb->context;
565 	int status;
566 
567 	if (urb->status) {
568 		dev_warn(&serial->dev->dev,
569 			 "%s - non-zero urb status: %i\n", __func__,
570 			 urb->status);
571 		return;
572 	}
573 
574 	qt2_process_read_urb(urb);
575 
576 	status = usb_submit_urb(urb, GFP_ATOMIC);
577 	if (status != 0)
578 		dev_err(&serial->dev->dev,
579 			"%s - resubmit read urb failed: %i\n",
580 			__func__, status);
581 }
582 
583 static int qt2_setup_urbs(struct usb_serial *serial)
584 {
585 	struct usb_serial_port *port0;
586 	struct qt2_serial_private *serial_priv;
587 	int status;
588 
589 	port0 = serial->port[0];
590 
591 	serial_priv = usb_get_serial_data(serial);
592 	serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
593 	if (!serial_priv->read_urb)
594 		return -ENOMEM;
595 
596 	usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
597 			  usb_rcvbulkpipe(serial->dev,
598 					  port0->bulk_in_endpointAddress),
599 			  serial_priv->read_buffer,
600 			  QT2_READ_BUFFER_SIZE,
601 			  qt2_read_bulk_callback, serial);
602 
603 	status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
604 	if (status != 0) {
605 		dev_err(&serial->dev->dev,
606 			"%s - submit read urb failed %i\n", __func__, status);
607 		usb_free_urb(serial_priv->read_urb);
608 		return status;
609 	}
610 
611 	return 0;
612 }
613 
614 static int qt2_attach(struct usb_serial *serial)
615 {
616 	struct qt2_serial_private *serial_priv;
617 	int status;
618 
619 	/* power on unit */
620 	status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
621 				 0xc2, 0x40, 0x8000, 0, NULL, 0,
622 				 QT2_USB_TIMEOUT);
623 	if (status < 0) {
624 		dev_err(&serial->dev->dev,
625 			"%s - failed to power on unit: %i\n", __func__, status);
626 		return status;
627 	}
628 
629 	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
630 	if (!serial_priv)
631 		return -ENOMEM;
632 
633 	serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
634 	if (!serial_priv->read_buffer) {
635 		status = -ENOMEM;
636 		goto err_buf;
637 	}
638 
639 	usb_set_serial_data(serial, serial_priv);
640 
641 	status = qt2_setup_urbs(serial);
642 	if (status != 0)
643 		goto attach_failed;
644 
645 	return 0;
646 
647 attach_failed:
648 	kfree(serial_priv->read_buffer);
649 err_buf:
650 	kfree(serial_priv);
651 	return status;
652 }
653 
654 static int qt2_port_probe(struct usb_serial_port *port)
655 {
656 	struct usb_serial *serial = port->serial;
657 	struct qt2_port_private *port_priv;
658 	u8 bEndpointAddress;
659 
660 	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
661 	if (!port_priv)
662 		return -ENOMEM;
663 
664 	spin_lock_init(&port_priv->lock);
665 	spin_lock_init(&port_priv->urb_lock);
666 	port_priv->port = port;
667 
668 	port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
669 	if (!port_priv->write_buffer)
670 		goto err_buf;
671 
672 	port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
673 	if (!port_priv->write_urb)
674 		goto err_urb;
675 
676 	bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
677 	usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
678 				usb_sndbulkpipe(serial->dev, bEndpointAddress),
679 				port_priv->write_buffer,
680 				QT2_WRITE_BUFFER_SIZE,
681 				qt2_write_bulk_callback, port);
682 
683 	usb_set_serial_port_data(port, port_priv);
684 
685 	return 0;
686 err_urb:
687 	kfree(port_priv->write_buffer);
688 err_buf:
689 	kfree(port_priv);
690 	return -ENOMEM;
691 }
692 
693 static void qt2_port_remove(struct usb_serial_port *port)
694 {
695 	struct qt2_port_private *port_priv;
696 
697 	port_priv = usb_get_serial_port_data(port);
698 	usb_free_urb(port_priv->write_urb);
699 	kfree(port_priv->write_buffer);
700 	kfree(port_priv);
701 }
702 
703 static int qt2_tiocmget(struct tty_struct *tty)
704 {
705 	struct usb_serial_port *port = tty->driver_data;
706 	struct usb_device *dev = port->serial->dev;
707 	struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
708 	u8 *d;
709 	int r;
710 
711 	d = kzalloc(2, GFP_KERNEL);
712 	if (!d)
713 		return -ENOMEM;
714 
715 	r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
716 	if (r < 0)
717 		goto mget_out;
718 
719 	r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
720 	if (r < 0)
721 		goto mget_out;
722 
723 	r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
724 	    (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
725 	    (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
726 	    (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
727 	    (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
728 	    (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
729 
730 mget_out:
731 	kfree(d);
732 	return r;
733 }
734 
735 static int qt2_tiocmset(struct tty_struct *tty,
736 			unsigned int set, unsigned int clear)
737 {
738 	struct qt2_port_private *port_priv;
739 
740 	port_priv = usb_get_serial_port_data(tty->driver_data);
741 	return update_mctrl(port_priv, set, clear);
742 }
743 
744 static int qt2_break_ctl(struct tty_struct *tty, int break_state)
745 {
746 	struct usb_serial_port *port = tty->driver_data;
747 	struct qt2_port_private *port_priv;
748 	int status;
749 	u16 val;
750 
751 	port_priv = usb_get_serial_port_data(port);
752 
753 	val = (break_state == -1) ? 1 : 0;
754 
755 	status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
756 				 val, port_priv->device_port);
757 	if (status < 0) {
758 		dev_warn(&port->dev,
759 			 "%s - failed to send control message: %i\n", __func__,
760 			 status);
761 		return status;
762 	}
763 
764 	return 0;
765 }
766 
767 
768 
769 static void qt2_dtr_rts(struct usb_serial_port *port, int on)
770 {
771 	struct usb_device *dev = port->serial->dev;
772 	struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
773 
774 	/* Disable flow control */
775 	if (!on) {
776 		if (qt2_setregister(dev, port_priv->device_port,
777 					   UART_MCR, 0) < 0)
778 			dev_warn(&port->dev, "error from flowcontrol urb\n");
779 	}
780 	/* drop RTS and DTR */
781 	if (on)
782 		update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
783 	else
784 		update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
785 }
786 
787 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
788 {
789 	struct qt2_port_private *port_priv;
790 	u8 newMSR = (u8) *ch;
791 	unsigned long flags;
792 
793 	/* May be called from qt2_process_read_urb() for an unbound port. */
794 	port_priv = usb_get_serial_port_data(port);
795 	if (!port_priv)
796 		return;
797 
798 	spin_lock_irqsave(&port_priv->lock, flags);
799 	port_priv->shadowMSR = newMSR;
800 	spin_unlock_irqrestore(&port_priv->lock, flags);
801 
802 	if (newMSR & UART_MSR_ANY_DELTA) {
803 		/* update input line counters */
804 		if (newMSR & UART_MSR_DCTS)
805 			port->icount.cts++;
806 		if (newMSR & UART_MSR_DDSR)
807 			port->icount.dsr++;
808 		if (newMSR & UART_MSR_DDCD)
809 			port->icount.dcd++;
810 		if (newMSR & UART_MSR_TERI)
811 			port->icount.rng++;
812 
813 		wake_up_interruptible(&port->port.delta_msr_wait);
814 	}
815 }
816 
817 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
818 {
819 	struct qt2_port_private *port_priv;
820 	struct async_icount *icount;
821 	unsigned long flags;
822 	u8 newLSR = (u8) *ch;
823 
824 	/* May be called from qt2_process_read_urb() for an unbound port. */
825 	port_priv = usb_get_serial_port_data(port);
826 	if (!port_priv)
827 		return;
828 
829 	if (newLSR & UART_LSR_BI)
830 		newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
831 
832 	spin_lock_irqsave(&port_priv->lock, flags);
833 	port_priv->shadowLSR = newLSR;
834 	spin_unlock_irqrestore(&port_priv->lock, flags);
835 
836 	icount = &port->icount;
837 
838 	if (newLSR & UART_LSR_BRK_ERROR_BITS) {
839 
840 		if (newLSR & UART_LSR_BI)
841 			icount->brk++;
842 
843 		if (newLSR & UART_LSR_OE)
844 			icount->overrun++;
845 
846 		if (newLSR & UART_LSR_PE)
847 			icount->parity++;
848 
849 		if (newLSR & UART_LSR_FE)
850 			icount->frame++;
851 	}
852 
853 }
854 
855 static unsigned int qt2_write_room(struct tty_struct *tty)
856 {
857 	struct usb_serial_port *port = tty->driver_data;
858 	struct qt2_port_private *port_priv;
859 	unsigned long flags;
860 	unsigned int r;
861 
862 	port_priv = usb_get_serial_port_data(port);
863 
864 	spin_lock_irqsave(&port_priv->urb_lock, flags);
865 
866 	if (port_priv->urb_in_use)
867 		r = 0;
868 	else
869 		r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
870 
871 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
872 
873 	return r;
874 }
875 
876 static int qt2_write(struct tty_struct *tty,
877 		     struct usb_serial_port *port,
878 		     const unsigned char *buf, int count)
879 {
880 	struct qt2_port_private *port_priv;
881 	struct urb *write_urb;
882 	unsigned char *data;
883 	unsigned long flags;
884 	int status;
885 	int bytes_out = 0;
886 
887 	port_priv = usb_get_serial_port_data(port);
888 
889 	if (port_priv->write_urb == NULL) {
890 		dev_err(&port->dev, "%s - no output urb\n", __func__);
891 		return 0;
892 	}
893 	write_urb = port_priv->write_urb;
894 
895 	count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
896 
897 	data = write_urb->transfer_buffer;
898 	spin_lock_irqsave(&port_priv->urb_lock, flags);
899 	if (port_priv->urb_in_use) {
900 		dev_err(&port->dev, "qt2_write - urb is in use\n");
901 		goto write_out;
902 	}
903 
904 	*data++ = QT2_CONTROL_BYTE;
905 	*data++ = QT2_CONTROL_BYTE;
906 	*data++ = port_priv->device_port;
907 	put_unaligned_le16(count, data);
908 	data += 2;
909 	memcpy(data, buf, count);
910 
911 	write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
912 
913 	status = usb_submit_urb(write_urb, GFP_ATOMIC);
914 	if (status == 0) {
915 		port_priv->urb_in_use = true;
916 		bytes_out += count;
917 	}
918 
919 write_out:
920 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
921 	return bytes_out;
922 }
923 
924 
925 static struct usb_serial_driver qt2_device = {
926 	.driver = {
927 		.owner = THIS_MODULE,
928 		.name = "quatech-serial",
929 	},
930 	.description	     = DRIVER_DESC,
931 	.id_table	     = id_table,
932 	.open		     = qt2_open,
933 	.close		     = qt2_close,
934 	.write               = qt2_write,
935 	.write_room          = qt2_write_room,
936 	.calc_num_ports      = qt2_calc_num_ports,
937 	.attach              = qt2_attach,
938 	.release             = qt2_release,
939 	.disconnect          = qt2_disconnect,
940 	.port_probe          = qt2_port_probe,
941 	.port_remove         = qt2_port_remove,
942 	.dtr_rts             = qt2_dtr_rts,
943 	.break_ctl           = qt2_break_ctl,
944 	.tiocmget            = qt2_tiocmget,
945 	.tiocmset            = qt2_tiocmset,
946 	.tiocmiwait          = usb_serial_generic_tiocmiwait,
947 	.get_icount	     = usb_serial_generic_get_icount,
948 	.set_termios         = qt2_set_termios,
949 };
950 
951 static struct usb_serial_driver *const serial_drivers[] = {
952 	&qt2_device, NULL
953 };
954 
955 module_usb_serial_driver(serial_drivers, id_table);
956 
957 MODULE_DESCRIPTION(DRIVER_DESC);
958 MODULE_LICENSE("GPL v2");
959