1d97d5c0cSVladimir Kondratyev /*- 2d97d5c0cSVladimir Kondratyev * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3d97d5c0cSVladimir Kondratyev * 4d97d5c0cSVladimir Kondratyev * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org> 5d97d5c0cSVladimir Kondratyev * Copyright (c) 2019 Greg V <greg@unrelenting.technology> 6d97d5c0cSVladimir Kondratyev * 7d97d5c0cSVladimir Kondratyev * Redistribution and use in source and binary forms, with or without 8d97d5c0cSVladimir Kondratyev * modification, are permitted provided that the following conditions 9d97d5c0cSVladimir Kondratyev * are met: 10d97d5c0cSVladimir Kondratyev * 1. Redistributions of source code must retain the above copyright 11d97d5c0cSVladimir Kondratyev * notice, this list of conditions and the following disclaimer. 12d97d5c0cSVladimir Kondratyev * 2. Redistributions in binary form must reproduce the above copyright 13d97d5c0cSVladimir Kondratyev * notice, this list of conditions and the following disclaimer in the 14d97d5c0cSVladimir Kondratyev * documentation and/or other materials provided with the distribution. 15d97d5c0cSVladimir Kondratyev * 16d97d5c0cSVladimir Kondratyev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17d97d5c0cSVladimir Kondratyev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18d97d5c0cSVladimir Kondratyev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19d97d5c0cSVladimir Kondratyev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20d97d5c0cSVladimir Kondratyev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21d97d5c0cSVladimir Kondratyev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22d97d5c0cSVladimir Kondratyev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23d97d5c0cSVladimir Kondratyev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24d97d5c0cSVladimir Kondratyev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25d97d5c0cSVladimir Kondratyev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26d97d5c0cSVladimir Kondratyev * SUCH DAMAGE. 27d97d5c0cSVladimir Kondratyev */ 28d97d5c0cSVladimir Kondratyev 29d97d5c0cSVladimir Kondratyev #include <sys/cdefs.h> 30d97d5c0cSVladimir Kondratyev __FBSDID("$FreeBSD$"); 31d97d5c0cSVladimir Kondratyev 32d97d5c0cSVladimir Kondratyev /* 33d97d5c0cSVladimir Kondratyev * Generic / MS Windows compatible HID pen tablet driver: 34d97d5c0cSVladimir Kondratyev * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/required-hid-top-level-collections 35d97d5c0cSVladimir Kondratyev * 36d97d5c0cSVladimir Kondratyev * Tested on: Wacom WCOM50C1 (Google Pixelbook "eve") 37d97d5c0cSVladimir Kondratyev */ 38d97d5c0cSVladimir Kondratyev 39d97d5c0cSVladimir Kondratyev #include <sys/param.h> 40d97d5c0cSVladimir Kondratyev #include <sys/bus.h> 41d97d5c0cSVladimir Kondratyev #include <sys/kernel.h> 42d97d5c0cSVladimir Kondratyev #include <sys/malloc.h> 43d97d5c0cSVladimir Kondratyev #include <sys/module.h> 44d97d5c0cSVladimir Kondratyev #include <sys/sysctl.h> 45d97d5c0cSVladimir Kondratyev 46d97d5c0cSVladimir Kondratyev #include <dev/evdev/input.h> 47d97d5c0cSVladimir Kondratyev #include <dev/evdev/evdev.h> 48d97d5c0cSVladimir Kondratyev 49d97d5c0cSVladimir Kondratyev #include <dev/hid/hid.h> 50d97d5c0cSVladimir Kondratyev #include <dev/hid/hidbus.h> 51d97d5c0cSVladimir Kondratyev #include <dev/hid/hidmap.h> 52d97d5c0cSVladimir Kondratyev #include <dev/hid/hidrdesc.h> 53d97d5c0cSVladimir Kondratyev 54d97d5c0cSVladimir Kondratyev #include "usbdevs.h" 55d97d5c0cSVladimir Kondratyev 56d97d5c0cSVladimir Kondratyev static const uint8_t hpen_graphire_report_descr[] = 57d97d5c0cSVladimir Kondratyev { HID_GRAPHIRE_REPORT_DESCR() }; 58d97d5c0cSVladimir Kondratyev static const uint8_t hpen_graphire3_4x5_report_descr[] = 59d97d5c0cSVladimir Kondratyev { HID_GRAPHIRE3_4X5_REPORT_DESCR() }; 60d97d5c0cSVladimir Kondratyev 61d97d5c0cSVladimir Kondratyev static hidmap_cb_t hpen_battery_strenght_cb; 62d97d5c0cSVladimir Kondratyev static hidmap_cb_t hpen_final_digi_cb; 63d97d5c0cSVladimir Kondratyev static hidmap_cb_t hpen_final_pen_cb; 64d97d5c0cSVladimir Kondratyev 65d97d5c0cSVladimir Kondratyev #define HPEN_MAP_BUT(usage, code) \ 66d97d5c0cSVladimir Kondratyev HIDMAP_KEY(HUP_DIGITIZERS, HUD_##usage, code) 67d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS(usage, code) \ 68d97d5c0cSVladimir Kondratyev HIDMAP_ABS(HUP_DIGITIZERS, HUD_##usage, code) 69d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS_GD(usage, code) \ 70d97d5c0cSVladimir Kondratyev HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code) 71d97d5c0cSVladimir Kondratyev #define HPEN_MAP_ABS_CB(usage, cb) \ 72d97d5c0cSVladimir Kondratyev HIDMAP_ABS_CB(HUP_DIGITIZERS, HUD_##usage, &cb) 73d97d5c0cSVladimir Kondratyev 74d97d5c0cSVladimir Kondratyev /* Generic map digitizer page map according to hut1_12v2.pdf */ 75*a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_pen[] = { 76d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS_GD(X, ABS_X), .required = true }, 77d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS_GD(Y, ABS_Y), .required = true }, 78d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS( TIP_PRESSURE, ABS_PRESSURE) }, 79d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS( X_TILT, ABS_TILT_X) }, 80d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS( Y_TILT, ABS_TILT_Y) }, 81*a36bdfc2SVladimir Kondratyev { HPEN_MAP_ABS( CONTACTID, 0), .forbidden = true }, 82*a36bdfc2SVladimir Kondratyev { HPEN_MAP_ABS( CONTACTCOUNT, 0), .forbidden = true }, 83d97d5c0cSVladimir Kondratyev { HPEN_MAP_ABS_CB(BATTERY_STRENGTH, hpen_battery_strenght_cb) }, 84d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( TOUCH, BTN_TOUCH) }, 85d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( TIP_SWITCH, BTN_TOUCH) }, 86d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( SEC_TIP_SWITCH, BTN_TOUCH) }, 87d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( BARREL_SWITCH, BTN_STYLUS) }, 88d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( INVERT, BTN_TOOL_RUBBER) }, 89d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( ERASER, BTN_TOUCH) }, 90d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( TABLET_PICK, BTN_STYLUS2) }, 91d97d5c0cSVladimir Kondratyev { HPEN_MAP_BUT( SEC_BARREL_SWITCH,BTN_STYLUS2) }, 92*a36bdfc2SVladimir Kondratyev { HIDMAP_FINAL_CB( &hpen_final_pen_cb) }, 93d97d5c0cSVladimir Kondratyev }; 94d97d5c0cSVladimir Kondratyev 95*a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_stylus[] = { 96*a36bdfc2SVladimir Kondratyev { HPEN_MAP_BUT( IN_RANGE, BTN_TOOL_PEN) }, 97*a36bdfc2SVladimir Kondratyev }; 98*a36bdfc2SVladimir Kondratyev static const struct hidmap_item hpen_map_finger[] = { 99*a36bdfc2SVladimir Kondratyev { HPEN_MAP_BUT( IN_RANGE, BTN_TOOL_FINGER) }, 100d97d5c0cSVladimir Kondratyev }; 101d97d5c0cSVladimir Kondratyev 102d97d5c0cSVladimir Kondratyev static const struct hid_device_id hpen_devs[] = { 103d97d5c0cSVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_DIGITIZER) }, 104d97d5c0cSVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_PEN) }, 105*a36bdfc2SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN), 106*a36bdfc2SVladimir Kondratyev HID_BVP(BUS_USB, USB_VENDOR_EGALAX, USB_PRODUCT_EGALAX_TPANEL) }, 107*a36bdfc2SVladimir Kondratyev }; 108*a36bdfc2SVladimir Kondratyev 109*a36bdfc2SVladimir Kondratyev /* Do not autoload legacy pen driver for all touchscreen */ 110*a36bdfc2SVladimir Kondratyev static const struct hid_device_id hpen_devs_no_load[] = { 111*a36bdfc2SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) }, 112d97d5c0cSVladimir Kondratyev }; 113d97d5c0cSVladimir Kondratyev 114d97d5c0cSVladimir Kondratyev static int 115d97d5c0cSVladimir Kondratyev hpen_battery_strenght_cb(HIDMAP_CB_ARGS) 116d97d5c0cSVladimir Kondratyev { 117d97d5c0cSVladimir Kondratyev struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV(); 118d97d5c0cSVladimir Kondratyev int32_t data; 119d97d5c0cSVladimir Kondratyev 120d97d5c0cSVladimir Kondratyev switch (HIDMAP_CB_GET_STATE()) { 121d97d5c0cSVladimir Kondratyev case HIDMAP_CB_IS_ATTACHING: 122d97d5c0cSVladimir Kondratyev evdev_support_event(evdev, EV_PWR); 123d97d5c0cSVladimir Kondratyev /* TODO */ 124d97d5c0cSVladimir Kondratyev break; 125d97d5c0cSVladimir Kondratyev case HIDMAP_CB_IS_RUNNING: 126d97d5c0cSVladimir Kondratyev data = ctx.data; 127d97d5c0cSVladimir Kondratyev /* TODO */ 12816079c72SRyan Libby break; 12916079c72SRyan Libby default: 13016079c72SRyan Libby break; 131d97d5c0cSVladimir Kondratyev } 132d97d5c0cSVladimir Kondratyev 133d97d5c0cSVladimir Kondratyev return (0); 134d97d5c0cSVladimir Kondratyev } 135d97d5c0cSVladimir Kondratyev 136d97d5c0cSVladimir Kondratyev static int 137d97d5c0cSVladimir Kondratyev hpen_final_pen_cb(HIDMAP_CB_ARGS) 138d97d5c0cSVladimir Kondratyev { 139d97d5c0cSVladimir Kondratyev struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV(); 140d97d5c0cSVladimir Kondratyev 141*a36bdfc2SVladimir Kondratyev if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) { 142*a36bdfc2SVladimir Kondratyev if (hidbus_get_usage(HIDMAP_CB_GET_DEV()) == 143*a36bdfc2SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER)) 144*a36bdfc2SVladimir Kondratyev evdev_support_prop(evdev, INPUT_PROP_POINTER); 145*a36bdfc2SVladimir Kondratyev else 146d97d5c0cSVladimir Kondratyev evdev_support_prop(evdev, INPUT_PROP_DIRECT); 147*a36bdfc2SVladimir Kondratyev } 148d97d5c0cSVladimir Kondratyev 149d97d5c0cSVladimir Kondratyev /* Do not execute callback at interrupt handler and detach */ 150d97d5c0cSVladimir Kondratyev return (ENOSYS); 151d97d5c0cSVladimir Kondratyev } 152d97d5c0cSVladimir Kondratyev 153d97d5c0cSVladimir Kondratyev static void 154d97d5c0cSVladimir Kondratyev hpen_identify(driver_t *driver, device_t parent) 155d97d5c0cSVladimir Kondratyev { 156d97d5c0cSVladimir Kondratyev const struct hid_device_info *hw = hid_get_device_info(parent); 157d97d5c0cSVladimir Kondratyev 158d97d5c0cSVladimir Kondratyev /* the report descriptor for the Wacom Graphire is broken */ 159d97d5c0cSVladimir Kondratyev if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM) { 160d97d5c0cSVladimir Kondratyev switch (hw->idProduct) { 161d97d5c0cSVladimir Kondratyev case USB_PRODUCT_WACOM_GRAPHIRE: 162d97d5c0cSVladimir Kondratyev hid_set_report_descr(parent, 163d97d5c0cSVladimir Kondratyev hpen_graphire_report_descr, 164d97d5c0cSVladimir Kondratyev sizeof(hpen_graphire_report_descr)); 165d97d5c0cSVladimir Kondratyev break; 166d97d5c0cSVladimir Kondratyev 167d97d5c0cSVladimir Kondratyev case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 168d97d5c0cSVladimir Kondratyev hid_set_report_descr(parent, 169d97d5c0cSVladimir Kondratyev hpen_graphire3_4x5_report_descr, 170d97d5c0cSVladimir Kondratyev sizeof(hpen_graphire3_4x5_report_descr)); 171d97d5c0cSVladimir Kondratyev break; 172d97d5c0cSVladimir Kondratyev } 173d97d5c0cSVladimir Kondratyev } 174d97d5c0cSVladimir Kondratyev } 175d97d5c0cSVladimir Kondratyev 176d97d5c0cSVladimir Kondratyev static int 177d97d5c0cSVladimir Kondratyev hpen_probe(device_t dev) 178d97d5c0cSVladimir Kondratyev { 179d97d5c0cSVladimir Kondratyev struct hidmap *hm = device_get_softc(dev); 180*a36bdfc2SVladimir Kondratyev const char *desc; 181*a36bdfc2SVladimir Kondratyev void *d_ptr; 182*a36bdfc2SVladimir Kondratyev hid_size_t d_len; 183d97d5c0cSVladimir Kondratyev int error; 184d97d5c0cSVladimir Kondratyev 185*a36bdfc2SVladimir Kondratyev if (HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs_no_load) != 0) { 186d97d5c0cSVladimir Kondratyev error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hpen_devs); 187d97d5c0cSVladimir Kondratyev if (error != 0) 188d97d5c0cSVladimir Kondratyev return (error); 189*a36bdfc2SVladimir Kondratyev } 190d97d5c0cSVladimir Kondratyev 191d97d5c0cSVladimir Kondratyev hidmap_set_dev(hm, dev); 192d97d5c0cSVladimir Kondratyev 193*a36bdfc2SVladimir Kondratyev /* Check if report descriptor belongs to a HID pen device */ 194*a36bdfc2SVladimir Kondratyev error = HIDMAP_ADD_MAP(hm, hpen_map_pen, NULL); 195d97d5c0cSVladimir Kondratyev if (error != 0) 196d97d5c0cSVladimir Kondratyev return (error); 197d97d5c0cSVladimir Kondratyev 198*a36bdfc2SVladimir Kondratyev if (hid_get_report_descr(dev, &d_ptr, &d_len) != 0) 199*a36bdfc2SVladimir Kondratyev return (ENXIO); 200*a36bdfc2SVladimir Kondratyev 201*a36bdfc2SVladimir Kondratyev if (hidbus_is_collection(d_ptr, d_len, 202*a36bdfc2SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER), hidbus_get_index(dev))) { 203*a36bdfc2SVladimir Kondratyev HIDMAP_ADD_MAP(hm, hpen_map_finger, NULL); 204*a36bdfc2SVladimir Kondratyev desc = "TouchScreen"; 205*a36bdfc2SVladimir Kondratyev } else { 206*a36bdfc2SVladimir Kondratyev HIDMAP_ADD_MAP(hm, hpen_map_stylus, NULL); 207*a36bdfc2SVladimir Kondratyev desc = "Pen"; 208*a36bdfc2SVladimir Kondratyev } 209*a36bdfc2SVladimir Kondratyev if (hidbus_get_usage(dev) == HID_USAGE2(HUP_DIGITIZERS, HUD_DIGITIZER)) 210*a36bdfc2SVladimir Kondratyev desc = "Digitizer"; 211*a36bdfc2SVladimir Kondratyev 212*a36bdfc2SVladimir Kondratyev hidbus_set_desc(dev, desc); 213d97d5c0cSVladimir Kondratyev 214d97d5c0cSVladimir Kondratyev return (BUS_PROBE_DEFAULT); 215d97d5c0cSVladimir Kondratyev } 216d97d5c0cSVladimir Kondratyev 217d97d5c0cSVladimir Kondratyev static int 218d97d5c0cSVladimir Kondratyev hpen_attach(device_t dev) 219d97d5c0cSVladimir Kondratyev { 220d97d5c0cSVladimir Kondratyev const struct hid_device_info *hw = hid_get_device_info(dev); 221d97d5c0cSVladimir Kondratyev struct hidmap *hm = device_get_softc(dev); 222d97d5c0cSVladimir Kondratyev int error; 223d97d5c0cSVladimir Kondratyev 224d97d5c0cSVladimir Kondratyev if (hw->idBus == BUS_USB && hw->idVendor == USB_VENDOR_WACOM && 225d97d5c0cSVladimir Kondratyev hw->idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { 226d97d5c0cSVladimir Kondratyev /* 227d97d5c0cSVladimir Kondratyev * The Graphire3 needs 0x0202 to be written to 228d97d5c0cSVladimir Kondratyev * feature report ID 2 before it'll start 229d97d5c0cSVladimir Kondratyev * returning digitizer data. 230d97d5c0cSVladimir Kondratyev */ 231d97d5c0cSVladimir Kondratyev static const uint8_t reportbuf[3] = {2, 2, 2}; 232d97d5c0cSVladimir Kondratyev error = hid_set_report(dev, reportbuf, sizeof(reportbuf), 233d97d5c0cSVladimir Kondratyev HID_FEATURE_REPORT, reportbuf[0]); 234d97d5c0cSVladimir Kondratyev if (error) 235d97d5c0cSVladimir Kondratyev device_printf(dev, "set feature report failed, " 236d97d5c0cSVladimir Kondratyev "error=%d (ignored)\n", error); 237d97d5c0cSVladimir Kondratyev } 238d97d5c0cSVladimir Kondratyev 239d97d5c0cSVladimir Kondratyev return (hidmap_attach(hm)); 240d97d5c0cSVladimir Kondratyev } 241d97d5c0cSVladimir Kondratyev 242d97d5c0cSVladimir Kondratyev static int 243d97d5c0cSVladimir Kondratyev hpen_detach(device_t dev) 244d97d5c0cSVladimir Kondratyev { 245d97d5c0cSVladimir Kondratyev return (hidmap_detach(device_get_softc(dev))); 246d97d5c0cSVladimir Kondratyev } 247d97d5c0cSVladimir Kondratyev 248d97d5c0cSVladimir Kondratyev 249d97d5c0cSVladimir Kondratyev static devclass_t hpen_devclass; 250d97d5c0cSVladimir Kondratyev static device_method_t hpen_methods[] = { 251d97d5c0cSVladimir Kondratyev DEVMETHOD(device_identify, hpen_identify), 252d97d5c0cSVladimir Kondratyev DEVMETHOD(device_probe, hpen_probe), 253d97d5c0cSVladimir Kondratyev DEVMETHOD(device_attach, hpen_attach), 254d97d5c0cSVladimir Kondratyev DEVMETHOD(device_detach, hpen_detach), 255d97d5c0cSVladimir Kondratyev 256d97d5c0cSVladimir Kondratyev DEVMETHOD_END 257d97d5c0cSVladimir Kondratyev }; 258d97d5c0cSVladimir Kondratyev 259d97d5c0cSVladimir Kondratyev DEFINE_CLASS_0(hpen, hpen_driver, hpen_methods, sizeof(struct hidmap)); 260d97d5c0cSVladimir Kondratyev DRIVER_MODULE(hpen, hidbus, hpen_driver, hpen_devclass, NULL, 0); 261d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hid, 1, 1, 1); 262d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hidbus, 1, 1, 1); 263d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, hidmap, 1, 1, 1); 264d97d5c0cSVladimir Kondratyev MODULE_DEPEND(hpen, evdev, 1, 1, 1); 265d97d5c0cSVladimir Kondratyev MODULE_VERSION(hpen, 1); 266d97d5c0cSVladimir Kondratyev HID_PNP_INFO(hpen_devs); 267