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