Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c Add drivers for HID over I2C devices, and a driv...



details:   https://anonhg.NetBSD.org/src/rev/e81840c51c03
branches:  trunk
changeset: 828382:e81840c51c03
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Sun Dec 10 17:05:54 2017 +0000

description:
Add drivers for HID over I2C devices, and a driver for I2C mices.
>From OpenBSD.

diffstat:

 sys/dev/i2c/files.i2c |   21 +-
 sys/dev/i2c/ihidev.c  |  874 ++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/i2c/ihidev.h  |  160 +++++++++
 sys/dev/i2c/ims.c     |  204 +++++++++++
 4 files changed, 1258 insertions(+), 1 deletions(-)

diffs (truncated from 1282 to 300 lines):

diff -r bdc87bbbf97a -r e81840c51c03 sys/dev/i2c/files.i2c
--- a/sys/dev/i2c/files.i2c     Sun Dec 10 17:03:07 2017 +0000
+++ b/sys/dev/i2c/files.i2c     Sun Dec 10 17:05:54 2017 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.i2c,v 1.78 2017/10/07 18:22:06 jmcneill Exp $
+#      $NetBSD: files.i2c,v 1.79 2017/12/10 17:05:54 bouyer Exp $
 
 obsolete defflag       opt_i2cbus.h            I2C_SCAN
 define i2cbus { }
@@ -284,3 +284,22 @@
 device sy8106a
 attach sy8106a at iic
 file   dev/i2c/sy8106a.c                       sy8106a
