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