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