Skip to contentSkip to conversation

hid-mcp2200: added driver for GPIOs of MCP2200

PATCH Superseded Johannes Roith 3 participants 4 messages Jun 23, 2023 – Jul 30, 2023 HID

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hid-mcp2200: added driver for GPIOs of MCP2200

Added a gpiochip compatible driver to control the 8 GPIOs of the MCP2200 by using the HID interface.

Using GPIOs with alternative functions (GP0<->SSPND, GP1<->USBCFG, GP6<->RXLED, GP7<->TXLED) will reset the functions, if set (unset by default).

The driver was tested while also using the UART of the chip. Setting and reading the GPIOs has no effect on the UART communication. However, a reset is triggered after the CONFIGURE command. If the GPIO Direction is constantly changed, this will affect the communication at low baud rates. This is a hardware problem of the MCP2200 and is not caused by the driver.

Feedback from reviewers Christophe JAILLET <[email protected]> and Andi Shyti <[email protected]> was added.

---
 drivers/hid/Kconfig       |  10 +
 drivers/hid/Makefile      |   1 +
 drivers/hid/hid-ids.h     |   1 +
 drivers/hid/hid-mcp2200.c | 416 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 428 insertions(+)
 create mode 100644 drivers/hid/hid-mcp2200.c
drivers/hid/Kconfig +10 -0
index 4ce012f83253..ca7927d22c23 100644
@@ -1283,6 +1283,16 @@ config HID_MCP2221
 	To compile this driver as a module, choose M here: the module
 	will be called hid-mcp2221.ko.
 
+config HID_MCP2200
+   tristate "Microchip MCP2200 HID USB-to-GPIO bridge"
+   depends on USB_HID
+   imply GPIOLIB
+   help
+   Provides GPIO functionality over USB-HID through MCP2200 device.
+
+   To compile this driver as a module, choose M here: the module
+   will be called hid-mcp2200.ko.
+
 config HID_KUNIT_TEST
 	tristate "KUnit tests for HID" if !KUNIT_ALL_TESTS
 	depends on KUNIT=y
drivers/hid/Makefile +1 -0
index 5d37cacbde33..d593fb982f7d 100644
@@ -79,6 +79,7 @@ obj-$(CONFIG_HID_MACALLY)	+= hid-macally.o
 obj-$(CONFIG_HID_MAGICMOUSE)	+= hid-magicmouse.o
 obj-$(CONFIG_HID_MALTRON)	+= hid-maltron.o
 obj-$(CONFIG_HID_MCP2221)	+= hid-mcp2221.o
+obj-$(CONFIG_HID_MCP2200)	+= hid-mcp2200.o
 obj-$(CONFIG_HID_MAYFLASH)	+= hid-mf.o
 obj-$(CONFIG_HID_MEGAWORLD_FF)	+= hid-megaworld.o
 obj-$(CONFIG_HID_MICROSOFT)	+= hid-microsoft.o
drivers/hid/hid-ids.h +1 -0
index 5d29abac2300..017e37a171a8 100644
@@ -912,6 +912,7 @@
 #define USB_DEVICE_ID_PICK16F1454_V2	0xf2f7
 #define USB_DEVICE_ID_LUXAFOR		0xf372
 #define USB_DEVICE_ID_MCP2221		0x00dd
+#define USB_DEVICE_ID_MCP2200		0x00df
 
 #define USB_VENDOR_ID_MICROSOFT		0x045e
 #define USB_DEVICE_ID_SIDEWINDER_GV	0x003b
drivers/hid/hid-mcp2200.c new file +416 -0

416 lines not on this page · raw message

+

Signature · 1 line
2.41.0
#1 lore raw

Hi Johannes,

Quoted 14 lines · Johannes Roith

Added a gpiochip compatible driver to control the 8 GPIOs of the MCP2200 by using the HID interface.

Using GPIOs with alternative functions (GP0<->SSPND, GP1<->USBCFG, GP6<->RXLED, GP7<->TXLED) will reset the functions, if set (unset by default).

The driver was tested while also using the UART of the chip. Setting and reading the GPIOs has no effect on the UART communication. However, a reset is triggered after the CONFIGURE command. If the GPIO Direction is constantly changed, this will affect the communication at low baud rates. This is a hardware problem of the MCP2200 and is not caused by the driver.

Quoted 4 lines

Feedback from reviewers Christophe JAILLET <[email protected]> and Andi Shyti <[email protected]> was added.

Just one note... please add a versioning to your patches and a changelog.

I saw that your previous version did not receive any reply (sorry)... in that case you could add a prefix RESEND.

So that this patch would look like:

[PATCH RESEND v3] hid-mcp2200: Added driver for GPIOs of MCP2200

(note "Added" with capital letter)

[...]

Quoted 1 line

+/* Altternative pin assignments */

Alternative

