xref: /linux/drivers/usb/serial/ch341.c (revision 00a0d0d65b61241a718d0aee96f46b9a2d93bf26)
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/init.h>
206ce76104SFrank A Kingswood #include <linux/tty.h>
216ce76104SFrank A Kingswood #include <linux/module.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
236ce76104SFrank A Kingswood #include <linux/usb.h>
246ce76104SFrank A Kingswood #include <linux/usb/serial.h>
256ce76104SFrank A Kingswood #include <linux/serial.h>
265be796f0SJohan Hovold #include <asm/unaligned.h>
276ce76104SFrank A Kingswood 
28664d5df9SWerner Cornelius #define DEFAULT_BAUD_RATE 9600
296ce76104SFrank A Kingswood #define DEFAULT_TIMEOUT   1000
306ce76104SFrank A Kingswood 
31664d5df9SWerner Cornelius /* flags for IO-Bits */
32664d5df9SWerner Cornelius #define CH341_BIT_RTS (1 << 6)
33664d5df9SWerner Cornelius #define CH341_BIT_DTR (1 << 5)
34664d5df9SWerner Cornelius 
35664d5df9SWerner Cornelius /******************************/
36664d5df9SWerner Cornelius /* interrupt pipe definitions */
37664d5df9SWerner Cornelius /******************************/
38664d5df9SWerner Cornelius /* always 4 interrupt bytes */
39664d5df9SWerner Cornelius /* first irq byte normally 0x08 */
40664d5df9SWerner Cornelius /* second irq byte base 0x7d + below */
41664d5df9SWerner Cornelius /* third irq byte base 0x94 + below */
42664d5df9SWerner Cornelius /* fourth irq byte normally 0xee */
43664d5df9SWerner Cornelius 
44664d5df9SWerner Cornelius /* second interrupt byte */
45664d5df9SWerner Cornelius #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
46664d5df9SWerner Cornelius 
47664d5df9SWerner Cornelius /* status returned in third interrupt answer byte, inverted in data
48664d5df9SWerner Cornelius    from irq */
49664d5df9SWerner Cornelius #define CH341_BIT_CTS 0x01
50664d5df9SWerner Cornelius #define CH341_BIT_DSR 0x02
51664d5df9SWerner Cornelius #define CH341_BIT_RI  0x04
52664d5df9SWerner Cornelius #define CH341_BIT_DCD 0x08
53664d5df9SWerner Cornelius #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
54664d5df9SWerner Cornelius 
55664d5df9SWerner Cornelius /*******************************/
56664d5df9SWerner Cornelius /* baudrate calculation factor */
57664d5df9SWerner Cornelius /*******************************/
58664d5df9SWerner Cornelius #define CH341_BAUDBASE_FACTOR 1532620800
59664d5df9SWerner Cornelius #define CH341_BAUDBASE_DIVMAX 3
60664d5df9SWerner Cornelius 
61492896f0STim Small /* Break support - the information used to implement this was gleaned from
62492896f0STim Small  * the Net/FreeBSD uchcom.c driver by Takanori Watanabe.  Domo arigato.
63492896f0STim Small  */
64492896f0STim Small 
65492896f0STim Small #define CH341_REQ_WRITE_REG    0x9A
66492896f0STim Small #define CH341_REQ_READ_REG     0x95
67492896f0STim Small #define CH341_REG_BREAK1       0x05
68492896f0STim Small #define CH341_REG_BREAK2       0x18
69492896f0STim Small #define CH341_NBREAK_BITS_REG1 0x01
70492896f0STim Small #define CH341_NBREAK_BITS_REG2 0x40
71492896f0STim Small 
72492896f0STim Small 
736ce76104SFrank A Kingswood static int debug;
746ce76104SFrank A Kingswood 
757d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
766ce76104SFrank A Kingswood 	{ USB_DEVICE(0x4348, 0x5523) },
7782078234SMichael F. Robbins 	{ USB_DEVICE(0x1a86, 0x7523) },
786ce76104SFrank A Kingswood 	{ },
796ce76104SFrank A Kingswood };
806ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table);
816ce76104SFrank A Kingswood 
826ce76104SFrank A Kingswood struct ch341_private {
83664d5df9SWerner Cornelius 	spinlock_t lock; /* access lock */
84664d5df9SWerner Cornelius 	wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
85664d5df9SWerner Cornelius 	unsigned baud_rate; /* set baud rate */
86664d5df9SWerner Cornelius 	u8 line_control; /* set line control value RTS/DTR */
87664d5df9SWerner Cornelius 	u8 line_status; /* active status of modem control inputs */
88664d5df9SWerner Cornelius 	u8 multi_status_change; /* status changed multiple since last call */
896ce76104SFrank A Kingswood };
906ce76104SFrank A Kingswood 
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;
956ce76104SFrank A Kingswood 	dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
966ce76104SFrank A Kingswood 		(int)request, (int)value, (int)index);
976ce76104SFrank A Kingswood 
986ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
996ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1006ce76104SFrank A Kingswood 			    value, index, NULL, 0, DEFAULT_TIMEOUT);
1016ce76104SFrank A Kingswood 
1026ce76104SFrank A Kingswood 	return r;
1036ce76104SFrank A Kingswood }
1046ce76104SFrank A Kingswood 
1056ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev,
1066ce76104SFrank A Kingswood 			    u8 request, u16 value, u16 index,
1076ce76104SFrank A Kingswood 			    char *buf, unsigned bufsize)
1086ce76104SFrank A Kingswood {
1096ce76104SFrank A Kingswood 	int r;
1106ce76104SFrank A Kingswood 	dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
1116ce76104SFrank A Kingswood 		(int)request, (int)value, (int)index, buf, (int)bufsize);
1126ce76104SFrank A Kingswood 
1136ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
1146ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1156ce76104SFrank A Kingswood 			    value, index, buf, bufsize, DEFAULT_TIMEOUT);
1166ce76104SFrank A Kingswood 	return r;
1176ce76104SFrank A Kingswood }
1186ce76104SFrank A Kingswood 
11993b6497dSAdrian Bunk static int ch341_set_baudrate(struct usb_device *dev,
12093b6497dSAdrian Bunk 			      struct ch341_private *priv)
1216ce76104SFrank A Kingswood {
1226ce76104SFrank A Kingswood 	short a, b;
1236ce76104SFrank A Kingswood 	int r;
124664d5df9SWerner Cornelius 	unsigned long factor;
125664d5df9SWerner Cornelius 	short divisor;
1266ce76104SFrank A Kingswood 
1276ce76104SFrank A Kingswood 	dbg("ch341_set_baudrate(%d)", priv->baud_rate);
128664d5df9SWerner Cornelius 
129664d5df9SWerner Cornelius 	if (!priv->baud_rate)
1306ce76104SFrank A Kingswood 		return -EINVAL;
131664d5df9SWerner Cornelius 	factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
132664d5df9SWerner Cornelius 	divisor = CH341_BAUDBASE_DIVMAX;
133664d5df9SWerner Cornelius 
134664d5df9SWerner Cornelius 	while ((factor > 0xfff0) && divisor) {
135664d5df9SWerner Cornelius 		factor >>= 3;
136664d5df9SWerner Cornelius 		divisor--;
1376ce76104SFrank A Kingswood 	}
1386ce76104SFrank A Kingswood 
139664d5df9SWerner Cornelius 	if (factor > 0xfff0)
140664d5df9SWerner Cornelius 		return -EINVAL;
141664d5df9SWerner Cornelius 
142664d5df9SWerner Cornelius 	factor = 0x10000 - factor;
143664d5df9SWerner Cornelius 	a = (factor & 0xff00) | divisor;
144664d5df9SWerner Cornelius 	b = factor & 0xff;
145664d5df9SWerner Cornelius 
1466ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0x9a, 0x1312, a);
1476ce76104SFrank A Kingswood 	if (!r)
1486ce76104SFrank A Kingswood 		r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
1496ce76104SFrank A Kingswood 
1506ce76104SFrank A Kingswood 	return r;
1516ce76104SFrank A Kingswood }
1526ce76104SFrank A Kingswood 
153664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control)
1546ce76104SFrank A Kingswood {
155664d5df9SWerner Cornelius 	dbg("ch341_set_handshake(0x%02x)", control);
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 	dbg("ch341_get_status()");
1676ce76104SFrank A Kingswood 
1686ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
1696ce76104SFrank A Kingswood 	if (!buffer)
1706ce76104SFrank A Kingswood 		return -ENOMEM;
1716ce76104SFrank A Kingswood 
1726ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
1736ce76104SFrank A Kingswood 	if (r < 0)
1746ce76104SFrank A Kingswood 		goto out;
1756ce76104SFrank A Kingswood 
176664d5df9SWerner Cornelius 	/* setup the private status if available */
177664d5df9SWerner Cornelius 	if (r == 2) {
1786ce76104SFrank A Kingswood 		r = 0;
179664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
180664d5df9SWerner Cornelius 		priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
181664d5df9SWerner Cornelius 		priv->multi_status_change = 0;
182664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
183664d5df9SWerner Cornelius 	} else
184664d5df9SWerner Cornelius 		r = -EPROTO;
1856ce76104SFrank A Kingswood 
1866ce76104SFrank A Kingswood out:	kfree(buffer);
1876ce76104SFrank A Kingswood 	return r;
1886ce76104SFrank A Kingswood }
1896ce76104SFrank A Kingswood 
1906ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */
1916ce76104SFrank A Kingswood 
19293b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
1936ce76104SFrank A Kingswood {
1946ce76104SFrank A Kingswood 	char *buffer;
1956ce76104SFrank A Kingswood 	int r;
1966ce76104SFrank A Kingswood 	const unsigned size = 8;
1976ce76104SFrank A Kingswood 
1986ce76104SFrank A Kingswood 	dbg("ch341_configure()");
1996ce76104SFrank A Kingswood 
2006ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
2016ce76104SFrank A Kingswood 	if (!buffer)
2026ce76104SFrank A Kingswood 		return -ENOMEM;
2036ce76104SFrank A Kingswood 
2046ce76104SFrank A Kingswood 	/* expect two bytes 0x27 0x00 */
2056ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
2066ce76104SFrank A Kingswood 	if (r < 0)
2076ce76104SFrank A Kingswood 		goto out;
2086ce76104SFrank A Kingswood 
2096ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0, 0);
2106ce76104SFrank A Kingswood 	if (r < 0)
2116ce76104SFrank A Kingswood 		goto out;
2126ce76104SFrank A Kingswood 
2136ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2146ce76104SFrank A Kingswood 	if (r < 0)
2156ce76104SFrank A Kingswood 		goto out;
2166ce76104SFrank A Kingswood 
2176ce76104SFrank A Kingswood 	/* expect two bytes 0x56 0x00 */
2186ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
2196ce76104SFrank A Kingswood 	if (r < 0)
2206ce76104SFrank A Kingswood 		goto out;
2216ce76104SFrank A Kingswood 
2226ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
2236ce76104SFrank A Kingswood 	if (r < 0)
2246ce76104SFrank A Kingswood 		goto out;
2256ce76104SFrank A Kingswood 
2266ce76104SFrank A Kingswood 	/* expect 0xff 0xee */
227664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2286ce76104SFrank A Kingswood 	if (r < 0)
2296ce76104SFrank A Kingswood 		goto out;
2306ce76104SFrank A Kingswood 
2316ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
2326ce76104SFrank A Kingswood 	if (r < 0)
2336ce76104SFrank A Kingswood 		goto out;
2346ce76104SFrank A Kingswood 
2356ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2366ce76104SFrank A Kingswood 	if (r < 0)
2376ce76104SFrank A Kingswood 		goto out;
2386ce76104SFrank A Kingswood 
239664d5df9SWerner Cornelius 	r = ch341_set_handshake(dev, priv->line_control);
2406ce76104SFrank A Kingswood 	if (r < 0)
2416ce76104SFrank A Kingswood 		goto out;
2426ce76104SFrank A Kingswood 
2436ce76104SFrank A Kingswood 	/* expect 0x9f 0xee */
244664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2456ce76104SFrank A Kingswood 
2466ce76104SFrank A Kingswood out:	kfree(buffer);
2476ce76104SFrank A Kingswood 	return r;
2486ce76104SFrank A Kingswood }
2496ce76104SFrank A Kingswood 
2506ce76104SFrank A Kingswood /* allocate private data */
2516ce76104SFrank A Kingswood static int ch341_attach(struct usb_serial *serial)
2526ce76104SFrank A Kingswood {
2536ce76104SFrank A Kingswood 	struct ch341_private *priv;
2546ce76104SFrank A Kingswood 	int r;
2556ce76104SFrank A Kingswood 
2566ce76104SFrank A Kingswood 	dbg("ch341_attach()");
2576ce76104SFrank A Kingswood 
2586ce76104SFrank A Kingswood 	/* private data */
2596ce76104SFrank A Kingswood 	priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
2606ce76104SFrank A Kingswood 	if (!priv)
2616ce76104SFrank A Kingswood 		return -ENOMEM;
2626ce76104SFrank A Kingswood 
263664d5df9SWerner Cornelius 	spin_lock_init(&priv->lock);
264664d5df9SWerner Cornelius 	init_waitqueue_head(&priv->delta_msr_wait);
2656ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
266664d5df9SWerner Cornelius 	priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
2676ce76104SFrank A Kingswood 
2686ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
2696ce76104SFrank A Kingswood 	if (r < 0)
2706ce76104SFrank A Kingswood 		goto error;
2716ce76104SFrank A Kingswood 
2726ce76104SFrank A Kingswood 	usb_set_serial_port_data(serial->port[0], priv);
2736ce76104SFrank A Kingswood 	return 0;
2746ce76104SFrank A Kingswood 
2756ce76104SFrank A Kingswood error:	kfree(priv);
2766ce76104SFrank A Kingswood 	return r;
2776ce76104SFrank A Kingswood }
2786ce76104SFrank A Kingswood 
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 
292664d5df9SWerner Cornelius 	dbg("%s - port %d", __func__, port->number);
293335f8514SAlan Cox 	/* drop DTR and RTS */
294335f8514SAlan Cox 	spin_lock_irqsave(&priv->lock, flags);
295335f8514SAlan Cox 	if (on)
296335f8514SAlan Cox 		priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
297335f8514SAlan Cox 	else
298335f8514SAlan Cox 		priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
299335f8514SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
300335f8514SAlan Cox 	ch341_set_handshake(port->serial->dev, priv->line_control);
301335f8514SAlan Cox 	wake_up_interruptible(&priv->delta_msr_wait);
302335f8514SAlan Cox }
303335f8514SAlan Cox 
304335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port)
305335f8514SAlan Cox {
306335f8514SAlan Cox 	dbg("%s - port %d", __func__, port->number);
307664d5df9SWerner Cornelius 
308f26788daSJohan Hovold 	usb_serial_generic_close(port);
309664d5df9SWerner Cornelius 	usb_kill_urb(port->interrupt_in_urb);
310664d5df9SWerner Cornelius }
311664d5df9SWerner Cornelius 
312664d5df9SWerner Cornelius 
3136ce76104SFrank A Kingswood /* open this device, set default parameters */
314a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
3156ce76104SFrank A Kingswood {
3166ce76104SFrank A Kingswood 	struct usb_serial *serial = port->serial;
3176ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
3186ce76104SFrank A Kingswood 	int r;
3196ce76104SFrank A Kingswood 
3206ce76104SFrank A Kingswood 	dbg("ch341_open()");
3216ce76104SFrank A Kingswood 
3226ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
3236ce76104SFrank A Kingswood 
3246ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
3256ce76104SFrank A Kingswood 	if (r)
3266ce76104SFrank A Kingswood 		goto out;
3276ce76104SFrank A Kingswood 
328664d5df9SWerner Cornelius 	r = ch341_set_handshake(serial->dev, priv->line_control);
3296ce76104SFrank A Kingswood 	if (r)
3306ce76104SFrank A Kingswood 		goto out;
3316ce76104SFrank A Kingswood 
3326ce76104SFrank A Kingswood 	r = ch341_set_baudrate(serial->dev, priv);
3336ce76104SFrank A Kingswood 	if (r)
3346ce76104SFrank A Kingswood 		goto out;
3356ce76104SFrank A Kingswood 
336664d5df9SWerner Cornelius 	dbg("%s - submitting interrupt urb", __func__);
337664d5df9SWerner Cornelius 	port->interrupt_in_urb->dev = serial->dev;
338664d5df9SWerner Cornelius 	r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
339664d5df9SWerner Cornelius 	if (r) {
340664d5df9SWerner Cornelius 		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
341664d5df9SWerner Cornelius 			" error %d\n", __func__, r);
342335f8514SAlan Cox 		ch341_close(port);
343664d5df9SWerner Cornelius 		return -EPROTO;
344664d5df9SWerner Cornelius 	}
345664d5df9SWerner Cornelius 
346a509a7e4SAlan Cox 	r = usb_serial_generic_open(tty, port);
3476ce76104SFrank A Kingswood 
3486ce76104SFrank A Kingswood out:	return r;
3496ce76104SFrank A Kingswood }
3506ce76104SFrank A Kingswood 
3516ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and
3526ce76104SFrank A Kingswood  * tty->termios contains the new setting to be used.
3536ce76104SFrank A Kingswood  */
35495da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty,
35595da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3566ce76104SFrank A Kingswood {
3576ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(port);
3586ce76104SFrank A Kingswood 	unsigned baud_rate;
359664d5df9SWerner Cornelius 	unsigned long flags;
3606ce76104SFrank A Kingswood 
3616ce76104SFrank A Kingswood 	dbg("ch341_set_termios()");
3626ce76104SFrank A Kingswood 
3636ce76104SFrank A Kingswood 	baud_rate = tty_get_baud_rate(tty);
3646ce76104SFrank A Kingswood 
3656ce76104SFrank A Kingswood 	priv->baud_rate = baud_rate;
366664d5df9SWerner Cornelius 
367664d5df9SWerner Cornelius 	if (baud_rate) {
368664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
369664d5df9SWerner Cornelius 		priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
370664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
371664d5df9SWerner Cornelius 		ch341_set_baudrate(port->serial->dev, priv);
372664d5df9SWerner Cornelius 	} else {
373664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
374664d5df9SWerner Cornelius 		priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
375664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
3766ce76104SFrank A Kingswood 	}
3776ce76104SFrank A Kingswood 
378664d5df9SWerner Cornelius 	ch341_set_handshake(port->serial->dev, priv->line_control);
3796ce76104SFrank A Kingswood 
3806ce76104SFrank A Kingswood 	/* Unimplemented:
3816ce76104SFrank A Kingswood 	 * (cflag & CSIZE) : data bits [5, 8]
3826ce76104SFrank A Kingswood 	 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
3836ce76104SFrank A Kingswood 	 * (cflag & CSTOPB) : stop bits [1, 2]
3846ce76104SFrank A Kingswood 	 */
385664d5df9SWerner Cornelius }
38673f59308SAlan Cox 
387492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state)
388492896f0STim Small {
389492896f0STim Small 	const uint16_t ch341_break_reg =
390492896f0STim Small 		CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
391492896f0STim Small 	struct usb_serial_port *port = tty->driver_data;
392492896f0STim Small 	int r;
393492896f0STim Small 	uint16_t reg_contents;
394f2b5cc83SJohan Hovold 	uint8_t *break_reg;
395492896f0STim Small 
396492896f0STim Small 	dbg("%s()", __func__);
397492896f0STim Small 
398f2b5cc83SJohan Hovold 	break_reg = kmalloc(2, GFP_KERNEL);
399f2b5cc83SJohan Hovold 	if (!break_reg) {
400f2b5cc83SJohan Hovold 		dev_err(&port->dev, "%s - kmalloc failed\n", __func__);
401f2b5cc83SJohan Hovold 		return;
402f2b5cc83SJohan Hovold 	}
403f2b5cc83SJohan Hovold 
404492896f0STim Small 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
405f2b5cc83SJohan Hovold 			ch341_break_reg, 0, break_reg, 2);
406492896f0STim Small 	if (r < 0) {
4076a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
4086a9b15feSJohan Hovold 				__func__, r);
409f2b5cc83SJohan Hovold 		goto out;
410492896f0STim Small 	}
411492896f0STim Small 	dbg("%s - initial ch341 break register contents - reg1: %x, reg2: %x",
412492896f0STim Small 			__func__, break_reg[0], break_reg[1]);
413492896f0STim Small 	if (break_state != 0) {
414492896f0STim Small 		dbg("%s - Enter break state requested", __func__);
415492896f0STim Small 		break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
416492896f0STim Small 		break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
417492896f0STim Small 	} else {
418492896f0STim Small 		dbg("%s - Leave break state requested", __func__);
419492896f0STim Small 		break_reg[0] |= CH341_NBREAK_BITS_REG1;
420492896f0STim Small 		break_reg[1] |= CH341_NBREAK_BITS_REG2;
421492896f0STim Small 	}
422492896f0STim Small 	dbg("%s - New ch341 break register contents - reg1: %x, reg2: %x",
423492896f0STim Small 			__func__, break_reg[0], break_reg[1]);
4245be796f0SJohan Hovold 	reg_contents = get_unaligned_le16(break_reg);
425492896f0STim Small 	r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
426492896f0STim Small 			ch341_break_reg, reg_contents);
427492896f0STim Small 	if (r < 0)
4286a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
4296a9b15feSJohan Hovold 				__func__, r);
430f2b5cc83SJohan Hovold out:
431f2b5cc83SJohan Hovold 	kfree(break_reg);
432492896f0STim Small }
433492896f0STim Small 
43420b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty,
435664d5df9SWerner Cornelius 			  unsigned int set, unsigned int clear)
436664d5df9SWerner Cornelius {
437664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
438664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
439664d5df9SWerner Cornelius 	unsigned long flags;
440664d5df9SWerner Cornelius 	u8 control;
441664d5df9SWerner Cornelius 
442664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
443664d5df9SWerner Cornelius 	if (set & TIOCM_RTS)
444664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_RTS;
445664d5df9SWerner Cornelius 	if (set & TIOCM_DTR)
446664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_DTR;
447664d5df9SWerner Cornelius 	if (clear & TIOCM_RTS)
448664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_RTS;
449664d5df9SWerner Cornelius 	if (clear & TIOCM_DTR)
450664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_DTR;
451664d5df9SWerner Cornelius 	control = priv->line_control;
452664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
453664d5df9SWerner Cornelius 
454664d5df9SWerner Cornelius 	return ch341_set_handshake(port->serial->dev, control);
455664d5df9SWerner Cornelius }
456664d5df9SWerner Cornelius 
457664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb)
458664d5df9SWerner Cornelius {
459664d5df9SWerner Cornelius 	struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
460664d5df9SWerner Cornelius 	unsigned char *data = urb->transfer_buffer;
461664d5df9SWerner Cornelius 	unsigned int actual_length = urb->actual_length;
462664d5df9SWerner Cornelius 	int status;
463664d5df9SWerner Cornelius 
464664d5df9SWerner Cornelius 	dbg("%s (%d)", __func__, port->number);
465664d5df9SWerner Cornelius 
466664d5df9SWerner Cornelius 	switch (urb->status) {
467664d5df9SWerner Cornelius 	case 0:
468664d5df9SWerner Cornelius 		/* success */
469664d5df9SWerner Cornelius 		break;
470664d5df9SWerner Cornelius 	case -ECONNRESET:
471664d5df9SWerner Cornelius 	case -ENOENT:
472664d5df9SWerner Cornelius 	case -ESHUTDOWN:
473664d5df9SWerner Cornelius 		/* this urb is terminated, clean up */
474664d5df9SWerner Cornelius 		dbg("%s - urb shutting down with status: %d", __func__,
475664d5df9SWerner Cornelius 		    urb->status);
476664d5df9SWerner Cornelius 		return;
477664d5df9SWerner Cornelius 	default:
478664d5df9SWerner Cornelius 		dbg("%s - nonzero urb status received: %d", __func__,
479664d5df9SWerner Cornelius 		    urb->status);
480664d5df9SWerner Cornelius 		goto exit;
481664d5df9SWerner Cornelius 	}
482664d5df9SWerner Cornelius 
483664d5df9SWerner Cornelius 	usb_serial_debug_data(debug, &port->dev, __func__,
484664d5df9SWerner Cornelius 			      urb->actual_length, urb->transfer_buffer);
485664d5df9SWerner Cornelius 
486664d5df9SWerner Cornelius 	if (actual_length >= 4) {
487664d5df9SWerner Cornelius 		struct ch341_private *priv = usb_get_serial_port_data(port);
488664d5df9SWerner Cornelius 		unsigned long flags;
489d14fc1a7SLibor Pechacek 		u8 prev_line_status = priv->line_status;
490664d5df9SWerner Cornelius 
491664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
492664d5df9SWerner Cornelius 		priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
493664d5df9SWerner Cornelius 		if ((data[1] & CH341_MULT_STAT))
494664d5df9SWerner Cornelius 			priv->multi_status_change = 1;
495664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
496d14fc1a7SLibor Pechacek 
497d14fc1a7SLibor Pechacek 		if ((priv->line_status ^ prev_line_status) & CH341_BIT_DCD) {
498d14fc1a7SLibor Pechacek 			struct tty_struct *tty = tty_port_tty_get(&port->port);
499d14fc1a7SLibor Pechacek 			if (tty)
500d14fc1a7SLibor Pechacek 				usb_serial_handle_dcd_change(port, tty,
501d14fc1a7SLibor Pechacek 					    priv->line_status & CH341_BIT_DCD);
502d14fc1a7SLibor Pechacek 			tty_kref_put(tty);
503d14fc1a7SLibor Pechacek 		}
504d14fc1a7SLibor Pechacek 
505664d5df9SWerner Cornelius 		wake_up_interruptible(&priv->delta_msr_wait);
506664d5df9SWerner Cornelius 	}
507664d5df9SWerner Cornelius 
508664d5df9SWerner Cornelius exit:
509664d5df9SWerner Cornelius 	status = usb_submit_urb(urb, GFP_ATOMIC);
510664d5df9SWerner Cornelius 	if (status)
511664d5df9SWerner Cornelius 		dev_err(&urb->dev->dev,
512664d5df9SWerner Cornelius 			"%s - usb_submit_urb failed with result %d\n",
513664d5df9SWerner Cornelius 			__func__, status);
514664d5df9SWerner Cornelius }
515664d5df9SWerner Cornelius 
516664d5df9SWerner Cornelius static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
517664d5df9SWerner Cornelius {
518664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
519664d5df9SWerner Cornelius 	unsigned long flags;
520664d5df9SWerner Cornelius 	u8 prevstatus;
521664d5df9SWerner Cornelius 	u8 status;
522664d5df9SWerner Cornelius 	u8 changed;
523664d5df9SWerner Cornelius 	u8 multi_change = 0;
524664d5df9SWerner Cornelius 
525664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
526664d5df9SWerner Cornelius 	prevstatus = priv->line_status;
527664d5df9SWerner Cornelius 	priv->multi_status_change = 0;
528664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
529664d5df9SWerner Cornelius 
530664d5df9SWerner Cornelius 	while (!multi_change) {
531664d5df9SWerner Cornelius 		interruptible_sleep_on(&priv->delta_msr_wait);
532664d5df9SWerner Cornelius 		/* see if a signal did it */
533664d5df9SWerner Cornelius 		if (signal_pending(current))
534664d5df9SWerner Cornelius 			return -ERESTARTSYS;
535664d5df9SWerner Cornelius 
536664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
537664d5df9SWerner Cornelius 		status = priv->line_status;
538664d5df9SWerner Cornelius 		multi_change = priv->multi_status_change;
539664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
540664d5df9SWerner Cornelius 
541664d5df9SWerner Cornelius 		changed = prevstatus ^ status;
542664d5df9SWerner Cornelius 
543664d5df9SWerner Cornelius 		if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
544664d5df9SWerner Cornelius 		    ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
545664d5df9SWerner Cornelius 		    ((arg & TIOCM_CD)  && (changed & CH341_BIT_DCD)) ||
546664d5df9SWerner Cornelius 		    ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
547664d5df9SWerner Cornelius 			return 0;
548664d5df9SWerner Cornelius 		}
549664d5df9SWerner Cornelius 		prevstatus = status;
550664d5df9SWerner Cornelius 	}
551664d5df9SWerner Cornelius 
552664d5df9SWerner Cornelius 	return 0;
553664d5df9SWerner Cornelius }
554664d5df9SWerner Cornelius 
555*00a0d0d6SAlan Cox static int ch341_ioctl(struct tty_struct *tty,
556664d5df9SWerner Cornelius 			unsigned int cmd, unsigned long arg)
557664d5df9SWerner Cornelius {
558664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
559664d5df9SWerner Cornelius 	dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
560664d5df9SWerner Cornelius 
561664d5df9SWerner Cornelius 	switch (cmd) {
562664d5df9SWerner Cornelius 	case TIOCMIWAIT:
563664d5df9SWerner Cornelius 		dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
564664d5df9SWerner Cornelius 		return wait_modem_info(port, arg);
565664d5df9SWerner Cornelius 
566664d5df9SWerner Cornelius 	default:
567664d5df9SWerner Cornelius 		dbg("%s not supported = 0x%04x", __func__, cmd);
568664d5df9SWerner Cornelius 		break;
569664d5df9SWerner Cornelius 	}
570664d5df9SWerner Cornelius 
571664d5df9SWerner Cornelius 	return -ENOIOCTLCMD;
572664d5df9SWerner Cornelius }
573664d5df9SWerner Cornelius 
57460b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty)
575664d5df9SWerner Cornelius {
576664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
577664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
578664d5df9SWerner Cornelius 	unsigned long flags;
579664d5df9SWerner Cornelius 	u8 mcr;
580664d5df9SWerner Cornelius 	u8 status;
581664d5df9SWerner Cornelius 	unsigned int result;
582664d5df9SWerner Cornelius 
583664d5df9SWerner Cornelius 	dbg("%s (%d)", __func__, port->number);
584664d5df9SWerner Cornelius 
585664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
586664d5df9SWerner Cornelius 	mcr = priv->line_control;
587664d5df9SWerner Cornelius 	status = priv->line_status;
588664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
589664d5df9SWerner Cornelius 
590664d5df9SWerner Cornelius 	result = ((mcr & CH341_BIT_DTR)		? TIOCM_DTR : 0)
591664d5df9SWerner Cornelius 		  | ((mcr & CH341_BIT_RTS)	? TIOCM_RTS : 0)
592664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_CTS)	? TIOCM_CTS : 0)
593664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DSR)	? TIOCM_DSR : 0)
594664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_RI)	? TIOCM_RI  : 0)
595664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DCD)	? TIOCM_CD  : 0);
596664d5df9SWerner Cornelius 
597664d5df9SWerner Cornelius 	dbg("%s - result = %x", __func__, result);
598664d5df9SWerner Cornelius 
599664d5df9SWerner Cornelius 	return result;
6006ce76104SFrank A Kingswood }
6016ce76104SFrank A Kingswood 
6021ded7ea4SMing Lei 
6031ded7ea4SMing Lei static int ch341_reset_resume(struct usb_interface *intf)
6041ded7ea4SMing Lei {
6051ded7ea4SMing Lei 	struct usb_device *dev = interface_to_usbdev(intf);
6061ded7ea4SMing Lei 	struct usb_serial *serial = NULL;
6071ded7ea4SMing Lei 	struct ch341_private *priv;
6081ded7ea4SMing Lei 
6091ded7ea4SMing Lei 	serial = usb_get_intfdata(intf);
6101ded7ea4SMing Lei 	priv = usb_get_serial_port_data(serial->port[0]);
6111ded7ea4SMing Lei 
6121ded7ea4SMing Lei 	/*reconfigure ch341 serial port after bus-reset*/
6131ded7ea4SMing Lei 	ch341_configure(dev, priv);
6141ded7ea4SMing Lei 
6151ded7ea4SMing Lei 	usb_serial_resume(intf);
6161ded7ea4SMing Lei 
6171ded7ea4SMing Lei 	return 0;
6181ded7ea4SMing Lei }
6191ded7ea4SMing Lei 
6206ce76104SFrank A Kingswood static struct usb_driver ch341_driver = {
6216ce76104SFrank A Kingswood 	.name		= "ch341",
6226ce76104SFrank A Kingswood 	.probe		= usb_serial_probe,
6236ce76104SFrank A Kingswood 	.disconnect	= usb_serial_disconnect,
6241ded7ea4SMing Lei 	.suspend	= usb_serial_suspend,
6251ded7ea4SMing Lei 	.resume		= usb_serial_resume,
6261ded7ea4SMing Lei 	.reset_resume	= ch341_reset_resume,
6276ce76104SFrank A Kingswood 	.id_table	= id_table,
6286ce76104SFrank A Kingswood 	.no_dynamic_id	= 1,
6291ded7ea4SMing Lei 	.supports_autosuspend =	1,
6306ce76104SFrank A Kingswood };
6316ce76104SFrank A Kingswood 
6326ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = {
6336ce76104SFrank A Kingswood 	.driver = {
6346ce76104SFrank A Kingswood 		.owner	= THIS_MODULE,
6356ce76104SFrank A Kingswood 		.name	= "ch341-uart",
6366ce76104SFrank A Kingswood 	},
6376ce76104SFrank A Kingswood 	.id_table          = id_table,
6386ce76104SFrank A Kingswood 	.usb_driver        = &ch341_driver,
6396ce76104SFrank A Kingswood 	.num_ports         = 1,
6406ce76104SFrank A Kingswood 	.open              = ch341_open,
641335f8514SAlan Cox 	.dtr_rts	   = ch341_dtr_rts,
642335f8514SAlan Cox 	.carrier_raised	   = ch341_carrier_raised,
643664d5df9SWerner Cornelius 	.close             = ch341_close,
644664d5df9SWerner Cornelius 	.ioctl             = ch341_ioctl,
6456ce76104SFrank A Kingswood 	.set_termios       = ch341_set_termios,
646492896f0STim Small 	.break_ctl         = ch341_break_ctl,
647664d5df9SWerner Cornelius 	.tiocmget          = ch341_tiocmget,
648664d5df9SWerner Cornelius 	.tiocmset          = ch341_tiocmset,
649664d5df9SWerner Cornelius 	.read_int_callback = ch341_read_int_callback,
6506ce76104SFrank A Kingswood 	.attach            = ch341_attach,
6516ce76104SFrank A Kingswood };
6526ce76104SFrank A Kingswood 
6536ce76104SFrank A Kingswood static int __init ch341_init(void)
6546ce76104SFrank A Kingswood {
6556ce76104SFrank A Kingswood 	int retval;
6566ce76104SFrank A Kingswood 
6576ce76104SFrank A Kingswood 	retval = usb_serial_register(&ch341_device);
6586ce76104SFrank A Kingswood 	if (retval)
6596ce76104SFrank A Kingswood 		return retval;
6606ce76104SFrank A Kingswood 	retval = usb_register(&ch341_driver);
6616ce76104SFrank A Kingswood 	if (retval)
6626ce76104SFrank A Kingswood 		usb_serial_deregister(&ch341_device);
6636ce76104SFrank A Kingswood 	return retval;
6646ce76104SFrank A Kingswood }
6656ce76104SFrank A Kingswood 
6666ce76104SFrank A Kingswood static void __exit ch341_exit(void)
6676ce76104SFrank A Kingswood {
6686ce76104SFrank A Kingswood 	usb_deregister(&ch341_driver);
6696ce76104SFrank A Kingswood 	usb_serial_deregister(&ch341_device);
6706ce76104SFrank A Kingswood }
6716ce76104SFrank A Kingswood 
6726ce76104SFrank A Kingswood module_init(ch341_init);
6736ce76104SFrank A Kingswood module_exit(ch341_exit);
6746ce76104SFrank A Kingswood MODULE_LICENSE("GPL");
6756ce76104SFrank A Kingswood 
6766ce76104SFrank A Kingswood module_param(debug, bool, S_IRUGO | S_IWUSR);
6776ce76104SFrank A Kingswood MODULE_PARM_DESC(debug, "Debug enabled or not");
6786ce76104SFrank A Kingswood 
6796ce76104SFrank A Kingswood /* EOF ch341.c */
680