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