The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/hid/hconf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
    5  * Copyright (c) 2020 Andriy Gapon <avg@FreeBSD.org>
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 /*
   33  * Digitizer configuration top-level collection support.
   34  * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections
   35  */
   36 
   37 #include "opt_hid.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/bus.h>
   41 #include <sys/kernel.h>
   42 #include <sys/lock.h>
   43 #include <sys/malloc.h>
   44 #include <sys/module.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/systm.h>
   47 #include <sys/sx.h>
   48 
   49 #define HID_DEBUG_VAR   hconf_debug
   50 #include <dev/hid/hid.h>
   51 #include <dev/hid/hidbus.h>
   52 
   53 #include <dev/hid/hconf.h>
   54 
   55 #ifdef HID_DEBUG
   56 static int hconf_debug = 0;
   57 
   58 static SYSCTL_NODE(_hw_hid, OID_AUTO, hconf, CTLFLAG_RW, 0,
   59     "Digitizer configuration top-level collection");
   60 SYSCTL_INT(_hw_hid_hconf, OID_AUTO, debug, CTLFLAG_RWTUN,
   61     &hconf_debug, 1, "Debug level");
   62 #endif
   63 
   64 enum feature_control_type {
   65         INPUT_MODE = 0,
   66         SURFACE_SWITCH,
   67         BUTTONS_SWITCH,
   68         CONTROLS_COUNT
   69 };
   70 
   71 struct feature_control_descr {
   72         const char      *name;
   73         const char      *descr;
   74         uint16_t        usage;
   75         u_int           value;
   76 } feature_control_descrs[] = {
   77         [INPUT_MODE] = {
   78                 .name = "input_mode",
   79                 .descr = "HID device input mode: 0 = mouse, 3 = touchpad",
   80                 .usage = HUD_INPUT_MODE,
   81                 .value = HCONF_INPUT_MODE_MOUSE,
   82         },
   83         [SURFACE_SWITCH] = {
   84                 .name = "surface_switch",
   85                 .descr = "Enable / disable switch for surface: 1 = on, 0 = off",
   86                 .usage = HUD_SURFACE_SWITCH,
   87                 .value = 1,
   88         },
   89         [BUTTONS_SWITCH] = {
   90                 .name = "buttons_switch",
   91                 .descr = "Enable / disable switch for buttons: 1 = on, 0 = off",
   92                 .usage = HUD_BUTTONS_SWITCH,
   93                 .value = 1,
   94         },
   95 };
   96 
   97 struct feature_control {
   98         u_int                   val;
   99         struct hid_location     loc;
  100         hid_size_t              rlen;
  101         uint8_t                 rid;
  102 };
  103 
  104 struct hconf_softc {
  105         device_t                dev;
  106         struct sx               lock;
  107 
  108         struct feature_control  feature_controls[CONTROLS_COUNT];
  109 };
  110 
  111 static device_probe_t           hconf_probe;
  112 static device_attach_t          hconf_attach;
  113 static device_detach_t          hconf_detach;
  114 static device_resume_t          hconf_resume;
  115 
  116 static device_method_t hconf_methods[] = {
  117 
  118         DEVMETHOD(device_probe,         hconf_probe),
  119         DEVMETHOD(device_attach,        hconf_attach),
  120         DEVMETHOD(device_detach,        hconf_detach),
  121         DEVMETHOD(device_resume,        hconf_resume),
  122 
  123         DEVMETHOD_END
  124 };
  125 
  126 static driver_t hconf_driver = {
  127         .name = "hconf",
  128         .methods = hconf_methods,
  129         .size = sizeof(struct hconf_softc),
  130 };
  131 
  132 static const struct hid_device_id hconf_devs[] = {
  133         { HID_TLC(HUP_DIGITIZERS, HUD_CONFIG) },
  134 };
  135 
  136 static int
  137 hconf_set_feature_control(struct hconf_softc *sc, int ctrl_id, u_int val)
  138 {
  139         struct feature_control *fc;
  140         uint8_t *fbuf;
  141         int error;
  142         int i;
  143 
  144         KASSERT(ctrl_id >= 0 && ctrl_id < CONTROLS_COUNT,
  145             ("impossible ctrl id %d", ctrl_id));
  146         fc = &sc->feature_controls[ctrl_id];
  147         if (fc->rlen <= 1)
  148                 return (ENXIO);
  149 
  150         fbuf = malloc(fc->rlen, M_TEMP, M_WAITOK | M_ZERO);
  151         sx_xlock(&sc->lock);
  152 
  153         /*
  154          * Assume the report is write-only. Then we have to check for other
  155          * controls that may share the same report and set their bits as well.
  156          */
  157         bzero(fbuf + 1, fc->rlen - 1);
  158         for (i = 0; i < nitems(sc->feature_controls); i++) {
  159                 struct feature_control *ofc = &sc->feature_controls[i];
  160 
  161                 /* Skip unrelated report IDs. */
  162                 if (ofc->rid != fc->rid)
  163                         continue;
  164                 KASSERT(fc->rlen == ofc->rlen,
  165                     ("different lengths for report %d: %d vs %d\n",
  166                     fc->rid, fc->rlen, ofc->rlen));
  167                 hid_put_udata(fbuf + 1, ofc->rlen - 1, &ofc->loc,
  168                     i == ctrl_id ? val : ofc->val);
  169         }
  170 
  171         fbuf[0] = fc->rid;
  172 
  173         error = hid_set_report(sc->dev, fbuf, fc->rlen,
  174             HID_FEATURE_REPORT, fc->rid);
  175         if (error == 0)
  176                 fc->val = val;
  177 
  178         sx_unlock(&sc->lock);
  179         free(fbuf, M_TEMP);
  180 
  181         return (error);
  182 }
  183 
  184 static int
  185 hconf_feature_control_handler(SYSCTL_HANDLER_ARGS)
  186 {
  187         struct feature_control *fc;
  188         struct hconf_softc *sc = arg1;
  189         int ctrl_id = arg2;
  190         u_int value;
  191         int error;
  192 
  193         if (ctrl_id < 0 || ctrl_id >= CONTROLS_COUNT)
  194                 return (ENXIO);
  195 
  196         fc = &sc->feature_controls[ctrl_id];
  197         value = fc->val;
  198         error = sysctl_handle_int(oidp, &value, 0, req);
  199         if (error != 0 || req->newptr == NULL)
  200                 return (error);
  201 
  202         error = hconf_set_feature_control(sc, ctrl_id, value);
  203         if (error != 0) {
  204                 DPRINTF("Failed to set %s: %d\n",
  205                     feature_control_descrs[ctrl_id].name, error);
  206         }
  207         return (0);
  208 }
  209 
  210 
  211 static int
  212 hconf_parse_feature(struct feature_control *fc, uint8_t tlc_index,
  213     uint16_t usage, void *d_ptr, hid_size_t d_len)
  214 {
  215         uint32_t flags;
  216 
  217         if (!hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_DIGITIZERS, usage),
  218             hid_feature, tlc_index, 0, &fc->loc, &flags, &fc->rid, NULL))
  219                 return (ENOENT);
  220 
  221         if ((flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE)
  222                 return (EINVAL);
  223 
  224         fc->rlen = hid_report_size(d_ptr, d_len, hid_feature, fc->rid);
  225         return (0);
  226 }
  227 
  228 static int
  229 hconf_probe(device_t dev)
  230 {
  231         int error;
  232 
  233         error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hconf_devs);
  234         if (error != 0)
  235                 return (error);
  236 
  237         hidbus_set_desc(dev, "Configuration");
  238 
  239         return (BUS_PROBE_DEFAULT);
  240 }
  241 
  242 static int
  243 hconf_attach(device_t dev)
  244 {
  245         struct hconf_softc *sc = device_get_softc(dev);
  246         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
  247         struct sysctl_oid *tree = device_get_sysctl_tree(dev);
  248         void *d_ptr;
  249         hid_size_t d_len;
  250         uint8_t tlc_index;
  251         int error;
  252         int i;
  253 
  254         error = hid_get_report_descr(dev, &d_ptr, &d_len);
  255         if (error) {
  256                 device_printf(dev, "could not retrieve report descriptor from "
  257                     "device: %d\n", error);
  258                 return (ENXIO);
  259         }
  260 
  261         sc->dev = dev;
  262         sx_init(&sc->lock, device_get_nameunit(dev));
  263 
  264         tlc_index = hidbus_get_index(dev);
  265         for (i = 0; i < nitems(sc->feature_controls); i++) {
  266                 (void)hconf_parse_feature(&sc->feature_controls[i], tlc_index,
  267                     feature_control_descrs[i].usage, d_ptr, d_len);
  268                 if (sc->feature_controls[i].rlen > 1) {
  269                         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  270                             feature_control_descrs[i].name,
  271                             CTLTYPE_UINT | CTLFLAG_RW,
  272                             sc, i, hconf_feature_control_handler, "I",
  273                             feature_control_descrs[i].descr);
  274                 }
  275                 sc->feature_controls[i].val = feature_control_descrs[i].value;
  276         }
  277 
  278         return (0);
  279 }
  280 
  281 static int
  282 hconf_detach(device_t dev)
  283 {
  284         struct hconf_softc *sc = device_get_softc(dev);
  285 
  286         sx_destroy(&sc->lock);
  287 
  288         return (0);
  289 }
  290 
  291 static int
  292 hconf_resume(device_t dev)
  293 {
  294         struct hconf_softc *sc = device_get_softc(dev);
  295         int error;
  296         int i;
  297 
  298         for (i = 0; i < nitems(sc->feature_controls); i++) {
  299                 if (sc->feature_controls[i].rlen < 2)
  300                         continue;
  301                 /* Do not update usages to default value */
  302                 if (sc->feature_controls[i].val ==
  303                     feature_control_descrs[i].value)
  304                         continue;
  305                 error = hconf_set_feature_control(sc, i,
  306                     sc->feature_controls[i].val);
  307                 if (error != 0) {
  308                         DPRINTF("Failed to restore %s: %d\n",
  309                             feature_control_descrs[i].name, error);
  310                 }
  311         }
  312 
  313         return (0);
  314 }
  315 
  316 int
  317 hconf_set_input_mode(device_t dev, enum hconf_input_mode mode)
  318 {
  319         struct hconf_softc *sc = device_get_softc(dev);
  320 
  321         return (hconf_set_feature_control(sc, INPUT_MODE, mode));
  322 }
  323 
  324 DRIVER_MODULE(hconf, hidbus, hconf_driver, NULL, NULL);
  325 MODULE_DEPEND(hconf, hidbus, 1, 1, 1);
  326 MODULE_DEPEND(hconf, hid, 1, 1, 1);
  327 MODULE_VERSION(hconf, 1);
  328 HID_PNP_INFO(hconf_devs);

Cache object: 53f073d8346dbe41a36a3d2e9744358f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.