xref: /linux/drivers/usb/serial/ch341.c (revision 9f4dc05107a6db3743e6b9ea4014cbdc3795682d)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
26ce76104SFrank A Kingswood /*
36ce76104SFrank A Kingswood  * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
4664d5df9SWerner Cornelius  * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
5664d5df9SWerner Cornelius  * Copyright 2009, Boris Hajduk <boris@hajduk.org>
66ce76104SFrank A Kingswood  *
76ce76104SFrank A Kingswood  * ch341.c implements a serial port driver for the Winchiphead CH341.
86ce76104SFrank A Kingswood  *
96ce76104SFrank A Kingswood  * The CH341 device can be used to implement an RS232 asynchronous
106ce76104SFrank A Kingswood  * serial port, an IEEE-1284 parallel printer port or a memory-like
116ce76104SFrank A Kingswood  * interface. In all cases the CH341 supports an I2C interface as well.
126ce76104SFrank A Kingswood  * This driver only supports the asynchronous serial interface.
136ce76104SFrank A Kingswood  */
146ce76104SFrank A Kingswood 
156ce76104SFrank A Kingswood #include <linux/kernel.h>
166ce76104SFrank A Kingswood #include <linux/tty.h>
176ce76104SFrank A Kingswood #include <linux/module.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
196ce76104SFrank A Kingswood #include <linux/usb.h>
206ce76104SFrank A Kingswood #include <linux/usb/serial.h>
216ce76104SFrank A Kingswood #include <linux/serial.h>
225be796f0SJohan Hovold #include <asm/unaligned.h>
236ce76104SFrank A Kingswood 
24664d5df9SWerner Cornelius #define DEFAULT_BAUD_RATE 9600
256ce76104SFrank A Kingswood #define DEFAULT_TIMEOUT   1000
266ce76104SFrank A Kingswood 
27664d5df9SWerner Cornelius /* flags for IO-Bits */
28664d5df9SWerner Cornelius #define CH341_BIT_RTS (1 << 6)
29664d5df9SWerner Cornelius #define CH341_BIT_DTR (1 << 5)
30664d5df9SWerner Cornelius 
31664d5df9SWerner Cornelius /******************************/
32664d5df9SWerner Cornelius /* interrupt pipe definitions */
33664d5df9SWerner Cornelius /******************************/
34664d5df9SWerner Cornelius /* always 4 interrupt bytes */
35664d5df9SWerner Cornelius /* first irq byte normally 0x08 */
36664d5df9SWerner Cornelius /* second irq byte base 0x7d + below */
37664d5df9SWerner Cornelius /* third irq byte base 0x94 + below */
38664d5df9SWerner Cornelius /* fourth irq byte normally 0xee */
39664d5df9SWerner Cornelius 
40664d5df9SWerner Cornelius /* second interrupt byte */
41664d5df9SWerner Cornelius #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
42664d5df9SWerner Cornelius 
43664d5df9SWerner Cornelius /* status returned in third interrupt answer byte, inverted in data
44664d5df9SWerner Cornelius    from irq */
45664d5df9SWerner Cornelius #define CH341_BIT_CTS 0x01
46664d5df9SWerner Cornelius #define CH341_BIT_DSR 0x02
47664d5df9SWerner Cornelius #define CH341_BIT_RI  0x04
48664d5df9SWerner Cornelius #define CH341_BIT_DCD 0x08
49664d5df9SWerner Cornelius #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
50664d5df9SWerner Cornelius 
51492896f0STim Small /* Break support - the information used to implement this was gleaned from
52492896f0STim Small  * the Net/FreeBSD uchcom.c driver by Takanori Watanabe.  Domo arigato.
53492896f0STim Small  */
54492896f0STim Small 
556fde8d29SAidan Thornton #define CH341_REQ_READ_VERSION 0x5F
56492896f0STim Small #define CH341_REQ_WRITE_REG    0x9A
57492896f0STim Small #define CH341_REQ_READ_REG     0x95
586fde8d29SAidan Thornton #define CH341_REQ_SERIAL_INIT  0xA1
596fde8d29SAidan Thornton #define CH341_REQ_MODEM_CTRL   0xA4
60492896f0STim Small 
616fde8d29SAidan Thornton #define CH341_REG_BREAK        0x05
622c509d1cSMichael Hanselmann #define CH341_REG_PRESCALER    0x12
632c509d1cSMichael Hanselmann #define CH341_REG_DIVISOR      0x13
646fde8d29SAidan Thornton #define CH341_REG_LCR          0x18
652c509d1cSMichael Hanselmann #define CH341_REG_LCR2         0x25
662c509d1cSMichael Hanselmann 
676fde8d29SAidan Thornton #define CH341_NBREAK_BITS      0x01
686fde8d29SAidan Thornton 
696fde8d29SAidan Thornton #define CH341_LCR_ENABLE_RX    0x80
706fde8d29SAidan Thornton #define CH341_LCR_ENABLE_TX    0x40
716fde8d29SAidan Thornton #define CH341_LCR_MARK_SPACE   0x20
726fde8d29SAidan Thornton #define CH341_LCR_PAR_EVEN     0x10
736fde8d29SAidan Thornton #define CH341_LCR_ENABLE_PAR   0x08
746fde8d29SAidan Thornton #define CH341_LCR_STOP_BITS_2  0x04
756fde8d29SAidan Thornton #define CH341_LCR_CS8          0x03
766fde8d29SAidan Thornton #define CH341_LCR_CS7          0x02
776fde8d29SAidan Thornton #define CH341_LCR_CS6          0x01
786fde8d29SAidan Thornton #define CH341_LCR_CS5          0x00
79492896f0STim Small 
80c432df15SJohan Hovold #define CH341_QUIRK_LIMITED_PRESCALER	BIT(0)
810580baa4SMichael Hanselmann #define CH341_QUIRK_SIMULATE_BREAK	BIT(1)
82c432df15SJohan Hovold 
837d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
84d0781383Swangyanqing 	{ USB_DEVICE(0x1a86, 0x5523) },
85bf193bfcSJohan Hovold 	{ USB_DEVICE(0x1a86, 0x7522) },
86bf193bfcSJohan Hovold 	{ USB_DEVICE(0x1a86, 0x7523) },
87fa77ce20SStephan Brunner 	{ USB_DEVICE(0x2184, 0x0057) },
88bf193bfcSJohan Hovold 	{ USB_DEVICE(0x4348, 0x5523) },
895563b3b6SNiv Sardi 	{ USB_DEVICE(0x9986, 0x7523) },
906ce76104SFrank A Kingswood 	{ },
916ce76104SFrank A Kingswood };
926ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table);
936ce76104SFrank A Kingswood 
946ce76104SFrank A Kingswood struct ch341_private {
95664d5df9SWerner Cornelius 	spinlock_t lock; /* access lock */
96664d5df9SWerner Cornelius 	unsigned baud_rate; /* set baud rate */
97beea33d4SJohan Hovold 	u8 mcr;
98e8024460SJohan Hovold 	u8 msr;
993cca8624SJohan Hovold 	u8 lcr;
1008e83622aSJohan Hovold 
101c404bf4aSMichael Hanselmann 	unsigned long quirks;
1028e83622aSJohan Hovold 	u8 version;
1038e83622aSJohan Hovold 
1040580baa4SMichael Hanselmann 	unsigned long break_end;
1056ce76104SFrank A Kingswood };
1066ce76104SFrank A Kingswood 
107aa91def4SNicolas PLANEL static void ch341_set_termios(struct tty_struct *tty,
108aa91def4SNicolas PLANEL 			      struct usb_serial_port *port,
109f6d47fe5SIlpo Järvinen 			      const struct ktermios *old_termios);
110aa91def4SNicolas PLANEL 
1116ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request,
1126ce76104SFrank A Kingswood 			     u16 value, u16 index)
1136ce76104SFrank A Kingswood {
1146ce76104SFrank A Kingswood 	int r;
11579cbeeafSGreg Kroah-Hartman 
11691e0efcdSJohan Hovold 	dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
11791e0efcdSJohan Hovold 		request, value, index);
1186ce76104SFrank A Kingswood 
1196ce76104SFrank A Kingswood 	r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
1206ce76104SFrank A Kingswood 			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1216ce76104SFrank A Kingswood 			    value, index, NULL, 0, DEFAULT_TIMEOUT);
1222d5a9c72SJohan Hovold 	if (r < 0)
1232d5a9c72SJohan Hovold 		dev_err(&dev->dev, "failed to send control message: %d\n", r);
1246ce76104SFrank A Kingswood 
1256ce76104SFrank A Kingswood 	return r;
1266ce76104SFrank A Kingswood }
1276ce76104SFrank A Kingswood 
1286ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev,
1296ce76104SFrank A Kingswood 			    u8 request, u16 value, u16 index,
1306ce76104SFrank A Kingswood 			    char *buf, unsigned bufsize)
1316ce76104SFrank A Kingswood {
1326ce76104SFrank A Kingswood 	int r;
13379cbeeafSGreg Kroah-Hartman 
13491e0efcdSJohan Hovold 	dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
13591e0efcdSJohan Hovold 		request, value, index, bufsize);
1366ce76104SFrank A Kingswood 
13774f26645SHimadri Pandya 	r = usb_control_msg_recv(dev, 0, request,
1386ce76104SFrank A Kingswood 				 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
13974f26645SHimadri Pandya 				 value, index, buf, bufsize, DEFAULT_TIMEOUT,
14074f26645SHimadri Pandya 				 GFP_KERNEL);
14174f26645SHimadri Pandya 	if (r) {
1422d5a9c72SJohan Hovold 		dev_err(&dev->dev, "failed to receive control message: %d\n",
1432d5a9c72SJohan Hovold 			r);
1446ce76104SFrank A Kingswood 		return r;
1456ce76104SFrank A Kingswood 	}
1466ce76104SFrank A Kingswood 
1472d5a9c72SJohan Hovold 	return 0;
1482d5a9c72SJohan Hovold }
1492d5a9c72SJohan Hovold 
15035714565SJohan Hovold #define CH341_CLKRATE		48000000
15135714565SJohan Hovold #define CH341_CLK_DIV(ps, fact)	(1 << (12 - 3 * (ps) - (fact)))
15235714565SJohan Hovold #define CH341_MIN_RATE(ps)	(CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512))
15335714565SJohan Hovold 
15435714565SJohan Hovold static const speed_t ch341_min_rates[] = {
15535714565SJohan Hovold 	CH341_MIN_RATE(0),
15635714565SJohan Hovold 	CH341_MIN_RATE(1),
15735714565SJohan Hovold 	CH341_MIN_RATE(2),
15835714565SJohan Hovold 	CH341_MIN_RATE(3),
15935714565SJohan Hovold };
16035714565SJohan Hovold 
16186f6da29SJohan Hovold /* Supported range is 46 to 3000000 bps. */
16286f6da29SJohan Hovold #define CH341_MIN_BPS	DIV_ROUND_UP(CH341_CLKRATE, CH341_CLK_DIV(0, 0) * 256)
16386f6da29SJohan Hovold #define CH341_MAX_BPS	(CH341_CLKRATE / (CH341_CLK_DIV(3, 0) * 2))
16486f6da29SJohan Hovold 
16535714565SJohan Hovold /*
16635714565SJohan Hovold  * The device line speed is given by the following equation:
16735714565SJohan Hovold  *
16835714565SJohan Hovold  *	baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
16935714565SJohan Hovold  *
17035714565SJohan Hovold  *		0 <= ps <= 3,
17135714565SJohan Hovold  *		0 <= fact <= 1,
17235714565SJohan Hovold  *		2 <= div <= 256 if fact = 0, or
17335714565SJohan Hovold  *		9 <= div <= 256 if fact = 1
17435714565SJohan Hovold  */
1750580baa4SMichael Hanselmann static int ch341_get_divisor(struct ch341_private *priv, speed_t speed)
17635714565SJohan Hovold {
17735714565SJohan Hovold 	unsigned int fact, div, clk_div;
178c432df15SJohan Hovold 	bool force_fact0 = false;
17935714565SJohan Hovold 	int ps;
18035714565SJohan Hovold 
18135714565SJohan Hovold 	/*
18235714565SJohan Hovold 	 * Clamp to supported range, this makes the (ps < 0) and (div < 2)
18335714565SJohan Hovold 	 * sanity checks below redundant.
18435714565SJohan Hovold 	 */
18586f6da29SJohan Hovold 	speed = clamp_val(speed, CH341_MIN_BPS, CH341_MAX_BPS);
18635714565SJohan Hovold 
18735714565SJohan Hovold 	/*
18835714565SJohan Hovold 	 * Start with highest possible base clock (fact = 1) that will give a
18935714565SJohan Hovold 	 * divisor strictly less than 512.
19035714565SJohan Hovold 	 */
19135714565SJohan Hovold 	fact = 1;
19235714565SJohan Hovold 	for (ps = 3; ps >= 0; ps--) {
19335714565SJohan Hovold 		if (speed > ch341_min_rates[ps])
19435714565SJohan Hovold 			break;
19535714565SJohan Hovold 	}
19635714565SJohan Hovold 
19735714565SJohan Hovold 	if (ps < 0)
19835714565SJohan Hovold 		return -EINVAL;
19935714565SJohan Hovold 
20035714565SJohan Hovold 	/* Determine corresponding divisor, rounding down. */
20135714565SJohan Hovold 	clk_div = CH341_CLK_DIV(ps, fact);
20235714565SJohan Hovold 	div = CH341_CLKRATE / (clk_div * speed);
20335714565SJohan Hovold 
204c432df15SJohan Hovold 	/* Some devices require a lower base clock if ps < 3. */
205c432df15SJohan Hovold 	if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER))
206c432df15SJohan Hovold 		force_fact0 = true;
207c432df15SJohan Hovold 
20835714565SJohan Hovold 	/* Halve base clock (fact = 0) if required. */
209c432df15SJohan Hovold 	if (div < 9 || div > 255 || force_fact0) {
21035714565SJohan Hovold 		div /= 2;
21135714565SJohan Hovold 		clk_div *= 2;
21235714565SJohan Hovold 		fact = 0;
21335714565SJohan Hovold 	}
21435714565SJohan Hovold 
21535714565SJohan Hovold 	if (div < 2)
21635714565SJohan Hovold 		return -EINVAL;
21735714565SJohan Hovold 
21835714565SJohan Hovold 	/*
21935714565SJohan Hovold 	 * Pick next divisor if resulting rate is closer to the requested one,
22035714565SJohan Hovold 	 * scale up to avoid rounding errors on low rates.
22135714565SJohan Hovold 	 */
22235714565SJohan Hovold 	if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >=
22335714565SJohan Hovold 			16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
22435714565SJohan Hovold 		div++;
22535714565SJohan Hovold 
2267c3d0228SJohan Hovold 	/*
2277c3d0228SJohan Hovold 	 * Prefer lower base clock (fact = 0) if even divisor.
2287c3d0228SJohan Hovold 	 *
2297c3d0228SJohan Hovold 	 * Note that this makes the receiver more tolerant to errors.
2307c3d0228SJohan Hovold 	 */
2317c3d0228SJohan Hovold 	if (fact == 1 && div % 2 == 0) {
2327c3d0228SJohan Hovold 		div /= 2;
2337c3d0228SJohan Hovold 		fact = 0;
2347c3d0228SJohan Hovold 	}
2357c3d0228SJohan Hovold 
23635714565SJohan Hovold 	return (0x100 - div) << 8 | fact << 2 | ps;
23735714565SJohan Hovold }
23835714565SJohan Hovold 
23955fa15b5SJohan Hovold static int ch341_set_baudrate_lcr(struct usb_device *dev,
2400580baa4SMichael Hanselmann 				  struct ch341_private *priv,
2410580baa4SMichael Hanselmann 				  speed_t baud_rate, u8 lcr)
2426ce76104SFrank A Kingswood {
24335714565SJohan Hovold 	int val;
2446ce76104SFrank A Kingswood 	int r;
2456ce76104SFrank A Kingswood 
2460580baa4SMichael Hanselmann 	if (!baud_rate)
2476ce76104SFrank A Kingswood 		return -EINVAL;
248664d5df9SWerner Cornelius 
2490580baa4SMichael Hanselmann 	val = ch341_get_divisor(priv, baud_rate);
25035714565SJohan Hovold 	if (val < 0)
251664d5df9SWerner Cornelius 		return -EINVAL;
252664d5df9SWerner Cornelius 
25355fa15b5SJohan Hovold 	/*
25455fa15b5SJohan Hovold 	 * CH341A buffers data until a full endpoint-size packet (32 bytes)
25555fa15b5SJohan Hovold 	 * has been received unless bit 7 is set.
25641ca302aSJohan Hovold 	 *
25741ca302aSJohan Hovold 	 * At least one device with version 0x27 appears to have this bit
25841ca302aSJohan Hovold 	 * inverted.
25955fa15b5SJohan Hovold 	 */
26041ca302aSJohan Hovold 	if (priv->version > 0x27)
26135714565SJohan Hovold 		val |= BIT(7);
26255fa15b5SJohan Hovold 
2632c509d1cSMichael Hanselmann 	r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
2642c509d1cSMichael Hanselmann 			      CH341_REG_DIVISOR << 8 | CH341_REG_PRESCALER,
2652c509d1cSMichael Hanselmann 			      val);
26655fa15b5SJohan Hovold 	if (r)
26755fa15b5SJohan Hovold 		return r;
26855fa15b5SJohan Hovold 
2692c509d1cSMichael Hanselmann 	/*
2702c509d1cSMichael Hanselmann 	 * Chip versions before version 0x30 as read using
2712c509d1cSMichael Hanselmann 	 * CH341_REQ_READ_VERSION used separate registers for line control
2722c509d1cSMichael Hanselmann 	 * (stop bits, parity and word length). Version 0x30 and above use
2732c509d1cSMichael Hanselmann 	 * CH341_REG_LCR only and CH341_REG_LCR2 is always set to zero.
2742c509d1cSMichael Hanselmann 	 */
2758e83622aSJohan Hovold 	if (priv->version < 0x30)
2768e83622aSJohan Hovold 		return 0;
2778e83622aSJohan Hovold 
2782c509d1cSMichael Hanselmann 	r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
2792c509d1cSMichael Hanselmann 			      CH341_REG_LCR2 << 8 | CH341_REG_LCR, lcr);
28055fa15b5SJohan Hovold 	if (r)
28155fa15b5SJohan Hovold 		return r;
2826ce76104SFrank A Kingswood 
2836ce76104SFrank A Kingswood 	return r;
2846ce76104SFrank A Kingswood }
2856ce76104SFrank A Kingswood 
286664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control)
2876ce76104SFrank A Kingswood {
2886fde8d29SAidan Thornton 	return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
2896ce76104SFrank A Kingswood }
2906ce76104SFrank A Kingswood 
291664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
2926ce76104SFrank A Kingswood {
2932d5a9c72SJohan Hovold 	const unsigned int size = 2;
29474f26645SHimadri Pandya 	u8 buffer[2];
2956ce76104SFrank A Kingswood 	int r;
296664d5df9SWerner Cornelius 	unsigned long flags;
2976ce76104SFrank A Kingswood 
2986fde8d29SAidan Thornton 	r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
29974f26645SHimadri Pandya 	if (r)
30074f26645SHimadri Pandya 		return r;
3016ce76104SFrank A Kingswood 
302664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
303e8024460SJohan Hovold 	priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
304664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
3056ce76104SFrank A Kingswood 
30674f26645SHimadri Pandya 	return 0;
3076ce76104SFrank A Kingswood }
3086ce76104SFrank A Kingswood 
3096ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */
3106ce76104SFrank A Kingswood 
31193b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
3126ce76104SFrank A Kingswood {
3132d5a9c72SJohan Hovold 	const unsigned int size = 2;
31474f26645SHimadri Pandya 	u8 buffer[2];
3156ce76104SFrank A Kingswood 	int r;
3166ce76104SFrank A Kingswood 
3176ce76104SFrank A Kingswood 	/* expect two bytes 0x27 0x00 */
3186fde8d29SAidan Thornton 	r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
31974f26645SHimadri Pandya 	if (r)
32074f26645SHimadri Pandya 		return r;
3218e83622aSJohan Hovold 
3228e83622aSJohan Hovold 	priv->version = buffer[0];
3238e83622aSJohan Hovold 	dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version);
3246ce76104SFrank A Kingswood 
3256fde8d29SAidan Thornton 	r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
3266ce76104SFrank A Kingswood 	if (r < 0)
32774f26645SHimadri Pandya 		return r;
3286ce76104SFrank A Kingswood 
3290580baa4SMichael Hanselmann 	r = ch341_set_baudrate_lcr(dev, priv, priv->baud_rate, priv->lcr);
3306ce76104SFrank A Kingswood 	if (r < 0)
33174f26645SHimadri Pandya 		return r;
3326ce76104SFrank A Kingswood 
333beea33d4SJohan Hovold 	r = ch341_set_handshake(dev, priv->mcr);
33474f26645SHimadri Pandya 	if (r < 0)
3356ce76104SFrank A Kingswood 		return r;
33674f26645SHimadri Pandya 
33774f26645SHimadri Pandya 	return 0;
3386ce76104SFrank A Kingswood }
3396ce76104SFrank A Kingswood 
340c404bf4aSMichael Hanselmann static int ch341_detect_quirks(struct usb_serial_port *port)
341c404bf4aSMichael Hanselmann {
342c404bf4aSMichael Hanselmann 	struct ch341_private *priv = usb_get_serial_port_data(port);
343c404bf4aSMichael Hanselmann 	struct usb_device *udev = port->serial->dev;
344c404bf4aSMichael Hanselmann 	const unsigned int size = 2;
345c404bf4aSMichael Hanselmann 	unsigned long quirks = 0;
34674f26645SHimadri Pandya 	u8 buffer[2];
347c404bf4aSMichael Hanselmann 	int r;
348c404bf4aSMichael Hanselmann 
349c404bf4aSMichael Hanselmann 	/*
350c404bf4aSMichael Hanselmann 	 * A subset of CH34x devices does not support all features. The
351c404bf4aSMichael Hanselmann 	 * prescaler is limited and there is no support for sending a RS232
352c404bf4aSMichael Hanselmann 	 * break condition. A read failure when trying to set up the latter is
353c404bf4aSMichael Hanselmann 	 * used to detect these devices.
354c404bf4aSMichael Hanselmann 	 */
35574f26645SHimadri Pandya 	r = usb_control_msg_recv(udev, 0, CH341_REQ_READ_REG,
356c404bf4aSMichael Hanselmann 				 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
35774f26645SHimadri Pandya 				 CH341_REG_BREAK, 0, &buffer, size,
35874f26645SHimadri Pandya 				 DEFAULT_TIMEOUT, GFP_KERNEL);
359c404bf4aSMichael Hanselmann 	if (r == -EPIPE) {
3600580baa4SMichael Hanselmann 		dev_info(&port->dev, "break control not supported, using simulated break\n");
3610580baa4SMichael Hanselmann 		quirks = CH341_QUIRK_LIMITED_PRESCALER | CH341_QUIRK_SIMULATE_BREAK;
362c404bf4aSMichael Hanselmann 		r = 0;
36374f26645SHimadri Pandya 	} else if (r) {
364c404bf4aSMichael Hanselmann 		dev_err(&port->dev, "failed to read break control: %d\n", r);
365c404bf4aSMichael Hanselmann 	}
366c404bf4aSMichael Hanselmann 
367c404bf4aSMichael Hanselmann 	if (quirks) {
368c404bf4aSMichael Hanselmann 		dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks);
369c404bf4aSMichael Hanselmann 		priv->quirks |= quirks;
370c404bf4aSMichael Hanselmann 	}
371c404bf4aSMichael Hanselmann 
372c404bf4aSMichael Hanselmann 	return r;
373c404bf4aSMichael Hanselmann }
374c404bf4aSMichael Hanselmann 
375456c5be5SJohan Hovold static int ch341_port_probe(struct usb_serial_port *port)
3766ce76104SFrank A Kingswood {
3776ce76104SFrank A Kingswood 	struct ch341_private *priv;
3786ce76104SFrank A Kingswood 	int r;
3796ce76104SFrank A Kingswood 
3806ce76104SFrank A Kingswood 	priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
3816ce76104SFrank A Kingswood 	if (!priv)
3826ce76104SFrank A Kingswood 		return -ENOMEM;
3836ce76104SFrank A Kingswood 
384664d5df9SWerner Cornelius 	spin_lock_init(&priv->lock);
3856ce76104SFrank A Kingswood 	priv->baud_rate = DEFAULT_BAUD_RATE;
3867c61b0d5SJohan Hovold 	/*
3877c61b0d5SJohan Hovold 	 * Some CH340 devices appear unable to change the initial LCR
3887c61b0d5SJohan Hovold 	 * settings, so set a sane 8N1 default.
3897c61b0d5SJohan Hovold 	 */
3907c61b0d5SJohan Hovold 	priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
3916ce76104SFrank A Kingswood 
392456c5be5SJohan Hovold 	r = ch341_configure(port->serial->dev, priv);
3936ce76104SFrank A Kingswood 	if (r < 0)
3946ce76104SFrank A Kingswood 		goto error;
3956ce76104SFrank A Kingswood 
396456c5be5SJohan Hovold 	usb_set_serial_port_data(port, priv);
397c404bf4aSMichael Hanselmann 
398c404bf4aSMichael Hanselmann 	r = ch341_detect_quirks(port);
399c404bf4aSMichael Hanselmann 	if (r < 0)
400c404bf4aSMichael Hanselmann 		goto error;
401c404bf4aSMichael Hanselmann 
4026ce76104SFrank A Kingswood 	return 0;
4036ce76104SFrank A Kingswood 
4046ce76104SFrank A Kingswood error:	kfree(priv);
4056ce76104SFrank A Kingswood 	return r;
4066ce76104SFrank A Kingswood }
4076ce76104SFrank A Kingswood 
408c5d1448fSUwe Kleine-König static void ch341_port_remove(struct usb_serial_port *port)
409456c5be5SJohan Hovold {
410456c5be5SJohan Hovold 	struct ch341_private *priv;
411456c5be5SJohan Hovold 
412456c5be5SJohan Hovold 	priv = usb_get_serial_port_data(port);
413456c5be5SJohan Hovold 	kfree(priv);
414456c5be5SJohan Hovold }
415456c5be5SJohan Hovold 
416335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port)
417335f8514SAlan Cox {
418335f8514SAlan Cox 	struct ch341_private *priv = usb_get_serial_port_data(port);
419e8024460SJohan Hovold 	if (priv->msr & CH341_BIT_DCD)
420335f8514SAlan Cox 		return 1;
421335f8514SAlan Cox 	return 0;
422335f8514SAlan Cox }
423335f8514SAlan Cox 
424335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on)
425664d5df9SWerner Cornelius {
426664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
427664d5df9SWerner Cornelius 	unsigned long flags;
428664d5df9SWerner Cornelius 
429335f8514SAlan Cox 	/* drop DTR and RTS */
430335f8514SAlan Cox 	spin_lock_irqsave(&priv->lock, flags);
431335f8514SAlan Cox 	if (on)
432beea33d4SJohan Hovold 		priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
433335f8514SAlan Cox 	else
434beea33d4SJohan Hovold 		priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
435335f8514SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
436beea33d4SJohan Hovold 	ch341_set_handshake(port->serial->dev, priv->mcr);
437335f8514SAlan Cox }
438335f8514SAlan Cox 
439335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port)
440335f8514SAlan Cox {
441f26788daSJohan Hovold 	usb_serial_generic_close(port);
442664d5df9SWerner Cornelius 	usb_kill_urb(port->interrupt_in_urb);
443664d5df9SWerner Cornelius }
444664d5df9SWerner Cornelius 
445664d5df9SWerner Cornelius 
4466ce76104SFrank A Kingswood /* open this device, set default parameters */
447a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
4486ce76104SFrank A Kingswood {
449456c5be5SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
4506ce76104SFrank A Kingswood 	int r;
4516ce76104SFrank A Kingswood 
452aa91def4SNicolas PLANEL 	if (tty)
453aa91def4SNicolas PLANEL 		ch341_set_termios(tty, port, NULL);
4546ce76104SFrank A Kingswood 
455d9a38a87SJohan Hovold 	dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
456664d5df9SWerner Cornelius 	r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
457664d5df9SWerner Cornelius 	if (r) {
458d9a38a87SJohan Hovold 		dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
459d9a38a87SJohan Hovold 			__func__, r);
460f2950b78SJohan Hovold 		return r;
461664d5df9SWerner Cornelius 	}
462664d5df9SWerner Cornelius 
463a0467a96SJohan Hovold 	r = ch341_get_status(port->serial->dev, priv);
464a0467a96SJohan Hovold 	if (r < 0) {
465a0467a96SJohan Hovold 		dev_err(&port->dev, "failed to read modem status: %d\n", r);
466a0467a96SJohan Hovold 		goto err_kill_interrupt_urb;
467a0467a96SJohan Hovold 	}
468a0467a96SJohan Hovold 
469a509a7e4SAlan Cox 	r = usb_serial_generic_open(tty, port);
470f2950b78SJohan Hovold 	if (r)
471f2950b78SJohan Hovold 		goto err_kill_interrupt_urb;
4726ce76104SFrank A Kingswood 
473f2950b78SJohan Hovold 	return 0;
474f2950b78SJohan Hovold 
475f2950b78SJohan Hovold err_kill_interrupt_urb:
476f2950b78SJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
477f2950b78SJohan Hovold 
478f2950b78SJohan Hovold 	return r;
4796ce76104SFrank A Kingswood }
4806ce76104SFrank A Kingswood 
4816ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and
4826ce76104SFrank A Kingswood  * tty->termios contains the new setting to be used.
4836ce76104SFrank A Kingswood  */
48495da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty,
485f6d47fe5SIlpo Järvinen 			      struct usb_serial_port *port,
486f6d47fe5SIlpo Järvinen 			      const struct ktermios *old_termios)
4876ce76104SFrank A Kingswood {
4886ce76104SFrank A Kingswood 	struct ch341_private *priv = usb_get_serial_port_data(port);
4896ce76104SFrank A Kingswood 	unsigned baud_rate;
490664d5df9SWerner Cornelius 	unsigned long flags;
491448b6dc5SJohan Hovold 	u8 lcr;
4924e46c410SAidan Thornton 	int r;
4934e46c410SAidan Thornton 
4944e46c410SAidan Thornton 	/* redundant changes may cause the chip to lose bytes */
4954e46c410SAidan Thornton 	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
4964e46c410SAidan Thornton 		return;
4976ce76104SFrank A Kingswood 
4986ce76104SFrank A Kingswood 	baud_rate = tty_get_baud_rate(tty);
4996ce76104SFrank A Kingswood 
500448b6dc5SJohan Hovold 	lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
501664d5df9SWerner Cornelius 
502ba781bdfSAidan Thornton 	switch (C_CSIZE(tty)) {
503ba781bdfSAidan Thornton 	case CS5:
504448b6dc5SJohan Hovold 		lcr |= CH341_LCR_CS5;
505ba781bdfSAidan Thornton 		break;
506ba781bdfSAidan Thornton 	case CS6:
507448b6dc5SJohan Hovold 		lcr |= CH341_LCR_CS6;
508ba781bdfSAidan Thornton 		break;
509ba781bdfSAidan Thornton 	case CS7:
510448b6dc5SJohan Hovold 		lcr |= CH341_LCR_CS7;
511ba781bdfSAidan Thornton 		break;
512ba781bdfSAidan Thornton 	case CS8:
513448b6dc5SJohan Hovold 		lcr |= CH341_LCR_CS8;
514ba781bdfSAidan Thornton 		break;
515ba781bdfSAidan Thornton 	}
516ba781bdfSAidan Thornton 
517ba781bdfSAidan Thornton 	if (C_PARENB(tty)) {
518448b6dc5SJohan Hovold 		lcr |= CH341_LCR_ENABLE_PAR;
519ba781bdfSAidan Thornton 		if (C_PARODD(tty) == 0)
520448b6dc5SJohan Hovold 			lcr |= CH341_LCR_PAR_EVEN;
521ba781bdfSAidan Thornton 		if (C_CMSPAR(tty))
522448b6dc5SJohan Hovold 			lcr |= CH341_LCR_MARK_SPACE;
523ba781bdfSAidan Thornton 	}
524ba781bdfSAidan Thornton 
525ba781bdfSAidan Thornton 	if (C_CSTOPB(tty))
526448b6dc5SJohan Hovold 		lcr |= CH341_LCR_STOP_BITS_2;
5274e46c410SAidan Thornton 
528664d5df9SWerner Cornelius 	if (baud_rate) {
529a20047f3SJohan Hovold 		priv->baud_rate = baud_rate;
530a20047f3SJohan Hovold 
5310580baa4SMichael Hanselmann 		r = ch341_set_baudrate_lcr(port->serial->dev, priv,
5320580baa4SMichael Hanselmann 					   priv->baud_rate, lcr);
5334e46c410SAidan Thornton 		if (r < 0 && old_termios) {
5344e46c410SAidan Thornton 			priv->baud_rate = tty_termios_baud_rate(old_termios);
5354e46c410SAidan Thornton 			tty_termios_copy_hw(&tty->termios, old_termios);
5363cca8624SJohan Hovold 		} else if (r == 0) {
537448b6dc5SJohan Hovold 			priv->lcr = lcr;
5384e46c410SAidan Thornton 		}
5396ce76104SFrank A Kingswood 	}
5406ce76104SFrank A Kingswood 
541030ee7aeSJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
542030ee7aeSJohan Hovold 	if (C_BAUD(tty) == B0)
543beea33d4SJohan Hovold 		priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
544030ee7aeSJohan Hovold 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
545beea33d4SJohan Hovold 		priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
546030ee7aeSJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
5476ce76104SFrank A Kingswood 
548beea33d4SJohan Hovold 	ch341_set_handshake(port->serial->dev, priv->mcr);
549664d5df9SWerner Cornelius }
55073f59308SAlan Cox 
5510580baa4SMichael Hanselmann /*
5520580baa4SMichael Hanselmann  * A subset of all CH34x devices don't support a real break condition and
5530580baa4SMichael Hanselmann  * reading CH341_REG_BREAK fails (see also ch341_detect_quirks). This function
5540580baa4SMichael Hanselmann  * simulates a break condition by lowering the baud rate to the minimum
5550580baa4SMichael Hanselmann  * supported by the hardware upon enabling the break condition and sending
5560580baa4SMichael Hanselmann  * a NUL byte.
5570580baa4SMichael Hanselmann  *
5580580baa4SMichael Hanselmann  * Incoming data is corrupted while the break condition is being simulated.
5590580baa4SMichael Hanselmann  *
5600580baa4SMichael Hanselmann  * Normally the duration of the break condition can be controlled individually
5610580baa4SMichael Hanselmann  * by userspace using TIOCSBRK and TIOCCBRK or by passing an argument to
5620580baa4SMichael Hanselmann  * TCSBRKP. Due to how the simulation is implemented the duration can't be
5630580baa4SMichael Hanselmann  * controlled. The duration is always about (1s / 46bd * 9bit) = 196ms.
5640580baa4SMichael Hanselmann  */
5656ff58ae1SJohan Hovold static int ch341_simulate_break(struct tty_struct *tty, int break_state)
5660580baa4SMichael Hanselmann {
5670580baa4SMichael Hanselmann 	struct usb_serial_port *port = tty->driver_data;
5680580baa4SMichael Hanselmann 	struct ch341_private *priv = usb_get_serial_port_data(port);
5690580baa4SMichael Hanselmann 	unsigned long now, delay;
5706ff58ae1SJohan Hovold 	int r, r2;
5710580baa4SMichael Hanselmann 
5720580baa4SMichael Hanselmann 	if (break_state != 0) {
5730580baa4SMichael Hanselmann 		dev_dbg(&port->dev, "enter break state requested\n");
5740580baa4SMichael Hanselmann 
5750580baa4SMichael Hanselmann 		r = ch341_set_baudrate_lcr(port->serial->dev, priv,
5760580baa4SMichael Hanselmann 				CH341_MIN_BPS,
5770580baa4SMichael Hanselmann 				CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8);
5780580baa4SMichael Hanselmann 		if (r < 0) {
5790580baa4SMichael Hanselmann 			dev_err(&port->dev,
5800580baa4SMichael Hanselmann 				"failed to change baud rate to %u: %d\n",
5810580baa4SMichael Hanselmann 				CH341_MIN_BPS, r);
5820580baa4SMichael Hanselmann 			goto restore;
5830580baa4SMichael Hanselmann 		}
5840580baa4SMichael Hanselmann 
5850580baa4SMichael Hanselmann 		r = tty_put_char(tty, '\0');
5860580baa4SMichael Hanselmann 		if (r < 0) {
5870580baa4SMichael Hanselmann 			dev_err(&port->dev,
5880580baa4SMichael Hanselmann 				"failed to write NUL byte for simulated break condition: %d\n",
5890580baa4SMichael Hanselmann 				r);
5900580baa4SMichael Hanselmann 			goto restore;
5910580baa4SMichael Hanselmann 		}
5920580baa4SMichael Hanselmann 
5930580baa4SMichael Hanselmann 		/*
5946d0bdc42SMichael Hanselmann 		 * Compute expected transmission duration including safety
5956d0bdc42SMichael Hanselmann 		 * margin. The original baud rate is only restored after the
5966d0bdc42SMichael Hanselmann 		 * computed point in time.
5976d0bdc42SMichael Hanselmann 		 *
5986d0bdc42SMichael Hanselmann 		 * 11 bits = 1 start, 8 data, 1 stop, 1 margin
5990580baa4SMichael Hanselmann 		 */
6006d0bdc42SMichael Hanselmann 		priv->break_end = jiffies + (11 * HZ / CH341_MIN_BPS);
6010580baa4SMichael Hanselmann 
6026ff58ae1SJohan Hovold 		return 0;
6030580baa4SMichael Hanselmann 	}
6040580baa4SMichael Hanselmann 
6050580baa4SMichael Hanselmann 	dev_dbg(&port->dev, "leave break state requested\n");
6060580baa4SMichael Hanselmann 
6070580baa4SMichael Hanselmann 	now = jiffies;
6080580baa4SMichael Hanselmann 
6090580baa4SMichael Hanselmann 	if (time_before(now, priv->break_end)) {
6100580baa4SMichael Hanselmann 		/* Wait until NUL byte is written */
6110580baa4SMichael Hanselmann 		delay = priv->break_end - now;
6120580baa4SMichael Hanselmann 		dev_dbg(&port->dev,
6130580baa4SMichael Hanselmann 			"wait %d ms while transmitting NUL byte at %u baud\n",
6140580baa4SMichael Hanselmann 			jiffies_to_msecs(delay), CH341_MIN_BPS);
6150580baa4SMichael Hanselmann 		schedule_timeout_interruptible(delay);
6160580baa4SMichael Hanselmann 	}
6170580baa4SMichael Hanselmann 
6186ff58ae1SJohan Hovold 	r = 0;
6190580baa4SMichael Hanselmann restore:
6200580baa4SMichael Hanselmann 	/* Restore original baud rate */
6216ff58ae1SJohan Hovold 	r2 = ch341_set_baudrate_lcr(port->serial->dev, priv, priv->baud_rate,
6220580baa4SMichael Hanselmann 			priv->lcr);
6236ff58ae1SJohan Hovold 	if (r2 < 0) {
6240580baa4SMichael Hanselmann 		dev_err(&port->dev,
6250580baa4SMichael Hanselmann 			"restoring original baud rate of %u failed: %d\n",
6266ff58ae1SJohan Hovold 			priv->baud_rate, r2);
6276ff58ae1SJohan Hovold 		return r2;
6280580baa4SMichael Hanselmann 	}
6290580baa4SMichael Hanselmann 
6306ff58ae1SJohan Hovold 	return r;
6316ff58ae1SJohan Hovold }
6326ff58ae1SJohan Hovold 
6336ff58ae1SJohan Hovold static int ch341_break_ctl(struct tty_struct *tty, int break_state)
634492896f0STim Small {
635492896f0STim Small 	const uint16_t ch341_break_reg =
6366fde8d29SAidan Thornton 			((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
637492896f0STim Small 	struct usb_serial_port *port = tty->driver_data;
6380580baa4SMichael Hanselmann 	struct ch341_private *priv = usb_get_serial_port_data(port);
639492896f0STim Small 	int r;
640492896f0STim Small 	uint16_t reg_contents;
64174f26645SHimadri Pandya 	uint8_t break_reg[2];
642492896f0STim Small 
6436ff58ae1SJohan Hovold 	if (priv->quirks & CH341_QUIRK_SIMULATE_BREAK)
6446ff58ae1SJohan Hovold 		return ch341_simulate_break(tty, break_state);
6450580baa4SMichael Hanselmann 
646492896f0STim Small 	r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
647f2b5cc83SJohan Hovold 			ch341_break_reg, 0, break_reg, 2);
64874f26645SHimadri Pandya 	if (r) {
6496a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control read error (%d)\n",
6506a9b15feSJohan Hovold 				__func__, r);
6516ff58ae1SJohan Hovold 		if (r > 0)
6526ff58ae1SJohan Hovold 			r = -EIO;
6536ff58ae1SJohan Hovold 		return r;
654492896f0STim Small 	}
65579cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
656492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
657492896f0STim Small 	if (break_state != 0) {
65879cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
6596fde8d29SAidan Thornton 		break_reg[0] &= ~CH341_NBREAK_BITS;
6606fde8d29SAidan Thornton 		break_reg[1] &= ~CH341_LCR_ENABLE_TX;
661492896f0STim Small 	} else {
66279cbeeafSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
6636fde8d29SAidan Thornton 		break_reg[0] |= CH341_NBREAK_BITS;
6646fde8d29SAidan Thornton 		break_reg[1] |= CH341_LCR_ENABLE_TX;
665492896f0STim Small 	}
66679cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
667492896f0STim Small 		__func__, break_reg[0], break_reg[1]);
6685be796f0SJohan Hovold 	reg_contents = get_unaligned_le16(break_reg);
669492896f0STim Small 	r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
670492896f0STim Small 			ch341_break_reg, reg_contents);
6716ff58ae1SJohan Hovold 	if (r < 0) {
6726a9b15feSJohan Hovold 		dev_err(&port->dev, "%s - USB control write error (%d)\n",
6736a9b15feSJohan Hovold 				__func__, r);
6746ff58ae1SJohan Hovold 		return r;
6756ff58ae1SJohan Hovold 	}
6766ff58ae1SJohan Hovold 
6776ff58ae1SJohan Hovold 	return 0;
678492896f0STim Small }
679492896f0STim Small 
68020b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty,
681664d5df9SWerner Cornelius 			  unsigned int set, unsigned int clear)
682664d5df9SWerner Cornelius {
683664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
684664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
685664d5df9SWerner Cornelius 	unsigned long flags;
686664d5df9SWerner Cornelius 	u8 control;
687664d5df9SWerner Cornelius 
688664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
689664d5df9SWerner Cornelius 	if (set & TIOCM_RTS)
690beea33d4SJohan Hovold 		priv->mcr |= CH341_BIT_RTS;
691664d5df9SWerner Cornelius 	if (set & TIOCM_DTR)
692beea33d4SJohan Hovold 		priv->mcr |= CH341_BIT_DTR;
693664d5df9SWerner Cornelius 	if (clear & TIOCM_RTS)
694beea33d4SJohan Hovold 		priv->mcr &= ~CH341_BIT_RTS;
695664d5df9SWerner Cornelius 	if (clear & TIOCM_DTR)
696beea33d4SJohan Hovold 		priv->mcr &= ~CH341_BIT_DTR;
697beea33d4SJohan Hovold 	control = priv->mcr;
698664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
699664d5df9SWerner Cornelius 
700664d5df9SWerner Cornelius 	return ch341_set_handshake(port->serial->dev, control);
701664d5df9SWerner Cornelius }
702664d5df9SWerner Cornelius 
703e8024460SJohan Hovold static void ch341_update_status(struct usb_serial_port *port,
704ac035628SJohan Hovold 					unsigned char *data, size_t len)
705ac035628SJohan Hovold {
706ac035628SJohan Hovold 	struct ch341_private *priv = usb_get_serial_port_data(port);
707b770081fSJohan Hovold 	struct tty_struct *tty;
708ac035628SJohan Hovold 	unsigned long flags;
709b770081fSJohan Hovold 	u8 status;
710b770081fSJohan Hovold 	u8 delta;
711ac035628SJohan Hovold 
712ac035628SJohan Hovold 	if (len < 4)
713ac035628SJohan Hovold 		return;
714ac035628SJohan Hovold 
715b770081fSJohan Hovold 	status = ~data[2] & CH341_BITS_MODEM_STAT;
716b770081fSJohan Hovold 
717ac035628SJohan Hovold 	spin_lock_irqsave(&priv->lock, flags);
718e8024460SJohan Hovold 	delta = status ^ priv->msr;
719e8024460SJohan Hovold 	priv->msr = status;
720ac035628SJohan Hovold 	spin_unlock_irqrestore(&priv->lock, flags);
721ac035628SJohan Hovold 
722fd74b0b1SJohan Hovold 	if (data[1] & CH341_MULT_STAT)
723fd74b0b1SJohan Hovold 		dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
724fd74b0b1SJohan Hovold 
725d984fe91SJohan Hovold 	if (!delta)
726d984fe91SJohan Hovold 		return;
727d984fe91SJohan Hovold 
7285e409a26SJohan Hovold 	if (delta & CH341_BIT_CTS)
7295e409a26SJohan Hovold 		port->icount.cts++;
7305e409a26SJohan Hovold 	if (delta & CH341_BIT_DSR)
7315e409a26SJohan Hovold 		port->icount.dsr++;
7325e409a26SJohan Hovold 	if (delta & CH341_BIT_RI)
7335e409a26SJohan Hovold 		port->icount.rng++;
734b770081fSJohan Hovold 	if (delta & CH341_BIT_DCD) {
7355e409a26SJohan Hovold 		port->icount.dcd++;
736b770081fSJohan Hovold 		tty = tty_port_tty_get(&port->port);
737b770081fSJohan Hovold 		if (tty) {
738ac035628SJohan Hovold 			usb_serial_handle_dcd_change(port, tty,
739b770081fSJohan Hovold 						status & CH341_BIT_DCD);
740ac035628SJohan Hovold 			tty_kref_put(tty);
741ac035628SJohan Hovold 		}
742b770081fSJohan Hovold 	}
743ac035628SJohan Hovold 
744ac035628SJohan Hovold 	wake_up_interruptible(&port->port.delta_msr_wait);
745ac035628SJohan Hovold }
746ac035628SJohan Hovold 
747664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb)
748664d5df9SWerner Cornelius {
749271ec2d2SJohan Hovold 	struct usb_serial_port *port = urb->context;
750664d5df9SWerner Cornelius 	unsigned char *data = urb->transfer_buffer;
751271ec2d2SJohan Hovold 	unsigned int len = urb->actual_length;
752664d5df9SWerner Cornelius 	int status;
753664d5df9SWerner Cornelius 
754664d5df9SWerner Cornelius 	switch (urb->status) {
755664d5df9SWerner Cornelius 	case 0:
756664d5df9SWerner Cornelius 		/* success */
757664d5df9SWerner Cornelius 		break;
758664d5df9SWerner Cornelius 	case -ECONNRESET:
759664d5df9SWerner Cornelius 	case -ENOENT:
760664d5df9SWerner Cornelius 	case -ESHUTDOWN:
761664d5df9SWerner Cornelius 		/* this urb is terminated, clean up */
762271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
76379cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
764664d5df9SWerner Cornelius 		return;
765664d5df9SWerner Cornelius 	default:
766271ec2d2SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
76779cbeeafSGreg Kroah-Hartman 			__func__, urb->status);
768664d5df9SWerner Cornelius 		goto exit;
769664d5df9SWerner Cornelius 	}
770664d5df9SWerner Cornelius 
771271ec2d2SJohan Hovold 	usb_serial_debug_data(&port->dev, __func__, len, data);
772e8024460SJohan Hovold 	ch341_update_status(port, data, len);
773664d5df9SWerner Cornelius exit:
774664d5df9SWerner Cornelius 	status = usb_submit_urb(urb, GFP_ATOMIC);
775271ec2d2SJohan Hovold 	if (status) {
776271ec2d2SJohan Hovold 		dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
777664d5df9SWerner Cornelius 			__func__, status);
778664d5df9SWerner Cornelius 	}
779271ec2d2SJohan Hovold }
780664d5df9SWerner Cornelius 
78160b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty)
782664d5df9SWerner Cornelius {
783664d5df9SWerner Cornelius 	struct usb_serial_port *port = tty->driver_data;
784664d5df9SWerner Cornelius 	struct ch341_private *priv = usb_get_serial_port_data(port);
785664d5df9SWerner Cornelius 	unsigned long flags;
786664d5df9SWerner Cornelius 	u8 mcr;
787664d5df9SWerner Cornelius 	u8 status;
788664d5df9SWerner Cornelius 	unsigned int result;
789664d5df9SWerner Cornelius 
790664d5df9SWerner Cornelius 	spin_lock_irqsave(&priv->lock, flags);
791beea33d4SJohan Hovold 	mcr = priv->mcr;
792e8024460SJohan Hovold 	status = priv->msr;
793664d5df9SWerner Cornelius 	spin_unlock_irqrestore(&priv->lock, flags);
794664d5df9SWerner Cornelius 
795664d5df9SWerner Cornelius 	result = ((mcr & CH341_BIT_DTR)		? TIOCM_DTR : 0)
796664d5df9SWerner Cornelius 		  | ((mcr & CH341_BIT_RTS)	? TIOCM_RTS : 0)
797664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_CTS)	? TIOCM_CTS : 0)
798664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DSR)	? TIOCM_DSR : 0)
799664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_RI)	? TIOCM_RI  : 0)
800664d5df9SWerner Cornelius 		  | ((status & CH341_BIT_DCD)	? TIOCM_CD  : 0);
801664d5df9SWerner Cornelius 
80279cbeeafSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
803664d5df9SWerner Cornelius 
804664d5df9SWerner Cornelius 	return result;
8056ce76104SFrank A Kingswood }
8066ce76104SFrank A Kingswood 
807622b80cfSGreg Kroah-Hartman static int ch341_reset_resume(struct usb_serial *serial)
8081ded7ea4SMing Lei {
809ce5e2928SJohan Hovold 	struct usb_serial_port *port = serial->port[0];
8104d5ef53fSJohan Hovold 	struct ch341_private *priv;
811ce5e2928SJohan Hovold 	int ret;
8121ded7ea4SMing Lei 
8134d5ef53fSJohan Hovold 	priv = usb_get_serial_port_data(port);
8144d5ef53fSJohan Hovold 	if (!priv)
8154d5ef53fSJohan Hovold 		return 0;
8164d5ef53fSJohan Hovold 
8171ded7ea4SMing Lei 	/* reconfigure ch341 serial port after bus-reset */
8182bfd1c96SGreg Kroah-Hartman 	ch341_configure(serial->dev, priv);
8191ded7ea4SMing Lei 
820ce5e2928SJohan Hovold 	if (tty_port_initialized(&port->port)) {
821ce5e2928SJohan Hovold 		ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
822ce5e2928SJohan Hovold 		if (ret) {
823ce5e2928SJohan Hovold 			dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
824ce5e2928SJohan Hovold 				ret);
825ce5e2928SJohan Hovold 			return ret;
826ce5e2928SJohan Hovold 		}
827a0467a96SJohan Hovold 
828a0467a96SJohan Hovold 		ret = ch341_get_status(port->serial->dev, priv);
829a0467a96SJohan Hovold 		if (ret < 0) {
830a0467a96SJohan Hovold 			dev_err(&port->dev, "failed to read modem status: %d\n",
831a0467a96SJohan Hovold 				ret);
832a0467a96SJohan Hovold 		}
833ce5e2928SJohan Hovold 	}
834ce5e2928SJohan Hovold 
835ce5e2928SJohan Hovold 	return usb_serial_generic_resume(serial);
8361ded7ea4SMing Lei }
8371ded7ea4SMing Lei 
8386ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = {
8396ce76104SFrank A Kingswood 	.driver = {
8406ce76104SFrank A Kingswood 		.owner	= THIS_MODULE,
8416ce76104SFrank A Kingswood 		.name	= "ch341-uart",
8426ce76104SFrank A Kingswood 	},
8436ce76104SFrank A Kingswood 	.id_table          = id_table,
8446ce76104SFrank A Kingswood 	.num_ports         = 1,
8456ce76104SFrank A Kingswood 	.open              = ch341_open,
846335f8514SAlan Cox 	.dtr_rts	   = ch341_dtr_rts,
847335f8514SAlan Cox 	.carrier_raised	   = ch341_carrier_raised,
848664d5df9SWerner Cornelius 	.close             = ch341_close,
8496ce76104SFrank A Kingswood 	.set_termios       = ch341_set_termios,
850492896f0STim Small 	.break_ctl         = ch341_break_ctl,
851664d5df9SWerner Cornelius 	.tiocmget          = ch341_tiocmget,
852664d5df9SWerner Cornelius 	.tiocmset          = ch341_tiocmset,
8535e409a26SJohan Hovold 	.tiocmiwait        = usb_serial_generic_tiocmiwait,
854664d5df9SWerner Cornelius 	.read_int_callback = ch341_read_int_callback,
855456c5be5SJohan Hovold 	.port_probe        = ch341_port_probe,
856456c5be5SJohan Hovold 	.port_remove       = ch341_port_remove,
8571c1eaba8SGreg Kroah-Hartman 	.reset_resume      = ch341_reset_resume,
8586ce76104SFrank A Kingswood };
8596ce76104SFrank A Kingswood 
86008a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
86108a4f6bcSAlan Stern 	&ch341_device, NULL
86208a4f6bcSAlan Stern };
86308a4f6bcSAlan Stern 
86468e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
8656ce76104SFrank A Kingswood 
866*9f4dc051SJeff Johnson MODULE_DESCRIPTION("Winchiphead CH341 USB Serial driver");
867627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
868