Index: usbdevices.config
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/usbdevices.config,v
retrieving revision 1.4
diff -u -r1.4 usbdevices.config
--- usbdevices.config   3 Apr 2006 08:53:22 -0000       1.4
+++ usbdevices.config   1 Feb 2007 15:13:55 -0000
@@ -68,6 +68,10 @@
uirda* at uhub? port ? configuration ? interface ?
irframe* at uirda?

+# SigmaTel STIr4210/4220/4116 USB/IrDA Bridge - not quite UIRDA
+stuirda* at uhub? port ?
+irframe* at stuirda?
+
# SigmaTel STIr4200 USB/IrDA Bridge
ustir* at uhub? port ?
irframe* at ustir?
Index: files.usb
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/files.usb,v
retrieving revision 1.73
diff -u -r1.73 files.usb
--- files.usb   5 Jan 2007 17:16:22 -0000       1.73
+++ files.usb   1 Feb 2007 15:13:55 -0000
@@ -112,7 +112,12 @@
# IrDA bridges
device uirda: irbus
attach uirda at usbdevif
-file   dev/usb/uirda.c                 uirda
+file   dev/usb/uirda.c                 uirda | stuirda
+
+# SigmaTel not quite UIRDA IrDA bridge
+device stuirda: irbus
+attach stuirda at usbdevif
+file   dev/usb/stuirda.c               stuirda

# SigmaTel IrDA bridge
device ustir: irbus, irdasir
Index: uirda.c
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/uirda.c,v
retrieving revision 1.22
diff -u -r1.22 uirda.c
--- uirda.c     16 Nov 2006 01:33:27 -0000      1.22
+++ uirda.c     1 Feb 2007 15:13:55 -0000
@@ -60,46 +60,17 @@
#include <dev/ir/irdaio.h>
#include <dev/ir/irframevar.h>

+#include <dev/usb/uirdavar.h>
+
#ifdef UIRDA_DEBUG
#define DPRINTF(x)     if (uirdadebug) logprintf x
#define DPRINTFN(n,x)  if (uirdadebug>(n)) logprintf x
-int    uirdadebug = 0;
+int    uirdadebug = 1;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

-/*
- * Protocol related definitions
- */
-
-#define UIRDA_INPUT_HEADER_SIZE 1
-/* Inbound header byte */
-#define UIRDA_MEDIA_BUSY       0x80
-#define UIRDA_SPEED_MASK       0x0f
-#define UIRDA_NO_SPEED         0x00
-#define UIRDA_2400             0x01
-#define UIRDA_9600             0x02
-#define UIRDA_19200            0x03
-#define UIRDA_38400            0x04
-#define UIRDA_57600            0x05
-#define UIRDA_115200           0x06
-#define UIRDA_576000           0x07
-#define UIRDA_1152000          0x08
-#define UIRDA_4000000          0x09
-
-#define UIRDA_OUTPUT_HEADER_SIZE 1
-/* Outbound header byte */
-#define UIRDA_EB_NO_CHANGE     0x00
-#define UIRDA_EB_48            0x10
-#define UIRDA_EB_24            0x20
-#define UIRDA_EB_12            0x30
-#define UIRDA_EB_6             0x40
-#define UIRDA_EB_3             0x50
-#define UIRDA_EB_2             0x60
-#define UIRDA_EB_1             0x70
-#define UIRDA_EB_0             0x80
-/* Speeds as above */

/* Class specific requests */
#define UR_IRDA_RECEIVING              0x01    /* Receive in progress? */
@@ -108,60 +79,6 @@
#define UR_IRDA_SET_UNICAST_LIST       0x05    /* opt */
#define UR_IRDA_GET_DESC               0x06

