Home | History | Annotate | Line # | Download | only in usb
uirda.c revision 1.12
      1  1.12  augustss /*	$NetBSD: uirda.c,v 1.12 2002/08/22 09:57:13 augustss 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.12  augustss __KERNEL_RCSID(0, "$NetBSD: uirda.c,v 1.12 2002/08/22 09:57:13 augustss 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.12  augustss #ifdef UIRDA_DEBUG
     64   1.1  augustss #define DPRINTF(x)	if (uirdadebug) logprintf x
     65   1.1  augustss #define DPRINTFN(n,x)	if (uirdadebug>(n)) logprintf x
     66   1.7  augustss int	uirdadebug = 0;
     67   1.1  augustss #else
     68   1.1  augustss #define DPRINTF(x)
     69   1.1  augustss #define DPRINTFN(n,x)
     70   1.1  augustss #endif
     71   1.1  augustss 
     72   1.1  augustss /*
     73   1.1  augustss  * Protocol related definitions
     74   1.1  augustss  */
     75   1.1  augustss 
     76   1.7  augustss #define UIRDA_INPUT_HEADER_SIZE 1
     77   1.1  augustss /* Inbound header byte */
     78   1.1  augustss #define UIRDA_MEDIA_BUSY	0x80
     79   1.1  augustss #define UIRDA_SPEED_MASK	0x0f
     80   1.1  augustss #define UIRDA_NO_SPEED		0x00
     81   1.1  augustss #define UIRDA_2400		0x01
     82   1.1  augustss #define UIRDA_9600		0x02
     83   1.1  augustss #define UIRDA_19200		0x03
     84   1.1  augustss #define UIRDA_38400		0x04
     85   1.1  augustss #define UIRDA_57600		0x05
     86   1.1  augustss #define UIRDA_115200		0x06
     87   1.1  augustss #define UIRDA_576000		0x07
     88   1.1  augustss #define UIRDA_1152000		0x08
     89   1.1  augustss #define UIRDA_4000000		0x09
     90   1.1  augustss 
     91   1.7  augustss #define UIRDA_OUTPUT_HEADER_SIZE 1
     92   1.1  augustss /* Outbound header byte */
     93   1.1  augustss #define UIRDA_EB_NO_CHANGE	0x00
     94   1.1  augustss #define UIRDA_EB_48		0x10
     95   1.1  augustss #define UIRDA_EB_24		0x20
     96   1.1  augustss #define UIRDA_EB_12		0x30
     97   1.1  augustss #define UIRDA_EB_6		0x40
     98   1.1  augustss #define UIRDA_EB_3		0x50
     99   1.1  augustss #define UIRDA_EB_2		0x60
    100   1.1  augustss #define UIRDA_EB_1		0x70
    101   1.1  augustss #define UIRDA_EB_0		0x80
    102   1.1  augustss /* Speeds as above */
    103   1.1  augustss 
    104   1.1  augustss /* Class specific requests */
    105   1.1  augustss #define UR_IRDA_RECEIVING		0x01	/* Receive in progress? */
    106   1.1  augustss #define UR_IRDA_CHECK_MEDIA_BUSY	0x03
    107   1.1  augustss #define UR_IRDA_SET_RATE_SNIFF		0x04	/* opt */
    108   1.1  augustss #define UR_IRDA_SET_UNICAST_LIST	0x05	/* opt */
    109   1.1  augustss #define UR_IRDA_GET_DESC		0x06
    110   1.1  augustss 
    111   1.1  augustss typedef struct {
    112   1.1  augustss 	uByte		bLength;
    113   1.1  augustss 	uByte		bDescriptorType;
    114   1.1  augustss #define UDESC_IRDA	0x21
    115   1.4  augustss 	uWord		bcdSpecRevision;
    116   1.1  augustss 	uByte		bmDataSize;
    117   1.1  augustss #define UI_DS_2048	0x20
    118   1.1  augustss #define UI_DS_1024	0x10
    119   1.1  augustss #define UI_DS_512	0x08
    120   1.1  augustss #define UI_DS_256	0x04
    121   1.1  augustss #define UI_DS_128	0x02
    122   1.1  augustss #define UI_DS_64	0x01
    123   1.1  augustss 	uByte		bmWindowSize;
    124   1.1  augustss #define UI_WS_7		0x40
    125   1.1  augustss #define UI_WS_6		0x20
    126   1.1  augustss #define UI_WS_5		0x10
    127   1.1  augustss #define UI_WS_4		0x08
    128   1.1  augustss #define UI_WS_3		0x04
    129   1.1  augustss #define UI_WS_2		0x02
    130   1.1  augustss #define UI_WS_1		0x01
    131   1.1  augustss 	uByte		bmMinTurnaroundTime;
    132   1.1  augustss #define UI_TA_0		0x80
    133   1.1  augustss #define UI_TA_10	0x40
    134   1.1  augustss #define UI_TA_50	0x20
    135   1.1  augustss #define UI_TA_100	0x10
    136   1.1  augustss #define UI_TA_500	0x08
    137   1.1  augustss #define UI_TA_1000	0x04
    138   1.1  augustss #define UI_TA_5000	0x02
    139   1.1  augustss #define UI_TA_10000	0x01
    140   1.1  augustss 	uWord		wBaudRate;
    141   1.1  augustss #define UI_BR_4000000	0x0100
    142   1.1  augustss #define UI_BR_1152000	0x0080
    143   1.1  augustss #define UI_BR_576000	0x0040
    144   1.1  augustss #define UI_BR_115200	0x0020
    145   1.1  augustss #define UI_BR_57600	0x0010
    146   1.1  augustss #define UI_BR_38400	0x0008
    147   1.1  augustss #define UI_BR_19200	0x0004
    148   1.1  augustss #define UI_BR_9600	0x0002
    149   1.1  augustss #define UI_BR_2400	0x0001
    150   1.1  augustss 	uByte		bmAdditionalBOFs;
    151   1.1  augustss #define UI_EB_0		0x80
    152   1.1  augustss #define UI_EB_1		0x40
    153   1.1  augustss #define UI_EB_2		0x20
    154   1.1  augustss #define UI_EB_3		0x10
    155   1.1  augustss #define UI_EB_6		0x08
    156   1.1  augustss #define UI_EB_12	0x04
    157   1.1  augustss #define UI_EB_24	0x02
    158   1.1  augustss #define UI_EB_48	0x01
    159   1.1  augustss 	uByte		bIrdaSniff;
    160   1.1  augustss 	uByte		bMaxUnicastList;
    161   1.1  augustss } UPACKED usb_irda_descriptor_t;
    162   1.1  augustss #define USB_IRDA_DESCRIPTOR_SIZE 12
    163   1.1  augustss 
    164   1.1  augustss 
    165   1.1  augustss #define UIRDA_NEBOFS 8
    166   1.1  augustss static struct {
    167   1.1  augustss 	int count;
    168   1.1  augustss 	int mask;
    169   1.1  augustss 	int header;
    170   1.1  augustss } uirda_ebofs[UIRDA_NEBOFS] = {
    171   1.1  augustss 	{ 0, UI_EB_0, UIRDA_EB_0 },
    172   1.1  augustss 	{ 1, UI_EB_1, UIRDA_EB_1 },
    173   1.1  augustss 	{ 2, UI_EB_2, UIRDA_EB_2 },
    174   1.1  augustss 	{ 3, UI_EB_3, UIRDA_EB_3 },
    175   1.1  augustss 	{ 6, UI_EB_6, UIRDA_EB_6 },
    176   1.1  augustss 	{ 12, UI_EB_12, UIRDA_EB_12 },
    177   1.1  augustss 	{ 24, UI_EB_24, UIRDA_EB_24 },
    178   1.1  augustss 	{ 48, UI_EB_48, UIRDA_EB_48 }
    179   1.1  augustss };
    180   1.1  augustss 
    181   1.1  augustss #define UIRDA_NSPEEDS 9
    182   1.1  augustss static struct {
    183   1.1  augustss 	int speed;
    184   1.1  augustss 	int mask;
    185   1.1  augustss 	int header;
    186   1.1  augustss } uirda_speeds[UIRDA_NSPEEDS] = {
    187   1.1  augustss 	{ 4000000, UI_BR_4000000, UIRDA_4000000 },
    188   1.1  augustss 	{ 1152000, UI_BR_1152000, UIRDA_1152000 },
    189   1.1  augustss 	{ 576000, UI_BR_576000, UIRDA_576000 },
    190   1.1  augustss 	{ 115200, UI_BR_115200, UIRDA_115200 },
    191   1.1  augustss 	{ 57600, UI_BR_57600, UIRDA_57600 },
    192   1.1  augustss 	{ 38400, UI_BR_38400, UIRDA_38400 },
    193   1.1  augustss 	{ 19200, UI_BR_19200, UIRDA_19200 },
    194   1.1  augustss 	{ 9600, UI_BR_9600, UIRDA_9600 },
    195   1.1  augustss 	{ 2400, UI_BR_2400, UIRDA_2400 },
    196   1.1  augustss };
    197   1.1  augustss 
    198   1.1  augustss struct uirda_softc {
    199   1.1  augustss  	USBBASEDEVICE		sc_dev;
    200   1.1  augustss 	usbd_device_handle	sc_udev;
    201   1.1  augustss 	usbd_interface_handle	sc_iface;
    202   1.1  augustss 
    203   1.1  augustss 	struct lock		sc_rd_buf_lk;
    204   1.1  augustss 	u_int8_t		*sc_rd_buf;
    205   1.1  augustss 	int			sc_rd_addr;
    206   1.1  augustss 	usbd_pipe_handle	sc_rd_pipe;
    207   1.1  augustss 	usbd_xfer_handle	sc_rd_xfer;
    208   1.1  augustss 	struct selinfo		sc_rd_sel;
    209   1.1  augustss 	u_int			sc_rd_count;
    210   1.4  augustss 	u_char			sc_rd_err;
    211   1.1  augustss 
    212   1.1  augustss 	struct lock		sc_wr_buf_lk;
    213   1.1  augustss 	u_int8_t		*sc_wr_buf;
    214   1.1  augustss 	int			sc_wr_addr;
    215   1.1  augustss 	usbd_xfer_handle	sc_wr_xfer;
    216   1.1  augustss 	usbd_pipe_handle	sc_wr_pipe;
    217   1.6  augustss 	int			sc_wr_hdr;
    218   1.1  augustss 
    219   1.1  augustss 	struct device		*sc_child;
    220   1.1  augustss 	struct irda_params	sc_params;
    221   1.1  augustss 	usb_irda_descriptor_t	sc_irdadesc;
    222   1.1  augustss 
    223   1.1  augustss 	int			sc_refcnt;
    224   1.1  augustss 	char			sc_dying;
    225   1.1  augustss };
    226   1.1  augustss 
    227   1.1  augustss #define UIRDA_WR_TIMEOUT 200
    228   1.1  augustss 
    229   1.9  augustss int uirda_open(void *h, int flag, int mode, usb_proc_ptr p);
    230   1.9  augustss int uirda_close(void *h, int flag, int mode, usb_proc_ptr p);
    231   1.1  augustss int uirda_read(void *h, struct uio *uio, int flag);
    232   1.1  augustss int uirda_write(void *h, struct uio *uio, int flag);
    233   1.1  augustss int uirda_set_params(void *h, struct irda_params *params);
    234   1.1  augustss int uirda_get_speeds(void *h, int *speeds);
    235   1.1  augustss int uirda_get_turnarounds(void *h, int *times);
    236   1.9  augustss int uirda_poll(void *h, int events, usb_proc_ptr p);
    237   1.1  augustss 
    238   1.1  augustss struct irframe_methods uirda_methods = {
    239  1.11  augustss 	uirda_open, uirda_close, uirda_read, uirda_write, uirda_poll,
    240   1.1  augustss 	uirda_set_params, uirda_get_speeds, uirda_get_turnarounds
    241   1.1  augustss };
    242   1.1  augustss 
    243   1.1  augustss void uirda_rd_cb(usbd_xfer_handle xfer,	usbd_private_handle priv,
    244   1.1  augustss 		 usbd_status status);
    245   1.1  augustss usbd_status uirda_start_read(struct uirda_softc *sc);
    246   1.1  augustss 
    247   1.1  augustss usb_descriptor_t *usb_find_desc(usbd_device_handle dev, int type);
    248   1.1  augustss 
    249  1.11  augustss /*
    250   1.1  augustss  * These devices don't quite follow the spec.  Speed changing is broken
    251   1.1  augustss  * and they don't handle windows.
    252   1.1  augustss  * But we change speed in a safe way, and don't use windows now.
    253   1.1  augustss  * Some devices also seem to have an interrupt pipe that can be ignored.
    254   1.1  augustss  *
    255   1.1  augustss  * Table information taken from Linux driver.
    256   1.1  augustss  */
    257   1.1  augustss Static const struct usb_devno uirda_devs[] = {
    258   1.1  augustss 	{ USB_VENDOR_ACTISYS, USB_PRODUCT_ACTISYS_IR2000U },
    259   1.1  augustss 	{ USB_VENDOR_EXTENDED, USB_PRODUCT_EXTENDED_XTNDACCESS },
    260   1.1  augustss 	{ USB_VENDOR_KAWATSU, USB_PRODUCT_KAWATSU_KC180 },
    261   1.1  augustss };
    262   1.1  augustss #define uirda_lookup(v, p) (usb_lookup(uirda_devs, v, p))
    263   1.1  augustss 
    264   1.1  augustss USB_DECLARE_DRIVER(uirda);
    265   1.1  augustss 
    266   1.1  augustss USB_MATCH(uirda)
    267   1.1  augustss {
    268   1.1  augustss 	USB_MATCH_START(uirda, uaa);
    269   1.1  augustss 	usb_interface_descriptor_t *id;
    270   1.1  augustss 
    271   1.1  augustss 	DPRINTFN(50,("uirda_match\n"));
    272   1.1  augustss 
    273   1.1  augustss 	if (uaa->iface == NULL)
    274   1.1  augustss 		return (UMATCH_NONE);
    275   1.1  augustss 
    276   1.1  augustss 	if (uirda_lookup(uaa->vendor, uaa->product) != NULL)
    277   1.1  augustss 		return (UMATCH_VENDOR_PRODUCT);
    278   1.1  augustss 
    279   1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    280   1.1  augustss 	if (id != NULL &&
    281   1.1  augustss 	    id->bInterfaceClass == UICLASS_APPL_SPEC &&
    282   1.1  augustss 	    id->bInterfaceSubClass == UISUBCLASS_IRDA &&
    283   1.1  augustss 	    id->bInterfaceProtocol == UIPROTO_IRDA)
    284   1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    285   1.1  augustss 	return (UMATCH_NONE);
    286   1.1  augustss }
    287   1.1  augustss 
    288   1.1  augustss USB_ATTACH(uirda)
    289   1.1  augustss {
    290   1.1  augustss 	USB_ATTACH_START(uirda, sc, uaa);
    291   1.1  augustss 	usbd_device_handle	dev = uaa->device;
    292   1.1  augustss 	usbd_interface_handle	iface = uaa->iface;
    293   1.1  augustss 	char			devinfo[1024];
    294   1.1  augustss 	usb_endpoint_descriptor_t *ed;
    295   1.1  augustss 	usbd_status		err;
    296   1.1  augustss 	u_int8_t		epcount;
    297   1.4  augustss 	u_int			specrev;
    298   1.1  augustss 	int			i;
    299   1.1  augustss 	struct ir_attach_args	ia;
    300   1.1  augustss 
    301   1.1  augustss 	DPRINTFN(10,("uirda_attach: sc=%p\n", sc));
    302   1.1  augustss 
    303   1.1  augustss 	usbd_devinfo(dev, 0, devinfo);
    304   1.1  augustss 	USB_ATTACH_SETUP;
    305   1.1  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    306   1.1  augustss 
    307   1.1  augustss 	sc->sc_udev = dev;
    308   1.1  augustss 	sc->sc_iface = iface;
    309   1.1  augustss 
    310   1.1  augustss 	epcount = 0;
    311   1.1  augustss 	(void)usbd_endpoint_count(iface, &epcount);
    312   1.1  augustss 
    313   1.1  augustss 	sc->sc_rd_addr = -1;
    314   1.1  augustss 	sc->sc_wr_addr = -1;
    315   1.1  augustss 	for (i = 0; i < epcount; i++) {
    316   1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    317   1.1  augustss 		if (ed == NULL) {
    318   1.1  augustss 			printf("%s: couldn't get ep %d\n",
    319   1.1  augustss 			    USBDEVNAME(sc->sc_dev), i);
    320   1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    321   1.1  augustss 		}
    322   1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    323   1.1  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    324   1.1  augustss 			sc->sc_rd_addr = ed->bEndpointAddress;
    325   1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    326   1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    327   1.1  augustss 			sc->sc_wr_addr = ed->bEndpointAddress;
    328   1.1  augustss 		}
    329   1.1  augustss 	}
    330   1.1  augustss 	if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
    331   1.1  augustss 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    332   1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    333   1.1  augustss 	}
    334   1.1  augustss 
    335   1.1  augustss 	/* Get the IrDA descriptor */
    336   1.1  augustss 	err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
    337   1.1  augustss 		  USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
    338   1.1  augustss 	if (err) {
    339   1.8  augustss 		/* maybe it's embedded in the config desc? */
    340   1.1  augustss 		void *d = usb_find_desc(sc->sc_udev, UDESC_IRDA);
    341   1.1  augustss 		if (d == NULL) {
    342   1.1  augustss 			printf("%s: Cannot get IrDA descriptor\n",
    343   1.1  augustss 			       USBDEVNAME(sc->sc_dev));
    344   1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    345   1.1  augustss 		}
    346   1.1  augustss 		memcpy(&sc->sc_irdadesc, d, USB_IRDA_DESCRIPTOR_SIZE);
    347   1.1  augustss 	}
    348   1.8  augustss 	DPRINTF(("uirda_attach: bmDataSize=0x%02x bmWindowSize=0x%02x "
    349   1.8  augustss 		 "bmMinTurnaroundTime=0x%02x wBaudRate=0x%04x "
    350   1.8  augustss 		 "bmAdditionalBOFs=0x%02x bIrdaSniff=%d bMaxUnicastList=%d\n",
    351   1.4  augustss 		 sc->sc_irdadesc.bmDataSize,
    352   1.4  augustss 		 sc->sc_irdadesc.bmWindowSize,
    353   1.4  augustss 		 sc->sc_irdadesc.bmMinTurnaroundTime,
    354   1.4  augustss 		 UGETW(sc->sc_irdadesc.wBaudRate),
    355   1.4  augustss 		 sc->sc_irdadesc.bmAdditionalBOFs,
    356   1.4  augustss 		 sc->sc_irdadesc.bIrdaSniff,
    357   1.4  augustss 		 sc->sc_irdadesc.bMaxUnicastList));
    358   1.4  augustss 
    359   1.4  augustss 	specrev = UGETW(sc->sc_irdadesc.bcdSpecRevision);
    360   1.8  augustss 	printf("%s: USB-IrDA protocol version %x.%02x\n",
    361   1.8  augustss 	       USBDEVNAME(sc->sc_dev), specrev >> 8, specrev & 0xff);
    362   1.1  augustss 
    363   1.1  augustss 	DPRINTFN(10, ("uirda_attach: %p\n", sc->sc_udev));
    364   1.1  augustss 
    365   1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    366   1.1  augustss 			   USBDEV(sc->sc_dev));
    367   1.3  augustss 
    368   1.3  augustss 	lockinit(&sc->sc_wr_buf_lk, PZERO, "iirwrl", 0, 0);
    369   1.3  augustss 	lockinit(&sc->sc_rd_buf_lk, PZERO, "uirrdl", 0, 0);
    370   1.1  augustss 
    371   1.1  augustss 	ia.ia_type = IR_TYPE_IRFRAME;
    372   1.1  augustss 	ia.ia_methods = &uirda_methods;
    373   1.1  augustss 	ia.ia_handle = sc;
    374   1.1  augustss 
    375   1.1  augustss 	sc->sc_child = config_found(self, &ia, ir_print);
    376   1.1  augustss 
    377   1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    378   1.1  augustss }
    379   1.1  augustss 
    380   1.1  augustss USB_DETACH(uirda)
    381   1.1  augustss {
    382   1.1  augustss 	USB_DETACH_START(uirda, sc);
    383   1.1  augustss 	int s;
    384   1.1  augustss 	int rv = 0;
    385   1.1  augustss 
    386   1.1  augustss 	DPRINTF(("uirda_detach: sc=%p flags=%d\n", sc, flags));
    387   1.1  augustss 
    388   1.1  augustss 	sc->sc_dying = 1;
    389   1.1  augustss 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    390   1.1  augustss 	if (sc->sc_rd_pipe != NULL) {
    391   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    392   1.1  augustss 		usbd_close_pipe(sc->sc_rd_pipe);
    393   1.1  augustss 		sc->sc_rd_pipe = NULL;
    394   1.1  augustss 	}
    395   1.1  augustss 	if (sc->sc_wr_pipe != NULL) {
    396   1.1  augustss 		usbd_abort_pipe(sc->sc_wr_pipe);
    397   1.1  augustss 		usbd_close_pipe(sc->sc_wr_pipe);
    398   1.1  augustss 		sc->sc_wr_pipe = NULL;
    399   1.1  augustss 	}
    400   1.1  augustss 	wakeup(&sc->sc_rd_count);
    401   1.1  augustss 
    402   1.1  augustss 	s = splusb();
    403   1.1  augustss 	if (--sc->sc_refcnt >= 0) {
    404   1.1  augustss 		/* Wait for processes to go away. */
    405   1.1  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    406   1.1  augustss 	}
    407   1.1  augustss 	splx(s);
    408   1.1  augustss 
    409   1.1  augustss 	if (sc->sc_child != NULL) {
    410   1.1  augustss 		rv = config_detach(sc->sc_child, flags);
    411   1.1  augustss 		sc->sc_child = NULL;
    412   1.1  augustss 	}
    413   1.1  augustss 
    414   1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    415   1.1  augustss 			   USBDEV(sc->sc_dev));
    416   1.1  augustss 
    417   1.1  augustss 	return (rv);
    418   1.1  augustss }
    419   1.1  augustss 
    420   1.1  augustss int
    421   1.1  augustss uirda_activate(device_ptr_t self, enum devact act)
    422   1.1  augustss {
    423   1.1  augustss 	struct uirda_softc *sc = (struct uirda_softc *)self;
    424   1.1  augustss 	int error = 0;
    425   1.1  augustss 
    426   1.1  augustss 	switch (act) {
    427   1.1  augustss 	case DVACT_ACTIVATE:
    428   1.1  augustss 		return (EOPNOTSUPP);
    429   1.1  augustss 		break;
    430   1.1  augustss 
    431   1.1  augustss 	case DVACT_DEACTIVATE:
    432   1.1  augustss 		sc->sc_dying = 1;
    433   1.1  augustss 		if (sc->sc_child != NULL)
    434   1.1  augustss 			error = config_deactivate(sc->sc_child);
    435   1.1  augustss 		break;
    436   1.1  augustss 	}
    437   1.1  augustss 	return (error);
    438   1.1  augustss }
    439   1.1  augustss 
    440   1.1  augustss int
    441   1.9  augustss uirda_open(void *h, int flag, int mode, usb_proc_ptr p)
    442   1.1  augustss {
    443   1.1  augustss 	struct uirda_softc *sc = h;
    444   1.1  augustss 	int error;
    445   1.1  augustss 	usbd_status err;
    446   1.1  augustss 
    447  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    448   1.1  augustss 
    449   1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
    450   1.1  augustss 	if (err) {
    451   1.1  augustss 		error = EIO;
    452   1.1  augustss 		goto bad1;
    453   1.1  augustss 	}
    454   1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
    455   1.1  augustss 	if (err) {
    456   1.1  augustss 		error = EIO;
    457   1.1  augustss 		goto bad2;
    458   1.1  augustss 	}
    459   1.1  augustss 	sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
    460   1.1  augustss 	if (sc->sc_rd_xfer == NULL) {
    461   1.1  augustss 		error = ENOMEM;
    462   1.1  augustss 		goto bad3;
    463   1.1  augustss 	}
    464   1.1  augustss 	sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
    465   1.1  augustss 	if (sc->sc_wr_xfer == NULL) {
    466   1.1  augustss 		error = ENOMEM;
    467   1.1  augustss 		goto bad4;
    468   1.1  augustss 	}
    469   1.5  augustss 	sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
    470   1.7  augustss 			    IRDA_MAX_FRAME_SIZE + UIRDA_INPUT_HEADER_SIZE);
    471   1.5  augustss 	if (sc->sc_rd_buf == NULL) {
    472   1.5  augustss 		error = ENOMEM;
    473   1.5  augustss 		goto bad5;
    474   1.5  augustss 	}
    475   1.5  augustss 	sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
    476   1.7  augustss 			    IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE);
    477   1.5  augustss 	if (sc->sc_wr_buf == NULL) {
    478   1.5  augustss 		error = ENOMEM;
    479   1.5  augustss 		goto bad5;
    480   1.5  augustss 	}
    481   1.2  augustss 	sc->sc_rd_count = 0;
    482   1.4  augustss 	sc->sc_rd_err = 0;
    483   1.2  augustss 	sc->sc_params.speed = 0;
    484   1.2  augustss 	sc->sc_params.ebofs = 0;
    485   1.6  augustss 	sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
    486   1.6  augustss 	sc->sc_wr_hdr = -1;
    487   1.6  augustss 
    488   1.6  augustss 	err = uirda_start_read(sc);
    489   1.6  augustss 	/* XXX check err */
    490   1.2  augustss 
    491   1.1  augustss 	return (0);
    492   1.1  augustss 
    493   1.5  augustss bad5:
    494   1.5  augustss 	usbd_free_xfer(sc->sc_wr_xfer);
    495   1.5  augustss 	sc->sc_wr_xfer = NULL;
    496   1.1  augustss bad4:
    497   1.1  augustss 	usbd_free_xfer(sc->sc_rd_xfer);
    498   1.1  augustss 	sc->sc_rd_xfer = NULL;
    499   1.1  augustss bad3:
    500   1.1  augustss 	usbd_close_pipe(sc->sc_wr_pipe);
    501   1.1  augustss 	sc->sc_wr_pipe = NULL;
    502   1.1  augustss bad2:
    503   1.1  augustss 	usbd_close_pipe(sc->sc_rd_pipe);
    504   1.1  augustss 	sc->sc_rd_pipe = NULL;
    505   1.1  augustss bad1:
    506   1.1  augustss 	return (error);
    507   1.1  augustss }
    508   1.1  augustss 
    509   1.1  augustss int
    510   1.9  augustss uirda_close(void *h, int flag, int mode, usb_proc_ptr p)
    511   1.1  augustss {
    512   1.1  augustss 	struct uirda_softc *sc = h;
    513   1.1  augustss 
    514  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    515   1.1  augustss 
    516   1.1  augustss 	if (sc->sc_rd_pipe != NULL) {
    517   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    518   1.1  augustss 		usbd_close_pipe(sc->sc_rd_pipe);
    519   1.1  augustss 		sc->sc_rd_pipe = NULL;
    520   1.1  augustss 	}
    521   1.2  augustss 	if (sc->sc_wr_pipe != NULL) {
    522   1.2  augustss 		usbd_abort_pipe(sc->sc_wr_pipe);
    523   1.2  augustss 		usbd_close_pipe(sc->sc_wr_pipe);
    524   1.2  augustss 		sc->sc_wr_pipe = NULL;
    525   1.2  augustss 	}
    526   1.1  augustss 	if (sc->sc_rd_xfer != NULL) {
    527   1.1  augustss 		usbd_free_xfer(sc->sc_rd_xfer);
    528   1.1  augustss 		sc->sc_rd_xfer = NULL;
    529   1.2  augustss 		sc->sc_rd_buf = NULL;
    530   1.1  augustss 	}
    531   1.1  augustss 	if (sc->sc_wr_xfer != NULL) {
    532   1.1  augustss 		usbd_free_xfer(sc->sc_wr_xfer);
    533   1.1  augustss 		sc->sc_wr_xfer = NULL;
    534   1.2  augustss 		sc->sc_wr_buf = NULL;
    535   1.1  augustss 	}
    536   1.1  augustss 
    537   1.1  augustss 	return (0);
    538   1.1  augustss }
    539   1.1  augustss 
    540   1.1  augustss int
    541   1.1  augustss uirda_read(void *h, struct uio *uio, int flag)
    542   1.1  augustss {
    543   1.1  augustss 	struct uirda_softc *sc = h;
    544   1.1  augustss 	usbd_status err;
    545   1.1  augustss 	int s;
    546   1.1  augustss 	int error;
    547   1.2  augustss 	u_int n;
    548   1.1  augustss 
    549  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    550   1.1  augustss 
    551   1.1  augustss 	if (sc->sc_dying)
    552   1.1  augustss 		return (EIO);
    553   1.1  augustss 
    554   1.5  augustss #ifdef DIAGNOSTIC
    555   1.1  augustss 	if (sc->sc_rd_buf == NULL)
    556   1.1  augustss 		return (EINVAL);
    557   1.5  augustss #endif
    558   1.1  augustss 
    559   1.2  augustss 	sc->sc_refcnt++;
    560   1.2  augustss 
    561   1.2  augustss 	do {
    562   1.2  augustss 		s = splusb();
    563   1.2  augustss 		while (sc->sc_rd_count == 0) {
    564   1.4  augustss 			DPRINTFN(5,("uirda_read: calling tsleep()\n"));
    565   1.2  augustss 			error = tsleep(&sc->sc_rd_count, PZERO | PCATCH,
    566   1.2  augustss 				       "uirdrd", 0);
    567   1.2  augustss 			if (sc->sc_dying)
    568   1.2  augustss 				error = EIO;
    569   1.2  augustss 			if (error) {
    570   1.2  augustss 				splx(s);
    571   1.2  augustss 				DPRINTF(("uirda_read: tsleep() = %d\n", error));
    572   1.2  augustss 				goto ret;
    573   1.2  augustss 			}
    574   1.1  augustss 		}
    575   1.2  augustss 		splx(s);
    576  1.11  augustss 
    577   1.2  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
    578   1.7  augustss 		n = sc->sc_rd_count - UIRDA_INPUT_HEADER_SIZE;
    579  1.10  augustss 		DPRINTFN(1,("%s: sc=%p n=%u, hdr=0x%02x\n", __func__,
    580   1.7  augustss 			    sc, n, sc->sc_rd_buf[0]));
    581   1.2  augustss 		if (n > uio->uio_resid)
    582   1.2  augustss 			error = EINVAL;
    583   1.2  augustss 		else
    584   1.7  augustss 			error = uiomove(sc->sc_rd_buf+UIRDA_INPUT_HEADER_SIZE,
    585   1.7  augustss 					n, uio);
    586   1.2  augustss 		sc->sc_rd_count = 0;
    587   1.2  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    588  1.11  augustss 
    589   1.2  augustss 		err = uirda_start_read(sc);
    590   1.2  augustss 		/* XXX check err */
    591   1.2  augustss 
    592   1.2  augustss 	} while (n == 0);
    593   1.2  augustss 
    594   1.2  augustss 	DPRINTFN(1,("uirda_read: return %d\n", error));
    595   1.1  augustss 
    596   1.2  augustss  ret:
    597   1.1  augustss 	if (--sc->sc_refcnt < 0)
    598   1.1  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    599   1.2  augustss 	return (error);
    600   1.1  augustss }
    601   1.1  augustss 
    602   1.1  augustss int
    603   1.1  augustss uirda_write(void *h, struct uio *uio, int flag)
    604   1.1  augustss {
    605   1.1  augustss 	struct uirda_softc *sc = h;
    606   1.1  augustss 	usbd_status err;
    607   1.1  augustss 	u_int32_t n;
    608   1.1  augustss 	int error = 0;
    609   1.1  augustss 
    610  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    611   1.1  augustss 
    612   1.1  augustss 	if (sc->sc_dying)
    613   1.1  augustss 		return (EIO);
    614   1.1  augustss 
    615   1.5  augustss #ifdef DIAGNOSTIC
    616   1.5  augustss 	if (sc->sc_wr_buf == NULL)
    617   1.5  augustss 		return (EINVAL);
    618   1.5  augustss #endif
    619   1.5  augustss 
    620   1.1  augustss 	n = uio->uio_resid;
    621   1.5  augustss 	if (n > sc->sc_params.maxsize)
    622   1.1  augustss 		return (EINVAL);
    623   1.1  augustss 
    624   1.1  augustss 	sc->sc_refcnt++;
    625   1.1  augustss 	lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    626   1.1  augustss 
    627   1.1  augustss 	sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
    628   1.7  augustss 	error = uiomove(sc->sc_wr_buf + UIRDA_OUTPUT_HEADER_SIZE, n, uio);
    629   1.1  augustss 	if (!error) {
    630   1.1  augustss 		DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
    631   1.1  augustss 
    632   1.7  augustss 		n++;
    633   1.1  augustss 		err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    634   1.1  augustss 			  USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    635   1.1  augustss 			  UIRDA_WR_TIMEOUT,
    636   1.1  augustss 			  sc->sc_wr_buf, &n, "uirdawr");
    637   1.1  augustss 		DPRINTFN(2, ("uirdawrite: err=%d\n", err));
    638   1.1  augustss 		if (err) {
    639   1.1  augustss 			if (err == USBD_INTERRUPTED)
    640   1.1  augustss 				error = EINTR;
    641   1.1  augustss 			else if (err == USBD_TIMEOUT)
    642   1.1  augustss 				error = ETIMEDOUT;
    643   1.1  augustss 			else
    644   1.1  augustss 				error = EIO;
    645   1.1  augustss 		}
    646   1.1  augustss 	}
    647   1.1  augustss 
    648   1.1  augustss 	lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    649   1.1  augustss 	if (--sc->sc_refcnt < 0)
    650   1.1  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    651   1.1  augustss 
    652  1.10  augustss 	DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
    653   1.1  augustss 	return (error);
    654   1.1  augustss }
    655   1.1  augustss 
    656   1.1  augustss int
    657   1.9  augustss uirda_poll(void *h, int events, usb_proc_ptr p)
    658   1.1  augustss {
    659   1.1  augustss 	struct uirda_softc *sc = h;
    660   1.1  augustss 	int revents = 0;
    661   1.1  augustss 	int s;
    662   1.1  augustss 
    663  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    664   1.1  augustss 
    665   1.1  augustss 	s = splusb();
    666   1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    667   1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    668   1.1  augustss 	if (events & (POLLIN | POLLRDNORM)) {
    669   1.1  augustss 		if (sc->sc_rd_count != 0) {
    670  1.10  augustss 			DPRINTFN(2,("%s: have data\n", __func__));
    671   1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    672   1.1  augustss 		} else {
    673  1.10  augustss 			DPRINTFN(2,("%s: recording select\n", __func__));
    674   1.1  augustss 			selrecord(p, &sc->sc_rd_sel);
    675   1.1  augustss 		}
    676   1.1  augustss 	}
    677   1.1  augustss 	splx(s);
    678   1.1  augustss 
    679   1.1  augustss 	return (revents);
    680   1.1  augustss }
    681   1.1  augustss 
    682   1.1  augustss int
    683   1.1  augustss uirda_set_params(void *h, struct irda_params *p)
    684   1.1  augustss {
    685   1.1  augustss 	struct uirda_softc *sc = h;
    686   1.1  augustss 	usbd_status err;
    687   1.1  augustss 	int i;
    688   1.1  augustss 	u_int8_t hdr;
    689   1.1  augustss 	u_int32_t n;
    690   1.1  augustss 	u_int mask;
    691   1.1  augustss 
    692  1.10  augustss 	DPRINTF(("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
    693   1.1  augustss 		 sc, p->speed, p->ebofs, p->maxsize));
    694   1.1  augustss 
    695   1.1  augustss 	if (sc->sc_dying)
    696   1.1  augustss 		return (EIO);
    697   1.1  augustss 
    698   1.1  augustss 	hdr = 0;
    699   1.1  augustss 	if (p->ebofs != sc->sc_params.ebofs) {
    700   1.1  augustss 		/* round up ebofs */
    701   1.1  augustss 		mask = sc->sc_irdadesc.bmAdditionalBOFs;
    702   1.1  augustss 		for (i = 0; i < UIRDA_NEBOFS; i++) {
    703   1.1  augustss 			if ((mask & uirda_ebofs[i].mask) &&
    704   1.1  augustss 			    uirda_ebofs[i].count >= p->ebofs) {
    705   1.1  augustss 				hdr = uirda_ebofs[i].header;
    706   1.1  augustss 				goto found1;
    707   1.1  augustss 			}
    708   1.1  augustss 		}
    709   1.1  augustss 		/* no good value found */
    710   1.1  augustss 		return (EINVAL);
    711   1.1  augustss 	found1:
    712   1.6  augustss 		DPRINTF(("uirda_set_params: ebofs hdr=0x%02x\n", hdr));
    713   1.1  augustss 		;
    714  1.11  augustss 
    715   1.1  augustss 	}
    716   1.6  augustss 	if (hdr != 0 || p->speed != sc->sc_params.speed) {
    717   1.1  augustss 		/* find speed */
    718   1.1  augustss 		mask = UGETW(sc->sc_irdadesc.wBaudRate);
    719   1.1  augustss 		for (i = 0; i < UIRDA_NSPEEDS; i++) {
    720   1.1  augustss 			if ((mask & uirda_speeds[i].mask) &&
    721   1.1  augustss 			    uirda_speeds[i].speed == p->speed) {
    722   1.1  augustss 				hdr |= uirda_speeds[i].header;
    723   1.1  augustss 				goto found2;
    724   1.1  augustss 			}
    725   1.1  augustss 		}
    726   1.1  augustss 		/* no good value found */
    727   1.1  augustss 		return (EINVAL);
    728   1.1  augustss 	found2:
    729   1.6  augustss 		DPRINTF(("uirda_set_params: speed hdr=0x%02x\n", hdr));
    730   1.1  augustss 		;
    731   1.1  augustss 	}
    732   1.1  augustss 	if (p->maxsize != sc->sc_params.maxsize) {
    733   1.5  augustss 		if (p->maxsize > IRDA_MAX_FRAME_SIZE)
    734   1.5  augustss 			return (EINVAL);
    735   1.5  augustss 		sc->sc_params.maxsize = p->maxsize;
    736   1.5  augustss #if 0
    737  1.10  augustss 		DPRINTF(("%s: new buffers, old size=%d\n", __func__,
    738   1.2  augustss 			 sc->sc_params.maxsize));
    739   1.1  augustss 		if (p->maxsize > 10000 || p < 0) /* XXX */
    740   1.1  augustss 			return (EINVAL);
    741   1.1  augustss 
    742   1.1  augustss 		/* Change the write buffer */
    743   1.1  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    744   1.1  augustss 		if (sc->sc_wr_buf != NULL)
    745   1.1  augustss 			usbd_free_buffer(sc->sc_wr_xfer);
    746   1.1  augustss 		sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer, p->maxsize+1);
    747   1.1  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    748   1.1  augustss 		if (sc->sc_wr_buf == NULL)
    749   1.1  augustss 			return (ENOMEM);
    750   1.1  augustss 
    751   1.1  augustss 		/* Change the read buffer */
    752   1.1  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
    753   1.1  augustss 		usbd_abort_pipe(sc->sc_rd_pipe);
    754   1.1  augustss 		if (sc->sc_rd_buf != NULL)
    755   1.1  augustss 			usbd_free_buffer(sc->sc_rd_xfer);
    756   1.1  augustss 		sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer, p->maxsize+1);
    757   1.1  augustss 		sc->sc_rd_count = 0;
    758   1.2  augustss 		if (sc->sc_rd_buf == NULL) {
    759   1.2  augustss 			lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    760   1.2  augustss 			return (ENOMEM);
    761   1.2  augustss 		}
    762   1.4  augustss 		sc->sc_params.maxsize = p->maxsize;
    763   1.1  augustss 		err = uirda_start_read(sc); /* XXX check */
    764   1.1  augustss 		lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
    765   1.5  augustss #endif
    766   1.1  augustss 	}
    767   1.6  augustss 	if (hdr != 0 && hdr != sc->sc_wr_hdr) {
    768  1.11  augustss 		/*
    769   1.1  augustss 		 * A change has occurred, transmit a 0 length frame with
    770   1.1  augustss 		 * the new settings.  The 0 length frame is not sent to the
    771   1.1  augustss 		 * device.
    772   1.1  augustss 		 */
    773   1.6  augustss 		DPRINTF(("%s: sc=%p setting header 0x%02x\n",
    774  1.10  augustss 			 __func__, sc, hdr));
    775   1.6  augustss 		sc->sc_wr_hdr = hdr;
    776   1.2  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    777   1.2  augustss 		sc->sc_wr_buf[0] = hdr;
    778   1.7  augustss 		n = UIRDA_OUTPUT_HEADER_SIZE;
    779   1.1  augustss 		err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    780   1.1  augustss 			  USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
    781   1.2  augustss 			  UIRDA_WR_TIMEOUT, sc->sc_wr_buf, &n, "uirdast");
    782   1.2  augustss 		if (err) {
    783   1.1  augustss 			printf("%s: set failed, err=%d\n",
    784   1.1  augustss 			    USBDEVNAME(sc->sc_dev), err);
    785   1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_wr_pipe);
    786   1.1  augustss 		}
    787   1.6  augustss 		lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    788   1.1  augustss 	}
    789   1.1  augustss 
    790   1.1  augustss 	sc->sc_params = *p;
    791   1.1  augustss 
    792   1.1  augustss 	return (0);
    793   1.1  augustss }
    794   1.1  augustss 
    795   1.1  augustss int
    796   1.1  augustss uirda_get_speeds(void *h, int *speeds)
    797   1.1  augustss {
    798   1.1  augustss 	struct uirda_softc *sc = h;
    799   1.1  augustss 	u_int isp;
    800   1.1  augustss 	u_int usp;
    801   1.1  augustss 
    802  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    803   1.1  augustss 
    804   1.1  augustss 	if (sc->sc_dying)
    805   1.1  augustss 		return (EIO);
    806   1.1  augustss 
    807   1.1  augustss 	usp = UGETW(sc->sc_irdadesc.wBaudRate);
    808   1.1  augustss 	isp = 0;
    809   1.1  augustss 	if (usp & UI_BR_4000000) isp |= IRDA_SPEED_4000000;
    810   1.1  augustss 	if (usp & UI_BR_1152000) isp |= IRDA_SPEED_1152000;
    811   1.1  augustss 	if (usp & UI_BR_576000)  isp |= IRDA_SPEED_576000;
    812   1.1  augustss 	if (usp & UI_BR_115200)  isp |= IRDA_SPEED_115200;
    813   1.1  augustss 	if (usp & UI_BR_57600)   isp |= IRDA_SPEED_57600;
    814   1.1  augustss 	if (usp & UI_BR_38400)   isp |= IRDA_SPEED_38400;
    815   1.1  augustss 	if (usp & UI_BR_19200)   isp |= IRDA_SPEED_19200;
    816   1.1  augustss 	if (usp & UI_BR_9600)    isp |= IRDA_SPEED_9600;
    817   1.1  augustss 	if (usp & UI_BR_2400)    isp |= IRDA_SPEED_2400;
    818   1.1  augustss 	*speeds = isp;
    819   1.1  augustss 	return (0);
    820   1.1  augustss }
    821   1.1  augustss 
    822   1.1  augustss int
    823   1.1  augustss uirda_get_turnarounds(void *h, int *turnarounds)
    824   1.1  augustss {
    825   1.1  augustss 	struct uirda_softc *sc = h;
    826   1.1  augustss 	u_int ita;
    827   1.1  augustss 	u_int uta;
    828   1.1  augustss 
    829  1.10  augustss 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    830   1.1  augustss 
    831   1.1  augustss 	if (sc->sc_dying)
    832   1.1  augustss 		return (EIO);
    833   1.1  augustss 
    834   1.1  augustss 	uta = sc->sc_irdadesc.bmMinTurnaroundTime;
    835   1.1  augustss 	ita = 0;
    836   1.1  augustss 	if (uta & UI_TA_0)     ita |= IRDA_TURNT_0;
    837   1.1  augustss 	if (uta & UI_TA_10)    ita |= IRDA_TURNT_10;
    838   1.1  augustss 	if (uta & UI_TA_50)    ita |= IRDA_TURNT_50;
    839   1.1  augustss 	if (uta & UI_TA_100)   ita |= IRDA_TURNT_100;
    840   1.1  augustss 	if (uta & UI_TA_500)   ita |= IRDA_TURNT_500;
    841   1.1  augustss 	if (uta & UI_TA_1000)  ita |= IRDA_TURNT_1000;
    842   1.1  augustss 	if (uta & UI_TA_5000)  ita |= IRDA_TURNT_5000;
    843   1.1  augustss 	if (uta & UI_TA_10000) ita |= IRDA_TURNT_10000;
    844   1.1  augustss 	*turnarounds = ita;
    845   1.1  augustss 	return (0);
    846   1.1  augustss }
    847   1.1  augustss 
    848   1.1  augustss void
    849   1.1  augustss uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    850   1.1  augustss 	    usbd_status status)
    851   1.1  augustss {
    852   1.1  augustss 	struct uirda_softc *sc = priv;
    853   1.1  augustss 	u_int32_t size;
    854   1.1  augustss 
    855  1.10  augustss 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    856   1.2  augustss 
    857   1.1  augustss 	if (status == USBD_CANCELLED) /* this is normal */
    858   1.1  augustss 		return;
    859   1.1  augustss 	if (status) {
    860   1.7  augustss 		size = UIRDA_INPUT_HEADER_SIZE;
    861   1.4  augustss 		sc->sc_rd_err = 1;
    862   1.4  augustss 	} else {
    863   1.4  augustss 		usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
    864   1.1  augustss 	}
    865  1.10  augustss 	DPRINTFN(1,("%s: sc=%p size=%u, err=%d\n", __func__, sc, size,
    866   1.4  augustss 		    sc->sc_rd_err));
    867   1.1  augustss 	sc->sc_rd_count = size;
    868   1.2  augustss 	wakeup(&sc->sc_rd_count); /* XXX should use flag */
    869   1.1  augustss 	selwakeup(&sc->sc_rd_sel);
    870   1.1  augustss }
    871   1.1  augustss 
    872   1.1  augustss usbd_status
    873   1.1  augustss uirda_start_read(struct uirda_softc *sc)
    874   1.1  augustss {
    875   1.1  augustss 	usbd_status err;
    876   1.1  augustss 
    877  1.10  augustss 	DPRINTFN(1,("%s: sc=%p, size=%d\n", __func__, sc,
    878   1.7  augustss 		    sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE));
    879   1.2  augustss 
    880   1.1  augustss 	if (sc->sc_dying)
    881   1.1  augustss 		return (USBD_IOERROR);
    882   1.1  augustss 
    883   1.4  augustss 	if (sc->sc_rd_err) {
    884   1.4  augustss 		sc->sc_rd_err = 0;
    885   1.4  augustss 		DPRINTF(("uirda_start_read: clear stall\n"));
    886   1.4  augustss 		usbd_clear_endpoint_stall(sc->sc_rd_pipe);
    887   1.4  augustss 	}
    888   1.4  augustss 
    889   1.1  augustss 	usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
    890   1.7  augustss 			sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE,
    891   1.2  augustss 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
    892   1.2  augustss 			USBD_NO_TIMEOUT, uirda_rd_cb);
    893   1.1  augustss 	err = usbd_transfer(sc->sc_rd_xfer);
    894   1.4  augustss 	if (err != USBD_IN_PROGRESS) {
    895   1.4  augustss 		DPRINTF(("uirda_start_read: err=%d\n", err));
    896   1.1  augustss 		return (err);
    897   1.4  augustss 	}
    898   1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    899   1.1  augustss }
    900