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