Skip to contentSkip to conversation

USB: serial: pl2303: add PL2303G GPIO_A and GPIO_B functions

PATCH In review Charles Yeh 3 participants 3 messages Jun 18, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: serial: pl2303: add PL2303G GPIO_A and GPIO_B functions

PL2303G (TYPE_HXN) series ICs: PL2303GC, PL2303GS, PL2303GT, PL2303GR,.. etc.

For example:
PL2303GC can provide up to 16 GPIO (general purpose input/output) signals. they are composed of GPIO_A0~A7 and GPIO_B0~B7 respectively.

In addition to the original UART functions such as Tx, Rx, RTS, CTS, etc., users can also use GPIO to implement certain applications, such as turning on/off the LED power.

Users can download the PL2303G Linux GPIO app: PL2303G_Linux_GPIO_Test.c from the Prolific website.

---
 drivers/usb/serial/pl2303.c | 182 ++++++++++++++++++++++++++++++++++++
 1 file changed, 182 insertions(+)
drivers/usb/serial/pl2303.c +182 -0
index 22579d0d8ab8..be7e695bea60 100644
@@ -175,6 +175,37 @@ MODULE_DEVICE_TABLE(usb, id_table);
 #define PL2303_HXN_FLOWCTRL_RTS_CTS	0x18
 #define PL2303_HXN_FLOWCTRL_XON_XOFF	0x0c
 
+
+struct PL2303G_GPIO {
+	u8 GP_Branch;
+	u8 Number;
+	u8 Value;
+};
+
+
+#define GPIO_AB_CONTROL_MODE		_IOW(0x81, 10, struct PL2303G_GPIO)
+#define GPIO_AB_OUTPUT_MODE		_IOW(0x81, 11, struct PL2303G_GPIO)
+#define GPIO_AB_SET_VALUE		_IOW(0x81, 12, struct PL2303G_GPIO)
+#define GPIO_AB_GET_VALUE		_IOR(0x81, 13, struct PL2303G_GPIO)
+
+#define GPIO_A_DATA_REG		0x00
+#define GPIO_A_OE_REG		0x02
+#define GPIO_A_CE_REG		0x04
+
+#define GPIO_B_DATA_REG		0x01
+#define GPIO_B_OE_REG		0x03
+#define GPIO_B_CE_REG		0x05
+
+#define PL2303N_CE_MODE		0
+#define PL2303N_OE_MODE		1
+#define PL2303N_SET_MODE	2
+#define PL2303N_GET_MODE	2
+
+#define GET_GPIO_REG(mode, branch) \
+		((mode) == 0 ? ((branch) == 0 ? GPIO_A_CE_REG : GPIO_B_CE_REG) : \
+		(mode) == 1 ? ((branch) == 0 ? GPIO_A_OE_REG : GPIO_B_OE_REG) : \
+					((branch) == 0 ? GPIO_A_DATA_REG : GPIO_B_DATA_REG))
+
 static int pl2303_set_break(struct usb_serial_port *port, bool enable);
 
 enum pl2303_type {
@@ -1095,6 +1126,156 @@ static int pl2303_carrier_raised(struct usb_serial_port *port)
 	return 0;
 }
 
