1 // SPDX-License-Identifier: GPL-2.0
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
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>
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
);
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 */
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
{
199 static const struct pl2303_type_data pl2303_type_data
[TYPE_COUNT
] = {
201 .max_baud_rate
= 1228800,
202 .quirks
= PL2303_QUIRK_LEGACY
,
203 .no_autoxonxoff
= true,
206 .max_baud_rate
= 12000000,
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
;
221 if (spriv
->type
== &pl2303_type_data
[TYPE_HXN
])
222 request
= VENDOR_READ_NREQUEST
;
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);
230 dev_err(dev
, "%s - failed to read [%04x]: %d\n", __func__
,
238 dev_dbg(dev
, "%s - [%04x] = %02x\n", __func__
, value
, buf
[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
;
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
;
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);
261 dev_err(dev
, "%s - failed to write [%04x]: %d\n", __func__
,
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
);
275 buf
= kmalloc(1, GFP_KERNEL
);
279 if (spriv
->type
== &pl2303_type_data
[TYPE_HXN
])
280 ret
= pl2303_vendor_read(serial
, reg
, buf
);
282 ret
= pl2303_vendor_read(serial
, reg
| 0x80, buf
);
290 ret
= pl2303_vendor_write(serial
, reg
, *buf
);
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
);
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
;
320 if (interface
== dev
->actconfig
->interface
[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
))
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
;
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
;
347 if (quirks
& PL2303_QUIRK_ENDPOINT_HACK
) {
348 ret
= pl2303_endpoint_hack(serial
, epds
);
353 if (epds
->num_interrupt_in
< 1) {
354 dev_err(dev
, "required interrupt-in endpoint missing\n");
361 static int pl2303_startup(struct usb_serial
*serial
)
363 struct pl2303_serial_private
*spriv
;
364 enum pl2303_type type
= TYPE_01
;
368 spriv
= kzalloc(sizeof(*spriv
), GFP_KERNEL
);
372 buf
= kmalloc(1, GFP_KERNEL
);
378 if (serial
->dev
->descriptor
.bDeviceClass
== 0x02)
379 type
= TYPE_01
; /* type 0 */
380 else if (serial
->dev
->descriptor
.bMaxPacketSize0
== 0x40)
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);
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);
417 pl2303_vendor_write(serial
, 2, 0x44);
425 static void pl2303_release(struct usb_serial
*serial
)
427 struct pl2303_serial_private
*spriv
= usb_get_serial_data(serial
);
432 static int pl2303_port_probe(struct usb_serial_port
*port
)
434 struct pl2303_private
*priv
;
436 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
440 spin_lock_init(&priv
->lock
);
442 usb_set_serial_port_data(port
, priv
);
444 port
->port
.drain_delay
= 256;
449 static int pl2303_port_remove(struct usb_serial_port
*port
)
451 struct pl2303_private
*priv
= usb_get_serial_port_data(port
);
458 static int pl2303_set_control_lines(struct usb_serial_port
*port
, u8 value
)
460 struct usb_device
*dev
= port
->serial
->dev
;
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);
469 dev_err(&port
->dev
, "%s - failed: %d\n", __func__
, retval
);
475 * Returns the nearest supported baud rate that can be set directly without
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
488 for (i
= 0; i
< ARRAY_SIZE(baud_sup
); ++i
) {
489 if (baud_sup
[i
] > baud
)
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];
504 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
507 static speed_t
pl2303_encode_baud_rate_direct(unsigned char buf
[4],
510 put_unaligned_le32(baud
, buf
);
515 static speed_t
pl2303_encode_baud_rate_divisor(unsigned char buf
[4],
518 unsigned int baseline
, mantissa
, exponent
;
521 * Apparently the formula is:
522 * baudrate = 12M * 32 / (mantissa * 4^exponent)
524 * mantissa = buf[8:0]
525 * exponent = buf[11:9]
527 baseline
= 12000000 * 32;
528 mantissa
= baseline
/ baud
;
530 mantissa
= 1; /* Avoid dividing by zero if baud > 32*12M. */
532 while (mantissa
>= 512) {
534 mantissa
>>= 2; /* divide by 4 */
537 /* Exponent is maxed. Trim mantissa and leave. */
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);
554 static void pl2303_encode_baud_rate(struct tty_struct
*tty
,
555 struct usb_serial_port
*port
,
558 struct usb_serial
*serial
= port
->serial
;
559 struct pl2303_serial_private
*spriv
= usb_get_serial_data(serial
);
563 baud
= tty_get_baud_rate(tty
);
564 dev_dbg(&port
->dev
, "baud requested = %u\n", baud
);
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
);
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
;
591 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
592 GET_LINE_REQUEST
, GET_LINE_REQUEST_TYPE
,
595 dev_err(&port
->dev
, "%s - failed: %d\n", __func__
, ret
);
603 dev_dbg(&port
->dev
, "%s - %7ph\n", __func__
, buf
);
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
;
614 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
615 SET_LINE_REQUEST
, SET_LINE_REQUEST_TYPE
,
618 dev_err(&port
->dev
, "%s - failed: %d\n", __func__
, ret
);
622 dev_dbg(&port
->dev
, "%s - %7ph\n", __func__
, buf
);
627 static bool pl2303_termios_change(const struct ktermios
*a
, const struct ktermios
*b
)
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
))
643 if (START_CHAR(tty
) != 0x11 || STOP_CHAR(tty
) != 0x13)
646 if (type
->no_autoxonxoff
)
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
);
663 if (old_termios
&& !pl2303_termios_change(&tty
->termios
, old_termios
))
666 buf
= kzalloc(7, GFP_KERNEL
);
668 /* Report back no change occurred */
670 tty
->termios
= *old_termios
;
674 pl2303_get_line_request(port
, buf
);
676 switch (C_CSIZE(tty
)) {
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 */
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
) {
705 dev_dbg(&port
->dev
, "stop bits = 1.5\n");
708 dev_dbg(&port
->dev
, "stop bits = 2\n");
712 dev_dbg(&port
->dev
, "stop bits = 1\n");
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 */
724 dev_dbg(&port
->dev
, "parity = mark\n");
727 dev_dbg(&port
->dev
, "parity = odd\n");
732 dev_dbg(&port
->dev
, "parity = space\n");
735 dev_dbg(&port
->dev
, "parity = even\n");
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
);
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
);
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
);
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
);
792 pl2303_update_reg(serial
, 0, PL2303_FLOWCTRL_MASK
, 0xc0);
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
);
800 pl2303_update_reg(serial
, 0, PL2303_FLOWCTRL_MASK
, 0);
807 static void pl2303_dtr_rts(struct usb_serial_port
*port
, int on
)
809 struct pl2303_private
*priv
= usb_get_serial_port_data(port
);
813 spin_lock_irqsave(&priv
->lock
, flags
);
815 priv
->line_control
|= (CONTROL_DTR
| CONTROL_RTS
);
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
);
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
);
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
);
847 pl2303_vendor_write(serial
, 8, 0);
848 pl2303_vendor_write(serial
, 9, 0);
854 pl2303_set_termios(tty
, port
, NULL
);
856 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
858 dev_err(&port
->dev
, "failed to submit interrupt urb: %d\n",
863 result
= usb_serial_generic_open(tty
, port
);
865 usb_kill_urb(port
->interrupt_in_urb
);
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
);
881 spin_lock_irqsave(&priv
->lock
, flags
);
883 priv
->line_control
|= CONTROL_RTS
;
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
);
895 return usb_translate_errors(ret
);
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
);
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
);
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
)
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;
948 static void pl2303_set_break(struct usb_serial_port
*port
, bool enable
)
950 struct usb_serial
*serial
= port
->serial
;
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
,
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
,
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
;
985 unsigned int status_idx
= UART_STATE_INDEX
;
989 if (spriv
->quirks
& PL2303_QUIRK_UART_STATE_IDX0
)
992 if (actual_length
< status_idx
+ 1)
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
)
1009 if (delta
& UART_DSR
)
1011 if (delta
& UART_RING
)
1013 if (delta
& UART_DCD
) {
1015 tty
= tty_port_tty_get(&port
->port
);
1017 usb_serial_handle_dcd_change(port
, 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
;
1042 /* this urb is terminated, clean up */
1043 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
1047 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
1052 usb_serial_debug_data(&port
->dev
, __func__
,
1053 urb
->actual_length
, urb
->transfer_buffer
);
1055 pl2303_update_line_status(port
, data
, actual_length
);
1058 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1061 "%s - usb_submit_urb failed with result %d\n",
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
;
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
)
1086 * Break takes precedence over parity, which takes precedence over
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__
,
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
],
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
= {
1118 .owner
= THIS_MODULE
,
1121 .id_table
= id_table
,
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");