1 /* $NetBSD: udsir.c,v 1.2 2016/04/23 10:15:32 skrll Exp $ */ 2 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by David Sainty <David.Sainty@dtsp.co.nz> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: udsir.c,v 1.2 2016/04/23 10:15:32 skrll Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/device.h> 37 #include <sys/errno.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/kmem.h> 41 #include <sys/conf.h> 42 #include <sys/file.h> 43 #include <sys/poll.h> 44 #include <sys/select.h> 45 #include <sys/proc.h> 46 #include <sys/kthread.h> 47 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usbdevs.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdi_util.h> 52 53 #include <dev/ir/ir.h> 54 #include <dev/ir/irdaio.h> 55 #include <dev/ir/irframevar.h> 56 #include <dev/ir/sir.h> 57 58 #ifdef UDSIR_DEBUG 59 #define DPRINTFN(n,x) if (udsirdebug > (n)) printf x 60 int udsirdebug = 0; 61 #else 62 #define DPRINTFN(n,x) 63 #endif 64 65 /* Max size with framing. */ 66 #define MAX_UDSIR_OUTPUT_FRAME (2 * IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4) 67 68 struct udsir_softc { 69 device_t sc_dev; 70 struct usbd_device *sc_udev; 71 struct usbd_interface *sc_iface; 72 73 uint8_t *sc_ur_buf; /* Unencapsulated frame */ 74 u_int sc_ur_framelen; 75 76 uint8_t *sc_rd_buf; /* Raw incoming data stream */ 77 int sc_rd_maxpsz; 78 size_t sc_rd_index; 79 int sc_rd_addr; 80 struct usbd_pipe *sc_rd_pipe; 81 struct usbd_xfer *sc_rd_xfer; 82 u_int sc_rd_count; 83 int sc_rd_readinprogress; 84 int sc_rd_expectdataticks; 85 u_char sc_rd_err; 86 struct framestate sc_framestate; 87 struct lwp *sc_thread; 88 struct selinfo sc_rd_sel; 89 90 uint8_t *sc_wr_buf; 91 int sc_wr_maxpsz; 92 int sc_wr_addr; 93 int sc_wr_stalewrite; 94 struct usbd_xfer *sc_wr_xfer; 95 struct usbd_pipe *sc_wr_pipe; 96 struct selinfo sc_wr_sel; 97 98 enum { 99 udir_input, /* Receiving data */ 100 udir_output, /* Transmitting data */ 101 udir_stalled, /* Error preventing data flow */ 102 udir_idle /* Neither receiving nor transmitting */ 103 } sc_direction; 104 105 device_t sc_child; 106 struct irda_params sc_params; 107 108 int sc_refcnt; 109 char sc_closing; 110 char sc_dying; 111 }; 112 113 /* True if we cannot safely read data from the device */ 114 #define UDSIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0) 115 116 #define UDSIR_WR_TIMEOUT 200 117 118 static int udsir_match(device_t, cfdata_t, void *); 119 static void udsir_attach(device_t, device_t, void *); 120 static int udsir_detach(device_t, int); 121 static void udsir_childdet(device_t, device_t); 122 static int udsir_activate(device_t, enum devact); 123 124 static int udsir_open(void *, int, int, struct lwp *); 125 static int udsir_close(void *, int, int, struct lwp *); 126 static int udsir_read(void *, struct uio *, int); 127 static int udsir_write(void *, struct uio *, int); 128 static int udsir_poll(void *, int, struct lwp *); 129 static int udsir_kqfilter(void *, struct knote *); 130 static int udsir_set_params(void *, struct irda_params *); 131 static int udsir_get_speeds(void *, int *); 132 static int udsir_get_turnarounds(void *, int *); 133 134 static void filt_udsirrdetach(struct knote *); 135 static int filt_udsirread(struct knote *, long); 136 static void filt_udsirwdetach(struct knote *); 137 static int filt_udsirwrite(struct knote *, long); 138 139 static void udsir_thread(void *); 140 141 #ifdef UDSIR_DEBUG 142 static void udsir_dumpdata(uint8_t const *, size_t, char const *); 143 #endif 144 static int deframe_rd_ur(struct udsir_softc *); 145 static void udsir_periodic(struct udsir_softc *); 146 static void udsir_rd_cb(struct usbd_xfer *, void *, usbd_status); 147 static usbd_status udsir_start_read(struct udsir_softc *); 148 149 CFATTACH_DECL2_NEW(udsir, sizeof(struct udsir_softc), 150 udsir_match, udsir_attach, udsir_detach, 151 udsir_activate, NULL, udsir_childdet); 152 153 static struct irframe_methods const udsir_methods = { 154 udsir_open, udsir_close, udsir_read, udsir_write, udsir_poll, 155 udsir_kqfilter, udsir_set_params, udsir_get_speeds, udsir_get_turnarounds, 156 }; 157 158 static int 159 udsir_match(device_t parent, cfdata_t match, void *aux) 160 { 161 struct usbif_attach_arg *uiaa = aux; 162 163 DPRINTFN(50, ("udsir_match\n")); 164 165 if (uiaa->uiaa_vendor == USB_VENDOR_KINGSUN && 166 uiaa->uiaa_product == USB_PRODUCT_KINGSUN_IRDA) 167 return UMATCH_VENDOR_PRODUCT; 168 169 return UMATCH_NONE; 170 } 171 172 static void 173 udsir_attach(device_t parent, device_t self, void *aux) 174 { 175 struct udsir_softc *sc = device_private(self); 176 struct usbif_attach_arg *uiaa = aux; 177 struct usbd_device *dev = uiaa->uiaa_device; 178 struct usbd_interface *iface = uiaa->uiaa_iface; 179 char *devinfop; 180 usb_endpoint_descriptor_t *ed; 181 uint8_t epcount; 182 int i; 183 struct ir_attach_args ia; 184 185 DPRINTFN(10, ("udsir_attach: sc=%p\n", sc)); 186 187 sc->sc_dev = self; 188 189 aprint_naive("\n"); 190 aprint_normal("\n"); 191 192 devinfop = usbd_devinfo_alloc(dev, 0); 193 aprint_normal_dev(self, "%s\n", devinfop); 194 usbd_devinfo_free(devinfop); 195 196 sc->sc_udev = dev; 197 sc->sc_iface = iface; 198 199 epcount = 0; 200 (void)usbd_endpoint_count(iface, &epcount); 201 202 sc->sc_rd_addr = -1; 203 sc->sc_wr_addr = -1; 204 for (i = 0; i < epcount; i++) { 205 ed = usbd_interface2endpoint_descriptor(iface, i); 206 if (ed == NULL) { 207 aprint_error_dev(self, "couldn't get ep %d\n", i); 208 return; 209 } 210 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 211 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 212 sc->sc_rd_addr = ed->bEndpointAddress; 213 sc->sc_rd_maxpsz = UGETW(ed->wMaxPacketSize); 214 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 215 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 216 sc->sc_wr_addr = ed->bEndpointAddress; 217 sc->sc_wr_maxpsz = UGETW(ed->wMaxPacketSize); 218 } 219 } 220 if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) { 221 aprint_error_dev(self, "missing endpoint\n"); 222 return; 223 } 224 225 DPRINTFN(10, ("udsir_attach: %p\n", sc->sc_udev)); 226 227 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 228 sc->sc_dev); 229 230 ia.ia_type = IR_TYPE_IRFRAME; 231 ia.ia_methods = &udsir_methods; 232 ia.ia_handle = sc; 233 234 sc->sc_child = config_found(self, &ia, ir_print); 235 selinit(&sc->sc_rd_sel); 236 selinit(&sc->sc_wr_sel); 237 238 return; 239 } 240 241 static int 242 udsir_detach(device_t self, int flags) 243 { 244 struct udsir_softc *sc = device_private(self); 245 int s; 246 int rv = 0; 247 248 DPRINTFN(0, ("udsir_detach: sc=%p flags=%d\n", sc, flags)); 249 250 sc->sc_closing = sc->sc_dying = 1; 251 252 wakeup(&sc->sc_thread); 253 254 while (sc->sc_thread != NULL) 255 tsleep(&sc->sc_closing, PWAIT, "usircl", 0); 256 257 /* Abort all pipes. Causes processes waiting for transfer to wake. */ 258 if (sc->sc_rd_pipe != NULL) { 259 usbd_abort_pipe(sc->sc_rd_pipe); 260 } 261 if (sc->sc_wr_pipe != NULL) { 262 usbd_abort_pipe(sc->sc_wr_pipe); 263 } 264 if (sc->sc_rd_xfer != NULL) { 265 usbd_destroy_xfer(sc->sc_rd_xfer); 266 sc->sc_rd_xfer = NULL; 267 sc->sc_rd_buf = NULL; 268 } 269 if (sc->sc_wr_xfer != NULL) { 270 usbd_destroy_xfer(sc->sc_wr_xfer); 271 sc->sc_wr_xfer = NULL; 272 sc->sc_wr_buf = NULL; 273 } 274 /* Close pipes. */ 275 if (sc->sc_rd_pipe != NULL) { 276 usbd_close_pipe(sc->sc_rd_pipe); 277 sc->sc_rd_pipe = NULL; 278 } 279 if (sc->sc_wr_pipe != NULL) { 280 usbd_close_pipe(sc->sc_wr_pipe); 281 sc->sc_wr_pipe = NULL; 282 } 283 wakeup(&sc->sc_ur_framelen); 284 wakeup(&sc->sc_wr_buf); 285 286 s = splusb(); 287 if (--sc->sc_refcnt >= 0) { 288 /* Wait for processes to go away. */ 289 usb_detach_waitold(sc->sc_dev); 290 } 291 splx(s); 292 293 if (sc->sc_child != NULL) 294 rv = config_detach(sc->sc_child, flags); 295 296 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 297 298 seldestroy(&sc->sc_rd_sel); 299 seldestroy(&sc->sc_wr_sel); 300 301 return rv; 302 } 303 304 static void 305 udsir_childdet(device_t self, device_t child) 306 { 307 struct udsir_softc *sc = device_private(self); 308 309 KASSERT(sc->sc_child == child); 310 sc->sc_child = NULL; 311 } 312 313 static int 314 udsir_activate(device_t self, enum devact act) 315 { 316 struct udsir_softc *sc = device_private(self); 317 318 switch (act) { 319 case DVACT_DEACTIVATE: 320 sc->sc_dying = 1; 321 return 0; 322 default: 323 return EOPNOTSUPP; 324 } 325 } 326 327 /* ARGSUSED */ 328 static int 329 udsir_open(void *h, int flag, int mode, struct lwp *l) 330 { 331 struct udsir_softc *sc = h; 332 int error; 333 usbd_status err; 334 335 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 336 337 err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe); 338 if (err != USBD_NORMAL_COMPLETION) { 339 error = EIO; 340 goto bad1; 341 } 342 err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe); 343 if (err != USBD_NORMAL_COMPLETION) { 344 error = EIO; 345 goto bad2; 346 } 347 error = usbd_create_xfer(sc->sc_rd_pipe, sc->sc_rd_maxpsz, 348 USBD_SHORT_XFER_OK, 0, &sc->sc_rd_xfer); 349 if (error) 350 goto bad3; 351 352 error = usbd_create_xfer(sc->sc_wr_pipe, IRDA_MAX_FRAME_SIZE, 353 USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer); 354 if (error) 355 goto bad4; 356 357 sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer); 358 sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer); 359 360 sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP); 361 if (sc->sc_ur_buf == NULL) { 362 error = ENOMEM; 363 goto bad5; 364 } 365 366 sc->sc_rd_index = sc->sc_rd_count = 0; 367 sc->sc_closing = 0; 368 sc->sc_rd_readinprogress = 0; 369 sc->sc_rd_expectdataticks = 0; 370 sc->sc_ur_framelen = 0; 371 sc->sc_rd_err = 0; 372 sc->sc_wr_stalewrite = 0; 373 sc->sc_direction = udir_idle; 374 sc->sc_params.speed = 0; 375 sc->sc_params.ebofs = 0; 376 sc->sc_params.maxsize = min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz); 377 378 deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE); 379 380 /* Increment reference for thread */ 381 sc->sc_refcnt++; 382 383 error = kthread_create(PRI_NONE, 0, NULL, udsir_thread, sc, 384 &sc->sc_thread, "%s", device_xname(sc->sc_dev)); 385 if (error) { 386 sc->sc_refcnt--; 387 goto bad5; 388 } 389 390 return 0; 391 392 bad5: 393 usbd_destroy_xfer(sc->sc_wr_xfer); 394 sc->sc_wr_xfer = NULL; 395 bad4: 396 usbd_destroy_xfer(sc->sc_rd_xfer); 397 sc->sc_rd_xfer = NULL; 398 bad3: 399 usbd_close_pipe(sc->sc_wr_pipe); 400 sc->sc_wr_pipe = NULL; 401 bad2: 402 usbd_close_pipe(sc->sc_rd_pipe); 403 sc->sc_rd_pipe = NULL; 404 bad1: 405 return error; 406 } 407 408 /* ARGSUSED */ 409 static int 410 udsir_close(void *h, int flag, int mode, struct lwp *l) 411 { 412 struct udsir_softc *sc = h; 413 414 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 415 416 sc->sc_refcnt++; 417 418 sc->sc_rd_readinprogress = 1; 419 sc->sc_closing = 1; 420 421 wakeup(&sc->sc_thread); 422 423 while (sc->sc_thread != NULL) 424 tsleep(&sc->sc_closing, PWAIT, "usircl", 0); 425 426 if (sc->sc_rd_pipe != NULL) { 427 usbd_abort_pipe(sc->sc_rd_pipe); 428 } 429 if (sc->sc_wr_pipe != NULL) { 430 usbd_abort_pipe(sc->sc_wr_pipe); 431 } 432 if (sc->sc_rd_xfer != NULL) { 433 usbd_destroy_xfer(sc->sc_rd_xfer); 434 sc->sc_rd_xfer = NULL; 435 sc->sc_rd_buf = NULL; 436 } 437 if (sc->sc_wr_xfer != NULL) { 438 usbd_destroy_xfer(sc->sc_wr_xfer); 439 sc->sc_wr_xfer = NULL; 440 sc->sc_wr_buf = NULL; 441 } 442 if (sc->sc_rd_pipe != NULL) { 443 usbd_close_pipe(sc->sc_rd_pipe); 444 sc->sc_rd_pipe = NULL; 445 } 446 if (sc->sc_wr_pipe != NULL) { 447 usbd_close_pipe(sc->sc_wr_pipe); 448 sc->sc_wr_pipe = NULL; 449 } 450 if (sc->sc_ur_buf != NULL) { 451 kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE); 452 sc->sc_ur_buf = NULL; 453 } 454 455 if (--sc->sc_refcnt < 0) 456 usb_detach_wakeupold(sc->sc_dev); 457 458 return 0; 459 } 460 461 /* ARGSUSED */ 462 static int 463 udsir_read(void *h, struct uio *uio, int flag) 464 { 465 struct udsir_softc *sc = h; 466 int s; 467 int error; 468 u_int uframelen; 469 470 DPRINTFN(1, ("%s: sc=%p\n", __func__, sc)); 471 472 if (sc->sc_dying) 473 return EIO; 474 475 #ifdef DIAGNOSTIC 476 if (sc->sc_rd_buf == NULL) 477 return EINVAL; 478 #endif 479 480 sc->sc_refcnt++; 481 482 if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc)) 483 /* Possibly wake up polling thread */ 484 wakeup(&sc->sc_thread); 485 486 do { 487 s = splusb(); 488 while (sc->sc_ur_framelen == 0) { 489 DPRINTFN(5, ("%s: calling tsleep()\n", __func__)); 490 error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH, 491 "usirrd", 0); 492 if (sc->sc_dying) 493 error = EIO; 494 if (error) { 495 splx(s); 496 DPRINTFN(0, ("%s: tsleep() = %d\n", 497 __func__, error)); 498 goto ret; 499 } 500 } 501 splx(s); 502 503 uframelen = sc->sc_ur_framelen; 504 DPRINTFN(1, ("%s: sc=%p framelen=%u, hdr=0x%02x\n", 505 __func__, sc, uframelen, sc->sc_ur_buf[0])); 506 if (uframelen > uio->uio_resid) 507 error = EINVAL; 508 else 509 error = uiomove(sc->sc_ur_buf, uframelen, uio); 510 sc->sc_ur_framelen = 0; 511 512 if (deframe_rd_ur(sc) == 0 && uframelen > 0) { 513 /* 514 * Need to wait for another read to obtain a 515 * complete frame... If we also obtained 516 * actual data, wake up the possibly sleeping 517 * thread immediately... 518 */ 519 wakeup(&sc->sc_thread); 520 } 521 } while (uframelen == 0); 522 523 DPRINTFN(1, ("%s: return %d\n", __func__, error)); 524 525 ret: 526 if (--sc->sc_refcnt < 0) 527 usb_detach_wakeupold(sc->sc_dev); 528 return error; 529 } 530 531 /* ARGSUSED */ 532 static int 533 udsir_write(void *h, struct uio *uio, int flag) 534 { 535 struct udsir_softc *sc = h; 536 usbd_status err; 537 uint32_t wrlen; 538 int error, sirlength; 539 uint8_t *wrbuf; 540 int s; 541 542 DPRINTFN(1, ("%s: sc=%p\n", __func__, sc)); 543 544 if (sc->sc_dying) 545 return EIO; 546 547 #ifdef DIAGNOSTIC 548 if (sc->sc_wr_buf == NULL) 549 return EINVAL; 550 #endif 551 552 wrlen = uio->uio_resid; 553 if (wrlen > sc->sc_wr_maxpsz) 554 return EINVAL; 555 556 sc->sc_refcnt++; 557 558 if (!UDSIR_BLOCK_RX_DATA(sc)) { 559 /* 560 * If reads are not blocked, determine what action we 561 * should potentially take... 562 */ 563 if (sc->sc_direction == udir_output) { 564 /* 565 * If the last operation was an output, wait for the 566 * polling thread to check for incoming data. 567 */ 568 sc->sc_wr_stalewrite = 1; 569 wakeup(&sc->sc_thread); 570 } else if (!sc->sc_rd_readinprogress && 571 (sc->sc_direction == udir_idle || 572 sc->sc_direction == udir_input)) { 573 /* If idle, check for input before outputting */ 574 udsir_start_read(sc); 575 } 576 } 577 578 s = splusb(); 579 while (sc->sc_wr_stalewrite || 580 (sc->sc_direction != udir_output && 581 sc->sc_direction != udir_idle)) { 582 DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, " 583 "calling tsleep()\n", 584 __func__, sc, sc->sc_wr_stalewrite, 585 sc->sc_direction)); 586 error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH, "usirwr", 0); 587 if (sc->sc_dying) 588 error = EIO; 589 if (error) { 590 splx(s); 591 DPRINTFN(0, ("%s: tsleep() = %d\n", __func__, error)); 592 goto ret; 593 } 594 } 595 splx(s); 596 597 wrbuf = sc->sc_wr_buf; 598 599 sirlength = irda_sir_frame(wrbuf, MAX_UDSIR_OUTPUT_FRAME, 600 uio, sc->sc_params.ebofs); 601 if (sirlength < 0) 602 error = -sirlength; 603 else { 604 uint32_t btlen; 605 606 DPRINTFN(1, ("%s: transfer %u bytes\n", 607 __func__, (unsigned int)wrlen)); 608 609 btlen = sirlength; 610 611 sc->sc_direction = udir_output; 612 613 #ifdef UDSIR_DEBUG 614 if (udsirdebug >= 20) 615 udsir_dumpdata(wrbuf, btlen, __func__); 616 #endif 617 618 err = usbd_intr_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe, 619 USBD_FORCE_SHORT_XFER, UDSIR_WR_TIMEOUT, 620 wrbuf, &btlen); 621 DPRINTFN(2, ("%s: err=%d\n", __func__, err)); 622 if (err != USBD_NORMAL_COMPLETION) { 623 if (err == USBD_INTERRUPTED) 624 error = EINTR; 625 else if (err == USBD_TIMEOUT) 626 error = ETIMEDOUT; 627 else 628 error = EIO; 629 } else 630 error = 0; 631 } 632 633 ret: 634 if (--sc->sc_refcnt < 0) 635 usb_detach_wakeupold(sc->sc_dev); 636 637 DPRINTFN(1, ("%s: sc=%p done\n", __func__, sc)); 638 return error; 639 } 640 641 static int 642 udsir_poll(void *h, int events, struct lwp *l) 643 { 644 struct udsir_softc *sc = h; 645 int revents = 0; 646 647 DPRINTFN(1, ("%s: sc=%p\n", __func__, sc)); 648 649 if (events & (POLLOUT | POLLWRNORM)) { 650 if (sc->sc_direction != udir_input) 651 revents |= events & (POLLOUT | POLLWRNORM); 652 else { 653 DPRINTFN(2, ("%s: recording write select\n", __func__)); 654 selrecord(l, &sc->sc_wr_sel); 655 } 656 } 657 658 if (events & (POLLIN | POLLRDNORM)) { 659 if (sc->sc_ur_framelen != 0) { 660 DPRINTFN(2, ("%s: have data\n", __func__)); 661 revents |= events & (POLLIN | POLLRDNORM); 662 } else { 663 DPRINTFN(2, ("%s: recording read select\n", __func__)); 664 selrecord(l, &sc->sc_rd_sel); 665 } 666 } 667 668 return revents; 669 } 670 671 static const struct filterops udsirread_filtops = 672 { 1, NULL, filt_udsirrdetach, filt_udsirread }; 673 static const struct filterops udsirwrite_filtops = 674 { 1, NULL, filt_udsirwdetach, filt_udsirwrite }; 675 676 static int 677 udsir_kqfilter(void *h, struct knote *kn) 678 { 679 struct udsir_softc *sc = h; 680 struct klist *klist; 681 int s; 682 683 switch (kn->kn_filter) { 684 case EVFILT_READ: 685 klist = &sc->sc_rd_sel.sel_klist; 686 kn->kn_fop = &udsirread_filtops; 687 break; 688 case EVFILT_WRITE: 689 klist = &sc->sc_wr_sel.sel_klist; 690 kn->kn_fop = &udsirwrite_filtops; 691 break; 692 default: 693 return (EINVAL); 694 } 695 696 kn->kn_hook = sc; 697 698 s = splusb(); 699 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 700 splx(s); 701 702 return (0); 703 } 704 705 static int 706 udsir_set_params(void *h, struct irda_params *p) 707 { 708 struct udsir_softc *sc = h; 709 710 DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", 711 __func__, sc, p->speed, p->ebofs, p->maxsize)); 712 713 if (sc->sc_dying) 714 return EIO; 715 716 if (p->speed != 9600) 717 return EINVAL; 718 719 if (p->maxsize != sc->sc_params.maxsize) { 720 if (p->maxsize > min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz)) 721 return EINVAL; 722 sc->sc_params.maxsize = p->maxsize; 723 } 724 725 sc->sc_params = *p; 726 727 return 0; 728 } 729 730 static int 731 udsir_get_speeds(void *h, int *speeds) 732 { 733 struct udsir_softc *sc = h; 734 735 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 736 737 if (sc->sc_dying) 738 return EIO; 739 740 /* Support only 9600bps now. */ 741 *speeds = IRDA_SPEED_9600; 742 743 return 0; 744 } 745 746 static int 747 udsir_get_turnarounds(void *h, int *turnarounds) 748 { 749 struct udsir_softc *sc = h; 750 751 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 752 753 if (sc->sc_dying) 754 return EIO; 755 756 /* 757 * Documentation is on the light side with respect to 758 * turnaround time for this device. 759 */ 760 *turnarounds = IRDA_TURNT_10000; 761 762 return 0; 763 } 764 765 static void 766 filt_udsirrdetach(struct knote *kn) 767 { 768 struct udsir_softc *sc = kn->kn_hook; 769 int s; 770 771 s = splusb(); 772 SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext); 773 splx(s); 774 } 775 776 /* ARGSUSED */ 777 static int 778 filt_udsirread(struct knote *kn, long hint) 779 { 780 struct udsir_softc *sc = kn->kn_hook; 781 782 kn->kn_data = sc->sc_ur_framelen; 783 return (kn->kn_data > 0); 784 } 785 786 static void 787 filt_udsirwdetach(struct knote *kn) 788 { 789 struct udsir_softc *sc = kn->kn_hook; 790 int s; 791 792 s = splusb(); 793 SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext); 794 splx(s); 795 } 796 797 /* ARGSUSED */ 798 static int 799 filt_udsirwrite(struct knote *kn, long hint) 800 { 801 struct udsir_softc *sc = kn->kn_hook; 802 803 kn->kn_data = 0; 804 return (sc->sc_direction != udir_input); 805 } 806 807 808 static void 809 udsir_thread(void *arg) 810 { 811 struct udsir_softc *sc = arg; 812 int error; 813 814 DPRINTFN(20, ("%s: starting polling thread\n", __func__)); 815 816 while (!sc->sc_closing) { 817 if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc)) 818 udsir_periodic(sc); 819 820 if (!sc->sc_closing) { 821 error = tsleep(&sc->sc_thread, PWAIT, "udsir", hz / 10); 822 if (error == EWOULDBLOCK && 823 sc->sc_rd_expectdataticks > 0) 824 /* 825 * After a timeout decrement the tick 826 * counter within which time we expect 827 * data to arrive if we are receiving 828 * data... 829 */ 830 sc->sc_rd_expectdataticks--; 831 } 832 } 833 834 DPRINTFN(20, ("%s: exiting polling thread\n", __func__)); 835 836 sc->sc_thread = NULL; 837 838 wakeup(&sc->sc_closing); 839 840 if (--sc->sc_refcnt < 0) 841 usb_detach_wakeupold(sc->sc_dev); 842 843 kthread_exit(0); 844 } 845 846 #ifdef UDSIR_DEBUG 847 static void 848 udsir_dumpdata(uint8_t const *data, size_t dlen, char const *desc) 849 { 850 size_t bdindex; 851 852 printf("%s: (%lx)", desc, (unsigned long)dlen); 853 for (bdindex = 0; bdindex < dlen; bdindex++) 854 printf(" %02x", (unsigned int)data[bdindex]); 855 printf("\n"); 856 } 857 #endif 858 859 /* Returns 0 if more data required, 1 if a complete frame was extracted */ 860 static int 861 deframe_rd_ur(struct udsir_softc *sc) 862 { 863 864 if (sc->sc_rd_index == 0) { 865 KASSERT(sc->sc_rd_count == sc->sc_rd_maxpsz); 866 /* valid count */ 867 sc->sc_rd_count = sc->sc_rd_buf[sc->sc_rd_index++] + 1; 868 KASSERT(sc->sc_rd_count < sc->sc_rd_maxpsz); 869 } 870 871 while (sc->sc_rd_index < sc->sc_rd_count) { 872 uint8_t const *buf; 873 size_t buflen; 874 enum frameresult fresult; 875 876 buf = &sc->sc_rd_buf[sc->sc_rd_index]; 877 buflen = sc->sc_rd_count - sc->sc_rd_index; 878 879 fresult = deframe_process(&sc->sc_framestate, &buf, &buflen); 880 881 sc->sc_rd_index = sc->sc_rd_count - buflen; 882 883 DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult)); 884 885 switch (fresult) { 886 case FR_IDLE: 887 case FR_INPROGRESS: 888 case FR_FRAMEBADFCS: 889 case FR_FRAMEMALFORMED: 890 case FR_BUFFEROVERRUN: 891 break; 892 case FR_FRAMEOK: 893 sc->sc_ur_framelen = sc->sc_framestate.bufindex; 894 wakeup(&sc->sc_ur_framelen); /* XXX should use flag */ 895 selnotify(&sc->sc_rd_sel, 0, 0); 896 return 1; 897 } 898 } 899 900 /* Reset indices into USB-side buffer */ 901 sc->sc_rd_index = sc->sc_rd_count = 0; 902 903 return 0; 904 } 905 906 /* 907 * Direction transitions: 908 * 909 * udsir_periodic() can switch the direction from: 910 * 911 * output -> idle 912 * output -> stalled 913 * stalled -> idle 914 * idle -> input 915 * 916 * udsir_rd_cb() can switch the direction from: 917 * 918 * input -> stalled 919 * input -> idle 920 * 921 * udsir_write() can switch the direction from: 922 * 923 * idle -> output 924 */ 925 static void 926 udsir_periodic(struct udsir_softc *sc) 927 { 928 929 DPRINTFN(60, ("%s: direction = %d\n", __func__, sc->sc_direction)); 930 931 if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) { 932 /* 933 * In a stale write case, we need to check if the 934 * write has completed. Once that has happened, the 935 * write is no longer stale. 936 * 937 * But note that we may immediately start a read poll... 938 */ 939 sc->sc_wr_stalewrite = 0; 940 wakeup(&sc->sc_wr_buf); 941 } 942 943 if (!sc->sc_rd_readinprogress && 944 (sc->sc_direction == udir_idle || 945 sc->sc_direction == udir_input)) 946 /* Do a read poll if appropriate... */ 947 udsir_start_read(sc); 948 } 949 950 static void 951 udsir_rd_cb(struct usbd_xfer *xfer, void * priv, usbd_status status) 952 { 953 struct udsir_softc *sc = priv; 954 uint32_t size; 955 956 DPRINTFN(60, ("%s: sc=%p\n", __func__, sc)); 957 958 /* Read is no longer in progress */ 959 sc->sc_rd_readinprogress = 0; 960 961 if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */ 962 return; 963 if (status) { 964 size = 0; 965 sc->sc_rd_err = 1; 966 967 if (sc->sc_direction == udir_input || 968 sc->sc_direction == udir_idle) { 969 /* 970 * Receive error, probably need to clear error 971 * condition. 972 */ 973 sc->sc_direction = udir_stalled; 974 } 975 } else 976 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL); 977 978 sc->sc_rd_index = 0; 979 sc->sc_rd_count = size; 980 981 DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60), 982 ("%s: sc=%p size=%u, err=%d\n", 983 __func__, sc, size, sc->sc_rd_err)); 984 985 #ifdef UDSIR_DEBUG 986 if (udsirdebug >= 20 && size > 0) 987 udsir_dumpdata(sc->sc_rd_buf, size, __func__); 988 #endif 989 990 if (deframe_rd_ur(sc) == 0) { 991 if (!deframe_isclear(&sc->sc_framestate) && size == 0 && 992 sc->sc_rd_expectdataticks == 0) { 993 /* 994 * Expected data, but didn't get it 995 * within expected time... 996 */ 997 DPRINTFN(5,("%s: incoming packet timeout\n", 998 __func__)); 999 deframe_clear(&sc->sc_framestate); 1000 } else if (size > 0) { 1001 /* 1002 * If we also received actual data, reset the 1003 * data read timeout and wake up the possibly 1004 * sleeping thread... 1005 */ 1006 sc->sc_rd_expectdataticks = 2; 1007 wakeup(&sc->sc_thread); 1008 } 1009 } 1010 1011 /* 1012 * Check if incoming data has stopped, or that we cannot 1013 * safely read any more data. In the case of the latter we 1014 * must switch to idle so that a write will not block... 1015 */ 1016 if (sc->sc_direction == udir_input && 1017 ((size == 0 && sc->sc_rd_expectdataticks == 0) || 1018 UDSIR_BLOCK_RX_DATA(sc))) { 1019 DPRINTFN(8, ("%s: idling on packet timeout, " 1020 "complete frame, or no data\n", __func__)); 1021 sc->sc_direction = udir_idle; 1022 1023 /* Wake up for possible output */ 1024 wakeup(&sc->sc_wr_buf); 1025 selnotify(&sc->sc_wr_sel, 0, 0); 1026 } 1027 } 1028 1029 static usbd_status 1030 udsir_start_read(struct udsir_softc *sc) 1031 { 1032 usbd_status err; 1033 1034 DPRINTFN(60, ("%s: sc=%p, size=%d\n", __func__, sc, sc->sc_rd_maxpsz)); 1035 1036 if (sc->sc_dying) 1037 return USBD_IOERROR; 1038 1039 if (UDSIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) { 1040 /* 1041 * Can't start reading just yet. Since we aren't 1042 * going to start a read, have to switch direction to 1043 * idle. 1044 */ 1045 sc->sc_direction = udir_idle; 1046 return USBD_NORMAL_COMPLETION; 1047 } 1048 1049 /* Starting a read... */ 1050 sc->sc_rd_readinprogress = 1; 1051 sc->sc_direction = udir_input; 1052 1053 if (sc->sc_rd_err) { 1054 sc->sc_rd_err = 0; 1055 DPRINTFN(0, ("%s: clear stall\n", __func__)); 1056 usbd_clear_endpoint_stall(sc->sc_rd_pipe); 1057 } 1058 1059 usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf, sc->sc_rd_maxpsz, 1060 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udsir_rd_cb); 1061 err = usbd_transfer(sc->sc_rd_xfer); 1062 if (err != USBD_IN_PROGRESS) { 1063 DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err)); 1064 return err; 1065 } 1066 return USBD_NORMAL_COMPLETION; 1067 } 1068