1 /* $OpenBSD: ums.c,v 1.45 2020/08/23 11:08:02 mglocker Exp $ */ 2 /* $NetBSD: ums.c,v 1.60 2003/03/11 16:44:00 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/device.h> 42 #include <sys/ioctl.h> 43 44 #include <dev/usb/usb.h> 45 #include <dev/usb/usbhid.h> 46 47 #include <dev/usb/usbdi.h> 48 #include <dev/usb/usbdi_util.h> 49 #include <dev/usb/usbdevs.h> 50 #include <dev/usb/usb_quirks.h> 51 #include <dev/usb/uhidev.h> 52 53 #include <dev/wscons/wsconsio.h> 54 #include <dev/wscons/wsmousevar.h> 55 56 #include <dev/hid/hidmsvar.h> 57 58 struct ums_softc { 59 struct uhidev sc_hdev; 60 struct hidms sc_ms; 61 }; 62 63 void ums_intr(struct uhidev *addr, void *ibuf, u_int len); 64 65 int ums_enable(void *); 66 void ums_disable(void *); 67 int ums_ioctl(void *, u_long, caddr_t, int, struct proc *); 68 void ums_fix_elecom_descriptor(struct ums_softc *, void *, int, int); 69 70 const struct wsmouse_accessops ums_accessops = { 71 ums_enable, 72 ums_ioctl, 73 ums_disable, 74 }; 75 76 int ums_match(struct device *, void *, void *); 77 void ums_attach(struct device *, struct device *, void *); 78 int ums_detach(struct device *, int); 79 80 struct cfdriver ums_cd = { 81 NULL, "ums", DV_DULL 82 }; 83 84 const struct cfattach ums_ca = { 85 sizeof(struct ums_softc), ums_match, ums_attach, ums_detach 86 }; 87 88 int 89 ums_match(struct device *parent, void *match, void *aux) 90 { 91 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 92 int size; 93 void *desc; 94 95 uhidev_get_report_desc(uha->parent, &desc, &size); 96 97 if (hid_is_collection(desc, size, uha->reportid, 98 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER))) 99 return (UMATCH_IFACECLASS); 100 101 if (hid_is_collection(desc, size, uha->reportid, 102 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) 103 return (UMATCH_IFACECLASS); 104 105 if (hid_is_collection(desc, size, uha->reportid, 106 HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN))) 107 return (UMATCH_IFACECLASS); 108 109 if (hid_is_collection(desc, size, uha->reportid, 110 HID_USAGE2(HUP_DIGITIZERS, HUD_PEN))) 111 return (UMATCH_IFACECLASS); 112 113 return (UMATCH_NONE); 114 } 115 116 void 117 ums_attach(struct device *parent, struct device *self, void *aux) 118 { 119 struct ums_softc *sc = (struct ums_softc *)self; 120 struct hidms *ms = &sc->sc_ms; 121 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 122 struct usb_attach_arg *uaa = uha->uaa; 123 int size, repid; 124 void *desc; 125 u_int32_t quirks, qflags = 0; 126 127 sc->sc_hdev.sc_intr = ums_intr; 128 sc->sc_hdev.sc_parent = uha->parent; 129 sc->sc_hdev.sc_udev = uaa->device; 130 sc->sc_hdev.sc_report_id = uha->reportid; 131 132 usbd_set_idle(uha->parent->sc_udev, uha->parent->sc_ifaceno, 0, 0); 133 134 quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags; 135 uhidev_get_report_desc(uha->parent, &desc, &size); 136 137 if (uaa->vendor == USB_VENDOR_ELECOM) 138 ums_fix_elecom_descriptor(sc, desc, size, uaa->product); 139 140 repid = uha->reportid; 141 sc->sc_hdev.sc_isize = hid_report_size(desc, size, hid_input, repid); 142 sc->sc_hdev.sc_osize = hid_report_size(desc, size, hid_output, repid); 143 sc->sc_hdev.sc_fsize = hid_report_size(desc, size, hid_feature, repid); 144 145 if (quirks & UQ_MS_REVZ) 146 qflags |= HIDMS_REVZ; 147 if (quirks & UQ_SPUR_BUT_UP) 148 qflags |= HIDMS_SPUR_BUT_UP; 149 if (quirks & UQ_MS_BAD_CLASS) 150 qflags |= HIDMS_MS_BAD_CLASS; 151 if (quirks & UQ_MS_LEADING_BYTE) 152 qflags |= HIDMS_LEADINGBYTE; 153 154 if (hidms_setup(self, ms, qflags, uha->reportid, desc, size) != 0) 155 return; 156 157 /* 158 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has 159 * five Report IDs: 19, 23, 24, 17, 18 (in the order they appear in 160 * report descriptor), it seems that report 17 contains the necessary 161 * mouse information (3-buttons, X, Y, wheel) so we specify it 162 * manually. 163 */ 164 if (uaa->vendor == USB_VENDOR_MICROSOFT && 165 uaa->product == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3) { 166 ms->sc_flags = HIDMS_Z; 167 ms->sc_num_buttons = 3; 168 /* XXX change sc_hdev isize to 5? */ 169 ms->sc_loc_x.pos = 8; 170 ms->sc_loc_y.pos = 16; 171 ms->sc_loc_z.pos = 24; 172 ms->sc_loc_btn[0].pos = 0; 173 ms->sc_loc_btn[1].pos = 1; 174 ms->sc_loc_btn[2].pos = 2; 175 } 176 177 hidms_attach(ms, &ums_accessops); 178 } 179 180 int 181 ums_detach(struct device *self, int flags) 182 { 183 struct ums_softc *sc = (struct ums_softc *)self; 184 struct hidms *ms = &sc->sc_ms; 185 186 return hidms_detach(ms, flags); 187 } 188 189 void 190 ums_intr(struct uhidev *addr, void *buf, u_int len) 191 { 192 struct ums_softc *sc = (struct ums_softc *)addr; 193 struct hidms *ms = &sc->sc_ms; 194 195 if (ms->sc_enabled != 0) 196 hidms_input(ms, (uint8_t *)buf, len); 197 } 198 199 int 200 ums_enable(void *v) 201 { 202 struct ums_softc *sc = v; 203 struct hidms *ms = &sc->sc_ms; 204 int rv; 205 206 if (usbd_is_dying(sc->sc_hdev.sc_udev)) 207 return EIO; 208 209 if ((rv = hidms_enable(ms)) != 0) 210 return rv; 211 212 return uhidev_open(&sc->sc_hdev); 213 } 214 215 void 216 ums_disable(void *v) 217 { 218 struct ums_softc *sc = v; 219 struct hidms *ms = &sc->sc_ms; 220 221 hidms_disable(ms); 222 uhidev_close(&sc->sc_hdev); 223 } 224 225 int 226 ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 227 { 228 struct ums_softc *sc = v; 229 struct hidms *ms = &sc->sc_ms; 230 int rc; 231 232 rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p); 233 if (rc != -1) 234 return rc; 235 rc = hidms_ioctl(ms, cmd, data, flag, p); 236 if (rc != -1) 237 return rc; 238 239 switch (cmd) { 240 case WSMOUSEIO_GTYPE: 241 *(u_int *)data = WSMOUSE_TYPE_USB; 242 return 0; 243 default: 244 return -1; 245 } 246 } 247 248 /* 249 * Some ELECOM devices present flawed report descriptors. Instead of a 5-bit 250 * field and a 3-bit padding as defined by the descriptor they actually use 251 * 6 or 8 Bits to report button states, see 252 * https://flameeyes.blog/2017/04/24/elecom-deft-and-the-broken-descriptor 253 * This function adapts the Report Count value for the button page, its Usage 254 * Maximum and the report size for the padding bits (at offset 31). 255 */ 256 void 257 ums_fix_elecom_descriptor(struct ums_softc *sc, void *desc, int size, 258 int product) 259 { 260 static uByte match[] = { /* a descriptor fragment, offset: 12 */ 261 0x95, 0x05, 0x75, 0x01, /* Report Count (5), Report Size (1) */ 262 0x05, 0x09, 0x19, 0x01, /* Usage Page (Button), Usage Minimum (1) */ 263 0x29, 0x05, /* Usage Maximum (5) */ 264 }; 265 uByte *udesc = desc; 266 int nbuttons; 267 268 switch (product) { 269 case USB_PRODUCT_ELECOM_MXT3URBK: /* EX-G Trackballs */ 270 case USB_PRODUCT_ELECOM_MXT3DRBK: 271 case USB_PRODUCT_ELECOM_MXT4DRBK: 272 nbuttons = 6; 273 break; 274 case USB_PRODUCT_ELECOM_MDT1URBK: /* DEFT Trackballs */ 275 case USB_PRODUCT_ELECOM_MDT1DRBK: 276 case USB_PRODUCT_ELECOM_MHT1URBK: /* HUGE Trackballs */ 277 case USB_PRODUCT_ELECOM_MHT1DRBK: 278 nbuttons = 8; 279 break; 280 default: 281 return; 282 } 283 284 if (udesc == NULL || size < 32 285 || memcmp(&udesc[12], match, sizeof(match)) 286 || udesc[30] != 0x75 || udesc[31] != 3) 287 return; 288 289 printf("%s: fixing Elecom report descriptor (buttons: %d)\n", 290 sc->sc_hdev.sc_dev.dv_xname, nbuttons); 291 udesc[13] = nbuttons; 292 udesc[21] = nbuttons; 293 udesc[31] = 8 - nbuttons; 294 } 295