+static int pl2303N_gpio_get(struct usb_serial *serial, void __user *data, u8 u8Mode)
+{
+	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
+	struct PL2303G_GPIO gpio;
+	unsigned char *buf;
+	u8 channel;
+	u8 kdatPar;
+	int retval = 0;
+
+	/* only support TYPE_HXN */
+	if (spriv->type != &pl2303_type_data[TYPE_HXN])
+		return -ENOIOCTLCMD;
+
+	buf = kmalloc(1, GFP_KERNEL);
+	if (!buf)
+		return -ENOIOCTLCMD;
+
+	if (copy_from_user(&gpio, data, sizeof(gpio))) {
+		retval = -EFAULT;
+		goto err_out;
+	}
+
+	channel = GET_GPIO_REG(u8Mode, gpio.GP_Branch);
+
+	retval = pl2303_vendor_read(serial, channel, buf);
+
+	if (retval < 0)
+		goto err_out;
+
+	if (gpio.Number <= 7)
+		kdatPar = 1 << gpio.Number;
+	else
+		kdatPar = 0x01;
+
+	if (*buf & kdatPar)
+		gpio.Value = 1;
+	else
+		gpio.Value = 0;
+
+	if (copy_to_user(data, &gpio, sizeof(gpio))) {
+		retval = -EFAULT;
+		goto err_out;
+	}
+
+err_out:
+
+	kfree(buf);
+
+	return retval;
+
+}
+
+static int pl2303N_gpio_set(struct usb_serial *serial, void __user *data, u8 u8Mode)
+{
+	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
+	struct PL2303G_GPIO gpio;
+	unsigned char *buf;
+	u8 kdatPar;
+	u8 channel;
+	int retval = 0;
+
+	/* only support TYPE_HXN */
+	if (spriv->type != &pl2303_type_data[TYPE_HXN])
+		return -ENOIOCTLCMD;
+
+	buf = kmalloc(1, GFP_KERNEL);
+	if (!buf)
+		return -ENOIOCTLCMD;
+
+	if (copy_from_user(&gpio, data, sizeof(gpio))) {
+		retval = -EFAULT;
+		goto err_out;
+	}
+
+	channel = GET_GPIO_REG(u8Mode, gpio.GP_Branch);
+
+	retval = pl2303_vendor_read(serial, channel, buf);
+
+	if (retval < 0)
+		goto err_out;
+
+	if (gpio.Number <= 7)
+		kdatPar = 1 << gpio.Number;
+	else
+		kdatPar = 0x01;
+
+	if (gpio.Value)
+		*buf |= kdatPar;
+	else
+		*buf &= ~kdatPar;
+
+	retval = pl2303_vendor_write(serial, channel, *buf);
+
+	if (retval < 0)
+		goto err_out;
+
+err_out:
+
+	kfree(buf);
+
+	return retval;
+
+}
+
+
+static int pl2303_ioctl(struct tty_struct *tty,
+			unsigned int cmd, unsigned long arg)
+{
+	int retval = 0;
+	struct serial_struct ser;
+	struct usb_serial_port *port = tty->driver_data;
+	struct usb_serial *serial = port->serial;
+
+	switch (cmd) {
+	case TIOCGSERIAL:
+		memset(&ser, 0, sizeof(ser));
+		ser.type = PORT_16654;
+		ser.line = port->minor;
+		ser.port = port->port_number;
+		ser.baud_base = 460800;
+
+		if (copy_to_user((void __user *)arg, &ser, sizeof(ser)))
+			return -EFAULT;
+
+		return 0;
+	case GPIO_AB_CONTROL_MODE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_CE_MODE);
+		break;
+	case GPIO_AB_OUTPUT_MODE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_OE_MODE);
+		break;
+	case GPIO_AB_SET_VALUE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_SET_MODE);
+		break;
+	case GPIO_AB_GET_VALUE:
+		retval = pl2303N_gpio_get(serial, (void __user *) arg,
+								PL2303N_GET_MODE);
+		break;
+	default:
+		retval = -ENOIOCTLCMD;
+		break;
+	}
+
+	return retval;
+
+}
+
 static int pl2303_set_break(struct usb_serial_port *port, bool enable)
 {
 	struct usb_serial *serial = port->serial;
@@ -1285,6 +1466,7 @@ static struct usb_serial_driver pl2303_device = {
 	.close =		pl2303_close,
 	.dtr_rts =		pl2303_dtr_rts,
 	.carrier_raised =	pl2303_carrier_raised,
+	.ioctl =		pl2303_ioctl,
 	.break_ctl =		pl2303_break_ctl,
 	.set_termios =		pl2303_set_termios,
 	.tiocmget =		pl2303_tiocmget,
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 23 lines · Charles Yeh

PL2303G (TYPE_HXN) series ICs: PL2303GC, PL2303GS, PL2303GT, PL2303GR,.. etc.

For example:
PL2303GC can provide up to 16 GPIO (general purpose input/output) signals. they are composed of GPIO_A0~A7 and GPIO_B0~B7 respectively.

In addition to the original UART functions such as Tx, Rx, RTS, CTS, etc., users can also use GPIO to implement certain applications, such as turning on/off the LED power.

Users can download the PL2303G Linux GPIO app: PL2303G_Linux_GPIO_Test.c from the Prolific website.

---
 drivers/usb/serial/pl2303.c | 182 ++++++++++++++++++++++++++++++++++++
 1 file changed, 182 insertions(+)
drivers/usb/serial/pl2303.c +0 -0
index 22579d0d8ab8..be7e695bea60 100644

drivers/usb/serial/pl2303.c MODULE_DEVICE_TABLE(usb, id_table); · line 182

Quoted 8 lines
drivers/usb/serial/pl2303.c +5 -0
@@ -175,6 +175,37 @@ MODULE_DEVICE_TABLE(usb, id_table);
 #define PL2303_HXN_FLOWCTRL_RTS_CTS	0x18
 #define PL2303_HXN_FLOWCTRL_XON_XOFF	0x0c
 
+
+struct PL2303G_GPIO {
+	u8 GP_Branch;
+	u8 Number;
+	u8 Value;

We use lower case symbol names in the kernel.

drivers/usb/serial/pl2303.c MODULE_DEVICE_TABLE(usb, id_table); · line 189

Quoted 7 lines
drivers/usb/serial/pl2303.c +7 -0
+};
+
+
+#define GPIO_AB_CONTROL_MODE		_IOW(0x81, 10, struct PL2303G_GPIO)
+#define GPIO_AB_OUTPUT_MODE		_IOW(0x81, 11, struct PL2303G_GPIO)
+#define GPIO_AB_SET_VALUE		_IOW(0x81, 12, struct PL2303G_GPIO)
+#define GPIO_AB_GET_VALUE		_IOR(0x81, 13, struct PL2303G_GPIO)

No, custom ioctls won't do. We have a standard interface for accessing GPIOs so you need to model this as gpio chips similar to what the ftdi_sio and cp210x drivers do.

Quoted 10 lines
drivers/usb/serial/pl2303.c +10 -0
+static int pl2303_ioctl(struct tty_struct *tty,
+			unsigned int cmd, unsigned long arg)
+{
+	int retval = 0;
+	struct serial_struct ser;
+	struct usb_serial_port *port = tty->driver_data;
+	struct usb_serial *serial = port->serial;
+
+	switch (cmd) {
+	case TIOCGSERIAL:

drivers/usb/serial/pl2303.c static int pl2303_carrier_raised(struct usb_serial_port *port) · line 1253

Quoted 10 lines
drivers/usb/serial/pl2303.c +10 -0
+		memset(&ser, 0, sizeof(ser));
+		ser.type = PORT_16654;
+		ser.line = port->minor;
+		ser.port = port->port_number;
+		ser.baud_base = 460800;
+
+		if (copy_to_user((void __user *)arg, &ser, sizeof(ser)))
+			return -EFAULT;
+
+		return 0;

This looks unrelated.

Quoted 14 lines
drivers/usb/serial/pl2303.c +14 -0
+	case GPIO_AB_CONTROL_MODE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_CE_MODE);
+		break;
+	case GPIO_AB_OUTPUT_MODE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_OE_MODE);
+		break;
+	case GPIO_AB_SET_VALUE:
+		retval = pl2303N_gpio_set(serial, (void __user *) arg,
+								PL2303N_SET_MODE);
+		break;
+	case GPIO_AB_GET_VALUE:
+		retval = pl2303N_gpio_get(serial, (void __user *) arg,

drivers/usb/serial/pl2303.c static int pl2303_carrier_raised(struct usb_serial_port *port) · line 1277

Quoted 10 lines
drivers/usb/serial/pl2303.c +10 -0
+								PL2303N_GET_MODE);
+		break;
+	default:
+		retval = -ENOIOCTLCMD;
+		break;
+	}
+
+	return retval;
+
+}

