Skip to content

Commit 33a6b48

Browse files
hpeterjhovold
authored andcommitted
USB: serial: f81232: add control driver for F81534A
The Fintek F81534A series contains 1 HUB, 1 GPIO device and n UARTs. The UARTs are disabled by default and need to be enabled by the GPIO device (2c42:16F8). When F81534A plug to host, we can only see 1 HUB and 1 GPIO device and we write 0x8fff to GPIO device register F81534A_CTRL_CMD_ENABLE_PORT (116h) to enable all available serial ports. Signed-off-by: Ji-Ze Hong (Peter Hong) <[email protected]> [johan: reword commit message and an error message slightly] Signed-off-by: Johan Hovold <[email protected]>
1 parent 615e58c commit 33a6b48

File tree

1 file changed

+134
-1
lines changed

1 file changed

+134
-1
lines changed

drivers/usb/serial/f81232.c

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
{ USB_DEVICE(0x2c42, 0x1635) }, /* 8 port UART device */ \
3737
{ USB_DEVICE(0x2c42, 0x1636) } /* 12 port UART device */
3838

39+
#define F81534A_CTRL_ID \
40+
{ USB_DEVICE(0x2c42, 0x16f8) } /* Global control device */
41+
3942
static const struct usb_device_id f81232_id_table[] = {
4043
F81232_ID,
4144
{ } /* Terminating entry */
@@ -46,9 +49,15 @@ static const struct usb_device_id f81534a_id_table[] = {
4649
{ } /* Terminating entry */
4750
};
4851

52+
static const struct usb_device_id f81534a_ctrl_id_table[] = {
53+
F81534A_CTRL_ID,
54+
{ } /* Terminating entry */
55+
};
56+
4957
static const struct usb_device_id combined_id_table[] = {
5058
F81232_ID,
5159
F81534A_SERIES_ID,
60+
F81534A_CTRL_ID,
5261
{ } /* Terminating entry */
5362
};
5463
MODULE_DEVICE_TABLE(usb, combined_id_table);
@@ -61,6 +70,7 @@ MODULE_DEVICE_TABLE(usb, combined_id_table);
6170
#define F81232_REGISTER_REQUEST 0xa0
6271
#define F81232_GET_REGISTER 0xc0
6372
#define F81232_SET_REGISTER 0x40
73+
#define F81534A_ACCESS_REG_RETRY 2
6474

6575
#define SERIAL_BASE_ADDRESS 0x0120
6676
#define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
@@ -101,6 +111,8 @@ MODULE_DEVICE_TABLE(usb, combined_id_table);
101111
#define F81534A_GPIO_MODE1_OUTPUT BIT(1)
102112
#define F81534A_GPIO_MODE0_OUTPUT BIT(0)
103113

114+
#define F81534A_CTRL_CMD_ENABLE_PORT 0x116
115+
104116
struct f81232_private {
105117
struct mutex lock;
106118
u8 modem_control;
@@ -848,6 +860,93 @@ static void f81232_lsr_worker(struct work_struct *work)
848860
dev_warn(&port->dev, "read LSR failed: %d\n", status);
849861
}
850862

863+
static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
864+
u16 size, void *val)
865+
{
866+
struct usb_device *dev = interface_to_usbdev(intf);
867+
int retry = F81534A_ACCESS_REG_RETRY;
868+
int status;
869+
u8 *tmp;
870+
871+
tmp = kmemdup(val, size, GFP_KERNEL);
872+
if (!tmp)
873+
return -ENOMEM;
874+
875+
while (retry--) {
876+
status = usb_control_msg(dev,
877+
usb_sndctrlpipe(dev, 0),
878+
F81232_REGISTER_REQUEST,
879+
F81232_SET_REGISTER,
880+
reg,
881+
0,
882+
tmp,
883+
size,
884+
USB_CTRL_SET_TIMEOUT);
885+
if (status < 0) {
886+
status = usb_translate_errors(status);
887+
if (status == -EIO)
888+
continue;
889+
} else if (status != size) {
890+
/* Retry on short transfers */
891+
status = -EIO;
892+
continue;
893+
} else {
894+
status = 0;
895+
}
896+
897+
break;
898+
}
899+
900+
if (status) {
901+
dev_err(&intf->dev, "failed to set register 0x%x: %d\n",
902+
reg, status);
903+
}
904+
905+
kfree(tmp);
906+
return status;
907+
}
908+
909+
static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
910+
{
911+
unsigned char enable[2] = {0};
912+
int status;
913+
914+
/*
915+
* Enable all available serial ports, define as following:
916+
* bit 15 : Reset behavior (when HUB got soft reset)
917+
* 0: maintain all serial port enabled state.
918+
* 1: disable all serial port.
919+
* bit 0~11 : Serial port enable bit.
920+
*/
921+
if (en) {
922+
enable[0] = 0xff;
923+
enable[1] = 0x8f;
924+
}
925+
926+
status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
927+
sizeof(enable), enable);
928+
if (status)
929+
dev_err(&intf->dev, "failed to enable ports: %d\n", status);
930+
931+
return status;
932+
}
933+
934+
static int f81534a_ctrl_probe(struct usb_interface *intf,
935+
const struct usb_device_id *id)
936+
{
937+
return f81534a_ctrl_enable_all_ports(intf, true);
938+
}
939+
940+
static void f81534a_ctrl_disconnect(struct usb_interface *intf)
941+
{
942+
f81534a_ctrl_enable_all_ports(intf, false);
943+
}
944+
945+
static int f81534a_ctrl_resume(struct usb_interface *intf)
946+
{
947+
return f81534a_ctrl_enable_all_ports(intf, true);
948+
}
949+
851950
static int f81232_port_probe(struct usb_serial_port *port)
852951
{
853952
struct f81232_private *priv;
@@ -975,7 +1074,41 @@ static struct usb_serial_driver * const serial_drivers[] = {
9751074
NULL,
9761075
};
9771076

978-
module_usb_serial_driver(serial_drivers, combined_id_table);
1077+
static struct usb_driver f81534a_ctrl_driver = {
1078+
.name = "f81534a_ctrl",
1079+
.id_table = f81534a_ctrl_id_table,
1080+
.probe = f81534a_ctrl_probe,
1081+
.disconnect = f81534a_ctrl_disconnect,
1082+
.resume = f81534a_ctrl_resume,
1083+
};
1084+
1085+
static int __init f81232_init(void)
1086+
{
1087+
int status;
1088+
1089+
status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
1090+
KBUILD_MODNAME);
1091+
if (status)
1092+
return status;
1093+
1094+
status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
1095+
combined_id_table);
1096+
if (status) {
1097+
usb_deregister(&f81534a_ctrl_driver);
1098+
return status;
1099+
}
1100+
1101+
return 0;
1102+
}
1103+
1104+
static void __exit f81232_exit(void)
1105+
{
1106+
usb_serial_deregister_drivers(serial_drivers);
1107+
usb_deregister(&f81534a_ctrl_driver);
1108+
}
1109+
1110+
module_init(f81232_init);
1111+
module_exit(f81232_exit);
9791112

9801113
MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
9811114
MODULE_AUTHOR("Greg Kroah-Hartman <[email protected]>");

0 commit comments

Comments
 (0)