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