treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / serial / pl2303.c
blobaab737e1e7b65aab669f768adab0681962c1b7d8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Prolific PL2303 USB to serial adaptor driver
5 * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2003 IBM Corp.
8 * Original driver for 2.2.x by anonymous
10 * See Documentation/usb/usb-serial.rst for more information on using this
11 * driver
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"
31 #define PL2303_QUIRK_UART_STATE_IDX0 BIT(0)
32 #define PL2303_QUIRK_LEGACY BIT(1)
33 #define PL2303_QUIRK_ENDPOINT_HACK BIT(2)
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_LD960_PRODUCT_ID) },
103 { USB_DEVICE(HP_VENDOR_ID, HP_LD960TA_PRODUCT_ID) },
104 { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
105 { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
106 { USB_DEVICE(HP_VENDOR_ID, HP_LM920_PRODUCT_ID) },
107 { USB_DEVICE(HP_VENDOR_ID, HP_LM940_PRODUCT_ID) },
108 { USB_DEVICE(HP_VENDOR_ID, HP_TD620_PRODUCT_ID) },
109 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
110 { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
111 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
112 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
113 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
114 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
115 { USB_DEVICE(AT_VENDOR_ID, AT_VTKIT3_PRODUCT_ID) },
116 { } /* Terminating entry */
119 MODULE_DEVICE_TABLE(usb, id_table);
121 #define SET_LINE_REQUEST_TYPE 0x21
122 #define SET_LINE_REQUEST 0x20
124 #define SET_CONTROL_REQUEST_TYPE 0x21
125 #define SET_CONTROL_REQUEST 0x22
126 #define CONTROL_DTR 0x01
127 #define CONTROL_RTS 0x02
129 #define BREAK_REQUEST_TYPE 0x21
130 #define BREAK_REQUEST 0x23
131 #define BREAK_ON 0xffff
132 #define BREAK_OFF 0x0000
134 #define GET_LINE_REQUEST_TYPE 0xa1
135 #define GET_LINE_REQUEST 0x21
137 #define VENDOR_WRITE_REQUEST_TYPE 0x40
138 #define VENDOR_WRITE_REQUEST 0x01
139 #define VENDOR_WRITE_NREQUEST 0x80
141 #define VENDOR_READ_REQUEST_TYPE 0xc0
142 #define VENDOR_READ_REQUEST 0x01
143 #define VENDOR_READ_NREQUEST 0x81
145 #define UART_STATE_INDEX 8
146 #define UART_STATE_MSR_MASK 0x8b
147 #define UART_STATE_TRANSIENT_MASK 0x74
148 #define UART_DCD 0x01
149 #define UART_DSR 0x02
150 #define UART_BREAK_ERROR 0x04
151 #define UART_RING 0x08
152 #define UART_FRAME_ERROR 0x10
153 #define UART_PARITY_ERROR 0x20
154 #define UART_OVERRUN_ERROR 0x40
155 #define UART_CTS 0x80
157 #define PL2303_FLOWCTRL_MASK 0xf0
159 #define PL2303_READ_TYPE_HX_STATUS 0x8080
161 #define PL2303_HXN_RESET_REG 0x07
162 #define PL2303_HXN_RESET_UPSTREAM_PIPE 0x02
163 #define PL2303_HXN_RESET_DOWNSTREAM_PIPE 0x01
165 #define PL2303_HXN_FLOWCTRL_REG 0x0a
166 #define PL2303_HXN_FLOWCTRL_MASK 0x1c
167 #define PL2303_HXN_FLOWCTRL_NONE 0x1c
168 #define PL2303_HXN_FLOWCTRL_RTS_CTS 0x18
169 #define PL2303_HXN_FLOWCTRL_XON_XOFF 0x0c
171 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
173 enum pl2303_type {
174 TYPE_01, /* Type 0 and 1 (difference unknown) */
175 TYPE_HX, /* HX version of the pl2303 chip */
176 TYPE_HXN, /* HXN version of the pl2303 chip */
177 TYPE_COUNT
180 struct pl2303_type_data {
181 speed_t max_baud_rate;
182 unsigned long quirks;
183 unsigned int no_autoxonxoff:1;
186 struct pl2303_serial_private {
187 const struct pl2303_type_data *type;
188 unsigned long quirks;
191 struct pl2303_private {
192 spinlock_t lock;
193 u8 line_control;
194 u8 line_status;
196 u8 line_settings[7];
199 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
200 [TYPE_01] = {
201 .max_baud_rate = 1228800,
202 .quirks = PL2303_QUIRK_LEGACY,
203 .no_autoxonxoff = true,
205 [TYPE_HX] = {
206 .max_baud_rate = 12000000,
208 [TYPE_HXN] = {
209 .max_baud_rate = 12000000,
213 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
214 unsigned char buf[1])
216 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
217 struct device *dev = &serial->interface->dev;
218 u8 request;
219 int res;
221 if (spriv->type == &pl2303_type_data[TYPE_HXN])
222 request = VENDOR_READ_NREQUEST;
223 else
224 request = VENDOR_READ_REQUEST;
226 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
227 request, VENDOR_READ_REQUEST_TYPE,
228 value, 0, buf, 1, 100);
229 if (res != 1) {
230 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
231 value, res);
232 if (res >= 0)
233 res = -EIO;
235 return res;
238 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
240 return 0;
243 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
245 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
246 struct device *dev = &serial->interface->dev;
247 u8 request;
248 int res;
250 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
252 if (spriv->type == &pl2303_type_data[TYPE_HXN])
253 request = VENDOR_WRITE_NREQUEST;
254 else
255 request = VENDOR_WRITE_REQUEST;
257 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
258 request, VENDOR_WRITE_REQUEST_TYPE,
259 value, index, NULL, 0, 100);
260 if (res) {
261 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
262 value, res);
263 return res;
266 return 0;
269 static int pl2303_update_reg(struct usb_serial *serial, u8 reg, u8 mask, u8 val)
271 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
272 int ret = 0;
273 u8 *buf;
275 buf = kmalloc(1, GFP_KERNEL);
276 if (!buf)
277 return -ENOMEM;
279 if (spriv->type == &pl2303_type_data[TYPE_HXN])
280 ret = pl2303_vendor_read(serial, reg, buf);
281 else
282 ret = pl2303_vendor_read(serial, reg | 0x80, buf);
284 if (ret)
285 goto out_free;
287 *buf &= ~mask;
288 *buf |= val & mask;
290 ret = pl2303_vendor_write(serial, reg, *buf);
291 out_free:
292 kfree(buf);
294 return ret;
297 static int pl2303_probe(struct usb_serial *serial,
298 const struct usb_device_id *id)
300 usb_set_serial_data(serial, (void *)id->driver_info);
302 return 0;
306 * Use interrupt endpoint from first interface if available.
308 * This is needed due to the looney way its endpoints are set up.
310 static int pl2303_endpoint_hack(struct usb_serial *serial,
311 struct usb_serial_endpoints *epds)
313 struct usb_interface *interface = serial->interface;
314 struct usb_device *dev = serial->dev;
315 struct device *ddev = &interface->dev;
316 struct usb_host_interface *iface_desc;
317 struct usb_endpoint_descriptor *endpoint;
318 unsigned int i;
320 if (interface == dev->actconfig->interface[0])
321 return 0;
323 /* check out the endpoints of the other interface */
324 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
326 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
327 endpoint = &iface_desc->endpoint[i].desc;
329 if (!usb_endpoint_is_int_in(endpoint))
330 continue;
332 dev_dbg(ddev, "found interrupt in on separate interface\n");
333 if (epds->num_interrupt_in < ARRAY_SIZE(epds->interrupt_in))
334 epds->interrupt_in[epds->num_interrupt_in++] = endpoint;
337 return 0;
340 static int pl2303_calc_num_ports(struct usb_serial *serial,
341 struct usb_serial_endpoints *epds)
343 unsigned long quirks = (unsigned long)usb_get_serial_data(serial);
344 struct device *dev = &serial->interface->dev;
345 int ret;
347 if (quirks & PL2303_QUIRK_ENDPOINT_HACK) {
348 ret = pl2303_endpoint_hack(serial, epds);
349 if (ret)
350 return ret;
353 if (epds->num_interrupt_in < 1) {
354 dev_err(dev, "required interrupt-in endpoint missing\n");
355 return -ENODEV;
358 return 1;
361 static int pl2303_startup(struct usb_serial *serial)
363 struct pl2303_serial_private *spriv;
364 enum pl2303_type type = TYPE_01;
365 unsigned char *buf;
366 int res;
368 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
369 if (!spriv)
370 return -ENOMEM;
372 buf = kmalloc(1, GFP_KERNEL);
373 if (!buf) {
374 kfree(spriv);
375 return -ENOMEM;
378 if (serial->dev->descriptor.bDeviceClass == 0x02)
379 type = TYPE_01; /* type 0 */
380 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
381 type = TYPE_HX;
382 else if (serial->dev->descriptor.bDeviceClass == 0x00)
383 type = TYPE_01; /* type 1 */
384 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
385 type = TYPE_01; /* type 1 */
386 dev_dbg(&serial->interface->dev, "device type: %d\n", type);
388 if (type == TYPE_HX) {
389 res = usb_control_msg(serial->dev,
390 usb_rcvctrlpipe(serial->dev, 0),
391 VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
392 PL2303_READ_TYPE_HX_STATUS, 0, buf, 1, 100);
393 if (res != 1)
394 type = TYPE_HXN;
397 spriv->type = &pl2303_type_data[type];
398 spriv->quirks = (unsigned long)usb_get_serial_data(serial);
399 spriv->quirks |= spriv->type->quirks;
401 usb_set_serial_data(serial, spriv);
403 if (type != TYPE_HXN) {
404 pl2303_vendor_read(serial, 0x8484, buf);
405 pl2303_vendor_write(serial, 0x0404, 0);
406 pl2303_vendor_read(serial, 0x8484, buf);
407 pl2303_vendor_read(serial, 0x8383, buf);
408 pl2303_vendor_read(serial, 0x8484, buf);
409 pl2303_vendor_write(serial, 0x0404, 1);
410 pl2303_vendor_read(serial, 0x8484, buf);
411 pl2303_vendor_read(serial, 0x8383, buf);
412 pl2303_vendor_write(serial, 0, 1);
413 pl2303_vendor_write(serial, 1, 0);
414 if (spriv->quirks & PL2303_QUIRK_LEGACY)
415 pl2303_vendor_write(serial, 2, 0x24);
416 else
417 pl2303_vendor_write(serial, 2, 0x44);
420 kfree(buf);
422 return 0;
425 static void pl2303_release(struct usb_serial *serial)
427 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
429 kfree(spriv);
432 static int pl2303_port_probe(struct usb_serial_port *port)
434 struct pl2303_private *priv;
436 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
437 if (!priv)
438 return -ENOMEM;
440 spin_lock_init(&priv->lock);
442 usb_set_serial_port_data(port, priv);
444 port->port.drain_delay = 256;
446 return 0;
449 static int pl2303_port_remove(struct usb_serial_port *port)
451 struct pl2303_private *priv = usb_get_serial_port_data(port);
453 kfree(priv);
455 return 0;
458 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
460 struct usb_device *dev = port->serial->dev;
461 int retval;
463 dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
465 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
466 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
467 value, 0, NULL, 0, 100);
468 if (retval)
469 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
471 return retval;
475 * Returns the nearest supported baud rate that can be set directly without
476 * using divisors.
478 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
480 static const speed_t baud_sup[] = {
481 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
482 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
483 614400, 921600, 1228800, 2457600, 3000000, 6000000
486 unsigned i;
488 for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
489 if (baud_sup[i] > baud)
490 break;
493 if (i == ARRAY_SIZE(baud_sup))
494 baud = baud_sup[i - 1];
495 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
496 baud = baud_sup[i - 1];
497 else
498 baud = baud_sup[i];
500 return baud;
504 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
505 * use 9600 baud.
507 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
508 speed_t baud)
510 put_unaligned_le32(baud, buf);
512 return baud;
515 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
516 speed_t baud)
518 unsigned int baseline, mantissa, exponent;
521 * Apparently the formula is:
522 * baudrate = 12M * 32 / (mantissa * 4^exponent)
523 * where
524 * mantissa = buf[8:0]
525 * exponent = buf[11:9]
527 baseline = 12000000 * 32;
528 mantissa = baseline / baud;
529 if (mantissa == 0)
530 mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
531 exponent = 0;
532 while (mantissa >= 512) {
533 if (exponent < 7) {
534 mantissa >>= 2; /* divide by 4 */
535 exponent++;
536 } else {
537 /* Exponent is maxed. Trim mantissa and leave. */
538 mantissa = 511;
539 break;
543 buf[3] = 0x80;
544 buf[2] = 0;
545 buf[1] = exponent << 1 | mantissa >> 8;
546 buf[0] = mantissa & 0xff;
548 /* Calculate and return the exact baud rate. */
549 baud = (baseline / mantissa) >> (exponent << 1);
551 return baud;
554 static void pl2303_encode_baud_rate(struct tty_struct *tty,
555 struct usb_serial_port *port,
556 u8 buf[4])
558 struct usb_serial *serial = port->serial;
559 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
560 speed_t baud_sup;
561 speed_t baud;
563 baud = tty_get_baud_rate(tty);
564 dev_dbg(&port->dev, "baud requested = %u\n", baud);
565 if (!baud)
566 return;
568 if (spriv->type->max_baud_rate)
569 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
571 * Use direct method for supported baud rates, otherwise use divisors.
573 baud_sup = pl2303_get_supported_baud_rate(baud);
575 if (baud == baud_sup)
576 baud = pl2303_encode_baud_rate_direct(buf, baud);
577 else
578 baud = pl2303_encode_baud_rate_divisor(buf, baud);
580 /* Save resulting baud rate */
581 tty_encode_baud_rate(tty, baud, baud);
582 dev_dbg(&port->dev, "baud set = %u\n", baud);
585 static int pl2303_get_line_request(struct usb_serial_port *port,
586 unsigned char buf[7])
588 struct usb_device *udev = port->serial->dev;
589 int ret;
591 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
592 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
593 0, 0, buf, 7, 100);
594 if (ret != 7) {
595 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
597 if (ret >= 0)
598 ret = -EIO;
600 return ret;
603 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
605 return 0;
608 static int pl2303_set_line_request(struct usb_serial_port *port,
609 unsigned char buf[7])
611 struct usb_device *udev = port->serial->dev;
612 int ret;
614 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
615 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
616 0, 0, buf, 7, 100);
617 if (ret < 0) {
618 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
619 return ret;
622 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
624 return 0;
627 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b)
629 bool ixon_change;
631 ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) ||
632 a->c_cc[VSTART] != b->c_cc[VSTART] ||
633 a->c_cc[VSTOP] != b->c_cc[VSTOP];
635 return tty_termios_hw_change(a, b) || ixon_change;
638 static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type)
640 if (!I_IXON(tty) || I_IXANY(tty))
641 return false;
643 if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13)
644 return false;
646 if (type->no_autoxonxoff)
647 return false;
649 return true;
652 static void pl2303_set_termios(struct tty_struct *tty,
653 struct usb_serial_port *port, struct ktermios *old_termios)
655 struct usb_serial *serial = port->serial;
656 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
657 struct pl2303_private *priv = usb_get_serial_port_data(port);
658 unsigned long flags;
659 unsigned char *buf;
660 int ret;
661 u8 control;
663 if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
664 return;
666 buf = kzalloc(7, GFP_KERNEL);
667 if (!buf) {
668 /* Report back no change occurred */
669 if (old_termios)
670 tty->termios = *old_termios;
671 return;
674 pl2303_get_line_request(port, buf);
676 switch (C_CSIZE(tty)) {
677 case CS5:
678 buf[6] = 5;
679 break;
680 case CS6:
681 buf[6] = 6;
682 break;
683 case CS7:
684 buf[6] = 7;
685 break;
686 default:
687 case CS8:
688 buf[6] = 8;
690 dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
692 /* For reference buf[0]:buf[3] baud rate value */
693 pl2303_encode_baud_rate(tty, port, &buf[0]);
695 /* For reference buf[4]=0 is 1 stop bits */
696 /* For reference buf[4]=1 is 1.5 stop bits */
697 /* For reference buf[4]=2 is 2 stop bits */
698 if (C_CSTOPB(tty)) {
700 * NOTE: Comply with "real" UARTs / RS232:
701 * use 1.5 instead of 2 stop bits with 5 data bits
703 if (C_CSIZE(tty) == CS5) {
704 buf[4] = 1;
705 dev_dbg(&port->dev, "stop bits = 1.5\n");
706 } else {
707 buf[4] = 2;
708 dev_dbg(&port->dev, "stop bits = 2\n");
710 } else {
711 buf[4] = 0;
712 dev_dbg(&port->dev, "stop bits = 1\n");
715 if (C_PARENB(tty)) {
716 /* For reference buf[5]=0 is none parity */
717 /* For reference buf[5]=1 is odd parity */
718 /* For reference buf[5]=2 is even parity */
719 /* For reference buf[5]=3 is mark parity */
720 /* For reference buf[5]=4 is space parity */
721 if (C_PARODD(tty)) {
722 if (C_CMSPAR(tty)) {
723 buf[5] = 3;
724 dev_dbg(&port->dev, "parity = mark\n");
725 } else {
726 buf[5] = 1;
727 dev_dbg(&port->dev, "parity = odd\n");
729 } else {
730 if (C_CMSPAR(tty)) {
731 buf[5] = 4;
732 dev_dbg(&port->dev, "parity = space\n");
733 } else {
734 buf[5] = 2;
735 dev_dbg(&port->dev, "parity = even\n");
738 } else {
739 buf[5] = 0;
740 dev_dbg(&port->dev, "parity = none\n");
744 * Some PL2303 are known to lose bytes if you change serial settings
745 * even to the same values as before. Thus we actually need to filter
746 * in this specific case.
748 * Note that the tty_termios_hw_change check above is not sufficient
749 * as a previously requested baud rate may differ from the one
750 * actually used (and stored in old_termios).
752 * NOTE: No additional locking needed for line_settings as it is
753 * only used in set_termios, which is serialised against itself.
755 if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
756 ret = pl2303_set_line_request(port, buf);
757 if (!ret)
758 memcpy(priv->line_settings, buf, 7);
761 /* change control lines if we are switching to or from B0 */
762 spin_lock_irqsave(&priv->lock, flags);
763 control = priv->line_control;
764 if (C_BAUD(tty) == B0)
765 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
766 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
767 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
768 if (control != priv->line_control) {
769 control = priv->line_control;
770 spin_unlock_irqrestore(&priv->lock, flags);
771 pl2303_set_control_lines(port, control);
772 } else {
773 spin_unlock_irqrestore(&priv->lock, flags);
776 if (C_CRTSCTS(tty)) {
777 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
778 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
779 } else if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
780 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
781 PL2303_HXN_FLOWCTRL_MASK,
782 PL2303_HXN_FLOWCTRL_RTS_CTS);
783 } else {
784 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
786 } else if (pl2303_enable_xonxoff(tty, spriv->type)) {
787 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
788 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
789 PL2303_HXN_FLOWCTRL_MASK,
790 PL2303_HXN_FLOWCTRL_XON_XOFF);
791 } else {
792 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
794 } else {
795 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
796 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
797 PL2303_HXN_FLOWCTRL_MASK,
798 PL2303_HXN_FLOWCTRL_NONE);
799 } else {
800 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
804 kfree(buf);
807 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
809 struct pl2303_private *priv = usb_get_serial_port_data(port);
810 unsigned long flags;
811 u8 control;
813 spin_lock_irqsave(&priv->lock, flags);
814 if (on)
815 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
816 else
817 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
818 control = priv->line_control;
819 spin_unlock_irqrestore(&priv->lock, flags);
821 pl2303_set_control_lines(port, control);
824 static void pl2303_close(struct usb_serial_port *port)
826 usb_serial_generic_close(port);
827 usb_kill_urb(port->interrupt_in_urb);
828 pl2303_set_break(port, false);
831 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
833 struct usb_serial *serial = port->serial;
834 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
835 int result;
837 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
838 usb_clear_halt(serial->dev, port->write_urb->pipe);
839 usb_clear_halt(serial->dev, port->read_urb->pipe);
840 } else {
841 /* reset upstream data pipes */
842 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
843 pl2303_vendor_write(serial, PL2303_HXN_RESET_REG,
844 PL2303_HXN_RESET_UPSTREAM_PIPE |
845 PL2303_HXN_RESET_DOWNSTREAM_PIPE);
846 } else {
847 pl2303_vendor_write(serial, 8, 0);
848 pl2303_vendor_write(serial, 9, 0);
852 /* Setup termios */
853 if (tty)
854 pl2303_set_termios(tty, port, NULL);
856 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
857 if (result) {
858 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
859 result);
860 return result;
863 result = usb_serial_generic_open(tty, port);
864 if (result) {
865 usb_kill_urb(port->interrupt_in_urb);
866 return result;
869 return 0;
872 static int pl2303_tiocmset(struct tty_struct *tty,
873 unsigned int set, unsigned int clear)
875 struct usb_serial_port *port = tty->driver_data;
876 struct pl2303_private *priv = usb_get_serial_port_data(port);
877 unsigned long flags;
878 u8 control;
879 int ret;
881 spin_lock_irqsave(&priv->lock, flags);
882 if (set & TIOCM_RTS)
883 priv->line_control |= CONTROL_RTS;
884 if (set & TIOCM_DTR)
885 priv->line_control |= CONTROL_DTR;
886 if (clear & TIOCM_RTS)
887 priv->line_control &= ~CONTROL_RTS;
888 if (clear & TIOCM_DTR)
889 priv->line_control &= ~CONTROL_DTR;
890 control = priv->line_control;
891 spin_unlock_irqrestore(&priv->lock, flags);
893 ret = pl2303_set_control_lines(port, control);
894 if (ret)
895 return usb_translate_errors(ret);
897 return 0;
900 static int pl2303_tiocmget(struct tty_struct *tty)
902 struct usb_serial_port *port = tty->driver_data;
903 struct pl2303_private *priv = usb_get_serial_port_data(port);
904 unsigned long flags;
905 unsigned int mcr;
906 unsigned int status;
907 unsigned int result;
909 spin_lock_irqsave(&priv->lock, flags);
910 mcr = priv->line_control;
911 status = priv->line_status;
912 spin_unlock_irqrestore(&priv->lock, flags);
914 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
915 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
916 | ((status & UART_CTS) ? TIOCM_CTS : 0)
917 | ((status & UART_DSR) ? TIOCM_DSR : 0)
918 | ((status & UART_RING) ? TIOCM_RI : 0)
919 | ((status & UART_DCD) ? TIOCM_CD : 0);
921 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
923 return result;
926 static int pl2303_carrier_raised(struct usb_serial_port *port)
928 struct pl2303_private *priv = usb_get_serial_port_data(port);
930 if (priv->line_status & UART_DCD)
931 return 1;
933 return 0;
936 static int pl2303_get_serial(struct tty_struct *tty,
937 struct serial_struct *ss)
939 struct usb_serial_port *port = tty->driver_data;
941 ss->type = PORT_16654;
942 ss->line = port->minor;
943 ss->port = port->port_number;
944 ss->baud_base = 460800;
945 return 0;
948 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
950 struct usb_serial *serial = port->serial;
951 u16 state;
952 int result;
954 if (enable)
955 state = BREAK_ON;
956 else
957 state = BREAK_OFF;
959 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
960 state == BREAK_OFF ? "off" : "on");
962 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
963 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
964 0, NULL, 0, 100);
965 if (result)
966 dev_err(&port->dev, "error sending break = %d\n", result);
969 static void pl2303_break_ctl(struct tty_struct *tty, int state)
971 struct usb_serial_port *port = tty->driver_data;
973 pl2303_set_break(port, state);
976 static void pl2303_update_line_status(struct usb_serial_port *port,
977 unsigned char *data,
978 unsigned int actual_length)
980 struct usb_serial *serial = port->serial;
981 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
982 struct pl2303_private *priv = usb_get_serial_port_data(port);
983 struct tty_struct *tty;
984 unsigned long flags;
985 unsigned int status_idx = UART_STATE_INDEX;
986 u8 status;
987 u8 delta;
989 if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
990 status_idx = 0;
992 if (actual_length < status_idx + 1)
993 return;
995 status = data[status_idx];
997 /* Save off the uart status for others to look at */
998 spin_lock_irqsave(&priv->lock, flags);
999 delta = priv->line_status ^ status;
1000 priv->line_status = status;
1001 spin_unlock_irqrestore(&priv->lock, flags);
1003 if (status & UART_BREAK_ERROR)
1004 usb_serial_handle_break(port);
1006 if (delta & UART_STATE_MSR_MASK) {
1007 if (delta & UART_CTS)
1008 port->icount.cts++;
1009 if (delta & UART_DSR)
1010 port->icount.dsr++;
1011 if (delta & UART_RING)
1012 port->icount.rng++;
1013 if (delta & UART_DCD) {
1014 port->icount.dcd++;
1015 tty = tty_port_tty_get(&port->port);
1016 if (tty) {
1017 usb_serial_handle_dcd_change(port, tty,
1018 status & UART_DCD);
1019 tty_kref_put(tty);
1023 wake_up_interruptible(&port->port.delta_msr_wait);
1027 static void pl2303_read_int_callback(struct urb *urb)
1029 struct usb_serial_port *port = urb->context;
1030 unsigned char *data = urb->transfer_buffer;
1031 unsigned int actual_length = urb->actual_length;
1032 int status = urb->status;
1033 int retval;
1035 switch (status) {
1036 case 0:
1037 /* success */
1038 break;
1039 case -ECONNRESET:
1040 case -ENOENT:
1041 case -ESHUTDOWN:
1042 /* this urb is terminated, clean up */
1043 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
1044 __func__, status);
1045 return;
1046 default:
1047 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
1048 __func__, status);
1049 goto exit;
1052 usb_serial_debug_data(&port->dev, __func__,
1053 urb->actual_length, urb->transfer_buffer);
1055 pl2303_update_line_status(port, data, actual_length);
1057 exit:
1058 retval = usb_submit_urb(urb, GFP_ATOMIC);
1059 if (retval) {
1060 dev_err(&port->dev,
1061 "%s - usb_submit_urb failed with result %d\n",
1062 __func__, retval);
1066 static void pl2303_process_read_urb(struct urb *urb)
1068 struct usb_serial_port *port = urb->context;
1069 struct pl2303_private *priv = usb_get_serial_port_data(port);
1070 unsigned char *data = urb->transfer_buffer;
1071 char tty_flag = TTY_NORMAL;
1072 unsigned long flags;
1073 u8 line_status;
1074 int i;
1076 /* update line status */
1077 spin_lock_irqsave(&priv->lock, flags);
1078 line_status = priv->line_status;
1079 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1080 spin_unlock_irqrestore(&priv->lock, flags);
1082 if (!urb->actual_length)
1083 return;
1086 * Break takes precedence over parity, which takes precedence over
1087 * framing errors.
1089 if (line_status & UART_BREAK_ERROR)
1090 tty_flag = TTY_BREAK;
1091 else if (line_status & UART_PARITY_ERROR)
1092 tty_flag = TTY_PARITY;
1093 else if (line_status & UART_FRAME_ERROR)
1094 tty_flag = TTY_FRAME;
1096 if (tty_flag != TTY_NORMAL)
1097 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
1098 tty_flag);
1099 /* overrun is special, not associated with a char */
1100 if (line_status & UART_OVERRUN_ERROR)
1101 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1103 if (port->port.console && port->sysrq) {
1104 for (i = 0; i < urb->actual_length; ++i)
1105 if (!usb_serial_handle_sysrq_char(port, data[i]))
1106 tty_insert_flip_char(&port->port, data[i],
1107 tty_flag);
1108 } else {
1109 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
1110 urb->actual_length);
1113 tty_flip_buffer_push(&port->port);
1116 static struct usb_serial_driver pl2303_device = {
1117 .driver = {
1118 .owner = THIS_MODULE,
1119 .name = "pl2303",
1121 .id_table = id_table,
1122 .num_bulk_in = 1,
1123 .num_bulk_out = 1,
1124 .num_interrupt_in = 0, /* see pl2303_calc_num_ports */
1125 .bulk_in_size = 256,
1126 .bulk_out_size = 256,
1127 .open = pl2303_open,
1128 .close = pl2303_close,
1129 .dtr_rts = pl2303_dtr_rts,
1130 .carrier_raised = pl2303_carrier_raised,
1131 .get_serial = pl2303_get_serial,
1132 .break_ctl = pl2303_break_ctl,
1133 .set_termios = pl2303_set_termios,
1134 .tiocmget = pl2303_tiocmget,
1135 .tiocmset = pl2303_tiocmset,
1136 .tiocmiwait = usb_serial_generic_tiocmiwait,
1137 .process_read_urb = pl2303_process_read_urb,
1138 .read_int_callback = pl2303_read_int_callback,
1139 .probe = pl2303_probe,
1140 .calc_num_ports = pl2303_calc_num_ports,
1141 .attach = pl2303_startup,
1142 .release = pl2303_release,
1143 .port_probe = pl2303_port_probe,
1144 .port_remove = pl2303_port_remove,
1147 static struct usb_serial_driver * const serial_drivers[] = {
1148 &pl2303_device, NULL
1151 module_usb_serial_driver(serial_drivers, id_table);
1153 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1154 MODULE_LICENSE("GPL v2");