Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Aug 2020 13:41:42 +0000 (UTC)
From:      Andriy Gapon <avg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r363951 - in head/sys: conf dev/usb/misc modules/usb modules/usb/cp2112
Message-ID:  <202008061341.076Dfgp8050921@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: avg
Date: Thu Aug  6 13:41:42 2020
New Revision: 363951
URL: https://svnweb.freebsd.org/changeset/base/363951

Log:
  cp2112: driver for the namesake GPIO and I2C master gadget
  
  Documentation:
  - CP2112 Datasheet
    https://www.silabs.com/documents/public/data-sheets/cp2112-datasheet.pdf
  - AN495: CP2112 Interface Specification
    https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
  - CP2112 Errata
    https://www.silabs.com/documents/public/errata/cp2112-errata.pdf
  
  The logic is implemented as three sub-drivers.
  The parent driver claims the USB device and creates two child devices.
  One acts as a GPIO controller and the other is an I2C controller.
  
  Tested with CP2112 revision F02.
  Both features seem to work.
  HTU21 sensor was used as an I2C slave.
  
  Reviewed by:	adrian, hselasky
  MFC after:	2 weeks
  Relnotes:	maybe
  Differential Revision: https://reviews.freebsd.org/D25359

Added:
  head/sys/dev/usb/misc/cp2112.c   (contents, props changed)
  head/sys/modules/usb/cp2112/
  head/sys/modules/usb/cp2112/Makefile   (contents, props changed)
Modified:
  head/sys/conf/files
  head/sys/modules/usb/Makefile

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files	Thu Aug  6 13:25:04 2020	(r363950)
+++ head/sys/conf/files	Thu Aug  6 13:41:42 2020	(r363951)
@@ -3331,6 +3331,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

