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