[4/6] tty: serial: amba-pl011: replace TIOCMBIT macros by static functions

Message ID 20231026-mbly-uart-v1-4-9258eea297d3@bootlin.com
State New
Headers
Series Cleanup AMBA PL011 driver |

Commit Message

Théo Lebrun Oct. 26, 2023, 10:41 a.m. UTC
  The driver uses two TIOCMBIT macros inside pl011_{get,set}_mctrl to
simplify the logic. Those look scary to checkpatch because they contain
ifs without do-while loops.

Avoid the macros by creating small equivalent static functions; that
lets the compiler do its type checking & avoids checkpatch errors.

For the second instance __assign_bit is not usable because it deals with
unsigned long pointers whereas we have an unsigned int in
pl011_set_mctrl.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
 drivers/tty/serial/amba-pl011.c | 46 +++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
  

Comments

Linus Walleij Oct. 26, 2023, 1:46 p.m. UTC | #1
On Thu, Oct 26, 2023 at 12:41 PM Théo Lebrun <theo.lebrun@bootlin.com> wrote:

> The driver uses two TIOCMBIT macros inside pl011_{get,set}_mctrl to
> simplify the logic. Those look scary to checkpatch because they contain
> ifs without do-while loops.
>
> Avoid the macros by creating small equivalent static functions; that
> lets the compiler do its type checking & avoids checkpatch errors.
>
> For the second instance __assign_bit is not usable because it deals with
> unsigned long pointers whereas we have an unsigned int in
> pl011_set_mctrl.
>
> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>

The kernel looks better after this patch than before, so:
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Since the eternal defines uses masks rather than bits we can't
use __set_bit() & friends in this case and that's life.

Yours,
Linus Walleij
  
Hugo Villeneuve Oct. 26, 2023, 2:24 p.m. UTC | #2
On Thu, 26 Oct 2023 12:41:21 +0200
Théo Lebrun <theo.lebrun@bootlin.com> wrote:

Hi,

> The driver uses two TIOCMBIT macros inside pl011_{get,set}_mctrl to
> simplify the logic. Those look scary to checkpatch because they contain
> ifs without do-while loops.
> 
> Avoid the macros by creating small equivalent static functions; that
> lets the compiler do its type checking & avoids checkpatch errors.
> 
> For the second instance __assign_bit is not usable because it deals with
> unsigned long pointers whereas we have an unsigned int in
> pl011_set_mctrl.
> 
> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
> ---
>  drivers/tty/serial/amba-pl011.c | 46 +++++++++++++++++++++--------------------
>  1 file changed, 24 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
> index 0d53973374de..bb3082c4d35c 100644
> --- a/drivers/tty/serial/amba-pl011.c
> +++ b/drivers/tty/serial/amba-pl011.c
> @@ -1087,7 +1087,6 @@ static void pl011_dma_rx_poll(struct timer_list *t)
>  	 */
>  	if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
>  			> uap->dmarx.poll_timeout) {
> -

This should go into a separate patch, or simply be merged with one
of your other coding style/whitespace cleanup patches.

Hugo.


>  		spin_lock_irqsave(&uap->port.lock, flags);
>  		pl011_dma_rx_stop(uap);
>  		uap->im |= UART011_RXIM;
> @@ -1595,6 +1594,12 @@ static unsigned int pl011_tx_empty(struct uart_port *port)
>  							0 : TIOCSER_TEMT;
>  }
>  
> +static void pl011_maybe_set_bit(bool cond, unsigned int *ptr, unsigned int mask)
> +{
> +	if (cond)
> +		*ptr |= mask;
> +}
> +
>  static unsigned int pl011_get_mctrl(struct uart_port *port)
>  {
>  	struct uart_amba_port *uap =
> @@ -1602,18 +1607,22 @@ static unsigned int pl011_get_mctrl(struct uart_port *port)
>  	unsigned int result = 0;
>  	unsigned int status = pl011_read(uap, REG_FR);
>  
> -#define TIOCMBIT(uartbit, tiocmbit)	\
> -	if (status & uartbit)		\
> -		result |= tiocmbit
> +	pl011_maybe_set_bit(status & UART01x_FR_DCD, &result, TIOCM_CAR);
> +	pl011_maybe_set_bit(status & uap->vendor->fr_dsr, &result, TIOCM_DSR);
> +	pl011_maybe_set_bit(status & uap->vendor->fr_cts, &result, TIOCM_CTS);
> +	pl011_maybe_set_bit(status & uap->vendor->fr_ri, &result, TIOCM_RNG);
>  
> -	TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
> -	TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
> -	TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
> -	TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
> -#undef TIOCMBIT
>  	return result;
>  }
>  
> +static void pl011_assign_bit(bool cond, unsigned int *ptr, unsigned int mask)
> +{
> +	if (cond)
> +		*ptr |= mask;
> +	else
> +		*ptr &= ~mask;
> +}
> +
>  static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
>  {
>  	struct uart_amba_port *uap =
> @@ -1622,23 +1631,16 @@ static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
>  
>  	cr = pl011_read(uap, REG_CR);
>  
> -#define	TIOCMBIT(tiocmbit, uartbit)		\
> -	if (mctrl & tiocmbit)		\
> -		cr |= uartbit;		\
> -	else				\
> -		cr &= ~uartbit
> -
> -	TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
> -	TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
> -	TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
> -	TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
> -	TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
> +	pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTS);
> +	pl011_assign_bit(mctrl & TIOCM_DTR, &cr, UART011_CR_DTR);
> +	pl011_assign_bit(mctrl & TIOCM_OUT1, &cr, UART011_CR_OUT1);
> +	pl011_assign_bit(mctrl & TIOCM_OUT2, &cr, UART011_CR_OUT2);
> +	pl011_assign_bit(mctrl & TIOCM_LOOP, &cr, UART011_CR_LBE);
>  
>  	if (port->status & UPSTAT_AUTORTS) {
>  		/* We need to disable auto-RTS if we want to turn RTS off */
> -		TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
> +		pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTSEN);
>  	}
> -#undef TIOCMBIT
>  
>  	pl011_write(cr, uap, REG_CR);
>  }
> 
> -- 
> 2.41.0
>
  
