Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 28 Aug 2020 10:27:24 +0000 (UTC)
From:      Andriy Gapon <avg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r364912 - in stable/12: share/man/man4 sys/conf sys/dev/usb/misc sys/modules/usb sys/modules/usb/cp2112
Message-ID:  <202008281027.07SAROiY029076@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: avg
Date: Fri Aug 28 10:27:24 2020
New Revision: 364912
URL: https://svnweb.freebsd.org/changeset/base/364912

Log:
  MFC r363951,r364143,r364144: cp2112: driver for the namesake GPIO and I2C master gadget

Added:
  stable/12/share/man/man4/cp2112.4
     - copied unchanged from r364144, head/share/man/man4/cp2112.4
  stable/12/sys/dev/usb/misc/cp2112.c
     - copied, changed from r363951, head/sys/dev/usb/misc/cp2112.c
  stable/12/sys/modules/usb/cp2112/
     - copied from r363951, head/sys/modules/usb/cp2112/
Modified:
  stable/12/sys/conf/files
  stable/12/sys/modules/usb/Makefile
Directory Properties:
  stable/12/   (props changed)

Copied: stable/12/share/man/man4/cp2112.4 (from r364144, head/share/man/man4/cp2112.4)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/share/man/man4/cp2112.4	Fri Aug 28 10:27:24 2020	(r364912, copy of r364144, head/share/man/man4/cp2112.4)
@@ -0,0 +1,87 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+.\"
+.\" Copyright (c) 2020 Andriy Gapon <avg@FreeBSD.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd August 12, 2020
+.Dt CP2112 4
+.Os
+.Sh NAME
+.Nm cp2112
+.Nd driver for a USB GPIO and I2C peripheral device
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following lines in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device cp2112"
+.Cd "device usb"
+.Cd "device gpio"
+.Cd "device iicbus"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+cp2112_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for Silicon Labs CP2112 device.
+The device has 8 general purpose I/O pins and an I2C controller that supports
+a subset of the I2C protocol.
+.Pp
+All pins support both input and output modes.
+An output pin can be configured either for open-drain or push-pull operation.
+Pins 0, 1 and 7 support special functions: I2C transmit indication,
+I2C receive indication and clock output respectively.
+At the moment the
+.Nm
+driver does not provide a way to enable and configure the special functions.
+.Pp
+The I2C controller supports read transactions with up to 512 bytes of data,
+write transactions with up to 61 bytes of data and a write followed by
+the repeated start followed by a read transactions where the write can be
+up to 16 bytes and the read can be up to 512 bytes.
+Zero length transfers are not supported.
+The
+.Nm
+driver creates a
+.Xr gpio 4
+and
+.Xr iicbus 4
+child buses to expose the respective functions.
+.Sh SEE ALSO
+.Xr gpio 4 ,
+.Xr iicbus 4 ,
+.Xr usb 4
+.Sh HISTORY
+The
+.Nm
+driver and this manual page was written by
+.An Andriy Gapon Aq Mt avg@FreeBSD.org .

Modified: stable/12/sys/conf/files
==============================================================================
--- stable/12/sys/conf/files	Fri Aug 28 10:21:11 2020	(r364911)
+++ stable/12/sys/conf/files	Fri Aug 28 10:27:24 2020	(r364912)
@@ -3438,6 +3438,7 @@ dev/usb/serial/usb_serial.c 	optional ucom | u3g | uar
 #
 # USB misc drivers
 #
+dev/usb/misc/cp2112.c		optional cp2112
 dev/usb/misc/ufm.c		optional ufm
 dev/usb/misc/udbp.c		optional udbp
 dev/usb/misc/ugold.c		optional ugold

Copied and modified: stable/12/sys/dev/usb/misc/cp2112.c (from r363951, head/sys/dev/usb/misc/cp2112.c)
==============================================================================
--- head/sys/dev/usb/misc/cp2112.c	Thu Aug  6 13:41:42 2020	(r363951, copy source)
+++ stable/12/sys/dev/usb/misc/cp2112.c	Fri Aug 28 10:27:24 2020	(r364912)
@@ -66,6 +66,8 @@ __FBSDID("$FreeBSD$");
 #define	USB_DEBUG_VAR usb_debug
 #include <dev/usb/usb_debug.h>
 
+#define	SIZEOF_FIELD(_s, _f)	sizeof(((struct _s *)NULL)->_f)
+
 #define	CP2112GPIO_LOCK(sc)	sx_xlock(&sc->gpio_lock)
 #define	CP2112GPIO_UNLOCK(sc)	sx_xunlock(&sc->gpio_lock)
 #define	CP2112GPIO_LOCKED(sc)	sx_assert(&sc->gpio_lock, SX_XLOCKED)
@@ -93,8 +95,13 @@ __FBSDID("$FreeBSD$");
 #define	CP2112_REQ_LOCK			0x20
 #define	CP2112_REQ_USB_CFG		0x21
 
