2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Abstract 1 to 1 HID input usage to evdev event mapper driver.
37 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
46 #include <dev/evdev/input.h>
47 #include <dev/evdev/evdev.h>
49 #include <dev/hid/hid.h>
50 #include <dev/hid/hidbus.h>
51 #include <dev/hid/hidmap.h>
54 #define DPRINTFN(hm, n, fmt, ...) do { \
55 if ((hm)->debug_var != NULL && *(hm)->debug_var >= (n)) { \
56 device_printf((hm)->dev, "%s: " fmt, \
57 __FUNCTION__ ,##__VA_ARGS__); \
60 #define DPRINTF(hm, ...) DPRINTFN(hm, 1, __VA_ARGS__)
62 #define DPRINTF(...) do { } while (0)
63 #define DPRINTFN(...) do { } while (0)
66 static evdev_open_t hidmap_ev_open;
67 static evdev_close_t hidmap_ev_close;
69 #define HIDMAP_WANT_MERGE_KEYS(hm) ((hm)->key_rel != NULL)
71 #define HIDMAP_FOREACH_ITEM(hm, mi, uoff) \
72 for (u_int _map = 0, _item = 0, _uoff_priv = -1; \
73 ((mi) = hidmap_get_next_map_item( \
74 (hm), &_map, &_item, &_uoff_priv, &(uoff))) != NULL;)
77 hidmap_get_next_map_index(const struct hidmap_item *map, int nmap_items,
78 uint32_t *index, uint16_t *usage_offset)
82 if ((*index != 0 || *usage_offset != 0) &&
83 *usage_offset >= map[*index].nusages) {
88 return (*index < nmap_items);
91 static inline const struct hidmap_item *
92 hidmap_get_next_map_item(struct hidmap *hm, u_int *map, u_int *item,
93 u_int *uoff_priv, uint16_t *uoff)
97 while (!hidmap_get_next_map_index(
98 hm->map[*map], hm->nmap_items[*map], item, uoff)) {
102 if (*map >= hm->nmaps)
107 return (hm->map[*map] + *item);
111 _hidmap_set_debug_var(struct hidmap *hm, int *debug_var)
114 hm->debug_var = debug_var;
119 hidmap_ev_close(struct evdev_dev *evdev)
121 return (hidbus_intr_stop(evdev_get_softc(evdev)));
125 hidmap_ev_open(struct evdev_dev *evdev)
127 return (hidbus_intr_start(evdev_get_softc(evdev)));
131 hidmap_support_key(struct hidmap *hm, uint16_t key)
133 if (hm->key_press == NULL) {
134 hm->key_press = malloc(howmany(KEY_CNT, 8), M_DEVBUF,
136 evdev_support_event(hm->evdev, EV_KEY);
140 hm->key_min = MIN(hm->key_min, key);
141 hm->key_max = MAX(hm->key_max, key);
142 if (isset(hm->key_press, key)) {
143 if (hm->key_rel == NULL)
144 hm->key_rel = malloc(howmany(KEY_CNT, 8), M_DEVBUF,
147 setbit(hm->key_press, key);
148 evdev_support_key(hm->evdev, key);
153 hidmap_push_key(struct hidmap *hm, uint16_t key, int32_t value)
155 if (HIDMAP_WANT_MERGE_KEYS(hm))
156 setbit(value != 0 ? hm->key_press : hm->key_rel, key);
158 evdev_push_key(hm->evdev, key, value);
162 hidmap_sync_keys(struct hidmap *hm)
167 for (j = hm->key_min / 8; j <= hm->key_max / 8; j++) {
168 if (hm->key_press[j] != hm->key_rel[j]) {
169 for (i = j * 8; i < j * 8 + 8; i++) {
170 press = isset(hm->key_press, i);
171 rel = isset(hm->key_rel, i);
173 evdev_push_key(hm->evdev, i, press);
177 bzero(hm->key_press, howmany(KEY_CNT, 8));
178 bzero(hm->key_rel, howmany(KEY_CNT, 8));
182 hidmap_intr(void *context, void *buf, hid_size_t len)
184 struct hidmap *hm = context;
185 struct hidmap_hid_item *hi;
186 const struct hidmap_item *mi;
191 bool found, do_sync = false;
193 DPRINTFN(hm, 6, "hm=%p len=%d\n", hm, len);
194 DPRINTFN(hm, 6, "data = %*D\n", len, buf, " ");
196 /* Strip leading "report ID" byte */
197 if (hm->hid_items[0].id) {
198 id = *(uint8_t *)buf;
200 buf = (uint8_t *)buf + 1;
206 for (hi = hm->hid_items; hi < hm->hid_items + hm->nhid_items; hi++) {
207 /* At first run callbacks that not tied to HID items */
208 if (hi->type == HIDMAP_TYPE_FINALCB) {
209 DPRINTFN(hm, 6, "type=%d item=%*D\n", hi->type,
210 (int)sizeof(hi->cb), &hi->cb, " ");
211 if (hi->cb(hm, hi, (union hidmap_cb_ctx){.rid = id})
217 /* Ignore irrelevant reports */
222 * 5.8. If Logical Minimum and Logical Maximum are both
223 * positive values then the contents of a field can be assumed
224 * to be an unsigned value. Otherwise, all integer values are
225 * signed values represented in 2’s complement format.
227 data = hi->lmin < 0 || hi->lmax < 0
228 ? hid_get_data(buf, len, &hi->loc)
229 : hid_get_udata(buf, len, &hi->loc);
231 DPRINTFN(hm, 6, "type=%d data=%d item=%*D\n", hi->type, data,
232 (int)sizeof(hi->cb), &hi->cb, " ");
234 if (hi->invert_value && hi->type < HIDMAP_TYPE_ARR_LIST)
235 data = hi->evtype == EV_REL
237 : hi->lmin + hi->lmax - data;
240 case HIDMAP_TYPE_CALLBACK:
241 if (hi->cb(hm, hi, (union hidmap_cb_ctx){.data = data})
246 case HIDMAP_TYPE_VAR_NULLST:
248 * 5.10. If the host or the device receives an
249 * out-of-range value then the current value for the
250 * respective control will not be modified.
252 if (data < hi->lmin || data > hi->lmax)
255 case HIDMAP_TYPE_VARIABLE:
257 * Ignore reports for absolute data if the data did not
258 * change and for relative data if data is 0.
259 * Evdev layer filters out them anyway.
261 if (data == (hi->evtype == EV_REL ? 0 : hi->last_val))
263 if (hi->evtype == EV_KEY)
264 hidmap_push_key(hm, hi->code, data);
266 evdev_push_event(hm->evdev, hi->evtype,
271 case HIDMAP_TYPE_ARR_LIST:
274 * 6.2.2.5. An out-of range value in an array field
275 * is considered no controls asserted.
277 if (data < hi->lmin || data > hi->lmax)
280 * 6.2.2.5. Rather than returning a single bit for each
281 * button in the group, an array returns an index in
282 * each field that corresponds to the pressed button.
284 key = hi->codes[data - hi->lmin];
285 if (key == KEY_RESERVED)
286 DPRINTF(hm, "Can not map unknown HID "
287 "array index: %08x\n", data);
290 case HIDMAP_TYPE_ARR_RANGE:
293 * 6.2.2.5. An out-of range value in an array field
294 * is considered no controls asserted.
296 if (data < hi->lmin || data > hi->lmax)
299 * When the input field is an array and the usage is
300 * specified with a range instead of an ID, we have to
301 * derive the actual usage by using the item value as
302 * an index in the usage range list.
304 usage = data - hi->lmin + hi->umin;
306 HIDMAP_FOREACH_ITEM(hm, mi, uoff) {
307 if (usage == mi->usage + uoff &&
308 mi->type == EV_KEY && !mi->has_cb) {
315 DPRINTF(hm, "Can not map unknown HID "
316 "usage: %08x\n", usage);
318 if (key == HIDMAP_KEY_NULL || key == hi->last_key)
320 if (hi->last_key != KEY_RESERVED)
321 hidmap_push_key(hm, hi->last_key, 0);
322 if (key != KEY_RESERVED)
323 hidmap_push_key(hm, key, 1);
328 KASSERT(0, ("Unknown map type (%d)", hi->type));
334 if (HIDMAP_WANT_MERGE_KEYS(hm))
335 hidmap_sync_keys(hm);
336 evdev_sync(hm->evdev);
341 can_map_callback(struct hid_item *hi, const struct hidmap_item *mi,
342 uint16_t usage_offset)
345 return (mi->has_cb && !mi->final_cb &&
346 hi->usage == mi->usage + usage_offset &&
347 (mi->relabs == HIDMAP_RELABS_ANY ||
348 !(hi->flags & HIO_RELATIVE) == !(mi->relabs == HIDMAP_RELATIVE)));
352 can_map_variable(struct hid_item *hi, const struct hidmap_item *mi,
353 uint16_t usage_offset)
356 return ((hi->flags & HIO_VARIABLE) != 0 && !mi->has_cb &&
357 hi->usage == mi->usage + usage_offset &&
358 (mi->relabs == HIDMAP_RELABS_ANY ||
359 !(hi->flags & HIO_RELATIVE) == !(mi->relabs == HIDMAP_RELATIVE)));
363 can_map_arr_range(struct hid_item *hi, const struct hidmap_item *mi,
364 uint16_t usage_offset)
367 return ((hi->flags & HIO_VARIABLE) == 0 && !mi->has_cb &&
368 hi->usage_minimum <= mi->usage + usage_offset &&
369 hi->usage_maximum >= mi->usage + usage_offset &&
370 mi->type == EV_KEY &&
371 (mi->code != KEY_RESERVED && mi->code != HIDMAP_KEY_NULL));
375 can_map_arr_list(struct hid_item *hi, const struct hidmap_item *mi,
376 uint32_t usage, uint16_t usage_offset)
379 return ((hi->flags & HIO_VARIABLE) == 0 && !mi->has_cb &&
380 usage == mi->usage + usage_offset &&
381 mi->type == EV_KEY &&
382 (mi->code != KEY_RESERVED && mi->code != HIDMAP_KEY_NULL));
386 hidmap_probe_hid_item(struct hid_item *hi, const struct hidmap_item *map,
387 int nitems_map, hidmap_caps_t caps)
393 #define HIDMAP_FOREACH_INDEX(map, nitems, idx, uoff) \
394 for ((idx) = 0, (uoff) = -1; \
395 hidmap_get_next_map_index((map), (nitems), &(idx), &(uoff));)
397 HIDMAP_FOREACH_INDEX(map, nitems_map, i, uoff) {
398 if (can_map_callback(hi, map + i, uoff)) {
399 if (map[i].cb(NULL, NULL,
400 (union hidmap_cb_ctx){.hi = hi}) != 0)
407 if (hi->flags & HIO_VARIABLE) {
408 HIDMAP_FOREACH_INDEX(map, nitems_map, i, uoff) {
409 if (can_map_variable(hi, map + i, uoff)) {
410 KASSERT(map[i].type == EV_KEY ||
411 map[i].type == EV_REL ||
412 map[i].type == EV_ABS ||
413 map[i].type == EV_SW,
414 ("Unsupported event type"));
422 if (hi->usage_minimum != 0 || hi->usage_maximum != 0) {
423 HIDMAP_FOREACH_INDEX(map, nitems_map, i, uoff) {
424 if (can_map_arr_range(hi, map + i, uoff)) {
432 for (j = 0; j < hi->nusages; j++) {
433 HIDMAP_FOREACH_INDEX(map, nitems_map, i, uoff) {
434 if (can_map_arr_list(hi, map+i, hi->usages[j], uoff)) {
445 hidmap_probe_hid_descr(void *d_ptr, hid_size_t d_len, uint8_t tlc_index,
446 const struct hidmap_item *map, int nitems_map, hidmap_caps_t caps)
450 uint32_t i, items = 0;
451 bool do_free = false;
454 caps = malloc(HIDMAP_CAPS_SZ(nitems_map), M_DEVBUF, M_WAITOK);
457 bzero (caps, HIDMAP_CAPS_SZ(nitems_map));
460 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
461 HIDBUS_FOREACH_ITEM(hd, &hi, tlc_index) {
462 if (hi.kind != hid_input)
464 if (hi.flags & HIO_CONST)
466 for (i = 0; i < hi.loc.count; i++, hi.loc.pos += hi.loc.size)
467 if (hidmap_probe_hid_item(&hi, map, nitems_map, caps))
472 /* Take finalizing callbacks in to account */
473 for (i = 0; i < nitems_map; i++) {
474 if (map[i].has_cb && map[i].final_cb &&
475 map[i].cb(NULL, NULL, (union hidmap_cb_ctx){}) == 0) {
481 /* Check that all mandatory usages are present in report descriptor */
483 for (i = 0; i < nitems_map; i++) {
484 if (map[i].required && isclr(caps, i)) {
492 free(caps, M_DEVBUF);
498 hidmap_add_map(struct hidmap *hm, const struct hidmap_item *map,
499 int nitems_map, hidmap_caps_t caps)
505 uint8_t tlc_index = hidbus_get_index(hm->dev);
507 /* Avoid double-adding of map in probe() handler */
508 for (i = 0; i < hm->nmaps; i++)
509 if (hm->map[i] == map)
512 error = hid_get_report_descr(hm->dev, &d_ptr, &d_len);
514 device_printf(hm->dev, "could not retrieve report descriptor "
515 "from device: %d\n", error);
519 hm->cb_state = HIDMAP_CB_IS_PROBING;
520 items = hidmap_probe_hid_descr(d_ptr, d_len, tlc_index, map,
525 KASSERT(hm->nmaps < HIDMAP_MAX_MAPS,
526 ("Not more than %d maps is supported", HIDMAP_MAX_MAPS));
527 hm->nhid_items += items;
528 hm->map[hm->nmaps] = map;
529 hm->nmap_items[hm->nmaps] = nitems_map;
536 hidmap_parse_hid_item(struct hidmap *hm, struct hid_item *hi,
537 struct hidmap_hid_item *item)
539 const struct hidmap_item *mi;
540 struct hidmap_hid_item hi_temp;
545 HIDMAP_FOREACH_ITEM(hm, mi, uoff) {
546 if (can_map_callback(hi, mi, uoff)) {
547 bzero(&hi_temp, sizeof(hi_temp));
549 hi_temp.type = HIDMAP_TYPE_CALLBACK;
551 * Values returned by probe- and attach-stage
552 * callbacks MUST be identical.
554 if (mi->cb(hm, &hi_temp,
555 (union hidmap_cb_ctx){.hi = hi}) != 0)
557 bcopy(&hi_temp, item, sizeof(hi_temp));
562 if (hi->flags & HIO_VARIABLE) {
563 HIDMAP_FOREACH_ITEM(hm, mi, uoff) {
564 if (can_map_variable(hi, mi, uoff)) {
565 item->evtype = mi->type;
566 item->code = mi->code + uoff;
567 item->type = hi->flags & HIO_NULLSTATE
568 ? HIDMAP_TYPE_VAR_NULLST
569 : HIDMAP_TYPE_VARIABLE;
571 item->invert_value = mi->invert_value;
574 hidmap_support_key(hm, item->code);
577 evdev_support_event(hm->evdev, EV_REL);
578 evdev_support_rel(hm->evdev,
582 evdev_support_event(hm->evdev, EV_ABS);
583 evdev_support_abs(hm->evdev,
589 hid_item_resolution(hi));
592 evdev_support_event(hm->evdev, EV_SW);
593 evdev_support_sw(hm->evdev,
597 KASSERT(0, ("Unsupported event type"));
605 if (hi->usage_minimum != 0 || hi->usage_maximum != 0) {
606 HIDMAP_FOREACH_ITEM(hm, mi, uoff) {
607 if (can_map_arr_range(hi, mi, uoff)) {
608 hidmap_support_key(hm, mi->code + uoff);
614 item->umin = hi->usage_minimum;
615 item->type = HIDMAP_TYPE_ARR_RANGE;
616 item->last_key = KEY_RESERVED;
620 for (i = 0; i < hi->nusages; i++) {
621 HIDMAP_FOREACH_ITEM(hm, mi, uoff) {
622 if (can_map_arr_list(hi, mi, hi->usages[i], uoff)) {
623 hidmap_support_key(hm, mi->code + uoff);
624 if (item->codes == NULL)
625 item->codes = malloc(
626 hi->nusages * sizeof(uint16_t),
627 M_DEVBUF, M_WAITOK | M_ZERO);
628 item->codes[i] = mi->code + uoff;
636 item->type = HIDMAP_TYPE_ARR_LIST;
637 item->last_key = KEY_RESERVED;
640 item->id = hi->report_ID;
643 item->lmin = hi->logical_minimum;
644 item->lmax = hi->logical_maximum;
646 DPRINTFN(hm, 6, "usage=%04x id=%d loc=%u/%u type=%d item=%*D\n",
647 hi->usage, hi->report_ID, hi->loc.pos, hi->loc.size, item->type,
648 (int)sizeof(item->cb), &item->cb, " ");
654 hidmap_parse_hid_descr(struct hidmap *hm, uint8_t tlc_index)
656 const struct hidmap_item *map;
657 struct hidmap_hid_item *item = hm->hid_items;
664 error = hid_get_report_descr(hm->dev, &d_ptr, &d_len);
666 DPRINTF(hm, "could not retrieve report descriptor from "
667 "device: %d\n", error);
672 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
673 HIDBUS_FOREACH_ITEM(hd, &hi, tlc_index) {
674 if (hi.kind != hid_input)
676 if (hi.flags & HIO_CONST)
678 for (i = 0; i < hi.loc.count; i++, hi.loc.pos += hi.loc.size)
679 if (hidmap_parse_hid_item(hm, &hi, item))
681 KASSERT(item <= hm->hid_items + hm->nhid_items,
682 ("Parsed HID item array overflow"));
686 /* Add finalizing callbacks to the end of list */
687 for (i = 0; i < hm->nmaps; i++) {
688 for (map = hm->map[i];
689 map < hm->map[i] + hm->nmap_items[i];
691 if (map->has_cb && map->final_cb &&
692 map->cb(hm, item, (union hidmap_cb_ctx){}) == 0) {
694 item->type = HIDMAP_TYPE_FINALCB;
701 * Resulting number of parsed HID items can be less than expected as
702 * map items might be duplicated in different maps. Save real number.
704 if (hm->nhid_items != item - hm->hid_items)
705 DPRINTF(hm, "Parsed HID item number mismatch: expected=%u "
706 "result=%td\n", hm->nhid_items, item - hm->hid_items);
707 hm->nhid_items = item - hm->hid_items;
709 if (HIDMAP_WANT_MERGE_KEYS(hm))
710 bzero(hm->key_press, howmany(KEY_CNT, 8));
716 hidmap_probe(struct hidmap* hm, device_t dev,
717 const struct hid_device_id *id, int nitems_id,
718 const struct hidmap_item *map, int nitems_map,
719 const char *suffix, hidmap_caps_t caps)
723 error = hidbus_lookup_driver_info(dev, id, nitems_id);
727 hidmap_set_dev(hm, dev);
729 error = hidmap_add_map(hm, map, nitems_map, caps);
733 hidbus_set_desc(dev, suffix);
735 return (BUS_PROBE_DEFAULT);
739 hidmap_attach(struct hidmap* hm)
741 const struct hid_device_info *hw = hid_get_device_info(hm->dev);
748 if (hm->debug_var == NULL) {
749 hm->debug_var = &hm->debug_level;
750 snprintf(tunable, sizeof(tunable), "hw.hid.%s.debug",
751 device_get_name(hm->dev));
752 TUNABLE_INT_FETCH(tunable, &hm->debug_level);
753 SYSCTL_ADD_INT(device_get_sysctl_ctx(hm->dev),
754 SYSCTL_CHILDREN(device_get_sysctl_tree(hm->dev)),
755 OID_AUTO, "debug", CTLFLAG_RWTUN,
756 &hm->debug_level, 0, "Verbosity level");
760 DPRINTFN(hm, 11, "hm=%p\n", hm);
762 hm->cb_state = HIDMAP_CB_IS_ATTACHING;
764 hm->hid_items = malloc(hm->nhid_items * sizeof(struct hid_item),
765 M_DEVBUF, M_WAITOK | M_ZERO);
767 hidbus_set_intr(hm->dev, hidmap_intr, hm);
768 hm->evdev_methods = (struct evdev_methods) {
769 .ev_open = &hidmap_ev_open,
770 .ev_close = &hidmap_ev_close,
773 hm->evdev = evdev_alloc();
774 evdev_set_name(hm->evdev, device_get_desc(hm->dev));
775 evdev_set_phys(hm->evdev, device_get_nameunit(hm->dev));
776 evdev_set_id(hm->evdev, hw->idBus, hw->idVendor, hw->idProduct,
778 evdev_set_serial(hm->evdev, hw->serial);
779 evdev_set_flag(hm->evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */
780 evdev_support_event(hm->evdev, EV_SYN);
781 error = hidmap_parse_hid_descr(hm, hidbus_get_index(hm->dev));
783 DPRINTF(hm, "error=%d\n", error);
788 evdev_set_methods(hm->evdev, hm->dev, &hm->evdev_methods);
789 hm->cb_state = HIDMAP_CB_IS_RUNNING;
791 error = evdev_register(hm->evdev);
793 DPRINTF(hm, "error=%d\n", error);
802 hidmap_detach(struct hidmap* hm)
804 struct hidmap_hid_item *hi;
806 DPRINTFN(hm, 11, "\n");
808 hm->cb_state = HIDMAP_CB_IS_DETACHING;
810 evdev_free(hm->evdev);
811 if (hm->hid_items != NULL) {
812 for (hi = hm->hid_items;
813 hi < hm->hid_items + hm->nhid_items;
815 if (hi->type == HIDMAP_TYPE_FINALCB ||
816 hi->type == HIDMAP_TYPE_CALLBACK)
817 hi->cb(hm, hi, (union hidmap_cb_ctx){});
818 else if (hi->type == HIDMAP_TYPE_ARR_LIST)
819 free(hi->codes, M_DEVBUF);
820 free(hm->hid_items, M_DEVBUF);
823 free(hm->key_press, M_DEVBUF);
824 free(hm->key_rel, M_DEVBUF);
829 MODULE_DEPEND(hidmap, hid, 1, 1, 1);
830 MODULE_DEPEND(hidmap, hidbus, 1, 1, 1);
831 MODULE_DEPEND(hidmap, evdev, 1, 1, 1);
832 MODULE_VERSION(hidmap, 1);