-typedef struct {
-       uByte           bLength;
-       uByte           bDescriptorType;
-#define UDESC_IRDA     0x21
-       uWord           bcdSpecRevision;
-       uByte           bmDataSize;
-#define UI_DS_2048     0x20
-#define UI_DS_1024     0x10
-#define UI_DS_512      0x08
-#define UI_DS_256      0x04
-#define UI_DS_128      0x02
-#define UI_DS_64       0x01
-       uByte           bmWindowSize;
-#define UI_WS_7                0x40
-#define UI_WS_6                0x20
-#define UI_WS_5                0x10
-#define UI_WS_4                0x08
-#define UI_WS_3                0x04
-#define UI_WS_2                0x02
-#define UI_WS_1                0x01
-       uByte           bmMinTurnaroundTime;
-#define UI_TA_0                0x80
-#define UI_TA_10       0x40
-#define UI_TA_50       0x20
-#define UI_TA_100      0x10
-#define UI_TA_500      0x08
-#define UI_TA_1000     0x04
-#define UI_TA_5000     0x02
-#define UI_TA_10000    0x01
-       uWord           wBaudRate;
-#define UI_BR_4000000  0x0100
-#define UI_BR_1152000  0x0080
-#define UI_BR_576000   0x0040
-#define UI_BR_115200   0x0020
-#define UI_BR_57600    0x0010
-#define UI_BR_38400    0x0008
-#define UI_BR_19200    0x0004
-#define UI_BR_9600     0x0002
-#define UI_BR_2400     0x0001
-       uByte           bmAdditionalBOFs;
-#define UI_EB_0                0x80
-#define UI_EB_1                0x40
-#define UI_EB_2                0x20
-#define UI_EB_3                0x10
-#define UI_EB_6                0x08
-#define UI_EB_12       0x04
-#define UI_EB_24       0x02
-#define UI_EB_48       0x01
-       uByte           bIrdaSniff;
-       uByte           bMaxUnicastList;
-} UPACKED usb_irda_descriptor_t;
-#define USB_IRDA_DESCRIPTOR_SIZE 12
-
-
#define UIRDA_NEBOFS 8
static struct {
       int count;
@@ -195,37 +112,7 @@
       { 2400, UI_BR_2400, UIRDA_2400 },
};

-struct uirda_softc {
-       USBBASEDEVICE           sc_dev;
-       usbd_device_handle      sc_udev;
-       usbd_interface_handle   sc_iface;
-
-       struct lock             sc_rd_buf_lk;
-       u_int8_t                *sc_rd_buf;
-       int                     sc_rd_addr;
-       usbd_pipe_handle        sc_rd_pipe;
-       usbd_xfer_handle        sc_rd_xfer;
-       struct selinfo          sc_rd_sel;
-       u_int                   sc_rd_count;
-       u_char                  sc_rd_err;
-
-       struct lock             sc_wr_buf_lk;
-       u_int8_t                *sc_wr_buf;
-       int                     sc_wr_addr;
-       usbd_xfer_handle        sc_wr_xfer;
-       usbd_pipe_handle        sc_wr_pipe;
-       int                     sc_wr_hdr;
-       struct selinfo          sc_wr_sel;
-
-       struct device           *sc_child;
-       struct irda_params      sc_params;
-       usb_irda_descriptor_t   sc_irdadesc;

-       int                     sc_refcnt;
-       char                    sc_dying;
-};
-
-#define UIRDA_WR_TIMEOUT 200

int uirda_open(void *h, int flag, int mode, struct lwp *l);
int uirda_close(void *h, int flag, int mode, struct lwp *l);
@@ -309,6 +196,9 @@
       sc->sc_udev = dev;
       sc->sc_iface = iface;

+       if (sc->sc_hdszi == 0)
+               sc->sc_hdszi = UIRDA_INPUT_HEADER_SIZE;
+
       epcount = 0;
       (void)usbd_endpoint_count(iface, &epcount);

@@ -334,9 +224,19 @@
               USB_ATTACH_ERROR_RETURN;
       }

+       if (sc->sc_loadfw(sc) != 0) {
+               USB_ATTACH_ERROR_RETURN;
+       }
+
       /* Get the IrDA descriptor */
-       err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
+       err = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
+               USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
+       printf("error %d reading class desc\n", err);
+       if (err) {
+               err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
                 USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
+       }
+       printf("error %d reading desc\n", err);
       if (err) {
               /* maybe it's embedded in the config desc? */
               const void *d = usb_find_desc(sc->sc_udev, UDESC_IRDA,
@@ -348,9 +248,12 @@
               }
               memcpy(&sc->sc_irdadesc, d, USB_IRDA_DESCRIPTOR_SIZE);
       }
