dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / serial / pl2303.c
blob3da25ad267a2631cfe782302866ac39071065d37
1 /*
2 * Prolific PL2303 USB to serial adaptor driver
4 * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2003 IBM Corp.
7 * Original driver for 2.2.x by anonymous
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * See Documentation/usb/usb-serial.txt for more information on using this
14 * driver
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
30 #include <asm/unaligned.h>
31 #include "pl2303.h"
34 #define PL2303_QUIRK_UART_STATE_IDX0 BIT(0)
35 #define PL2303_QUIRK_LEGACY BIT(1)
37 static const struct usb_device_id id_table[] = {
38 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
39 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
40 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
41 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
42 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_CHILITAG) },
43 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
44 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
45 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
46 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
47 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
48 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
49 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
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 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485) },
54 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
55 { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
56 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
57 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
58 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
59 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
60 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
61 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
62 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
63 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
64 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
65 { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
66 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
67 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1),
68 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
69 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65),
70 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
71 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75),
72 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
73 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
74 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
75 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
76 { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
79 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
80 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
81 { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
82 { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
83 { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
84 { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
85 { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
86 { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
87 { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
88 { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
89 { USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
90 { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
91 { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
92 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
93 { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
94 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
95 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
96 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
97 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
98 { } /* Terminating entry */
101 MODULE_DEVICE_TABLE(usb, id_table);
103 #define SET_LINE_REQUEST_TYPE 0x21
104 #define SET_LINE_REQUEST 0x20
106 #define SET_CONTROL_REQUEST_TYPE 0x21
107 #define SET_CONTROL_REQUEST 0x22
108 #define CONTROL_DTR 0x01
109 #define CONTROL_RTS 0x02
111 #define BREAK_REQUEST_TYPE 0x21
112 #define BREAK_REQUEST 0x23
113 #define BREAK_ON 0xffff
114 #define BREAK_OFF 0x0000
116 #define GET_LINE_REQUEST_TYPE 0xa1
117 #define GET_LINE_REQUEST 0x21
119 #define VENDOR_WRITE_REQUEST_TYPE 0x40
120 #define VENDOR_WRITE_REQUEST 0x01
122 #define VENDOR_READ_REQUEST_TYPE 0xc0
123 #define VENDOR_READ_REQUEST 0x01
125 #define UART_STATE_INDEX 8
126 #define UART_STATE_MSR_MASK 0x8b
127 #define UART_STATE_TRANSIENT_MASK 0x74
128 #define UART_DCD 0x01
129 #define UART_DSR 0x02
130 #define UART_BREAK_ERROR 0x04
131 #define UART_RING 0x08
132 #define UART_FRAME_ERROR 0x10
133 #define UART_PARITY_ERROR 0x20
134 #define UART_OVERRUN_ERROR 0x40
135 #define UART_CTS 0x80
137 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
139 enum pl2303_type {
140 TYPE_01, /* Type 0 and 1 (difference unknown) */
141 TYPE_HX, /* HX version of the pl2303 chip */
142 TYPE_COUNT
145 struct pl2303_type_data {
146 speed_t max_baud_rate;
147 unsigned long quirks;
150 struct pl2303_serial_private {
151 const struct pl2303_type_data *type;
152 unsigned long quirks;
155 struct pl2303_private {
156 spinlock_t lock;
157 u8 line_control;
158 u8 line_status;
160 u8 line_settings[7];
163 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
164 [TYPE_01] = {
165 .max_baud_rate = 1228800,
166 .quirks = PL2303_QUIRK_LEGACY,
168 [TYPE_HX] = {
169 .max_baud_rate = 12000000,
173 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
174 unsigned char buf[1])
176 struct device *dev = &serial->interface->dev;
177 int res;
179 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
180 VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
181 value, 0, buf, 1, 100);
182 if (res != 1) {
183 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
184 value, res);
185 if (res >= 0)
186 res = -EIO;
188 return res;
191 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
193 return 0;
196 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
198 struct device *dev = &serial->interface->dev;
199 int res;
201 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
203 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
204 VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
205 value, index, NULL, 0, 100);
206 if (res) {
207 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
208 value, res);
209 return res;
212 return 0;
215 static int pl2303_probe(struct usb_serial *serial,
216 const struct usb_device_id *id)
218 usb_set_serial_data(serial, (void *)id->driver_info);
220 return 0;
223 static int pl2303_startup(struct usb_serial *serial)
225 struct pl2303_serial_private *spriv;
226 unsigned char num_ports = serial->num_ports;
227 enum pl2303_type type = TYPE_01;
228 unsigned char *buf;
230 if (serial->num_bulk_in < num_ports ||
231 serial->num_bulk_out < num_ports ||
232 serial->num_interrupt_in < num_ports) {
233 dev_err(&serial->interface->dev, "missing endpoints\n");
234 return -ENODEV;
237 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
238 if (!spriv)
239 return -ENOMEM;
241 buf = kmalloc(1, GFP_KERNEL);
242 if (!buf) {
243 kfree(spriv);
244 return -ENOMEM;
247 if (serial->dev->descriptor.bDeviceClass == 0x02)
248 type = TYPE_01; /* type 0 */
249 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
250 type = TYPE_HX;
251 else if (serial->dev->descriptor.bDeviceClass == 0x00)
252 type = TYPE_01; /* type 1 */
253 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
254 type = TYPE_01; /* type 1 */
255 dev_dbg(&serial->interface->dev, "device type: %d\n", type);
257 spriv->type = &pl2303_type_data[type];
258 spriv->quirks = (unsigned long)usb_get_serial_data(serial);
259 spriv->quirks |= spriv->type->quirks;
261 usb_set_serial_data(serial, spriv);
263 pl2303_vendor_read(serial, 0x8484, buf);
264 pl2303_vendor_write(serial, 0x0404, 0);
265 pl2303_vendor_read(serial, 0x8484, buf);
266 pl2303_vendor_read(serial, 0x8383, buf);
267 pl2303_vendor_read(serial, 0x8484, buf);
268 pl2303_vendor_write(serial, 0x0404, 1);
269 pl2303_vendor_read(serial, 0x8484, buf);
270 pl2303_vendor_read(serial, 0x8383, buf);
271 pl2303_vendor_write(serial, 0, 1);
272 pl2303_vendor_write(serial, 1, 0);
273 if (spriv->quirks & PL2303_QUIRK_LEGACY)
274 pl2303_vendor_write(serial, 2, 0x24);
275 else
276 pl2303_vendor_write(serial, 2, 0x44);
278 kfree(buf);
280 return 0;
283 static void pl2303_release(struct usb_serial *serial)
285 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
287 kfree(spriv);
290 static int pl2303_port_probe(struct usb_serial_port *port)
292 struct pl2303_private *priv;
294 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
295 if (!priv)
296 return -ENOMEM;
298 spin_lock_init(&priv->lock);
300 usb_set_serial_port_data(port, priv);
302 port->port.drain_delay = 256;
304 return 0;
307 static int pl2303_port_remove(struct usb_serial_port *port)
309 struct pl2303_private *priv = usb_get_serial_port_data(port);
311 kfree(priv);
313 return 0;
316 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
318 struct usb_device *dev = port->serial->dev;
319 int retval;
321 dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
323 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
324 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
325 value, 0, NULL, 0, 100);
326 if (retval)
327 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
329 return retval;
333 * Returns the nearest supported baud rate that can be set directly without
334 * using divisors.
336 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
338 static const speed_t baud_sup[] = {
339 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
340 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
341 614400, 921600, 1228800, 2457600, 3000000, 6000000
344 unsigned i;
346 for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
347 if (baud_sup[i] > baud)
348 break;
351 if (i == ARRAY_SIZE(baud_sup))
352 baud = baud_sup[i - 1];
353 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
354 baud = baud_sup[i - 1];
355 else
356 baud = baud_sup[i];
358 return baud;
362 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
363 * use 9600 baud.
365 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
366 speed_t baud)
368 put_unaligned_le32(baud, buf);
370 return baud;
373 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
374 speed_t baud)
376 unsigned int baseline, mantissa, exponent;
379 * Apparently the formula is:
380 * baudrate = 12M * 32 / (mantissa * 4^exponent)
381 * where
382 * mantissa = buf[8:0]
383 * exponent = buf[11:9]
385 baseline = 12000000 * 32;
386 mantissa = baseline / baud;
387 if (mantissa == 0)
388 mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
389 exponent = 0;
390 while (mantissa >= 512) {
391 if (exponent < 7) {
392 mantissa >>= 2; /* divide by 4 */
393 exponent++;
394 } else {
395 /* Exponent is maxed. Trim mantissa and leave. */
396 mantissa = 511;
397 break;
401 buf[3] = 0x80;
402 buf[2] = 0;
403 buf[1] = exponent << 1 | mantissa >> 8;
404 buf[0] = mantissa & 0xff;
406 /* Calculate and return the exact baud rate. */
407 baud = (baseline / mantissa) >> (exponent << 1);
409 return baud;
412 static void pl2303_encode_baud_rate(struct tty_struct *tty,
413 struct usb_serial_port *port,
414 u8 buf[4])
416 struct usb_serial *serial = port->serial;
417 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
418 speed_t baud_sup;
419 speed_t baud;
421 baud = tty_get_baud_rate(tty);
422 dev_dbg(&port->dev, "baud requested = %u\n", baud);
423 if (!baud)
424 return;
426 if (spriv->type->max_baud_rate)
427 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
429 * Use direct method for supported baud rates, otherwise use divisors.
431 baud_sup = pl2303_get_supported_baud_rate(baud);
433 if (baud == baud_sup)
434 baud = pl2303_encode_baud_rate_direct(buf, baud);
435 else
436 baud = pl2303_encode_baud_rate_divisor(buf, baud);
438 /* Save resulting baud rate */
439 tty_encode_baud_rate(tty, baud, baud);
440 dev_dbg(&port->dev, "baud set = %u\n", baud);
443 static int pl2303_get_line_request(struct usb_serial_port *port,
444 unsigned char buf[7])
446 struct usb_device *udev = port->serial->dev;
447 int ret;
449 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
450 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
451 0, 0, buf, 7, 100);
452 if (ret != 7) {
453 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
455 if (ret > 0)
456 ret = -EIO;
458 return ret;
461 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
463 return 0;
466 static int pl2303_set_line_request(struct usb_serial_port *port,
467 unsigned char buf[7])
469 struct usb_device *udev = port->serial->dev;
470 int ret;
472 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
473 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
474 0, 0, buf, 7, 100);
475 if (ret != 7) {
476 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
478 if (ret > 0)
479 ret = -EIO;
481 return ret;
484 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
486 return 0;
489 static void pl2303_set_termios(struct tty_struct *tty,
490 struct usb_serial_port *port, struct ktermios *old_termios)
492 struct usb_serial *serial = port->serial;
493 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
494 struct pl2303_private *priv = usb_get_serial_port_data(port);
495 unsigned long flags;
496 unsigned char *buf;
497 int ret;
498 u8 control;
500 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
501 return;
503 buf = kzalloc(7, GFP_KERNEL);
504 if (!buf) {
505 /* Report back no change occurred */
506 if (old_termios)
507 tty->termios = *old_termios;
508 return;
511 pl2303_get_line_request(port, buf);
513 switch (C_CSIZE(tty)) {
514 case CS5:
515 buf[6] = 5;
516 break;
517 case CS6:
518 buf[6] = 6;
519 break;
520 case CS7:
521 buf[6] = 7;
522 break;
523 default:
524 case CS8:
525 buf[6] = 8;
527 dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
529 /* For reference buf[0]:buf[3] baud rate value */
530 pl2303_encode_baud_rate(tty, port, &buf[0]);
532 /* For reference buf[4]=0 is 1 stop bits */
533 /* For reference buf[4]=1 is 1.5 stop bits */
534 /* For reference buf[4]=2 is 2 stop bits */
535 if (C_CSTOPB(tty)) {
537 * NOTE: Comply with "real" UARTs / RS232:
538 * use 1.5 instead of 2 stop bits with 5 data bits
540 if (C_CSIZE(tty) == CS5) {
541 buf[4] = 1;
542 dev_dbg(&port->dev, "stop bits = 1.5\n");
543 } else {
544 buf[4] = 2;
545 dev_dbg(&port->dev, "stop bits = 2\n");
547 } else {
548 buf[4] = 0;
549 dev_dbg(&port->dev, "stop bits = 1\n");
552 if (C_PARENB(tty)) {
553 /* For reference buf[5]=0 is none parity */
554 /* For reference buf[5]=1 is odd parity */
555 /* For reference buf[5]=2 is even parity */
556 /* For reference buf[5]=3 is mark parity */
557 /* For reference buf[5]=4 is space parity */
558 if (C_PARODD(tty)) {
559 if (C_CMSPAR(tty)) {
560 buf[5] = 3;
561 dev_dbg(&port->dev, "parity = mark\n");
562 } else {
563 buf[5] = 1;
564 dev_dbg(&port->dev, "parity = odd\n");
566 } else {
567 if (C_CMSPAR(tty)) {
568 buf[5] = 4;
569 dev_dbg(&port->dev, "parity = space\n");
570 } else {
571 buf[5] = 2;
572 dev_dbg(&port->dev, "parity = even\n");
575 } else {
576 buf[5] = 0;
577 dev_dbg(&port->dev, "parity = none\n");
581 * Some PL2303 are known to lose bytes if you change serial settings
582 * even to the same values as before. Thus we actually need to filter
583 * in this specific case.
585 * Note that the tty_termios_hw_change check above is not sufficient
586 * as a previously requested baud rate may differ from the one
587 * actually used (and stored in old_termios).
589 * NOTE: No additional locking needed for line_settings as it is
590 * only used in set_termios, which is serialised against itself.
592 if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
593 ret = pl2303_set_line_request(port, buf);
594 if (!ret)
595 memcpy(priv->line_settings, buf, 7);
598 /* change control lines if we are switching to or from B0 */
599 spin_lock_irqsave(&priv->lock, flags);
600 control = priv->line_control;
601 if (C_BAUD(tty) == B0)
602 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
603 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
604 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
605 if (control != priv->line_control) {
606 control = priv->line_control;
607 spin_unlock_irqrestore(&priv->lock, flags);
608 pl2303_set_control_lines(port, control);
609 } else {
610 spin_unlock_irqrestore(&priv->lock, flags);
613 if (C_CRTSCTS(tty)) {
614 if (spriv->quirks & PL2303_QUIRK_LEGACY)
615 pl2303_vendor_write(serial, 0x0, 0x41);
616 else
617 pl2303_vendor_write(serial, 0x0, 0x61);
618 } else {
619 pl2303_vendor_write(serial, 0x0, 0x0);
622 kfree(buf);
625 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
627 struct pl2303_private *priv = usb_get_serial_port_data(port);
628 unsigned long flags;
629 u8 control;
631 spin_lock_irqsave(&priv->lock, flags);
632 if (on)
633 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
634 else
635 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
636 control = priv->line_control;
637 spin_unlock_irqrestore(&priv->lock, flags);
639 pl2303_set_control_lines(port, control);
642 static void pl2303_close(struct usb_serial_port *port)
644 usb_serial_generic_close(port);
645 usb_kill_urb(port->interrupt_in_urb);
646 pl2303_set_break(port, false);
649 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
651 struct usb_serial *serial = port->serial;
652 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
653 int result;
655 if (spriv->quirks & PL2303_QUIRK_LEGACY) {
656 usb_clear_halt(serial->dev, port->write_urb->pipe);
657 usb_clear_halt(serial->dev, port->read_urb->pipe);
658 } else {
659 /* reset upstream data pipes */
660 pl2303_vendor_write(serial, 8, 0);
661 pl2303_vendor_write(serial, 9, 0);
664 /* Setup termios */
665 if (tty)
666 pl2303_set_termios(tty, port, NULL);
668 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
669 if (result) {
670 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
671 result);
672 return result;
675 result = usb_serial_generic_open(tty, port);
676 if (result) {
677 usb_kill_urb(port->interrupt_in_urb);
678 return result;
681 return 0;
684 static int pl2303_tiocmset(struct tty_struct *tty,
685 unsigned int set, unsigned int clear)
687 struct usb_serial_port *port = tty->driver_data;
688 struct pl2303_private *priv = usb_get_serial_port_data(port);
689 unsigned long flags;
690 u8 control;
691 int ret;
693 spin_lock_irqsave(&priv->lock, flags);
694 if (set & TIOCM_RTS)
695 priv->line_control |= CONTROL_RTS;
696 if (set & TIOCM_DTR)
697 priv->line_control |= CONTROL_DTR;
698 if (clear & TIOCM_RTS)
699 priv->line_control &= ~CONTROL_RTS;
700 if (clear & TIOCM_DTR)
701 priv->line_control &= ~CONTROL_DTR;
702 control = priv->line_control;
703 spin_unlock_irqrestore(&priv->lock, flags);
705 ret = pl2303_set_control_lines(port, control);
706 if (ret)
707 return usb_translate_errors(ret);
709 return 0;
712 static int pl2303_tiocmget(struct tty_struct *tty)
714 struct usb_serial_port *port = tty->driver_data;
715 struct pl2303_private *priv = usb_get_serial_port_data(port);
716 unsigned long flags;
717 unsigned int mcr;
718 unsigned int status;
719 unsigned int result;
721 spin_lock_irqsave(&priv->lock, flags);
722 mcr = priv->line_control;
723 status = priv->line_status;
724 spin_unlock_irqrestore(&priv->lock, flags);
726 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
727 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
728 | ((status & UART_CTS) ? TIOCM_CTS : 0)
729 | ((status & UART_DSR) ? TIOCM_DSR : 0)
730 | ((status & UART_RING) ? TIOCM_RI : 0)
731 | ((status & UART_DCD) ? TIOCM_CD : 0);
733 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
735 return result;
738 static int pl2303_carrier_raised(struct usb_serial_port *port)
740 struct pl2303_private *priv = usb_get_serial_port_data(port);
742 if (priv->line_status & UART_DCD)
743 return 1;
745 return 0;
748 static int pl2303_ioctl(struct tty_struct *tty,
749 unsigned int cmd, unsigned long arg)
751 struct serial_struct ser;
752 struct usb_serial_port *port = tty->driver_data;
754 switch (cmd) {
755 case TIOCGSERIAL:
756 memset(&ser, 0, sizeof ser);
757 ser.type = PORT_16654;
758 ser.line = port->minor;
759 ser.port = port->port_number;
760 ser.baud_base = 460800;
762 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
763 return -EFAULT;
765 return 0;
766 default:
767 break;
770 return -ENOIOCTLCMD;
773 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
775 struct usb_serial *serial = port->serial;
776 u16 state;
777 int result;
779 if (enable)
780 state = BREAK_ON;
781 else
782 state = BREAK_OFF;
784 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
785 state == BREAK_OFF ? "off" : "on");
787 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
788 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
789 0, NULL, 0, 100);
790 if (result)
791 dev_err(&port->dev, "error sending break = %d\n", result);
794 static void pl2303_break_ctl(struct tty_struct *tty, int state)
796 struct usb_serial_port *port = tty->driver_data;
798 pl2303_set_break(port, state);
801 static void pl2303_update_line_status(struct usb_serial_port *port,
802 unsigned char *data,
803 unsigned int actual_length)
805 struct usb_serial *serial = port->serial;
806 struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
807 struct pl2303_private *priv = usb_get_serial_port_data(port);
808 struct tty_struct *tty;
809 unsigned long flags;
810 unsigned int status_idx = UART_STATE_INDEX;
811 u8 status;
812 u8 delta;
814 if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
815 status_idx = 0;
817 if (actual_length < status_idx + 1)
818 return;
820 status = data[status_idx];
822 /* Save off the uart status for others to look at */
823 spin_lock_irqsave(&priv->lock, flags);
824 delta = priv->line_status ^ status;
825 priv->line_status = status;
826 spin_unlock_irqrestore(&priv->lock, flags);
828 if (status & UART_BREAK_ERROR)
829 usb_serial_handle_break(port);
831 if (delta & UART_STATE_MSR_MASK) {
832 if (delta & UART_CTS)
833 port->icount.cts++;
834 if (delta & UART_DSR)
835 port->icount.dsr++;
836 if (delta & UART_RING)
837 port->icount.rng++;
838 if (delta & UART_DCD) {
839 port->icount.dcd++;
840 tty = tty_port_tty_get(&port->port);
841 if (tty) {
842 usb_serial_handle_dcd_change(port, tty,
843 status & UART_DCD);
844 tty_kref_put(tty);
848 wake_up_interruptible(&port->port.delta_msr_wait);
852 static void pl2303_read_int_callback(struct urb *urb)
854 struct usb_serial_port *port = urb->context;
855 unsigned char *data = urb->transfer_buffer;
856 unsigned int actual_length = urb->actual_length;
857 int status = urb->status;
858 int retval;
860 switch (status) {
861 case 0:
862 /* success */
863 break;
864 case -ECONNRESET:
865 case -ENOENT:
866 case -ESHUTDOWN:
867 /* this urb is terminated, clean up */
868 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
869 __func__, status);
870 return;
871 default:
872 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
873 __func__, status);
874 goto exit;
877 usb_serial_debug_data(&port->dev, __func__,
878 urb->actual_length, urb->transfer_buffer);
880 pl2303_update_line_status(port, data, actual_length);
882 exit:
883 retval = usb_submit_urb(urb, GFP_ATOMIC);
884 if (retval) {
885 dev_err(&port->dev,
886 "%s - usb_submit_urb failed with result %d\n",
887 __func__, retval);
891 static void pl2303_process_read_urb(struct urb *urb)
893 struct usb_serial_port *port = urb->context;
894 struct pl2303_private *priv = usb_get_serial_port_data(port);
895 unsigned char *data = urb->transfer_buffer;
896 char tty_flag = TTY_NORMAL;
897 unsigned long flags;
898 u8 line_status;
899 int i;
901 /* update line status */
902 spin_lock_irqsave(&priv->lock, flags);
903 line_status = priv->line_status;
904 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
905 spin_unlock_irqrestore(&priv->lock, flags);
907 if (!urb->actual_length)
908 return;
911 * Break takes precedence over parity, which takes precedence over
912 * framing errors.
914 if (line_status & UART_BREAK_ERROR)
915 tty_flag = TTY_BREAK;
916 else if (line_status & UART_PARITY_ERROR)
917 tty_flag = TTY_PARITY;
918 else if (line_status & UART_FRAME_ERROR)
919 tty_flag = TTY_FRAME;
921 if (tty_flag != TTY_NORMAL)
922 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
923 tty_flag);
924 /* overrun is special, not associated with a char */
925 if (line_status & UART_OVERRUN_ERROR)
926 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
928 if (port->port.console && port->sysrq) {
929 for (i = 0; i < urb->actual_length; ++i)
930 if (!usb_serial_handle_sysrq_char(port, data[i]))
931 tty_insert_flip_char(&port->port, data[i],
932 tty_flag);
933 } else {
934 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
935 urb->actual_length);
938 tty_flip_buffer_push(&port->port);
941 static struct usb_serial_driver pl2303_device = {
942 .driver = {
943 .owner = THIS_MODULE,
944 .name = "pl2303",
946 .id_table = id_table,
947 .num_ports = 1,
948 .bulk_in_size = 256,
949 .bulk_out_size = 256,
950 .open = pl2303_open,
951 .close = pl2303_close,
952 .dtr_rts = pl2303_dtr_rts,
953 .carrier_raised = pl2303_carrier_raised,
954 .ioctl = pl2303_ioctl,
955 .break_ctl = pl2303_break_ctl,
956 .set_termios = pl2303_set_termios,
957 .tiocmget = pl2303_tiocmget,
958 .tiocmset = pl2303_tiocmset,
959 .tiocmiwait = usb_serial_generic_tiocmiwait,
960 .process_read_urb = pl2303_process_read_urb,
961 .read_int_callback = pl2303_read_int_callback,
962 .probe = pl2303_probe,
963 .attach = pl2303_startup,
964 .release = pl2303_release,
965 .port_probe = pl2303_port_probe,
966 .port_remove = pl2303_port_remove,
969 static struct usb_serial_driver * const serial_drivers[] = {
970 &pl2303_device, NULL
973 module_usb_serial_driver(serial_drivers, id_table);
975 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
976 MODULE_LICENSE("GPL");