usbdi.c revision 1.78 1 /* $NetBSD: usbdi.c,v 1.78 2001/01/19 04:01:10 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 endpoints = iface->endpoints;
638 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
639 if (err)
640 return (err);
641
642 /* new setting works, we can free old endpoints */
643 if (endpoints != NULL)
644 free(endpoints, M_USB);
645
646 #ifdef DIAGNOSTIC
647 if (iface->idesc == NULL) {
648 printf("usbd_set_interface: NULL pointer\n");
649 return (USBD_INVAL);
650 }
651 #endif
652
653 req.bmRequestType = UT_WRITE_INTERFACE;
654 req.bRequest = UR_SET_INTERFACE;
655 USETW(req.wValue, iface->idesc->bAlternateSetting);
656 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
657 USETW(req.wLength, 0);
658 return (usbd_do_request(iface->device, &req, 0));
659 }
660
661 int
662 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
663 {
664 char *p = (char *)cdesc;
665 char *end = p + UGETW(cdesc->wTotalLength);
666 usb_interface_descriptor_t *d;
667 int n;
668
669 for (n = 0; p < end; p += d->bLength) {
670 d = (usb_interface_descriptor_t *)p;
671 if (p + d->bLength <= end &&
672 d->bDescriptorType == UDESC_INTERFACE &&
673 d->bInterfaceNumber == ifaceno)
674 n++;
675 }
676 return (n);
677 }
678
679 int
680 usbd_get_interface_altindex(usbd_interface_handle iface)
681 {
682 return (iface->altindex);
683 }
684
685 usbd_status
686 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
687 {
688 usb_device_request_t req;
689
690 req.bmRequestType = UT_READ_INTERFACE;
691 req.bRequest = UR_GET_INTERFACE;
692 USETW(req.wValue, 0);
693 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
694 USETW(req.wLength, 1);
695 return (usbd_do_request(iface->device, &req, aiface));
696 }
697
698 /*** Internal routines ***/
699
700 /* Dequeue all pipe operations, called at splusb(). */
701 Static usbd_status
702 usbd_ar_pipe(usbd_pipe_handle pipe)
703 {
704 usbd_xfer_handle xfer;
705
706 SPLUSBCHECK;
707
708 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
709 #ifdef USB_DEBUG
710 if (usbdebug > 5)
711 usbd_dump_queue(pipe);
712 #endif
713 pipe->repeat = 0;
714 pipe->aborting = 1;
715 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
716 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
717 pipe, xfer, pipe->methods));
718 /* Make the HC abort it (and invoke the callback). */
719 pipe->methods->abort(xfer);
720 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
721 }
722 pipe->aborting = 0;
723 return (USBD_NORMAL_COMPLETION);
724 }
725
726 /* Called at splusb() */
727 void
728 usb_transfer_complete(usbd_xfer_handle xfer)
729 {
730 usbd_pipe_handle pipe = xfer->pipe;
731 usb_dma_t *dmap = &xfer->dmabuf;
732 int repeat = pipe->repeat;
733 int polling;
734
735 SPLUSBCHECK;
736
737 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
738 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
739
740 #ifdef DIAGNOSTIC
741 if (pipe == NULL) {
742 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
743 return;
744 }
745 #endif
746 polling = pipe->device->bus->use_polling;
747 /* XXXX */
748 if (polling)
749 pipe->running = 0;
750
751 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
752 usbd_xfer_isread(xfer)) {
753 #ifdef DIAGNOSTIC
754 if (xfer->actlen > xfer->length) {
755 printf("usb_transfer_complete: actlen > len %d > %d\n",
756 xfer->actlen, xfer->length);
757 xfer->actlen = xfer->length;
758 }
759 #endif
760 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
761 }
762
763 /* if we allocated the buffer in usbd_transfer() we free it here. */
764 if (xfer->rqflags & URQ_AUTO_DMABUF) {
765 if (!repeat) {
766 struct usbd_bus *bus = pipe->device->bus;
767 bus->methods->freem(bus, dmap);
768 xfer->rqflags &= ~URQ_AUTO_DMABUF;
769 }
770 }
771
772 if (!repeat) {
773 /* Remove request from queue. */
774 #ifdef DIAGNOSTIC
775 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
776 printf("usb_transfer_complete: bad dequeue %p != %p\n",
777 xfer, SIMPLEQ_FIRST(&pipe->queue));
778 #endif
779 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
780 }
781 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
782 repeat, SIMPLEQ_FIRST(&pipe->queue)));
783
784 /* Count completed transfers. */
785 ++pipe->device->bus->stats.requests
786 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
787
788 xfer->done = 1;
789 if (!xfer->status && xfer->actlen < xfer->length &&
790 !(xfer->flags & USBD_SHORT_XFER_OK)) {
791 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
792 xfer->actlen, xfer->length));
793 xfer->status = USBD_SHORT_XFER;
794 }
795
796 if (xfer->callback)
797 xfer->callback(xfer, xfer->priv, xfer->status);
798
799 #ifdef DIAGNOSTIC
800 if (pipe->methods->done != NULL)
801 pipe->methods->done(xfer);
802 else
803 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
804 #else
805 pipe->methods->done(xfer);
806 #endif
807
808 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
809 wakeup(xfer);
810
811 if (!repeat) {
812 /* XXX should we stop the queue on all errors? */
813 if ((xfer->status == USBD_CANCELLED ||
814 xfer->status == USBD_TIMEOUT) &&
815 pipe->iface != NULL) /* not control pipe */
816 pipe->running = 0;
817 else
818 usbd_start_next(pipe);
819 }
820 }
821
822 usbd_status
823 usb_insert_transfer(usbd_xfer_handle xfer)
824 {
825 usbd_pipe_handle pipe = xfer->pipe;
826 usbd_status err;
827 int s;
828
829 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
830 pipe, pipe->running, xfer->timeout));
831 s = splusb();
832 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
833 if (pipe->running)
834 err = USBD_IN_PROGRESS;
835 else {
836 pipe->running = 1;
837 err = USBD_NORMAL_COMPLETION;
838 }
839 splx(s);
840 return (err);
841 }
842
843 /* Called at splusb() */
844 void
845 usbd_start_next(usbd_pipe_handle pipe)
846 {
847 usbd_xfer_handle xfer;
848 usbd_status err;
849
850 SPLUSBCHECK;
851
852 #ifdef DIAGNOSTIC
853 if (pipe == NULL) {
854 printf("usbd_start_next: pipe == NULL\n");
855 return;
856 }
857 if (pipe->methods == NULL || pipe->methods->start == NULL) {
858 printf("usbd_start_next: pipe=%p no start method\n", pipe);
859 return;
860 }
861 #endif
862
863 /* Get next request in queue. */
864 xfer = SIMPLEQ_FIRST(&pipe->queue);
865 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
866 if (xfer == NULL) {
867 pipe->running = 0;
868 } else {
869 err = pipe->methods->start(xfer);
870 if (err != USBD_IN_PROGRESS) {
871 printf("usbd_start_next: error=%d\n", err);
872 pipe->running = 0;
873 /* XXX do what? */
874 }
875 }
876 }
877
878 usbd_status
879 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
880 {
881 return (usbd_do_request_flags(dev, req, data, 0, 0));
882 }
883
884 usbd_status
885 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
886 void *data, u_int16_t flags, int *actlen)
887 {
888 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
889 data, flags, actlen));
890 }
891
892 usbd_status
893 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
894 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
895 {
896 usbd_xfer_handle xfer;
897 usbd_status err;
898
899 #ifdef DIAGNOSTIC
900 #if defined(__i386__) && defined(__FreeBSD__)
901 KASSERT(intr_nesting_level == 0,
902 ("usbd_do_request: in interrupt context"));
903 #endif
904 if (dev->bus->intr_context) {
905 printf("usbd_do_request: not in process context\n");
906 return (USBD_INVAL);
907 }
908 #endif
909
910 xfer = usbd_alloc_xfer(dev);
911 if (xfer == NULL)
912 return (USBD_NOMEM);
913 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
914 data, UGETW(req->wLength), flags, 0);
915 xfer->pipe = pipe;
916 err = usbd_sync_transfer(xfer);
917 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
918 if (xfer->actlen > xfer->length)
919 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
920 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
921 dev->address, xfer->request.bmRequestType,
922 xfer->request.bRequest, UGETW(xfer->request.wValue),
923 UGETW(xfer->request.wIndex),
924 UGETW(xfer->request.wLength),
925 xfer->length, xfer->actlen));
926 #endif
927 if (actlen != NULL)
928 *actlen = xfer->actlen;
929 if (err == USBD_STALLED) {
930 /*
931 * The control endpoint has stalled. Control endpoints
932 * should not halt, but some may do so anyway so clear
933 * any halt condition.
934 */
935 usb_device_request_t treq;
936 usb_status_t status;
937 u_int16_t s;
938 usbd_status nerr;
939
940 treq.bmRequestType = UT_READ_ENDPOINT;
941 treq.bRequest = UR_GET_STATUS;
942 USETW(treq.wValue, 0);
943 USETW(treq.wIndex, 0);
944 USETW(treq.wLength, sizeof(usb_status_t));
945 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
946 &treq, &status,sizeof(usb_status_t),
947 0, 0);
948 nerr = usbd_sync_transfer(xfer);
949 if (nerr)
950 goto bad;
951 s = UGETW(status.wStatus);
952 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
953 if (!(s & UES_HALT))
954 goto bad;
955 treq.bmRequestType = UT_WRITE_ENDPOINT;
956 treq.bRequest = UR_CLEAR_FEATURE;
957 USETW(treq.wValue, UF_ENDPOINT_HALT);
958 USETW(treq.wIndex, 0);
959 USETW(treq.wLength, 0);
960 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
961 &treq, &status, 0, 0, 0);
962 nerr = usbd_sync_transfer(xfer);
963 if (nerr)
964 goto bad;
965 }
966
967 bad:
968 usbd_free_xfer(xfer);
969 return (err);
970 }
971
972 void
973 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
974 usbd_status status)
975 {
976 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
977 if (xfer->actlen > xfer->length)
978 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
979 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
980 xfer->pipe->device->address,
981 xfer->request.bmRequestType,
982 xfer->request.bRequest, UGETW(xfer->request.wValue),
983 UGETW(xfer->request.wIndex),
984 UGETW(xfer->request.wLength),
985 xfer->length, xfer->actlen));
986 #endif
987 usbd_free_xfer(xfer);
988 }
989
990 /*
991 * Execute a request without waiting for completion.
992 * Can be used from interrupt context.
993 */
994 usbd_status
995 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
996 void *data)
997 {
998 usbd_xfer_handle xfer;
999 usbd_status err;
1000
1001 xfer = usbd_alloc_xfer(dev);
1002 if (xfer == NULL)
1003 return (USBD_NOMEM);
1004 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1005 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1006 err = usbd_transfer(xfer);
1007 if (err != USBD_IN_PROGRESS) {
1008 usbd_free_xfer(xfer);
1009 return (err);
1010 }
1011 return (USBD_NORMAL_COMPLETION);
1012 }
1013
1014 const struct usbd_quirks *
1015 usbd_get_quirks(usbd_device_handle dev)
1016 {
1017 return (dev->quirks);
1018 }
1019
1020 /* XXX do periodic free() of free list */
1021
1022 /*
1023 * Called from keyboard driver when in polling mode.
1024 */
1025 void
1026 usbd_dopoll(usbd_interface_handle iface)
1027 {
1028 iface->device->bus->methods->do_poll(iface->device->bus);
1029 }
1030
1031 void
1032 usbd_set_polling(usbd_device_handle dev, int on)
1033 {
1034 if (on)
1035 dev->bus->use_polling++;
1036 else
1037 dev->bus->use_polling--;
1038 }
1039
1040
1041 usb_endpoint_descriptor_t *
1042 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1043 {
1044 struct usbd_endpoint *ep;
1045 int i;
1046
1047 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1048 ep = &iface->endpoints[i];
1049 if (ep->edesc->bEndpointAddress == address)
1050 return (iface->endpoints[i].edesc);
1051 }
1052 return (0);
1053 }
1054
1055 /*
1056 * usbd_ratecheck() can limit the number of error messages that occurs.
1057 * When a device is unplugged it may take up to 0.25s for the hub driver
1058 * to notice it. If the driver continuosly tries to do I/O operations
1059 * this can generate a large number of messages.
1060 */
1061 int
1062 usbd_ratecheck(struct timeval *last)
1063 {
1064 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1065
1066 return (ratecheck(last, &errinterval));
1067 }
1068
1069 #if defined(__FreeBSD__)
1070 int
1071 usbd_driver_load(module_t mod, int what, void *arg)
1072 {
1073 /* XXX should implement something like a function that removes all generic devices */
1074
1075 return (0);
1076 }
1077
1078 #endif
1079