serial: Add ioctl to enable auto rs485 mode with some Exar UARTs
PATCH v2 1/1 serial: Add ioctl to enable auto rs485 mode with some Exar UARTs
Some Exar UARTs support a auto rs485 mode. In this mode the UART's RTS# pin is activated during transmitting and can be used to enable a rs485 line driver. This has nothing to do with attempts to do this by manually by asserting/ deasserting handshake lines.
--- drivers/serial/8250.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/serial_reg.h | 1 + 2 files changed, 65 insertions(+), 0 deletions(-)
drivers/serial/8250.c +64 -0
@@ -2511,6 +2511,69 @@ serial8250_type(struct uart_port *port)return uart_config[type].name; } +static int +serial8250_ioctl_port (struct uart_port *port,unsigned int cmd,unsigned long arg) +{ +struct uart_8250_port *up = (struct uart_8250_port *)port; +unsigned long flags; + +switch (cmd) { +case TIOCSRS485 : + { +struct serial_rs485 rs485ctrl; +unsigned char fctr; + +if (port->type !=PORT_16850 ) +return -ENOTTY ; + +if (copy_from_user (&rs485ctrl, + (struct serial_rs485 *)arg, +sizeof (rs485ctrl))) +return -EFAULT ; + +spin_lock_irqsave (&up->port.lock, flags); +serial_outp (up,UART_LCR ,0xbf ); + fctr =serial_inp (up,UART_FCTR ); +if (rs485ctrl.flags &SER_RS485_ENABLED ) + fctr |=UART_FCTR_RS485 ; +else + fctr &= ~UART_FCTR_RS485 ; +serial_outp (up,UART_FCTR , fctr); +serial_outp (up,UART_LCR ,0 ); +spin_unlock_irqrestore (&up->port.lock, flags); +return 0 ; + } + +case TIOCGRS485 : + { +struct serial_rs485 rs485ctrl; + +if (port->type !=PORT_16850 ) +return -ENOTTY ; + +spin_lock_irqsave (&up->port.lock, flags); +serial_outp (up,UART_LCR ,0xbf ); +if (serial_inp (up,UART_FCTR ) &UART_FCTR_RS485 ) + rs485ctrl.flags =SER_RS485_ENABLED ; +else + rs485ctrl.flags =0 ; +serial_outp (up,UART_LCR ,0 ); +spin_unlock_irqrestore (&up->port.lock, flags); + +if (copy_to_user ((struct serial_rs485 *)arg, + &rs485ctrl, +sizeof (rs485ctrl))) +return -EFAULT ; +return 0 ; + } + +default : +return -ENOIOCTLCMD ; + } + +return 0 ; +} +static struct uart_ops serial8250_pops = { .tx_empty = serial8250_tx_empty, .set_mctrl = serial8250_set_mctrl,
@@ -2529,6 +2592,7 @@ static struct uart_ops serial8250_pops = { .request_port = serial8250_request_port, .config_port = serial8250_config_port, .verify_port = serial8250_verify_port, + .ioctl = serial8250_ioctl_port, #ifdef CONFIG_CONSOLE_POLL .poll_get_char = serial8250_get_poll_char, .poll_put_char = serial8250_put_poll_char,
include/linux/serial_reg.h +1 -0
@@ -191,6 +191,7 @@#define UART_FCTR_RTS_8DELAY 0x03#define UART_FCTR_IRDA 0x04/* IrDa data encode select */ #define UART_FCTR_TX_INT 0x08/* Tx interrupt type select */ +#define UART_FCTR_RS485 0x08/* Auto RS485 mode */ #define UART_FCTR_TRGA 0x00/* Tx/Rx 550 trigger table select */ #define UART_FCTR_TRGB 0x10/* Tx/Rx 650 trigger table select */ #define UART_FCTR_TRGC 0x20/* Tx/Rx 654 trigger table select */
Signature · 1 line
1.5.6.3
Hi Mathias,
Quoted 5 lines · On Friday 02 January 2009 17:26:09 Matthias Fuchs wrote:
Some Exar UARTs support a auto rs485 mode. In this mode the UART's RTS# pin is activated during transmitting and can be used to enable a rs485 line driver. This has nothing to do with attempts to do this by manually by asserting/ deasserting handshake lines.
I've tested your patch and it mostly works. See comments inline.
Quoted 27 lines
Signed-off-by Matthias Fuchs <[email protected]>--- drivers/serial/8250.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/serial_reg.h | 1 + 2 files changed, 65 insertions(+), 0 deletions(-)drivers/serial/8250.c +11
-0@@ -2511,6 +2511,69 @@ serial8250_type(struct uart_port *port)return uart_config[type].name; } +static int +serial8250_ioctl_port (struct uart_port *port,unsigned int cmd,unsigned long arg) +{ +struct uart_8250_port *up = (struct uart_8250_port *)port; +unsigned long flags; + +switch (cmd) { +case TIOCSRS485 : + { +struct serial_rs485 rs485ctrl; +unsigned char fctr; +
drivers/serial/8250.c serial8250_type(struct uart_port *port) · line 2535
Quoted 10 lines
drivers/serial/8250.c +10
-0+if (port->type !=PORT_16850 ) +return -ENOTTY ; + +if (copy_from_user (&rs485ctrl, + (struct serial_rs485 *)arg, +sizeof (rs485ctrl))) +return -EFAULT ; + +spin_lock_irqsave (&up->port.lock, flags); +serial_outp (up,UART_LCR ,0xbf );
You need to save the current LCR and restore it when you're done.
Quoted 10 lines
drivers/serial/8250.c +10
-0+ fctr =serial_inp (up,UART_FCTR ); +if (rs485ctrl.flags &SER_RS485_ENABLED ) + fctr |=UART_FCTR_RS485 ; +else + fctr &= ~UART_FCTR_RS485 ; +serial_outp (up,UART_FCTR , fctr); +serial_outp (up,UART_LCR ,0 ); +spin_unlock_irqrestore (&up->port.lock, flags); +return 0 ; + }
drivers/serial/8250.c serial8250_type(struct uart_port *port) · line 2555
Quoted 10 lines
drivers/serial/8250.c +10
-0+ +case TIOCGRS485 : + { +struct serial_rs485 rs485ctrl; + +if (port->type !=PORT_16850 ) +return -ENOTTY ; + +spin_lock_irqsave (&up->port.lock, flags); +serial_outp (up,UART_LCR ,0xbf );
Same here.
Quoted 14 lines
drivers/serial/8250.c +14
-0+if (serial_inp (up,UART_FCTR ) &UART_FCTR_RS485 ) + rs485ctrl.flags =SER_RS485_ENABLED ; +else + rs485ctrl.flags =0 ; +serial_outp (up,UART_LCR ,0 ); +spin_unlock_irqrestore (&up->port.lock, flags); + +if (copy_to_user ((struct serial_rs485 *)arg, + &rs485ctrl, +sizeof (rs485ctrl))) +return -EFAULT ; +return 0 ; + } +
drivers/serial/8250.c serial8250_type(struct uart_port *port) · line 2579
Quoted 10 lines
drivers/serial/8250.c +7
-0+default : +return -ENOIOCTLCMD ; + } + +return 0 ; +} +static struct uart_ops serial8250_pops = { .tx_empty = serial8250_tx_empty, .set_mctrl = serial8250_set_mctrl,
Best regards,
Signature · 9 lines
Laurent Pinchart CSE Semaphore Belgium Chaussee de Bruxelles, 732A B-1410 Waterloo Belgium T +32 (2) 387 42 59 F +32 (2) 387 42 75
Quoted 3 lines · Matthias Fuchs
Some Exar UARTs support a auto rs485 mode. In this mode the UART's RTS# pin is activated during transmitting and can be used to enable a rs485 line driver.
--->8----
drivers/serial/8250.c serial8250_type(struct uart_port *port) · line 2549
Quoted 4 lines
drivers/serial/8250.c +4
-0+ +case TIOCGRS485 : + { +struct serial_rs485 rs485ctrl;
Should the TIOCGRS485 call set delay_rts_before_send structure element to 0 to demonstrate that there is no delay ability? memset(&rs485ctrl, 0, sizeof(struct serial_rs485)); missing?
Quoted 19 lines
drivers/serial/8250.c +19
-0+ +if (port->type !=PORT_16850 ) +return -ENOTTY ; + +spin_lock_irqsave (&up->port.lock, flags); +serial_outp (up,UART_LCR ,0xbf ); +if (serial_inp (up,UART_FCTR ) &UART_FCTR_RS485 ) + rs485ctrl.flags =SER_RS485_ENABLED ; +else + rs485ctrl.flags =0 ; +serial_outp (up,UART_LCR ,0 ); +spin_unlock_irqrestore (&up->port.lock, flags); + +if (copy_to_user ((struct serial_rs485 *)arg, + &rs485ctrl, +sizeof (rs485ctrl))) +return -EFAULT ; +return 0 ; + }
Signature · 1 line
Christopher Gibson <[email protected]>
Quoted 6 lines · Matthias Fuchs
Quoted 3 lines
Some Exar UARTs support a auto rs485 mode. In this mode the UART's RTS# pin is activated during transmitting and can be used to enable a rs485 line driver.
--->8----
Quoted 8 lines
drivers/serial/8250.c serial8250_type(struct uart_port *port) · line 2549
Quoted 4 lines
drivers/serial/8250.c +4
-0+ +case TIOCGRS485 : + { +struct serial_rs485 rs485ctrl;Should the TIOCGRS485 call set
delay_rts_before_sendstructure element to 0 to demonstrate that there is no delay ability?memset(&rs485ctrl, 0, sizeof(struct serial_rs485)); missing?
Yes, that makes sense. I will add the memset and also add the saving of the LCR as Laurent requested.
Matthias