usbdi.c revision 1.75 1 /* $NetBSD: usbdi.c,v 1.75 2000/06/01 15:51:27 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.28 1999/11/17 22:33:49 n_hibma 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/param.h>
42 #include <sys/systm.h>
43 #if defined(__NetBSD__) || defined(__OpenBSD__)
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #elif defined(__FreeBSD__)
47 #include <sys/module.h>
48 #include <sys/bus.h>
49 #include <sys/conf.h>
50 #include "usb_if.h"
51 #if defined(DIAGNOSTIC) && defined(__i386__)
52 #include <machine/cpu.h>
53 #endif
54 #endif
55 #include <sys/malloc.h>
56 #include <sys/proc.h>
57
58 #include <machine/bus.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
64 #include <dev/usb/usb_mem.h>
65
66 #if defined(__FreeBSD__)
67 #include "usb_if.h"
68 #endif
69
70 #ifdef USB_DEBUG
71 #define DPRINTF(x) if (usbdebug) logprintf x
72 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
73 extern int usbdebug;
74 #else
75 #define DPRINTF(x)
76 #define DPRINTFN(n,x)
77 #endif
78
79 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
80 Static void usbd_do_request_async_cb
81 (usbd_xfer_handle, usbd_private_handle, usbd_status);
82 Static void usbd_start_next(usbd_pipe_handle pipe);
83 Static usbd_status usbd_open_pipe_ival
84 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
85
86 Static int usbd_nbuses = 0;
87
88 void
89 usbd_init(void)
90 {
91 usbd_nbuses++;
92 }
93
94 void
95 usbd_finish(void)
96 {
97 --usbd_nbuses;
98 }
99
100 static __inline int
101 usbd_xfer_isread(usbd_xfer_handle xfer)
102 {
103 if (xfer->rqflags & URQ_REQUEST)
104 return (xfer->request.bmRequestType & UT_READ);
105 else
106 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
107 UE_DIR_IN);
108 }
109
110 #ifdef USB_DEBUG
111 void usbd_dump_queue(usbd_pipe_handle pipe);
112 void
113 usbd_dump_queue(usbd_pipe_handle pipe)
114 {
115 usbd_xfer_handle xfer;
116
117 printf("usbd_dump_queue: pipe=%p\n", pipe);
118 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
119 xfer;
120 xfer = SIMPLEQ_NEXT(xfer, next)) {
121 printf(" xfer=%p\n", xfer);
122 }
123 }
124 #endif
125
126 usbd_status
127 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
128 u_int8_t flags, usbd_pipe_handle *pipe)
129 {
130 return (usbd_open_pipe_ival(iface, address, flags, pipe,
131 USBD_DEFAULT_INTERVAL));
132 }
133
134 usbd_status
135 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
136 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
137 {
138 usbd_pipe_handle p;
139 struct usbd_endpoint *ep;
140 usbd_status err;
141 int i;
142
143 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
144 iface, address, flags));
145
146 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
147 ep = &iface->endpoints[i];
148 if (ep->edesc == NULL)
149 return (USBD_IOERROR);
150 if (ep->edesc->bEndpointAddress == address)
151 goto found;
152 }
153 return (USBD_BAD_ADDRESS);
154 found:
155 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
156 return (USBD_IN_USE);
157 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
158 if (err)
159 return (err);
160 LIST_INSERT_HEAD(&iface->pipes, p, next);
161 *pipe = p;
162 return (USBD_NORMAL_COMPLETION);
163 }
164
165 usbd_status
166 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
167 u_int8_t flags, usbd_pipe_handle *pipe,
168 usbd_private_handle priv, void *buffer, u_int32_t len,
169 usbd_callback cb, int ival)
170 {
171 usbd_status err;
172 usbd_xfer_handle xfer;
173 usbd_pipe_handle ipipe;
174
175 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
176 address, flags, len));
177
178 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
179 &ipipe, ival);
180 if (err)
181 return (err);
182 xfer = usbd_alloc_xfer(iface->device);
183 if (xfer == NULL) {
184 err = USBD_NOMEM;
185 goto bad1;
186 }
187 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
188 USBD_NO_TIMEOUT, cb);
189 ipipe->intrxfer = xfer;
190 ipipe->repeat = 1;
191 err = usbd_transfer(xfer);
192 *pipe = ipipe;
193 if (err != USBD_IN_PROGRESS)
194 goto bad2;
195 return (USBD_NORMAL_COMPLETION);
196
197 bad2:
198 ipipe->intrxfer = NULL;
199 ipipe->repeat = 0;
200 usbd_free_xfer(xfer);
201 bad1:
202 usbd_close_pipe(ipipe);
203 return (err);
204 }
205
206 usbd_status
207 usbd_close_pipe(usbd_pipe_handle pipe)
208 {
209 #ifdef DIAGNOSTIC
210 if (pipe == NULL) {
211 printf("usbd_close_pipe: pipe==NULL\n");
212 return (USBD_NORMAL_COMPLETION);
213 }
214 #endif
215
216 if (--pipe->refcnt != 0)
217 return (USBD_NORMAL_COMPLETION);
218 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
219 return (USBD_PENDING_REQUESTS);
220 LIST_REMOVE(pipe, next);
221 pipe->endpoint->refcnt--;
222 pipe->methods->close(pipe);
223 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
224 if (callout_pending(&pipe->abort_handle)) {
225 callout_stop(&pipe->abort_handle);
226 printf("usbd_close_pipe: abort_handle pending");
227 }
228 #endif
229 if (pipe->intrxfer != NULL)
230 usbd_free_xfer(pipe->intrxfer);
231 free(pipe, M_USB);
232 return (USBD_NORMAL_COMPLETION);
233 }
234
235 usbd_status
236 usbd_transfer(usbd_xfer_handle xfer)
237 {
238 usbd_pipe_handle pipe = xfer->pipe;
239 usb_dma_t *dmap = &xfer->dmabuf;
240 usbd_status err;
241 u_int size;
242 int s;
243
244 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
245 xfer, xfer->flags, pipe, pipe->running));
246 #ifdef USB_DEBUG
247 if (usbdebug > 5)
248 usbd_dump_queue(pipe);
249 #endif
250 xfer->done = 0;
251
252 if (pipe->aborting)
253 return (USBD_CANCELLED);
254
255 size = xfer->length;
256 /* If there is no buffer, allocate one. */
257 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
258 struct usbd_bus *bus = pipe->device->bus;
259
260 #ifdef DIAGNOSTIC
261 if (xfer->rqflags & URQ_AUTO_DMABUF)
262 printf("usbd_transfer: has old buffer!\n");
263 #endif
264 err = bus->methods->allocm(bus, dmap, size);
265 if (err)
266 return (err);
267 xfer->rqflags |= URQ_AUTO_DMABUF;
268 }
269
270 /* Copy data if going out. */
271 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
272 !usbd_xfer_isread(xfer))
273 memcpy(KERNADDR(dmap), xfer->buffer, size);
274
275 err = pipe->methods->transfer(xfer);
276
277 if (err != USBD_IN_PROGRESS && err) {
278 /* The transfer has not been queued, so free buffer. */
279 if (xfer->rqflags & URQ_AUTO_DMABUF) {
280 struct usbd_bus *bus = pipe->device->bus;
281
282 bus->methods->freem(bus, &xfer->dmabuf);
283 xfer->rqflags &= ~URQ_AUTO_DMABUF;
284 }
285 }
286
287 if (!(xfer->flags & USBD_SYNCHRONOUS))
288 return (err);
289
290 /* Sync transfer, wait for completion. */
291 if (err != USBD_IN_PROGRESS)
292 return (err);
293 s = splusb();
294 if (!xfer->done) {
295 if (pipe->device->bus->use_polling)
296 panic("usbd_transfer: not done\n");
297 /* XXX Temporary hack XXX */
298 if (xfer->flags & USBD_NO_TSLEEP) {
299 int i;
300 usbd_bus_handle bus = pipe->device->bus;
301 int to = xfer->timeout * 1000;
302 for (i = 0; i < to; i += 10) {
303 delay(10);
304 bus->methods->do_poll(bus);
305 if (xfer->done)
306 break;
307 }
308 /* XXX Is this right, what about the HC timeout? */
309 if (!xfer->done) {
310 pipe->methods->abort(xfer);
311 xfer->status = USBD_TIMEOUT;
312 }
313 } else
314 /* XXX End hack XXX */
315 tsleep(xfer, PRIBIO, "usbsyn", 0);
316 }
317 splx(s);
318 return (xfer->status);
319 }
320
321 /* Like usbd_transfer(), but waits for completion. */
322 usbd_status
323 usbd_sync_transfer(usbd_xfer_handle xfer)
324 {
325 xfer->flags |= USBD_SYNCHRONOUS;
326 return (usbd_transfer(xfer));
327 }
328
329 void *
330 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
331 {
332 struct usbd_bus *bus = xfer->device->bus;
333 usbd_status err;
334
335 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
336 if (err)
337 return (0);
338 xfer->rqflags |= URQ_DEV_DMABUF;
339 return (KERNADDR(&xfer->dmabuf));
340 }
341
342 void
343 usbd_free_buffer(usbd_xfer_handle xfer)
344 {
345 #ifdef DIAGNOSTIC
346 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
347 printf("usbd_free_buffer: no buffer\n");
348 return;
349 }
350 #endif
351 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
352 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
353 }
354
355 void *
356 usbd_get_buffer(usbd_xfer_handle xfer)
357 {
358 if (!(xfer->rqflags & URQ_DEV_DMABUF))
359 return (0);
360 return (KERNADDR(&xfer->dmabuf));
361 }
362
363 usbd_xfer_handle
364 usbd_alloc_xfer(usbd_device_handle dev)
365 {
366 usbd_xfer_handle xfer;
367
368 xfer = dev->bus->methods->allocx(dev->bus);
369 if (xfer == NULL)
370 return (NULL);
371 xfer->device = dev;
372 usb_callout_init(xfer->timeout_handle);
373 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
374 return (xfer);
375 }
376
377 usbd_status
378 usbd_free_xfer(usbd_xfer_handle xfer)
379 {
380 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
381 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
382 usbd_free_buffer(xfer);
383 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
384 if (callout_pending(&xfer->timeout_handle)) {
385 callout_stop(&xfer->timeout_handle);
386 printf("usbd_free_xfer: timout_handle pending");
387 }
388 #endif
389 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
390 return (USBD_NORMAL_COMPLETION);
391 }
392
393 void
394 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
395 usbd_private_handle priv, void *buffer, u_int32_t length,
396 u_int16_t flags, u_int32_t timeout,
397 usbd_callback callback)
398 {
399 xfer->pipe = pipe;
400 xfer->priv = priv;
401 xfer->buffer = buffer;
402 xfer->length = length;
403 xfer->actlen = 0;
404 xfer->flags = flags;
405 xfer->timeout = timeout;
406 xfer->status = USBD_NOT_STARTED;
407 xfer->callback = callback;
408 xfer->rqflags &= ~URQ_REQUEST;
409 xfer->nframes = 0;
410 }
411
412 void
413 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
414 usbd_private_handle priv, u_int32_t timeout,
415 usb_device_request_t *req, void *buffer,
416 u_int32_t length, u_int16_t flags,
417 usbd_callback callback)
418 {
419 xfer->pipe = dev->default_pipe;
420 xfer->priv = priv;
421 xfer->buffer = buffer;
422 xfer->length = length;
423 xfer->actlen = 0;
424 xfer->flags = flags;
425 xfer->timeout = timeout;
426 xfer->status = USBD_NOT_STARTED;
427 xfer->callback = callback;
428 xfer->request = *req;
429 xfer->rqflags |= URQ_REQUEST;
430 xfer->nframes = 0;
431 }
432
433 void
434 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
435 usbd_private_handle priv, u_int16_t *frlengths,
436 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
437 {
438 xfer->pipe = pipe;
439 xfer->priv = priv;
440 xfer->buffer = 0;
441 xfer->length = 0;
442 xfer->actlen = 0;
443 xfer->flags = flags;
444 xfer->timeout = USBD_NO_TIMEOUT;
445 xfer->status = USBD_NOT_STARTED;
446 xfer->callback = callback;
447 xfer->rqflags &= ~URQ_REQUEST;
448 xfer->frlengths = frlengths;
449 xfer->nframes = nframes;
450 }
451
452 void
453 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
454 void **buffer, u_int32_t *count, usbd_status *status)
455 {
456 if (priv != NULL)
457 *priv = xfer->priv;
458 if (buffer != NULL)
459 *buffer = xfer->buffer;
460 if (count != NULL)
461 *count = xfer->actlen;
462 if (status != NULL)
463 *status = xfer->status;
464 }
465
466 usb_config_descriptor_t *
467 usbd_get_config_descriptor(usbd_device_handle dev)
468 {
469 #ifdef DIAGNOSTIC
470 if (dev == NULL) {
471 printf("usbd_get_config_descriptor: dev == NULL\n");
472 return (NULL);
473 }
474 #endif
475 return (dev->cdesc);
476 }
477
478 usb_interface_descriptor_t *
479 usbd_get_interface_descriptor(usbd_interface_handle iface)
480 {
481 #ifdef DIAGNOSTIC
482 if (iface == NULL) {
483 printf("usbd_get_interface_descriptor: dev == NULL\n");
484 return (NULL);
485 }
486 #endif
487 return (iface->idesc);
488 }
489
490 usb_device_descriptor_t *
491 usbd_get_device_descriptor(usbd_device_handle dev)
492 {
493 return (&dev->ddesc);
494 }
495
496 usb_endpoint_descriptor_t *
497 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
498 {
499 if (index >= iface->idesc->bNumEndpoints)
500 return (0);
501 return (iface->endpoints[index].edesc);
502 }
503
504 usbd_status
505 usbd_abort_pipe(usbd_pipe_handle pipe)
506 {
507 usbd_status err;
508 int s;
509
510 #ifdef DIAGNOSTIC
511 if (pipe == NULL) {
512 printf("usbd_close_pipe: pipe==NULL\n");
513 return (USBD_NORMAL_COMPLETION);
514 }
515 #endif
516 s = splusb();
517 err = usbd_ar_pipe(pipe);
518 splx(s);
519 return (err);
520 }
521
522 usbd_status
523 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
524 {
525 usbd_device_handle dev = pipe->device;
526 usb_device_request_t req;
527 usbd_status err;
528
529 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
530
531 /*
532 * Clearing en endpoint stall resets the enpoint toggle, so
533 * do the same to the HC toggle.
534 */
535 pipe->methods->cleartoggle(pipe);
536
537 req.bmRequestType = UT_WRITE_ENDPOINT;
538 req.bRequest = UR_CLEAR_FEATURE;
539 USETW(req.wValue, UF_ENDPOINT_HALT);
540 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
541 USETW(req.wLength, 0);
542 err = usbd_do_request(dev, &req, 0);
543 #if 0
544 XXX should we do this?
545 if (!err) {
546 pipe->state = USBD_PIPE_ACTIVE;
547 /* XXX activate pipe */
548 }
549 #endif
550 return (err);
551 }
552
553 usbd_status
554 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
555 {
556 usbd_device_handle dev = pipe->device;
557 usb_device_request_t req;
558 usbd_status err;
559
560 pipe->methods->cleartoggle(pipe);
561
562 req.bmRequestType = UT_WRITE_ENDPOINT;
563 req.bRequest = UR_CLEAR_FEATURE;
564 USETW(req.wValue, UF_ENDPOINT_HALT);
565 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
566 USETW(req.wLength, 0);
567 err = usbd_do_request_async(dev, &req, 0);
568 return (err);
569 }
570
571 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
572 void
573 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
574 {
575 pipe->methods->cleartoggle(pipe);
576 }
577
578 usbd_status
579 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
580 {
581 #ifdef DIAGNOSTIC
582 if (iface == NULL || iface->idesc == NULL) {
583 printf("usbd_endpoint_count: NULL pointer\n");
584 return (USBD_INVAL);
585 }
586 #endif
587 *count = iface->idesc->bNumEndpoints;
588 return (USBD_NORMAL_COMPLETION);
589 }
590
591 usbd_status
592 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
593 {
594 if (dev->cdesc == NULL)
595 return (USBD_NOT_CONFIGURED);
596 *count = dev->cdesc->bNumInterface;
597 return (USBD_NORMAL_COMPLETION);
598 }
599
600 usbd_status
601 usbd_interface2device_handle(usbd_interface_handle iface,
602 usbd_device_handle *dev)
603 {
604 *dev = iface->device;
605 return (USBD_NORMAL_COMPLETION);
606 }
607
608 usbd_status
609 usbd_device2interface_handle(usbd_device_handle dev,
610 u_int8_t ifaceno, usbd_interface_handle *iface)
611 {
612 if (dev->cdesc == NULL)
613 return (USBD_NOT_CONFIGURED);
614 if (ifaceno >= dev->cdesc->bNumInterface)
615 return (USBD_INVAL);
616 *iface = &dev->ifaces[ifaceno];
617 return (USBD_NORMAL_COMPLETION);
618 }
619
620 usbd_device_handle
621 usbd_pipe2device_handle(usbd_pipe_handle pipe)
622 {
623 return (pipe->device);
624 }
625
626 /* XXXX use altno */
627 usbd_status
628 usbd_set_interface(usbd_interface_handle iface, int altidx)
629 {
630 usb_device_request_t req;
631 usbd_status err;
632 void *endpoints;
633
634 if (LIST_FIRST(&iface->pipes) != 0)
635 return (USBD_IN_USE);
636
637 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
638 if (err)
639 return (err);
640
641 /* new setting work, we can free old endpoints */
642 if (endpoints != NULL)
643 free(endpoints, M_USB);
644
645 #ifdef DIAGNOSTIC
646 if (iface->idesc == NULL) {
647 printf("usbd_set_interface: NULL pointer\n");
648 return (USBD_INVAL);
649 }
650 #endif
651
652 req.bmRequestType = UT_WRITE_INTERFACE;
653 req.bRequest = UR_SET_INTERFACE;
654 USETW(req.wValue, iface->idesc->bAlternateSetting);
655 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
656 USETW(req.wLength, 0);
657 return (usbd_do_request(iface->device, &req, 0));
658 }
659
660 int
661 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
662 {
663 char *p = (char *)cdesc;
664 char *end = p + UGETW(cdesc->wTotalLength);
665 usb_interface_descriptor_t *d;
666 int n;
667
668 for (n = 0; p < end; p += d->bLength) {
669 d = (usb_interface_descriptor_t *)p;
670 if (p + d->bLength <= end &&
671 d->bDescriptorType == UDESC_INTERFACE &&
672 d->bInterfaceNumber == ifaceno)
673 n++;
674 }
675 return (n);
676 }
677
678 int
679 usbd_get_interface_altindex(usbd_interface_handle iface)
680 {
681 return (iface->altindex);
682 }
683
684 usbd_status
685 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
686 {
687 usb_device_request_t req;
688
689 req.bmRequestType = UT_READ_INTERFACE;
690 req.bRequest = UR_GET_INTERFACE;
691 USETW(req.wValue, 0);
692 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
693 USETW(req.wLength, 1);
694 return (usbd_do_request(iface->device, &req, aiface));
695 }
696
697 /*** Internal routines ***/
698
699 /* Dequeue all pipe operations, called at splusb(). */
700 Static usbd_status
701 usbd_ar_pipe(usbd_pipe_handle pipe)
702 {
703 usbd_xfer_handle xfer;
704
705 SPLUSBCHECK;
706
707 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
708 #ifdef USB_DEBUG
709 if (usbdebug > 5)
710 usbd_dump_queue(pipe);
711 #endif
712 pipe->repeat = 0;
713 pipe->aborting = 1;
714 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
715 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
716 pipe, xfer, pipe->methods));
717 /* Make the HC abort it (and invoke the callback). */
718 pipe->methods->abort(xfer);
719 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
720 }
721 pipe->aborting = 0;
722 return (USBD_NORMAL_COMPLETION);
723 }
724
725 /* Called at splusb() */
726 void
727 usb_transfer_complete(usbd_xfer_handle xfer)
728 {
729 usbd_pipe_handle pipe = xfer->pipe;
730 usb_dma_t *dmap = &xfer->dmabuf;
731 int repeat = pipe->repeat;
732 int polling;
733
734 SPLUSBCHECK;
735
736 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
737 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
738
739 #ifdef DIAGNOSTIC
740 if (pipe == NULL) {
741 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
742 return;
743 }
744 #endif
745 polling = pipe->device->bus->use_polling;
746 /* XXXX */
747 if (polling)
748 pipe->running = 0;
749
750 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
751 usbd_xfer_isread(xfer)) {
752 #ifdef DIAGNOSTIC
753 if (xfer->actlen > xfer->length) {
754 printf("usb_transfer_complete: actlen > len %d > %d\n",
755 xfer->actlen, xfer->length);
756 xfer->actlen = xfer->length;
757 }
758 #endif
759 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
760 }
761
762 /* if we allocated the buffer in usbd_transfer() we free it here. */
763 if (xfer->rqflags & URQ_AUTO_DMABUF) {
764 if (!repeat) {
765 struct usbd_bus *bus = pipe->device->bus;
766 bus->methods->freem(bus, dmap);
767 xfer->rqflags &= ~URQ_AUTO_DMABUF;
768 }
769 }
770
771 if (!repeat) {
772 /* Remove request from queue. */
773 #ifdef DIAGNOSTIC
774 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
775 printf("usb_transfer_complete: bad dequeue %p != %p\n",
776 xfer, SIMPLEQ_FIRST(&pipe->queue));
777 #endif
778 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
779 }
780 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
781 repeat, SIMPLEQ_FIRST(&pipe->queue)));
782
783 /* Count completed transfers. */
784 ++pipe->device->bus->stats.requests
785 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
786
787 xfer->done = 1;
788 if (!xfer->status && xfer->actlen < xfer->length &&
789 !(xfer->flags & USBD_SHORT_XFER_OK)) {
790 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
791 xfer->actlen, xfer->length));
792 xfer->status = USBD_SHORT_XFER;
793 }
794
795 if (xfer->callback)
796 xfer->callback(xfer, xfer->priv, xfer->status);
797
798 #ifdef DIAGNOSTIC
799 if (pipe->methods->done != NULL)
800 pipe->methods->done(xfer);
801 else
802 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
803 #else
804 pipe->methods->done(xfer);
805 #endif
806
807 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
808 wakeup(xfer);
809
810 if (!repeat) {
811 /* XXX should we stop the queue on all errors? */
812 if ((xfer->status == USBD_CANCELLED ||
813 xfer->status == USBD_TIMEOUT) &&
814 pipe->iface != NULL) /* not control pipe */
815 pipe->running = 0;
816 else
817 usbd_start_next(pipe);
818 }
819 }
820
821 usbd_status
822 usb_insert_transfer(usbd_xfer_handle xfer)
823 {
824 usbd_pipe_handle pipe = xfer->pipe;
825 usbd_status err;
826 int s;
827
828 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
829 pipe, pipe->running, xfer->timeout));
830 s = splusb();
831 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
832 if (pipe->running)
833 err = USBD_IN_PROGRESS;
834 else {
835 pipe->running = 1;
836 err = USBD_NORMAL_COMPLETION;
837 }
838 splx(s);
839 return (err);
840 }
841
842 /* Called at splusb() */
843 void
844 usbd_start_next(usbd_pipe_handle pipe)
845 {
846 usbd_xfer_handle xfer;
847 usbd_status err;
848
849 SPLUSBCHECK;
850
851 #ifdef DIAGNOSTIC
852 if (pipe == NULL) {
853 printf("usbd_start_next: pipe == NULL\n");
854 return;
855 }
856 if (pipe->methods == NULL || pipe->methods->start == NULL) {
857 printf("usbd_start_next: pipe=%p no start method\n", pipe);
858 return;
859 }
860 #endif
861
862 /* Get next request in queue. */
863 xfer = SIMPLEQ_FIRST(&pipe->queue);
864 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
865 if (xfer == NULL) {
866 pipe->running = 0;
867 } else {
868 err = pipe->methods->start(xfer);
869 if (err != USBD_IN_PROGRESS) {
870 printf("usbd_start_next: error=%d\n", err);
871 pipe->running = 0;
872 /* XXX do what? */
873 }
874 }
875 }
876
877 usbd_status
878 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
879 {
880 return (usbd_do_request_flags(dev, req, data, 0, 0));
881 }
882
883 usbd_status
884 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
885 void *data, u_int16_t flags, int *actlen)
886 {
887 usbd_xfer_handle xfer;
888 usbd_status err;
889
890 #ifdef DIAGNOSTIC
891 #if defined(__i386__) && defined(__FreeBSD__)
892 KASSERT(intr_nesting_level == 0,
893 ("usbd_do_request: in interrupt context"));
894 #endif
895 if (dev->bus->intr_context) {
896 printf("usbd_do_request: not in process context\n");
897 return (USBD_INVAL);
898 }
899 #endif
900
901 xfer = usbd_alloc_xfer(dev);
902 if (xfer == NULL)
903 return (USBD_NOMEM);
904 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
905 data, UGETW(req->wLength), flags, 0);
906 err = usbd_sync_transfer(xfer);
907 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
908 if (xfer->actlen > xfer->length)
909 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
910 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
911 dev->address, xfer->request.bmRequestType,
912 xfer->request.bRequest, UGETW(xfer->request.wValue),
913 UGETW(xfer->request.wIndex),
914 UGETW(xfer->request.wLength),
915 xfer->length, xfer->actlen));
916 #endif
917 if (actlen != NULL)
918 *actlen = xfer->actlen;
919 if (err == USBD_STALLED) {
920 /*
921 * The control endpoint has stalled. Control endpoints
922 * should not halt, but some may do so anyway so clear
923 * any halt condition.
924 */
925 usb_device_request_t treq;
926 usb_status_t status;
927 u_int16_t s;
928 usbd_status nerr;
929
930 treq.bmRequestType = UT_READ_ENDPOINT;
931 treq.bRequest = UR_GET_STATUS;
932 USETW(treq.wValue, 0);
933 USETW(treq.wIndex, 0);
934 USETW(treq.wLength, sizeof(usb_status_t));
935 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
936 &treq, &status,sizeof(usb_status_t),
937 0, 0);
938 nerr = usbd_sync_transfer(xfer);
939 if (nerr)
940 goto bad;
941 s = UGETW(status.wStatus);
942 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
943 if (!(s & UES_HALT))
944 goto bad;
945 treq.bmRequestType = UT_WRITE_ENDPOINT;
946 treq.bRequest = UR_CLEAR_FEATURE;
947 USETW(treq.wValue, UF_ENDPOINT_HALT);
948 USETW(treq.wIndex, 0);
949 USETW(treq.wLength, 0);
950 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
951 &treq, &status, 0, 0, 0);
952 nerr = usbd_sync_transfer(xfer);
953 if (nerr)
954 goto bad;
955 }
956
957 bad:
958 usbd_free_xfer(xfer);
959 return (err);
960 }
961
962 void
963 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
964 usbd_status status)
965 {
966 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
967 if (xfer->actlen > xfer->length)
968 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
969 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
970 xfer->pipe->device->address,
971 xfer->request.bmRequestType,
972 xfer->request.bRequest, UGETW(xfer->request.wValue),
973 UGETW(xfer->request.wIndex),
974 UGETW(xfer->request.wLength),
975 xfer->length, xfer->actlen));
976 #endif
977 usbd_free_xfer(xfer);
978 }
979
980 /*
981 * Execute a request without waiting for completion.
982 * Can be used from interrupt context.
983 */
984 usbd_status
985 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
986 void *data)
987 {
988 usbd_xfer_handle xfer;
989 usbd_status err;
990
991 xfer = usbd_alloc_xfer(dev);
992 if (xfer == NULL)
993 return (USBD_NOMEM);
994 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
995 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
996 err = usbd_transfer(xfer);
997 if (err != USBD_IN_PROGRESS) {
998 usbd_free_xfer(xfer);
999 return (err);
1000 }
1001 return (USBD_NORMAL_COMPLETION);
1002 }
1003
1004 struct usbd_quirks *
1005 usbd_get_quirks(usbd_device_handle dev)
1006 {
1007 return (dev->quirks);
1008 }
1009
1010 /* XXX do periodic free() of free list */
1011
1012 /*
1013 * Called from keyboard driver when in polling mode.
1014 */
1015 void
1016 usbd_dopoll(usbd_interface_handle iface)
1017 {
1018 iface->device->bus->methods->do_poll(iface->device->bus);
1019 }
1020
1021 void
1022 usbd_set_polling(usbd_device_handle dev, int on)
1023 {
1024 if (on)
1025 dev->bus->use_polling++;
1026 else
1027 dev->bus->use_polling--;
1028 }
1029
1030
1031 usb_endpoint_descriptor_t *
1032 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1033 {
1034 struct usbd_endpoint *ep;
1035 int i;
1036
1037 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1038 ep = &iface->endpoints[i];
1039 if (ep->edesc->bEndpointAddress == address)
1040 return (iface->endpoints[i].edesc);
1041 }
1042 return (0);
1043 }
1044
1045 /*
1046 * usbd_ratecheck() can limit the number of error messages that occurs.
1047 * When a device is unplugged it may take up to 0.25s for the hub driver
1048 * to notice it. If the driver continuosly tries to do I/O operations
1049 * this can generate a large number of messages.
1050 */
1051 int
1052 usbd_ratecheck(struct timeval *last)
1053 {
1054 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1055
1056 return (ratecheck(last, &errinterval));
1057 }
1058
1059 #if defined(__FreeBSD__)
1060 int
1061 usbd_driver_load(module_t mod, int what, void *arg)
1062 {
1063 /* XXX should implement something like a function that removes all generic devices */
1064
1065 return (0);
1066 }
1067
1068 #endif
1069