xref: /openbmc/linux/drivers/usb/serial/pl2303.c (revision 07c7c6bf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Prolific PL2303 USB to serial adaptor driver
4  *
5  * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2003 IBM Corp.
7  *
8  * Original driver for 2.2.x by anonymous
9  *
10  * See Documentation/usb/usb-serial.txt for more information on using this
11  * driver
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/spinlock.h>
24 #include <linux/uaccess.h>
25 #include <linux/usb.h>
26 #include <linux/usb/serial.h>
27 #include <asm/unaligned.h>
28 #include "pl2303.h"
29 
30 
31 #define PL2303_QUIRK_UART_STATE_IDX0		BIT(0)
32 #define PL2303_QUIRK_LEGACY			BIT(1)
33 #define PL2303_QUIRK_ENDPOINT_HACK		BIT(2)
34 
35 static const struct usb_device_id id_table[] = {
36 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID),
37 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
38 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
39 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
40 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
41 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_CHILITAG) },
42 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
43 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
44 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
45 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
46 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
47 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
48 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
49 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_TB) },
50 	{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
51 	{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
52 	{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID),
53 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
54 	{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485),
55 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
56 	{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC232B),
57 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
58 	{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
59 	{ USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
60 	{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
61 	{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
62 	{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
63 	{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
64 	{ USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
65 	{ USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
66 	{ USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
67 	{ USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
68 	{ USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
69 	{ USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
70 	{ USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
71 	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1),
72 		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
73 	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65),
74 		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
75 	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75),
76 		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
77 	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81),
78 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
79 	{ USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
80 	{ USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
81 	{ USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
82 	{ USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
83 	{ USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
84 	{ USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
85 	{ USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
86 	{ USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
87 	{ USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
88 	{ USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID),
89 		.driver_info = PL2303_QUIRK_ENDPOINT_HACK },
90 	{ USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
91 	{ USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
92 	{ USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
93 	{ USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
94 	{ USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
95 	{ USB_DEVICE(HP_VENDOR_ID, HP_LD220TA_PRODUCT_ID) },
96 	{ USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
97 	{ USB_DEVICE(HP_VENDOR_ID, HP_LD960TA_PRODUCT_ID) },
98 	{ USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
99 	{ USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
100 	{ USB_DEVICE(HP_VENDOR_ID, HP_LM920_PRODUCT_ID) },
101 	{ USB_DEVICE(HP_VENDOR_ID, HP_LM940_PRODUCT_ID) },
102 	{ USB_DEVICE(HP_VENDOR_ID, HP_TD620_PRODUCT_ID) },
103 	{ USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
104 	{ USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
105 	{ USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
106 	{ USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
107 	{ USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
108 	{ USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
109 	{ }					/* Terminating entry */
110 };
111 
112 MODULE_DEVICE_TABLE(usb, id_table);
113 
114 #define SET_LINE_REQUEST_TYPE		0x21
115 #define SET_LINE_REQUEST		0x20
116 
117 #define SET_CONTROL_REQUEST_TYPE	0x21
118 #define SET_CONTROL_REQUEST		0x22
119 #define CONTROL_DTR			0x01
120 #define CONTROL_RTS			0x02
121 
122 #define BREAK_REQUEST_TYPE		0x21
123 #define BREAK_REQUEST			0x23
124 #define BREAK_ON			0xffff
125 #define BREAK_OFF			0x0000
126 
127 #define GET_LINE_REQUEST_TYPE		0xa1
128 #define GET_LINE_REQUEST		0x21
129 
130 #define VENDOR_WRITE_REQUEST_TYPE	0x40
131 #define VENDOR_WRITE_REQUEST		0x01
132 
133 #define VENDOR_READ_REQUEST_TYPE	0xc0
134 #define VENDOR_READ_REQUEST		0x01
135 
136 #define UART_STATE_INDEX		8
137 #define UART_STATE_MSR_MASK		0x8b
138 #define UART_STATE_TRANSIENT_MASK	0x74
139 #define UART_DCD			0x01
140 #define UART_DSR			0x02
141 #define UART_BREAK_ERROR		0x04
142 #define UART_RING			0x08
143 #define UART_FRAME_ERROR		0x10
144 #define UART_PARITY_ERROR		0x20
145 #define UART_OVERRUN_ERROR		0x40
146 #define UART_CTS			0x80
147 
148 #define PL2303_FLOWCTRL_MASK		0xf0
149 
150 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
151 
152 enum pl2303_type {
153 	TYPE_01,	/* Type 0 and 1 (difference unknown) */
154 	TYPE_HX,	/* HX version of the pl2303 chip */
155 	TYPE_COUNT
156 };
157 
158 struct pl2303_type_data {
159 	speed_t max_baud_rate;
160 	unsigned long quirks;
161 	unsigned int no_autoxonxoff:1;
162 };
163 
164 struct pl2303_serial_private {
165 	const struct pl2303_type_data *type;
166 	unsigned long quirks;
167 };
168 
169 struct pl2303_private {
170 	spinlock_t lock;
171 	u8 line_control;
172 	u8 line_status;
173 
174 	u8 line_settings[7];
175 };
176 
177 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
178 	[TYPE_01] = {
179 		.max_baud_rate		= 1228800,
180 		.quirks			= PL2303_QUIRK_LEGACY,
181 		.no_autoxonxoff		= true,
182 	},
183 	[TYPE_HX] = {
184 		.max_baud_rate		= 12000000,
185 	},
186 };
187 
188 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
189 							unsigned char buf[1])
190 {
191 	struct device *dev = &serial->interface->dev;
192 	int res;
193 
194 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
195 			VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
196 			value, 0, buf, 1, 100);
197 	if (res != 1) {
198 		dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
199 								value, res);
200 		if (res >= 0)
201 			res = -EIO;
202 
203 		return res;
204 	}
205 
206 	dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
207 
208 	return 0;
209 }
210 
211 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
212 {
213 	struct device *dev = &serial->interface->dev;
214 	int res;
215 
216 	dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
217 
218 	res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
219 			VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
220 			value, index, NULL, 0, 100);
221 	if (res) {
222 		dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
223 								value, res);
224 		return res;
225 	}
226 
227 	return 0;
228 }
229 
230 static int pl2303_update_reg(struct usb_serial *serial, u8 reg, u8 mask, u8 val)
231 {
232 	int ret = 0;
233 	u8 *buf;
234 
235 	buf = kmalloc(1, GFP_KERNEL);
236 	if (!buf)
237 		return -ENOMEM;
238 
239 	ret = pl2303_vendor_read(serial, reg | 0x80, buf);
240 	if (ret)
241 		goto out_free;
242 
243 	*buf &= ~mask;
244 	*buf |= val & mask;
245 
246 	ret = pl2303_vendor_write(serial, reg, *buf);
247 out_free:
248 	kfree(buf);
249 
250 	return ret;
251 }
252 
253 static int pl2303_probe(struct usb_serial *serial,
254 					const struct usb_device_id *id)
255 {
256 	usb_set_serial_data(serial, (void *)id->driver_info);
257 
258 	return 0;
259 }
260 
261 /*
262  * Use interrupt endpoint from first interface if available.
263  *
264  * This is needed due to the looney way its endpoints are set up.
265  */
266 static int pl2303_endpoint_hack(struct usb_serial *serial,
267 					struct usb_serial_endpoints *epds)
268 {
269 	struct usb_interface *interface = serial->interface;
270 	struct usb_device *dev = serial->dev;
271 	struct device *ddev = &interface->dev;
272 	struct usb_host_interface *iface_desc;
273 	struct usb_endpoint_descriptor *endpoint;
274 	unsigned int i;
275 
276 	if (interface == dev->actconfig->interface[0])
277 		return 0;
278 
279 	/* check out the endpoints of the other interface */
280 	iface_desc = dev->actconfig->interface[0]->cur_altsetting;
281 
282 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
283 		endpoint = &iface_desc->endpoint[i].desc;
284 
285 		if (!usb_endpoint_is_int_in(endpoint))
286 			continue;
287 
288 		dev_dbg(ddev, "found interrupt in on separate interface\n");
289 		if (epds->num_interrupt_in < ARRAY_SIZE(epds->interrupt_in))
290 			epds->interrupt_in[epds->num_interrupt_in++] = endpoint;
291 	}
292 
293 	return 0;
294 }
295 
296 static int pl2303_calc_num_ports(struct usb_serial *serial,
297 					struct usb_serial_endpoints *epds)
298 {
299 	unsigned long quirks = (unsigned long)usb_get_serial_data(serial);
300 	struct device *dev = &serial->interface->dev;
301 	int ret;
302 
303 	if (quirks & PL2303_QUIRK_ENDPOINT_HACK) {
304 		ret = pl2303_endpoint_hack(serial, epds);
305 		if (ret)
306 			return ret;
307 	}
308 
309 	if (epds->num_interrupt_in < 1) {
310 		dev_err(dev, "required interrupt-in endpoint missing\n");
311 		return -ENODEV;
312 	}
313 
314 	return 1;
315 }
316 
317 static int pl2303_startup(struct usb_serial *serial)
318 {
319 	struct pl2303_serial_private *spriv;
320 	enum pl2303_type type = TYPE_01;
321 	unsigned char *buf;
322 
323 	spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
324 	if (!spriv)
325 		return -ENOMEM;
326 
327 	buf = kmalloc(1, GFP_KERNEL);
328 	if (!buf) {
329 		kfree(spriv);
330 		return -ENOMEM;
331 	}
332 
333 	if (serial->dev->descriptor.bDeviceClass == 0x02)
334 		type = TYPE_01;		/* type 0 */
335 	else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
336 		type = TYPE_HX;
337 	else if (serial->dev->descriptor.bDeviceClass == 0x00)
338 		type = TYPE_01;		/* type 1 */
339 	else if (serial->dev->descriptor.bDeviceClass == 0xFF)
340 		type = TYPE_01;		/* type 1 */
341 	dev_dbg(&serial->interface->dev, "device type: %d\n", type);
342 
343 	spriv->type = &pl2303_type_data[type];
344 	spriv->quirks = (unsigned long)usb_get_serial_data(serial);
345 	spriv->quirks |= spriv->type->quirks;
346 
347 	usb_set_serial_data(serial, spriv);
348 
349 	pl2303_vendor_read(serial, 0x8484, buf);
350 	pl2303_vendor_write(serial, 0x0404, 0);
351 	pl2303_vendor_read(serial, 0x8484, buf);
352 	pl2303_vendor_read(serial, 0x8383, buf);
353 	pl2303_vendor_read(serial, 0x8484, buf);
354 	pl2303_vendor_write(serial, 0x0404, 1);
355 	pl2303_vendor_read(serial, 0x8484, buf);
356 	pl2303_vendor_read(serial, 0x8383, buf);
357 	pl2303_vendor_write(serial, 0, 1);
358 	pl2303_vendor_write(serial, 1, 0);
359 	if (spriv->quirks & PL2303_QUIRK_LEGACY)
360 		pl2303_vendor_write(serial, 2, 0x24);
361 	else
362 		pl2303_vendor_write(serial, 2, 0x44);
363 
364 	kfree(buf);
365 
366 	return 0;
367 }
368 
369 static void pl2303_release(struct usb_serial *serial)
370 {
371 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
372 
373 	kfree(spriv);
374 }
375 
376 static int pl2303_port_probe(struct usb_serial_port *port)
377 {
378 	struct pl2303_private *priv;
379 
380 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
381 	if (!priv)
382 		return -ENOMEM;
383 
384 	spin_lock_init(&priv->lock);
385 
386 	usb_set_serial_port_data(port, priv);
387 
388 	port->port.drain_delay = 256;
389 
390 	return 0;
391 }
392 
393 static int pl2303_port_remove(struct usb_serial_port *port)
394 {
395 	struct pl2303_private *priv = usb_get_serial_port_data(port);
396 
397 	kfree(priv);
398 
399 	return 0;
400 }
401 
402 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
403 {
404 	struct usb_device *dev = port->serial->dev;
405 	int retval;
406 
407 	dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
408 
409 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
410 				 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
411 				 value, 0, NULL, 0, 100);
412 	if (retval)
413 		dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
414 
415 	return retval;
416 }
417 
418 /*
419  * Returns the nearest supported baud rate that can be set directly without
420  * using divisors.
421  */
422 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
423 {
424 	static const speed_t baud_sup[] = {
425 		75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
426 		14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
427 		614400, 921600, 1228800, 2457600, 3000000, 6000000
428 	};
429 
430 	unsigned i;
431 
432 	for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
433 		if (baud_sup[i] > baud)
434 			break;
435 	}
436 
437 	if (i == ARRAY_SIZE(baud_sup))
438 		baud = baud_sup[i - 1];
439 	else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
440 		baud = baud_sup[i - 1];
441 	else
442 		baud = baud_sup[i];
443 
444 	return baud;
445 }
446 
447 /*
448  * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
449  *       use 9600 baud.
450  */
451 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
452 								speed_t baud)
453 {
454 	put_unaligned_le32(baud, buf);
455 
456 	return baud;
457 }
458 
459 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
460 								speed_t baud)
461 {
462 	unsigned int baseline, mantissa, exponent;
463 
464 	/*
465 	 * Apparently the formula is:
466 	 *   baudrate = 12M * 32 / (mantissa * 4^exponent)
467 	 * where
468 	 *   mantissa = buf[8:0]
469 	 *   exponent = buf[11:9]
470 	 */
471 	baseline = 12000000 * 32;
472 	mantissa = baseline / baud;
473 	if (mantissa == 0)
474 		mantissa = 1;	/* Avoid dividing by zero if baud > 32*12M. */
475 	exponent = 0;
476 	while (mantissa >= 512) {
477 		if (exponent < 7) {
478 			mantissa >>= 2;	/* divide by 4 */
479 			exponent++;
480 		} else {
481 			/* Exponent is maxed. Trim mantissa and leave. */
482 			mantissa = 511;
483 			break;
484 		}
485 	}
486 
487 	buf[3] = 0x80;
488 	buf[2] = 0;
489 	buf[1] = exponent << 1 | mantissa >> 8;
490 	buf[0] = mantissa & 0xff;
491 
492 	/* Calculate and return the exact baud rate. */
493 	baud = (baseline / mantissa) >> (exponent << 1);
494 
495 	return baud;
496 }
497 
498 static void pl2303_encode_baud_rate(struct tty_struct *tty,
499 					struct usb_serial_port *port,
500 					u8 buf[4])
501 {
502 	struct usb_serial *serial = port->serial;
503 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
504 	speed_t	baud_sup;
505 	speed_t baud;
506 
507 	baud = tty_get_baud_rate(tty);
508 	dev_dbg(&port->dev, "baud requested = %u\n", baud);
509 	if (!baud)
510 		return;
511 
512 	if (spriv->type->max_baud_rate)
513 		baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
514 	/*
515 	 * Use direct method for supported baud rates, otherwise use divisors.
516 	 */
517 	baud_sup = pl2303_get_supported_baud_rate(baud);
518 
519 	if (baud == baud_sup)
520 		baud = pl2303_encode_baud_rate_direct(buf, baud);
521 	else
522 		baud = pl2303_encode_baud_rate_divisor(buf, baud);
523 
524 	/* Save resulting baud rate */
525 	tty_encode_baud_rate(tty, baud, baud);
526 	dev_dbg(&port->dev, "baud set = %u\n", baud);
527 }
528 
529 static int pl2303_get_line_request(struct usb_serial_port *port,
530 							unsigned char buf[7])
531 {
532 	struct usb_device *udev = port->serial->dev;
533 	int ret;
534 
535 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
536 				GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
537 				0, 0, buf, 7, 100);
538 	if (ret != 7) {
539 		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
540 
541 		if (ret >= 0)
542 			ret = -EIO;
543 
544 		return ret;
545 	}
546 
547 	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
548 
549 	return 0;
550 }
551 
552 static int pl2303_set_line_request(struct usb_serial_port *port,
553 							unsigned char buf[7])
554 {
555 	struct usb_device *udev = port->serial->dev;
556 	int ret;
557 
558 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
559 				SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
560 				0, 0, buf, 7, 100);
561 	if (ret < 0) {
562 		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
563 		return ret;
564 	}
565 
566 	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
567 
568 	return 0;
569 }
570 
571 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b)
572 {
573 	bool ixon_change;
574 
575 	ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) ||
576 			a->c_cc[VSTART] != b->c_cc[VSTART] ||
577 			a->c_cc[VSTOP] != b->c_cc[VSTOP];
578 
579 	return tty_termios_hw_change(a, b) || ixon_change;
580 }
581 
582 static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type)
583 {
584 	if (!I_IXON(tty) || I_IXANY(tty))
585 		return false;
586 
587 	if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13)
588 		return false;
589 
590 	if (type->no_autoxonxoff)
591 		return false;
592 
593 	return true;
594 }
595 
596 static void pl2303_set_termios(struct tty_struct *tty,
597 		struct usb_serial_port *port, struct ktermios *old_termios)
598 {
599 	struct usb_serial *serial = port->serial;
600 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
601 	struct pl2303_private *priv = usb_get_serial_port_data(port);
602 	unsigned long flags;
603 	unsigned char *buf;
604 	int ret;
605 	u8 control;
606 
607 	if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
608 		return;
609 
610 	buf = kzalloc(7, GFP_KERNEL);
611 	if (!buf) {
612 		/* Report back no change occurred */
613 		if (old_termios)
614 			tty->termios = *old_termios;
615 		return;
616 	}
617 
618 	pl2303_get_line_request(port, buf);
619 
620 	switch (C_CSIZE(tty)) {
621 	case CS5:
622 		buf[6] = 5;
623 		break;
624 	case CS6:
625 		buf[6] = 6;
626 		break;
627 	case CS7:
628 		buf[6] = 7;
629 		break;
630 	default:
631 	case CS8:
632 		buf[6] = 8;
633 	}
634 	dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
635 
636 	/* For reference buf[0]:buf[3] baud rate value */
637 	pl2303_encode_baud_rate(tty, port, &buf[0]);
638 
639 	/* For reference buf[4]=0 is 1 stop bits */
640 	/* For reference buf[4]=1 is 1.5 stop bits */
641 	/* For reference buf[4]=2 is 2 stop bits */
642 	if (C_CSTOPB(tty)) {
643 		/*
644 		 * NOTE: Comply with "real" UARTs / RS232:
645 		 *       use 1.5 instead of 2 stop bits with 5 data bits
646 		 */
647 		if (C_CSIZE(tty) == CS5) {
648 			buf[4] = 1;
649 			dev_dbg(&port->dev, "stop bits = 1.5\n");
650 		} else {
651 			buf[4] = 2;
652 			dev_dbg(&port->dev, "stop bits = 2\n");
653 		}
654 	} else {
655 		buf[4] = 0;
656 		dev_dbg(&port->dev, "stop bits = 1\n");
657 	}
658 
659 	if (C_PARENB(tty)) {
660 		/* For reference buf[5]=0 is none parity */
661 		/* For reference buf[5]=1 is odd parity */
662 		/* For reference buf[5]=2 is even parity */
663 		/* For reference buf[5]=3 is mark parity */
664 		/* For reference buf[5]=4 is space parity */
665 		if (C_PARODD(tty)) {
666 			if (C_CMSPAR(tty)) {
667 				buf[5] = 3;
668 				dev_dbg(&port->dev, "parity = mark\n");
669 			} else {
670 				buf[5] = 1;
671 				dev_dbg(&port->dev, "parity = odd\n");
672 			}
673 		} else {
674 			if (C_CMSPAR(tty)) {
675 				buf[5] = 4;
676 				dev_dbg(&port->dev, "parity = space\n");
677 			} else {
678 				buf[5] = 2;
679 				dev_dbg(&port->dev, "parity = even\n");
680 			}
681 		}
682 	} else {
683 		buf[5] = 0;
684 		dev_dbg(&port->dev, "parity = none\n");
685 	}
686 
687 	/*
688 	 * Some PL2303 are known to lose bytes if you change serial settings
689 	 * even to the same values as before. Thus we actually need to filter
690 	 * in this specific case.
691 	 *
692 	 * Note that the tty_termios_hw_change check above is not sufficient
693 	 * as a previously requested baud rate may differ from the one
694 	 * actually used (and stored in old_termios).
695 	 *
696 	 * NOTE: No additional locking needed for line_settings as it is
697 	 *       only used in set_termios, which is serialised against itself.
698 	 */
699 	if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
700 		ret = pl2303_set_line_request(port, buf);
701 		if (!ret)
702 			memcpy(priv->line_settings, buf, 7);
703 	}
704 
705 	/* change control lines if we are switching to or from B0 */
706 	spin_lock_irqsave(&priv->lock, flags);
707 	control = priv->line_control;
708 	if (C_BAUD(tty) == B0)
709 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
710 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
711 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
712 	if (control != priv->line_control) {
713 		control = priv->line_control;
714 		spin_unlock_irqrestore(&priv->lock, flags);
715 		pl2303_set_control_lines(port, control);
716 	} else {
717 		spin_unlock_irqrestore(&priv->lock, flags);
718 	}
719 
720 	if (C_CRTSCTS(tty)) {
721 		if (spriv->quirks & PL2303_QUIRK_LEGACY)
722 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
723 		else
724 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
725 	} else if (pl2303_enable_xonxoff(tty, spriv->type)) {
726 		pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
727 	} else {
728 		pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
729 	}
730 
731 	kfree(buf);
732 }
733 
734 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
735 {
736 	struct pl2303_private *priv = usb_get_serial_port_data(port);
737 	unsigned long flags;
738 	u8 control;
739 
740 	spin_lock_irqsave(&priv->lock, flags);
741 	if (on)
742 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
743 	else
744 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
745 	control = priv->line_control;
746 	spin_unlock_irqrestore(&priv->lock, flags);
747 
748 	pl2303_set_control_lines(port, control);
749 }
750 
751 static void pl2303_close(struct usb_serial_port *port)
752 {
753 	usb_serial_generic_close(port);
754 	usb_kill_urb(port->interrupt_in_urb);
755 	pl2303_set_break(port, false);
756 }
757 
758 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
759 {
760 	struct usb_serial *serial = port->serial;
761 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
762 	int result;
763 
764 	if (spriv->quirks & PL2303_QUIRK_LEGACY) {
765 		usb_clear_halt(serial->dev, port->write_urb->pipe);
766 		usb_clear_halt(serial->dev, port->read_urb->pipe);
767 	} else {
768 		/* reset upstream data pipes */
769 		pl2303_vendor_write(serial, 8, 0);
770 		pl2303_vendor_write(serial, 9, 0);
771 	}
772 
773 	/* Setup termios */
774 	if (tty)
775 		pl2303_set_termios(tty, port, NULL);
776 
777 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
778 	if (result) {
779 		dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
780 			result);
781 		return result;
782 	}
783 
784 	result = usb_serial_generic_open(tty, port);
785 	if (result) {
786 		usb_kill_urb(port->interrupt_in_urb);
787 		return result;
788 	}
789 
790 	return 0;
791 }
792 
793 static int pl2303_tiocmset(struct tty_struct *tty,
794 			   unsigned int set, unsigned int clear)
795 {
796 	struct usb_serial_port *port = tty->driver_data;
797 	struct pl2303_private *priv = usb_get_serial_port_data(port);
798 	unsigned long flags;
799 	u8 control;
800 	int ret;
801 
802 	spin_lock_irqsave(&priv->lock, flags);
803 	if (set & TIOCM_RTS)
804 		priv->line_control |= CONTROL_RTS;
805 	if (set & TIOCM_DTR)
806 		priv->line_control |= CONTROL_DTR;
807 	if (clear & TIOCM_RTS)
808 		priv->line_control &= ~CONTROL_RTS;
809 	if (clear & TIOCM_DTR)
810 		priv->line_control &= ~CONTROL_DTR;
811 	control = priv->line_control;
812 	spin_unlock_irqrestore(&priv->lock, flags);
813 
814 	ret = pl2303_set_control_lines(port, control);
815 	if (ret)
816 		return usb_translate_errors(ret);
817 
818 	return 0;
819 }
820 
821 static int pl2303_tiocmget(struct tty_struct *tty)
822 {
823 	struct usb_serial_port *port = tty->driver_data;
824 	struct pl2303_private *priv = usb_get_serial_port_data(port);
825 	unsigned long flags;
826 	unsigned int mcr;
827 	unsigned int status;
828 	unsigned int result;
829 
830 	spin_lock_irqsave(&priv->lock, flags);
831 	mcr = priv->line_control;
832 	status = priv->line_status;
833 	spin_unlock_irqrestore(&priv->lock, flags);
834 
835 	result = ((mcr & CONTROL_DTR)		? TIOCM_DTR : 0)
836 		  | ((mcr & CONTROL_RTS)	? TIOCM_RTS : 0)
837 		  | ((status & UART_CTS)	? TIOCM_CTS : 0)
838 		  | ((status & UART_DSR)	? TIOCM_DSR : 0)
839 		  | ((status & UART_RING)	? TIOCM_RI  : 0)
840 		  | ((status & UART_DCD)	? TIOCM_CD  : 0);
841 
842 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
843 
844 	return result;
845 }
846 
847 static int pl2303_carrier_raised(struct usb_serial_port *port)
848 {
849 	struct pl2303_private *priv = usb_get_serial_port_data(port);
850 
851 	if (priv->line_status & UART_DCD)
852 		return 1;
853 
854 	return 0;
855 }
856 
857 static int pl2303_get_serial(struct tty_struct *tty,
858 			struct serial_struct *ss)
859 {
860 	struct usb_serial_port *port = tty->driver_data;
861 
862 	ss->type = PORT_16654;
863 	ss->line = port->minor;
864 	ss->port = port->port_number;
865 	ss->baud_base = 460800;
866 	return 0;
867 }
868 
869 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
870 {
871 	struct usb_serial *serial = port->serial;
872 	u16 state;
873 	int result;
874 
875 	if (enable)
876 		state = BREAK_ON;
877 	else
878 		state = BREAK_OFF;
879 
880 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
881 			state == BREAK_OFF ? "off" : "on");
882 
883 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
884 				 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
885 				 0, NULL, 0, 100);
886 	if (result)
887 		dev_err(&port->dev, "error sending break = %d\n", result);
888 }
889 
890 static void pl2303_break_ctl(struct tty_struct *tty, int state)
891 {
892 	struct usb_serial_port *port = tty->driver_data;
893 
894 	pl2303_set_break(port, state);
895 }
896 
897 static void pl2303_update_line_status(struct usb_serial_port *port,
898 				      unsigned char *data,
899 				      unsigned int actual_length)
900 {
901 	struct usb_serial *serial = port->serial;
902 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
903 	struct pl2303_private *priv = usb_get_serial_port_data(port);
904 	struct tty_struct *tty;
905 	unsigned long flags;
906 	unsigned int status_idx = UART_STATE_INDEX;
907 	u8 status;
908 	u8 delta;
909 
910 	if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
911 		status_idx = 0;
912 
913 	if (actual_length < status_idx + 1)
914 		return;
915 
916 	status = data[status_idx];
917 
918 	/* Save off the uart status for others to look at */
919 	spin_lock_irqsave(&priv->lock, flags);
920 	delta = priv->line_status ^ status;
921 	priv->line_status = status;
922 	spin_unlock_irqrestore(&priv->lock, flags);
923 
924 	if (status & UART_BREAK_ERROR)
925 		usb_serial_handle_break(port);
926 
927 	if (delta & UART_STATE_MSR_MASK) {
928 		if (delta & UART_CTS)
929 			port->icount.cts++;
930 		if (delta & UART_DSR)
931 			port->icount.dsr++;
932 		if (delta & UART_RING)
933 			port->icount.rng++;
934 		if (delta & UART_DCD) {
935 			port->icount.dcd++;
936 			tty = tty_port_tty_get(&port->port);
937 			if (tty) {
938 				usb_serial_handle_dcd_change(port, tty,
939 							status & UART_DCD);
940 				tty_kref_put(tty);
941 			}
942 		}
943 
944 		wake_up_interruptible(&port->port.delta_msr_wait);
945 	}
946 }
947 
948 static void pl2303_read_int_callback(struct urb *urb)
949 {
950 	struct usb_serial_port *port =  urb->context;
951 	unsigned char *data = urb->transfer_buffer;
952 	unsigned int actual_length = urb->actual_length;
953 	int status = urb->status;
954 	int retval;
955 
956 	switch (status) {
957 	case 0:
958 		/* success */
959 		break;
960 	case -ECONNRESET:
961 	case -ENOENT:
962 	case -ESHUTDOWN:
963 		/* this urb is terminated, clean up */
964 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
965 			__func__, status);
966 		return;
967 	default:
968 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
969 			__func__, status);
970 		goto exit;
971 	}
972 
973 	usb_serial_debug_data(&port->dev, __func__,
974 			      urb->actual_length, urb->transfer_buffer);
975 
976 	pl2303_update_line_status(port, data, actual_length);
977 
978 exit:
979 	retval = usb_submit_urb(urb, GFP_ATOMIC);
980 	if (retval) {
981 		dev_err(&port->dev,
982 			"%s - usb_submit_urb failed with result %d\n",
983 			__func__, retval);
984 	}
985 }
986 
987 static void pl2303_process_read_urb(struct urb *urb)
988 {
989 	struct usb_serial_port *port = urb->context;
990 	struct pl2303_private *priv = usb_get_serial_port_data(port);
991 	unsigned char *data = urb->transfer_buffer;
992 	char tty_flag = TTY_NORMAL;
993 	unsigned long flags;
994 	u8 line_status;
995 	int i;
996 
997 	/* update line status */
998 	spin_lock_irqsave(&priv->lock, flags);
999 	line_status = priv->line_status;
1000 	priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1001 	spin_unlock_irqrestore(&priv->lock, flags);
1002 
1003 	if (!urb->actual_length)
1004 		return;
1005 
1006 	/*
1007 	 * Break takes precedence over parity, which takes precedence over
1008 	 * framing errors.
1009 	 */
1010 	if (line_status & UART_BREAK_ERROR)
1011 		tty_flag = TTY_BREAK;
1012 	else if (line_status & UART_PARITY_ERROR)
1013 		tty_flag = TTY_PARITY;
1014 	else if (line_status & UART_FRAME_ERROR)
1015 		tty_flag = TTY_FRAME;
1016 
1017 	if (tty_flag != TTY_NORMAL)
1018 		dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
1019 								tty_flag);
1020 	/* overrun is special, not associated with a char */
1021 	if (line_status & UART_OVERRUN_ERROR)
1022 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1023 
1024 	if (port->port.console && port->sysrq) {
1025 		for (i = 0; i < urb->actual_length; ++i)
1026 			if (!usb_serial_handle_sysrq_char(port, data[i]))
1027 				tty_insert_flip_char(&port->port, data[i],
1028 						tty_flag);
1029 	} else {
1030 		tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
1031 							urb->actual_length);
1032 	}
1033 
1034 	tty_flip_buffer_push(&port->port);
1035 }
1036 
1037 static struct usb_serial_driver pl2303_device = {
1038 	.driver = {
1039 		.owner =	THIS_MODULE,
1040 		.name =		"pl2303",
1041 	},
1042 	.id_table =		id_table,
1043 	.num_bulk_in =		1,
1044 	.num_bulk_out =		1,
1045 	.num_interrupt_in =	0,	/* see pl2303_calc_num_ports */
1046 	.bulk_in_size =		256,
1047 	.bulk_out_size =	256,
1048 	.open =			pl2303_open,
1049 	.close =		pl2303_close,
1050 	.dtr_rts =		pl2303_dtr_rts,
1051 	.carrier_raised =	pl2303_carrier_raised,
1052 	.get_serial =		pl2303_get_serial,
1053 	.break_ctl =		pl2303_break_ctl,
1054 	.set_termios =		pl2303_set_termios,
1055 	.tiocmget =		pl2303_tiocmget,
1056 	.tiocmset =		pl2303_tiocmset,
1057 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1058 	.process_read_urb =	pl2303_process_read_urb,
1059 	.read_int_callback =	pl2303_read_int_callback,
1060 	.probe =		pl2303_probe,
1061 	.calc_num_ports =	pl2303_calc_num_ports,
1062 	.attach =		pl2303_startup,
1063 	.release =		pl2303_release,
1064 	.port_probe =		pl2303_port_probe,
1065 	.port_remove =		pl2303_port_remove,
1066 };
1067 
1068 static struct usb_serial_driver * const serial_drivers[] = {
1069 	&pl2303_device, NULL
1070 };
1071 
1072 module_usb_serial_driver(serial_drivers, id_table);
1073 
1074 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1075 MODULE_LICENSE("GPL v2");
1076