xref: /openbmc/linux/drivers/usb/serial/pl2303.c (revision 6bfb56e9)
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:
440 		case 0x105:
441 		case 0x305:
442 		case 0x405:
443 		case 0x605:
444 			/*
445 			 * Assume it's an HXN-type if the device doesn't
446 			 * support the old read request value.
447 			 */
448 			if (!pl2303_supports_hx_status(serial))
449 				return TYPE_HXN;
450 			break;
451 		case 0x300:
452 			return TYPE_TA;
453 		case 0x500:
454 			return TYPE_TB;
455 		}
456 		break;
457 	}
458 
459 	dev_err(&serial->interface->dev,
460 			"unknown device type, please report to linux-usb@vger.kernel.org\n");
461 	return -ENODEV;
462 }
463 
464 static int pl2303_startup(struct usb_serial *serial)
465 {
466 	struct pl2303_serial_private *spriv;
467 	enum pl2303_type type;
468 	unsigned char *buf;
469 	int ret;
470 
471 	ret = pl2303_detect_type(serial);
472 	if (ret < 0)
473 		return ret;
474 
475 	type = ret;
476 	dev_dbg(&serial->interface->dev, "device type: %s\n", pl2303_type_data[type].name);
477 
478 	spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
479 	if (!spriv)
480 		return -ENOMEM;
481 
482 	spriv->type = &pl2303_type_data[type];
483 	spriv->quirks = (unsigned long)usb_get_serial_data(serial);
484 	spriv->quirks |= spriv->type->quirks;
485 
486 	usb_set_serial_data(serial, spriv);
487 
488 	if (type != TYPE_HXN) {
489 		buf = kmalloc(1, GFP_KERNEL);
490 		if (!buf) {
491 			kfree(spriv);
492 			return -ENOMEM;
493 		}
494 
495 		pl2303_vendor_read(serial, 0x8484, buf);
496 		pl2303_vendor_write(serial, 0x0404, 0);
497 		pl2303_vendor_read(serial, 0x8484, buf);
498 		pl2303_vendor_read(serial, 0x8383, buf);
499 		pl2303_vendor_read(serial, 0x8484, buf);
500 		pl2303_vendor_write(serial, 0x0404, 1);
501 		pl2303_vendor_read(serial, 0x8484, buf);
502 		pl2303_vendor_read(serial, 0x8383, buf);
503 		pl2303_vendor_write(serial, 0, 1);
504 		pl2303_vendor_write(serial, 1, 0);
505 		if (spriv->quirks & PL2303_QUIRK_LEGACY)
506 			pl2303_vendor_write(serial, 2, 0x24);
507 		else
508 			pl2303_vendor_write(serial, 2, 0x44);
509 
510 		kfree(buf);
511 	}
512 
513 	return 0;
514 }
515 
516 static void pl2303_release(struct usb_serial *serial)
517 {
518 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
519 
520 	kfree(spriv);
521 }
522 
523 static int pl2303_port_probe(struct usb_serial_port *port)
524 {
525 	struct pl2303_private *priv;
526 
527 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
528 	if (!priv)
529 		return -ENOMEM;
530 
531 	spin_lock_init(&priv->lock);
532 
533 	usb_set_serial_port_data(port, priv);
534 
535 	port->port.drain_delay = 256;
536 
537 	return 0;
538 }
539 
540 static void pl2303_port_remove(struct usb_serial_port *port)
541 {
542 	struct pl2303_private *priv = usb_get_serial_port_data(port);
543 
544 	kfree(priv);
545 }
546 
547 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
548 {
549 	struct usb_device *dev = port->serial->dev;
550 	int retval;
551 
552 	dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
553 
554 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
555 				 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
556 				 value, 0, NULL, 0, 100);
557 	if (retval)
558 		dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
559 
560 	return retval;
561 }
562 
563 /*
564  * Returns the nearest supported baud rate that can be set directly without
565  * using divisors.
566  */
567 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
568 {
569 	static const speed_t baud_sup[] = {
570 		75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
571 		14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
572 		614400, 921600, 1228800, 2457600, 3000000, 6000000
573 	};
574 
575 	unsigned i;
576 
577 	for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
578 		if (baud_sup[i] > baud)
579 			break;
580 	}
581 
582 	if (i == ARRAY_SIZE(baud_sup))
583 		baud = baud_sup[i - 1];
584 	else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
585 		baud = baud_sup[i - 1];
586 	else
587 		baud = baud_sup[i];
588 
589 	return baud;
590 }
591 
592 /*
593  * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
594  *       use 9600 baud.
595  */
596 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
597 								speed_t baud)
598 {
599 	put_unaligned_le32(baud, buf);
600 
601 	return baud;
602 }
603 
604 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
605 								speed_t baud)
606 {
607 	unsigned int baseline, mantissa, exponent;
608 
609 	/*
610 	 * Apparently the formula is:
611 	 *   baudrate = 12M * 32 / (mantissa * 4^exponent)
612 	 * where
613 	 *   mantissa = buf[8:0]
614 	 *   exponent = buf[11:9]
615 	 */
616 	baseline = 12000000 * 32;
617 	mantissa = baseline / baud;
618 	if (mantissa == 0)
619 		mantissa = 1;	/* Avoid dividing by zero if baud > 32*12M. */
620 	exponent = 0;
621 	while (mantissa >= 512) {
622 		if (exponent < 7) {
623 			mantissa >>= 2;	/* divide by 4 */
624 			exponent++;
625 		} else {
626 			/* Exponent is maxed. Trim mantissa and leave. */
627 			mantissa = 511;
628 			break;
629 		}
630 	}
631 
632 	buf[3] = 0x80;
633 	buf[2] = 0;
634 	buf[1] = exponent << 1 | mantissa >> 8;
635 	buf[0] = mantissa & 0xff;
636 
637 	/* Calculate and return the exact baud rate. */
638 	baud = (baseline / mantissa) >> (exponent << 1);
639 
640 	return baud;
641 }
642 
643 static speed_t pl2303_encode_baud_rate_divisor_alt(unsigned char buf[4],
644 								speed_t baud)
645 {
646 	unsigned int baseline, mantissa, exponent;
647 
648 	/*
649 	 * Apparently, for the TA version the formula is:
650 	 *   baudrate = 12M * 32 / (mantissa * 2^exponent)
651 	 * where
652 	 *   mantissa = buf[10:0]
653 	 *   exponent = buf[15:13 16]
654 	 */
655 	baseline = 12000000 * 32;
656 	mantissa = baseline / baud;
657 	if (mantissa == 0)
658 		mantissa = 1;   /* Avoid dividing by zero if baud > 32*12M. */
659 	exponent = 0;
660 	while (mantissa >= 2048) {
661 		if (exponent < 15) {
662 			mantissa >>= 1; /* divide by 2 */
663 			exponent++;
664 		} else {
665 			/* Exponent is maxed. Trim mantissa and leave. */
666 			mantissa = 2047;
667 			break;
668 		}
669 	}
670 
671 	buf[3] = 0x80;
672 	buf[2] = exponent & 0x01;
673 	buf[1] = (exponent & ~0x01) << 4 | mantissa >> 8;
674 	buf[0] = mantissa & 0xff;
675 
676 	/* Calculate and return the exact baud rate. */
677 	baud = (baseline / mantissa) >> exponent;
678 
679 	return baud;
680 }
681 
682 static void pl2303_encode_baud_rate(struct tty_struct *tty,
683 					struct usb_serial_port *port,
684 					u8 buf[4])
685 {
686 	struct usb_serial *serial = port->serial;
687 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
688 	speed_t	baud_sup;
689 	speed_t baud;
690 
691 	baud = tty_get_baud_rate(tty);
692 	dev_dbg(&port->dev, "baud requested = %u\n", baud);
693 	if (!baud)
694 		return;
695 
696 	if (spriv->type->max_baud_rate)
697 		baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
698 	/*
699 	 * Use direct method for supported baud rates, otherwise use divisors.
700 	 * Newer chip types do not support divisor encoding.
701 	 */
702 	if (spriv->type->no_divisors)
703 		baud_sup = baud;
704 	else
705 		baud_sup = pl2303_get_supported_baud_rate(baud);
706 
707 	if (baud == baud_sup)
708 		baud = pl2303_encode_baud_rate_direct(buf, baud);
709 	else if (spriv->type->alt_divisors)
710 		baud = pl2303_encode_baud_rate_divisor_alt(buf, baud);
711 	else
712 		baud = pl2303_encode_baud_rate_divisor(buf, baud);
713 
714 	/* Save resulting baud rate */
715 	tty_encode_baud_rate(tty, baud, baud);
716 	dev_dbg(&port->dev, "baud set = %u\n", baud);
717 }
718 
719 static int pl2303_get_line_request(struct usb_serial_port *port,
720 							unsigned char buf[7])
721 {
722 	struct usb_device *udev = port->serial->dev;
723 	int ret;
724 
725 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
726 				GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
727 				0, 0, buf, 7, 100);
728 	if (ret != 7) {
729 		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
730 
731 		if (ret >= 0)
732 			ret = -EIO;
733 
734 		return ret;
735 	}
736 
737 	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
738 
739 	return 0;
740 }
741 
742 static int pl2303_set_line_request(struct usb_serial_port *port,
743 							unsigned char buf[7])
744 {
745 	struct usb_device *udev = port->serial->dev;
746 	int ret;
747 
748 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
749 				SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
750 				0, 0, buf, 7, 100);
751 	if (ret < 0) {
752 		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
753 		return ret;
754 	}
755 
756 	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
757 
758 	return 0;
759 }
760 
761 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b)
762 {
763 	bool ixon_change;
764 
765 	ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) ||
766 			a->c_cc[VSTART] != b->c_cc[VSTART] ||
767 			a->c_cc[VSTOP] != b->c_cc[VSTOP];
768 
769 	return tty_termios_hw_change(a, b) || ixon_change;
770 }
771 
772 static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type)
773 {
774 	if (!I_IXON(tty) || I_IXANY(tty))
775 		return false;
776 
777 	if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13)
778 		return false;
779 
780 	if (type->no_autoxonxoff)
781 		return false;
782 
783 	return true;
784 }
785 
786 static void pl2303_set_termios(struct tty_struct *tty,
787 		struct usb_serial_port *port, struct ktermios *old_termios)
788 {
789 	struct usb_serial *serial = port->serial;
790 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
791 	struct pl2303_private *priv = usb_get_serial_port_data(port);
792 	unsigned long flags;
793 	unsigned char *buf;
794 	int ret;
795 	u8 control;
796 
797 	if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
798 		return;
799 
800 	buf = kzalloc(7, GFP_KERNEL);
801 	if (!buf) {
802 		/* Report back no change occurred */
803 		if (old_termios)
804 			tty->termios = *old_termios;
805 		return;
806 	}
807 
808 	pl2303_get_line_request(port, buf);
809 
810 	buf[6] = tty_get_char_size(tty->termios.c_cflag);
811 	dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
812 
813 	/* For reference buf[0]:buf[3] baud rate value */
814 	pl2303_encode_baud_rate(tty, port, &buf[0]);
815 
816 	/* For reference buf[4]=0 is 1 stop bits */
817 	/* For reference buf[4]=1 is 1.5 stop bits */
818 	/* For reference buf[4]=2 is 2 stop bits */
819 	if (C_CSTOPB(tty)) {
820 		/*
821 		 * NOTE: Comply with "real" UARTs / RS232:
822 		 *       use 1.5 instead of 2 stop bits with 5 data bits
823 		 */
824 		if (C_CSIZE(tty) == CS5) {
825 			buf[4] = 1;
826 			dev_dbg(&port->dev, "stop bits = 1.5\n");
827 		} else {
828 			buf[4] = 2;
829 			dev_dbg(&port->dev, "stop bits = 2\n");
830 		}
831 	} else {
832 		buf[4] = 0;
833 		dev_dbg(&port->dev, "stop bits = 1\n");
834 	}
835 
836 	if (C_PARENB(tty)) {
837 		/* For reference buf[5]=0 is none parity */
838 		/* For reference buf[5]=1 is odd parity */
839 		/* For reference buf[5]=2 is even parity */
840 		/* For reference buf[5]=3 is mark parity */
841 		/* For reference buf[5]=4 is space parity */
842 		if (C_PARODD(tty)) {
843 			if (C_CMSPAR(tty)) {
844 				buf[5] = 3;
845 				dev_dbg(&port->dev, "parity = mark\n");
846 			} else {
847 				buf[5] = 1;
848 				dev_dbg(&port->dev, "parity = odd\n");
849 			}
850 		} else {
851 			if (C_CMSPAR(tty)) {
852 				buf[5] = 4;
853 				dev_dbg(&port->dev, "parity = space\n");
854 			} else {
855 				buf[5] = 2;
856 				dev_dbg(&port->dev, "parity = even\n");
857 			}
858 		}
859 	} else {
860 		buf[5] = 0;
861 		dev_dbg(&port->dev, "parity = none\n");
862 	}
863 
864 	/*
865 	 * Some PL2303 are known to lose bytes if you change serial settings
866 	 * even to the same values as before. Thus we actually need to filter
867 	 * in this specific case.
868 	 *
869 	 * Note that the tty_termios_hw_change check above is not sufficient
870 	 * as a previously requested baud rate may differ from the one
871 	 * actually used (and stored in old_termios).
872 	 *
873 	 * NOTE: No additional locking needed for line_settings as it is
874 	 *       only used in set_termios, which is serialised against itself.
875 	 */
876 	if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
877 		ret = pl2303_set_line_request(port, buf);
878 		if (!ret)
879 			memcpy(priv->line_settings, buf, 7);
880 	}
881 
882 	/* change control lines if we are switching to or from B0 */
883 	spin_lock_irqsave(&priv->lock, flags);
884 	control = priv->line_control;
885 	if (C_BAUD(tty) == B0)
886 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
887 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
888 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
889 	if (control != priv->line_control) {
890 		control = priv->line_control;
891 		spin_unlock_irqrestore(&priv->lock, flags);
892 		pl2303_set_control_lines(port, control);
893 	} else {
894 		spin_unlock_irqrestore(&priv->lock, flags);
895 	}
896 
897 	if (C_CRTSCTS(tty)) {
898 		if (spriv->quirks & PL2303_QUIRK_LEGACY) {
899 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
900 		} else if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
901 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
902 					PL2303_HXN_FLOWCTRL_MASK,
903 					PL2303_HXN_FLOWCTRL_RTS_CTS);
904 		} else {
905 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
906 		}
907 	} else if (pl2303_enable_xonxoff(tty, spriv->type)) {
908 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
909 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
910 					PL2303_HXN_FLOWCTRL_MASK,
911 					PL2303_HXN_FLOWCTRL_XON_XOFF);
912 		} else {
913 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
914 		}
915 	} else {
916 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
917 			pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
918 					PL2303_HXN_FLOWCTRL_MASK,
919 					PL2303_HXN_FLOWCTRL_NONE);
920 		} else {
921 			pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
922 		}
923 	}
924 
925 	kfree(buf);
926 }
927 
928 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
929 {
930 	struct pl2303_private *priv = usb_get_serial_port_data(port);
931 	unsigned long flags;
932 	u8 control;
933 
934 	spin_lock_irqsave(&priv->lock, flags);
935 	if (on)
936 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
937 	else
938 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
939 	control = priv->line_control;
940 	spin_unlock_irqrestore(&priv->lock, flags);
941 
942 	pl2303_set_control_lines(port, control);
943 }
944 
945 static void pl2303_close(struct usb_serial_port *port)
946 {
947 	usb_serial_generic_close(port);
948 	usb_kill_urb(port->interrupt_in_urb);
949 	pl2303_set_break(port, false);
950 }
951 
952 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
953 {
954 	struct usb_serial *serial = port->serial;
955 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
956 	int result;
957 
958 	if (spriv->quirks & PL2303_QUIRK_LEGACY) {
959 		usb_clear_halt(serial->dev, port->write_urb->pipe);
960 		usb_clear_halt(serial->dev, port->read_urb->pipe);
961 	} else {
962 		/* reset upstream data pipes */
963 		if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
964 			pl2303_vendor_write(serial, PL2303_HXN_RESET_REG,
965 					PL2303_HXN_RESET_UPSTREAM_PIPE |
966 					PL2303_HXN_RESET_DOWNSTREAM_PIPE);
967 		} else {
968 			pl2303_vendor_write(serial, 8, 0);
969 			pl2303_vendor_write(serial, 9, 0);
970 		}
971 	}
972 
973 	/* Setup termios */
974 	if (tty)
975 		pl2303_set_termios(tty, port, NULL);
976 
977 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
978 	if (result) {
979 		dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
980 			result);
981 		return result;
982 	}
983 
984 	result = usb_serial_generic_open(tty, port);
985 	if (result) {
986 		usb_kill_urb(port->interrupt_in_urb);
987 		return result;
988 	}
989 
990 	return 0;
991 }
992 
993 static int pl2303_tiocmset(struct tty_struct *tty,
994 			   unsigned int set, unsigned int clear)
995 {
996 	struct usb_serial_port *port = tty->driver_data;
997 	struct pl2303_private *priv = usb_get_serial_port_data(port);
998 	unsigned long flags;
999 	u8 control;
1000 	int ret;
1001 
1002 	spin_lock_irqsave(&priv->lock, flags);
1003 	if (set & TIOCM_RTS)
1004 		priv->line_control |= CONTROL_RTS;
1005 	if (set & TIOCM_DTR)
1006 		priv->line_control |= CONTROL_DTR;
1007 	if (clear & TIOCM_RTS)
1008 		priv->line_control &= ~CONTROL_RTS;
1009 	if (clear & TIOCM_DTR)
1010 		priv->line_control &= ~CONTROL_DTR;
1011 	control = priv->line_control;
1012 	spin_unlock_irqrestore(&priv->lock, flags);
1013 
1014 	ret = pl2303_set_control_lines(port, control);
1015 	if (ret)
1016 		return usb_translate_errors(ret);
1017 
1018 	return 0;
1019 }
1020 
1021 static int pl2303_tiocmget(struct tty_struct *tty)
1022 {
1023 	struct usb_serial_port *port = tty->driver_data;
1024 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1025 	unsigned long flags;
1026 	unsigned int mcr;
1027 	unsigned int status;
1028 	unsigned int result;
1029 
1030 	spin_lock_irqsave(&priv->lock, flags);
1031 	mcr = priv->line_control;
1032 	status = priv->line_status;
1033 	spin_unlock_irqrestore(&priv->lock, flags);
1034 
1035 	result = ((mcr & CONTROL_DTR)		? TIOCM_DTR : 0)
1036 		  | ((mcr & CONTROL_RTS)	? TIOCM_RTS : 0)
1037 		  | ((status & UART_CTS)	? TIOCM_CTS : 0)
1038 		  | ((status & UART_DSR)	? TIOCM_DSR : 0)
1039 		  | ((status & UART_RING)	? TIOCM_RI  : 0)
1040 		  | ((status & UART_DCD)	? TIOCM_CD  : 0);
1041 
1042 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
1043 
1044 	return result;
1045 }
1046 
1047 static int pl2303_carrier_raised(struct usb_serial_port *port)
1048 {
1049 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1050 
1051 	if (priv->line_status & UART_DCD)
1052 		return 1;
1053 
1054 	return 0;
1055 }
1056 
1057 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
1058 {
1059 	struct usb_serial *serial = port->serial;
1060 	u16 state;
1061 	int result;
1062 
1063 	if (enable)
1064 		state = BREAK_ON;
1065 	else
1066 		state = BREAK_OFF;
1067 
1068 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1069 			state == BREAK_OFF ? "off" : "on");
1070 
1071 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1072 				 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
1073 				 0, NULL, 0, 100);
1074 	if (result)
1075 		dev_err(&port->dev, "error sending break = %d\n", result);
1076 }
1077 
1078 static void pl2303_break_ctl(struct tty_struct *tty, int state)
1079 {
1080 	struct usb_serial_port *port = tty->driver_data;
1081 
1082 	pl2303_set_break(port, state);
1083 }
1084 
1085 static void pl2303_update_line_status(struct usb_serial_port *port,
1086 				      unsigned char *data,
1087 				      unsigned int actual_length)
1088 {
1089 	struct usb_serial *serial = port->serial;
1090 	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
1091 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1092 	struct tty_struct *tty;
1093 	unsigned long flags;
1094 	unsigned int status_idx = UART_STATE_INDEX;
1095 	u8 status;
1096 	u8 delta;
1097 
1098 	if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
1099 		status_idx = 0;
1100 
1101 	if (actual_length < status_idx + 1)
1102 		return;
1103 
1104 	status = data[status_idx];
1105 
1106 	/* Save off the uart status for others to look at */
1107 	spin_lock_irqsave(&priv->lock, flags);
1108 	delta = priv->line_status ^ status;
1109 	priv->line_status = status;
1110 	spin_unlock_irqrestore(&priv->lock, flags);
1111 
1112 	if (status & UART_BREAK_ERROR)
1113 		usb_serial_handle_break(port);
1114 
1115 	if (delta & UART_STATE_MSR_MASK) {
1116 		if (delta & UART_CTS)
1117 			port->icount.cts++;
1118 		if (delta & UART_DSR)
1119 			port->icount.dsr++;
1120 		if (delta & UART_RING)
1121 			port->icount.rng++;
1122 		if (delta & UART_DCD) {
1123 			port->icount.dcd++;
1124 			tty = tty_port_tty_get(&port->port);
1125 			if (tty) {
1126 				usb_serial_handle_dcd_change(port, tty,
1127 							status & UART_DCD);
1128 				tty_kref_put(tty);
1129 			}
1130 		}
1131 
1132 		wake_up_interruptible(&port->port.delta_msr_wait);
1133 	}
1134 }
1135 
1136 static void pl2303_read_int_callback(struct urb *urb)
1137 {
1138 	struct usb_serial_port *port =  urb->context;
1139 	unsigned char *data = urb->transfer_buffer;
1140 	unsigned int actual_length = urb->actual_length;
1141 	int status = urb->status;
1142 	int retval;
1143 
1144 	switch (status) {
1145 	case 0:
1146 		/* success */
1147 		break;
1148 	case -ECONNRESET:
1149 	case -ENOENT:
1150 	case -ESHUTDOWN:
1151 		/* this urb is terminated, clean up */
1152 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
1153 			__func__, status);
1154 		return;
1155 	default:
1156 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
1157 			__func__, status);
1158 		goto exit;
1159 	}
1160 
1161 	usb_serial_debug_data(&port->dev, __func__,
1162 			      urb->actual_length, urb->transfer_buffer);
1163 
1164 	pl2303_update_line_status(port, data, actual_length);
1165 
1166 exit:
1167 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1168 	if (retval) {
1169 		dev_err(&port->dev,
1170 			"%s - usb_submit_urb failed with result %d\n",
1171 			__func__, retval);
1172 	}
1173 }
1174 
1175 static void pl2303_process_read_urb(struct urb *urb)
1176 {
1177 	struct usb_serial_port *port = urb->context;
1178 	struct pl2303_private *priv = usb_get_serial_port_data(port);
1179 	unsigned char *data = urb->transfer_buffer;
1180 	char tty_flag = TTY_NORMAL;
1181 	unsigned long flags;
1182 	u8 line_status;
1183 	int i;
1184 
1185 	/* update line status */
1186 	spin_lock_irqsave(&priv->lock, flags);
1187 	line_status = priv->line_status;
1188 	priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1189 	spin_unlock_irqrestore(&priv->lock, flags);
1190 
1191 	if (!urb->actual_length)
1192 		return;
1193 
1194 	/*
1195 	 * Break takes precedence over parity, which takes precedence over
1196 	 * framing errors.
1197 	 */
1198 	if (line_status & UART_BREAK_ERROR)
1199 		tty_flag = TTY_BREAK;
1200 	else if (line_status & UART_PARITY_ERROR)
1201 		tty_flag = TTY_PARITY;
1202 	else if (line_status & UART_FRAME_ERROR)
1203 		tty_flag = TTY_FRAME;
1204 
1205 	if (tty_flag != TTY_NORMAL)
1206 		dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
1207 								tty_flag);
1208 	/* overrun is special, not associated with a char */
1209 	if (line_status & UART_OVERRUN_ERROR)
1210 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1211 
1212 	if (port->sysrq) {
1213 		for (i = 0; i < urb->actual_length; ++i)
1214 			if (!usb_serial_handle_sysrq_char(port, data[i]))
1215 				tty_insert_flip_char(&port->port, data[i],
1216 						tty_flag);
1217 	} else {
1218 		tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
1219 							urb->actual_length);
1220 	}
1221 
1222 	tty_flip_buffer_push(&port->port);
1223 }
1224 
1225 static struct usb_serial_driver pl2303_device = {
1226 	.driver = {
1227 		.owner =	THIS_MODULE,
1228 		.name =		"pl2303",
1229 	},
1230 	.id_table =		id_table,
1231 	.num_bulk_in =		1,
1232 	.num_bulk_out =		1,
1233 	.num_interrupt_in =	0,	/* see pl2303_calc_num_ports */
1234 	.bulk_in_size =		256,
1235 	.bulk_out_size =	256,
1236 	.open =			pl2303_open,
1237 	.close =		pl2303_close,
1238 	.dtr_rts =		pl2303_dtr_rts,
1239 	.carrier_raised =	pl2303_carrier_raised,
1240 	.break_ctl =		pl2303_break_ctl,
1241 	.set_termios =		pl2303_set_termios,
1242 	.tiocmget =		pl2303_tiocmget,
1243 	.tiocmset =		pl2303_tiocmset,
1244 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1245 	.process_read_urb =	pl2303_process_read_urb,
1246 	.read_int_callback =	pl2303_read_int_callback,
1247 	.probe =		pl2303_probe,
1248 	.calc_num_ports =	pl2303_calc_num_ports,
1249 	.attach =		pl2303_startup,
1250 	.release =		pl2303_release,
1251 	.port_probe =		pl2303_port_probe,
1252 	.port_remove =		pl2303_port_remove,
1253 };
1254 
1255 static struct usb_serial_driver * const serial_drivers[] = {
1256 	&pl2303_device, NULL
1257 };
1258 
1259 module_usb_serial_driver(serial_drivers, id_table);
1260 
1261 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1262 MODULE_LICENSE("GPL v2");
1263