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[] = { 8446ee4abbSJan-Niklas Burfeind { USB_DEVICE(0x1a86, 0x5512) }, 85d0781383Swangyanqing { USB_DEVICE(0x1a86, 0x5523) }, 86bf193bfcSJohan Hovold { USB_DEVICE(0x1a86, 0x7522) }, 87bf193bfcSJohan Hovold { USB_DEVICE(0x1a86, 0x7523) }, 88*fa77ce20SStephan Brunner { USB_DEVICE(0x2184, 0x0057) }, 89bf193bfcSJohan Hovold { USB_DEVICE(0x4348, 0x5523) }, 905563b3b6SNiv Sardi { USB_DEVICE(0x9986, 0x7523) }, 916ce76104SFrank A Kingswood { }, 926ce76104SFrank A Kingswood }; 936ce76104SFrank A Kingswood MODULE_DEVICE_TABLE(usb, id_table); 946ce76104SFrank A Kingswood 956ce76104SFrank A Kingswood struct ch341_private { 96664d5df9SWerner Cornelius spinlock_t lock; /* access lock */ 97664d5df9SWerner Cornelius unsigned baud_rate; /* set baud rate */ 98beea33d4SJohan Hovold u8 mcr; 99e8024460SJohan Hovold u8 msr; 1003cca8624SJohan Hovold u8 lcr; 101c404bf4aSMichael Hanselmann unsigned long quirks; 1020580baa4SMichael Hanselmann unsigned long break_end; 1036ce76104SFrank A Kingswood }; 1046ce76104SFrank A Kingswood 105aa91def4SNicolas PLANEL static void ch341_set_termios(struct tty_struct *tty, 106aa91def4SNicolas PLANEL struct usb_serial_port *port, 107aa91def4SNicolas PLANEL struct ktermios *old_termios); 108aa91def4SNicolas PLANEL 1096ce76104SFrank A Kingswood static int ch341_control_out(struct usb_device *dev, u8 request, 1106ce76104SFrank A Kingswood u16 value, u16 index) 1116ce76104SFrank A Kingswood { 1126ce76104SFrank A Kingswood int r; 11379cbeeafSGreg Kroah-Hartman 11491e0efcdSJohan Hovold dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__, 11591e0efcdSJohan Hovold request, value, index); 1166ce76104SFrank A Kingswood 1176ce76104SFrank A Kingswood r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, 1186ce76104SFrank A Kingswood USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 1196ce76104SFrank A Kingswood value, index, NULL, 0, DEFAULT_TIMEOUT); 1202d5a9c72SJohan Hovold if (r < 0) 1212d5a9c72SJohan Hovold dev_err(&dev->dev, "failed to send control message: %d\n", r); 1226ce76104SFrank A Kingswood 1236ce76104SFrank A Kingswood return r; 1246ce76104SFrank A Kingswood } 1256ce76104SFrank A Kingswood 1266ce76104SFrank A Kingswood static int ch341_control_in(struct usb_device *dev, 1276ce76104SFrank A Kingswood u8 request, u16 value, u16 index, 1286ce76104SFrank A Kingswood char *buf, unsigned bufsize) 1296ce76104SFrank A Kingswood { 1306ce76104SFrank A Kingswood int r; 13179cbeeafSGreg Kroah-Hartman 13291e0efcdSJohan Hovold dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__, 13391e0efcdSJohan Hovold request, value, index, bufsize); 1346ce76104SFrank A Kingswood 13574f26645SHimadri Pandya r = usb_control_msg_recv(dev, 0, request, 1366ce76104SFrank A Kingswood USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 13774f26645SHimadri Pandya value, index, buf, bufsize, DEFAULT_TIMEOUT, 13874f26645SHimadri Pandya GFP_KERNEL); 13974f26645SHimadri Pandya if (r) { 1402d5a9c72SJohan Hovold dev_err(&dev->dev, "failed to receive control message: %d\n", 1412d5a9c72SJohan Hovold r); 1426ce76104SFrank A Kingswood return r; 1436ce76104SFrank A Kingswood } 1446ce76104SFrank A Kingswood 1452d5a9c72SJohan Hovold return 0; 1462d5a9c72SJohan Hovold } 1472d5a9c72SJohan Hovold 14835714565SJohan Hovold #define CH341_CLKRATE 48000000 14935714565SJohan Hovold #define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact))) 15035714565SJohan Hovold #define CH341_MIN_RATE(ps) (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512)) 15135714565SJohan Hovold 15235714565SJohan Hovold static const speed_t ch341_min_rates[] = { 15335714565SJohan Hovold CH341_MIN_RATE(0), 15435714565SJohan Hovold CH341_MIN_RATE(1), 15535714565SJohan Hovold CH341_MIN_RATE(2), 15635714565SJohan Hovold CH341_MIN_RATE(3), 15735714565SJohan Hovold }; 15835714565SJohan Hovold 15986f6da29SJohan Hovold /* Supported range is 46 to 3000000 bps. */ 16086f6da29SJohan Hovold #define CH341_MIN_BPS DIV_ROUND_UP(CH341_CLKRATE, CH341_CLK_DIV(0, 0) * 256) 16186f6da29SJohan Hovold #define CH341_MAX_BPS (CH341_CLKRATE / (CH341_CLK_DIV(3, 0) * 2)) 16286f6da29SJohan Hovold 16335714565SJohan Hovold /* 16435714565SJohan Hovold * The device line speed is given by the following equation: 16535714565SJohan Hovold * 16635714565SJohan Hovold * baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where 16735714565SJohan Hovold * 16835714565SJohan Hovold * 0 <= ps <= 3, 16935714565SJohan Hovold * 0 <= fact <= 1, 17035714565SJohan Hovold * 2 <= div <= 256 if fact = 0, or 17135714565SJohan Hovold * 9 <= div <= 256 if fact = 1 17235714565SJohan Hovold */ 1730580baa4SMichael Hanselmann static int ch341_get_divisor(struct ch341_private *priv, speed_t speed) 17435714565SJohan Hovold { 17535714565SJohan Hovold unsigned int fact, div, clk_div; 176c432df15SJohan Hovold bool force_fact0 = false; 17735714565SJohan Hovold int ps; 17835714565SJohan Hovold 17935714565SJohan Hovold /* 18035714565SJohan Hovold * Clamp to supported range, this makes the (ps < 0) and (div < 2) 18135714565SJohan Hovold * sanity checks below redundant. 18235714565SJohan Hovold */ 18386f6da29SJohan Hovold speed = clamp_val(speed, CH341_MIN_BPS, CH341_MAX_BPS); 18435714565SJohan Hovold 18535714565SJohan Hovold /* 18635714565SJohan Hovold * Start with highest possible base clock (fact = 1) that will give a 18735714565SJohan Hovold * divisor strictly less than 512. 18835714565SJohan Hovold */ 18935714565SJohan Hovold fact = 1; 19035714565SJohan Hovold for (ps = 3; ps >= 0; ps--) { 19135714565SJohan Hovold if (speed > ch341_min_rates[ps]) 19235714565SJohan Hovold break; 19335714565SJohan Hovold } 19435714565SJohan Hovold 19535714565SJohan Hovold if (ps < 0) 19635714565SJohan Hovold return -EINVAL; 19735714565SJohan Hovold 19835714565SJohan Hovold /* Determine corresponding divisor, rounding down. */ 19935714565SJohan Hovold clk_div = CH341_CLK_DIV(ps, fact); 20035714565SJohan Hovold div = CH341_CLKRATE / (clk_div * speed); 20135714565SJohan Hovold 202c432df15SJohan Hovold /* Some devices require a lower base clock if ps < 3. */ 203c432df15SJohan Hovold if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER)) 204c432df15SJohan Hovold force_fact0 = true; 205c432df15SJohan Hovold 20635714565SJohan Hovold /* Halve base clock (fact = 0) if required. */ 207c432df15SJohan Hovold if (div < 9 || div > 255 || force_fact0) { 20835714565SJohan Hovold div /= 2; 20935714565SJohan Hovold clk_div *= 2; 21035714565SJohan Hovold fact = 0; 21135714565SJohan Hovold } 21235714565SJohan Hovold 21335714565SJohan Hovold if (div < 2) 21435714565SJohan Hovold return -EINVAL; 21535714565SJohan Hovold 21635714565SJohan Hovold /* 21735714565SJohan Hovold * Pick next divisor if resulting rate is closer to the requested one, 21835714565SJohan Hovold * scale up to avoid rounding errors on low rates. 21935714565SJohan Hovold */ 22035714565SJohan Hovold if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >= 22135714565SJohan Hovold 16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1))) 22235714565SJohan Hovold div++; 22335714565SJohan Hovold 2247c3d0228SJohan Hovold /* 2257c3d0228SJohan Hovold * Prefer lower base clock (fact = 0) if even divisor. 2267c3d0228SJohan Hovold * 2277c3d0228SJohan Hovold * Note that this makes the receiver more tolerant to errors. 2287c3d0228SJohan Hovold */ 2297c3d0228SJohan Hovold if (fact == 1 && div % 2 == 0) { 2307c3d0228SJohan Hovold div /= 2; 2317c3d0228SJohan Hovold fact = 0; 2327c3d0228SJohan Hovold } 2337c3d0228SJohan Hovold 23435714565SJohan Hovold return (0x100 - div) << 8 | fact << 2 | ps; 23535714565SJohan Hovold } 23635714565SJohan Hovold 23755fa15b5SJohan Hovold static int ch341_set_baudrate_lcr(struct usb_device *dev, 2380580baa4SMichael Hanselmann struct ch341_private *priv, 2390580baa4SMichael Hanselmann speed_t baud_rate, u8 lcr) 2406ce76104SFrank A Kingswood { 24135714565SJohan Hovold int val; 2426ce76104SFrank A Kingswood int r; 2436ce76104SFrank A Kingswood 2440580baa4SMichael Hanselmann if (!baud_rate) 2456ce76104SFrank A Kingswood return -EINVAL; 246664d5df9SWerner Cornelius 2470580baa4SMichael Hanselmann val = ch341_get_divisor(priv, baud_rate); 24835714565SJohan Hovold if (val < 0) 249664d5df9SWerner Cornelius return -EINVAL; 250664d5df9SWerner Cornelius 25155fa15b5SJohan Hovold /* 25255fa15b5SJohan Hovold * CH341A buffers data until a full endpoint-size packet (32 bytes) 25355fa15b5SJohan Hovold * has been received unless bit 7 is set. 25455fa15b5SJohan Hovold */ 25535714565SJohan Hovold val |= BIT(7); 25655fa15b5SJohan Hovold 2572c509d1cSMichael Hanselmann r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 2582c509d1cSMichael Hanselmann CH341_REG_DIVISOR << 8 | CH341_REG_PRESCALER, 2592c509d1cSMichael Hanselmann val); 26055fa15b5SJohan Hovold if (r) 26155fa15b5SJohan Hovold return r; 26255fa15b5SJohan Hovold 2632c509d1cSMichael Hanselmann /* 2642c509d1cSMichael Hanselmann * Chip versions before version 0x30 as read using 2652c509d1cSMichael Hanselmann * CH341_REQ_READ_VERSION used separate registers for line control 2662c509d1cSMichael Hanselmann * (stop bits, parity and word length). Version 0x30 and above use 2672c509d1cSMichael Hanselmann * CH341_REG_LCR only and CH341_REG_LCR2 is always set to zero. 2682c509d1cSMichael Hanselmann */ 2692c509d1cSMichael Hanselmann r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 2702c509d1cSMichael Hanselmann CH341_REG_LCR2 << 8 | CH341_REG_LCR, lcr); 27155fa15b5SJohan Hovold if (r) 27255fa15b5SJohan Hovold return r; 2736ce76104SFrank A Kingswood 2746ce76104SFrank A Kingswood return r; 2756ce76104SFrank A Kingswood } 2766ce76104SFrank A Kingswood 277664d5df9SWerner Cornelius static int ch341_set_handshake(struct usb_device *dev, u8 control) 2786ce76104SFrank A Kingswood { 2796fde8d29SAidan Thornton return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0); 2806ce76104SFrank A Kingswood } 2816ce76104SFrank A Kingswood 282664d5df9SWerner Cornelius static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv) 2836ce76104SFrank A Kingswood { 2842d5a9c72SJohan Hovold const unsigned int size = 2; 28574f26645SHimadri Pandya u8 buffer[2]; 2866ce76104SFrank A Kingswood int r; 287664d5df9SWerner Cornelius unsigned long flags; 2886ce76104SFrank A Kingswood 2896fde8d29SAidan Thornton r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size); 29074f26645SHimadri Pandya if (r) 29174f26645SHimadri Pandya return r; 2926ce76104SFrank A Kingswood 293664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 294e8024460SJohan Hovold priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT; 295664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 2966ce76104SFrank A Kingswood 29774f26645SHimadri Pandya return 0; 2986ce76104SFrank A Kingswood } 2996ce76104SFrank A Kingswood 3006ce76104SFrank A Kingswood /* -------------------------------------------------------------------------- */ 3016ce76104SFrank A Kingswood 30293b6497dSAdrian Bunk static int ch341_configure(struct usb_device *dev, struct ch341_private *priv) 3036ce76104SFrank A Kingswood { 3042d5a9c72SJohan Hovold const unsigned int size = 2; 30574f26645SHimadri Pandya u8 buffer[2]; 3066ce76104SFrank A Kingswood int r; 3076ce76104SFrank A Kingswood 3086ce76104SFrank A Kingswood /* expect two bytes 0x27 0x00 */ 3096fde8d29SAidan Thornton r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size); 31074f26645SHimadri Pandya if (r) 31174f26645SHimadri Pandya return r; 312a98b6900SAidan Thornton dev_dbg(&dev->dev, "Chip version: 0x%02x\n", buffer[0]); 3136ce76104SFrank A Kingswood 3146fde8d29SAidan Thornton r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0); 3156ce76104SFrank A Kingswood if (r < 0) 31674f26645SHimadri Pandya return r; 3176ce76104SFrank A Kingswood 3180580baa4SMichael Hanselmann r = ch341_set_baudrate_lcr(dev, priv, priv->baud_rate, priv->lcr); 3196ce76104SFrank A Kingswood if (r < 0) 32074f26645SHimadri Pandya return r; 3216ce76104SFrank A Kingswood 322beea33d4SJohan Hovold r = ch341_set_handshake(dev, priv->mcr); 32374f26645SHimadri Pandya if (r < 0) 3246ce76104SFrank A Kingswood return r; 32574f26645SHimadri Pandya 32674f26645SHimadri Pandya return 0; 3276ce76104SFrank A Kingswood } 3286ce76104SFrank A Kingswood 329c404bf4aSMichael Hanselmann static int ch341_detect_quirks(struct usb_serial_port *port) 330c404bf4aSMichael Hanselmann { 331c404bf4aSMichael Hanselmann struct ch341_private *priv = usb_get_serial_port_data(port); 332c404bf4aSMichael Hanselmann struct usb_device *udev = port->serial->dev; 333c404bf4aSMichael Hanselmann const unsigned int size = 2; 334c404bf4aSMichael Hanselmann unsigned long quirks = 0; 33574f26645SHimadri Pandya u8 buffer[2]; 336c404bf4aSMichael Hanselmann int r; 337c404bf4aSMichael Hanselmann 338c404bf4aSMichael Hanselmann /* 339c404bf4aSMichael Hanselmann * A subset of CH34x devices does not support all features. The 340c404bf4aSMichael Hanselmann * prescaler is limited and there is no support for sending a RS232 341c404bf4aSMichael Hanselmann * break condition. A read failure when trying to set up the latter is 342c404bf4aSMichael Hanselmann * used to detect these devices. 343c404bf4aSMichael Hanselmann */ 34474f26645SHimadri Pandya r = usb_control_msg_recv(udev, 0, CH341_REQ_READ_REG, 345c404bf4aSMichael Hanselmann USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 34674f26645SHimadri Pandya CH341_REG_BREAK, 0, &buffer, size, 34774f26645SHimadri Pandya DEFAULT_TIMEOUT, GFP_KERNEL); 348c404bf4aSMichael Hanselmann if (r == -EPIPE) { 3490580baa4SMichael Hanselmann dev_info(&port->dev, "break control not supported, using simulated break\n"); 3500580baa4SMichael Hanselmann quirks = CH341_QUIRK_LIMITED_PRESCALER | CH341_QUIRK_SIMULATE_BREAK; 351c404bf4aSMichael Hanselmann r = 0; 35274f26645SHimadri Pandya } else if (r) { 353c404bf4aSMichael Hanselmann dev_err(&port->dev, "failed to read break control: %d\n", r); 354c404bf4aSMichael Hanselmann } 355c404bf4aSMichael Hanselmann 356c404bf4aSMichael Hanselmann if (quirks) { 357c404bf4aSMichael Hanselmann dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks); 358c404bf4aSMichael Hanselmann priv->quirks |= quirks; 359c404bf4aSMichael Hanselmann } 360c404bf4aSMichael Hanselmann 361c404bf4aSMichael Hanselmann return r; 362c404bf4aSMichael Hanselmann } 363c404bf4aSMichael Hanselmann 364456c5be5SJohan Hovold static int ch341_port_probe(struct usb_serial_port *port) 3656ce76104SFrank A Kingswood { 3666ce76104SFrank A Kingswood struct ch341_private *priv; 3676ce76104SFrank A Kingswood int r; 3686ce76104SFrank A Kingswood 3696ce76104SFrank A Kingswood priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL); 3706ce76104SFrank A Kingswood if (!priv) 3716ce76104SFrank A Kingswood return -ENOMEM; 3726ce76104SFrank A Kingswood 373664d5df9SWerner Cornelius spin_lock_init(&priv->lock); 3746ce76104SFrank A Kingswood priv->baud_rate = DEFAULT_BAUD_RATE; 3757c61b0d5SJohan Hovold /* 3767c61b0d5SJohan Hovold * Some CH340 devices appear unable to change the initial LCR 3777c61b0d5SJohan Hovold * settings, so set a sane 8N1 default. 3787c61b0d5SJohan Hovold */ 3797c61b0d5SJohan Hovold priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8; 3806ce76104SFrank A Kingswood 381456c5be5SJohan Hovold r = ch341_configure(port->serial->dev, priv); 3826ce76104SFrank A Kingswood if (r < 0) 3836ce76104SFrank A Kingswood goto error; 3846ce76104SFrank A Kingswood 385456c5be5SJohan Hovold usb_set_serial_port_data(port, priv); 386c404bf4aSMichael Hanselmann 387c404bf4aSMichael Hanselmann r = ch341_detect_quirks(port); 388c404bf4aSMichael Hanselmann if (r < 0) 389c404bf4aSMichael Hanselmann goto error; 390c404bf4aSMichael Hanselmann 3916ce76104SFrank A Kingswood return 0; 3926ce76104SFrank A Kingswood 3936ce76104SFrank A Kingswood error: kfree(priv); 3946ce76104SFrank A Kingswood return r; 3956ce76104SFrank A Kingswood } 3966ce76104SFrank A Kingswood 397c5d1448fSUwe Kleine-König static void ch341_port_remove(struct usb_serial_port *port) 398456c5be5SJohan Hovold { 399456c5be5SJohan Hovold struct ch341_private *priv; 400456c5be5SJohan Hovold 401456c5be5SJohan Hovold priv = usb_get_serial_port_data(port); 402456c5be5SJohan Hovold kfree(priv); 403456c5be5SJohan Hovold } 404456c5be5SJohan Hovold 405335f8514SAlan Cox static int ch341_carrier_raised(struct usb_serial_port *port) 406335f8514SAlan Cox { 407335f8514SAlan Cox struct ch341_private *priv = usb_get_serial_port_data(port); 408e8024460SJohan Hovold if (priv->msr & CH341_BIT_DCD) 409335f8514SAlan Cox return 1; 410335f8514SAlan Cox return 0; 411335f8514SAlan Cox } 412335f8514SAlan Cox 413335f8514SAlan Cox static void ch341_dtr_rts(struct usb_serial_port *port, int on) 414664d5df9SWerner Cornelius { 415664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 416664d5df9SWerner Cornelius unsigned long flags; 417664d5df9SWerner Cornelius 418335f8514SAlan Cox /* drop DTR and RTS */ 419335f8514SAlan Cox spin_lock_irqsave(&priv->lock, flags); 420335f8514SAlan Cox if (on) 421beea33d4SJohan Hovold priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR; 422335f8514SAlan Cox else 423beea33d4SJohan Hovold priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR); 424335f8514SAlan Cox spin_unlock_irqrestore(&priv->lock, flags); 425beea33d4SJohan Hovold ch341_set_handshake(port->serial->dev, priv->mcr); 426335f8514SAlan Cox } 427335f8514SAlan Cox 428335f8514SAlan Cox static void ch341_close(struct usb_serial_port *port) 429335f8514SAlan Cox { 430f26788daSJohan Hovold usb_serial_generic_close(port); 431664d5df9SWerner Cornelius usb_kill_urb(port->interrupt_in_urb); 432664d5df9SWerner Cornelius } 433664d5df9SWerner Cornelius 434664d5df9SWerner Cornelius 4356ce76104SFrank A Kingswood /* open this device, set default parameters */ 436a509a7e4SAlan Cox static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) 4376ce76104SFrank A Kingswood { 438456c5be5SJohan Hovold struct ch341_private *priv = usb_get_serial_port_data(port); 4396ce76104SFrank A Kingswood int r; 4406ce76104SFrank A Kingswood 441aa91def4SNicolas PLANEL if (tty) 442aa91def4SNicolas PLANEL ch341_set_termios(tty, port, NULL); 4436ce76104SFrank A Kingswood 444d9a38a87SJohan Hovold dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__); 445664d5df9SWerner Cornelius r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 446664d5df9SWerner Cornelius if (r) { 447d9a38a87SJohan Hovold dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", 448d9a38a87SJohan Hovold __func__, r); 449f2950b78SJohan Hovold return r; 450664d5df9SWerner Cornelius } 451664d5df9SWerner Cornelius 452a0467a96SJohan Hovold r = ch341_get_status(port->serial->dev, priv); 453a0467a96SJohan Hovold if (r < 0) { 454a0467a96SJohan Hovold dev_err(&port->dev, "failed to read modem status: %d\n", r); 455a0467a96SJohan Hovold goto err_kill_interrupt_urb; 456a0467a96SJohan Hovold } 457a0467a96SJohan Hovold 458a509a7e4SAlan Cox r = usb_serial_generic_open(tty, port); 459f2950b78SJohan Hovold if (r) 460f2950b78SJohan Hovold goto err_kill_interrupt_urb; 4616ce76104SFrank A Kingswood 462f2950b78SJohan Hovold return 0; 463f2950b78SJohan Hovold 464f2950b78SJohan Hovold err_kill_interrupt_urb: 465f2950b78SJohan Hovold usb_kill_urb(port->interrupt_in_urb); 466f2950b78SJohan Hovold 467f2950b78SJohan Hovold return r; 4686ce76104SFrank A Kingswood } 4696ce76104SFrank A Kingswood 4706ce76104SFrank A Kingswood /* Old_termios contains the original termios settings and 4716ce76104SFrank A Kingswood * tty->termios contains the new setting to be used. 4726ce76104SFrank A Kingswood */ 47395da310eSAlan Cox static void ch341_set_termios(struct tty_struct *tty, 47495da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 4756ce76104SFrank A Kingswood { 4766ce76104SFrank A Kingswood struct ch341_private *priv = usb_get_serial_port_data(port); 4776ce76104SFrank A Kingswood unsigned baud_rate; 478664d5df9SWerner Cornelius unsigned long flags; 479448b6dc5SJohan Hovold u8 lcr; 4804e46c410SAidan Thornton int r; 4814e46c410SAidan Thornton 4824e46c410SAidan Thornton /* redundant changes may cause the chip to lose bytes */ 4834e46c410SAidan Thornton if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) 4844e46c410SAidan Thornton return; 4856ce76104SFrank A Kingswood 4866ce76104SFrank A Kingswood baud_rate = tty_get_baud_rate(tty); 4876ce76104SFrank A Kingswood 488448b6dc5SJohan Hovold lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX; 489664d5df9SWerner Cornelius 490ba781bdfSAidan Thornton switch (C_CSIZE(tty)) { 491ba781bdfSAidan Thornton case CS5: 492448b6dc5SJohan Hovold lcr |= CH341_LCR_CS5; 493ba781bdfSAidan Thornton break; 494ba781bdfSAidan Thornton case CS6: 495448b6dc5SJohan Hovold lcr |= CH341_LCR_CS6; 496ba781bdfSAidan Thornton break; 497ba781bdfSAidan Thornton case CS7: 498448b6dc5SJohan Hovold lcr |= CH341_LCR_CS7; 499ba781bdfSAidan Thornton break; 500ba781bdfSAidan Thornton case CS8: 501448b6dc5SJohan Hovold lcr |= CH341_LCR_CS8; 502ba781bdfSAidan Thornton break; 503ba781bdfSAidan Thornton } 504ba781bdfSAidan Thornton 505ba781bdfSAidan Thornton if (C_PARENB(tty)) { 506448b6dc5SJohan Hovold lcr |= CH341_LCR_ENABLE_PAR; 507ba781bdfSAidan Thornton if (C_PARODD(tty) == 0) 508448b6dc5SJohan Hovold lcr |= CH341_LCR_PAR_EVEN; 509ba781bdfSAidan Thornton if (C_CMSPAR(tty)) 510448b6dc5SJohan Hovold lcr |= CH341_LCR_MARK_SPACE; 511ba781bdfSAidan Thornton } 512ba781bdfSAidan Thornton 513ba781bdfSAidan Thornton if (C_CSTOPB(tty)) 514448b6dc5SJohan Hovold lcr |= CH341_LCR_STOP_BITS_2; 5154e46c410SAidan Thornton 516664d5df9SWerner Cornelius if (baud_rate) { 517a20047f3SJohan Hovold priv->baud_rate = baud_rate; 518a20047f3SJohan Hovold 5190580baa4SMichael Hanselmann r = ch341_set_baudrate_lcr(port->serial->dev, priv, 5200580baa4SMichael Hanselmann priv->baud_rate, lcr); 5214e46c410SAidan Thornton if (r < 0 && old_termios) { 5224e46c410SAidan Thornton priv->baud_rate = tty_termios_baud_rate(old_termios); 5234e46c410SAidan Thornton tty_termios_copy_hw(&tty->termios, old_termios); 5243cca8624SJohan Hovold } else if (r == 0) { 525448b6dc5SJohan Hovold priv->lcr = lcr; 5264e46c410SAidan Thornton } 5276ce76104SFrank A Kingswood } 5286ce76104SFrank A Kingswood 529030ee7aeSJohan Hovold spin_lock_irqsave(&priv->lock, flags); 530030ee7aeSJohan Hovold if (C_BAUD(tty) == B0) 531beea33d4SJohan Hovold priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS); 532030ee7aeSJohan Hovold else if (old_termios && (old_termios->c_cflag & CBAUD) == B0) 533beea33d4SJohan Hovold priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS); 534030ee7aeSJohan Hovold spin_unlock_irqrestore(&priv->lock, flags); 5356ce76104SFrank A Kingswood 536beea33d4SJohan Hovold ch341_set_handshake(port->serial->dev, priv->mcr); 537664d5df9SWerner Cornelius } 53873f59308SAlan Cox 5390580baa4SMichael Hanselmann /* 5400580baa4SMichael Hanselmann * A subset of all CH34x devices don't support a real break condition and 5410580baa4SMichael Hanselmann * reading CH341_REG_BREAK fails (see also ch341_detect_quirks). This function 5420580baa4SMichael Hanselmann * simulates a break condition by lowering the baud rate to the minimum 5430580baa4SMichael Hanselmann * supported by the hardware upon enabling the break condition and sending 5440580baa4SMichael Hanselmann * a NUL byte. 5450580baa4SMichael Hanselmann * 5460580baa4SMichael Hanselmann * Incoming data is corrupted while the break condition is being simulated. 5470580baa4SMichael Hanselmann * 5480580baa4SMichael Hanselmann * Normally the duration of the break condition can be controlled individually 5490580baa4SMichael Hanselmann * by userspace using TIOCSBRK and TIOCCBRK or by passing an argument to 5500580baa4SMichael Hanselmann * TCSBRKP. Due to how the simulation is implemented the duration can't be 5510580baa4SMichael Hanselmann * controlled. The duration is always about (1s / 46bd * 9bit) = 196ms. 5520580baa4SMichael Hanselmann */ 5530580baa4SMichael Hanselmann static void ch341_simulate_break(struct tty_struct *tty, int break_state) 5540580baa4SMichael Hanselmann { 5550580baa4SMichael Hanselmann struct usb_serial_port *port = tty->driver_data; 5560580baa4SMichael Hanselmann struct ch341_private *priv = usb_get_serial_port_data(port); 5570580baa4SMichael Hanselmann unsigned long now, delay; 5580580baa4SMichael Hanselmann int r; 5590580baa4SMichael Hanselmann 5600580baa4SMichael Hanselmann if (break_state != 0) { 5610580baa4SMichael Hanselmann dev_dbg(&port->dev, "enter break state requested\n"); 5620580baa4SMichael Hanselmann 5630580baa4SMichael Hanselmann r = ch341_set_baudrate_lcr(port->serial->dev, priv, 5640580baa4SMichael Hanselmann CH341_MIN_BPS, 5650580baa4SMichael Hanselmann CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8); 5660580baa4SMichael Hanselmann if (r < 0) { 5670580baa4SMichael Hanselmann dev_err(&port->dev, 5680580baa4SMichael Hanselmann "failed to change baud rate to %u: %d\n", 5690580baa4SMichael Hanselmann CH341_MIN_BPS, r); 5700580baa4SMichael Hanselmann goto restore; 5710580baa4SMichael Hanselmann } 5720580baa4SMichael Hanselmann 5730580baa4SMichael Hanselmann r = tty_put_char(tty, '\0'); 5740580baa4SMichael Hanselmann if (r < 0) { 5750580baa4SMichael Hanselmann dev_err(&port->dev, 5760580baa4SMichael Hanselmann "failed to write NUL byte for simulated break condition: %d\n", 5770580baa4SMichael Hanselmann r); 5780580baa4SMichael Hanselmann goto restore; 5790580baa4SMichael Hanselmann } 5800580baa4SMichael Hanselmann 5810580baa4SMichael Hanselmann /* 5826d0bdc42SMichael Hanselmann * Compute expected transmission duration including safety 5836d0bdc42SMichael Hanselmann * margin. The original baud rate is only restored after the 5846d0bdc42SMichael Hanselmann * computed point in time. 5856d0bdc42SMichael Hanselmann * 5866d0bdc42SMichael Hanselmann * 11 bits = 1 start, 8 data, 1 stop, 1 margin 5870580baa4SMichael Hanselmann */ 5886d0bdc42SMichael Hanselmann priv->break_end = jiffies + (11 * HZ / CH341_MIN_BPS); 5890580baa4SMichael Hanselmann 5900580baa4SMichael Hanselmann return; 5910580baa4SMichael Hanselmann } 5920580baa4SMichael Hanselmann 5930580baa4SMichael Hanselmann dev_dbg(&port->dev, "leave break state requested\n"); 5940580baa4SMichael Hanselmann 5950580baa4SMichael Hanselmann now = jiffies; 5960580baa4SMichael Hanselmann 5970580baa4SMichael Hanselmann if (time_before(now, priv->break_end)) { 5980580baa4SMichael Hanselmann /* Wait until NUL byte is written */ 5990580baa4SMichael Hanselmann delay = priv->break_end - now; 6000580baa4SMichael Hanselmann dev_dbg(&port->dev, 6010580baa4SMichael Hanselmann "wait %d ms while transmitting NUL byte at %u baud\n", 6020580baa4SMichael Hanselmann jiffies_to_msecs(delay), CH341_MIN_BPS); 6030580baa4SMichael Hanselmann schedule_timeout_interruptible(delay); 6040580baa4SMichael Hanselmann } 6050580baa4SMichael Hanselmann 6060580baa4SMichael Hanselmann restore: 6070580baa4SMichael Hanselmann /* Restore original baud rate */ 6080580baa4SMichael Hanselmann r = ch341_set_baudrate_lcr(port->serial->dev, priv, priv->baud_rate, 6090580baa4SMichael Hanselmann priv->lcr); 6100580baa4SMichael Hanselmann if (r < 0) 6110580baa4SMichael Hanselmann dev_err(&port->dev, 6120580baa4SMichael Hanselmann "restoring original baud rate of %u failed: %d\n", 6130580baa4SMichael Hanselmann priv->baud_rate, r); 6140580baa4SMichael Hanselmann } 6150580baa4SMichael Hanselmann 616492896f0STim Small static void ch341_break_ctl(struct tty_struct *tty, int break_state) 617492896f0STim Small { 618492896f0STim Small const uint16_t ch341_break_reg = 6196fde8d29SAidan Thornton ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK; 620492896f0STim Small struct usb_serial_port *port = tty->driver_data; 6210580baa4SMichael Hanselmann struct ch341_private *priv = usb_get_serial_port_data(port); 622492896f0STim Small int r; 623492896f0STim Small uint16_t reg_contents; 62474f26645SHimadri Pandya uint8_t break_reg[2]; 625492896f0STim Small 6260580baa4SMichael Hanselmann if (priv->quirks & CH341_QUIRK_SIMULATE_BREAK) { 6270580baa4SMichael Hanselmann ch341_simulate_break(tty, break_state); 6280580baa4SMichael Hanselmann return; 6290580baa4SMichael Hanselmann } 6300580baa4SMichael Hanselmann 631492896f0STim Small r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG, 632f2b5cc83SJohan Hovold ch341_break_reg, 0, break_reg, 2); 63374f26645SHimadri Pandya if (r) { 6346a9b15feSJohan Hovold dev_err(&port->dev, "%s - USB control read error (%d)\n", 6356a9b15feSJohan Hovold __func__, r); 63674f26645SHimadri Pandya return; 637492896f0STim Small } 63879cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n", 639492896f0STim Small __func__, break_reg[0], break_reg[1]); 640492896f0STim Small if (break_state != 0) { 64179cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__); 6426fde8d29SAidan Thornton break_reg[0] &= ~CH341_NBREAK_BITS; 6436fde8d29SAidan Thornton break_reg[1] &= ~CH341_LCR_ENABLE_TX; 644492896f0STim Small } else { 64579cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__); 6466fde8d29SAidan Thornton break_reg[0] |= CH341_NBREAK_BITS; 6476fde8d29SAidan Thornton break_reg[1] |= CH341_LCR_ENABLE_TX; 648492896f0STim Small } 64979cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n", 650492896f0STim Small __func__, break_reg[0], break_reg[1]); 6515be796f0SJohan Hovold reg_contents = get_unaligned_le16(break_reg); 652492896f0STim Small r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG, 653492896f0STim Small ch341_break_reg, reg_contents); 654492896f0STim Small if (r < 0) 6556a9b15feSJohan Hovold dev_err(&port->dev, "%s - USB control write error (%d)\n", 6566a9b15feSJohan Hovold __func__, r); 657492896f0STim Small } 658492896f0STim Small 65920b9d177SAlan Cox static int ch341_tiocmset(struct tty_struct *tty, 660664d5df9SWerner Cornelius unsigned int set, unsigned int clear) 661664d5df9SWerner Cornelius { 662664d5df9SWerner Cornelius struct usb_serial_port *port = tty->driver_data; 663664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 664664d5df9SWerner Cornelius unsigned long flags; 665664d5df9SWerner Cornelius u8 control; 666664d5df9SWerner Cornelius 667664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 668664d5df9SWerner Cornelius if (set & TIOCM_RTS) 669beea33d4SJohan Hovold priv->mcr |= CH341_BIT_RTS; 670664d5df9SWerner Cornelius if (set & TIOCM_DTR) 671beea33d4SJohan Hovold priv->mcr |= CH341_BIT_DTR; 672664d5df9SWerner Cornelius if (clear & TIOCM_RTS) 673beea33d4SJohan Hovold priv->mcr &= ~CH341_BIT_RTS; 674664d5df9SWerner Cornelius if (clear & TIOCM_DTR) 675beea33d4SJohan Hovold priv->mcr &= ~CH341_BIT_DTR; 676beea33d4SJohan Hovold control = priv->mcr; 677664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 678664d5df9SWerner Cornelius 679664d5df9SWerner Cornelius return ch341_set_handshake(port->serial->dev, control); 680664d5df9SWerner Cornelius } 681664d5df9SWerner Cornelius 682e8024460SJohan Hovold static void ch341_update_status(struct usb_serial_port *port, 683ac035628SJohan Hovold unsigned char *data, size_t len) 684ac035628SJohan Hovold { 685ac035628SJohan Hovold struct ch341_private *priv = usb_get_serial_port_data(port); 686b770081fSJohan Hovold struct tty_struct *tty; 687ac035628SJohan Hovold unsigned long flags; 688b770081fSJohan Hovold u8 status; 689b770081fSJohan Hovold u8 delta; 690ac035628SJohan Hovold 691ac035628SJohan Hovold if (len < 4) 692ac035628SJohan Hovold return; 693ac035628SJohan Hovold 694b770081fSJohan Hovold status = ~data[2] & CH341_BITS_MODEM_STAT; 695b770081fSJohan Hovold 696ac035628SJohan Hovold spin_lock_irqsave(&priv->lock, flags); 697e8024460SJohan Hovold delta = status ^ priv->msr; 698e8024460SJohan Hovold priv->msr = status; 699ac035628SJohan Hovold spin_unlock_irqrestore(&priv->lock, flags); 700ac035628SJohan Hovold 701fd74b0b1SJohan Hovold if (data[1] & CH341_MULT_STAT) 702fd74b0b1SJohan Hovold dev_dbg(&port->dev, "%s - multiple status change\n", __func__); 703fd74b0b1SJohan Hovold 704d984fe91SJohan Hovold if (!delta) 705d984fe91SJohan Hovold return; 706d984fe91SJohan Hovold 7075e409a26SJohan Hovold if (delta & CH341_BIT_CTS) 7085e409a26SJohan Hovold port->icount.cts++; 7095e409a26SJohan Hovold if (delta & CH341_BIT_DSR) 7105e409a26SJohan Hovold port->icount.dsr++; 7115e409a26SJohan Hovold if (delta & CH341_BIT_RI) 7125e409a26SJohan Hovold port->icount.rng++; 713b770081fSJohan Hovold if (delta & CH341_BIT_DCD) { 7145e409a26SJohan Hovold port->icount.dcd++; 715b770081fSJohan Hovold tty = tty_port_tty_get(&port->port); 716b770081fSJohan Hovold if (tty) { 717ac035628SJohan Hovold usb_serial_handle_dcd_change(port, tty, 718b770081fSJohan Hovold status & CH341_BIT_DCD); 719ac035628SJohan Hovold tty_kref_put(tty); 720ac035628SJohan Hovold } 721b770081fSJohan Hovold } 722ac035628SJohan Hovold 723ac035628SJohan Hovold wake_up_interruptible(&port->port.delta_msr_wait); 724ac035628SJohan Hovold } 725ac035628SJohan Hovold 726664d5df9SWerner Cornelius static void ch341_read_int_callback(struct urb *urb) 727664d5df9SWerner Cornelius { 728271ec2d2SJohan Hovold struct usb_serial_port *port = urb->context; 729664d5df9SWerner Cornelius unsigned char *data = urb->transfer_buffer; 730271ec2d2SJohan Hovold unsigned int len = urb->actual_length; 731664d5df9SWerner Cornelius int status; 732664d5df9SWerner Cornelius 733664d5df9SWerner Cornelius switch (urb->status) { 734664d5df9SWerner Cornelius case 0: 735664d5df9SWerner Cornelius /* success */ 736664d5df9SWerner Cornelius break; 737664d5df9SWerner Cornelius case -ECONNRESET: 738664d5df9SWerner Cornelius case -ENOENT: 739664d5df9SWerner Cornelius case -ESHUTDOWN: 740664d5df9SWerner Cornelius /* this urb is terminated, clean up */ 741271ec2d2SJohan Hovold dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", 74279cbeeafSGreg Kroah-Hartman __func__, urb->status); 743664d5df9SWerner Cornelius return; 744664d5df9SWerner Cornelius default: 745271ec2d2SJohan Hovold dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", 74679cbeeafSGreg Kroah-Hartman __func__, urb->status); 747664d5df9SWerner Cornelius goto exit; 748664d5df9SWerner Cornelius } 749664d5df9SWerner Cornelius 750271ec2d2SJohan Hovold usb_serial_debug_data(&port->dev, __func__, len, data); 751e8024460SJohan Hovold ch341_update_status(port, data, len); 752664d5df9SWerner Cornelius exit: 753664d5df9SWerner Cornelius status = usb_submit_urb(urb, GFP_ATOMIC); 754271ec2d2SJohan Hovold if (status) { 755271ec2d2SJohan Hovold dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n", 756664d5df9SWerner Cornelius __func__, status); 757664d5df9SWerner Cornelius } 758271ec2d2SJohan Hovold } 759664d5df9SWerner Cornelius 76060b33c13SAlan Cox static int ch341_tiocmget(struct tty_struct *tty) 761664d5df9SWerner Cornelius { 762664d5df9SWerner Cornelius struct usb_serial_port *port = tty->driver_data; 763664d5df9SWerner Cornelius struct ch341_private *priv = usb_get_serial_port_data(port); 764664d5df9SWerner Cornelius unsigned long flags; 765664d5df9SWerner Cornelius u8 mcr; 766664d5df9SWerner Cornelius u8 status; 767664d5df9SWerner Cornelius unsigned int result; 768664d5df9SWerner Cornelius 769664d5df9SWerner Cornelius spin_lock_irqsave(&priv->lock, flags); 770beea33d4SJohan Hovold mcr = priv->mcr; 771e8024460SJohan Hovold status = priv->msr; 772664d5df9SWerner Cornelius spin_unlock_irqrestore(&priv->lock, flags); 773664d5df9SWerner Cornelius 774664d5df9SWerner Cornelius result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0) 775664d5df9SWerner Cornelius | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0) 776664d5df9SWerner Cornelius | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0) 777664d5df9SWerner Cornelius | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0) 778664d5df9SWerner Cornelius | ((status & CH341_BIT_RI) ? TIOCM_RI : 0) 779664d5df9SWerner Cornelius | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0); 780664d5df9SWerner Cornelius 78179cbeeafSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); 782664d5df9SWerner Cornelius 783664d5df9SWerner Cornelius return result; 7846ce76104SFrank A Kingswood } 7856ce76104SFrank A Kingswood 786622b80cfSGreg Kroah-Hartman static int ch341_reset_resume(struct usb_serial *serial) 7871ded7ea4SMing Lei { 788ce5e2928SJohan Hovold struct usb_serial_port *port = serial->port[0]; 7894d5ef53fSJohan Hovold struct ch341_private *priv; 790ce5e2928SJohan Hovold int ret; 7911ded7ea4SMing Lei 7924d5ef53fSJohan Hovold priv = usb_get_serial_port_data(port); 7934d5ef53fSJohan Hovold if (!priv) 7944d5ef53fSJohan Hovold return 0; 7954d5ef53fSJohan Hovold 7961ded7ea4SMing Lei /* reconfigure ch341 serial port after bus-reset */ 7972bfd1c96SGreg Kroah-Hartman ch341_configure(serial->dev, priv); 7981ded7ea4SMing Lei 799ce5e2928SJohan Hovold if (tty_port_initialized(&port->port)) { 800ce5e2928SJohan Hovold ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 801ce5e2928SJohan Hovold if (ret) { 802ce5e2928SJohan Hovold dev_err(&port->dev, "failed to submit interrupt urb: %d\n", 803ce5e2928SJohan Hovold ret); 804ce5e2928SJohan Hovold return ret; 805ce5e2928SJohan Hovold } 806a0467a96SJohan Hovold 807a0467a96SJohan Hovold ret = ch341_get_status(port->serial->dev, priv); 808a0467a96SJohan Hovold if (ret < 0) { 809a0467a96SJohan Hovold dev_err(&port->dev, "failed to read modem status: %d\n", 810a0467a96SJohan Hovold ret); 811a0467a96SJohan Hovold } 812ce5e2928SJohan Hovold } 813ce5e2928SJohan Hovold 814ce5e2928SJohan Hovold return usb_serial_generic_resume(serial); 8151ded7ea4SMing Lei } 8161ded7ea4SMing Lei 8176ce76104SFrank A Kingswood static struct usb_serial_driver ch341_device = { 8186ce76104SFrank A Kingswood .driver = { 8196ce76104SFrank A Kingswood .owner = THIS_MODULE, 8206ce76104SFrank A Kingswood .name = "ch341-uart", 8216ce76104SFrank A Kingswood }, 8226ce76104SFrank A Kingswood .id_table = id_table, 8236ce76104SFrank A Kingswood .num_ports = 1, 8246ce76104SFrank A Kingswood .open = ch341_open, 825335f8514SAlan Cox .dtr_rts = ch341_dtr_rts, 826335f8514SAlan Cox .carrier_raised = ch341_carrier_raised, 827664d5df9SWerner Cornelius .close = ch341_close, 8286ce76104SFrank A Kingswood .set_termios = ch341_set_termios, 829492896f0STim Small .break_ctl = ch341_break_ctl, 830664d5df9SWerner Cornelius .tiocmget = ch341_tiocmget, 831664d5df9SWerner Cornelius .tiocmset = ch341_tiocmset, 8325e409a26SJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 833664d5df9SWerner Cornelius .read_int_callback = ch341_read_int_callback, 834456c5be5SJohan Hovold .port_probe = ch341_port_probe, 835456c5be5SJohan Hovold .port_remove = ch341_port_remove, 8361c1eaba8SGreg Kroah-Hartman .reset_resume = ch341_reset_resume, 8376ce76104SFrank A Kingswood }; 8386ce76104SFrank A Kingswood 83908a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 84008a4f6bcSAlan Stern &ch341_device, NULL 84108a4f6bcSAlan Stern }; 84208a4f6bcSAlan Stern 84368e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 8446ce76104SFrank A Kingswood 845627cfa89SJohan Hovold MODULE_LICENSE("GPL v2"); 846