Johan

maintainer
#2 lore raw
In reply to Charles Yeh
Quoted 23 lines · Charles Yeh

PL2303G (TYPE_HXN) series ICs: PL2303GC, PL2303GS, PL2303GT, PL2303GR,.. etc.

For example:
PL2303GC can provide up to 16 GPIO (general purpose input/output) signals. they are composed of GPIO_A0~A7 and GPIO_B0~B7 respectively.

In addition to the original UART functions such as Tx, Rx, RTS, CTS, etc., users can also use GPIO to implement certain applications, such as turning on/off the LED power.

Users can download the PL2303G Linux GPIO app: PL2303G_Linux_GPIO_Test.c from the Prolific website.

---
 drivers/usb/serial/pl2303.c | 182 ++++++++++++++++++++++++++++++++++++
 1 file changed, 182 insertions(+)
drivers/usb/serial/pl2303.c +0 -0
index 22579d0d8ab8..be7e695bea60 100644

drivers/usb/serial/pl2303.c MODULE_DEVICE_TABLE(usb, id_table); · line 179

Quoted 5 lines
drivers/usb/serial/pl2303.c +2 -0
@@ -175,6 +175,37 @@ MODULE_DEVICE_TABLE(usb, id_table);
 #define PL2303_HXN_FLOWCTRL_RTS_CTS	0x18
 #define PL2303_HXN_FLOWCTRL_XON_XOFF	0x0c
 
