Home | History | Annotate | Line # | Download | only in usb
uirda.c revision 1.25
      1  1.25        is /*	$NetBSD: uirda.c,v 1.25 2007/05/11 13:12:14 is Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.6  augustss  * by Lennart Augustsson (lennart (at) augustsson.net).
      9   1.1  augustss  *
     10   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.1  augustss  * modification, are permitted provided that the following conditions
     12   1.1  augustss  * are met:
     13   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19   1.1  augustss  *    must display the following acknowledgement:
     20   1.1  augustss  *        This product includes software developed by the NetBSD
     21   1.1  augustss  *        Foundation, Inc. and its contributors.
     22   1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  augustss  *    contributors may be used to endorse or promote products derived
     24   1.1  augustss  *    from this software without specific prior written permission.
     25   1.1  augustss  *
     26   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  augustss  */
     38   1.1  augustss 
     39   1.1  augustss #include <sys/cdefs.h>
     40  1.25        is __KERNEL_RCSID(0, "$NetBSD: uirda.c,v 1.25 2007/05/11 13:12:14 is Exp $");
     41   1.1  augustss 
     42   1.1  augustss #include <sys/param.h>
     43   1.1  augustss #include <sys/systm.h>
     44   1.1  augustss #include <sys/kernel.h>
     45   1.1  augustss #include <sys/device.h>
     46   1.1  augustss #include <sys/lock.h>
     47   1.1  augustss #include <sys/ioctl.h>
     48   1.1  augustss #include <sys/conf.h>
     49   1.1  augustss #include <sys/file.h>
     50   1.1  augustss #include <sys/poll.h>
     51   1.1  augustss #include <sys/select.h>
     52   1.1  augustss #include <sys/proc.h>
     53   1.1  augustss 
     54   1.1  augustss #include <dev/usb/usb.h>
     55   1.1  augustss #include <dev/usb/usbdi.h>
     56   1.1  augustss #include <dev/usb/usbdi_util.h>
     57   1.1  augustss #include <dev/usb/usbdevs.h>
     58   1.1  augustss 
     59   1.1  augustss #include <dev/ir/ir.h>
     60   1.1  augustss #include <dev/ir/irdaio.h>
     61   1.1  augustss #include <dev/ir/irframevar.h>
     62   1.1  augustss 
     63  1.25        is #include <dev/usb/uirdavar.h>
     64  1.25        is 
     65  1.12  augustss #ifdef UIRDA_DEBUG
     66   1.1  augustss #define DPRINTF(x)	if (uirdadebug) logprintf x
     67   1.1  augustss #define DPRINTFN(n,x)	if (uirdadebug>(n)) logprintf x
     68   1.7  augustss int	uirdadebug = 0;
     69   1.1  augustss #else
     70   1.1  augustss #define DPRINTF(x)
     71   1.1  augustss #define DPRINTFN(n,x)
     72   1.1  augustss #endif
     73   1.1  augustss 
     74   1.1  augustss 
     75   1.1  augustss /* Class specific requests */
     76   1.1  augustss #define UR_IRDA_RECEIVING		0x01	/* Receive in progress? */
     77   1.1  augustss #define UR_IRDA_CHECK_MEDIA_BUSY	0x03
     78   1.1  augustss #define UR_IRDA_SET_RATE_SNIFF		0x04	/* opt */
     79   1.1  augustss #define UR_IRDA_SET_UNICAST_LIST	0x05	/* opt */
     80   1.1  augustss #define UR_IRDA_GET_DESC		0x06
     81   1.1  augustss 
     82   1.1  augustss #define UIRDA_NEBOFS 8
     83   1.1  augustss static struct {
     84   1.1  augustss 	int count;
     85   1.1  augustss 	int mask;
     86   1.1  augustss 	int header;
     87   1.1  augustss } uirda_ebofs[UIRDA_NEBOFS] = {
     88   1.1  augustss 	{ 0, UI_EB_0, UIRDA_EB_0 },
     89   1.1  augustss 	{ 1, UI_EB_1, UIRDA_EB_1 },
     90   1.1  augustss 	{ 2, UI_EB_2, UIRDA_EB_2 },
     91   1.1  augustss 	{ 3, UI_EB_3, UIRDA_EB_3 },
     92   1.1  augustss 	{ 6, UI_EB_6, UIRDA_EB_6 },
     93   1.1  augustss 	{ 12, UI_EB_12, UIRDA_EB_12 },
     94   1.1  augustss 	{ 24, UI_EB_24, UIRDA_EB_24 },
     95   1.1  augustss 	{ 48, UI_EB_48, UIRDA_EB_48 }
     96   1.1  augustss };
     97   1.1  augustss 
     98   1.1  augustss #define UIRDA_NSPEEDS 9
     99   1.1  augustss static struct {
    100   1.1  augustss 	int speed;
    101   1.1  augustss 	int mask;
    102   1.1  augustss 	int header;
    103   1.1  augustss } uirda_speeds[UIRDA_NSPEEDS] = {
    104   1.1  augustss 	{ 4000000, UI_BR_4000000, UIRDA_4000000 },
    105   1.1  augustss 	{ 1152000, UI_BR_1152000, UIRDA_1152000 },
    106   1.1  augustss 	{ 576000, UI_BR_576000, UIRDA_576000 },
    107   1.1  augustss 	{ 115200, UI_BR_115200, UIRDA_115200 },
    108   1.1  augustss 	{ 57600, UI_BR_57600, UIRDA_57600 },
    109   1.1  augustss 	{ 38400, UI_BR_38400, UIRDA_38400 },
    110   1.1  augustss 	{ 19200, UI_BR_19200, UIRDA_19200 },
    111   1.1  augustss 	{ 9600, UI_BR_9600, UIRDA_9600 },
    112   1.1  augustss 	{ 2400, UI_BR_2400, UIRDA_2400 },
    113   1.1  augustss };
    114   1.1  augustss 
    115   1.1  augustss 
    116   1.1  augustss 
    117  1.20  christos int uirda_open(void *h, int flag, int mode, struct lwp *l);
    118  1.20  christos int uirda_close(void *h, int flag, int mode, struct lwp *l);
    119   1.1  augustss int uirda_read(void *h, struct uio *uio, int flag);
    120   1.1  augustss int uirda_write(void *h, struct uio *uio, int flag);
    121   1.1  augustss int uirda_set_params(void *h, struct irda_params *params);
    122   1.1  augustss int uirda_get_speeds(void *h, int *speeds);
    123   1.1  augustss int uirda_get_turnarounds(void *h, int *times);
    124  1.20  christos int uirda_poll(void *h, int events, struct lwp *l);
    125  1.13  jdolecek int uirda_kqfilter(void *h, struct knote *kn);
    126   1.1  augustss 
    127   1.1  augustss struct irframe_methods uirda_methods = {
    128  1.11  augustss 	uirda_open, uirda_close, uirda_read, uirda_write, uirda_poll,
    129  1.13  jdolecek 	uirda_kqfilter, uirda_set_params, uirda_get_speeds,
    130  1.13  jdolecek 	uirda_get_turnarounds
    131   1.1  augustss };
    132   1.1  augustss 
    133   1.1  augustss void uirda_rd_cb(usbd_xfer_handle xfer,	usbd_private_handle priv,
    134   1.1  augustss 		 usbd_status status);
    135   1.1  augustss usbd_status uirda_start_read(struct uirda_softc *sc);
    136   1.1  augustss 
    137  1.11  augustss /*
    138   1.1  augustss  * These devices don't quite follow the spec.  Speed changing is broken
    139   1.1  augustss  * and they don't handle windows.
    140   1.1  augustss  * But we change speed in a safe way, and don't use windows now.
    141   1.1  augustss  * Some devices also seem to have an interrupt pipe that can be ignored.
    142   1.1  augustss  *
    143   1.1  augustss  * Table information taken from Linux driver.
    144   1.1  augustss  */
    145   1.1  augustss Static const struct usb_devno uirda_devs[] = {
    146   1.1  augustss 	{ USB_VENDOR_ACTISYS, USB_PRODUCT_ACTISYS_IR2000U },
    147   1.1  augustss 	{ USB_VENDOR_EXTENDED, USB_PRODUCT_EXTENDED_XTNDACCESS },
    148   1.1  augustss 	{ USB_VENDOR_KAWATSU, USB_PRODUCT_KAWATSU_KC180 },
    149   1.1  augustss };
    150   1.1  augustss #define uirda_lookup(v, p) (usb_lookup(uirda_devs, v, p))
    151   1.1  augustss 
    152   1.1  augustss USB_DECLARE_DRIVER(uirda);
    153   1.1  augustss 
    154   1.1  augustss USB_MATCH(uirda)
    155   1.1  augustss {
    156  1.24  drochner 	USB_IFMATCH_START(uirda, uaa);
    157   1.1  augustss 
    158   1.1  augustss 	DPRINTFN(50,("uirda_match\n"));
    159   1.1  augustss 
    160   1.1  augustss 	if (uirda_lookup(uaa->vendor, uaa->product) != NULL)
    161   1.1  augustss 		return (UMATCH_VENDOR_PRODUCT);
    162   1.1  augustss 
    163  1.24  drochner 	if (uaa->class == UICLASS_APPL_SPEC &&
    164  1.24  drochner 	    uaa->subclass == UISUBCLASS_IRDA &&
    165  1.24  drochner 	    uaa->proto == UIPROTO_IRDA)
    166   1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    167   1.1  augustss 	return (UMATCH_NONE);
    168   1.1  augustss }
    169   1.1  augustss 
    170   1.1  augustss USB_ATTACH(uirda)
    171   1.1  augustss {
    172  1.24  drochner 	USB_IFATTACH_START(uirda, sc, uaa);
    173   1.1  augustss 	usbd_device_handle	dev = uaa->device;
    174   1.1  augustss 	usbd_interface_handle	iface = uaa->iface;
    175  1.19  augustss 	char			*devinfop;
    176   1.1  augustss 	usb_endpoint_descriptor_t *ed;
    177   1.1  augustss 	usbd_status		err;
    178   1.1  augustss 	u_int8_t		epcount;
    179   1.4  augustss 	u_int			specrev;
    180   1.1  augustss 	int			i;
    181   1.1  augustss 	struct ir_attach_args	ia;
    182   1.1  augustss 
    183   1.1  augustss 	DPRINTFN(10,("uirda_attach: sc=%p\n", sc));
    184   1.1  augustss 
    185  1.19  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    186   1.1  augustss 	USB_ATTACH_SETUP;
    187  1.19  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    188  1.19  augustss 	usbd_devinfo_free(devinfop);
    189   1.1  augustss 
    190   1.1  augustss 	sc->sc_udev = dev;
    191   1.1  augustss 	sc->sc_iface = iface;
    192   1.1  augustss 
    193  1.25        is 	if (sc->sc_hdszi == 0)
    194  1.25        is 		sc->sc_hdszi = UIRDA_INPUT_HEADER_SIZE;
    195  1.25        is 
    196   1.1  augustss 	epcount = 0;
    197   1.1  augustss 	(void)usbd_endpoint_count(iface, &epcount);
    198   1.1  augustss 
    199   1.1  augustss 	sc->sc_rd_addr = -1;
    200   1.1  augustss 	sc->sc_wr_addr = -1;
    201   1.1  augustss 	for (i = 0; i < epcount; i++) {
    202   1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    203   1.1  augustss 		if (ed == NULL) {
    204   1.1  augustss 			printf("%s: couldn't get ep %d\n",
    205   1.1  augustss 			    USBDEVNAME(sc->sc_dev), i);
    206   1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    207   1.1  augustss 		}
    208   1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    209   1.1  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    210   1.1  augustss 			sc->sc_rd_addr = ed->bEndpointAddress;
    211   1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    212   1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    213   1.1  augustss 			sc->sc_wr_addr = ed->bEndpointAddress;
    214   1.1  augustss 		}
    215   1.1  augustss 	}
    216   1.1  augustss 	if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
    217   1.1  augustss 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    218   1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    219   1.1  augustss 	}
    220   1.1  augustss 
    221  1.25        is 	if (sc->sc_loadfw(sc) != 0) {
    222  1.25        is 		USB_ATTACH_ERROR_RETURN;
    223  1.25        is 	}
    224  1.25        is 
    225   1.1  augustss 	/* Get the IrDA descriptor */
    226  1.25        is 	err = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
    227  1.25        is 		USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
    228  1.25        is 	printf("error %d reading class desc\n", err);
    229  1.25        is 	if (err) {
    230  1.25        is 		err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
    231   1.1  augustss 		  USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
    232  1.25        is 	}
    233  1.25        is 	printf("error %d reading desc\n", err);
    234   1.1  augustss 	if (err) {
    235   1.8  augustss 		/* maybe it's embedded in the config desc? */
    236  1.23  drochner 		usbd_desc_iter_t iter;
    237  1.23  drochner 		const usb_descriptor_t *d;
    238  1.23  drochner 		usb_desc_iter_init(sc->sc_udev, &iter);
    239  1.23  drochner 		for (;;) {
    240  1.23  drochner 			d = usb_desc_iter_next(&iter);
    241  1.23  drochner 			if (!d || d->bDescriptorType == UDESC_IRDA)
    242  1.23  drochner 				break;
    243  1.23  drochner 		}
    244   1.1  augustss 		if (d == NULL) {
    245   1.1  augustss 			printf("%s: Cannot get IrDA descriptor\n",
    246   1.1  augustss 			       USBDEVNAME(sc->sc_dev));
    247   1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    248   1.1  augustss 		}
    249   1.1  augustss 		memcpy(&sc->sc_irdadesc, d, USB_IRDA_DESCRIPTOR_SIZE);
    250   1.1  augustss 	}
    251  1.25        is 	DPRINTF(("uirda_attach: bDescriptorSize %d bDescriptorType 0x%x "
    252  1.25        is 		 "bmDataSize=0x%02x bmWindowSize=0x%02x "
    253   1.8  augustss 		 "bmMinTurnaroundTime=0x%02x wBaudRate=0x%04x "
    254   1.8  augustss 		 "bmAdditionalBOFs=0x%02x bIrdaSniff=%d bMaxUnicastList=%d\n",
    255  1.25        is 		 sc->sc_irdadesc.bLength,
    256  1.25        is 		 sc->sc_irdadesc.bDescriptorType,
    257   1.4  augustss 		 sc->sc_irdadesc.bmDataSize,
    258   1.4  augustss 		 sc->sc_irdadesc.bmWindowSize,
    259   1.4  augustss 		 sc->sc_irdadesc.bmMinTurnaroundTime,
    260   1.4  augustss 		 UGETW(sc->sc_irdadesc.wBaudRate),
    261   1.4  augustss 		 sc->sc_irdadesc.bmAdditionalBOFs,
    262   1.4  augustss 		 sc->sc_irdadesc.bIrdaSniff,
    263   1.4  augustss 		 sc->sc_irdadesc.bMaxUnicastList));
    264   1.4  augustss 
    265   1.4  augustss 	specrev = UGETW(sc->sc_irdadesc.bcdSpecRevision);
    266   1.8  augustss 	printf("%s: USB-IrDA protocol version %x.%02x\n",
    267   1.8  augustss 	       USBDEVNAME(sc->sc_dev), specrev >> 8, specrev & 0xff);
    268   1.1  augustss 
    269   1.1  augustss 	DPRINTFN(10, ("uirda_attach: %p\n", sc->sc_udev));
    270   1.1  augustss 
    271   1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    272   1.1  augustss 			   USBDEV(sc->sc_dev));
    273   1.3  augustss 
    274   1.3  augustss 	lockinit(&sc->sc_wr_buf_lk, PZERO, "iirwrl", 0, 0);
    275   1.3  augustss 	lockinit(&sc->sc_rd_buf_lk, PZERO, "uirrdl", 0, 0);
    276   1.1  augustss 
    277   1.1  augustss 	ia.ia_type = IR_TYPE_IRFRAME;
    278  1.25        is 	ia.ia_methods = sc->sc_irm ? sc->sc_irm : &uirda_methods;
    279   1.1  augustss 	ia.ia_handle = sc;
    280   1.1  augustss 
    281   1.1  augustss 	sc->sc_child = config_found(self, &ia, ir_print);
    282   1.1  augustss 
    283   1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    284   1.1  augustss }
    285   1.1  augustss 
    286   1.1  augustss USB_DETACH(uirda)
    287   1.1  augustss {
    288   1.1  augustss 	USB_DETACH_START(uirda, sc);
    289   1.1  augustss 	int s;
    290   1.1  augustss 	int rv = 0;
    291   1.1  augustss 
    292   1.1  augustss 	DPRINTF(("uirda_detach: sc=%p flags=%d\n", sc, flags));
    293   1.1  augustss 
    294   1.1  augustss 	sc->sc_dying = 1;
    295   1.1  augustss 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    296   1.1  augustss 	if (sc->sc_rd_pipe != NULL) {
    297   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    298   1.1  augustss 		usbd_close_pipe(sc->sc_rd_pipe);
    299   1.1  augustss 		sc->sc_rd_pipe = NULL;
    300   1.1  augustss 	}
    301   1.1  augustss 	if (sc->sc_wr_pipe != NULL) {
    302   1.1  augustss 		usbd_abort_pipe(sc->sc_wr_pipe);
    303   1.1  augustss 		usbd_close_pipe(sc->sc_wr_pipe);
    304   1.1  augustss 		sc->sc_wr_pipe = NULL;
    305   1.1  augustss 	}
    306   1.1  augustss 	wakeup(&sc->sc_rd_count);
    307   1.1  augustss 
    308   1.1  augustss 	s = splusb();
    309   1.1  augustss 	if (--sc->sc_refcnt >= 0) {
    310   1.1  augustss 		/* Wait for processes to go away. */
    311   1.1  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    312   1.1  augustss 	}
    313   1.1  augustss 	splx(s);
    314   1.1  augustss 
    315   1.1  augustss 	if (sc->sc_child != NULL) {
    316   1.1  augustss 		rv = config_detach(sc->sc_child, flags);
    317   1.1  augustss 		sc->sc_child = NULL;
    318   1.1  augustss 	}
    319   1.1  augustss 
    320   1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    321   1.1  augustss 			   USBDEV(sc->sc_dev));
    322   1.1  augustss 
    323   1.1  augustss 	return (rv);
    324   1.1  augustss }
    325   1.1  augustss 
    326   1.1  augustss int
    327   1.1  augustss uirda_activate(device_ptr_t self, enum devact act)
    328   1.1  augustss {
    329   1.1  augustss 	struct uirda_softc *sc = (struct uirda_softc *)self;
    330   1.1  augustss 	int error = 0;
    331   1.1  augustss 
    332   1.1  augustss 	switch (act) {
    333   1.1  augustss 	case DVACT_ACTIVATE:
    334   1.1  augustss 		return (EOPNOTSUPP);
    335   1.1  augustss 		break;
    336   1.1  augustss 
    337   1.1  augustss 	case DVACT_DEACTIVATE:
    338   1.1  augustss 		sc->sc_dying = 1;
    339   1.1  augustss 		if (sc->sc_child != NULL)
    340   1.1  augustss 			error = config_deactivate(sc->sc_child);
    341   1.1  augustss 		break;
    342   1.1  augustss 	}
    343   1.1  augustss 	return (error);
    344   1.1  augustss }
    345   1.1  augustss 
    346   1.1  augustss int
    347  1.22  christos uirda_open(void *h, int flag, int mode,
    348  1.22  christos     struct lwp *l)
    349   1.1  augustss {
    350   1.1  augustss 	struct uirda_softc *sc = h;
    351   1.1  augustss 	int error;
    352   1.1  augustss 	usbd_status err;
    353   1.1  augustss 
    354  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    355   1.1  augustss 
    356   1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
    357   1.1  augustss 	if (err) {
    358   1.1  augustss 		error = EIO;
    359   1.1  augustss 		goto bad1;
    360   1.1  augustss 	}
    361   1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
    362   1.1  augustss 	if (err) {
    363   1.1  augustss 		error = EIO;
    364   1.1  augustss 		goto bad2;
    365   1.1  augustss 	}
    366   1.1  augustss 	sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
    367   1.1  augustss 	if (sc->sc_rd_xfer == NULL) {
    368   1.1  augustss 		error = ENOMEM;
    369   1.1  augustss 		goto bad3;
    370   1.1  augustss 	}
    371   1.1  augustss 	sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
    372   1.1  augustss 	if (sc->sc_wr_xfer == NULL) {
    373   1.1  augustss 		error = ENOMEM;
    374   1.1  augustss 		goto bad4;
    375   1.1  augustss 	}
    376   1.5  augustss 	sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
    377  1.25        is 			    IRDA_MAX_FRAME_SIZE + sc->sc_hdszi);
    378   1.5  augustss 	if (sc->sc_rd_buf == NULL) {
    379   1.5  augustss 		error = ENOMEM;
    380   1.5  augustss 		goto bad5;
    381   1.5  augustss 	}
    382   1.5  augustss 	sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
    383  1.25        is 			    IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE +
    384  1.25        is 				2 + 1 /* worst case ST-UIRDA */);
    385   1.5  augustss 	if (sc->sc_wr_buf == NULL) {
    386   1.5  augustss 		error = ENOMEM;
    387   1.5  augustss 		goto bad5;
    388   1.5  augustss 	}
    389   1.2  augustss 	sc->sc_rd_count = 0;
    390   1.4  augustss 	sc->sc_rd_err = 0;
    391   1.2  augustss 	sc->sc_params.speed = 0;
    392   1.2  augustss 	sc->sc_params.ebofs = 0;
    393   1.6  augustss 	sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
    394   1.6  augustss 	sc->sc_wr_hdr = -1;
    395   1.6  augustss 
    396   1.6  augustss 	err = uirda_start_read(sc);
    397   1.6  augustss 	/* XXX check err */
    398   1.2  augustss 
    399   1.1  augustss 	return (0);
    400   1.1  augustss 
    401   1.5  augustss bad5:
    402   1.5  augustss 	usbd_free_xfer(sc->sc_wr_xfer);
    403   1.5  augustss 	sc->sc_wr_xfer = NULL;
    404   1.1  augustss bad4:
    405   1.1  augustss 	usbd_free_xfer(sc->sc_rd_xfer);
    406   1.1  augustss 	sc->sc_rd_xfer = NULL;
    407   1.1  augustss bad3:
    408   1.1  augustss 	usbd_close_pipe(sc->sc_wr_pipe);
    409   1.1  augustss 	sc->sc_wr_pipe = NULL;
    410   1.1  augustss bad2:
    411   1.1  augustss 	usbd_close_pipe(sc->sc_rd_pipe);
    412   1.1  augustss 	sc->sc_rd_pipe = NULL;
    413   1.1  augustss bad1:
    414   1.1  augustss 	return (error);
    415   1.1  augustss }
    416   1.1  augustss 
    417   1.1  augustss int
    418  1.22  christos uirda_close(void *h, int flag, int mode,
    419  1.22  christos     struct lwp *l)
    420   1.1  augustss {
    421   1.1  augustss 	struct uirda_softc *sc = h;
    422   1.1  augustss 
    423  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    424   1.1  augustss 
    425   1.1  augustss 	if (sc->sc_rd_pipe != NULL) {
    426   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    427   1.1  augustss 		usbd_close_pipe(sc->sc_rd_pipe);
    428   1.1  augustss 		sc->sc_rd_pipe = NULL;
    429   1.1  augustss 	}
    430   1.2  augustss 	if (sc->sc_wr_pipe != NULL) {
    431   1.2  augustss 		usbd_abort_pipe(sc->sc_wr_pipe);
    432   1.2  augustss 		usbd_close_pipe(sc->sc_wr_pipe);
    433   1.2  augustss 		sc->sc_wr_pipe = NULL;
    434   1.2  augustss 	}
    435   1.1  augustss 	if (sc->sc_rd_xfer != NULL) {
    436   1.1  augustss 		usbd_free_xfer(sc->sc_rd_xfer);
    437   1.1  augustss 		sc->sc_rd_xfer = NULL;
    438   1.2  augustss 		sc->sc_rd_buf = NULL;
    439   1.1  augustss 	}
    440   1.1  augustss 	if (sc->sc_wr_xfer != NULL) {
    441   1.1  augustss 		usbd_free_xfer(sc->sc_wr_xfer);
    442   1.1  augustss 		sc->sc_wr_xfer = NULL;
    443   1.2  augustss 		sc->sc_wr_buf = NULL;
    444   1.1  augustss 	}
    445   1.1  augustss 
    446   1.1  augustss 	return (0);
    447   1.1  augustss }
    448   1.1  augustss 
    449   1.1  augustss int
    450  1.22  christos uirda_read(void *h, struct uio *uio, int flag)
    451   1.1  augustss {
    452   1.1  augustss 	struct uirda_softc *sc = h;
    453   1.1  augustss 	usbd_status err;
    454   1.1  augustss 	int s;
    455   1.1  augustss 	int error;
    456   1.2  augustss 	u_int n;
    457   1.1  augustss 
    458  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    459   1.1  augustss 
    460   1.1  augustss 	if (sc->sc_dying)
    461   1.1  augustss 		return (EIO);
    462   1.1  augustss 
    463   1.5  augustss #ifdef DIAGNOSTIC
    464   1.1  augustss 	if (sc->sc_rd_buf == NULL)
    465   1.1  augustss 		return (EINVAL);
    466   1.5  augustss #endif
    467   1.1  augustss 
    468   1.2  augustss 	sc->sc_refcnt++;
    469   1.2  augustss 
    470   1.2  augustss 	do {
    471   1.2  augustss 		s = splusb();
    472   1.2  augustss 		while (sc->sc_rd_count == 0) {
    473   1.4  augustss 			DPRINTFN(5,("uirda_read: calling tsleep()\n"));
    474   1.2  augustss 			error = tsleep(&sc->sc_rd_count, PZERO | PCATCH,
    475   1.2  augustss 				       "uirdrd", 0);
    476   1.2  augustss 			if (sc->sc_dying)
    477   1.2  augustss 				error = EIO;
    478   1.2  augustss 			if (error) {
    479   1.2  augustss 				splx(s);
    480   1.2  augustss 				DPRINTF(("uirda_read: tsleep() = %d\n", error));
    481   1.2  augustss 				goto ret;
    482   1.2  augustss 			}
    483   1.1  augustss 		}
    484   1.2  augustss 		splx(s);
    485  1.11  augustss 
    486   1.2  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
    487  1.25        is 		n = sc->sc_rd_count - sc->sc_hdszi;
    488  1.10  augustss 		DPRINTFN(1,("%s: sc=%p n=%u, hdr=0x%02x\n", __func__,
    489   1.7  augustss 			    sc, n, sc->sc_rd_buf[0]));
    490   1.2  augustss 		if (n > uio->uio_resid)
    491   1.2  augustss 			error = EINVAL;
    492   1.2  augustss 		else
    493  1.25        is 			error = uiomove(sc->sc_rd_buf + sc->sc_hdszi, n, uio);
    494   1.2  augustss 		sc->sc_rd_count = 0;
    495   1.2  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    496  1.11  augustss 
    497   1.2  augustss 		err = uirda_start_read(sc);
    498   1.2  augustss 		/* XXX check err */
    499   1.2  augustss 
    500   1.2  augustss 	} while (n == 0);
    501   1.2  augustss 
    502   1.2  augustss 	DPRINTFN(1,("uirda_read: return %d\n", error));
    503   1.1  augustss 
    504   1.2  augustss  ret:
    505   1.1  augustss 	if (--sc->sc_refcnt < 0)
    506   1.1  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    507   1.2  augustss 	return (error);
    508   1.1  augustss }
    509   1.1  augustss 
    510   1.1  augustss int
    511  1.22  christos uirda_write(void *h, struct uio *uio, int flag)
    512   1.1  augustss {
    513   1.1  augustss 	struct uirda_softc *sc = h;
    514   1.1  augustss 	usbd_status err;
    515   1.1  augustss 	u_int32_t n;
    516   1.1  augustss 	int error = 0;
    517   1.1  augustss 
    518  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    519   1.1  augustss 
    520   1.1  augustss 	if (sc->sc_dying)
    521   1.1  augustss 		return (EIO);
    522   1.1  augustss 
    523   1.5  augustss #ifdef DIAGNOSTIC
    524   1.5  augustss 	if (sc->sc_wr_buf == NULL)
    525   1.5  augustss 		return (EINVAL);
    526   1.5  augustss #endif
    527   1.5  augustss 
    528   1.1  augustss 	n = uio->uio_resid;
    529   1.5  augustss 	if (n > sc->sc_params.maxsize)
    530   1.1  augustss 		return (EINVAL);
    531   1.1  augustss 
    532   1.1  augustss 	sc->sc_refcnt++;
    533   1.1  augustss 	lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    534   1.1  augustss 
    535   1.1  augustss 	sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
    536   1.7  augustss 	error = uiomove(sc->sc_wr_buf + UIRDA_OUTPUT_HEADER_SIZE, n, uio);
    537   1.1  augustss 	if (!error) {
    538   1.1  augustss 		DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
    539   1.1  augustss 
    540  1.25        is 		n += UIRDA_OUTPUT_HEADER_SIZE;
    541   1.1  augustss 		err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    542   1.1  augustss 			  USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    543   1.1  augustss 			  UIRDA_WR_TIMEOUT,
    544   1.1  augustss 			  sc->sc_wr_buf, &n, "uirdawr");
    545   1.1  augustss 		DPRINTFN(2, ("uirdawrite: err=%d\n", err));
    546   1.1  augustss 		if (err) {
    547   1.1  augustss 			if (err == USBD_INTERRUPTED)
    548   1.1  augustss 				error = EINTR;
    549   1.1  augustss 			else if (err == USBD_TIMEOUT)
    550   1.1  augustss 				error = ETIMEDOUT;
    551   1.1  augustss 			else
    552   1.1  augustss 				error = EIO;
    553   1.1  augustss 		}
    554   1.1  augustss 	}
    555   1.1  augustss 
    556   1.1  augustss 	lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    557   1.1  augustss 	if (--sc->sc_refcnt < 0)
    558   1.1  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    559   1.1  augustss 
    560  1.10  augustss 	DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
    561   1.1  augustss 	return (error);
    562   1.1  augustss }
    563   1.1  augustss 
    564   1.1  augustss int
    565  1.20  christos uirda_poll(void *h, int events, struct lwp *l)
    566   1.1  augustss {
    567   1.1  augustss 	struct uirda_softc *sc = h;
    568   1.1  augustss 	int revents = 0;
    569   1.1  augustss 	int s;
    570   1.1  augustss 
    571  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    572   1.1  augustss 
    573   1.1  augustss 	s = splusb();
    574   1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    575   1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    576   1.1  augustss 	if (events & (POLLIN | POLLRDNORM)) {
    577   1.1  augustss 		if (sc->sc_rd_count != 0) {
    578  1.10  augustss 			DPRINTFN(2,("%s: have data\n", __func__));
    579   1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    580   1.1  augustss 		} else {
    581  1.10  augustss 			DPRINTFN(2,("%s: recording select\n", __func__));
    582  1.20  christos 			selrecord(l, &sc->sc_rd_sel);
    583   1.1  augustss 		}
    584   1.1  augustss 	}
    585   1.1  augustss 	splx(s);
    586   1.1  augustss 
    587   1.1  augustss 	return (revents);
    588   1.1  augustss }
    589   1.1  augustss 
    590  1.13  jdolecek static void
    591  1.13  jdolecek filt_uirdardetach(struct knote *kn)
    592  1.13  jdolecek {
    593  1.13  jdolecek 	struct uirda_softc *sc = kn->kn_hook;
    594  1.13  jdolecek 	int s;
    595  1.13  jdolecek 
    596  1.13  jdolecek 	s = splusb();
    597  1.14  christos 	SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
    598  1.13  jdolecek 	splx(s);
    599  1.13  jdolecek }
    600  1.13  jdolecek 
    601  1.13  jdolecek static int
    602  1.22  christos filt_uirdaread(struct knote *kn, long hint)
    603  1.13  jdolecek {
    604  1.13  jdolecek 	struct uirda_softc *sc = kn->kn_hook;
    605  1.13  jdolecek 
    606  1.13  jdolecek 	kn->kn_data = sc->sc_rd_count;
    607  1.13  jdolecek 	return (kn->kn_data > 0);
    608  1.13  jdolecek }
    609  1.13  jdolecek 
    610  1.13  jdolecek static void
    611  1.13  jdolecek filt_uirdawdetach(struct knote *kn)
    612  1.13  jdolecek {
    613  1.13  jdolecek 	struct uirda_softc *sc = kn->kn_hook;
    614  1.13  jdolecek 	int s;
    615  1.13  jdolecek 
    616  1.13  jdolecek 	s = splusb();
    617  1.14  christos 	SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
    618  1.13  jdolecek 	splx(s);
    619  1.13  jdolecek }
    620  1.13  jdolecek 
    621  1.13  jdolecek static const struct filterops uirdaread_filtops =
    622  1.13  jdolecek 	{ 1, NULL, filt_uirdardetach, filt_uirdaread };
    623  1.13  jdolecek static const struct filterops uirdawrite_filtops =
    624  1.13  jdolecek 	{ 1, NULL, filt_uirdawdetach, filt_seltrue };
    625  1.13  jdolecek 
    626  1.13  jdolecek int
    627  1.22  christos uirda_kqfilter(void *h, struct knote *kn)
    628  1.13  jdolecek {
    629  1.13  jdolecek 	struct uirda_softc *sc = kn->kn_hook;
    630  1.13  jdolecek 	struct klist *klist;
    631  1.13  jdolecek 	int s;
    632  1.13  jdolecek 
    633  1.13  jdolecek 	switch (kn->kn_filter) {
    634  1.13  jdolecek 	case EVFILT_READ:
    635  1.14  christos 		klist = &sc->sc_rd_sel.sel_klist;
    636  1.13  jdolecek 		kn->kn_fop = &uirdaread_filtops;
    637  1.13  jdolecek 		break;
    638  1.13  jdolecek 	case EVFILT_WRITE:
    639  1.14  christos 		klist = &sc->sc_wr_sel.sel_klist;
    640  1.13  jdolecek 		kn->kn_fop = &uirdawrite_filtops;
    641  1.13  jdolecek 		break;
    642  1.13  jdolecek 	default:
    643  1.13  jdolecek 		return (1);
    644  1.13  jdolecek 	}
    645  1.13  jdolecek 
    646  1.13  jdolecek 	kn->kn_hook = sc;
    647  1.13  jdolecek 
    648  1.13  jdolecek 	s = splusb();
    649  1.13  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    650  1.13  jdolecek 	splx(s);
    651  1.13  jdolecek 
    652  1.13  jdolecek 	return (0);
    653  1.13  jdolecek }
    654  1.13  jdolecek 
    655   1.1  augustss int
    656   1.1  augustss uirda_set_params(void *h, struct irda_params *p)
    657   1.1  augustss {
    658   1.1  augustss 	struct uirda_softc *sc = h;
    659   1.1  augustss 	usbd_status err;
    660   1.1  augustss 	int i;
    661   1.1  augustss 	u_int8_t hdr;
    662   1.1  augustss 	u_int32_t n;
    663   1.1  augustss 	u_int mask;
    664   1.1  augustss 
    665  1.10  augustss 	DPRINTF(("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
    666   1.1  augustss 		 sc, p->speed, p->ebofs, p->maxsize));
    667   1.1  augustss 
    668   1.1  augustss 	if (sc->sc_dying)
    669   1.1  augustss 		return (EIO);
    670   1.1  augustss 
    671   1.1  augustss 	hdr = 0;
    672   1.1  augustss 	if (p->ebofs != sc->sc_params.ebofs) {
    673   1.1  augustss 		/* round up ebofs */
    674  1.25        is 		mask = 1 /* sc->sc_irdadesc.bmAdditionalBOFs*/;
    675  1.25        is 		DPRINTF(("u.s.p.: mask=0x%x, sc->ebofs=%d, p->ebofs=%d\n",
    676  1.25        is 			mask, sc->sc_params.ebofs, p->ebofs));
    677   1.1  augustss 		for (i = 0; i < UIRDA_NEBOFS; i++) {
    678  1.25        is 			DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
    679  1.25        is 				i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
    680   1.1  augustss 			if ((mask & uirda_ebofs[i].mask) &&
    681   1.1  augustss 			    uirda_ebofs[i].count >= p->ebofs) {
    682   1.1  augustss 				hdr = uirda_ebofs[i].header;
    683   1.1  augustss 				goto found1;
    684   1.1  augustss 			}
    685   1.1  augustss 		}
    686  1.25        is 		for (i = 0; i < UIRDA_NEBOFS; i++) {
    687  1.25        is 			DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
    688  1.25        is 				i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
    689  1.25        is 			if ((mask & uirda_ebofs[i].mask)) {
    690  1.25        is 				hdr = uirda_ebofs[i].header;
    691  1.25        is 				goto found1;
    692  1.25        is 			}
    693  1.25        is 		}
    694   1.1  augustss 		/* no good value found */
    695   1.1  augustss 		return (EINVAL);
    696   1.1  augustss 	found1:
    697   1.6  augustss 		DPRINTF(("uirda_set_params: ebofs hdr=0x%02x\n", hdr));
    698   1.1  augustss 		;
    699  1.11  augustss 
    700   1.1  augustss 	}
    701   1.6  augustss 	if (hdr != 0 || p->speed != sc->sc_params.speed) {
    702   1.1  augustss 		/* find speed */
    703   1.1  augustss 		mask = UGETW(sc->sc_irdadesc.wBaudRate);
    704   1.1  augustss 		for (i = 0; i < UIRDA_NSPEEDS; i++) {
    705   1.1  augustss 			if ((mask & uirda_speeds[i].mask) &&
    706   1.1  augustss 			    uirda_speeds[i].speed == p->speed) {
    707   1.1  augustss 				hdr |= uirda_speeds[i].header;
    708   1.1  augustss 				goto found2;
    709   1.1  augustss 			}
    710   1.1  augustss 		}
    711   1.1  augustss 		/* no good value found */
    712   1.1  augustss 		return (EINVAL);
    713   1.1  augustss 	found2:
    714   1.6  augustss 		DPRINTF(("uirda_set_params: speed hdr=0x%02x\n", hdr));
    715   1.1  augustss 		;
    716   1.1  augustss 	}
    717   1.1  augustss 	if (p->maxsize != sc->sc_params.maxsize) {
    718   1.5  augustss 		if (p->maxsize > IRDA_MAX_FRAME_SIZE)
    719   1.5  augustss 			return (EINVAL);
    720   1.5  augustss 		sc->sc_params.maxsize = p->maxsize;
    721   1.5  augustss #if 0
    722  1.10  augustss 		DPRINTF(("%s: new buffers, old size=%d\n", __func__,
    723   1.2  augustss 			 sc->sc_params.maxsize));
    724   1.1  augustss 		if (p->maxsize > 10000 || p < 0) /* XXX */
    725   1.1  augustss 			return (EINVAL);
    726   1.1  augustss 
    727   1.1  augustss 		/* Change the write buffer */
    728   1.1  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    729   1.1  augustss 		if (sc->sc_wr_buf != NULL)
    730   1.1  augustss 			usbd_free_buffer(sc->sc_wr_xfer);
    731   1.1  augustss 		sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer, p->maxsize+1);
    732   1.1  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    733   1.1  augustss 		if (sc->sc_wr_buf == NULL)
    734   1.1  augustss 			return (ENOMEM);
    735   1.1  augustss 
    736   1.1  augustss 		/* Change the read buffer */
    737   1.1  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
    738   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    739   1.1  augustss 		if (sc->sc_rd_buf != NULL)
    740   1.1  augustss 			usbd_free_buffer(sc->sc_rd_xfer);
    741   1.1  augustss 		sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer, p->maxsize+1);
    742   1.1  augustss 		sc->sc_rd_count = 0;
    743   1.2  augustss 		if (sc->sc_rd_buf == NULL) {
    744   1.2  augustss 			lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    745   1.2  augustss 			return (ENOMEM);
    746   1.2  augustss 		}
    747   1.4  augustss 		sc->sc_params.maxsize = p->maxsize;
    748   1.1  augustss 		err = uirda_start_read(sc); /* XXX check */
    749   1.1  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    750   1.5  augustss #endif
    751   1.1  augustss 	}
    752   1.6  augustss 	if (hdr != 0 && hdr != sc->sc_wr_hdr) {
    753  1.11  augustss 		/*
    754   1.1  augustss 		 * A change has occurred, transmit a 0 length frame with
    755   1.1  augustss 		 * the new settings.  The 0 length frame is not sent to the
    756   1.1  augustss 		 * device.
    757   1.1  augustss 		 */
    758   1.6  augustss 		DPRINTF(("%s: sc=%p setting header 0x%02x\n",
    759  1.10  augustss 			 __func__, sc, hdr));
    760   1.6  augustss 		sc->sc_wr_hdr = hdr;
    761   1.2  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    762   1.2  augustss 		sc->sc_wr_buf[0] = hdr;
    763   1.7  augustss 		n = UIRDA_OUTPUT_HEADER_SIZE;
    764   1.1  augustss 		err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    765   1.1  augustss 			  USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    766   1.2  augustss 			  UIRDA_WR_TIMEOUT, sc->sc_wr_buf, &n, "uirdast");
    767   1.2  augustss 		if (err) {
    768   1.1  augustss 			printf("%s: set failed, err=%d\n",
    769   1.1  augustss 			    USBDEVNAME(sc->sc_dev), err);
    770   1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_wr_pipe);
    771   1.1  augustss 		}
    772   1.6  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    773   1.1  augustss 	}
    774   1.1  augustss 
    775   1.1  augustss 	sc->sc_params = *p;
    776   1.1  augustss 
    777   1.1  augustss 	return (0);
    778   1.1  augustss }
    779   1.1  augustss 
    780   1.1  augustss int
    781   1.1  augustss uirda_get_speeds(void *h, int *speeds)
    782   1.1  augustss {
    783   1.1  augustss 	struct uirda_softc *sc = h;
    784   1.1  augustss 	u_int isp;
    785   1.1  augustss 	u_int usp;
    786   1.1  augustss 
    787  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    788   1.1  augustss 
    789   1.1  augustss 	if (sc->sc_dying)
    790   1.1  augustss 		return (EIO);
    791   1.1  augustss 
    792   1.1  augustss 	usp = UGETW(sc->sc_irdadesc.wBaudRate);
    793   1.1  augustss 	isp = 0;
    794   1.1  augustss 	if (usp & UI_BR_4000000) isp |= IRDA_SPEED_4000000;
    795   1.1  augustss 	if (usp & UI_BR_1152000) isp |= IRDA_SPEED_1152000;
    796   1.1  augustss 	if (usp & UI_BR_576000)  isp |= IRDA_SPEED_576000;
    797   1.1  augustss 	if (usp & UI_BR_115200)  isp |= IRDA_SPEED_115200;
    798   1.1  augustss 	if (usp & UI_BR_57600)   isp |= IRDA_SPEED_57600;
    799   1.1  augustss 	if (usp & UI_BR_38400)   isp |= IRDA_SPEED_38400;
    800   1.1  augustss 	if (usp & UI_BR_19200)   isp |= IRDA_SPEED_19200;
    801   1.1  augustss 	if (usp & UI_BR_9600)    isp |= IRDA_SPEED_9600;
    802   1.1  augustss 	if (usp & UI_BR_2400)    isp |= IRDA_SPEED_2400;
    803   1.1  augustss 	*speeds = isp;
    804  1.25        is 	DPRINTF(("%s: speeds = 0x%x\n", __func__, isp));
    805   1.1  augustss 	return (0);
    806   1.1  augustss }
    807   1.1  augustss 
    808   1.1  augustss int
    809   1.1  augustss uirda_get_turnarounds(void *h, int *turnarounds)
    810   1.1  augustss {
    811   1.1  augustss 	struct uirda_softc *sc = h;
    812   1.1  augustss 	u_int ita;
    813   1.1  augustss 	u_int uta;
    814   1.1  augustss 
    815  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    816   1.1  augustss 
    817   1.1  augustss 	if (sc->sc_dying)
    818   1.1  augustss 		return (EIO);
    819   1.1  augustss 
    820   1.1  augustss 	uta = sc->sc_irdadesc.bmMinTurnaroundTime;
    821   1.1  augustss 	ita = 0;
    822   1.1  augustss 	if (uta & UI_TA_0)     ita |= IRDA_TURNT_0;
    823   1.1  augustss 	if (uta & UI_TA_10)    ita |= IRDA_TURNT_10;
    824   1.1  augustss 	if (uta & UI_TA_50)    ita |= IRDA_TURNT_50;
    825   1.1  augustss 	if (uta & UI_TA_100)   ita |= IRDA_TURNT_100;
    826   1.1  augustss 	if (uta & UI_TA_500)   ita |= IRDA_TURNT_500;
    827   1.1  augustss 	if (uta & UI_TA_1000)  ita |= IRDA_TURNT_1000;
    828   1.1  augustss 	if (uta & UI_TA_5000)  ita |= IRDA_TURNT_5000;
    829   1.1  augustss 	if (uta & UI_TA_10000) ita |= IRDA_TURNT_10000;
    830   1.1  augustss 	*turnarounds = ita;
    831   1.1  augustss 	return (0);
    832   1.1  augustss }
    833   1.1  augustss 
    834   1.1  augustss void
    835   1.1  augustss uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    836   1.1  augustss 	    usbd_status status)
    837   1.1  augustss {
    838   1.1  augustss 	struct uirda_softc *sc = priv;
    839   1.1  augustss 	u_int32_t size;
    840   1.1  augustss 
    841  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    842   1.2  augustss 
    843   1.1  augustss 	if (status == USBD_CANCELLED) /* this is normal */
    844   1.1  augustss 		return;
    845   1.1  augustss 	if (status) {
    846  1.25        is 		size = sc->sc_hdszi;
    847   1.4  augustss 		sc->sc_rd_err = 1;
    848   1.4  augustss 	} else {
    849   1.4  augustss 		usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
    850   1.1  augustss 	}
    851  1.10  augustss 	DPRINTFN(1,("%s: sc=%p size=%u, err=%d\n", __func__, sc, size,
    852   1.4  augustss 		    sc->sc_rd_err));
    853   1.1  augustss 	sc->sc_rd_count = size;
    854   1.2  augustss 	wakeup(&sc->sc_rd_count); /* XXX should use flag */
    855  1.13  jdolecek 	selnotify(&sc->sc_rd_sel, 0);
    856   1.1  augustss }
    857   1.1  augustss 
    858   1.1  augustss usbd_status
    859   1.1  augustss uirda_start_read(struct uirda_softc *sc)
    860   1.1  augustss {
    861   1.1  augustss 	usbd_status err;
    862   1.1  augustss 
    863  1.10  augustss 	DPRINTFN(1,("%s: sc=%p, size=%d\n", __func__, sc,
    864   1.7  augustss 		    sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE));
    865   1.2  augustss 
    866   1.1  augustss 	if (sc->sc_dying)
    867   1.1  augustss 		return (USBD_IOERROR);
    868   1.1  augustss 
    869   1.4  augustss 	if (sc->sc_rd_err) {
    870   1.4  augustss 		sc->sc_rd_err = 0;
    871   1.4  augustss 		DPRINTF(("uirda_start_read: clear stall\n"));
    872   1.4  augustss 		usbd_clear_endpoint_stall(sc->sc_rd_pipe);
    873   1.4  augustss 	}
    874   1.4  augustss 
    875   1.1  augustss 	usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
    876  1.25        is 			sc->sc_params.maxsize + sc->sc_hdszi,
    877   1.2  augustss 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
    878   1.2  augustss 			USBD_NO_TIMEOUT, uirda_rd_cb);
    879   1.1  augustss 	err = usbd_transfer(sc->sc_rd_xfer);
    880   1.4  augustss 	if (err != USBD_IN_PROGRESS) {
    881   1.4  augustss 		DPRINTF(("uirda_start_read: err=%d\n", err));
    882   1.1  augustss 		return (err);
    883   1.4  augustss 	}
    884   1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    885   1.1  augustss }
    886  1.25        is 
    887  1.25        is usbd_status
    888  1.25        is usbd_get_class_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
    889  1.25        is {
    890  1.25        is 	usb_device_request_t req;
    891  1.25        is 
    892  1.25        is 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
    893  1.25        is 		    type, index, len));
    894  1.25        is 
    895  1.25        is 	req.bmRequestType = 0xa1; /* XXX ? */
    896  1.25        is 	req.bRequest = UR_GET_DESCRIPTOR;
    897  1.25        is 	USETW2(req.wValue, type, index);
    898  1.25        is 	USETW(req.wIndex, 0);
    899  1.25        is 	USETW(req.wLength, len);
    900  1.25        is 	return (usbd_do_request(dev, &req, desc));
    901  1.25        is }
    902