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