Quoted 5 lines
drivers/hid/hid-mcp2200.c new file +5 -0
+#define TXLED		2
+#define RXLED		3
+#define USBCFG		6
+#define SSPND		7
+#define MCP_NGPIO	8

[...]

drivers/hid/hid-mcp2200.c line 109

Quoted 9 lines
drivers/hid/hid-mcp2200.c new file +9 -0
+static int mcp_cmd_read_all(struct mcp2200 *mcp)
+{
+	struct mcp_read_all *read_all;
+	int len, t;
+
+	reinit_completion(&mcp->wait_in_report);
+	read_all = kzalloc(sizeof(struct mcp_read_all), GFP_KERNEL);
+	if (!read_all)
+		return -ENOMEM;

any reason for allocating read_all dynamically?

drivers/hid/hid-mcp2200.c line 115

Quoted 6 lines
drivers/hid/hid-mcp2200.c new file +6 -0
+
+	read_all->cmd = READ_ALL;
+
+	mutex_lock(&mcp->lock);
+	len = hid_hw_output_report(mcp->hdev, (u8 *) read_all,
+			sizeof(struct mcp_read_all));

Please align "sizeof" with "mcp"

drivers/hid/hid-mcp2200.c line 123

Quoted 7 lines
drivers/hid/hid-mcp2200.c new file +7 -0
+	mutex_unlock(&mcp->lock);
+	kfree(read_all);
+
+	if (len != sizeof(struct mcp_read_all))
+		return -EINVAL;
+
+	t = wait_for_completion_timeout(&mcp->wait_in_report, msecs_to_jiffies(4000));

not an error, just a suggestion... would be nice to keep lines withing 80 characters whenever possible... and here it's possible :)

drivers/hid/hid-mcp2200.c line 132

Quoted 9 lines
drivers/hid/hid-mcp2200.c new file +9 -0
+	if (!t)
+		return -ETIMEDOUT;
+
+	/* return status, negative value if wrong response was received */
+	return mcp->status;
+}
+
+static void mcp_set_multiple(struct gpio_chip *gc, unsigned long *mask,
+				  unsigned long *bits)

please align "struct" with "unsigned"

Quoted 14 lines
drivers/hid/hid-mcp2200.c new file +14 -0
+{
+	struct mcp2200 *mcp = gpiochip_get_data(gc);
+	u8 value;
+	int status;
+	struct mcp_set_clear_outputs *cmd;
+
+	cmd = kzalloc(sizeof(struct mcp_set_clear_outputs), GFP_KERNEL);
+	if (!cmd)
+		return;
+
+	mutex_lock(&mcp->lock);
+
+	value = mcp->gpio_val & ~*mask;
+	value |= (*mask & *bits);

drivers/hid/hid-mcp2200.c line 156

Quoted 10 lines
drivers/hid/hid-mcp2200.c new file +10 -0
+
+	cmd->cmd = SET_CLEAR_OUTPUTS;
+	cmd->set_bmap = value;
+	cmd->clear_bmap = ~(value);
+
+	status = hid_hw_output_report(mcp->hdev, (u8 *) cmd,
+		       sizeof(struct mcp_set_clear_outputs));
+
+	mutex_unlock(&mcp->lock);
+	kfree(cmd);

again, any reason for allocating dynamically cmd?

drivers/hid/hid-mcp2200.c line 159

Quoted 2 lines
drivers/hid/hid-mcp2200.c new file +2 -0
+	if (status == sizeof(struct mcp_set_clear_outputs))
+		mcp->gpio_val = value;

should this be inside the lock?

Quoted 1 line

+}

[...]

drivers/hid/hid-mcp2200.c line 192

Quoted 7 lines
drivers/hid/hid-mcp2200.c new file +7 -0
+static int mcp_get(struct gpio_chip *gc, unsigned int gpio_nr)
+{
+	unsigned long mask = 0, bits = 0;
+
+	mask = (1 << gpio_nr);
+	mcp_get_multiple(gc, &mask, &bits);
+	return (bits > 0) ? 1 : 0;

eventually you could just:

	return bits > 0;
Quoted 1 line

+}

[...]

drivers/hid/hid-mcp2200.c line 344

Quoted 13 lines
drivers/hid/hid-mcp2200.c new file +13 -0
+static int mcp2200_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+	int ret;
+	struct mcp2200 *mcp;
+
+	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
+	if (!mcp)
+		return -ENOMEM;
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "can't parse reports\n");
+		return ret;

Is it dev_err_probe() preferred to hid_err() here?

Quoted 19 lines
drivers/hid/hid-mcp2200.c new file +19 -0
+	}
+
+	/*
+	 * This driver uses the .raw_event callback and therefore does not need any
+	 * HID_CONNECT_xxx flags.
+	 */
+	ret = hid_hw_start(hdev, 0);
+	if (ret) {
+		hid_err(hdev, "can't start hardware\n");
+		return ret;
+	}
+
+	hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
+			hdev->version & 0xff, hdev->name, hdev->phys);
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "can't open device\n");
+		hid_hw_stop(hdev);

