xref: /freebsd/sys/dev/hid/ietp.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020, 2022 Vladimir Kondratyev <wulf@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Elan I2C Touchpad driver. Based on Linux driver.
30  * https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/drivers/input/mouse/elan_i2c_core.c
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 
47 #include <dev/evdev/evdev.h>
48 #include <dev/evdev/input.h>
49 
50 #include <dev/iicbus/iic.h>
51 #include <dev/iicbus/iicbus.h>
52 
53 #define HID_DEBUG_VAR   ietp_debug
54 #include <dev/hid/hid.h>
55 #include <dev/hid/hidbus.h>
56 #include <dev/hid/hidquirk.h>
57 
58 #ifdef HID_DEBUG
59 static SYSCTL_NODE(_hw_hid, OID_AUTO, ietp, CTLFLAG_RW, 0,
60     "Elantech Touchpad");
61 static int ietp_debug = 1;
62 SYSCTL_INT(_hw_hid_ietp, OID_AUTO, debug, CTLFLAG_RWTUN,
63     &ietp_debug, 1, "Debug level");
64 #endif
65 
66 #define	IETP_PATTERN		0x0100
67 #define	IETP_UNIQUEID		0x0101
68 #define	IETP_FW_VERSION		0x0102
69 #define	IETP_IC_TYPE		0x0103
70 #define	IETP_OSM_VERSION	0x0103
71 #define	IETP_NSM_VERSION	0x0104
72 #define	IETP_TRACENUM		0x0105
73 #define	IETP_MAX_X_AXIS		0x0106
74 #define	IETP_MAX_Y_AXIS		0x0107
75 #define	IETP_RESOLUTION		0x0108
76 #define	IETP_PRESSURE		0x010A
77 
78 #define	IETP_CONTROL		0x0300
79 #define	IETP_CTRL_ABSOLUTE	0x0001
80 #define	IETP_CTRL_STANDARD	0x0000
81 
82 #define	IETP_REPORT_LEN_LO	32
83 #define	IETP_REPORT_LEN_HI	37
84 #define	IETP_MAX_FINGERS	5
85 
86 #define	IETP_REPORT_ID_LO	0x5D
87 #define	IETP_REPORT_ID_HI	0x60
88 
89 #define	IETP_TOUCH_INFO		1
90 #define	IETP_FINGER_DATA	2
91 #define	IETP_FINGER_DATA_LEN	5
92 #define	IETP_HOVER_INFO		28
93 #define	IETP_WH_DATA		31
94 
95 #define	IETP_TOUCH_LMB		(1 << 0)
96 #define	IETP_TOUCH_RMB		(1 << 1)
97 #define	IETP_TOUCH_MMB		(1 << 2)
98 
99 #define	IETP_MAX_PRESSURE	255
100 #define	IETP_FWIDTH_REDUCE	90
101 #define	IETP_FINGER_MAX_WIDTH	15
102 #define	IETP_PRESSURE_BASE	25
103 
104 struct ietp_softc {
105 	device_t		dev;
106 
107 	struct evdev_dev	*evdev;
108 	uint8_t			report_id;
109 	hid_size_t		report_len;
110 
111 	uint16_t		product_id;
112 	uint16_t		ic_type;
113 
114 	int32_t			pressure_base;
115 	uint16_t		max_x;
116 	uint16_t		max_y;
117 	uint16_t		trace_x;
118 	uint16_t		trace_y;
119 	uint16_t		res_x;		/* dots per mm */
120 	uint16_t		res_y;
121 	bool			hi_precision;
122 	bool			is_clickpad;
123 	bool			has_3buttons;
124 };
125 
126 static evdev_open_t	ietp_ev_open;
127 static evdev_close_t	ietp_ev_close;
128 static hid_intr_t	ietp_intr;
129 
130 static int		ietp_probe(struct ietp_softc *);
131 static int		ietp_attach(struct ietp_softc *);
132 static int		ietp_detach(struct ietp_softc *);
133 static int32_t		ietp_res2dpmm(uint8_t, bool);
134 
135 static device_identify_t ietp_iic_identify;
136 static device_probe_t	ietp_iic_probe;
137 static device_attach_t	ietp_iic_attach;
138 static device_detach_t	ietp_iic_detach;
139 static device_resume_t	ietp_iic_resume;
140 
141 static int		ietp_iic_read_reg(device_t, uint16_t, size_t, void *);
142 static int		ietp_iic_write_reg(device_t, uint16_t, uint16_t);
143 static int		ietp_iic_set_absolute_mode(device_t, bool);
144 
145 #define	IETP_IIC_DEV(pnp) \
146     { HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE), HID_BUS(BUS_I2C), HID_PNP(pnp) }
147 
148 static const struct hid_device_id ietp_iic_devs[] = {
149 	IETP_IIC_DEV("ELAN0000"),
150 	IETP_IIC_DEV("ELAN0100"),
151 	IETP_IIC_DEV("ELAN0600"),
152 	IETP_IIC_DEV("ELAN0601"),
153 	IETP_IIC_DEV("ELAN0602"),
154 	IETP_IIC_DEV("ELAN0603"),
155 	IETP_IIC_DEV("ELAN0604"),
156 	IETP_IIC_DEV("ELAN0605"),
157 	IETP_IIC_DEV("ELAN0606"),
158 	IETP_IIC_DEV("ELAN0607"),
159 	IETP_IIC_DEV("ELAN0608"),
160 	IETP_IIC_DEV("ELAN0609"),
161 	IETP_IIC_DEV("ELAN060B"),
162 	IETP_IIC_DEV("ELAN060C"),
163 	IETP_IIC_DEV("ELAN060F"),
164 	IETP_IIC_DEV("ELAN0610"),
165 	IETP_IIC_DEV("ELAN0611"),
166 	IETP_IIC_DEV("ELAN0612"),
167 	IETP_IIC_DEV("ELAN0615"),
168 	IETP_IIC_DEV("ELAN0616"),
169 	IETP_IIC_DEV("ELAN0617"),
170 	IETP_IIC_DEV("ELAN0618"),
171 	IETP_IIC_DEV("ELAN0619"),
172 	IETP_IIC_DEV("ELAN061A"),
173 	IETP_IIC_DEV("ELAN061B"),
174 	IETP_IIC_DEV("ELAN061C"),
175 	IETP_IIC_DEV("ELAN061D"),
176 	IETP_IIC_DEV("ELAN061E"),
177 	IETP_IIC_DEV("ELAN061F"),
178 	IETP_IIC_DEV("ELAN0620"),
179 	IETP_IIC_DEV("ELAN0621"),
180 	IETP_IIC_DEV("ELAN0622"),
181 	IETP_IIC_DEV("ELAN0623"),
182 	IETP_IIC_DEV("ELAN0624"),
183 	IETP_IIC_DEV("ELAN0625"),
184 	IETP_IIC_DEV("ELAN0626"),
185 	IETP_IIC_DEV("ELAN0627"),
186 	IETP_IIC_DEV("ELAN0628"),
187 	IETP_IIC_DEV("ELAN0629"),
188 	IETP_IIC_DEV("ELAN062A"),
189 	IETP_IIC_DEV("ELAN062B"),
190 	IETP_IIC_DEV("ELAN062C"),
191 	IETP_IIC_DEV("ELAN062D"),
192 	IETP_IIC_DEV("ELAN062E"),	/* Lenovo V340 Whiskey Lake U */
193 	IETP_IIC_DEV("ELAN062F"),	/* Lenovo V340 Comet Lake U */
194 	IETP_IIC_DEV("ELAN0631"),
195 	IETP_IIC_DEV("ELAN0632"),
196 	IETP_IIC_DEV("ELAN0633"),	/* Lenovo S145 */
197 	IETP_IIC_DEV("ELAN0634"),	/* Lenovo V340 Ice lake */
198 	IETP_IIC_DEV("ELAN0635"),	/* Lenovo V1415-IIL */
199 	IETP_IIC_DEV("ELAN0636"),	/* Lenovo V1415-Dali */
200 	IETP_IIC_DEV("ELAN0637"),	/* Lenovo V1415-IGLR */
201 	IETP_IIC_DEV("ELAN1000"),
202 };
203 
204 static uint8_t const ietp_dummy_rdesc[] = {
205 	0x05, HUP_GENERIC_DESKTOP,	/* Usage Page (Generic Desktop Ctrls)	*/
206 	0x09, HUG_MOUSE,		/* Usage (Mouse)			*/
207 	0xA1, 0x01,			/* Collection (Application)		*/
208 	0x09, 0x01,			/*   Usage (0x01)			*/
209 	0x95, IETP_REPORT_LEN_LO,	/*   Report Count (IETP_REPORT_LEN_LO)	*/
210 	0x75, 0x08,			/*   Report Size (8)			*/
211 	0x81, 0x02,			/*   Input (Data,Var,Abs)		*/
212 	0xC0,				/* End Collection			*/
213 };
214 
215 static const struct evdev_methods ietp_evdev_methods = {
216 	.ev_open = &ietp_ev_open,
217 	.ev_close = &ietp_ev_close,
218 };
219 
220 static int
221 ietp_ev_open(struct evdev_dev *evdev)
222 {
223 	return (hidbus_intr_start(evdev_get_softc(evdev)));
224 }
225 
226 static int
227 ietp_ev_close(struct evdev_dev *evdev)
228 {
229 	return (hidbus_intr_stop(evdev_get_softc(evdev)));
230 }
231 
232 static int
233 ietp_probe(struct ietp_softc *sc)
234 {
235 	if (hidbus_find_child(device_get_parent(sc->dev),
236 	    HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHPAD)) != NULL) {
237 		DPRINTFN(5, "Ignore HID-compatible touchpad on %s\n",
238 		    device_get_nameunit(device_get_parent(sc->dev)));
239 		return (ENXIO);
240 	}
241 
242 	device_set_desc(sc->dev, "Elan Touchpad");
243 
244 	return (BUS_PROBE_DEFAULT);
245 }
246 
247 static int
248 ietp_attach(struct ietp_softc *sc)
249 {
250 	const struct hid_device_info *hw = hid_get_device_info(sc->dev);
251 	void *d_ptr;
252 	hid_size_t d_len;
253 	int32_t minor, major;
254 	int error;
255 
256 	sc->report_id = sc->hi_precision ?
257 	    IETP_REPORT_ID_HI : IETP_REPORT_ID_LO;
258 	sc->report_len = sc->hi_precision ?
259 	    IETP_REPORT_LEN_HI : IETP_REPORT_LEN_LO;
260 
261 	/* Try to detect 3-rd button by relative mouse TLC */
262 	if (!sc->is_clickpad) {
263 		error = hid_get_report_descr(sc->dev, &d_ptr, &d_len);
264 		if (error != 0) {
265 			device_printf(sc->dev, "could not retrieve report "
266 			    "descriptor from device: %d\n", error);
267 			return (ENXIO);
268 		}
269 		if (hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_BUTTON, 3),
270 		    hid_input, hidbus_get_index(sc->dev), 0, NULL, NULL, NULL,
271 		    NULL))
272 			sc->has_3buttons = true;
273 	}
274 
275 	sc->evdev = evdev_alloc();
276 	evdev_set_name(sc->evdev, device_get_desc(sc->dev));
277 	evdev_set_phys(sc->evdev, device_get_nameunit(sc->dev));
278 	evdev_set_id(sc->evdev, hw->idBus, hw->idVendor, hw->idProduct,
279 	    hw->idVersion);
280 	evdev_set_serial(sc->evdev, hw->serial);
281 	evdev_set_methods(sc->evdev, sc->dev, &ietp_evdev_methods);
282 	evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT);
283 	evdev_set_flag(sc->evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */
284 
285 	evdev_support_event(sc->evdev, EV_SYN);
286 	evdev_support_event(sc->evdev, EV_ABS);
287 	evdev_support_event(sc->evdev, EV_KEY);
288 	evdev_support_prop(sc->evdev, INPUT_PROP_POINTER);
289 	evdev_support_key(sc->evdev, BTN_LEFT);
290 	if (sc->is_clickpad) {
291 		evdev_support_prop(sc->evdev, INPUT_PROP_BUTTONPAD);
292 	} else {
293 		evdev_support_key(sc->evdev, BTN_RIGHT);
294 		if (sc->has_3buttons)
295 			evdev_support_key(sc->evdev, BTN_MIDDLE);
296 	}
297 
298 	major = IETP_FINGER_MAX_WIDTH * MAX(sc->trace_x, sc->trace_y);
299 	minor = IETP_FINGER_MAX_WIDTH * MIN(sc->trace_x, sc->trace_y);
300 
301 	evdev_support_abs(sc->evdev, ABS_MT_SLOT,
302 	    0, IETP_MAX_FINGERS - 1, 0, 0, 0);
303 	evdev_support_abs(sc->evdev, ABS_MT_TRACKING_ID,
304 	    -1, IETP_MAX_FINGERS - 1, 0, 0, 0);
305 	evdev_support_abs(sc->evdev, ABS_MT_POSITION_X,
306 	    0, sc->max_x, 0, 0, sc->res_x);
307 	evdev_support_abs(sc->evdev, ABS_MT_POSITION_Y,
308 	    0, sc->max_y, 0, 0, sc->res_y);
309 	evdev_support_abs(sc->evdev, ABS_MT_PRESSURE,
310 	    0, IETP_MAX_PRESSURE, 0, 0, 0);
311 	evdev_support_abs(sc->evdev, ABS_MT_ORIENTATION, 0, 1, 0, 0, 0);
312 	evdev_support_abs(sc->evdev, ABS_MT_TOUCH_MAJOR, 0, major, 0, 0, 0);
313 	evdev_support_abs(sc->evdev, ABS_MT_TOUCH_MINOR, 0, minor, 0, 0, 0);
314 	evdev_support_abs(sc->evdev, ABS_DISTANCE, 0, 1, 0, 0, 0);
315 
316 	error = evdev_register(sc->evdev);
317 	if (error != 0) {
318 		ietp_detach(sc);
319 		return (ENOMEM);
320 	}
321 
322 	hidbus_set_intr(sc->dev, ietp_intr, sc);
323 
324 	device_printf(sc->dev, "[%d:%d], %s\n", sc->max_x, sc->max_y,
325 	    sc->is_clickpad ? "clickpad" :
326 	    sc->has_3buttons ? "3 buttons" : "2 buttons");
327 
328 	return (0);
329 }
330 
331 static int
332 ietp_detach(struct ietp_softc *sc)
333 {
334 	evdev_free(sc->evdev);
335 
336 	return (0);
337 }
338 
339 static void
340 ietp_intr(void *context, void *buf, hid_size_t len)
341 {
342 	struct ietp_softc *sc = context;
343 	union evdev_mt_slot slot_data;
344 	uint8_t *report, *fdata;
345 	int32_t finger;
346 	int32_t x, y, w, h, wh;
347 
348 	/* we seem to get 0 length reports sometimes, ignore them */
349 	if (len == 0)
350 		return;
351 	if (len != sc->report_len) {
352 		DPRINTF("wrong report length (%d vs %d expected)", len, sc->report_len);
353 		return;
354 	}
355 
356 	report = buf;
357 	if (*report != sc->report_id)
358 		return;
359 
360 	evdev_push_key(sc->evdev, BTN_LEFT,
361 	    report[IETP_TOUCH_INFO] & IETP_TOUCH_LMB);
362 	evdev_push_key(sc->evdev, BTN_MIDDLE,
363 	    report[IETP_TOUCH_INFO] & IETP_TOUCH_MMB);
364 	evdev_push_key(sc->evdev, BTN_RIGHT,
365 	    report[IETP_TOUCH_INFO] & IETP_TOUCH_RMB);
366 	evdev_push_abs(sc->evdev, ABS_DISTANCE,
367 	    (report[IETP_HOVER_INFO] & 0x40) >> 6);
368 
369 	for (finger = 0, fdata = report + IETP_FINGER_DATA;
370 	     finger < IETP_MAX_FINGERS;
371 	     finger++, fdata += IETP_FINGER_DATA_LEN) {
372 		if ((report[IETP_TOUCH_INFO] & (1 << (finger + 3))) != 0) {
373 			if (sc->hi_precision) {
374 				x = fdata[0] << 8 | fdata[1];
375 				y = fdata[2] << 8 | fdata[3];
376 				wh = report[IETP_WH_DATA + finger];
377 			} else {
378 				x = (fdata[0] & 0xf0) << 4 | fdata[1];
379 				y = (fdata[0] & 0x0f) << 8 | fdata[2];
380 				wh = fdata[3];
381 			}
382 
383 			if (x > sc->max_x || y > sc->max_y) {
384 				DPRINTF("[%d] x=%d y=%d over max (%d, %d)",
385 				    finger, x, y, sc->max_x, sc->max_y);
386 				continue;
387 			}
388 
389 			/* Reduce trace size to not treat large finger as palm */
390 			w = (wh & 0x0F) * (sc->trace_x - IETP_FWIDTH_REDUCE);
391 			h = (wh >> 4) * (sc->trace_y - IETP_FWIDTH_REDUCE);
392 
393 			slot_data = (union evdev_mt_slot) {
394 				.id = finger,
395 				.x = x,
396 				.y = sc->max_y - y,
397 				.p = MIN((int32_t)fdata[4] + sc->pressure_base,
398 				    IETP_MAX_PRESSURE),
399 				.ori = w > h ? 1 : 0,
400 				.maj = MAX(w, h),
401 				.min = MIN(w, h),
402 			};
403 			evdev_mt_push_slot(sc->evdev, finger, &slot_data);
404 		} else {
405 			evdev_push_abs(sc->evdev, ABS_MT_SLOT, finger);
406 			evdev_push_abs(sc->evdev, ABS_MT_TRACKING_ID, -1);
407 		}
408 	}
409 
410 	evdev_sync(sc->evdev);
411 }
412 
413 static int32_t
414 ietp_res2dpmm(uint8_t res, bool hi_precision)
415 {
416 	int32_t dpi;
417 
418 	dpi = hi_precision ? 300 + res * 100 : 790 + res * 10;
419 
420 	return (dpi * 10 /254);
421 }
422 
423 static void
424 ietp_iic_identify(driver_t *driver, device_t parent)
425 {
426 	void *d_ptr;
427 	hid_size_t d_len;
428 	int isize;
429 	uint8_t iid;
430 
431 	if (HIDBUS_LOOKUP_ID(parent, ietp_iic_devs) == NULL)
432 		return;
433 	if (hid_get_report_descr(parent, &d_ptr, &d_len) != 0)
434 		return;
435 
436 	/*
437 	 * Some Elantech trackpads have a mangled HID report descriptor, which
438 	 * reads as having an incorrect input size (i.e. < IETP_REPORT_LEN_LO).
439 	 * If the input size is incorrect, load a dummy report descriptor.
440 	 */
441 
442 	isize = hid_report_size_max(d_ptr, d_len, hid_input, &iid);
443 	if (isize >= IETP_REPORT_LEN_LO)
444 		return;
445 
446 	hid_set_report_descr(parent, ietp_dummy_rdesc,
447 	    sizeof(ietp_dummy_rdesc));
448 }
449 
450 static int
451 ietp_iic_probe(device_t dev)
452 {
453 	struct ietp_softc *sc = device_get_softc(dev);
454 	device_t iichid;
455 	int error;
456 
457 	error = HIDBUS_LOOKUP_DRIVER_INFO(dev, ietp_iic_devs);
458 	if (error != 0)
459 		return (error);
460 
461 	iichid = device_get_parent(device_get_parent(dev));
462 	if (device_get_devclass(iichid) != devclass_find("iichid"))
463 		return (ENXIO);
464 
465 	sc->dev = dev;
466 
467 	return (ietp_probe(sc));
468 }
469 
470 static int
471 ietp_iic_attach(device_t dev)
472 {
473 	struct ietp_softc *sc = device_get_softc(dev);
474 	uint16_t buf, reg;
475 	uint8_t *buf8;
476 	uint8_t pattern;
477 
478 	buf8 = (uint8_t *)&buf;
479 
480 	if (ietp_iic_read_reg(dev, IETP_UNIQUEID, sizeof(buf), &buf) != 0) {
481 		device_printf(sc->dev, "failed reading product ID\n");
482 		return (EIO);
483 	}
484 	sc->product_id = le16toh(buf);
485 
486 	if (ietp_iic_read_reg(dev, IETP_PATTERN, sizeof(buf), &buf) != 0) {
487 		device_printf(sc->dev, "failed reading pattern\n");
488 		return (EIO);
489 	}
490 	pattern = buf == 0xFFFF ? 0 : buf8[1];
491 	sc->hi_precision = pattern >= 0x02;
492 
493 	reg = pattern >= 0x01 ? IETP_IC_TYPE : IETP_OSM_VERSION;
494 	if (ietp_iic_read_reg(dev, reg, sizeof(buf), &buf) != 0) {
495 		device_printf(sc->dev, "failed reading IC type\n");
496 		return (EIO);
497 	}
498 	sc->ic_type = pattern >= 0x01 ? be16toh(buf) : buf8[1];
499 
500 	if (ietp_iic_read_reg(dev, IETP_NSM_VERSION, sizeof(buf), &buf) != 0) {
501 		device_printf(sc->dev, "failed reading SM version\n");
502 		return (EIO);
503 	}
504 	sc->is_clickpad = (buf8[0] & 0x10) != 0;
505 
506 	if (ietp_iic_set_absolute_mode(dev, true) != 0) {
507 		device_printf(sc->dev, "failed to set absolute mode\n");
508 		return (EIO);
509 	}
510 
511 	if (ietp_iic_read_reg(dev, IETP_MAX_X_AXIS, sizeof(buf), &buf) != 0) {
512 		device_printf(sc->dev, "failed reading max x\n");
513 		return (EIO);
514 	}
515 	sc->max_x = le16toh(buf);
516 
517 	if (ietp_iic_read_reg(dev, IETP_MAX_Y_AXIS, sizeof(buf), &buf) != 0) {
518 		device_printf(sc->dev, "failed reading max y\n");
519 		return (EIO);
520 	}
521 	sc->max_y = le16toh(buf);
522 
523 	if (ietp_iic_read_reg(dev, IETP_TRACENUM, sizeof(buf), &buf) != 0) {
524 		device_printf(sc->dev, "failed reading trace info\n");
525 		return (EIO);
526 	}
527 	sc->trace_x = sc->max_x / buf8[0];
528 	sc->trace_y = sc->max_y / buf8[1];
529 
530 	if (ietp_iic_read_reg(dev, IETP_PRESSURE, sizeof(buf), &buf) != 0) {
531 		device_printf(sc->dev, "failed reading pressure format\n");
532 		return (EIO);
533 	}
534 	sc->pressure_base = (buf8[0] & 0x10) ? 0 : IETP_PRESSURE_BASE;
535 
536 	if (ietp_iic_read_reg(dev, IETP_RESOLUTION, sizeof(buf), &buf)  != 0) {
537 		device_printf(sc->dev, "failed reading resolution\n");
538 		return (EIO);
539 	}
540 	/* Conversion from internal format to dot per mm */
541 	sc->res_x = ietp_res2dpmm(buf8[0], sc->hi_precision);
542 	sc->res_y = ietp_res2dpmm(buf8[1], sc->hi_precision);
543 
544 	return (ietp_attach(sc));
545 }
546 
547 static int
548 ietp_iic_detach(device_t dev)
549 {
550 	struct ietp_softc *sc = device_get_softc(dev);
551 
552 	if (ietp_iic_set_absolute_mode(dev, false) != 0)
553 		device_printf(dev, "failed setting standard mode\n");
554 
555 	return (ietp_detach(sc));
556 }
557 
558 static int
559 ietp_iic_resume(device_t dev)
560 {
561 	if (ietp_iic_set_absolute_mode(dev, true) != 0) {
562 		device_printf(dev, "reset when resuming failed: \n");
563 		return (EIO);
564 	}
565 
566 	return (0);
567 }
568 
569 static int
570 ietp_iic_set_absolute_mode(device_t dev, bool enable)
571 {
572 	struct ietp_softc *sc = device_get_softc(dev);
573 	static const struct {
574 		uint16_t	ic_type;
575 		uint16_t	product_id;
576 	} special_fw[] = {
577 	    { 0x0E, 0x05 }, { 0x0E, 0x06 }, { 0x0E, 0x07 }, { 0x0E, 0x09 },
578 	    { 0x0E, 0x13 }, { 0x08, 0x26 },
579 	};
580 	uint16_t val;
581 	int i, error;
582 	bool require_wakeup;
583 
584 	error = 0;
585 
586 	/*
587 	 * Some ASUS touchpads need to be powered on to enter absolute mode.
588 	 */
589 	require_wakeup = false;
590 	for (i = 0; i < nitems(special_fw); i++) {
591 		if (sc->ic_type == special_fw[i].ic_type &&
592 		    sc->product_id == special_fw[i].product_id) {
593 			require_wakeup = true;
594 			break;
595 		}
596 	}
597 
598 	if (require_wakeup && hidbus_intr_start(dev) != 0) {
599 		device_printf(dev, "failed writing poweron command\n");
600 		return (EIO);
601 	}
602 
603 	val = enable ? IETP_CTRL_ABSOLUTE : IETP_CTRL_STANDARD;
604 	if (ietp_iic_write_reg(dev, IETP_CONTROL, val) != 0) {
605 		device_printf(dev, "failed setting absolute mode\n");
606 		error = EIO;
607 	}
608 
609 	if (require_wakeup && hidbus_intr_stop(dev) != 0) {
610 		device_printf(dev, "failed writing poweroff command\n");
611 		error = EIO;
612 	}
613 
614 	return (error);
615 }
616 
617 static int
618 ietp_iic_read_reg(device_t dev, uint16_t reg, size_t len, void *val)
619 {
620 	device_t iichid = device_get_parent(device_get_parent(dev));
621 	uint16_t addr = iicbus_get_addr(iichid) << 1;
622 	uint8_t cmd[2] = { reg & 0xff, (reg >> 8) & 0xff };
623 	struct iic_msg msgs[2] = {
624 	    { addr, IIC_M_WR | IIC_M_NOSTOP,  sizeof(cmd), cmd },
625 	    { addr, IIC_M_RD, len, val },
626 	};
627 	struct iic_rdwr_data ird = { msgs, nitems(msgs) };
628 	int error;
629 
630 	DPRINTF("Read reg 0x%04x with size %zu\n", reg, len);
631 
632 	error = hid_ioctl(dev, I2CRDWR, (uintptr_t)&ird);
633 	if (error != 0)
634 		return (error);
635 
636 	DPRINTF("Response: %*D\n", (int)len, val, " ");
637 
638 	return (0);
639 }
640 
641 static int
642 ietp_iic_write_reg(device_t dev, uint16_t reg, uint16_t val)
643 {
644 	device_t iichid = device_get_parent(device_get_parent(dev));
645 	uint16_t addr = iicbus_get_addr(iichid) << 1;
646 	uint8_t cmd[4] = { reg & 0xff, (reg >> 8) & 0xff,
647 			   val & 0xff, (val >> 8) & 0xff };
648 	struct iic_msg msgs[1] = {
649 	    { addr, IIC_M_WR, sizeof(cmd), cmd },
650 	};
651 	struct iic_rdwr_data ird = { msgs, nitems(msgs) };
652 
653 	DPRINTF("Write reg 0x%04x with value 0x%04x\n", reg, val);
654 
655 	return (hid_ioctl(dev, I2CRDWR, (uintptr_t)&ird));
656 }
657 
658 static device_method_t ietp_methods[] = {
659 	DEVMETHOD(device_identify,	ietp_iic_identify),
660 	DEVMETHOD(device_probe,		ietp_iic_probe),
661 	DEVMETHOD(device_attach,	ietp_iic_attach),
662 	DEVMETHOD(device_detach,	ietp_iic_detach),
663 	DEVMETHOD(device_resume,	ietp_iic_resume),
664 	DEVMETHOD_END
665 };
666 
667 static driver_t ietp_driver = {
668 	.name = "ietp",
669 	.methods = ietp_methods,
670 	.size = sizeof(struct ietp_softc),
671 };
672 
673 DRIVER_MODULE(ietp, hidbus, ietp_driver, NULL, NULL);
674 MODULE_DEPEND(ietp, hidbus, 1, 1, 1);
675 MODULE_DEPEND(ietp, hid, 1, 1, 1);
676 MODULE_DEPEND(ietp, evdev, 1, 1, 1);
677 MODULE_VERSION(ietp, 1);
678 HID_PNP_INFO(ietp_iic_devs);
679