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