Re: [RESEND PATCH v4 2/3] usb: serial: xr_serial: Add gpiochip support

From: Manivannan Sadhasivam
Date: Sun Jul 26 2020 - 11:52:32 EST


On Wed, Jul 01, 2020 at 03:02:06PM +0200, Johan Hovold wrote:
> On Sun, Jun 07, 2020 at 09:53:49PM +0530, Manivannan Sadhasivam wrote:
> > Add gpiochip support for Maxlinear/Exar USB to serial converter
> > for controlling the available gpios.
> >
> > Inspired from cp210x usb to serial converter driver.
> >
> > Cc: Linus Walleij <linus.walleij@xxxxxxxxxx>
> > Cc: linux-gpio@xxxxxxxxxxxxxxx
> > Reviewed-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
> > Signed-off-by: Manivannan Sadhasivam <mani@xxxxxxxxxx>
> > ---
> > drivers/usb/serial/xr_serial.c | 209 ++++++++++++++++++++++++++++++++-
> > 1 file changed, 208 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/usb/serial/xr_serial.c b/drivers/usb/serial/xr_serial.c
> > index bb7df79cc129..2240fbc9ea7f 100644
> > --- a/drivers/usb/serial/xr_serial.c
> > +++ b/drivers/usb/serial/xr_serial.c
> > @@ -10,6 +10,7 @@
> > * Copyright (c) 2020 Manivannan Sadhasivam <mani@xxxxxxxxxx>
> > */
> >
> > +#include <linux/gpio/driver.h>
> > #include <linux/kernel.h>
> > #include <linux/module.h>
> > #include <linux/slab.h>
> > @@ -17,6 +18,18 @@
> > #include <linux/usb.h>
> > #include <linux/usb/serial.h>
> >
> > +#ifdef CONFIG_GPIOLIB
> > +enum gpio_pins {
> > + GPIO_RI = 0,
> > + GPIO_CD,
> > + GPIO_DSR,
> > + GPIO_DTR,
> > + GPIO_CTS,
> > + GPIO_RTS,
> > + GPIO_MAX,
> > +};
> > +#endif
>
> Try to avoid littering the driver with GPIOLIB ifdefs. One or two is
> fine, but no more even if it means declaring an unused type. Add
> stubbed out helpers where appropriate.
>
> > +
> > static void xr_set_termios(struct tty_struct *tty,
> > struct usb_serial_port *port,
> > struct ktermios *old_termios);
> > @@ -39,6 +52,11 @@ struct xr_uart_regs {
> > };
> >
> > struct xr_port_private {
> > +#ifdef CONFIG_GPIOLIB
> > + struct gpio_chip gc;
> > + bool gpio_registered;
> > + enum gpio_pins pin_status[GPIO_MAX];
> > +#endif
> > const struct xr_uart_regs *regs;
> > bool port_open;
> > };
> > @@ -390,6 +408,13 @@ static void xr_set_flow_mode(struct tty_struct *tty,
> > */
> > gpio_mode &= ~UART_MODE_GPIO_MASK;
> > if (cflag & CRTSCTS) {
> > +#ifdef CONFIG_GPIOLIB
> > + /* Check if the CTS/RTS pins are occupied */
> > + if (port_priv->pin_status[GPIO_RTS] ||
> > + port_priv->pin_status[GPIO_CTS])
> > + return;
> > +#endif
>
> You cannot just bail out as this could leave software flow enabled etc.
>
> You also need to claim these pins once at open or leave them be. We
> don't want CRTSCTS to suddenly start toggling because a pin is released
> by gpiolib.
>
> That is, determine who owns each pin at open() and keep it that way till
> close() (by setting some flags at open).
>
> > +
> > dev_dbg(&port->dev, "Enabling hardware flow ctrl\n");
> > flow = UART_FLOW_MODE_HW;
> > gpio_mode |= UART_MODE_RTS_CTS;
> > @@ -497,6 +522,17 @@ static int xr_tiocmset_port(struct usb_serial_port *port,
> > u8 gpio_set = 0;
> > u8 gpio_clr = 0;
> >
> > +#ifdef CONFIG_GPIOLIB
> > + /* Check if the RTS/DTR pins are occupied */
> > + if (set & TIOCM_RTS || clear & TIOCM_RTS)
> > + if (port_priv->pin_status[GPIO_RTS])
> > + return -EBUSY;
> > +
> > + if (set & TIOCM_DTR || clear & TIOCM_DTR)
> > + if (port_priv->pin_status[GPIO_DTR])
> > + return -EBUSY;
> > +#endif
>
> Same here. And perhaps just ignoring the pins managed by gpiolib is
> better (cf. gpiolib and pinctrl being orthogonal).

You mean, we can just make TX,RX,CTS,RTS pins controlled only by the serial
driver and the rest only by gpiolib?

Thanks,
Mani

>
> > +
> > /* Modem control pins are active low, so set & clr are swapped */
> > if (set & TIOCM_RTS)
> > gpio_clr |= UART_MODE_RTS;
> > @@ -589,9 +625,175 @@ static void xr_break_ctl(struct tty_struct *tty, int break_state)
> > state);
> > }
> >
> > +#ifdef CONFIG_GPIOLIB
> > +
> > +static int xr_gpio_request(struct gpio_chip *gc, unsigned int offset)
> > +{
> > + struct usb_serial_port *port = gpiochip_get_data(gc);
> > + struct xr_port_private *port_priv = usb_get_serial_port_data(port);
> > +
> > + /*
> > + * Do not proceed if the port is open. This is done to avoid changing
> > + * the GPIO configuration used by the serial driver.
> > + */
> > + if (port_priv->port_open)
> > + return -EBUSY;
> > +
> > + /* Mark the GPIO pin as busy */
> > + port_priv->pin_status[offset] = true;
>
> You need a lock to serialise against open/close properly.
>
> > +
> > + return 0;
> > +}
> > +
> > +static void xr_gpio_free(struct gpio_chip *gc, unsigned int offset)
> > +{
> > + struct usb_serial_port *port = gpiochip_get_data(gc);
> > + struct xr_port_private *port_priv = usb_get_serial_port_data(port);
> > +
> > + /* Mark the GPIO pin as free */
> > + port_priv->pin_status[offset] = false;
> > +}
>
> Johan