1 /*-
2 * Copyright (c) 2018-2019 Marc Priggemeyer <marc.priggemeyer@gmail.com>
3 * Copyright (c) 2019-2020 Vladimir Kondratyev <wulf@FreeBSD.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * I2C HID transport backend.
29 */
30
31 #include <sys/cdefs.h>
32 #include "opt_hid.h"
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/callout.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/proc.h>
43 #include <sys/rman.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <sys/taskqueue.h>
47
48 #include <machine/resource.h>
49
50 #include <contrib/dev/acpica/include/acpi.h>
51 #include <contrib/dev/acpica/include/accommon.h>
52 #include <dev/acpica/acpivar.h>
53
54 #include <dev/evdev/input.h>
55
56 #include <dev/hid/hid.h>
57 #include <dev/hid/hidquirk.h>
58
59 #include <dev/iicbus/iic.h>
60 #include <dev/iicbus/iicbus.h>
61 #include <dev/iicbus/iiconf.h>
62
63 #include "hid_if.h"
64
65 #ifdef IICHID_DEBUG
66 static int iichid_debug = 0;
67
68 static SYSCTL_NODE(_hw, OID_AUTO, iichid, CTLFLAG_RW, 0, "I2C HID");
69 SYSCTL_INT(_hw_iichid, OID_AUTO, debug, CTLFLAG_RWTUN,
70 &iichid_debug, 1, "Debug level");
71
72 #define DPRINTFN(sc, n, ...) do { \
73 if (iichid_debug >= (n)) \
74 device_printf((sc)->dev, __VA_ARGS__); \
75 } while (0)
76 #define DPRINTF(sc, ...) DPRINTFN(sc, 1, __VA_ARGS__)
77 #else
78 #define DPRINTFN(...) do {} while (0)
79 #define DPRINTF(...) do {} while (0)
80 #endif
81
82 typedef hid_size_t iichid_size_t;
83 #define IICHID_SIZE_MAX (UINT16_MAX - 2)
84
85 /* 7.2 */
86 enum {
87 I2C_HID_CMD_DESCR = 0x0,
88 I2C_HID_CMD_RESET = 0x1,
89 I2C_HID_CMD_GET_REPORT = 0x2,
90 I2C_HID_CMD_SET_REPORT = 0x3,
91 I2C_HID_CMD_GET_IDLE = 0x4,
92 I2C_HID_CMD_SET_IDLE = 0x5,
93 I2C_HID_CMD_GET_PROTO = 0x6,
94 I2C_HID_CMD_SET_PROTO = 0x7,
95 I2C_HID_CMD_SET_POWER = 0x8,
96 };
97
98 #define I2C_HID_POWER_ON 0x0
99 #define I2C_HID_POWER_OFF 0x1
100
101 #define IICHID_RESET_TIMEOUT 5 /* seconds */
102
103 /*
104 * Since interrupt resource acquisition is not always possible (in case of GPIO
105 * interrupts) iichid now supports a sampling_mode.
106 * Set dev.iichid.<unit>.sampling_rate_slow to a value greater then 0
107 * to activate sampling. A value of 0 is possible but will not reset the
108 * callout and, thereby, disable further report requests. Do not set the
109 * sampling_rate_fast value too high as it may result in periodical lags of
110 * cursor motion.
111 */
112 #define IICHID_SAMPLING_RATE_FAST 80
113 #define IICHID_SAMPLING_RATE_SLOW 10
114 #define IICHID_SAMPLING_HYSTERESIS 16 /* ~ 2x fast / slow */
115
116 /* 5.1.1 - HID Descriptor Format */
117 struct i2c_hid_desc {
118 uint16_t wHIDDescLength;
119 uint16_t bcdVersion;
120 uint16_t wReportDescLength;
121 uint16_t wReportDescRegister;
122 uint16_t wInputRegister;
123 uint16_t wMaxInputLength;
124 uint16_t wOutputRegister;
125 uint16_t wMaxOutputLength;
126 uint16_t wCommandRegister;
127 uint16_t wDataRegister;
128 uint16_t wVendorID;
129 uint16_t wProductID;
130 uint16_t wVersionID;
131 uint32_t reserved;
132 } __packed;
133
134 #define IICHID_REG_NONE -1
135 #define IICHID_REG_ACPI (UINT16_MAX + 1)
136 #define IICHID_REG_ELAN 0x0001
137
138 static const struct iichid_id {
139 char *id;
140 int reg;
141 } iichid_ids[] = {
142 { "ELAN0000", IICHID_REG_ELAN },
143 { "PNP0C50", IICHID_REG_ACPI },
144 { "ACPI0C50", IICHID_REG_ACPI },
145 { NULL, 0 },
146 };
147
148 enum iichid_powerstate_how {
149 IICHID_PS_NULL,
150 IICHID_PS_ON,
151 IICHID_PS_OFF,
152 };
153
154 /*
155 * Locking: no internal locks are used. To serialize access to shared members,
156 * external iicbus lock should be taken. That allows to make locking greatly
157 * simple at the cost of running front interrupt handlers with locked bus.
158 */
159 struct iichid_softc {
160 device_t dev;
161 struct mtx mtx;
162
163 bool probe_done;
164 int probe_result;
165
166 struct hid_device_info hw;
167 uint16_t addr; /* Shifted left by 1 */
168 struct i2c_hid_desc desc;
169
170 hid_intr_t *intr_handler;
171 void *intr_ctx;
172 uint8_t *intr_buf;
173 iichid_size_t intr_bufsize;
174
175 int irq_rid;
176 struct resource *irq_res;
177 void *irq_cookie;
178
179 #ifdef IICHID_SAMPLING
180 int sampling_rate_slow; /* iicbus lock */
181 int sampling_rate_fast;
182 int sampling_hysteresis;
183 int missing_samples; /* iicbus lock */
184 int dup_samples; /* iicbus lock */
185 iichid_size_t dup_size; /* iicbus lock */
186 bool callout_setup; /* iicbus lock */
187 uint8_t *dup_buf;
188 struct taskqueue *taskqueue;
189 struct timeout_task sampling_task; /* iicbus lock */
190 #endif
191
192 struct task suspend_task;
193 bool open; /* iicbus lock */
194 bool suspend; /* iicbus lock */
195 bool power_on; /* iicbus lock */
196 bool reset_acked; /* iichid mtx */
197 };
198
199 static device_probe_t iichid_probe;
200 static device_attach_t iichid_attach;
201 static device_detach_t iichid_detach;
202 static device_resume_t iichid_resume;
203 static device_suspend_t iichid_suspend;
204
205 static void iichid_suspend_task(void *, int);
206
207 #ifdef IICHID_SAMPLING
208 static int iichid_setup_callout(struct iichid_softc *);
209 static int iichid_reset_callout(struct iichid_softc *);
210 static void iichid_teardown_callout(struct iichid_softc *);
211 #endif
212
213 static inline int
acpi_is_iichid(ACPI_HANDLE handle)214 acpi_is_iichid(ACPI_HANDLE handle)
215 {
216 const struct iichid_id *ids;
217 UINT32 sta;
218 int reg;
219
220 for (ids = iichid_ids; ids->id != NULL; ids++) {
221 if (acpi_MatchHid(handle, ids->id)) {
222 reg = ids->reg;
223 break;
224 }
225 }
226 if (ids->id == NULL)
227 return (IICHID_REG_NONE);
228
229 /*
230 * If no _STA method or if it failed, then assume that
231 * the device is present.
232 */
233 if (ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) ||
234 ACPI_DEVICE_PRESENT(sta))
235 return (reg);
236
237 return (IICHID_REG_NONE);
238 }
239
240 static ACPI_STATUS
iichid_get_config_reg(ACPI_HANDLE handle,uint16_t * config_reg)241 iichid_get_config_reg(ACPI_HANDLE handle, uint16_t *config_reg)
242 {
243 ACPI_OBJECT *result;
244 ACPI_BUFFER acpi_buf;
245 ACPI_STATUS status;
246
247 /*
248 * function (_DSM) to be evaluated to retrieve the address of
249 * the configuration register of the HID device.
250 */
251 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
252 static uint8_t dsm_guid[ACPI_UUID_LENGTH] = {
253 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
254 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
255 };
256
257 status = acpi_EvaluateDSMTyped(handle, dsm_guid, 1, 1, NULL, &acpi_buf,
258 ACPI_TYPE_INTEGER);
259 if (ACPI_FAILURE(status)) {
260 printf("%s: error evaluating _DSM\n", __func__);
261 return (status);
262 }
263 result = (ACPI_OBJECT *) acpi_buf.Pointer;
264 *config_reg = result->Integer.Value & 0xFFFF;
265
266 AcpiOsFree(result);
267 return (status);
268 }
269
270 static int
iichid_cmd_read(struct iichid_softc * sc,void * buf,iichid_size_t maxlen,iichid_size_t * actual_len)271 iichid_cmd_read(struct iichid_softc* sc, void *buf, iichid_size_t maxlen,
272 iichid_size_t *actual_len)
273 {
274 int error;
275
276 /*
277 * 6.1.3 - Retrieval of Input Reports
278 * DEVICE returns the length (2 Bytes) and the entire Input Report.
279 */
280
281 memset(buf, 0xaa, 2); // In case nothing gets read
282 struct iic_msg msgs[] = {
283 { sc->addr, IIC_M_RD, maxlen, buf },
284 };
285
286 if (!sc->reset_acked) {
287 msgs[0].len = 2;
288 }
289
290 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
291 if (error != 0)
292 return (error);
293
294 DPRINTFN(sc, 5, "%*D\n", msgs[0].len, msgs[0].buf, " ");
295
296 uint16_t actlen = le16dec(buf);
297
298 if (actlen == 0) {
299 if (!sc->reset_acked) {
300 mtx_lock(&sc->mtx);
301 sc->reset_acked = true;
302 wakeup(&sc->reset_acked);
303 mtx_unlock(&sc->mtx);
304 }
305 }
306
307 if (actlen <= 2 || actlen > maxlen) {
308 actlen = 0;
309 }
310 if (actual_len != NULL) {
311 *actual_len = actlen;
312 }
313
314 return (error);
315 }
316
317 static int
iichid_cmd_write(struct iichid_softc * sc,const void * buf,iichid_size_t len)318 iichid_cmd_write(struct iichid_softc *sc, const void *buf, iichid_size_t len)
319 {
320 /* 6.2.3 - Sending Output Reports. */
321 uint8_t *cmdreg = (uint8_t *)&sc->desc.wOutputRegister;
322 uint16_t replen = 2 + len;
323 uint8_t cmd[4] = { cmdreg[0], cmdreg[1], replen & 0xFF, replen >> 8 };
324 struct iic_msg msgs[] = {
325 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, sizeof(cmd), cmd},
326 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
327 };
328
329 if (le16toh(sc->desc.wMaxOutputLength) == 0)
330 return (IIC_ENOTSUPP);
331 if (len < 2)
332 return (IIC_ENOTSUPP);
333
334 DPRINTF(sc, "HID command I2C_HID_CMD_WRITE (len %d): "
335 "%*D\n", len, len, buf, " ");
336
337 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
338 }
339
340 static int
iichid_cmd_get_hid_desc(struct iichid_softc * sc,uint16_t config_reg,struct i2c_hid_desc * hid_desc)341 iichid_cmd_get_hid_desc(struct iichid_softc *sc, uint16_t config_reg,
342 struct i2c_hid_desc *hid_desc)
343 {
344 /*
345 * 5.2.2 - HID Descriptor Retrieval
346 * register is passed from the controller.
347 */
348 uint16_t cmd = htole16(config_reg);
349 struct iic_msg msgs[] = {
350 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
351 { sc->addr, IIC_M_RD, sizeof(*hid_desc), (uint8_t *)hid_desc },
352 };
353 int error;
354
355 DPRINTF(sc, "HID command I2C_HID_CMD_DESCR at 0x%x\n", config_reg);
356
357 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
358 if (error != 0)
359 return (error);
360
361 DPRINTF(sc, "HID descriptor: %*D\n",
362 (int)sizeof(struct i2c_hid_desc), hid_desc, " ");
363
364 return (0);
365 }
366
367 static int
iichid_set_power(struct iichid_softc * sc,uint8_t param)368 iichid_set_power(struct iichid_softc *sc, uint8_t param)
369 {
370 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
371 uint8_t cmd[] = { cmdreg[0], cmdreg[1], param, I2C_HID_CMD_SET_POWER };
372 struct iic_msg msgs[] = {
373 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
374 };
375
376 DPRINTF(sc, "HID command I2C_HID_CMD_SET_POWER(%d)\n", param);
377
378 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
379 }
380
381 static int
iichid_reset(struct iichid_softc * sc)382 iichid_reset(struct iichid_softc *sc)
383 {
384 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
385 uint8_t cmd[] = { cmdreg[0], cmdreg[1], 0, I2C_HID_CMD_RESET };
386 struct iic_msg msgs[] = {
387 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
388 };
389
390 DPRINTF(sc, "HID command I2C_HID_CMD_RESET\n");
391
392 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
393 }
394
395 static int
iichid_cmd_get_report_desc(struct iichid_softc * sc,void * buf,iichid_size_t len)396 iichid_cmd_get_report_desc(struct iichid_softc* sc, void *buf,
397 iichid_size_t len)
398 {
399 uint16_t cmd = sc->desc.wReportDescRegister;
400 struct iic_msg msgs[] = {
401 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
402 { sc->addr, IIC_M_RD, len, buf },
403 };
404 int error;
405
406 DPRINTF(sc, "HID command I2C_HID_REPORT_DESCR at 0x%x with size %d\n",
407 le16toh(cmd), len);
408
409 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
410 if (error != 0)
411 return (error);
412
413 DPRINTF(sc, "HID report descriptor: %*D\n", len, buf, " ");
414
415 return (0);
416 }
417
418 static int
iichid_cmd_get_report(struct iichid_softc * sc,void * buf,iichid_size_t maxlen,iichid_size_t * actual_len,uint8_t type,uint8_t id)419 iichid_cmd_get_report(struct iichid_softc* sc, void *buf, iichid_size_t maxlen,
420 iichid_size_t *actual_len, uint8_t type, uint8_t id)
421 {
422 /*
423 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
424 * report ID >= 15 is necessary, then the Report ID in the Low Byte
425 * must be set to 1111 and a Third Byte is appended to the protocol.
426 * This Third Byte contains the entire/actual report ID."
427 */
428 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
429 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
430 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/
431 cmdreg[0] ,
432 cmdreg[1] ,
433 (id >= 15 ? 15 | (type << 4): id | (type << 4)),
434 I2C_HID_CMD_GET_REPORT ,
435 (id >= 15 ? id : dtareg[0] ),
436 (id >= 15 ? dtareg[0] : dtareg[1] ),
437 (id >= 15 ? dtareg[1] : 0 ),
438 };
439 int cmdlen = (id >= 15 ? 7 : 6 );
440 uint8_t actbuf[2] = { 0, 0 };
441 uint16_t actlen;
442 int d, error;
443 struct iic_msg msgs[] = {
444 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd },
445 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, 2, actbuf },
446 { sc->addr, IIC_M_RD | IIC_M_NOSTART, maxlen, buf },
447 };
448
449 if (maxlen == 0)
450 return (EINVAL);
451
452 DPRINTF(sc, "HID command I2C_HID_CMD_GET_REPORT %d "
453 "(type %d, len %d)\n", id, type, maxlen);
454
455 /*
456 * 7.2.2.2 - Response will be a 2-byte length value, the report
457 * id (1 byte, if defined in Report Descriptor), and then the report.
458 */
459 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
460 if (error != 0)
461 return (error);
462
463 actlen = actbuf[0] | actbuf[1] << 8;
464 if (actlen != maxlen + 2)
465 DPRINTF(sc, "response size %d != expected length %d\n",
466 actlen, maxlen + 2);
467
468 if (actlen <= 2 || actlen == 0xFFFF)
469 return (ENOMSG);
470
471 d = id != 0 ? *(uint8_t *)buf : 0;
472 if (d != id) {
473 DPRINTF(sc, "response report id %d != %d\n", d, id);
474 return (EBADMSG);
475 }
476
477 actlen -= 2;
478 if (actlen > maxlen)
479 actlen = maxlen;
480 if (actual_len != NULL)
481 *actual_len = actlen;
482
483 DPRINTF(sc, "response: %*D %*D\n", 2, actbuf, " ", actlen, buf, " ");
484
485 return (0);
486 }
487
488 static int
iichid_cmd_set_report(struct iichid_softc * sc,const void * buf,iichid_size_t len,uint8_t type,uint8_t id)489 iichid_cmd_set_report(struct iichid_softc* sc, const void *buf,
490 iichid_size_t len, uint8_t type, uint8_t id)
491 {
492 /*
493 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
494 * report ID >= 15 is necessary, then the Report ID in the Low Byte
495 * must be set to 1111 and a Third Byte is appended to the protocol.
496 * This Third Byte contains the entire/actual report ID."
497 */
498 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
499 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
500 uint16_t replen = 2 + len;
501 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/
502 cmdreg[0] ,
503 cmdreg[1] ,
504 (id >= 15 ? 15 | (type << 4): id | (type << 4)),
505 I2C_HID_CMD_SET_REPORT ,
506 (id >= 15 ? id : dtareg[0] ),
507 (id >= 15 ? dtareg[0] : dtareg[1] ),
508 (id >= 15 ? dtareg[1] : replen & 0xff ),
509 (id >= 15 ? replen & 0xff : replen >> 8 ),
510 (id >= 15 ? replen >> 8 : 0 ),
511 };
512 int cmdlen = (id >= 15 ? 9 : 8 );
513 struct iic_msg msgs[] = {
514 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd},
515 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
516 };
517
518 DPRINTF(sc, "HID command I2C_HID_CMD_SET_REPORT %d (type %d, len %d): "
519 "%*D\n", id, type, len, len, buf, " ");
520
521 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
522 }
523
524 #ifdef IICHID_SAMPLING
525 static void
iichid_sampling_task(void * context,int pending)526 iichid_sampling_task(void *context, int pending)
527 {
528 struct iichid_softc *sc;
529 device_t parent;
530 iichid_size_t actual;
531 bool bus_requested;
532 int error, rate;
533
534 sc = context;
535 parent = device_get_parent(sc->dev);
536
537 bus_requested = false;
538 if (iicbus_request_bus(parent, sc->dev, IIC_WAIT) != 0)
539 goto rearm;
540 bus_requested = true;
541
542 if (!sc->power_on)
543 goto out;
544
545 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
546 if (error == 0) {
547 if (actual > 0) {
548 sc->intr_handler(sc->intr_ctx, sc->intr_buf + 2, actual - 2);
549 sc->missing_samples = 0;
550 if (sc->dup_size != actual ||
551 memcmp(sc->dup_buf, sc->intr_buf, actual) != 0) {
552 sc->dup_size = actual;
553 memcpy(sc->dup_buf, sc->intr_buf, actual);
554 sc->dup_samples = 0;
555 } else
556 ++sc->dup_samples;
557 } else {
558 if (++sc->missing_samples == 1)
559 sc->intr_handler(sc->intr_ctx, sc->intr_buf + 2, 0);
560 sc->dup_samples = 0;
561 }
562 } else
563 DPRINTF(sc, "read error occurred: %d\n", error);
564
565 rearm:
566 if (sc->callout_setup && sc->sampling_rate_slow > 0) {
567 if (sc->missing_samples >= sc->sampling_hysteresis ||
568 sc->dup_samples >= sc->sampling_hysteresis)
569 rate = sc->sampling_rate_slow;
570 else
571 rate = sc->sampling_rate_fast;
572 taskqueue_enqueue_timeout_sbt(sc->taskqueue, &sc->sampling_task,
573 SBT_1S / MAX(rate, 1), 0, C_PREL(2));
574 }
575 out:
576 if (bus_requested)
577 iicbus_release_bus(parent, sc->dev);
578 }
579 #endif /* IICHID_SAMPLING */
580
581 static void
iichid_intr(void * context)582 iichid_intr(void *context)
583 {
584 struct iichid_softc *sc;
585 device_t parent;
586 iichid_size_t actual;
587 int error;
588
589 sc = context;
590 parent = device_get_parent(sc->dev);
591
592 /*
593 * Designware(IG4) driver-specific hack.
594 * Requesting of an I2C bus with IIC_DONTWAIT parameter enables polled
595 * mode in the driver, making possible iicbus_transfer execution from
596 * interrupt handlers and callouts.
597 */
598 if (iicbus_request_bus(parent, sc->dev, IIC_DONTWAIT) != 0)
599 return;
600
601 /*
602 * Reading of input reports of I2C devices residing in SLEEP state is
603 * not allowed and often returns a garbage. If a HOST needs to
604 * communicate with the DEVICE it MUST issue a SET POWER command
605 * (to ON) before any other command. As some hardware requires reads to
606 * acknowledge interrupts we fetch only length header and discard it.
607 */
608 THREAD_SLEEPING_OK();
609 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
610 THREAD_NO_SLEEPING();
611 if (error == 0) {
612 if (sc->power_on && sc->open) {
613 if (actual != 0)
614 sc->intr_handler(sc->intr_ctx, sc->intr_buf + 2,
615 actual - 2);
616 else
617 DPRINTF(sc, "no data received\n");
618 }
619 } else
620 DPRINTF(sc, "read error occurred: %d\n", error);
621
622 iicbus_release_bus(parent, sc->dev);
623 }
624
625 static int
iichid_set_power_state(struct iichid_softc * sc,enum iichid_powerstate_how how_open,enum iichid_powerstate_how how_suspend)626 iichid_set_power_state(struct iichid_softc *sc,
627 enum iichid_powerstate_how how_open,
628 enum iichid_powerstate_how how_suspend)
629 {
630 device_t parent;
631 int error;
632 int how_request;
633 bool power_on;
634
635 /*
636 * Request iicbus early as sc->suspend and sc->power_on
637 * are protected by iicbus internal lock.
638 */
639 parent = device_get_parent(sc->dev);
640 /* Allow to interrupt open()/close() handlers by SIGINT */
641 how_request = how_open == IICHID_PS_NULL ? IIC_WAIT : IIC_INTRWAIT;
642 error = iicbus_request_bus(parent, sc->dev, how_request);
643 if (error != 0)
644 return (error);
645
646 switch (how_open) {
647 case IICHID_PS_ON:
648 sc->open = true;
649 break;
650 case IICHID_PS_OFF:
651 sc->open = false;
652 break;
653 case IICHID_PS_NULL:
654 default:
655 break;
656 }
657
658 switch (how_suspend) {
659 case IICHID_PS_ON:
660 sc->suspend = false;
661 break;
662 case IICHID_PS_OFF:
663 sc->suspend = true;
664 break;
665 case IICHID_PS_NULL:
666 default:
667 break;
668 }
669
670 power_on = sc->open & !sc->suspend;
671
672 if (power_on != sc->power_on) {
673 error = iichid_set_power(sc,
674 power_on ? I2C_HID_POWER_ON : I2C_HID_POWER_OFF);
675
676 sc->power_on = power_on;
677 #ifdef IICHID_SAMPLING
678 if (sc->sampling_rate_slow >= 0 && sc->intr_handler != NULL) {
679 if (power_on) {
680 iichid_setup_callout(sc);
681 iichid_reset_callout(sc);
682 } else
683 iichid_teardown_callout(sc);
684 }
685 #endif
686 }
687
688 iicbus_release_bus(parent, sc->dev);
689
690 return (error);
691 }
692
693 static int
iichid_setup_interrupt(struct iichid_softc * sc)694 iichid_setup_interrupt(struct iichid_softc *sc)
695 {
696 sc->irq_cookie = 0;
697
698 int error = bus_setup_intr(sc->dev, sc->irq_res,
699 INTR_TYPE_TTY|INTR_MPSAFE, NULL, iichid_intr, sc, &sc->irq_cookie);
700 if (error != 0)
701 DPRINTF(sc, "Could not setup interrupt handler\n");
702 else
703 DPRINTF(sc, "successfully setup interrupt\n");
704
705 return (error);
706 }
707
708 static void
iichid_teardown_interrupt(struct iichid_softc * sc)709 iichid_teardown_interrupt(struct iichid_softc *sc)
710 {
711 if (sc->irq_cookie)
712 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_cookie);
713
714 sc->irq_cookie = 0;
715 }
716
717 #ifdef IICHID_SAMPLING
718 static int
iichid_setup_callout(struct iichid_softc * sc)719 iichid_setup_callout(struct iichid_softc *sc)
720 {
721
722 if (sc->sampling_rate_slow < 0) {
723 DPRINTF(sc, "sampling_rate is below 0, can't setup callout\n");
724 return (EINVAL);
725 }
726
727 sc->callout_setup = true;
728 DPRINTF(sc, "successfully setup callout\n");
729 return (0);
730 }
731
732 static int
iichid_reset_callout(struct iichid_softc * sc)733 iichid_reset_callout(struct iichid_softc *sc)
734 {
735
736 if (sc->sampling_rate_slow <= 0) {
737 DPRINTF(sc, "sampling_rate is below or equal to 0, "
738 "can't reset callout\n");
739 return (EINVAL);
740 }
741
742 if (!sc->callout_setup)
743 return (EINVAL);
744
745 /* Start with slow sampling. */
746 sc->missing_samples = sc->sampling_hysteresis;
747 sc->dup_samples = 0;
748 sc->dup_size = 0;
749 taskqueue_enqueue_timeout(sc->taskqueue, &sc->sampling_task, 0);
750
751 return (0);
752 }
753
754 static void
iichid_teardown_callout(struct iichid_softc * sc)755 iichid_teardown_callout(struct iichid_softc *sc)
756 {
757
758 sc->callout_setup = false;
759 taskqueue_cancel_timeout(sc->taskqueue, &sc->sampling_task, NULL);
760 DPRINTF(sc, "tore callout down\n");
761 }
762
763 static int
iichid_sysctl_sampling_rate_handler(SYSCTL_HANDLER_ARGS)764 iichid_sysctl_sampling_rate_handler(SYSCTL_HANDLER_ARGS)
765 {
766 struct iichid_softc *sc;
767 device_t parent;
768 int error, oldval, value;
769
770 sc = arg1;
771
772 value = sc->sampling_rate_slow;
773 error = sysctl_handle_int(oidp, &value, 0, req);
774
775 if (error != 0 || req->newptr == NULL ||
776 value == sc->sampling_rate_slow)
777 return (error);
778
779 /* Can't switch to interrupt mode if it is not supported. */
780 if (sc->irq_res == NULL && value < 0)
781 return (EINVAL);
782
783 parent = device_get_parent(sc->dev);
784 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
785 if (error != 0)
786 return (iic2errno(error));
787
788 oldval = sc->sampling_rate_slow;
789 sc->sampling_rate_slow = value;
790
791 if (oldval < 0 && value >= 0) {
792 iichid_teardown_interrupt(sc);
793 if (sc->power_on)
794 iichid_setup_callout(sc);
795 } else if (oldval >= 0 && value < 0) {
796 if (sc->power_on)
797 iichid_teardown_callout(sc);
798 iichid_setup_interrupt(sc);
799 }
800
801 if (sc->power_on && value > 0)
802 iichid_reset_callout(sc);
803
804 iicbus_release_bus(parent, sc->dev);
805
806 DPRINTF(sc, "new sampling_rate value: %d\n", value);
807
808 return (0);
809 }
810 #endif /* IICHID_SAMPLING */
811
812 static void
iichid_intr_setup(device_t dev,device_t child __unused,hid_intr_t intr,void * context,struct hid_rdesc_info * rdesc)813 iichid_intr_setup(device_t dev, device_t child __unused, hid_intr_t intr,
814 void *context, struct hid_rdesc_info *rdesc)
815 {
816 struct iichid_softc *sc;
817 device_t parent;
818
819 if (intr == NULL)
820 return;
821
822 sc = device_get_softc(dev);
823 /*
824 * Start with wMaxInputLength to follow HID-over-I2C specs. Than if
825 * semi-HID device like ietp(4) requested changing of input buffer
826 * size with report descriptor overloading, find the longest input
827 * report in the descriptor, and add two for the length field.
828 */
829 rdesc->rdsize = rdesc->rdsize == 0 ?
830 le16toh(sc->desc.wMaxInputLength) - 2 : rdesc->isize;
831 /* Write and get/set_report sizes are limited by I2C-HID protocol. */
832 rdesc->grsize = rdesc->srsize = IICHID_SIZE_MAX;
833 rdesc->wrsize = IICHID_SIZE_MAX;
834
835 parent = device_get_parent(sc->dev);
836 iicbus_request_bus(parent, sc->dev, IIC_WAIT);
837
838 sc->intr_handler = intr;
839 sc->intr_ctx = context;
840 sc->intr_bufsize = rdesc->rdsize + 2;
841 sc->intr_buf = realloc(sc->intr_buf, sc->intr_bufsize,
842 M_DEVBUF, M_WAITOK | M_ZERO);
843 #ifdef IICHID_SAMPLING
844 sc->dup_buf = realloc(sc->dup_buf, sc->intr_bufsize,
845 M_DEVBUF, M_WAITOK | M_ZERO);
846 taskqueue_start_threads(&sc->taskqueue, 1, PI_TTY,
847 "%s taskq", device_get_nameunit(sc->dev));
848 #endif
849 iicbus_release_bus(parent, sc->dev);
850 }
851
852 static void
iichid_intr_unsetup(device_t dev,device_t child __unused)853 iichid_intr_unsetup(device_t dev, device_t child __unused)
854 {
855 #ifdef IICHID_SAMPLING
856 struct iichid_softc *sc;
857
858 sc = device_get_softc(dev);
859 taskqueue_drain_all(sc->taskqueue);
860 #endif
861 }
862
863 static int
iichid_intr_start(device_t dev,device_t child __unused)864 iichid_intr_start(device_t dev, device_t child __unused)
865 {
866 struct iichid_softc *sc;
867
868 sc = device_get_softc(dev);
869 DPRINTF(sc, "iichid device open\n");
870 if (!sc->open)
871 iichid_set_power_state(sc, IICHID_PS_ON, IICHID_PS_NULL);
872
873 return (0);
874 }
875
876 static int
iichid_intr_stop(device_t dev,device_t child __unused)877 iichid_intr_stop(device_t dev, device_t child __unused)
878 {
879 struct iichid_softc *sc;
880
881 sc = device_get_softc(dev);
882 DPRINTF(sc, "iichid device close\n");
883 /*
884 * 8.2 - The HOST determines that there are no active applications
885 * that are currently using the specific HID DEVICE. The HOST
886 * is recommended to issue a HIPO command to the DEVICE to force
887 * the DEVICE in to a lower power state.
888 */
889 iichid_set_power_state(sc, IICHID_PS_OFF, IICHID_PS_NULL);
890
891 return (0);
892 }
893
894 static void
iichid_intr_poll(device_t dev,device_t child __unused)895 iichid_intr_poll(device_t dev, device_t child __unused)
896 {
897 struct iichid_softc *sc;
898 iichid_size_t actual;
899 int error;
900
901 sc = device_get_softc(dev);
902 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
903 if (error == 0 && actual != 0)
904 sc->intr_handler(sc->intr_ctx, sc->intr_buf + 2, actual);
905 }
906
907 /*
908 * HID interface
909 */
910 static int
iichid_get_rdesc(device_t dev,device_t child __unused,void * buf,hid_size_t len)911 iichid_get_rdesc(device_t dev, device_t child __unused, void *buf,
912 hid_size_t len)
913 {
914 struct iichid_softc *sc;
915 int error;
916
917 sc = device_get_softc(dev);
918 error = iichid_cmd_get_report_desc(sc, buf, len);
919 if (error)
920 DPRINTF(sc, "failed to fetch report descriptor: %d\n", error);
921
922 return (iic2errno(error));
923 }
924
925 static int
iichid_read(device_t dev,device_t child __unused,void * buf,hid_size_t maxlen,hid_size_t * actlen)926 iichid_read(device_t dev, device_t child __unused, void *buf,
927 hid_size_t maxlen, hid_size_t *actlen)
928 {
929 struct iichid_softc *sc;
930 device_t parent;
931 uint8_t *tmpbuf;
932 int error;
933
934 if (maxlen > IICHID_SIZE_MAX)
935 return (EMSGSIZE);
936 sc = device_get_softc(dev);
937 parent = device_get_parent(sc->dev);
938 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
939 if (error == 0) {
940 tmpbuf = malloc(maxlen + 2, M_DEVBUF, M_WAITOK | M_ZERO);
941 error = iichid_cmd_read(sc, tmpbuf, maxlen + 2, actlen);
942 iicbus_release_bus(parent, sc->dev);
943 if (*actlen > 0)
944 memcpy(buf, tmpbuf + 2, *actlen);
945 free(tmpbuf, M_DEVBUF);
946 }
947 return (iic2errno(error));
948 }
949
950 static int
iichid_write(device_t dev,device_t child __unused,const void * buf,hid_size_t len)951 iichid_write(device_t dev, device_t child __unused, const void *buf,
952 hid_size_t len)
953 {
954 struct iichid_softc *sc;
955
956 if (len > IICHID_SIZE_MAX)
957 return (EMSGSIZE);
958 sc = device_get_softc(dev);
959 return (iic2errno(iichid_cmd_write(sc, buf, len)));
960 }
961
962 static int
iichid_get_report(device_t dev,device_t child __unused,void * buf,hid_size_t maxlen,hid_size_t * actlen,uint8_t type,uint8_t id)963 iichid_get_report(device_t dev, device_t child __unused, void *buf,
964 hid_size_t maxlen, hid_size_t *actlen, uint8_t type, uint8_t id)
965 {
966 struct iichid_softc *sc;
967
968 if (maxlen > IICHID_SIZE_MAX)
969 return (EMSGSIZE);
970 sc = device_get_softc(dev);
971 return (iic2errno(
972 iichid_cmd_get_report(sc, buf, maxlen, actlen, type, id)));
973 }
974
975 static int
iichid_set_report(device_t dev,device_t child __unused,const void * buf,hid_size_t len,uint8_t type,uint8_t id)976 iichid_set_report(device_t dev, device_t child __unused, const void *buf,
977 hid_size_t len, uint8_t type, uint8_t id)
978 {
979 struct iichid_softc *sc;
980
981 if (len > IICHID_SIZE_MAX)
982 return (EMSGSIZE);
983 sc = device_get_softc(dev);
984 return (iic2errno(iichid_cmd_set_report(sc, buf, len, type, id)));
985 }
986
987 static int
iichid_set_idle(device_t dev,device_t child __unused,uint16_t duration,uint8_t id)988 iichid_set_idle(device_t dev, device_t child __unused,
989 uint16_t duration, uint8_t id)
990 {
991 return (ENOTSUP);
992 }
993
994 static int
iichid_set_protocol(device_t dev,device_t child __unused,uint16_t protocol)995 iichid_set_protocol(device_t dev, device_t child __unused, uint16_t protocol)
996 {
997 return (ENOTSUP);
998 }
999
1000 static int
iichid_ioctl(device_t dev,device_t child __unused,unsigned long cmd,uintptr_t data)1001 iichid_ioctl(device_t dev, device_t child __unused, unsigned long cmd,
1002 uintptr_t data)
1003 {
1004 int error;
1005
1006 switch (cmd) {
1007 case I2CRDWR:
1008 error = iic2errno(iicbus_transfer(dev,
1009 ((struct iic_rdwr_data *)data)->msgs,
1010 ((struct iic_rdwr_data *)data)->nmsgs));
1011 break;
1012 default:
1013 error = EINVAL;
1014 }
1015
1016 return (error);
1017 }
1018
1019 static int
iichid_fill_device_info(struct i2c_hid_desc * desc,ACPI_HANDLE handle,struct hid_device_info * hw)1020 iichid_fill_device_info(struct i2c_hid_desc *desc, ACPI_HANDLE handle,
1021 struct hid_device_info *hw)
1022 {
1023 ACPI_DEVICE_INFO *device_info;
1024
1025 hw->idBus = BUS_I2C;
1026 hw->idVendor = le16toh(desc->wVendorID);
1027 hw->idProduct = le16toh(desc->wProductID);
1028 hw->idVersion = le16toh(desc->wVersionID);
1029
1030 /* get ACPI HID. It is a base part of the device name. */
1031 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &device_info)))
1032 return (ENXIO);
1033
1034 if (device_info->Valid & ACPI_VALID_HID)
1035 strlcpy(hw->idPnP, device_info->HardwareId.String,
1036 HID_PNP_ID_SIZE);
1037 snprintf(hw->name, sizeof(hw->name), "%s:%02lX %04X:%04X",
1038 (device_info->Valid & ACPI_VALID_HID) ?
1039 device_info->HardwareId.String : "Unknown",
1040 (device_info->Valid & ACPI_VALID_UID) ?
1041 strtoul(device_info->UniqueId.String, NULL, 10) : 0UL,
1042 le16toh(desc->wVendorID), le16toh(desc->wProductID));
1043
1044 AcpiOsFree(device_info);
1045
1046 strlcpy(hw->serial, "", sizeof(hw->serial));
1047 hw->rdescsize = le16toh(desc->wReportDescLength);
1048 if (desc->wOutputRegister == 0 || desc->wMaxOutputLength == 0)
1049 hid_add_dynamic_quirk(hw, HQ_NOWRITE);
1050
1051 return (0);
1052 }
1053
1054 static int
iichid_probe(device_t dev)1055 iichid_probe(device_t dev)
1056 {
1057 struct iichid_softc *sc;
1058 ACPI_HANDLE handle;
1059 uint16_t config_reg;
1060 int error, reg;
1061
1062 sc = device_get_softc(dev);
1063 sc->dev = dev;
1064 if (sc->probe_done)
1065 goto done;
1066
1067 sc->probe_done = true;
1068 sc->probe_result = ENXIO;
1069
1070 if (acpi_disabled("iichid"))
1071 return (ENXIO);
1072
1073 sc->addr = iicbus_get_addr(dev) << 1;
1074 if (sc->addr == 0)
1075 return (ENXIO);
1076
1077 handle = acpi_get_handle(dev);
1078 if (handle == NULL)
1079 return (ENXIO);
1080
1081 reg = acpi_is_iichid(handle);
1082 if (reg == IICHID_REG_NONE)
1083 return (ENXIO);
1084
1085 if (reg == IICHID_REG_ACPI) {
1086 if (ACPI_FAILURE(iichid_get_config_reg(handle, &config_reg)))
1087 return (ENXIO);
1088 } else
1089 config_reg = (uint16_t)reg;
1090
1091 DPRINTF(sc, " IICbus addr : 0x%02X\n", sc->addr >> 1);
1092 DPRINTF(sc, " HID descriptor reg: 0x%02X\n", config_reg);
1093
1094 error = iichid_cmd_get_hid_desc(sc, config_reg, &sc->desc);
1095 if (error) {
1096 DPRINTF(sc, "could not retrieve HID descriptor from the "
1097 "device: %d\n", error);
1098 return (ENXIO);
1099 }
1100
1101 if (le16toh(sc->desc.wHIDDescLength) != 30 ||
1102 le16toh(sc->desc.bcdVersion) != 0x100 ||
1103 le16toh(sc->desc.wMaxInputLength) < 2) {
1104 DPRINTF(sc, "HID descriptor is broken\n");
1105 return (ENXIO);
1106 }
1107
1108 /* Setup hid_device_info so we can figure out quirks for the device. */
1109 if (iichid_fill_device_info(&sc->desc, handle, &sc->hw) != 0) {
1110 DPRINTF(sc, "error evaluating AcpiGetObjectInfo\n");
1111 return (ENXIO);
1112 }
1113
1114 if (hid_test_quirk(&sc->hw, HQ_HID_IGNORE))
1115 return (ENXIO);
1116
1117 sc->probe_result = BUS_PROBE_DEFAULT;
1118 done:
1119 if (sc->probe_result <= BUS_PROBE_SPECIFIC)
1120 device_set_descf(dev, "%s I2C HID device", sc->hw.name);
1121 return (sc->probe_result);
1122 }
1123
1124 static int
iichid_attach(device_t dev)1125 iichid_attach(device_t dev)
1126 {
1127 struct iichid_softc *sc;
1128 device_t child;
1129 int error;
1130
1131 sc = device_get_softc(dev);
1132 error = iichid_set_power(sc, I2C_HID_POWER_ON);
1133 if (error) {
1134 device_printf(dev, "failed to power on: %d\n", error);
1135 return (ENXIO);
1136 }
1137 sc->power_on = true;
1138
1139 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1140 sc->intr_bufsize = le16toh(sc->desc.wMaxInputLength) - 2;
1141 sc->intr_buf = malloc(sc->intr_bufsize, M_DEVBUF, M_WAITOK | M_ZERO);
1142 TASK_INIT(&sc->suspend_task, 0, iichid_suspend_task, sc);
1143 #ifdef IICHID_SAMPLING
1144 sc->taskqueue = taskqueue_create_fast("iichid_tq", M_WAITOK | M_ZERO,
1145 taskqueue_thread_enqueue, &sc->taskqueue);
1146 TIMEOUT_TASK_INIT(sc->taskqueue, &sc->sampling_task, 0,
1147 iichid_sampling_task, sc);
1148
1149 sc->sampling_rate_slow = -1;
1150 sc->sampling_rate_fast = IICHID_SAMPLING_RATE_FAST;
1151 sc->sampling_hysteresis = IICHID_SAMPLING_HYSTERESIS;
1152 sc->dup_buf = malloc(sc->intr_bufsize, M_DEVBUF, M_WAITOK | M_ZERO);
1153 #endif
1154
1155 sc->irq_rid = 0;
1156 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
1157 &sc->irq_rid, RF_ACTIVE);
1158
1159 if (sc->irq_res != NULL) {
1160 DPRINTF(sc, "allocated irq at %p and rid %d\n",
1161 sc->irq_res, sc->irq_rid);
1162 error = iichid_setup_interrupt(sc);
1163 }
1164
1165 if (sc->irq_res == NULL || error != 0) {
1166 #ifdef IICHID_SAMPLING
1167 device_printf(sc->dev,
1168 "Using sampling mode\n");
1169 sc->sampling_rate_slow = IICHID_SAMPLING_RATE_SLOW;
1170 #else
1171 device_printf(sc->dev, "Interrupt setup failed\n");
1172 if (sc->irq_res != NULL)
1173 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1174 sc->irq_res);
1175 iichid_detach(dev);
1176 error = ENXIO;
1177 goto done;
1178 #endif
1179 }
1180
1181 #ifdef IICHID_SAMPLING
1182 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1183 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1184 OID_AUTO, "sampling_rate_slow", CTLTYPE_INT | CTLFLAG_RWTUN,
1185 sc, 0, iichid_sysctl_sampling_rate_handler, "I",
1186 "idle sampling rate in num/second");
1187 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1188 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1189 OID_AUTO, "sampling_rate_fast", CTLFLAG_RWTUN,
1190 &sc->sampling_rate_fast, 0,
1191 "active sampling rate in num/second");
1192 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1193 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1194 OID_AUTO, "sampling_hysteresis", CTLFLAG_RWTUN,
1195 &sc->sampling_hysteresis, 0,
1196 "number of missing samples before enabling of slow mode");
1197 hid_add_dynamic_quirk(&sc->hw, HQ_IICHID_SAMPLING);
1198 #endif /* IICHID_SAMPLING */
1199
1200 /*
1201 * Windows driver sleeps for 1ms between the SET_POWER and RESET
1202 * commands. So we too as some devices may depend on this.
1203 */
1204 pause("iichid", (hz + 999) / 1000);
1205
1206 error = iichid_reset(sc);
1207 if (error) {
1208 device_printf(dev, "failed to reset hardware: %d\n", error);
1209 iichid_detach(dev);
1210 error = ENXIO;
1211 goto done;
1212 }
1213
1214 /* Wait for RESET response */
1215 #ifdef IICHID_SAMPLING
1216 if (sc->sampling_rate_slow >= 0) {
1217 pause("iichid", (hz + 999) / 1000);
1218 (void)iichid_cmd_read(sc, sc->intr_buf, 0, NULL);
1219 } else
1220 #endif /* IICHID_SAMPLING */
1221 {
1222 mtx_lock(&sc->mtx);
1223 if (!sc->reset_acked && !cold) {
1224 error = mtx_sleep(&sc->reset_acked, &sc->mtx, 0,
1225 "iichid_reset", hz * IICHID_RESET_TIMEOUT);
1226 if (error != 0)
1227 device_printf(sc->dev,
1228 "Reset timeout expired\n");
1229 }
1230 mtx_unlock(&sc->mtx);
1231 }
1232
1233 child = device_add_child(dev, "hidbus", DEVICE_UNIT_ANY);
1234 if (child == NULL) {
1235 device_printf(sc->dev, "Could not add I2C device\n");
1236 iichid_detach(dev);
1237 error = ENOMEM;
1238 goto done;
1239 }
1240
1241 device_set_ivars(child, &sc->hw);
1242 bus_attach_children(dev);
1243 error = 0;
1244 done:
1245 iicbus_request_bus(device_get_parent(dev), dev, IIC_WAIT);
1246 if (!sc->open) {
1247 (void)iichid_set_power(sc, I2C_HID_POWER_OFF);
1248 sc->power_on = false;
1249 }
1250 iicbus_release_bus(device_get_parent(dev), dev);
1251 return (error);
1252 }
1253
1254 static int
iichid_detach(device_t dev)1255 iichid_detach(device_t dev)
1256 {
1257 struct iichid_softc *sc;
1258 int error;
1259
1260 sc = device_get_softc(dev);
1261 error = bus_generic_detach(dev);
1262 if (error)
1263 return (error);
1264 iichid_teardown_interrupt(sc);
1265 if (sc->irq_res != NULL)
1266 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1267 sc->irq_res);
1268 #ifdef IICHID_SAMPLING
1269 if (sc->taskqueue != NULL)
1270 taskqueue_free(sc->taskqueue);
1271 sc->taskqueue = NULL;
1272 free(sc->dup_buf, M_DEVBUF);
1273 #endif
1274 free(sc->intr_buf, M_DEVBUF);
1275 mtx_destroy(&sc->mtx);
1276 return (0);
1277 }
1278
1279 static void
iichid_suspend_task(void * context,int pending)1280 iichid_suspend_task(void *context, int pending)
1281 {
1282 struct iichid_softc *sc = context;
1283
1284 iichid_teardown_interrupt(sc);
1285 }
1286
1287 static int
iichid_suspend(device_t dev)1288 iichid_suspend(device_t dev)
1289 {
1290 struct iichid_softc *sc;
1291 int error;
1292
1293 sc = device_get_softc(dev);
1294 (void)bus_generic_suspend(dev);
1295 /*
1296 * 8.2 - The HOST is going into a deep power optimized state and wishes
1297 * to put all the devices into a low power state also. The HOST
1298 * is recommended to issue a HIPO command to the DEVICE to force
1299 * the DEVICE in to a lower power state.
1300 */
1301 DPRINTF(sc, "Suspend called, setting device to power_state 1\n");
1302 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_OFF);
1303 if (error != 0)
1304 DPRINTF(sc, "Could not set power_state, error: %d\n", error);
1305 else
1306 DPRINTF(sc, "Successfully set power_state\n");
1307
1308 #ifdef IICHID_SAMPLING
1309 if (sc->sampling_rate_slow < 0)
1310 #endif
1311 {
1312 /*
1313 * bus_teardown_intr can not be executed right here as it wants
1314 * to run on certain CPU to interacts with LAPIC while suspend
1315 * thread is bound to CPU0. So run it from taskqueue context.
1316 */
1317 #ifdef IICHID_SAMPLING
1318 #define suspend_thread sc->taskqueue
1319 #else
1320 #define suspend_thread taskqueue_thread
1321 #endif
1322 taskqueue_enqueue(suspend_thread, &sc->suspend_task);
1323 taskqueue_drain(suspend_thread, &sc->suspend_task);
1324 }
1325
1326 return (0);
1327 }
1328
1329 static int
iichid_resume(device_t dev)1330 iichid_resume(device_t dev)
1331 {
1332 struct iichid_softc *sc;
1333 int error;
1334
1335 sc = device_get_softc(dev);
1336 #ifdef IICHID_SAMPLING
1337 if (sc->sampling_rate_slow < 0)
1338 #endif
1339 iichid_setup_interrupt(sc);
1340
1341 DPRINTF(sc, "Resume called, setting device to power_state 0\n");
1342 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_ON);
1343 if (error != 0)
1344 DPRINTF(sc, "Could not set power_state, error: %d\n", error);
1345 else
1346 DPRINTF(sc, "Successfully set power_state\n");
1347 (void)bus_generic_resume(dev);
1348
1349 return (0);
1350 }
1351
1352 static device_method_t iichid_methods[] = {
1353 DEVMETHOD(device_probe, iichid_probe),
1354 DEVMETHOD(device_attach, iichid_attach),
1355 DEVMETHOD(device_detach, iichid_detach),
1356 DEVMETHOD(device_suspend, iichid_suspend),
1357 DEVMETHOD(device_resume, iichid_resume),
1358
1359 DEVMETHOD(hid_intr_setup, iichid_intr_setup),
1360 DEVMETHOD(hid_intr_unsetup, iichid_intr_unsetup),
1361 DEVMETHOD(hid_intr_start, iichid_intr_start),
1362 DEVMETHOD(hid_intr_stop, iichid_intr_stop),
1363 DEVMETHOD(hid_intr_poll, iichid_intr_poll),
1364
1365 /* HID interface */
1366 DEVMETHOD(hid_get_rdesc, iichid_get_rdesc),
1367 DEVMETHOD(hid_read, iichid_read),
1368 DEVMETHOD(hid_write, iichid_write),
1369 DEVMETHOD(hid_get_report, iichid_get_report),
1370 DEVMETHOD(hid_set_report, iichid_set_report),
1371 DEVMETHOD(hid_set_idle, iichid_set_idle),
1372 DEVMETHOD(hid_set_protocol, iichid_set_protocol),
1373 DEVMETHOD(hid_ioctl, iichid_ioctl),
1374
1375 DEVMETHOD_END
1376 };
1377
1378 static driver_t iichid_driver = {
1379 .name = "iichid",
1380 .methods = iichid_methods,
1381 .size = sizeof(struct iichid_softc),
1382 };
1383
1384 DRIVER_MODULE(iichid, iicbus, iichid_driver, NULL, NULL);
1385 MODULE_DEPEND(iichid, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1386 MODULE_DEPEND(iichid, acpi, 1, 1, 1);
1387 MODULE_DEPEND(iichid, hid, 1, 1, 1);
1388 MODULE_DEPEND(iichid, hidbus, 1, 1, 1);
1389 MODULE_VERSION(iichid, 1);
1390 IICBUS_ACPI_PNP_INFO(iichid_ids);
1391