-       DPRINTF(("uirda_attach: bmDataSize=0x%02x bmWindowSize=0x%02x "
+       DPRINTF(("uirda_attach: bDescriptorSize %d bDescriptorType 0x%x "
+                "bmDataSize=0x%02x bmWindowSize=0x%02x "
                "bmMinTurnaroundTime=0x%02x wBaudRate=0x%04x "
                "bmAdditionalBOFs=0x%02x bIrdaSniff=%d bMaxUnicastList=%d\n",
+                sc->sc_irdadesc.bLength,
+                sc->sc_irdadesc.bDescriptorType,
                sc->sc_irdadesc.bmDataSize,
                sc->sc_irdadesc.bmWindowSize,
                sc->sc_irdadesc.bmMinTurnaroundTime,
@@ -372,7 +275,7 @@
       lockinit(&sc->sc_rd_buf_lk, PZERO, "uirrdl", 0, 0);

       ia.ia_type = IR_TYPE_IRFRAME;
-       ia.ia_methods = &uirda_methods;
+       ia.ia_methods = sc->sc_irm ? sc->sc_irm : &uirda_methods;
       ia.ia_handle = sc;

       sc->sc_child = config_found(self, &ia, ir_print);
@@ -471,13 +374,14 @@
               goto bad4;
       }
       sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
-                           IRDA_MAX_FRAME_SIZE + UIRDA_INPUT_HEADER_SIZE);
+                           IRDA_MAX_FRAME_SIZE + sc->sc_hdszi);
       if (sc->sc_rd_buf == NULL) {
               error = ENOMEM;
               goto bad5;
       }
       sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
-                           IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE);
+                           IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE +
+                               2 + 1 /* worst case ST-UIRDA */);
       if (sc->sc_wr_buf == NULL) {
               error = ENOMEM;
               goto bad5;
@@ -580,14 +484,13 @@
               splx(s);

               lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
-               n = sc->sc_rd_count - UIRDA_INPUT_HEADER_SIZE;
+               n = sc->sc_rd_count - sc->sc_hdszi;
               DPRINTFN(1,("%s: sc=%p n=%u, hdr=0x%02x\n", __func__,
                           sc, n, sc->sc_rd_buf[0]));
               if (n > uio->uio_resid)
                       error = EINVAL;
               else
-                       error = uiomove(sc->sc_rd_buf+UIRDA_INPUT_HEADER_SIZE,
-                                       n, uio);
+                       error = uiomove(sc->sc_rd_buf + sc->sc_hdszi, n, uio);
               sc->sc_rd_count = 0;
               lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);

@@ -634,7 +537,7 @@
       if (!error) {
               DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));

-               n++;
+               n += UIRDA_OUTPUT_HEADER_SIZE;
               err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
                         USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
                         UIRDA_WR_TIMEOUT,
@@ -768,14 +671,26 @@
       hdr = 0;
       if (p->ebofs != sc->sc_params.ebofs) {
               /* round up ebofs */
-               mask = sc->sc_irdadesc.bmAdditionalBOFs;
+               mask = 1 /* sc->sc_irdadesc.bmAdditionalBOFs*/;
+               DPRINTF(("u.s.p.: mask=0x%x, sc->ebofs=%d, p->ebofs=%d\n",
+                       mask, sc->sc_params.ebofs, p->ebofs));
               for (i = 0; i < UIRDA_NEBOFS; i++) {
+                       DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
+                               i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
                       if ((mask & uirda_ebofs[i].mask) &&
                           uirda_ebofs[i].count >= p->ebofs) {
                               hdr = uirda_ebofs[i].header;
                               goto found1;
                       }
               }
+               for (i = 0; i < UIRDA_NEBOFS; i++) {
+                       DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
+                               i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
+                       if ((mask & uirda_ebofs[i].mask)) {
+                               hdr = uirda_ebofs[i].header;
+                               goto found1;
+                       }
+               }
               /* no good value found */
               return (EINVAL);
       found1:
@@ -886,6 +801,7 @@
       if (usp & UI_BR_9600)    isp |= IRDA_SPEED_9600;
       if (usp & UI_BR_2400)    isp |= IRDA_SPEED_2400;
       *speeds = isp;
+       DPRINTF(("%s: speeds = 0x%x\n", __func__, isp));
       return (0);
}

