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