Théo Lebrun Oct. 26, 2023, 2:37 p.m. UTC | #3
Hello,

On Thu Oct 26, 2023 at 4:24 PM CEST, Hugo Villeneuve wrote:
> On Thu, 26 Oct 2023 12:41:21 +0200
> Théo Lebrun <theo.lebrun@bootlin.com> wrote:
> > The driver uses two TIOCMBIT macros inside pl011_{get,set}_mctrl to
> > simplify the logic. Those look scary to checkpatch because they contain
> > ifs without do-while loops.
> > 
> > Avoid the macros by creating small equivalent static functions; that
> > lets the compiler do its type checking & avoids checkpatch errors.
> > 
> > For the second instance __assign_bit is not usable because it deals with
> > unsigned long pointers whereas we have an unsigned int in
> > pl011_set_mctrl.
> > 
> > Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
> > ---
> >  drivers/tty/serial/amba-pl011.c | 46 +++++++++++++++++++++--------------------
> >  1 file changed, 24 insertions(+), 22 deletions(-)
> > 
> > diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
> > index 0d53973374de..bb3082c4d35c 100644
> > --- a/drivers/tty/serial/amba-pl011.c
> > +++ b/drivers/tty/serial/amba-pl011.c
> > @@ -1087,7 +1087,6 @@ static void pl011_dma_rx_poll(struct timer_list *t)
> >  	 */
> >  	if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
> >  			> uap->dmarx.poll_timeout) {
> > -
>
> This should go into a separate patch, or simply be merged with one
> of your other coding style/whitespace cleanup patches.

Indeed, added to "tty: serial: amba-pl011: cleanup driver". Thanks.

Regards,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
  

Patch

diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
index 0d53973374de..bb3082c4d35c 100644
--- a/drivers/tty/serial/amba-pl011.c
+++ b/drivers/tty/serial/amba-pl011.c
@@ -1087,7 +1087,6 @@  static void pl011_dma_rx_poll(struct timer_list *t)
 	 */
 	if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
 			> uap->dmarx.poll_timeout) {
-
 		spin_lock_irqsave(&uap->port.lock, flags);
 		pl011_dma_rx_stop(uap);
 		uap->im |= UART011_RXIM;
@@ -1595,6 +1594,12 @@  static unsigned int pl011_tx_empty(struct uart_port *port)
 							0 : TIOCSER_TEMT;
 }
 
+static void pl011_maybe_set_bit(bool cond, unsigned int *ptr, unsigned int mask)
+{
+	if (cond)
+		*ptr |= mask;
+}
+
 static unsigned int pl011_get_mctrl(struct uart_port *port)
 {
 	struct uart_amba_port *uap =
@@ -1602,18 +1607,22 @@  static unsigned int pl011_get_mctrl(struct uart_port *port)
 	unsigned int result = 0;
 	unsigned int status = pl011_read(uap, REG_FR);
 
-#define TIOCMBIT(uartbit, tiocmbit)	\
-	if (status & uartbit)		\
-		result |= tiocmbit
+	pl011_maybe_set_bit(status & UART01x_FR_DCD, &result, TIOCM_CAR);
+	pl011_maybe_set_bit(status & uap->vendor->fr_dsr, &result, TIOCM_DSR);
+	pl011_maybe_set_bit(status & uap->vendor->fr_cts, &result, TIOCM_CTS);
+	pl011_maybe_set_bit(status & uap->vendor->fr_ri, &result, TIOCM_RNG);
 
-	TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
-	TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
-	TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
-	TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
-#undef TIOCMBIT
 	return result;
 }
 
+static void pl011_assign_bit(bool cond, unsigned int *ptr, unsigned int mask)
+{
+	if (cond)
+		*ptr |= mask;
+	else
+		*ptr &= ~mask;
+}
+
 static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
 {
 	struct uart_amba_port *uap =
@@ -1622,23 +1631,16 @@  static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
 
 	cr = pl011_read(uap, REG_CR);
 
-#define	TIOCMBIT(tiocmbit, uartbit)		\
-	if (mctrl & tiocmbit)		\
-		cr |= uartbit;		\
-	else				\
-		cr &= ~uartbit
-
-	TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
-	TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
-	TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
-	TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
-	TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
+	pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTS);
+	pl011_assign_bit(mctrl & TIOCM_DTR, &cr, UART011_CR_DTR);
+	pl011_assign_bit(mctrl & TIOCM_OUT1, &cr, UART011_CR_OUT1);
+	pl011_assign_bit(mctrl & TIOCM_OUT2, &cr, UART011_CR_OUT2);
+	pl011_assign_bit(mctrl & TIOCM_LOOP, &cr, UART011_CR_LBE);
 
 	if (port->status & UPSTAT_AUTORTS) {
 		/* We need to disable auto-RTS if we want to turn RTS off */
-		TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
+		pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTSEN);
 	}
-#undef TIOCMBIT
 
 	pl011_write(cr, uap, REG_CR);
 }