xref: /linux/drivers/usb/serial/ch341.c (revision d0781383038e983a63843a9a6a067ed781db89c1)
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) },
78*d0781383Swangyanqing 	{ USB_DEVICE(0x1a86, 0x5523) },
796ce76104SFrank A Kingswood 	{ },
806ce76104SFrank A Kingswood };
816ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table);
826ce76104SFrank A Kingswood 
836ce76104SFrank A Kingswood struct ch341_private {
84664d5df9SWerner Cornelius 	spinlock_t lock; /* access lock */
85664d5df9SWerner Cornelius 	wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
86664d5df9SWerner Cornelius 	unsigned baud_rate; /* set baud rate */
87664d5df9SWerner Cornelius 	u8 line_control; /* set line control value RTS/DTR */
88664d5df9SWerner Cornelius 	u8 line_status; /* active status of modem control inputs */
89664d5df9SWerner Cornelius 	u8 multi_status_change; /* status changed multiple since last call */
906ce76104SFrank A Kingswood };
916ce76104SFrank A Kingswood 
926ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request,
936ce76104SFrank A Kingswood 			     u16 value, u16 index)
946ce76104SFrank A Kingswood {
956ce76104SFrank A Kingswood 	int r;
966ce76104SFrank A Kingswood 	dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
976ce76104SFrank A Kingswood 		(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;
1116ce76104SFrank A Kingswood 	dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
1126ce76104SFrank A Kingswood 		(int)request, (int)value, (int)index, buf, (int)bufsize);
1136ce76104SFrank A Kingswood 
1146ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
1156ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1166ce76104SFrank A Kingswood 			    value, index, buf, bufsize, DEFAULT_TIMEOUT);
1176ce76104SFrank A Kingswood 	return r;
1186ce76104SFrank A Kingswood }
1196ce76104SFrank A Kingswood 
12093b6497dSAdrian Bunk static int ch341_set_baudrate(struct usb_device *dev,
12193b6497dSAdrian Bunk 			      struct ch341_private *priv)
1226ce76104SFrank A Kingswood {
1236ce76104SFrank A Kingswood 	short a, b;
1246ce76104SFrank A Kingswood 	int r;
125664d5df9SWerner Cornelius 	unsigned long factor;
126664d5df9SWerner Cornelius 	short divisor;
1276ce76104SFrank A Kingswood 
1286ce76104SFrank A Kingswood 	dbg("ch341_set_baudrate(%d)", priv->baud_rate);
129664d5df9SWerner Cornelius 
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 	dbg("ch341_set_handshake(0x%02x)", control);
157664d5df9SWerner Cornelius 	return ch341_control_out(dev, 0xa4, ~control, 0);
1586ce76104SFrank A Kingswood }
1596ce76104SFrank A Kingswood 
160664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
1616ce76104SFrank A Kingswood {
1626ce76104SFrank A Kingswood 	char *buffer;
1636ce76104SFrank A Kingswood 	int r;
1646ce76104SFrank A Kingswood 	const unsigned size = 8;
165664d5df9SWerner Cornelius 	unsigned long flags;
1666ce76104SFrank A Kingswood 
1676ce76104SFrank A Kingswood 	dbg("ch341_get_status()");
1686ce76104SFrank A Kingswood 
1696ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
1706ce76104SFrank A Kingswood 	if (!buffer)
1716ce76104SFrank A Kingswood 		return -ENOMEM;
1726ce76104SFrank A Kingswood 
1736ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
1746ce76104SFrank A Kingswood 	if (r < 0)
1756ce76104SFrank A Kingswood 		goto out;
1766ce76104SFrank A Kingswood 
177664d5df9SWerner Cornelius 	/* setup the private status if available */
178664d5df9SWerner Cornelius 	if (r == 2) {
1796ce76104SFrank A Kingswood 		r = 0;
180664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
181664d5df9SWerner Cornelius 		priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
182664d5df9SWerner Cornelius 		priv->multi_status_change = 0;
183664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
184664d5df9SWerner Cornelius 	} else
185664d5df9SWerner Cornelius 		r = -EPROTO;
1866ce76104SFrank A Kingswood 
1876ce76104SFrank A Kingswood out:	kfree(buffer);
1886ce76104SFrank A Kingswood 	return r;
1896ce76104SFrank A Kingswood }
1906ce76104SFrank A Kingswood 
1916ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */
1926ce76104SFrank A Kingswood 
19393b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
1946ce76104SFrank A Kingswood {
1956ce76104SFrank A Kingswood 	char *buffer;
1966ce76104SFrank A Kingswood 	int r;
1976ce76104SFrank A Kingswood 	const unsigned size = 8;
1986ce76104SFrank A Kingswood 
1996ce76104SFrank A Kingswood 	dbg("ch341_configure()");
2006ce76104SFrank A Kingswood 
2016ce76104SFrank A Kingswood 	buffer = kmalloc(size, GFP_KERNEL);
2026ce76104SFrank A Kingswood 	if (!buffer)
2036ce76104SFrank A Kingswood 		return -ENOMEM;
2046ce76104SFrank A Kingswood 
2056ce76104SFrank A Kingswood 	/* expect two bytes 0x27 0x00 */
2066ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
2076ce76104SFrank A Kingswood 	if (r < 0)
2086ce76104SFrank A Kingswood 		goto out;
2096ce76104SFrank A Kingswood 
2106ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0, 0);
2116ce76104SFrank A Kingswood 	if (r < 0)
2126ce76104SFrank A Kingswood 		goto out;
2136ce76104SFrank A Kingswood 
2146ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2156ce76104SFrank A Kingswood 	if (r < 0)
2166ce76104SFrank A Kingswood 		goto out;
2176ce76104SFrank A Kingswood 
2186ce76104SFrank A Kingswood 	/* expect two bytes 0x56 0x00 */
2196ce76104SFrank A Kingswood 	r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
2206ce76104SFrank A Kingswood 	if (r < 0)
2216ce76104SFrank A Kingswood 		goto out;
2226ce76104SFrank A Kingswood 
2236ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
2246ce76104SFrank A Kingswood 	if (r < 0)
2256ce76104SFrank A Kingswood 		goto out;
2266ce76104SFrank A Kingswood 
2276ce76104SFrank A Kingswood 	/* expect 0xff 0xee */
228664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2296ce76104SFrank A Kingswood 	if (r < 0)
2306ce76104SFrank A Kingswood 		goto out;
2316ce76104SFrank A Kingswood 
2326ce76104SFrank A Kingswood 	r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
2336ce76104SFrank A Kingswood 	if (r < 0)
2346ce76104SFrank A Kingswood 		goto out;
2356ce76104SFrank A Kingswood 
2366ce76104SFrank A Kingswood 	r = ch341_set_baudrate(dev, priv);
2376ce76104SFrank A Kingswood 	if (r < 0)
2386ce76104SFrank A Kingswood 		goto out;
2396ce76104SFrank A Kingswood 
240664d5df9SWerner Cornelius 	r = ch341_set_handshake(dev, priv->line_control);
2416ce76104SFrank A Kingswood 	if (r < 0)
2426ce76104SFrank A Kingswood 		goto out;
2436ce76104SFrank A Kingswood 
2446ce76104SFrank A Kingswood 	/* expect 0x9f 0xee */
245664d5df9SWerner Cornelius 	r = ch341_get_status(dev, priv);
2466ce76104SFrank A Kingswood 
2476ce76104SFrank A Kingswood out:	kfree(buffer);
2486ce76104SFrank A Kingswood 	return r;
2496ce76104SFrank A Kingswood }
2506ce76104SFrank A Kingswood 
2516ce76104SFrank A Kingswood /* allocate private data */
2526ce76104SFrank A Kingswood static int ch341_attach(struct usb_serial *serial)
2536ce76104SFrank A Kingswood {
2546ce76104SFrank A Kingswood 	struct ch341_private *priv;
2556ce76104SFrank A Kingswood 	int r;
2566ce76104SFrank A Kingswood 
2576ce76104SFrank A Kingswood 	dbg("ch341_attach()");
2586ce76104SFrank A Kingswood 
2596ce76104SFrank A Kingswood 	/* private data */
2606ce76104SFrank A Kingswood 	priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
2616ce76104SFrank A Kingswood 	if (!priv)
2626ce76104SFrank A Kingswood 		return -ENOMEM;
2636ce76104SFrank A Kingswood 
264664d5df9SWerner Cornelius 	spin_lock_init(&priv->lock);
265664d5df9SWerner Cornelius 	init_waitqueue_head(&priv->delta_msr_wait);
2666ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
267664d5df9SWerner Cornelius 	priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
2686ce76104SFrank A Kingswood 
2696ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
2706ce76104SFrank A Kingswood 	if (r < 0)
2716ce76104SFrank A Kingswood 		goto error;
2726ce76104SFrank A Kingswood 
2736ce76104SFrank A Kingswood 	usb_set_serial_port_data(serial->port[0], priv);
2746ce76104SFrank A Kingswood 	return 0;
2756ce76104SFrank A Kingswood 
2766ce76104SFrank A Kingswood error:	kfree(priv);
2776ce76104SFrank A Kingswood 	return r;
2786ce76104SFrank A Kingswood }
2796ce76104SFrank A Kingswood 
280335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port)
281335f8514SAlan Cox {
282335f8514SAlan Cox 	struct ch341_private *priv = usb_get_serial_port_data(port);
283335f8514SAlan Cox 	if (priv->line_status & CH341_BIT_DCD)
284335f8514SAlan Cox 		return 1;
285335f8514SAlan Cox 	return 0;
286335f8514SAlan Cox }
287335f8514SAlan Cox 
288335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on)
289664d5df9SWerner Cornelius {
290664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
291664d5df9SWerner Cornelius 	unsigned long flags;
292664d5df9SWerner Cornelius 
293664d5df9SWerner Cornelius 	dbg("%s - port %d", __func__, port->number);
294335f8514SAlan Cox 	/* drop DTR and RTS */
295335f8514SAlan Cox 	spin_lock_irqsave(&priv->lock, flags);
296335f8514SAlan Cox 	if (on)
297335f8514SAlan Cox 		priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
298335f8514SAlan Cox 	else
299335f8514SAlan Cox 		priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
300335f8514SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
301335f8514SAlan Cox 	ch341_set_handshake(port->serial->dev, priv->line_control);
302335f8514SAlan Cox 	wake_up_interruptible(&priv->delta_msr_wait);
303335f8514SAlan Cox }
304335f8514SAlan Cox 
305335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port)
306335f8514SAlan Cox {
307335f8514SAlan Cox 	dbg("%s - port %d", __func__, port->number);
308664d5df9SWerner Cornelius 
309f26788daSJohan Hovold 	usb_serial_generic_close(port);
310664d5df9SWerner Cornelius 	usb_kill_urb(port->interrupt_in_urb);
311664d5df9SWerner Cornelius }
312664d5df9SWerner Cornelius 
313664d5df9SWerner Cornelius 
3146ce76104SFrank A Kingswood /* open this device, set default parameters */
315a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
3166ce76104SFrank A Kingswood {
3176ce76104SFrank A Kingswood 	struct usb_serial *serial = port->serial;
3186ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
3196ce76104SFrank A Kingswood 	int r;
3206ce76104SFrank A Kingswood 
3216ce76104SFrank A Kingswood 	dbg("ch341_open()");
3226ce76104SFrank A Kingswood 
3236ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
3246ce76104SFrank A Kingswood 
3256ce76104SFrank A Kingswood 	r = ch341_configure(serial->dev, priv);
3266ce76104SFrank A Kingswood 	if (r)
3276ce76104SFrank A Kingswood 		goto out;
3286ce76104SFrank A Kingswood 
329664d5df9SWerner Cornelius 	r = ch341_set_handshake(serial->dev, priv->line_control);
3306ce76104SFrank A Kingswood 	if (r)
3316ce76104SFrank A Kingswood 		goto out;
3326ce76104SFrank A Kingswood 
3336ce76104SFrank A Kingswood 	r = ch341_set_baudrate(serial->dev, priv);
3346ce76104SFrank A Kingswood 	if (r)
3356ce76104SFrank A Kingswood 		goto out;
3366ce76104SFrank A Kingswood 
337664d5df9SWerner Cornelius 	dbg("%s - submitting interrupt urb", __func__);
338664d5df9SWerner Cornelius 	port->interrupt_in_urb->dev = serial->dev;
339664d5df9SWerner Cornelius 	r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
340664d5df9SWerner Cornelius 	if (r) {
341664d5df9SWerner Cornelius 		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
342664d5df9SWerner Cornelius 			" error %d\n", __func__, r);
343335f8514SAlan Cox 		ch341_close(port);
344664d5df9SWerner Cornelius 		return -EPROTO;
345664d5df9SWerner Cornelius 	}
346664d5df9SWerner Cornelius 
347a509a7e4SAlan Cox 	r = usb_serial_generic_open(tty, port);
3486ce76104SFrank A Kingswood 
3496ce76104SFrank A Kingswood out:	return r;
3506ce76104SFrank A Kingswood }
3516ce76104SFrank A Kingswood 
3526ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and
3536ce76104SFrank A Kingswood  * tty->termios contains the new setting to be used.
3546ce76104SFrank A Kingswood  */
35595da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty,
35695da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3576ce76104SFrank A Kingswood {
3586ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(port);
3596ce76104SFrank A Kingswood 	unsigned baud_rate;
360664d5df9SWerner Cornelius 	unsigned long flags;
3616ce76104SFrank A Kingswood 
3626ce76104SFrank A Kingswood 	dbg("ch341_set_termios()");
3636ce76104SFrank A Kingswood 
3646ce76104SFrank A Kingswood 	baud_rate = tty_get_baud_rate(tty);
3656ce76104SFrank A Kingswood 
3666ce76104SFrank A Kingswood 	priv->baud_rate = baud_rate;
367664d5df9SWerner Cornelius 
368664d5df9SWerner Cornelius 	if (baud_rate) {
369664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
370664d5df9SWerner Cornelius 		priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
371664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
372664d5df9SWerner Cornelius 		ch341_set_baudrate(port->serial->dev, priv);
373664d5df9SWerner Cornelius 	} else {
374664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
375664d5df9SWerner Cornelius 		priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
376664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
3776ce76104SFrank A Kingswood 	}
3786ce76104SFrank A Kingswood 
379664d5df9SWerner Cornelius 	ch341_set_handshake(port->serial->dev, priv->line_control);
3806ce76104SFrank A Kingswood 
3816ce76104SFrank A Kingswood 	/* Unimplemented:
3826ce76104SFrank A Kingswood 	 * (cflag & CSIZE) : data bits [5, 8]
3836ce76104SFrank A Kingswood 	 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
3846ce76104SFrank A Kingswood 	 * (cflag & CSTOPB) : stop bits [1, 2]
3856ce76104SFrank A Kingswood 	 */
386664d5df9SWerner Cornelius }
38773f59308SAlan Cox 
388492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state)
389492896f0STim Small {
390492896f0STim Small 	const uint16_t ch341_break_reg =
391492896f0STim Small 		CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
392492896f0STim Small 	struct usb_serial_port *port = tty->driver_data;
393492896f0STim Small 	int r;
394492896f0STim Small 	uint16_t reg_contents;
395f2b5cc83SJohan Hovold 	uint8_t *break_reg;
396492896f0STim Small 
397492896f0STim Small 	dbg("%s()", __func__);
398492896f0STim Small 
399f2b5cc83SJohan Hovold 	break_reg = kmalloc(2, GFP_KERNEL);
400f2b5cc83SJohan Hovold 	if (!break_reg) {
401f2b5cc83SJohan Hovold 		dev_err(&port->dev, "%s - kmalloc failed\n", __func__);
402f2b5cc83SJohan Hovold 		return;
403f2b5cc83SJohan Hovold 	}
404f2b5cc83SJohan Hovold 
405492896f0STim Small 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
406f2b5cc83SJohan Hovold 			ch341_break_reg, 0, break_reg, 2);
407492896f0STim Small 	if (r < 0) {
4086a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
4096a9b15feSJohan Hovold 				__func__, r);
410f2b5cc83SJohan Hovold 		goto out;
411492896f0STim Small 	}
412492896f0STim Small 	dbg("%s - initial ch341 break register contents - reg1: %x, reg2: %x",
413492896f0STim Small 			__func__, break_reg[0], break_reg[1]);
414492896f0STim Small 	if (break_state != 0) {
415492896f0STim Small 		dbg("%s - Enter break state requested", __func__);
416492896f0STim Small 		break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
417492896f0STim Small 		break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
418492896f0STim Small 	} else {
419492896f0STim Small 		dbg("%s - Leave break state requested", __func__);
420492896f0STim Small 		break_reg[0] |= CH341_NBREAK_BITS_REG1;
421492896f0STim Small 		break_reg[1] |= CH341_NBREAK_BITS_REG2;
422492896f0STim Small 	}
423492896f0STim Small 	dbg("%s - New ch341 break register contents - reg1: %x, reg2: %x",
424492896f0STim Small 			__func__, break_reg[0], break_reg[1]);
4255be796f0SJohan Hovold 	reg_contents = get_unaligned_le16(break_reg);
426492896f0STim Small 	r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
427492896f0STim Small 			ch341_break_reg, reg_contents);
428492896f0STim Small 	if (r < 0)
4296a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
4306a9b15feSJohan Hovold 				__func__, r);
431f2b5cc83SJohan Hovold out:
432f2b5cc83SJohan Hovold 	kfree(break_reg);
433492896f0STim Small }
434492896f0STim Small 
435664d5df9SWerner Cornelius static int ch341_tiocmset(struct tty_struct *tty, struct file *file,
436664d5df9SWerner Cornelius 			  unsigned int set, unsigned int clear)
437664d5df9SWerner Cornelius {
438664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
439664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
440664d5df9SWerner Cornelius 	unsigned long flags;
441664d5df9SWerner Cornelius 	u8 control;
442664d5df9SWerner Cornelius 
443664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
444664d5df9SWerner Cornelius 	if (set & TIOCM_RTS)
445664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_RTS;
446664d5df9SWerner Cornelius 	if (set & TIOCM_DTR)
447664d5df9SWerner Cornelius 		priv->line_control |= CH341_BIT_DTR;
448664d5df9SWerner Cornelius 	if (clear & TIOCM_RTS)
449664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_RTS;
450664d5df9SWerner Cornelius 	if (clear & TIOCM_DTR)
451664d5df9SWerner Cornelius 		priv->line_control &= ~CH341_BIT_DTR;
452664d5df9SWerner Cornelius 	control = priv->line_control;
453664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
454664d5df9SWerner Cornelius 
455664d5df9SWerner Cornelius 	return ch341_set_handshake(port->serial->dev, control);
456664d5df9SWerner Cornelius }
457664d5df9SWerner Cornelius 
458664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb)
459664d5df9SWerner Cornelius {
460664d5df9SWerner Cornelius 	struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
461664d5df9SWerner Cornelius 	unsigned char *data = urb->transfer_buffer;
462664d5df9SWerner Cornelius 	unsigned int actual_length = urb->actual_length;
463664d5df9SWerner Cornelius 	int status;
464664d5df9SWerner Cornelius 
465664d5df9SWerner Cornelius 	dbg("%s (%d)", __func__, port->number);
466664d5df9SWerner Cornelius 
467664d5df9SWerner Cornelius 	switch (urb->status) {
468664d5df9SWerner Cornelius 	case 0:
469664d5df9SWerner Cornelius 		/* success */
470664d5df9SWerner Cornelius 		break;
471664d5df9SWerner Cornelius 	case -ECONNRESET:
472664d5df9SWerner Cornelius 	case -ENOENT:
473664d5df9SWerner Cornelius 	case -ESHUTDOWN:
474664d5df9SWerner Cornelius 		/* this urb is terminated, clean up */
475664d5df9SWerner Cornelius 		dbg("%s - urb shutting down with status: %d", __func__,
476664d5df9SWerner Cornelius 		    urb->status);
477664d5df9SWerner Cornelius 		return;
478664d5df9SWerner Cornelius 	default:
479664d5df9SWerner Cornelius 		dbg("%s - nonzero urb status received: %d", __func__,
480664d5df9SWerner Cornelius 		    urb->status);
481664d5df9SWerner Cornelius 		goto exit;
482664d5df9SWerner Cornelius 	}
483664d5df9SWerner Cornelius 
484664d5df9SWerner Cornelius 	usb_serial_debug_data(debug, &port->dev, __func__,
485664d5df9SWerner Cornelius 			      urb->actual_length, urb->transfer_buffer);
486664d5df9SWerner Cornelius 
487664d5df9SWerner Cornelius 	if (actual_length >= 4) {
488664d5df9SWerner Cornelius 		struct ch341_private *priv = usb_get_serial_port_data(port);
489664d5df9SWerner Cornelius 		unsigned long flags;
490d14fc1a7SLibor Pechacek 		u8 prev_line_status = priv->line_status;
491664d5df9SWerner Cornelius 
492664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
493664d5df9SWerner Cornelius 		priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
494664d5df9SWerner Cornelius 		if ((data[1] & CH341_MULT_STAT))
495664d5df9SWerner Cornelius 			priv->multi_status_change = 1;
496664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
497d14fc1a7SLibor Pechacek 
498d14fc1a7SLibor Pechacek 		if ((priv->line_status ^ prev_line_status) & CH341_BIT_DCD) {
499d14fc1a7SLibor Pechacek 			struct tty_struct *tty = tty_port_tty_get(&port->port);
500d14fc1a7SLibor Pechacek 			if (tty)
501d14fc1a7SLibor Pechacek 				usb_serial_handle_dcd_change(port, tty,
502d14fc1a7SLibor Pechacek 					    priv->line_status & CH341_BIT_DCD);
503d14fc1a7SLibor Pechacek 			tty_kref_put(tty);
504d14fc1a7SLibor Pechacek 		}
505d14fc1a7SLibor Pechacek 
506664d5df9SWerner Cornelius 		wake_up_interruptible(&priv->delta_msr_wait);
507664d5df9SWerner Cornelius 	}
508664d5df9SWerner Cornelius 
509664d5df9SWerner Cornelius exit:
510664d5df9SWerner Cornelius 	status = usb_submit_urb(urb, GFP_ATOMIC);
511664d5df9SWerner Cornelius 	if (status)
512664d5df9SWerner Cornelius 		dev_err(&urb->dev->dev,
513664d5df9SWerner Cornelius 			"%s - usb_submit_urb failed with result %d\n",
514664d5df9SWerner Cornelius 			__func__, status);
515664d5df9SWerner Cornelius }
516664d5df9SWerner Cornelius 
517664d5df9SWerner Cornelius static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
518664d5df9SWerner Cornelius {
519664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
520664d5df9SWerner Cornelius 	unsigned long flags;
521664d5df9SWerner Cornelius 	u8 prevstatus;
522664d5df9SWerner Cornelius 	u8 status;
523664d5df9SWerner Cornelius 	u8 changed;
524664d5df9SWerner Cornelius 	u8 multi_change = 0;
525664d5df9SWerner Cornelius 
526664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
527664d5df9SWerner Cornelius 	prevstatus = priv->line_status;
528664d5df9SWerner Cornelius 	priv->multi_status_change = 0;
529664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
530664d5df9SWerner Cornelius 
531664d5df9SWerner Cornelius 	while (!multi_change) {
532664d5df9SWerner Cornelius 		interruptible_sleep_on(&priv->delta_msr_wait);
533664d5df9SWerner Cornelius 		/* see if a signal did it */
534664d5df9SWerner Cornelius 		if (signal_pending(current))
535664d5df9SWerner Cornelius 			return -ERESTARTSYS;
536664d5df9SWerner Cornelius 
537664d5df9SWerner Cornelius 		spin_lock_irqsave(&priv->lock, flags);
538664d5df9SWerner Cornelius 		status = priv->line_status;
539664d5df9SWerner Cornelius 		multi_change = priv->multi_status_change;
540664d5df9SWerner Cornelius 		spin_unlock_irqrestore(&priv->lock, flags);
541664d5df9SWerner Cornelius 
542664d5df9SWerner Cornelius 		changed = prevstatus ^ status;
543664d5df9SWerner Cornelius 
544664d5df9SWerner Cornelius 		if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
545664d5df9SWerner Cornelius 		    ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
546664d5df9SWerner Cornelius 		    ((arg & TIOCM_CD)  && (changed & CH341_BIT_DCD)) ||
547664d5df9SWerner Cornelius 		    ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
548664d5df9SWerner Cornelius 			return 0;
549664d5df9SWerner Cornelius 		}
550664d5df9SWerner Cornelius 		prevstatus = status;
551664d5df9SWerner Cornelius 	}
552664d5df9SWerner Cornelius 
553664d5df9SWerner Cornelius 	return 0;
554664d5df9SWerner Cornelius }
555664d5df9SWerner Cornelius 
556664d5df9SWerner Cornelius /*static int ch341_ioctl(struct usb_serial_port *port, struct file *file,*/
557664d5df9SWerner Cornelius static int ch341_ioctl(struct tty_struct *tty, struct file *file,
558664d5df9SWerner Cornelius 			unsigned int cmd, unsigned long arg)
559664d5df9SWerner Cornelius {
560664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
561664d5df9SWerner Cornelius 	dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
562664d5df9SWerner Cornelius 
563664d5df9SWerner Cornelius 	switch (cmd) {
564664d5df9SWerner Cornelius 	case TIOCMIWAIT:
565664d5df9SWerner Cornelius 		dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
566664d5df9SWerner Cornelius 		return wait_modem_info(port, arg);
567664d5df9SWerner Cornelius 
568664d5df9SWerner Cornelius 	default:
569664d5df9SWerner Cornelius 		dbg("%s not supported = 0x%04x", __func__, cmd);
570664d5df9SWerner Cornelius 		break;
571664d5df9SWerner Cornelius 	}
572664d5df9SWerner Cornelius 
573664d5df9SWerner Cornelius 	return -ENOIOCTLCMD;
574664d5df9SWerner Cornelius }
575664d5df9SWerner Cornelius 
576664d5df9SWerner Cornelius static int ch341_tiocmget(struct tty_struct *tty, struct file *file)
577664d5df9SWerner Cornelius {
578664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
579664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
580664d5df9SWerner Cornelius 	unsigned long flags;
581664d5df9SWerner Cornelius 	u8 mcr;
582664d5df9SWerner Cornelius 	u8 status;
583664d5df9SWerner Cornelius 	unsigned int result;
584664d5df9SWerner Cornelius 
585664d5df9SWerner Cornelius 	dbg("%s (%d)", __func__, port->number);
586664d5df9SWerner Cornelius 
587664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
588664d5df9SWerner Cornelius 	mcr = priv->line_control;
589664d5df9SWerner Cornelius 	status = priv->line_status;
590664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
591664d5df9SWerner Cornelius 
592664d5df9SWerner Cornelius 	result = ((mcr & CH341_BIT_DTR)		? TIOCM_DTR : 0)
593664d5df9SWerner Cornelius 		  | ((mcr & CH341_BIT_RTS)	? TIOCM_RTS : 0)
594664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_CTS)	? TIOCM_CTS : 0)
595664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DSR)	? TIOCM_DSR : 0)
596664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_RI)	? TIOCM_RI  : 0)
597664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DCD)	? TIOCM_CD  : 0);
598664d5df9SWerner Cornelius 
599664d5df9SWerner Cornelius 	dbg("%s - result = %x", __func__, result);
600664d5df9SWerner Cornelius 
601664d5df9SWerner Cornelius 	return result;
6026ce76104SFrank A Kingswood }
6036ce76104SFrank A Kingswood 
6041ded7ea4SMing Lei 
6051ded7ea4SMing Lei static int ch341_reset_resume(struct usb_interface *intf)
6061ded7ea4SMing Lei {
6071ded7ea4SMing Lei 	struct usb_device *dev = interface_to_usbdev(intf);
6081ded7ea4SMing Lei 	struct usb_serial *serial = NULL;
6091ded7ea4SMing Lei 	struct ch341_private *priv;
6101ded7ea4SMing Lei 
6111ded7ea4SMing Lei 	serial = usb_get_intfdata(intf);
6121ded7ea4SMing Lei 	priv = usb_get_serial_port_data(serial->port[0]);
6131ded7ea4SMing Lei 
6141ded7ea4SMing Lei 	/*reconfigure ch341 serial port after bus-reset*/
6151ded7ea4SMing Lei 	ch341_configure(dev, priv);
6161ded7ea4SMing Lei 
6171ded7ea4SMing Lei 	usb_serial_resume(intf);
6181ded7ea4SMing Lei 
6191ded7ea4SMing Lei 	return 0;
6201ded7ea4SMing Lei }
6211ded7ea4SMing Lei 
6226ce76104SFrank A Kingswood static struct usb_driver ch341_driver = {
6236ce76104SFrank A Kingswood 	.name		= "ch341",
6246ce76104SFrank A Kingswood 	.probe		= usb_serial_probe,
6256ce76104SFrank A Kingswood 	.disconnect	= usb_serial_disconnect,
6261ded7ea4SMing Lei 	.suspend	= usb_serial_suspend,
6271ded7ea4SMing Lei 	.resume		= usb_serial_resume,
6281ded7ea4SMing Lei 	.reset_resume	= ch341_reset_resume,
6296ce76104SFrank A Kingswood 	.id_table	= id_table,
6306ce76104SFrank A Kingswood 	.no_dynamic_id	= 1,
6311ded7ea4SMing Lei 	.supports_autosuspend =	1,
6326ce76104SFrank A Kingswood };
6336ce76104SFrank A Kingswood 
6346ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = {
6356ce76104SFrank A Kingswood 	.driver = {
6366ce76104SFrank A Kingswood 		.owner	= THIS_MODULE,
6376ce76104SFrank A Kingswood 		.name	= "ch341-uart",
6386ce76104SFrank A Kingswood 	},
6396ce76104SFrank A Kingswood 	.id_table          = id_table,
6406ce76104SFrank A Kingswood 	.usb_driver        = &ch341_driver,
6416ce76104SFrank A Kingswood 	.num_ports         = 1,
6426ce76104SFrank A Kingswood 	.open              = ch341_open,
643335f8514SAlan Cox 	.dtr_rts	   = ch341_dtr_rts,
644335f8514SAlan Cox 	.carrier_raised	   = ch341_carrier_raised,
645664d5df9SWerner Cornelius 	.close             = ch341_close,
646664d5df9SWerner Cornelius 	.ioctl             = ch341_ioctl,
6476ce76104SFrank A Kingswood 	.set_termios       = ch341_set_termios,
648492896f0STim Small 	.break_ctl         = ch341_break_ctl,
649664d5df9SWerner Cornelius 	.tiocmget          = ch341_tiocmget,
650664d5df9SWerner Cornelius 	.tiocmset          = ch341_tiocmset,
651664d5df9SWerner Cornelius 	.read_int_callback = ch341_read_int_callback,
6526ce76104SFrank A Kingswood 	.attach            = ch341_attach,
6536ce76104SFrank A Kingswood };
6546ce76104SFrank A Kingswood 
6556ce76104SFrank A Kingswood static int __init ch341_init(void)
6566ce76104SFrank A Kingswood {
6576ce76104SFrank A Kingswood 	int retval;
6586ce76104SFrank A Kingswood 
6596ce76104SFrank A Kingswood 	retval = usb_serial_register(&ch341_device);
6606ce76104SFrank A Kingswood 	if (retval)
6616ce76104SFrank A Kingswood 		return retval;
6626ce76104SFrank A Kingswood 	retval = usb_register(&ch341_driver);
6636ce76104SFrank A Kingswood 	if (retval)
6646ce76104SFrank A Kingswood 		usb_serial_deregister(&ch341_device);
6656ce76104SFrank A Kingswood 	return retval;
6666ce76104SFrank A Kingswood }
6676ce76104SFrank A Kingswood 
6686ce76104SFrank A Kingswood static void __exit ch341_exit(void)
6696ce76104SFrank A Kingswood {
6706ce76104SFrank A Kingswood 	usb_deregister(&ch341_driver);
6716ce76104SFrank A Kingswood 	usb_serial_deregister(&ch341_device);
6726ce76104SFrank A Kingswood }
6736ce76104SFrank A Kingswood 
6746ce76104SFrank A Kingswood module_init(ch341_init);
6756ce76104SFrank A Kingswood module_exit(ch341_exit);
6766ce76104SFrank A Kingswood MODULE_LICENSE("GPL");
6776ce76104SFrank A Kingswood 
6786ce76104SFrank A Kingswood module_param(debug, bool, S_IRUGO | S_IWUSR);
6796ce76104SFrank A Kingswood MODULE_PARM_DESC(debug, "Debug enabled or not");
6806ce76104SFrank A Kingswood 
6816ce76104SFrank A Kingswood /* EOF ch341.c */
682