1
2
3
4
5
6
7
8
9
10
11
12
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
28#define CH341_BIT_RTS (1 << 6)
29#define CH341_BIT_DTR (1 << 5)
30
31
32
33
34
35
36
37
38
39
40
41#define CH341_MULT_STAT 0x04
42
43
44
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
50
51
52
53
54
55#define CH341_REQ_READ_VERSION 0x5F
56#define CH341_REQ_WRITE_REG 0x9A
57#define CH341_REQ_READ_REG 0x95
58#define CH341_REQ_SERIAL_INIT 0xA1
59#define CH341_REQ_MODEM_CTRL 0xA4
60
61#define CH341_REG_BREAK 0x05
62#define CH341_REG_LCR 0x18
63#define CH341_NBREAK_BITS 0x01
64
65#define CH341_LCR_ENABLE_RX 0x80
66#define CH341_LCR_ENABLE_TX 0x40
67#define CH341_LCR_MARK_SPACE 0x20
68#define CH341_LCR_PAR_EVEN 0x10
69#define CH341_LCR_ENABLE_PAR 0x08
70#define CH341_LCR_STOP_BITS_2 0x04
71#define CH341_LCR_CS8 0x03
72#define CH341_LCR_CS7 0x02
73#define CH341_LCR_CS6 0x01
74#define CH341_LCR_CS5 0x00
75
76#define CH341_QUIRK_LIMITED_PRESCALER BIT(0)
77
78static const struct usb_device_id id_table[] = {
79 { USB_DEVICE(0x4348, 0x5523) },
80 { USB_DEVICE(0x1a86, 0x7522) },
81 { USB_DEVICE(0x1a86, 0x7523) },
82 { USB_DEVICE(0x1a86, 0x5523) },
83 { },
84};
85MODULE_DEVICE_TABLE(usb, id_table);
86
87struct ch341_private {
88 spinlock_t lock;
89 unsigned baud_rate;
90 u8 mcr;
91 u8 msr;
92 u8 lcr;
93 unsigned long quirks;
94};
95
96static void ch341_set_termios(struct tty_struct *tty,
97 struct usb_serial_port *port,
98 struct ktermios *old_termios);
99
100static int ch341_control_out(struct usb_device *dev, u8 request,
101 u16 value, u16 index)
102{
103 int r;
104
105 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
106 request, value, index);
107
108 r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
109 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
110 value, index, NULL, 0, DEFAULT_TIMEOUT);
111 if (r < 0)
112 dev_err(&dev->dev, "failed to send control message: %d\n", r);
113
114 return r;
115}
116
117static int ch341_control_in(struct usb_device *dev,
118 u8 request, u16 value, u16 index,
119 char *buf, unsigned bufsize)
120{
121 int r;
122
123 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
124 request, value, index, bufsize);
125
126 r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
127 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
128 value, index, buf, bufsize, DEFAULT_TIMEOUT);
129 if (r < (int)bufsize) {
130 if (r >= 0) {
131 dev_err(&dev->dev,
132 "short control message received (%d < %u)\n",
133 r, bufsize);
134 r = -EIO;
135 }
136
137 dev_err(&dev->dev, "failed to receive control message: %d\n",
138 r);
139 return r;
140 }
141
142 return 0;
143}
144
145#define CH341_CLKRATE 48000000
146#define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact)))
147#define CH341_MIN_RATE(ps) (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512))
148
149static const speed_t ch341_min_rates[] = {
150 CH341_MIN_RATE(0),
151 CH341_MIN_RATE(1),
152 CH341_MIN_RATE(2),
153 CH341_MIN_RATE(3),
154};
155
156
157
158
159
160
161
162
163
164
165
166static int ch341_get_divisor(struct ch341_private *priv)
167{
168 unsigned int fact, div, clk_div;
169 speed_t speed = priv->baud_rate;
170 bool force_fact0 = false;
171 int ps;
172
173
174
175
176
177 speed = clamp(speed, 46U, 3000000U);
178
179
180
181
182
183 fact = 1;
184 for (ps = 3; ps >= 0; ps--) {
185 if (speed > ch341_min_rates[ps])
186 break;
187 }
188
189 if (ps < 0)
190 return -EINVAL;
191
192
193 clk_div = CH341_CLK_DIV(ps, fact);
194 div = CH341_CLKRATE / (clk_div * speed);
195
196
197 if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER))
198 force_fact0 = true;
199
200
201 if (div < 9 || div > 255 || force_fact0) {
202 div /= 2;
203 clk_div *= 2;
204 fact = 0;
205 }
206
207 if (div < 2)
208 return -EINVAL;
209
210
211
212
213
214 if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >=
215 16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
216 div++;
217
218
219
220
221
222
223 if (fact == 1 && div % 2 == 0) {
224 div /= 2;
225 fact = 0;
226 }
227
228 return (0x100 - div) << 8 | fact << 2 | ps;
229}
230
231static int ch341_set_baudrate_lcr(struct usb_device *dev,
232 struct ch341_private *priv, u8 lcr)
233{
234 int val;
235 int r;
236
237 if (!priv->baud_rate)
238 return -EINVAL;
239
240 val = ch341_get_divisor(priv);
241 if (val < 0)
242 return -EINVAL;
243
244
245
246
247
248 val |= BIT(7);
249
250 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, val);
251 if (r)
252 return r;
253
254 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, lcr);
255 if (r)
256 return r;
257
258 return r;
259}
260
261static int ch341_set_handshake(struct usb_device *dev, u8 control)
262{
263 return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
264}
265
266static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
267{
268 const unsigned int size = 2;
269 char *buffer;
270 int r;
271 unsigned long flags;
272
273 buffer = kmalloc(size, GFP_KERNEL);
274 if (!buffer)
275 return -ENOMEM;
276
277 r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
278 if (r < 0)
279 goto out;
280
281 spin_lock_irqsave(&priv->lock, flags);
282 priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
283 spin_unlock_irqrestore(&priv->lock, flags);
284
285out: kfree(buffer);
286 return r;
287}
288
289
290
291static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
292{
293 const unsigned int size = 2;
294 char *buffer;
295 int r;
296
297 buffer = kmalloc(size, GFP_KERNEL);
298 if (!buffer)
299 return -ENOMEM;
300
301
302 r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
303 if (r < 0)
304 goto out;
305 dev_dbg(&dev->dev, "Chip version: 0x%02x\n", buffer[0]);
306
307 r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
308 if (r < 0)
309 goto out;
310
311 r = ch341_set_baudrate_lcr(dev, priv, priv->lcr);
312 if (r < 0)
313 goto out;
314
315 r = ch341_set_handshake(dev, priv->mcr);
316
317out: kfree(buffer);
318 return r;
319}
320
321static int ch341_detect_quirks(struct usb_serial_port *port)
322{
323 struct ch341_private *priv = usb_get_serial_port_data(port);
324 struct usb_device *udev = port->serial->dev;
325 const unsigned int size = 2;
326 unsigned long quirks = 0;
327 char *buffer;
328 int r;
329
330 buffer = kmalloc(size, GFP_KERNEL);
331 if (!buffer)
332 return -ENOMEM;
333
334
335
336
337
338
339
340 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), CH341_REQ_READ_REG,
341 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
342 CH341_REG_BREAK, 0, buffer, size, DEFAULT_TIMEOUT);
343 if (r == -EPIPE) {
344 dev_dbg(&port->dev, "break control not supported\n");
345 quirks = CH341_QUIRK_LIMITED_PRESCALER;
346 r = 0;
347 goto out;
348 }
349
350 if (r != size) {
351 if (r >= 0)
352 r = -EIO;
353 dev_err(&port->dev, "failed to read break control: %d\n", r);
354 goto out;
355 }
356
357 r = 0;
358out:
359 kfree(buffer);
360
361 if (quirks) {
362 dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks);
363 priv->quirks |= quirks;
364 }
365
366 return r;
367}
368
369static int ch341_port_probe(struct usb_serial_port *port)
370{
371 struct ch341_private *priv;
372 int r;
373
374 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
375 if (!priv)
376 return -ENOMEM;
377
378 spin_lock_init(&priv->lock);
379 priv->baud_rate = DEFAULT_BAUD_RATE;
380
381
382
383
384 priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
385
386 r = ch341_configure(port->serial->dev, priv);
387 if (r < 0)
388 goto error;
389
390 usb_set_serial_port_data(port, priv);
391
392 r = ch341_detect_quirks(port);
393 if (r < 0)
394 goto error;
395
396 return 0;
397
398error: kfree(priv);
399 return r;
400}
401
402static int ch341_port_remove(struct usb_serial_port *port)
403{
404 struct ch341_private *priv;
405
406 priv = usb_get_serial_port_data(port);
407 kfree(priv);
408
409 return 0;
410}
411
412static int ch341_carrier_raised(struct usb_serial_port *port)
413{
414 struct ch341_private *priv = usb_get_serial_port_data(port);
415 if (priv->msr & CH341_BIT_DCD)
416 return 1;
417 return 0;
418}
419
420static void ch341_dtr_rts(struct usb_serial_port *port, int on)
421{
422 struct ch341_private *priv = usb_get_serial_port_data(port);
423 unsigned long flags;
424
425
426 spin_lock_irqsave(&priv->lock, flags);
427 if (on)
428 priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
429 else
430 priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
431 spin_unlock_irqrestore(&priv->lock, flags);
432 ch341_set_handshake(port->serial->dev, priv->mcr);
433}
434
435static void ch341_close(struct usb_serial_port *port)
436{
437 usb_serial_generic_close(port);
438 usb_kill_urb(port->interrupt_in_urb);
439}
440
441
442
443static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
444{
445 struct ch341_private *priv = usb_get_serial_port_data(port);
446 int r;
447
448 if (tty)
449 ch341_set_termios(tty, port, NULL);
450
451 dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
452 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
453 if (r) {
454 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
455 __func__, r);
456 return r;
457 }
458
459 r = ch341_get_status(port->serial->dev, priv);
460 if (r < 0) {
461 dev_err(&port->dev, "failed to read modem status: %d\n", r);
462 goto err_kill_interrupt_urb;
463 }
464
465 r = usb_serial_generic_open(tty, port);
466 if (r)
467 goto err_kill_interrupt_urb;
468
469 return 0;
470
471err_kill_interrupt_urb:
472 usb_kill_urb(port->interrupt_in_urb);
473
474 return r;
475}
476
477
478
479
480static void ch341_set_termios(struct tty_struct *tty,
481 struct usb_serial_port *port, struct ktermios *old_termios)
482{
483 struct ch341_private *priv = usb_get_serial_port_data(port);
484 unsigned baud_rate;
485 unsigned long flags;
486 u8 lcr;
487 int r;
488
489
490 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
491 return;
492
493 baud_rate = tty_get_baud_rate(tty);
494
495 lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
496
497 switch (C_CSIZE(tty)) {
498 case CS5:
499 lcr |= CH341_LCR_CS5;
500 break;
501 case CS6:
502 lcr |= CH341_LCR_CS6;
503 break;
504 case CS7:
505 lcr |= CH341_LCR_CS7;
506 break;
507 case CS8:
508 lcr |= CH341_LCR_CS8;
509 break;
510 }
511
512 if (C_PARENB(tty)) {
513 lcr |= CH341_LCR_ENABLE_PAR;
514 if (C_PARODD(tty) == 0)
515 lcr |= CH341_LCR_PAR_EVEN;
516 if (C_CMSPAR(tty))
517 lcr |= CH341_LCR_MARK_SPACE;
518 }
519
520 if (C_CSTOPB(tty))
521 lcr |= CH341_LCR_STOP_BITS_2;
522
523 if (baud_rate) {
524 priv->baud_rate = baud_rate;
525
526 r = ch341_set_baudrate_lcr(port->serial->dev, priv, lcr);
527 if (r < 0 && old_termios) {
528 priv->baud_rate = tty_termios_baud_rate(old_termios);
529 tty_termios_copy_hw(&tty->termios, old_termios);
530 } else if (r == 0) {
531 priv->lcr = lcr;
532 }
533 }
534
535 spin_lock_irqsave(&priv->lock, flags);
536 if (C_BAUD(tty) == B0)
537 priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
538 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
539 priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
540 spin_unlock_irqrestore(&priv->lock, flags);
541
542 ch341_set_handshake(port->serial->dev, priv->mcr);
543}
544
545static void ch341_break_ctl(struct tty_struct *tty, int break_state)
546{
547 const uint16_t ch341_break_reg =
548 ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
549 struct usb_serial_port *port = tty->driver_data;
550 int r;
551 uint16_t reg_contents;
552 uint8_t *break_reg;
553
554 break_reg = kmalloc(2, GFP_KERNEL);
555 if (!break_reg)
556 return;
557
558 r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
559 ch341_break_reg, 0, break_reg, 2);
560 if (r < 0) {
561 dev_err(&port->dev, "%s - USB control read error (%d)\n",
562 __func__, r);
563 goto out;
564 }
565 dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
566 __func__, break_reg[0], break_reg[1]);
567 if (break_state != 0) {
568 dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
569 break_reg[0] &= ~CH341_NBREAK_BITS;
570 break_reg[1] &= ~CH341_LCR_ENABLE_TX;
571 } else {
572 dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
573 break_reg[0] |= CH341_NBREAK_BITS;
574 break_reg[1] |= CH341_LCR_ENABLE_TX;
575 }
576 dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
577 __func__, break_reg[0], break_reg[1]);
578 reg_contents = get_unaligned_le16(break_reg);
579 r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
580 ch341_break_reg, reg_contents);
581 if (r < 0)
582 dev_err(&port->dev, "%s - USB control write error (%d)\n",
583 __func__, r);
584out:
585 kfree(break_reg);
586}
587
588static int ch341_tiocmset(struct tty_struct *tty,
589 unsigned int set, unsigned int clear)
590{
591 struct usb_serial_port *port = tty->driver_data;
592 struct ch341_private *priv = usb_get_serial_port_data(port);
593 unsigned long flags;
594 u8 control;
595
596 spin_lock_irqsave(&priv->lock, flags);
597 if (set & TIOCM_RTS)
598 priv->mcr |= CH341_BIT_RTS;
599 if (set & TIOCM_DTR)
600 priv->mcr |= CH341_BIT_DTR;
601 if (clear & TIOCM_RTS)
602 priv->mcr &= ~CH341_BIT_RTS;
603 if (clear & TIOCM_DTR)
604 priv->mcr &= ~CH341_BIT_DTR;
605 control = priv->mcr;
606 spin_unlock_irqrestore(&priv->lock, flags);
607
608 return ch341_set_handshake(port->serial->dev, control);
609}
610
611static void ch341_update_status(struct usb_serial_port *port,
612 unsigned char *data, size_t len)
613{
614 struct ch341_private *priv = usb_get_serial_port_data(port);
615 struct tty_struct *tty;
616 unsigned long flags;
617 u8 status;
618 u8 delta;
619
620 if (len < 4)
621 return;
622
623 status = ~data[2] & CH341_BITS_MODEM_STAT;
624
625 spin_lock_irqsave(&priv->lock, flags);
626 delta = status ^ priv->msr;
627 priv->msr = status;
628 spin_unlock_irqrestore(&priv->lock, flags);
629
630 if (data[1] & CH341_MULT_STAT)
631 dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
632
633 if (!delta)
634 return;
635
636 if (delta & CH341_BIT_CTS)
637 port->icount.cts++;
638 if (delta & CH341_BIT_DSR)
639 port->icount.dsr++;
640 if (delta & CH341_BIT_RI)
641 port->icount.rng++;
642 if (delta & CH341_BIT_DCD) {
643 port->icount.dcd++;
644 tty = tty_port_tty_get(&port->port);
645 if (tty) {
646 usb_serial_handle_dcd_change(port, tty,
647 status & CH341_BIT_DCD);
648 tty_kref_put(tty);
649 }
650 }
651
652 wake_up_interruptible(&port->port.delta_msr_wait);
653}
654
655static void ch341_read_int_callback(struct urb *urb)
656{
657 struct usb_serial_port *port = urb->context;
658 unsigned char *data = urb->transfer_buffer;
659 unsigned int len = urb->actual_length;
660 int status;
661
662 switch (urb->status) {
663 case 0:
664
665 break;
666 case -ECONNRESET:
667 case -ENOENT:
668 case -ESHUTDOWN:
669
670 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
671 __func__, urb->status);
672 return;
673 default:
674 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
675 __func__, urb->status);
676 goto exit;
677 }
678
679 usb_serial_debug_data(&port->dev, __func__, len, data);
680 ch341_update_status(port, data, len);
681exit:
682 status = usb_submit_urb(urb, GFP_ATOMIC);
683 if (status) {
684 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
685 __func__, status);
686 }
687}
688
689static int ch341_tiocmget(struct tty_struct *tty)
690{
691 struct usb_serial_port *port = tty->driver_data;
692 struct ch341_private *priv = usb_get_serial_port_data(port);
693 unsigned long flags;
694 u8 mcr;
695 u8 status;
696 unsigned int result;
697
698 spin_lock_irqsave(&priv->lock, flags);
699 mcr = priv->mcr;
700 status = priv->msr;
701 spin_unlock_irqrestore(&priv->lock, flags);
702
703 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
704 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
705 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
706 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
707 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
708 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
709
710 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
711
712 return result;
713}
714
715static int ch341_reset_resume(struct usb_serial *serial)
716{
717 struct usb_serial_port *port = serial->port[0];
718 struct ch341_private *priv;
719 int ret;
720
721 priv = usb_get_serial_port_data(port);
722 if (!priv)
723 return 0;
724
725
726 ch341_configure(serial->dev, priv);
727
728 if (tty_port_initialized(&port->port)) {
729 ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
730 if (ret) {
731 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
732 ret);
733 return ret;
734 }
735
736 ret = ch341_get_status(port->serial->dev, priv);
737 if (ret < 0) {
738 dev_err(&port->dev, "failed to read modem status: %d\n",
739 ret);
740 }
741 }
742
743 return usb_serial_generic_resume(serial);
744}
745
746static struct usb_serial_driver ch341_device = {
747 .driver = {
748 .owner = THIS_MODULE,
749 .name = "ch341-uart",
750 },
751 .id_table = id_table,
752 .num_ports = 1,
753 .open = ch341_open,
754 .dtr_rts = ch341_dtr_rts,
755 .carrier_raised = ch341_carrier_raised,
756 .close = ch341_close,
757 .set_termios = ch341_set_termios,
758 .break_ctl = ch341_break_ctl,
759 .tiocmget = ch341_tiocmget,
760 .tiocmset = ch341_tiocmset,
761 .tiocmiwait = usb_serial_generic_tiocmiwait,
762 .read_int_callback = ch341_read_int_callback,
763 .port_probe = ch341_port_probe,
764 .port_remove = ch341_port_remove,
765 .reset_resume = ch341_reset_resume,
766};
767
768static struct usb_serial_driver * const serial_drivers[] = {
769 &ch341_device, NULL
770};
771
772module_usb_serial_driver(serial_drivers, id_table);
773
774MODULE_LICENSE("GPL v2");
775