xref: /linux/drivers/usb/serial/ch341.c (revision aa91def41a7bb1fd65492934ce6bea19202b6080)
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 
64492896f0STim Small #define CH341_REQ_WRITE_REG    0x9A
65492896f0STim Small #define CH341_REQ_READ_REG     0x95
66492896f0STim Small #define CH341_REG_BREAK1       0x05
67492896f0STim Small #define CH341_REG_BREAK2       0x18
68492896f0STim Small #define CH341_NBREAK_BITS_REG1 0x01
69492896f0STim Small #define CH341_NBREAK_BITS_REG2 0x40
70492896f0STim Small 
71492896f0STim Small 
727d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
736ce76104SFrank A Kingswood 	{ USB_DEVICE(0x4348, 0x5523) },
7482078234SMichael F. Robbins 	{ USB_DEVICE(0x1a86, 0x7523) },
75d0781383Swangyanqing 	{ USB_DEVICE(0x1a86, 0x5523) },
766ce76104SFrank A Kingswood 	{ },
776ce76104SFrank A Kingswood };
786ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table);
796ce76104SFrank A Kingswood 
806ce76104SFrank A Kingswood struct ch341_private {
81664d5df9SWerner Cornelius 	spinlock_t lock; /* access lock */
82664d5df9SWerner Cornelius 	unsigned baud_rate; /* set baud rate */
83664d5df9SWerner Cornelius 	u8 line_control; /* set line control value RTS/DTR */
84664d5df9SWerner Cornelius 	u8 line_status; /* active status of modem control inputs */
856ce76104SFrank A Kingswood };
866ce76104SFrank A Kingswood 
87*aa91def4SNicolas PLANEL static void ch341_set_termios(struct tty_struct *tty,
88*aa91def4SNicolas PLANEL 			      struct usb_serial_port *port,
89*aa91def4SNicolas PLANEL 			      struct ktermios *old_termios);
90*aa91def4SNicolas PLANEL 
916ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request,
926ce76104SFrank A Kingswood 			     u16 value, u16 index)
936ce76104SFrank A Kingswood {
946ce76104SFrank A Kingswood 	int r;
9579cbeeafSGreg Kroah-Hartman 
9679cbeeafSGreg Kroah-Hartman 	dev_dbg(&dev->dev, "ch341_control_out(%02x,%02x,%04x,%04x)\n",
9779cbeeafSGreg Kroah-Hartman 		USB_DIR_OUT|0x40, (int)request, (int)value, (int)index);
986ce76104SFrank A Kingswood 
996ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
1006ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1016ce76104SFrank A Kingswood 			    value, index, NULL, 0, DEFAULT_TIMEOUT);
1026ce76104SFrank A Kingswood 
1036ce76104SFrank A Kingswood 	return r;
1046ce76104SFrank A Kingswood }
1056ce76104SFrank A Kingswood 
1066ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev,
1076ce76104SFrank A Kingswood 			    u8 request, u16 value, u16 index,
1086ce76104SFrank A Kingswood 			    char *buf, unsigned bufsize)
1096ce76104SFrank A Kingswood {
1106ce76104SFrank A Kingswood 	int r;
11179cbeeafSGreg Kroah-Hartman 
11279cbeeafSGreg Kroah-Hartman 	dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
11379cbeeafSGreg Kroah-Hartman 		USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf,
11479cbeeafSGreg Kroah-Hartman 		(int)bufsize);
1156ce76104SFrank A Kingswood 
1166ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
1176ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1186ce76104SFrank A Kingswood 			    value, index, buf, bufsize, DEFAULT_TIMEOUT);
1196ce76104SFrank A Kingswood 	return r;
1206ce76104SFrank A Kingswood }
1216ce76104SFrank A Kingswood 
12293b6497dSAdrian Bunk static int ch341_set_baudrate(struct usb_device *dev,
12393b6497dSAdrian Bunk 			      struct ch341_private *priv)
1246ce76104SFrank A Kingswood {
1256ce76104SFrank A Kingswood 	short a, b;
1266ce76104SFrank A Kingswood 	int r;
127664d5df9SWerner Cornelius 	unsigned long factor;
128664d5df9SWerner Cornelius 	short divisor;
1296ce76104SFrank A Kingswood 
130664d5df9SWerner Cornelius 	if (!priv->baud_rate)
1316ce76104SFrank A Kingswood 		return -EINVAL;
132664d5df9SWerner Cornelius 	factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
133664d5df9SWerner Cornelius 	divisor = CH341_BAUDBASE_DIVMAX;
134664d5df9SWerner Cornelius 
135664d5df9SWerner Cornelius 	while ((factor > 0xfff0) && divisor) {
136664d5df9SWerner Cornelius 		factor >>= 3;
137664d5df9SWerner Cornelius 		divisor--;
1386ce76104SFrank A Kingswood 	}
1396ce76104SFrank A Kingswood 
140664d5df9SWerner Cornelius 	if (factor > 0xfff0)
141664d5df9SWerner Cornelius 		return -EINVAL;
142664d5df9SWerner Cornelius 
143664d5df9SWerner Cornelius 	factor = 0x10000 - factor;
144664d5df9SWerner Cornelius 	a = (factor & 0xff00) | divisor;
145664d5df9SWerner Cornelius 	b = factor & 0xff;
146664d5df9SWerner Cornelius 
1476ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0x9a, 0x1312, a);
1486ce76104SFrank A Kingswood 	if (!r)
1496ce76104SFrank A Kingswood 		r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
1506ce76104SFrank A Kingswood 
1516ce76104SFrank A Kingswood 	return r;
1526ce76104SFrank A Kingswood }
1536ce76104SFrank A Kingswood 
154664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control)
1556ce76104SFrank A Kingswood {
156664d5df9SWerner Cornelius 	return ch341_control_out(dev, 0xa4, ~control, 0);
1576ce76104SFrank A Kingswood }
1586ce76104SFrank A Kingswood 
159664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
1606ce76104SFrank A Kingswood {
1616ce76104SFrank A Kingswood 	char *buffer;
1626ce76104SFrank A Kingswood 	int r;
1636ce76104SFrank A Kingswood 	const unsigned size = 8;
164664d5df9SWerner Cornelius 	unsigned long flags;
1656ce76104SFrank A Kingswood 
1666ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
1676ce76104SFrank A Kingswood 	if (!buffer)
1686ce76104SFrank A Kingswood 		return -ENOMEM;
1696ce76104SFrank A Kingswood 
1706ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
1716ce76104SFrank A Kingswood 	if (r < 0)
1726ce76104SFrank A Kingswood 		goto out;
1736ce76104SFrank A Kingswood 
174664d5df9SWerner Cornelius 	/* setup the private status if available */
175664d5df9SWerner Cornelius 	if (r == 2) {
1766ce76104SFrank A Kingswood 		r = 0;
177664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
178664d5df9SWerner Cornelius 		priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
179664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
180664d5df9SWerner Cornelius 	} else
181664d5df9SWerner Cornelius 		r = -EPROTO;
1826ce76104SFrank A Kingswood 
1836ce76104SFrank A Kingswood out:	kfree(buffer);
1846ce76104SFrank A Kingswood 	return r;
1856ce76104SFrank A Kingswood }
1866ce76104SFrank A Kingswood 
1876ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */
1886ce76104SFrank A Kingswood 
18993b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
1906ce76104SFrank A Kingswood {
1916ce76104SFrank A Kingswood 	char *buffer;
1926ce76104SFrank A Kingswood 	int r;
1936ce76104SFrank A Kingswood 	const unsigned size = 8;
1946ce76104SFrank A Kingswood 
1956ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
1966ce76104SFrank A Kingswood 	if (!buffer)
1976ce76104SFrank A Kingswood 		return -ENOMEM;
1986ce76104SFrank A Kingswood 
1996ce76104SFrank A Kingswood 	/* expect two bytes 0x27 0x00 */
2006ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
2016ce76104SFrank A Kingswood 	if (r < 0)
2026ce76104SFrank A Kingswood 		goto out;
2036ce76104SFrank A Kingswood 
2046ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0, 0);
2056ce76104SFrank A Kingswood 	if (r < 0)
2066ce76104SFrank A Kingswood 		goto out;
2076ce76104SFrank A Kingswood 
2086ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2096ce76104SFrank A Kingswood 	if (r < 0)
2106ce76104SFrank A Kingswood 		goto out;
2116ce76104SFrank A Kingswood 
2126ce76104SFrank A Kingswood 	/* expect two bytes 0x56 0x00 */
2136ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
2146ce76104SFrank A Kingswood 	if (r < 0)
2156ce76104SFrank A Kingswood 		goto out;
2166ce76104SFrank A Kingswood 
2176ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
2186ce76104SFrank A Kingswood 	if (r < 0)
2196ce76104SFrank A Kingswood 		goto out;
2206ce76104SFrank A Kingswood 
2216ce76104SFrank A Kingswood 	/* expect 0xff 0xee */
222664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2236ce76104SFrank A Kingswood 	if (r < 0)
2246ce76104SFrank A Kingswood 		goto out;
2256ce76104SFrank A Kingswood 
2266ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
2276ce76104SFrank A Kingswood 	if (r < 0)
2286ce76104SFrank A Kingswood 		goto out;
2296ce76104SFrank A Kingswood 
2306ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2316ce76104SFrank A Kingswood 	if (r < 0)
2326ce76104SFrank A Kingswood 		goto out;
2336ce76104SFrank A Kingswood 
234664d5df9SWerner Cornelius 	r = ch341_set_handshake(dev, priv->line_control);
2356ce76104SFrank A Kingswood 	if (r < 0)
2366ce76104SFrank A Kingswood 		goto out;
2376ce76104SFrank A Kingswood 
2386ce76104SFrank A Kingswood 	/* expect 0x9f 0xee */
239664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2406ce76104SFrank A Kingswood 
2416ce76104SFrank A Kingswood out:	kfree(buffer);
2426ce76104SFrank A Kingswood 	return r;
2436ce76104SFrank A Kingswood }
2446ce76104SFrank A Kingswood 
245456c5be5SJohan Hovold static int ch341_port_probe(struct usb_serial_port *port)
2466ce76104SFrank A Kingswood {
2476ce76104SFrank A Kingswood 	struct ch341_private *priv;
2486ce76104SFrank A Kingswood 	int r;
2496ce76104SFrank A Kingswood 
2506ce76104SFrank A Kingswood 	priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
2516ce76104SFrank A Kingswood 	if (!priv)
2526ce76104SFrank A Kingswood 		return -ENOMEM;
2536ce76104SFrank A Kingswood 
254664d5df9SWerner Cornelius 	spin_lock_init(&priv->lock);
2556ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
256664d5df9SWerner Cornelius 	priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
2576ce76104SFrank A Kingswood 
258456c5be5SJohan Hovold 	r = ch341_configure(port->serial->dev, priv);
2596ce76104SFrank A Kingswood 	if (r < 0)
2606ce76104SFrank A Kingswood 		goto error;
2616ce76104SFrank A Kingswood 
262456c5be5SJohan Hovold 	usb_set_serial_port_data(port, priv);
2636ce76104SFrank A Kingswood 	return 0;
2646ce76104SFrank A Kingswood 
2656ce76104SFrank A Kingswood error:	kfree(priv);
2666ce76104SFrank A Kingswood 	return r;
2676ce76104SFrank A Kingswood }
2686ce76104SFrank A Kingswood 
269456c5be5SJohan Hovold static int ch341_port_remove(struct usb_serial_port *port)
270456c5be5SJohan Hovold {
271456c5be5SJohan Hovold 	struct ch341_private *priv;
272456c5be5SJohan Hovold 
273456c5be5SJohan Hovold 	priv = usb_get_serial_port_data(port);
274456c5be5SJohan Hovold 	kfree(priv);
275456c5be5SJohan Hovold 
276456c5be5SJohan Hovold 	return 0;
277456c5be5SJohan Hovold }
278456c5be5SJohan Hovold 
279335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port)
280335f8514SAlan Cox {
281335f8514SAlan Cox 	struct ch341_private *priv = usb_get_serial_port_data(port);
282335f8514SAlan Cox 	if (priv->line_status & CH341_BIT_DCD)
283335f8514SAlan Cox 		return 1;
284335f8514SAlan Cox 	return 0;
285335f8514SAlan Cox }
286335f8514SAlan Cox 
287335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on)
288664d5df9SWerner Cornelius {
289664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
290664d5df9SWerner Cornelius 	unsigned long flags;
291664d5df9SWerner Cornelius 
292335f8514SAlan Cox 	/* drop DTR and RTS */
293335f8514SAlan Cox 	spin_lock_irqsave(&priv->lock, flags);
294335f8514SAlan Cox 	if (on)
295335f8514SAlan Cox 		priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
296335f8514SAlan Cox 	else
297335f8514SAlan Cox 		priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
298335f8514SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
299335f8514SAlan Cox 	ch341_set_handshake(port->serial->dev, priv->line_control);
300335f8514SAlan Cox }
301335f8514SAlan Cox 
302335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port)
303335f8514SAlan Cox {
304f26788daSJohan Hovold 	usb_serial_generic_close(port);
305664d5df9SWerner Cornelius 	usb_kill_urb(port->interrupt_in_urb);
306664d5df9SWerner Cornelius }
307664d5df9SWerner Cornelius 
308664d5df9SWerner Cornelius 
3096ce76104SFrank A Kingswood /* open this device, set default parameters */
310a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
3116ce76104SFrank A Kingswood {
3126ce76104SFrank A Kingswood 	struct usb_serial *serial = port->serial;
313456c5be5SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
3146ce76104SFrank A Kingswood 	int r;
3156ce76104SFrank A Kingswood 
3166ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
3176ce76104SFrank A Kingswood 	if (r)
3186ce76104SFrank A Kingswood 		goto out;
3196ce76104SFrank A Kingswood 
320*aa91def4SNicolas PLANEL 	if (tty)
321*aa91def4SNicolas PLANEL 		ch341_set_termios(tty, port, NULL);
3226ce76104SFrank A Kingswood 
323d9a38a87SJohan Hovold 	dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
324664d5df9SWerner Cornelius 	r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
325664d5df9SWerner Cornelius 	if (r) {
326d9a38a87SJohan Hovold 		dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
327d9a38a87SJohan Hovold 			__func__, r);
328335f8514SAlan Cox 		ch341_close(port);
32906946a66SJohan Hovold 		goto out;
330664d5df9SWerner Cornelius 	}
331664d5df9SWerner Cornelius 
332a509a7e4SAlan Cox 	r = usb_serial_generic_open(tty, port);
3336ce76104SFrank A Kingswood 
3346ce76104SFrank A Kingswood out:	return r;
3356ce76104SFrank A Kingswood }
3366ce76104SFrank A Kingswood 
3376ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and
3386ce76104SFrank A Kingswood  * tty->termios contains the new setting to be used.
3396ce76104SFrank A Kingswood  */
34095da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty,
34195da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3426ce76104SFrank A Kingswood {
3436ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(port);
3446ce76104SFrank A Kingswood 	unsigned baud_rate;
345664d5df9SWerner Cornelius 	unsigned long flags;
3466ce76104SFrank A Kingswood 
3476ce76104SFrank A Kingswood 	baud_rate = tty_get_baud_rate(tty);
3486ce76104SFrank A Kingswood 
3496ce76104SFrank A Kingswood 	priv->baud_rate = baud_rate;
350664d5df9SWerner Cornelius 
351664d5df9SWerner Cornelius 	if (baud_rate) {
352664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
353664d5df9SWerner Cornelius 		priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
354664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
355664d5df9SWerner Cornelius 		ch341_set_baudrate(port->serial->dev, priv);
356664d5df9SWerner Cornelius 	} else {
357664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
358664d5df9SWerner Cornelius 		priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
359664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
3606ce76104SFrank A Kingswood 	}
3616ce76104SFrank A Kingswood 
362664d5df9SWerner Cornelius 	ch341_set_handshake(port->serial->dev, priv->line_control);
3636ce76104SFrank A Kingswood 
3646ce76104SFrank A Kingswood 	/* Unimplemented:
3656ce76104SFrank A Kingswood 	 * (cflag & CSIZE) : data bits [5, 8]
3666ce76104SFrank A Kingswood 	 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
3676ce76104SFrank A Kingswood 	 * (cflag & CSTOPB) : stop bits [1, 2]
3686ce76104SFrank A Kingswood 	 */
369664d5df9SWerner Cornelius }
37073f59308SAlan Cox 
371492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state)
372492896f0STim Small {
373492896f0STim Small 	const uint16_t ch341_break_reg =
374492896f0STim Small 		CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
375492896f0STim Small 	struct usb_serial_port *port = tty->driver_data;
376492896f0STim Small 	int r;
377492896f0STim Small 	uint16_t reg_contents;
378f2b5cc83SJohan Hovold 	uint8_t *break_reg;
379492896f0STim Small 
380f2b5cc83SJohan Hovold 	break_reg = kmalloc(2, GFP_KERNEL);
38110c642d0SJohan Hovold 	if (!break_reg)
382f2b5cc83SJohan Hovold 		return;
383f2b5cc83SJohan Hovold 
384492896f0STim Small 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
385f2b5cc83SJohan Hovold 			ch341_break_reg, 0, break_reg, 2);
386492896f0STim Small 	if (r < 0) {
3876a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
3886a9b15feSJohan Hovold 				__func__, r);
389f2b5cc83SJohan Hovold 		goto out;
390492896f0STim Small 	}
39179cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
392492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
393492896f0STim Small 	if (break_state != 0) {
39479cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
395492896f0STim Small 		break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
396492896f0STim Small 		break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
397492896f0STim Small 	} else {
39879cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
399492896f0STim Small 		break_reg[0] |= CH341_NBREAK_BITS_REG1;
400492896f0STim Small 		break_reg[1] |= CH341_NBREAK_BITS_REG2;
401492896f0STim Small 	}
40279cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
403492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
4045be796f0SJohan Hovold 	reg_contents = get_unaligned_le16(break_reg);
405492896f0STim Small 	r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
406492896f0STim Small 			ch341_break_reg, reg_contents);
407492896f0STim Small 	if (r < 0)
4086a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
4096a9b15feSJohan Hovold 				__func__, r);
410f2b5cc83SJohan Hovold out:
411f2b5cc83SJohan Hovold 	kfree(break_reg);
412492896f0STim Small }
413492896f0STim Small 
41420b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty,
415664d5df9SWerner Cornelius 			  unsigned int set, unsigned int clear)
416664d5df9SWerner Cornelius {
417664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
418664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
419664d5df9SWerner Cornelius 	unsigned long flags;
420664d5df9SWerner Cornelius 	u8 control;
421664d5df9SWerner Cornelius 
422664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
423664d5df9SWerner Cornelius 	if (set & TIOCM_RTS)
424664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_RTS;
425664d5df9SWerner Cornelius 	if (set & TIOCM_DTR)
426664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_DTR;
427664d5df9SWerner Cornelius 	if (clear & TIOCM_RTS)
428664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_RTS;
429664d5df9SWerner Cornelius 	if (clear & TIOCM_DTR)
430664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_DTR;
431664d5df9SWerner Cornelius 	control = priv->line_control;
432664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
433664d5df9SWerner Cornelius 
434664d5df9SWerner Cornelius 	return ch341_set_handshake(port->serial->dev, control);
435664d5df9SWerner Cornelius }
436664d5df9SWerner Cornelius 
437ac035628SJohan Hovold static void ch341_update_line_status(struct usb_serial_port *port,
438ac035628SJohan Hovold 					unsigned char *data, size_t len)
439ac035628SJohan Hovold {
440ac035628SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
441b770081fSJohan Hovold 	struct tty_struct *tty;
442ac035628SJohan Hovold 	unsigned long flags;
443b770081fSJohan Hovold 	u8 status;
444b770081fSJohan Hovold 	u8 delta;
445ac035628SJohan Hovold 
446ac035628SJohan Hovold 	if (len < 4)
447ac035628SJohan Hovold 		return;
448ac035628SJohan Hovold 
449b770081fSJohan Hovold 	status = ~data[2] & CH341_BITS_MODEM_STAT;
450b770081fSJohan Hovold 
451ac035628SJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
452b770081fSJohan Hovold 	delta = status ^ priv->line_status;
453b770081fSJohan Hovold 	priv->line_status = status;
454ac035628SJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
455ac035628SJohan Hovold 
456fd74b0b1SJohan Hovold 	if (data[1] & CH341_MULT_STAT)
457fd74b0b1SJohan Hovold 		dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
458fd74b0b1SJohan Hovold 
459d984fe91SJohan Hovold 	if (!delta)
460d984fe91SJohan Hovold 		return;
461d984fe91SJohan Hovold 
4625e409a26SJohan Hovold 	if (delta & CH341_BIT_CTS)
4635e409a26SJohan Hovold 		port->icount.cts++;
4645e409a26SJohan Hovold 	if (delta & CH341_BIT_DSR)
4655e409a26SJohan Hovold 		port->icount.dsr++;
4665e409a26SJohan Hovold 	if (delta & CH341_BIT_RI)
4675e409a26SJohan Hovold 		port->icount.rng++;
468b770081fSJohan Hovold 	if (delta & CH341_BIT_DCD) {
4695e409a26SJohan Hovold 		port->icount.dcd++;
470b770081fSJohan Hovold 		tty = tty_port_tty_get(&port->port);
471b770081fSJohan Hovold 		if (tty) {
472ac035628SJohan Hovold 			usb_serial_handle_dcd_change(port, tty,
473b770081fSJohan Hovold 						status & CH341_BIT_DCD);
474ac035628SJohan Hovold 			tty_kref_put(tty);
475ac035628SJohan Hovold 		}
476b770081fSJohan Hovold 	}
477ac035628SJohan Hovold 
478ac035628SJohan Hovold 	wake_up_interruptible(&port->port.delta_msr_wait);
479ac035628SJohan Hovold }
480ac035628SJohan Hovold 
481664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb)
482664d5df9SWerner Cornelius {
483271ec2d2SJohan Hovold 	struct usb_serial_port *port = urb->context;
484664d5df9SWerner Cornelius 	unsigned char *data = urb->transfer_buffer;
485271ec2d2SJohan Hovold 	unsigned int len = urb->actual_length;
486664d5df9SWerner Cornelius 	int status;
487664d5df9SWerner Cornelius 
488664d5df9SWerner Cornelius 	switch (urb->status) {
489664d5df9SWerner Cornelius 	case 0:
490664d5df9SWerner Cornelius 		/* success */
491664d5df9SWerner Cornelius 		break;
492664d5df9SWerner Cornelius 	case -ECONNRESET:
493664d5df9SWerner Cornelius 	case -ENOENT:
494664d5df9SWerner Cornelius 	case -ESHUTDOWN:
495664d5df9SWerner Cornelius 		/* this urb is terminated, clean up */
496271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
49779cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
498664d5df9SWerner Cornelius 		return;
499664d5df9SWerner Cornelius 	default:
500271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
50179cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
502664d5df9SWerner Cornelius 		goto exit;
503664d5df9SWerner Cornelius 	}
504664d5df9SWerner Cornelius 
505271ec2d2SJohan Hovold 	usb_serial_debug_data(&port->dev, __func__, len, data);
506271ec2d2SJohan Hovold 	ch341_update_line_status(port, data, len);
507664d5df9SWerner Cornelius exit:
508664d5df9SWerner Cornelius 	status = usb_submit_urb(urb, GFP_ATOMIC);
509271ec2d2SJohan Hovold 	if (status) {
510271ec2d2SJohan Hovold 		dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
511664d5df9SWerner Cornelius 			__func__, status);
512664d5df9SWerner Cornelius 	}
513271ec2d2SJohan Hovold }
514664d5df9SWerner Cornelius 
51560b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty)
516664d5df9SWerner Cornelius {
517664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
518664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
519664d5df9SWerner Cornelius 	unsigned long flags;
520664d5df9SWerner Cornelius 	u8 mcr;
521664d5df9SWerner Cornelius 	u8 status;
522664d5df9SWerner Cornelius 	unsigned int result;
523664d5df9SWerner Cornelius 
524664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
525664d5df9SWerner Cornelius 	mcr = priv->line_control;
526664d5df9SWerner Cornelius 	status = priv->line_status;
527664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
528664d5df9SWerner Cornelius 
529664d5df9SWerner Cornelius 	result = ((mcr & CH341_BIT_DTR)		? TIOCM_DTR : 0)
530664d5df9SWerner Cornelius 		  | ((mcr & CH341_BIT_RTS)	? TIOCM_RTS : 0)
531664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_CTS)	? TIOCM_CTS : 0)
532664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DSR)	? TIOCM_DSR : 0)
533664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_RI)	? TIOCM_RI  : 0)
534664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DCD)	? TIOCM_CD  : 0);
535664d5df9SWerner Cornelius 
53679cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
537664d5df9SWerner Cornelius 
538664d5df9SWerner Cornelius 	return result;
5396ce76104SFrank A Kingswood }
5406ce76104SFrank A Kingswood 
541622b80cfSGreg Kroah-Hartman static int ch341_reset_resume(struct usb_serial *serial)
5421ded7ea4SMing Lei {
5431ded7ea4SMing Lei 	struct ch341_private *priv;
5441ded7ea4SMing Lei 
5451ded7ea4SMing Lei 	priv = usb_get_serial_port_data(serial->port[0]);
5461ded7ea4SMing Lei 
5471ded7ea4SMing Lei 	/* reconfigure ch341 serial port after bus-reset */
5482bfd1c96SGreg Kroah-Hartman 	ch341_configure(serial->dev, priv);
5491ded7ea4SMing Lei 
5501ded7ea4SMing Lei 	return 0;
5511ded7ea4SMing Lei }
5521ded7ea4SMing Lei 
5536ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = {
5546ce76104SFrank A Kingswood 	.driver = {
5556ce76104SFrank A Kingswood 		.owner	= THIS_MODULE,
5566ce76104SFrank A Kingswood 		.name	= "ch341-uart",
5576ce76104SFrank A Kingswood 	},
5586ce76104SFrank A Kingswood 	.id_table          = id_table,
5596ce76104SFrank A Kingswood 	.num_ports         = 1,
5606ce76104SFrank A Kingswood 	.open              = ch341_open,
561335f8514SAlan Cox 	.dtr_rts	   = ch341_dtr_rts,
562335f8514SAlan Cox 	.carrier_raised	   = ch341_carrier_raised,
563664d5df9SWerner Cornelius 	.close             = ch341_close,
5646ce76104SFrank A Kingswood 	.set_termios       = ch341_set_termios,
565492896f0STim Small 	.break_ctl         = ch341_break_ctl,
566664d5df9SWerner Cornelius 	.tiocmget          = ch341_tiocmget,
567664d5df9SWerner Cornelius 	.tiocmset          = ch341_tiocmset,
5685e409a26SJohan Hovold 	.tiocmiwait        = usb_serial_generic_tiocmiwait,
569664d5df9SWerner Cornelius 	.read_int_callback = ch341_read_int_callback,
570456c5be5SJohan Hovold 	.port_probe        = ch341_port_probe,
571456c5be5SJohan Hovold 	.port_remove       = ch341_port_remove,
5721c1eaba8SGreg Kroah-Hartman 	.reset_resume      = ch341_reset_resume,
5736ce76104SFrank A Kingswood };
5746ce76104SFrank A Kingswood 
57508a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
57608a4f6bcSAlan Stern 	&ch341_device, NULL
57708a4f6bcSAlan Stern };
57808a4f6bcSAlan Stern 
57968e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
5806ce76104SFrank A Kingswood 
5816ce76104SFrank A Kingswood MODULE_LICENSE("GPL");
582