dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / usb / serial / pl2303.c
blobbb3f9aa4a9093fe4fcf6ebc192cf1d040d6dc8c3
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.txt 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(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
51 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
52 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID),
53 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
54 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485),
55 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
56 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC232B),
57 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
58 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
59 { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
60 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
61 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
62 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
63 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
64 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
65 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
66 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
67 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
68 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
69 { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
70 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
71 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1),
72 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
73 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65),
74 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
75 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75),
76 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
77 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81),
78 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
79 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
80 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
81 { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
82 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
83 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
84 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
85 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
86 { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
87 { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
88 { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID),
89 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
90 { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
91 { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
92 { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
93 { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
94 { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
95 { USB_DEVICE(HP_VENDOR_ID, HP_LD220TA_PRODUCT_ID) },
96 { USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
97 { USB_DEVICE(HP_VENDOR_ID, HP_LD960TA_PRODUCT_ID) },
98 { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
99 { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
100 { USB_DEVICE(HP_VENDOR_ID, HP_LM920_PRODUCT_ID) },
101 { USB_DEVICE(HP_VENDOR_ID, HP_LM940_PRODUCT_ID) },
102 { USB_DEVICE(HP_VENDOR_ID, HP_TD620_PRODUCT_ID) },
103 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
104 { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
105 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
106 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
107 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
108 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
109 { } /* Terminating entry */
112 MODULE_DEVICE_TABLE(usb, id_table);
114 #define SET_LINE_REQUEST_TYPE 0x21
115 #define SET_LINE_REQUEST 0x20
117 #define SET_CONTROL_REQUEST_TYPE 0x21
118 #define SET_CONTROL_REQUEST 0x22
119 #define CONTROL_DTR 0x01
120 #define CONTROL_RTS 0x02
122 #define BREAK_REQUEST_TYPE 0x21
123 #define BREAK_REQUEST 0x23
124 #define BREAK_ON 0xffff
125 #define BREAK_OFF 0x0000
127 #define GET_LINE_REQUEST_TYPE 0xa1
128 #define GET_LINE_REQUEST 0x21
130 #define VENDOR_WRITE_REQUEST_TYPE 0x40
131 #define VENDOR_WRITE_REQUEST 0x01
133 #define VENDOR_READ_REQUEST_TYPE 0xc0
134 #define VENDOR_READ_REQUEST 0x01
136 #define UART_STATE_INDEX 8
137 #define UART_STATE_MSR_MASK 0x8b
138 #define UART_STATE_TRANSIENT_MASK 0x74
139 #define UART_DCD 0x01
140 #define UART_DSR 0x02
141 #define UART_BREAK_ERROR 0x04
142 #define UART_RING 0x08
143 #define UART_FRAME_ERROR 0x10
144 #define UART_PARITY_ERROR 0x20
145 #define UART_OVERRUN_ERROR 0x40
146 #define UART_CTS 0x80
148 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
150 enum pl2303_type {
151 TYPE_01, /* Type 0 and 1 (difference unknown) */
152 TYPE_HX, /* HX version of the pl2303 chip */
153 TYPE_COUNT
156 struct pl2303_type_data {
157 speed_t max_baud_rate;
158 unsigned long quirks;
161 struct pl2303_serial_private {
162 const struct pl2303_type_data *type;
163 unsigned long quirks;
166 struct pl2303_private {
167 spinlock_t lock;
168 u8 line_control;
169 u8 line_status;
171 u8 line_settings[7];
174 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
175 [TYPE_01] = {
176 .max_baud_rate = 1228800,
177 .quirks = PL2303_QUIRK_LEGACY,
179 [TYPE_HX] = {
180 .max_baud_rate = 12000000,
184 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
185 unsigned char buf[1])
187 struct device *dev = &serial->interface->dev;
188 int res;
190 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
191 VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
192 value, 0, buf, 1, 100);
193 if (res != 1) {
194 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
195 value, res);
196 if (res >= 0)
197 res = -EIO;
199 return res;
202 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
204 return 0;
207 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
209 struct device *dev = &serial->interface->dev;
210 int res;
212 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
214 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
215 VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
216 value, index, NULL, 0, 100);
217 if (res) {
218 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
219 value, res);
220 return res;
223 return 0;
226 static int pl2303_probe(struct usb_serial *serial,
227 const struct usb_device_id *id)
229 usb_set_serial_data(serial, (void *)id->driver_info);
231 return 0;
235 * Use interrupt endpoint from first interface if available.
237 * This is needed due to the looney way its endpoints are set up.
239 static int pl2303_endpoint_hack(struct usb_serial *serial,
240 struct usb_serial_endpoints *epds)
242 struct usb_interface *interface = serial->interface;
243 struct usb_device *dev = serial->dev;
244 struct device *ddev = &interface->dev;
245 struct usb_host_interface *iface_desc;
246 struct usb_endpoint_descriptor *endpoint;
247 unsigned int i;
249 if (interface == dev->actconfig->interface[0])
250 return 0;
252 /* check out the endpoints of the other interface */
253 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
255 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
256 endpoint = &iface_desc->endpoint[i].desc;
258 if (!usb_endpoint_is_int_in(endpoint))
259 continue;
261 dev_dbg(ddev, "found interrupt in on separate interface\n");
262 if (epds->num_interrupt_in < ARRAY_SIZE(epds->interrupt_in))
263 epds->interrupt_in[epds->num_interrupt_in++] = endpoint;
266 return 0;
269 static int pl2303_calc_num_ports(struct usb_serial *serial,
270 struct usb_serial_endpoints *epds)
272 unsigned long quirks = (unsigned long)usb_get_serial_data(serial);
273 struct device *dev = &serial->interface->dev;
274 int ret;
276 if (quirks & PL2303_QUIRK_ENDPOINT_HACK) {
277 ret = pl2303_endpoint_hack(serial, epds);
278 if (ret)
279 return ret;
282 if (epds->num_interrupt_in < 1) {
283 dev_err(dev, "required interrupt-in endpoint missing\n");
284 return -ENODEV;
287 return 1;
290 static int pl2303_startup(struct usb_serial *serial)
292 struct pl2303_serial_private *spriv;
293 enum pl2303_type type = TYPE_01;
294 unsigned char *buf;
296 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
297 if (!spriv)
298 return -ENOMEM;
300 buf = kmalloc(1, GFP_KERNEL);
301 if (!buf) {
302 kfree(spriv);
303 return -ENOMEM;
306 if (serial->dev->descriptor.bDeviceClass == 0x02)
307 type = TYPE_01; /* type 0 */
308 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
309 type = TYPE_HX;
310 else if (serial->dev->descriptor.bDeviceClass == 0x00)
311 type = TYPE_01; /* type 1 */
312 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
313 type = TYPE_01; /* type 1 */
314 dev_dbg(&serial->interface->dev, "device type: %d\n", type);
316 spriv->type = &pl2303_type_data[type];
317 spriv->quirks = (unsigned long)usb_get_serial_data(serial);
318 spriv->quirks |= spriv->type->quirks;
320 usb_set_serial_data(serial, spriv);
322 pl2303_vendor_read(serial, 0x8484, buf);
323 pl2303_vendor_write(serial, 0x0404, 0);
324 pl2303_vendor_read(serial, 0x8484, buf);
325 pl2303_vendor_read(serial, 0x8383, buf);
326 pl2303_vendor_read(serial, 0x8484, buf);
327 pl2303_vendor_write(serial, 0x0404, 1);
328 pl2303_vendor_read(serial, 0x8484, buf);
329 pl2303_vendor_read(serial, 0x8383, buf);
330 pl2303_vendor_write(serial, 0, 1);
331 pl2303_vendor_write(serial, 1, 0);
332 if (spriv->quirks & PL2303_QUIRK_LEGACY)
333 pl2303_vendor_write(serial, 2, 0x24);
334 else
335 pl2303_vendor_write(serial, 2, 0x44);
337 kfree(buf);
339 return 0;
342 static void pl2303_release(struct usb_serial *serial)
344 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
346 kfree(spriv);
349 static int pl2303_port_probe(struct usb_serial_port *port)
351 struct pl2303_private *priv;
353 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
354 if (!priv)
355 return -ENOMEM;
357 spin_lock_init(&priv->lock);
359 usb_set_serial_port_data(port, priv);
361 port->port.drain_delay = 256;
363 return 0;
366 static int pl2303_port_remove(struct usb_serial_port *port)
368 struct pl2303_private *priv = usb_get_serial_port_data(port);
370 kfree(priv);
372 return 0;
375 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
377 struct usb_device *dev = port->serial->dev;
378 int retval;
380 dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
382 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
383 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
384 value, 0, NULL, 0, 100);
385 if (retval)
386 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
388 return retval;
392 * Returns the nearest supported baud rate that can be set directly without
393 * using divisors.
395 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
397 static const speed_t baud_sup[] = {
398 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
399 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
400 614400, 921600, 1228800, 2457600, 3000000, 6000000
403 unsigned i;
405 for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
406 if (baud_sup[i] > baud)
407 break;
410 if (i == ARRAY_SIZE(baud_sup))
411 baud = baud_sup[i - 1];
412 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
413 baud = baud_sup[i - 1];
414 else
415 baud = baud_sup[i];
417 return baud;
421 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
422 * use 9600 baud.
424 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
425 speed_t baud)
427 put_unaligned_le32(baud, buf);
429 return baud;
432 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
433 speed_t baud)
435 unsigned int baseline, mantissa, exponent;
438 * Apparently the formula is:
439 * baudrate = 12M * 32 / (mantissa * 4^exponent)
440 * where
441 * mantissa = buf[8:0]
442 * exponent = buf[11:9]
444 baseline = 12000000 * 32;
445 mantissa = baseline / baud;
446 if (mantissa == 0)
447 mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
448 exponent = 0;
449 while (mantissa >= 512) {
450 if (exponent < 7) {
451 mantissa >>= 2; /* divide by 4 */
452 exponent++;
453 } else {
454 /* Exponent is maxed. Trim mantissa and leave. */
455 mantissa = 511;
456 break;
460 buf[3] = 0x80;
461 buf[2] = 0;
462 buf[1] = exponent << 1 | mantissa >> 8;
463 buf[0] = mantissa & 0xff;
465 /* Calculate and return the exact baud rate. */
466 baud = (baseline / mantissa) >> (exponent << 1);
468 return baud;
471 static void pl2303_encode_baud_rate(struct tty_struct *tty,
472 struct usb_serial_port *port,
473 u8 buf[4])
475 struct usb_serial *serial = port->serial;
476 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
477 speed_t baud_sup;
478 speed_t baud;
480 baud = tty_get_baud_rate(tty);
481 dev_dbg(&port->dev, "baud requested = %u\n", baud);
482 if (!baud)
483 return;
485 if (spriv->type->max_baud_rate)
486 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
488 * Use direct method for supported baud rates, otherwise use divisors.
490 baud_sup = pl2303_get_supported_baud_rate(baud);
492 if (baud == baud_sup)
493 baud = pl2303_encode_baud_rate_direct(buf, baud);
494 else
495 baud = pl2303_encode_baud_rate_divisor(buf, baud);
497 /* Save resulting baud rate */
498 tty_encode_baud_rate(tty, baud, baud);
499 dev_dbg(&port->dev, "baud set = %u\n", baud);
502 static int pl2303_get_line_request(struct usb_serial_port *port,
503 unsigned char buf[7])
505 struct usb_device *udev = port->serial->dev;
506 int ret;
508 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
509 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
510 0, 0, buf, 7, 100);
511 if (ret != 7) {
512 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
514 if (ret >= 0)
515 ret = -EIO;
517 return ret;
520 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
522 return 0;
525 static int pl2303_set_line_request(struct usb_serial_port *port,
526 unsigned char buf[7])
528 struct usb_device *udev = port->serial->dev;
529 int ret;
531 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
532 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
533 0, 0, buf, 7, 100);
534 if (ret < 0) {
535 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
536 return ret;
539 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
541 return 0;
544 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b)
546 bool ixon_change;
548 ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) ||
549 a->c_cc[VSTART] != b->c_cc[VSTART] ||
550 a->c_cc[VSTOP] != b->c_cc[VSTOP];
552 return tty_termios_hw_change(a, b) || ixon_change;
555 static void pl2303_set_termios(struct tty_struct *tty,
556 struct usb_serial_port *port, struct ktermios *old_termios)
558 struct usb_serial *serial = port->serial;
559 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
560 struct pl2303_private *priv = usb_get_serial_port_data(port);
561 unsigned long flags;
562 unsigned char *buf;
563 int ret;
564 u8 control;
566 if (old_termios && !pl2303_termios_change(&tty->termios, old_termios))
567 return;
569 buf = kzalloc(7, GFP_KERNEL);
570 if (!buf) {
571 /* Report back no change occurred */
572 if (old_termios)
573 tty->termios = *old_termios;
574 return;
577 pl2303_get_line_request(port, buf);
579 switch (C_CSIZE(tty)) {
580 case CS5:
581 buf[6] = 5;
582 break;
583 case CS6:
584 buf[6] = 6;
585 break;
586 case CS7:
587 buf[6] = 7;
588 break;
589 default:
590 case CS8:
591 buf[6] = 8;
593 dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
595 /* For reference buf[0]:buf[3] baud rate value */
596 pl2303_encode_baud_rate(tty, port, &buf[0]);
598 /* For reference buf[4]=0 is 1 stop bits */
599 /* For reference buf[4]=1 is 1.5 stop bits */
600 /* For reference buf[4]=2 is 2 stop bits */
601 if (C_CSTOPB(tty)) {
603 * NOTE: Comply with "real" UARTs / RS232:
604 * use 1.5 instead of 2 stop bits with 5 data bits
606 if (C_CSIZE(tty) == CS5) {
607 buf[4] = 1;
608 dev_dbg(&port->dev, "stop bits = 1.5\n");
609 } else {
610 buf[4] = 2;
611 dev_dbg(&port->dev, "stop bits = 2\n");
613 } else {
614 buf[4] = 0;
615 dev_dbg(&port->dev, "stop bits = 1\n");
618 if (C_PARENB(tty)) {
619 /* For reference buf[5]=0 is none parity */
620 /* For reference buf[5]=1 is odd parity */
621 /* For reference buf[5]=2 is even parity */
622 /* For reference buf[5]=3 is mark parity */
623 /* For reference buf[5]=4 is space parity */
624 if (C_PARODD(tty)) {
625 if (C_CMSPAR(tty)) {
626 buf[5] = 3;
627 dev_dbg(&port->dev, "parity = mark\n");
628 } else {
629 buf[5] = 1;
630 dev_dbg(&port->dev, "parity = odd\n");
632 } else {
633 if (C_CMSPAR(tty)) {
634 buf[5] = 4;
635 dev_dbg(&port->dev, "parity = space\n");
636 } else {
637 buf[5] = 2;
638 dev_dbg(&port->dev, "parity = even\n");
641 } else {
642 buf[5] = 0;
643 dev_dbg(&port->dev, "parity = none\n");
647 * Some PL2303 are known to lose bytes if you change serial settings
648 * even to the same values as before. Thus we actually need to filter
649 * in this specific case.
651 * Note that the tty_termios_hw_change check above is not sufficient
652 * as a previously requested baud rate may differ from the one
653 * actually used (and stored in old_termios).
655 * NOTE: No additional locking needed for line_settings as it is
656 * only used in set_termios, which is serialised against itself.
658 if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
659 ret = pl2303_set_line_request(port, buf);
660 if (!ret)
661 memcpy(priv->line_settings, buf, 7);
664 /* change control lines if we are switching to or from B0 */
665 spin_lock_irqsave(&priv->lock, flags);
666 control = priv->line_control;
667 if (C_BAUD(tty) == B0)
668 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
669 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
670 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
671 if (control != priv->line_control) {
672 control = priv->line_control;
673 spin_unlock_irqrestore(&priv->lock, flags);
674 pl2303_set_control_lines(port, control);
675 } else {
676 spin_unlock_irqrestore(&priv->lock, flags);
679 if (C_CRTSCTS(tty)) {
680 if (spriv->quirks & PL2303_QUIRK_LEGACY)
681 pl2303_vendor_write(serial, 0x0, 0x41);
682 else
683 pl2303_vendor_write(serial, 0x0, 0x61);
684 } else if (I_IXON(tty) && !I_IXANY(tty) && START_CHAR(tty) == 0x11 &&
685 STOP_CHAR(tty) == 0x13) {
686 pl2303_vendor_write(serial, 0x0, 0xc0);
687 } else {
688 pl2303_vendor_write(serial, 0x0, 0x0);
691 kfree(buf);
694 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
696 struct pl2303_private *priv = usb_get_serial_port_data(port);
697 unsigned long flags;
698 u8 control;
700 spin_lock_irqsave(&priv->lock, flags);
701 if (on)
702 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
703 else
704 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
705 control = priv->line_control;
706 spin_unlock_irqrestore(&priv->lock, flags);
708 pl2303_set_control_lines(port, control);
711 static void pl2303_close(struct usb_serial_port *port)
713 usb_serial_generic_close(port);
714 usb_kill_urb(port->interrupt_in_urb);
715 pl2303_set_break(port, false);
718 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
720 struct usb_serial *serial = port->serial;
721 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
722 int result;
724 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
725 usb_clear_halt(serial->dev, port->write_urb->pipe);
726 usb_clear_halt(serial->dev, port->read_urb->pipe);
727 } else {
728 /* reset upstream data pipes */
729 pl2303_vendor_write(serial, 8, 0);
730 pl2303_vendor_write(serial, 9, 0);
733 /* Setup termios */
734 if (tty)
735 pl2303_set_termios(tty, port, NULL);
737 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
738 if (result) {
739 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
740 result);
741 return result;
744 result = usb_serial_generic_open(tty, port);
745 if (result) {
746 usb_kill_urb(port->interrupt_in_urb);
747 return result;
750 return 0;
753 static int pl2303_tiocmset(struct tty_struct *tty,
754 unsigned int set, unsigned int clear)
756 struct usb_serial_port *port = tty->driver_data;
757 struct pl2303_private *priv = usb_get_serial_port_data(port);
758 unsigned long flags;
759 u8 control;
760 int ret;
762 spin_lock_irqsave(&priv->lock, flags);
763 if (set & TIOCM_RTS)
764 priv->line_control |= CONTROL_RTS;
765 if (set & TIOCM_DTR)
766 priv->line_control |= CONTROL_DTR;
767 if (clear & TIOCM_RTS)
768 priv->line_control &= ~CONTROL_RTS;
769 if (clear & TIOCM_DTR)
770 priv->line_control &= ~CONTROL_DTR;
771 control = priv->line_control;
772 spin_unlock_irqrestore(&priv->lock, flags);
774 ret = pl2303_set_control_lines(port, control);
775 if (ret)
776 return usb_translate_errors(ret);
778 return 0;
781 static int pl2303_tiocmget(struct tty_struct *tty)
783 struct usb_serial_port *port = tty->driver_data;
784 struct pl2303_private *priv = usb_get_serial_port_data(port);
785 unsigned long flags;
786 unsigned int mcr;
787 unsigned int status;
788 unsigned int result;
790 spin_lock_irqsave(&priv->lock, flags);
791 mcr = priv->line_control;
792 status = priv->line_status;
793 spin_unlock_irqrestore(&priv->lock, flags);
795 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
796 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
797 | ((status & UART_CTS) ? TIOCM_CTS : 0)
798 | ((status & UART_DSR) ? TIOCM_DSR : 0)
799 | ((status & UART_RING) ? TIOCM_RI : 0)
800 | ((status & UART_DCD) ? TIOCM_CD : 0);
802 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
804 return result;
807 static int pl2303_carrier_raised(struct usb_serial_port *port)
809 struct pl2303_private *priv = usb_get_serial_port_data(port);
811 if (priv->line_status & UART_DCD)
812 return 1;
814 return 0;
817 static int pl2303_get_serial(struct tty_struct *tty,
818 struct serial_struct *ss)
820 struct usb_serial_port *port = tty->driver_data;
822 ss->type = PORT_16654;
823 ss->line = port->minor;
824 ss->port = port->port_number;
825 ss->baud_base = 460800;
826 return 0;
829 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
831 struct usb_serial *serial = port->serial;
832 u16 state;
833 int result;
835 if (enable)
836 state = BREAK_ON;
837 else
838 state = BREAK_OFF;
840 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
841 state == BREAK_OFF ? "off" : "on");
843 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
844 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
845 0, NULL, 0, 100);
846 if (result)
847 dev_err(&port->dev, "error sending break = %d\n", result);
850 static void pl2303_break_ctl(struct tty_struct *tty, int state)
852 struct usb_serial_port *port = tty->driver_data;
854 pl2303_set_break(port, state);
857 static void pl2303_update_line_status(struct usb_serial_port *port,
858 unsigned char *data,
859 unsigned int actual_length)
861 struct usb_serial *serial = port->serial;
862 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
863 struct pl2303_private *priv = usb_get_serial_port_data(port);
864 struct tty_struct *tty;
865 unsigned long flags;
866 unsigned int status_idx = UART_STATE_INDEX;
867 u8 status;
868 u8 delta;
870 if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
871 status_idx = 0;
873 if (actual_length < status_idx + 1)
874 return;
876 status = data[status_idx];
878 /* Save off the uart status for others to look at */
879 spin_lock_irqsave(&priv->lock, flags);
880 delta = priv->line_status ^ status;
881 priv->line_status = status;
882 spin_unlock_irqrestore(&priv->lock, flags);
884 if (status & UART_BREAK_ERROR)
885 usb_serial_handle_break(port);
887 if (delta & UART_STATE_MSR_MASK) {
888 if (delta & UART_CTS)
889 port->icount.cts++;
890 if (delta & UART_DSR)
891 port->icount.dsr++;
892 if (delta & UART_RING)
893 port->icount.rng++;
894 if (delta & UART_DCD) {
895 port->icount.dcd++;
896 tty = tty_port_tty_get(&port->port);
897 if (tty) {
898 usb_serial_handle_dcd_change(port, tty,
899 status & UART_DCD);
900 tty_kref_put(tty);
904 wake_up_interruptible(&port->port.delta_msr_wait);
908 static void pl2303_read_int_callback(struct urb *urb)
910 struct usb_serial_port *port = urb->context;
911 unsigned char *data = urb->transfer_buffer;
912 unsigned int actual_length = urb->actual_length;
913 int status = urb->status;
914 int retval;
916 switch (status) {
917 case 0:
918 /* success */
919 break;
920 case -ECONNRESET:
921 case -ENOENT:
922 case -ESHUTDOWN:
923 /* this urb is terminated, clean up */
924 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
925 __func__, status);
926 return;
927 default:
928 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
929 __func__, status);
930 goto exit;
933 usb_serial_debug_data(&port->dev, __func__,
934 urb->actual_length, urb->transfer_buffer);
936 pl2303_update_line_status(port, data, actual_length);
938 exit:
939 retval = usb_submit_urb(urb, GFP_ATOMIC);
940 if (retval) {
941 dev_err(&port->dev,
942 "%s - usb_submit_urb failed with result %d\n",
943 __func__, retval);
947 static void pl2303_process_read_urb(struct urb *urb)
949 struct usb_serial_port *port = urb->context;
950 struct pl2303_private *priv = usb_get_serial_port_data(port);
951 unsigned char *data = urb->transfer_buffer;
952 char tty_flag = TTY_NORMAL;
953 unsigned long flags;
954 u8 line_status;
955 int i;
957 /* update line status */
958 spin_lock_irqsave(&priv->lock, flags);
959 line_status = priv->line_status;
960 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
961 spin_unlock_irqrestore(&priv->lock, flags);
963 if (!urb->actual_length)
964 return;
967 * Break takes precedence over parity, which takes precedence over
968 * framing errors.
970 if (line_status & UART_BREAK_ERROR)
971 tty_flag = TTY_BREAK;
972 else if (line_status & UART_PARITY_ERROR)
973 tty_flag = TTY_PARITY;
974 else if (line_status & UART_FRAME_ERROR)
975 tty_flag = TTY_FRAME;
977 if (tty_flag != TTY_NORMAL)
978 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
979 tty_flag);
980 /* overrun is special, not associated with a char */
981 if (line_status & UART_OVERRUN_ERROR)
982 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
984 if (port->port.console && port->sysrq) {
985 for (i = 0; i < urb->actual_length; ++i)
986 if (!usb_serial_handle_sysrq_char(port, data[i]))
987 tty_insert_flip_char(&port->port, data[i],
988 tty_flag);
989 } else {
990 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
991 urb->actual_length);
994 tty_flip_buffer_push(&port->port);
997 static struct usb_serial_driver pl2303_device = {
998 .driver = {
999 .owner = THIS_MODULE,
1000 .name = "pl2303",
1002 .id_table = id_table,
1003 .num_bulk_in = 1,
1004 .num_bulk_out = 1,
1005 .num_interrupt_in = 0, /* see pl2303_calc_num_ports */
1006 .bulk_in_size = 256,
1007 .bulk_out_size = 256,
1008 .open = pl2303_open,
1009 .close = pl2303_close,
1010 .dtr_rts = pl2303_dtr_rts,
1011 .carrier_raised = pl2303_carrier_raised,
1012 .get_serial = pl2303_get_serial,
1013 .break_ctl = pl2303_break_ctl,
1014 .set_termios = pl2303_set_termios,
1015 .tiocmget = pl2303_tiocmget,
1016 .tiocmset = pl2303_tiocmset,
1017 .tiocmiwait = usb_serial_generic_tiocmiwait,
1018 .process_read_urb = pl2303_process_read_urb,
1019 .read_int_callback = pl2303_read_int_callback,
1020 .probe = pl2303_probe,
1021 .calc_num_ports = pl2303_calc_num_ports,
1022 .attach = pl2303_startup,
1023 .release = pl2303_release,
1024 .port_probe = pl2303_port_probe,
1025 .port_remove = pl2303_port_remove,
1028 static struct usb_serial_driver * const serial_drivers[] = {
1029 &pl2303_device, NULL
1032 module_usb_serial_driver(serial_drivers, id_table);
1034 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1035 MODULE_LICENSE("GPL v2");