usbdi.c revision 1.119.12.7 1 /* $NetBSD: usbdi.c,v 1.119.12.7 2009/11/12 07:55:42 uebayasi Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.99 2006/11/27 18:39:02 marius Exp $ */
3
4 /*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.119.12.7 2009/11/12 07:55:42 uebayasi Exp $");
43 /* __FBSDID("$FreeBSD: src/sys/dev/usb/usbdi.c,v 1.99 2006/11/27 18:39:02 marius Exp $"); */
44
45 #ifdef __NetBSD__
46 #include "opt_compat_netbsd.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #include "usb_if.h"
58 #if defined(DIAGNOSTIC) && defined(__i386__)
59 #include <machine/cpu.h>
60 #endif
61 #endif
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/proc.h>
65
66 #include <machine/bus.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdivar.h>
72 /* #include <dev/usb/usb_mem.h> */
73 #include <dev/usb/usb_quirks.h>
74
75 #if defined(__FreeBSD__)
76 #include "usb_if.h"
77 #define delay(d) DELAY(d)
78 #endif
79
80 /* UTF-8 encoding stuff */
81 #include <fs/unicode.h>
82
83 #ifdef USB_DEBUG
84 #define DPRINTF(x) if (usbdebug) logprintf x
85 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
86 extern int usbdebug;
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n,x)
90 #endif
91
92 Static usbd_xfer_handle usbd_alloc_xfer_flag(usbd_device_handle,
93 usbd_pipe_handle pipe, enum usbd_waitflg);
94 Static void usbd_free_buffer_flag(usbd_xfer_handle, enum usbd_waitflg);
95 Static usbd_status usbd_free_xfer_flag(usbd_xfer_handle, enum usbd_waitflg);
96 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
97 Static void usbd_callback_task(void *);
98 Static void usbd_do_request_async_cb
99 (usbd_xfer_handle, usbd_private_handle, usbd_status);
100 Static void usbd_do_request_async_task(void *);
101 Static void usbd_start_next(usbd_pipe_handle pipe);
102 Static usbd_status usbd_open_pipe_ival
103 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
104
105 int
106 usbd_xfer_isread(usbd_xfer_handle xfer)
107 {
108 if (xfer->rqflags & URQ_REQUEST)
109 return (xfer->request.bmRequestType & UT_READ);
110 else
111 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
112 UE_DIR_IN);
113 }
114
115 #ifdef USB_DEBUG
116 void
117 usbd_dump_iface(struct usbd_interface *iface)
118 {
119 printf("usbd_dump_iface: iface=%p\n", iface);
120 if (iface == NULL)
121 return;
122 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
123 iface->device, iface->idesc, iface->index, iface->altindex,
124 iface->priv);
125 }
126
127 void
128 usbd_dump_device(struct usbd_device *dev)
129 {
130 printf("usbd_dump_device: dev=%p\n", dev);
131 if (dev == NULL)
132 return;
133 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
134 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
135 "power=%d langid=%d\n",
136 dev->address, dev->config, dev->depth, dev->speed,
137 dev->self_powered, dev->power, dev->langid);
138 }
139
140 void
141 usbd_dump_endpoint(struct usbd_endpoint *endp)
142 {
143 printf("usbd_dump_endpoint: endp=%p\n", endp);
144 if (endp == NULL)
145 return;
146 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
147 if (endp->edesc)
148 printf(" bEndpointAddress=0x%02x\n",
149 endp->edesc->bEndpointAddress);
150 }
151
152 void
153 usbd_dump_queue(usbd_pipe_handle pipe)
154 {
155 usbd_xfer_handle xfer;
156
157 printf("usbd_dump_queue: pipe=%p\n", pipe);
158 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
159 printf(" xfer=%p\n", xfer);
160 }
161 }
162
163 void
164 usbd_dump_pipe(usbd_pipe_handle pipe)
165 {
166 printf("usbd_dump_pipe: pipe=%p\n", pipe);
167 if (pipe == NULL)
168 return;
169 usbd_dump_iface(pipe->iface);
170 usbd_dump_device(pipe->device);
171 usbd_dump_endpoint(pipe->endpoint);
172 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
173 pipe->refcnt, pipe->running, pipe->aborting);
174 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
175 pipe->intrxfer, pipe->repeat, pipe->interval);
176 }
177 #endif
178
179 usbd_status
180 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
181 u_int8_t flags, usbd_pipe_handle *pipe)
182 {
183 return (usbd_open_pipe_ival(iface, address, flags, pipe,
184 USBD_DEFAULT_INTERVAL));
185 }
186
187 usbd_status
188 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
189 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
190 {
191 usbd_pipe_handle p;
192 struct usbd_endpoint *ep;
193 usbd_status err;
194 int i;
195
196 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
197 iface, address, flags));
198
199 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
200 ep = &iface->endpoints[i];
201 if (ep->edesc == NULL)
202 return (USBD_IOERROR);
203 if (ep->edesc->bEndpointAddress == address)
204 goto found;
205 }
206 return (USBD_BAD_ADDRESS);
207 found:
208 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
209 return (USBD_IN_USE);
210 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
211 if (err)
212 return (err);
213 LIST_INSERT_HEAD(&iface->pipes, p, next);
214 *pipe = p;
215 return (USBD_NORMAL_COMPLETION);
216 }
217
218 usbd_status
219 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
220 u_int8_t flags, usbd_pipe_handle *pipe,
221 usbd_private_handle priv, void *buffer, u_int32_t len,
222 usbd_callback cb, int ival)
223 {
224 usbd_status err;
225 usbd_xfer_handle xfer;
226 usbd_pipe_handle ipipe;
227
228 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
229 address, flags, len));
230
231 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
232 &ipipe, ival);
233 if (err)
234 return (err);
235 xfer = usbd_alloc_xfer(iface->device, ipipe);
236 if (xfer == NULL) {
237 err = USBD_NOMEM;
238 goto bad1;
239 }
240 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
241 USBD_NO_TIMEOUT, cb);
242 ipipe->intrxfer = xfer;
243 ipipe->repeat = 1;
244 err = usbd_transfer(xfer);
245 *pipe = ipipe;
246 if (err != USBD_IN_PROGRESS && err)
247 goto bad2;
248 return (USBD_NORMAL_COMPLETION);
249
250 bad2:
251 ipipe->intrxfer = NULL;
252 ipipe->repeat = 0;
253 usbd_free_xfer(xfer);
254 bad1:
255 usbd_close_pipe(ipipe);
256 return (err);
257 }
258
259 usbd_status
260 usbd_close_pipe(usbd_pipe_handle pipe)
261 {
262 #ifdef DIAGNOSTIC
263 if (pipe == NULL) {
264 printf("usbd_close_pipe: pipe==NULL\n");
265 return (USBD_NORMAL_COMPLETION);
266 }
267 #endif
268
269 if (--pipe->refcnt != 0)
270 return (USBD_NORMAL_COMPLETION);
271 if (! SIMPLEQ_EMPTY(&pipe->queue))
272 return (USBD_PENDING_REQUESTS);
273 LIST_REMOVE(pipe, next);
274 pipe->endpoint->refcnt--;
275 pipe->methods->close(pipe);
276 if (pipe->intrxfer != NULL)
277 usbd_free_xfer(pipe->intrxfer);
278 free(pipe, M_USB);
279 return (USBD_NORMAL_COMPLETION);
280 }
281
282 usbd_status
283 usbd_transfer(usbd_xfer_handle xfer)
284 {
285 usbd_pipe_handle pipe = xfer->pipe;
286 usbd_status err;
287 u_int size, flags;
288 int s;
289
290 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%x, pipe=%p, running=%d\n",
291 xfer, xfer->flags, pipe, pipe->running));
292 #ifdef USB_DEBUG
293 if (usbdebug > 5)
294 usbd_dump_queue(pipe);
295 #endif
296 xfer->done = 0;
297
298 if (pipe->aborting)
299 return (USBD_CANCELLED);
300
301 size = xfer->length;
302 flags = xfer->flags;
303 /* If there is no buffer, allocate one. */
304 if (!(xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) && size != 0) {
305 struct usbd_bus *bus = pipe->device->bus;
306
307 #ifdef DIAGNOSTIC
308 if (xfer->rqflags & URQ_AUTO_BUF)
309 printf("usbd_transfer: has old buffer!\n");
310 if (xfer->device->bus->intr_context)
311 panic("usbd_transfer: allocm not in process context");
312 #endif
313 err = bus->methods->allocm(bus, xfer,
314 (flags & USBD_NO_COPY) ? xfer->buffer : NULL,
315 size);
316 if (err)
317 return (err);
318 xfer->rqflags |= URQ_AUTO_BUF;
319 }
320 #ifdef DIAGNOSTIC
321 if ((xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) != 0 &&
322 size > xfer->bufsize) {
323 panic("usbd_transfer: buffer size overrun size=%u bufsize=%lu rqflags=%x",
324 size, (unsigned long)xfer->bufsize, xfer->rqflags);
325 }
326 #endif
327
328 #ifdef DIAGNOSTIC
329 if ((flags & USBD_NO_COPY) == 0 && size != 0) {
330 if (xfer->rqflags & URQ_DEV_MAP_BUFFER)
331 printf("usbd_transfer: USBD_NO_COPY recommended with mapped buffer");
332 if (xfer->rqflags & URQ_DEV_MAP_MBUF)
333 panic("usbd_transfer: USBD_NO_COPY required with mapped mbuf");
334 }
335 #endif
336 /* Copy data if going out. */
337 if (!(flags & USBD_NO_COPY) && size != 0 &&
338 !usbd_xfer_isread(xfer) && xfer->buffer != xfer->hcbuffer) {
339 DPRINTFN(5, ("usbd_transfer: copy %p (alloc) <- %p (buffer), %u bytes\n",
340 xfer->hcbuffer, xfer->buffer, (unsigned)size));
341 memcpy(xfer->hcbuffer, xfer->buffer, size);
342 }
343
344 err = pipe->methods->transfer(xfer);
345 /*
346 * Note xfer may not be valid after non-error completion
347 * of the transfer method unless synchronous.
348 */
349
350 if (err != USBD_IN_PROGRESS && err) {
351 /* The transfer has not been queued, so free buffer. */
352 if (xfer->rqflags & URQ_AUTO_BUF) {
353 struct usbd_bus *bus = pipe->device->bus;
354
355 #ifdef DIAGNOSTIC
356 if (bus->intr_context)
357 panic("usbd_transfer: freem not in process context");
358 #endif
359 bus->methods->freem(bus, xfer, U_WAITOK);
360 xfer->rqflags &= ~URQ_AUTO_BUF;
361 }
362 }
363
364 if (!(flags & USBD_SYNCHRONOUS))
365 return (xfer->done ? 0 : USBD_IN_PROGRESS);
366
367 /* Sync transfer, wait for completion. */
368 s = splusb();
369 while (!xfer->done) {
370 if (pipe->device->bus->use_polling)
371 panic("usbd_transfer: not done");
372 tsleep(xfer, PRIBIO, "usbsyn", 0);
373 }
374 splx(s);
375 return (xfer->status);
376 }
377
378 /* Like usbd_transfer(), but waits for completion. */
379 usbd_status
380 usbd_sync_transfer(usbd_xfer_handle xfer)
381 {
382 xfer->flags |= USBD_SYNCHRONOUS;
383 return (usbd_transfer(xfer));
384 }
385
386 void *
387 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
388 {
389 struct usbd_bus *bus = xfer->device->bus;
390 usbd_status err;
391
392 USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == 0,
393 ("usbd_alloc_buffer: xfer already has a buffer"));
394
395 #ifdef DIAGNOSTIC
396 if (xfer->device->bus->intr_context)
397 panic("usbd_alloc_buffer: allocm not in process context");
398 #endif
399 err = bus->methods->allocm(bus, xfer, NULL, size);
400 if (err)
401 return (NULL);
402 xfer->bufsize = size;
403 xfer->rqflags |= URQ_DEV_BUF;
404 return xfer->hcbuffer;
405 }
406
407 Static void
408 usbd_free_buffer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
409 {
410 struct usbd_bus *bus;
411
412 USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == URQ_DEV_BUF,
413 ("usbd_free_buffer_flag: no/auto buffer"));
414
415 xfer->rqflags &= ~(URQ_DEV_BUF | URQ_AUTO_BUF);
416 xfer->bufsize = 0;
417 bus = xfer->device->bus;
418 #ifdef DIAGNOSTIC
419 if (waitflg == U_WAITOK && bus->intr_context)
420 panic("usbd_free_buffer: freem not in process context");
421 #endif
422 bus->methods->freem(bus, xfer, waitflg);
423
424 xfer->hcbuffer = NULL;
425 }
426
427 void
428 usbd_free_buffer(usbd_xfer_handle xfer)
429 {
430 usbd_free_buffer_flag(xfer, U_WAITOK);
431 }
432
433 void *
434 usbd_get_buffer(usbd_xfer_handle xfer)
435 {
436 if (!(xfer->rqflags & URQ_DEV_BUF))
437 return (NULL);
438 return (xfer->hcbuffer);
439 }
440
441 /*
442 * map buffer handlings
443 */
444
445 /* Allocate mapping resources to prepare buffer mapping. */
446 usbd_status
447 usbd_map_alloc(usbd_xfer_handle xfer)
448 {
449 struct usbd_bus *bus = xfer->device->bus;
450 usbd_status err;
451
452 #ifdef DIAGNOSTIC
453 if (bus->intr_context)
454 panic("usbd_map_alloc: map_alloc not in process context");
455 #endif
456 USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0,
457 ("usbd_map_alloc: map already allocated"));
458
459 err = bus->methods->map_alloc(xfer);
460 if (err != USBD_NORMAL_COMPLETION)
461 return (err);
462
463 xfer->rqflags |= URQ_DEV_MAP_PREPARED;
464
465 return (USBD_NORMAL_COMPLETION);
466 }
467
468 /* Free mapping resources. */
469 void
470 usbd_map_free(usbd_xfer_handle xfer)
471 {
472 struct usbd_bus *bus = xfer->device->bus;
473
474 #ifdef DIAGNOSTIC
475 if (bus->intr_context)
476 panic("usbd_map_free: map_free not in process context");
477 #endif
478 USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED),
479 ("usbd_map_free: map not allocated"));
480
481 bus->methods->map_free(xfer);
482
483 xfer->rqflags &= ~URQ_DEV_MAP_PREPARED;
484 }
485
486 /* Map plain buffer. */
487 void
488 usbd_map_buffer(usbd_xfer_handle xfer, void *buf, u_int32_t size)
489 {
490 struct usbd_bus *bus = xfer->device->bus;
491
492 #ifdef DIAGNOSTIC
493 if ((xfer->rqflags & URQ_MASK_BUF))
494 panic("usbd_map_buffer: already has buffer (rqflags=%x)",
495 xfer->rqflags);
496 if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
497 panic("usbd_map_buffer: mapm (without map_alloc) not in process context");
498 #endif
499
500 bus->methods->mapm(xfer, buf, size);
501
502 xfer->bufsize = size;
503 xfer->rqflags |= URQ_DEV_MAP_BUFFER;
504 }
505
506 usbd_xfer_handle
507 /* XXX API change */
508 #if 0
509 usbd_alloc_xfer(usbd_device_handle dev)
510 #else
511 usbd_alloc_xfer(usbd_device_handle dev, usbd_pipe_handle pipe)
512 #endif
513 {
514 return (usbd_alloc_xfer_flag(dev, pipe, U_WAITOK));
515 }
516
517 usbd_xfer_handle
518 usbd_alloc_default_xfer(usbd_device_handle dev)
519 {
520 return (usbd_alloc_xfer_flag(dev, dev->default_pipe, U_WAITOK));
521 }
522
523 /* Map mbuf(9) chain. */
524 usbd_status
525 usbd_map_buffer_mbuf(usbd_xfer_handle xfer, struct mbuf *chain)
526 {
527 struct usbd_bus *bus = xfer->device->bus;
528 usbd_status err;
529
530 #ifdef DIAGNOSTIC
531 if ((xfer->rqflags & URQ_MASK_BUF))
532 panic("usbd_map_buffer_mbuf: already has buffer (rqflags=%x)",
533 xfer->rqflags);
534 if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
535 panic("usbd_map_buffer_mbuf: mapm_mbuf (without map_alloc) not in process context");
536 #endif
537
538 err = bus->methods->mapm_mbuf(xfer, chain);
539 if (err != USBD_NORMAL_COMPLETION)
540 return (err);
541
542 xfer->bufsize = chain->m_pkthdr.len;
543 xfer->mbuf_chain = chain;
544 xfer->rqflags |= URQ_DEV_MAP_MBUF;
545
546 return (err);
547 }
548
549 /* Unmap plain buffer or mbuf(9) chain. */
550 void
551 usbd_unmap_buffer(usbd_xfer_handle xfer)
552 {
553 struct usbd_bus *bus = xfer->device->bus;
554
555 USB_KASSERT2((xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF)),
556 ("usbd_unmap_buffer: no map"));
557 #ifdef DIAGNOSTIC
558 if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
559 panic("usbd_unmap_buffer: unmapm (without map_alloc) not in process context");
560 #endif
561
562 bus->methods->unmapm(xfer);
563
564 xfer->rqflags &= ~(URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF);
565 xfer->bufsize = 0;
566 xfer->mbuf_chain = NULL;
567 }
568
569 Static usbd_xfer_handle
570 usbd_alloc_xfer_flag(usbd_device_handle dev, usbd_pipe_handle pipe,
571 enum usbd_waitflg waitflg)
572 {
573 usbd_xfer_handle xfer;
574
575 #ifdef DIAGNOSTIC
576 if (waitflg == U_WAITOK && dev->bus->intr_context)
577 panic("usbd_alloc_xfer: allocx not in process context");
578 if (pipe == NULL)
579 panic("usbd_alloc_xfer: pipe == NULL");
580 #endif
581 xfer = dev->bus->methods->allocx(dev->bus, pipe, waitflg);
582 if (xfer == NULL)
583 return (NULL);
584 xfer->pipe = pipe;
585 xfer->device = dev;
586 usb_callout_init(xfer->timeout_handle);
587 DPRINTFN(5,("usbd_alloc_xfer_flag() = %p\n", xfer));
588 return (xfer);
589 }
590
591 usbd_status
592 usbd_free_xfer(usbd_xfer_handle xfer)
593 {
594
595 return (usbd_free_xfer_flag(xfer, U_WAITOK));
596 }
597
598 Static usbd_status
599 usbd_free_xfer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
600 {
601 DPRINTFN(5,("usbd_free_xfer_flag: %p %d\n", xfer, waitflg));
602 usb_rem_task(xfer->device, &xfer->task);
603 if (xfer->rqflags & (URQ_DEV_BUF | URQ_AUTO_BUF))
604 usbd_free_buffer_flag(xfer, waitflg);
605 if (xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF))
606 usbd_unmap_buffer(xfer);
607 if (waitflg == U_WAITOK && (xfer->rqflags & URQ_DEV_MAP_PREPARED))
608 usbd_map_free(xfer);
609 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
610 if (callout_pending(&xfer->timeout_handle)) {
611 callout_stop(&xfer->timeout_handle);
612 printf("usbd_free_xfer_flag: timout_handle pending");
613 }
614 #endif
615 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
616 return (USBD_NORMAL_COMPLETION);
617 }
618
619 void
620 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
621 usbd_private_handle priv, void *buffer, u_int32_t length,
622 u_int16_t flags, u_int32_t timeout,
623 usbd_callback callback)
624 {
625 USB_KASSERT(xfer->pipe == pipe); /* XXX pipe arg should go */
626 xfer->priv = priv;
627 xfer->buffer = buffer;
628 xfer->length = length;
629 xfer->actlen = 0;
630 xfer->flags = flags;
631 xfer->timeout = timeout;
632 xfer->status = USBD_NOT_STARTED;
633 xfer->callback = callback;
634 xfer->rqflags &= ~URQ_REQUEST;
635 xfer->nframes = 0;
636 }
637
638 void
639 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
640 usbd_private_handle priv, u_int32_t timeout,
641 usb_device_request_t *req, void *buffer,
642 u_int32_t length, u_int16_t flags,
643 usbd_callback callback)
644 {
645 USB_KASSERT(xfer->pipe == dev->default_pipe); /* XXX */
646 xfer->priv = priv;
647 xfer->buffer = buffer;
648 xfer->length = length;
649 xfer->actlen = 0;
650 xfer->flags = flags;
651 xfer->timeout = timeout;
652 xfer->status = USBD_NOT_STARTED;
653 xfer->callback = callback;
654 xfer->request = *req;
655 xfer->rqflags |= URQ_REQUEST;
656 xfer->nframes = 0;
657 }
658
659 void
660 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
661 usbd_private_handle priv, u_int16_t *frlengths,
662 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
663 {
664 int i;
665
666 USB_KASSERT(xfer->pipe == pipe); /* XXX pipe arg should go */
667 xfer->priv = priv;
668 xfer->buffer = 0;
669 xfer->length = 0;
670 for (i = 0; i < nframes; i++)
671 xfer->length += frlengths[i];
672 xfer->actlen = 0;
673 xfer->flags = flags;
674 xfer->timeout = USBD_NO_TIMEOUT;
675 xfer->status = USBD_NOT_STARTED;
676 xfer->callback = callback;
677 xfer->rqflags &= ~URQ_REQUEST;
678 xfer->frlengths = frlengths;
679 xfer->nframes = nframes;
680 }
681
682 void
683 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
684 void **buffer, u_int32_t *count, usbd_status *status)
685 {
686 if (priv != NULL)
687 *priv = xfer->priv;
688 if (buffer != NULL)
689 *buffer = xfer->buffer;
690 if (count != NULL)
691 *count = xfer->actlen;
692 if (status != NULL)
693 *status = xfer->status;
694 }
695
696 usb_config_descriptor_t *
697 usbd_get_config_descriptor(usbd_device_handle dev)
698 {
699 #ifdef DIAGNOSTIC
700 if (dev == NULL) {
701 printf("usbd_get_config_descriptor: dev == NULL\n");
702 return (NULL);
703 }
704 #endif
705 return (dev->cdesc);
706 }
707
708 int
709 usbd_get_speed(usbd_device_handle dev)
710 {
711 return (dev->speed);
712 }
713
714 usb_interface_descriptor_t *
715 usbd_get_interface_descriptor(usbd_interface_handle iface)
716 {
717 #ifdef DIAGNOSTIC
718 if (iface == NULL) {
719 printf("usbd_get_interface_descriptor: dev == NULL\n");
720 return (NULL);
721 }
722 #endif
723 return (iface->idesc);
724 }
725
726 usb_device_descriptor_t *
727 usbd_get_device_descriptor(usbd_device_handle dev)
728 {
729 return (&dev->ddesc);
730 }
731
732 usb_endpoint_descriptor_t *
733 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
734 {
735 if (index >= iface->idesc->bNumEndpoints)
736 return (0);
737 return (iface->endpoints[index].edesc);
738 }
739
740 usbd_status
741 usbd_abort_default_pipe(usbd_device_handle dev)
742 {
743 return (usbd_abort_pipe(dev->default_pipe));
744 }
745
746 usbd_status
747 usbd_abort_pipe(usbd_pipe_handle pipe)
748 {
749 usbd_status err;
750 int s;
751
752 #ifdef DIAGNOSTIC
753 if (pipe == NULL) {
754 printf("usbd_abort_pipe: pipe==NULL\n");
755 return (USBD_NORMAL_COMPLETION);
756 }
757 #endif
758 s = splusb();
759 err = usbd_ar_pipe(pipe);
760 splx(s);
761 return (err);
762 }
763
764 usbd_status
765 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
766 {
767 usbd_device_handle dev = pipe->device;
768 usb_device_request_t req;
769 usbd_status err;
770
771 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
772
773 /*
774 * Clearing en endpoint stall resets the endpoint toggle, so
775 * do the same to the HC toggle.
776 */
777 pipe->methods->cleartoggle(pipe);
778
779 req.bmRequestType = UT_WRITE_ENDPOINT;
780 req.bRequest = UR_CLEAR_FEATURE;
781 USETW(req.wValue, UF_ENDPOINT_HALT);
782 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
783 USETW(req.wLength, 0);
784 err = usbd_do_request(dev, &req, 0);
785 #if 0
786 XXX should we do this?
787 if (!err) {
788 pipe->state = USBD_PIPE_ACTIVE;
789 /* XXX activate pipe */
790 }
791 #endif
792 return (err);
793 }
794
795 usbd_status
796 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
797 {
798 usbd_device_handle dev = pipe->device;
799 usb_device_request_t req;
800 usbd_status err;
801
802 pipe->methods->cleartoggle(pipe);
803
804 req.bmRequestType = UT_WRITE_ENDPOINT;
805 req.bRequest = UR_CLEAR_FEATURE;
806 USETW(req.wValue, UF_ENDPOINT_HALT);
807 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
808 USETW(req.wLength, 0);
809 err = usbd_do_request_async(dev, &req, 0);
810 return (err);
811 }
812
813 void
814 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
815 {
816 pipe->methods->cleartoggle(pipe);
817 }
818
819 usbd_status
820 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
821 {
822 #ifdef DIAGNOSTIC
823 if (iface == NULL || iface->idesc == NULL) {
824 printf("usbd_endpoint_count: NULL pointer\n");
825 return (USBD_INVAL);
826 }
827 #endif
828 *count = iface->idesc->bNumEndpoints;
829 return (USBD_NORMAL_COMPLETION);
830 }
831
832 usbd_status
833 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
834 {
835 if (dev->cdesc == NULL)
836 return (USBD_NOT_CONFIGURED);
837 *count = dev->cdesc->bNumInterface;
838 return (USBD_NORMAL_COMPLETION);
839 }
840
841 void
842 usbd_interface2device_handle(usbd_interface_handle iface,
843 usbd_device_handle *dev)
844 {
845 *dev = iface->device;
846 }
847
848 usbd_status
849 usbd_device2interface_handle(usbd_device_handle dev,
850 u_int8_t ifaceno, usbd_interface_handle *iface)
851 {
852 if (dev->cdesc == NULL)
853 return (USBD_NOT_CONFIGURED);
854 if (ifaceno >= dev->cdesc->bNumInterface)
855 return (USBD_INVAL);
856 *iface = &dev->ifaces[ifaceno];
857 return (USBD_NORMAL_COMPLETION);
858 }
859
860 usbd_device_handle
861 usbd_pipe2device_handle(usbd_pipe_handle pipe)
862 {
863 return (pipe->device);
864 }
865
866 /* XXXX use altno */
867 usbd_status
868 usbd_set_interface(usbd_interface_handle iface, int altidx)
869 {
870 usb_device_request_t req;
871 usbd_status err;
872 void *endpoints;
873
874 if (LIST_FIRST(&iface->pipes) != 0)
875 return (USBD_IN_USE);
876
877 endpoints = iface->endpoints;
878 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
879 if (err)
880 return (err);
881
882 /* new setting works, we can free old endpoints */
883 if (endpoints != NULL)
884 free(endpoints, M_USB);
885
886 #ifdef DIAGNOSTIC
887 if (iface->idesc == NULL) {
888 printf("usbd_set_interface: NULL pointer\n");
889 return (USBD_INVAL);
890 }
891 #endif
892
893 req.bmRequestType = UT_WRITE_INTERFACE;
894 req.bRequest = UR_SET_INTERFACE;
895 USETW(req.wValue, iface->idesc->bAlternateSetting);
896 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
897 USETW(req.wLength, 0);
898 return (usbd_do_request(iface->device, &req, 0));
899 }
900
901 int
902 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
903 {
904 char *p = (char *)cdesc;
905 char *end = p + UGETW(cdesc->wTotalLength);
906 usb_interface_descriptor_t *d;
907 int n;
908
909 for (n = 0; p < end; p += d->bLength) {
910 d = (usb_interface_descriptor_t *)p;
911 if (p + d->bLength <= end &&
912 d->bDescriptorType == UDESC_INTERFACE &&
913 d->bInterfaceNumber == ifaceno)
914 n++;
915 }
916 return (n);
917 }
918
919 int
920 usbd_get_interface_altindex(usbd_interface_handle iface)
921 {
922 return (iface->altindex);
923 }
924
925 usbd_status
926 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
927 {
928 usb_device_request_t req;
929
930 req.bmRequestType = UT_READ_INTERFACE;
931 req.bRequest = UR_GET_INTERFACE;
932 USETW(req.wValue, 0);
933 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
934 USETW(req.wLength, 1);
935 return (usbd_do_request(iface->device, &req, aiface));
936 }
937
938 /*** Internal routines ***/
939
940 /* Dequeue all pipe operations, called at splusb(). */
941 Static usbd_status
942 usbd_ar_pipe(usbd_pipe_handle pipe)
943 {
944 usbd_xfer_handle xfer;
945
946 SPLUSBCHECK;
947
948 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
949 #ifdef USB_DEBUG
950 if (usbdebug > 5)
951 usbd_dump_queue(pipe);
952 #endif
953 pipe->repeat = 0;
954 pipe->aborting = 1;
955 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
956 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
957 pipe, xfer, pipe->methods));
958 /* Make the HC abort it (and invoke the callback). */
959 pipe->methods->abort(xfer);
960 USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
961 ("usbd_ar_pipe"));
962 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
963 }
964 pipe->aborting = 0;
965 return (USBD_NORMAL_COMPLETION);
966 }
967
968 /* Called at splusb() */
969 void
970 usb_transfer_complete(usbd_xfer_handle xfer)
971 {
972 usbd_pipe_handle pipe = xfer->pipe;
973 int sync = xfer->flags & USBD_SYNCHRONOUS;
974 int erred = xfer->status == USBD_CANCELLED ||
975 xfer->status == USBD_TIMEOUT;
976 int repeat, polling;
977
978 SPLUSBCHECK;
979
980 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
981 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
982 #ifdef DIAGNOSTIC
983 if (xfer->busy_free != XFER_ONQU) {
984 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
985 xfer, xfer->busy_free);
986 }
987 #endif
988
989 #ifdef DIAGNOSTIC
990 if (pipe == NULL) {
991 printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
992 return;
993 }
994 #endif
995 repeat = pipe->repeat;
996 polling = pipe->device->bus->use_polling;
997 /* XXXX */
998 if (polling)
999 pipe->running = 0;
1000
1001 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
1002 usbd_xfer_isread(xfer)) {
1003 #ifdef DIAGNOSTIC
1004 if (xfer->actlen > xfer->length) {
1005 printf("usb_transfer_complete: actlen > len %d > %d\n",
1006 xfer->actlen, xfer->length);
1007 xfer->actlen = xfer->length;
1008 }
1009 #endif
1010 /* Copy data if it is not already in the correct buffer. */
1011 if (!(xfer->flags & USBD_NO_COPY) && xfer->hcbuffer != NULL) {
1012 DPRINTFN(5, ("usb_transfer_complete: copy %p (buffer) <- %p (alloc), %u bytes\n",
1013 xfer->buffer, xfer->hcbuffer, (unsigned)xfer->actlen));
1014 #ifdef DIAGNOSTIC
1015 if ((xfer->rqflags & (URQ_AUTO_BUF | URQ_DEV_BUF)) == 0)
1016 panic("usb_transfer_complete: USBD_NO_COPY required with mapped buffer");
1017 #endif
1018 memcpy(xfer->buffer, xfer->hcbuffer, xfer->actlen);
1019 }
1020 }
1021
1022 /*
1023 * if we allocated or mapped the buffer in usbd_transfer()
1024 * we unmap it here.
1025 */
1026 if (xfer->rqflags & URQ_AUTO_BUF) {
1027 if (!repeat) {
1028 struct usbd_bus *bus = pipe->device->bus;
1029 bus->methods->freem(bus, xfer, U_NOWAIT);
1030 xfer->rqflags &= ~URQ_AUTO_BUF;
1031 }
1032 }
1033
1034 if (!repeat) {
1035 /* Remove request from queue. */
1036 #ifdef DIAGNOSTIC
1037 xfer->busy_free = XFER_BUSY;
1038 #endif
1039 USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) == xfer,
1040 ("usb_transfer_complete: bad dequeue"));
1041 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
1042 }
1043 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
1044 repeat, SIMPLEQ_FIRST(&pipe->queue)));
1045
1046 /* Count completed transfers. */
1047 ++pipe->device->bus->stats.uds_requests
1048 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
1049
1050 xfer->done = 1;
1051 if (!xfer->status && xfer->actlen < xfer->length &&
1052 !(xfer->flags & USBD_SHORT_XFER_OK)) {
1053 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
1054 xfer->actlen, xfer->length));
1055 xfer->status = USBD_SHORT_XFER;
1056 }
1057
1058 /*
1059 * For repeat operations, call the callback first, as the xfer
1060 * will not go away and the "done" method may modify it. Otherwise
1061 * reverse the order in case the callback wants to free or reuse
1062 * the xfer.
1063 */
1064 if (repeat) {
1065 if (xfer->callback)
1066 xfer->callback(xfer, xfer->priv, xfer->status);
1067 pipe->methods->done(xfer);
1068 } else {
1069 pipe->methods->done(xfer);
1070 if (xfer->callback) {
1071 if (xfer->flags & USBD_CALLBACK_AS_TASK) {
1072 /*
1073 * Callback as a task (in thread context).
1074 */
1075 usb_init_task(&xfer->task, usbd_callback_task,
1076 xfer);
1077 usb_add_task(xfer->device, &xfer->task,
1078 USB_TASKQ_DRIVER);
1079 } else
1080 xfer->callback(xfer, xfer->priv, xfer->status);
1081 }
1082 }
1083
1084 if (sync && !polling)
1085 wakeup(xfer);
1086
1087 if (!repeat) {
1088 /* XXX should we stop the queue on all errors? */
1089 if (erred && pipe->iface != NULL) /* not control pipe */
1090 pipe->running = 0;
1091 else
1092 usbd_start_next(pipe);
1093 }
1094 }
1095
1096 Static void
1097 usbd_callback_task(void *arg)
1098 {
1099 usbd_xfer_handle xfer = arg;
1100
1101 xfer->callback(xfer, xfer->priv, xfer->status);
1102 }
1103
1104 usbd_status
1105 usb_insert_transfer(usbd_xfer_handle xfer)
1106 {
1107 usbd_pipe_handle pipe = xfer->pipe;
1108 usbd_status err;
1109 int s;
1110
1111 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
1112 pipe, pipe->running, xfer->timeout));
1113 #ifdef DIAGNOSTIC
1114 if (xfer->busy_free != XFER_BUSY) {
1115 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
1116 xfer, xfer->busy_free);
1117 return (USBD_INVAL);
1118 }
1119 xfer->busy_free = XFER_ONQU;
1120 #endif
1121 s = splusb();
1122 USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
1123 ("usb_insert_transfer"));
1124 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
1125 if (pipe->running)
1126 err = USBD_IN_PROGRESS;
1127 else {
1128 pipe->running = 1;
1129 err = USBD_NORMAL_COMPLETION;
1130 }
1131 splx(s);
1132 return (err);
1133 }
1134
1135 /* Called at splusb() */
1136 void
1137 usbd_start_next(usbd_pipe_handle pipe)
1138 {
1139 usbd_xfer_handle xfer;
1140 usbd_status err;
1141
1142 SPLUSBCHECK;
1143
1144 #ifdef DIAGNOSTIC
1145 if (pipe == NULL) {
1146 printf("usbd_start_next: pipe == NULL\n");
1147 return;
1148 }
1149 if (pipe->methods == NULL || pipe->methods->start == NULL) {
1150 printf("usbd_start_next: pipe=%p no start method\n", pipe);
1151 return;
1152 }
1153 #endif
1154
1155 /* Get next request in queue. */
1156 xfer = SIMPLEQ_FIRST(&pipe->queue);
1157 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
1158 if (xfer == NULL) {
1159 pipe->running = 0;
1160 } else {
1161 err = pipe->methods->start(xfer);
1162 if (err != USBD_IN_PROGRESS) {
1163 printf("usbd_start_next: error=%d\n", err);
1164 pipe->running = 0;
1165 /* XXX do what? */
1166 }
1167 }
1168 }
1169
1170 usbd_status
1171 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
1172 {
1173 return (usbd_do_request_flags(dev, req, data, 0, 0,
1174 USBD_DEFAULT_TIMEOUT));
1175 }
1176
1177 usbd_status
1178 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
1179 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
1180 {
1181 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
1182 data, flags, actlen, timo));
1183 }
1184
1185 usbd_status
1186 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
1187 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
1188 u_int32_t timeout)
1189 {
1190 usbd_xfer_handle xfer;
1191 usbd_status err;
1192
1193 #ifdef DIAGNOSTIC
1194 #if defined(__i386__) && defined(__FreeBSD__)
1195 KASSERT(curthread->td_intr_nesting_level == 0,
1196 ("usbd_do_request: in interrupt context"));
1197 #endif
1198 if (dev->bus->intr_context) {
1199 panic("usbd_do_request: not in process context");
1200 }
1201 #endif
1202
1203 xfer = usbd_alloc_xfer(dev, pipe);
1204 if (xfer == NULL)
1205 return (USBD_NOMEM);
1206 xfer->pipe = dev->default_pipe; /* XXX */
1207 /* XXX */
1208 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
1209 data, UGETW(req->wLength), flags, 0);
1210 /* XXX */
1211 xfer->pipe = pipe;
1212 err = usbd_sync_transfer(xfer);
1213 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1214 if (xfer->actlen > xfer->length) {
1215 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1216 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1217 dev->address, xfer->request.bmRequestType,
1218 xfer->request.bRequest, UGETW(xfer->request.wValue),
1219 UGETW(xfer->request.wIndex),
1220 UGETW(xfer->request.wLength),
1221 xfer->length, xfer->actlen));
1222 }
1223 #endif
1224 if (actlen != NULL)
1225 *actlen = xfer->actlen;
1226 if (err == USBD_STALLED) {
1227 /*
1228 * The control endpoint has stalled. Control endpoints
1229 * should not halt, but some may do so anyway so clear
1230 * any halt condition.
1231 */
1232 usb_device_request_t treq;
1233 usb_status_t status;
1234 u_int16_t s;
1235 usbd_status nerr;
1236
1237 treq.bmRequestType = UT_READ_ENDPOINT;
1238 treq.bRequest = UR_GET_STATUS;
1239 USETW(treq.wValue, 0);
1240 USETW(treq.wIndex, 0);
1241 USETW(treq.wLength, sizeof(usb_status_t));
1242 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1243 &treq, &status,sizeof(usb_status_t),
1244 0, 0);
1245 nerr = usbd_sync_transfer(xfer);
1246 if (nerr)
1247 goto bad;
1248 s = UGETW(status.wStatus);
1249 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1250 if (!(s & UES_HALT))
1251 goto bad;
1252 treq.bmRequestType = UT_WRITE_ENDPOINT;
1253 treq.bRequest = UR_CLEAR_FEATURE;
1254 USETW(treq.wValue, UF_ENDPOINT_HALT);
1255 USETW(treq.wIndex, 0);
1256 USETW(treq.wLength, 0);
1257 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1258 &treq, &status, 0, 0, 0);
1259 nerr = usbd_sync_transfer(xfer);
1260 if (nerr)
1261 goto bad;
1262 }
1263
1264 bad:
1265 usbd_free_xfer(xfer);
1266 return (err);
1267 }
1268
1269 void
1270 usbd_do_request_async_cb(usbd_xfer_handle xfer,
1271 usbd_private_handle priv, usbd_status status)
1272 {
1273 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1274 if (xfer->actlen > xfer->length) {
1275 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1276 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1277 xfer->pipe->device->address,
1278 xfer->request.bmRequestType,
1279 xfer->request.bRequest, UGETW(xfer->request.wValue),
1280 UGETW(xfer->request.wIndex),
1281 UGETW(xfer->request.wLength),
1282 xfer->length, xfer->actlen));
1283 }
1284 #endif
1285 usbd_free_xfer_flag(xfer, U_NOWAIT);
1286 }
1287
1288 /*
1289 * Execute a request without waiting for completion.
1290 * Can be used from interrupt context.
1291 */
1292 usbd_status
1293 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1294 void *data)
1295 {
1296 usbd_xfer_handle xfer;
1297
1298 xfer = usbd_alloc_xfer_flag(dev, dev->default_pipe, U_NOWAIT);
1299 if (xfer == NULL)
1300 return (USBD_NOMEM);
1301 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1302 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1303
1304 /*
1305 * Request will be processed later as a task (in thread context).
1306 */
1307 usb_init_task(&xfer->task, usbd_do_request_async_task, xfer);
1308 usb_add_task(dev, &xfer->task, USB_TASKQ_HC);
1309
1310 return (USBD_IN_PROGRESS);
1311 }
1312
1313 Static void
1314 usbd_do_request_async_task(void *arg)
1315 {
1316 usbd_xfer_handle xfer = arg;
1317 usbd_status err;
1318
1319 /* XXX impossible to notify caller the error */
1320 err = usbd_transfer(xfer);
1321 if (err != USBD_IN_PROGRESS && err) {
1322 usbd_free_xfer(xfer);
1323 }
1324 }
1325
1326 const struct usbd_quirks *
1327 usbd_get_quirks(usbd_device_handle dev)
1328 {
1329 #ifdef DIAGNOSTIC
1330 if (dev == NULL) {
1331 printf("usbd_get_quirks: dev == NULL\n");
1332 return 0;
1333 }
1334 #endif
1335 return (dev->quirks);
1336 }
1337
1338 /* XXX do periodic free() of free list */
1339
1340 /*
1341 * Called from keyboard driver when in polling mode.
1342 */
1343 void
1344 usbd_dopoll(usbd_interface_handle iface)
1345 {
1346 iface->device->bus->methods->do_poll(iface->device->bus);
1347 }
1348
1349 void
1350 usbd_set_polling(usbd_device_handle dev, int on)
1351 {
1352 if (on)
1353 dev->bus->use_polling++;
1354 else
1355 dev->bus->use_polling--;
1356 /* When polling we need to make sure there is nothing pending to do. */
1357 if (dev->bus->use_polling)
1358 dev->bus->methods->soft_intr(dev->bus);
1359 }
1360
1361
1362 usb_endpoint_descriptor_t *
1363 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1364 {
1365 struct usbd_endpoint *ep;
1366 int i;
1367
1368 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1369 ep = &iface->endpoints[i];
1370 if (ep->edesc->bEndpointAddress == address)
1371 return (iface->endpoints[i].edesc);
1372 }
1373 return (0);
1374 }
1375
1376 /*
1377 * usbd_ratecheck() can limit the number of error messages that occurs.
1378 * When a device is unplugged it may take up to 0.25s for the hub driver
1379 * to notice it. If the driver continuosly tries to do I/O operations
1380 * this can generate a large number of messages.
1381 */
1382 int
1383 usbd_ratecheck(struct timeval *last)
1384 {
1385 #if defined(__NetBSD__)
1386 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1387
1388 return (ratecheck(last, &errinterval));
1389 #elif defined(__FreeBSD__)
1390 if (last->tv_sec == time_second)
1391 return (0);
1392 last->tv_sec = time_second;
1393 return (1);
1394 #endif
1395 }
1396
1397 /*
1398 * Search for a vendor/product pair in an array. The item size is
1399 * given as an argument.
1400 */
1401 const struct usb_devno *
1402 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1403 u_int16_t vendor, u_int16_t product)
1404 {
1405 while (nentries-- > 0) {
1406 u_int16_t tproduct = tbl->ud_product;
1407 if (tbl->ud_vendor == vendor &&
1408 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1409 return (tbl);
1410 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1411 }
1412 return (NULL);
1413 }
1414
1415
1416 void
1417 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1418 {
1419 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1420
1421 iter->cur = (const uByte *)cd;
1422 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1423 }
1424
1425 const usb_descriptor_t *
1426 usb_desc_iter_next(usbd_desc_iter_t *iter)
1427 {
1428 const usb_descriptor_t *desc;
1429
1430 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1431 if (iter->cur != iter->end)
1432 printf("usb_desc_iter_next: bad descriptor\n");
1433 return NULL;
1434 }
1435 desc = (const usb_descriptor_t *)iter->cur;
1436 if (desc->bLength == 0) {
1437 printf("usb_desc_iter_next: descriptor length = 0\n");
1438 return NULL;
1439 }
1440 iter->cur += desc->bLength;
1441 if (iter->cur > iter->end) {
1442 printf("usb_desc_iter_next: descriptor length too large\n");
1443 return NULL;
1444 }
1445 return desc;
1446 }
1447
1448 usbd_status
1449 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1450 {
1451 return usbd_get_string0(dev, si, buf, 1);
1452 }
1453
1454 usbd_status
1455 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
1456 {
1457 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1458 usb_string_descriptor_t us;
1459 char *s;
1460 int i, n;
1461 u_int16_t c;
1462 usbd_status err;
1463 int size;
1464
1465 buf[0] = '\0';
1466 if (si == 0)
1467 return (USBD_INVAL);
1468 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1469 return (USBD_STALLED);
1470 if (dev->langid == USBD_NOLANG) {
1471 /* Set up default language */
1472 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1473 &size);
1474 if (err || size < 4) {
1475 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1476 dev->langid = 0; /* Well, just pick something then */
1477 } else {
1478 /* Pick the first language as the default. */
1479 dev->langid = UGETW(us.bString[0]);
1480 }
1481 }
1482 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1483 if (err)
1484 return (err);
1485 s = buf;
1486 n = size / 2 - 1;
1487 if (unicode) {
1488 for (i = 0; i < n; i++) {
1489 c = UGETW(us.bString[i]);
1490 if (swap)
1491 c = (c >> 8) | (c << 8);
1492 s += wput_utf8(s, 3, c);
1493 }
1494 *s++ = 0;
1495 }
1496 #if defined(__NetBSD__) && defined(COMPAT_30)
1497 else {
1498 for (i = 0; i < n; i++) {
1499 c = UGETW(us.bString[i]);
1500 if (swap)
1501 c = (c >> 8) | (c << 8);
1502 *s++ = (c < 0x80) ? c : '?';
1503 }
1504 *s++ = 0;
1505 }
1506 #endif
1507 return (USBD_NORMAL_COMPLETION);
1508 }
1509
1510 #if defined(__FreeBSD__)
1511 int
1512 usbd_driver_load(module_t mod, int what, void *arg)
1513 {
1514 /* XXX should implement something like a function that removes all generic devices */
1515
1516 return (0);
1517 }
1518
1519 #endif
1520