hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/usb/serial/pl2303.c
....@@ -7,7 +7,7 @@
77 *
88 * Original driver for 2.2.x by anonymous
99 *
10
- * See Documentation/usb/usb-serial.txt for more information on using this
10
+ * See Documentation/usb/usb-serial.rst for more information on using this
1111 * driver
1212 */
1313
....@@ -47,6 +47,12 @@
4747 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
4848 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
4949 { 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) },
5056 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
5157 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
5258 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID),
....@@ -100,6 +106,7 @@
100106 { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
101107 { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
102108 { USB_DEVICE(HP_VENDOR_ID, HP_LM920_PRODUCT_ID) },
109
+ { USB_DEVICE(HP_VENDOR_ID, HP_LM930_PRODUCT_ID) },
103110 { USB_DEVICE(HP_VENDOR_ID, HP_LM940_PRODUCT_ID) },
104111 { USB_DEVICE(HP_VENDOR_ID, HP_TD620_PRODUCT_ID) },
105112 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
....@@ -110,6 +117,7 @@
110117 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530GC_PRODUCT_ID) },
111118 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
112119 { USB_DEVICE(AT_VENDOR_ID, AT_VTKIT3_PRODUCT_ID) },
120
+ { USB_DEVICE(IBM_VENDOR_ID, IBM_PRODUCT_ID) },
113121 { } /* Terminating entry */
114122 };
115123
....@@ -133,9 +141,11 @@
133141
134142 #define VENDOR_WRITE_REQUEST_TYPE 0x40
135143 #define VENDOR_WRITE_REQUEST 0x01
144
+#define VENDOR_WRITE_NREQUEST 0x80
136145
137146 #define VENDOR_READ_REQUEST_TYPE 0xc0
138147 #define VENDOR_READ_REQUEST 0x01
148
+#define VENDOR_READ_NREQUEST 0x81
139149
140150 #define UART_STATE_INDEX 8
141151 #define UART_STATE_MSR_MASK 0x8b
....@@ -149,17 +159,34 @@
149159 #define UART_OVERRUN_ERROR 0x40
150160 #define UART_CTS 0x80
151161
162
+#define PL2303_FLOWCTRL_MASK 0xf0
163
+
164
+#define PL2303_READ_TYPE_HX_STATUS 0x8080
165
+
166
+#define PL2303_HXN_RESET_REG 0x07
167
+#define PL2303_HXN_RESET_UPSTREAM_PIPE 0x02
168
+#define PL2303_HXN_RESET_DOWNSTREAM_PIPE 0x01
169
+
170
+#define PL2303_HXN_FLOWCTRL_REG 0x0a
171
+#define PL2303_HXN_FLOWCTRL_MASK 0x1c
172
+#define PL2303_HXN_FLOWCTRL_NONE 0x1c
173
+#define PL2303_HXN_FLOWCTRL_RTS_CTS 0x18
174
+#define PL2303_HXN_FLOWCTRL_XON_XOFF 0x0c
175
+
152176 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
153177
154178 enum pl2303_type {
155179 TYPE_01, /* Type 0 and 1 (difference unknown) */
156180 TYPE_HX, /* HX version of the pl2303 chip */
181
+ TYPE_HXN, /* HXN version of the pl2303 chip */
157182 TYPE_COUNT
158183 };
159184
160185 struct pl2303_type_data {
161186 speed_t max_baud_rate;
162187 unsigned long quirks;
188
+ unsigned int no_autoxonxoff:1;
189
+ unsigned int no_divisors:1;
163190 };
164191
165192 struct pl2303_serial_private {
....@@ -177,22 +204,34 @@
177204
178205 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
179206 [TYPE_01] = {
180
- .max_baud_rate = 1228800,
181
- .quirks = PL2303_QUIRK_LEGACY,
207
+ .max_baud_rate = 1228800,
208
+ .quirks = PL2303_QUIRK_LEGACY,
209
+ .no_autoxonxoff = true,
182210 },
183211 [TYPE_HX] = {
184
- .max_baud_rate = 12000000,
212
+ .max_baud_rate = 12000000,
213
+ },
214
+ [TYPE_HXN] = {
215
+ .max_baud_rate = 12000000,
216
+ .no_divisors = true,
185217 },
186218 };
187219
188220 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
189221 unsigned char buf[1])
190222 {
223
+ struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
191224 struct device *dev = &serial->interface->dev;
225
+ u8 request;
192226 int res;
193227
228
+ if (spriv->type == &pl2303_type_data[TYPE_HXN])
229
+ request = VENDOR_READ_NREQUEST;
230
+ else
231
+ request = VENDOR_READ_REQUEST;
232
+
194233 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
195
- VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
234
+ request, VENDOR_READ_REQUEST_TYPE,
196235 value, 0, buf, 1, 100);
197236 if (res != 1) {
198237 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
....@@ -210,13 +249,20 @@
210249
211250 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
212251 {
252
+ struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
213253 struct device *dev = &serial->interface->dev;
254
+ u8 request;
214255 int res;
215256
216257 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
217258
259
+ if (spriv->type == &pl2303_type_data[TYPE_HXN])
260
+ request = VENDOR_WRITE_NREQUEST;
261
+ else
262
+ request = VENDOR_WRITE_REQUEST;
263
+
218264 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
219
- VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
265
+ request, VENDOR_WRITE_REQUEST_TYPE,
220266 value, index, NULL, 0, 100);
221267 if (res) {
222268 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
....@@ -225,6 +271,34 @@
225271 }
226272
227273 return 0;
274
+}
275
+
276
+static int pl2303_update_reg(struct usb_serial *serial, u8 reg, u8 mask, u8 val)
277
+{
278
+ struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
279
+ int ret = 0;
280
+ u8 *buf;
281
+
282
+ buf = kmalloc(1, GFP_KERNEL);
283
+ if (!buf)
284
+ return -ENOMEM;
285
+
286
+ if (spriv->type == &pl2303_type_data[TYPE_HXN])
287
+ ret = pl2303_vendor_read(serial, reg, buf);
288
+ else
289
+ ret = pl2303_vendor_read(serial, reg | 0x80, buf);
290
+
291
+ if (ret)
292
+ goto out_free;
293
+
294
+ *buf &= ~mask;
295
+ *buf |= val & mask;
296
+
297
+ ret = pl2303_vendor_write(serial, reg, *buf);
298
+out_free:
299
+ kfree(buf);
300
+
301
+ return ret;
228302 }
229303
230304 static int pl2303_probe(struct usb_serial *serial,
....@@ -296,6 +370,7 @@
296370 struct pl2303_serial_private *spriv;
297371 enum pl2303_type type = TYPE_01;
298372 unsigned char *buf;
373
+ int res;
299374
300375 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
301376 if (!spriv)
....@@ -317,26 +392,37 @@
317392 type = TYPE_01; /* type 1 */
318393 dev_dbg(&serial->interface->dev, "device type: %d\n", type);
319394
395
+ if (type == TYPE_HX) {
396
+ res = usb_control_msg(serial->dev,
397
+ usb_rcvctrlpipe(serial->dev, 0),
398
+ VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
399
+ PL2303_READ_TYPE_HX_STATUS, 0, buf, 1, 100);
400
+ if (res != 1)
401
+ type = TYPE_HXN;
402
+ }
403
+
320404 spriv->type = &pl2303_type_data[type];
321405 spriv->quirks = (unsigned long)usb_get_serial_data(serial);
322406 spriv->quirks |= spriv->type->quirks;
323407
324408 usb_set_serial_data(serial, spriv);
325409
326
- pl2303_vendor_read(serial, 0x8484, buf);
327
- pl2303_vendor_write(serial, 0x0404, 0);
328
- pl2303_vendor_read(serial, 0x8484, buf);
329
- pl2303_vendor_read(serial, 0x8383, buf);
330
- pl2303_vendor_read(serial, 0x8484, buf);
331
- pl2303_vendor_write(serial, 0x0404, 1);
332
- pl2303_vendor_read(serial, 0x8484, buf);
333
- pl2303_vendor_read(serial, 0x8383, buf);
334
- pl2303_vendor_write(serial, 0, 1);
335
- pl2303_vendor_write(serial, 1, 0);
336
- if (spriv->quirks & PL2303_QUIRK_LEGACY)
337
- pl2303_vendor_write(serial, 2, 0x24);
338
- else
339
- pl2303_vendor_write(serial, 2, 0x44);
410
+ if (type != TYPE_HXN) {
411
+ pl2303_vendor_read(serial, 0x8484, buf);
412
+ pl2303_vendor_write(serial, 0x0404, 0);
413
+ pl2303_vendor_read(serial, 0x8484, buf);
414
+ pl2303_vendor_read(serial, 0x8383, buf);
415
+ pl2303_vendor_read(serial, 0x8484, buf);
416
+ pl2303_vendor_write(serial, 0x0404, 1);
417
+ pl2303_vendor_read(serial, 0x8484, buf);
418
+ pl2303_vendor_read(serial, 0x8383, buf);
419
+ pl2303_vendor_write(serial, 0, 1);
420
+ pl2303_vendor_write(serial, 1, 0);
421
+ if (spriv->quirks & PL2303_QUIRK_LEGACY)
422
+ pl2303_vendor_write(serial, 2, 0x24);
423
+ else
424
+ pl2303_vendor_write(serial, 2, 0x44);
425
+ }
340426
341427 kfree(buf);
342428
....@@ -490,8 +576,12 @@
490576 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
491577 /*
492578 * Use direct method for supported baud rates, otherwise use divisors.
579
+ * Newer chip types do not support divisor encoding.
493580 */
494
- baud_sup = pl2303_get_supported_baud_rate(baud);
581
+ if (spriv->type->no_divisors)
582
+ baud_sup = baud;
583
+ else
584
+ baud_sup = pl2303_get_supported_baud_rate(baud);
495585
496586 if (baud == baud_sup)
497587 baud = pl2303_encode_baud_rate_direct(buf, baud);
....@@ -554,6 +644,20 @@
554644 a->c_cc[VSTOP] != b->c_cc[VSTOP];
555645
556646 return tty_termios_hw_change(a, b) || ixon_change;
647
+}
648
+
649
+static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type)
650
+{
651
+ if (!I_IXON(tty) || I_IXANY(tty))
652
+ return false;
653
+
654
+ if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13)
655
+ return false;
656
+
657
+ if (type->no_autoxonxoff)
658
+ return false;
659
+
660
+ return true;
557661 }
558662
559663 static void pl2303_set_termios(struct tty_struct *tty,
....@@ -681,15 +785,31 @@
681785 }
682786
683787 if (C_CRTSCTS(tty)) {
684
- if (spriv->quirks & PL2303_QUIRK_LEGACY)
685
- pl2303_vendor_write(serial, 0x0, 0x41);
686
- else
687
- pl2303_vendor_write(serial, 0x0, 0x61);
688
- } else if (I_IXON(tty) && !I_IXANY(tty) && START_CHAR(tty) == 0x11 &&
689
- STOP_CHAR(tty) == 0x13) {
690
- pl2303_vendor_write(serial, 0x0, 0xc0);
788
+ if (spriv->quirks & PL2303_QUIRK_LEGACY) {
789
+ pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
790
+ } else if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
791
+ pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
792
+ PL2303_HXN_FLOWCTRL_MASK,
793
+ PL2303_HXN_FLOWCTRL_RTS_CTS);
794
+ } else {
795
+ pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
796
+ }
797
+ } else if (pl2303_enable_xonxoff(tty, spriv->type)) {
798
+ if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
799
+ pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
800
+ PL2303_HXN_FLOWCTRL_MASK,
801
+ PL2303_HXN_FLOWCTRL_XON_XOFF);
802
+ } else {
803
+ pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0);
804
+ }
691805 } else {
692
- pl2303_vendor_write(serial, 0x0, 0x0);
806
+ if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
807
+ pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
808
+ PL2303_HXN_FLOWCTRL_MASK,
809
+ PL2303_HXN_FLOWCTRL_NONE);
810
+ } else {
811
+ pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
812
+ }
693813 }
694814
695815 kfree(buf);
....@@ -730,8 +850,14 @@
730850 usb_clear_halt(serial->dev, port->read_urb->pipe);
731851 } else {
732852 /* reset upstream data pipes */
733
- pl2303_vendor_write(serial, 8, 0);
734
- pl2303_vendor_write(serial, 9, 0);
853
+ if (spriv->type == &pl2303_type_data[TYPE_HXN]) {
854
+ pl2303_vendor_write(serial, PL2303_HXN_RESET_REG,
855
+ PL2303_HXN_RESET_UPSTREAM_PIPE |
856
+ PL2303_HXN_RESET_DOWNSTREAM_PIPE);
857
+ } else {
858
+ pl2303_vendor_write(serial, 8, 0);
859
+ pl2303_vendor_write(serial, 9, 0);
860
+ }
735861 }
736862
737863 /* Setup termios */
....@@ -818,29 +944,16 @@
818944 return 0;
819945 }
820946
821
-static int pl2303_ioctl(struct tty_struct *tty,
822
- unsigned int cmd, unsigned long arg)
947
+static int pl2303_get_serial(struct tty_struct *tty,
948
+ struct serial_struct *ss)
823949 {
824
- struct serial_struct ser;
825950 struct usb_serial_port *port = tty->driver_data;
826951
827
- switch (cmd) {
828
- case TIOCGSERIAL:
829
- memset(&ser, 0, sizeof ser);
830
- ser.type = PORT_16654;
831
- ser.line = port->minor;
832
- ser.port = port->port_number;
833
- ser.baud_base = 460800;
834
-
835
- if (copy_to_user((void __user *)arg, &ser, sizeof ser))
836
- return -EFAULT;
837
-
838
- return 0;
839
- default:
840
- break;
841
- }
842
-
843
- return -ENOIOCTLCMD;
952
+ ss->type = PORT_16654;
953
+ ss->line = port->minor;
954
+ ss->port = port->port_number;
955
+ ss->baud_base = 460800;
956
+ return 0;
844957 }
845958
846959 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
....@@ -998,7 +1111,7 @@
9981111 if (line_status & UART_OVERRUN_ERROR)
9991112 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
10001113
1001
- if (port->port.console && port->sysrq) {
1114
+ if (port->sysrq) {
10021115 for (i = 0; i < urb->actual_length; ++i)
10031116 if (!usb_serial_handle_sysrq_char(port, data[i]))
10041117 tty_insert_flip_char(&port->port, data[i],
....@@ -1026,7 +1139,7 @@
10261139 .close = pl2303_close,
10271140 .dtr_rts = pl2303_dtr_rts,
10281141 .carrier_raised = pl2303_carrier_raised,
1029
- .ioctl = pl2303_ioctl,
1142
+ .get_serial = pl2303_get_serial,
10301143 .break_ctl = pl2303_break_ctl,
10311144 .set_termios = pl2303_set_termios,
10321145 .tiocmget = pl2303_tiocmget,