1 /* $NetBSD: ugen.c,v 1.92 2007/03/04 06:02:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2004 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@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Copyright (c) 2006 BBN Technologies Corp. All rights reserved. 12 * Effort sponsored in part by the Defense Advanced Research Projects 13 * Agency (DARPA) and the Department of the Interior National Business 14 * Center under agreement number NBCHC050166. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the NetBSD 27 * Foundation, Inc. and its contributors. 28 * 4. Neither the name of The NetBSD Foundation nor the names of its 29 * contributors may be used to endorse or promote products derived 30 * from this software without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 33 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 34 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 36 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42 * POSSIBILITY OF SUCH DAMAGE. 43 */ 44 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.92 2007/03/04 06:02:49 christos Exp $"); 48 49 #include "opt_ugen_bulk_ra_wb.h" 50 #include "opt_compat_netbsd.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/malloc.h> 56 #if defined(__NetBSD__) || defined(__OpenBSD__) 57 #include <sys/device.h> 58 #include <sys/ioctl.h> 59 #elif defined(__FreeBSD__) 60 #include <sys/module.h> 61 #include <sys/bus.h> 62 #include <sys/ioccom.h> 63 #include <sys/conf.h> 64 #include <sys/fcntl.h> 65 #include <sys/filio.h> 66 #endif 67 #include <sys/conf.h> 68 #include <sys/tty.h> 69 #include <sys/file.h> 70 #include <sys/select.h> 71 #include <sys/proc.h> 72 #include <sys/vnode.h> 73 #include <sys/poll.h> 74 75 #include <dev/usb/usb.h> 76 #include <dev/usb/usbdi.h> 77 #include <dev/usb/usbdi_util.h> 78 79 #ifdef UGEN_DEBUG 80 #define DPRINTF(x) if (ugendebug) logprintf x 81 #define DPRINTFN(n,x) if (ugendebug>(n)) logprintf x 82 int ugendebug = 0; 83 #else 84 #define DPRINTF(x) 85 #define DPRINTFN(n,x) 86 #endif 87 88 #define UGEN_CHUNK 128 /* chunk size for read */ 89 #define UGEN_IBSIZE 1020 /* buffer size */ 90 #define UGEN_BBSIZE 1024 91 92 #define UGEN_NISOFRAMES 500 /* 0.5 seconds worth */ 93 #define UGEN_NISOREQS 6 /* number of outstanding xfer requests */ 94 #define UGEN_NISORFRMS 4 /* number of frames (miliseconds) per req */ 95 96 #define UGEN_BULK_RA_WB_BUFSIZE 16384 /* default buffer size */ 97 #define UGEN_BULK_RA_WB_BUFMAX (1 << 20) /* maximum allowed buffer */ 98 99 struct ugen_endpoint { 100 struct ugen_softc *sc; 101 usb_endpoint_descriptor_t *edesc; 102 usbd_interface_handle iface; 103 int state; 104 #define UGEN_ASLP 0x02 /* waiting for data */ 105 #define UGEN_SHORT_OK 0x04 /* short xfers are OK */ 106 #define UGEN_BULK_RA 0x08 /* in bulk read-ahead mode */ 107 #define UGEN_BULK_WB 0x10 /* in bulk write-behind mode */ 108 #define UGEN_RA_WB_STOP 0x20 /* RA/WB xfer is stopped (buffer full/empty) */ 109 usbd_pipe_handle pipeh; 110 struct clist q; 111 struct selinfo rsel; 112 u_char *ibuf; /* start of buffer (circular for isoc) */ 113 u_char *fill; /* location for input (isoc) */ 114 u_char *limit; /* end of circular buffer (isoc) */ 115 u_char *cur; /* current read location (isoc) */ 116 u_int32_t timeout; 117 #ifdef UGEN_BULK_RA_WB 118 u_int32_t ra_wb_bufsize; /* requested size for RA/WB buffer */ 119 u_int32_t ra_wb_reqsize; /* requested xfer length for RA/WB */ 120 u_int32_t ra_wb_used; /* how much is in buffer */ 121 u_int32_t ra_wb_xferlen; /* current xfer length for RA/WB */ 122 usbd_xfer_handle ra_wb_xfer; 123 #endif 124 struct isoreq { 125 struct ugen_endpoint *sce; 126 usbd_xfer_handle xfer; 127 void *dmabuf; 128 u_int16_t sizes[UGEN_NISORFRMS]; 129 } isoreqs[UGEN_NISOREQS]; 130 }; 131 132 struct ugen_softc { 133 USBBASEDEVICE sc_dev; /* base device */ 134 usbd_device_handle sc_udev; 135 136 char sc_is_open[USB_MAX_ENDPOINTS]; 137 struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2]; 138 #define OUT 0 139 #define IN 1 140 141 int sc_refcnt; 142 char sc_buffer[UGEN_BBSIZE]; 143 u_char sc_dying; 144 }; 145 146 #if defined(__NetBSD__) 147 dev_type_open(ugenopen); 148 dev_type_close(ugenclose); 149 dev_type_read(ugenread); 150 dev_type_write(ugenwrite); 151 dev_type_ioctl(ugenioctl); 152 dev_type_poll(ugenpoll); 153 dev_type_kqfilter(ugenkqfilter); 154 155 const struct cdevsw ugen_cdevsw = { 156 ugenopen, ugenclose, ugenread, ugenwrite, ugenioctl, 157 nostop, notty, ugenpoll, nommap, ugenkqfilter, D_OTHER, 158 }; 159 #elif defined(__OpenBSD__) 160 cdev_decl(ugen); 161 #elif defined(__FreeBSD__) 162 d_open_t ugenopen; 163 d_close_t ugenclose; 164 d_read_t ugenread; 165 d_write_t ugenwrite; 166 d_ioctl_t ugenioctl; 167 d_poll_t ugenpoll; 168 169 #define UGEN_CDEV_MAJOR 114 170 171 Static struct cdevsw ugen_cdevsw = { 172 /* open */ ugenopen, 173 /* close */ ugenclose, 174 /* read */ ugenread, 175 /* write */ ugenwrite, 176 /* ioctl */ ugenioctl, 177 /* poll */ ugenpoll, 178 /* mmap */ nommap, 179 /* strategy */ nostrategy, 180 /* name */ "ugen", 181 /* maj */ UGEN_CDEV_MAJOR, 182 /* dump */ nodump, 183 /* psize */ nopsize, 184 /* flags */ 0, 185 /* bmaj */ -1 186 }; 187 #endif 188 189 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, 190 usbd_status status); 191 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr, 192 usbd_status status); 193 #ifdef UGEN_BULK_RA_WB 194 Static void ugen_bulkra_intr(usbd_xfer_handle xfer, usbd_private_handle addr, 195 usbd_status status); 196 Static void ugen_bulkwb_intr(usbd_xfer_handle xfer, usbd_private_handle addr, 197 usbd_status status); 198 #endif 199 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int); 200 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int); 201 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long, 202 void *, int, struct lwp *); 203 Static int ugen_set_config(struct ugen_softc *sc, int configno); 204 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc, 205 int index, int *lenp); 206 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int); 207 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx); 208 209 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf) 210 #define UGENENDPOINT(n) (minor(n) & 0xf) 211 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e))) 212 213 USB_DECLARE_DRIVER(ugen); 214 215 USB_MATCH(ugen) 216 { 217 USB_MATCH_START(ugen, uaa); 218 219 if (match->cf_flags & 1) 220 return (UMATCH_HIGHEST); 221 else if (uaa->usegeneric) 222 return (UMATCH_GENERIC); 223 else 224 return (UMATCH_NONE); 225 } 226 227 USB_ATTACH(ugen) 228 { 229 USB_ATTACH_START(ugen, sc, uaa); 230 usbd_device_handle udev; 231 char *devinfop; 232 usbd_status err; 233 int conf; 234 235 devinfop = usbd_devinfo_alloc(uaa->device, 0); 236 USB_ATTACH_SETUP; 237 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); 238 usbd_devinfo_free(devinfop); 239 240 sc->sc_udev = udev = uaa->device; 241 242 /* First set configuration index 0, the default one for ugen. */ 243 err = usbd_set_config_index(udev, 0, 0); 244 if (err) { 245 printf("%s: setting configuration index 0 failed\n", 246 USBDEVNAME(sc->sc_dev)); 247 sc->sc_dying = 1; 248 USB_ATTACH_ERROR_RETURN; 249 } 250 conf = usbd_get_config_descriptor(udev)->bConfigurationValue; 251 252 /* Set up all the local state for this configuration. */ 253 err = ugen_set_config(sc, conf); 254 if (err) { 255 printf("%s: setting configuration %d failed\n", 256 USBDEVNAME(sc->sc_dev), conf); 257 sc->sc_dying = 1; 258 USB_ATTACH_ERROR_RETURN; 259 } 260 261 #ifdef __FreeBSD__ 262 { 263 static int global_init_done = 0; 264 if (!global_init_done) { 265 cdevsw_add(&ugen_cdevsw); 266 global_init_done = 1; 267 } 268 } 269 #endif 270 271 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 272 USBDEV(sc->sc_dev)); 273 274 USB_ATTACH_SUCCESS_RETURN; 275 } 276 277 Static int 278 ugen_set_config(struct ugen_softc *sc, int configno) 279 { 280 usbd_device_handle dev = sc->sc_udev; 281 usbd_interface_handle iface; 282 usb_endpoint_descriptor_t *ed; 283 struct ugen_endpoint *sce; 284 u_int8_t niface, nendpt; 285 int ifaceno, endptno, endpt; 286 usbd_status err; 287 int dir; 288 289 DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n", 290 USBDEVNAME(sc->sc_dev), configno, sc)); 291 292 /* 293 * We start at 1, not 0, because we don't care whether the 294 * control endpoint is open or not. It is always present. 295 */ 296 for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++) 297 if (sc->sc_is_open[endptno]) { 298 DPRINTFN(1, 299 ("ugen_set_config: %s - endpoint %d is open\n", 300 USBDEVNAME(sc->sc_dev), endptno)); 301 return (USBD_IN_USE); 302 } 303 304 /* Avoid setting the current value. */ 305 if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) { 306 err = usbd_set_config_no(dev, configno, 1); 307 if (err) 308 return (err); 309 } 310 311 err = usbd_interface_count(dev, &niface); 312 if (err) 313 return (err); 314 memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints); 315 for (ifaceno = 0; ifaceno < niface; ifaceno++) { 316 DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno)); 317 err = usbd_device2interface_handle(dev, ifaceno, &iface); 318 if (err) 319 return (err); 320 err = usbd_endpoint_count(iface, &nendpt); 321 if (err) 322 return (err); 323 for (endptno = 0; endptno < nendpt; endptno++) { 324 ed = usbd_interface2endpoint_descriptor(iface,endptno); 325 KASSERT(ed != NULL); 326 endpt = ed->bEndpointAddress; 327 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 328 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 329 DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x" 330 "(%d,%d), sce=%p\n", 331 endptno, endpt, UE_GET_ADDR(endpt), 332 UE_GET_DIR(endpt), sce)); 333 sce->sc = sc; 334 sce->edesc = ed; 335 sce->iface = iface; 336 } 337 } 338 return (USBD_NORMAL_COMPLETION); 339 } 340 341 int 342 ugenopen(dev_t dev, int flag, int mode, struct lwp *l) 343 { 344 struct ugen_softc *sc; 345 int unit = UGENUNIT(dev); 346 int endpt = UGENENDPOINT(dev); 347 usb_endpoint_descriptor_t *edesc; 348 struct ugen_endpoint *sce; 349 int dir, isize; 350 usbd_status err; 351 usbd_xfer_handle xfer; 352 void *tbuf; 353 int i, j; 354 355 USB_GET_SC_OPEN(ugen, unit, sc); 356 357 DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n", 358 flag, mode, unit, endpt)); 359 360 if (sc == NULL || sc->sc_dying) 361 return (ENXIO); 362 363 /* The control endpoint allows multiple opens. */ 364 if (endpt == USB_CONTROL_ENDPOINT) { 365 sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1; 366 return (0); 367 } 368 369 if (sc->sc_is_open[endpt]) 370 return (EBUSY); 371 372 /* Make sure there are pipes for all directions. */ 373 for (dir = OUT; dir <= IN; dir++) { 374 if (flag & (dir == OUT ? FWRITE : FREAD)) { 375 sce = &sc->sc_endpoints[endpt][dir]; 376 if (sce == 0 || sce->edesc == 0) 377 return (ENXIO); 378 } 379 } 380 381 /* Actually open the pipes. */ 382 /* XXX Should back out properly if it fails. */ 383 for (dir = OUT; dir <= IN; dir++) { 384 if (!(flag & (dir == OUT ? FWRITE : FREAD))) 385 continue; 386 sce = &sc->sc_endpoints[endpt][dir]; 387 sce->state = 0; 388 sce->timeout = USBD_NO_TIMEOUT; 389 DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n", 390 sc, endpt, dir, sce)); 391 edesc = sce->edesc; 392 switch (edesc->bmAttributes & UE_XFERTYPE) { 393 case UE_INTERRUPT: 394 if (dir == OUT) { 395 err = usbd_open_pipe(sce->iface, 396 edesc->bEndpointAddress, 0, &sce->pipeh); 397 if (err) 398 return (EIO); 399 break; 400 } 401 isize = UGETW(edesc->wMaxPacketSize); 402 if (isize == 0) /* shouldn't happen */ 403 return (EINVAL); 404 sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK); 405 DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n", 406 endpt, isize)); 407 if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1) 408 return (ENOMEM); 409 err = usbd_open_pipe_intr(sce->iface, 410 edesc->bEndpointAddress, 411 USBD_SHORT_XFER_OK, &sce->pipeh, sce, 412 sce->ibuf, isize, ugenintr, 413 USBD_DEFAULT_INTERVAL); 414 if (err) { 415 free(sce->ibuf, M_USBDEV); 416 clfree(&sce->q); 417 return (EIO); 418 } 419 DPRINTFN(5, ("ugenopen: interrupt open done\n")); 420 break; 421 case UE_BULK: 422 err = usbd_open_pipe(sce->iface, 423 edesc->bEndpointAddress, 0, &sce->pipeh); 424 if (err) 425 return (EIO); 426 #ifdef UGEN_BULK_RA_WB 427 sce->ra_wb_bufsize = UGEN_BULK_RA_WB_BUFSIZE; 428 /* 429 * Use request size for non-RA/WB transfers 430 * as the default. 431 */ 432 sce->ra_wb_reqsize = UGEN_BBSIZE; 433 #endif 434 break; 435 case UE_ISOCHRONOUS: 436 if (dir == OUT) 437 return (EINVAL); 438 isize = UGETW(edesc->wMaxPacketSize); 439 if (isize == 0) /* shouldn't happen */ 440 return (EINVAL); 441 sce->ibuf = malloc(isize * UGEN_NISOFRAMES, 442 M_USBDEV, M_WAITOK); 443 sce->cur = sce->fill = sce->ibuf; 444 sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES; 445 DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n", 446 endpt, isize)); 447 err = usbd_open_pipe(sce->iface, 448 edesc->bEndpointAddress, 0, &sce->pipeh); 449 if (err) { 450 free(sce->ibuf, M_USBDEV); 451 return (EIO); 452 } 453 for(i = 0; i < UGEN_NISOREQS; ++i) { 454 sce->isoreqs[i].sce = sce; 455 xfer = usbd_alloc_xfer(sc->sc_udev); 456 if (xfer == 0) 457 goto bad; 458 sce->isoreqs[i].xfer = xfer; 459 tbuf = usbd_alloc_buffer 460 (xfer, isize * UGEN_NISORFRMS); 461 if (tbuf == 0) { 462 i++; 463 goto bad; 464 } 465 sce->isoreqs[i].dmabuf = tbuf; 466 for(j = 0; j < UGEN_NISORFRMS; ++j) 467 sce->isoreqs[i].sizes[j] = isize; 468 usbd_setup_isoc_xfer 469 (xfer, sce->pipeh, &sce->isoreqs[i], 470 sce->isoreqs[i].sizes, 471 UGEN_NISORFRMS, USBD_NO_COPY, 472 ugen_isoc_rintr); 473 (void)usbd_transfer(xfer); 474 } 475 DPRINTFN(5, ("ugenopen: isoc open done\n")); 476 break; 477 bad: 478 while (--i >= 0) /* implicit buffer free */ 479 usbd_free_xfer(sce->isoreqs[i].xfer); 480 return (ENOMEM); 481 case UE_CONTROL: 482 sce->timeout = USBD_DEFAULT_TIMEOUT; 483 return (EINVAL); 484 } 485 } 486 sc->sc_is_open[endpt] = 1; 487 return (0); 488 } 489 490 int 491 ugenclose(dev_t dev, int flag, int mode, struct lwp *l) 492 { 493 int endpt = UGENENDPOINT(dev); 494 struct ugen_softc *sc; 495 struct ugen_endpoint *sce; 496 int dir; 497 int i; 498 499 USB_GET_SC(ugen, UGENUNIT(dev), sc); 500 501 DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n", 502 flag, mode, UGENUNIT(dev), endpt)); 503 504 #ifdef DIAGNOSTIC 505 if (!sc->sc_is_open[endpt]) { 506 printf("ugenclose: not open\n"); 507 return (EINVAL); 508 } 509 #endif 510 511 if (endpt == USB_CONTROL_ENDPOINT) { 512 DPRINTFN(5, ("ugenclose: close control\n")); 513 sc->sc_is_open[endpt] = 0; 514 return (0); 515 } 516 517 for (dir = OUT; dir <= IN; dir++) { 518 if (!(flag & (dir == OUT ? FWRITE : FREAD))) 519 continue; 520 sce = &sc->sc_endpoints[endpt][dir]; 521 if (sce == NULL || sce->pipeh == NULL) 522 continue; 523 DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n", 524 endpt, dir, sce)); 525 526 usbd_abort_pipe(sce->pipeh); 527 usbd_close_pipe(sce->pipeh); 528 sce->pipeh = NULL; 529 530 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 531 case UE_INTERRUPT: 532 ndflush(&sce->q, sce->q.c_cc); 533 clfree(&sce->q); 534 break; 535 case UE_ISOCHRONOUS: 536 for (i = 0; i < UGEN_NISOREQS; ++i) 537 usbd_free_xfer(sce->isoreqs[i].xfer); 538 break; 539 #ifdef UGEN_BULK_RA_WB 540 case UE_BULK: 541 if (sce->state & (UGEN_BULK_RA | UGEN_BULK_WB)) 542 /* ibuf freed below */ 543 usbd_free_xfer(sce->ra_wb_xfer); 544 break; 545 #endif 546 default: 547 break; 548 } 549 550 if (sce->ibuf != NULL) { 551 free(sce->ibuf, M_USBDEV); 552 sce->ibuf = NULL; 553 clfree(&sce->q); 554 } 555 } 556 sc->sc_is_open[endpt] = 0; 557 558 return (0); 559 } 560 561 Static int 562 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag) 563 { 564 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN]; 565 u_int32_t n, tn; 566 usbd_xfer_handle xfer; 567 usbd_status err; 568 int s; 569 int error = 0; 570 571 DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt)); 572 573 if (sc->sc_dying) 574 return (EIO); 575 576 if (endpt == USB_CONTROL_ENDPOINT) 577 return (ENODEV); 578 579 #ifdef DIAGNOSTIC 580 if (sce->edesc == NULL) { 581 printf("ugenread: no edesc\n"); 582 return (EIO); 583 } 584 if (sce->pipeh == NULL) { 585 printf("ugenread: no pipe\n"); 586 return (EIO); 587 } 588 #endif 589 590 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 591 case UE_INTERRUPT: 592 /* Block until activity occurred. */ 593 s = splusb(); 594 while (sce->q.c_cc == 0) { 595 if (flag & IO_NDELAY) { 596 splx(s); 597 return (EWOULDBLOCK); 598 } 599 sce->state |= UGEN_ASLP; 600 DPRINTFN(5, ("ugenread: sleep on %p\n", sce)); 601 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0); 602 DPRINTFN(5, ("ugenread: woke, error=%d\n", error)); 603 if (sc->sc_dying) 604 error = EIO; 605 if (error) { 606 sce->state &= ~UGEN_ASLP; 607 break; 608 } 609 } 610 splx(s); 611 612 /* Transfer as many chunks as possible. */ 613 while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) { 614 n = min(sce->q.c_cc, uio->uio_resid); 615 if (n > sizeof(sc->sc_buffer)) 616 n = sizeof(sc->sc_buffer); 617 618 /* Remove a small chunk from the input queue. */ 619 q_to_b(&sce->q, sc->sc_buffer, n); 620 DPRINTFN(5, ("ugenread: got %d chars\n", n)); 621 622 /* Copy the data to the user process. */ 623 error = uiomove(sc->sc_buffer, n, uio); 624 if (error) 625 break; 626 } 627 break; 628 case UE_BULK: 629 #ifdef UGEN_BULK_RA_WB 630 if (sce->state & UGEN_BULK_RA) { 631 DPRINTFN(5, ("ugenread: BULK_RA req: %zd used: %d\n", 632 uio->uio_resid, sce->ra_wb_used)); 633 xfer = sce->ra_wb_xfer; 634 635 s = splusb(); 636 if (sce->ra_wb_used == 0 && flag & IO_NDELAY) { 637 splx(s); 638 return (EWOULDBLOCK); 639 } 640 while (uio->uio_resid > 0 && !error) { 641 while (sce->ra_wb_used == 0) { 642 sce->state |= UGEN_ASLP; 643 DPRINTFN(5, 644 ("ugenread: sleep on %p\n", 645 sce)); 646 error = tsleep(sce, PZERO | PCATCH, 647 "ugenrb", 0); 648 DPRINTFN(5, 649 ("ugenread: woke, error=%d\n", 650 error)); 651 if (sc->sc_dying) 652 error = EIO; 653 if (error) { 654 sce->state &= ~UGEN_ASLP; 655 break; 656 } 657 } 658 659 /* Copy data to the process. */ 660 while (uio->uio_resid > 0 661 && sce->ra_wb_used > 0) { 662 n = min(uio->uio_resid, 663 sce->ra_wb_used); 664 n = min(n, sce->limit - sce->cur); 665 error = uiomove(sce->cur, n, uio); 666 if (error) 667 break; 668 sce->cur += n; 669 sce->ra_wb_used -= n; 670 if (sce->cur == sce->limit) 671 sce->cur = sce->ibuf; 672 } 673 674 /* 675 * If the transfers stopped because the 676 * buffer was full, restart them. 677 */ 678 if (sce->state & UGEN_RA_WB_STOP && 679 sce->ra_wb_used < sce->limit - sce->ibuf) { 680 n = (sce->limit - sce->ibuf) 681 - sce->ra_wb_used; 682 usbd_setup_xfer(xfer, 683 sce->pipeh, sce, NULL, 684 min(n, sce->ra_wb_xferlen), 685 USBD_NO_COPY, USBD_NO_TIMEOUT, 686 ugen_bulkra_intr); 687 sce->state &= ~UGEN_RA_WB_STOP; 688 err = usbd_transfer(xfer); 689 if (err != USBD_IN_PROGRESS) 690 /* 691 * The transfer has not been 692 * queued. Setting STOP 693 * will make us try 694 * again at the next read. 695 */ 696 sce->state |= UGEN_RA_WB_STOP; 697 } 698 } 699 splx(s); 700 break; 701 } 702 #endif 703 xfer = usbd_alloc_xfer(sc->sc_udev); 704 if (xfer == 0) 705 return (ENOMEM); 706 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) { 707 DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n)); 708 tn = n; 709 err = usbd_bulk_transfer( 710 xfer, sce->pipeh, 711 sce->state & UGEN_SHORT_OK ? 712 USBD_SHORT_XFER_OK : 0, 713 sce->timeout, sc->sc_buffer, &tn, "ugenrb"); 714 if (err) { 715 if (err == USBD_INTERRUPTED) 716 error = EINTR; 717 else if (err == USBD_TIMEOUT) 718 error = ETIMEDOUT; 719 else 720 error = EIO; 721 break; 722 } 723 DPRINTFN(1, ("ugenread: got %d bytes\n", tn)); 724 error = uiomove(sc->sc_buffer, tn, uio); 725 if (error || tn < n) 726 break; 727 } 728 usbd_free_xfer(xfer); 729 break; 730 case UE_ISOCHRONOUS: 731 s = splusb(); 732 while (sce->cur == sce->fill) { 733 if (flag & IO_NDELAY) { 734 splx(s); 735 return (EWOULDBLOCK); 736 } 737 sce->state |= UGEN_ASLP; 738 DPRINTFN(5, ("ugenread: sleep on %p\n", sce)); 739 error = tsleep(sce, PZERO | PCATCH, "ugenri", 0); 740 DPRINTFN(5, ("ugenread: woke, error=%d\n", error)); 741 if (sc->sc_dying) 742 error = EIO; 743 if (error) { 744 sce->state &= ~UGEN_ASLP; 745 break; 746 } 747 } 748 749 while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) { 750 if(sce->fill > sce->cur) 751 n = min(sce->fill - sce->cur, uio->uio_resid); 752 else 753 n = min(sce->limit - sce->cur, uio->uio_resid); 754 755 DPRINTFN(5, ("ugenread: isoc got %d chars\n", n)); 756 757 /* Copy the data to the user process. */ 758 error = uiomove(sce->cur, n, uio); 759 if (error) 760 break; 761 sce->cur += n; 762 if(sce->cur >= sce->limit) 763 sce->cur = sce->ibuf; 764 } 765 splx(s); 766 break; 767 768 769 default: 770 return (ENXIO); 771 } 772 return (error); 773 } 774 775 int 776 ugenread(dev_t dev, struct uio *uio, int flag) 777 { 778 int endpt = UGENENDPOINT(dev); 779 struct ugen_softc *sc; 780 int error; 781 782 USB_GET_SC(ugen, UGENUNIT(dev), sc); 783 784 sc->sc_refcnt++; 785 error = ugen_do_read(sc, endpt, uio, flag); 786 if (--sc->sc_refcnt < 0) 787 usb_detach_wakeup(USBDEV(sc->sc_dev)); 788 return (error); 789 } 790 791 Static int 792 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, 793 int flag) 794 { 795 struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT]; 796 u_int32_t n; 797 int error = 0; 798 #ifdef UGEN_BULK_RA_WB 799 int s; 800 u_int32_t tn; 801 char *dbuf; 802 #endif 803 usbd_xfer_handle xfer; 804 usbd_status err; 805 806 DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt)); 807 808 if (sc->sc_dying) 809 return (EIO); 810 811 if (endpt == USB_CONTROL_ENDPOINT) 812 return (ENODEV); 813 814 #ifdef DIAGNOSTIC 815 if (sce->edesc == NULL) { 816 printf("ugenwrite: no edesc\n"); 817 return (EIO); 818 } 819 if (sce->pipeh == NULL) { 820 printf("ugenwrite: no pipe\n"); 821 return (EIO); 822 } 823 #endif 824 825 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 826 case UE_BULK: 827 #ifdef UGEN_BULK_RA_WB 828 if (sce->state & UGEN_BULK_WB) { 829 DPRINTFN(5, ("ugenwrite: BULK_WB req: %zd used: %d\n", 830 uio->uio_resid, sce->ra_wb_used)); 831 xfer = sce->ra_wb_xfer; 832 833 s = splusb(); 834 if (sce->ra_wb_used == sce->limit - sce->ibuf && 835 flag & IO_NDELAY) { 836 splx(s); 837 return (EWOULDBLOCK); 838 } 839 while (uio->uio_resid > 0 && !error) { 840 while (sce->ra_wb_used == 841 sce->limit - sce->ibuf) { 842 sce->state |= UGEN_ASLP; 843 DPRINTFN(5, 844 ("ugenwrite: sleep on %p\n", 845 sce)); 846 error = tsleep(sce, PZERO | PCATCH, 847 "ugenwb", 0); 848 DPRINTFN(5, 849 ("ugenwrite: woke, error=%d\n", 850 error)); 851 if (sc->sc_dying) 852 error = EIO; 853 if (error) { 854 sce->state &= ~UGEN_ASLP; 855 break; 856 } 857 } 858 859 /* Copy data from the process. */ 860 while (uio->uio_resid > 0 && 861 sce->ra_wb_used < sce->limit - sce->ibuf) { 862 n = min(uio->uio_resid, 863 (sce->limit - sce->ibuf) 864 - sce->ra_wb_used); 865 n = min(n, sce->limit - sce->fill); 866 error = uiomove(sce->fill, n, uio); 867 if (error) 868 break; 869 sce->fill += n; 870 sce->ra_wb_used += n; 871 if (sce->fill == sce->limit) 872 sce->fill = sce->ibuf; 873 } 874 875 /* 876 * If the transfers stopped because the 877 * buffer was empty, restart them. 878 */ 879 if (sce->state & UGEN_RA_WB_STOP && 880 sce->ra_wb_used > 0) { 881 dbuf = (char *)usbd_get_buffer(xfer); 882 n = min(sce->ra_wb_used, 883 sce->ra_wb_xferlen); 884 tn = min(n, sce->limit - sce->cur); 885 memcpy(dbuf, sce->cur, tn); 886 dbuf += tn; 887 if (n - tn > 0) 888 memcpy(dbuf, sce->ibuf, 889 n - tn); 890 usbd_setup_xfer(xfer, 891 sce->pipeh, sce, NULL, n, 892 USBD_NO_COPY, USBD_NO_TIMEOUT, 893 ugen_bulkwb_intr); 894 sce->state &= ~UGEN_RA_WB_STOP; 895 err = usbd_transfer(xfer); 896 if (err != USBD_IN_PROGRESS) 897 /* 898 * The transfer has not been 899 * queued. Setting STOP 900 * will make us try again 901 * at the next read. 902 */ 903 sce->state |= UGEN_RA_WB_STOP; 904 } 905 } 906 splx(s); 907 break; 908 } 909 #endif 910 xfer = usbd_alloc_xfer(sc->sc_udev); 911 if (xfer == 0) 912 return (EIO); 913 while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) { 914 error = uiomove(sc->sc_buffer, n, uio); 915 if (error) 916 break; 917 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n)); 918 err = usbd_bulk_transfer(xfer, sce->pipeh, 0, 919 sce->timeout, sc->sc_buffer, &n,"ugenwb"); 920 if (err) { 921 if (err == USBD_INTERRUPTED) 922 error = EINTR; 923 else if (err == USBD_TIMEOUT) 924 error = ETIMEDOUT; 925 else 926 error = EIO; 927 break; 928 } 929 } 930 usbd_free_xfer(xfer); 931 break; 932 case UE_INTERRUPT: 933 xfer = usbd_alloc_xfer(sc->sc_udev); 934 if (xfer == 0) 935 return (EIO); 936 while ((n = min(UGETW(sce->edesc->wMaxPacketSize), 937 uio->uio_resid)) != 0) { 938 error = uiomove(sc->sc_buffer, n, uio); 939 if (error) 940 break; 941 DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n)); 942 err = usbd_intr_transfer(xfer, sce->pipeh, 0, 943 sce->timeout, sc->sc_buffer, &n, "ugenwi"); 944 if (err) { 945 if (err == USBD_INTERRUPTED) 946 error = EINTR; 947 else if (err == USBD_TIMEOUT) 948 error = ETIMEDOUT; 949 else 950 error = EIO; 951 break; 952 } 953 } 954 usbd_free_xfer(xfer); 955 break; 956 default: 957 return (ENXIO); 958 } 959 return (error); 960 } 961 962 int 963 ugenwrite(dev_t dev, struct uio *uio, int flag) 964 { 965 int endpt = UGENENDPOINT(dev); 966 struct ugen_softc *sc; 967 int error; 968 969 USB_GET_SC(ugen, UGENUNIT(dev), sc); 970 971 sc->sc_refcnt++; 972 error = ugen_do_write(sc, endpt, uio, flag); 973 if (--sc->sc_refcnt < 0) 974 usb_detach_wakeup(USBDEV(sc->sc_dev)); 975 return (error); 976 } 977 978 #if defined(__NetBSD__) || defined(__OpenBSD__) 979 int 980 ugen_activate(device_ptr_t self, enum devact act) 981 { 982 struct ugen_softc *sc = (struct ugen_softc *)self; 983 984 switch (act) { 985 case DVACT_ACTIVATE: 986 return (EOPNOTSUPP); 987 988 case DVACT_DEACTIVATE: 989 sc->sc_dying = 1; 990 break; 991 } 992 return (0); 993 } 994 #endif 995 996 USB_DETACH(ugen) 997 { 998 USB_DETACH_START(ugen, sc); 999 struct ugen_endpoint *sce; 1000 int i, dir; 1001 int s; 1002 #if defined(__NetBSD__) || defined(__OpenBSD__) 1003 int maj, mn; 1004 1005 DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags)); 1006 #elif defined(__FreeBSD__) 1007 DPRINTF(("ugen_detach: sc=%p\n", sc)); 1008 #endif 1009 1010 sc->sc_dying = 1; 1011 /* Abort all pipes. Causes processes waiting for transfer to wake. */ 1012 for (i = 0; i < USB_MAX_ENDPOINTS; i++) { 1013 for (dir = OUT; dir <= IN; dir++) { 1014 sce = &sc->sc_endpoints[i][dir]; 1015 if (sce && sce->pipeh) 1016 usbd_abort_pipe(sce->pipeh); 1017 } 1018 } 1019 1020 s = splusb(); 1021 if (--sc->sc_refcnt >= 0) { 1022 /* Wake everyone */ 1023 for (i = 0; i < USB_MAX_ENDPOINTS; i++) 1024 wakeup(&sc->sc_endpoints[i][IN]); 1025 /* Wait for processes to go away. */ 1026 usb_detach_wait(USBDEV(sc->sc_dev)); 1027 } 1028 splx(s); 1029 1030 #if defined(__NetBSD__) || defined(__OpenBSD__) 1031 /* locate the major number */ 1032 #if defined(__NetBSD__) 1033 maj = cdevsw_lookup_major(&ugen_cdevsw); 1034 #elif defined(__OpenBSD__) 1035 for (maj = 0; maj < nchrdev; maj++) 1036 if (cdevsw[maj].d_open == ugenopen) 1037 break; 1038 #endif 1039 1040 /* Nuke the vnodes for any open instances (calls close). */ 1041 mn = device_unit(self) * USB_MAX_ENDPOINTS; 1042 vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR); 1043 #elif defined(__FreeBSD__) 1044 /* XXX not implemented yet */ 1045 #endif 1046 1047 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 1048 USBDEV(sc->sc_dev)); 1049 1050 return (0); 1051 } 1052 1053 Static void 1054 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 1055 { 1056 struct ugen_endpoint *sce = addr; 1057 /*struct ugen_softc *sc = sce->sc;*/ 1058 u_int32_t count; 1059 u_char *ibuf; 1060 1061 if (status == USBD_CANCELLED) 1062 return; 1063 1064 if (status != USBD_NORMAL_COMPLETION) { 1065 DPRINTF(("ugenintr: status=%d\n", status)); 1066 if (status == USBD_STALLED) 1067 usbd_clear_endpoint_stall_async(sce->pipeh); 1068 return; 1069 } 1070 1071 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 1072 ibuf = sce->ibuf; 1073 1074 DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n", 1075 xfer, status, count)); 1076 DPRINTFN(5, (" data = %02x %02x %02x\n", 1077 ibuf[0], ibuf[1], ibuf[2])); 1078 1079 (void)b_to_q(ibuf, count, &sce->q); 1080 1081 if (sce->state & UGEN_ASLP) { 1082 sce->state &= ~UGEN_ASLP; 1083 DPRINTFN(5, ("ugen_intr: waking %p\n", sce)); 1084 wakeup(sce); 1085 } 1086 selnotify(&sce->rsel, 0); 1087 } 1088 1089 Static void 1090 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr, 1091 usbd_status status) 1092 { 1093 struct isoreq *req = addr; 1094 struct ugen_endpoint *sce = req->sce; 1095 u_int32_t count, n; 1096 int i, isize; 1097 1098 /* Return if we are aborting. */ 1099 if (status == USBD_CANCELLED) 1100 return; 1101 1102 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 1103 DPRINTFN(5,("ugen_isoc_rintr: xfer %ld, count=%d\n", 1104 (long)(req - sce->isoreqs), count)); 1105 1106 /* throw away oldest input if the buffer is full */ 1107 if(sce->fill < sce->cur && sce->cur <= sce->fill + count) { 1108 sce->cur += count; 1109 if(sce->cur >= sce->limit) 1110 sce->cur = sce->ibuf + (sce->limit - sce->cur); 1111 DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n", 1112 count)); 1113 } 1114 1115 isize = UGETW(sce->edesc->wMaxPacketSize); 1116 for (i = 0; i < UGEN_NISORFRMS; i++) { 1117 u_int32_t actlen = req->sizes[i]; 1118 char const *tbuf = (char const *)req->dmabuf + isize * i; 1119 1120 /* copy data to buffer */ 1121 while (actlen > 0) { 1122 n = min(actlen, sce->limit - sce->fill); 1123 memcpy(sce->fill, tbuf, n); 1124 1125 tbuf += n; 1126 actlen -= n; 1127 sce->fill += n; 1128 if(sce->fill == sce->limit) 1129 sce->fill = sce->ibuf; 1130 } 1131 1132 /* setup size for next transfer */ 1133 req->sizes[i] = isize; 1134 } 1135 1136 usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS, 1137 USBD_NO_COPY, ugen_isoc_rintr); 1138 (void)usbd_transfer(xfer); 1139 1140 if (sce->state & UGEN_ASLP) { 1141 sce->state &= ~UGEN_ASLP; 1142 DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce)); 1143 wakeup(sce); 1144 } 1145 selnotify(&sce->rsel, 0); 1146 } 1147 1148 #ifdef UGEN_BULK_RA_WB 1149 Static void 1150 ugen_bulkra_intr(usbd_xfer_handle xfer, usbd_private_handle addr, 1151 usbd_status status) 1152 { 1153 struct ugen_endpoint *sce = addr; 1154 u_int32_t count, n; 1155 char const *tbuf; 1156 usbd_status err; 1157 1158 /* Return if we are aborting. */ 1159 if (status == USBD_CANCELLED) 1160 return; 1161 1162 if (status != USBD_NORMAL_COMPLETION) { 1163 DPRINTF(("ugen_bulkra_intr: status=%d\n", status)); 1164 sce->state |= UGEN_RA_WB_STOP; 1165 if (status == USBD_STALLED) 1166 usbd_clear_endpoint_stall_async(sce->pipeh); 1167 return; 1168 } 1169 1170 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 1171 1172 /* Keep track of how much is in the buffer. */ 1173 sce->ra_wb_used += count; 1174 1175 /* Copy data to buffer. */ 1176 tbuf = (char const *)usbd_get_buffer(sce->ra_wb_xfer); 1177 n = min(count, sce->limit - sce->fill); 1178 memcpy(sce->fill, tbuf, n); 1179 tbuf += n; 1180 count -= n; 1181 sce->fill += n; 1182 if (sce->fill == sce->limit) 1183 sce->fill = sce->ibuf; 1184 if (count > 0) { 1185 memcpy(sce->fill, tbuf, count); 1186 sce->fill += count; 1187 } 1188 1189 /* Set up the next request if necessary. */ 1190 n = (sce->limit - sce->ibuf) - sce->ra_wb_used; 1191 if (n > 0) { 1192 usbd_setup_xfer(xfer, sce->pipeh, sce, NULL, 1193 min(n, sce->ra_wb_xferlen), USBD_NO_COPY, 1194 USBD_NO_TIMEOUT, ugen_bulkra_intr); 1195 err = usbd_transfer(xfer); 1196 if (err != USBD_IN_PROGRESS) { 1197 printf("usbd_bulkra_intr: error=%d\n", err); 1198 /* 1199 * The transfer has not been queued. Setting STOP 1200 * will make us try again at the next read. 1201 */ 1202 sce->state |= UGEN_RA_WB_STOP; 1203 } 1204 } 1205 else 1206 sce->state |= UGEN_RA_WB_STOP; 1207 1208 if (sce->state & UGEN_ASLP) { 1209 sce->state &= ~UGEN_ASLP; 1210 DPRINTFN(5, ("ugen_bulkra_intr: waking %p\n", sce)); 1211 wakeup(sce); 1212 } 1213 selnotify(&sce->rsel, 0); 1214 } 1215 1216 Static void 1217 ugen_bulkwb_intr(usbd_xfer_handle xfer, usbd_private_handle addr, 1218 usbd_status status) 1219 { 1220 struct ugen_endpoint *sce = addr; 1221 u_int32_t count, n; 1222 char *tbuf; 1223 usbd_status err; 1224 1225 /* Return if we are aborting. */ 1226 if (status == USBD_CANCELLED) 1227 return; 1228 1229 if (status != USBD_NORMAL_COMPLETION) { 1230 DPRINTF(("ugen_bulkwb_intr: status=%d\n", status)); 1231 sce->state |= UGEN_RA_WB_STOP; 1232 if (status == USBD_STALLED) 1233 usbd_clear_endpoint_stall_async(sce->pipeh); 1234 return; 1235 } 1236 1237 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); 1238 1239 /* Keep track of how much is in the buffer. */ 1240 sce->ra_wb_used -= count; 1241 1242 /* Update buffer pointers. */ 1243 sce->cur += count; 1244 if (sce->cur >= sce->limit) 1245 sce->cur = sce->ibuf + (sce->cur - sce->limit); 1246 1247 /* Set up next request if necessary. */ 1248 if (sce->ra_wb_used > 0) { 1249 /* copy data from buffer */ 1250 tbuf = (char *)usbd_get_buffer(sce->ra_wb_xfer); 1251 count = min(sce->ra_wb_used, sce->ra_wb_xferlen); 1252 n = min(count, sce->limit - sce->cur); 1253 memcpy(tbuf, sce->cur, n); 1254 tbuf += n; 1255 if (count - n > 0) 1256 memcpy(tbuf, sce->ibuf, count - n); 1257 1258 usbd_setup_xfer(xfer, sce->pipeh, sce, NULL, 1259 count, USBD_NO_COPY, USBD_NO_TIMEOUT, ugen_bulkwb_intr); 1260 err = usbd_transfer(xfer); 1261 if (err != USBD_IN_PROGRESS) { 1262 printf("usbd_bulkwb_intr: error=%d\n", err); 1263 /* 1264 * The transfer has not been queued. Setting STOP 1265 * will make us try again at the next write. 1266 */ 1267 sce->state |= UGEN_RA_WB_STOP; 1268 } 1269 } 1270 else 1271 sce->state |= UGEN_RA_WB_STOP; 1272 1273 if (sce->state & UGEN_ASLP) { 1274 sce->state &= ~UGEN_ASLP; 1275 DPRINTFN(5, ("ugen_bulkwb_intr: waking %p\n", sce)); 1276 wakeup(sce); 1277 } 1278 selnotify(&sce->rsel, 0); 1279 } 1280 #endif 1281 1282 Static usbd_status 1283 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno) 1284 { 1285 usbd_interface_handle iface; 1286 usb_endpoint_descriptor_t *ed; 1287 usbd_status err; 1288 struct ugen_endpoint *sce; 1289 u_int8_t niface, nendpt, endptno, endpt; 1290 int dir; 1291 1292 DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno)); 1293 1294 err = usbd_interface_count(sc->sc_udev, &niface); 1295 if (err) 1296 return (err); 1297 if (ifaceidx < 0 || ifaceidx >= niface) 1298 return (USBD_INVAL); 1299 1300 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface); 1301 if (err) 1302 return (err); 1303 err = usbd_endpoint_count(iface, &nendpt); 1304 if (err) 1305 return (err); 1306 /* XXX should only do this after setting new altno has succeeded */ 1307 for (endptno = 0; endptno < nendpt; endptno++) { 1308 ed = usbd_interface2endpoint_descriptor(iface,endptno); 1309 endpt = ed->bEndpointAddress; 1310 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 1311 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 1312 sce->sc = 0; 1313 sce->edesc = 0; 1314 sce->iface = 0; 1315 } 1316 1317 /* change setting */ 1318 err = usbd_set_interface(iface, altno); 1319 if (err) 1320 return (err); 1321 1322 err = usbd_endpoint_count(iface, &nendpt); 1323 if (err) 1324 return (err); 1325 for (endptno = 0; endptno < nendpt; endptno++) { 1326 ed = usbd_interface2endpoint_descriptor(iface,endptno); 1327 KASSERT(ed != NULL); 1328 endpt = ed->bEndpointAddress; 1329 dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT; 1330 sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir]; 1331 sce->sc = sc; 1332 sce->edesc = ed; 1333 sce->iface = iface; 1334 } 1335 return (0); 1336 } 1337 1338 /* Retrieve a complete descriptor for a certain device and index. */ 1339 Static usb_config_descriptor_t * 1340 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp) 1341 { 1342 usb_config_descriptor_t *cdesc, *tdesc, cdescr; 1343 int len; 1344 usbd_status err; 1345 1346 if (index == USB_CURRENT_CONFIG_INDEX) { 1347 tdesc = usbd_get_config_descriptor(sc->sc_udev); 1348 len = UGETW(tdesc->wTotalLength); 1349 if (lenp) 1350 *lenp = len; 1351 cdesc = malloc(len, M_TEMP, M_WAITOK); 1352 memcpy(cdesc, tdesc, len); 1353 DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len)); 1354 } else { 1355 err = usbd_get_config_desc(sc->sc_udev, index, &cdescr); 1356 if (err) 1357 return (0); 1358 len = UGETW(cdescr.wTotalLength); 1359 DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len)); 1360 if (lenp) 1361 *lenp = len; 1362 cdesc = malloc(len, M_TEMP, M_WAITOK); 1363 err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len); 1364 if (err) { 1365 free(cdesc, M_TEMP); 1366 return (0); 1367 } 1368 } 1369 return (cdesc); 1370 } 1371 1372 Static int 1373 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx) 1374 { 1375 usbd_interface_handle iface; 1376 usbd_status err; 1377 1378 err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface); 1379 if (err) 1380 return (-1); 1381 return (usbd_get_interface_altindex(iface)); 1382 } 1383 1384 Static int 1385 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd, 1386 void *addr, int flag, struct lwp *l) 1387 { 1388 struct ugen_endpoint *sce; 1389 usbd_status err; 1390 usbd_interface_handle iface; 1391 struct usb_config_desc *cd; 1392 usb_config_descriptor_t *cdesc; 1393 struct usb_interface_desc *id; 1394 usb_interface_descriptor_t *idesc; 1395 struct usb_endpoint_desc *ed; 1396 usb_endpoint_descriptor_t *edesc; 1397 struct usb_alt_interface *ai; 1398 struct usb_string_desc *si; 1399 u_int8_t conf, alt; 1400 1401 DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd)); 1402 if (sc->sc_dying) 1403 return (EIO); 1404 1405 switch (cmd) { 1406 case FIONBIO: 1407 /* All handled in the upper FS layer. */ 1408 return (0); 1409 case USB_SET_SHORT_XFER: 1410 if (endpt == USB_CONTROL_ENDPOINT) 1411 return (EINVAL); 1412 /* This flag only affects read */ 1413 sce = &sc->sc_endpoints[endpt][IN]; 1414 if (sce == NULL || sce->pipeh == NULL) 1415 return (EINVAL); 1416 if (*(int *)addr) 1417 sce->state |= UGEN_SHORT_OK; 1418 else 1419 sce->state &= ~UGEN_SHORT_OK; 1420 return (0); 1421 case USB_SET_TIMEOUT: 1422 sce = &sc->sc_endpoints[endpt][IN]; 1423 if (sce == NULL 1424 /* XXX this shouldn't happen, but the distinction between 1425 input and output pipes isn't clear enough. 1426 || sce->pipeh == NULL */ 1427 ) 1428 return (EINVAL); 1429 sce->timeout = *(int *)addr; 1430 return (0); 1431 case USB_SET_BULK_RA: 1432 #ifdef UGEN_BULK_RA_WB 1433 if (endpt == USB_CONTROL_ENDPOINT) 1434 return (EINVAL); 1435 sce = &sc->sc_endpoints[endpt][IN]; 1436 if (sce == NULL || sce->pipeh == NULL) 1437 return (EINVAL); 1438 edesc = sce->edesc; 1439 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK) 1440 return (EINVAL); 1441 1442 if (*(int *)addr) { 1443 /* Only turn RA on if it's currently off. */ 1444 if (sce->state & UGEN_BULK_RA) 1445 return (0); 1446 1447 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0) 1448 /* shouldn't happen */ 1449 return (EINVAL); 1450 sce->ra_wb_xfer = usbd_alloc_xfer(sc->sc_udev); 1451 if (sce->ra_wb_xfer == NULL) 1452 return (ENOMEM); 1453 sce->ra_wb_xferlen = sce->ra_wb_reqsize; 1454 /* 1455 * Set up a dmabuf because we reuse the xfer with 1456 * the same (max) request length like isoc. 1457 */ 1458 if (usbd_alloc_buffer(sce->ra_wb_xfer, 1459 sce->ra_wb_xferlen) == 0) { 1460 usbd_free_xfer(sce->ra_wb_xfer); 1461 return (ENOMEM); 1462 } 1463 sce->ibuf = malloc(sce->ra_wb_bufsize, 1464 M_USBDEV, M_WAITOK); 1465 sce->fill = sce->cur = sce->ibuf; 1466 sce->limit = sce->ibuf + sce->ra_wb_bufsize; 1467 sce->ra_wb_used = 0; 1468 sce->state |= UGEN_BULK_RA; 1469 sce->state &= ~UGEN_RA_WB_STOP; 1470 /* Now start reading. */ 1471 usbd_setup_xfer(sce->ra_wb_xfer, sce->pipeh, sce, 1472 NULL, 1473 min(sce->ra_wb_xferlen, sce->ra_wb_bufsize), 1474 USBD_NO_COPY, USBD_NO_TIMEOUT, 1475 ugen_bulkra_intr); 1476 err = usbd_transfer(sce->ra_wb_xfer); 1477 if (err != USBD_IN_PROGRESS) { 1478 sce->state &= ~UGEN_BULK_RA; 1479 free(sce->ibuf, M_USBDEV); 1480 sce->ibuf = NULL; 1481 usbd_free_xfer(sce->ra_wb_xfer); 1482 return (EIO); 1483 } 1484 } else { 1485 /* Only turn RA off if it's currently on. */ 1486 if (!(sce->state & UGEN_BULK_RA)) 1487 return (0); 1488 1489 sce->state &= ~UGEN_BULK_RA; 1490 usbd_abort_pipe(sce->pipeh); 1491 usbd_free_xfer(sce->ra_wb_xfer); 1492 /* 1493 * XXX Discard whatever's in the buffer, but we 1494 * should keep it around and drain the buffer 1495 * instead. 1496 */ 1497 free(sce->ibuf, M_USBDEV); 1498 sce->ibuf = NULL; 1499 } 1500 return (0); 1501 #else 1502 return (EOPNOTSUPP); 1503 #endif 1504 case USB_SET_BULK_WB: 1505 #ifdef UGEN_BULK_RA_WB 1506 if (endpt == USB_CONTROL_ENDPOINT) 1507 return (EINVAL); 1508 sce = &sc->sc_endpoints[endpt][OUT]; 1509 if (sce == NULL || sce->pipeh == NULL) 1510 return (EINVAL); 1511 edesc = sce->edesc; 1512 if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK) 1513 return (EINVAL); 1514 1515 if (*(int *)addr) { 1516 /* Only turn WB on if it's currently off. */ 1517 if (sce->state & UGEN_BULK_WB) 1518 return (0); 1519 1520 if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0) 1521 /* shouldn't happen */ 1522 return (EINVAL); 1523 sce->ra_wb_xfer = usbd_alloc_xfer(sc->sc_udev); 1524 if (sce->ra_wb_xfer == NULL) 1525 return (ENOMEM); 1526 sce->ra_wb_xferlen = sce->ra_wb_reqsize; 1527 /* 1528 * Set up a dmabuf because we reuse the xfer with 1529 * the same (max) request length like isoc. 1530 */ 1531 if (usbd_alloc_buffer(sce->ra_wb_xfer, 1532 sce->ra_wb_xferlen) == 0) { 1533 usbd_free_xfer(sce->ra_wb_xfer); 1534 return (ENOMEM); 1535 } 1536 sce->ibuf = malloc(sce->ra_wb_bufsize, 1537 M_USBDEV, M_WAITOK); 1538 sce->fill = sce->cur = sce->ibuf; 1539 sce->limit = sce->ibuf + sce->ra_wb_bufsize; 1540 sce->ra_wb_used = 0; 1541 sce->state |= UGEN_BULK_WB | UGEN_RA_WB_STOP; 1542 } else { 1543 /* Only turn WB off if it's currently on. */ 1544 if (!(sce->state & UGEN_BULK_WB)) 1545 return (0); 1546 1547 sce->state &= ~UGEN_BULK_WB; 1548 /* 1549 * XXX Discard whatever's in the buffer, but we 1550 * should keep it around and keep writing to 1551 * drain the buffer instead. 1552 */ 1553 usbd_abort_pipe(sce->pipeh); 1554 usbd_free_xfer(sce->ra_wb_xfer); 1555 free(sce->ibuf, M_USBDEV); 1556 sce->ibuf = NULL; 1557 } 1558 return (0); 1559 #else 1560 return (EOPNOTSUPP); 1561 #endif 1562 case USB_SET_BULK_RA_OPT: 1563 case USB_SET_BULK_WB_OPT: 1564 #ifdef UGEN_BULK_RA_WB 1565 { 1566 struct usb_bulk_ra_wb_opt *opt; 1567 1568 if (endpt == USB_CONTROL_ENDPOINT) 1569 return (EINVAL); 1570 opt = (struct usb_bulk_ra_wb_opt *)addr; 1571 if (cmd == USB_SET_BULK_RA_OPT) 1572 sce = &sc->sc_endpoints[endpt][IN]; 1573 else 1574 sce = &sc->sc_endpoints[endpt][OUT]; 1575 if (sce == NULL || sce->pipeh == NULL) 1576 return (EINVAL); 1577 if (opt->ra_wb_buffer_size < 1 || 1578 opt->ra_wb_buffer_size > UGEN_BULK_RA_WB_BUFMAX || 1579 opt->ra_wb_request_size < 1 || 1580 opt->ra_wb_request_size > opt->ra_wb_buffer_size) 1581 return (EINVAL); 1582 /* 1583 * XXX These changes do not take effect until the 1584 * next time RA/WB mode is enabled but they ought to 1585 * take effect immediately. 1586 */ 1587 sce->ra_wb_bufsize = opt->ra_wb_buffer_size; 1588 sce->ra_wb_reqsize = opt->ra_wb_request_size; 1589 return (0); 1590 } 1591 #else 1592 return (EOPNOTSUPP); 1593 #endif 1594 default: 1595 break; 1596 } 1597 1598 if (endpt != USB_CONTROL_ENDPOINT) 1599 return (EINVAL); 1600 1601 switch (cmd) { 1602 #ifdef UGEN_DEBUG 1603 case USB_SETDEBUG: 1604 ugendebug = *(int *)addr; 1605 break; 1606 #endif 1607 case USB_GET_CONFIG: 1608 err = usbd_get_config(sc->sc_udev, &conf); 1609 if (err) 1610 return (EIO); 1611 *(int *)addr = conf; 1612 break; 1613 case USB_SET_CONFIG: 1614 if (!(flag & FWRITE)) 1615 return (EPERM); 1616 err = ugen_set_config(sc, *(int *)addr); 1617 switch (err) { 1618 case USBD_NORMAL_COMPLETION: 1619 break; 1620 case USBD_IN_USE: 1621 return (EBUSY); 1622 default: 1623 return (EIO); 1624 } 1625 break; 1626 case USB_GET_ALTINTERFACE: 1627 ai = (struct usb_alt_interface *)addr; 1628 err = usbd_device2interface_handle(sc->sc_udev, 1629 ai->uai_interface_index, &iface); 1630 if (err) 1631 return (EINVAL); 1632 idesc = usbd_get_interface_descriptor(iface); 1633 if (idesc == NULL) 1634 return (EIO); 1635 ai->uai_alt_no = idesc->bAlternateSetting; 1636 break; 1637 case USB_SET_ALTINTERFACE: 1638 if (!(flag & FWRITE)) 1639 return (EPERM); 1640 ai = (struct usb_alt_interface *)addr; 1641 err = usbd_device2interface_handle(sc->sc_udev, 1642 ai->uai_interface_index, &iface); 1643 if (err) 1644 return (EINVAL); 1645 err = ugen_set_interface(sc, ai->uai_interface_index, 1646 ai->uai_alt_no); 1647 if (err) 1648 return (EINVAL); 1649 break; 1650 case USB_GET_NO_ALT: 1651 ai = (struct usb_alt_interface *)addr; 1652 cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0); 1653 if (cdesc == NULL) 1654 return (EINVAL); 1655 idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0); 1656 if (idesc == NULL) { 1657 free(cdesc, M_TEMP); 1658 return (EINVAL); 1659 } 1660 ai->uai_alt_no = usbd_get_no_alts(cdesc, 1661 idesc->bInterfaceNumber); 1662 free(cdesc, M_TEMP); 1663 break; 1664 case USB_GET_DEVICE_DESC: 1665 *(usb_device_descriptor_t *)addr = 1666 *usbd_get_device_descriptor(sc->sc_udev); 1667 break; 1668 case USB_GET_CONFIG_DESC: 1669 cd = (struct usb_config_desc *)addr; 1670 cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0); 1671 if (cdesc == NULL) 1672 return (EINVAL); 1673 cd->ucd_desc = *cdesc; 1674 free(cdesc, M_TEMP); 1675 break; 1676 case USB_GET_INTERFACE_DESC: 1677 id = (struct usb_interface_desc *)addr; 1678 cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0); 1679 if (cdesc == NULL) 1680 return (EINVAL); 1681 if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX && 1682 id->uid_alt_index == USB_CURRENT_ALT_INDEX) 1683 alt = ugen_get_alt_index(sc, id->uid_interface_index); 1684 else 1685 alt = id->uid_alt_index; 1686 idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt); 1687 if (idesc == NULL) { 1688 free(cdesc, M_TEMP); 1689 return (EINVAL); 1690 } 1691 id->uid_desc = *idesc; 1692 free(cdesc, M_TEMP); 1693 break; 1694 case USB_GET_ENDPOINT_DESC: 1695 ed = (struct usb_endpoint_desc *)addr; 1696 cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0); 1697 if (cdesc == NULL) 1698 return (EINVAL); 1699 if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX && 1700 ed->ued_alt_index == USB_CURRENT_ALT_INDEX) 1701 alt = ugen_get_alt_index(sc, ed->ued_interface_index); 1702 else 1703 alt = ed->ued_alt_index; 1704 edesc = usbd_find_edesc(cdesc, ed->ued_interface_index, 1705 alt, ed->ued_endpoint_index); 1706 if (edesc == NULL) { 1707 free(cdesc, M_TEMP); 1708 return (EINVAL); 1709 } 1710 ed->ued_desc = *edesc; 1711 free(cdesc, M_TEMP); 1712 break; 1713 case USB_GET_FULL_DESC: 1714 { 1715 int len; 1716 struct iovec iov; 1717 struct uio uio; 1718 struct usb_full_desc *fd = (struct usb_full_desc *)addr; 1719 int error; 1720 1721 cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len); 1722 if (len > fd->ufd_size) 1723 len = fd->ufd_size; 1724 iov.iov_base = (void *)fd->ufd_data; 1725 iov.iov_len = len; 1726 uio.uio_iov = &iov; 1727 uio.uio_iovcnt = 1; 1728 uio.uio_resid = len; 1729 uio.uio_offset = 0; 1730 uio.uio_rw = UIO_READ; 1731 uio.uio_vmspace = l->l_proc->p_vmspace; 1732 error = uiomove((void *)cdesc, len, &uio); 1733 free(cdesc, M_TEMP); 1734 return (error); 1735 } 1736 case USB_GET_STRING_DESC: { 1737 int len; 1738 si = (struct usb_string_desc *)addr; 1739 err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index, 1740 si->usd_language_id, &si->usd_desc, &len); 1741 if (err) 1742 return (EINVAL); 1743 break; 1744 } 1745 case USB_DO_REQUEST: 1746 { 1747 struct usb_ctl_request *ur = (void *)addr; 1748 int len = UGETW(ur->ucr_request.wLength); 1749 struct iovec iov; 1750 struct uio uio; 1751 void *ptr = 0; 1752 usbd_status xerr; 1753 int error = 0; 1754 1755 if (!(flag & FWRITE)) 1756 return (EPERM); 1757 /* Avoid requests that would damage the bus integrity. */ 1758 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 1759 ur->ucr_request.bRequest == UR_SET_ADDRESS) || 1760 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 1761 ur->ucr_request.bRequest == UR_SET_CONFIG) || 1762 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE && 1763 ur->ucr_request.bRequest == UR_SET_INTERFACE)) 1764 return (EINVAL); 1765 1766 if (len < 0 || len > 32767) 1767 return (EINVAL); 1768 if (len != 0) { 1769 iov.iov_base = (void *)ur->ucr_data; 1770 iov.iov_len = len; 1771 uio.uio_iov = &iov; 1772 uio.uio_iovcnt = 1; 1773 uio.uio_resid = len; 1774 uio.uio_offset = 0; 1775 uio.uio_rw = 1776 ur->ucr_request.bmRequestType & UT_READ ? 1777 UIO_READ : UIO_WRITE; 1778 uio.uio_vmspace = l->l_proc->p_vmspace; 1779 ptr = malloc(len, M_TEMP, M_WAITOK); 1780 if (uio.uio_rw == UIO_WRITE) { 1781 error = uiomove(ptr, len, &uio); 1782 if (error) 1783 goto ret; 1784 } 1785 } 1786 sce = &sc->sc_endpoints[endpt][IN]; 1787 xerr = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request, 1788 ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout); 1789 if (xerr) { 1790 error = EIO; 1791 goto ret; 1792 } 1793 if (len != 0) { 1794 if (uio.uio_rw == UIO_READ) { 1795 error = uiomove(ptr, len, &uio); 1796 if (error) 1797 goto ret; 1798 } 1799 } 1800 ret: 1801 if (ptr) 1802 free(ptr, M_TEMP); 1803 return (error); 1804 } 1805 case USB_GET_DEVICEINFO: 1806 usbd_fill_deviceinfo(sc->sc_udev, 1807 (struct usb_device_info *)addr, 0); 1808 break; 1809 #ifdef COMPAT_30 1810 case USB_GET_DEVICEINFO_OLD: 1811 usbd_fill_deviceinfo_old(sc->sc_udev, 1812 (struct usb_device_info_old *)addr, 0); 1813 1814 break; 1815 #endif 1816 default: 1817 return (EINVAL); 1818 } 1819 return (0); 1820 } 1821 1822 int 1823 ugenioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1824 { 1825 int endpt = UGENENDPOINT(dev); 1826 struct ugen_softc *sc; 1827 int error; 1828 1829 USB_GET_SC(ugen, UGENUNIT(dev), sc); 1830 1831 sc->sc_refcnt++; 1832 error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, l); 1833 if (--sc->sc_refcnt < 0) 1834 usb_detach_wakeup(USBDEV(sc->sc_dev)); 1835 return (error); 1836 } 1837 1838 int 1839 ugenpoll(dev_t dev, int events, struct lwp *l) 1840 { 1841 struct ugen_softc *sc; 1842 struct ugen_endpoint *sce_in, *sce_out; 1843 int revents = 0; 1844 int s; 1845 1846 USB_GET_SC(ugen, UGENUNIT(dev), sc); 1847 1848 if (sc->sc_dying) 1849 return (POLLHUP); 1850 1851 sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN]; 1852 sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT]; 1853 if (sce_in == NULL && sce_out == NULL) 1854 return (POLLERR); 1855 #ifdef DIAGNOSTIC 1856 if (!sce_in->edesc && !sce_out->edesc) { 1857 printf("ugenpoll: no edesc\n"); 1858 return (POLLERR); 1859 } 1860 /* It's possible to have only one pipe open. */ 1861 if (!sce_in->pipeh && !sce_out->pipeh) { 1862 printf("ugenpoll: no pipe\n"); 1863 return (POLLERR); 1864 } 1865 #endif 1866 s = splusb(); 1867 if (sce_in && sce_in->pipeh && (events & (POLLIN | POLLRDNORM))) 1868 switch (sce_in->edesc->bmAttributes & UE_XFERTYPE) { 1869 case UE_INTERRUPT: 1870 if (sce_in->q.c_cc > 0) 1871 revents |= events & (POLLIN | POLLRDNORM); 1872 else 1873 selrecord(l, &sce_in->rsel); 1874 break; 1875 case UE_ISOCHRONOUS: 1876 if (sce_in->cur != sce_in->fill) 1877 revents |= events & (POLLIN | POLLRDNORM); 1878 else 1879 selrecord(l, &sce_in->rsel); 1880 break; 1881 case UE_BULK: 1882 #ifdef UGEN_BULK_RA_WB 1883 if (sce_in->state & UGEN_BULK_RA) { 1884 if (sce_in->ra_wb_used > 0) 1885 revents |= events & 1886 (POLLIN | POLLRDNORM); 1887 else 1888 selrecord(l, &sce_in->rsel); 1889 break; 1890 } 1891 #endif 1892 /* 1893 * We have no easy way of determining if a read will 1894 * yield any data or a write will happen. 1895 * Pretend they will. 1896 */ 1897 revents |= events & (POLLIN | POLLRDNORM); 1898 break; 1899 default: 1900 break; 1901 } 1902 if (sce_out && sce_out->pipeh && (events & (POLLOUT | POLLWRNORM))) 1903 switch (sce_out->edesc->bmAttributes & UE_XFERTYPE) { 1904 case UE_INTERRUPT: 1905 case UE_ISOCHRONOUS: 1906 /* XXX unimplemented */ 1907 break; 1908 case UE_BULK: 1909 #ifdef UGEN_BULK_RA_WB 1910 if (sce_out->state & UGEN_BULK_WB) { 1911 if (sce_out->ra_wb_used < 1912 sce_out->limit - sce_out->ibuf) 1913 revents |= events & 1914 (POLLOUT | POLLWRNORM); 1915 else 1916 selrecord(l, &sce_out->rsel); 1917 break; 1918 } 1919 #endif 1920 /* 1921 * We have no easy way of determining if a read will 1922 * yield any data or a write will happen. 1923 * Pretend they will. 1924 */ 1925 revents |= events & (POLLOUT | POLLWRNORM); 1926 break; 1927 default: 1928 break; 1929 } 1930 1931 1932 splx(s); 1933 return (revents); 1934 } 1935 1936 static void 1937 filt_ugenrdetach(struct knote *kn) 1938 { 1939 struct ugen_endpoint *sce = kn->kn_hook; 1940 int s; 1941 1942 s = splusb(); 1943 SLIST_REMOVE(&sce->rsel.sel_klist, kn, knote, kn_selnext); 1944 splx(s); 1945 } 1946 1947 static int 1948 filt_ugenread_intr(struct knote *kn, long hint) 1949 { 1950 struct ugen_endpoint *sce = kn->kn_hook; 1951 1952 kn->kn_data = sce->q.c_cc; 1953 return (kn->kn_data > 0); 1954 } 1955 1956 static int 1957 filt_ugenread_isoc(struct knote *kn, long hint) 1958 { 1959 struct ugen_endpoint *sce = kn->kn_hook; 1960 1961 if (sce->cur == sce->fill) 1962 return (0); 1963 1964 if (sce->cur < sce->fill) 1965 kn->kn_data = sce->fill - sce->cur; 1966 else 1967 kn->kn_data = (sce->limit - sce->cur) + 1968 (sce->fill - sce->ibuf); 1969 1970 return (1); 1971 } 1972 1973 #ifdef UGEN_BULK_RA_WB 1974 static int 1975 filt_ugenread_bulk(struct knote *kn, long hint) 1976 { 1977 struct ugen_endpoint *sce = kn->kn_hook; 1978 1979 if (!(sce->state & UGEN_BULK_RA)) 1980 /* 1981 * We have no easy way of determining if a read will 1982 * yield any data or a write will happen. 1983 * So, emulate "seltrue". 1984 */ 1985 return (filt_seltrue(kn, hint)); 1986 1987 if (sce->ra_wb_used == 0) 1988 return (0); 1989 1990 kn->kn_data = sce->ra_wb_used; 1991 1992 return (1); 1993 } 1994 1995 static int 1996 filt_ugenwrite_bulk(struct knote *kn, long hint) 1997 { 1998 struct ugen_endpoint *sce = kn->kn_hook; 1999 2000 if (!(sce->state & UGEN_BULK_WB)) 2001 /* 2002 * We have no easy way of determining if a read will 2003 * yield any data or a write will happen. 2004 * So, emulate "seltrue". 2005 */ 2006 return (filt_seltrue(kn, hint)); 2007 2008 if (sce->ra_wb_used == sce->limit - sce->ibuf) 2009 return (0); 2010 2011 kn->kn_data = (sce->limit - sce->ibuf) - sce->ra_wb_used; 2012 2013 return (1); 2014 } 2015 #endif 2016 2017 static const struct filterops ugenread_intr_filtops = 2018 { 1, NULL, filt_ugenrdetach, filt_ugenread_intr }; 2019 2020 static const struct filterops ugenread_isoc_filtops = 2021 { 1, NULL, filt_ugenrdetach, filt_ugenread_isoc }; 2022 2023 #ifdef UGEN_BULK_RA_WB 2024 static const struct filterops ugenread_bulk_filtops = 2025 { 1, NULL, filt_ugenrdetach, filt_ugenread_bulk }; 2026 2027 static const struct filterops ugenwrite_bulk_filtops = 2028 { 1, NULL, filt_ugenrdetach, filt_ugenwrite_bulk }; 2029 #else 2030 static const struct filterops ugen_seltrue_filtops = 2031 { 1, NULL, filt_ugenrdetach, filt_seltrue }; 2032 #endif 2033 2034 int 2035 ugenkqfilter(dev_t dev, struct knote *kn) 2036 { 2037 struct ugen_softc *sc; 2038 struct ugen_endpoint *sce; 2039 struct klist *klist; 2040 int s; 2041 2042 USB_GET_SC(ugen, UGENUNIT(dev), sc); 2043 2044 if (sc->sc_dying) 2045 return (1); 2046 2047 switch (kn->kn_filter) { 2048 case EVFILT_READ: 2049 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN]; 2050 if (sce == NULL) 2051 return (1); 2052 2053 klist = &sce->rsel.sel_klist; 2054 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 2055 case UE_INTERRUPT: 2056 kn->kn_fop = &ugenread_intr_filtops; 2057 break; 2058 case UE_ISOCHRONOUS: 2059 kn->kn_fop = &ugenread_isoc_filtops; 2060 break; 2061 case UE_BULK: 2062 #ifdef UGEN_BULK_RA_WB 2063 kn->kn_fop = &ugenread_bulk_filtops; 2064 break; 2065 #else 2066 /* 2067 * We have no easy way of determining if a read will 2068 * yield any data or a write will happen. 2069 * So, emulate "seltrue". 2070 */ 2071 kn->kn_fop = &ugen_seltrue_filtops; 2072 #endif 2073 break; 2074 default: 2075 return (1); 2076 } 2077 break; 2078 2079 case EVFILT_WRITE: 2080 sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT]; 2081 if (sce == NULL) 2082 return (1); 2083 2084 klist = &sce->rsel.sel_klist; 2085 switch (sce->edesc->bmAttributes & UE_XFERTYPE) { 2086 case UE_INTERRUPT: 2087 case UE_ISOCHRONOUS: 2088 /* XXX poll doesn't support this */ 2089 return (1); 2090 2091 case UE_BULK: 2092 #ifdef UGEN_BULK_RA_WB 2093 kn->kn_fop = &ugenwrite_bulk_filtops; 2094 #else 2095 /* 2096 * We have no easy way of determining if a read will 2097 * yield any data or a write will happen. 2098 * So, emulate "seltrue". 2099 */ 2100 kn->kn_fop = &ugen_seltrue_filtops; 2101 #endif 2102 break; 2103 default: 2104 return (1); 2105 } 2106 break; 2107 2108 default: 2109 return (1); 2110 } 2111 2112 kn->kn_hook = sce; 2113 2114 s = splusb(); 2115 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 2116 splx(s); 2117 2118 return (0); 2119 } 2120 2121 #if defined(__FreeBSD__) 2122 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0); 2123 #endif 2124