xref: /linux/drivers/usb/serial/ch341.c (revision 55fa15b5987db22b4f35d3f0798928c126be5f1c)
16ce76104SFrank A Kingswood /*
26ce76104SFrank A Kingswood  * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
3664d5df9SWerner Cornelius  * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
4664d5df9SWerner Cornelius  * Copyright 2009, Boris Hajduk <boris@hajduk.org>
56ce76104SFrank A Kingswood  *
66ce76104SFrank A Kingswood  * ch341.c implements a serial port driver for the Winchiphead CH341.
76ce76104SFrank A Kingswood  *
86ce76104SFrank A Kingswood  * The CH341 device can be used to implement an RS232 asynchronous
96ce76104SFrank A Kingswood  * serial port, an IEEE-1284 parallel printer port or a memory-like
106ce76104SFrank A Kingswood  * interface. In all cases the CH341 supports an I2C interface as well.
116ce76104SFrank A Kingswood  * This driver only supports the asynchronous serial interface.
126ce76104SFrank A Kingswood  *
136ce76104SFrank A Kingswood  * This program is free software; you can redistribute it and/or
146ce76104SFrank A Kingswood  * modify it under the terms of the GNU General Public License version
156ce76104SFrank A Kingswood  * 2 as published by the Free Software Foundation.
166ce76104SFrank A Kingswood  */
176ce76104SFrank A Kingswood 
186ce76104SFrank A Kingswood #include <linux/kernel.h>
196ce76104SFrank A Kingswood #include <linux/tty.h>
206ce76104SFrank A Kingswood #include <linux/module.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
226ce76104SFrank A Kingswood #include <linux/usb.h>
236ce76104SFrank A Kingswood #include <linux/usb/serial.h>
246ce76104SFrank A Kingswood #include <linux/serial.h>
255be796f0SJohan Hovold #include <asm/unaligned.h>
266ce76104SFrank A Kingswood 
27664d5df9SWerner Cornelius #define DEFAULT_BAUD_RATE 9600
286ce76104SFrank A Kingswood #define DEFAULT_TIMEOUT   1000
296ce76104SFrank A Kingswood 
30664d5df9SWerner Cornelius /* flags for IO-Bits */
31664d5df9SWerner Cornelius #define CH341_BIT_RTS (1 << 6)
32664d5df9SWerner Cornelius #define CH341_BIT_DTR (1 << 5)
33664d5df9SWerner Cornelius 
34664d5df9SWerner Cornelius /******************************/
35664d5df9SWerner Cornelius /* interrupt pipe definitions */
36664d5df9SWerner Cornelius /******************************/
37664d5df9SWerner Cornelius /* always 4 interrupt bytes */
38664d5df9SWerner Cornelius /* first irq byte normally 0x08 */
39664d5df9SWerner Cornelius /* second irq byte base 0x7d + below */
40664d5df9SWerner Cornelius /* third irq byte base 0x94 + below */
41664d5df9SWerner Cornelius /* fourth irq byte normally 0xee */
42664d5df9SWerner Cornelius 
43664d5df9SWerner Cornelius /* second interrupt byte */
44664d5df9SWerner Cornelius #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
45664d5df9SWerner Cornelius 
46664d5df9SWerner Cornelius /* status returned in third interrupt answer byte, inverted in data
47664d5df9SWerner Cornelius    from irq */
48664d5df9SWerner Cornelius #define CH341_BIT_CTS 0x01
49664d5df9SWerner Cornelius #define CH341_BIT_DSR 0x02
50664d5df9SWerner Cornelius #define CH341_BIT_RI  0x04
51664d5df9SWerner Cornelius #define CH341_BIT_DCD 0x08
52664d5df9SWerner Cornelius #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
53664d5df9SWerner Cornelius 
54664d5df9SWerner Cornelius /*******************************/
55664d5df9SWerner Cornelius /* baudrate calculation factor */
56664d5df9SWerner Cornelius /*******************************/
57664d5df9SWerner Cornelius #define CH341_BAUDBASE_FACTOR 1532620800
58664d5df9SWerner Cornelius #define CH341_BAUDBASE_DIVMAX 3
59664d5df9SWerner Cornelius 
60492896f0STim Small /* Break support - the information used to implement this was gleaned from
61492896f0STim Small  * the Net/FreeBSD uchcom.c driver by Takanori Watanabe.  Domo arigato.
62492896f0STim Small  */
63492896f0STim Small 
646fde8d29SAidan Thornton #define CH341_REQ_READ_VERSION 0x5F
65492896f0STim Small #define CH341_REQ_WRITE_REG    0x9A
66492896f0STim Small #define CH341_REQ_READ_REG     0x95
676fde8d29SAidan Thornton #define CH341_REQ_SERIAL_INIT  0xA1
686fde8d29SAidan Thornton #define CH341_REQ_MODEM_CTRL   0xA4
69492896f0STim Small 
706fde8d29SAidan Thornton #define CH341_REG_BREAK        0x05
716fde8d29SAidan Thornton #define CH341_REG_LCR          0x18
726fde8d29SAidan Thornton #define CH341_NBREAK_BITS      0x01
736fde8d29SAidan Thornton 
746fde8d29SAidan Thornton #define CH341_LCR_ENABLE_RX    0x80
756fde8d29SAidan Thornton #define CH341_LCR_ENABLE_TX    0x40
766fde8d29SAidan Thornton #define CH341_LCR_MARK_SPACE   0x20
776fde8d29SAidan Thornton #define CH341_LCR_PAR_EVEN     0x10
786fde8d29SAidan Thornton #define CH341_LCR_ENABLE_PAR   0x08
796fde8d29SAidan Thornton #define CH341_LCR_STOP_BITS_2  0x04
806fde8d29SAidan Thornton #define CH341_LCR_CS8          0x03
816fde8d29SAidan Thornton #define CH341_LCR_CS7          0x02
826fde8d29SAidan Thornton #define CH341_LCR_CS6          0x01
836fde8d29SAidan Thornton #define CH341_LCR_CS5          0x00
84492896f0STim Small 
857d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
866ce76104SFrank A Kingswood 	{ USB_DEVICE(0x4348, 0x5523) },
8782078234SMichael F. Robbins 	{ USB_DEVICE(0x1a86, 0x7523) },
88d0781383Swangyanqing 	{ USB_DEVICE(0x1a86, 0x5523) },
896ce76104SFrank A Kingswood 	{ },
906ce76104SFrank A Kingswood };
916ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table);
926ce76104SFrank A Kingswood 
936ce76104SFrank A Kingswood struct ch341_private {
94664d5df9SWerner Cornelius 	spinlock_t lock; /* access lock */
95664d5df9SWerner Cornelius 	unsigned baud_rate; /* set baud rate */
96664d5df9SWerner Cornelius 	u8 line_control; /* set line control value RTS/DTR */
97664d5df9SWerner Cornelius 	u8 line_status; /* active status of modem control inputs */
983cca8624SJohan Hovold 	u8 lcr;
996ce76104SFrank A Kingswood };
1006ce76104SFrank A Kingswood 
101aa91def4SNicolas PLANEL static void ch341_set_termios(struct tty_struct *tty,
102aa91def4SNicolas PLANEL 			      struct usb_serial_port *port,
103aa91def4SNicolas PLANEL 			      struct ktermios *old_termios);
104aa91def4SNicolas PLANEL 
1056ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request,
1066ce76104SFrank A Kingswood 			     u16 value, u16 index)
1076ce76104SFrank A Kingswood {
1086ce76104SFrank A Kingswood 	int r;
10979cbeeafSGreg Kroah-Hartman 
11079cbeeafSGreg Kroah-Hartman 	dev_dbg(&dev->dev, "ch341_control_out(%02x,%02x,%04x,%04x)\n",
11179cbeeafSGreg Kroah-Hartman 		USB_DIR_OUT|0x40, (int)request, (int)value, (int)index);
1126ce76104SFrank A Kingswood 
1136ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
1146ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1156ce76104SFrank A Kingswood 			    value, index, NULL, 0, DEFAULT_TIMEOUT);
1166ce76104SFrank A Kingswood 
1176ce76104SFrank A Kingswood 	return r;
1186ce76104SFrank A Kingswood }
1196ce76104SFrank A Kingswood 
1206ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev,
1216ce76104SFrank A Kingswood 			    u8 request, u16 value, u16 index,
1226ce76104SFrank A Kingswood 			    char *buf, unsigned bufsize)
1236ce76104SFrank A Kingswood {
1246ce76104SFrank A Kingswood 	int r;
12579cbeeafSGreg Kroah-Hartman 
12679cbeeafSGreg Kroah-Hartman 	dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
12779cbeeafSGreg Kroah-Hartman 		USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf,
12879cbeeafSGreg Kroah-Hartman 		(int)bufsize);
1296ce76104SFrank A Kingswood 
1306ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
1316ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1326ce76104SFrank A Kingswood 			    value, index, buf, bufsize, DEFAULT_TIMEOUT);
1336ce76104SFrank A Kingswood 	return r;
1346ce76104SFrank A Kingswood }
1356ce76104SFrank A Kingswood 
136*55fa15b5SJohan Hovold static int ch341_set_baudrate_lcr(struct usb_device *dev,
137*55fa15b5SJohan Hovold 				  struct ch341_private *priv, u8 lcr)
1386ce76104SFrank A Kingswood {
1394e46c410SAidan Thornton 	short a;
1406ce76104SFrank A Kingswood 	int r;
141664d5df9SWerner Cornelius 	unsigned long factor;
142664d5df9SWerner Cornelius 	short divisor;
1436ce76104SFrank A Kingswood 
144664d5df9SWerner Cornelius 	if (!priv->baud_rate)
1456ce76104SFrank A Kingswood 		return -EINVAL;
146664d5df9SWerner Cornelius 	factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
147664d5df9SWerner Cornelius 	divisor = CH341_BAUDBASE_DIVMAX;
148664d5df9SWerner Cornelius 
149664d5df9SWerner Cornelius 	while ((factor > 0xfff0) && divisor) {
150664d5df9SWerner Cornelius 		factor >>= 3;
151664d5df9SWerner Cornelius 		divisor--;
1526ce76104SFrank A Kingswood 	}
1536ce76104SFrank A Kingswood 
154664d5df9SWerner Cornelius 	if (factor > 0xfff0)
155664d5df9SWerner Cornelius 		return -EINVAL;
156664d5df9SWerner Cornelius 
157664d5df9SWerner Cornelius 	factor = 0x10000 - factor;
158664d5df9SWerner Cornelius 	a = (factor & 0xff00) | divisor;
159664d5df9SWerner Cornelius 
160*55fa15b5SJohan Hovold 	/*
161*55fa15b5SJohan Hovold 	 * CH341A buffers data until a full endpoint-size packet (32 bytes)
162*55fa15b5SJohan Hovold 	 * has been received unless bit 7 is set.
163*55fa15b5SJohan Hovold 	 */
164*55fa15b5SJohan Hovold 	a |= BIT(7);
165*55fa15b5SJohan Hovold 
166*55fa15b5SJohan Hovold 	r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a);
167*55fa15b5SJohan Hovold 	if (r)
168*55fa15b5SJohan Hovold 		return r;
169*55fa15b5SJohan Hovold 
170*55fa15b5SJohan Hovold 	r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, lcr);
171*55fa15b5SJohan Hovold 	if (r)
172*55fa15b5SJohan Hovold 		return r;
1736ce76104SFrank A Kingswood 
1746ce76104SFrank A Kingswood 	return r;
1756ce76104SFrank A Kingswood }
1766ce76104SFrank A Kingswood 
177664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control)
1786ce76104SFrank A Kingswood {
1796fde8d29SAidan Thornton 	return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
1806ce76104SFrank A Kingswood }
1816ce76104SFrank A Kingswood 
182664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
1836ce76104SFrank A Kingswood {
1846ce76104SFrank A Kingswood 	char *buffer;
1856ce76104SFrank A Kingswood 	int r;
1866ce76104SFrank A Kingswood 	const unsigned size = 8;
187664d5df9SWerner Cornelius 	unsigned long flags;
1886ce76104SFrank A Kingswood 
1896ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
1906ce76104SFrank A Kingswood 	if (!buffer)
1916ce76104SFrank A Kingswood 		return -ENOMEM;
1926ce76104SFrank A Kingswood 
1936fde8d29SAidan Thornton 	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
1946ce76104SFrank A Kingswood 	if (r < 0)
1956ce76104SFrank A Kingswood 		goto out;
1966ce76104SFrank A Kingswood 
197664d5df9SWerner Cornelius 	/* setup the private status if available */
198664d5df9SWerner Cornelius 	if (r == 2) {
1996ce76104SFrank A Kingswood 		r = 0;
200664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
201664d5df9SWerner Cornelius 		priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
202664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
203664d5df9SWerner Cornelius 	} else
204664d5df9SWerner Cornelius 		r = -EPROTO;
2056ce76104SFrank A Kingswood 
2066ce76104SFrank A Kingswood out:	kfree(buffer);
2076ce76104SFrank A Kingswood 	return r;
2086ce76104SFrank A Kingswood }
2096ce76104SFrank A Kingswood 
2106ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */
2116ce76104SFrank A Kingswood 
21293b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
2136ce76104SFrank A Kingswood {
2146ce76104SFrank A Kingswood 	char *buffer;
2156ce76104SFrank A Kingswood 	int r;
2166ce76104SFrank A Kingswood 	const unsigned size = 8;
2176ce76104SFrank A Kingswood 
2186ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
2196ce76104SFrank A Kingswood 	if (!buffer)
2206ce76104SFrank A Kingswood 		return -ENOMEM;
2216ce76104SFrank A Kingswood 
2226ce76104SFrank A Kingswood 	/* expect two bytes 0x27 0x00 */
2236fde8d29SAidan Thornton 	r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
2246ce76104SFrank A Kingswood 	if (r < 0)
2256ce76104SFrank A Kingswood 		goto out;
226a98b6900SAidan Thornton 	dev_dbg(&dev->dev, "Chip version: 0x%02x\n", buffer[0]);
2276ce76104SFrank A Kingswood 
2286fde8d29SAidan Thornton 	r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
2296ce76104SFrank A Kingswood 	if (r < 0)
2306ce76104SFrank A Kingswood 		goto out;
2316ce76104SFrank A Kingswood 
2326ce76104SFrank A Kingswood 	/* expect two bytes 0x56 0x00 */
2336fde8d29SAidan Thornton 	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x2518, 0, buffer, size);
2346ce76104SFrank A Kingswood 	if (r < 0)
2356ce76104SFrank A Kingswood 		goto out;
2366ce76104SFrank A Kingswood 
2376fde8d29SAidan Thornton 	r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, 0x0050);
2386ce76104SFrank A Kingswood 	if (r < 0)
2396ce76104SFrank A Kingswood 		goto out;
2406ce76104SFrank A Kingswood 
2416ce76104SFrank A Kingswood 	/* expect 0xff 0xee */
242664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2436ce76104SFrank A Kingswood 	if (r < 0)
2446ce76104SFrank A Kingswood 		goto out;
2456ce76104SFrank A Kingswood 
246*55fa15b5SJohan Hovold 	r = ch341_set_baudrate_lcr(dev, priv, priv->lcr);
2476ce76104SFrank A Kingswood 	if (r < 0)
2486ce76104SFrank A Kingswood 		goto out;
2496ce76104SFrank A Kingswood 
250664d5df9SWerner Cornelius 	r = ch341_set_handshake(dev, priv->line_control);
2516ce76104SFrank A Kingswood 	if (r < 0)
2526ce76104SFrank A Kingswood 		goto out;
2536ce76104SFrank A Kingswood 
2546ce76104SFrank A Kingswood 	/* expect 0x9f 0xee */
255664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2566ce76104SFrank A Kingswood 
2576ce76104SFrank A Kingswood out:	kfree(buffer);
2586ce76104SFrank A Kingswood 	return r;
2596ce76104SFrank A Kingswood }
2606ce76104SFrank A Kingswood 
261456c5be5SJohan Hovold static int ch341_port_probe(struct usb_serial_port *port)
2626ce76104SFrank A Kingswood {
2636ce76104SFrank A Kingswood 	struct ch341_private *priv;
2646ce76104SFrank A Kingswood 	int r;
2656ce76104SFrank A Kingswood 
2666ce76104SFrank A Kingswood 	priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
2676ce76104SFrank A Kingswood 	if (!priv)
2686ce76104SFrank A Kingswood 		return -ENOMEM;
2696ce76104SFrank A Kingswood 
270664d5df9SWerner Cornelius 	spin_lock_init(&priv->lock);
2716ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
2726ce76104SFrank A Kingswood 
273456c5be5SJohan Hovold 	r = ch341_configure(port->serial->dev, priv);
2746ce76104SFrank A Kingswood 	if (r < 0)
2756ce76104SFrank A Kingswood 		goto error;
2766ce76104SFrank A Kingswood 
277456c5be5SJohan Hovold 	usb_set_serial_port_data(port, priv);
2786ce76104SFrank A Kingswood 	return 0;
2796ce76104SFrank A Kingswood 
2806ce76104SFrank A Kingswood error:	kfree(priv);
2816ce76104SFrank A Kingswood 	return r;
2826ce76104SFrank A Kingswood }
2836ce76104SFrank A Kingswood 
284456c5be5SJohan Hovold static int ch341_port_remove(struct usb_serial_port *port)
285456c5be5SJohan Hovold {
286456c5be5SJohan Hovold 	struct ch341_private *priv;
287456c5be5SJohan Hovold 
288456c5be5SJohan Hovold 	priv = usb_get_serial_port_data(port);
289456c5be5SJohan Hovold 	kfree(priv);
290456c5be5SJohan Hovold 
291456c5be5SJohan Hovold 	return 0;
292456c5be5SJohan Hovold }
293456c5be5SJohan Hovold 
294335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port)
295335f8514SAlan Cox {
296335f8514SAlan Cox 	struct ch341_private *priv = usb_get_serial_port_data(port);
297335f8514SAlan Cox 	if (priv->line_status & CH341_BIT_DCD)
298335f8514SAlan Cox 		return 1;
299335f8514SAlan Cox 	return 0;
300335f8514SAlan Cox }
301335f8514SAlan Cox 
302335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on)
303664d5df9SWerner Cornelius {
304664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
305664d5df9SWerner Cornelius 	unsigned long flags;
306664d5df9SWerner Cornelius 
307335f8514SAlan Cox 	/* drop DTR and RTS */
308335f8514SAlan Cox 	spin_lock_irqsave(&priv->lock, flags);
309335f8514SAlan Cox 	if (on)
310335f8514SAlan Cox 		priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
311335f8514SAlan Cox 	else
312335f8514SAlan Cox 		priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
313335f8514SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
314335f8514SAlan Cox 	ch341_set_handshake(port->serial->dev, priv->line_control);
315335f8514SAlan Cox }
316335f8514SAlan Cox 
317335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port)
318335f8514SAlan Cox {
319f26788daSJohan Hovold 	usb_serial_generic_close(port);
320664d5df9SWerner Cornelius 	usb_kill_urb(port->interrupt_in_urb);
321664d5df9SWerner Cornelius }
322664d5df9SWerner Cornelius 
323664d5df9SWerner Cornelius 
3246ce76104SFrank A Kingswood /* open this device, set default parameters */
325a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
3266ce76104SFrank A Kingswood {
3276ce76104SFrank A Kingswood 	struct usb_serial *serial = port->serial;
328456c5be5SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
3296ce76104SFrank A Kingswood 	int r;
3306ce76104SFrank A Kingswood 
3316ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
3326ce76104SFrank A Kingswood 	if (r)
333f2950b78SJohan Hovold 		return r;
3346ce76104SFrank A Kingswood 
335aa91def4SNicolas PLANEL 	if (tty)
336aa91def4SNicolas PLANEL 		ch341_set_termios(tty, port, NULL);
3376ce76104SFrank A Kingswood 
338d9a38a87SJohan Hovold 	dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
339664d5df9SWerner Cornelius 	r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
340664d5df9SWerner Cornelius 	if (r) {
341d9a38a87SJohan Hovold 		dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
342d9a38a87SJohan Hovold 			__func__, r);
343f2950b78SJohan Hovold 		return r;
344664d5df9SWerner Cornelius 	}
345664d5df9SWerner Cornelius 
346a509a7e4SAlan Cox 	r = usb_serial_generic_open(tty, port);
347f2950b78SJohan Hovold 	if (r)
348f2950b78SJohan Hovold 		goto err_kill_interrupt_urb;
3496ce76104SFrank A Kingswood 
350f2950b78SJohan Hovold 	return 0;
351f2950b78SJohan Hovold 
352f2950b78SJohan Hovold err_kill_interrupt_urb:
353f2950b78SJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
354f2950b78SJohan Hovold 
355f2950b78SJohan Hovold 	return r;
3566ce76104SFrank A Kingswood }
3576ce76104SFrank A Kingswood 
3586ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and
3596ce76104SFrank A Kingswood  * tty->termios contains the new setting to be used.
3606ce76104SFrank A Kingswood  */
36195da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty,
36295da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3636ce76104SFrank A Kingswood {
3646ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(port);
3656ce76104SFrank A Kingswood 	unsigned baud_rate;
366664d5df9SWerner Cornelius 	unsigned long flags;
3674e46c410SAidan Thornton 	unsigned char ctrl;
3684e46c410SAidan Thornton 	int r;
3694e46c410SAidan Thornton 
3704e46c410SAidan Thornton 	/* redundant changes may cause the chip to lose bytes */
3714e46c410SAidan Thornton 	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
3724e46c410SAidan Thornton 		return;
3736ce76104SFrank A Kingswood 
3746ce76104SFrank A Kingswood 	baud_rate = tty_get_baud_rate(tty);
3756ce76104SFrank A Kingswood 
376ba781bdfSAidan Thornton 	ctrl = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
377664d5df9SWerner Cornelius 
378ba781bdfSAidan Thornton 	switch (C_CSIZE(tty)) {
379ba781bdfSAidan Thornton 	case CS5:
380ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_CS5;
381ba781bdfSAidan Thornton 		break;
382ba781bdfSAidan Thornton 	case CS6:
383ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_CS6;
384ba781bdfSAidan Thornton 		break;
385ba781bdfSAidan Thornton 	case CS7:
386ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_CS7;
387ba781bdfSAidan Thornton 		break;
388ba781bdfSAidan Thornton 	case CS8:
389ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_CS8;
390ba781bdfSAidan Thornton 		break;
391ba781bdfSAidan Thornton 	}
392ba781bdfSAidan Thornton 
393ba781bdfSAidan Thornton 	if (C_PARENB(tty)) {
394ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_ENABLE_PAR;
395ba781bdfSAidan Thornton 		if (C_PARODD(tty) == 0)
396ba781bdfSAidan Thornton 			ctrl |= CH341_LCR_PAR_EVEN;
397ba781bdfSAidan Thornton 		if (C_CMSPAR(tty))
398ba781bdfSAidan Thornton 			ctrl |= CH341_LCR_MARK_SPACE;
399ba781bdfSAidan Thornton 	}
400ba781bdfSAidan Thornton 
401ba781bdfSAidan Thornton 	if (C_CSTOPB(tty))
402ba781bdfSAidan Thornton 		ctrl |= CH341_LCR_STOP_BITS_2;
4034e46c410SAidan Thornton 
404664d5df9SWerner Cornelius 	if (baud_rate) {
405a20047f3SJohan Hovold 		priv->baud_rate = baud_rate;
406a20047f3SJohan Hovold 
407*55fa15b5SJohan Hovold 		r = ch341_set_baudrate_lcr(port->serial->dev, priv, ctrl);
4084e46c410SAidan Thornton 		if (r < 0 && old_termios) {
4094e46c410SAidan Thornton 			priv->baud_rate = tty_termios_baud_rate(old_termios);
4104e46c410SAidan Thornton 			tty_termios_copy_hw(&tty->termios, old_termios);
4113cca8624SJohan Hovold 		} else if (r == 0) {
4123cca8624SJohan Hovold 			priv->lcr = ctrl;
4134e46c410SAidan Thornton 		}
4146ce76104SFrank A Kingswood 	}
4156ce76104SFrank A Kingswood 
416030ee7aeSJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
417030ee7aeSJohan Hovold 	if (C_BAUD(tty) == B0)
418030ee7aeSJohan Hovold 		priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
419030ee7aeSJohan Hovold 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
420030ee7aeSJohan Hovold 		priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
421030ee7aeSJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
4226ce76104SFrank A Kingswood 
423030ee7aeSJohan Hovold 	ch341_set_handshake(port->serial->dev, priv->line_control);
424664d5df9SWerner Cornelius }
42573f59308SAlan Cox 
426492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state)
427492896f0STim Small {
428492896f0STim Small 	const uint16_t ch341_break_reg =
4296fde8d29SAidan Thornton 			((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
430492896f0STim Small 	struct usb_serial_port *port = tty->driver_data;
431492896f0STim Small 	int r;
432492896f0STim Small 	uint16_t reg_contents;
433f2b5cc83SJohan Hovold 	uint8_t *break_reg;
434492896f0STim Small 
435f2b5cc83SJohan Hovold 	break_reg = kmalloc(2, GFP_KERNEL);
43610c642d0SJohan Hovold 	if (!break_reg)
437f2b5cc83SJohan Hovold 		return;
438f2b5cc83SJohan Hovold 
439492896f0STim Small 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
440f2b5cc83SJohan Hovold 			ch341_break_reg, 0, break_reg, 2);
441492896f0STim Small 	if (r < 0) {
4426a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
4436a9b15feSJohan Hovold 				__func__, r);
444f2b5cc83SJohan Hovold 		goto out;
445492896f0STim Small 	}
44679cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
447492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
448492896f0STim Small 	if (break_state != 0) {
44979cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
4506fde8d29SAidan Thornton 		break_reg[0] &= ~CH341_NBREAK_BITS;
4516fde8d29SAidan Thornton 		break_reg[1] &= ~CH341_LCR_ENABLE_TX;
452492896f0STim Small 	} else {
45379cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
4546fde8d29SAidan Thornton 		break_reg[0] |= CH341_NBREAK_BITS;
4556fde8d29SAidan Thornton 		break_reg[1] |= CH341_LCR_ENABLE_TX;
456492896f0STim Small 	}
45779cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
458492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
4595be796f0SJohan Hovold 	reg_contents = get_unaligned_le16(break_reg);
460492896f0STim Small 	r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
461492896f0STim Small 			ch341_break_reg, reg_contents);
462492896f0STim Small 	if (r < 0)
4636a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
4646a9b15feSJohan Hovold 				__func__, r);
465f2b5cc83SJohan Hovold out:
466f2b5cc83SJohan Hovold 	kfree(break_reg);
467492896f0STim Small }
468492896f0STim Small 
46920b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty,
470664d5df9SWerner Cornelius 			  unsigned int set, unsigned int clear)
471664d5df9SWerner Cornelius {
472664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
473664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
474664d5df9SWerner Cornelius 	unsigned long flags;
475664d5df9SWerner Cornelius 	u8 control;
476664d5df9SWerner Cornelius 
477664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
478664d5df9SWerner Cornelius 	if (set & TIOCM_RTS)
479664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_RTS;
480664d5df9SWerner Cornelius 	if (set & TIOCM_DTR)
481664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_DTR;
482664d5df9SWerner Cornelius 	if (clear & TIOCM_RTS)
483664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_RTS;
484664d5df9SWerner Cornelius 	if (clear & TIOCM_DTR)
485664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_DTR;
486664d5df9SWerner Cornelius 	control = priv->line_control;
487664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
488664d5df9SWerner Cornelius 
489664d5df9SWerner Cornelius 	return ch341_set_handshake(port->serial->dev, control);
490664d5df9SWerner Cornelius }
491664d5df9SWerner Cornelius 
492ac035628SJohan Hovold static void ch341_update_line_status(struct usb_serial_port *port,
493ac035628SJohan Hovold 					unsigned char *data, size_t len)
494ac035628SJohan Hovold {
495ac035628SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
496b770081fSJohan Hovold 	struct tty_struct *tty;
497ac035628SJohan Hovold 	unsigned long flags;
498b770081fSJohan Hovold 	u8 status;
499b770081fSJohan Hovold 	u8 delta;
500ac035628SJohan Hovold 
501ac035628SJohan Hovold 	if (len < 4)
502ac035628SJohan Hovold 		return;
503ac035628SJohan Hovold 
504b770081fSJohan Hovold 	status = ~data[2] & CH341_BITS_MODEM_STAT;
505b770081fSJohan Hovold 
506ac035628SJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
507b770081fSJohan Hovold 	delta = status ^ priv->line_status;
508b770081fSJohan Hovold 	priv->line_status = status;
509ac035628SJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
510ac035628SJohan Hovold 
511fd74b0b1SJohan Hovold 	if (data[1] & CH341_MULT_STAT)
512fd74b0b1SJohan Hovold 		dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
513fd74b0b1SJohan Hovold 
514d984fe91SJohan Hovold 	if (!delta)
515d984fe91SJohan Hovold 		return;
516d984fe91SJohan Hovold 
5175e409a26SJohan Hovold 	if (delta & CH341_BIT_CTS)
5185e409a26SJohan Hovold 		port->icount.cts++;
5195e409a26SJohan Hovold 	if (delta & CH341_BIT_DSR)
5205e409a26SJohan Hovold 		port->icount.dsr++;
5215e409a26SJohan Hovold 	if (delta & CH341_BIT_RI)
5225e409a26SJohan Hovold 		port->icount.rng++;
523b770081fSJohan Hovold 	if (delta & CH341_BIT_DCD) {
5245e409a26SJohan Hovold 		port->icount.dcd++;
525b770081fSJohan Hovold 		tty = tty_port_tty_get(&port->port);
526b770081fSJohan Hovold 		if (tty) {
527ac035628SJohan Hovold 			usb_serial_handle_dcd_change(port, tty,
528b770081fSJohan Hovold 						status & CH341_BIT_DCD);
529ac035628SJohan Hovold 			tty_kref_put(tty);
530ac035628SJohan Hovold 		}
531b770081fSJohan Hovold 	}
532ac035628SJohan Hovold 
533ac035628SJohan Hovold 	wake_up_interruptible(&port->port.delta_msr_wait);
534ac035628SJohan Hovold }
535ac035628SJohan Hovold 
536664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb)
537664d5df9SWerner Cornelius {
538271ec2d2SJohan Hovold 	struct usb_serial_port *port = urb->context;
539664d5df9SWerner Cornelius 	unsigned char *data = urb->transfer_buffer;
540271ec2d2SJohan Hovold 	unsigned int len = urb->actual_length;
541664d5df9SWerner Cornelius 	int status;
542664d5df9SWerner Cornelius 
543664d5df9SWerner Cornelius 	switch (urb->status) {
544664d5df9SWerner Cornelius 	case 0:
545664d5df9SWerner Cornelius 		/* success */
546664d5df9SWerner Cornelius 		break;
547664d5df9SWerner Cornelius 	case -ECONNRESET:
548664d5df9SWerner Cornelius 	case -ENOENT:
549664d5df9SWerner Cornelius 	case -ESHUTDOWN:
550664d5df9SWerner Cornelius 		/* this urb is terminated, clean up */
551271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
55279cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
553664d5df9SWerner Cornelius 		return;
554664d5df9SWerner Cornelius 	default:
555271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
55679cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
557664d5df9SWerner Cornelius 		goto exit;
558664d5df9SWerner Cornelius 	}
559664d5df9SWerner Cornelius 
560271ec2d2SJohan Hovold 	usb_serial_debug_data(&port->dev, __func__, len, data);
561271ec2d2SJohan Hovold 	ch341_update_line_status(port, data, len);
562664d5df9SWerner Cornelius exit:
563664d5df9SWerner Cornelius 	status = usb_submit_urb(urb, GFP_ATOMIC);
564271ec2d2SJohan Hovold 	if (status) {
565271ec2d2SJohan Hovold 		dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
566664d5df9SWerner Cornelius 			__func__, status);
567664d5df9SWerner Cornelius 	}
568271ec2d2SJohan Hovold }
569664d5df9SWerner Cornelius 
57060b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty)
571664d5df9SWerner Cornelius {
572664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
573664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
574664d5df9SWerner Cornelius 	unsigned long flags;
575664d5df9SWerner Cornelius 	u8 mcr;
576664d5df9SWerner Cornelius 	u8 status;
577664d5df9SWerner Cornelius 	unsigned int result;
578664d5df9SWerner Cornelius 
579664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
580664d5df9SWerner Cornelius 	mcr = priv->line_control;
581664d5df9SWerner Cornelius 	status = priv->line_status;
582664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
583664d5df9SWerner Cornelius 
584664d5df9SWerner Cornelius 	result = ((mcr & CH341_BIT_DTR)		? TIOCM_DTR : 0)
585664d5df9SWerner Cornelius 		  | ((mcr & CH341_BIT_RTS)	? TIOCM_RTS : 0)
586664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_CTS)	? TIOCM_CTS : 0)
587664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DSR)	? TIOCM_DSR : 0)
588664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_RI)	? TIOCM_RI  : 0)
589664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DCD)	? TIOCM_CD  : 0);
590664d5df9SWerner Cornelius 
59179cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
592664d5df9SWerner Cornelius 
593664d5df9SWerner Cornelius 	return result;
5946ce76104SFrank A Kingswood }
5956ce76104SFrank A Kingswood 
596622b80cfSGreg Kroah-Hartman static int ch341_reset_resume(struct usb_serial *serial)
5971ded7ea4SMing Lei {
598ce5e2928SJohan Hovold 	struct usb_serial_port *port = serial->port[0];
599ce5e2928SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
600ce5e2928SJohan Hovold 	int ret;
6011ded7ea4SMing Lei 
6021ded7ea4SMing Lei 	/* reconfigure ch341 serial port after bus-reset */
6032bfd1c96SGreg Kroah-Hartman 	ch341_configure(serial->dev, priv);
6041ded7ea4SMing Lei 
605ce5e2928SJohan Hovold 	if (tty_port_initialized(&port->port)) {
606ce5e2928SJohan Hovold 		ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
607ce5e2928SJohan Hovold 		if (ret) {
608ce5e2928SJohan Hovold 			dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
609ce5e2928SJohan Hovold 				ret);
610ce5e2928SJohan Hovold 			return ret;
611ce5e2928SJohan Hovold 		}
612ce5e2928SJohan Hovold 	}
613ce5e2928SJohan Hovold 
614ce5e2928SJohan Hovold 	return usb_serial_generic_resume(serial);
6151ded7ea4SMing Lei }
6161ded7ea4SMing Lei 
6176ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = {
6186ce76104SFrank A Kingswood 	.driver = {
6196ce76104SFrank A Kingswood 		.owner	= THIS_MODULE,
6206ce76104SFrank A Kingswood 		.name	= "ch341-uart",
6216ce76104SFrank A Kingswood 	},
6226ce76104SFrank A Kingswood 	.id_table          = id_table,
6236ce76104SFrank A Kingswood 	.num_ports         = 1,
6246ce76104SFrank A Kingswood 	.open              = ch341_open,
625335f8514SAlan Cox 	.dtr_rts	   = ch341_dtr_rts,
626335f8514SAlan Cox 	.carrier_raised	   = ch341_carrier_raised,
627664d5df9SWerner Cornelius 	.close             = ch341_close,
6286ce76104SFrank A Kingswood 	.set_termios       = ch341_set_termios,
629492896f0STim Small 	.break_ctl         = ch341_break_ctl,
630664d5df9SWerner Cornelius 	.tiocmget          = ch341_tiocmget,
631664d5df9SWerner Cornelius 	.tiocmset          = ch341_tiocmset,
6325e409a26SJohan Hovold 	.tiocmiwait        = usb_serial_generic_tiocmiwait,
633664d5df9SWerner Cornelius 	.read_int_callback = ch341_read_int_callback,
634456c5be5SJohan Hovold 	.port_probe        = ch341_port_probe,
635456c5be5SJohan Hovold 	.port_remove       = ch341_port_remove,
6361c1eaba8SGreg Kroah-Hartman 	.reset_resume      = ch341_reset_resume,
6376ce76104SFrank A Kingswood };
6386ce76104SFrank A Kingswood 
63908a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
64008a4f6bcSAlan Stern 	&ch341_device, NULL
64108a4f6bcSAlan Stern };
64208a4f6bcSAlan Stern 
64368e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
6446ce76104SFrank A Kingswood 
6456ce76104SFrank A Kingswood MODULE_LICENSE("GPL");
646