xref: /openbmc/linux/drivers/usb/serial/pl2303.c (revision b755c25f)
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 int 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,
793 			       const struct ktermios *old_termios)
794 {
795 	struct usb_serial *serial = port->serial;
796 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
797 	struct pl2303_private *priv = usb_get_serial_port_data(port);
798 	unsigned long flags;
799 	unsigned char *buf;
800 	int ret;
801 	u8 control;
802 
803 	if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
804 		return;
805 
806 	buf = kzalloc(7, GFP_KERNEL);
807 	if (!buf) {
808 		/* Report back no change occurred */
809 		if (old_termios)
810 			tty->termios = *old_termios;
811 		return;
812 	}
813 
814 	pl2303_get_line_request(port, buf);
815 
816 	buf[6] = tty_get_char_size(tty->termios.c_cflag);
817 	dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
818 
819 	/* For reference buf[0]:buf[3] baud rate value */
820 	pl2303_encode_baud_rate(tty, port, &buf[0]);
821 
822 	/* For reference buf[4]=0 is 1 stop bits */
823 	/* For reference buf[4]=1 is 1.5 stop bits */
824 	/* For reference buf[4]=2 is 2 stop bits */
825 	if (C_CSTOPB(tty)) {
826 		/*
827 		 * NOTE: Comply with "real" UARTs / RS232:
828 		 *       use 1.5 instead of 2 stop bits with 5 data bits
829 		 */
830 		if (C_CSIZE(tty) == CS5) {
831 			buf[4] = 1;
832 			dev_dbg(&port->dev, "stop bits = 1.5\n");
833 		} else {
834 			buf[4] = 2;
835 			dev_dbg(&port->dev, "stop bits = 2\n");
836 		}
837 	} else {
838 		buf[4] = 0;
839 		dev_dbg(&port->dev, "stop bits = 1\n");
840 	}
841 
842 	if (C_PARENB(tty)) {
843 		/* For reference buf[5]=0 is none parity */
844 		/* For reference buf[5]=1 is odd parity */
845 		/* For reference buf[5]=2 is even parity */
846 		/* For reference buf[5]=3 is mark parity */
847 		/* For reference buf[5]=4 is space parity */
848 		if (C_PARODD(tty)) {
849 			if (C_CMSPAR(tty)) {
850 				buf[5] = 3;
851 				dev_dbg(&port->dev, "parity = mark\n");
852 			} else {
853 				buf[5] = 1;
854 				dev_dbg(&port->dev, "parity = odd\n");
855 			}
856 		} else {
857 			if (C_CMSPAR(tty)) {
858 				buf[5] = 4;
859 				dev_dbg(&port->dev, "parity = space\n");
860 			} else {
861 				buf[5] = 2;
862 				dev_dbg(&port->dev, "parity = even\n");
863 			}
864 		}
865 	} else {
866 		buf[5] = 0;
867 		dev_dbg(&port->dev, "parity = none\n");
868 	}
869 
870 	/*
871 	 * Some PL2303 are known to lose bytes if you change serial settings
872 	 * even to the same values as before. Thus we actually need to filter
873 	 * in this specific case.
874 	 *
875 	 * Note that the tty_termios_hw_change check above is not sufficient
876 	 * as a previously requested baud rate may differ from the one
877 	 * actually used (and stored in old_termios).
878 	 *
879 	 * NOTE: No additional locking needed for line_settings as it is
880 	 *       only used in set_termios, which is serialised against itself.
881 	 */
882 	if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
883 		ret = pl2303_set_line_request(port, buf);
884 		if (!ret)
885 			memcpy(priv->line_settings, buf, 7);
886 	}
887 
888 	/* change control lines if we are switching to or from B0 */
889 	spin_lock_irqsave(&priv->lock, flags);
890 	control = priv->line_control;
891 	if (C_BAUD(tty) == B0)
892 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
893 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
894 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
895 	if (control != priv->line_control) {
896 		control = priv->line_control;
897 		spin_unlock_irqrestore(&priv->lock, flags);
898 		pl2303_set_control_lines(port, control);
899 	} else {
900 		spin_unlock_irqrestore(&priv->lock, flags);
901 	}
902 
903 	if (C_CRTSCTS(tty)) {
904 		if (spriv->quirks & PL2303_QUIRK_LEGACY) {
905 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
906 		} else if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
907 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
908 					PL2303_HXN_FLOWCTRL_MASK,
909 					PL2303_HXN_FLOWCTRL_RTS_CTS);
910 		} else {
911 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
912 		}
913 	} else if (pl2303_enable_xonxoff(tty, spriv->type)) {
914 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
915 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
916 					PL2303_HXN_FLOWCTRL_MASK,
917 					PL2303_HXN_FLOWCTRL_XON_XOFF);
918 		} else {
919 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
920 		}
921 	} else {
922 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
923 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
924 					PL2303_HXN_FLOWCTRL_MASK,
925 					PL2303_HXN_FLOWCTRL_NONE);
926 		} else {
927 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
928 		}
929 	}
930 
931 	kfree(buf);
932 }
933 
934 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
935 {
936 	struct pl2303_private *priv = usb_get_serial_port_data(port);
937 	unsigned long flags;
938 	u8 control;
939 
940 	spin_lock_irqsave(&priv->lock, flags);
941 	if (on)
942 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
943 	else
944 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
945 	control = priv->line_control;
946 	spin_unlock_irqrestore(&priv->lock, flags);
947 
948 	pl2303_set_control_lines(port, control);
949 }
950 
951 static void pl2303_close(struct usb_serial_port *port)
952 {
953 	usb_serial_generic_close(port);
954 	usb_kill_urb(port->interrupt_in_urb);
955 	pl2303_set_break(port, false);
956 }
957 
958 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
959 {
960 	struct usb_serial *serial = port->serial;
961 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
962 	int result;
963 
964 	if (spriv->quirks & PL2303_QUIRK_LEGACY) {
965 		usb_clear_halt(serial->dev, port->write_urb->pipe);
966 		usb_clear_halt(serial->dev, port->read_urb->pipe);
967 	} else {
968 		/* reset upstream data pipes */
969 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
970 			pl2303_vendor_write(serial, PL2303_HXN_RESET_REG,
971 					PL2303_HXN_RESET_UPSTREAM_PIPE |
972 					PL2303_HXN_RESET_DOWNSTREAM_PIPE);
973 		} else {
974 			pl2303_vendor_write(serial, 8, 0);
975 			pl2303_vendor_write(serial, 9, 0);
976 		}
977 	}
978 
979 	/* Setup termios */
980 	if (tty)
981 		pl2303_set_termios(tty, port, NULL);
982 
983 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
984 	if (result) {
985 		dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
986 			result);
987 		return result;
988 	}
989 
990 	result = usb_serial_generic_open(tty, port);
991 	if (result) {
992 		usb_kill_urb(port->interrupt_in_urb);
993 		return result;
994 	}
995 
996 	return 0;
997 }
998 
999 static int pl2303_tiocmset(struct tty_struct *tty,
1000 			   unsigned int set, unsigned int clear)
1001 {
1002 	struct usb_serial_port *port = tty->driver_data;
1003 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1004 	unsigned long flags;
1005 	u8 control;
1006 	int ret;
1007 
1008 	spin_lock_irqsave(&priv->lock, flags);
1009 	if (set & TIOCM_RTS)
1010 		priv->line_control |= CONTROL_RTS;
1011 	if (set & TIOCM_DTR)
1012 		priv->line_control |= CONTROL_DTR;
1013 	if (clear & TIOCM_RTS)
1014 		priv->line_control &= ~CONTROL_RTS;
1015 	if (clear & TIOCM_DTR)
1016 		priv->line_control &= ~CONTROL_DTR;
1017 	control = priv->line_control;
1018 	spin_unlock_irqrestore(&priv->lock, flags);
1019 
1020 	ret = pl2303_set_control_lines(port, control);
1021 	if (ret)
1022 		return usb_translate_errors(ret);
1023 
1024 	return 0;
1025 }
1026 
1027 static int pl2303_tiocmget(struct tty_struct *tty)
1028 {
1029 	struct usb_serial_port *port = tty->driver_data;
1030 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1031 	unsigned long flags;
1032 	unsigned int mcr;
1033 	unsigned int status;
1034 	unsigned int result;
1035 
1036 	spin_lock_irqsave(&priv->lock, flags);
1037 	mcr = priv->line_control;
1038 	status = priv->line_status;
1039 	spin_unlock_irqrestore(&priv->lock, flags);
1040 
1041 	result = ((mcr & CONTROL_DTR)		? TIOCM_DTR : 0)
1042 		  | ((mcr & CONTROL_RTS)	? TIOCM_RTS : 0)
1043 		  | ((status & UART_CTS)	? TIOCM_CTS : 0)
1044 		  | ((status & UART_DSR)	? TIOCM_DSR : 0)
1045 		  | ((status & UART_RING)	? TIOCM_RI  : 0)
1046 		  | ((status & UART_DCD)	? TIOCM_CD  : 0);
1047 
1048 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
1049 
1050 	return result;
1051 }
1052 
1053 static int pl2303_carrier_raised(struct usb_serial_port *port)
1054 {
1055 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1056 
1057 	if (priv->line_status & UART_DCD)
1058 		return 1;
1059 
1060 	return 0;
1061 }
1062 
1063 static int pl2303_set_break(struct usb_serial_port *port, bool enable)
1064 {
1065 	struct usb_serial *serial = port->serial;
1066 	u16 state;
1067 	int result;
1068 
1069 	if (enable)
1070 		state = BREAK_ON;
1071 	else
1072 		state = BREAK_OFF;
1073 
1074 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1075 			state == BREAK_OFF ? "off" : "on");
1076 
1077 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1078 				 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
1079 				 0, NULL, 0, 100);
1080 	if (result) {
1081 		dev_err(&port->dev, "error sending break = %d\n", result);
1082 		return result;
1083 	}
1084 
1085 	return 0;
1086 }
1087 
1088 static int pl2303_break_ctl(struct tty_struct *tty, int state)
1089 {
1090 	struct usb_serial_port *port = tty->driver_data;
1091 
1092 	return pl2303_set_break(port, state);
1093 }
1094 
1095 static void pl2303_update_line_status(struct usb_serial_port *port,
1096 				      unsigned char *data,
1097 				      unsigned int actual_length)
1098 {
1099 	struct usb_serial *serial = port->serial;
1100 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
1101 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1102 	struct tty_struct *tty;
1103 	unsigned long flags;
1104 	unsigned int status_idx = UART_STATE_INDEX;
1105 	u8 status;
1106 	u8 delta;
1107 
1108 	if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
1109 		status_idx = 0;
1110 
1111 	if (actual_length < status_idx + 1)
1112 		return;
1113 
1114 	status = data[status_idx];
1115 
1116 	/* Save off the uart status for others to look at */
1117 	spin_lock_irqsave(&priv->lock, flags);
1118 	delta = priv->line_status ^ status;
1119 	priv->line_status = status;
1120 	spin_unlock_irqrestore(&priv->lock, flags);
1121 
1122 	if (status & UART_BREAK_ERROR)
1123 		usb_serial_handle_break(port);
1124 
1125 	if (delta & UART_STATE_MSR_MASK) {
1126 		if (delta & UART_CTS)
1127 			port->icount.cts++;
1128 		if (delta & UART_DSR)
1129 			port->icount.dsr++;
1130 		if (delta & UART_RING)
1131 			port->icount.rng++;
1132 		if (delta & UART_DCD) {
1133 			port->icount.dcd++;
1134 			tty = tty_port_tty_get(&port->port);
1135 			if (tty) {
1136 				usb_serial_handle_dcd_change(port, tty,
1137 							status & UART_DCD);
1138 				tty_kref_put(tty);
1139 			}
1140 		}
1141 
1142 		wake_up_interruptible(&port->port.delta_msr_wait);
1143 	}
1144 }
1145 
1146 static void pl2303_read_int_callback(struct urb *urb)
1147 {
1148 	struct usb_serial_port *port =  urb->context;
1149 	unsigned char *data = urb->transfer_buffer;
1150 	unsigned int actual_length = urb->actual_length;
1151 	int status = urb->status;
1152 	int retval;
1153 
1154 	switch (status) {
1155 	case 0:
1156 		/* success */
1157 		break;
1158 	case -ECONNRESET:
1159 	case -ENOENT:
1160 	case -ESHUTDOWN:
1161 		/* this urb is terminated, clean up */
1162 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
1163 			__func__, status);
1164 		return;
1165 	default:
1166 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
1167 			__func__, status);
1168 		goto exit;
1169 	}
1170 
1171 	usb_serial_debug_data(&port->dev, __func__,
1172 			      urb->actual_length, urb->transfer_buffer);
1173 
1174 	pl2303_update_line_status(port, data, actual_length);
1175 
1176 exit:
1177 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1178 	if (retval) {
1179 		dev_err(&port->dev,
1180 			"%s - usb_submit_urb failed with result %d\n",
1181 			__func__, retval);
1182 	}
1183 }
1184 
1185 static void pl2303_process_read_urb(struct urb *urb)
1186 {
1187 	struct usb_serial_port *port = urb->context;
1188 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1189 	unsigned char *data = urb->transfer_buffer;
1190 	char tty_flag = TTY_NORMAL;
1191 	unsigned long flags;
1192 	u8 line_status;
1193 	int i;
1194 
1195 	/* update line status */
1196 	spin_lock_irqsave(&priv->lock, flags);
1197 	line_status = priv->line_status;
1198 	priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1199 	spin_unlock_irqrestore(&priv->lock, flags);
1200 
1201 	if (!urb->actual_length)
1202 		return;
1203 
1204 	/*
1205 	 * Break takes precedence over parity, which takes precedence over
1206 	 * framing errors.
1207 	 */
1208 	if (line_status & UART_BREAK_ERROR)
1209 		tty_flag = TTY_BREAK;
1210 	else if (line_status & UART_PARITY_ERROR)
1211 		tty_flag = TTY_PARITY;
1212 	else if (line_status & UART_FRAME_ERROR)
1213 		tty_flag = TTY_FRAME;
1214 
1215 	if (tty_flag != TTY_NORMAL)
1216 		dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
1217 								tty_flag);
1218 	/* overrun is special, not associated with a char */
1219 	if (line_status & UART_OVERRUN_ERROR)
1220 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1221 
1222 	if (port->sysrq) {
1223 		for (i = 0; i < urb->actual_length; ++i)
1224 			if (!usb_serial_handle_sysrq_char(port, data[i]))
1225 				tty_insert_flip_char(&port->port, data[i],
1226 						tty_flag);
1227 	} else {
1228 		tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
1229 							urb->actual_length);
1230 	}
1231 
1232 	tty_flip_buffer_push(&port->port);
1233 }
1234 
1235 static struct usb_serial_driver pl2303_device = {
1236 	.driver = {
1237 		.owner =	THIS_MODULE,
1238 		.name =		"pl2303",
1239 	},
1240 	.id_table =		id_table,
1241 	.num_bulk_in =		1,
1242 	.num_bulk_out =		1,
1243 	.num_interrupt_in =	0,	/* see pl2303_calc_num_ports */
1244 	.bulk_in_size =		256,
1245 	.bulk_out_size =	256,
1246 	.open =			pl2303_open,
1247 	.close =		pl2303_close,
1248 	.dtr_rts =		pl2303_dtr_rts,
1249 	.carrier_raised =	pl2303_carrier_raised,
1250 	.break_ctl =		pl2303_break_ctl,
1251 	.set_termios =		pl2303_set_termios,
1252 	.tiocmget =		pl2303_tiocmget,
1253 	.tiocmset =		pl2303_tiocmset,
1254 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1255 	.process_read_urb =	pl2303_process_read_urb,
1256 	.read_int_callback =	pl2303_read_int_callback,
1257 	.probe =		pl2303_probe,
1258 	.calc_num_ports =	pl2303_calc_num_ports,
1259 	.attach =		pl2303_startup,
1260 	.release =		pl2303_release,
1261 	.port_probe =		pl2303_port_probe,
1262 	.port_remove =		pl2303_port_remove,
1263 };
1264 
1265 static struct usb_serial_driver * const serial_drivers[] = {
1266 	&pl2303_device, NULL
1267 };
1268 
1269 module_usb_serial_driver(serial_drivers, id_table);
1270 
1271 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1272 MODULE_LICENSE("GPL v2");
1273