@@ -927,7 +843,7 @@
       if (status == USBD_CANCELLED) /* this is normal */
               return;
       if (status) {
-               size = UIRDA_INPUT_HEADER_SIZE;
+               size = sc->sc_hdszi;
               sc->sc_rd_err = 1;
       } else {
               usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
@@ -957,7 +873,7 @@
       }

       usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
-                       sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE,
+                       sc->sc_params.maxsize + sc->sc_hdszi,
                       USBD_SHORT_XFER_OK | USBD_NO_COPY,
                       USBD_NO_TIMEOUT, uirda_rd_cb);
       err = usbd_transfer(sc->sc_rd_xfer);
@@ -967,3 +883,19 @@
       }
       return (USBD_NORMAL_COMPLETION);
}
+
+usbd_status
+usbd_get_class_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
+{
+       usb_device_request_t req;
+
+       DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
+                   type, index, len));
+
+       req.bmRequestType = 0xa1; /* XXX ? */
+       req.bRequest = UR_GET_DESCRIPTOR;
+       USETW2(req.wValue, type, index);
+       USETW(req.wIndex, 0);
+       USETW(req.wLength, len);
+       return (usbd_do_request(dev, &req, desc));
+}
--- /dev/null   2007-02-01 16:17:53.000000000 +0100
+++ uirdavar.h  2007-02-01 16:24:23.000000000 +0100
@@ -0,0 +1,171 @@
+/*     $NetBSD$        */
+
+/*
+ * Copyright (c) 2001,2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lennart Augustsson ([email protected]).
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/*
+ * Protocol related definitions
+ */
+
+#define UIRDA_INPUT_HEADER_SIZE 1
+/* Inbound header byte */
+#define UIRDA_MEDIA_BUSY       0x80
+#define UIRDA_SPEED_MASK       0x0f
+#define UIRDA_NO_SPEED         0x00
+#define UIRDA_2400             0x01
+#define UIRDA_9600             0x02
+#define UIRDA_19200            0x03
+#define UIRDA_38400            0x04
+#define UIRDA_57600            0x05
+#define UIRDA_115200           0x06
+#define UIRDA_576000           0x07
+#define UIRDA_1152000          0x08
+#define UIRDA_4000000          0x09
+
+#define UIRDA_OUTPUT_HEADER_SIZE 1
+/* Outbound header byte */
+#define UIRDA_EB_NO_CHANGE     0x00
+#define UIRDA_EB_48            0x10
+#define UIRDA_EB_24            0x20
+#define UIRDA_EB_12            0x30
+#define UIRDA_EB_6             0x40
+#define UIRDA_EB_3             0x50
+#define UIRDA_EB_2             0x60
+#define UIRDA_EB_1             0x70
+#define UIRDA_EB_0             0x80
+/* Speeds as above */
+
+#define UIRDA_WR_TIMEOUT 200
+
+typedef struct {
+       uByte           bLength;
+       uByte           bDescriptorType;
+#define UDESC_IRDA     0x21
+       uWord           bcdSpecRevision;
+       uByte           bmDataSize;
+#define UI_DS_2048     0x20
+#define UI_DS_1024     0x10
+#define UI_DS_512      0x08
+#define UI_DS_256      0x04
+#define UI_DS_128      0x02
+#define UI_DS_64       0x01
+       uByte           bmWindowSize;
+#define UI_WS_7                0x40
+#define UI_WS_6                0x20
+#define UI_WS_5                0x10
+#define UI_WS_4                0x08
+#define UI_WS_3                0x04
+#define UI_WS_2                0x02
+#define UI_WS_1                0x01
+       uByte           bmMinTurnaroundTime;
+#define UI_TA_0                0x80
+#define UI_TA_10       0x40
+#define UI_TA_50       0x20
+#define UI_TA_100      0x10
+#define UI_TA_500      0x08
+#define UI_TA_1000     0x04
+#define UI_TA_5000     0x02
+#define UI_TA_10000    0x01
+       uWord           wBaudRate;
+#define UI_BR_4000000  0x0100
+#define UI_BR_1152000  0x0080
+#define UI_BR_576000   0x0040
+#define UI_BR_115200   0x0020
+#define UI_BR_57600    0x0010
+#define UI_BR_38400    0x0008
+#define UI_BR_19200    0x0004
+#define UI_BR_9600     0x0002
+#define UI_BR_2400     0x0001
+       uByte           bmAdditionalBOFs;
+#define UI_EB_0                0x80
+#define UI_EB_1                0x40
+#define UI_EB_2                0x20
+#define UI_EB_3                0x10
+#define UI_EB_6                0x08
+#define UI_EB_12       0x04
+#define UI_EB_24       0x02
+#define UI_EB_48       0x01
+       uByte           bIrdaSniff;
+       uByte           bMaxUnicastList;
+} UPACKED usb_irda_descriptor_t;
+#define USB_IRDA_DESCRIPTOR_SIZE 12
+
+
+struct uirda_softc {
+       USBBASEDEVICE           sc_dev;
+       usbd_device_handle      sc_udev;
+       usbd_interface_handle   sc_iface;
+
+       struct lock             sc_rd_buf_lk;
+       u_int8_t                *sc_rd_buf;
+       int                     sc_rd_addr;
+       usbd_pipe_handle        sc_rd_pipe;
+       usbd_xfer_handle        sc_rd_xfer;
+       struct selinfo          sc_rd_sel;
+       u_int                   sc_rd_count;
+       u_char                  sc_rd_err;
+
+       struct lock             sc_wr_buf_lk;
+       u_int8_t                *sc_wr_buf;
+       int                     sc_wr_addr;
+       usbd_xfer_handle        sc_wr_xfer;
+       usbd_pipe_handle        sc_wr_pipe;
+       int                     sc_wr_hdr;
+       struct selinfo          sc_wr_sel;
+
+       struct device           *sc_child;
+       struct irda_params      sc_params;
+       usb_irda_descriptor_t   sc_irdadesc;
+
+       int                     sc_refcnt;
+       char                    sc_dying;
+       u_int8_t                sc_hdszi; /* set to value if != 1 needed */
+
+       int                     (*sc_loadfw)(struct uirda_softc *);
+       struct irframe_methods  *sc_irm;
+};
+
+usbd_status usbd_get_class_desc(usbd_device_handle, int type, int index,
+       int len, void *desc);
+
+int uirda_open(void *h, int flag, int mode, struct lwp *l);
+int uirda_close(void *h, int flag, int mode, struct lwp *l);
+int uirda_read(void *h, struct uio *uio, int flag);
+int uirda_write(void *h, struct uio *uio, int flag);
+int uirda_set_params(void *h, struct irda_params *params);
+int uirda_get_speeds(void *h, int *speeds);
+int uirda_get_turnarounds(void *h, int *times);
+int uirda_poll(void *h, int events, struct lwp *l);
+int uirda_kqfilter(void *h, struct knote *kn);
--- /dev/null   2007-02-01 16:17:53.000000000 +0100
+++ stuirda.c   2007-02-01 16:24:26.000000000 +0100
@@ -0,0 +1,339 @@
+/*     $NetBSD$        */
+
+/*
+ * Copyright (c) 2001,2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lennart Augustsson ([email protected]).
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+#include <sys/param.h>
+
+#include <sys/device.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/lock.h>
+#include <sys/ioctl.h>
+#include <sys/conf.h>
+#include <sys/file.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+#include <sys/proc.h>
+
+
+#include <dev/firmload.h>
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/usb_port.h>
+
+#include <dev/ir/ir.h>
+#include <dev/ir/irdaio.h>
+#include <dev/ir/irframevar.h>
+
+#include <dev/usb/uirdavar.h>
+
+#ifdef UIRDA_DEBUG
+#define DPRINTF(x)     if (stuirdadebug) logprintf x
+#define DPRINTFN(n,x)  if (stuirdadebug>(n)) logprintf x
+int    stuirdadebug = 1;
+#else
+#define DPRINTF(x)
+#define DPRINTFN(n,x)
+#endif
+
+struct stuirda_softc {
+       struct uirda_softc sc_uirda;
+};
+
+int stuirda_fwload(struct uirda_softc *sc);
+
+/*
+ * These devices need firmware download.
+ */
+Static const struct usb_devno stuirda_devs[] = {
+       { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4210 },
+       { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4220 },
+       { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4116 },
+};
+#define stuirda_lookup(v, p) (usb_lookup(stuirda_devs, v, p))
+
+int stuirda_write(void *h, struct uio *uio, int flag);
+
+struct irframe_methods stuirda_methods = {
+       uirda_open, uirda_close, uirda_read, stuirda_write, uirda_poll,
+       uirda_kqfilter, uirda_set_params, uirda_get_speeds,
+       uirda_get_turnarounds
+};
+
+#define STUIRDA_HEADER_SIZE 3
+
+#define stuirda_activate uirda_activate
+#define stuirda_detach uirda_detach
+
+USB_DECLARE_DRIVER(stuirda);
+
+USB_MATCH(stuirda)
+{
+       USB_MATCH_START(stuirda, uaa);
+
+       DPRINTFN(50,("stuirda_match\n"));
+
+       if (uaa->iface == NULL)
+               return (UMATCH_NONE);
+
+       if (stuirda_lookup(uaa->vendor, uaa->product) != NULL)
+               return (UMATCH_VENDOR_PRODUCT);
+
+       return (UMATCH_NONE);
+}
+
+void uirda_attach(struct device *,struct device *,void *);
+
+USB_ATTACH(stuirda)
+{
+       USB_ATTACH_START(stuirda, sc, uaa);
+
+       (void)uaa;
+
+       sc->sc_uirda.sc_loadfw = stuirda_fwload;
+       sc->sc_uirda.sc_irm = &stuirda_methods;
+       sc->sc_uirda.sc_hdszi = STUIRDA_HEADER_SIZE;
+
+       uirda_attach(parent,self,aux);
+}
+
+int
+stuirda_fwload(struct uirda_softc *sc) {
+
+
+       int rc;
+       firmware_handle_t fh;
+       off_t fwsize;
+       usb_device_descriptor_t usbddsc;
+       usbd_xfer_handle        fwxfer;
+       usbd_pipe_handle        fwpipe;
+       usbd_status status;
+       usb_device_request_t req;
+       char *buffer;
+       char *p;
+       char fwname[12];
+       int n;
+       u_int8_t *usbbuf;
+       /* size_t bsize; */
+
+       printf("%s: needing to download firmware\n",
+               USBDEVNAME(sc->sc_dev));
+
+       status = usbd_get_device_desc(sc->sc_udev, &usbddsc);
+       if (status) {
+               printf("%s: can't get device descriptor, status %d\n",
+                   USBDEVNAME(sc->sc_dev), status);
+               return status;
+       }
+
+       rc = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
+               USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
+       printf("error %d reading class desc\n", rc);
+
+       sprintf(fwname, "4210%02x%02x.sb",
+               usbddsc.bcdDevice[1],
+               usbddsc.bcdDevice[0]);
+
+       printf("%s: Attempting to load firmware %s\n",
+               USBDEVNAME(sc->sc_dev), fwname);
+
+       rc = firmware_open("uirda", fwname, &fh);
+
+       if (rc) {
+               printf("%s: Cannot load firmware\n",
+                       USBDEVNAME(sc->sc_dev));
+               return rc;
+       }
+       fwsize = firmware_get_size(fh);
+
+       printf("%s: Firmware size %lld\n",
+               USBDEVNAME(sc->sc_dev), (long long)fwsize);
+
+       buffer = firmware_malloc(fwsize);
+       if (buffer == NULL) {
+               printf("%s: Cannot load firmware: out of memory\n",
+                       USBDEVNAME(sc->sc_dev));
+               goto giveup2;
+       }
+
+       rc = firmware_read(fh, 0, buffer, (size_t)fwsize);
+
+       if (rc) {
+               printf("%s: Cannot read firmware\n", USBDEVNAME(sc->sc_dev));
+               goto giveup3;
+       }
+
+       for (p = buffer + sizeof("Product Version:");
+           p < buffer + fwsize - 5; p++) {
+
+               if (0x1A == *p)
+                       break;
+       }
+       if (0x1a != *p || memcmp(p+1, "STMP", 4) != 0) {
+               /* firmware bad */
+               printf("%s: Bad firmware\n", USBDEVNAME(sc->sc_dev));
+               goto giveup3;
+       }
+
+       p += 5;
+
+       req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+       req.bRequest = 2 /* XXX magic */;
+       USETW(req.wValue, 0);
+       USETW(req.wIndex, 0);
+       USETW(req.wLength, 0);
+       rc = usbd_do_request(sc->sc_udev, &req, 0);
+       if (rc) {
+               printf("%s: Cannot switch to f/w d/l mode, error %d\n",
+                       USBDEVNAME(sc->sc_dev), rc);
+               goto giveup4;
+       }
+
+       delay(100000);
+
+       rc = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &fwpipe);
+       if (rc) {
+               printf("%s: Cannot open pipe, rc=%d\n",
+                   USBDEVNAME(sc->sc_dev), rc);
+               goto giveup3;
+       }
+       fwxfer = usbd_alloc_xfer(sc->sc_udev);
+       if (fwxfer == NULL) {
+               printf("%s: Cannot alloc xfer\n", USBDEVNAME(sc->sc_dev));
+               goto giveup4;
+       }
+       usbbuf = usbd_alloc_buffer(fwxfer, 1024);
+       if (usbbuf == NULL) {
+               printf("%s: Cannot alloc usb buf\n", USBDEVNAME(sc->sc_dev));
+               goto giveup5;
+       }
+       n = (buffer + fwsize - p);
+       while (n > 0) {
+               if (n > 1023)
+                       n = 1023;
+               memcpy(usbbuf, p, n);
+               rc = usbd_bulk_transfer(fwxfer, fwpipe,
+                   USBD_SYNCHRONOUS|USBD_FORCE_SHORT_XFER,
+                   5000, usbbuf, &n, "uirda-fw-wr");
+               printf("%s: write: rc=%d, %d left\n",
+                   USBDEVNAME(sc->sc_dev), rc, n);
+               if (rc) {
+                       printf("%s: write: rc=%d, %d bytes written\n",
+                           USBDEVNAME(sc->sc_dev), rc, n);
+                       goto giveup4;
+               }
+               printf("%s: written %d\n", USBDEVNAME(sc->sc_dev), n);
+               p += n;
+               n = (buffer + fwsize - p);
+       }
+       delay(100000);
+       /* TODO: more code here */
+       rc = 0;
+       usbd_free_buffer(fwxfer);
+
+       giveup5: usbd_free_xfer(fwxfer);
+       giveup4: usbd_close_pipe(fwpipe);
+       giveup3: firmware_free(buffer, fwsize);
+       giveup2: firmware_close(fh);
+
+       return rc;
+
+}
+
+int
+stuirda_write(void *h, struct uio *uio, int flag)
+{
+       struct uirda_softc *sc = h;
+       usbd_status err;
+       u_int32_t n;
+       int error = 0;
+
+       DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
+
+       if (sc->sc_dying)
+               return (EIO);
+
+#ifdef DIAGNOSTIC
+       if (sc->sc_wr_buf == NULL)
+               return (EINVAL);
+#endif
+
+       n = uio->uio_resid;
+       if (n > sc->sc_params.maxsize)
+               return (EINVAL);
+
+       sc->sc_refcnt++;
+       lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
+
+       sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
+
+       sc->sc_wr_buf[1] = 0;
+       sc->sc_wr_buf[2] = 7; /* XXX turnaround - maximum for now */
+       if ((n > 0 && (n % 128) == 0 && (n % 512) != 0)) {
+               sc->sc_wr_buf[1] = 1;
+       }
+
+       error = uiomove(sc->sc_wr_buf + STUIRDA_HEADER_SIZE, n, uio);
+       if (!error) {
+               DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
+
+               n += STUIRDA_HEADER_SIZE + sc->sc_wr_buf[1];
+               err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
+                         USBD_FORCE_SHORT_XFER|USBD_NO_COPY,
+                         UIRDA_WR_TIMEOUT,
+                         sc->sc_wr_buf, &n, "uirdawr");
+               DPRINTFN(2, ("uirdawrite: err=%d\n", err));
+               if (err) {
+                       if (err == USBD_INTERRUPTED)
+                               error = EINTR;
+                       else if (err == USBD_TIMEOUT)
+                               error = ETIMEDOUT;
+                       else
+                               error = EIO;
+               }
+       }
+
+       lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
+       if (--sc->sc_refcnt < 0)
+               usb_detach_wakeup(USBDEV(sc->sc_dev));
+
+       DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
+       return (error);
+}