1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
4 * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
5 * Copyright 2009, Boris Hajduk <boris@hajduk.org>
6 *
7 * ch341.c implements a serial port driver for the Winchiphead CH341.
8 *
9 * The CH341 device can be used to implement an RS232 asynchronous
10 * serial port, an IEEE-1284 parallel printer port or a memory-like
11 * interface. In all cases the CH341 supports an I2C interface as well.
12 * This driver only supports the asynchronous serial interface.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/tty.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/serial.h>
22 #include <asm/unaligned.h>
23
24 #define DEFAULT_BAUD_RATE 9600
25 #define DEFAULT_TIMEOUT 1000
26
27 /* flags for IO-Bits */
28 #define CH341_BIT_RTS (1 << 6)
29 #define CH341_BIT_DTR (1 << 5)
30
31 /******************************/
32 /* interrupt pipe definitions */
33 /******************************/
34 /* always 4 interrupt bytes */
35 /* first irq byte normally 0x08 */
36 /* second irq byte base 0x7d + below */
37 /* third irq byte base 0x94 + below */
38 /* fourth irq byte normally 0xee */
39
40 /* second interrupt byte */
41 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
42
43 /* status returned in third interrupt answer byte, inverted in data
44 from irq */
45 #define CH341_BIT_CTS 0x01
46 #define CH341_BIT_DSR 0x02
47 #define CH341_BIT_RI 0x04
48 #define CH341_BIT_DCD 0x08
49 #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
50
51 /*******************************/
52 /* baudrate calculation factor */
53 /*******************************/
54 #define CH341_BAUDBASE_FACTOR 1532620800
55 #define CH341_BAUDBASE_DIVMAX 3
56
57 /* Break support - the information used to implement this was gleaned from
58 * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato.
59 */
60
61 #define CH341_REQ_READ_VERSION 0x5F
62 #define CH341_REQ_WRITE_REG 0x9A
63 #define CH341_REQ_READ_REG 0x95
64 #define CH341_REQ_SERIAL_INIT 0xA1
65 #define CH341_REQ_MODEM_CTRL 0xA4
66
67 #define CH341_REG_BREAK 0x05
68 #define CH341_REG_LCR 0x18
69 #define CH341_NBREAK_BITS 0x01
70
71 #define CH341_LCR_ENABLE_RX 0x80
72 #define CH341_LCR_ENABLE_TX 0x40
73 #define CH341_LCR_MARK_SPACE 0x20
74 #define CH341_LCR_PAR_EVEN 0x10
75 #define CH341_LCR_ENABLE_PAR 0x08
76 #define CH341_LCR_STOP_BITS_2 0x04
77 #define CH341_LCR_CS8 0x03
78 #define CH341_LCR_CS7 0x02
79 #define CH341_LCR_CS6 0x01
80 #define CH341_LCR_CS5 0x00
81
82 static const struct usb_device_id id_table[] = {
83 { USB_DEVICE(0x4348, 0x5523) },
84 { USB_DEVICE(0x1a86, 0x7522) },
85 { USB_DEVICE(0x1a86, 0x7523) },
86 { USB_DEVICE(0x1a86, 0x5523) },
87 { },
88 };
89 MODULE_DEVICE_TABLE(usb, id_table);
90
91 struct ch341_private {
92 spinlock_t lock; /* access lock */
93 unsigned baud_rate; /* set baud rate */
94 u8 mcr;
95 u8 msr;
96 u8 lcr;
97 };
98
99 static void ch341_set_termios(struct tty_struct *tty,
100 struct usb_serial_port *port,
101 struct ktermios *old_termios);
102
ch341_control_out(struct usb_device * dev,u8 request,u16 value,u16 index)103 static int ch341_control_out(struct usb_device *dev, u8 request,
104 u16 value, u16 index)
105 {
106 int r;
107
108 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
109 request, value, index);
110
111 r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
112 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
113 value, index, NULL, 0, DEFAULT_TIMEOUT);
114 if (r < 0)
115 dev_err(&dev->dev, "failed to send control message: %d\n", r);
116
117 return r;
118 }
119
ch341_control_in(struct usb_device * dev,u8 request,u16 value,u16 index,char * buf,unsigned bufsize)120 static int ch341_control_in(struct usb_device *dev,
121 u8 request, u16 value, u16 index,
122 char *buf, unsigned bufsize)
123 {
124 int r;
125
126 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
127 request, value, index, bufsize);
128
129 r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
130 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
131 value, index, buf, bufsize, DEFAULT_TIMEOUT);
132 if (r < (int)bufsize) {
133 if (r >= 0) {
134 dev_err(&dev->dev,
135 "short control message received (%d < %u)\n",
136 r, bufsize);
137 r = -EIO;
138 }
139
140 dev_err(&dev->dev, "failed to receive control message: %d\n",
141 r);
142 return r;
143 }
144
145 return 0;
146 }
147
ch341_set_baudrate_lcr(struct usb_device * dev,struct ch341_private * priv,u8 lcr)148 static int ch341_set_baudrate_lcr(struct usb_device *dev,
149 struct ch341_private *priv, u8 lcr)
150 {
151 short a;
152 int r;
153 unsigned long factor;
154 short divisor;
155
156 if (!priv->baud_rate)
157 return -EINVAL;
158 factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
159 divisor = CH341_BAUDBASE_DIVMAX;
160
161 while ((factor > 0xfff0) && divisor) {
162 factor >>= 3;
163 divisor--;
164 }
165
166 if (factor > 0xfff0)
167 return -EINVAL;
168
169 factor = 0x10000 - factor;
170 a = (factor & 0xff00) | divisor;
171
172 /*
173 * CH341A buffers data until a full endpoint-size packet (32 bytes)
174 * has been received unless bit 7 is set.
175 */
176 a |= BIT(7);
177
178 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a);
179 if (r)
180 return r;
181
182 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, lcr);
183 if (r)
184 return r;
185
186 return r;
187 }
188
ch341_set_handshake(struct usb_device * dev,u8 control)189 static int ch341_set_handshake(struct usb_device *dev, u8 control)
190 {
191 return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
192 }
193
ch341_get_status(struct usb_device * dev,struct ch341_private * priv)194 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
195 {
196 const unsigned int size = 2;
197 char *buffer;
198 int r;
199 unsigned long flags;
200
201 buffer = kmalloc(size, GFP_KERNEL);
202 if (!buffer)
203 return -ENOMEM;
204
205 r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
206 if (r < 0)
207 goto out;
208
209 spin_lock_irqsave(&priv->lock, flags);
210 priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
211 spin_unlock_irqrestore(&priv->lock, flags);
212
213 out: kfree(buffer);
214 return r;
215 }
216
217 /* -------------------------------------------------------------------------- */
218
ch341_configure(struct usb_device * dev,struct ch341_private * priv)219 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
220 {
221 const unsigned int size = 2;
222 char *buffer;
223 int r;
224
225 buffer = kmalloc(size, GFP_KERNEL);
226 if (!buffer)
227 return -ENOMEM;
228
229 /* expect two bytes 0x27 0x00 */
230 r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
231 if (r < 0)
232 goto out;
233 dev_dbg(&dev->dev, "Chip version: 0x%02x\n", buffer[0]);
234
235 r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
236 if (r < 0)
237 goto out;
238
239 r = ch341_set_baudrate_lcr(dev, priv, priv->lcr);
240 if (r < 0)
241 goto out;
242
243 r = ch341_set_handshake(dev, priv->mcr);
244
245 out: kfree(buffer);
246 return r;
247 }
248
ch341_port_probe(struct usb_serial_port * port)249 static int ch341_port_probe(struct usb_serial_port *port)
250 {
251 struct ch341_private *priv;
252 int r;
253
254 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
255 if (!priv)
256 return -ENOMEM;
257
258 spin_lock_init(&priv->lock);
259 priv->baud_rate = DEFAULT_BAUD_RATE;
260 /*
261 * Some CH340 devices appear unable to change the initial LCR
262 * settings, so set a sane 8N1 default.
263 */
264 priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
265
266 r = ch341_configure(port->serial->dev, priv);
267 if (r < 0)
268 goto error;
269
270 usb_set_serial_port_data(port, priv);
271 return 0;
272
273 error: kfree(priv);
274 return r;
275 }
276
ch341_port_remove(struct usb_serial_port * port)277 static int ch341_port_remove(struct usb_serial_port *port)
278 {
279 struct ch341_private *priv;
280
281 priv = usb_get_serial_port_data(port);
282 kfree(priv);
283
284 return 0;
285 }
286
ch341_carrier_raised(struct usb_serial_port * port)287 static int ch341_carrier_raised(struct usb_serial_port *port)
288 {
289 struct ch341_private *priv = usb_get_serial_port_data(port);
290 if (priv->msr & CH341_BIT_DCD)
291 return 1;
292 return 0;
293 }
294
ch341_dtr_rts(struct usb_serial_port * port,int on)295 static void ch341_dtr_rts(struct usb_serial_port *port, int on)
296 {
297 struct ch341_private *priv = usb_get_serial_port_data(port);
298 unsigned long flags;
299
300 /* drop DTR and RTS */
301 spin_lock_irqsave(&priv->lock, flags);
302 if (on)
303 priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
304 else
305 priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
306 spin_unlock_irqrestore(&priv->lock, flags);
307 ch341_set_handshake(port->serial->dev, priv->mcr);
308 }
309
ch341_close(struct usb_serial_port * port)310 static void ch341_close(struct usb_serial_port *port)
311 {
312 usb_serial_generic_close(port);
313 usb_kill_urb(port->interrupt_in_urb);
314 }
315
316
317 /* open this device, set default parameters */
ch341_open(struct tty_struct * tty,struct usb_serial_port * port)318 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
319 {
320 struct ch341_private *priv = usb_get_serial_port_data(port);
321 int r;
322
323 if (tty)
324 ch341_set_termios(tty, port, NULL);
325
326 dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
327 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
328 if (r) {
329 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
330 __func__, r);
331 return r;
332 }
333
334 r = ch341_get_status(port->serial->dev, priv);
335 if (r < 0) {
336 dev_err(&port->dev, "failed to read modem status: %d\n", r);
337 goto err_kill_interrupt_urb;
338 }
339
340 r = usb_serial_generic_open(tty, port);
341 if (r)
342 goto err_kill_interrupt_urb;
343
344 return 0;
345
346 err_kill_interrupt_urb:
347 usb_kill_urb(port->interrupt_in_urb);
348
349 return r;
350 }
351
352 /* Old_termios contains the original termios settings and
353 * tty->termios contains the new setting to be used.
354 */
ch341_set_termios(struct tty_struct * tty,struct usb_serial_port * port,struct ktermios * old_termios)355 static void ch341_set_termios(struct tty_struct *tty,
356 struct usb_serial_port *port, struct ktermios *old_termios)
357 {
358 struct ch341_private *priv = usb_get_serial_port_data(port);
359 unsigned baud_rate;
360 unsigned long flags;
361 u8 lcr;
362 int r;
363
364 /* redundant changes may cause the chip to lose bytes */
365 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
366 return;
367
368 baud_rate = tty_get_baud_rate(tty);
369
370 lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
371
372 switch (C_CSIZE(tty)) {
373 case CS5:
374 lcr |= CH341_LCR_CS5;
375 break;
376 case CS6:
377 lcr |= CH341_LCR_CS6;
378 break;
379 case CS7:
380 lcr |= CH341_LCR_CS7;
381 break;
382 case CS8:
383 lcr |= CH341_LCR_CS8;
384 break;
385 }
386
387 if (C_PARENB(tty)) {
388 lcr |= CH341_LCR_ENABLE_PAR;
389 if (C_PARODD(tty) == 0)
390 lcr |= CH341_LCR_PAR_EVEN;
391 if (C_CMSPAR(tty))
392 lcr |= CH341_LCR_MARK_SPACE;
393 }
394
395 if (C_CSTOPB(tty))
396 lcr |= CH341_LCR_STOP_BITS_2;
397
398 if (baud_rate) {
399 priv->baud_rate = baud_rate;
400
401 r = ch341_set_baudrate_lcr(port->serial->dev, priv, lcr);
402 if (r < 0 && old_termios) {
403 priv->baud_rate = tty_termios_baud_rate(old_termios);
404 tty_termios_copy_hw(&tty->termios, old_termios);
405 } else if (r == 0) {
406 priv->lcr = lcr;
407 }
408 }
409
410 spin_lock_irqsave(&priv->lock, flags);
411 if (C_BAUD(tty) == B0)
412 priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
413 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
414 priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
415 spin_unlock_irqrestore(&priv->lock, flags);
416
417 ch341_set_handshake(port->serial->dev, priv->mcr);
418 }
419
ch341_break_ctl(struct tty_struct * tty,int break_state)420 static void ch341_break_ctl(struct tty_struct *tty, int break_state)
421 {
422 const uint16_t ch341_break_reg =
423 ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
424 struct usb_serial_port *port = tty->driver_data;
425 int r;
426 uint16_t reg_contents;
427 uint8_t *break_reg;
428
429 break_reg = kmalloc(2, GFP_KERNEL);
430 if (!break_reg)
431 return;
432
433 r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
434 ch341_break_reg, 0, break_reg, 2);
435 if (r < 0) {
436 dev_err(&port->dev, "%s - USB control read error (%d)\n",
437 __func__, r);
438 goto out;
439 }
440 dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
441 __func__, break_reg[0], break_reg[1]);
442 if (break_state != 0) {
443 dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
444 break_reg[0] &= ~CH341_NBREAK_BITS;
445 break_reg[1] &= ~CH341_LCR_ENABLE_TX;
446 } else {
447 dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
448 break_reg[0] |= CH341_NBREAK_BITS;
449 break_reg[1] |= CH341_LCR_ENABLE_TX;
450 }
451 dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
452 __func__, break_reg[0], break_reg[1]);
453 reg_contents = get_unaligned_le16(break_reg);
454 r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
455 ch341_break_reg, reg_contents);
456 if (r < 0)
457 dev_err(&port->dev, "%s - USB control write error (%d)\n",
458 __func__, r);
459 out:
460 kfree(break_reg);
461 }
462
ch341_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)463 static int ch341_tiocmset(struct tty_struct *tty,
464 unsigned int set, unsigned int clear)
465 {
466 struct usb_serial_port *port = tty->driver_data;
467 struct ch341_private *priv = usb_get_serial_port_data(port);
468 unsigned long flags;
469 u8 control;
470
471 spin_lock_irqsave(&priv->lock, flags);
472 if (set & TIOCM_RTS)
473 priv->mcr |= CH341_BIT_RTS;
474 if (set & TIOCM_DTR)
475 priv->mcr |= CH341_BIT_DTR;
476 if (clear & TIOCM_RTS)
477 priv->mcr &= ~CH341_BIT_RTS;
478 if (clear & TIOCM_DTR)
479 priv->mcr &= ~CH341_BIT_DTR;
480 control = priv->mcr;
481 spin_unlock_irqrestore(&priv->lock, flags);
482
483 return ch341_set_handshake(port->serial->dev, control);
484 }
485
ch341_update_status(struct usb_serial_port * port,unsigned char * data,size_t len)486 static void ch341_update_status(struct usb_serial_port *port,
487 unsigned char *data, size_t len)
488 {
489 struct ch341_private *priv = usb_get_serial_port_data(port);
490 struct tty_struct *tty;
491 unsigned long flags;
492 u8 status;
493 u8 delta;
494
495 if (len < 4)
496 return;
497
498 status = ~data[2] & CH341_BITS_MODEM_STAT;
499
500 spin_lock_irqsave(&priv->lock, flags);
501 delta = status ^ priv->msr;
502 priv->msr = status;
503 spin_unlock_irqrestore(&priv->lock, flags);
504
505 if (data[1] & CH341_MULT_STAT)
506 dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
507
508 if (!delta)
509 return;
510
511 if (delta & CH341_BIT_CTS)
512 port->icount.cts++;
513 if (delta & CH341_BIT_DSR)
514 port->icount.dsr++;
515 if (delta & CH341_BIT_RI)
516 port->icount.rng++;
517 if (delta & CH341_BIT_DCD) {
518 port->icount.dcd++;
519 tty = tty_port_tty_get(&port->port);
520 if (tty) {
521 usb_serial_handle_dcd_change(port, tty,
522 status & CH341_BIT_DCD);
523 tty_kref_put(tty);
524 }
525 }
526
527 wake_up_interruptible(&port->port.delta_msr_wait);
528 }
529
ch341_read_int_callback(struct urb * urb)530 static void ch341_read_int_callback(struct urb *urb)
531 {
532 struct usb_serial_port *port = urb->context;
533 unsigned char *data = urb->transfer_buffer;
534 unsigned int len = urb->actual_length;
535 int status;
536
537 switch (urb->status) {
538 case 0:
539 /* success */
540 break;
541 case -ECONNRESET:
542 case -ENOENT:
543 case -ESHUTDOWN:
544 /* this urb is terminated, clean up */
545 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
546 __func__, urb->status);
547 return;
548 default:
549 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
550 __func__, urb->status);
551 goto exit;
552 }
553
554 usb_serial_debug_data(&port->dev, __func__, len, data);
555 ch341_update_status(port, data, len);
556 exit:
557 status = usb_submit_urb(urb, GFP_ATOMIC);
558 if (status) {
559 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
560 __func__, status);
561 }
562 }
563
ch341_tiocmget(struct tty_struct * tty)564 static int ch341_tiocmget(struct tty_struct *tty)
565 {
566 struct usb_serial_port *port = tty->driver_data;
567 struct ch341_private *priv = usb_get_serial_port_data(port);
568 unsigned long flags;
569 u8 mcr;
570 u8 status;
571 unsigned int result;
572
573 spin_lock_irqsave(&priv->lock, flags);
574 mcr = priv->mcr;
575 status = priv->msr;
576 spin_unlock_irqrestore(&priv->lock, flags);
577
578 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
579 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
580 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
581 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
582 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
583 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
584
585 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
586
587 return result;
588 }
589
ch341_reset_resume(struct usb_serial * serial)590 static int ch341_reset_resume(struct usb_serial *serial)
591 {
592 struct usb_serial_port *port = serial->port[0];
593 struct ch341_private *priv;
594 int ret;
595
596 priv = usb_get_serial_port_data(port);
597 if (!priv)
598 return 0;
599
600 /* reconfigure ch341 serial port after bus-reset */
601 ch341_configure(serial->dev, priv);
602
603 if (tty_port_initialized(&port->port)) {
604 ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
605 if (ret) {
606 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
607 ret);
608 return ret;
609 }
610
611 ret = ch341_get_status(port->serial->dev, priv);
612 if (ret < 0) {
613 dev_err(&port->dev, "failed to read modem status: %d\n",
614 ret);
615 }
616 }
617
618 return usb_serial_generic_resume(serial);
619 }
620
621 static struct usb_serial_driver ch341_device = {
622 .driver = {
623 .owner = THIS_MODULE,
624 .name = "ch341-uart",
625 },
626 .id_table = id_table,
627 .num_ports = 1,
628 .open = ch341_open,
629 .dtr_rts = ch341_dtr_rts,
630 .carrier_raised = ch341_carrier_raised,
631 .close = ch341_close,
632 .set_termios = ch341_set_termios,
633 .break_ctl = ch341_break_ctl,
634 .tiocmget = ch341_tiocmget,
635 .tiocmset = ch341_tiocmset,
636 .tiocmiwait = usb_serial_generic_tiocmiwait,
637 .read_int_callback = ch341_read_int_callback,
638 .port_probe = ch341_port_probe,
639 .port_remove = ch341_port_remove,
640 .reset_resume = ch341_reset_resume,
641 };
642
643 static struct usb_serial_driver * const serial_drivers[] = {
644 &ch341_device, NULL
645 };
646
647 module_usb_serial_driver(serial_drivers, id_table);
648
649 MODULE_LICENSE("GPL v2");
650