1 /* $NetBSD: ustir.c,v 1.42 2019/09/22 07:28:35 dsainty 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 <dsainty@NetBSD.org> 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: ustir.c,v 1.42 2019/09/22 07:28:35 dsainty Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "opt_usb.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/kmem.h> 44 #include <sys/conf.h> 45 #include <sys/file.h> 46 #include <sys/poll.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/kthread.h> 50 51 #ifdef USTIR_DEBUG_IOCTLS 52 #include <sys/ioctl.h> 53 #include <dev/usb/ustir.h> 54 #endif 55 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/ustirreg.h> 61 62 #include <dev/ir/ir.h> 63 #include <dev/ir/irdaio.h> 64 #include <dev/ir/irframevar.h> 65 #include <dev/ir/sir.h> 66 67 #ifdef USTIR_DEBUG 68 #define DPRINTFN(n,x) if (ustirdebug>(n)) printf x 69 int ustirdebug = 0; 70 #else 71 #define DPRINTFN(n,x) 72 #endif 73 74 /* Max size with framing. */ 75 #define MAX_USTIR_OUTPUT_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + STIR_OUTPUT_HEADER_SIZE + 4) 76 77 #define USTIR_NSPEEDS 9 78 struct ustir_speedrec { 79 unsigned int speed; 80 unsigned int config; 81 }; 82 83 Static struct ustir_speedrec const ustir_speeds[USTIR_NSPEEDS] = { 84 { 4000000, STIR_BRMODE_4000000 }, 85 { 1152000, STIR_BRMODE_1152000 }, 86 { 576000, STIR_BRMODE_576000 }, 87 { 115200, STIR_BRMODE_115200 }, 88 { 57600, STIR_BRMODE_57600 }, 89 { 38400, STIR_BRMODE_38400 }, 90 { 19200, STIR_BRMODE_19200 }, 91 { 9600, STIR_BRMODE_9600 }, 92 { 2400, STIR_BRMODE_2400 } 93 }; 94 95 struct ustir_softc { 96 device_t sc_dev; 97 struct usbd_device *sc_udev; 98 struct usbd_interface *sc_iface; 99 enum { 100 USTIR_INIT_NONE, 101 USTIR_INIT_INITED 102 } sc_init_state; 103 104 uint8_t *sc_ur_buf; /* Unencapsulated frame */ 105 u_int sc_ur_framelen; 106 107 uint8_t *sc_rd_buf; /* Raw incoming data stream */ 108 size_t sc_rd_index; 109 int sc_rd_addr; 110 struct usbd_pipe *sc_rd_pipe; 111 struct usbd_xfer *sc_rd_xfer; 112 u_int sc_rd_count; 113 int sc_rd_readinprogress; 114 u_int sc_rd_expectdataticks; 115 u_char sc_rd_err; 116 struct framestate sc_framestate; 117 struct lwp *sc_thread; 118 struct selinfo sc_rd_sel; 119 120 uint8_t *sc_wr_buf; 121 int sc_wr_addr; 122 int sc_wr_stalewrite; 123 struct usbd_xfer *sc_wr_xfer; 124 struct usbd_pipe *sc_wr_pipe; 125 struct selinfo sc_wr_sel; 126 127 enum { 128 udir_input, /* Receiving data */ 129 udir_output, /* Transmitting data */ 130 udir_stalled, /* Error preventing data flow */ 131 udir_idle /* Neither receiving nor transmitting */ 132 } sc_direction; 133 134 struct ustir_speedrec const *sc_speedrec; 135 136 device_t sc_child; 137 struct irda_params sc_params; 138 139 int sc_refcnt; 140 char sc_closing; 141 char sc_dying; 142 }; 143 144 /* True if we cannot safely read data from the device */ 145 #define USTIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0) 146 147 #define USTIR_WR_TIMEOUT 200 148 149 Static int ustir_activate(device_t, enum devact); 150 Static int ustir_open(void *, int, int, struct lwp *); 151 Static int ustir_close(void *, int, int, struct lwp *); 152 Static int ustir_read(void *, struct uio *, int); 153 Static int ustir_write(void *, struct uio *, int); 154 Static int ustir_set_params(void *, struct irda_params *); 155 Static int ustir_get_speeds(void *, int *); 156 Static int ustir_get_turnarounds(void *, int *); 157 Static int ustir_poll(void *, int, struct lwp *); 158 Static int ustir_kqfilter(void *, struct knote *); 159 160 #ifdef USTIR_DEBUG_IOCTLS 161 Static int ustir_ioctl(void *, u_long, void *, int, struct lwp *); 162 #endif 163 164 Static struct irframe_methods const ustir_methods = { 165 ustir_open, ustir_close, ustir_read, ustir_write, ustir_poll, 166 ustir_kqfilter, ustir_set_params, ustir_get_speeds, 167 ustir_get_turnarounds, 168 #ifdef USTIR_DEBUG_IOCTLS 169 ustir_ioctl 170 #endif 171 }; 172 173 Static void ustir_rd_cb(struct usbd_xfer *, void *, usbd_status); 174 Static usbd_status ustir_start_read(struct ustir_softc *); 175 Static void ustir_periodic(struct ustir_softc *); 176 Static void ustir_thread(void *); 177 178 static usbd_status 179 ustir_read_reg(struct ustir_softc *sc, unsigned int reg, uint8_t *data) 180 { 181 usb_device_request_t req; 182 183 req.bmRequestType = UT_READ_VENDOR_DEVICE; 184 req.bRequest = STIR_CMD_READMULTIREG; 185 USETW(req.wValue, 0); 186 USETW(req.wIndex, reg); 187 USETW(req.wLength, 1); 188 189 return usbd_do_request(sc->sc_udev, &req, data); 190 } 191 192 static usbd_status 193 ustir_write_reg(struct ustir_softc *sc, unsigned int reg, uint8_t data) 194 { 195 usb_device_request_t req; 196 197 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 198 req.bRequest = STIR_CMD_WRITESINGLEREG; 199 USETW(req.wValue, data); 200 USETW(req.wIndex, reg); 201 USETW(req.wLength, 0); 202 203 return usbd_do_request(sc->sc_udev, &req, NULL); 204 } 205 206 #ifdef USTIR_DEBUG 207 static void 208 ustir_dumpdata(uint8_t const *data, size_t dlen, char const *desc) 209 { 210 size_t bdindex; 211 printf("%s: (%lx)", desc, (unsigned long)dlen); 212 for (bdindex = 0; bdindex < dlen; bdindex++) 213 printf(" %02x", (unsigned int)data[bdindex]); 214 printf("\n"); 215 } 216 #endif 217 218 int ustir_match(device_t, cfdata_t, void *); 219 void ustir_attach(device_t, device_t, void *); 220 void ustir_childdet(device_t, device_t); 221 int ustir_detach(device_t, int); 222 int ustir_activate(device_t, enum devact); 223 224 CFATTACH_DECL2_NEW(ustir, sizeof(struct ustir_softc), ustir_match, 225 ustir_attach, ustir_detach, ustir_activate, NULL, ustir_childdet); 226 227 int 228 ustir_match(device_t parent, cfdata_t match, void *aux) 229 { 230 struct usb_attach_arg *uaa = aux; 231 232 DPRINTFN(50,("ustir_match\n")); 233 234 if (uaa->uaa_vendor == USB_VENDOR_SIGMATEL && 235 uaa->uaa_product == USB_PRODUCT_SIGMATEL_IRDA) 236 return UMATCH_VENDOR_PRODUCT; 237 238 return UMATCH_NONE; 239 } 240 241 void 242 ustir_attach(device_t parent, device_t self, void *aux) 243 { 244 struct ustir_softc *sc = device_private(self); 245 struct usb_attach_arg *uaa = aux; 246 struct usbd_device *dev = uaa->uaa_device; 247 struct usbd_interface *iface; 248 char *devinfop; 249 usb_endpoint_descriptor_t *ed; 250 uint8_t epcount; 251 int i; 252 struct ir_attach_args ia; 253 254 DPRINTFN(10,("ustir_attach: sc=%p\n", sc)); 255 256 sc->sc_dev = self; 257 sc->sc_init_state = USTIR_INIT_NONE; 258 259 aprint_naive("\n"); 260 aprint_normal("\n"); 261 262 devinfop = usbd_devinfo_alloc(dev, 0); 263 aprint_normal_dev(self, "%s\n", devinfop); 264 usbd_devinfo_free(devinfop); 265 266 if (usbd_set_config_index(dev, 0, 1) 267 || usbd_device2interface_handle(dev, 0, &iface)) { 268 aprint_error_dev(self, "Configuration failed\n"); 269 return; 270 } 271 272 sc->sc_udev = dev; 273 sc->sc_iface = iface; 274 275 epcount = 0; 276 (void)usbd_endpoint_count(iface, &epcount); 277 278 sc->sc_rd_addr = -1; 279 sc->sc_wr_addr = -1; 280 for (i = 0; i < epcount; i++) { 281 ed = usbd_interface2endpoint_descriptor(iface, i); 282 if (ed == NULL) { 283 aprint_error_dev(self, "couldn't get ep %d\n", i); 284 return; 285 } 286 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 287 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 288 sc->sc_rd_addr = ed->bEndpointAddress; 289 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 290 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 291 sc->sc_wr_addr = ed->bEndpointAddress; 292 } 293 } 294 if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) { 295 aprint_error_dev(self, "missing endpoint\n"); 296 return; 297 } 298 299 DPRINTFN(10, ("ustir_attach: %p\n", sc->sc_udev)); 300 301 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 302 303 ia.ia_type = IR_TYPE_IRFRAME; 304 ia.ia_methods = &ustir_methods; 305 ia.ia_handle = sc; 306 307 sc->sc_child = config_found(self, &ia, ir_print); 308 selinit(&sc->sc_rd_sel); 309 selinit(&sc->sc_wr_sel); 310 sc->sc_init_state = USTIR_INIT_INITED; 311 312 return; 313 } 314 315 void 316 ustir_childdet(device_t self, device_t child) 317 { 318 struct ustir_softc *sc = device_private(self); 319 320 KASSERT(sc->sc_child == child); 321 sc->sc_child = NULL; 322 } 323 324 int 325 ustir_detach(device_t self, int flags) 326 { 327 struct ustir_softc *sc = device_private(self); 328 int s; 329 int rv = 0; 330 331 DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc, flags)); 332 333 sc->sc_closing = sc->sc_dying = 1; 334 335 wakeup(&sc->sc_thread); 336 337 while (sc->sc_thread != NULL) 338 tsleep(&sc->sc_closing, PWAIT, "usircl", 0); 339 340 /* Abort all pipes. Causes processes waiting for transfer to wake. */ 341 if (sc->sc_rd_pipe != NULL) { 342 usbd_abort_pipe(sc->sc_rd_pipe); 343 } 344 if (sc->sc_wr_pipe != NULL) { 345 usbd_abort_pipe(sc->sc_wr_pipe); 346 } 347 if (sc->sc_rd_xfer != NULL) { 348 usbd_destroy_xfer(sc->sc_rd_xfer); 349 sc->sc_rd_xfer = NULL; 350 sc->sc_rd_buf = NULL; 351 } 352 if (sc->sc_wr_xfer != NULL) { 353 usbd_destroy_xfer(sc->sc_wr_xfer); 354 sc->sc_wr_xfer = NULL; 355 sc->sc_wr_buf = NULL; 356 } 357 if (sc->sc_rd_pipe != NULL) { 358 usbd_close_pipe(sc->sc_rd_pipe); 359 sc->sc_rd_pipe = NULL; 360 } 361 if (sc->sc_wr_pipe != NULL) { 362 usbd_close_pipe(sc->sc_wr_pipe); 363 sc->sc_wr_pipe = NULL; 364 } 365 wakeup(&sc->sc_ur_framelen); 366 wakeup(&sc->sc_wr_buf); 367 368 s = splusb(); 369 if (--sc->sc_refcnt >= 0) { 370 /* Wait for processes to go away. */ 371 usb_detach_waitold(sc->sc_dev); 372 } 373 splx(s); 374 375 if (sc->sc_child != NULL) 376 rv = config_detach(sc->sc_child, flags); 377 378 if (sc->sc_init_state >= USTIR_INIT_INITED) { 379 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 380 sc->sc_dev); 381 seldestroy(&sc->sc_rd_sel); 382 seldestroy(&sc->sc_wr_sel); 383 } 384 385 return rv; 386 } 387 388 /* Returns 0 if more data required, 1 if a complete frame was extracted */ 389 static int 390 deframe_rd_ur(struct ustir_softc *sc) 391 { 392 while (sc->sc_rd_index < sc->sc_rd_count) { 393 uint8_t const *buf; 394 size_t buflen; 395 enum frameresult fresult; 396 397 buf = &sc->sc_rd_buf[sc->sc_rd_index]; 398 buflen = sc->sc_rd_count - sc->sc_rd_index; 399 400 fresult = deframe_process(&sc->sc_framestate, &buf, &buflen); 401 402 sc->sc_rd_index = sc->sc_rd_count - buflen; 403 404 DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult)); 405 406 switch (fresult) { 407 case FR_IDLE: 408 case FR_INPROGRESS: 409 case FR_FRAMEBADFCS: 410 case FR_FRAMEMALFORMED: 411 case FR_BUFFEROVERRUN: 412 break; 413 case FR_FRAMEOK: 414 sc->sc_ur_framelen = sc->sc_framestate.bufindex; 415 wakeup(&sc->sc_ur_framelen); /* XXX should use flag */ 416 selnotify(&sc->sc_rd_sel, 0, 0); 417 return 1; 418 } 419 } 420 421 /* Reset indices into USB-side buffer */ 422 sc->sc_rd_index = sc->sc_rd_count = 0; 423 424 return 0; 425 } 426 427 /* 428 * Direction transitions: 429 * 430 * ustir_periodic() can switch the direction from: 431 * 432 * output -> idle 433 * output -> stalled 434 * stalled -> idle 435 * idle -> input 436 * 437 * ustir_rd_cb() can switch the direction from: 438 * 439 * input -> stalled 440 * input -> idle 441 * 442 * ustir_write() can switch the direction from: 443 * 444 * idle -> output 445 */ 446 Static void 447 ustir_periodic(struct ustir_softc *sc) 448 { 449 DPRINTFN(60, ("%s: direction = %d\n", 450 __func__, sc->sc_direction)); 451 452 if (sc->sc_direction == udir_output || 453 sc->sc_direction == udir_stalled) { 454 usbd_status err; 455 uint8_t regval; 456 457 DPRINTFN(60, ("%s: reading status register\n", 458 __func__)); 459 460 err = ustir_read_reg(sc, STIR_REG_STATUS, 461 ®val); 462 if (err != USBD_NORMAL_COMPLETION) { 463 aprint_error_dev(sc->sc_dev, 464 "status register read failed: %s\n", 465 usbd_errstr(err)); 466 } else { 467 DPRINTFN(10, ("%s: status register = 0x%x\n", 468 __func__, 469 (unsigned int)regval)); 470 if (sc->sc_direction == udir_output && 471 !(regval & STIR_RSTATUS_FFDIR)) 472 /* Output has completed */ 473 sc->sc_direction = udir_idle; 474 if (regval & STIR_RSTATUS_FFOVER) { 475 /* 476 * On an overrun the FIFO hangs, and 477 * any data bulk transfers will stall. 478 * Reset the FIFO. 479 */ 480 sc->sc_direction = udir_stalled; 481 482 DPRINTFN(10, ("%s: clearing FIFO error\n", 483 __func__)); 484 485 err = ustir_write_reg(sc, STIR_REG_STATUS, 486 STIR_RSTATUS_FFCLR); 487 /* XXX if we fail partway through 488 * this, we may not recover? */ 489 if (err == USBD_NORMAL_COMPLETION) 490 err = ustir_write_reg(sc, 491 STIR_REG_STATUS, 492 0); 493 if (err != USBD_NORMAL_COMPLETION) { 494 aprint_error_dev(sc->sc_dev, 495 "FIFO reset failed: %s\n", 496 usbd_errstr(err)); 497 } else { 498 /* FIFO reset */ 499 sc->sc_direction = udir_idle; 500 } 501 } 502 } 503 } 504 505 if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) { 506 /* 507 * In a stale write case, we need to check if the 508 * write has completed. Once that has happened, the 509 * write is no longer stale. 510 * 511 * But note that we may immediately start a read poll... 512 */ 513 sc->sc_wr_stalewrite = 0; 514 wakeup(&sc->sc_wr_buf); 515 } 516 517 if (!sc->sc_rd_readinprogress && 518 (sc->sc_direction == udir_idle || 519 sc->sc_direction == udir_input)) 520 /* Do a read poll if appropriate... */ 521 ustir_start_read(sc); 522 } 523 524 Static void 525 ustir_thread(void *arg) 526 { 527 struct ustir_softc *sc = arg; 528 529 DPRINTFN(20, ("%s: starting polling thread\n", __func__)); 530 531 while (!sc->sc_closing) { 532 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc)) 533 ustir_periodic(sc); 534 535 if (!sc->sc_closing) { 536 int error; 537 error = tsleep(&sc->sc_thread, PWAIT, 538 "ustir", hz / 10); 539 if (error == EWOULDBLOCK && 540 sc->sc_rd_expectdataticks > 0) 541 /* 542 * After a timeout decrement the tick 543 * counter within which time we expect 544 * data to arrive if we are receiving 545 * data... 546 */ 547 sc->sc_rd_expectdataticks--; 548 } 549 } 550 551 DPRINTFN(20, ("%s: exiting polling thread\n", __func__)); 552 553 sc->sc_thread = NULL; 554 555 wakeup(&sc->sc_closing); 556 557 if (--sc->sc_refcnt < 0) 558 usb_detach_wakeupold(sc->sc_dev); 559 560 kthread_exit(0); 561 } 562 563 Static void 564 ustir_rd_cb(struct usbd_xfer *xfer, void *priv, 565 usbd_status status) 566 { 567 struct ustir_softc *sc = priv; 568 uint32_t size; 569 570 DPRINTFN(60, ("%s: sc=%p\n", __func__, sc)); 571 572 /* Read is no longer in progress */ 573 sc->sc_rd_readinprogress = 0; 574 575 if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */ 576 return; 577 if (status) { 578 size = 0; 579 sc->sc_rd_err = 1; 580 581 if (sc->sc_direction == udir_input || 582 sc->sc_direction == udir_idle) { 583 /* 584 * Receive error, probably need to clear error 585 * condition. 586 */ 587 sc->sc_direction = udir_stalled; 588 } 589 } else { 590 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL); 591 } 592 593 sc->sc_rd_index = 0; 594 sc->sc_rd_count = size; 595 596 DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60), 597 ("%s: sc=%p size=%u, err=%d\n", __func__, 598 sc, size, sc->sc_rd_err)); 599 600 #ifdef USTIR_DEBUG 601 if (ustirdebug >= 20 && size > 0) 602 ustir_dumpdata(sc->sc_rd_buf, size, __func__); 603 #endif 604 605 if (!deframe_rd_ur(sc)) { 606 if (!deframe_isclear(&sc->sc_framestate) && size == 0 && 607 sc->sc_rd_expectdataticks == 0) { 608 /* 609 * Expected data, but didn't get it 610 * within expected time... 611 */ 612 DPRINTFN(5,("%s: incoming packet timeout\n", 613 __func__)); 614 deframe_clear(&sc->sc_framestate); 615 } else if (size > 0) { 616 /* 617 * If we also received actual data, reset the 618 * data read timeout and wake up the possibly 619 * sleeping thread... 620 */ 621 sc->sc_rd_expectdataticks = 2; 622 wakeup(&sc->sc_thread); 623 } 624 } 625 626 /* 627 * Check if incoming data has stopped, or that we cannot 628 * safely read any more data. In the case of the latter we 629 * must switch to idle so that a write will not block... 630 */ 631 if (sc->sc_direction == udir_input && 632 ((size == 0 && sc->sc_rd_expectdataticks == 0) || 633 USTIR_BLOCK_RX_DATA(sc))) { 634 DPRINTFN(8,("%s: idling on packet timeout, " 635 "complete frame, or no data\n", __func__)); 636 sc->sc_direction = udir_idle; 637 638 /* Wake up for possible output */ 639 wakeup(&sc->sc_wr_buf); 640 selnotify(&sc->sc_wr_sel, 0, 0); 641 } 642 } 643 644 Static usbd_status 645 ustir_start_read(struct ustir_softc *sc) 646 { 647 usbd_status err; 648 649 DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc, 650 sc->sc_params.maxsize)); 651 652 if (sc->sc_dying) 653 return USBD_IOERROR; 654 655 if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) { 656 /* 657 * Can't start reading just yet. Since we aren't 658 * going to start a read, have to switch direction to 659 * idle. 660 */ 661 sc->sc_direction = udir_idle; 662 return USBD_NORMAL_COMPLETION; 663 } 664 665 /* Starting a read... */ 666 sc->sc_rd_readinprogress = 1; 667 sc->sc_direction = udir_input; 668 669 if (sc->sc_rd_err) { 670 sc->sc_rd_err = 0; 671 DPRINTFN(0, ("%s: clear stall\n", __func__)); 672 usbd_clear_endpoint_stall(sc->sc_rd_pipe); 673 } 674 675 usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf, 676 sc->sc_params.maxsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, 677 ustir_rd_cb); 678 err = usbd_transfer(sc->sc_rd_xfer); 679 if (err != USBD_IN_PROGRESS) { 680 DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err)); 681 return err; 682 } 683 return USBD_NORMAL_COMPLETION; 684 } 685 686 Static int 687 ustir_activate(device_t self, enum devact act) 688 { 689 struct ustir_softc *sc = device_private(self); 690 691 switch (act) { 692 case DVACT_DEACTIVATE: 693 sc->sc_dying = 1; 694 return 0; 695 default: 696 return EOPNOTSUPP; 697 } 698 } 699 700 /* ARGSUSED */ 701 Static int 702 ustir_open(void *h, int flag, int mode, 703 struct lwp *l) 704 { 705 struct ustir_softc *sc = h; 706 int error; 707 usbd_status err; 708 709 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 710 711 err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe); 712 if (err != USBD_NORMAL_COMPLETION) { 713 error = EIO; 714 goto bad1; 715 } 716 err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe); 717 if (err != USBD_NORMAL_COMPLETION) { 718 error = EIO; 719 goto bad2; 720 } 721 error = usbd_create_xfer(sc->sc_rd_pipe, IRDA_MAX_FRAME_SIZE, 722 0, 0, &sc->sc_rd_xfer); 723 if (error) 724 goto bad3; 725 sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer); 726 727 error = usbd_create_xfer(sc->sc_wr_pipe, 728 IRDA_MAX_FRAME_SIZE + STIR_OUTPUT_HEADER_SIZE, 729 USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer); 730 if (error) 731 goto bad4; 732 sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer); 733 734 sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP); 735 sc->sc_rd_index = sc->sc_rd_count = 0; 736 sc->sc_closing = 0; 737 sc->sc_rd_readinprogress = 0; 738 sc->sc_rd_expectdataticks = 0; 739 sc->sc_ur_framelen = 0; 740 sc->sc_rd_err = 0; 741 sc->sc_wr_stalewrite = 0; 742 sc->sc_speedrec = NULL; 743 sc->sc_direction = udir_idle; 744 sc->sc_params.speed = 0; 745 sc->sc_params.ebofs = 0; 746 sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE; 747 748 deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE); 749 750 /* Increment reference for thread */ 751 sc->sc_refcnt++; 752 753 error = kthread_create(PRI_NONE, 0, NULL, ustir_thread, sc, 754 &sc->sc_thread, "%s", device_xname(sc->sc_dev)); 755 if (error) { 756 sc->sc_refcnt--; 757 goto bad5; 758 } 759 760 return 0; 761 762 bad5: 763 usbd_destroy_xfer(sc->sc_wr_xfer); 764 sc->sc_wr_xfer = NULL; 765 bad4: 766 usbd_destroy_xfer(sc->sc_rd_xfer); 767 sc->sc_rd_xfer = NULL; 768 bad3: 769 usbd_close_pipe(sc->sc_wr_pipe); 770 sc->sc_wr_pipe = NULL; 771 bad2: 772 usbd_close_pipe(sc->sc_rd_pipe); 773 sc->sc_rd_pipe = NULL; 774 bad1: 775 return error; 776 } 777 778 /* ARGSUSED */ 779 Static int 780 ustir_close(void *h, int flag, int mode, 781 struct lwp *l) 782 { 783 struct ustir_softc *sc = h; 784 785 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 786 787 sc->sc_refcnt++; 788 789 sc->sc_rd_readinprogress = 1; 790 sc->sc_closing = 1; 791 792 wakeup(&sc->sc_thread); 793 794 while (sc->sc_thread != NULL) 795 tsleep(&sc->sc_closing, PWAIT, "usircl", 0); 796 797 if (sc->sc_rd_pipe != NULL) { 798 usbd_abort_pipe(sc->sc_rd_pipe); 799 sc->sc_rd_pipe = NULL; 800 } 801 if (sc->sc_wr_pipe != NULL) { 802 usbd_abort_pipe(sc->sc_wr_pipe); 803 sc->sc_wr_pipe = NULL; 804 } 805 if (sc->sc_rd_xfer != NULL) { 806 usbd_destroy_xfer(sc->sc_rd_xfer); 807 sc->sc_rd_xfer = NULL; 808 sc->sc_rd_buf = NULL; 809 } 810 if (sc->sc_wr_xfer != NULL) { 811 usbd_destroy_xfer(sc->sc_wr_xfer); 812 sc->sc_wr_xfer = NULL; 813 sc->sc_wr_buf = NULL; 814 } 815 if (sc->sc_ur_buf != NULL) { 816 kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE); 817 sc->sc_ur_buf = NULL; 818 } 819 if (sc->sc_rd_pipe != NULL) { 820 usbd_close_pipe(sc->sc_rd_pipe); 821 sc->sc_rd_pipe = NULL; 822 } 823 if (sc->sc_wr_pipe != NULL) { 824 usbd_close_pipe(sc->sc_wr_pipe); 825 sc->sc_wr_pipe = NULL; 826 } 827 828 if (--sc->sc_refcnt < 0) 829 usb_detach_wakeupold(sc->sc_dev); 830 831 return 0; 832 } 833 834 /* ARGSUSED */ 835 Static int 836 ustir_read(void *h, struct uio *uio, int flag) 837 { 838 struct ustir_softc *sc = h; 839 int s; 840 int error; 841 u_int uframelen; 842 843 DPRINTFN(1,("%s: sc=%p\n", __func__, sc)); 844 845 if (sc->sc_dying) 846 return EIO; 847 848 #ifdef DIAGNOSTIC 849 if (sc->sc_rd_buf == NULL) 850 return EINVAL; 851 #endif 852 853 sc->sc_refcnt++; 854 855 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc)) 856 /* Possibly wake up polling thread */ 857 wakeup(&sc->sc_thread); 858 859 do { 860 s = splusb(); 861 while (sc->sc_ur_framelen == 0) { 862 DPRINTFN(5,("%s: calling tsleep()\n", __func__)); 863 error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH, 864 "usirrd", 0); 865 if (sc->sc_dying) 866 error = EIO; 867 if (error) { 868 splx(s); 869 DPRINTFN(0, ("%s: tsleep() = %d\n", 870 __func__, error)); 871 goto ret; 872 } 873 } 874 splx(s); 875 876 uframelen = sc->sc_ur_framelen; 877 DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n", 878 __func__, sc, uframelen, sc->sc_ur_buf[0])); 879 if (uframelen > uio->uio_resid) 880 error = EINVAL; 881 else 882 error = uiomove(sc->sc_ur_buf, uframelen, uio); 883 sc->sc_ur_framelen = 0; 884 885 if (!deframe_rd_ur(sc) && uframelen > 0) { 886 /* 887 * Need to wait for another read to obtain a 888 * complete frame... If we also obtained 889 * actual data, wake up the possibly sleeping 890 * thread immediately... 891 */ 892 wakeup(&sc->sc_thread); 893 } 894 } while (uframelen == 0); 895 896 DPRINTFN(1,("%s: return %d\n", __func__, error)); 897 898 ret: 899 if (--sc->sc_refcnt < 0) 900 usb_detach_wakeupold(sc->sc_dev); 901 return error; 902 } 903 904 /* ARGSUSED */ 905 Static int 906 ustir_write(void *h, struct uio *uio, int flag) 907 { 908 struct ustir_softc *sc = h; 909 usbd_status err; 910 uint32_t wrlen; 911 int error, sirlength; 912 uint8_t *wrbuf; 913 int s; 914 915 DPRINTFN(1,("%s: sc=%p\n", __func__, sc)); 916 917 if (sc->sc_dying) 918 return EIO; 919 920 #ifdef DIAGNOSTIC 921 if (sc->sc_wr_buf == NULL) 922 return EINVAL; 923 #endif 924 925 wrlen = uio->uio_resid; 926 if (wrlen > sc->sc_params.maxsize) 927 return EINVAL; 928 929 sc->sc_refcnt++; 930 931 if (!USTIR_BLOCK_RX_DATA(sc)) { 932 /* 933 * If reads are not blocked, determine what action we 934 * should potentially take... 935 */ 936 if (sc->sc_direction == udir_output) { 937 /* 938 * If the last operation was an output, wait for the 939 * polling thread to check for incoming data. 940 */ 941 sc->sc_wr_stalewrite = 1; 942 wakeup(&sc->sc_thread); 943 } else if (!sc->sc_rd_readinprogress && 944 (sc->sc_direction == udir_idle || 945 sc->sc_direction == udir_input)) { 946 /* If idle, check for input before outputting */ 947 ustir_start_read(sc); 948 } 949 } 950 951 s = splusb(); 952 while (sc->sc_wr_stalewrite || 953 (sc->sc_direction != udir_output && 954 sc->sc_direction != udir_idle)) { 955 DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, " 956 "calling tsleep()\n", __func__, 957 sc, sc->sc_wr_stalewrite, sc->sc_direction)); 958 error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH, 959 "usirwr", 0); 960 if (sc->sc_dying) 961 error = EIO; 962 if (error) { 963 splx(s); 964 DPRINTFN(0, ("%s: tsleep() = %d\n", __func__, 965 error)); 966 goto ret; 967 } 968 } 969 splx(s); 970 971 wrbuf = sc->sc_wr_buf; 972 973 /* Build header */ 974 wrbuf[0] = STIR_OUTPUT_HEADER_BYTE0; 975 wrbuf[1] = STIR_OUTPUT_HEADER_BYTE1; 976 977 sirlength = irda_sir_frame(&wrbuf[STIR_OUTPUT_HEADER_SIZE], 978 MAX_USTIR_OUTPUT_FRAME - 979 STIR_OUTPUT_HEADER_SIZE, 980 uio, sc->sc_params.ebofs); 981 if (sirlength < 0) { 982 error = -sirlength; 983 } else { 984 uint32_t btlen; 985 986 DPRINTFN(1, ("%s: transfer %u bytes\n", __func__, 987 (unsigned int)wrlen)); 988 989 wrbuf[2] = sirlength & 0xff; 990 wrbuf[3] = (sirlength >> 8) & 0xff; 991 992 btlen = STIR_OUTPUT_HEADER_SIZE + sirlength; 993 994 sc->sc_direction = udir_output; 995 996 #ifdef USTIR_DEBUG 997 if (ustirdebug >= 20) 998 ustir_dumpdata(wrbuf, btlen, __func__); 999 #endif 1000 1001 err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe, 1002 USBD_FORCE_SHORT_XFER, USTIR_WR_TIMEOUT, wrbuf, &btlen); 1003 DPRINTFN(2, ("%s: err=%d\n", __func__, err)); 1004 if (err != USBD_NORMAL_COMPLETION) { 1005 if (err == USBD_INTERRUPTED) 1006 error = EINTR; 1007 else if (err == USBD_TIMEOUT) 1008 error = ETIMEDOUT; 1009 else 1010 error = EIO; 1011 } else { 1012 error = 0; 1013 } 1014 } 1015 1016 ret: 1017 if (--sc->sc_refcnt < 0) 1018 usb_detach_wakeupold(sc->sc_dev); 1019 1020 DPRINTFN(1,("%s: sc=%p done\n", __func__, sc)); 1021 return error; 1022 } 1023 1024 Static int 1025 ustir_poll(void *h, int events, struct lwp *l) 1026 { 1027 struct ustir_softc *sc = h; 1028 int revents = 0; 1029 1030 DPRINTFN(1,("%s: sc=%p\n", __func__, sc)); 1031 1032 if (events & (POLLOUT | POLLWRNORM)) { 1033 if (sc->sc_direction != udir_input) { 1034 revents |= events & (POLLOUT | POLLWRNORM); 1035 } else { 1036 DPRINTFN(2,("%s: recording write select\n", 1037 __func__)); 1038 selrecord(l, &sc->sc_wr_sel); 1039 } 1040 } 1041 1042 if (events & (POLLIN | POLLRDNORM)) { 1043 if (sc->sc_ur_framelen != 0) { 1044 DPRINTFN(2,("%s: have data\n", __func__)); 1045 revents |= events & (POLLIN | POLLRDNORM); 1046 } else { 1047 DPRINTFN(2,("%s: recording read select\n", 1048 __func__)); 1049 selrecord(l, &sc->sc_rd_sel); 1050 } 1051 } 1052 1053 return revents; 1054 } 1055 1056 static void 1057 filt_ustirrdetach(struct knote *kn) 1058 { 1059 struct ustir_softc *sc = kn->kn_hook; 1060 int s; 1061 1062 s = splusb(); 1063 SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext); 1064 splx(s); 1065 } 1066 1067 /* ARGSUSED */ 1068 static int 1069 filt_ustirread(struct knote *kn, long hint) 1070 { 1071 struct ustir_softc *sc = kn->kn_hook; 1072 1073 kn->kn_data = sc->sc_ur_framelen; 1074 return kn->kn_data > 0; 1075 } 1076 1077 static void 1078 filt_ustirwdetach(struct knote *kn) 1079 { 1080 struct ustir_softc *sc = kn->kn_hook; 1081 int s; 1082 1083 s = splusb(); 1084 SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext); 1085 splx(s); 1086 } 1087 1088 /* ARGSUSED */ 1089 static int 1090 filt_ustirwrite(struct knote *kn, long hint) 1091 { 1092 struct ustir_softc *sc = kn->kn_hook; 1093 1094 kn->kn_data = 0; 1095 return sc->sc_direction != udir_input; 1096 } 1097 1098 static const struct filterops ustirread_filtops = { 1099 .f_isfd = 1, 1100 .f_attach = NULL, 1101 .f_detach = filt_ustirrdetach, 1102 .f_event = filt_ustirread, 1103 }; 1104 1105 static const struct filterops ustirwrite_filtops = { 1106 .f_isfd = 1, 1107 .f_attach = NULL, 1108 .f_detach = filt_ustirwdetach, 1109 .f_event = filt_ustirwrite, 1110 }; 1111 1112 Static int 1113 ustir_kqfilter(void *h, struct knote *kn) 1114 { 1115 struct ustir_softc *sc = h; 1116 struct klist *klist; 1117 int s; 1118 1119 switch (kn->kn_filter) { 1120 case EVFILT_READ: 1121 klist = &sc->sc_rd_sel.sel_klist; 1122 kn->kn_fop = &ustirread_filtops; 1123 break; 1124 case EVFILT_WRITE: 1125 klist = &sc->sc_wr_sel.sel_klist; 1126 kn->kn_fop = &ustirwrite_filtops; 1127 break; 1128 default: 1129 return EINVAL; 1130 } 1131 1132 kn->kn_hook = sc; 1133 1134 s = splusb(); 1135 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 1136 splx(s); 1137 1138 return 0; 1139 } 1140 1141 #ifdef USTIR_DEBUG_IOCTLS 1142 Static int ustir_ioctl(void *h, u_long cmd, void *addr, int flag, struct lwp *l) 1143 { 1144 struct ustir_softc *sc = h; 1145 int error; 1146 unsigned int regnum; 1147 usbd_status err; 1148 uint8_t regdata; 1149 1150 if (sc->sc_dying) 1151 return EIO; 1152 1153 sc->sc_refcnt++; 1154 1155 error = 0; 1156 switch (cmd) { 1157 case USTIR_READ_REGISTER: 1158 regnum = *(unsigned int *)addr; 1159 1160 if (regnum > STIR_MAX_REG) { 1161 error = EINVAL; 1162 break; 1163 } 1164 1165 err = ustir_read_reg(sc, regnum, ®data); 1166 1167 DPRINTFN(10, ("%s: regget(%u) = 0x%x\n", __func__, 1168 regnum, (unsigned int)regdata)); 1169 1170 *(unsigned int *)addr = regdata; 1171 if (err != USBD_NORMAL_COMPLETION) { 1172 printf("%s: register read failed: %s\n", 1173 device_xname(sc->sc_dev), 1174 usbd_errstr(err)); 1175 error = EIO; 1176 } 1177 break; 1178 1179 case USTIR_WRITE_REGISTER: 1180 regnum = *(unsigned int *)addr; 1181 regdata = (regnum >> 8) & 0xff; 1182 regnum = regnum & 0xff; 1183 1184 if (regnum > STIR_MAX_REG) { 1185 error = EINVAL; 1186 break; 1187 } 1188 1189 DPRINTFN(10, ("%s: regset(%u, 0x%x)\n", __func__, 1190 regnum, (unsigned int)regdata)); 1191 1192 err = ustir_write_reg(sc, regnum, regdata); 1193 if (err != USBD_NORMAL_COMPLETION) { 1194 printf("%s: register write failed: %s\n", 1195 device_xname(sc->sc_dev), 1196 usbd_errstr(err)); 1197 error = EIO; 1198 } 1199 break; 1200 1201 case USTIR_DEBUG_LEVEL: 1202 #ifdef USTIR_DEBUG 1203 ustirdebug = *(int *)addr; 1204 #endif 1205 break; 1206 1207 case USTIR_DEBUG_OPERATION: 1208 break; 1209 1210 default: 1211 error = EINVAL; 1212 break; 1213 } 1214 1215 if (--sc->sc_refcnt < 0) 1216 usb_detach_wakeupold(sc->sc_dev); 1217 1218 return error; 1219 } 1220 #endif 1221 1222 Static int 1223 ustir_set_params(void *h, struct irda_params *p) 1224 { 1225 struct ustir_softc *sc = h; 1226 struct ustir_speedrec const *speedblk; 1227 int i; 1228 1229 DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__, 1230 sc, p->speed, p->ebofs, p->maxsize)); 1231 1232 if (sc->sc_dying) 1233 return EIO; 1234 1235 speedblk = NULL; 1236 1237 if (sc->sc_speedrec == NULL || p->speed != sc->sc_speedrec->speed) { 1238 /* find speed */ 1239 for (i = 0; i < USTIR_NSPEEDS; i++) { 1240 if (ustir_speeds[i].speed == p->speed) { 1241 speedblk = &ustir_speeds[i]; 1242 goto found2; 1243 } 1244 } 1245 /* no good value found */ 1246 return EINVAL; 1247 found2: 1248 ; 1249 } 1250 if (p->maxsize != sc->sc_params.maxsize) { 1251 if (p->maxsize > IRDA_MAX_FRAME_SIZE) 1252 return EINVAL; 1253 sc->sc_params.maxsize = p->maxsize; 1254 } 1255 1256 sc->sc_params = *p; 1257 1258 if (speedblk != NULL) { 1259 usbd_status err; 1260 uint8_t regmode; 1261 uint8_t regbrate; 1262 1263 sc->sc_speedrec = speedblk; 1264 1265 regmode = STIR_BRMODE_MODEREG(speedblk->config); 1266 regbrate = STIR_BRMODE_BRATEREG(speedblk->config); 1267 1268 /* 1269 * FFSPRST must be set to enable the FIFO. 1270 */ 1271 regmode |= STIR_RMODE_FFSPRST; 1272 1273 DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__, 1274 (unsigned int)regbrate)); 1275 err = ustir_write_reg(sc, STIR_REG_BRATE, regbrate); 1276 if (err == USBD_NORMAL_COMPLETION) { 1277 DPRINTFN(10, ("%s: setting MODE = %x\n", __func__, 1278 (unsigned int)regmode)); 1279 err = ustir_write_reg(sc, STIR_REG_MODE, regmode); 1280 } 1281 if (err != USBD_NORMAL_COMPLETION) { 1282 DPRINTFN(10, ("%s: error setting register: %s\n", 1283 __func__, usbd_errstr(err))); 1284 return EIO; 1285 } 1286 } 1287 1288 return 0; 1289 } 1290 1291 Static int 1292 ustir_get_speeds(void *h, int *speeds) 1293 { 1294 struct ustir_softc *sc = h; 1295 1296 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 1297 1298 if (sc->sc_dying) 1299 return EIO; 1300 1301 /* All these speeds are supported */ 1302 *speeds = IRDA_SPEED_4000000 | 1303 IRDA_SPEED_1152000 | 1304 IRDA_SPEED_576000 | 1305 IRDA_SPEED_115200 | 1306 IRDA_SPEED_57600 | 1307 IRDA_SPEED_38400 | 1308 IRDA_SPEED_19200 | 1309 IRDA_SPEED_9600 | 1310 IRDA_SPEED_2400; 1311 1312 return 0; 1313 } 1314 1315 Static int 1316 ustir_get_turnarounds(void *h, int *turnarounds) 1317 { 1318 struct ustir_softc *sc = h; 1319 1320 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc)); 1321 1322 if (sc->sc_dying) 1323 return EIO; 1324 1325 /* 1326 * Documentation is on the light side with respect to 1327 * turnaround time for this device. 1328 */ 1329 *turnarounds = IRDA_TURNT_10000; 1330 1331 return 0; 1332 } 1333