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