| Commit | Line | Data |
|---|---|---|
| 12bd3c8b SW |
1 | /* $NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $ */ |
| 2 | ||
| 3 | /* Also already merged from NetBSD: | |
| 4 | * $NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $ | |
| 5 | */ | |
| 6 | ||
| 12bd3c8b SW |
7 | /*- |
| 8 | * Copyright (c) 1998 The NetBSD Foundation, Inc. | |
| 9 | * All rights reserved. | |
| 10 | * | |
| 11 | * This code is derived from software contributed to The NetBSD Foundation | |
| 12 | * by Lennart Augustsson (lennart@augustsson.net) at | |
| 13 | * Carlstedt Research & Technology. | |
| 14 | * | |
| 15 | * Redistribution and use in source and binary forms, with or without | |
| 16 | * modification, are permitted provided that the following conditions | |
| 17 | * are met: | |
| 18 | * 1. Redistributions of source code must retain the above copyright | |
| 19 | * notice, this list of conditions and the following disclaimer. | |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 21 | * notice, this list of conditions and the following disclaimer in the | |
| 22 | * documentation and/or other materials provided with the distribution. | |
| 23 | * | |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
| 25 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| 26 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
| 28 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 34 | * POSSIBILITY OF SUCH DAMAGE. | |
| 35 | */ | |
| 36 | ||
| 37 | /* | |
| 38 | * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf | |
| 39 | */ | |
| 40 | ||
| 41 | #include <sys/stdint.h> | |
| 12bd3c8b SW |
42 | #include <sys/param.h> |
| 43 | #include <sys/queue.h> | |
| 44 | #include <sys/types.h> | |
| 45 | #include <sys/systm.h> | |
| 46 | #include <sys/kernel.h> | |
| 47 | #include <sys/bus.h> | |
| 48 | #include <sys/module.h> | |
| 49 | #include <sys/lock.h> | |
| 12bd3c8b SW |
50 | #include <sys/condvar.h> |
| 51 | #include <sys/sysctl.h> | |
| 12bd3c8b SW |
52 | #include <sys/unistd.h> |
| 53 | #include <sys/callout.h> | |
| 54 | #include <sys/malloc.h> | |
| 55 | #include <sys/priv.h> | |
| 56 | #include <sys/conf.h> | |
| 57 | #include <sys/fcntl.h> | |
| 58 | ||
| 8d7664cb SW |
59 | #include <bus/u4b/usbdevs.h> |
| 60 | #include <bus/u4b/usb.h> | |
| 61 | #include <bus/u4b/usbdi.h> | |
| 62 | #include <bus/u4b/usbdi_util.h> | |
| 63 | #include <bus/u4b/usbhid.h> | |
| 64 | #include <bus/u4b/usb_ioctl.h> | |
| 12bd3c8b SW |
65 | |
| 66 | #define USB_DEBUG_VAR uhid_debug | |
| 8d7664cb | 67 | #include <bus/u4b/usb_debug.h> |
| 12bd3c8b | 68 | |
| 8d7664cb SW |
69 | #include <bus/u4b/input/usb_rdesc.h> |
| 70 | #include <bus/u4b/quirk/usb_quirk.h> | |
| 12bd3c8b SW |
71 | |
| 72 | #ifdef USB_DEBUG | |
| 73 | static int uhid_debug = 0; | |
| 74 | ||
| 75 | static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid"); | |
| 76 | SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW, | |
| 77 | &uhid_debug, 0, "Debug level"); | |
| 78 | #endif | |
| 79 | ||
| 80 | #define UHID_BSIZE 1024 /* bytes, buffer size */ | |
| 81 | #define UHID_FRAME_NUM 50 /* bytes, frame number */ | |
| 82 | ||
| 83 | enum { | |
| 84 | UHID_INTR_DT_RD, | |
| 85 | UHID_CTRL_DT_WR, | |
| 86 | UHID_CTRL_DT_RD, | |
| 87 | UHID_N_TRANSFER, | |
| 88 | }; | |
| 89 | ||
| 90 | struct uhid_softc { | |
| 91 | struct usb_fifo_sc sc_fifo; | |
| 8d7664cb | 92 | struct lock sc_lock; |
| 12bd3c8b SW |
93 | |
| 94 | struct usb_xfer *sc_xfer[UHID_N_TRANSFER]; | |
| 95 | struct usb_device *sc_udev; | |
| 96 | void *sc_repdesc_ptr; | |
| 97 | ||
| 98 | uint32_t sc_isize; | |
| 99 | uint32_t sc_osize; | |
| 100 | uint32_t sc_fsize; | |
| 101 | ||
| 102 | uint16_t sc_repdesc_size; | |
| 103 | ||
| 104 | uint8_t sc_iface_no; | |
| 105 | uint8_t sc_iface_index; | |
| 106 | uint8_t sc_iid; | |
| 107 | uint8_t sc_oid; | |
| 108 | uint8_t sc_fid; | |
| 109 | uint8_t sc_flags; | |
| 110 | #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */ | |
| 111 | #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are | |
| 112 | * static */ | |
| 113 | }; | |
| 114 | ||
| 115 | static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()}; | |
| 116 | static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()}; | |
| 117 | static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()}; | |
| 118 | ||
| 119 | /* prototypes */ | |
| 120 | ||
| 121 | static device_probe_t uhid_probe; | |
| 122 | static device_attach_t uhid_attach; | |
| 123 | static device_detach_t uhid_detach; | |
| 124 | ||
| 125 | static usb_callback_t uhid_intr_callback; | |
| 126 | static usb_callback_t uhid_write_callback; | |
| 127 | static usb_callback_t uhid_read_callback; | |
| 128 | ||
| 129 | static usb_fifo_cmd_t uhid_start_read; | |
| 130 | static usb_fifo_cmd_t uhid_stop_read; | |
| 131 | static usb_fifo_cmd_t uhid_start_write; | |
| 132 | static usb_fifo_cmd_t uhid_stop_write; | |
| 133 | static usb_fifo_open_t uhid_open; | |
| 134 | static usb_fifo_close_t uhid_close; | |
| 135 | static usb_fifo_ioctl_t uhid_ioctl; | |
| 136 | ||
| 137 | static struct usb_fifo_methods uhid_fifo_methods = { | |
| 138 | .f_open = &uhid_open, | |
| 139 | .f_close = &uhid_close, | |
| 140 | .f_ioctl = &uhid_ioctl, | |
| 141 | .f_start_read = &uhid_start_read, | |
| 142 | .f_stop_read = &uhid_stop_read, | |
| 143 | .f_start_write = &uhid_start_write, | |
| 144 | .f_stop_write = &uhid_stop_write, | |
| 145 | .basename[0] = "uhid", | |
| 146 | }; | |
| 147 | ||
| 148 | static void | |
| 149 | uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 150 | { | |
| 151 | struct uhid_softc *sc = usbd_xfer_softc(xfer); | |
| 152 | struct usb_page_cache *pc; | |
| 153 | int actlen; | |
| 154 | ||
| 155 | usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); | |
| 156 | ||
| 157 | switch (USB_GET_STATE(xfer)) { | |
| 158 | case USB_ST_TRANSFERRED: | |
| 159 | DPRINTF("transferred!\n"); | |
| 160 | ||
| 161 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 162 | ||
| 163 | /* | |
| 164 | * If the ID byte is non zero we allow descriptors | |
| 165 | * having multiple sizes: | |
| 166 | */ | |
| 167 | if ((actlen >= sc->sc_isize) || | |
| 168 | ((actlen > 0) && (sc->sc_iid != 0))) { | |
| 169 | /* limit report length to the maximum */ | |
| 170 | if (actlen > sc->sc_isize) | |
| 171 | actlen = sc->sc_isize; | |
| 172 | usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, | |
| 173 | 0, actlen, 1); | |
| 174 | } else { | |
| 175 | /* ignore it */ | |
| 176 | DPRINTF("ignored transfer, %d bytes\n", actlen); | |
| 177 | } | |
| 178 | ||
| 179 | case USB_ST_SETUP: | |
| 180 | re_submit: | |
| 181 | if (usb_fifo_put_bytes_max( | |
| 182 | sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { | |
| 183 | usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize); | |
| 184 | usbd_transfer_submit(xfer); | |
| 185 | } | |
| 186 | return; | |
| 187 | ||
| 188 | default: /* Error */ | |
| 189 | if (error != USB_ERR_CANCELLED) { | |
| 190 | /* try to clear stall first */ | |
| 191 | usbd_xfer_set_stall(xfer); | |
| 192 | goto re_submit; | |
| 193 | } | |
| 194 | return; | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | static void | |
| 199 | uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no, | |
| 200 | uint8_t type, uint8_t id, uint16_t size) | |
| 201 | { | |
| 202 | req->bmRequestType = UT_WRITE_CLASS_INTERFACE; | |
| 203 | req->bRequest = UR_SET_REPORT; | |
| 204 | USETW2(req->wValue, type, id); | |
| 205 | req->wIndex[0] = iface_no; | |
| 206 | req->wIndex[1] = 0; | |
| 207 | USETW(req->wLength, size); | |
| 208 | } | |
| 209 | ||
| 210 | static void | |
| 211 | uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no, | |
| 212 | uint8_t type, uint8_t id, uint16_t size) | |
| 213 | { | |
| 214 | req->bmRequestType = UT_READ_CLASS_INTERFACE; | |
| 215 | req->bRequest = UR_GET_REPORT; | |
| 216 | USETW2(req->wValue, type, id); | |
| 217 | req->wIndex[0] = iface_no; | |
| 218 | req->wIndex[1] = 0; | |
| 219 | USETW(req->wLength, size); | |
| 220 | } | |
| 221 | ||
| 222 | static void | |
| 223 | uhid_write_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 224 | { | |
| 225 | struct uhid_softc *sc = usbd_xfer_softc(xfer); | |
| 226 | struct usb_device_request req; | |
| 227 | struct usb_page_cache *pc; | |
| 228 | uint32_t size = sc->sc_osize; | |
| 229 | uint32_t actlen; | |
| 230 | uint8_t id; | |
| 231 | ||
| 232 | switch (USB_GET_STATE(xfer)) { | |
| 233 | case USB_ST_TRANSFERRED: | |
| 234 | case USB_ST_SETUP: | |
| 235 | /* try to extract the ID byte */ | |
| 236 | if (sc->sc_oid) { | |
| 237 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 238 | if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, | |
| 239 | 0, 1, &actlen, 0)) { | |
| 240 | if (actlen != 1) { | |
| 241 | goto tr_error; | |
| 242 | } | |
| 243 | usbd_copy_out(pc, 0, &id, 1); | |
| 244 | ||
| 245 | } else { | |
| 246 | return; | |
| 247 | } | |
| 248 | if (size) { | |
| 249 | size--; | |
| 250 | } | |
| 251 | } else { | |
| 252 | id = 0; | |
| 253 | } | |
| 254 | ||
| 255 | pc = usbd_xfer_get_frame(xfer, 1); | |
| 256 | if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, | |
| 257 | 0, UHID_BSIZE, &actlen, 1)) { | |
| 258 | if (actlen != size) { | |
| 259 | goto tr_error; | |
| 260 | } | |
| 261 | uhid_fill_set_report | |
| 262 | (&req, sc->sc_iface_no, | |
| 263 | UHID_OUTPUT_REPORT, id, size); | |
| 264 | ||
| 265 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 266 | usbd_copy_in(pc, 0, &req, sizeof(req)); | |
| 267 | ||
| 268 | usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); | |
| 269 | usbd_xfer_set_frame_len(xfer, 1, size); | |
| 270 | usbd_xfer_set_frames(xfer, size ? 2 : 1); | |
| 271 | usbd_transfer_submit(xfer); | |
| 272 | } | |
| 273 | return; | |
| 274 | ||
| 275 | default: | |
| 276 | tr_error: | |
| 277 | /* bomb out */ | |
| 278 | usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]); | |
| 279 | return; | |
| 280 | } | |
| 281 | } | |
| 282 | ||
| 283 | static void | |
| 284 | uhid_read_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 285 | { | |
| 286 | struct uhid_softc *sc = usbd_xfer_softc(xfer); | |
| 287 | struct usb_device_request req; | |
| 288 | struct usb_page_cache *pc; | |
| 289 | ||
| 290 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 291 | ||
| 292 | switch (USB_GET_STATE(xfer)) { | |
| 293 | case USB_ST_TRANSFERRED: | |
| 294 | usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req), | |
| 295 | sc->sc_isize, 1); | |
| 296 | return; | |
| 297 | ||
| 298 | case USB_ST_SETUP: | |
| 299 | ||
| 300 | if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) { | |
| 301 | ||
| 302 | uhid_fill_get_report | |
| 303 | (&req, sc->sc_iface_no, UHID_INPUT_REPORT, | |
| 304 | sc->sc_iid, sc->sc_isize); | |
| 305 | ||
| 306 | usbd_copy_in(pc, 0, &req, sizeof(req)); | |
| 307 | ||
| 308 | usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); | |
| 309 | usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize); | |
| 310 | usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1); | |
| 311 | usbd_transfer_submit(xfer); | |
| 312 | } | |
| 313 | return; | |
| 314 | ||
| 315 | default: /* Error */ | |
| 316 | /* bomb out */ | |
| 317 | usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]); | |
| 318 | return; | |
| 319 | } | |
| 320 | } | |
| 321 | ||
| 322 | static const struct usb_config uhid_config[UHID_N_TRANSFER] = { | |
| 323 | ||
| 324 | [UHID_INTR_DT_RD] = { | |
| 325 | .type = UE_INTERRUPT, | |
| 326 | .endpoint = UE_ADDR_ANY, | |
| 327 | .direction = UE_DIR_IN, | |
| 328 | .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, | |
| 329 | .bufsize = UHID_BSIZE, | |
| 330 | .callback = &uhid_intr_callback, | |
| 331 | }, | |
| 332 | ||
| 333 | [UHID_CTRL_DT_WR] = { | |
| 334 | .type = UE_CONTROL, | |
| 335 | .endpoint = 0x00, /* Control pipe */ | |
| 336 | .direction = UE_DIR_ANY, | |
| 337 | .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, | |
| 338 | .callback = &uhid_write_callback, | |
| 339 | .timeout = 1000, /* 1 second */ | |
| 340 | }, | |
| 341 | ||
| 342 | [UHID_CTRL_DT_RD] = { | |
| 343 | .type = UE_CONTROL, | |
| 344 | .endpoint = 0x00, /* Control pipe */ | |
| 345 | .direction = UE_DIR_ANY, | |
| 346 | .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, | |
| 347 | .callback = &uhid_read_callback, | |
| 348 | .timeout = 1000, /* 1 second */ | |
| 349 | }, | |
| 350 | }; | |
| 351 | ||
| 352 | static void | |
| 353 | uhid_start_read(struct usb_fifo *fifo) | |
| 354 | { | |
| 355 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 356 | ||
| 357 | if (sc->sc_flags & UHID_FLAG_IMMED) { | |
| 358 | usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]); | |
| 359 | } else { | |
| 360 | usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]); | |
| 361 | } | |
| 362 | } | |
| 363 | ||
| 364 | static void | |
| 365 | uhid_stop_read(struct usb_fifo *fifo) | |
| 366 | { | |
| 367 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 368 | ||
| 369 | usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]); | |
| 370 | usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]); | |
| 371 | } | |
| 372 | ||
| 373 | static void | |
| 374 | uhid_start_write(struct usb_fifo *fifo) | |
| 375 | { | |
| 376 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 377 | ||
| 378 | usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]); | |
| 379 | } | |
| 380 | ||
| 381 | static void | |
| 382 | uhid_stop_write(struct usb_fifo *fifo) | |
| 383 | { | |
| 384 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 385 | ||
| 386 | usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]); | |
| 387 | } | |
| 388 | ||
| 389 | static int | |
| 390 | uhid_get_report(struct uhid_softc *sc, uint8_t type, | |
| 391 | uint8_t id, void *kern_data, void *user_data, | |
| 392 | uint16_t len) | |
| 393 | { | |
| 394 | int err; | |
| 395 | uint8_t free_data = 0; | |
| 396 | ||
| 397 | if (kern_data == NULL) { | |
| 8d7664cb | 398 | kern_data = kmalloc(len, M_USBDEV, M_WAITOK); |
| 12bd3c8b SW |
399 | if (kern_data == NULL) { |
| 400 | err = ENOMEM; | |
| 401 | goto done; | |
| 402 | } | |
| 403 | free_data = 1; | |
| 404 | } | |
| 405 | err = usbd_req_get_report(sc->sc_udev, NULL, kern_data, | |
| 406 | len, sc->sc_iface_index, type, id); | |
| 407 | if (err) { | |
| 408 | err = ENXIO; | |
| 409 | goto done; | |
| 410 | } | |
| 411 | if (user_data) { | |
| 412 | /* dummy buffer */ | |
| 413 | err = copyout(kern_data, user_data, len); | |
| 414 | if (err) { | |
| 415 | goto done; | |
| 416 | } | |
| 417 | } | |
| 418 | done: | |
| 419 | if (free_data) { | |
| 8d7664cb | 420 | kfree(kern_data, M_USBDEV); |
| 12bd3c8b SW |
421 | } |
| 422 | return (err); | |
| 423 | } | |
| 424 | ||
| 425 | static int | |
| 426 | uhid_set_report(struct uhid_softc *sc, uint8_t type, | |
| 427 | uint8_t id, void *kern_data, void *user_data, | |
| 428 | uint16_t len) | |
| 429 | { | |
| 430 | int err; | |
| 431 | uint8_t free_data = 0; | |
| 432 | ||
| 433 | if (kern_data == NULL) { | |
| 8d7664cb | 434 | kern_data = kmalloc(len, M_USBDEV, M_WAITOK); |
| 12bd3c8b SW |
435 | if (kern_data == NULL) { |
| 436 | err = ENOMEM; | |
| 437 | goto done; | |
| 438 | } | |
| 439 | free_data = 1; | |
| 440 | err = copyin(user_data, kern_data, len); | |
| 441 | if (err) { | |
| 442 | goto done; | |
| 443 | } | |
| 444 | } | |
| 445 | err = usbd_req_set_report(sc->sc_udev, NULL, kern_data, | |
| 446 | len, sc->sc_iface_index, type, id); | |
| 447 | if (err) { | |
| 448 | err = ENXIO; | |
| 449 | goto done; | |
| 450 | } | |
| 451 | done: | |
| 452 | if (free_data) { | |
| 8d7664cb | 453 | kfree(kern_data, M_USBDEV); |
| 12bd3c8b SW |
454 | } |
| 455 | return (err); | |
| 456 | } | |
| 457 | ||
| 458 | static int | |
| 459 | uhid_open(struct usb_fifo *fifo, int fflags) | |
| 460 | { | |
| 461 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 462 | ||
| 463 | /* | |
| 464 | * The buffers are one byte larger than maximum so that one | |
| 465 | * can detect too large read/writes and short transfers: | |
| 466 | */ | |
| 467 | if (fflags & FREAD) { | |
| 468 | /* reset flags */ | |
| 469 | sc->sc_flags &= ~UHID_FLAG_IMMED; | |
| 470 | ||
| 471 | if (usb_fifo_alloc_buffer(fifo, | |
| 472 | sc->sc_isize + 1, UHID_FRAME_NUM)) { | |
| 473 | return (ENOMEM); | |
| 474 | } | |
| 475 | } | |
| 476 | if (fflags & FWRITE) { | |
| 477 | if (usb_fifo_alloc_buffer(fifo, | |
| 478 | sc->sc_osize + 1, UHID_FRAME_NUM)) { | |
| 479 | return (ENOMEM); | |
| 480 | } | |
| 481 | } | |
| 482 | return (0); | |
| 483 | } | |
| 484 | ||
| 485 | static void | |
| 486 | uhid_close(struct usb_fifo *fifo, int fflags) | |
| 487 | { | |
| 488 | if (fflags & (FREAD | FWRITE)) { | |
| 489 | usb_fifo_free_buffer(fifo); | |
| 490 | } | |
| 491 | } | |
| 492 | ||
| 493 | static int | |
| 494 | uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, | |
| 495 | int fflags) | |
| 496 | { | |
| 497 | struct uhid_softc *sc = usb_fifo_softc(fifo); | |
| 498 | struct usb_gen_descriptor *ugd; | |
| 499 | uint32_t size; | |
| 500 | int error = 0; | |
| 501 | uint8_t id; | |
| 502 | ||
| 503 | switch (cmd) { | |
| 504 | case USB_GET_REPORT_DESC: | |
| 505 | ugd = addr; | |
| 506 | if (sc->sc_repdesc_size > ugd->ugd_maxlen) { | |
| 507 | size = ugd->ugd_maxlen; | |
| 508 | } else { | |
| 509 | size = sc->sc_repdesc_size; | |
| 510 | } | |
| 511 | ugd->ugd_actlen = size; | |
| 512 | if (ugd->ugd_data == NULL) | |
| 513 | break; /* descriptor length only */ | |
| 514 | error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size); | |
| 515 | break; | |
| 516 | ||
| 517 | case USB_SET_IMMED: | |
| 518 | if (!(fflags & FREAD)) { | |
| 519 | error = EPERM; | |
| 520 | break; | |
| 521 | } | |
| 522 | if (*(int *)addr) { | |
| 523 | ||
| 524 | /* do a test read */ | |
| 525 | ||
| 526 | error = uhid_get_report(sc, UHID_INPUT_REPORT, | |
| 527 | sc->sc_iid, NULL, NULL, sc->sc_isize); | |
| 528 | if (error) { | |
| 529 | break; | |
| 530 | } | |
| 8d7664cb | 531 | lockmgr(&sc->sc_lock, LK_EXCLUSIVE); |
| 12bd3c8b | 532 | sc->sc_flags |= UHID_FLAG_IMMED; |
| 8d7664cb | 533 | lockmgr(&sc->sc_lock, LK_RELEASE); |
| 12bd3c8b | 534 | } else { |
| 8d7664cb | 535 | lockmgr(&sc->sc_lock, LK_EXCLUSIVE); |
| 12bd3c8b | 536 | sc->sc_flags &= ~UHID_FLAG_IMMED; |
| 8d7664cb | 537 | lockmgr(&sc->sc_lock, LK_RELEASE); |
| 12bd3c8b SW |
538 | } |
| 539 | break; | |
| 540 | ||
| 541 | case USB_GET_REPORT: | |
| 542 | if (!(fflags & FREAD)) { | |
| 543 | error = EPERM; | |
| 544 | break; | |
| 545 | } | |
| 546 | ugd = addr; | |
| 547 | switch (ugd->ugd_report_type) { | |
| 548 | case UHID_INPUT_REPORT: | |
| 549 | size = sc->sc_isize; | |
| 550 | id = sc->sc_iid; | |
| 551 | break; | |
| 552 | case UHID_OUTPUT_REPORT: | |
| 553 | size = sc->sc_osize; | |
| 554 | id = sc->sc_oid; | |
| 555 | break; | |
| 556 | case UHID_FEATURE_REPORT: | |
| 557 | size = sc->sc_fsize; | |
| 558 | id = sc->sc_fid; | |
| 559 | break; | |
| 560 | default: | |
| 561 | return (EINVAL); | |
| 562 | } | |
| 563 | if (id != 0) | |
| 564 | copyin(ugd->ugd_data, &id, 1); | |
| 565 | error = uhid_get_report(sc, ugd->ugd_report_type, id, | |
| 566 | NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); | |
| 567 | break; | |
| 568 | ||
| 569 | case USB_SET_REPORT: | |
| 570 | if (!(fflags & FWRITE)) { | |
| 571 | error = EPERM; | |
| 572 | break; | |
| 573 | } | |
| 574 | ugd = addr; | |
| 575 | switch (ugd->ugd_report_type) { | |
| 576 | case UHID_INPUT_REPORT: | |
| 577 | size = sc->sc_isize; | |
| 578 | id = sc->sc_iid; | |
| 579 | break; | |
| 580 | case UHID_OUTPUT_REPORT: | |
| 581 | size = sc->sc_osize; | |
| 582 | id = sc->sc_oid; | |
| 583 | break; | |
| 584 | case UHID_FEATURE_REPORT: | |
| 585 | size = sc->sc_fsize; | |
| 586 | id = sc->sc_fid; | |
| 587 | break; | |
| 588 | default: | |
| 589 | return (EINVAL); | |
| 590 | } | |
| 591 | if (id != 0) | |
| 592 | copyin(ugd->ugd_data, &id, 1); | |
| 593 | error = uhid_set_report(sc, ugd->ugd_report_type, id, | |
| 594 | NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); | |
| 595 | break; | |
| 596 | ||
| 597 | case USB_GET_REPORT_ID: | |
| 598 | *(int *)addr = 0; /* XXX: we only support reportid 0? */ | |
| 599 | break; | |
| 600 | ||
| 601 | default: | |
| 602 | error = EINVAL; | |
| 603 | break; | |
| 604 | } | |
| 605 | return (error); | |
| 606 | } | |
| 607 | ||
| 608 | static const STRUCT_USB_HOST_ID uhid_devs[] = { | |
| 609 | /* generic HID class */ | |
| 610 | {USB_IFACE_CLASS(UICLASS_HID),}, | |
| 611 | /* the Xbox 360 gamepad doesn't use the HID class */ | |
| 612 | {USB_IFACE_CLASS(UICLASS_VENDOR), | |
| 613 | USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER), | |
| 614 | USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),}, | |
| 615 | }; | |
| 616 | ||
| 617 | static int | |
| 618 | uhid_probe(device_t dev) | |
| 619 | { | |
| 620 | struct usb_attach_arg *uaa = device_get_ivars(dev); | |
| 621 | int error; | |
| 622 | ||
| 623 | DPRINTFN(11, "\n"); | |
| 624 | ||
| 625 | if (uaa->usb_mode != USB_MODE_HOST) | |
| 626 | return (ENXIO); | |
| 627 | ||
| 628 | error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa); | |
| 629 | if (error) | |
| 630 | return (error); | |
| 631 | ||
| 632 | if (usb_test_quirk(uaa, UQ_HID_IGNORE)) | |
| 633 | return (ENXIO); | |
| 634 | ||
| 635 | /* | |
| 636 | * Don't attach to mouse and keyboard devices, hence then no | |
| 637 | * "nomatch" event is generated and then ums and ukbd won't | |
| 638 | * attach properly when loaded. | |
| 639 | */ | |
| 640 | if ((uaa->info.bInterfaceClass == UICLASS_HID) && | |
| 641 | (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && | |
| 642 | ((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) || | |
| 643 | (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))) { | |
| 644 | return (ENXIO); | |
| 645 | } | |
| 646 | ||
| 647 | return (BUS_PROBE_GENERIC); | |
| 648 | } | |
| 649 | ||
| 650 | static int | |
| 651 | uhid_attach(device_t dev) | |
| 652 | { | |
| 653 | struct usb_attach_arg *uaa = device_get_ivars(dev); | |
| 654 | struct uhid_softc *sc = device_get_softc(dev); | |
| 655 | int unit = device_get_unit(dev); | |
| 656 | int error = 0; | |
| 657 | ||
| 658 | DPRINTFN(10, "sc=%p\n", sc); | |
| 659 | ||
| 660 | device_set_usb_desc(dev); | |
| 661 | ||
| 8d7664cb | 662 | lockinit(&sc->sc_lock, "uhid lock", 0, LK_CANRECURSE); |
| 12bd3c8b SW |
663 | |
| 664 | sc->sc_udev = uaa->device; | |
| 665 | ||
| 666 | sc->sc_iface_no = uaa->info.bIfaceNum; | |
| 667 | sc->sc_iface_index = uaa->info.bIfaceIndex; | |
| 668 | ||
| 669 | error = usbd_transfer_setup(uaa->device, | |
| 670 | &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config, | |
| 8d7664cb | 671 | UHID_N_TRANSFER, sc, &sc->sc_lock); |
| 12bd3c8b SW |
672 | |
| 673 | if (error) { | |
| 674 | DPRINTF("error=%s\n", usbd_errstr(error)); | |
| 675 | goto detach; | |
| 676 | } | |
| 677 | if (uaa->info.idVendor == USB_VENDOR_WACOM) { | |
| 678 | ||
| 679 | /* the report descriptor for the Wacom Graphire is broken */ | |
| 680 | ||
| 681 | if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) { | |
| 682 | ||
| 683 | sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr); | |
| 684 | sc->sc_repdesc_ptr = (void *)&uhid_graphire_report_descr; | |
| 685 | sc->sc_flags |= UHID_FLAG_STATIC_DESC; | |
| 686 | ||
| 687 | } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { | |
| 688 | ||
| 689 | static uint8_t reportbuf[] = {2, 2, 2}; | |
| 690 | ||
| 691 | /* | |
| 692 | * The Graphire3 needs 0x0202 to be written to | |
| 693 | * feature report ID 2 before it'll start | |
| 694 | * returning digitizer data. | |
| 695 | */ | |
| 696 | error = usbd_req_set_report(uaa->device, NULL, | |
| 697 | reportbuf, sizeof(reportbuf), | |
| 698 | uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2); | |
| 699 | ||
| 700 | if (error) { | |
| 701 | DPRINTF("set report failed, error=%s (ignored)\n", | |
| 702 | usbd_errstr(error)); | |
| 703 | } | |
| 704 | sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr); | |
| 705 | sc->sc_repdesc_ptr = (void *)&uhid_graphire3_4x5_report_descr; | |
| 706 | sc->sc_flags |= UHID_FLAG_STATIC_DESC; | |
| 707 | } | |
| 708 | } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) && | |
| 709 | (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) && | |
| 710 | (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { | |
| 711 | ||
| 712 | /* the Xbox 360 gamepad has no report descriptor */ | |
| 713 | sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr); | |
| 714 | sc->sc_repdesc_ptr = (void *)&uhid_xb360gp_report_descr; | |
| 715 | sc->sc_flags |= UHID_FLAG_STATIC_DESC; | |
| 716 | } | |
| 717 | if (sc->sc_repdesc_ptr == NULL) { | |
| 718 | ||
| 719 | error = usbd_req_get_hid_desc(uaa->device, NULL, | |
| 720 | &sc->sc_repdesc_ptr, &sc->sc_repdesc_size, | |
| 721 | M_USBDEV, uaa->info.bIfaceIndex); | |
| 722 | ||
| 723 | if (error) { | |
| 724 | device_printf(dev, "no report descriptor\n"); | |
| 725 | goto detach; | |
| 726 | } | |
| 727 | } | |
| 728 | error = usbd_req_set_idle(uaa->device, NULL, | |
| 729 | uaa->info.bIfaceIndex, 0, 0); | |
| 730 | ||
| 731 | if (error) { | |
| 732 | DPRINTF("set idle failed, error=%s (ignored)\n", | |
| 733 | usbd_errstr(error)); | |
| 734 | } | |
| 735 | sc->sc_isize = hid_report_size | |
| 736 | (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid); | |
| 737 | ||
| 738 | sc->sc_osize = hid_report_size | |
| 739 | (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid); | |
| 740 | ||
| 741 | sc->sc_fsize = hid_report_size | |
| 742 | (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid); | |
| 743 | ||
| 744 | if (sc->sc_isize > UHID_BSIZE) { | |
| 745 | DPRINTF("input size is too large, " | |
| 746 | "%d bytes (truncating)\n", | |
| 747 | sc->sc_isize); | |
| 748 | sc->sc_isize = UHID_BSIZE; | |
| 749 | } | |
| 750 | if (sc->sc_osize > UHID_BSIZE) { | |
| 751 | DPRINTF("output size is too large, " | |
| 752 | "%d bytes (truncating)\n", | |
| 753 | sc->sc_osize); | |
| 754 | sc->sc_osize = UHID_BSIZE; | |
| 755 | } | |
| 756 | if (sc->sc_fsize > UHID_BSIZE) { | |
| 757 | DPRINTF("feature size is too large, " | |
| 758 | "%d bytes (truncating)\n", | |
| 759 | sc->sc_fsize); | |
| 760 | sc->sc_fsize = UHID_BSIZE; | |
| 761 | } | |
| 762 | ||
| 8d7664cb | 763 | error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock, |
| 12bd3c8b SW |
764 | &uhid_fifo_methods, &sc->sc_fifo, |
| 765 | unit, 0 - 1, uaa->info.bIfaceIndex, | |
| 766 | UID_ROOT, GID_OPERATOR, 0644); | |
| 767 | if (error) { | |
| 768 | goto detach; | |
| 769 | } | |
| 770 | return (0); /* success */ | |
| 771 | ||
| 772 | detach: | |
| 773 | uhid_detach(dev); | |
| 774 | return (ENOMEM); | |
| 775 | } | |
| 776 | ||
| 777 | static int | |
| 778 | uhid_detach(device_t dev) | |
| 779 | { | |
| 780 | struct uhid_softc *sc = device_get_softc(dev); | |
| 781 | ||
| 782 | usb_fifo_detach(&sc->sc_fifo); | |
| 783 | ||
| 784 | usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER); | |
| 785 | ||
| 786 | if (sc->sc_repdesc_ptr) { | |
| 787 | if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) { | |
| 8d7664cb | 788 | kfree(sc->sc_repdesc_ptr, M_USBDEV); |
| 12bd3c8b SW |
789 | } |
| 790 | } | |
| 8d7664cb | 791 | lockuninit(&sc->sc_lock); |
| 12bd3c8b SW |
792 | |
| 793 | return (0); | |
| 794 | } | |
| 795 | ||
| 796 | static devclass_t uhid_devclass; | |
| 797 | ||
| 798 | static device_method_t uhid_methods[] = { | |
| 799 | DEVMETHOD(device_probe, uhid_probe), | |
| 800 | DEVMETHOD(device_attach, uhid_attach), | |
| 801 | DEVMETHOD(device_detach, uhid_detach), | |
| 802 | {0, 0} | |
| 803 | }; | |
| 804 | ||
| 805 | static driver_t uhid_driver = { | |
| 806 | .name = "uhid", | |
| 807 | .methods = uhid_methods, | |
| 808 | .size = sizeof(struct uhid_softc), | |
| 809 | }; | |
| 810 | ||
| 811 | DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0); | |
| 812 | MODULE_DEPEND(uhid, usb, 1, 1, 1); | |
| 813 | MODULE_VERSION(uhid, 1); |