Vladimir Kondratyev afd590d9e5 hid: Import hidmap and bunch of drivers based on it
hidmap is a kernel module that maps HID input usages to evdev events.

Following dependent drivers is included in the commit:

hms       - HID mouse driver.
hcons     - Consumer page AKA Multimedia keys driver.
hsctrl    - System Controls page (Power/Sleep keys) driver.
ps4dshock - Sony DualShock 4 gamepad driver.

Reviewed by:	hselasky
Differential revision:	https://reviews.freebsd.org/D27993
2021-01-08 02:18:44 +03:00

268 lines
8.0 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf
*/
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <dev/evdev/input.h>
#include <dev/evdev/evdev.h>
#include <dev/hid/hid.h>
#include <dev/hid/hidbus.h>
#include <dev/hid/hidmap.h>
#include <dev/hid/hidquirk.h>
#include <dev/hid/hidrdesc.h>
static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() };
enum {
HMS_REL_X,
HMS_REL_Y,
HMS_REL_Z,
HMS_ABS_X,
HMS_ABS_Y,
HMS_ABS_Z,
HMS_HWHEEL,
HMS_BTN,
HMS_BTN_MS1,
HMS_BTN_MS2,
HMS_FINAL_CB,
};
static hidmap_cb_t hms_final_cb;
#define HMS_MAP_BUT_RG(usage_from, usage_to, code) \
{ HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) }
#define HMS_MAP_BUT_MS(usage, code) \
{ HIDMAP_KEY(HUP_MICROSOFT, usage, code) }
#define HMS_MAP_ABS(usage, code) \
{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) }
#define HMS_MAP_REL(usage, code) \
{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) }
#define HMS_MAP_REL_REV(usage, code) \
{ HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true }
#define HMS_MAP_REL_CN(usage, code) \
{ HIDMAP_REL(HUP_CONSUMER, usage, code) }
#define HMS_FINAL_CB(cb) \
{ HIDMAP_FINAL_CB(&cb) }
static const struct hidmap_item hms_map[] = {
[HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X),
[HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y),
[HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z),
[HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X),
[HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y),
[HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z),
[HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL),
[HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE),
[HMS_BTN_MS1] = HMS_MAP_BUT_MS(1, BTN_RIGHT),
[HMS_BTN_MS2] = HMS_MAP_BUT_MS(2, BTN_MIDDLE),
[HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb),
};
static const struct hidmap_item hms_map_wheel[] = {
HMS_MAP_REL(HUG_WHEEL, REL_WHEEL),
};
static const struct hidmap_item hms_map_wheel_rev[] = {
HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL),
};
/* A match on these entries will load hms */
static const struct hid_device_id hms_devs[] = {
{ HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) },
};
struct hms_softc {
struct hidmap hm;
HIDMAP_CAPS(caps, hms_map);
};
static int
hms_final_cb(HIDMAP_CB_ARGS)
{
struct hms_softc *sc = HIDMAP_CB_GET_SOFTC();
struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) {
if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y))
evdev_support_prop(evdev, INPUT_PROP_DIRECT);
else
evdev_support_prop(evdev, INPUT_PROP_POINTER);
}
/* Do not execute callback at interrupt handler and detach */
return (ENOSYS);
}
static void
hms_identify(driver_t *driver, device_t parent)
{
const struct hid_device_info *hw = hid_get_device_info(parent);
void *d_ptr;
hid_size_t d_len;
int error;
/*
* If device claimed boot protocol support but do not have report
* descriptor, load one defined in "Appendix B.2" of HID1_11.pdf
*/
error = hid_get_report_descr(parent, &d_ptr, &d_len);
if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) ||
(error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) &&
hid_is_mouse(d_ptr, d_len)))
(void)hid_set_report_descr(parent, hms_boot_desc,
sizeof(hms_boot_desc));
}
static int
hms_probe(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
int error;
error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs);
if (error != 0)
return (error);
hidmap_set_dev(&sc->hm, dev);
/* Check if report descriptor belongs to mouse */
error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps);
if (error != 0)
return (error);
/* There should be at least one X or Y axis */
if (!hidmap_test_cap(sc->caps, HMS_REL_X) &&
!hidmap_test_cap(sc->caps, HMS_REL_X) &&
!hidmap_test_cap(sc->caps, HMS_ABS_X) &&
!hidmap_test_cap(sc->caps, HMS_ABS_Y))
return (ENXIO);
if (hidmap_test_cap(sc->caps, HMS_ABS_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y))
hidbus_set_desc(dev, "Tablet");
else
hidbus_set_desc(dev, "Mouse");
return (BUS_PROBE_DEFAULT);
}
static int
hms_attach(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
const struct hid_device_info *hw = hid_get_device_info(dev);
struct hidmap_hid_item *hi;
HIDMAP_CAPS(cap_wheel, hms_map_wheel);
void *d_ptr;
hid_size_t d_len;
bool set_report_proto;
int error, nbuttons = 0;
/*
* Set the report (non-boot) protocol if report descriptor has not been
* overloaded with boot protocol report descriptor.
*
* Mice without boot protocol support may choose not to implement
* Set_Protocol at all; Ignore any error.
*/
error = hid_get_report_descr(dev, &d_ptr, &d_len);
set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) &&
memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0);
(void)hid_set_protocol(dev, set_report_proto ? 1 : 0);
if (hid_test_quirk(hw, HQ_MS_REVZ))
HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel);
else
HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel);
error = hidmap_attach(&sc->hm);
if (error)
return (error);
/* Count number of input usages of variable type mapped to buttons */
for (hi = sc->hm.hid_items;
hi < sc->hm.hid_items + sc->hm.nhid_items;
hi++)
if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY)
nbuttons++;
/* announce information about the mouse */
device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
nbuttons,
(hidmap_test_cap(sc->caps, HMS_REL_X) ||
hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "",
(hidmap_test_cap(sc->caps, HMS_REL_Y) ||
hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "",
(hidmap_test_cap(sc->caps, HMS_REL_Z) ||
hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "",
hidmap_test_cap(cap_wheel, 0) ? "W" : "",
hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "",
sc->hm.hid_items[0].id);
return (0);
}
static int
hms_detach(device_t dev)
{
struct hms_softc *sc = device_get_softc(dev);
return (hidmap_detach(&sc->hm));
}
static devclass_t hms_devclass;
static device_method_t hms_methods[] = {
DEVMETHOD(device_identify, hms_identify),
DEVMETHOD(device_probe, hms_probe),
DEVMETHOD(device_attach, hms_attach),
DEVMETHOD(device_detach, hms_detach),
DEVMETHOD_END
};
DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc));
DRIVER_MODULE(hms, hidbus, hms_driver, hms_devclass, NULL, 0);
MODULE_DEPEND(hms, hid, 1, 1, 1);
MODULE_DEPEND(hms, hidbus, 1, 1, 1);
MODULE_DEPEND(hms, hidmap, 1, 1, 1);
MODULE_DEPEND(hms, evdev, 1, 1, 1);
MODULE_VERSION(hms, 1);
HID_PNP_INFO(hms_devs);