1 /* $OpenBSD: ugen.c,v 1.94 2016/05/24 05:35:01 mpi Exp $ */ 2 /* $NetBSD: ugen.c,v 1.63 2002/11/26 18:49:48 christos Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/ugen.c,v 1.26 1999/11/17 22:33:41 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/device.h> 41 #include <sys/ioctl.h> 42 #include <sys/conf.h> 43 #include <sys/tty.h> 44 #include <sys/file.h> 45 #include <sys/selinfo.h> 46 #include <sys/vnode.h> 47 #include <sys/poll.h> 48 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdi_util.h> 52 53 #ifdef UGEN_DEBUG 54 #define DPRINTF(x) do { if (ugendebug) printf x; } while (0) 55 #define DPRINTFN(n,x) do { if (ugendebug>(n)) printf x; } while (0) 56 int ugendebug = 0; 57 #else 58 #define DPRINTF(x) 59 #define DPRINTFN(n,x) 60 #endif 61 62 #define UGEN_CHUNK 128 /* chunk size for read */ 63 #define UGEN_IBSIZE 1020 /* buffer size */ 64 #define UGEN_BBSIZE 1024 65 66 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */ 67 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */ 68 #define UGEN_NISORFRMS 4 /* number of frames (milliseconds) per req */ 69 70 struct ugen_endpoint { 71 struct ugen_softc *sc; 72 usb_endpoint_descriptor_t *edesc; 73 struct usbd_interface *iface; 74 int state; 75 #define UGEN_ASLP 0x02 /* waiting for data */ 76 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */ 77 struct usbd_pipe *pipeh; 78 struct clist q; 79 struct selinfo rsel; 80 u_char *ibuf; /* start of buffer (circular for isoc) */ 81 u_char *fill; /* location for input (isoc) */ 82 u_char *limit; /* end of circular buffer (isoc) */ 83 u_char *cur; /* current read location (isoc) */ 84 u_int32_t timeout; 85 struct isoreq { 86 struct ugen_endpoint *sce; 87 struct usbd_xfer *xfer; 88 void *dmabuf; 89 u_int16_t sizes[UGEN_NISORFRMS]; 90 } isoreqs[UGEN_NISOREQS]; 91 }; 92 93 struct ugen_softc { 94 struct device sc_dev; /* base device */ 95 struct usbd_device *sc_udev; 96 97 char sc_is_open[USB_MAX_ENDPOINTS]; 98 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2]; 99 #define OUT 0 100 #define IN 1 101 102 int sc_refcnt; 103 u_char sc_secondary; 104 }; 105 106 void ugenintr(struct usbd_xfer *xfer, void *addr, usbd_status status); 107 void ugen_isoc_rintr(struct usbd_xfer *xfer, void *addr, usbd_status status); 108 int ugen_do_read(struct ugen_softc *, int, struct uio *, int); 109 int ugen_do_write(struct ugen_softc *, int, struct uio *, int); 110 int ugen_do_ioctl(struct ugen_softc *, int, u_long, caddr_t, int, 111 struct proc *); 112 int ugen_do_close(struct ugen_softc *, int, int); 113 int ugen_set_config(struct ugen_softc *sc, int configno); 114 int ugen_set_interface(struct ugen_softc *, int, int); 115 int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx); 116 117 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf) 118 #define UGENENDPOINT(n) (minor(n) & 0xf) 119 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e))) 120 121 int ugen_match(struct device *, void *, void *); 122 void ugen_attach(struct device *, struct device *, void *); 123 int ugen_detach(struct device *, int); 124 125 struct cfdriver ugen_cd = { 126 NULL, "ugen", DV_DULL 127 }; 128 129 const struct cfattach ugen_ca = { 130 sizeof(struct ugen_softc), ugen_match, ugen_attach, ugen_detach 131 }; 132 133 int 134 ugen_match(struct device *parent, void *match, void *aux) 135 { 136 struct usb_attach_arg *uaa = aux; 137 138 if (uaa->usegeneric) { 139 return (UMATCH_GENERIC); 140 } else 141 return (UMATCH_NONE); 142 } 143 144 void 145 ugen_attach(struct device *parent, struct device *self, void *aux) 146 { 147 struct ugen_softc *sc = (struct ugen_softc *)self; 148 struct usb_attach_arg *uaa = aux; 149 struct usbd_device *udev; 150 usbd_status err; 151 int conf; 152 153 sc->sc_udev = udev = uaa->device; 154 155 if (usbd_get_devcnt(udev) > 0) 156 sc->sc_secondary = 1; 157 158 if (!sc->sc_secondary) { 159 /* First set configuration index 0, the default one for ugen. */ 160 err = usbd_set_config_index(udev, 0, 0); 161 if (err) { 162 printf("%s: setting configuration index 0 failed\n", 163 sc->sc_dev.dv_xname); 164 usbd_deactivate(sc->sc_udev); 165 return; 166 } 167 } 168 conf = usbd_get_config_descriptor(udev)->bConfigurationValue; 169 170 /* Set up all the local state for this configuration. */ 171 err = ugen_set_config(sc, conf); 172 if (err) { 173 printf("%s: setting configuration %d failed\n", 174 sc->sc_dev.dv_xname, conf); 175 usbd_deactivate(sc->sc_udev); 176 return; 177 } 178 } 179 180 int 181 ugen_set_config(struct ugen_softc *sc, int configno) 182 { 183 struct usbd_device *dev = sc->sc_udev; 184 usb_config_descriptor_t *cdesc; 185 usb_interface_descriptor_t *id; 186 struct usbd_interface *iface; 187 usb_endpoint_descriptor_t *ed; 188 struct ugen_endpoint *sce; 189 int ifaceno, endptno, endpt; 190 int err, dir; 191 192 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n", 193 sc->sc_dev.dv_xname, configno, sc)); 194 195 /* 196 * We start at 1, not 0, because we don't care whether the 197 * control endpoint is open or not. It is always present. 198 */ 199 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) 200 if (sc->sc_is_open[endptno]) { 201 DPRINTFN(1, 202 ("ugen_set_config: %s - endpoint %d is open\n", 203 sc->sc_dev.dv_xname, endptno)); 204 return (USBD_IN_USE); 205 } 206 207 /* Avoid setting the current value. */ 208 cdesc = usbd_get_config_descriptor(dev); 209 if (cdesc == NULL || cdesc->bConfigurationValue != configno) { 210 if (sc->sc_secondary) { 211 printf("%s: secondary, not changing config to %d\n", 212 __func__, configno); 213 return (USBD_IN_USE); 214 } else { 215 err = usbd_set_config_no(dev, configno, 1); 216 if (err) 217 return (err); 218 } 219 } 220 221 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints); 222 for (ifaceno = 0; ifaceno < cdesc->bNumInterface; ifaceno++) { 223 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno)); 224 if (usbd_iface_claimed(sc->sc_udev, ifaceno)) { 225 DPRINTF(("%s: iface %d not available\n", __func__, 226 ifaceno)); 227 continue; 228 } 229 err = usbd_device2interface_handle(dev, ifaceno, &iface); 230 if (err) 231 return (err); 232 id = usbd_get_interface_descriptor(iface); 233 for (endptno = 0; endptno < id->bNumEndpoints; endptno++) { 234 ed = usbd_interface2endpoint_descriptor(iface,endptno); 235 endpt = ed->bEndpointAddress; 236 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 237 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 238 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x" 239 "(%d,%d), sce=%p\n", 240 endptno, endpt, UE_GET_ADDR(endpt), 241 UE_GET_DIR(endpt), sce)); 242 sce->sc = sc; 243 sce->edesc = ed; 244 sce->iface = iface; 245 } 246 } 247 return (0); 248 } 249 250 int 251 ugenopen(dev_t dev, int flag, int mode, struct proc *p) 252 { 253 struct ugen_softc *sc; 254 int unit = UGENUNIT(dev); 255 int endpt = UGENENDPOINT(dev); 256 usb_endpoint_descriptor_t *edesc; 257 struct ugen_endpoint *sce; 258 int dir, isize; 259 usbd_status err; 260 struct usbd_xfer *xfer; 261 void *buf; 262 int i, j; 263 264 if (unit >= ugen_cd.cd_ndevs) 265 return (ENXIO); 266 sc = ugen_cd.cd_devs[unit]; 267 if (sc == NULL) 268 return (ENXIO); 269 270 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n", 271 flag, mode, unit, endpt)); 272 273 if (sc == NULL || usbd_is_dying(sc->sc_udev)) 274 return (ENXIO); 275 276 if (sc->sc_is_open[endpt]) 277 return (EBUSY); 278 279 if (endpt == USB_CONTROL_ENDPOINT) { 280 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1; 281 return (0); 282 } 283 284 /* Make sure there are pipes for all directions. */ 285 for (dir = OUT; dir <= IN; dir++) { 286 if (flag & (dir == OUT ? FWRITE : FREAD)) { 287 sce = &sc->sc_endpoints[endpt][dir]; 288 if (sce == 0 || sce->edesc == 0) 289 return (ENXIO); 290 } 291 } 292 293 /* Actually open the pipes. */ 294 /* XXX Should back out properly if it fails. */ 295 for (dir = OUT; dir <= IN; dir++) { 296 if (!(flag & (dir == OUT ? FWRITE : FREAD))) 297 continue; 298 sce = &sc->sc_endpoints[endpt][dir]; 299 sce->state = 0; 300 sce->timeout = USBD_NO_TIMEOUT; 301 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n", 302 sc, endpt, dir, sce)); 303 edesc = sce->edesc; 304 switch (edesc->bmAttributes & UE_XFERTYPE) { 305 case UE_INTERRUPT: 306 if (dir == OUT) { 307 err = usbd_open_pipe(sce->iface, 308 edesc->bEndpointAddress, 0, &sce->pipeh); 309 if (err) 310 return (EIO); 311 break; 312 } 313 isize = UGETW(edesc->wMaxPacketSize); 314 if (isize == 0) /* shouldn't happen */ 315 return (EINVAL); 316 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK); 317 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n", 318 endpt, isize)); 319 clalloc(&sce->q, UGEN_IBSIZE, 0); 320 err = usbd_open_pipe_intr(sce->iface, 321 edesc->bEndpointAddress, 322 USBD_SHORT_XFER_OK, &sce->pipeh, sce, 323 sce->ibuf, isize, ugenintr, 324 USBD_DEFAULT_INTERVAL); 325 if (err) { 326 free(sce->ibuf, M_USBDEV, 0); 327 clfree(&sce->q); 328 return (EIO); 329 } 330 DPRINTFN(5, ("ugenopen: interrupt open done\n")); 331 break; 332 case UE_BULK: 333 err = usbd_open_pipe(sce->iface, 334 edesc->bEndpointAddress, 0, &sce->pipeh); 335 if (err) 336 return (EIO); 337 break; 338 case UE_ISOCHRONOUS: 339 if (dir == OUT) 340 return (EINVAL); 341 isize = UGETW(edesc->wMaxPacketSize); 342 if (isize == 0) /* shouldn't happen */ 343 return (EINVAL); 344 sce->ibuf = mallocarray(isize, UGEN_NISOFRAMES, 345 M_USBDEV, M_WAITOK); 346 sce->cur = sce->fill = sce->ibuf; 347 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES; 348 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n", 349 endpt, isize)); 350 err = usbd_open_pipe(sce->iface, 351 edesc->bEndpointAddress, 0, &sce->pipeh); 352 if (err) { 353 free(sce->ibuf, M_USBDEV, 0); 354 return (EIO); 355 } 356 for(i = 0; i < UGEN_NISOREQS; ++i) { 357 sce->isoreqs[i].sce = sce; 358 xfer = usbd_alloc_xfer(sc->sc_udev); 359 if (xfer == 0) 360 goto bad; 361 sce->isoreqs[i].xfer = xfer; 362 buf = usbd_alloc_buffer 363 (xfer, isize * UGEN_NISORFRMS); 364 if (buf == 0) { 365 i++; 366 goto bad; 367 } 368 sce->isoreqs[i].dmabuf = buf; 369 for(j = 0; j < UGEN_NISORFRMS; ++j) 370 sce->isoreqs[i].sizes[j] = isize; 371 usbd_setup_isoc_xfer(xfer, sce->pipeh, 372 &sce->isoreqs[i], sce->isoreqs[i].sizes, 373 UGEN_NISORFRMS, USBD_NO_COPY | 374 USBD_SHORT_XFER_OK, ugen_isoc_rintr); 375 (void)usbd_transfer(xfer); 376 } 377 DPRINTFN(5, ("ugenopen: isoc open done\n")); 378 break; 379 bad: 380 while (--i >= 0) /* implicit buffer free */ 381 usbd_free_xfer(sce->isoreqs[i].xfer); 382 return (ENOMEM); 383 case UE_CONTROL: 384 sce->timeout = USBD_DEFAULT_TIMEOUT; 385 return (EINVAL); 386 } 387 } 388 sc->sc_is_open[endpt] = 1; 389 return (0); 390 } 391 392 int 393 ugenclose(dev_t dev, int flag, int mode, struct proc *p) 394 { 395 struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 396 int endpt = UGENENDPOINT(dev); 397 int error; 398 399 if (sc == NULL || usbd_is_dying(sc->sc_udev)) 400 return (EIO); 401 402 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n", 403 flag, mode, UGENUNIT(dev), endpt)); 404 405 sc->sc_refcnt++; 406 error = ugen_do_close(sc, endpt, flag); 407 if (--sc->sc_refcnt < 0) 408 usb_detach_wakeup(&sc->sc_dev); 409 410 return (error); 411 } 412 413 int 414 ugen_do_close(struct ugen_softc *sc, int endpt, int flag) 415 { 416 struct ugen_endpoint *sce; 417 int dir, i; 418 419 #ifdef DIAGNOSTIC 420 if (!sc->sc_is_open[endpt]) { 421 printf("ugenclose: not open\n"); 422 return (EINVAL); 423 } 424 #endif 425 426 if (endpt == USB_CONTROL_ENDPOINT) { 427 DPRINTFN(5, ("ugenclose: close control\n")); 428 sc->sc_is_open[endpt] = 0; 429 return (0); 430 } 431 432 for (dir = OUT; dir <= IN; dir++) { 433 if (!(flag & (dir == OUT ? FWRITE : FREAD))) 434 continue; 435 sce = &sc->sc_endpoints[endpt][dir]; 436 if (sce == NULL || sce->pipeh == NULL) 437 continue; 438 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n", 439 endpt, dir, sce)); 440 441 usbd_close_pipe(sce->pipeh); 442 sce->pipeh = NULL; 443 444 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 445 case UE_INTERRUPT: 446 ndflush(&sce->q, sce->q.c_cc); 447 clfree(&sce->q); 448 break; 449 case UE_ISOCHRONOUS: 450 for (i = 0; i < UGEN_NISOREQS; ++i) 451 usbd_free_xfer(sce->isoreqs[i].xfer); 452 453 default: 454 break; 455 } 456 457 if (sce->ibuf != NULL) { 458 free(sce->ibuf, M_USBDEV, 0); 459 sce->ibuf = NULL; 460 } 461 } 462 sc->sc_is_open[endpt] = 0; 463 464 return (0); 465 } 466 467 int 468 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag) 469 { 470 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN]; 471 u_int32_t tn; 472 size_t n; 473 char buf[UGEN_BBSIZE]; 474 struct usbd_xfer *xfer; 475 usbd_status err; 476 int s; 477 int flags, error = 0; 478 u_char buffer[UGEN_CHUNK]; 479 480 DPRINTFN(5, ("%s: ugenread: %d\n", sc->sc_dev.dv_xname, endpt)); 481 482 if (usbd_is_dying(sc->sc_udev)) 483 return (EIO); 484 485 if (endpt == USB_CONTROL_ENDPOINT) 486 return (ENODEV); 487 488 #ifdef DIAGNOSTIC 489 if (sce->edesc == NULL) { 490 printf("ugenread: no edesc\n"); 491 return (EIO); 492 } 493 if (sce->pipeh == NULL) { 494 printf("ugenread: no pipe\n"); 495 return (EIO); 496 } 497 #endif 498 499 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 500 case UE_INTERRUPT: 501 /* Block until activity occurred. */ 502 s = splusb(); 503 while (sce->q.c_cc == 0) { 504 if (flag & IO_NDELAY) { 505 splx(s); 506 return (EWOULDBLOCK); 507 } 508 sce->state |= UGEN_ASLP; 509 DPRINTFN(5, ("ugenread: sleep on %p\n", sce)); 510 error = tsleep(sce, PZERO | PCATCH, "ugenri", 511 (sce->timeout * hz) / 1000); 512 sce->state &= ~UGEN_ASLP; 513 DPRINTFN(5, ("ugenread: woke, error=%d\n", error)); 514 if (usbd_is_dying(sc->sc_udev)) 515 error = EIO; 516 if (error == EWOULDBLOCK) { /* timeout, return 0 */ 517 error = 0; 518 break; 519 } 520 if (error) 521 break; 522 } 523 splx(s); 524 525 /* Transfer as many chunks as possible. */ 526 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) { 527 n = ulmin(sce->q.c_cc, uio->uio_resid); 528 if (n > sizeof(buffer)) 529 n = sizeof(buffer); 530 531 /* Remove a small chunk from the input queue. */ 532 q_to_b(&sce->q, buffer, n); 533 DPRINTFN(5, ("ugenread: got %zu chars\n", n)); 534 535 /* Copy the data to the user process. */ 536 error = uiomove(buffer, n, uio); 537 if (error) 538 break; 539 } 540 break; 541 case UE_BULK: 542 xfer = usbd_alloc_xfer(sc->sc_udev); 543 if (xfer == 0) 544 return (ENOMEM); 545 flags = USBD_SYNCHRONOUS; 546 if (sce->state & UGEN_SHORT_OK) 547 flags |= USBD_SHORT_XFER_OK; 548 if (sce->timeout == 0) 549 flags |= USBD_CATCH; 550 while ((n = ulmin(UGEN_BBSIZE, uio->uio_resid)) != 0) { 551 DPRINTFN(1, ("ugenread: start transfer %zu bytes\n",n)); 552 usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n, 553 flags, sce->timeout, NULL); 554 err = usbd_transfer(xfer); 555 if (err) { 556 usbd_clear_endpoint_stall(sce->pipeh); 557 if (err == USBD_INTERRUPTED) 558 error = EINTR; 559 else if (err == USBD_TIMEOUT) 560 error = ETIMEDOUT; 561 else 562 error = EIO; 563 break; 564 } 565 usbd_get_xfer_status(xfer, NULL, NULL, &tn, NULL); 566 DPRINTFN(1, ("ugenread: got %u bytes\n", tn)); 567 error = uiomove(buf, tn, uio); 568 if (error || tn < n) 569 break; 570 } 571 usbd_free_xfer(xfer); 572 break; 573 case UE_ISOCHRONOUS: 574 s = splusb(); 575 while (sce->cur == sce->fill) { 576 if (flag & IO_NDELAY) { 577 splx(s); 578 return (EWOULDBLOCK); 579 } 580 sce->state |= UGEN_ASLP; 581 DPRINTFN(5, ("ugenread: sleep on %p\n", sce)); 582 error = tsleep(sce, PZERO | PCATCH, "ugenri", 583 (sce->timeout * hz) / 1000); 584 sce->state &= ~UGEN_ASLP; 585 DPRINTFN(5, ("ugenread: woke, error=%d\n", error)); 586 if (usbd_is_dying(sc->sc_udev)) 587 error = EIO; 588 if (error == EWOULDBLOCK) { /* timeout, return 0 */ 589 error = 0; 590 break; 591 } 592 if (error) 593 break; 594 } 595 596 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) { 597 if(sce->fill > sce->cur) 598 n = ulmin(sce->fill - sce->cur, uio->uio_resid); 599 else 600 n = ulmin(sce->limit - sce->cur, uio->uio_resid); 601 602 DPRINTFN(5, ("ugenread: isoc got %zu chars\n", n)); 603 604 /* Copy the data to the user process. */ 605 error = uiomove(sce->cur, n, uio); 606 if (error) 607 break; 608 sce->cur += n; 609 if(sce->cur >= sce->limit) 610 sce->cur = sce->ibuf; 611 } 612 splx(s); 613 break; 614 615 616 default: 617 return (ENXIO); 618 } 619 return (error); 620 } 621 622 int 623 ugenread(dev_t dev, struct uio *uio, int flag) 624 { 625 int endpt = UGENENDPOINT(dev); 626 struct ugen_softc *sc; 627 int error; 628 629 sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 630 631 sc->sc_refcnt++; 632 error = ugen_do_read(sc, endpt, uio, flag); 633 if (--sc->sc_refcnt < 0) 634 usb_detach_wakeup(&sc->sc_dev); 635 return (error); 636 } 637 638 int 639 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag) 640 { 641 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT]; 642 size_t n; 643 int flags, error = 0; 644 char buf[UGEN_BBSIZE]; 645 struct usbd_xfer *xfer; 646 usbd_status err; 647 648 DPRINTFN(5, ("%s: ugenwrite: %d\n", sc->sc_dev.dv_xname, endpt)); 649 650 if (usbd_is_dying(sc->sc_udev)) 651 return (EIO); 652 653 if (endpt == USB_CONTROL_ENDPOINT) 654 return (ENODEV); 655 656 #ifdef DIAGNOSTIC 657 if (sce->edesc == NULL) { 658 printf("ugenwrite: no edesc\n"); 659 return (EIO); 660 } 661 if (sce->pipeh == NULL) { 662 printf("ugenwrite: no pipe\n"); 663 return (EIO); 664 } 665 #endif 666 flags = USBD_SYNCHRONOUS; 667 if (sce->timeout == 0) 668 flags |= USBD_CATCH; 669 670 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 671 case UE_BULK: 672 xfer = usbd_alloc_xfer(sc->sc_udev); 673 if (xfer == 0) 674 return (EIO); 675 while ((n = ulmin(UGEN_BBSIZE, uio->uio_resid)) != 0) { 676 error = uiomove(buf, n, uio); 677 if (error) 678 break; 679 DPRINTFN(1, ("ugenwrite: transfer %zu bytes\n", n)); 680 usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n, 681 flags, sce->timeout, NULL); 682 err = usbd_transfer(xfer); 683 if (err) { 684 usbd_clear_endpoint_stall(sce->pipeh); 685 if (err == USBD_INTERRUPTED) 686 error = EINTR; 687 else if (err == USBD_TIMEOUT) 688 error = ETIMEDOUT; 689 else 690 error = EIO; 691 break; 692 } 693 } 694 usbd_free_xfer(xfer); 695 break; 696 case UE_INTERRUPT: 697 xfer = usbd_alloc_xfer(sc->sc_udev); 698 if (xfer == 0) 699 return (EIO); 700 while ((n = ulmin(UGETW(sce->edesc->wMaxPacketSize), 701 uio->uio_resid)) != 0) { 702 error = uiomove(buf, n, uio); 703 if (error) 704 break; 705 DPRINTFN(1, ("ugenwrite: transfer %zu bytes\n", n)); 706 usbd_setup_xfer(xfer, sce->pipeh, 0, buf, n, 707 flags, sce->timeout, NULL); 708 err = usbd_transfer(xfer); 709 if (err) { 710 usbd_clear_endpoint_stall(sce->pipeh); 711 if (err == USBD_INTERRUPTED) 712 error = EINTR; 713 else if (err == USBD_TIMEOUT) 714 error = ETIMEDOUT; 715 else 716 error = EIO; 717 break; 718 } 719 } 720 usbd_free_xfer(xfer); 721 break; 722 default: 723 return (ENXIO); 724 } 725 return (error); 726 } 727 728 int 729 ugenwrite(dev_t dev, struct uio *uio, int flag) 730 { 731 int endpt = UGENENDPOINT(dev); 732 struct ugen_softc *sc; 733 int error; 734 735 sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 736 737 sc->sc_refcnt++; 738 error = ugen_do_write(sc, endpt, uio, flag); 739 if (--sc->sc_refcnt < 0) 740 usb_detach_wakeup(&sc->sc_dev); 741 return (error); 742 } 743 744 int 745 ugen_detach(struct device *self, int flags) 746 { 747 struct ugen_softc *sc = (struct ugen_softc *)self; 748 struct ugen_endpoint *sce; 749 int i, dir, endptno; 750 int s, maj, mn; 751 752 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags)); 753 754 /* Abort all pipes. Causes processes waiting for transfer to wake. */ 755 for (i = 0; i < USB_MAX_ENDPOINTS; i++) { 756 for (dir = OUT; dir <= IN; dir++) { 757 sce = &sc->sc_endpoints[i][dir]; 758 if (sce && sce->pipeh) 759 usbd_abort_pipe(sce->pipeh); 760 } 761 } 762 763 s = splusb(); 764 if (--sc->sc_refcnt >= 0) { 765 /* Wake everyone */ 766 for (i = 0; i < USB_MAX_ENDPOINTS; i++) 767 wakeup(&sc->sc_endpoints[i][IN]); 768 /* Wait for processes to go away. */ 769 usb_detach_wait(&sc->sc_dev); 770 } 771 splx(s); 772 773 /* locate the major number */ 774 for (maj = 0; maj < nchrdev; maj++) 775 if (cdevsw[maj].d_open == ugenopen) 776 break; 777 778 /* Nuke the vnodes for any open instances (calls close). */ 779 mn = self->dv_unit * USB_MAX_ENDPOINTS; 780 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR); 781 782 for (endptno = 0; endptno < USB_MAX_ENDPOINTS; endptno++) { 783 if (sc->sc_is_open[endptno]) 784 ugen_do_close(sc, endptno, FREAD|FWRITE); 785 } 786 return (0); 787 } 788 789 void 790 ugenintr(struct usbd_xfer *xfer, void *addr, usbd_status status) 791 { 792 struct ugen_endpoint *sce = addr; 793 /*struct ugen_softc *sc = sce->sc;*/ 794 u_int32_t count; 795 u_char *ibuf; 796 797 if (status == USBD_CANCELLED) 798 return; 799 800 if (status != USBD_NORMAL_COMPLETION) { 801 DPRINTF(("ugenintr: status=%d\n", status)); 802 if (status == USBD_STALLED) 803 usbd_clear_endpoint_stall_async(sce->pipeh); 804 return; 805 } 806 807 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 808 ibuf = sce->ibuf; 809 810 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n", 811 xfer, status, count)); 812 DPRINTFN(5, (" data = %02x %02x %02x\n", 813 ibuf[0], ibuf[1], ibuf[2])); 814 815 (void)b_to_q(ibuf, count, &sce->q); 816 817 if (sce->state & UGEN_ASLP) { 818 sce->state &= ~UGEN_ASLP; 819 DPRINTFN(5, ("ugen_intr: waking %p\n", sce)); 820 wakeup(sce); 821 } 822 selwakeup(&sce->rsel); 823 } 824 825 void 826 ugen_isoc_rintr(struct usbd_xfer *xfer, void *addr, usbd_status status) 827 { 828 struct isoreq *req = addr; 829 struct ugen_endpoint *sce = req->sce; 830 u_int32_t count, n; 831 int i, isize; 832 833 /* Return if we are aborting. */ 834 if (status == USBD_CANCELLED) 835 return; 836 837 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 838 DPRINTFN(5,("%s: xfer %ld, count=%d\n", __func__, req - sce->isoreqs, 839 count)); 840 841 /* throw away oldest input if the buffer is full */ 842 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) { 843 sce->cur += count; 844 if(sce->cur >= sce->limit) 845 sce->cur = sce->ibuf + (sce->limit - sce->cur); 846 DPRINTFN(5, ("%s: throwing away %d bytes\n", __func__, count)); 847 } 848 849 isize = UGETW(sce->edesc->wMaxPacketSize); 850 for (i = 0; i < UGEN_NISORFRMS; i++) { 851 u_int32_t actlen = req->sizes[i]; 852 char const *buf = (char const *)req->dmabuf + isize * i; 853 854 /* copy data to buffer */ 855 while (actlen > 0) { 856 n = min(actlen, sce->limit - sce->fill); 857 memcpy(sce->fill, buf, n); 858 859 buf += n; 860 actlen -= n; 861 sce->fill += n; 862 if(sce->fill == sce->limit) 863 sce->fill = sce->ibuf; 864 } 865 866 /* setup size for next transfer */ 867 req->sizes[i] = isize; 868 } 869 870 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS, 871 USBD_NO_COPY | USBD_SHORT_XFER_OK, ugen_isoc_rintr); 872 (void)usbd_transfer(xfer); 873 874 if (sce->state & UGEN_ASLP) { 875 sce->state &= ~UGEN_ASLP; 876 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce)); 877 wakeup(sce); 878 } 879 selwakeup(&sce->rsel); 880 } 881 882 int 883 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno) 884 { 885 struct usbd_interface *iface; 886 usb_config_descriptor_t *cdesc; 887 usb_interface_descriptor_t *id; 888 usb_endpoint_descriptor_t *ed; 889 struct ugen_endpoint *sce; 890 uint8_t endptno, endpt; 891 int dir, err; 892 893 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno)); 894 895 cdesc = usbd_get_config_descriptor(sc->sc_udev); 896 if (ifaceidx < 0 || ifaceidx >= cdesc->bNumInterface || 897 usbd_iface_claimed(sc->sc_udev, ifaceidx)) 898 return (USBD_INVAL); 899 900 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface); 901 if (err) 902 return (err); 903 id = usbd_get_interface_descriptor(iface); 904 for (endptno = 0; endptno < id->bNumEndpoints; endptno++) { 905 ed = usbd_interface2endpoint_descriptor(iface,endptno); 906 endpt = ed->bEndpointAddress; 907 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 908 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 909 sce->sc = 0; 910 sce->edesc = 0; 911 sce->iface = 0; 912 } 913 914 /* Try to change setting, if this fails put back the descriptors. */ 915 err = usbd_set_interface(iface, altno); 916 917 id = usbd_get_interface_descriptor(iface); 918 for (endptno = 0; endptno < id->bNumEndpoints; endptno++) { 919 ed = usbd_interface2endpoint_descriptor(iface,endptno); 920 endpt = ed->bEndpointAddress; 921 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 922 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 923 sce->sc = sc; 924 sce->edesc = ed; 925 sce->iface = iface; 926 } 927 return (err); 928 } 929 930 int 931 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx) 932 { 933 struct usbd_interface *iface; 934 usbd_status err; 935 936 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface); 937 if (err) 938 return (-1); 939 return (usbd_get_interface_altindex(iface)); 940 } 941 942 int 943 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd, caddr_t addr, 944 int flag, struct proc *p) 945 { 946 struct ugen_endpoint *sce; 947 int err; 948 struct usbd_interface *iface; 949 struct usb_config_desc *cd; 950 usb_config_descriptor_t *cdesc; 951 struct usb_interface_desc *id; 952 usb_interface_descriptor_t *idesc; 953 struct usb_endpoint_desc *ed; 954 usb_endpoint_descriptor_t *edesc; 955 struct usb_alt_interface *ai; 956 u_int8_t conf, alt; 957 958 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd)); 959 if (usbd_is_dying(sc->sc_udev)) 960 return (EIO); 961 962 switch (cmd) { 963 case FIONBIO: 964 /* All handled in the upper FS layer. */ 965 return (0); 966 case USB_SET_SHORT_XFER: 967 if (endpt == USB_CONTROL_ENDPOINT) 968 return (EINVAL); 969 /* This flag only affects read */ 970 sce = &sc->sc_endpoints[endpt][IN]; 971 if (sce == NULL || sce->pipeh == NULL) 972 return (EINVAL); 973 if (*(int *)addr) 974 sce->state |= UGEN_SHORT_OK; 975 else 976 sce->state &= ~UGEN_SHORT_OK; 977 return (0); 978 case USB_SET_TIMEOUT: 979 sce = &sc->sc_endpoints[endpt][IN]; 980 if (sce == NULL) 981 return (EINVAL); 982 sce->timeout = *(int *)addr; 983 sce = &sc->sc_endpoints[endpt][OUT]; 984 if (sce == NULL) 985 return (EINVAL); 986 sce->timeout = *(int *)addr; 987 return (0); 988 default: 989 break; 990 } 991 992 if (endpt != USB_CONTROL_ENDPOINT) 993 return (EINVAL); 994 995 switch (cmd) { 996 #ifdef UGEN_DEBUG 997 case USB_SETDEBUG: 998 ugendebug = *(int *)addr; 999 break; 1000 #endif 1001 case USB_GET_CONFIG: 1002 err = usbd_get_config(sc->sc_udev, &conf); 1003 if (err) 1004 return (EIO); 1005 *(int *)addr = conf; 1006 break; 1007 case USB_SET_CONFIG: 1008 if (!(flag & FWRITE)) 1009 return (EPERM); 1010 err = ugen_set_config(sc, *(int *)addr); 1011 switch (err) { 1012 case USBD_NORMAL_COMPLETION: 1013 break; 1014 case USBD_IN_USE: 1015 return (EBUSY); 1016 default: 1017 return (EIO); 1018 } 1019 break; 1020 case USB_GET_ALTINTERFACE: 1021 ai = (struct usb_alt_interface *)addr; 1022 err = usbd_device2interface_handle(sc->sc_udev, 1023 ai->uai_interface_index, &iface); 1024 if (err) 1025 return (EINVAL); 1026 idesc = usbd_get_interface_descriptor(iface); 1027 if (idesc == NULL) 1028 return (EIO); 1029 ai->uai_alt_no = idesc->bAlternateSetting; 1030 break; 1031 case USB_SET_ALTINTERFACE: 1032 if (!(flag & FWRITE)) 1033 return (EPERM); 1034 ai = (struct usb_alt_interface *)addr; 1035 err = usbd_device2interface_handle(sc->sc_udev, 1036 ai->uai_interface_index, &iface); 1037 if (err) 1038 return (EINVAL); 1039 err = ugen_set_interface(sc, ai->uai_interface_index, 1040 ai->uai_alt_no); 1041 if (err) 1042 return (EINVAL); 1043 break; 1044 case USB_GET_NO_ALT: 1045 ai = (struct usb_alt_interface *)addr; 1046 cdesc = usbd_get_cdesc(sc->sc_udev, ai->uai_config_index, 0); 1047 if (cdesc == NULL) 1048 return (EINVAL); 1049 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0); 1050 if (idesc == NULL) { 1051 free(cdesc, M_TEMP, 0); 1052 return (EINVAL); 1053 } 1054 ai->uai_alt_no = usbd_get_no_alts(cdesc, 1055 idesc->bInterfaceNumber); 1056 free(cdesc, M_TEMP, 0); 1057 break; 1058 case USB_GET_DEVICE_DESC: 1059 *(usb_device_descriptor_t *)addr = 1060 *usbd_get_device_descriptor(sc->sc_udev); 1061 break; 1062 case USB_GET_CONFIG_DESC: 1063 cd = (struct usb_config_desc *)addr; 1064 cdesc = usbd_get_cdesc(sc->sc_udev, cd->ucd_config_index, 0); 1065 if (cdesc == NULL) 1066 return (EINVAL); 1067 cd->ucd_desc = *cdesc; 1068 free(cdesc, M_TEMP, 0); 1069 break; 1070 case USB_GET_INTERFACE_DESC: 1071 id = (struct usb_interface_desc *)addr; 1072 cdesc = usbd_get_cdesc(sc->sc_udev, id->uid_config_index, 0); 1073 if (cdesc == NULL) 1074 return (EINVAL); 1075 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX && 1076 id->uid_alt_index == USB_CURRENT_ALT_INDEX) 1077 alt = ugen_get_alt_index(sc, id->uid_interface_index); 1078 else 1079 alt = id->uid_alt_index; 1080 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt); 1081 if (idesc == NULL) { 1082 free(cdesc, M_TEMP, 0); 1083 return (EINVAL); 1084 } 1085 id->uid_desc = *idesc; 1086 free(cdesc, M_TEMP, 0); 1087 break; 1088 case USB_GET_ENDPOINT_DESC: 1089 ed = (struct usb_endpoint_desc *)addr; 1090 cdesc = usbd_get_cdesc(sc->sc_udev, ed->ued_config_index, 0); 1091 if (cdesc == NULL) 1092 return (EINVAL); 1093 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX && 1094 ed->ued_alt_index == USB_CURRENT_ALT_INDEX) 1095 alt = ugen_get_alt_index(sc, ed->ued_interface_index); 1096 else 1097 alt = ed->ued_alt_index; 1098 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index, 1099 alt, ed->ued_endpoint_index); 1100 if (edesc == NULL) { 1101 free(cdesc, M_TEMP, 0); 1102 return (EINVAL); 1103 } 1104 ed->ued_desc = *edesc; 1105 free(cdesc, M_TEMP, 0); 1106 break; 1107 case USB_GET_FULL_DESC: 1108 { 1109 u_int len; 1110 struct iovec iov; 1111 struct uio uio; 1112 struct usb_full_desc *fd = (struct usb_full_desc *)addr; 1113 int error; 1114 1115 cdesc = usbd_get_cdesc(sc->sc_udev, fd->ufd_config_index, &len); 1116 if (cdesc == NULL) 1117 return (EINVAL); 1118 if (len > fd->ufd_size) 1119 len = fd->ufd_size; 1120 iov.iov_base = (caddr_t)fd->ufd_data; 1121 iov.iov_len = len; 1122 uio.uio_iov = &iov; 1123 uio.uio_iovcnt = 1; 1124 uio.uio_resid = len; 1125 uio.uio_offset = 0; 1126 uio.uio_segflg = UIO_USERSPACE; 1127 uio.uio_rw = UIO_READ; 1128 uio.uio_procp = p; 1129 error = uiomove((void *)cdesc, len, &uio); 1130 free(cdesc, M_TEMP, 0); 1131 return (error); 1132 } 1133 case USB_DO_REQUEST: 1134 { 1135 struct usb_ctl_request *ur = (void *)addr; 1136 size_t len = UGETW(ur->ucr_request.wLength); 1137 struct iovec iov; 1138 struct uio uio; 1139 void *ptr = 0; 1140 int error = 0; 1141 1142 if (!(flag & FWRITE)) 1143 return (EPERM); 1144 /* Avoid requests that would damage the bus integrity. */ 1145 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 1146 ur->ucr_request.bRequest == UR_SET_ADDRESS) || 1147 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 1148 ur->ucr_request.bRequest == UR_SET_CONFIG) || 1149 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE && 1150 ur->ucr_request.bRequest == UR_SET_INTERFACE)) 1151 return (EINVAL); 1152 1153 if (len > 32767) 1154 return (EINVAL); 1155 if (len != 0) { 1156 iov.iov_base = (caddr_t)ur->ucr_data; 1157 iov.iov_len = len; 1158 uio.uio_iov = &iov; 1159 uio.uio_iovcnt = 1; 1160 uio.uio_resid = len; 1161 uio.uio_offset = 0; 1162 uio.uio_segflg = UIO_USERSPACE; 1163 uio.uio_rw = 1164 ur->ucr_request.bmRequestType & UT_READ ? 1165 UIO_READ : UIO_WRITE; 1166 uio.uio_procp = p; 1167 ptr = malloc(len, M_TEMP, M_WAITOK); 1168 if (uio.uio_rw == UIO_WRITE) { 1169 error = uiomove(ptr, len, &uio); 1170 if (error) 1171 goto ret; 1172 } 1173 } 1174 sce = &sc->sc_endpoints[endpt][IN]; 1175 err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request, 1176 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout); 1177 if (err) { 1178 error = EIO; 1179 goto ret; 1180 } 1181 /* Only if USBD_SHORT_XFER_OK is set. */ 1182 if (len > ur->ucr_actlen) 1183 len = ur->ucr_actlen; 1184 if (len != 0) { 1185 if (uio.uio_rw == UIO_READ) { 1186 error = uiomove(ptr, len, &uio); 1187 if (error) 1188 goto ret; 1189 } 1190 } 1191 ret: 1192 if (ptr) 1193 free(ptr, M_TEMP, 0); 1194 return (error); 1195 } 1196 case USB_GET_DEVICEINFO: 1197 usbd_fill_deviceinfo(sc->sc_udev, 1198 (struct usb_device_info *)addr, 1); 1199 break; 1200 default: 1201 return (EINVAL); 1202 } 1203 return (0); 1204 } 1205 1206 int 1207 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1208 { 1209 int endpt = UGENENDPOINT(dev); 1210 struct ugen_softc *sc; 1211 int error; 1212 1213 sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 1214 1215 sc->sc_refcnt++; 1216 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p); 1217 if (--sc->sc_refcnt < 0) 1218 usb_detach_wakeup(&sc->sc_dev); 1219 return (error); 1220 } 1221 1222 int 1223 ugenpoll(dev_t dev, int events, struct proc *p) 1224 { 1225 struct ugen_softc *sc; 1226 struct ugen_endpoint *sce; 1227 int revents = 0; 1228 int s; 1229 1230 sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 1231 1232 if (usbd_is_dying(sc->sc_udev)) 1233 return (POLLERR); 1234 1235 /* XXX always IN */ 1236 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN]; 1237 if (sce == NULL) 1238 return (POLLERR); 1239 #ifdef DIAGNOSTIC 1240 if (!sce->edesc) { 1241 printf("ugenpoll: no edesc\n"); 1242 return (POLLERR); 1243 } 1244 if (!sce->pipeh) { 1245 printf("ugenpoll: no pipe\n"); 1246 return (POLLERR); 1247 } 1248 #endif 1249 s = splusb(); 1250 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 1251 case UE_INTERRUPT: 1252 if (events & (POLLIN | POLLRDNORM)) { 1253 if (sce->q.c_cc > 0) 1254 revents |= events & (POLLIN | POLLRDNORM); 1255 else 1256 selrecord(p, &sce->rsel); 1257 } 1258 break; 1259 case UE_ISOCHRONOUS: 1260 if (events & (POLLIN | POLLRDNORM)) { 1261 if (sce->cur != sce->fill) 1262 revents |= events & (POLLIN | POLLRDNORM); 1263 else 1264 selrecord(p, &sce->rsel); 1265 } 1266 break; 1267 case UE_BULK: 1268 /* 1269 * We have no easy way of determining if a read will 1270 * yield any data or a write will happen. 1271 * Pretend they will. 1272 */ 1273 revents |= events & 1274 (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM); 1275 break; 1276 default: 1277 break; 1278 } 1279 splx(s); 1280 return (revents); 1281 } 1282 1283 void filt_ugenrdetach(struct knote *); 1284 int filt_ugenread_intr(struct knote *, long); 1285 int filt_ugenread_isoc(struct knote *, long); 1286 int ugenkqfilter(dev_t, struct knote *); 1287 1288 void 1289 filt_ugenrdetach(struct knote *kn) 1290 { 1291 struct ugen_endpoint *sce = (void *)kn->kn_hook; 1292 int s; 1293 1294 s = splusb(); 1295 SLIST_REMOVE(&sce->rsel.si_note, kn, knote, kn_selnext); 1296 splx(s); 1297 } 1298 1299 int 1300 filt_ugenread_intr(struct knote *kn, long hint) 1301 { 1302 struct ugen_endpoint *sce = (void *)kn->kn_hook; 1303 1304 kn->kn_data = sce->q.c_cc; 1305 return (kn->kn_data > 0); 1306 } 1307 1308 int 1309 filt_ugenread_isoc(struct knote *kn, long hint) 1310 { 1311 struct ugen_endpoint *sce = (void *)kn->kn_hook; 1312 1313 if (sce->cur == sce->fill) 1314 return (0); 1315 1316 if (sce->cur < sce->fill) 1317 kn->kn_data = sce->fill - sce->cur; 1318 else 1319 kn->kn_data = (sce->limit - sce->cur) + 1320 (sce->fill - sce->ibuf); 1321 1322 return (1); 1323 } 1324 1325 struct filterops ugenread_intr_filtops = 1326 { 1, NULL, filt_ugenrdetach, filt_ugenread_intr }; 1327 1328 struct filterops ugenread_isoc_filtops = 1329 { 1, NULL, filt_ugenrdetach, filt_ugenread_isoc }; 1330 1331 struct filterops ugen_seltrue_filtops = 1332 { 1, NULL, filt_ugenrdetach, filt_seltrue }; 1333 1334 int 1335 ugenkqfilter(dev_t dev, struct knote *kn) 1336 { 1337 struct ugen_softc *sc; 1338 struct ugen_endpoint *sce; 1339 struct klist *klist; 1340 int s; 1341 1342 sc = ugen_cd.cd_devs[UGENUNIT(dev)]; 1343 1344 if (usbd_is_dying(sc->sc_udev)) 1345 return (ENXIO); 1346 1347 /* XXX always IN */ 1348 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN]; 1349 if (sce == NULL) 1350 return (EINVAL); 1351 1352 switch (kn->kn_filter) { 1353 case EVFILT_READ: 1354 klist = &sce->rsel.si_note; 1355 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 1356 case UE_INTERRUPT: 1357 kn->kn_fop = &ugenread_intr_filtops; 1358 break; 1359 case UE_ISOCHRONOUS: 1360 kn->kn_fop = &ugenread_isoc_filtops; 1361 break; 1362 case UE_BULK: 1363 /* 1364 * We have no easy way of determining if a read will 1365 * yield any data or a write will happen. 1366 * So, emulate "seltrue". 1367 */ 1368 kn->kn_fop = &ugen_seltrue_filtops; 1369 break; 1370 default: 1371 return (EINVAL); 1372 } 1373 break; 1374 1375 case EVFILT_WRITE: 1376 klist = &sce->rsel.si_note; 1377 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 1378 case UE_INTERRUPT: 1379 case UE_ISOCHRONOUS: 1380 /* XXX poll doesn't support this */ 1381 return (EINVAL); 1382 1383 case UE_BULK: 1384 /* 1385 * We have no easy way of determining if a read will 1386 * yield any data or a write will happen. 1387 * So, emulate "seltrue". 1388 */ 1389 kn->kn_fop = &ugen_seltrue_filtops; 1390 break; 1391 default: 1392 return (EINVAL); 1393 } 1394 break; 1395 1396 default: 1397 return (EINVAL); 1398 } 1399 1400 kn->kn_hook = (void *)sce; 1401 1402 s = splusb(); 1403 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1404 splx(s); 1405 1406 return (0); 1407 } 1408