ihidev.c revision 1.30 1 1.30 andvar /* $NetBSD: ihidev.c,v 1.30 2024/04/29 21:25:34 andvar 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.30 andvar __KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.30 2024/04/29 21:25:34 andvar 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.21 riastrad #include <sys/workqueue.h>
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.12 thorpej #include <dev/acpi/acpivar.h>
75 1.1 bouyer #include <dev/acpi/acpi_intr.h>
76 1.1 bouyer #endif
77 1.1 bouyer
78 1.1 bouyer #include "locators.h"
79 1.1 bouyer
80 1.1 bouyer /* #define IHIDEV_DEBUG */
81 1.1 bouyer
82 1.1 bouyer #ifdef IHIDEV_DEBUG
83 1.1 bouyer #define DPRINTF(x) printf x
84 1.1 bouyer #else
85 1.1 bouyer #define DPRINTF(x)
86 1.1 bouyer #endif
87 1.1 bouyer
88 1.1 bouyer /* 7.2 */
89 1.1 bouyer enum {
90 1.1 bouyer I2C_HID_CMD_DESCR = 0x0,
91 1.1 bouyer I2C_HID_CMD_RESET = 0x1,
92 1.1 bouyer I2C_HID_CMD_GET_REPORT = 0x2,
93 1.1 bouyer I2C_HID_CMD_SET_REPORT = 0x3,
94 1.1 bouyer I2C_HID_CMD_GET_IDLE = 0x4,
95 1.1 bouyer I2C_HID_CMD_SET_IDLE = 0x5,
96 1.1 bouyer I2C_HID_CMD_GET_PROTO = 0x6,
97 1.1 bouyer I2C_HID_CMD_SET_PROTO = 0x7,
98 1.1 bouyer I2C_HID_CMD_SET_POWER = 0x8,
99 1.1 bouyer
100 1.1 bouyer /* pseudo commands */
101 1.1 bouyer I2C_HID_REPORT_DESCR = 0x100,
102 1.1 bouyer };
103 1.1 bouyer
104 1.1 bouyer static int I2C_HID_POWER_ON = 0x0;
105 1.1 bouyer static int I2C_HID_POWER_OFF = 0x1;
106 1.1 bouyer
107 1.1 bouyer static int ihidev_match(device_t, cfdata_t, void *);
108 1.1 bouyer static void ihidev_attach(device_t, device_t, void *);
109 1.1 bouyer static int ihidev_detach(device_t, int);
110 1.1 bouyer CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
111 1.1 bouyer ihidev_match, ihidev_attach, ihidev_detach, NULL);
112 1.1 bouyer
113 1.21 riastrad static bool ihidev_intr_init(struct ihidev_softc *);
114 1.21 riastrad static void ihidev_intr_fini(struct ihidev_softc *);
115 1.12 thorpej
116 1.1 bouyer static bool ihidev_suspend(device_t, const pmf_qual_t *);
117 1.1 bouyer static bool ihidev_resume(device_t, const pmf_qual_t *);
118 1.1 bouyer static int ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
119 1.30 andvar #if NACPICA > 0
120 1.7 jmcneill static int ihidev_intr(void *);
121 1.21 riastrad static void ihidev_work(struct work *, void *);
122 1.30 andvar #endif
123 1.1 bouyer static int ihidev_reset(struct ihidev_softc *, bool);
124 1.1 bouyer static int ihidev_hid_desc_parse(struct ihidev_softc *);
125 1.1 bouyer
126 1.1 bouyer static int ihidev_maxrepid(void *, int);
127 1.1 bouyer static int ihidev_print(void *, const char *);
128 1.1 bouyer static int ihidev_submatch(device_t, cfdata_t, const int *, void *);
129 1.1 bouyer
130 1.17 thorpej static bool ihidev_acpi_get_info(struct ihidev_softc *);
131 1.17 thorpej
132 1.5 thorpej static const struct device_compatible_entry compat_data[] = {
133 1.16 thorpej { .compat = "PNP0C50" },
134 1.16 thorpej { .compat = "ACPI0C50" },
135 1.13 thorpej { .compat = "hid-over-i2c" },
136 1.18 thorpej DEVICE_COMPAT_EOL
137 1.4 thorpej };
138 1.4 thorpej
139 1.1 bouyer static int
140 1.1 bouyer ihidev_match(device_t parent, cfdata_t match, void *aux)
141 1.1 bouyer {
142 1.1 bouyer struct i2c_attach_args * const ia = aux;
143 1.3 thorpej int match_result;
144 1.3 thorpej
145 1.5 thorpej if (iic_use_direct_match(ia, match, compat_data, &match_result))
146 1.29 riastrad return match_result;
147 1.1 bouyer
148 1.1 bouyer return 0;
149 1.1 bouyer }
150 1.1 bouyer
151 1.1 bouyer static void
152 1.1 bouyer ihidev_attach(device_t parent, device_t self, void *aux)
153 1.1 bouyer {
154 1.1 bouyer struct ihidev_softc *sc = device_private(self);
155 1.1 bouyer struct i2c_attach_args *ia = aux;
156 1.1 bouyer struct ihidev_attach_arg iha;
157 1.1 bouyer device_t dev;
158 1.1 bouyer int repid, repsz;
159 1.1 bouyer int isize;
160 1.1 bouyer int locs[IHIDBUSCF_NLOCS];
161 1.1 bouyer
162 1.1 bouyer sc->sc_dev = self;
163 1.1 bouyer sc->sc_tag = ia->ia_tag;
164 1.1 bouyer sc->sc_addr = ia->ia_addr;
165 1.21 riastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
166 1.1 bouyer
167 1.17 thorpej sc->sc_phandle = ia->ia_cookie;
168 1.17 thorpej if (ia->ia_cookietype != I2C_COOKIE_ACPI) {
169 1.17 thorpej aprint_error(": unsupported device tree type\n");
170 1.17 thorpej return;
171 1.17 thorpej }
172 1.21 riastrad if (!ihidev_acpi_get_info(sc)) {
173 1.1 bouyer return;
174 1.1 bouyer }
175 1.1 bouyer
176 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
177 1.1 bouyer ihidev_hid_desc_parse(sc)) {
178 1.1 bouyer aprint_error(": failed fetching initial HID descriptor\n");
179 1.1 bouyer return;
180 1.1 bouyer }
181 1.1 bouyer
182 1.1 bouyer aprint_naive("\n");
183 1.1 bouyer aprint_normal(": vendor 0x%x product 0x%x, %s\n",
184 1.1 bouyer le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
185 1.1 bouyer ia->ia_name);
186 1.1 bouyer
187 1.1 bouyer sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
188 1.1 bouyer if (sc->sc_nrepid < 0)
189 1.1 bouyer return;
190 1.1 bouyer
191 1.1 bouyer aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
192 1.1 bouyer sc->sc_nrepid > 1 ? "s" : "");
193 1.1 bouyer
194 1.1 bouyer sc->sc_nrepid++;
195 1.1 bouyer sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
196 1.9 chs KM_SLEEP);
197 1.1 bouyer
198 1.1 bouyer /* find largest report size and allocate memory for input buffer */
199 1.1 bouyer sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
200 1.1 bouyer for (repid = 0; repid < sc->sc_nrepid; repid++) {
201 1.1 bouyer repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
202 1.1 bouyer hid_input, repid);
203 1.1 bouyer
204 1.1 bouyer isize = repsz + 2; /* two bytes for the length */
205 1.1 bouyer isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
206 1.1 bouyer if (isize > sc->sc_isize)
207 1.1 bouyer sc->sc_isize = isize;
208 1.1 bouyer
209 1.28 riastrad DPRINTF(("%s: repid %d size %d\n",
210 1.28 riastrad device_xname(sc->sc_dev), repid, repsz));
211 1.1 bouyer }
212 1.9 chs sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_SLEEP);
213 1.21 riastrad if (!ihidev_intr_init(sc)) {
214 1.12 thorpej return;
215 1.1 bouyer }
216 1.1 bouyer
217 1.1 bouyer iha.iaa = ia;
218 1.1 bouyer iha.parent = sc;
219 1.1 bouyer
220 1.1 bouyer /* Look for a driver claiming all report IDs first. */
221 1.1 bouyer iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
222 1.1 bouyer locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
223 1.19 thorpej dev = config_found(self, &iha, ihidev_print,
224 1.20 thorpej CFARGS(.submatch = ihidev_submatch,
225 1.20 thorpej .locators = locs));
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.19 thorpej dev = config_found(self, &iha, ihidev_print,
244 1.20 thorpej CFARGS(.submatch = ihidev_submatch,
245 1.20 thorpej .locators = locs));
246 1.1 bouyer sc->sc_subdevs[repid] = device_private(dev);
247 1.1 bouyer }
248 1.1 bouyer
249 1.1 bouyer /* power down until we're opened */
250 1.21 riastrad if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF,
251 1.21 riastrad false)) {
252 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to power down\n");
253 1.1 bouyer return;
254 1.1 bouyer }
255 1.1 bouyer if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
256 1.1 bouyer aprint_error_dev(self, "couldn't establish power handler\n");
257 1.1 bouyer }
258 1.1 bouyer
259 1.1 bouyer static int
260 1.1 bouyer ihidev_detach(device_t self, int flags)
261 1.1 bouyer {
262 1.1 bouyer struct ihidev_softc *sc = device_private(self);
263 1.21 riastrad int error;
264 1.21 riastrad
265 1.21 riastrad error = config_detach_children(self, flags);
266 1.21 riastrad if (error)
267 1.21 riastrad return error;
268 1.21 riastrad
269 1.21 riastrad pmf_device_deregister(self);
270 1.21 riastrad ihidev_intr_fini(sc);
271 1.21 riastrad
272 1.21 riastrad if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF,
273 1.21 riastrad true))
274 1.21 riastrad aprint_error_dev(sc->sc_dev, "failed to power down\n");
275 1.1 bouyer
276 1.1 bouyer if (sc->sc_ibuf != NULL) {
277 1.1 bouyer kmem_free(sc->sc_ibuf, sc->sc_isize);
278 1.1 bouyer sc->sc_ibuf = NULL;
279 1.1 bouyer }
280 1.1 bouyer
281 1.1 bouyer if (sc->sc_report != NULL)
282 1.1 bouyer kmem_free(sc->sc_report, sc->sc_reportlen);
283 1.1 bouyer
284 1.21 riastrad mutex_destroy(&sc->sc_lock);
285 1.21 riastrad
286 1.21 riastrad return 0;
287 1.1 bouyer }
288 1.1 bouyer
289 1.1 bouyer static bool
290 1.1 bouyer ihidev_suspend(device_t self, const pmf_qual_t *q)
291 1.1 bouyer {
292 1.1 bouyer struct ihidev_softc *sc = device_private(self);
293 1.1 bouyer
294 1.21 riastrad mutex_enter(&sc->sc_lock);
295 1.1 bouyer if (sc->sc_refcnt > 0) {
296 1.1 bouyer printf("ihidev power off\n");
297 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
298 1.1 bouyer &I2C_HID_POWER_OFF, true))
299 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to power down\n");
300 1.1 bouyer }
301 1.21 riastrad mutex_exit(&sc->sc_lock);
302 1.1 bouyer return true;
303 1.1 bouyer }
304 1.1 bouyer
305 1.1 bouyer static bool
306 1.1 bouyer ihidev_resume(device_t self, const pmf_qual_t *q)
307 1.1 bouyer {
308 1.1 bouyer struct ihidev_softc *sc = device_private(self);
309 1.1 bouyer
310 1.21 riastrad mutex_enter(&sc->sc_lock);
311 1.1 bouyer if (sc->sc_refcnt > 0) {
312 1.1 bouyer printf("ihidev power reset\n");
313 1.1 bouyer ihidev_reset(sc, true);
314 1.1 bouyer }
315 1.21 riastrad mutex_exit(&sc->sc_lock);
316 1.1 bouyer return true;
317 1.1 bouyer }
318 1.1 bouyer
319 1.1 bouyer static int
320 1.1 bouyer ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll)
321 1.1 bouyer {
322 1.1 bouyer int i, res = 1;
323 1.1 bouyer int flags = poll ? I2C_F_POLL : 0;
324 1.1 bouyer
325 1.1 bouyer iic_acquire_bus(sc->sc_tag, flags);
326 1.1 bouyer
327 1.1 bouyer switch (hidcmd) {
328 1.1 bouyer case I2C_HID_CMD_DESCR: {
329 1.1 bouyer /*
330 1.1 bouyer * 5.2.2 - HID Descriptor Retrieval
331 1.1 bouyer * register is passed from the controller
332 1.1 bouyer */
333 1.1 bouyer uint8_t cmd[] = {
334 1.1 bouyer htole16(sc->sc_hid_desc_addr) & 0xff,
335 1.1 bouyer htole16(sc->sc_hid_desc_addr) >> 8,
336 1.1 bouyer };
337 1.1 bouyer
338 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n",
339 1.28 riastrad device_xname(sc->sc_dev), htole16(sc->sc_hid_desc_addr)));
340 1.1 bouyer
341 1.1 bouyer /* 20 00 */
342 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
343 1.1 bouyer &cmd, sizeof(cmd), &sc->hid_desc_buf,
344 1.1 bouyer sizeof(struct i2c_hid_desc), flags);
345 1.1 bouyer
346 1.28 riastrad DPRINTF(("%s: HID descriptor:", device_xname(sc->sc_dev)));
347 1.1 bouyer for (i = 0; i < sizeof(struct i2c_hid_desc); i++)
348 1.1 bouyer DPRINTF((" %.2x", sc->hid_desc_buf[i]));
349 1.1 bouyer DPRINTF(("\n"));
350 1.1 bouyer
351 1.1 bouyer break;
352 1.1 bouyer }
353 1.1 bouyer case I2C_HID_CMD_RESET: {
354 1.1 bouyer uint8_t cmd[] = {
355 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) & 0xff,
356 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) >> 8,
357 1.1 bouyer 0,
358 1.1 bouyer I2C_HID_CMD_RESET,
359 1.1 bouyer };
360 1.1 bouyer
361 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n",
362 1.28 riastrad device_xname(sc->sc_dev)));
363 1.1 bouyer
364 1.1 bouyer /* 22 00 00 01 */
365 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
366 1.1 bouyer &cmd, sizeof(cmd), NULL, 0, flags);
367 1.1 bouyer
368 1.1 bouyer break;
369 1.1 bouyer }
370 1.1 bouyer case I2C_HID_CMD_GET_REPORT: {
371 1.1 bouyer struct i2c_hid_report_request *rreq =
372 1.1 bouyer (struct i2c_hid_report_request *)arg;
373 1.1 bouyer
374 1.1 bouyer uint8_t cmd[] = {
375 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) & 0xff,
376 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) >> 8,
377 1.1 bouyer 0,
378 1.1 bouyer I2C_HID_CMD_GET_REPORT,
379 1.1 bouyer 0, 0, 0,
380 1.1 bouyer };
381 1.1 bouyer int cmdlen = 7;
382 1.1 bouyer int dataoff = 4;
383 1.1 bouyer int report_id = rreq->id;
384 1.1 bouyer int report_id_len = 1;
385 1.1 bouyer int report_len = rreq->len + 2;
386 1.1 bouyer int d;
387 1.1 bouyer uint8_t *tmprep;
388 1.1 bouyer
389 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d "
390 1.28 riastrad "(type %d, len %d)\n", device_xname(sc->sc_dev), report_id,
391 1.1 bouyer rreq->type, rreq->len));
392 1.1 bouyer
393 1.1 bouyer /*
394 1.1 bouyer * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
395 1.1 bouyer * report ID >= 15 is necessary, then the Report ID in the Low
396 1.1 bouyer * Byte must be set to 1111 and a Third Byte is appended to the
397 1.1 bouyer * protocol. This Third Byte contains the entire/actual report
398 1.1 bouyer * ID."
399 1.1 bouyer */
400 1.1 bouyer if (report_id >= 15) {
401 1.1 bouyer cmd[dataoff++] = report_id;
402 1.1 bouyer report_id = 15;
403 1.1 bouyer report_id_len = 2;
404 1.1 bouyer } else
405 1.1 bouyer cmdlen--;
406 1.1 bouyer
407 1.1 bouyer cmd[2] = report_id | rreq->type << 4;
408 1.1 bouyer
409 1.1 bouyer cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff;
410 1.1 bouyer cmd[dataoff] = sc->hid_desc.wDataRegister >> 8;
411 1.1 bouyer
412 1.1 bouyer /*
413 1.1 bouyer * 7.2.2.2 - Response will be a 2-byte length value, the report
414 1.1 bouyer * id with length determined above, and then the report.
415 1.1 bouyer * Allocate rreq->len + 2 + 2 bytes, read into that temporary
416 1.1 bouyer * buffer, and then copy only the report back out to
417 1.1 bouyer * rreq->data.
418 1.1 bouyer */
419 1.1 bouyer report_len += report_id_len;
420 1.1 bouyer tmprep = kmem_zalloc(report_len, KM_NOSLEEP);
421 1.23 riastrad if (tmprep == NULL) {
422 1.23 riastrad /* XXX pool or preallocate? */
423 1.23 riastrad DPRINTF(("%s: out of memory\n",
424 1.23 riastrad device_xname(sc->sc_dev)));
425 1.23 riastrad res = ENOMEM;
426 1.23 riastrad break;
427 1.23 riastrad }
428 1.1 bouyer
429 1.1 bouyer /* type 3 id 8: 22 00 38 02 23 00 */
430 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
431 1.1 bouyer &cmd, cmdlen, tmprep, report_len, flags);
432 1.1 bouyer
433 1.1 bouyer d = tmprep[0] | tmprep[1] << 8;
434 1.1 bouyer if (d != report_len) {
435 1.1 bouyer DPRINTF(("%s: response size %d != expected length %d\n",
436 1.28 riastrad device_xname(sc->sc_dev), d, report_len));
437 1.1 bouyer }
438 1.1 bouyer
439 1.1 bouyer if (report_id_len == 2)
440 1.1 bouyer d = tmprep[2] | tmprep[3] << 8;
441 1.1 bouyer else
442 1.1 bouyer d = tmprep[2];
443 1.1 bouyer
444 1.1 bouyer if (d != rreq->id) {
445 1.1 bouyer DPRINTF(("%s: response report id %d != %d\n",
446 1.28 riastrad device_xname(sc->sc_dev), d, rreq->id));
447 1.1 bouyer iic_release_bus(sc->sc_tag, 0);
448 1.1 bouyer kmem_free(tmprep, report_len);
449 1.1 bouyer return (1);
450 1.1 bouyer }
451 1.1 bouyer
452 1.28 riastrad DPRINTF(("%s: response:", device_xname(sc->sc_dev)));
453 1.1 bouyer for (i = 0; i < report_len; i++)
454 1.1 bouyer DPRINTF((" %.2x", tmprep[i]));
455 1.1 bouyer DPRINTF(("\n"));
456 1.1 bouyer
457 1.1 bouyer memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len);
458 1.1 bouyer kmem_free(tmprep, report_len);
459 1.1 bouyer
460 1.1 bouyer break;
461 1.1 bouyer }
462 1.1 bouyer case I2C_HID_CMD_SET_REPORT: {
463 1.1 bouyer struct i2c_hid_report_request *rreq =
464 1.1 bouyer (struct i2c_hid_report_request *)arg;
465 1.1 bouyer
466 1.1 bouyer uint8_t cmd[] = {
467 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) & 0xff,
468 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) >> 8,
469 1.1 bouyer 0,
470 1.1 bouyer I2C_HID_CMD_SET_REPORT,
471 1.1 bouyer 0, 0, 0, 0, 0, 0,
472 1.1 bouyer };
473 1.1 bouyer int cmdlen = 10;
474 1.1 bouyer int report_id = rreq->id;
475 1.1 bouyer int report_len = 2 + (report_id ? 1 : 0) + rreq->len;
476 1.1 bouyer int dataoff;
477 1.1 bouyer uint8_t *finalcmd;
478 1.1 bouyer
479 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d "
480 1.28 riastrad "(type %d, len %d):", device_xname(sc->sc_dev), report_id,
481 1.1 bouyer rreq->type, rreq->len));
482 1.1 bouyer for (i = 0; i < rreq->len; i++)
483 1.1 bouyer DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i]));
484 1.1 bouyer DPRINTF(("\n"));
485 1.1 bouyer
486 1.1 bouyer /*
487 1.1 bouyer * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
488 1.1 bouyer * report ID >= 15 is necessary, then the Report ID in the Low
489 1.1 bouyer * Byte must be set to 1111 and a Third Byte is appended to the
490 1.1 bouyer * protocol. This Third Byte contains the entire/actual report
491 1.1 bouyer * ID."
492 1.1 bouyer */
493 1.1 bouyer dataoff = 4;
494 1.1 bouyer if (report_id >= 15) {
495 1.1 bouyer cmd[dataoff++] = report_id;
496 1.1 bouyer report_id = 15;
497 1.1 bouyer } else
498 1.1 bouyer cmdlen--;
499 1.1 bouyer
500 1.1 bouyer cmd[2] = report_id | rreq->type << 4;
501 1.1 bouyer
502 1.1 bouyer if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) {
503 1.1 bouyer cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
504 1.1 bouyer & 0xff;
505 1.1 bouyer cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
506 1.1 bouyer >> 8;
507 1.1 bouyer } else {
508 1.1 bouyer cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
509 1.1 bouyer & 0xff;
510 1.1 bouyer cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
511 1.1 bouyer >> 8;
512 1.1 bouyer }
513 1.1 bouyer
514 1.1 bouyer cmd[dataoff++] = report_len & 0xff;
515 1.1 bouyer cmd[dataoff++] = report_len >> 8;
516 1.1 bouyer cmd[dataoff] = rreq->id;
517 1.1 bouyer
518 1.1 bouyer finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP);
519 1.23 riastrad if (finalcmd == NULL) {
520 1.23 riastrad res = ENOMEM;
521 1.23 riastrad break;
522 1.23 riastrad }
523 1.1 bouyer
524 1.1 bouyer memcpy(finalcmd, cmd, cmdlen);
525 1.1 bouyer memcpy(finalcmd + cmdlen, rreq->data, rreq->len);
526 1.1 bouyer
527 1.1 bouyer /* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */
528 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
529 1.1 bouyer finalcmd, cmdlen + rreq->len, NULL, 0, flags);
530 1.1 bouyer kmem_free(finalcmd, cmdlen + rreq->len);
531 1.1 bouyer
532 1.1 bouyer break;
533 1.1 bouyer }
534 1.1 bouyer
535 1.1 bouyer case I2C_HID_CMD_SET_POWER: {
536 1.1 bouyer int power = *(int *)arg;
537 1.1 bouyer uint8_t cmd[] = {
538 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) & 0xff,
539 1.1 bouyer htole16(sc->hid_desc.wCommandRegister) >> 8,
540 1.1 bouyer power,
541 1.1 bouyer I2C_HID_CMD_SET_POWER,
542 1.1 bouyer };
543 1.1 bouyer
544 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n",
545 1.28 riastrad device_xname(sc->sc_dev), power));
546 1.1 bouyer
547 1.1 bouyer /* 22 00 00 08 */
548 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
549 1.1 bouyer &cmd, sizeof(cmd), NULL, 0, flags);
550 1.1 bouyer
551 1.1 bouyer break;
552 1.1 bouyer }
553 1.1 bouyer case I2C_HID_REPORT_DESCR: {
554 1.1 bouyer uint8_t cmd[] = {
555 1.1 bouyer htole16(sc->hid_desc.wReportDescRegister) & 0xff,
556 1.1 bouyer htole16(sc->hid_desc.wReportDescRegister) >> 8,
557 1.1 bouyer };
558 1.1 bouyer
559 1.1 bouyer DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with "
560 1.28 riastrad "size %d\n", device_xname(sc->sc_dev), cmd[0],
561 1.1 bouyer sc->sc_reportlen));
562 1.1 bouyer
563 1.1 bouyer /* 20 00 */
564 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
565 1.1 bouyer &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags);
566 1.1 bouyer
567 1.28 riastrad DPRINTF(("%s: HID report descriptor:",
568 1.28 riastrad device_xname(sc->sc_dev)));
569 1.1 bouyer for (i = 0; i < sc->sc_reportlen; i++)
570 1.1 bouyer DPRINTF((" %.2x", sc->sc_report[i]));
571 1.1 bouyer DPRINTF(("\n"));
572 1.1 bouyer
573 1.1 bouyer break;
574 1.1 bouyer }
575 1.1 bouyer default:
576 1.1 bouyer aprint_error_dev(sc->sc_dev, "unknown command %d\n",
577 1.1 bouyer hidcmd);
578 1.1 bouyer }
579 1.1 bouyer
580 1.1 bouyer iic_release_bus(sc->sc_tag, flags);
581 1.1 bouyer
582 1.1 bouyer return (res);
583 1.1 bouyer }
584 1.1 bouyer
585 1.1 bouyer static int
586 1.1 bouyer ihidev_reset(struct ihidev_softc *sc, bool poll)
587 1.1 bouyer {
588 1.28 riastrad DPRINTF(("%s: resetting\n", device_xname(sc->sc_dev)));
589 1.1 bouyer
590 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
591 1.1 bouyer &I2C_HID_POWER_ON, poll)) {
592 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to power on\n");
593 1.1 bouyer return (1);
594 1.1 bouyer }
595 1.1 bouyer
596 1.1 bouyer DELAY(1000);
597 1.1 bouyer
598 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
599 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
600 1.1 bouyer
601 1.1 bouyer ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
602 1.1 bouyer &I2C_HID_POWER_OFF, poll);
603 1.1 bouyer
604 1.1 bouyer return (1);
605 1.1 bouyer }
606 1.1 bouyer
607 1.1 bouyer DELAY(1000);
608 1.1 bouyer
609 1.1 bouyer return (0);
610 1.1 bouyer }
611 1.1 bouyer
612 1.1 bouyer /*
613 1.1 bouyer * 5.2.2 - HID Descriptor Retrieval
614 1.1 bouyer *
615 1.1 bouyer * parse HID Descriptor that has already been read into hid_desc with
616 1.1 bouyer * I2C_HID_CMD_DESCR
617 1.1 bouyer */
618 1.1 bouyer static int
619 1.1 bouyer ihidev_hid_desc_parse(struct ihidev_softc *sc)
620 1.1 bouyer {
621 1.1 bouyer int retries = 3;
622 1.1 bouyer
623 1.1 bouyer /* must be v01.00 */
624 1.1 bouyer if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
625 1.1 bouyer aprint_error_dev(sc->sc_dev,
626 1.1 bouyer "bad HID descriptor bcdVersion (0x%x)\n",
627 1.1 bouyer le16toh(sc->hid_desc.bcdVersion));
628 1.1 bouyer return (1);
629 1.1 bouyer }
630 1.1 bouyer
631 1.1 bouyer /* must be 30 bytes for v1.00 */
632 1.1 bouyer if (le16toh(sc->hid_desc.wHIDDescLength !=
633 1.1 bouyer sizeof(struct i2c_hid_desc))) {
634 1.1 bouyer aprint_error_dev(sc->sc_dev,
635 1.1 bouyer "bad HID descriptor size (%d != %zu)\n",
636 1.1 bouyer le16toh(sc->hid_desc.wHIDDescLength),
637 1.1 bouyer sizeof(struct i2c_hid_desc));
638 1.1 bouyer return (1);
639 1.1 bouyer }
640 1.1 bouyer
641 1.1 bouyer if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
642 1.1 bouyer aprint_error_dev(sc->sc_dev,
643 1.1 bouyer "bad HID report descriptor size (%d)\n",
644 1.1 bouyer le16toh(sc->hid_desc.wReportDescLength));
645 1.1 bouyer return (1);
646 1.1 bouyer }
647 1.1 bouyer
648 1.1 bouyer while (retries-- > 0) {
649 1.1 bouyer if (ihidev_reset(sc, false)) {
650 1.1 bouyer if (retries == 0)
651 1.1 bouyer return(1);
652 1.1 bouyer
653 1.1 bouyer DELAY(1000);
654 1.1 bouyer }
655 1.1 bouyer else
656 1.1 bouyer break;
657 1.1 bouyer }
658 1.1 bouyer
659 1.1 bouyer sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
660 1.22 riastrad sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_SLEEP);
661 1.1 bouyer
662 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
663 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
664 1.1 bouyer return (1);
665 1.1 bouyer }
666 1.1 bouyer
667 1.1 bouyer return (0);
668 1.1 bouyer }
669 1.1 bouyer
670 1.12 thorpej static bool
671 1.21 riastrad ihidev_intr_init(struct ihidev_softc *sc)
672 1.12 thorpej {
673 1.12 thorpej #if NACPICA > 0
674 1.12 thorpej ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
675 1.12 thorpej struct acpi_resources res;
676 1.12 thorpej ACPI_STATUS rv;
677 1.12 thorpej char buf[100];
678 1.12 thorpej
679 1.12 thorpej rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res,
680 1.12 thorpej &acpi_resource_parse_ops_quiet);
681 1.12 thorpej if (ACPI_FAILURE(rv)) {
682 1.12 thorpej aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n");
683 1.12 thorpej return false;
684 1.12 thorpej }
685 1.12 thorpej
686 1.12 thorpej const struct acpi_irq * const irq = acpi_res_irq(&res, 0);
687 1.12 thorpej if (irq == NULL) {
688 1.12 thorpej aprint_error_dev(sc->sc_dev, "no IRQ resource\n");
689 1.12 thorpej acpi_resource_cleanup(&res);
690 1.12 thorpej return false;
691 1.12 thorpej }
692 1.12 thorpej
693 1.12 thorpej sc->sc_intr_type =
694 1.12 thorpej irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL;
695 1.12 thorpej
696 1.12 thorpej acpi_resource_cleanup(&res);
697 1.12 thorpej
698 1.12 thorpej sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY,
699 1.12 thorpej false, ihidev_intr, sc, device_xname(sc->sc_dev));
700 1.12 thorpej if (sc->sc_ih == NULL) {
701 1.12 thorpej aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
702 1.12 thorpej return false;
703 1.12 thorpej }
704 1.12 thorpej aprint_normal_dev(sc->sc_dev, "interrupting at %s\n",
705 1.12 thorpej acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
706 1.12 thorpej
707 1.21 riastrad if (workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev), ihidev_work,
708 1.21 riastrad sc, PRI_NONE, IPL_TTY, WQ_MPSAFE)) {
709 1.12 thorpej aprint_error_dev(sc->sc_dev,
710 1.21 riastrad "can't establish workqueue\n");
711 1.12 thorpej return false;
712 1.12 thorpej }
713 1.21 riastrad sc->sc_work_pending = 0;
714 1.12 thorpej
715 1.12 thorpej return true;
716 1.12 thorpej #else
717 1.12 thorpej aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
718 1.12 thorpej return false;
719 1.12 thorpej #endif
720 1.12 thorpej }
721 1.12 thorpej
722 1.12 thorpej static void
723 1.21 riastrad ihidev_intr_fini(struct ihidev_softc *sc)
724 1.12 thorpej {
725 1.12 thorpej #if NACPICA > 0
726 1.12 thorpej if (sc->sc_ih != NULL) {
727 1.12 thorpej acpi_intr_disestablish(sc->sc_ih);
728 1.12 thorpej }
729 1.21 riastrad if (sc->sc_wq != NULL) {
730 1.21 riastrad workqueue_destroy(sc->sc_wq);
731 1.12 thorpej }
732 1.12 thorpej #endif
733 1.12 thorpej }
734 1.12 thorpej
735 1.30 andvar #if NACPICA > 0
736 1.12 thorpej static void
737 1.12 thorpej ihidev_intr_mask(struct ihidev_softc * const sc)
738 1.12 thorpej {
739 1.12 thorpej
740 1.12 thorpej if (sc->sc_intr_type == IST_LEVEL) {
741 1.12 thorpej acpi_intr_mask(sc->sc_ih);
742 1.12 thorpej }
743 1.12 thorpej }
744 1.12 thorpej
745 1.12 thorpej static void
746 1.12 thorpej ihidev_intr_unmask(struct ihidev_softc * const sc)
747 1.12 thorpej {
748 1.12 thorpej
749 1.12 thorpej if (sc->sc_intr_type == IST_LEVEL) {
750 1.12 thorpej acpi_intr_unmask(sc->sc_ih);
751 1.12 thorpej }
752 1.12 thorpej }
753 1.12 thorpej
754 1.7 jmcneill static int
755 1.1 bouyer ihidev_intr(void *arg)
756 1.1 bouyer {
757 1.12 thorpej struct ihidev_softc * const sc = arg;
758 1.12 thorpej
759 1.12 thorpej /*
760 1.21 riastrad * Schedule our work. If we're using a level-triggered
761 1.21 riastrad * interrupt, we have to mask it off while we wait for service.
762 1.12 thorpej */
763 1.21 riastrad if (atomic_swap_uint(&sc->sc_work_pending, 1) == 0)
764 1.21 riastrad workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
765 1.12 thorpej ihidev_intr_mask(sc);
766 1.12 thorpej
767 1.12 thorpej return 1;
768 1.12 thorpej }
769 1.12 thorpej
770 1.12 thorpej static void
771 1.21 riastrad ihidev_work(struct work *wk, void *arg)
772 1.12 thorpej {
773 1.12 thorpej struct ihidev_softc * const sc = arg;
774 1.1 bouyer struct ihidev *scd;
775 1.1 bouyer u_int psize;
776 1.1 bouyer int res, i;
777 1.1 bouyer u_char *p;
778 1.1 bouyer u_int rep = 0;
779 1.1 bouyer
780 1.21 riastrad atomic_store_relaxed(&sc->sc_work_pending, 0);
781 1.21 riastrad
782 1.21 riastrad mutex_enter(&sc->sc_lock);
783 1.21 riastrad
784 1.12 thorpej iic_acquire_bus(sc->sc_tag, 0);
785 1.1 bouyer res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
786 1.12 thorpej sc->sc_ibuf, sc->sc_isize, 0);
787 1.12 thorpej iic_release_bus(sc->sc_tag, 0);
788 1.1 bouyer if (res != 0)
789 1.12 thorpej goto out;
790 1.1 bouyer
791 1.1 bouyer /*
792 1.1 bouyer * 6.1.1 - First two bytes are the packet length, which must be less
793 1.1 bouyer * than or equal to wMaxInputLength
794 1.1 bouyer */
795 1.1 bouyer psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
796 1.1 bouyer if (!psize || psize > sc->sc_isize) {
797 1.1 bouyer DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
798 1.28 riastrad device_xname(sc->sc_dev), __func__, psize, sc->sc_isize));
799 1.12 thorpej goto out;
800 1.1 bouyer }
801 1.1 bouyer
802 1.1 bouyer /* 3rd byte is the report id */
803 1.1 bouyer p = sc->sc_ibuf + 2;
804 1.1 bouyer psize -= 2;
805 1.1 bouyer if (sc->sc_nrepid != 1)
806 1.1 bouyer rep = *p++, psize--;
807 1.1 bouyer
808 1.1 bouyer if (rep >= sc->sc_nrepid) {
809 1.1 bouyer aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
810 1.1 bouyer __func__, rep);
811 1.12 thorpej goto out;
812 1.1 bouyer }
813 1.1 bouyer
814 1.28 riastrad DPRINTF(("%s: %s: hid input (rep %d):", device_xname(sc->sc_dev),
815 1.12 thorpej __func__, rep));
816 1.1 bouyer for (i = 0; i < sc->sc_isize; i++)
817 1.1 bouyer DPRINTF((" %.2x", sc->sc_ibuf[i]));
818 1.1 bouyer DPRINTF(("\n"));
819 1.1 bouyer
820 1.1 bouyer scd = sc->sc_subdevs[rep];
821 1.1 bouyer if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
822 1.12 thorpej goto out;
823 1.1 bouyer
824 1.1 bouyer scd->sc_intr(scd, p, psize);
825 1.1 bouyer
826 1.12 thorpej out:
827 1.21 riastrad mutex_exit(&sc->sc_lock);
828 1.21 riastrad
829 1.12 thorpej /*
830 1.12 thorpej * If our interrupt is level-triggered, re-enable it now.
831 1.12 thorpej */
832 1.12 thorpej ihidev_intr_unmask(sc);
833 1.1 bouyer }
834 1.30 andvar #endif
835 1.1 bouyer
836 1.1 bouyer static int
837 1.1 bouyer ihidev_maxrepid(void *buf, int len)
838 1.1 bouyer {
839 1.1 bouyer struct hid_data *d;
840 1.1 bouyer struct hid_item h;
841 1.1 bouyer int maxid;
842 1.1 bouyer
843 1.1 bouyer maxid = -1;
844 1.1 bouyer h.report_ID = 0;
845 1.1 bouyer for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
846 1.6 jakllsch if ((int)h.report_ID > maxid)
847 1.1 bouyer maxid = h.report_ID;
848 1.1 bouyer hid_end_parse(d);
849 1.1 bouyer
850 1.1 bouyer return (maxid);
851 1.1 bouyer }
852 1.1 bouyer
853 1.1 bouyer static int
854 1.1 bouyer ihidev_print(void *aux, const char *pnp)
855 1.1 bouyer {
856 1.1 bouyer struct ihidev_attach_arg *iha = aux;
857 1.1 bouyer
858 1.1 bouyer if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
859 1.1 bouyer return (QUIET);
860 1.21 riastrad
861 1.1 bouyer if (pnp)
862 1.1 bouyer aprint_normal("hid at %s", pnp);
863 1.1 bouyer
864 1.1 bouyer if (iha->reportid != 0)
865 1.1 bouyer aprint_normal(" reportid %d", iha->reportid);
866 1.1 bouyer
867 1.1 bouyer return (UNCONF);
868 1.1 bouyer }
869 1.1 bouyer
870 1.1 bouyer static int
871 1.1 bouyer ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
872 1.1 bouyer {
873 1.1 bouyer struct ihidev_attach_arg *iha = aux;
874 1.1 bouyer
875 1.1 bouyer if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
876 1.1 bouyer cf->ihidevcf_reportid != iha->reportid)
877 1.1 bouyer return (0);
878 1.1 bouyer
879 1.1 bouyer return config_match(parent, cf, aux);
880 1.1 bouyer }
881 1.1 bouyer
882 1.1 bouyer int
883 1.1 bouyer ihidev_open(struct ihidev *scd)
884 1.1 bouyer {
885 1.1 bouyer struct ihidev_softc *sc = scd->sc_parent;
886 1.24 riastrad int error;
887 1.1 bouyer
888 1.28 riastrad DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
889 1.1 bouyer __func__, scd->sc_state, sc->sc_refcnt));
890 1.1 bouyer
891 1.24 riastrad mutex_enter(&sc->sc_lock);
892 1.24 riastrad
893 1.25 riastrad if (scd->sc_state & IHIDEV_OPEN || sc->sc_refcnt == INT_MAX) {
894 1.24 riastrad error = EBUSY;
895 1.24 riastrad goto out;
896 1.24 riastrad }
897 1.1 bouyer
898 1.1 bouyer scd->sc_state |= IHIDEV_OPEN;
899 1.1 bouyer
900 1.24 riastrad if (sc->sc_refcnt++ || sc->sc_isize == 0) {
901 1.24 riastrad error = 0;
902 1.24 riastrad goto out;
903 1.24 riastrad }
904 1.1 bouyer
905 1.1 bouyer /* power on */
906 1.1 bouyer ihidev_reset(sc, false);
907 1.24 riastrad error = 0;
908 1.1 bouyer
909 1.24 riastrad out: mutex_exit(&sc->sc_lock);
910 1.24 riastrad return error;
911 1.1 bouyer }
912 1.1 bouyer
913 1.1 bouyer void
914 1.1 bouyer ihidev_close(struct ihidev *scd)
915 1.1 bouyer {
916 1.1 bouyer struct ihidev_softc *sc = scd->sc_parent;
917 1.1 bouyer
918 1.28 riastrad DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
919 1.1 bouyer __func__, scd->sc_state, sc->sc_refcnt));
920 1.1 bouyer
921 1.24 riastrad mutex_enter(&sc->sc_lock);
922 1.24 riastrad
923 1.26 riastrad KASSERTMSG(scd->sc_state & IHIDEV_OPEN,
924 1.26 riastrad "%s: closing %s when not open",
925 1.26 riastrad device_xname(scd->sc_idev),
926 1.26 riastrad device_xname(sc->sc_dev));
927 1.1 bouyer scd->sc_state &= ~IHIDEV_OPEN;
928 1.1 bouyer
929 1.1 bouyer if (--sc->sc_refcnt)
930 1.24 riastrad goto out;
931 1.1 bouyer
932 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
933 1.1 bouyer &I2C_HID_POWER_OFF, false))
934 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to power down\n");
935 1.24 riastrad
936 1.24 riastrad out: mutex_exit(&sc->sc_lock);
937 1.1 bouyer }
938 1.1 bouyer
939 1.1 bouyer void
940 1.1 bouyer ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
941 1.1 bouyer {
942 1.1 bouyer *desc = sc->sc_report;
943 1.1 bouyer *size = sc->sc_reportlen;
944 1.1 bouyer }
945 1.1 bouyer
946 1.1 bouyer /* convert hid_* constants used throughout HID code to i2c HID equivalents */
947 1.1 bouyer int
948 1.1 bouyer ihidev_report_type_conv(int hid_type_id)
949 1.1 bouyer {
950 1.1 bouyer switch (hid_type_id) {
951 1.1 bouyer case hid_input:
952 1.1 bouyer return I2C_HID_REPORT_TYPE_INPUT;
953 1.1 bouyer case hid_output:
954 1.1 bouyer return I2C_HID_REPORT_TYPE_OUTPUT;
955 1.1 bouyer case hid_feature:
956 1.1 bouyer return I2C_HID_REPORT_TYPE_FEATURE;
957 1.1 bouyer default:
958 1.1 bouyer return -1;
959 1.1 bouyer }
960 1.1 bouyer }
961 1.1 bouyer
962 1.1 bouyer int
963 1.1 bouyer ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
964 1.1 bouyer {
965 1.1 bouyer struct ihidev_softc *sc = (struct ihidev_softc *)dev;
966 1.1 bouyer struct i2c_hid_report_request rreq;
967 1.1 bouyer int ctype;
968 1.1 bouyer
969 1.1 bouyer if ((ctype = ihidev_report_type_conv(type)) < 0)
970 1.1 bouyer return (1);
971 1.1 bouyer
972 1.1 bouyer rreq.type = ctype;
973 1.1 bouyer rreq.id = id;
974 1.1 bouyer rreq.data = data;
975 1.1 bouyer rreq.len = len;
976 1.1 bouyer
977 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
978 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed fetching report\n");
979 1.1 bouyer return (1);
980 1.1 bouyer }
981 1.1 bouyer
982 1.1 bouyer return 0;
983 1.1 bouyer }
984 1.1 bouyer
985 1.1 bouyer int
986 1.1 bouyer ihidev_set_report(struct device *dev, int type, int id, void *data,
987 1.1 bouyer int len)
988 1.1 bouyer {
989 1.1 bouyer struct ihidev_softc *sc = (struct ihidev_softc *)dev;
990 1.1 bouyer struct i2c_hid_report_request rreq;
991 1.1 bouyer int ctype;
992 1.1 bouyer
993 1.1 bouyer if ((ctype = ihidev_report_type_conv(type)) < 0)
994 1.1 bouyer return (1);
995 1.1 bouyer
996 1.1 bouyer rreq.type = ctype;
997 1.1 bouyer rreq.id = id;
998 1.1 bouyer rreq.data = data;
999 1.1 bouyer rreq.len = len;
1000 1.1 bouyer
1001 1.1 bouyer if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
1002 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed setting report\n");
1003 1.1 bouyer return (1);
1004 1.1 bouyer }
1005 1.1 bouyer
1006 1.1 bouyer return 0;
1007 1.1 bouyer }
1008 1.17 thorpej
1009 1.17 thorpej static bool
1010 1.17 thorpej ihidev_acpi_get_info(struct ihidev_softc *sc)
1011 1.21 riastrad {
1012 1.30 andvar #if NACPICA > 0
1013 1.17 thorpej ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
1014 1.17 thorpej ACPI_STATUS status;
1015 1.17 thorpej ACPI_INTEGER val;
1016 1.17 thorpej
1017 1.17 thorpej /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
1018 1.17 thorpej uint8_t i2c_hid_guid[] = {
1019 1.17 thorpej 0xF7, 0xF6, 0xDF, 0x3C,
1020 1.17 thorpej 0x67, 0x42,
1021 1.17 thorpej 0x55, 0x45,
1022 1.17 thorpej 0xAD, 0x05,
1023 1.17 thorpej 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
1024 1.17 thorpej };
1025 1.17 thorpej
1026 1.17 thorpej status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val);
1027 1.17 thorpej if (ACPI_FAILURE(status)) {
1028 1.17 thorpej aprint_error_dev(sc->sc_dev,
1029 1.17 thorpej "failed to get HidDescriptorAddress: %s\n",
1030 1.27 skrll AcpiFormatException(status));
1031 1.17 thorpej return false;
1032 1.17 thorpej }
1033 1.17 thorpej
1034 1.17 thorpej sc->sc_hid_desc_addr = (u_int)val;
1035 1.17 thorpej
1036 1.17 thorpej return true;
1037 1.30 andvar #else
1038 1.30 andvar return false;
1039 1.30 andvar #endif
1040 1.17 thorpej }
1041