WIP FPC-III support
[linux/fpc-iii.git] / drivers / usb / serial / pl2303.c
blobbe8067017eaa53916b4fe9b9745741bdc5a10212
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_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(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
117 { USB_DEVICE(AT_VENDOR_ID, AT_VTKIT3_PRODUCT_ID) },
118 { } /* Terminating entry */
121 MODULE_DEVICE_TABLE(usb, id_table);
123 #define SET_LINE_REQUEST_TYPE 0x21
124 #define SET_LINE_REQUEST 0x20
126 #define SET_CONTROL_REQUEST_TYPE 0x21
127 #define SET_CONTROL_REQUEST 0x22
128 #define CONTROL_DTR 0x01
129 #define CONTROL_RTS 0x02
131 #define BREAK_REQUEST_TYPE 0x21
132 #define BREAK_REQUEST 0x23
133 #define BREAK_ON 0xffff
134 #define BREAK_OFF 0x0000
136 #define GET_LINE_REQUEST_TYPE 0xa1
137 #define GET_LINE_REQUEST 0x21
139 #define VENDOR_WRITE_REQUEST_TYPE 0x40
140 #define VENDOR_WRITE_REQUEST 0x01
141 #define VENDOR_WRITE_NREQUEST 0x80
143 #define VENDOR_READ_REQUEST_TYPE 0xc0
144 #define VENDOR_READ_REQUEST 0x01
145 #define VENDOR_READ_NREQUEST 0x81
147 #define UART_STATE_INDEX 8
148 #define UART_STATE_MSR_MASK 0x8b
149 #define UART_STATE_TRANSIENT_MASK 0x74
150 #define UART_DCD 0x01
151 #define UART_DSR 0x02
152 #define UART_BREAK_ERROR 0x04
153 #define UART_RING 0x08
154 #define UART_FRAME_ERROR 0x10
155 #define UART_PARITY_ERROR 0x20
156 #define UART_OVERRUN_ERROR 0x40
157 #define UART_CTS 0x80
159 #define PL2303_FLOWCTRL_MASK 0xf0
161 #define PL2303_READ_TYPE_HX_STATUS 0x8080
163 #define PL2303_HXN_RESET_REG 0x07
164 #define PL2303_HXN_RESET_UPSTREAM_PIPE 0x02
165 #define PL2303_HXN_RESET_DOWNSTREAM_PIPE 0x01
167 #define PL2303_HXN_FLOWCTRL_REG 0x0a
168 #define PL2303_HXN_FLOWCTRL_MASK 0x1c
169 #define PL2303_HXN_FLOWCTRL_NONE 0x1c
170 #define PL2303_HXN_FLOWCTRL_RTS_CTS 0x18
171 #define PL2303_HXN_FLOWCTRL_XON_XOFF 0x0c
173 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
175 enum pl2303_type {
176 TYPE_01, /* Type 0 and 1 (difference unknown) */
177 TYPE_HX, /* HX version of the pl2303 chip */
178 TYPE_HXN, /* HXN version of the pl2303 chip */
179 TYPE_COUNT
182 struct pl2303_type_data {
183 speed_t max_baud_rate;
184 unsigned long quirks;
185 unsigned int no_autoxonxoff:1;
188 struct pl2303_serial_private {
189 const struct pl2303_type_data *type;
190 unsigned long quirks;
193 struct pl2303_private {
194 spinlock_t lock;
195 u8 line_control;
196 u8 line_status;
198 u8 line_settings[7];
201 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
202 [TYPE_01] = {
203 .max_baud_rate = 1228800,
204 .quirks = PL2303_QUIRK_LEGACY,
205 .no_autoxonxoff = true,
207 [TYPE_HX] = {
208 .max_baud_rate = 12000000,
210 [TYPE_HXN] = {
211 .max_baud_rate = 12000000,
215 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
216 unsigned char buf[1])
218 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
219 struct device *dev = &serial->interface->dev;
220 u8 request;
221 int res;
223 if (spriv->type == &pl2303_type_data[TYPE_HXN])
224 request = VENDOR_READ_NREQUEST;
225 else
226 request = VENDOR_READ_REQUEST;
228 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
229 request, VENDOR_READ_REQUEST_TYPE,
230 value, 0, buf, 1, 100);
231 if (res != 1) {
232 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
233 value, res);
234 if (res >= 0)
235 res = -EIO;
237 return res;
240 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
242 return 0;
245 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
247 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
248 struct device *dev = &serial->interface->dev;
249 u8 request;
250 int res;
252 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
254 if (spriv->type == &pl2303_type_data[TYPE_HXN])
255 request = VENDOR_WRITE_NREQUEST;
256 else
257 request = VENDOR_WRITE_REQUEST;
259 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
260 request, VENDOR_WRITE_REQUEST_TYPE,
261 value, index, NULL, 0, 100);
262 if (res) {
263 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
264 value, res);
265 return res;
268 return 0;
271 static int pl2303_update_reg(struct usb_serial *serial, u8 reg, u8 mask, u8 val)
273 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
274 int ret = 0;
275 u8 *buf;
277 buf = kmalloc(1, GFP_KERNEL);
278 if (!buf)
279 return -ENOMEM;
281 if (spriv->type == &pl2303_type_data[TYPE_HXN])
282 ret = pl2303_vendor_read(serial, reg, buf);
283 else
284 ret = pl2303_vendor_read(serial, reg | 0x80, buf);
286 if (ret)
287 goto out_free;
289 *buf &= ~mask;
290 *buf |= val & mask;
292 ret = pl2303_vendor_write(serial, reg, *buf);
293 out_free:
294 kfree(buf);
296 return ret;
299 static int pl2303_probe(struct usb_serial *serial,
300 const struct usb_device_id *id)
302 usb_set_serial_data(serial, (void *)id->driver_info);
304 return 0;
308 * Use interrupt endpoint from first interface if available.
310 * This is needed due to the looney way its endpoints are set up.
312 static int pl2303_endpoint_hack(struct usb_serial *serial,
313 struct usb_serial_endpoints *epds)
315 struct usb_interface *interface = serial->interface;
316 struct usb_device *dev = serial->dev;
317 struct device *ddev = &interface->dev;
318 struct usb_host_interface *iface_desc;
319 struct usb_endpoint_descriptor *endpoint;
320 unsigned int i;
322 if (interface == dev->actconfig->interface[0])
323 return 0;
325 /* check out the endpoints of the other interface */
326 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
328 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
329 endpoint = &iface_desc->endpoint[i].desc;
331 if (!usb_endpoint_is_int_in(endpoint))
332 continue;
334 dev_dbg(ddev, "found interrupt in on separate interface\n");
335 if (epds->num_interrupt_in < ARRAY_SIZE(epds->interrupt_in))
336 epds->interrupt_in[epds->num_interrupt_in++] = endpoint;
339 return 0;
342 static int pl2303_calc_num_ports(struct usb_serial *serial,
343 struct usb_serial_endpoints *epds)
345 unsigned long quirks = (unsigned long)usb_get_serial_data(serial);
346 struct device *dev = &serial->interface->dev;
347 int ret;
349 if (quirks & PL2303_QUIRK_ENDPOINT_HACK) {
350 ret = pl2303_endpoint_hack(serial, epds);
351 if (ret)
352 return ret;
355 if (epds->num_interrupt_in < 1) {
356 dev_err(dev, "required interrupt-in endpoint missing\n");
357 return -ENODEV;
360 return 1;
363 static int pl2303_startup(struct usb_serial *serial)
365 struct pl2303_serial_private *spriv;
366 enum pl2303_type type = TYPE_01;
367 unsigned char *buf;
368 int res;
370 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
371 if (!spriv)
372 return -ENOMEM;
374 buf = kmalloc(1, GFP_KERNEL);
375 if (!buf) {
376 kfree(spriv);
377 return -ENOMEM;
380 if (serial->dev->descriptor.bDeviceClass == 0x02)
381 type = TYPE_01; /* type 0 */
382 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
383 type = TYPE_HX;
384 else if (serial->dev->descriptor.bDeviceClass == 0x00)
385 type = TYPE_01; /* type 1 */
386 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
387 type = TYPE_01; /* type 1 */
388 dev_dbg(&serial->interface->dev, "device type: %d\n", type);
390 if (type == TYPE_HX) {
391 res = usb_control_msg(serial->dev,
392 usb_rcvctrlpipe(serial->dev, 0),
393 VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
394 PL2303_READ_TYPE_HX_STATUS, 0, buf, 1, 100);
395 if (res != 1)
396 type = TYPE_HXN;
399 spriv->type = &pl2303_type_data[type];
400 spriv->quirks = (unsigned long)usb_get_serial_data(serial);
401 spriv->quirks |= spriv->type->quirks;
403 usb_set_serial_data(serial, spriv);
405 if (type != TYPE_HXN) {
406 pl2303_vendor_read(serial, 0x8484, buf);
407 pl2303_vendor_write(serial, 0x0404, 0);
408 pl2303_vendor_read(serial, 0x8484, buf);
409 pl2303_vendor_read(serial, 0x8383, buf);
410 pl2303_vendor_read(serial, 0x8484, buf);
411 pl2303_vendor_write(serial, 0x0404, 1);
412 pl2303_vendor_read(serial, 0x8484, buf);
413 pl2303_vendor_read(serial, 0x8383, buf);
414 pl2303_vendor_write(serial, 0, 1);
415 pl2303_vendor_write(serial, 1, 0);
416 if (spriv->quirks & PL2303_QUIRK_LEGACY)
417 pl2303_vendor_write(serial, 2, 0x24);
418 else
419 pl2303_vendor_write(serial, 2, 0x44);
422 kfree(buf);
424 return 0;
427 static void pl2303_release(struct usb_serial *serial)
429 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
431 kfree(spriv);
434 static int pl2303_port_probe(struct usb_serial_port *port)
436 struct pl2303_private *priv;
438 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
439 if (!priv)
440 return -ENOMEM;
442 spin_lock_init(&priv->lock);
444 usb_set_serial_port_data(port, priv);
446 port->port.drain_delay = 256;
448 return 0;
451 static int pl2303_port_remove(struct usb_serial_port *port)
453 struct pl2303_private *priv = usb_get_serial_port_data(port);
455 kfree(priv);
457 return 0;
460 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
462 struct usb_device *dev = port->serial->dev;
463 int retval;
465 dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
467 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
468 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
469 value, 0, NULL, 0, 100);
470 if (retval)
471 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
473 return retval;
477 * Returns the nearest supported baud rate that can be set directly without
478 * using divisors.
480 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
482 static const speed_t baud_sup[] = {
483 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
484 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
485 614400, 921600, 1228800, 2457600, 3000000, 6000000
488 unsigned i;
490 for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
491 if (baud_sup[i] > baud)
492 break;
495 if (i == ARRAY_SIZE(baud_sup))
496 baud = baud_sup[i - 1];
497 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
498 baud = baud_sup[i - 1];
499 else
500 baud = baud_sup[i];
502 return baud;
506 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
507 * use 9600 baud.
509 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
510 speed_t baud)
512 put_unaligned_le32(baud, buf);
514 return baud;
517 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
518 speed_t baud)
520 unsigned int baseline, mantissa, exponent;
523 * Apparently the formula is:
524 * baudrate = 12M * 32 / (mantissa * 4^exponent)
525 * where
526 * mantissa = buf[8:0]
527 * exponent = buf[11:9]
529 baseline = 12000000 * 32;
530 mantissa = baseline / baud;
531 if (mantissa == 0)
532 mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
533 exponent = 0;
534 while (mantissa >= 512) {
535 if (exponent < 7) {
536 mantissa >>= 2; /* divide by 4 */
537 exponent++;
538 } else {
539 /* Exponent is maxed. Trim mantissa and leave. */
540 mantissa = 511;
541 break;
545 buf[3] = 0x80;
546 buf[2] = 0;
547 buf[1] = exponent << 1 | mantissa >> 8;
548 buf[0] = mantissa & 0xff;
550 /* Calculate and return the exact baud rate. */
551 baud = (baseline / mantissa) >> (exponent << 1);
553 return baud;
556 static void pl2303_encode_baud_rate(struct tty_struct *tty,
557 struct usb_serial_port *port,
558 u8 buf[4])
560 struct usb_serial *serial = port->serial;
561 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
562 speed_t baud_sup;
563 speed_t baud;
565 baud = tty_get_baud_rate(tty);
566 dev_dbg(&port->dev, "baud requested = %u\n", baud);
567 if (!baud)
568 return;
570 if (spriv->type->max_baud_rate)
571 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
573 * Use direct method for supported baud rates, otherwise use divisors.
575 baud_sup = pl2303_get_supported_baud_rate(baud);
577 if (baud == baud_sup)
578 baud = pl2303_encode_baud_rate_direct(buf, baud);
579 else
580 baud = pl2303_encode_baud_rate_divisor(buf, baud);
582 /* Save resulting baud rate */
583 tty_encode_baud_rate(tty, baud, baud);
584 dev_dbg(&port->dev, "baud set = %u\n", baud);
587 static int pl2303_get_line_request(struct usb_serial_port *port,
588 unsigned char buf[7])
590 struct usb_device *udev = port->serial->dev;
591 int ret;
593 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
594 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
595 0, 0, buf, 7, 100);
596 if (ret != 7) {
597 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
599 if (ret >= 0)
600 ret = -EIO;
602 return ret;
605 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
607 return 0;
610 static int pl2303_set_line_request(struct usb_serial_port *port,
611 unsigned char buf[7])
613 struct usb_device *udev = port->serial->dev;
614 int ret;
616 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
617 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
618 0, 0, buf, 7, 100);
619 if (ret < 0) {
620 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
621 return ret;
624 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
626 return 0;
629 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b)
631 bool ixon_change;
633 ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) ||
634 a->c_cc[VSTART] != b->c_cc[VSTART] ||
635 a->c_cc[VSTOP] != b->c_cc[VSTOP];
637 return tty_termios_hw_change(a, b) || ixon_change;
640 static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type)
642 if (!I_IXON(tty) || I_IXANY(tty))
643 return false;
645 if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13)
646 return false;
648 if (type->no_autoxonxoff)
649 return false;
651 return true;
654 static void pl2303_set_termios(struct tty_struct *tty,
655 struct usb_serial_port *port, struct ktermios *old_termios)
657 struct usb_serial *serial = port->serial;
658 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
659 struct pl2303_private *priv = usb_get_serial_port_data(port);
660 unsigned long flags;
661 unsigned char *buf;
662 int ret;
663 u8 control;
665 if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
666 return;
668 buf = kzalloc(7, GFP_KERNEL);
669 if (!buf) {
670 /* Report back no change occurred */
671 if (old_termios)
672 tty->termios = *old_termios;
673 return;
676 pl2303_get_line_request(port, buf);
678 switch (C_CSIZE(tty)) {
679 case CS5:
680 buf[6] = 5;
681 break;
682 case CS6:
683 buf[6] = 6;
684 break;
685 case CS7:
686 buf[6] = 7;
687 break;
688 default:
689 case CS8:
690 buf[6] = 8;
692 dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
694 /* For reference buf[0]:buf[3] baud rate value */
695 pl2303_encode_baud_rate(tty, port, &buf[0]);
697 /* For reference buf[4]=0 is 1 stop bits */
698 /* For reference buf[4]=1 is 1.5 stop bits */
699 /* For reference buf[4]=2 is 2 stop bits */
700 if (C_CSTOPB(tty)) {
702 * NOTE: Comply with "real" UARTs / RS232:
703 * use 1.5 instead of 2 stop bits with 5 data bits
705 if (C_CSIZE(tty) == CS5) {
706 buf[4] = 1;
707 dev_dbg(&port->dev, "stop bits = 1.5\n");
708 } else {
709 buf[4] = 2;
710 dev_dbg(&port->dev, "stop bits = 2\n");
712 } else {
713 buf[4] = 0;
714 dev_dbg(&port->dev, "stop bits = 1\n");
717 if (C_PARENB(tty)) {
718 /* For reference buf[5]=0 is none parity */
719 /* For reference buf[5]=1 is odd parity */
720 /* For reference buf[5]=2 is even parity */
721 /* For reference buf[5]=3 is mark parity */
722 /* For reference buf[5]=4 is space parity */
723 if (C_PARODD(tty)) {
724 if (C_CMSPAR(tty)) {
725 buf[5] = 3;
726 dev_dbg(&port->dev, "parity = mark\n");
727 } else {
728 buf[5] = 1;
729 dev_dbg(&port->dev, "parity = odd\n");
731 } else {
732 if (C_CMSPAR(tty)) {
733 buf[5] = 4;
734 dev_dbg(&port->dev, "parity = space\n");
735 } else {
736 buf[5] = 2;
737 dev_dbg(&port->dev, "parity = even\n");
740 } else {
741 buf[5] = 0;
742 dev_dbg(&port->dev, "parity = none\n");
746 * Some PL2303 are known to lose bytes if you change serial settings
747 * even to the same values as before. Thus we actually need to filter
748 * in this specific case.
750 * Note that the tty_termios_hw_change check above is not sufficient
751 * as a previously requested baud rate may differ from the one
752 * actually used (and stored in old_termios).
754 * NOTE: No additional locking needed for line_settings as it is
755 * only used in set_termios, which is serialised against itself.
757 if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
758 ret = pl2303_set_line_request(port, buf);
759 if (!ret)
760 memcpy(priv->line_settings, buf, 7);
763 /* change control lines if we are switching to or from B0 */
764 spin_lock_irqsave(&priv->lock, flags);
765 control = priv->line_control;
766 if (C_BAUD(tty) == B0)
767 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
768 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
769 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
770 if (control != priv->line_control) {
771 control = priv->line_control;
772 spin_unlock_irqrestore(&priv->lock, flags);
773 pl2303_set_control_lines(port, control);
774 } else {
775 spin_unlock_irqrestore(&priv->lock, flags);
778 if (C_CRTSCTS(tty)) {
779 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
780 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
781 } else if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
782 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
783 PL2303_HXN_FLOWCTRL_MASK,
784 PL2303_HXN_FLOWCTRL_RTS_CTS);
785 } else {
786 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
788 } else if (pl2303_enable_xonxoff(tty, spriv->type)) {
789 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
790 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
791 PL2303_HXN_FLOWCTRL_MASK,
792 PL2303_HXN_FLOWCTRL_XON_XOFF);
793 } else {
794 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
796 } else {
797 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
798 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
799 PL2303_HXN_FLOWCTRL_MASK,
800 PL2303_HXN_FLOWCTRL_NONE);
801 } else {
802 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
806 kfree(buf);
809 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
811 struct pl2303_private *priv = usb_get_serial_port_data(port);
812 unsigned long flags;
813 u8 control;
815 spin_lock_irqsave(&priv->lock, flags);
816 if (on)
817 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
818 else
819 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
820 control = priv->line_control;
821 spin_unlock_irqrestore(&priv->lock, flags);
823 pl2303_set_control_lines(port, control);
826 static void pl2303_close(struct usb_serial_port *port)
828 usb_serial_generic_close(port);
829 usb_kill_urb(port->interrupt_in_urb);
830 pl2303_set_break(port, false);
833 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
835 struct usb_serial *serial = port->serial;
836 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
837 int result;
839 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
840 usb_clear_halt(serial->dev, port->write_urb->pipe);
841 usb_clear_halt(serial->dev, port->read_urb->pipe);
842 } else {
843 /* reset upstream data pipes */
844 if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
845 pl2303_vendor_write(serial, PL2303_HXN_RESET_REG,
846 PL2303_HXN_RESET_UPSTREAM_PIPE |
847 PL2303_HXN_RESET_DOWNSTREAM_PIPE);
848 } else {
849 pl2303_vendor_write(serial, 8, 0);
850 pl2303_vendor_write(serial, 9, 0);
854 /* Setup termios */
855 if (tty)
856 pl2303_set_termios(tty, port, NULL);
858 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
859 if (result) {
860 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
861 result);
862 return result;
865 result = usb_serial_generic_open(tty, port);
866 if (result) {
867 usb_kill_urb(port->interrupt_in_urb);
868 return result;
871 return 0;
874 static int pl2303_tiocmset(struct tty_struct *tty,
875 unsigned int set, unsigned int clear)
877 struct usb_serial_port *port = tty->driver_data;
878 struct pl2303_private *priv = usb_get_serial_port_data(port);
879 unsigned long flags;
880 u8 control;
881 int ret;
883 spin_lock_irqsave(&priv->lock, flags);
884 if (set & TIOCM_RTS)
885 priv->line_control |= CONTROL_RTS;
886 if (set & TIOCM_DTR)
887 priv->line_control |= CONTROL_DTR;
888 if (clear & TIOCM_RTS)
889 priv->line_control &= ~CONTROL_RTS;
890 if (clear & TIOCM_DTR)
891 priv->line_control &= ~CONTROL_DTR;
892 control = priv->line_control;
893 spin_unlock_irqrestore(&priv->lock, flags);
895 ret = pl2303_set_control_lines(port, control);
896 if (ret)
897 return usb_translate_errors(ret);
899 return 0;
902 static int pl2303_tiocmget(struct tty_struct *tty)
904 struct usb_serial_port *port = tty->driver_data;
905 struct pl2303_private *priv = usb_get_serial_port_data(port);
906 unsigned long flags;
907 unsigned int mcr;
908 unsigned int status;
909 unsigned int result;
911 spin_lock_irqsave(&priv->lock, flags);
912 mcr = priv->line_control;
913 status = priv->line_status;
914 spin_unlock_irqrestore(&priv->lock, flags);
916 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
917 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
918 | ((status & UART_CTS) ? TIOCM_CTS : 0)
919 | ((status & UART_DSR) ? TIOCM_DSR : 0)
920 | ((status & UART_RING) ? TIOCM_RI : 0)
921 | ((status & UART_DCD) ? TIOCM_CD : 0);
923 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
925 return result;
928 static int pl2303_carrier_raised(struct usb_serial_port *port)
930 struct pl2303_private *priv = usb_get_serial_port_data(port);
932 if (priv->line_status & UART_DCD)
933 return 1;
935 return 0;
938 static int pl2303_get_serial(struct tty_struct *tty,
939 struct serial_struct *ss)
941 struct usb_serial_port *port = tty->driver_data;
943 ss->type = PORT_16654;
944 ss->line = port->minor;
945 ss->port = port->port_number;
946 ss->baud_base = 460800;
947 return 0;
950 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
952 struct usb_serial *serial = port->serial;
953 u16 state;
954 int result;
956 if (enable)
957 state = BREAK_ON;
958 else
959 state = BREAK_OFF;
961 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
962 state == BREAK_OFF ? "off" : "on");
964 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
965 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
966 0, NULL, 0, 100);
967 if (result)
968 dev_err(&port->dev, "error sending break = %d\n", result);
971 static void pl2303_break_ctl(struct tty_struct *tty, int state)
973 struct usb_serial_port *port = tty->driver_data;
975 pl2303_set_break(port, state);
978 static void pl2303_update_line_status(struct usb_serial_port *port,
979 unsigned char *data,
980 unsigned int actual_length)
982 struct usb_serial *serial = port->serial;
983 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
984 struct pl2303_private *priv = usb_get_serial_port_data(port);
985 struct tty_struct *tty;
986 unsigned long flags;
987 unsigned int status_idx = UART_STATE_INDEX;
988 u8 status;
989 u8 delta;
991 if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
992 status_idx = 0;
994 if (actual_length < status_idx + 1)
995 return;
997 status = data[status_idx];
999 /* Save off the uart status for others to look at */
1000 spin_lock_irqsave(&priv->lock, flags);
1001 delta = priv->line_status ^ status;
1002 priv->line_status = status;
1003 spin_unlock_irqrestore(&priv->lock, flags);
1005 if (status & UART_BREAK_ERROR)
1006 usb_serial_handle_break(port);
1008 if (delta & UART_STATE_MSR_MASK) {
1009 if (delta & UART_CTS)
1010 port->icount.cts++;
1011 if (delta & UART_DSR)
1012 port->icount.dsr++;
1013 if (delta & UART_RING)
1014 port->icount.rng++;
1015 if (delta & UART_DCD) {
1016 port->icount.dcd++;
1017 tty = tty_port_tty_get(&port->port);
1018 if (tty) {
1019 usb_serial_handle_dcd_change(port, tty,
1020 status & UART_DCD);
1021 tty_kref_put(tty);
1025 wake_up_interruptible(&port->port.delta_msr_wait);
1029 static void pl2303_read_int_callback(struct urb *urb)
1031 struct usb_serial_port *port = urb->context;
1032 unsigned char *data = urb->transfer_buffer;
1033 unsigned int actual_length = urb->actual_length;
1034 int status = urb->status;
1035 int retval;
1037 switch (status) {
1038 case 0:
1039 /* success */
1040 break;
1041 case -ECONNRESET:
1042 case -ENOENT:
1043 case -ESHUTDOWN:
1044 /* this urb is terminated, clean up */
1045 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
1046 __func__, status);
1047 return;
1048 default:
1049 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
1050 __func__, status);
1051 goto exit;
1054 usb_serial_debug_data(&port->dev, __func__,
1055 urb->actual_length, urb->transfer_buffer);
1057 pl2303_update_line_status(port, data, actual_length);
1059 exit:
1060 retval = usb_submit_urb(urb, GFP_ATOMIC);
1061 if (retval) {
1062 dev_err(&port->dev,
1063 "%s - usb_submit_urb failed with result %d\n",
1064 __func__, retval);
1068 static void pl2303_process_read_urb(struct urb *urb)
1070 struct usb_serial_port *port = urb->context;
1071 struct pl2303_private *priv = usb_get_serial_port_data(port);
1072 unsigned char *data = urb->transfer_buffer;
1073 char tty_flag = TTY_NORMAL;
1074 unsigned long flags;
1075 u8 line_status;
1076 int i;
1078 /* update line status */
1079 spin_lock_irqsave(&priv->lock, flags);
1080 line_status = priv->line_status;
1081 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1082 spin_unlock_irqrestore(&priv->lock, flags);
1084 if (!urb->actual_length)
1085 return;
1088 * Break takes precedence over parity, which takes precedence over
1089 * framing errors.
1091 if (line_status & UART_BREAK_ERROR)
1092 tty_flag = TTY_BREAK;
1093 else if (line_status & UART_PARITY_ERROR)
1094 tty_flag = TTY_PARITY;
1095 else if (line_status & UART_FRAME_ERROR)
1096 tty_flag = TTY_FRAME;
1098 if (tty_flag != TTY_NORMAL)
1099 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
1100 tty_flag);
1101 /* overrun is special, not associated with a char */
1102 if (line_status & UART_OVERRUN_ERROR)
1103 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1105 if (port->sysrq) {
1106 for (i = 0; i < urb->actual_length; ++i)
1107 if (!usb_serial_handle_sysrq_char(port, data[i]))
1108 tty_insert_flip_char(&port->port, data[i],
1109 tty_flag);
1110 } else {
1111 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
1112 urb->actual_length);
1115 tty_flip_buffer_push(&port->port);
1118 static struct usb_serial_driver pl2303_device = {
1119 .driver = {
1120 .owner = THIS_MODULE,
1121 .name = "pl2303",
1123 .id_table = id_table,
1124 .num_bulk_in = 1,
1125 .num_bulk_out = 1,
1126 .num_interrupt_in = 0, /* see pl2303_calc_num_ports */
1127 .bulk_in_size = 256,
1128 .bulk_out_size = 256,
1129 .open = pl2303_open,
1130 .close = pl2303_close,
1131 .dtr_rts = pl2303_dtr_rts,
1132 .carrier_raised = pl2303_carrier_raised,
1133 .get_serial = pl2303_get_serial,
1134 .break_ctl = pl2303_break_ctl,
1135 .set_termios = pl2303_set_termios,
1136 .tiocmget = pl2303_tiocmget,
1137 .tiocmset = pl2303_tiocmset,
1138 .tiocmiwait = usb_serial_generic_tiocmiwait,
1139 .process_read_urb = pl2303_process_read_urb,
1140 .read_int_callback = pl2303_read_int_callback,
1141 .probe = pl2303_probe,
1142 .calc_num_ports = pl2303_calc_num_ports,
1143 .attach = pl2303_startup,
1144 .release = pl2303_release,
1145 .port_probe = pl2303_port_probe,
1146 .port_remove = pl2303_port_remove,
1149 static struct usb_serial_driver * const serial_drivers[] = {
1150 &pl2303_device, NULL
1153 module_usb_serial_driver(serial_drivers, id_table);
1155 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1156 MODULE_LICENSE("GPL v2");