stuirda.c revision 1.2.16.4 1 1.2.16.4 yamt /* $NetBSD: stuirda.c,v 1.2.16.4 2008/01/21 09:44:44 yamt Exp $ */
2 1.2.16.2 yamt
3 1.2.16.2 yamt /*
4 1.2.16.2 yamt * Copyright (c) 2001,2007 The NetBSD Foundation, Inc.
5 1.2.16.2 yamt * All rights reserved.
6 1.2.16.2 yamt *
7 1.2.16.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.16.2 yamt * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.2.16.2 yamt *
10 1.2.16.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.16.2 yamt * modification, are permitted provided that the following conditions
12 1.2.16.2 yamt * are met:
13 1.2.16.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.16.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.16.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.16.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.16.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.16.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.2.16.2 yamt * must display the following acknowledgement:
20 1.2.16.2 yamt * This product includes software developed by the NetBSD
21 1.2.16.2 yamt * Foundation, Inc. and its contributors.
22 1.2.16.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.16.2 yamt * contributors may be used to endorse or promote products derived
24 1.2.16.2 yamt * from this software without specific prior written permission.
25 1.2.16.2 yamt *
26 1.2.16.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.16.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.16.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.16.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.16.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.16.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.16.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.16.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.16.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.16.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.16.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.2.16.2 yamt */
38 1.2.16.4 yamt
39 1.2.16.2 yamt #include <sys/cdefs.h>
40 1.2.16.4 yamt __KERNEL_RCSID(0, "$NetBSD: stuirda.c,v 1.2.16.4 2008/01/21 09:44:44 yamt Exp $");
41 1.2.16.4 yamt
42 1.2.16.2 yamt #include <sys/param.h>
43 1.2.16.2 yamt
44 1.2.16.2 yamt #include <sys/device.h>
45 1.2.16.2 yamt #include <sys/systm.h>
46 1.2.16.2 yamt #include <sys/kernel.h>
47 1.2.16.2 yamt #include <sys/device.h>
48 1.2.16.2 yamt #include <sys/ioctl.h>
49 1.2.16.2 yamt #include <sys/conf.h>
50 1.2.16.2 yamt #include <sys/file.h>
51 1.2.16.2 yamt #include <sys/poll.h>
52 1.2.16.2 yamt #include <sys/select.h>
53 1.2.16.2 yamt #include <sys/proc.h>
54 1.2.16.2 yamt
55 1.2.16.2 yamt
56 1.2.16.2 yamt #include <dev/firmload.h>
57 1.2.16.2 yamt #include <dev/usb/usb.h>
58 1.2.16.2 yamt #include <dev/usb/usbdi.h>
59 1.2.16.2 yamt #include <dev/usb/usbdi_util.h>
60 1.2.16.2 yamt #include <dev/usb/usbdevs.h>
61 1.2.16.2 yamt #include <dev/usb/usb_port.h>
62 1.2.16.2 yamt
63 1.2.16.2 yamt #include <dev/ir/ir.h>
64 1.2.16.2 yamt #include <dev/ir/irdaio.h>
65 1.2.16.2 yamt #include <dev/ir/irframevar.h>
66 1.2.16.2 yamt
67 1.2.16.2 yamt #include <dev/usb/uirdavar.h>
68 1.2.16.2 yamt
69 1.2.16.2 yamt #ifdef UIRDA_DEBUG
70 1.2.16.2 yamt #define DPRINTF(x) if (stuirdadebug) logprintf x
71 1.2.16.2 yamt #define DPRINTFN(n,x) if (stuirdadebug>(n)) logprintf x
72 1.2.16.2 yamt int stuirdadebug = 1;
73 1.2.16.2 yamt #else
74 1.2.16.2 yamt #define DPRINTF(x)
75 1.2.16.2 yamt #define DPRINTFN(n,x)
76 1.2.16.2 yamt #endif
77 1.2.16.2 yamt
78 1.2.16.2 yamt struct stuirda_softc {
79 1.2.16.2 yamt struct uirda_softc sc_uirda;
80 1.2.16.2 yamt };
81 1.2.16.2 yamt
82 1.2.16.2 yamt int stuirda_fwload(struct uirda_softc *sc);
83 1.2.16.2 yamt
84 1.2.16.2 yamt /*
85 1.2.16.2 yamt * These devices need firmware download.
86 1.2.16.2 yamt */
87 1.2.16.2 yamt Static const struct usb_devno stuirda_devs[] = {
88 1.2.16.2 yamt { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_SIR4116 },
89 1.2.16.2 yamt { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_FIR4210 },
90 1.2.16.2 yamt { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4220 },
91 1.2.16.2 yamt };
92 1.2.16.2 yamt #define stuirda_lookup(v, p) (usb_lookup(stuirda_devs, v, p))
93 1.2.16.2 yamt
94 1.2.16.2 yamt int stuirda_write(void *h, struct uio *uio, int flag);
95 1.2.16.2 yamt
96 1.2.16.2 yamt struct irframe_methods stuirda_methods = {
97 1.2.16.2 yamt uirda_open, uirda_close, uirda_read, stuirda_write, uirda_poll,
98 1.2.16.2 yamt uirda_kqfilter, uirda_set_params, uirda_get_speeds,
99 1.2.16.2 yamt uirda_get_turnarounds
100 1.2.16.2 yamt };
101 1.2.16.2 yamt
102 1.2.16.2 yamt #define STUIRDA_HEADER_SIZE 3
103 1.2.16.2 yamt
104 1.2.16.2 yamt #define stuirda_activate uirda_activate
105 1.2.16.2 yamt #define stuirda_detach uirda_detach
106 1.2.16.2 yamt
107 1.2.16.2 yamt USB_DECLARE_DRIVER(stuirda);
108 1.2.16.2 yamt
109 1.2.16.2 yamt USB_MATCH(stuirda)
110 1.2.16.2 yamt {
111 1.2.16.2 yamt USB_IFMATCH_START(stuirda, uaa);
112 1.2.16.2 yamt
113 1.2.16.2 yamt DPRINTFN(50,("stuirda_match\n"));
114 1.2.16.2 yamt
115 1.2.16.2 yamt if (stuirda_lookup(uaa->vendor, uaa->product) != NULL)
116 1.2.16.2 yamt return (UMATCH_VENDOR_PRODUCT);
117 1.2.16.2 yamt
118 1.2.16.2 yamt return (UMATCH_NONE);
119 1.2.16.2 yamt }
120 1.2.16.2 yamt
121 1.2.16.2 yamt void uirda_attach(struct device *,struct device *,void *);
122 1.2.16.2 yamt
123 1.2.16.2 yamt USB_ATTACH(stuirda)
124 1.2.16.2 yamt {
125 1.2.16.2 yamt USB_IFATTACH_START(stuirda, sc, uaa);
126 1.2.16.2 yamt
127 1.2.16.2 yamt (void)uaa;
128 1.2.16.2 yamt
129 1.2.16.2 yamt sc->sc_uirda.sc_loadfw = stuirda_fwload;
130 1.2.16.2 yamt sc->sc_uirda.sc_irm = &stuirda_methods;
131 1.2.16.2 yamt sc->sc_uirda.sc_hdszi = STUIRDA_HEADER_SIZE;
132 1.2.16.2 yamt
133 1.2.16.2 yamt uirda_attach(parent,self,aux);
134 1.2.16.2 yamt }
135 1.2.16.2 yamt
136 1.2.16.2 yamt int
137 1.2.16.2 yamt stuirda_fwload(struct uirda_softc *sc) {
138 1.2.16.2 yamt
139 1.2.16.2 yamt
140 1.2.16.2 yamt int rc;
141 1.2.16.2 yamt firmware_handle_t fh;
142 1.2.16.2 yamt off_t fwsize;
143 1.2.16.2 yamt usb_device_descriptor_t usbddsc;
144 1.2.16.2 yamt usbd_xfer_handle fwxfer;
145 1.2.16.2 yamt usbd_pipe_handle fwpipe;
146 1.2.16.2 yamt usbd_status status;
147 1.2.16.2 yamt usb_device_request_t req;
148 1.2.16.2 yamt char *buffer;
149 1.2.16.2 yamt char *p;
150 1.2.16.2 yamt char fwname[12];
151 1.2.16.2 yamt int n;
152 1.2.16.2 yamt u_int8_t *usbbuf;
153 1.2.16.2 yamt /* size_t bsize; */
154 1.2.16.2 yamt
155 1.2.16.2 yamt printf("%s: needing to download firmware\n",
156 1.2.16.2 yamt USBDEVNAME(sc->sc_dev));
157 1.2.16.2 yamt
158 1.2.16.2 yamt status = usbd_get_device_desc(sc->sc_udev, &usbddsc);
159 1.2.16.2 yamt if (status) {
160 1.2.16.2 yamt printf("%s: can't get device descriptor, status %d\n",
161 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), status);
162 1.2.16.2 yamt return status;
163 1.2.16.2 yamt }
164 1.2.16.2 yamt
165 1.2.16.2 yamt rc = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
166 1.2.16.2 yamt USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
167 1.2.16.2 yamt printf("error %d reading class desc\n", rc);
168 1.2.16.2 yamt
169 1.2.16.2 yamt sprintf(fwname, "4210%02x%02x.sb",
170 1.2.16.2 yamt usbddsc.bcdDevice[1],
171 1.2.16.2 yamt usbddsc.bcdDevice[0]);
172 1.2.16.2 yamt
173 1.2.16.2 yamt printf("%s: Attempting to load firmware %s\n",
174 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), fwname);
175 1.2.16.2 yamt
176 1.2.16.2 yamt rc = firmware_open("uirda", fwname, &fh);
177 1.2.16.2 yamt
178 1.2.16.2 yamt if (rc) {
179 1.2.16.2 yamt printf("%s: Cannot load firmware\n",
180 1.2.16.2 yamt USBDEVNAME(sc->sc_dev));
181 1.2.16.2 yamt return 0;
182 1.2.16.2 yamt return rc;
183 1.2.16.2 yamt }
184 1.2.16.2 yamt fwsize = firmware_get_size(fh);
185 1.2.16.2 yamt
186 1.2.16.2 yamt printf("%s: Firmware size %lld\n",
187 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), (long long)fwsize);
188 1.2.16.2 yamt
189 1.2.16.2 yamt buffer = firmware_malloc(fwsize);
190 1.2.16.2 yamt if (buffer == NULL) {
191 1.2.16.2 yamt printf("%s: Cannot load firmware: out of memory\n",
192 1.2.16.2 yamt USBDEVNAME(sc->sc_dev));
193 1.2.16.2 yamt goto giveup2;
194 1.2.16.2 yamt }
195 1.2.16.2 yamt
196 1.2.16.2 yamt rc = firmware_read(fh, 0, buffer, (size_t)fwsize);
197 1.2.16.2 yamt
198 1.2.16.2 yamt if (rc) {
199 1.2.16.2 yamt printf("%s: Cannot read firmware\n", USBDEVNAME(sc->sc_dev));
200 1.2.16.2 yamt goto giveup3;
201 1.2.16.2 yamt }
202 1.2.16.2 yamt
203 1.2.16.2 yamt for (p = buffer + sizeof("Product Version:");
204 1.2.16.2 yamt p < buffer + fwsize - 5; p++) {
205 1.2.16.2 yamt
206 1.2.16.2 yamt if (0x1A == *p)
207 1.2.16.2 yamt break;
208 1.2.16.2 yamt }
209 1.2.16.2 yamt if (0x1a != *p || memcmp(p+1, "STMP", 4) != 0) {
210 1.2.16.2 yamt /* firmware bad */
211 1.2.16.2 yamt printf("%s: Bad firmware\n", USBDEVNAME(sc->sc_dev));
212 1.2.16.2 yamt goto giveup3;
213 1.2.16.2 yamt }
214 1.2.16.2 yamt
215 1.2.16.2 yamt p += 5;
216 1.2.16.2 yamt
217 1.2.16.2 yamt req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
218 1.2.16.2 yamt req.bRequest = 2 /* XXX magic */;
219 1.2.16.2 yamt USETW(req.wValue, 0);
220 1.2.16.2 yamt USETW(req.wIndex, 0);
221 1.2.16.2 yamt USETW(req.wLength, 0);
222 1.2.16.2 yamt rc = usbd_do_request(sc->sc_udev, &req, 0);
223 1.2.16.2 yamt if (rc) {
224 1.2.16.2 yamt printf("%s: Cannot switch to f/w d/l mode, error %d\n",
225 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), rc);
226 1.2.16.2 yamt goto giveup4;
227 1.2.16.2 yamt }
228 1.2.16.2 yamt
229 1.2.16.2 yamt delay(100000);
230 1.2.16.2 yamt
231 1.2.16.2 yamt rc = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &fwpipe);
232 1.2.16.2 yamt if (rc) {
233 1.2.16.2 yamt printf("%s: Cannot open pipe, rc=%d\n",
234 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), rc);
235 1.2.16.2 yamt goto giveup3;
236 1.2.16.2 yamt }
237 1.2.16.2 yamt fwxfer = usbd_alloc_xfer(sc->sc_udev);
238 1.2.16.2 yamt if (fwxfer == NULL) {
239 1.2.16.2 yamt printf("%s: Cannot alloc xfer\n", USBDEVNAME(sc->sc_dev));
240 1.2.16.2 yamt goto giveup4;
241 1.2.16.2 yamt }
242 1.2.16.2 yamt usbbuf = usbd_alloc_buffer(fwxfer, 1024);
243 1.2.16.2 yamt if (usbbuf == NULL) {
244 1.2.16.2 yamt printf("%s: Cannot alloc usb buf\n", USBDEVNAME(sc->sc_dev));
245 1.2.16.2 yamt goto giveup5;
246 1.2.16.2 yamt }
247 1.2.16.2 yamt n = (buffer + fwsize - p);
248 1.2.16.2 yamt while (n > 0) {
249 1.2.16.2 yamt if (n > 1023)
250 1.2.16.2 yamt n = 1023;
251 1.2.16.2 yamt memcpy(usbbuf, p, n);
252 1.2.16.2 yamt rc = usbd_bulk_transfer(fwxfer, fwpipe,
253 1.2.16.2 yamt USBD_SYNCHRONOUS|USBD_FORCE_SHORT_XFER,
254 1.2.16.2 yamt 5000, usbbuf, &n, "uirda-fw-wr");
255 1.2.16.2 yamt printf("%s: write: rc=%d, %d left\n",
256 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), rc, n);
257 1.2.16.2 yamt if (rc) {
258 1.2.16.2 yamt printf("%s: write: rc=%d, %d bytes written\n",
259 1.2.16.2 yamt USBDEVNAME(sc->sc_dev), rc, n);
260 1.2.16.2 yamt goto giveup4;
261 1.2.16.2 yamt }
262 1.2.16.2 yamt printf("%s: written %d\n", USBDEVNAME(sc->sc_dev), n);
263 1.2.16.2 yamt p += n;
264 1.2.16.2 yamt n = (buffer + fwsize - p);
265 1.2.16.2 yamt }
266 1.2.16.2 yamt delay(100000);
267 1.2.16.2 yamt /* TODO: more code here */
268 1.2.16.2 yamt rc = 0;
269 1.2.16.2 yamt usbd_free_buffer(fwxfer);
270 1.2.16.2 yamt
271 1.2.16.2 yamt giveup5: usbd_free_xfer(fwxfer);
272 1.2.16.2 yamt giveup4: usbd_close_pipe(fwpipe);
273 1.2.16.2 yamt giveup3: firmware_free(buffer, fwsize);
274 1.2.16.2 yamt giveup2: firmware_close(fh);
275 1.2.16.2 yamt
276 1.2.16.2 yamt return rc;
277 1.2.16.2 yamt
278 1.2.16.2 yamt }
279 1.2.16.2 yamt
280 1.2.16.2 yamt int
281 1.2.16.2 yamt stuirda_write(void *h, struct uio *uio, int flag)
282 1.2.16.2 yamt {
283 1.2.16.2 yamt struct uirda_softc *sc = h;
284 1.2.16.2 yamt usbd_status err;
285 1.2.16.2 yamt u_int32_t n;
286 1.2.16.2 yamt int error = 0;
287 1.2.16.2 yamt
288 1.2.16.2 yamt DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
289 1.2.16.2 yamt
290 1.2.16.2 yamt if (sc->sc_dying)
291 1.2.16.2 yamt return (EIO);
292 1.2.16.2 yamt
293 1.2.16.2 yamt #ifdef DIAGNOSTIC
294 1.2.16.2 yamt if (sc->sc_wr_buf == NULL)
295 1.2.16.2 yamt return (EINVAL);
296 1.2.16.2 yamt #endif
297 1.2.16.2 yamt
298 1.2.16.2 yamt n = uio->uio_resid;
299 1.2.16.2 yamt if (n > sc->sc_params.maxsize)
300 1.2.16.2 yamt return (EINVAL);
301 1.2.16.2 yamt
302 1.2.16.2 yamt sc->sc_refcnt++;
303 1.2.16.3 yamt mutex_enter(&sc->sc_wr_buf_lk);
304 1.2.16.2 yamt
305 1.2.16.2 yamt sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
306 1.2.16.2 yamt
307 1.2.16.2 yamt sc->sc_wr_buf[1] = 0;
308 1.2.16.2 yamt sc->sc_wr_buf[2] = 7; /* XXX turnaround - maximum for now */
309 1.2.16.2 yamt if ((n > 0 && (n % 128) == 0 && (n % 512) != 0)) {
310 1.2.16.2 yamt sc->sc_wr_buf[1] = 1;
311 1.2.16.2 yamt }
312 1.2.16.2 yamt
313 1.2.16.2 yamt error = uiomove(sc->sc_wr_buf + STUIRDA_HEADER_SIZE, n, uio);
314 1.2.16.2 yamt if (!error) {
315 1.2.16.2 yamt DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
316 1.2.16.2 yamt
317 1.2.16.2 yamt n += STUIRDA_HEADER_SIZE + sc->sc_wr_buf[1];
318 1.2.16.2 yamt err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
319 1.2.16.2 yamt USBD_FORCE_SHORT_XFER|USBD_NO_COPY,
320 1.2.16.2 yamt UIRDA_WR_TIMEOUT,
321 1.2.16.2 yamt sc->sc_wr_buf, &n, "uirdawr");
322 1.2.16.2 yamt DPRINTFN(2, ("uirdawrite: err=%d\n", err));
323 1.2.16.2 yamt if (err) {
324 1.2.16.2 yamt if (err == USBD_INTERRUPTED)
325 1.2.16.2 yamt error = EINTR;
326 1.2.16.2 yamt else if (err == USBD_TIMEOUT)
327 1.2.16.2 yamt error = ETIMEDOUT;
328 1.2.16.2 yamt else
329 1.2.16.2 yamt error = EIO;
330 1.2.16.2 yamt }
331 1.2.16.2 yamt }
332 1.2.16.2 yamt
333 1.2.16.3 yamt mutex_exit(&sc->sc_wr_buf_lk);
334 1.2.16.2 yamt if (--sc->sc_refcnt < 0)
335 1.2.16.2 yamt usb_detach_wakeup(USBDEV(sc->sc_dev));
336 1.2.16.2 yamt
337 1.2.16.2 yamt DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
338 1.2.16.2 yamt return (error);
339 1.2.16.2 yamt }
340