diff mbox series

[v3,1/9] serial: core: move RS485 configuration tasks from drivers into core

Message ID 20220222011433.8761-2-LinoSanfilippo@gmx.de (mailing list archive)
State New, archived
Headers show
Series [v3,1/9] serial: core: move RS485 configuration tasks from drivers into core | expand

Commit Message

Lino Sanfilippo Feb. 22, 2022, 1:14 a.m. UTC
Several drivers that support setting the RS485 configuration via userspace
implement one or more of the following tasks:

- in case of an invalid RTS configuration (both RTS after send and RTS on
  send set or both unset) fall back to enable RTS on send and disable RTS
  after send

- nullify the padding field of the returned serial_rs485 struct

- copy the configuration into the uart port struct

- limit RTS delays to 100 ms

Move these tasks into the serial core to make them generic and to provide
a consistent behaviour among all drivers.

Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
---
 drivers/tty/serial/serial_core.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)


base-commit: a603ca60cebff8589882427a67f870ed946b3fc8

Comments

Jiri Slaby Feb. 22, 2022, 6:51 a.m. UTC | #1
On 22. 02. 22, 2:14, Lino Sanfilippo wrote:
> Several drivers that support setting the RS485 configuration via userspace
> implement one or more of the following tasks:
> 
> - in case of an invalid RTS configuration (both RTS after send and RTS on
>    send set or both unset) fall back to enable RTS on send and disable RTS
>    after send
> 
> - nullify the padding field of the returned serial_rs485 struct
> 
> - copy the configuration into the uart port struct
> 
> - limit RTS delays to 100 ms
> 
> Move these tasks into the serial core to make them generic and to provide
> a consistent behaviour among all drivers.
> 
> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
> ---
>   drivers/tty/serial/serial_core.c | 29 +++++++++++++++++++++++++++++
>   1 file changed, 29 insertions(+)
> 
> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 846192a7b4bf..2b3afe038c1c 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -42,6 +42,11 @@ static struct lock_class_key port_lock_key;
>   
>   #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
>   
> +/*
> + * Max time with active RTS before/after data is sent.
> + */
> +#define RS485_MAX_RTS_DELAY	100 /* msecs */
> +
>   static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
>   					struct ktermios *old_termios);
>   static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
> @@ -1282,8 +1287,32 @@ static int uart_set_rs485_config(struct uart_port *port,
>   	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
>   		return -EFAULT;
>   
> +	/* pick sane settings if the user hasn't */
> +	if (!(rs485.flags & SER_RS485_RTS_ON_SEND) ==
> +	    !(rs485.flags & SER_RS485_RTS_AFTER_SEND)) {
> +		pr_warn("invalid RTS setting, using RTS_ON_SEND instead\n");

Can't we have a device prefix here, so that everyone knows what device 
is affected? Without that, it's not that useful. At least port->name & 
port->line could be printed. The uart core uses dev_* prints, but prints 
also line as uport->dev can be NULL sometimes.

Hm, we could introduce uart_print family (to print something like what 
is in uart_report_port).

thanks,
Jiri Slaby Feb. 22, 2022, 6:52 a.m. UTC | #2
On 22. 02. 22, 7:51, Jiri Slaby wrote:
> On 22. 02. 22, 2:14, Lino Sanfilippo wrote:
>> Several drivers that support setting the RS485 configuration via 
>> userspace
>> implement one or more of the following tasks:
>>
>> - in case of an invalid RTS configuration (both RTS after send and RTS on
>>    send set or both unset) fall back to enable RTS on send and disable 
>> RTS
>>    after send
>>
>> - nullify the padding field of the returned serial_rs485 struct
>>
>> - copy the configuration into the uart port struct
>>
>> - limit RTS delays to 100 ms
>>
>> Move these tasks into the serial core to make them generic and to provide
>> a consistent behaviour among all drivers.
>>
>> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
>> ---
>>   drivers/tty/serial/serial_core.c | 29 +++++++++++++++++++++++++++++
>>   1 file changed, 29 insertions(+)
>>
>> diff --git a/drivers/tty/serial/serial_core.c 
>> b/drivers/tty/serial/serial_core.c
>> index 846192a7b4bf..2b3afe038c1c 100644
>> --- a/drivers/tty/serial/serial_core.c
>> +++ b/drivers/tty/serial/serial_core.c
>> @@ -42,6 +42,11 @@ static struct lock_class_key port_lock_key;
>>   #define HIGH_BITS_OFFSET    ((sizeof(long)-sizeof(int))*8)
>> +/*
>> + * Max time with active RTS before/after data is sent.
>> + */
>> +#define RS485_MAX_RTS_DELAY    100 /* msecs */
>> +
>>   static void uart_change_speed(struct tty_struct *tty, struct 
>> uart_state *state,
>>                       struct ktermios *old_termios);
>>   static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
>> @@ -1282,8 +1287,32 @@ static int uart_set_rs485_config(struct 
>> uart_port *port,
>>       if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
>>           return -EFAULT;
>> +    /* pick sane settings if the user hasn't */
>> +    if (!(rs485.flags & SER_RS485_RTS_ON_SEND) ==
>> +        !(rs485.flags & SER_RS485_RTS_AFTER_SEND)) {
>> +        pr_warn("invalid RTS setting, using RTS_ON_SEND instead\n");
> 
> Can't we have a device prefix here, so that everyone knows what device 
> is affected? Without that, it's not that useful. At least port->name & 
> port->line could be printed. The uart core uses dev_* prints, but prints 
> also line as uport->dev can be NULL sometimes.

And this comes from userspace, so should be ratelimited.
Lino Sanfilippo Feb. 25, 2022, 10:16 a.m. UTC | #3
Hi,


> On 22. 02. 22, 7:51, Jiri Slaby wrote:
> > On 22. 02. 22, 2:14, Lino Sanfilippo wrote:
> >> Several drivers that support setting the RS485 configuration via 
> >> userspace
> >> implement one or more of the following tasks:
> >>
> >> - in case of an invalid RTS configuration (both RTS after send and RTS on
> >>    send set or both unset) fall back to enable RTS on send and disable 
> >> RTS
> >>    after send
> >>
> >> - nullify the padding field of the returned serial_rs485 struct
> >>
> >> - copy the configuration into the uart port struct
> >>
> >> - limit RTS delays to 100 ms
> >>
> >> Move these tasks into the serial core to make them generic and to provide
> >> a consistent behaviour among all drivers.
> >>
> >> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
> >> ---
> >>   drivers/tty/serial/serial_core.c | 29 +++++++++++++++++++++++++++++
> >>   1 file changed, 29 insertions(+)
> >>
> >> diff --git a/drivers/tty/serial/serial_core.c 
> >> b/drivers/tty/serial/serial_core.c
> >> index 846192a7b4bf..2b3afe038c1c 100644
> >> --- a/drivers/tty/serial/serial_core.c
> >> +++ b/drivers/tty/serial/serial_core.c
> >> @@ -42,6 +42,11 @@ static struct lock_class_key port_lock_key;
> >>   #define HIGH_BITS_OFFSET    ((sizeof(long)-sizeof(int))*8)
> >> +/*
> >> + * Max time with active RTS before/after data is sent.
> >> + */
> >> +#define RS485_MAX_RTS_DELAY    100 /* msecs */
> >> +
> >>   static void uart_change_speed(struct tty_struct *tty, struct 
> >> uart_state *state,
> >>                       struct ktermios *old_termios);
> >>   static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
> >> @@ -1282,8 +1287,32 @@ static int uart_set_rs485_config(struct 
> >> uart_port *port,
> >>       if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
> >>           return -EFAULT;
> >> +    /* pick sane settings if the user hasn't */
> >> +    if (!(rs485.flags & SER_RS485_RTS_ON_SEND) ==
> >> +        !(rs485.flags & SER_RS485_RTS_AFTER_SEND)) {
> >> +        pr_warn("invalid RTS setting, using RTS_ON_SEND instead\n");
> > 
> > Can't we have a device prefix here, so that everyone knows what device 
> > is affected? Without that, it's not that useful. At least port->name & 
> > port->line could be printed. The uart core uses dev_* prints, but prints 
> > also line as uport->dev can be NULL sometimes.
> 
> And this comes from userspace, so should be ratelimited.
> 

Right, ratelimiting makes sense here. I will change it as suggested in the next patch version.
Thanks a lot for the review.

Regards,
Lino
diff mbox series

Patch

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 846192a7b4bf..2b3afe038c1c 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -42,6 +42,11 @@  static struct lock_class_key port_lock_key;
 
 #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
 
+/*
+ * Max time with active RTS before/after data is sent.
+ */
+#define RS485_MAX_RTS_DELAY	100 /* msecs */
+
 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
 					struct ktermios *old_termios);
 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
@@ -1282,8 +1287,32 @@  static int uart_set_rs485_config(struct uart_port *port,
 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
 		return -EFAULT;
 
+	/* pick sane settings if the user hasn't */
+	if (!(rs485.flags & SER_RS485_RTS_ON_SEND) ==
+	    !(rs485.flags & SER_RS485_RTS_AFTER_SEND)) {
+		pr_warn("invalid RTS setting, using RTS_ON_SEND instead\n");
+		rs485.flags |= SER_RS485_RTS_ON_SEND;
+		rs485.flags &= ~SER_RS485_RTS_AFTER_SEND;
+	}
+
+	if (rs485.delay_rts_before_send > RS485_MAX_RTS_DELAY) {
+		rs485.delay_rts_before_send = RS485_MAX_RTS_DELAY;
+		pr_warn("RTS delay before sending clamped to %u ms\n",
+			rs485.delay_rts_before_send);
+	}
+
+	if (rs485.delay_rts_after_send > RS485_MAX_RTS_DELAY) {
+		rs485.delay_rts_after_send = RS485_MAX_RTS_DELAY;
+		pr_warn("RTS delay after sending clamped to %u ms\n",
+			rs485.delay_rts_after_send);
+	}
+	/* Return clean padding area to userspace */
+	memset(rs485.padding, 0, sizeof(rs485.padding));
+
 	spin_lock_irqsave(&port->lock, flags);
 	ret = port->rs485_config(port, &rs485);
+	if (!ret)
+		port->rs485 = rs485;
 	spin_unlock_irqrestore(&port->lock, flags);
 	if (ret)
 		return ret;