16ce76104SFrank A Kingswood /* 26ce76104SFrank A Kingswood * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk> 3664d5df9SWerner Cornelius * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de> 4664d5df9SWerner Cornelius * Copyright 2009, Boris Hajduk <boris@hajduk.org> 56ce76104SFrank A Kingswood * 66ce76104SFrank A Kingswood * ch341.c implements a serial port driver for the Winchiphead CH341. 76ce76104SFrank A Kingswood * 86ce76104SFrank A Kingswood * The CH341 device can be used to implement an RS232 asynchronous 96ce76104SFrank A Kingswood * serial port, an IEEE-1284 parallel printer port or a memory-like 106ce76104SFrank A Kingswood * interface. In all cases the CH341 supports an I2C interface as well. 116ce76104SFrank A Kingswood * This driver only supports the asynchronous serial interface. 126ce76104SFrank A Kingswood * 136ce76104SFrank A Kingswood * This program is free software; you can redistribute it and/or 146ce76104SFrank A Kingswood * modify it under the terms of the GNU General Public License version 156ce76104SFrank A Kingswood * 2 as published by the Free Software Foundation. 166ce76104SFrank A Kingswood */ 176ce76104SFrank A Kingswood 186ce76104SFrank A Kingswood #include <linux/kernel.h> 196ce76104SFrank A Kingswood #include <linux/tty.h> 206ce76104SFrank A Kingswood #include <linux/module.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 226ce76104SFrank A Kingswood #include <linux/usb.h> 236ce76104SFrank A Kingswood #include <linux/usb/serial.h> 246ce76104SFrank A Kingswood #include <linux/serial.h> 255be796f0SJohan Hovold #include <asm/unaligned.h> 266ce76104SFrank A Kingswood 27664d5df9SWerner Cornelius #define DEFAULT_BAUD_RATE 9600 286ce76104SFrank A Kingswood #define DEFAULT_TIMEOUT 1000 296ce76104SFrank A Kingswood 30664d5df9SWerner Cornelius /* flags for IO-Bits */ 31664d5df9SWerner Cornelius #define CH341_BIT_RTS (1 << 6) 32664d5df9SWerner Cornelius #define CH341_BIT_DTR (1 << 5) 33664d5df9SWerner Cornelius 34664d5df9SWerner Cornelius /******************************/ 35664d5df9SWerner Cornelius /* interrupt pipe definitions */ 36664d5df9SWerner Cornelius /******************************/ 37664d5df9SWerner Cornelius /* always 4 interrupt bytes */ 38664d5df9SWerner Cornelius /* first irq byte normally 0x08 */ 39664d5df9SWerner Cornelius /* second irq byte base 0x7d + below */ 40664d5df9SWerner Cornelius /* third irq byte base 0x94 + below */ 41664d5df9SWerner Cornelius /* fourth irq byte normally 0xee */ 42664d5df9SWerner Cornelius 43664d5df9SWerner Cornelius /* second interrupt byte */ 44664d5df9SWerner Cornelius #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */ 45664d5df9SWerner Cornelius 46664d5df9SWerner Cornelius /* status returned in third interrupt answer byte, inverted in data 47664d5df9SWerner Cornelius from irq */ 48664d5df9SWerner Cornelius #define CH341_BIT_CTS 0x01 49664d5df9SWerner Cornelius #define CH341_BIT_DSR 0x02 50664d5df9SWerner Cornelius #define CH341_BIT_RI 0x04 51664d5df9SWerner Cornelius #define CH341_BIT_DCD 0x08 52664d5df9SWerner Cornelius #define CH341_BITS_MODEM_STAT 0x0f /* all bits */ 53664d5df9SWerner Cornelius 54664d5df9SWerner Cornelius /*******************************/ 55664d5df9SWerner Cornelius /* baudrate calculation factor */ 56664d5df9SWerner Cornelius /*******************************/ 57664d5df9SWerner Cornelius #define CH341_BAUDBASE_FACTOR 1532620800 58664d5df9SWerner Cornelius #define CH341_BAUDBASE_DIVMAX 3 59664d5df9SWerner Cornelius 60492896f0STim Small /* Break support - the information used to implement this was gleaned from 61492896f0STim Small * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato. 62492896f0STim Small */ 63492896f0STim Small 64*6fde8d29SAidan Thornton #define CH341_REQ_READ_VERSION 0x5F 65492896f0STim Small #define CH341_REQ_WRITE_REG 0x9A 66492896f0STim Small #define CH341_REQ_READ_REG 0x95 67*6fde8d29SAidan Thornton #define CH341_REQ_SERIAL_INIT 0xA1 68*6fde8d29SAidan Thornton #define CH341_REQ_MODEM_CTRL 0xA4 69492896f0STim Small 70*6fde8d29SAidan Thornton #define CH341_REG_BREAK 0x05 71*6fde8d29SAidan Thornton #define CH341_REG_LCR 0x18 72*6fde8d29SAidan Thornton #define CH341_NBREAK_BITS 0x01 73*6fde8d29SAidan Thornton 74*6fde8d29SAidan Thornton #define CH341_LCR_ENABLE_RX 0x80 75*6fde8d29SAidan Thornton #define CH341_LCR_ENABLE_TX 0x40 76*6fde8d29SAidan Thornton #define CH341_LCR_MARK_SPACE 0x20 77*6fde8d29SAidan Thornton #define CH341_LCR_PAR_EVEN 0x10 78*6fde8d29SAidan Thornton #define CH341_LCR_ENABLE_PAR 0x08 79*6fde8d29SAidan Thornton #define CH341_LCR_STOP_BITS_2 0x04 80*6fde8d29SAidan Thornton #define CH341_LCR_CS8 0x03 81*6fde8d29SAidan Thornton #define CH341_LCR_CS7 0x02 82*6fde8d29SAidan Thornton #define CH341_LCR_CS6 0x01 83*6fde8d29SAidan Thornton #define CH341_LCR_CS5 0x00 84492896f0STim Small 857d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = { 866ce76104SFrank A Kingswood { USB_DEVICE(0x4348, 0x5523) }, 8782078234SMichael F. Robbins { USB_DEVICE(0x1a86, 0x7523) }, 88d0781383Swangyanqing { USB_DEVICE(0x1a86, 0x5523) }, 896ce76104SFrank A Kingswood { }, 906ce76104SFrank A Kingswood }; 916ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table); 926ce76104SFrank A Kingswood 936ce76104SFrank A Kingswood struct ch341_private { 94664d5df9SWerner Cornelius spinlock_t lock; /* access lock */ 95664d5df9SWerner Cornelius unsigned baud_rate; /* set baud rate */ 96664d5df9SWerner Cornelius u8 line_control; /* set line control value RTS/DTR */ 97664d5df9SWerner Cornelius u8 line_status; /* active status of modem control inputs */ 986ce76104SFrank A Kingswood }; 996ce76104SFrank A Kingswood 100aa91def4SNicolas PLANEL static void ch341_set_termios(struct tty_struct *tty, 101aa91def4SNicolas PLANEL struct usb_serial_port *port, 102aa91def4SNicolas PLANEL struct ktermios *old_termios); 103aa91def4SNicolas PLANEL 1046ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request, 1056ce76104SFrank A Kingswood u16 value, u16 index) 1066ce76104SFrank A Kingswood { 1076ce76104SFrank A Kingswood int r; 10879cbeeafSGreg Kroah-Hartman 10979cbeeafSGreg Kroah-Hartman dev_dbg(&dev->dev, "ch341_control_out(%02x,%02x,%04x,%04x)\n", 11079cbeeafSGreg Kroah-Hartman USB_DIR_OUT|0x40, (int)request, (int)value, (int)index); 1116ce76104SFrank A Kingswood 1126ce76104SFrank A Kingswood r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, 1136ce76104SFrank A Kingswood USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 1146ce76104SFrank A Kingswood value, index, NULL, 0, DEFAULT_TIMEOUT); 1156ce76104SFrank A Kingswood 1166ce76104SFrank A Kingswood return r; 1176ce76104SFrank A Kingswood } 1186ce76104SFrank A Kingswood 1196ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev, 1206ce76104SFrank A Kingswood u8 request, u16 value, u16 index, 1216ce76104SFrank A Kingswood char *buf, unsigned bufsize) 1226ce76104SFrank A Kingswood { 1236ce76104SFrank A Kingswood int r; 12479cbeeafSGreg Kroah-Hartman 12579cbeeafSGreg Kroah-Hartman dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n", 12679cbeeafSGreg Kroah-Hartman USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf, 12779cbeeafSGreg Kroah-Hartman (int)bufsize); 1286ce76104SFrank A Kingswood 1296ce76104SFrank A Kingswood r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request, 1306ce76104SFrank A Kingswood USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 1316ce76104SFrank A Kingswood value, index, buf, bufsize, DEFAULT_TIMEOUT); 1326ce76104SFrank A Kingswood return r; 1336ce76104SFrank A Kingswood } 1346ce76104SFrank A Kingswood 13593b6497dSAdrian Bunk static int ch341_set_baudrate(struct usb_device *dev, 13693b6497dSAdrian Bunk struct ch341_private *priv) 1376ce76104SFrank A Kingswood { 1386ce76104SFrank A Kingswood short a, b; 1396ce76104SFrank A Kingswood int r; 140664d5df9SWerner Cornelius unsigned long factor; 141664d5df9SWerner Cornelius short divisor; 1426ce76104SFrank A Kingswood 143664d5df9SWerner Cornelius if (!priv->baud_rate) 1446ce76104SFrank A Kingswood return -EINVAL; 145664d5df9SWerner Cornelius factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate); 146664d5df9SWerner Cornelius divisor = CH341_BAUDBASE_DIVMAX; 147664d5df9SWerner Cornelius 148664d5df9SWerner Cornelius while ((factor > 0xfff0) && divisor) { 149664d5df9SWerner Cornelius factor >>= 3; 150664d5df9SWerner Cornelius divisor--; 1516ce76104SFrank A Kingswood } 1526ce76104SFrank A Kingswood 153664d5df9SWerner Cornelius if (factor > 0xfff0) 154664d5df9SWerner Cornelius return -EINVAL; 155664d5df9SWerner Cornelius 156664d5df9SWerner Cornelius factor = 0x10000 - factor; 157664d5df9SWerner Cornelius a = (factor & 0xff00) | divisor; 158664d5df9SWerner Cornelius b = factor & 0xff; 159664d5df9SWerner Cornelius 160*6fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a); 1616ce76104SFrank A Kingswood if (!r) 162*6fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x0f2c, b); 1636ce76104SFrank A Kingswood 1646ce76104SFrank A Kingswood return r; 1656ce76104SFrank A Kingswood } 1666ce76104SFrank A Kingswood 167664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control) 1686ce76104SFrank A Kingswood { 169*6fde8d29SAidan Thornton return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0); 1706ce76104SFrank A Kingswood } 1716ce76104SFrank A Kingswood 172664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv) 1736ce76104SFrank A Kingswood { 1746ce76104SFrank A Kingswood char *buffer; 1756ce76104SFrank A Kingswood int r; 1766ce76104SFrank A Kingswood const unsigned size = 8; 177664d5df9SWerner Cornelius unsigned long flags; 1786ce76104SFrank A Kingswood 1796ce76104SFrank A Kingswood buffer = kmalloc(size, GFP_KERNEL); 1806ce76104SFrank A Kingswood if (!buffer) 1816ce76104SFrank A Kingswood return -ENOMEM; 1826ce76104SFrank A Kingswood 183*6fde8d29SAidan Thornton r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size); 1846ce76104SFrank A Kingswood if (r < 0) 1856ce76104SFrank A Kingswood goto out; 1866ce76104SFrank A Kingswood 187664d5df9SWerner Cornelius /* setup the private status if available */ 188664d5df9SWerner Cornelius if (r == 2) { 1896ce76104SFrank A Kingswood r = 0; 190664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 191664d5df9SWerner Cornelius priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT; 192664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 193664d5df9SWerner Cornelius } else 194664d5df9SWerner Cornelius r = -EPROTO; 1956ce76104SFrank A Kingswood 1966ce76104SFrank A Kingswood out: kfree(buffer); 1976ce76104SFrank A Kingswood return r; 1986ce76104SFrank A Kingswood } 1996ce76104SFrank A Kingswood 2006ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */ 2016ce76104SFrank A Kingswood 20293b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv) 2036ce76104SFrank A Kingswood { 2046ce76104SFrank A Kingswood char *buffer; 2056ce76104SFrank A Kingswood int r; 2066ce76104SFrank A Kingswood const unsigned size = 8; 2076ce76104SFrank A Kingswood 2086ce76104SFrank A Kingswood buffer = kmalloc(size, GFP_KERNEL); 2096ce76104SFrank A Kingswood if (!buffer) 2106ce76104SFrank A Kingswood return -ENOMEM; 2116ce76104SFrank A Kingswood 2126ce76104SFrank A Kingswood /* expect two bytes 0x27 0x00 */ 213*6fde8d29SAidan Thornton r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size); 2146ce76104SFrank A Kingswood if (r < 0) 2156ce76104SFrank A Kingswood goto out; 2166ce76104SFrank A Kingswood 217*6fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0); 2186ce76104SFrank A Kingswood if (r < 0) 2196ce76104SFrank A Kingswood goto out; 2206ce76104SFrank A Kingswood 2216ce76104SFrank A Kingswood r = ch341_set_baudrate(dev, priv); 2226ce76104SFrank A Kingswood if (r < 0) 2236ce76104SFrank A Kingswood goto out; 2246ce76104SFrank A Kingswood 2256ce76104SFrank A Kingswood /* expect two bytes 0x56 0x00 */ 226*6fde8d29SAidan Thornton r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x2518, 0, buffer, size); 2276ce76104SFrank A Kingswood if (r < 0) 2286ce76104SFrank A Kingswood goto out; 2296ce76104SFrank A Kingswood 230*6fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, 0x0050); 2316ce76104SFrank A Kingswood if (r < 0) 2326ce76104SFrank A Kingswood goto out; 2336ce76104SFrank A Kingswood 2346ce76104SFrank A Kingswood /* expect 0xff 0xee */ 235664d5df9SWerner Cornelius r = ch341_get_status(dev, priv); 2366ce76104SFrank A Kingswood if (r < 0) 2376ce76104SFrank A Kingswood goto out; 2386ce76104SFrank A Kingswood 239*6fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0x501f, 0xd90a); 2406ce76104SFrank A Kingswood if (r < 0) 2416ce76104SFrank A Kingswood goto out; 2426ce76104SFrank A Kingswood 2436ce76104SFrank A Kingswood r = ch341_set_baudrate(dev, priv); 2446ce76104SFrank A Kingswood if (r < 0) 2456ce76104SFrank A Kingswood goto out; 2466ce76104SFrank A Kingswood 247664d5df9SWerner Cornelius r = ch341_set_handshake(dev, priv->line_control); 2486ce76104SFrank A Kingswood if (r < 0) 2496ce76104SFrank A Kingswood goto out; 2506ce76104SFrank A Kingswood 2516ce76104SFrank A Kingswood /* expect 0x9f 0xee */ 252664d5df9SWerner Cornelius r = ch341_get_status(dev, priv); 2536ce76104SFrank A Kingswood 2546ce76104SFrank A Kingswood out: kfree(buffer); 2556ce76104SFrank A Kingswood return r; 2566ce76104SFrank A Kingswood } 2576ce76104SFrank A Kingswood 258456c5be5SJohan Hovold static int ch341_port_probe(struct usb_serial_port *port) 2596ce76104SFrank A Kingswood { 2606ce76104SFrank A Kingswood struct ch341_private *priv; 2616ce76104SFrank A Kingswood int r; 2626ce76104SFrank A Kingswood 2636ce76104SFrank A Kingswood priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL); 2646ce76104SFrank A Kingswood if (!priv) 2656ce76104SFrank A Kingswood return -ENOMEM; 2666ce76104SFrank A Kingswood 267664d5df9SWerner Cornelius spin_lock_init(&priv->lock); 2686ce76104SFrank A Kingswood priv->baud_rate = DEFAULT_BAUD_RATE; 269664d5df9SWerner Cornelius priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR; 2706ce76104SFrank A Kingswood 271456c5be5SJohan Hovold r = ch341_configure(port->serial->dev, priv); 2726ce76104SFrank A Kingswood if (r < 0) 2736ce76104SFrank A Kingswood goto error; 2746ce76104SFrank A Kingswood 275456c5be5SJohan Hovold usb_set_serial_port_data(port, priv); 2766ce76104SFrank A Kingswood return 0; 2776ce76104SFrank A Kingswood 2786ce76104SFrank A Kingswood error: kfree(priv); 2796ce76104SFrank A Kingswood return r; 2806ce76104SFrank A Kingswood } 2816ce76104SFrank A Kingswood 282456c5be5SJohan Hovold static int ch341_port_remove(struct usb_serial_port *port) 283456c5be5SJohan Hovold { 284456c5be5SJohan Hovold struct ch341_private *priv; 285456c5be5SJohan Hovold 286456c5be5SJohan Hovold priv = usb_get_serial_port_data(port); 287456c5be5SJohan Hovold kfree(priv); 288456c5be5SJohan Hovold 289456c5be5SJohan Hovold return 0; 290456c5be5SJohan Hovold } 291456c5be5SJohan Hovold 292335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port) 293335f8514SAlan Cox { 294335f8514SAlan Cox struct ch341_private *priv = usb_get_serial_port_data(port); 295335f8514SAlan Cox if (priv->line_status & CH341_BIT_DCD) 296335f8514SAlan Cox return 1; 297335f8514SAlan Cox return 0; 298335f8514SAlan Cox } 299335f8514SAlan Cox 300335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on) 301664d5df9SWerner Cornelius { 302664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 303664d5df9SWerner Cornelius unsigned long flags; 304664d5df9SWerner Cornelius 305335f8514SAlan Cox /* drop DTR and RTS */ 306335f8514SAlan Cox spin_lock_irqsave(&priv->lock, flags); 307335f8514SAlan Cox if (on) 308335f8514SAlan Cox priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR; 309335f8514SAlan Cox else 310335f8514SAlan Cox priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR); 311335f8514SAlan Cox spin_unlock_irqrestore(&priv->lock, flags); 312335f8514SAlan Cox ch341_set_handshake(port->serial->dev, priv->line_control); 313335f8514SAlan Cox } 314335f8514SAlan Cox 315335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port) 316335f8514SAlan Cox { 317f26788daSJohan Hovold usb_serial_generic_close(port); 318664d5df9SWerner Cornelius usb_kill_urb(port->interrupt_in_urb); 319664d5df9SWerner Cornelius } 320664d5df9SWerner Cornelius 321664d5df9SWerner Cornelius 3226ce76104SFrank A Kingswood /* open this device, set default parameters */ 323a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) 3246ce76104SFrank A Kingswood { 3256ce76104SFrank A Kingswood struct usb_serial *serial = port->serial; 326456c5be5SJohan Hovold struct ch341_private *priv = usb_get_serial_port_data(port); 3276ce76104SFrank A Kingswood int r; 3286ce76104SFrank A Kingswood 3296ce76104SFrank A Kingswood r = ch341_configure(serial->dev, priv); 3306ce76104SFrank A Kingswood if (r) 3316ce76104SFrank A Kingswood goto out; 3326ce76104SFrank A Kingswood 333aa91def4SNicolas PLANEL if (tty) 334aa91def4SNicolas PLANEL ch341_set_termios(tty, port, NULL); 3356ce76104SFrank A Kingswood 336d9a38a87SJohan Hovold dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__); 337664d5df9SWerner Cornelius r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 338664d5df9SWerner Cornelius if (r) { 339d9a38a87SJohan Hovold dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", 340d9a38a87SJohan Hovold __func__, r); 34106946a66SJohan Hovold goto out; 342664d5df9SWerner Cornelius } 343664d5df9SWerner Cornelius 344a509a7e4SAlan Cox r = usb_serial_generic_open(tty, port); 3456ce76104SFrank A Kingswood 3466ce76104SFrank A Kingswood out: return r; 3476ce76104SFrank A Kingswood } 3486ce76104SFrank A Kingswood 3496ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and 3506ce76104SFrank A Kingswood * tty->termios contains the new setting to be used. 3516ce76104SFrank A Kingswood */ 35295da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty, 35395da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 3546ce76104SFrank A Kingswood { 3556ce76104SFrank A Kingswood struct ch341_private *priv = usb_get_serial_port_data(port); 3566ce76104SFrank A Kingswood unsigned baud_rate; 357664d5df9SWerner Cornelius unsigned long flags; 3586ce76104SFrank A Kingswood 3596ce76104SFrank A Kingswood baud_rate = tty_get_baud_rate(tty); 3606ce76104SFrank A Kingswood 3616ce76104SFrank A Kingswood priv->baud_rate = baud_rate; 362664d5df9SWerner Cornelius 363664d5df9SWerner Cornelius if (baud_rate) { 364664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 365664d5df9SWerner Cornelius priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS); 366664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 367664d5df9SWerner Cornelius ch341_set_baudrate(port->serial->dev, priv); 368664d5df9SWerner Cornelius } else { 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); 3726ce76104SFrank A Kingswood } 3736ce76104SFrank A Kingswood 374664d5df9SWerner Cornelius ch341_set_handshake(port->serial->dev, priv->line_control); 3756ce76104SFrank A Kingswood 3766ce76104SFrank A Kingswood /* Unimplemented: 3776ce76104SFrank A Kingswood * (cflag & CSIZE) : data bits [5, 8] 3786ce76104SFrank A Kingswood * (cflag & PARENB) : parity {NONE, EVEN, ODD} 3796ce76104SFrank A Kingswood * (cflag & CSTOPB) : stop bits [1, 2] 3806ce76104SFrank A Kingswood */ 381664d5df9SWerner Cornelius } 38273f59308SAlan Cox 383492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state) 384492896f0STim Small { 385492896f0STim Small const uint16_t ch341_break_reg = 386*6fde8d29SAidan Thornton ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK; 387492896f0STim Small struct usb_serial_port *port = tty->driver_data; 388492896f0STim Small int r; 389492896f0STim Small uint16_t reg_contents; 390f2b5cc83SJohan Hovold uint8_t *break_reg; 391492896f0STim Small 392f2b5cc83SJohan Hovold break_reg = kmalloc(2, GFP_KERNEL); 39310c642d0SJohan Hovold if (!break_reg) 394f2b5cc83SJohan Hovold return; 395f2b5cc83SJohan Hovold 396492896f0STim Small r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG, 397f2b5cc83SJohan Hovold ch341_break_reg, 0, break_reg, 2); 398492896f0STim Small if (r < 0) { 3996a9b15feSJohan Hovold dev_err(&port->dev, "%s - USB control read error (%d)\n", 4006a9b15feSJohan Hovold __func__, r); 401f2b5cc83SJohan Hovold goto out; 402492896f0STim Small } 40379cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n", 404492896f0STim Small __func__, break_reg[0], break_reg[1]); 405492896f0STim Small if (break_state != 0) { 40679cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__); 407*6fde8d29SAidan Thornton break_reg[0] &= ~CH341_NBREAK_BITS; 408*6fde8d29SAidan Thornton break_reg[1] &= ~CH341_LCR_ENABLE_TX; 409492896f0STim Small } else { 41079cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__); 411*6fde8d29SAidan Thornton break_reg[0] |= CH341_NBREAK_BITS; 412*6fde8d29SAidan Thornton break_reg[1] |= CH341_LCR_ENABLE_TX; 413492896f0STim Small } 41479cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n", 415492896f0STim Small __func__, break_reg[0], break_reg[1]); 4165be796f0SJohan Hovold reg_contents = get_unaligned_le16(break_reg); 417492896f0STim Small r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG, 418492896f0STim Small ch341_break_reg, reg_contents); 419492896f0STim Small if (r < 0) 4206a9b15feSJohan Hovold dev_err(&port->dev, "%s - USB control write error (%d)\n", 4216a9b15feSJohan Hovold __func__, r); 422f2b5cc83SJohan Hovold out: 423f2b5cc83SJohan Hovold kfree(break_reg); 424492896f0STim Small } 425492896f0STim Small 42620b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty, 427664d5df9SWerner Cornelius unsigned int set, unsigned int clear) 428664d5df9SWerner Cornelius { 429664d5df9SWerner Cornelius struct usb_serial_port *port = tty->driver_data; 430664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 431664d5df9SWerner Cornelius unsigned long flags; 432664d5df9SWerner Cornelius u8 control; 433664d5df9SWerner Cornelius 434664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 435664d5df9SWerner Cornelius if (set & TIOCM_RTS) 436664d5df9SWerner Cornelius priv->line_control |= CH341_BIT_RTS; 437664d5df9SWerner Cornelius if (set & TIOCM_DTR) 438664d5df9SWerner Cornelius priv->line_control |= CH341_BIT_DTR; 439664d5df9SWerner Cornelius if (clear & TIOCM_RTS) 440664d5df9SWerner Cornelius priv->line_control &= ~CH341_BIT_RTS; 441664d5df9SWerner Cornelius if (clear & TIOCM_DTR) 442664d5df9SWerner Cornelius priv->line_control &= ~CH341_BIT_DTR; 443664d5df9SWerner Cornelius control = priv->line_control; 444664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 445664d5df9SWerner Cornelius 446664d5df9SWerner Cornelius return ch341_set_handshake(port->serial->dev, control); 447664d5df9SWerner Cornelius } 448664d5df9SWerner Cornelius 449ac035628SJohan Hovold static void ch341_update_line_status(struct usb_serial_port *port, 450ac035628SJohan Hovold unsigned char *data, size_t len) 451ac035628SJohan Hovold { 452ac035628SJohan Hovold struct ch341_private *priv = usb_get_serial_port_data(port); 453b770081fSJohan Hovold struct tty_struct *tty; 454ac035628SJohan Hovold unsigned long flags; 455b770081fSJohan Hovold u8 status; 456b770081fSJohan Hovold u8 delta; 457ac035628SJohan Hovold 458ac035628SJohan Hovold if (len < 4) 459ac035628SJohan Hovold return; 460ac035628SJohan Hovold 461b770081fSJohan Hovold status = ~data[2] & CH341_BITS_MODEM_STAT; 462b770081fSJohan Hovold 463ac035628SJohan Hovold spin_lock_irqsave(&priv->lock, flags); 464b770081fSJohan Hovold delta = status ^ priv->line_status; 465b770081fSJohan Hovold priv->line_status = status; 466ac035628SJohan Hovold spin_unlock_irqrestore(&priv->lock, flags); 467ac035628SJohan Hovold 468fd74b0b1SJohan Hovold if (data[1] & CH341_MULT_STAT) 469fd74b0b1SJohan Hovold dev_dbg(&port->dev, "%s - multiple status change\n", __func__); 470fd74b0b1SJohan Hovold 471d984fe91SJohan Hovold if (!delta) 472d984fe91SJohan Hovold return; 473d984fe91SJohan Hovold 4745e409a26SJohan Hovold if (delta & CH341_BIT_CTS) 4755e409a26SJohan Hovold port->icount.cts++; 4765e409a26SJohan Hovold if (delta & CH341_BIT_DSR) 4775e409a26SJohan Hovold port->icount.dsr++; 4785e409a26SJohan Hovold if (delta & CH341_BIT_RI) 4795e409a26SJohan Hovold port->icount.rng++; 480b770081fSJohan Hovold if (delta & CH341_BIT_DCD) { 4815e409a26SJohan Hovold port->icount.dcd++; 482b770081fSJohan Hovold tty = tty_port_tty_get(&port->port); 483b770081fSJohan Hovold if (tty) { 484ac035628SJohan Hovold usb_serial_handle_dcd_change(port, tty, 485b770081fSJohan Hovold status & CH341_BIT_DCD); 486ac035628SJohan Hovold tty_kref_put(tty); 487ac035628SJohan Hovold } 488b770081fSJohan Hovold } 489ac035628SJohan Hovold 490ac035628SJohan Hovold wake_up_interruptible(&port->port.delta_msr_wait); 491ac035628SJohan Hovold } 492ac035628SJohan Hovold 493664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb) 494664d5df9SWerner Cornelius { 495271ec2d2SJohan Hovold struct usb_serial_port *port = urb->context; 496664d5df9SWerner Cornelius unsigned char *data = urb->transfer_buffer; 497271ec2d2SJohan Hovold unsigned int len = urb->actual_length; 498664d5df9SWerner Cornelius int status; 499664d5df9SWerner Cornelius 500664d5df9SWerner Cornelius switch (urb->status) { 501664d5df9SWerner Cornelius case 0: 502664d5df9SWerner Cornelius /* success */ 503664d5df9SWerner Cornelius break; 504664d5df9SWerner Cornelius case -ECONNRESET: 505664d5df9SWerner Cornelius case -ENOENT: 506664d5df9SWerner Cornelius case -ESHUTDOWN: 507664d5df9SWerner Cornelius /* this urb is terminated, clean up */ 508271ec2d2SJohan Hovold dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", 50979cbeeafSGreg Kroah-Hartman __func__, urb->status); 510664d5df9SWerner Cornelius return; 511664d5df9SWerner Cornelius default: 512271ec2d2SJohan Hovold dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", 51379cbeeafSGreg Kroah-Hartman __func__, urb->status); 514664d5df9SWerner Cornelius goto exit; 515664d5df9SWerner Cornelius } 516664d5df9SWerner Cornelius 517271ec2d2SJohan Hovold usb_serial_debug_data(&port->dev, __func__, len, data); 518271ec2d2SJohan Hovold ch341_update_line_status(port, data, len); 519664d5df9SWerner Cornelius exit: 520664d5df9SWerner Cornelius status = usb_submit_urb(urb, GFP_ATOMIC); 521271ec2d2SJohan Hovold if (status) { 522271ec2d2SJohan Hovold dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n", 523664d5df9SWerner Cornelius __func__, status); 524664d5df9SWerner Cornelius } 525271ec2d2SJohan Hovold } 526664d5df9SWerner Cornelius 52760b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty) 528664d5df9SWerner Cornelius { 529664d5df9SWerner Cornelius struct usb_serial_port *port = tty->driver_data; 530664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 531664d5df9SWerner Cornelius unsigned long flags; 532664d5df9SWerner Cornelius u8 mcr; 533664d5df9SWerner Cornelius u8 status; 534664d5df9SWerner Cornelius unsigned int result; 535664d5df9SWerner Cornelius 536664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 537664d5df9SWerner Cornelius mcr = priv->line_control; 538664d5df9SWerner Cornelius status = priv->line_status; 539664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 540664d5df9SWerner Cornelius 541664d5df9SWerner Cornelius result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0) 542664d5df9SWerner Cornelius | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0) 543664d5df9SWerner Cornelius | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0) 544664d5df9SWerner Cornelius | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0) 545664d5df9SWerner Cornelius | ((status & CH341_BIT_RI) ? TIOCM_RI : 0) 546664d5df9SWerner Cornelius | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0); 547664d5df9SWerner Cornelius 54879cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); 549664d5df9SWerner Cornelius 550664d5df9SWerner Cornelius return result; 5516ce76104SFrank A Kingswood } 5526ce76104SFrank A Kingswood 553622b80cfSGreg Kroah-Hartman static int ch341_reset_resume(struct usb_serial *serial) 5541ded7ea4SMing Lei { 5551ded7ea4SMing Lei struct ch341_private *priv; 5561ded7ea4SMing Lei 5571ded7ea4SMing Lei priv = usb_get_serial_port_data(serial->port[0]); 5581ded7ea4SMing Lei 5591ded7ea4SMing Lei /* reconfigure ch341 serial port after bus-reset */ 5602bfd1c96SGreg Kroah-Hartman ch341_configure(serial->dev, priv); 5611ded7ea4SMing Lei 5621ded7ea4SMing Lei return 0; 5631ded7ea4SMing Lei } 5641ded7ea4SMing Lei 5656ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = { 5666ce76104SFrank A Kingswood .driver = { 5676ce76104SFrank A Kingswood .owner = THIS_MODULE, 5686ce76104SFrank A Kingswood .name = "ch341-uart", 5696ce76104SFrank A Kingswood }, 5706ce76104SFrank A Kingswood .id_table = id_table, 5716ce76104SFrank A Kingswood .num_ports = 1, 5726ce76104SFrank A Kingswood .open = ch341_open, 573335f8514SAlan Cox .dtr_rts = ch341_dtr_rts, 574335f8514SAlan Cox .carrier_raised = ch341_carrier_raised, 575664d5df9SWerner Cornelius .close = ch341_close, 5766ce76104SFrank A Kingswood .set_termios = ch341_set_termios, 577492896f0STim Small .break_ctl = ch341_break_ctl, 578664d5df9SWerner Cornelius .tiocmget = ch341_tiocmget, 579664d5df9SWerner Cornelius .tiocmset = ch341_tiocmset, 5805e409a26SJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 581664d5df9SWerner Cornelius .read_int_callback = ch341_read_int_callback, 582456c5be5SJohan Hovold .port_probe = ch341_port_probe, 583456c5be5SJohan Hovold .port_remove = ch341_port_remove, 5841c1eaba8SGreg Kroah-Hartman .reset_resume = ch341_reset_resume, 5856ce76104SFrank A Kingswood }; 5866ce76104SFrank A Kingswood 58708a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 58808a4f6bcSAlan Stern &ch341_device, NULL 58908a4f6bcSAlan Stern }; 59008a4f6bcSAlan Stern 59168e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 5926ce76104SFrank A Kingswood 5936ce76104SFrank A Kingswood MODULE_LICENSE("GPL"); 594