@@ -93,6 +93,7 @@ struct xr_txrx_clk_mask {
93
93
#define XR_GPIO_MODE_SEL_DTR_DSR 0x2
94
94
#define XR_GPIO_MODE_SEL_RS485 0x3
95
95
#define XR_GPIO_MODE_SEL_RS485_ADDR 0x4
96
+ #define XR_GPIO_MODE_RS485_TX_H 0x8
96
97
#define XR_GPIO_MODE_TX_TOGGLE 0x100
97
98
#define XR_GPIO_MODE_RX_TOGGLE 0x200
98
99
@@ -237,6 +238,7 @@ static const struct xr_type xr_types[] = {
237
238
struct xr_data {
238
239
const struct xr_type * type ;
239
240
u8 channel ; /* zero-based index or interface number */
241
+ struct serial_rs485 rs485 ;
240
242
};
241
243
242
244
static int xr_set_reg (struct usb_serial_port * port , u8 channel , u16 reg , u16 val )
@@ -629,6 +631,7 @@ static void xr_set_flow_mode(struct tty_struct *tty,
629
631
struct xr_data * data = usb_get_serial_port_data (port );
630
632
const struct xr_type * type = data -> type ;
631
633
u16 flow , gpio_mode ;
634
+ bool rs485_enabled ;
632
635
int ret ;
633
636
634
637
ret = xr_get_reg_uart (port , type -> gpio_mode , & gpio_mode );
@@ -645,7 +648,17 @@ static void xr_set_flow_mode(struct tty_struct *tty,
645
648
/* Set GPIO mode for controlling the pins manually by default. */
646
649
gpio_mode &= ~XR_GPIO_MODE_SEL_MASK ;
647
650
648
- if (C_CRTSCTS (tty ) && C_BAUD (tty ) != B0 ) {
651
+ rs485_enabled = !!(data -> rs485 .flags & SER_RS485_ENABLED );
652
+ if (rs485_enabled ) {
653
+ dev_dbg (& port -> dev , "Enabling RS-485\n" );
654
+ gpio_mode |= XR_GPIO_MODE_SEL_RS485 ;
655
+ if (data -> rs485 .flags & SER_RS485_RTS_ON_SEND )
656
+ gpio_mode &= ~XR_GPIO_MODE_RS485_TX_H ;
657
+ else
658
+ gpio_mode |= XR_GPIO_MODE_RS485_TX_H ;
659
+ }
660
+
661
+ if (C_CRTSCTS (tty ) && C_BAUD (tty ) != B0 && !rs485_enabled ) {
649
662
dev_dbg (& port -> dev , "Enabling hardware flow ctrl\n" );
650
663
gpio_mode |= XR_GPIO_MODE_SEL_RTS_CTS ;
651
664
flow = XR_UART_FLOW_MODE_HW ;
@@ -809,6 +822,79 @@ static void xr_cdc_set_line_coding(struct tty_struct *tty,
809
822
kfree (lc );
810
823
}
811
824
825
+ static void xr_sanitize_serial_rs485 (struct serial_rs485 * rs485 )
826
+ {
827
+ if (!(rs485 -> flags & SER_RS485_ENABLED )) {
828
+ memset (rs485 , 0 , sizeof (* rs485 ));
829
+ return ;
830
+ }
831
+
832
+ /* RTS always toggles after TX */
833
+ if (rs485 -> flags & SER_RS485_RTS_ON_SEND )
834
+ rs485 -> flags &= ~SER_RS485_RTS_AFTER_SEND ;
835
+ else
836
+ rs485 -> flags |= SER_RS485_RTS_AFTER_SEND ;
837
+
838
+ /* Only the flags are implemented at the moment */
839
+ rs485 -> flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
840
+ SER_RS485_RTS_AFTER_SEND ;
841
+ rs485 -> delay_rts_before_send = 0 ;
842
+ rs485 -> delay_rts_after_send = 0 ;
843
+ memset (rs485 -> padding , 0 , sizeof (rs485 -> padding ));
844
+ }
845
+
846
+ static int xr_get_rs485_config (struct tty_struct * tty ,
847
+ struct serial_rs485 __user * argp )
848
+ {
849
+ struct usb_serial_port * port = tty -> driver_data ;
850
+ struct xr_data * data = usb_get_serial_port_data (port );
851
+
852
+ down_read (& tty -> termios_rwsem );
853
+ if (copy_to_user (argp , & data -> rs485 , sizeof (data -> rs485 ))) {
854
+ up_read (& tty -> termios_rwsem );
855
+ return - EFAULT ;
856
+ }
857
+ up_read (& tty -> termios_rwsem );
858
+
859
+ return 0 ;
860
+ }
861
+
862
+ static int xr_set_rs485_config (struct tty_struct * tty ,
863
+ struct serial_rs485 __user * argp )
864
+ {
865
+ struct usb_serial_port * port = tty -> driver_data ;
866
+ struct xr_data * data = usb_get_serial_port_data (port );
867
+ struct serial_rs485 rs485 ;
868
+
869
+ if (copy_from_user (& rs485 , argp , sizeof (rs485 )))
870
+ return - EFAULT ;
871
+ xr_sanitize_serial_rs485 (& rs485 );
872
+
873
+ down_write (& tty -> termios_rwsem );
874
+ data -> rs485 = rs485 ;
875
+ xr_set_flow_mode (tty , port , NULL );
876
+ up_write (& tty -> termios_rwsem );
877
+
878
+ if (copy_to_user (argp , & rs485 , sizeof (rs485 )))
879
+ return - EFAULT ;
880
+
881
+ return 0 ;
882
+ }
883
+
884
+ static int xr_ioctl (struct tty_struct * tty , unsigned int cmd , unsigned long arg )
885
+ {
886
+ void __user * argp = (void __user * )arg ;
887
+
888
+ switch (cmd ) {
889
+ case TIOCGRS485 :
890
+ return xr_get_rs485_config (tty , argp );
891
+ case TIOCSRS485 :
892
+ return xr_set_rs485_config (tty , argp );
893
+ }
894
+
895
+ return - ENOIOCTLCMD ;
896
+ }
897
+
812
898
static void xr_set_termios (struct tty_struct * tty ,
813
899
struct usb_serial_port * port ,
814
900
const struct ktermios * old_termios )
@@ -1010,6 +1096,7 @@ static struct usb_serial_driver xr_device = {
1010
1096
.set_termios = xr_set_termios ,
1011
1097
.tiocmget = xr_tiocmget ,
1012
1098
.tiocmset = xr_tiocmset ,
1099
+ .ioctl = xr_ioctl ,
1013
1100
.dtr_rts = xr_dtr_rts
1014
1101
};
1015
1102
0 commit comments