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