Home | History | Annotate | Line # | Download | only in i2c
ihidev.c revision 1.11
      1  1.10   thorpej /* $NetBSD: ihidev.c,v 1.11 2019/12/25 01:19:56 thorpej Exp $ */
      2   1.1    bouyer /* $OpenBSD ihidev.c,v 1.13 2017/04/08 02:57:23 deraadt Exp $ */
      3   1.1    bouyer 
      4   1.1    bouyer /*-
      5   1.1    bouyer  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      6   1.1    bouyer  * All rights reserved.
      7   1.1    bouyer  *
      8   1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      9   1.1    bouyer  * by Manuel Bouyer.
     10   1.1    bouyer  *
     11   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     12   1.1    bouyer  * modification, are permitted provided that the following conditions
     13   1.1    bouyer  * are met:
     14   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     15   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     16   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     18   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     19   1.1    bouyer  *
     20   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1    bouyer  */
     32   1.1    bouyer 
     33   1.1    bouyer /*
     34   1.1    bouyer  * Copyright (c) 2015, 2016 joshua stein <jcs (at) openbsd.org>
     35   1.1    bouyer  *
     36   1.1    bouyer  * Permission to use, copy, modify, and distribute this software for any
     37   1.1    bouyer  * purpose with or without fee is hereby granted, provided that the above
     38   1.1    bouyer  * copyright notice and this permission notice appear in all copies.
     39   1.1    bouyer  *
     40   1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     41   1.1    bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     42   1.1    bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     43   1.1    bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     44   1.1    bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     45   1.1    bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     46   1.1    bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     47   1.1    bouyer  */
     48   1.1    bouyer 
     49   1.1    bouyer /*
     50   1.1    bouyer  * HID-over-i2c driver
     51   1.1    bouyer  *
     52   1.1    bouyer  * https://msdn.microsoft.com/en-us/library/windows/hardware/dn642101%28v=vs.85%29.aspx
     53   1.1    bouyer  *
     54   1.1    bouyer  */
     55   1.1    bouyer 
     56   1.1    bouyer #include <sys/cdefs.h>
     57  1.10   thorpej __KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.11 2019/12/25 01:19:56 thorpej Exp $");
     58   1.1    bouyer 
     59   1.1    bouyer #include <sys/param.h>
     60   1.1    bouyer #include <sys/systm.h>
     61   1.1    bouyer #include <sys/device.h>
     62   1.1    bouyer #include <sys/kmem.h>
     63   1.1    bouyer 
     64   1.1    bouyer 
     65   1.1    bouyer #include <dev/i2c/i2cvar.h>
     66   1.1    bouyer #include <dev/i2c/ihidev.h>
     67   1.1    bouyer 
     68   1.1    bouyer #include <dev/hid/hid.h>
     69   1.1    bouyer 
     70   1.1    bouyer #if defined(__i386__) || defined(__amd64__)
     71   1.1    bouyer #  include "acpica.h"
     72   1.1    bouyer #endif
     73   1.1    bouyer #if NACPICA > 0
     74   1.1    bouyer #include <dev/acpi/acpi_intr.h>
     75   1.1    bouyer #endif
     76   1.1    bouyer 
     77   1.1    bouyer #include "locators.h"
     78   1.1    bouyer 
     79   1.1    bouyer /* #define IHIDEV_DEBUG */
     80   1.1    bouyer 
     81   1.1    bouyer #ifdef IHIDEV_DEBUG
     82   1.1    bouyer #define DPRINTF(x) printf x
     83   1.1    bouyer #else
     84   1.1    bouyer #define DPRINTF(x)
     85   1.1    bouyer #endif
     86   1.1    bouyer 
     87   1.1    bouyer /* 7.2 */
     88   1.1    bouyer enum {
     89   1.1    bouyer 	I2C_HID_CMD_DESCR	= 0x0,
     90   1.1    bouyer 	I2C_HID_CMD_RESET	= 0x1,
     91   1.1    bouyer 	I2C_HID_CMD_GET_REPORT	= 0x2,
     92   1.1    bouyer 	I2C_HID_CMD_SET_REPORT	= 0x3,
     93   1.1    bouyer 	I2C_HID_CMD_GET_IDLE	= 0x4,
     94   1.1    bouyer 	I2C_HID_CMD_SET_IDLE	= 0x5,
     95   1.1    bouyer 	I2C_HID_CMD_GET_PROTO	= 0x6,
     96   1.1    bouyer 	I2C_HID_CMD_SET_PROTO	= 0x7,
     97   1.1    bouyer 	I2C_HID_CMD_SET_POWER	= 0x8,
     98   1.1    bouyer 
     99   1.1    bouyer 	/* pseudo commands */
    100   1.1    bouyer 	I2C_HID_REPORT_DESCR	= 0x100,
    101   1.1    bouyer };
    102   1.1    bouyer 
    103   1.1    bouyer static int I2C_HID_POWER_ON	= 0x0;
    104   1.1    bouyer static int I2C_HID_POWER_OFF	= 0x1;
    105   1.1    bouyer 
    106   1.1    bouyer static int	ihidev_match(device_t, cfdata_t, void *);
    107   1.1    bouyer static void	ihidev_attach(device_t, device_t, void *);
    108   1.1    bouyer static int	ihidev_detach(device_t, int);
    109   1.1    bouyer CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
    110   1.1    bouyer     ihidev_match, ihidev_attach, ihidev_detach, NULL);
    111   1.1    bouyer 
    112   1.1    bouyer static bool	ihidev_suspend(device_t, const pmf_qual_t *);
    113   1.1    bouyer static bool	ihidev_resume(device_t, const pmf_qual_t *);
    114   1.1    bouyer static int	ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
    115   1.7  jmcneill static int	ihidev_intr(void *);
    116   1.1    bouyer static int	ihidev_reset(struct ihidev_softc *, bool);
    117   1.1    bouyer static int	ihidev_hid_desc_parse(struct ihidev_softc *);
    118   1.1    bouyer 
    119   1.1    bouyer static int	ihidev_maxrepid(void *, int);
    120   1.1    bouyer static int	ihidev_print(void *, const char *);
    121   1.1    bouyer static int	ihidev_submatch(device_t, cfdata_t, const int *, void *);
    122   1.1    bouyer 
    123   1.5   thorpej static const struct device_compatible_entry compat_data[] = {
    124   1.5   thorpej 	{ "hid-over-i2c",		0 },
    125   1.5   thorpej 	{ NULL,				0 }
    126   1.4   thorpej };
    127   1.4   thorpej 
    128   1.1    bouyer static int
    129   1.1    bouyer ihidev_match(device_t parent, cfdata_t match, void *aux)
    130   1.1    bouyer {
    131   1.1    bouyer 	struct i2c_attach_args * const ia = aux;
    132   1.3   thorpej 	int match_result;
    133   1.3   thorpej 
    134   1.5   thorpej 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    135   1.3   thorpej 		return I2C_MATCH_DIRECT_COMPATIBLE;
    136   1.1    bouyer 
    137   1.1    bouyer 	return 0;
    138   1.1    bouyer }
    139   1.1    bouyer 
    140   1.1    bouyer static void
    141   1.1    bouyer ihidev_attach(device_t parent, device_t self, void *aux)
    142   1.1    bouyer {
    143   1.1    bouyer 	struct ihidev_softc *sc = device_private(self);
    144   1.1    bouyer 	struct i2c_attach_args *ia = aux;
    145   1.1    bouyer 	struct ihidev_attach_arg iha;
    146   1.1    bouyer 	device_t dev;
    147   1.1    bouyer 	int repid, repsz;
    148   1.1    bouyer 	int isize;
    149   1.1    bouyer 	uint32_t v;
    150   1.1    bouyer 	int locs[IHIDBUSCF_NLOCS];
    151   1.1    bouyer 
    152   1.1    bouyer 
    153   1.1    bouyer 	sc->sc_dev = self;
    154   1.1    bouyer 	sc->sc_tag = ia->ia_tag;
    155   1.1    bouyer 	sc->sc_addr = ia->ia_addr;
    156   1.1    bouyer 	sc->sc_phandle = ia->ia_cookie;
    157   1.1    bouyer 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    158   1.1    bouyer 
    159   1.1    bouyer 	if (!prop_dictionary_get_uint32(ia->ia_prop, "hid-descr-addr", &v)) {
    160   1.1    bouyer 		aprint_error(": no hid-descr-addr value\n");
    161   1.1    bouyer 		return;
    162   1.1    bouyer 	}
    163   1.1    bouyer 
    164   1.1    bouyer 	sc->sc_hid_desc_addr = v;
    165   1.1    bouyer 
    166   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
    167   1.1    bouyer 	    ihidev_hid_desc_parse(sc)) {
    168   1.1    bouyer 		aprint_error(": failed fetching initial HID descriptor\n");
    169   1.1    bouyer 		return;
    170   1.1    bouyer 	}
    171   1.1    bouyer 
    172   1.1    bouyer 	aprint_naive("\n");
    173   1.1    bouyer 	aprint_normal(": vendor 0x%x product 0x%x, %s\n",
    174   1.1    bouyer 	    le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
    175   1.1    bouyer 	    ia->ia_name);
    176   1.1    bouyer 
    177   1.1    bouyer 	sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
    178   1.1    bouyer 	if (sc->sc_nrepid < 0)
    179   1.1    bouyer 		return;
    180   1.1    bouyer 
    181   1.1    bouyer 	aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
    182   1.1    bouyer 	    sc->sc_nrepid > 1 ? "s" : "");
    183   1.1    bouyer 
    184   1.1    bouyer 	sc->sc_nrepid++;
    185   1.1    bouyer 	sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
    186   1.9       chs 	    KM_SLEEP);
    187   1.1    bouyer 
    188   1.1    bouyer 	/* find largest report size and allocate memory for input buffer */
    189   1.1    bouyer 	sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
    190   1.1    bouyer 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    191   1.1    bouyer 		repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
    192   1.1    bouyer 		    hid_input, repid);
    193   1.1    bouyer 
    194   1.1    bouyer 		isize = repsz + 2; /* two bytes for the length */
    195   1.1    bouyer 		isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
    196   1.1    bouyer 		if (isize > sc->sc_isize)
    197   1.1    bouyer 			sc->sc_isize = isize;
    198   1.1    bouyer 
    199   1.1    bouyer 		DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
    200   1.1    bouyer 		    repsz));
    201   1.1    bouyer 	}
    202   1.9       chs 	sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_SLEEP);
    203  1.11   thorpej #if NACPICA > 0
    204  1.11   thorpej 	{
    205  1.11   thorpej 		char buf[100];
    206  1.11   thorpej 
    207  1.11   thorpej 		sc->sc_ih = acpi_intr_establish(self, sc->sc_phandle, IPL_TTY,
    208  1.11   thorpej 		    false, ihidev_intr, sc, device_xname(self));
    209  1.11   thorpej 		if (sc->sc_ih == NULL) {
    210  1.11   thorpej 			aprint_error_dev(self, "can't establish interrupt\n");
    211  1.11   thorpej 			return;
    212  1.11   thorpej 		}
    213  1.11   thorpej 		aprint_normal_dev(self, "interrupting at %s\n",
    214  1.11   thorpej 		    acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
    215   1.1    bouyer 	}
    216  1.11   thorpej #endif
    217   1.1    bouyer 
    218   1.1    bouyer 	iha.iaa = ia;
    219   1.1    bouyer 	iha.parent = sc;
    220   1.1    bouyer 
    221   1.1    bouyer 	/* Look for a driver claiming all report IDs first. */
    222   1.1    bouyer 	iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
    223   1.1    bouyer 	locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
    224   1.1    bouyer 	dev = config_found_sm_loc(self, "ihidbus", locs, &iha,
    225   1.1    bouyer 	    ihidev_print, ihidev_submatch);
    226   1.1    bouyer 	if (dev != NULL) {
    227   1.1    bouyer 		for (repid = 0; repid < sc->sc_nrepid; repid++)
    228   1.1    bouyer 			sc->sc_subdevs[repid] = device_private(dev);
    229   1.1    bouyer 		return;
    230   1.1    bouyer 	}
    231   1.1    bouyer 
    232   1.1    bouyer 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    233   1.1    bouyer 		if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input,
    234   1.1    bouyer 		    repid) == 0 &&
    235   1.1    bouyer 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    236   1.1    bouyer 		    hid_output, repid) == 0 &&
    237   1.1    bouyer 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    238   1.1    bouyer 		    hid_feature, repid) == 0)
    239   1.1    bouyer 			continue;
    240   1.1    bouyer 
    241   1.1    bouyer 		iha.reportid = repid;
    242   1.1    bouyer 		locs[IHIDBUSCF_REPORTID] = repid;
    243   1.1    bouyer 		dev = config_found_sm_loc(self, "ihidbus", locs,
    244   1.1    bouyer 		    &iha, ihidev_print, ihidev_submatch);
    245   1.1    bouyer 		sc->sc_subdevs[repid] = device_private(dev);
    246   1.1    bouyer 	}
    247   1.1    bouyer 
    248   1.1    bouyer 	/* power down until we're opened */
    249   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
    250   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    251   1.1    bouyer 		return;
    252   1.1    bouyer 	}
    253   1.1    bouyer 	if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
    254   1.1    bouyer 		aprint_error_dev(self, "couldn't establish power handler\n");
    255   1.1    bouyer }
    256   1.1    bouyer 
    257   1.1    bouyer static int
    258   1.1    bouyer ihidev_detach(device_t self, int flags)
    259   1.1    bouyer {
    260   1.1    bouyer 	struct ihidev_softc *sc = device_private(self);
    261   1.1    bouyer 
    262   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    263  1.11   thorpej #if NACPICA > 0
    264  1.11   thorpej 	if (sc->sc_ih != NULL)
    265  1.11   thorpej 		acpi_intr_disestablish(sc->sc_ih);
    266  1.11   thorpej #endif
    267   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    268   1.1    bouyer 	    &I2C_HID_POWER_OFF, true))
    269   1.1    bouyer 	aprint_error_dev(sc->sc_dev, "failed to power down\n");
    270   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    271   1.1    bouyer 	if (sc->sc_ibuf != NULL) {
    272   1.1    bouyer 		kmem_free(sc->sc_ibuf, sc->sc_isize);
    273   1.1    bouyer 		sc->sc_ibuf = NULL;
    274   1.1    bouyer 	}
    275   1.1    bouyer 
    276   1.1    bouyer 	if (sc->sc_report != NULL)
    277   1.1    bouyer 		kmem_free(sc->sc_report, sc->sc_reportlen);
    278   1.1    bouyer 
    279   1.1    bouyer 	pmf_device_deregister(self);
    280   1.1    bouyer 	return (0);
    281   1.1    bouyer }
    282   1.1    bouyer 
    283   1.1    bouyer static bool
    284   1.1    bouyer ihidev_suspend(device_t self, const pmf_qual_t *q)
    285   1.1    bouyer {
    286   1.1    bouyer 	struct ihidev_softc *sc = device_private(self);
    287   1.1    bouyer 
    288   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    289   1.1    bouyer 	if (sc->sc_refcnt > 0) {
    290   1.1    bouyer 		printf("ihidev power off\n");
    291   1.1    bouyer 		if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    292   1.1    bouyer 		    &I2C_HID_POWER_OFF, true))
    293   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    294   1.1    bouyer 	}
    295   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    296   1.1    bouyer 	return true;
    297   1.1    bouyer }
    298   1.1    bouyer 
    299   1.1    bouyer static bool
    300   1.1    bouyer ihidev_resume(device_t self, const pmf_qual_t *q)
    301   1.1    bouyer {
    302   1.1    bouyer 	struct ihidev_softc *sc = device_private(self);
    303   1.1    bouyer 
    304   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    305   1.1    bouyer 	if (sc->sc_refcnt > 0) {
    306   1.1    bouyer 		printf("ihidev power reset\n");
    307   1.1    bouyer 		ihidev_reset(sc, true);
    308   1.1    bouyer 	}
    309   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    310   1.1    bouyer 	return true;
    311   1.1    bouyer }
    312   1.1    bouyer 
    313   1.1    bouyer static int
    314   1.1    bouyer ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll)
    315   1.1    bouyer {
    316   1.1    bouyer 	int i, res = 1;
    317   1.1    bouyer 	int flags = poll ? I2C_F_POLL : 0;
    318   1.1    bouyer 
    319   1.1    bouyer 	iic_acquire_bus(sc->sc_tag, flags);
    320   1.1    bouyer 
    321   1.1    bouyer 	switch (hidcmd) {
    322   1.1    bouyer 	case I2C_HID_CMD_DESCR: {
    323   1.1    bouyer 		/*
    324   1.1    bouyer 		 * 5.2.2 - HID Descriptor Retrieval
    325   1.1    bouyer 		 * register is passed from the controller
    326   1.1    bouyer 		 */
    327   1.1    bouyer 		uint8_t cmd[] = {
    328   1.1    bouyer 			htole16(sc->sc_hid_desc_addr) & 0xff,
    329   1.1    bouyer 			htole16(sc->sc_hid_desc_addr) >> 8,
    330   1.1    bouyer 		};
    331   1.1    bouyer 
    332   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n",
    333   1.1    bouyer 		    sc->sc_dev.dv_xname, htole16(sc->sc_hid_desc_addr)));
    334   1.1    bouyer 
    335   1.1    bouyer 		/* 20 00 */
    336   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    337   1.1    bouyer 		    &cmd, sizeof(cmd), &sc->hid_desc_buf,
    338   1.1    bouyer 		    sizeof(struct i2c_hid_desc), flags);
    339   1.1    bouyer 
    340   1.1    bouyer 		DPRINTF(("%s: HID descriptor:", sc->sc_dev.dv_xname));
    341   1.1    bouyer 		for (i = 0; i < sizeof(struct i2c_hid_desc); i++)
    342   1.1    bouyer 			DPRINTF((" %.2x", sc->hid_desc_buf[i]));
    343   1.1    bouyer 		DPRINTF(("\n"));
    344   1.1    bouyer 
    345   1.1    bouyer 		break;
    346   1.1    bouyer 	}
    347   1.1    bouyer 	case I2C_HID_CMD_RESET: {
    348   1.1    bouyer 		uint8_t cmd[] = {
    349   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    350   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    351   1.1    bouyer 			0,
    352   1.1    bouyer 			I2C_HID_CMD_RESET,
    353   1.1    bouyer 		};
    354   1.1    bouyer 
    355   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n",
    356   1.1    bouyer 		    sc->sc_dev.dv_xname));
    357   1.1    bouyer 
    358   1.1    bouyer 		/* 22 00 00 01 */
    359   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    360   1.1    bouyer 		    &cmd, sizeof(cmd), NULL, 0, flags);
    361   1.1    bouyer 
    362   1.1    bouyer 		break;
    363   1.1    bouyer 	}
    364   1.1    bouyer 	case I2C_HID_CMD_GET_REPORT: {
    365   1.1    bouyer 		struct i2c_hid_report_request *rreq =
    366   1.1    bouyer 		    (struct i2c_hid_report_request *)arg;
    367   1.1    bouyer 
    368   1.1    bouyer 		uint8_t cmd[] = {
    369   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    370   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    371   1.1    bouyer 			0,
    372   1.1    bouyer 			I2C_HID_CMD_GET_REPORT,
    373   1.1    bouyer 			0, 0, 0,
    374   1.1    bouyer 		};
    375   1.1    bouyer 		int cmdlen = 7;
    376   1.1    bouyer 		int dataoff = 4;
    377   1.1    bouyer 		int report_id = rreq->id;
    378   1.1    bouyer 		int report_id_len = 1;
    379   1.1    bouyer 		int report_len = rreq->len + 2;
    380   1.1    bouyer 		int d;
    381   1.1    bouyer 		uint8_t *tmprep;
    382   1.1    bouyer 
    383   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d "
    384   1.1    bouyer 		    "(type %d, len %d)\n", sc->sc_dev.dv_xname, report_id,
    385   1.1    bouyer 		    rreq->type, rreq->len));
    386   1.1    bouyer 
    387   1.1    bouyer 		/*
    388   1.1    bouyer 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    389   1.1    bouyer 		 * report ID >= 15 is necessary, then the Report ID in the Low
    390   1.1    bouyer 		 * Byte must be set to 1111 and a Third Byte is appended to the
    391   1.1    bouyer 		 * protocol.  This Third Byte contains the entire/actual report
    392   1.1    bouyer 		 * ID."
    393   1.1    bouyer 		 */
    394   1.1    bouyer 		if (report_id >= 15) {
    395   1.1    bouyer 			cmd[dataoff++] = report_id;
    396   1.1    bouyer 			report_id = 15;
    397   1.1    bouyer 			report_id_len = 2;
    398   1.1    bouyer 		} else
    399   1.1    bouyer 			cmdlen--;
    400   1.1    bouyer 
    401   1.1    bouyer 		cmd[2] = report_id | rreq->type << 4;
    402   1.1    bouyer 
    403   1.1    bouyer 		cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff;
    404   1.1    bouyer 		cmd[dataoff] = sc->hid_desc.wDataRegister >> 8;
    405   1.1    bouyer 
    406   1.1    bouyer 		/*
    407   1.1    bouyer 		 * 7.2.2.2 - Response will be a 2-byte length value, the report
    408   1.1    bouyer 		 * id with length determined above, and then the report.
    409   1.1    bouyer 		 * Allocate rreq->len + 2 + 2 bytes, read into that temporary
    410   1.1    bouyer 		 * buffer, and then copy only the report back out to
    411   1.1    bouyer 		 * rreq->data.
    412   1.1    bouyer 		 */
    413   1.1    bouyer 		report_len += report_id_len;
    414   1.1    bouyer 		tmprep = kmem_zalloc(report_len, KM_NOSLEEP);
    415   1.1    bouyer 
    416   1.1    bouyer 		/* type 3 id 8: 22 00 38 02 23 00 */
    417   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    418   1.1    bouyer 		    &cmd, cmdlen, tmprep, report_len, flags);
    419   1.1    bouyer 
    420   1.1    bouyer 		d = tmprep[0] | tmprep[1] << 8;
    421   1.1    bouyer 		if (d != report_len) {
    422   1.1    bouyer 			DPRINTF(("%s: response size %d != expected length %d\n",
    423   1.1    bouyer 			    sc->sc_dev.dv_xname, d, report_len));
    424   1.1    bouyer 		}
    425   1.1    bouyer 
    426   1.1    bouyer 		if (report_id_len == 2)
    427   1.1    bouyer 			d = tmprep[2] | tmprep[3] << 8;
    428   1.1    bouyer 		else
    429   1.1    bouyer 			d = tmprep[2];
    430   1.1    bouyer 
    431   1.1    bouyer 		if (d != rreq->id) {
    432   1.1    bouyer 			DPRINTF(("%s: response report id %d != %d\n",
    433   1.1    bouyer 			    sc->sc_dev.dv_xname, d, rreq->id));
    434   1.1    bouyer 			iic_release_bus(sc->sc_tag, 0);
    435   1.1    bouyer 			kmem_free(tmprep, report_len);
    436   1.1    bouyer 			return (1);
    437   1.1    bouyer 		}
    438   1.1    bouyer 
    439   1.1    bouyer 		DPRINTF(("%s: response:", sc->sc_dev.dv_xname));
    440   1.1    bouyer 		for (i = 0; i < report_len; i++)
    441   1.1    bouyer 			DPRINTF((" %.2x", tmprep[i]));
    442   1.1    bouyer 		DPRINTF(("\n"));
    443   1.1    bouyer 
    444   1.1    bouyer 		memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len);
    445   1.1    bouyer 		kmem_free(tmprep, report_len);
    446   1.1    bouyer 
    447   1.1    bouyer 		break;
    448   1.1    bouyer 	}
    449   1.1    bouyer 	case I2C_HID_CMD_SET_REPORT: {
    450   1.1    bouyer 		struct i2c_hid_report_request *rreq =
    451   1.1    bouyer 		    (struct i2c_hid_report_request *)arg;
    452   1.1    bouyer 
    453   1.1    bouyer 		uint8_t cmd[] = {
    454   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    455   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    456   1.1    bouyer 			0,
    457   1.1    bouyer 			I2C_HID_CMD_SET_REPORT,
    458   1.1    bouyer 			0, 0, 0, 0, 0, 0,
    459   1.1    bouyer 		};
    460   1.1    bouyer 		int cmdlen = 10;
    461   1.1    bouyer 		int report_id = rreq->id;
    462   1.1    bouyer 		int report_len = 2 + (report_id ? 1 : 0) + rreq->len;
    463   1.1    bouyer 		int dataoff;
    464   1.1    bouyer 		uint8_t *finalcmd;
    465   1.1    bouyer 
    466   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d "
    467   1.1    bouyer 		    "(type %d, len %d):", sc->sc_dev.dv_xname, report_id,
    468   1.1    bouyer 		    rreq->type, rreq->len));
    469   1.1    bouyer 		for (i = 0; i < rreq->len; i++)
    470   1.1    bouyer 			DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i]));
    471   1.1    bouyer 		DPRINTF(("\n"));
    472   1.1    bouyer 
    473   1.1    bouyer 		/*
    474   1.1    bouyer 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    475   1.1    bouyer 		 * report ID >= 15 is necessary, then the Report ID in the Low
    476   1.1    bouyer 		 * Byte must be set to 1111 and a Third Byte is appended to the
    477   1.1    bouyer 		 * protocol.  This Third Byte contains the entire/actual report
    478   1.1    bouyer 		 * ID."
    479   1.1    bouyer 		 */
    480   1.1    bouyer 		dataoff = 4;
    481   1.1    bouyer 		if (report_id >= 15) {
    482   1.1    bouyer 			cmd[dataoff++] = report_id;
    483   1.1    bouyer 			report_id = 15;
    484   1.1    bouyer 		} else
    485   1.1    bouyer 			cmdlen--;
    486   1.1    bouyer 
    487   1.1    bouyer 		cmd[2] = report_id | rreq->type << 4;
    488   1.1    bouyer 
    489   1.1    bouyer 		if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) {
    490   1.1    bouyer 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    491   1.1    bouyer 			    & 0xff;
    492   1.1    bouyer 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    493   1.1    bouyer 			    >> 8;
    494   1.1    bouyer 		} else {
    495   1.1    bouyer 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    496   1.1    bouyer 			    & 0xff;
    497   1.1    bouyer 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    498   1.1    bouyer 			    >> 8;
    499   1.1    bouyer 		}
    500   1.1    bouyer 
    501   1.1    bouyer 		cmd[dataoff++] = report_len & 0xff;
    502   1.1    bouyer 		cmd[dataoff++] = report_len >> 8;
    503   1.1    bouyer 		cmd[dataoff] = rreq->id;
    504   1.1    bouyer 
    505   1.1    bouyer 		finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP);
    506   1.1    bouyer 
    507   1.1    bouyer 		memcpy(finalcmd, cmd, cmdlen);
    508   1.1    bouyer 		memcpy(finalcmd + cmdlen, rreq->data, rreq->len);
    509   1.1    bouyer 
    510   1.1    bouyer 		/* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */
    511   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    512   1.1    bouyer 		    finalcmd, cmdlen + rreq->len, NULL, 0, flags);
    513   1.1    bouyer 		kmem_free(finalcmd, cmdlen + rreq->len);
    514   1.1    bouyer 
    515   1.1    bouyer  		break;
    516   1.1    bouyer  	}
    517   1.1    bouyer 
    518   1.1    bouyer 	case I2C_HID_CMD_SET_POWER: {
    519   1.1    bouyer 		int power = *(int *)arg;
    520   1.1    bouyer 		uint8_t cmd[] = {
    521   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    522   1.1    bouyer 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    523   1.1    bouyer 			power,
    524   1.1    bouyer 			I2C_HID_CMD_SET_POWER,
    525   1.1    bouyer 		};
    526   1.1    bouyer 
    527   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n",
    528   1.1    bouyer 		    sc->sc_dev.dv_xname, power));
    529   1.1    bouyer 
    530   1.1    bouyer 		/* 22 00 00 08 */
    531   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    532   1.1    bouyer 		    &cmd, sizeof(cmd), NULL, 0, flags);
    533   1.1    bouyer 
    534   1.1    bouyer 		break;
    535   1.1    bouyer 	}
    536   1.1    bouyer 	case I2C_HID_REPORT_DESCR: {
    537   1.1    bouyer 		uint8_t cmd[] = {
    538   1.1    bouyer 			htole16(sc->hid_desc.wReportDescRegister) & 0xff,
    539   1.1    bouyer 			htole16(sc->hid_desc.wReportDescRegister) >> 8,
    540   1.1    bouyer 		};
    541   1.1    bouyer 
    542   1.1    bouyer 		DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with "
    543   1.1    bouyer 		    "size %d\n", sc->sc_dev.dv_xname, cmd[0],
    544   1.1    bouyer 		    sc->sc_reportlen));
    545   1.1    bouyer 
    546   1.1    bouyer 		/* 20 00 */
    547   1.1    bouyer 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    548   1.1    bouyer 		    &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags);
    549   1.1    bouyer 
    550   1.1    bouyer 		DPRINTF(("%s: HID report descriptor:", sc->sc_dev.dv_xname));
    551   1.1    bouyer 		for (i = 0; i < sc->sc_reportlen; i++)
    552   1.1    bouyer 			DPRINTF((" %.2x", sc->sc_report[i]));
    553   1.1    bouyer 		DPRINTF(("\n"));
    554   1.1    bouyer 
    555   1.1    bouyer 		break;
    556   1.1    bouyer 	}
    557   1.1    bouyer 	default:
    558   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "unknown command %d\n",
    559   1.1    bouyer 		    hidcmd);
    560   1.1    bouyer 	}
    561   1.1    bouyer 
    562   1.1    bouyer 	iic_release_bus(sc->sc_tag, flags);
    563   1.1    bouyer 
    564   1.1    bouyer 	return (res);
    565   1.1    bouyer }
    566   1.1    bouyer 
    567   1.1    bouyer static int
    568   1.1    bouyer ihidev_reset(struct ihidev_softc *sc, bool poll)
    569   1.1    bouyer {
    570   1.1    bouyer 	DPRINTF(("%s: resetting\n", sc->sc_dev.dv_xname));
    571   1.1    bouyer 
    572   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    573   1.1    bouyer 	    &I2C_HID_POWER_ON, poll)) {
    574   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed to power on\n");
    575   1.1    bouyer 		return (1);
    576   1.1    bouyer 	}
    577   1.1    bouyer 
    578   1.1    bouyer 	DELAY(1000);
    579   1.1    bouyer 
    580   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
    581   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
    582   1.1    bouyer 
    583   1.1    bouyer 		ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    584   1.1    bouyer 		    &I2C_HID_POWER_OFF, poll);
    585   1.1    bouyer 
    586   1.1    bouyer 		return (1);
    587   1.1    bouyer 	}
    588   1.1    bouyer 
    589   1.1    bouyer 	DELAY(1000);
    590   1.1    bouyer 
    591   1.1    bouyer 	return (0);
    592   1.1    bouyer }
    593   1.1    bouyer 
    594   1.1    bouyer /*
    595   1.1    bouyer  * 5.2.2 - HID Descriptor Retrieval
    596   1.1    bouyer  *
    597   1.1    bouyer  * parse HID Descriptor that has already been read into hid_desc with
    598   1.1    bouyer  * I2C_HID_CMD_DESCR
    599   1.1    bouyer  */
    600   1.1    bouyer static int
    601   1.1    bouyer ihidev_hid_desc_parse(struct ihidev_softc *sc)
    602   1.1    bouyer {
    603   1.1    bouyer 	int retries = 3;
    604   1.1    bouyer 
    605   1.1    bouyer 	/* must be v01.00 */
    606   1.1    bouyer 	if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
    607   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
    608   1.1    bouyer 		    "bad HID descriptor bcdVersion (0x%x)\n",
    609   1.1    bouyer 		    le16toh(sc->hid_desc.bcdVersion));
    610   1.1    bouyer 		return (1);
    611   1.1    bouyer 	}
    612   1.1    bouyer 
    613   1.1    bouyer 	/* must be 30 bytes for v1.00 */
    614   1.1    bouyer 	if (le16toh(sc->hid_desc.wHIDDescLength !=
    615   1.1    bouyer 	    sizeof(struct i2c_hid_desc))) {
    616   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
    617   1.1    bouyer 		    "bad HID descriptor size (%d != %zu)\n",
    618   1.1    bouyer 		    le16toh(sc->hid_desc.wHIDDescLength),
    619   1.1    bouyer 		    sizeof(struct i2c_hid_desc));
    620   1.1    bouyer 		return (1);
    621   1.1    bouyer 	}
    622   1.1    bouyer 
    623   1.1    bouyer 	if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
    624   1.1    bouyer 		aprint_error_dev(sc->sc_dev,
    625   1.1    bouyer 		    "bad HID report descriptor size (%d)\n",
    626   1.1    bouyer 		    le16toh(sc->hid_desc.wReportDescLength));
    627   1.1    bouyer 		return (1);
    628   1.1    bouyer 	}
    629   1.1    bouyer 
    630   1.1    bouyer 	while (retries-- > 0) {
    631   1.1    bouyer 		if (ihidev_reset(sc, false)) {
    632   1.1    bouyer 			if (retries == 0)
    633   1.1    bouyer 				return(1);
    634   1.1    bouyer 
    635   1.1    bouyer 			DELAY(1000);
    636   1.1    bouyer 		}
    637   1.1    bouyer 		else
    638   1.1    bouyer 			break;
    639   1.1    bouyer 	}
    640   1.1    bouyer 
    641   1.1    bouyer 	sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
    642   1.1    bouyer 	sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_NOSLEEP);
    643   1.1    bouyer 
    644   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
    645   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
    646   1.1    bouyer 		return (1);
    647   1.1    bouyer 	}
    648   1.1    bouyer 
    649   1.1    bouyer 	return (0);
    650   1.1    bouyer }
    651   1.1    bouyer 
    652   1.7  jmcneill static int
    653   1.1    bouyer ihidev_intr(void *arg)
    654   1.1    bouyer {
    655  1.11   thorpej 	struct ihidev_softc *sc = arg;
    656   1.1    bouyer 	struct ihidev *scd;
    657   1.1    bouyer 	u_int psize;
    658   1.1    bouyer 	int res, i;
    659   1.1    bouyer 	u_char *p;
    660   1.1    bouyer 	u_int rep = 0;
    661   1.1    bouyer 
    662  1.11   thorpej 	/*
    663  1.11   thorpej 	 * XXX: force I2C_F_POLL for now to avoid dwiic interrupting
    664  1.11   thorpej 	 * while we are interrupting
    665  1.11   thorpej 	 */
    666  1.11   thorpej 
    667  1.11   thorpej 	mutex_enter(&sc->sc_intr_lock);
    668  1.11   thorpej 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
    669  1.11   thorpej 
    670   1.1    bouyer 	res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
    671  1.11   thorpej 	    sc->sc_ibuf, sc->sc_isize, I2C_F_POLL);
    672  1.11   thorpej 
    673  1.11   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    674  1.11   thorpej 	mutex_exit(&sc->sc_intr_lock);
    675   1.1    bouyer 	if (res != 0)
    676  1.11   thorpej 		return 1;
    677   1.1    bouyer 
    678   1.1    bouyer 	/*
    679   1.1    bouyer 	 * 6.1.1 - First two bytes are the packet length, which must be less
    680   1.1    bouyer 	 * than or equal to wMaxInputLength
    681   1.1    bouyer 	 */
    682   1.1    bouyer 	psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
    683   1.1    bouyer 	if (!psize || psize > sc->sc_isize) {
    684   1.1    bouyer 		DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
    685   1.1    bouyer 		    sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize));
    686  1.11   thorpej 		return (1);
    687   1.1    bouyer 	}
    688   1.1    bouyer 
    689   1.1    bouyer 	/* 3rd byte is the report id */
    690   1.1    bouyer 	p = sc->sc_ibuf + 2;
    691   1.1    bouyer 	psize -= 2;
    692   1.1    bouyer 	if (sc->sc_nrepid != 1)
    693   1.1    bouyer 		rep = *p++, psize--;
    694   1.1    bouyer 
    695   1.1    bouyer 	if (rep >= sc->sc_nrepid) {
    696   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
    697   1.1    bouyer 		    __func__, rep);
    698  1.11   thorpej 		return (1);
    699   1.1    bouyer 	}
    700   1.1    bouyer 
    701  1.11   thorpej 	DPRINTF(("%s: ihidev_intr: hid input (rep %d):", sc->sc_dev.dv_xname,
    702  1.11   thorpej 	    rep));
    703   1.1    bouyer 	for (i = 0; i < sc->sc_isize; i++)
    704   1.1    bouyer 		DPRINTF((" %.2x", sc->sc_ibuf[i]));
    705   1.1    bouyer 	DPRINTF(("\n"));
    706   1.1    bouyer 
    707   1.1    bouyer 	scd = sc->sc_subdevs[rep];
    708   1.1    bouyer 	if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
    709  1.11   thorpej 		return (1);
    710   1.1    bouyer 
    711   1.1    bouyer 	scd->sc_intr(scd, p, psize);
    712   1.1    bouyer 
    713  1.11   thorpej 	return 1;
    714   1.1    bouyer }
    715   1.1    bouyer 
    716   1.1    bouyer static int
    717   1.1    bouyer ihidev_maxrepid(void *buf, int len)
    718   1.1    bouyer {
    719   1.1    bouyer 	struct hid_data *d;
    720   1.1    bouyer 	struct hid_item h;
    721   1.1    bouyer 	int maxid;
    722   1.1    bouyer 
    723   1.1    bouyer 	maxid = -1;
    724   1.1    bouyer 	h.report_ID = 0;
    725   1.1    bouyer 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    726   1.6  jakllsch 		if ((int)h.report_ID > maxid)
    727   1.1    bouyer 			maxid = h.report_ID;
    728   1.1    bouyer 	hid_end_parse(d);
    729   1.1    bouyer 
    730   1.1    bouyer 	return (maxid);
    731   1.1    bouyer }
    732   1.1    bouyer 
    733   1.1    bouyer static int
    734   1.1    bouyer ihidev_print(void *aux, const char *pnp)
    735   1.1    bouyer {
    736   1.1    bouyer 	struct ihidev_attach_arg *iha = aux;
    737   1.1    bouyer 
    738   1.1    bouyer 	if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
    739   1.1    bouyer 		return (QUIET);
    740   1.1    bouyer 
    741   1.1    bouyer 	if (pnp)
    742   1.1    bouyer 		aprint_normal("hid at %s", pnp);
    743   1.1    bouyer 
    744   1.1    bouyer 	if (iha->reportid != 0)
    745   1.1    bouyer 		aprint_normal(" reportid %d", iha->reportid);
    746   1.1    bouyer 
    747   1.1    bouyer 	return (UNCONF);
    748   1.1    bouyer }
    749   1.1    bouyer 
    750   1.1    bouyer static int
    751   1.1    bouyer ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    752   1.1    bouyer {
    753   1.1    bouyer 	struct ihidev_attach_arg *iha = aux;
    754   1.1    bouyer 
    755   1.1    bouyer 	if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
    756   1.1    bouyer 	    cf->ihidevcf_reportid != iha->reportid)
    757   1.1    bouyer 		return (0);
    758   1.1    bouyer 
    759   1.1    bouyer 	return config_match(parent, cf, aux);
    760   1.1    bouyer }
    761   1.1    bouyer 
    762   1.1    bouyer int
    763   1.1    bouyer ihidev_open(struct ihidev *scd)
    764   1.1    bouyer {
    765   1.1    bouyer 	struct ihidev_softc *sc = scd->sc_parent;
    766   1.1    bouyer 
    767   1.1    bouyer 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    768   1.1    bouyer 	    __func__, scd->sc_state, sc->sc_refcnt));
    769   1.1    bouyer 
    770   1.1    bouyer 	if (scd->sc_state & IHIDEV_OPEN)
    771   1.1    bouyer 		return (EBUSY);
    772   1.1    bouyer 
    773   1.1    bouyer 	scd->sc_state |= IHIDEV_OPEN;
    774   1.1    bouyer 
    775   1.1    bouyer 	if (sc->sc_refcnt++ || sc->sc_isize == 0)
    776   1.1    bouyer 		return (0);
    777   1.1    bouyer 
    778   1.1    bouyer 	/* power on */
    779   1.1    bouyer 	ihidev_reset(sc, false);
    780   1.1    bouyer 
    781   1.1    bouyer 	return (0);
    782   1.1    bouyer }
    783   1.1    bouyer 
    784   1.1    bouyer void
    785   1.1    bouyer ihidev_close(struct ihidev *scd)
    786   1.1    bouyer {
    787   1.1    bouyer 	struct ihidev_softc *sc = scd->sc_parent;
    788   1.1    bouyer 
    789   1.1    bouyer 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    790   1.1    bouyer 	    __func__, scd->sc_state, sc->sc_refcnt));
    791   1.1    bouyer 
    792   1.1    bouyer 	if (!(scd->sc_state & IHIDEV_OPEN))
    793   1.1    bouyer 		return;
    794   1.1    bouyer 
    795   1.1    bouyer 	scd->sc_state &= ~IHIDEV_OPEN;
    796   1.1    bouyer 
    797   1.1    bouyer 	if (--sc->sc_refcnt)
    798   1.1    bouyer 		return;
    799   1.1    bouyer 
    800   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    801   1.1    bouyer 	    &I2C_HID_POWER_OFF, false))
    802   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    803   1.1    bouyer }
    804   1.1    bouyer 
    805   1.1    bouyer void
    806   1.1    bouyer ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
    807   1.1    bouyer {
    808   1.1    bouyer 	*desc = sc->sc_report;
    809   1.1    bouyer 	*size = sc->sc_reportlen;
    810   1.1    bouyer }
    811   1.1    bouyer 
    812   1.1    bouyer /* convert hid_* constants used throughout HID code to i2c HID equivalents */
    813   1.1    bouyer int
    814   1.1    bouyer ihidev_report_type_conv(int hid_type_id)
    815   1.1    bouyer {
    816   1.1    bouyer 	switch (hid_type_id) {
    817   1.1    bouyer 	case hid_input:
    818   1.1    bouyer 		return I2C_HID_REPORT_TYPE_INPUT;
    819   1.1    bouyer 	case hid_output:
    820   1.1    bouyer 		return I2C_HID_REPORT_TYPE_OUTPUT;
    821   1.1    bouyer 	case hid_feature:
    822   1.1    bouyer 		return I2C_HID_REPORT_TYPE_FEATURE;
    823   1.1    bouyer 	default:
    824   1.1    bouyer 		return -1;
    825   1.1    bouyer 	}
    826   1.1    bouyer }
    827   1.1    bouyer 
    828   1.1    bouyer int
    829   1.1    bouyer ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
    830   1.1    bouyer {
    831   1.1    bouyer 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    832   1.1    bouyer 	struct i2c_hid_report_request rreq;
    833   1.1    bouyer 	int ctype;
    834   1.1    bouyer 
    835   1.1    bouyer 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    836   1.1    bouyer 		return (1);
    837   1.1    bouyer 
    838   1.1    bouyer 	rreq.type = ctype;
    839   1.1    bouyer 	rreq.id = id;
    840   1.1    bouyer 	rreq.data = data;
    841   1.1    bouyer 	rreq.len = len;
    842   1.1    bouyer 
    843   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
    844   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed fetching report\n");
    845   1.1    bouyer 		return (1);
    846   1.1    bouyer 	}
    847   1.1    bouyer 
    848   1.1    bouyer 	return 0;
    849   1.1    bouyer }
    850   1.1    bouyer 
    851   1.1    bouyer int
    852   1.1    bouyer ihidev_set_report(struct device *dev, int type, int id, void *data,
    853   1.1    bouyer     int len)
    854   1.1    bouyer {
    855   1.1    bouyer 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    856   1.1    bouyer 	struct i2c_hid_report_request rreq;
    857   1.1    bouyer 	int ctype;
    858   1.1    bouyer 
    859   1.1    bouyer 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    860   1.1    bouyer 		return (1);
    861   1.1    bouyer 
    862   1.1    bouyer 	rreq.type = ctype;
    863   1.1    bouyer 	rreq.id = id;
    864   1.1    bouyer 	rreq.data = data;
    865   1.1    bouyer 	rreq.len = len;
    866   1.1    bouyer 
    867   1.1    bouyer 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
    868   1.1    bouyer 		aprint_error_dev(sc->sc_dev, "failed setting report\n");
    869   1.1    bouyer 		return (1);
    870   1.1    bouyer 	}
    871   1.1    bouyer 
    872   1.1    bouyer 	return 0;
    873   1.1    bouyer }
    874