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