Re: [PATCH v1 3/4] usb: serial: mxuport: support serial interface mode configuration
From: Johan Hovold
Date: Thu May 07 2026 - 12:02:56 EST
On Tue, Mar 24, 2026 at 11:50:40AM +0800, Crescent Hsieh wrote:
> Add support for configuring the serial interface mode through
> TIOCSRS485 and TIOCGRS485 using struct serial_rs485.
>
> Sanitize the requested RS-485 settings and map them to the device
> interface modes before issuing the vendor command to the firmware.
>
> This allows userspace to switch between RS232, RS422, 2-wire RS485,
> and 4-wire RS485, and to query the current per-port configuration.
>
> Signed-off-by: Crescent Hsieh <crescentcy.hsieh@xxxxxxxx>
> +static void mxuport_sanitize_serial_rs485(struct serial_rs485 *rs485)
> +{
> + if (!(rs485->flags & SER_RS485_ENABLED)) {
> + memset(rs485, 0, sizeof(*rs485));
> + return;
> + }
> + if (rs485->flags & SER_RS485_MODE_RS422) {
> + rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422);
> + return;
> + }
> + rs485->flags &= (SER_RS485_ENABLED | SER_RS485_RX_DURING_TX);
I think you need to clear the other unsupported fields as well (i.e. the
delays).
> +
> + memset(rs485->padding, 0, sizeof(rs485->padding));
> +}
> +
> +static int mxuport_rs485_config(struct usb_serial_port *port,
> + struct serial_rs485 *rs485)
> +{
> + struct usb_serial *serial = port->serial;
> + u16 mode = MX_INT_RS232;
> +
> + if (rs485->flags & SER_RS485_ENABLED) {
> + if (rs485->flags & SER_RS485_MODE_RS422)
> + mode = MX_INT_RS422;
> + else if (rs485->flags & SER_RS485_RX_DURING_TX)
> + mode = MX_INT_4W_RS485;
> + else
> + mode = MX_INT_2W_RS485;
> + }
> +
> + return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE, mode,
> + port->port_number);
> +}
> +
> +static int mxuport_get_rs485_config(struct usb_serial_port *port,
> + struct serial_rs485 __user *rs485)
> +{
> + struct mxuport_port *mxport = usb_get_serial_port_data(port);
> +
> + if (copy_to_user(rs485, &mxport->rs485, sizeof(mxport->rs485)))
> + return -EFAULT;
> +
> + return 0;
> +}
> +
> +static int mxuport_set_rs485_config(struct usb_serial_port *port,
> + struct serial_rs485 __user *rs485_user)
> +{
> + struct mxuport_port *mxport = usb_get_serial_port_data(port);
> + struct serial_rs485 rs485;
> + int ret;
> +
> + if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
> + return -EFAULT;
> +
> + mxuport_sanitize_serial_rs485(&rs485);
> +
> + ret = mxuport_rs485_config(port, &rs485);
> + if (!ret)
> + mxport->rs485 = rs485;
We need some locking here as these ioctls can execute in parallel.
> +
> + if (copy_to_user(rs485_user, &mxport->rs485, sizeof(mxport->rs485)))
> + return -EFAULT;
> +
> + return 0;
> +}
Johan