+
+# HID over i2c
+# HID "bus"
+define  ihidbus {[ reportid = -1 ]}
+
+# HID root device for multiple report IDs
+device  ihidev: hid, ihidbus
+attach  ihidev at iic
+file    dev/i2c/ihidev.c                       ihidev
+
+#HID mice
+device ims: hid, hidms, wsmousedev
+attach ims at ihidbus
+file   dev/i2c/ims.c                           ims
+
+#HID multitouch
+device imt: hid, hidmt, wsmousedev
+attach imt at ihidbus
+file   dev/i2c/imt.c                           imt
diff -r bdc87bbbf97a -r e81840c51c03 sys/dev/i2c/ihidev.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/ihidev.c      Sun Dec 10 17:05:54 2017 +0000
@@ -0,0 +1,874 @@
+/* $NetBSD: ihidev.c,v 1.1 2017/12/10 17:05:54 bouyer Exp $ */
+/* $OpenBSD ihidev.c,v 1.13 2017/04/08 02:57:23 deraadt Exp $ */
+
+/*-
+ * Copyright (c) 2017 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Manuel Bouyer.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/*
+ * Copyright (c) 2015, 2016 joshua stein <jcs%openbsd.org@localhost>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * HID-over-i2c driver
+ *
+ * https://msdn.microsoft.com/en-us/library/windows/hardware/dn642101%28v=vs.85%29.aspx
+ *
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.1 2017/12/10 17:05:54 bouyer Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/kmem.h>
+
+
+#include <dev/i2c/i2cvar.h>
+#include <dev/i2c/ihidev.h>
+
+#include <dev/hid/hid.h>
+
+#if defined(__i386__) || defined(__amd64__)
+#  include "acpica.h"
+#endif
+#if NACPICA > 0
+#include <dev/acpi/acpi_intr.h>
+#endif
+
+#include "locators.h"
+
+/* #define IHIDEV_DEBUG */
+
+#ifdef IHIDEV_DEBUG
+#define DPRINTF(x) printf x
+#else
+#define DPRINTF(x)
+#endif
+
+/* 7.2 */
+enum {
+       I2C_HID_CMD_DESCR       = 0x0,
+       I2C_HID_CMD_RESET       = 0x1,
+       I2C_HID_CMD_GET_REPORT  = 0x2,
+       I2C_HID_CMD_SET_REPORT  = 0x3,
+       I2C_HID_CMD_GET_IDLE    = 0x4,
+       I2C_HID_CMD_SET_IDLE    = 0x5,
+       I2C_HID_CMD_GET_PROTO   = 0x6,
+       I2C_HID_CMD_SET_PROTO   = 0x7,
+       I2C_HID_CMD_SET_POWER   = 0x8,
+
+       /* pseudo commands */
+       I2C_HID_REPORT_DESCR    = 0x100,
+};
+
+static int I2C_HID_POWER_ON    = 0x0;
+static int I2C_HID_POWER_OFF   = 0x1;
+
+static int     ihidev_match(device_t, cfdata_t, void *);
+static void    ihidev_attach(device_t, device_t, void *);
+static int     ihidev_detach(device_t, int);
+CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
+    ihidev_match, ihidev_attach, ihidev_detach, NULL);
+
+static bool    ihidev_suspend(device_t, const pmf_qual_t *);
+static bool    ihidev_resume(device_t, const pmf_qual_t *);
+static int     ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
+static unsigned int ihidev_intr(void *);
+static int     ihidev_reset(struct ihidev_softc *, bool);
+static int     ihidev_hid_desc_parse(struct ihidev_softc *);
+
+static int     ihidev_maxrepid(void *, int);
+static int     ihidev_print(void *, const char *);
+static int     ihidev_submatch(device_t, cfdata_t, const int *, void *);
+
+static const char *ihidev_compats[] = {
+       "hid-over-i2c",
+       NULL
+};
+
+static int
+ihidev_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct i2c_attach_args * const ia = aux;
+
+       if (ia->ia_ncompat > 0) {
+               if (iic_compat_match(ia, ihidev_compats))
+                       return 1;
+       }
+       return 0;
+}
+
+static void
+ihidev_attach(device_t parent, device_t self, void *aux)
+{
+       struct ihidev_softc *sc = device_private(self);
+       struct i2c_attach_args *ia = aux;
+       struct ihidev_attach_arg iha;
+       device_t dev;
+       int repid, repsz;
+       int isize;
+       uint32_t v;
+       int locs[IHIDBUSCF_NLOCS];
+
+
+       sc->sc_dev = self;
+       sc->sc_tag = ia->ia_tag;
+       sc->sc_addr = ia->ia_addr;
+       sc->sc_phandle = ia->ia_cookie;
+       mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
+
+       if (!prop_dictionary_get_uint32(ia->ia_prop, "hid-descr-addr", &v)) {
+               aprint_error(": no hid-descr-addr value\n");
+               return;
+       }
+
+       sc->sc_hid_desc_addr = v;
+
+       if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
+           ihidev_hid_desc_parse(sc)) {
+               aprint_error(": failed fetching initial HID descriptor\n");
+               return;
+       }
+
+       aprint_naive("\n");
+       aprint_normal(": vendor 0x%x product 0x%x, %s\n",
+           le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
+           ia->ia_name);
+
+       sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
+       if (sc->sc_nrepid < 0)
+               return;
+
+       aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
+           sc->sc_nrepid > 1 ? "s" : "");
+
+       sc->sc_nrepid++;
+       sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
+           KM_NOSLEEP);
+       if (sc->sc_subdevs == NULL) {
+               aprint_error_dev(self, "failed allocating memory\n");
+               return;
+       }
+
+       /* find largest report size and allocate memory for input buffer */
+       sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
+       for (repid = 0; repid < sc->sc_nrepid; repid++) {
+               repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
+                   hid_input, repid);
+
+               isize = repsz + 2; /* two bytes for the length */
+               isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
+               if (isize > sc->sc_isize)
+                       sc->sc_isize = isize;
+
+               DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
+                   repsz));
+       }
+       sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_NOSLEEP);
+#if NACPICA > 0
+       {
+               char buf[100];
+
+               sc->sc_ih = acpi_intr_establish(self, sc->sc_phandle, ihidev_intr, sc);
+               if (sc->sc_ih == NULL)
+                       aprint_error_dev(self, "can't establish interrupt\n");
+               aprint_normal_dev(self, "interrupting at %s\n",
+                   acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
+       }
+#endif
+
+       iha.iaa = ia;
+       iha.parent = sc;
+
+       /* Look for a driver claiming all report IDs first. */
+       iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
+       locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
+       dev = config_found_sm_loc(self, "ihidbus", locs, &iha,
+           ihidev_print, ihidev_submatch);
+       if (dev != NULL) {
+               for (repid = 0; repid < sc->sc_nrepid; repid++)
+                       sc->sc_subdevs[repid] = device_private(dev);
+               return;
+       }
+
+       for (repid = 0; repid < sc->sc_nrepid; repid++) {
+               if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input,
+                   repid) == 0 &&
+                   hid_report_size(sc->sc_report, sc->sc_reportlen,
+                   hid_output, repid) == 0 &&
+                   hid_report_size(sc->sc_report, sc->sc_reportlen,
+                   hid_feature, repid) == 0)
+                       continue;
+
+               iha.reportid = repid;
+               locs[IHIDBUSCF_REPORTID] = repid;
+               dev = config_found_sm_loc(self, "ihidbus", locs,
+                   &iha, ihidev_print, ihidev_submatch);
+               sc->sc_subdevs[repid] = device_private(dev);
+       }
+
+       /* power down until we're opened */
+       if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
+               aprint_error_dev(sc->sc_dev, "failed to power down\n");
+               return;
+       }
+       if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
+               aprint_error_dev(self, "couldn't establish power handler\n");
+}
+
+static int
+ihidev_detach(device_t self, int flags)
+{
+       struct ihidev_softc *sc = device_private(self);
+
+       mutex_enter(&sc->sc_intr_lock);
+#if NACPICA > 0



Home | Main Index | Thread Index | Old Index