1 /* $NetBSD: stuirda.c,v 1.9 2008/10/13 10:01:24 is Exp $ */
4 * Copyright (c) 2001,2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: stuirda.c,v 1.9 2008/10/13 10:01:24 is Exp $");
35 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/ioctl.h>
45 #include <sys/select.h>
49 #include <dev/firmload.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
53 #include <dev/usb/usbdevs.h>
55 #include <dev/ir/ir.h>
56 #include <dev/ir/irdaio.h>
57 #include <dev/ir/irframevar.h>
59 #include <dev/usb/uirdavar.h>
62 #define DPRINTF(x) if (stuirdadebug) logprintf x
63 #define DPRINTFN(n,x) if (stuirdadebug>(n)) logprintf x
70 struct stuirda_softc
{
71 struct uirda_softc sc_uirda
;
74 int stuirda_fwload(struct uirda_softc
*sc
);
77 * These devices need firmware download.
79 Static
const struct usb_devno stuirda_devs
[] = {
80 { USB_VENDOR_SIGMATEL
, USB_PRODUCT_SIGMATEL_SIR4116
},
81 { USB_VENDOR_SIGMATEL
, USB_PRODUCT_SIGMATEL_FIR4210
},
82 { USB_VENDOR_SIGMATEL
, USB_PRODUCT_SIGMATEL_VFIR4220
},
84 #define stuirda_lookup(v, p) (usb_lookup(stuirda_devs, v, p))
86 int stuirda_write(void *h
, struct uio
*uio
, int flag
);
88 struct irframe_methods stuirda_methods
= {
89 uirda_open
, uirda_close
, uirda_read
, stuirda_write
, uirda_poll
,
90 uirda_kqfilter
, uirda_set_params
, uirda_get_speeds
,
94 #define STUIRDA_HEADER_SIZE 3
96 #define stuirda_activate uirda_activate
97 #define stuirda_detach uirda_detach
99 USB_DECLARE_DRIVER(stuirda
);
103 USB_IFMATCH_START(stuirda
, uaa
);
105 DPRINTFN(50,("stuirda_match\n"));
107 if (stuirda_lookup(uaa
->vendor
, uaa
->product
) != NULL
)
108 return (UMATCH_VENDOR_PRODUCT
);
110 return (UMATCH_NONE
);
113 void uirda_attach(device_t
, device_t
, void *);
117 USB_IFATTACH_START(stuirda
, sc
, uaa
);
121 sc
->sc_uirda
.sc_loadfw
= stuirda_fwload
;
122 sc
->sc_uirda
.sc_irm
= &stuirda_methods
;
123 sc
->sc_uirda
.sc_hdszi
= STUIRDA_HEADER_SIZE
;
125 uirda_attach(parent
,self
,aux
);
129 stuirda_fwload(struct uirda_softc
*sc
) {
133 firmware_handle_t fh
;
135 usb_device_descriptor_t usbddsc
;
136 usbd_xfer_handle fwxfer
;
137 usbd_pipe_handle fwpipe
;
139 usb_device_request_t req
;
147 printf("%s: needing to download firmware\n",
148 USBDEVNAME(sc
->sc_dev
));
150 status
= usbd_get_device_desc(sc
->sc_udev
, &usbddsc
);
152 printf("%s: can't get device descriptor, status %d\n",
153 USBDEVNAME(sc
->sc_dev
), status
);
157 rc
= usbd_get_class_desc(sc
->sc_udev
, UDESC_IRDA
, 0,
158 USB_IRDA_DESCRIPTOR_SIZE
, &sc
->sc_irdadesc
);
159 printf("error %d reading class desc\n", rc
);
161 snprintf(fwname
, sizeof(fwname
), "4210%02x%02x.sb",
162 usbddsc
.bcdDevice
[1],
163 usbddsc
.bcdDevice
[0]);
165 printf("%s: Attempting to load firmware %s\n",
166 USBDEVNAME(sc
->sc_dev
), fwname
);
168 rc
= firmware_open("stuirda", fwname
, &fh
);
171 printf("%s: Cannot load firmware\n",
172 USBDEVNAME(sc
->sc_dev
));
176 fwsize
= firmware_get_size(fh
);
178 printf("%s: Firmware size %lld\n",
179 USBDEVNAME(sc
->sc_dev
), (long long)fwsize
);
181 buffer
= firmware_malloc(fwsize
);
182 if (buffer
== NULL
) {
183 printf("%s: Cannot load firmware: out of memory\n",
184 USBDEVNAME(sc
->sc_dev
));
188 rc
= firmware_read(fh
, 0, buffer
, (size_t)fwsize
);
191 printf("%s: Cannot read firmware\n", USBDEVNAME(sc
->sc_dev
));
195 for (p
= buffer
+ sizeof("Product Version:");
196 p
< buffer
+ fwsize
- 5; p
++) {
201 if (0x1a != *p
|| memcmp(p
+1, "STMP", 4) != 0) {
203 printf("%s: Bad firmware\n", USBDEVNAME(sc
->sc_dev
));
209 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
210 req
.bRequest
= 2 /* XXX magic */;
211 USETW(req
.wValue
, 0);
212 USETW(req
.wIndex
, 0);
213 USETW(req
.wLength
, 0);
214 rc
= usbd_do_request(sc
->sc_udev
, &req
, 0);
216 printf("%s: Cannot switch to f/w d/l mode, error %d\n",
217 USBDEVNAME(sc
->sc_dev
), rc
);
223 rc
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_wr_addr
, 0, &fwpipe
);
225 printf("%s: Cannot open pipe, rc=%d\n",
226 USBDEVNAME(sc
->sc_dev
), rc
);
229 fwxfer
= usbd_alloc_xfer(sc
->sc_udev
);
230 if (fwxfer
== NULL
) {
231 printf("%s: Cannot alloc xfer\n", USBDEVNAME(sc
->sc_dev
));
234 usbbuf
= usbd_alloc_buffer(fwxfer
, 1024);
235 if (usbbuf
== NULL
) {
236 printf("%s: Cannot alloc usb buf\n", USBDEVNAME(sc
->sc_dev
));
239 n
= (buffer
+ fwsize
- p
);
243 memcpy(usbbuf
, p
, n
);
244 rc
= usbd_bulk_transfer(fwxfer
, fwpipe
,
245 USBD_SYNCHRONOUS
|USBD_FORCE_SHORT_XFER
,
246 5000, usbbuf
, &n
, "uirda-fw-wr");
247 printf("%s: write: rc=%d, %d left\n",
248 USBDEVNAME(sc
->sc_dev
), rc
, n
);
250 printf("%s: write: rc=%d, %d bytes written\n",
251 USBDEVNAME(sc
->sc_dev
), rc
, n
);
254 printf("%s: written %d\n", USBDEVNAME(sc
->sc_dev
), n
);
256 n
= (buffer
+ fwsize
- p
);
259 /* TODO: more code here */
261 usbd_free_buffer(fwxfer
);
263 giveup5
: usbd_free_xfer(fwxfer
);
264 giveup4
: usbd_close_pipe(fwpipe
);
265 giveup3
: firmware_free(buffer
, fwsize
);
266 giveup2
: firmware_close(fh
);
273 stuirda_write(void *h
, struct uio
*uio
, int flag
)
275 struct uirda_softc
*sc
= h
;
280 DPRINTFN(1,("%s: sc=%p\n", __func__
, sc
));
286 if (sc
->sc_wr_buf
== NULL
)
291 if (n
> sc
->sc_params
.maxsize
)
295 mutex_enter(&sc
->sc_wr_buf_lk
);
297 sc
->sc_wr_buf
[0] = UIRDA_EB_NO_CHANGE
| UIRDA_NO_SPEED
;
299 sc
->sc_wr_buf
[1] = 0;
300 sc
->sc_wr_buf
[2] = 7; /* XXX turnaround - maximum for now */
301 if ((n
> 0 && (n
% 128) == 0 && (n
% 512) != 0)) {
302 sc
->sc_wr_buf
[1] = 1;
305 error
= uiomove(sc
->sc_wr_buf
+ STUIRDA_HEADER_SIZE
, n
, uio
);
307 DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n
));
309 n
+= STUIRDA_HEADER_SIZE
+ sc
->sc_wr_buf
[1];
310 err
= usbd_bulk_transfer(sc
->sc_wr_xfer
, sc
->sc_wr_pipe
,
311 USBD_FORCE_SHORT_XFER
|USBD_NO_COPY
,
313 sc
->sc_wr_buf
, &n
, "uirdawr");
314 DPRINTFN(2, ("uirdawrite: err=%d\n", err
));
316 if (err
== USBD_INTERRUPTED
)
318 else if (err
== USBD_TIMEOUT
)
325 mutex_exit(&sc
->sc_wr_buf_lk
);
326 if (--sc
->sc_refcnt
< 0)
327 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
329 DPRINTFN(1,("%s: sc=%p done\n", __func__
, sc
));