+
+struct PL2303G_GPIO {

Didn't checkpatch complain about this? Linux structures are all lowercase.

drivers/usb/serial/pl2303.c MODULE_DEVICE_TABLE(usb, id_table); · line 182

Quoted 3 lines
drivers/usb/serial/pl2303.c +3 -0
+	u8 GP_Branch;
+	u8 Number;
+	u8 Value;

Same here, always use checkpatch.pl on your changes before sending them out to catch basic coding style issues please.

drivers/usb/serial/pl2303.c MODULE_DEVICE_TABLE(usb, id_table); · line 189

Quoted 7 lines
drivers/usb/serial/pl2303.c +7 -0
+};
+
+
+#define GPIO_AB_CONTROL_MODE		_IOW(0x81, 10, struct PL2303G_GPIO)
+#define GPIO_AB_OUTPUT_MODE		_IOW(0x81, 11, struct PL2303G_GPIO)
+#define GPIO_AB_SET_VALUE		_IOW(0x81, 12, struct PL2303G_GPIO)
+#define GPIO_AB_GET_VALUE		_IOR(0x81, 13, struct PL2303G_GPIO)

Why are you adding custom ioctls for just this driver?

And where is the user/kernel api definition of this so that userspace could know about it properly? You can't bury it in a driver like this.

And where did 0x81 and the other numbers come from?

Also, the structure here will not work in the user/kernel api at all, always use the proper user/kernel data structures if you are going to do this.

But overall, no, don't add a new user/kernel api for gpio devices, we have at least 2 already, please don't add another one. What is wrong with the current ones?

drivers/usb/serial/pl2303.c static int pl2303_carrier_raised(struct usb_serial_port *port) · line 1248

Quoted 7 lines
drivers/usb/serial/pl2303.c +7 -0
+	switch (cmd) {
+	case TIOCGSERIAL:
+		memset(&ser, 0, sizeof(ser));
+		ser.type = PORT_16654;
+		ser.line = port->minor;
+		ser.port = port->port_number;
+		ser.baud_base = 460800;

Are you sure this baud_base is correct?

thanks,

greg k-h