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