drivers/hid/hid-mcp2200.c line 373

Quoted 10 lines
drivers/hid/hid-mcp2200.c new file +10 -0
+		return ret;
+	}
+
+	mutex_init(&mcp->lock);
+	init_completion(&mcp->wait_in_report);
+	hid_set_drvdata(hdev, mcp);
+	mcp->hdev = hdev;
+
+	ret = devm_add_action_or_reset(&hdev->dev, mcp2200_hid_unregister, hdev);
+	if (ret)

hit_hw_stop()?

Maybe a goto is better?

Andi

Quoted 14 lines
drivers/hid/hid-mcp2200.c new file +14 -0
+		return ret;
+
+	mcp->gc = template_chip;
+	mcp->gc.parent = &hdev->dev;
+
+	ret = gpiochip_add_data(&mcp->gc, mcp);
+	if (ret < 0) {
+		dev_err(&hdev->dev, "Unable to register gpiochip\n");
+		hid_hw_stop(hdev);
+		return ret;
+	}
+
+	return 0;
+}
#2 lore raw
In reply to Johannes Roith

Le 23/06/2023 à 13:01, Johannes Roith a écrit :

Quoted 19 lines

Added a gpiochip compatible driver to control the 8 GPIOs of the MCP2200 by using the HID interface.

Using GPIOs with alternative functions (GP0<->SSPND, GP1<->USBCFG, GP6<->RXLED, GP7<->TXLED) will reset the functions, if set (unset by default).

The driver was tested while also using the UART of the chip. Setting and reading the GPIOs has no effect on the UART communication. However, a reset is triggered after the CONFIGURE command. If the GPIO Direction is constantly changed, this will affect the communication at low baud rates. This is a hardware problem of the MCP2200 and is not caused by the driver.

Feedback from reviewers Christophe JAILLET <[email protected]> and Andi Shyti <[email protected]> was added.

---

[...]

Quoted 38 lines
drivers/hid/hid-mcp2200.c new file +38 -0
+static int mcp2200_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+	int ret;
+	struct mcp2200 *mcp;
+
+	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
+	if (!mcp)
+		return -ENOMEM;
+
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "can't parse reports\n");
+		return ret;
+	}
+
+	/*
+	 * This driver uses the .raw_event callback and therefore does not need any
+	 * HID_CONNECT_xxx flags.
+	 */
+	ret = hid_hw_start(hdev, 0);
+	if (ret) {
+		hid_err(hdev, "can't start hardware\n");
+		return ret;
+	}
+
+	hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
+			hdev->version & 0xff, hdev->name, hdev->phys);
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "can't open device\n");
+		hid_hw_stop(hdev);
+		return ret;
+	}
+
+	mutex_init(&mcp->lock);
+	init_completion(&mcp->wait_in_report);
+	hid_set_drvdata(hdev, mcp);

drivers/hid/hid-mcp2200.c line 379

Quoted 10 lines
drivers/hid/hid-mcp2200.c new file +10 -0
+	mcp->hdev = hdev;
+
+	ret = devm_add_action_or_reset(&hdev->dev, mcp2200_hid_unregister, hdev);
+	if (ret)
+		return ret;
+
+	mcp->gc = template_chip;
+	mcp->gc.parent = &hdev->dev;
+
+	ret = gpiochip_add_data(&mcp->gc, mcp);

devm_gpiochip_add_data()?

drivers/hid/hid-mcp2200.c line 381

Quoted 2 lines
drivers/hid/hid-mcp2200.c new file +2 -0
+	if (ret < 0) {
+		dev_err(&hdev->dev, "Unable to register gpiochip\n");

hid_err() to be consistent?

Review

Quoted 1 line
(quoted diff) +1 -0
+		hid_hw_stop(hdev);

Isn't it already run by mcp2200_hid_unregister() registered a few lines above?

CJ

Quoted 14 lines
drivers/hid/hid-mcp2200.c new file +14 -0
+		return ret;
+	}
+
+	return 0;
+}
+
+static void mcp2200_remove(struct hid_device *hdev)
+{
+	struct mcp2200 *mcp;
+
+	mcp = hid_get_drvdata(hdev);
+	gpiochip_remove(&mcp->gc);
+}
+

[...]

#3 lore raw

Hello Andi, hello Christophe,

thanks for the feedback and the hints. I will add most of your suggestions to the driver and submit a new patch, this time with a correct Version Label.

I need to use dynamic memory allocation for the data passed to hid_hw_output_report, overwise the function doesn't work, 0 bytes will be sent. The memory has to be allocated dynamically because of the DMA usage in the background.

But all the over feedback looks good and will be added to the driver.

Best regards,
Johannes