Home | History | Annotate | Line # | Download | only in usb
      1 /*	$NetBSD: uhid.c,v 1.131 2025/12/13 12:22:20 jmcneill Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2004, 2008, 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna23.net).
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.131 2025/12/13 12:22:20 jmcneill Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_compat_netbsd.h"
     42 #include "opt_hid.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/types.h>
     47 
     48 #include <sys/compat_stub.h>
     49 #include <sys/conf.h>
     50 #include <sys/device.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/kernel.h>
     53 #include <sys/kmem.h>
     54 #include <sys/proc.h>
     55 #include <sys/systm.h>
     56 #include <sys/tty.h>
     57 #include <sys/container_of.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbhid.h>
     61 
     62 #include <dev/usb/usbdevs.h>
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usb_quirks.h>
     66 #include <dev/hid/hid.h>
     67 #include <dev/hid/uhid.h>
     68 
     69 #include <dev/usb/uhidev.h>
     70 
     71 #include "ioconf.h"
     72 
     73 #ifdef UHID_DEBUG
     74 #define DPRINTF(x)	if (uhiddebug) printf x
     75 #define DPRINTFN(n,x)	if (uhiddebug>(n)) printf x
     76 int	uhiddebug = 0;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n,x)
     80 #endif
     81 
     82 struct uhid_usb_softc {
     83 	struct uhid_softc sc_base;
     84 	struct uhidev *sc_hdev;
     85 	struct usbd_device *sc_udev;
     86 };
     87 
     88 static int	uhid_match(device_t, cfdata_t, void *);
     89 static void	uhid_attach(device_t, device_t, void *);
     90 static int	uhid_detach(device_t, int);
     91 
     92 static int	uhid_usb_ioctl(struct uhid_softc *, u_long, void *, int,
     93 			       struct lwp *);
     94 
     95 CFATTACH_DECL_NEW(uhid_usb, sizeof(struct uhid_usb_softc),
     96     uhid_match, uhid_attach, uhid_detach, NULL);
     97 
     98 static int
     99 uhid_match(device_t parent, cfdata_t match, void *aux)
    100 {
    101 #ifdef UHID_DEBUG
    102 	struct uhidev_attach_arg *uha = aux;
    103 #endif
    104 
    105 	DPRINTF(("uhid_match: report=%d\n", uha->reportid));
    106 
    107 	if (match->cf_flags & 1)
    108 		return UMATCH_HIGHEST;
    109 	else
    110 		return UMATCH_IFACECLASS_GENERIC;
    111 }
    112 
    113 static void
    114 uhid_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct uhid_usb_softc *usc = device_private(self);
    117 	struct uhid_softc *sc = &usc->sc_base;
    118 	struct uhidev_attach_arg *uha = aux;
    119 
    120 	sc->sc_dev = self;
    121 	sc->sc_report_id = uha->reportid;
    122 	sc->sc_ioctl = uhid_usb_ioctl;
    123 	sc->sc_hidev = uha->hidev;
    124 	usc->sc_hdev = uha->parent;
    125 	usc->sc_udev = uha->uiaa->uiaa_device;
    126 
    127 	uhid_attach_common(sc);
    128 }
    129 
    130 static int
    131 uhid_detach(device_t self, int flags)
    132 {
    133 	struct uhid_usb_softc *usc = device_private(self);
    134 	struct uhid_softc *sc = &usc->sc_base;
    135 
    136 	return uhid_detach_common(sc);
    137 }
    138 
    139 static int
    140 uhid_usb_ioctl(struct uhid_softc *sc, u_long cmd, void *addr, int flag,
    141     struct lwp *l)
    142 {
    143 	struct uhid_usb_softc *usc =
    144 	    container_of(sc, struct uhid_usb_softc, sc_base);
    145 	int size;
    146 	usbd_status err;
    147 
    148 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    149 
    150 	switch (cmd) {
    151 	case USB_GET_DEVICE_DESC:
    152 		*(usb_device_descriptor_t *)addr =
    153 			*usbd_get_device_descriptor(usc->sc_udev);
    154 		break;
    155 
    156 	case USB_GET_DEVICEINFO:
    157 		usbd_fill_deviceinfo(usc->sc_udev,
    158 				     (struct usb_device_info *)addr, 0);
    159 		break;
    160 	case USB_GET_DEVICEINFO_30:
    161 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
    162                     (usc->sc_udev,
    163 		      (struct usb_device_info30 *)addr, 0,
    164                       usbd_devinfo_vp, usbd_printBCD),
    165                     enosys(), err);
    166 		if (err == 0)
    167 			return 0;
    168 		break;
    169 	case USB_GET_STRING_DESC:
    170 	    {
    171 		struct usb_string_desc *si = (struct usb_string_desc *)addr;
    172 		err = usbd_get_string_desc(usc->sc_udev,
    173 			si->usd_string_index,
    174 			si->usd_language_id, &si->usd_desc, &size);
    175 		if (err)
    176 			return EINVAL;
    177 		break;
    178 	    }
    179 	default:
    180 		return EINVAL;
    181 	}
    182 
    183 	return 0;
    184 }
    185