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