Added: head/sys/dev/usb/misc/cp2112.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/dev/usb/misc/cp2112.c	Thu Aug  6 13:41:42 2020	(r363951)
@@ -0,0 +1,1381 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) Andriy Gapon
+ *
+ * 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.
+ *
+ */
+
+/*
+ * Hardware information links:
+ * - CP2112 Datasheet
+ *   https://www.silabs.com/documents/public/data-sheets/cp2112-datasheet.pdf
+ * - AN495: CP2112 Interface Specification
+ *   https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
+ * - CP2112 Errata
+ *   https://www.silabs.com/documents/public/errata/cp2112-errata.pdf
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/condvar.h>
+#include <sys/bus.h>
+#include <sys/gpio.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/module.h>
+#include <sys/mutex.h>
+#include <sys/sdt.h>
+#include <sys/sx.h>
+
+#include <dev/gpio/gpiobusvar.h>
+
+#include <dev/iicbus/iiconf.h>
+#include <dev/iicbus/iicbus.h>
+#include "iicbus_if.h"
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbhid.h>
+#include "usbdevs.h"
+
+#define	USB_DEBUG_VAR usb_debug
+#include <dev/usb/usb_debug.h>
+
+#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)
+
+#define	CP2112_PART_NUM			0x0c
+#define	CP2112_GPIO_COUNT		8
+#define	CP2112_REPORT_SIZE		64
+
+#define	CP2112_REQ_RESET		0x1
+#define	CP2112_REQ_GPIO_CFG		0x2
+#define	CP2112_REQ_GPIO_GET		0x3
+#define	CP2112_REQ_GPIO_SET		0x4
+#define	CP2112_REQ_VERSION		0x5
+#define	CP2112_REQ_SMB_CFG		0x6
+
+#define	CP2112_REQ_SMB_READ		0x10
+#define	CP2112_REQ_SMB_WRITE_READ	0x11
+#define	CP2112_REQ_SMB_READ_FORCE_SEND	0x12
+#define	CP2112_REQ_SMB_READ_RESPONSE	0x13
+#define	CP2112_REQ_SMB_WRITE		0x14
+#define	CP2112_REQ_SMB_XFER_STATUS_REQ	0x15
+#define	CP2112_REQ_SMB_XFER_STATUS_RESP	0x16
+#define	CP2112_REQ_SMB_CANCEL		0x17
+
+#define	CP2112_REQ_LOCK			0x20
+#define	CP2112_REQ_USB_CFG		0x21
+
+#define	CP2112_IIC_REPSTART_VER		2	/* Erratum CP2112_E10. */
+
+#define	CP2112_IIC_STATUS0_IDLE		0
+#define	CP2112_IIC_STATUS0_BUSY		1
+#define	CP2112_IIC_STATUS0_CMP		2
+#define	CP2112_IIC_STATUS0_ERROR	3
+
+#define	CP2112_IIC_STATUS1_TIMEOUT_NACK	0
+#define	CP2112_IIC_STATUS1_TIMEOUT_BUS	1
+#define	CP2112_IIC_STATUS1_ARB_LOST	2
+
+
+struct cp2112_softc {
+	device_t		sc_gpio_dev;
+	device_t		sc_iic_dev;
+	struct usb_device	*sc_udev;
+	uint8_t			sc_iface_index;
+	uint8_t			sc_version;
+};
+
+struct cp2112gpio_softc {
+	struct sx		gpio_lock;
+	device_t		busdev;
+	int			gpio_caps;
+	struct gpio_pin		pins[CP2112_GPIO_COUNT];
+};
+
+static int cp2112_detach(device_t dev);
+static int cp2112gpio_detach(device_t dev);
+static int cp2112iic_detach(device_t dev);
+
+static int
+cp2112_get_report(device_t dev, uint8_t id, void *data, uint16_t len)
+{
+	struct cp2112_softc *sc;
+	int err;
+
+	sc = device_get_softc(dev);
+	err = usbd_req_get_report(sc->sc_udev, NULL, data,
+	    len, sc->sc_iface_index, UHID_FEATURE_REPORT, id);
+	return (err);
+}
+
+static int
+cp2112_set_report(device_t dev, uint8_t id, void *data, uint16_t len)
+{
+	struct cp2112_softc *sc;
+	int err;
+
+	sc = device_get_softc(dev);
+	err = usbd_req_set_report(sc->sc_udev, NULL, data,
+	    len, sc->sc_iface_index, UHID_FEATURE_REPORT, id);
+	return (err);
+}
+
+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 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)
+		return (err);
+	*on = (data.state & ((uint8_t)1 << pin_num)) != 0;
+	return (0);
+
+}
+
+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 cp2112gpio_softc *sc;
+	int err;
+	bool actual;
+
+	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)
+		return (err);
+	err = cp2112_gpio_read_pin(dev, pin_num, &actual);
+	if (err != 0)
+		return (err);
+	if (actual != on)
+		return (EIO);
+	return (0);
+}
+
+static int
+cp2112_gpio_configure_write_pin(device_t dev, uint32_t pin_num,
+    bool output, bool pushpull)
+{
+	struct {
+		uint8_t id;
+		uint8_t output;
+		uint8_t pushpull;
+		uint8_t special;
+		uint8_t divider;
+	} __packed data;
+	struct cp2112gpio_softc *sc;
+	int err;
+	uint8_t mask;
+
+	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);
+	if (output) {
+		data.output |= mask;
+		if (pushpull)
+			data.pushpull |= mask;
+		else
+			data.pushpull &= ~mask;
+	} else {
+		data.output &= ~mask;
+		data.pushpull &= ~mask;
+	}
+
+	err = cp2112_set_report(device_get_parent(dev),
+	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
+	if (err != 0)
+		return (err);
+
+	/* Read back and verify. */
+	err = cp2112_get_report(device_get_parent(dev),
+	    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);
+	return (0);
+}
+
+static device_t
+cp2112_gpio_get_bus(device_t dev)
+{
+	struct cp2112gpio_softc *sc;
+
+	sc = device_get_softc(dev);
+	return (sc->busdev);
+}
+
+static int
+cp2112_gpio_pin_max(device_t dev, int *maxpin)
+{
+
+	*maxpin = CP2112_GPIO_COUNT - 1;
+	return (0);
+}
+
+static int
+cp2112_gpio_pin_set(device_t dev, uint32_t pin_num, uint32_t pin_value)
+{
+	struct cp2112gpio_softc *sc;
+	int err;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	err = cp2112_gpio_write_pin(dev, pin_num, pin_value != 0);
+	CP2112GPIO_UNLOCK(sc);
+
+	return (err);
+}
+
+static int
+cp2112_gpio_pin_get(device_t dev, uint32_t pin_num, uint32_t *pin_value)
+{
+	struct cp2112gpio_softc *sc;
+	int err;
+	bool on;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	err = cp2112_gpio_read_pin(dev, pin_num, &on);
+	CP2112GPIO_UNLOCK(sc);
+
+	if (err == 0)
+		*pin_value = on;
+	return (err);
+}
+
+static int
+cp2112_gpio_pin_toggle(device_t dev, uint32_t pin_num)
+{
+	struct cp2112gpio_softc *sc;
+	int err;
+	bool on;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	err = cp2112_gpio_read_pin(dev, pin_num, &on);
+	if (err == 0)
+		err = cp2112_gpio_write_pin(dev, pin_num, !on);
+	CP2112GPIO_UNLOCK(sc);
+
+	return (err);
+}
+
+static int
+cp2112_gpio_pin_getcaps(device_t dev, uint32_t pin_num, uint32_t *caps)
+{
+	struct cp2112gpio_softc *sc;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	*caps = sc->gpio_caps;
+	CP2112GPIO_UNLOCK(sc);
+
+	return (0);
+}
+
+static int
+cp2112_gpio_pin_getflags(device_t dev, uint32_t pin_num, uint32_t *flags)
+{
+	struct cp2112gpio_softc *sc;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	*flags = sc->pins[pin_num].gp_flags;
+	CP2112GPIO_UNLOCK(sc);
+
+	return (0);
+}
+
+static int
+cp2112_gpio_pin_getname(device_t dev, uint32_t pin_num, char *name)
+{
+	struct cp2112gpio_softc *sc;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	CP2112GPIO_LOCK(sc);
+	memcpy(name, sc->pins[pin_num].gp_name, GPIOMAXNAME);
+	CP2112GPIO_UNLOCK(sc);
+
+	return (0);
+}
+
+static int
+cp2112_gpio_pin_setflags(device_t dev, uint32_t pin_num, uint32_t flags)
+{
+	struct cp2112gpio_softc *sc;
+	struct gpio_pin *pin;
+	int err;
+
+	if (pin_num >= CP2112_GPIO_COUNT)
+		return (EINVAL);
+
+	sc = device_get_softc(dev);
+	if ((flags & sc->gpio_caps) != flags)
+		return (EINVAL);
+
+	if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) == 0)
+			return (EINVAL);
+	if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
+		(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
+			return (EINVAL);
+	}
+	if ((flags & GPIO_PIN_INPUT) != 0) {
+		if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) != 0)
+			return (EINVAL);
+	} else {
+		if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) ==
+		    (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL))
+			return (EINVAL);
+	}
+
+	CP2112GPIO_LOCK(sc);
+	pin = &sc->pins[pin_num];
+
+	/*
+	 * If neither push-pull or opendrain is explcitely 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);
+	err = cp2112_gpio_configure_write_pin(dev, pin_num,
+	    (flags & GPIO_PIN_OUTPUT) != 0,
+	    (flags & GPIO_PIN_PUSHPULL) != 0);
+	if (err == 0)
+		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");
+	return (BUS_PROBE_SPECIFIC);
+}
+
+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 cp2112gpio_softc *sc;
+	device_t cp2112;
+	int err;
+	int i;
+	uint8_t mask;
+
+	cp2112 = device_get_parent(dev);
+	sc = device_get_softc(dev);
+	sx_init(&sc->gpio_lock, "cp2112 lock");
+
+	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)
+		goto detach;
+
+	for (i = 0; i < CP2112_GPIO_COUNT; i++) {
+		struct gpio_pin *pin;
+
+		mask = (uint8_t)1 << i;
+		pin = &sc->pins[i];
+		pin->gp_flags = 0;
+
+		snprintf(pin->gp_name, GPIOMAXNAME, "GPIO%u", i);
+		pin->gp_name[GPIOMAXNAME - 1] = '\0';
+
+		if ((data.output & mask) != 0) {
+			pin->gp_flags |= GPIO_PIN_OUTPUT;
+			if ((data.pushpull & mask) != 0)
+				pin->gp_flags |= GPIO_PIN_PUSHPULL;
+			else
+				pin->gp_flags |= GPIO_PIN_OPENDRAIN;
+		} else {
+			pin->gp_flags |= GPIO_PIN_INPUT;
+		}
+	}
+
+	sc->busdev = gpiobus_attach_bus(dev);
+	if (sc->busdev == NULL) {
+		device_printf(dev, "gpiobus_attach_bus failed\n");
+		goto detach;
+	}
+	return (0);
+
+detach:
+	cp2112gpio_detach(dev);
+	return (ENXIO);
+}
+
+static int
+cp2112gpio_detach(device_t dev)
+{
+	struct cp2112gpio_softc *sc;
+
+	sc = device_get_softc(dev);
+	if (sc->busdev != NULL)
+		gpiobus_detach_bus(dev);
+	sx_destroy(&sc->gpio_lock);
+	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)
+{
+	struct cp2112iic_softc *sc;
+	struct cp2112_softc *psc;
+	struct usb_page_cache *pc;
+
+	sc = usbd_xfer_softc(xfer);
+	psc = device_get_softc(device_get_parent(sc->dev));
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+
+	switch (USB_GET_STATE(xfer)) {
+	case USB_ST_SETUP:
+		pc = usbd_xfer_get_frame(xfer, 0);
+		usbd_copy_in(pc, 0, sc->io.out.data, sc->io.out.len);
+		usbd_xfer_set_frame_len(xfer, 0, sc->io.out.len);
+		usbd_xfer_set_frames(xfer, 1);
+		usbd_transfer_submit(xfer);
+		break;
+	case USB_ST_TRANSFERRED:
+		sc->io.out.error = 0;
+		sc->io.out.done = 1;
+		cv_signal(&sc->io.cv);
+		break;
+	default:			/* Error */
+		device_printf(sc->dev, "write intr state %d error %d\n",
+		    USB_GET_STATE(xfer), error);
+		sc->io.out.error = IIC_EBUSERR;
+		cv_signal(&sc->io.cv);
+		if (error != USB_ERR_CANCELLED) {
+			/* try to clear stall first */
+			usbd_xfer_set_stall(xfer);
+		}
+		break;
+	}
+}
+
+static void
+cp2112iic_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
+{
+	struct cp2112iic_softc *sc = usbd_xfer_softc(xfer);
+	struct usb_page_cache *pc;
+	int act_len, len;
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+	usbd_xfer_status(xfer, &act_len, NULL, NULL, NULL);
+
+	switch (USB_GET_STATE(xfer)) {
+	case USB_ST_TRANSFERRED:
+		if (sc->io.in.done) {
+			device_printf(sc->dev,
+			    "interrupt while previous is pending, ignored\n");
+		} else if (sc->io.in.len == 0) {
+			uint8_t buf[8];
+
+			/*
+			 * There is a spurious Transfer Status Response and
+			 * zero-length Read Response during hardware
+			 * configuration.  Possibly they carry some information
+			 * about the initial bus state.
+			 */
+			if (device_is_attached(sc->dev)) {
+				device_printf(sc->dev,
+				    "unsolicited interrupt, ignored\n");
+				if (bootverbose) {
+					pc = usbd_xfer_get_frame(xfer, 0);
+					len = MIN(sizeof(buf), act_len);
+					usbd_copy_out(pc, 0, buf, len);
+					device_printf(sc->dev, "data: %*D\n",
+					    len, buf, " ");
+				}
+			} else {
+				pc = usbd_xfer_get_frame(xfer, 0);
+				len = MIN(sizeof(buf), act_len);
+				usbd_copy_out(pc, 0, buf, len);
+				if (buf[0] == CP2112_REQ_SMB_XFER_STATUS_RESP) {
+					device_printf(sc->dev,
+					    "initial bus status0 = 0x%02x, "
+					    "status1 = 0x%02x\n",
+					    buf[1], buf[2]);
+				}
+			}
+		} else if (act_len == CP2112_REPORT_SIZE) {
+			pc = usbd_xfer_get_frame(xfer, 0);
+			usbd_copy_out(pc, 0, sc->io.in.data, sc->io.in.len);
+			sc->io.in.error = 0;
+			sc->io.in.done = 1;
+		} else {
+			device_printf(sc->dev,
+			    "unexpected input report length %u\n", act_len);
+			sc->io.in.error = IIC_EBUSERR;
+			sc->io.in.done = 1;
+		}
+		cv_signal(&sc->io.cv);
+	case USB_ST_SETUP:
+tr_setup:
+		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
+		usbd_transfer_submit(xfer);
+		break;
+
+	default:			/* Error */
+		device_printf(sc->dev, "read intr state %d error %d\n",
+		    USB_GET_STATE(xfer), error);
+
+		sc->io.in.error = IIC_EBUSERR;
+		sc->io.in.done = 1;
+		cv_signal(&sc->io.cv);
+		if (error != USB_ERR_CANCELLED) {
+			/* try to clear stall first */
+			usbd_xfer_set_stall(xfer);
+			goto tr_setup;
+		}
+		break;
+	}
+}
+
+static const struct usb_config cp2112iic_config[CP2112_N_TRANSFER] = {
+	[CP2112_INTR_OUT] = {
+		.type = UE_INTERRUPT,
+		.endpoint = UE_ADDR_ANY,
+		.direction = UE_DIR_OUT,
+		.flags = { .pipe_bof = 1, .no_pipe_ok = 1, },
+		.bufsize = 0,	/* use wMaxPacketSize */
+		.callback = &cp2112iic_intr_write_callback,
+	},
+	[CP2112_INTR_IN] = {
+		.type = UE_INTERRUPT,
+		.endpoint = UE_ADDR_ANY,
+		.direction = UE_DIR_IN,
+		.flags = { .pipe_bof = 1, .short_xfer_ok = 1, },
+		.bufsize = 0,	/* use wMaxPacketSize */
+		.callback = &cp2112iic_intr_read_callback,
+	},
+};
+
+static int
+cp2112iic_send_req(struct cp2112iic_softc *sc, const void *data,
+    uint16_t len)
+{
+	int err;
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+	KASSERT(sc->io.out.done == 0, ("%s: conflicting request", __func__));
+
+	sc->io.out.data = data;
+	sc->io.out.len = len;
+
+	DTRACE_PROBE1(send__req, uint8_t, *(const uint8_t *)data);
+
+	usbd_transfer_start(sc->xfers[CP2112_INTR_OUT]);
+
+	while (!sc->io.out.done)
+		cv_wait(&sc->io.cv, &sc->io.lock);
+
+	usbd_transfer_stop(sc->xfers[CP2112_INTR_OUT]);
+
+	sc->io.out.done = 0;
+	sc->io.out.data = NULL;
+	sc->io.out.len = 0;
+	err = sc->io.out.error;
+	if (err != 0) {
+		device_printf(sc->dev, "output report 0x%02x failed: %d\n",
+		    *(const uint8_t*)data, err);
+	}
+	return (err);
+}
+
+static int
+cp2112iic_req_resp(struct cp2112iic_softc *sc, const void *req_data,
+    uint16_t req_len, void *resp_data, uint16_t resp_len)
+{
+	int err;
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+
+	/*
+	 * Prepare to receive a response interrupt even before the
+	 * request transfer is confirmed (USB_ST_TRANSFERED).
+	 */
+	KASSERT(sc->io.in.done == 0, ("%s: conflicting request", __func__));
+	sc->io.in.len = resp_len;
+	sc->io.in.data = resp_data;
+
+	err = cp2112iic_send_req(sc, req_data, req_len);
+	if (err != 0) {
+		sc->io.in.len = 0;
+		sc->io.in.data = NULL;
+		return (err);
+	}
+
+	while (!sc->io.in.done)
+		cv_wait(&sc->io.cv, &sc->io.lock);
+
+	err = sc->io.in.error;
+	sc->io.in.done = 0;
+	sc->io.in.error = 0;
+	sc->io.in.len = 0;
+	sc->io.in.data = NULL;
+	return (err);
+}
+
+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;
+	int err;
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+
+	do {
+		xfer_status_req.id = CP2112_REQ_SMB_XFER_STATUS_REQ;
+		xfer_status_req.request = 1;
+		err = cp2112iic_req_resp(sc,
+		    &xfer_status_req, sizeof(xfer_status_req),
+		    &xfer_status_resp, sizeof(xfer_status_resp));
+
+		if (xfer_status_resp.id != CP2112_REQ_SMB_XFER_STATUS_RESP) {
+			device_printf(sc->dev,
+			    "unexpected response 0x%02x to status request\n",
+			    xfer_status_resp.id);
+			err = IIC_EBUSERR;
+			goto out;
+		}
+
+		DTRACE_PROBE4(xfer__status, uint8_t, xfer_status_resp.status0,
+		    uint8_t, xfer_status_resp.status1,
+		    uint16_t, be16toh(xfer_status_resp.status2),
+		    uint16_t, be16toh(xfer_status_resp.status3));
+
+		switch (xfer_status_resp.status0) {
+		case CP2112_IIC_STATUS0_IDLE:
+			err = IIC_ESTATUS;
+			break;
+		case CP2112_IIC_STATUS0_BUSY:
+			err = ERESTART;	/* non-I2C, special handling */
+			break;
+		case CP2112_IIC_STATUS0_CMP:
+			err = IIC_NOERR;
+			break;
+		case CP2112_IIC_STATUS0_ERROR:
+			switch (xfer_status_resp.status1) {
+			case CP2112_IIC_STATUS1_TIMEOUT_NACK:
+				err = IIC_ENOACK;
+				break;
+			case CP2112_IIC_STATUS1_TIMEOUT_BUS:
+				err = IIC_ETIMEOUT;
+				break;
+			case CP2112_IIC_STATUS1_ARB_LOST:
+				err = IIC_EBUSBSY;
+				break;
+			default:
+				device_printf(sc->dev,
+				    "i2c error, status = 0x%02x\n",
+				    xfer_status_resp.status1);
+				err = IIC_ESTATUS;
+				break;
+			}
+			break;
+		default:
+			device_printf(sc->dev,
+			    "unknown i2c xfer status0 0x%02x\n",
+			    xfer_status_resp.status0);
+			err = IIC_EBUSERR;
+			break;
+		}
+
+	} while (err == ERESTART);
+out:
+	return (err);
+}
+
+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;
+	int err;
+
+	mtx_assert(&sc->io.lock, MA_OWNED);
+

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



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