Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
0004  * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
0005  * Copyright 2009, Boris Hajduk <boris@hajduk.org>
0006  *
0007  * ch341.c implements a serial port driver for the Winchiphead CH341.
0008  *
0009  * The CH341 device can be used to implement an RS232 asynchronous
0010  * serial port, an IEEE-1284 parallel printer port or a memory-like
0011  * interface. In all cases the CH341 supports an I2C interface as well.
0012  * This driver only supports the asynchronous serial interface.
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/tty.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/usb.h>
0020 #include <linux/usb/serial.h>
0021 #include <linux/serial.h>
0022 #include <asm/unaligned.h>
0023 
0024 #define DEFAULT_BAUD_RATE 9600
0025 #define DEFAULT_TIMEOUT   1000
0026 
0027 /* flags for IO-Bits */
0028 #define CH341_BIT_RTS (1 << 6)
0029 #define CH341_BIT_DTR (1 << 5)
0030 
0031 /******************************/
0032 /* interrupt pipe definitions */
0033 /******************************/
0034 /* always 4 interrupt bytes */
0035 /* first irq byte normally 0x08 */
0036 /* second irq byte base 0x7d + below */
0037 /* third irq byte base 0x94 + below */
0038 /* fourth irq byte normally 0xee */
0039 
0040 /* second interrupt byte */
0041 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
0042 
0043 /* status returned in third interrupt answer byte, inverted in data
0044    from irq */
0045 #define CH341_BIT_CTS 0x01
0046 #define CH341_BIT_DSR 0x02
0047 #define CH341_BIT_RI  0x04
0048 #define CH341_BIT_DCD 0x08
0049 #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
0050 
0051 /* Break support - the information used to implement this was gleaned from
0052  * the Net/FreeBSD uchcom.c driver by Takanori Watanabe.  Domo arigato.
0053  */
0054 
0055 #define CH341_REQ_READ_VERSION 0x5F
0056 #define CH341_REQ_WRITE_REG    0x9A
0057 #define CH341_REQ_READ_REG     0x95
0058 #define CH341_REQ_SERIAL_INIT  0xA1
0059 #define CH341_REQ_MODEM_CTRL   0xA4
0060 
0061 #define CH341_REG_BREAK        0x05
0062 #define CH341_REG_PRESCALER    0x12
0063 #define CH341_REG_DIVISOR      0x13
0064 #define CH341_REG_LCR          0x18
0065 #define CH341_REG_LCR2         0x25
0066 
0067 #define CH341_NBREAK_BITS      0x01
0068 
0069 #define CH341_LCR_ENABLE_RX    0x80
0070 #define CH341_LCR_ENABLE_TX    0x40
0071 #define CH341_LCR_MARK_SPACE   0x20
0072 #define CH341_LCR_PAR_EVEN     0x10
0073 #define CH341_LCR_ENABLE_PAR   0x08
0074 #define CH341_LCR_STOP_BITS_2  0x04
0075 #define CH341_LCR_CS8          0x03
0076 #define CH341_LCR_CS7          0x02
0077 #define CH341_LCR_CS6          0x01
0078 #define CH341_LCR_CS5          0x00
0079 
0080 #define CH341_QUIRK_LIMITED_PRESCALER   BIT(0)
0081 #define CH341_QUIRK_SIMULATE_BREAK  BIT(1)
0082 
0083 static const struct usb_device_id id_table[] = {
0084     { USB_DEVICE(0x1a86, 0x5523) },
0085     { USB_DEVICE(0x1a86, 0x7522) },
0086     { USB_DEVICE(0x1a86, 0x7523) },
0087     { USB_DEVICE(0x2184, 0x0057) },
0088     { USB_DEVICE(0x4348, 0x5523) },
0089     { USB_DEVICE(0x9986, 0x7523) },
0090     { },
0091 };
0092 MODULE_DEVICE_TABLE(usb, id_table);
0093 
0094 struct ch341_private {
0095     spinlock_t lock; /* access lock */
0096     unsigned baud_rate; /* set baud rate */
0097     u8 mcr;
0098     u8 msr;
0099     u8 lcr;
0100 
0101     unsigned long quirks;
0102     u8 version;
0103 
0104     unsigned long break_end;
0105 };
0106 
0107 static void ch341_set_termios(struct tty_struct *tty,
0108                   struct usb_serial_port *port,
0109                   struct ktermios *old_termios);
0110 
0111 static int ch341_control_out(struct usb_device *dev, u8 request,
0112                  u16 value, u16 index)
0113 {
0114     int r;
0115 
0116     dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
0117         request, value, index);
0118 
0119     r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
0120                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0121                 value, index, NULL, 0, DEFAULT_TIMEOUT);
0122     if (r < 0)
0123         dev_err(&dev->dev, "failed to send control message: %d\n", r);
0124 
0125     return r;
0126 }
0127 
0128 static int ch341_control_in(struct usb_device *dev,
0129                 u8 request, u16 value, u16 index,
0130                 char *buf, unsigned bufsize)
0131 {
0132     int r;
0133 
0134     dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
0135         request, value, index, bufsize);
0136 
0137     r = usb_control_msg_recv(dev, 0, request,
0138                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0139                  value, index, buf, bufsize, DEFAULT_TIMEOUT,
0140                  GFP_KERNEL);
0141     if (r) {
0142         dev_err(&dev->dev, "failed to receive control message: %d\n",
0143             r);
0144         return r;
0145     }
0146 
0147     return 0;
0148 }
0149 
0150 #define CH341_CLKRATE       48000000
0151 #define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact)))
0152 #define CH341_MIN_RATE(ps)  (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512))
0153 
0154 static const speed_t ch341_min_rates[] = {
0155     CH341_MIN_RATE(0),
0156     CH341_MIN_RATE(1),
0157     CH341_MIN_RATE(2),
0158     CH341_MIN_RATE(3),
0159 };
0160 
0161 /* Supported range is 46 to 3000000 bps. */
0162 #define CH341_MIN_BPS   DIV_ROUND_UP(CH341_CLKRATE, CH341_CLK_DIV(0, 0) * 256)
0163 #define CH341_MAX_BPS   (CH341_CLKRATE / (CH341_CLK_DIV(3, 0) * 2))
0164 
0165 /*
0166  * The device line speed is given by the following equation:
0167  *
0168  *  baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
0169  *
0170  *      0 <= ps <= 3,
0171  *      0 <= fact <= 1,
0172  *      2 <= div <= 256 if fact = 0, or
0173  *      9 <= div <= 256 if fact = 1
0174  */
0175 static int ch341_get_divisor(struct ch341_private *priv, speed_t speed)
0176 {
0177     unsigned int fact, div, clk_div;
0178     bool force_fact0 = false;
0179     int ps;
0180 
0181     /*
0182      * Clamp to supported range, this makes the (ps < 0) and (div < 2)
0183      * sanity checks below redundant.
0184      */
0185     speed = clamp_val(speed, CH341_MIN_BPS, CH341_MAX_BPS);
0186 
0187     /*
0188      * Start with highest possible base clock (fact = 1) that will give a
0189      * divisor strictly less than 512.
0190      */
0191     fact = 1;
0192     for (ps = 3; ps >= 0; ps--) {
0193         if (speed > ch341_min_rates[ps])
0194             break;
0195     }
0196 
0197     if (ps < 0)
0198         return -EINVAL;
0199 
0200     /* Determine corresponding divisor, rounding down. */
0201     clk_div = CH341_CLK_DIV(ps, fact);
0202     div = CH341_CLKRATE / (clk_div * speed);
0203 
0204     /* Some devices require a lower base clock if ps < 3. */
0205     if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER))
0206         force_fact0 = true;
0207 
0208     /* Halve base clock (fact = 0) if required. */
0209     if (div < 9 || div > 255 || force_fact0) {
0210         div /= 2;
0211         clk_div *= 2;
0212         fact = 0;
0213     }
0214 
0215     if (div < 2)
0216         return -EINVAL;
0217 
0218     /*
0219      * Pick next divisor if resulting rate is closer to the requested one,
0220      * scale up to avoid rounding errors on low rates.
0221      */
0222     if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >=
0223             16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
0224         div++;
0225 
0226     /*
0227      * Prefer lower base clock (fact = 0) if even divisor.
0228      *
0229      * Note that this makes the receiver more tolerant to errors.
0230      */
0231     if (fact == 1 && div % 2 == 0) {
0232         div /= 2;
0233         fact = 0;
0234     }
0235 
0236     return (0x100 - div) << 8 | fact << 2 | ps;
0237 }
0238 
0239 static int ch341_set_baudrate_lcr(struct usb_device *dev,
0240                   struct ch341_private *priv,
0241                   speed_t baud_rate, u8 lcr)
0242 {
0243     int val;
0244     int r;
0245 
0246     if (!baud_rate)
0247         return -EINVAL;
0248 
0249     val = ch341_get_divisor(priv, baud_rate);
0250     if (val < 0)
0251         return -EINVAL;
0252 
0253     /*
0254      * CH341A buffers data until a full endpoint-size packet (32 bytes)
0255      * has been received unless bit 7 is set.
0256      *
0257      * At least one device with version 0x27 appears to have this bit
0258      * inverted.
0259      */
0260     if (priv->version > 0x27)
0261         val |= BIT(7);
0262 
0263     r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
0264                   CH341_REG_DIVISOR << 8 | CH341_REG_PRESCALER,
0265                   val);
0266     if (r)
0267         return r;
0268 
0269     /*
0270      * Chip versions before version 0x30 as read using
0271      * CH341_REQ_READ_VERSION used separate registers for line control
0272      * (stop bits, parity and word length). Version 0x30 and above use
0273      * CH341_REG_LCR only and CH341_REG_LCR2 is always set to zero.
0274      */
0275     if (priv->version < 0x30)
0276         return 0;
0277 
0278     r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
0279                   CH341_REG_LCR2 << 8 | CH341_REG_LCR, lcr);
0280     if (r)
0281         return r;
0282 
0283     return r;
0284 }
0285 
0286 static int ch341_set_handshake(struct usb_device *dev, u8 control)
0287 {
0288     return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
0289 }
0290 
0291 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
0292 {
0293     const unsigned int size = 2;
0294     u8 buffer[2];
0295     int r;
0296     unsigned long flags;
0297 
0298     r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
0299     if (r)
0300         return r;
0301 
0302     spin_lock_irqsave(&priv->lock, flags);
0303     priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
0304     spin_unlock_irqrestore(&priv->lock, flags);
0305 
0306     return 0;
0307 }
0308 
0309 /* -------------------------------------------------------------------------- */
0310 
0311 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
0312 {
0313     const unsigned int size = 2;
0314     u8 buffer[2];
0315     int r;
0316 
0317     /* expect two bytes 0x27 0x00 */
0318     r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
0319     if (r)
0320         return r;
0321 
0322     priv->version = buffer[0];
0323     dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version);
0324 
0325     r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
0326     if (r < 0)
0327         return r;
0328 
0329     r = ch341_set_baudrate_lcr(dev, priv, priv->baud_rate, priv->lcr);
0330     if (r < 0)
0331         return r;
0332 
0333     r = ch341_set_handshake(dev, priv->mcr);
0334     if (r < 0)
0335         return r;
0336 
0337     return 0;
0338 }
0339 
0340 static int ch341_detect_quirks(struct usb_serial_port *port)
0341 {
0342     struct ch341_private *priv = usb_get_serial_port_data(port);
0343     struct usb_device *udev = port->serial->dev;
0344     const unsigned int size = 2;
0345     unsigned long quirks = 0;
0346     u8 buffer[2];
0347     int r;
0348 
0349     /*
0350      * A subset of CH34x devices does not support all features. The
0351      * prescaler is limited and there is no support for sending a RS232
0352      * break condition. A read failure when trying to set up the latter is
0353      * used to detect these devices.
0354      */
0355     r = usb_control_msg_recv(udev, 0, CH341_REQ_READ_REG,
0356                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0357                  CH341_REG_BREAK, 0, &buffer, size,
0358                  DEFAULT_TIMEOUT, GFP_KERNEL);
0359     if (r == -EPIPE) {
0360         dev_info(&port->dev, "break control not supported, using simulated break\n");
0361         quirks = CH341_QUIRK_LIMITED_PRESCALER | CH341_QUIRK_SIMULATE_BREAK;
0362         r = 0;
0363     } else if (r) {
0364         dev_err(&port->dev, "failed to read break control: %d\n", r);
0365     }
0366 
0367     if (quirks) {
0368         dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks);
0369         priv->quirks |= quirks;
0370     }
0371 
0372     return r;
0373 }
0374 
0375 static int ch341_port_probe(struct usb_serial_port *port)
0376 {
0377     struct ch341_private *priv;
0378     int r;
0379 
0380     priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
0381     if (!priv)
0382         return -ENOMEM;
0383 
0384     spin_lock_init(&priv->lock);
0385     priv->baud_rate = DEFAULT_BAUD_RATE;
0386     /*
0387      * Some CH340 devices appear unable to change the initial LCR
0388      * settings, so set a sane 8N1 default.
0389      */
0390     priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
0391 
0392     r = ch341_configure(port->serial->dev, priv);
0393     if (r < 0)
0394         goto error;
0395 
0396     usb_set_serial_port_data(port, priv);
0397 
0398     r = ch341_detect_quirks(port);
0399     if (r < 0)
0400         goto error;
0401 
0402     return 0;
0403 
0404 error:  kfree(priv);
0405     return r;
0406 }
0407 
0408 static void ch341_port_remove(struct usb_serial_port *port)
0409 {
0410     struct ch341_private *priv;
0411 
0412     priv = usb_get_serial_port_data(port);
0413     kfree(priv);
0414 }
0415 
0416 static int ch341_carrier_raised(struct usb_serial_port *port)
0417 {
0418     struct ch341_private *priv = usb_get_serial_port_data(port);
0419     if (priv->msr & CH341_BIT_DCD)
0420         return 1;
0421     return 0;
0422 }
0423 
0424 static void ch341_dtr_rts(struct usb_serial_port *port, int on)
0425 {
0426     struct ch341_private *priv = usb_get_serial_port_data(port);
0427     unsigned long flags;
0428 
0429     /* drop DTR and RTS */
0430     spin_lock_irqsave(&priv->lock, flags);
0431     if (on)
0432         priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
0433     else
0434         priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
0435     spin_unlock_irqrestore(&priv->lock, flags);
0436     ch341_set_handshake(port->serial->dev, priv->mcr);
0437 }
0438 
0439 static void ch341_close(struct usb_serial_port *port)
0440 {
0441     usb_serial_generic_close(port);
0442     usb_kill_urb(port->interrupt_in_urb);
0443 }
0444 
0445 
0446 /* open this device, set default parameters */
0447 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
0448 {
0449     struct ch341_private *priv = usb_get_serial_port_data(port);
0450     int r;
0451 
0452     if (tty)
0453         ch341_set_termios(tty, port, NULL);
0454 
0455     dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
0456     r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0457     if (r) {
0458         dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
0459             __func__, r);
0460         return r;
0461     }
0462 
0463     r = ch341_get_status(port->serial->dev, priv);
0464     if (r < 0) {
0465         dev_err(&port->dev, "failed to read modem status: %d\n", r);
0466         goto err_kill_interrupt_urb;
0467     }
0468 
0469     r = usb_serial_generic_open(tty, port);
0470     if (r)
0471         goto err_kill_interrupt_urb;
0472 
0473     return 0;
0474 
0475 err_kill_interrupt_urb:
0476     usb_kill_urb(port->interrupt_in_urb);
0477 
0478     return r;
0479 }
0480 
0481 /* Old_termios contains the original termios settings and
0482  * tty->termios contains the new setting to be used.
0483  */
0484 static void ch341_set_termios(struct tty_struct *tty,
0485         struct usb_serial_port *port, struct ktermios *old_termios)
0486 {
0487     struct ch341_private *priv = usb_get_serial_port_data(port);
0488     unsigned baud_rate;
0489     unsigned long flags;
0490     u8 lcr;
0491     int r;
0492 
0493     /* redundant changes may cause the chip to lose bytes */
0494     if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
0495         return;
0496 
0497     baud_rate = tty_get_baud_rate(tty);
0498 
0499     lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
0500 
0501     switch (C_CSIZE(tty)) {
0502     case CS5:
0503         lcr |= CH341_LCR_CS5;
0504         break;
0505     case CS6:
0506         lcr |= CH341_LCR_CS6;
0507         break;
0508     case CS7:
0509         lcr |= CH341_LCR_CS7;
0510         break;
0511     case CS8:
0512         lcr |= CH341_LCR_CS8;
0513         break;
0514     }
0515 
0516     if (C_PARENB(tty)) {
0517         lcr |= CH341_LCR_ENABLE_PAR;
0518         if (C_PARODD(tty) == 0)
0519             lcr |= CH341_LCR_PAR_EVEN;
0520         if (C_CMSPAR(tty))
0521             lcr |= CH341_LCR_MARK_SPACE;
0522     }
0523 
0524     if (C_CSTOPB(tty))
0525         lcr |= CH341_LCR_STOP_BITS_2;
0526 
0527     if (baud_rate) {
0528         priv->baud_rate = baud_rate;
0529 
0530         r = ch341_set_baudrate_lcr(port->serial->dev, priv,
0531                        priv->baud_rate, lcr);
0532         if (r < 0 && old_termios) {
0533             priv->baud_rate = tty_termios_baud_rate(old_termios);
0534             tty_termios_copy_hw(&tty->termios, old_termios);
0535         } else if (r == 0) {
0536             priv->lcr = lcr;
0537         }
0538     }
0539 
0540     spin_lock_irqsave(&priv->lock, flags);
0541     if (C_BAUD(tty) == B0)
0542         priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
0543     else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
0544         priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
0545     spin_unlock_irqrestore(&priv->lock, flags);
0546 
0547     ch341_set_handshake(port->serial->dev, priv->mcr);
0548 }
0549 
0550 /*
0551  * A subset of all CH34x devices don't support a real break condition and
0552  * reading CH341_REG_BREAK fails (see also ch341_detect_quirks). This function
0553  * simulates a break condition by lowering the baud rate to the minimum
0554  * supported by the hardware upon enabling the break condition and sending
0555  * a NUL byte.
0556  *
0557  * Incoming data is corrupted while the break condition is being simulated.
0558  *
0559  * Normally the duration of the break condition can be controlled individually
0560  * by userspace using TIOCSBRK and TIOCCBRK or by passing an argument to
0561  * TCSBRKP. Due to how the simulation is implemented the duration can't be
0562  * controlled. The duration is always about (1s / 46bd * 9bit) = 196ms.
0563  */
0564 static void ch341_simulate_break(struct tty_struct *tty, int break_state)
0565 {
0566     struct usb_serial_port *port = tty->driver_data;
0567     struct ch341_private *priv = usb_get_serial_port_data(port);
0568     unsigned long now, delay;
0569     int r;
0570 
0571     if (break_state != 0) {
0572         dev_dbg(&port->dev, "enter break state requested\n");
0573 
0574         r = ch341_set_baudrate_lcr(port->serial->dev, priv,
0575                 CH341_MIN_BPS,
0576                 CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8);
0577         if (r < 0) {
0578             dev_err(&port->dev,
0579                 "failed to change baud rate to %u: %d\n",
0580                 CH341_MIN_BPS, r);
0581             goto restore;
0582         }
0583 
0584         r = tty_put_char(tty, '\0');
0585         if (r < 0) {
0586             dev_err(&port->dev,
0587                 "failed to write NUL byte for simulated break condition: %d\n",
0588                 r);
0589             goto restore;
0590         }
0591 
0592         /*
0593          * Compute expected transmission duration including safety
0594          * margin. The original baud rate is only restored after the
0595          * computed point in time.
0596          *
0597          * 11 bits = 1 start, 8 data, 1 stop, 1 margin
0598          */
0599         priv->break_end = jiffies + (11 * HZ / CH341_MIN_BPS);
0600 
0601         return;
0602     }
0603 
0604     dev_dbg(&port->dev, "leave break state requested\n");
0605 
0606     now = jiffies;
0607 
0608     if (time_before(now, priv->break_end)) {
0609         /* Wait until NUL byte is written */
0610         delay = priv->break_end - now;
0611         dev_dbg(&port->dev,
0612             "wait %d ms while transmitting NUL byte at %u baud\n",
0613             jiffies_to_msecs(delay), CH341_MIN_BPS);
0614         schedule_timeout_interruptible(delay);
0615     }
0616 
0617 restore:
0618     /* Restore original baud rate */
0619     r = ch341_set_baudrate_lcr(port->serial->dev, priv, priv->baud_rate,
0620                    priv->lcr);
0621     if (r < 0)
0622         dev_err(&port->dev,
0623             "restoring original baud rate of %u failed: %d\n",
0624             priv->baud_rate, r);
0625 }
0626 
0627 static void ch341_break_ctl(struct tty_struct *tty, int break_state)
0628 {
0629     const uint16_t ch341_break_reg =
0630             ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
0631     struct usb_serial_port *port = tty->driver_data;
0632     struct ch341_private *priv = usb_get_serial_port_data(port);
0633     int r;
0634     uint16_t reg_contents;
0635     uint8_t break_reg[2];
0636 
0637     if (priv->quirks & CH341_QUIRK_SIMULATE_BREAK) {
0638         ch341_simulate_break(tty, break_state);
0639         return;
0640     }
0641 
0642     r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
0643             ch341_break_reg, 0, break_reg, 2);
0644     if (r) {
0645         dev_err(&port->dev, "%s - USB control read error (%d)\n",
0646                 __func__, r);
0647         return;
0648     }
0649     dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
0650         __func__, break_reg[0], break_reg[1]);
0651     if (break_state != 0) {
0652         dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
0653         break_reg[0] &= ~CH341_NBREAK_BITS;
0654         break_reg[1] &= ~CH341_LCR_ENABLE_TX;
0655     } else {
0656         dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
0657         break_reg[0] |= CH341_NBREAK_BITS;
0658         break_reg[1] |= CH341_LCR_ENABLE_TX;
0659     }
0660     dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
0661         __func__, break_reg[0], break_reg[1]);
0662     reg_contents = get_unaligned_le16(break_reg);
0663     r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
0664             ch341_break_reg, reg_contents);
0665     if (r < 0)
0666         dev_err(&port->dev, "%s - USB control write error (%d)\n",
0667                 __func__, r);
0668 }
0669 
0670 static int ch341_tiocmset(struct tty_struct *tty,
0671               unsigned int set, unsigned int clear)
0672 {
0673     struct usb_serial_port *port = tty->driver_data;
0674     struct ch341_private *priv = usb_get_serial_port_data(port);
0675     unsigned long flags;
0676     u8 control;
0677 
0678     spin_lock_irqsave(&priv->lock, flags);
0679     if (set & TIOCM_RTS)
0680         priv->mcr |= CH341_BIT_RTS;
0681     if (set & TIOCM_DTR)
0682         priv->mcr |= CH341_BIT_DTR;
0683     if (clear & TIOCM_RTS)
0684         priv->mcr &= ~CH341_BIT_RTS;
0685     if (clear & TIOCM_DTR)
0686         priv->mcr &= ~CH341_BIT_DTR;
0687     control = priv->mcr;
0688     spin_unlock_irqrestore(&priv->lock, flags);
0689 
0690     return ch341_set_handshake(port->serial->dev, control);
0691 }
0692 
0693 static void ch341_update_status(struct usb_serial_port *port,
0694                     unsigned char *data, size_t len)
0695 {
0696     struct ch341_private *priv = usb_get_serial_port_data(port);
0697     struct tty_struct *tty;
0698     unsigned long flags;
0699     u8 status;
0700     u8 delta;
0701 
0702     if (len < 4)
0703         return;
0704 
0705     status = ~data[2] & CH341_BITS_MODEM_STAT;
0706 
0707     spin_lock_irqsave(&priv->lock, flags);
0708     delta = status ^ priv->msr;
0709     priv->msr = status;
0710     spin_unlock_irqrestore(&priv->lock, flags);
0711 
0712     if (data[1] & CH341_MULT_STAT)
0713         dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
0714 
0715     if (!delta)
0716         return;
0717 
0718     if (delta & CH341_BIT_CTS)
0719         port->icount.cts++;
0720     if (delta & CH341_BIT_DSR)
0721         port->icount.dsr++;
0722     if (delta & CH341_BIT_RI)
0723         port->icount.rng++;
0724     if (delta & CH341_BIT_DCD) {
0725         port->icount.dcd++;
0726         tty = tty_port_tty_get(&port->port);
0727         if (tty) {
0728             usb_serial_handle_dcd_change(port, tty,
0729                         status & CH341_BIT_DCD);
0730             tty_kref_put(tty);
0731         }
0732     }
0733 
0734     wake_up_interruptible(&port->port.delta_msr_wait);
0735 }
0736 
0737 static void ch341_read_int_callback(struct urb *urb)
0738 {
0739     struct usb_serial_port *port = urb->context;
0740     unsigned char *data = urb->transfer_buffer;
0741     unsigned int len = urb->actual_length;
0742     int status;
0743 
0744     switch (urb->status) {
0745     case 0:
0746         /* success */
0747         break;
0748     case -ECONNRESET:
0749     case -ENOENT:
0750     case -ESHUTDOWN:
0751         /* this urb is terminated, clean up */
0752         dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
0753             __func__, urb->status);
0754         return;
0755     default:
0756         dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
0757             __func__, urb->status);
0758         goto exit;
0759     }
0760 
0761     usb_serial_debug_data(&port->dev, __func__, len, data);
0762     ch341_update_status(port, data, len);
0763 exit:
0764     status = usb_submit_urb(urb, GFP_ATOMIC);
0765     if (status) {
0766         dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
0767             __func__, status);
0768     }
0769 }
0770 
0771 static int ch341_tiocmget(struct tty_struct *tty)
0772 {
0773     struct usb_serial_port *port = tty->driver_data;
0774     struct ch341_private *priv = usb_get_serial_port_data(port);
0775     unsigned long flags;
0776     u8 mcr;
0777     u8 status;
0778     unsigned int result;
0779 
0780     spin_lock_irqsave(&priv->lock, flags);
0781     mcr = priv->mcr;
0782     status = priv->msr;
0783     spin_unlock_irqrestore(&priv->lock, flags);
0784 
0785     result = ((mcr & CH341_BIT_DTR)     ? TIOCM_DTR : 0)
0786           | ((mcr & CH341_BIT_RTS)  ? TIOCM_RTS : 0)
0787           | ((status & CH341_BIT_CTS)   ? TIOCM_CTS : 0)
0788           | ((status & CH341_BIT_DSR)   ? TIOCM_DSR : 0)
0789           | ((status & CH341_BIT_RI)    ? TIOCM_RI  : 0)
0790           | ((status & CH341_BIT_DCD)   ? TIOCM_CD  : 0);
0791 
0792     dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
0793 
0794     return result;
0795 }
0796 
0797 static int ch341_reset_resume(struct usb_serial *serial)
0798 {
0799     struct usb_serial_port *port = serial->port[0];
0800     struct ch341_private *priv;
0801     int ret;
0802 
0803     priv = usb_get_serial_port_data(port);
0804     if (!priv)
0805         return 0;
0806 
0807     /* reconfigure ch341 serial port after bus-reset */
0808     ch341_configure(serial->dev, priv);
0809 
0810     if (tty_port_initialized(&port->port)) {
0811         ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
0812         if (ret) {
0813             dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
0814                 ret);
0815             return ret;
0816         }
0817 
0818         ret = ch341_get_status(port->serial->dev, priv);
0819         if (ret < 0) {
0820             dev_err(&port->dev, "failed to read modem status: %d\n",
0821                 ret);
0822         }
0823     }
0824 
0825     return usb_serial_generic_resume(serial);
0826 }
0827 
0828 static struct usb_serial_driver ch341_device = {
0829     .driver = {
0830         .owner  = THIS_MODULE,
0831         .name   = "ch341-uart",
0832     },
0833     .id_table          = id_table,
0834     .num_ports         = 1,
0835     .open              = ch341_open,
0836     .dtr_rts       = ch341_dtr_rts,
0837     .carrier_raised    = ch341_carrier_raised,
0838     .close             = ch341_close,
0839     .set_termios       = ch341_set_termios,
0840     .break_ctl         = ch341_break_ctl,
0841     .tiocmget          = ch341_tiocmget,
0842     .tiocmset          = ch341_tiocmset,
0843     .tiocmiwait        = usb_serial_generic_tiocmiwait,
0844     .read_int_callback = ch341_read_int_callback,
0845     .port_probe        = ch341_port_probe,
0846     .port_remove       = ch341_port_remove,
0847     .reset_resume      = ch341_reset_resume,
0848 };
0849 
0850 static struct usb_serial_driver * const serial_drivers[] = {
0851     &ch341_device, NULL
0852 };
0853 
0854 module_usb_serial_driver(serial_drivers, id_table);
0855 
0856 MODULE_LICENSE("GPL v2");