Lines Matching +full:ps +full:- +full:speed
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
4 * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
10 * serial port, an IEEE-1284 parallel printer port or a memory-like
27 /* flags for IO-Bits */
51 /* Break support - the information used to implement this was gleaned from
116 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__, in ch341_control_out()
123 dev_err(&dev->dev, "failed to send control message: %d\n", r); in ch341_control_out()
134 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__, in ch341_control_in()
142 dev_err(&dev->dev, "failed to receive control message: %d\n", in ch341_control_in()
151 #define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact))) argument
152 #define CH341_MIN_RATE(ps) (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512)) argument
166 * The device line speed is given by the following equation:
168 * baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
170 * 0 <= ps <= 3,
175 static int ch341_get_divisor(struct ch341_private *priv, speed_t speed) in ch341_get_divisor() argument
179 int ps; in ch341_get_divisor() local
182 * Clamp to supported range, this makes the (ps < 0) and (div < 2) in ch341_get_divisor()
185 speed = clamp_val(speed, CH341_MIN_BPS, CH341_MAX_BPS); in ch341_get_divisor()
192 for (ps = 3; ps >= 0; ps--) { in ch341_get_divisor()
193 if (speed > ch341_min_rates[ps]) in ch341_get_divisor()
197 if (ps < 0) in ch341_get_divisor()
198 return -EINVAL; in ch341_get_divisor()
201 clk_div = CH341_CLK_DIV(ps, fact); in ch341_get_divisor()
202 div = CH341_CLKRATE / (clk_div * speed); in ch341_get_divisor()
204 /* Some devices require a lower base clock if ps < 3. */ in ch341_get_divisor()
205 if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER)) in ch341_get_divisor()
216 return -EINVAL; in ch341_get_divisor()
222 if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >= in ch341_get_divisor()
223 16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1))) in ch341_get_divisor()
236 return (0x100 - div) << 8 | fact << 2 | ps; in ch341_get_divisor()
247 return -EINVAL; in ch341_set_baudrate_lcr()
251 return -EINVAL; in ch341_set_baudrate_lcr()
254 * CH341A buffers data until a full endpoint-size packet (32 bytes) in ch341_set_baudrate_lcr()
260 if (priv->version > 0x27) in ch341_set_baudrate_lcr()
275 if (priv->version < 0x30) in ch341_set_baudrate_lcr()
302 spin_lock_irqsave(&priv->lock, flags); in ch341_get_status()
303 priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT; in ch341_get_status()
304 spin_unlock_irqrestore(&priv->lock, flags); in ch341_get_status()
309 /* -------------------------------------------------------------------------- */
322 priv->version = buffer[0]; in ch341_configure()
323 dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version); in ch341_configure()
329 r = ch341_set_baudrate_lcr(dev, priv, priv->baud_rate, priv->lcr); in ch341_configure()
333 r = ch341_set_handshake(dev, priv->mcr); in ch341_configure()
343 struct usb_device *udev = port->serial->dev; in ch341_detect_quirks()
359 if (r == -EPIPE) { in ch341_detect_quirks()
360 dev_info(&port->dev, "break control not supported, using simulated break\n"); in ch341_detect_quirks()
364 dev_err(&port->dev, "failed to read break control: %d\n", r); in ch341_detect_quirks()
368 dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks); in ch341_detect_quirks()
369 priv->quirks |= quirks; in ch341_detect_quirks()
382 return -ENOMEM; in ch341_port_probe()
384 spin_lock_init(&priv->lock); in ch341_port_probe()
385 priv->baud_rate = DEFAULT_BAUD_RATE; in ch341_port_probe()
390 priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8; in ch341_port_probe()
392 r = ch341_configure(port->serial->dev, priv); in ch341_port_probe()
419 if (priv->msr & CH341_BIT_DCD) in ch341_carrier_raised()
430 spin_lock_irqsave(&priv->lock, flags); in ch341_dtr_rts()
432 priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR; in ch341_dtr_rts()
434 priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR); in ch341_dtr_rts()
435 spin_unlock_irqrestore(&priv->lock, flags); in ch341_dtr_rts()
436 ch341_set_handshake(port->serial->dev, priv->mcr); in ch341_dtr_rts()
442 usb_kill_urb(port->interrupt_in_urb); in ch341_close()
455 dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__); in ch341_open()
456 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); in ch341_open()
458 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", in ch341_open()
463 r = ch341_get_status(port->serial->dev, priv); in ch341_open()
465 dev_err(&port->dev, "failed to read modem status: %d\n", r); in ch341_open()
476 usb_kill_urb(port->interrupt_in_urb); in ch341_open()
482 * tty->termios contains the new setting to be used.
495 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) in ch341_set_termios()
529 priv->baud_rate = baud_rate; in ch341_set_termios()
531 r = ch341_set_baudrate_lcr(port->serial->dev, priv, in ch341_set_termios()
532 priv->baud_rate, lcr); in ch341_set_termios()
534 priv->baud_rate = tty_termios_baud_rate(old_termios); in ch341_set_termios()
535 tty_termios_copy_hw(&tty->termios, old_termios); in ch341_set_termios()
537 priv->lcr = lcr; in ch341_set_termios()
541 spin_lock_irqsave(&priv->lock, flags); in ch341_set_termios()
543 priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS); in ch341_set_termios()
544 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0) in ch341_set_termios()
545 priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS); in ch341_set_termios()
546 spin_unlock_irqrestore(&priv->lock, flags); in ch341_set_termios()
548 ch341_set_handshake(port->serial->dev, priv->mcr); in ch341_set_termios()
567 struct usb_serial_port *port = tty->driver_data; in ch341_simulate_break()
573 dev_dbg(&port->dev, "enter break state requested\n"); in ch341_simulate_break()
575 r = ch341_set_baudrate_lcr(port->serial->dev, priv, in ch341_simulate_break()
579 dev_err(&port->dev, in ch341_simulate_break()
587 dev_err(&port->dev, in ch341_simulate_break()
600 priv->break_end = jiffies + (11 * HZ / CH341_MIN_BPS); in ch341_simulate_break()
605 dev_dbg(&port->dev, "leave break state requested\n"); in ch341_simulate_break()
609 if (time_before(now, priv->break_end)) { in ch341_simulate_break()
611 delay = priv->break_end - now; in ch341_simulate_break()
612 dev_dbg(&port->dev, in ch341_simulate_break()
621 r2 = ch341_set_baudrate_lcr(port->serial->dev, priv, priv->baud_rate, in ch341_simulate_break()
622 priv->lcr); in ch341_simulate_break()
624 dev_err(&port->dev, in ch341_simulate_break()
626 priv->baud_rate, r2); in ch341_simulate_break()
637 struct usb_serial_port *port = tty->driver_data; in ch341_break_ctl()
643 if (priv->quirks & CH341_QUIRK_SIMULATE_BREAK) in ch341_break_ctl()
646 r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG, in ch341_break_ctl()
649 dev_err(&port->dev, "%s - USB control read error (%d)\n", in ch341_break_ctl()
652 r = -EIO; in ch341_break_ctl()
655 dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n", in ch341_break_ctl()
658 dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__); in ch341_break_ctl()
662 dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__); in ch341_break_ctl()
666 dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n", in ch341_break_ctl()
669 r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG, in ch341_break_ctl()
672 dev_err(&port->dev, "%s - USB control write error (%d)\n", in ch341_break_ctl()
683 struct usb_serial_port *port = tty->driver_data; in ch341_tiocmset()
688 spin_lock_irqsave(&priv->lock, flags); in ch341_tiocmset()
690 priv->mcr |= CH341_BIT_RTS; in ch341_tiocmset()
692 priv->mcr |= CH341_BIT_DTR; in ch341_tiocmset()
694 priv->mcr &= ~CH341_BIT_RTS; in ch341_tiocmset()
696 priv->mcr &= ~CH341_BIT_DTR; in ch341_tiocmset()
697 control = priv->mcr; in ch341_tiocmset()
698 spin_unlock_irqrestore(&priv->lock, flags); in ch341_tiocmset()
700 return ch341_set_handshake(port->serial->dev, control); in ch341_tiocmset()
717 spin_lock_irqsave(&priv->lock, flags); in ch341_update_status()
718 delta = status ^ priv->msr; in ch341_update_status()
719 priv->msr = status; in ch341_update_status()
720 spin_unlock_irqrestore(&priv->lock, flags); in ch341_update_status()
723 dev_dbg(&port->dev, "%s - multiple status change\n", __func__); in ch341_update_status()
729 port->icount.cts++; in ch341_update_status()
731 port->icount.dsr++; in ch341_update_status()
733 port->icount.rng++; in ch341_update_status()
735 port->icount.dcd++; in ch341_update_status()
736 tty = tty_port_tty_get(&port->port); in ch341_update_status()
744 wake_up_interruptible(&port->port.delta_msr_wait); in ch341_update_status()
749 struct usb_serial_port *port = urb->context; in ch341_read_int_callback()
750 unsigned char *data = urb->transfer_buffer; in ch341_read_int_callback()
751 unsigned int len = urb->actual_length; in ch341_read_int_callback()
754 switch (urb->status) { in ch341_read_int_callback()
758 case -ECONNRESET: in ch341_read_int_callback()
759 case -ENOENT: in ch341_read_int_callback()
760 case -ESHUTDOWN: in ch341_read_int_callback()
762 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", in ch341_read_int_callback()
763 __func__, urb->status); in ch341_read_int_callback()
766 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", in ch341_read_int_callback()
767 __func__, urb->status); in ch341_read_int_callback()
771 usb_serial_debug_data(&port->dev, __func__, len, data); in ch341_read_int_callback()
776 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n", in ch341_read_int_callback()
783 struct usb_serial_port *port = tty->driver_data; in ch341_tiocmget()
790 spin_lock_irqsave(&priv->lock, flags); in ch341_tiocmget()
791 mcr = priv->mcr; in ch341_tiocmget()
792 status = priv->msr; in ch341_tiocmget()
793 spin_unlock_irqrestore(&priv->lock, flags); in ch341_tiocmget()
802 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); in ch341_tiocmget()
809 struct usb_serial_port *port = serial->port[0]; in ch341_reset_resume()
817 /* reconfigure ch341 serial port after bus-reset */ in ch341_reset_resume()
818 ch341_configure(serial->dev, priv); in ch341_reset_resume()
820 if (tty_port_initialized(&port->port)) { in ch341_reset_resume()
821 ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); in ch341_reset_resume()
823 dev_err(&port->dev, "failed to submit interrupt urb: %d\n", in ch341_reset_resume()
828 ret = ch341_get_status(port->serial->dev, priv); in ch341_reset_resume()
830 dev_err(&port->dev, "failed to read modem status: %d\n", in ch341_reset_resume()
840 .name = "ch341-uart",