+#define	CP2112_IIC_MAX_READ_LEN		512
 #define	CP2112_IIC_REPSTART_VER		2	/* Erratum CP2112_E10. */
 
+#define	CP2112_GPIO_SPEC_CLK7		1	/* Pin 7 is clock output. */
+#define	CP2112_GPIO_SPEC_TX0		2	/* Pin 0 pulses on USB TX. */
+#define	CP2112_GPIO_SPEC_RX1		4	/* Pin 1 pulses on USB RX. */
+
 #define	CP2112_IIC_STATUS0_IDLE		0
 #define	CP2112_IIC_STATUS0_BUSY		1
 #define	CP2112_IIC_STATUS0_CMP		2
@@ -104,7 +111,112 @@ __FBSDID("$FreeBSD$");
 #define	CP2112_IIC_STATUS1_TIMEOUT_BUS	1
 #define	CP2112_IIC_STATUS1_ARB_LOST	2
 
+/* CP2112_REQ_VERSION */
+struct version_request {
+	uint8_t id;
+	uint8_t part_num;
+	uint8_t version;
+} __packed;
 
+/* CP2112_REQ_GPIO_GET */
+struct gpio_get_req {
+	uint8_t id;
+	uint8_t state;
+} __packed;
+
+/* CP2112_REQ_GPIO_SET */
+struct gpio_set_req {
+	uint8_t id;
+	uint8_t state;
+	uint8_t mask;
+} __packed;
+
+/* CP2112_REQ_GPIO_CFG */
+struct gpio_config_req {
+	uint8_t id;
+	uint8_t output;
+	uint8_t pushpull;
+	uint8_t special;
+	uint8_t divider;
+} __packed;
+
+/* CP2112_REQ_SMB_XFER_STATUS_REQ */
+struct i2c_xfer_status_req {
+	uint8_t id;
+	uint8_t request;
+} __packed;
+
+/* CP2112_REQ_SMB_XFER_STATUS_RESP */
+struct i2c_xfer_status_resp {
+	uint8_t id;
+	uint8_t status0;
+	uint8_t status1;
+	uint16_t status2;
+	uint16_t status3;
+} __packed;
+
+/* CP2112_REQ_SMB_READ_FORCE_SEND */
+struct i2c_data_read_force_send_req {
+	uint8_t id;
+	uint16_t len;
+} __packed;
+
+/* CP2112_REQ_SMB_READ_RESPONSE */
+struct i2c_data_read_resp {
+	uint8_t id;
+	uint8_t status;
+	uint8_t len;
+	uint8_t data[61];
+} __packed;
+
+/* CP2112_REQ_SMB_READ */
+struct i2c_write_read_req {
+	uint8_t id;
+	uint8_t slave;
+	uint16_t rlen;
+	uint8_t wlen;
+	uint8_t wdata[16];
+} __packed;
+
+/* CP2112_REQ_SMB_WRITE */
+struct i2c_read_req {
+	uint8_t id;
+	uint8_t slave;
+	uint16_t len;
+} __packed;
+
+/* CP2112_REQ_SMB_WRITE_READ */
+struct i2c_write_req {
+	uint8_t id;
+	uint8_t slave;
+	uint8_t len;
+	uint8_t data[61];
+} __packed;
+
+/* CP2112_REQ_SMB_CFG */
+struct i2c_cfg_req {
+	uint8_t		id;
+	uint32_t	speed;		/* Hz */
+	uint8_t		slave_addr;	/* ACK only */
+	uint8_t		auto_send_read;	/* boolean */
+	uint16_t	write_timeout;	/* 0-1000 ms, 0 ~ no timeout */
+	uint16_t	read_timeout;	/* 0-1000 ms, 0 ~ no timeout */
+	uint8_t		scl_low_timeout;/* boolean */
+	uint16_t	retry_count;	/* 1-1000, 0 ~ forever */
+} __packed;
+
+enum cp2112_out_mode {
+	OUT_OD,
+	OUT_PP,
+	OUT_KEEP
+};
+
+enum {
+	CP2112_INTR_OUT = 0,
+	CP2112_INTR_IN,
+	CP2112_N_TRANSFER,
+};
+
 struct cp2112_softc {
 	device_t		sc_gpio_dev;
 	device_t		sc_iic_dev;
@@ -120,10 +232,38 @@ struct cp2112gpio_softc {
 	struct gpio_pin		pins[CP2112_GPIO_COUNT];
 };
 
+struct cp2112iic_softc {
+	device_t	dev;
+	device_t	iicbus_dev;
+	struct usb_xfer	*xfers[CP2112_N_TRANSFER];
+	u_char		own_addr;
+	struct {
+		struct mtx	lock;
+		struct cv	cv;
+		struct {
+			uint8_t		*data;
+			int		len;
+			int		done;
+			int		error;
+		}		in;
+		struct {
+			const uint8_t	*data;
+			int		len;
+			int		done;
+			int		error;
+		}		out;
+	}		io;
+};
+
 static int cp2112_detach(device_t dev);
 static int cp2112gpio_detach(device_t dev);
 static int cp2112iic_detach(device_t dev);
 
+static const STRUCT_USB_HOST_ID cp2112_devs[] = {
+	{ USB_VP(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2112) },
+	{ USB_VP(0x1009, USB_PRODUCT_SILABS_CP2112) }, /* XXX */
+};
+
 static int
 cp2112_get_report(device_t dev, uint8_t id, void *data, uint16_t len)
 {
@@ -143,25 +283,103 @@ cp2112_set_report(device_t dev, uint8_t id, void *data
 	int err;
 
 	sc = device_get_softc(dev);
+	*(uint8_t *)data = id;
 	err = usbd_req_set_report(sc->sc_udev, NULL, data,
 	    len, sc->sc_iface_index, UHID_FEATURE_REPORT, id);
 	return (err);
 }
 
 static int
+cp2112_probe(device_t dev)
+{
+	struct usb_attach_arg *uaa;
+
+	uaa = device_get_ivars(dev);
+	if (uaa->usb_mode != USB_MODE_HOST)
+		return (ENXIO);
+	if (uaa->info.bInterfaceClass != UICLASS_HID)
+		return (ENXIO);
+
+	if (usbd_lookup_id_by_uaa(cp2112_devs, sizeof(cp2112_devs), uaa) == 0)
+		return (BUS_PROBE_DEFAULT);
+	return (ENXIO);
+}
+
+static int
+cp2112_attach(device_t dev)
+{
+	struct version_request vdata;
+	struct usb_attach_arg *uaa;
+	struct cp2112_softc *sc;
+	int err;
+
+	uaa = device_get_ivars(dev);
+	sc = device_get_softc(dev);
+
+	device_set_usb_desc(dev);
+
+	sc->sc_udev = uaa->device;
+	sc->sc_iface_index = uaa->info.bIfaceIndex;
+
+	err = cp2112_get_report(dev, CP2112_REQ_VERSION, &vdata, sizeof(vdata));
+	if (err != 0)
+		goto detach;
+	device_printf(dev, "part number 0x%02x, version 0x%02x\n",
+	    vdata.part_num, vdata.version);
+	if (vdata.part_num != CP2112_PART_NUM) {
+		device_printf(dev, "unsupported part number\n");
+		goto detach;
+	}
+	sc->sc_version = vdata.version;
+	sc->sc_gpio_dev = device_add_child(dev, "gpio", -1);
+	if (sc->sc_gpio_dev != NULL) {
+		err = device_probe_and_attach(sc->sc_gpio_dev);
+		if (err != 0) {
+			device_printf(dev, "failed to attach gpio child\n");
+		}
+	} else {
+		device_printf(dev, "failed to create gpio child\n");
+	}
+
+	sc->sc_iic_dev = device_add_child(dev, "iichb", -1);
+	if (sc->sc_iic_dev != NULL) {
+		err = device_probe_and_attach(sc->sc_iic_dev);
+		if (err != 0) {
+			device_printf(dev, "failed to attach iic child\n");
+		}
+	} else {
+		device_printf(dev, "failed to create iic child\n");
+	}
+
+	return (0);
+
+detach:
+	cp2112_detach(dev);
+	return (ENXIO);
+}
+
+static int
+cp2112_detach(device_t dev)
+{
+	int err;
+
+	err = bus_generic_detach(dev);
+	if (err != 0)
+		return (err);
+	device_delete_children(dev);
+	return (0);
+}
+
+static int
 cp2112_gpio_read_pin(device_t dev, uint32_t pin_num, bool *on)
 {
-	struct {
-		uint8_t id;
-		uint8_t state;
-	} __packed data;
+	struct gpio_get_req data;
 	struct cp2112gpio_softc *sc;
 	int err;
 
 	sc = device_get_softc(dev);
 	CP2112GPIO_LOCKED(sc);
 
-	data.id = CP2112_REQ_GPIO_GET;
 	err = cp2112_get_report(device_get_parent(dev),
 	    CP2112_REQ_GPIO_GET, &data, sizeof(data));
 	if (err != 0)
@@ -174,11 +392,7 @@ cp2112_gpio_read_pin(device_t dev, uint32_t pin_num, b
 static int
 cp2112_gpio_write_pin(device_t dev, uint32_t pin_num, bool on)
 {
-	struct {
-		uint8_t id;
-		uint8_t state;
-		uint8_t mask;
-	} __packed data;
+	struct gpio_set_req data;
 	struct cp2112gpio_softc *sc;
 	int err;
 	bool actual;
@@ -186,10 +400,8 @@ cp2112_gpio_write_pin(device_t dev, uint32_t pin_num, 
 	sc = device_get_softc(dev);
 	CP2112GPIO_LOCKED(sc);
 
-	data.id = CP2112_REQ_GPIO_SET;
 	data.state = (uint8_t)on << pin_num;
 	data.mask = (uint8_t)1 << pin_num;
-
 	err = cp2112_set_report(device_get_parent(dev),
 	    CP2112_REQ_GPIO_SET, &data, sizeof(data));
 	if (err != 0)
@@ -204,15 +416,9 @@ cp2112_gpio_write_pin(device_t dev, uint32_t pin_num, 
 
 static int
 cp2112_gpio_configure_write_pin(device_t dev, uint32_t pin_num,
-    bool output, bool pushpull)
+    bool output, enum cp2112_out_mode *mode)
 {
-	struct {
-		uint8_t id;
-		uint8_t output;
-		uint8_t pushpull;
-		uint8_t special;
-		uint8_t divider;
-	} __packed data;
+	struct gpio_config_req data;
 	struct cp2112gpio_softc *sc;
 	int err;
 	uint8_t mask;
@@ -220,21 +426,26 @@ cp2112_gpio_configure_write_pin(device_t dev, uint32_t
 	sc = device_get_softc(dev);
 	CP2112GPIO_LOCKED(sc);
 
-	mask = (uint8_t)1 << pin_num;
-	data.id = CP2112_REQ_GPIO_CFG;
 	err = cp2112_get_report(device_get_parent(dev),
 	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
 	if (err != 0)
 		return (err);
+
+	mask = (uint8_t)1 << pin_num;
 	if (output) {
 		data.output |= mask;
-		if (pushpull)
+		switch (*mode) {
+		case OUT_PP:
 			data.pushpull |= mask;
-		else
+			break;
+		case OUT_OD:
 			data.pushpull &= ~mask;
+			break;
+		default:
+			break;
+		}
 	} else {
 		data.output &= ~mask;
-		data.pushpull &= ~mask;
 	}
 
 	err = cp2112_set_report(device_get_parent(dev),
@@ -247,10 +458,25 @@ cp2112_gpio_configure_write_pin(device_t dev, uint32_t
 	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
 	if (err != 0)
 		return (err);
+
 	if (((data.output & mask) != 0) != output)
 		return (EIO);
-	if (((data.pushpull & mask) != 0) != pushpull)
-		return (EIO);
+	if (output) {
+		switch (*mode) {
+		case OUT_PP:
+			if ((data.pushpull & mask) == 0)
+				return (EIO);
+			break;
+		case OUT_OD:
+			if ((data.pushpull & mask) != 0)
+				return (EIO);
+			break;
+		default:
+			*mode = (data.pushpull & mask) != 0 ?
+			    OUT_PP : OUT_OD;
+			break;
+		}
+	}
 	return (0);
 }
 
@@ -381,6 +607,7 @@ cp2112_gpio_pin_setflags(device_t dev, uint32_t pin_nu
 {
 	struct cp2112gpio_softc *sc;
 	struct gpio_pin *pin;
+	enum cp2112_out_mode out_mode;
 	int err;
 
 	if (pin_num >= CP2112_GPIO_COUNT)
@@ -405,118 +632,45 @@ cp2112_gpio_pin_setflags(device_t dev, uint32_t pin_nu
 			return (EINVAL);
 	}
 
-	CP2112GPIO_LOCK(sc);
-	pin = &sc->pins[pin_num];
-
 	/*
-	 * If neither push-pull or opendrain is explcitely requested, then
+	 * If neither push-pull or open-drain is explicitly requested, then
 	 * preserve the current state.
 	 */
-	if ((flags & GPIO_PIN_OUTPUT) != 0 &&
-	    (flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) == 0)
-		flags |= pin->gp_flags & (GPIO_PIN_OPENDRAIN|GPIO_PIN_PUSHPULL);
+	out_mode = OUT_KEEP;
+	if ((flags & GPIO_PIN_OUTPUT) != 0) {
+		if ((flags & GPIO_PIN_OPENDRAIN) != 0)
+			out_mode = OUT_OD;
+		if ((flags & GPIO_PIN_PUSHPULL) != 0)
+			out_mode = OUT_PP;
+	}
+
+	CP2112GPIO_LOCK(sc);
+	pin = &sc->pins[pin_num];
 	err = cp2112_gpio_configure_write_pin(dev, pin_num,
-	    (flags & GPIO_PIN_OUTPUT) != 0,
-	    (flags & GPIO_PIN_PUSHPULL) != 0);
-	if (err == 0)
+	    (flags & GPIO_PIN_OUTPUT) != 0, &out_mode);
+	if (err == 0) {
+		/*
+		 * If neither open-drain or push-pull was requested, then see
+		 * what hardware actually had.  Otherwise, it has been
+		 * reconfigured as requested.
+		 */
+		if ((flags & GPIO_PIN_OUTPUT) != 0 &&
+		    (flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) == 0) {
+			KASSERT(out_mode != OUT_KEEP,
+			    ("impossible current output mode"));
+			if (out_mode == OUT_OD)
+				flags |= GPIO_PIN_OPENDRAIN;
+			else
+				flags |= GPIO_PIN_PUSHPULL;
+		}
 		pin->gp_flags = flags;
+	}
 	CP2112GPIO_UNLOCK(sc);
 
 	return (err);
 }
 
-static const STRUCT_USB_HOST_ID cp2112_devs[] = {
-	{ USB_VP(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2112) },
-	{ USB_VP(0x1009, USB_PRODUCT_SILABS_CP2112) }, /* XXX */
-};
-
 static int
-cp2112_probe(device_t dev)
-{
-	struct usb_attach_arg *uaa;
-
-	uaa = device_get_ivars(dev);
-	if (uaa->usb_mode != USB_MODE_HOST)
-		return (ENXIO);
-	if (uaa->info.bInterfaceClass != UICLASS_HID)
-		return (ENXIO);
-
-	if (usbd_lookup_id_by_uaa(cp2112_devs, sizeof(cp2112_devs), uaa) == 0)
-		return (BUS_PROBE_DEFAULT);
-	return (ENXIO);
-}
-
-static int
-cp2112_attach(device_t dev)
-{
-	struct {
-		uint8_t id;
-		uint8_t part_num;
-		uint8_t version;
-	} __packed vdata;
-	struct usb_attach_arg *uaa;
-	struct cp2112_softc *sc;
-	int err;
-
-	uaa = device_get_ivars(dev);
-	sc = device_get_softc(dev);
-
-	device_set_usb_desc(dev);
-
-	sc->sc_udev = uaa->device;
-	sc->sc_iface_index = uaa->info.bIfaceIndex;
-
-	vdata.id = CP2112_REQ_VERSION;
-	err = cp2112_get_report(dev, CP2112_REQ_VERSION, &vdata, sizeof(vdata));
-	if (err != 0)
-		goto detach;
-	device_printf(dev, "part number 0x%02x, version 0x%02x\n",
-	    vdata.part_num, vdata.version);
-	if (vdata.part_num != CP2112_PART_NUM) {
-		device_printf(dev, "unsupported part number\n");
-		goto detach;
-	}
-	sc->sc_version = vdata.version;
-	sc->sc_gpio_dev = device_add_child(dev, "gpio", -1);
-	if (sc->sc_gpio_dev != NULL) {
-		err = device_probe_and_attach(sc->sc_gpio_dev);
-		if (err != 0) {
-			device_printf(dev, "failed to attach gpio child\n");
-		}
-	} else {
-		device_printf(dev, "failed to create gpio child\n");
-	}
-
-	sc->sc_iic_dev = device_add_child(dev, "iichb", -1);
-	if (sc->sc_iic_dev != NULL) {
-		err = device_probe_and_attach(sc->sc_iic_dev);
-		if (err != 0) {
-			device_printf(dev, "failed to attach iic child\n");
-		}
-	} else {
-		device_printf(dev, "failed to create iic child\n");
-	}
-
-	return (0);
-
-detach:
-	cp2112_detach(dev);
-	return (ENXIO);
-}
-
-static int
-cp2112_detach(device_t dev)
-{
-	int err;
-
-	err = bus_generic_detach(dev);
-	if (err != 0)
-		return (err);
-	device_delete_children(dev);
-	return (0);
-}
-
-static int
 cp2112gpio_probe(device_t dev)
 {
 	device_set_desc(dev, "CP2112 GPIO interface");
@@ -526,13 +680,7 @@ cp2112gpio_probe(device_t dev)
 static int
 cp2112gpio_attach(device_t dev)
 {
-	struct {
-		uint8_t id;
-		uint8_t output;
-		uint8_t pushpull;
-		uint8_t special;
-		uint8_t divider;
-	} __packed data;
+	struct gpio_config_req data;
 	struct cp2112gpio_softc *sc;
 	device_t cp2112;
 	int err;
@@ -546,7 +694,6 @@ cp2112gpio_attach(device_t dev)
 	sc->gpio_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
 	    GPIO_PIN_PUSHPULL;
 
-	data.id = CP2112_REQ_GPIO_CFG;
 	err = cp2112_get_report(cp2112, CP2112_REQ_GPIO_CFG,
 	    &data, sizeof(data));
 	if (err != 0)
@@ -562,7 +709,11 @@ cp2112gpio_attach(device_t dev)
 		snprintf(pin->gp_name, GPIOMAXNAME, "GPIO%u", i);
 		pin->gp_name[GPIOMAXNAME - 1] = '\0';
 
-		if ((data.output & mask) != 0) {
+		if ((i == 0 && (data.special & CP2112_GPIO_SPEC_TX0) != 0) ||
+		    (i == 1 && (data.special & CP2112_GPIO_SPEC_RX1) != 0) ||
+		    (i == 7 && (data.special & CP2112_GPIO_SPEC_CLK7) != 0)) {
+			/* Special mode means that a pin is not for GPIO. */
+		} else if ((data.output & mask) != 0) {
 			pin->gp_flags |= GPIO_PIN_OUTPUT;
 			if ((data.pushpull & mask) != 0)
 				pin->gp_flags |= GPIO_PIN_PUSHPULL;
@@ -597,94 +748,6 @@ cp2112gpio_detach(device_t dev)
 	return (0);
 }
 
-static device_method_t cp2112hid_methods[] = {
-	DEVMETHOD(device_probe,		cp2112_probe),
-	DEVMETHOD(device_attach,	cp2112_attach),
-	DEVMETHOD(device_detach,	cp2112_detach),
-
-	DEVMETHOD_END
-};
-
-static device_method_t cp2112gpio_methods[] = {
-	/* Device */
-	DEVMETHOD(device_probe,		cp2112gpio_probe),
-	DEVMETHOD(device_attach,	cp2112gpio_attach),
-	DEVMETHOD(device_detach,	cp2112gpio_detach),
-
-	/* GPIO */
-	DEVMETHOD(gpio_get_bus,		cp2112_gpio_get_bus),
-	DEVMETHOD(gpio_pin_max,		cp2112_gpio_pin_max),
-	DEVMETHOD(gpio_pin_get,		cp2112_gpio_pin_get),
-	DEVMETHOD(gpio_pin_set,		cp2112_gpio_pin_set),
-	DEVMETHOD(gpio_pin_toggle,	cp2112_gpio_pin_toggle),
-	DEVMETHOD(gpio_pin_getname,	cp2112_gpio_pin_getname),
-	DEVMETHOD(gpio_pin_getcaps,	cp2112_gpio_pin_getcaps),
-	DEVMETHOD(gpio_pin_getflags,	cp2112_gpio_pin_getflags),
-	DEVMETHOD(gpio_pin_setflags,	cp2112_gpio_pin_setflags),
-
-	DEVMETHOD_END
-};
-
-static driver_t cp2112hid_driver = {
-	.name = "cp2112hid",
-	.methods = cp2112hid_methods,
-	.size = sizeof(struct cp2112_softc),
-};
-
-static devclass_t cp2112hid_devclass;
-DRIVER_MODULE(cp2112hid, uhub, cp2112hid_driver, cp2112hid_devclass,
-    NULL, NULL);
-MODULE_DEPEND(cp2112hid, usb, 1, 1, 1);
-MODULE_VERSION(cp2112hid, 1);
-USB_PNP_HOST_INFO(cp2112_devs);
-
-static driver_t cp2112gpio_driver = {
-	.name = "gpio",
-	.methods = cp2112gpio_methods,
-	.size = sizeof(struct cp2112gpio_softc),
-};
-
-static devclass_t cp2112gpio_devclass;
-DRIVER_MODULE(cp2112gpio, cp2112hid, cp2112gpio_driver, cp2112gpio_devclass,
-    NULL, NULL);
-MODULE_DEPEND(cp2112gpio, cp2112hid, 1, 1, 1);
-MODULE_DEPEND(cp2112gpio, gpiobus, 1, 1, 1);
-MODULE_VERSION(cp2112gpio, 1);
-
-
-
-/* CP2112 I2C driver code. */
-
-
-enum {
-	CP2112_INTR_OUT = 0,
-	CP2112_INTR_IN,
-	CP2112_N_TRANSFER,
-};
-
-struct cp2112iic_softc {
-	device_t	dev;
-	device_t	iicbus_dev;
-	struct usb_xfer	*xfers[CP2112_N_TRANSFER];
-	u_char		own_addr;
-	struct {
-		struct mtx	lock;
-		struct cv	cv;
-		struct {
-			uint8_t		*data;
-			int		len;
-			int		done;
-			int		error;
-		}		in;
-		struct {
-			const uint8_t	*data;
-			int		len;
-			int		done;
-			int		error;
-		}		out;
-	}		io;
-};
-
 static void
 cp2112iic_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
 {
@@ -890,17 +953,8 @@ cp2112iic_req_resp(struct cp2112iic_softc *sc, const v
 static int
 cp2112iic_check_req_status(struct cp2112iic_softc *sc)
 {
-	struct {
-		uint8_t id;
-		uint8_t request;
-	} __packed xfer_status_req;
-	struct {
-		uint8_t id;
-		uint8_t status0;
-		uint8_t status1;
-		uint16_t status2;
-		uint16_t status3;
-	} __packed xfer_status_resp;
+	struct i2c_xfer_status_req xfer_status_req;
+	struct i2c_xfer_status_resp xfer_status_resp;
 	int err;
 
 	mtx_assert(&sc->io.lock, MA_OWNED);
@@ -971,16 +1025,8 @@ static int
 cp2112iic_read_data(struct cp2112iic_softc *sc, void *data, uint16_t in_len,
     uint16_t *out_len)
 {
-	struct {
-		uint8_t id;
-		uint16_t length;
-	} __packed data_read_force_send;
-	struct {
-		uint8_t id;
-		uint8_t status;
-		uint8_t length;
-		uint8_t data[61];
-	} __packed data_read_resp;
+	struct i2c_data_read_force_send_req data_read_force_send;
+	struct i2c_data_read_resp data_read_resp;
 	int err;
 
 	mtx_assert(&sc->io.lock, MA_OWNED);
@@ -993,7 +1039,7 @@ cp2112iic_read_data(struct cp2112iic_softc *sc, void *
 	if (in_len > sizeof(data_read_resp.data))
 		in_len = sizeof(data_read_resp.data);
 	data_read_force_send.id = CP2112_REQ_SMB_READ_FORCE_SEND;
-	data_read_force_send.length = htobe16(in_len);
+	data_read_force_send.len = htobe16(in_len);
 	err = cp2112iic_req_resp(sc,
 	    &data_read_force_send, sizeof(data_read_force_send),
 	    &data_read_resp, sizeof(data_read_resp));
@@ -1009,7 +1055,7 @@ cp2112iic_read_data(struct cp2112iic_softc *sc, void *
 	}
 
 	DTRACE_PROBE2(read__response, uint8_t, data_read_resp.status,
-	    uint8_t, data_read_resp.length);
+	    uint8_t, data_read_resp.len);
 
 	/*
 	 * We expect either the request completed status or, more typical for
@@ -1021,13 +1067,13 @@ cp2112iic_read_data(struct cp2112iic_softc *sc, void *
 		err = IIC_EBUSERR;
 		goto out;
 	}
-	if (data_read_resp.length > in_len) {
+	if (data_read_resp.len > in_len) {
 		device_printf(sc->dev, "device returns more data than asked\n");
 		err = IIC_EOVERFLOW;
 		goto out;
 	}
 
-	*out_len = data_read_resp.length;
+	*out_len = data_read_resp.len;
 	if (*out_len > 0)
 		memcpy(data, data_read_resp.data, *out_len);
 out:
@@ -1070,11 +1116,13 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 			reason = "message with no data";
 			break;
 		}
-		if ((msgs[i].flags & IIC_M_RD) != 0 && msgs[i].len > 512) {
+		if ((msgs[i].flags & IIC_M_RD) != 0 &&
+		    msgs[i].len > CP2112_IIC_MAX_READ_LEN) {
 			reason = "too long read";
 			break;
 		}
-		if ((msgs[i].flags & IIC_M_RD) == 0 && msgs[i].len > 61) {
+		if ((msgs[i].flags & IIC_M_RD) == 0 &&
+		    msgs[i].len > SIZEOF_FIELD(i2c_write_req, data)) {
 			reason = "too long write";
 			break;
 		}
@@ -1092,7 +1140,8 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 			reason = "write without stop";
 			break;
 		}
-		if ((msgs[i].flags & IIC_M_NOSTOP) != 0 && msgs[i].len > 16) {
+		if ((msgs[i].flags & IIC_M_NOSTOP) != 0 &&
+		    msgs[i].len > SIZEOF_FIELD(i2c_write_read_req, wdata)) {
 			reason = "too long write without stop";
 			break;
 		}
@@ -1120,22 +1169,16 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 
 	for (i = 0; i < nmsgs; i++) {
 		if (i + 1 < nmsgs && (msgs[i].flags & IIC_M_NOSTOP) != 0) {
-			KASSERT((msgs[i].flags & IIC_M_RD) == 0,
-			    ("read without stop"));
-			KASSERT((msgs[i + 1].flags & IIC_M_RD) != 0,
-			    ("write after write without stop"));
 			/*
 			 * Combine <write><repeated start><read> into a single
 			 * CP2112 operation.
 			 */
-			struct {
-				uint8_t id;
-				uint8_t slave;
-				uint16_t rlen;
-				uint8_t wlen;
-				uint8_t wdata[16];
-			} __packed req;
+			struct i2c_write_read_req req;
 
+			KASSERT((msgs[i].flags & IIC_M_RD) == 0,
+			    ("read without stop"));
+			KASSERT((msgs[i + 1].flags & IIC_M_RD) != 0,
+			    ("write after write without stop"));
 			req.id = CP2112_REQ_SMB_WRITE_READ;
 			req.slave = msgs[i].slave & ~LSB;
 			to_read = msgs[i + 1].len;
@@ -1150,11 +1193,7 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 			 */
 			i++;
 		} else if ((msgs[i].flags & IIC_M_RD) != 0) {
-			struct {
-				uint8_t id;
-				uint8_t slave;
-				uint16_t len;
-			} __packed req;
+			struct i2c_read_req req;
 
 			req.id = CP2112_REQ_SMB_READ;
 			req.slave = msgs[i].slave & ~LSB;
@@ -1162,12 +1201,7 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 			req.len = htobe16(to_read);
 			err = cp2112iic_send_req(sc, &req, sizeof(req));
 		} else {
-			struct {
-				uint8_t id;
-				uint8_t slave;
-				uint8_t len;
-				uint8_t data[61];
-			} __packed req;
+			struct i2c_write_req req;
 
 			req.id = CP2112_REQ_SMB_WRITE;
 			req.slave = msgs[i].slave & ~LSB;
@@ -1207,16 +1241,7 @@ cp2112iic_transfer(device_t dev, struct iic_msg *msgs,
 static int
 cp2112iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
 {
-	struct {
-		uint8_t		id;
-		uint32_t	speed;		/* Hz */
-		uint8_t		slave_addr;	/* ACK only */
-		uint8_t		auto_send_read;	/* boolean */
-		uint16_t	write_timeout;	/* 0-1000 ms, 0 ~ no timeout */
-		uint16_t	read_timeout;	/* 0-1000 ms, 0 ~ no timeout */
-		uint8_t		scl_low_timeout;/* boolean */
-		uint16_t	retry_count;	/* 1-1000, 0 ~ forever */
-	} __packed smb_cfg;
+	struct i2c_cfg_req i2c_cfg;
 	struct cp2112iic_softc *sc;
 	device_t cp2112;
 	u_int busfreq;
@@ -1229,16 +1254,15 @@ cp2112iic_reset(device_t dev, u_char speed, u_char add
 	else
 		busfreq = IICBUS_GET_FREQUENCY(sc->iicbus_dev, speed);
 
-	smb_cfg.id = CP2112_REQ_SMB_CFG;
 	err = cp2112_get_report(cp2112, CP2112_REQ_SMB_CFG,
-	    &smb_cfg, sizeof(smb_cfg));
+	    &i2c_cfg, sizeof(i2c_cfg));
 	if (err != 0) {
 		device_printf(dev, "failed to get CP2112_REQ_SMB_CFG report\n");
 		return (err);
 	}
 
 	if (oldaddr != NULL)
-		*oldaddr = smb_cfg.slave_addr;
+		*oldaddr = i2c_cfg.slave_addr;
 	/*
 	 * For simplicity we do not enable Auto Send Read
 	 * because of erratum CP2112_E101 (fixed in version 3).
@@ -1251,28 +1275,28 @@ cp2112iic_reset(device_t dev, u_char speed, u_char add
 	 * TODO: should the device reset request (0x01) be sent?
 	 * If the device disconnects as a result, then no.
 	 */
-	smb_cfg.speed = htobe32(busfreq);
+	i2c_cfg.speed = htobe32(busfreq);
 	if (addr != 0)
-		smb_cfg.slave_addr = addr;
-	smb_cfg.auto_send_read = 0;
-	smb_cfg.retry_count = htobe16(1);
-	smb_cfg.scl_low_timeout = 0;
+		i2c_cfg.slave_addr = addr;
+	i2c_cfg.auto_send_read = 0;
+	i2c_cfg.retry_count = htobe16(1);
+	i2c_cfg.scl_low_timeout = 0;
 	if (bootverbose) {
-		device_printf(dev, "speed %d Hz\n", be32toh(smb_cfg.speed));
-		device_printf(dev, "slave addr 0x%02x\n", smb_cfg.slave_addr);
+		device_printf(dev, "speed %d Hz\n", be32toh(i2c_cfg.speed));
+		device_printf(dev, "slave addr 0x%02x\n", i2c_cfg.slave_addr);
 		device_printf(dev, "auto send read %s\n",
-		    smb_cfg.auto_send_read ? "on" : "off");
+		    i2c_cfg.auto_send_read ? "on" : "off");
 		device_printf(dev, "write timeout %d ms (0 - disabled)\n",
-		    be16toh(smb_cfg.write_timeout));
+		    be16toh(i2c_cfg.write_timeout));
 		device_printf(dev, "read timeout %d ms (0 - disabled)\n",
-		    be16toh(smb_cfg.read_timeout));
+		    be16toh(i2c_cfg.read_timeout));
 		device_printf(dev, "scl low timeout %s\n",
-		    smb_cfg.scl_low_timeout ? "on" : "off");
+		    i2c_cfg.scl_low_timeout ? "on" : "off");
 		device_printf(dev, "retry count %d (0 - no limit)\n",
-		    be16toh(smb_cfg.retry_count));
+		    be16toh(i2c_cfg.retry_count));

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202008281027.07SAROiY029076>