usbdi.c revision 1.65 1 /* $NetBSD: usbdi.c,v 1.65 2000/03/08 15:34: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 (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 xfer->status = USBD_TIMEOUT;
321 }
322 } else
323 /* XXX End hack XXX */
324 tsleep(xfer, PRIBIO, "usbsyn", 0);
325 }
326 splx(s);
327 return (xfer->status);
328 }
329
330 /* Like usbd_transfer(), but waits for completion. */
331 usbd_status
332 usbd_sync_transfer(xfer)
333 usbd_xfer_handle xfer;
334 {
335 xfer->flags |= USBD_SYNCHRONOUS;
336 return (usbd_transfer(xfer));
337 }
338
339 void *
340 usbd_alloc_buffer(xfer, size)
341 usbd_xfer_handle xfer;
342 u_int32_t size;
343 {
344 struct usbd_bus *bus = xfer->device->bus;
345 usbd_status err;
346
347 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
348 if (err)
349 return (0);
350 xfer->rqflags |= URQ_DEV_DMABUF;
351 return (KERNADDR(&xfer->dmabuf));
352 }
353
354 void
355 usbd_free_buffer(xfer)
356 usbd_xfer_handle xfer;
357 {
358 #ifdef DIAGNOSTIC
359 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
360 printf("usbd_free_buffer: no buffer\n");
361 return;
362 }
363 #endif
364 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
365 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
366 }
367
368 void *
369 usbd_get_buffer(xfer)
370 usbd_xfer_handle xfer;
371 {
372 if (!(xfer->rqflags & URQ_DEV_DMABUF))
373 return (0);
374 return (KERNADDR(&xfer->dmabuf));
375 }
376
377 usbd_xfer_handle
378 usbd_alloc_xfer(dev)
379 usbd_device_handle dev;
380 {
381 usbd_xfer_handle xfer;
382
383 xfer = dev->bus->methods->allocx(dev->bus);
384 if (xfer == NULL)
385 return (NULL);
386 xfer->device = dev;
387 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
388 return (xfer);
389 }
390
391 usbd_status
392 usbd_free_xfer(xfer)
393 usbd_xfer_handle xfer;
394 {
395 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
396 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
397 usbd_free_buffer(xfer);
398 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
399 return (USBD_NORMAL_COMPLETION);
400 }
401
402 void
403 usbd_setup_xfer(xfer, pipe, priv, buffer, length, flags, timeout, callback)
404 usbd_xfer_handle xfer;
405 usbd_pipe_handle pipe;
406 usbd_private_handle priv;
407 void *buffer;
408 u_int32_t length;
409 u_int16_t flags;
410 u_int32_t timeout;
411 void (*callback) __P((usbd_xfer_handle,
412 usbd_private_handle,
413 usbd_status));
414 {
415 xfer->pipe = pipe;
416 xfer->priv = priv;
417 xfer->buffer = buffer;
418 xfer->length = length;
419 xfer->actlen = 0;
420 xfer->flags = flags;
421 xfer->timeout = timeout;
422 xfer->status = USBD_NOT_STARTED;
423 xfer->callback = callback;
424 xfer->rqflags &= ~URQ_REQUEST;
425 xfer->nframes = 0;
426 }
427
428 void
429 usbd_setup_default_xfer(xfer, dev, priv, timeout, req, buffer,
430 length, flags, callback)
431 usbd_xfer_handle xfer;
432 usbd_device_handle dev;
433 usbd_private_handle priv;
434 u_int32_t timeout;
435 usb_device_request_t *req;
436 void *buffer;
437 u_int32_t length;
438 u_int16_t flags;
439 void (*callback) __P((usbd_xfer_handle,
440 usbd_private_handle,
441 usbd_status));
442 {
443 xfer->pipe = dev->default_pipe;
444 xfer->priv = priv;
445 xfer->buffer = buffer;
446 xfer->length = length;
447 xfer->actlen = 0;
448 xfer->flags = flags;
449 xfer->timeout = timeout;
450 xfer->status = USBD_NOT_STARTED;
451 xfer->callback = callback;
452 xfer->request = *req;
453 xfer->rqflags |= URQ_REQUEST;
454 xfer->nframes = 0;
455 }
456
457 void
458 usbd_setup_isoc_xfer(xfer, pipe, priv, frlengths, nframes, flags, callback)
459 usbd_xfer_handle xfer;
460 usbd_pipe_handle pipe;
461 usbd_private_handle priv;
462 u_int16_t *frlengths;
463 u_int32_t nframes;
464 u_int16_t flags;
465 usbd_callback callback;
466 {
467 xfer->pipe = pipe;
468 xfer->priv = priv;
469 xfer->buffer = 0;
470 xfer->length = 0;
471 xfer->actlen = 0;
472 xfer->flags = flags;
473 xfer->timeout = USBD_NO_TIMEOUT;
474 xfer->status = USBD_NOT_STARTED;
475 xfer->callback = callback;
476 xfer->rqflags &= ~URQ_REQUEST;
477 xfer->frlengths = frlengths;
478 xfer->nframes = nframes;
479 }
480
481 void
482 usbd_get_xfer_status(xfer, priv, buffer, count, status)
483 usbd_xfer_handle xfer;
484 usbd_private_handle *priv;
485 void **buffer;
486 u_int32_t *count;
487 usbd_status *status;
488 {
489 if (priv != NULL)
490 *priv = xfer->priv;
491 if (buffer != NULL)
492 *buffer = xfer->buffer;
493 if (count != NULL)
494 *count = xfer->actlen;
495 if (status != NULL)
496 *status = xfer->status;
497 }
498
499 usb_config_descriptor_t *
500 usbd_get_config_descriptor(dev)
501 usbd_device_handle dev;
502 {
503 #ifdef DIAGNOSTIC
504 if (dev == NULL) {
505 printf("usbd_get_config_descriptor: dev == NULL\n");
506 return (NULL);
507 }
508 #endif
509 return (dev->cdesc);
510 }
511
512 usb_interface_descriptor_t *
513 usbd_get_interface_descriptor(iface)
514 usbd_interface_handle iface;
515 {
516 #ifdef DIAGNOSTIC
517 if (iface == NULL) {
518 printf("usbd_get_interface_descriptor: dev == NULL\n");
519 return (NULL);
520 }
521 #endif
522 return (iface->idesc);
523 }
524
525 usb_device_descriptor_t *
526 usbd_get_device_descriptor(dev)
527 usbd_device_handle dev;
528 {
529 return (&dev->ddesc);
530 }
531
532 usb_endpoint_descriptor_t *
533 usbd_interface2endpoint_descriptor(iface, index)
534 usbd_interface_handle iface;
535 u_int8_t index;
536 {
537 if (index >= iface->idesc->bNumEndpoints)
538 return (0);
539 return (iface->endpoints[index].edesc);
540 }
541
542 usbd_status
543 usbd_abort_pipe(pipe)
544 usbd_pipe_handle pipe;
545 {
546 usbd_status err;
547 int s;
548
549 #ifdef DIAGNOSTIC
550 if (pipe == NULL) {
551 printf("usbd_close_pipe: pipe==NULL\n");
552 return (USBD_NORMAL_COMPLETION);
553 }
554 #endif
555 s = splusb();
556 err = usbd_ar_pipe(pipe);
557 splx(s);
558 return (err);
559 }
560
561 usbd_status
562 usbd_clear_endpoint_stall(pipe)
563 usbd_pipe_handle pipe;
564 {
565 usbd_device_handle dev = pipe->device;
566 usb_device_request_t req;
567 usbd_status err;
568
569 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
570
571 /*
572 * Clearing en endpoint stall resets the enpoint toggle, so
573 * do the same to the HC toggle.
574 */
575 pipe->methods->cleartoggle(pipe);
576
577 req.bmRequestType = UT_WRITE_ENDPOINT;
578 req.bRequest = UR_CLEAR_FEATURE;
579 USETW(req.wValue, UF_ENDPOINT_HALT);
580 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
581 USETW(req.wLength, 0);
582 err = usbd_do_request(dev, &req, 0);
583 #if 0
584 XXX should we do this?
585 if (!err) {
586 pipe->state = USBD_PIPE_ACTIVE;
587 /* XXX activate pipe */
588 }
589 #endif
590 return (err);
591 }
592
593 usbd_status
594 usbd_clear_endpoint_stall_async(pipe)
595 usbd_pipe_handle pipe;
596 {
597 usbd_device_handle dev = pipe->device;
598 usb_device_request_t req;
599 usbd_status err;
600
601 pipe->methods->cleartoggle(pipe);
602
603 req.bmRequestType = UT_WRITE_ENDPOINT;
604 req.bRequest = UR_CLEAR_FEATURE;
605 USETW(req.wValue, UF_ENDPOINT_HALT);
606 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
607 USETW(req.wLength, 0);
608 err = usbd_do_request_async(dev, &req, 0);
609 return (err);
610 }
611
612 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
613 void
614 usbd_clear_endpoint_toggle(pipe)
615 usbd_pipe_handle pipe;
616 {
617 pipe->methods->cleartoggle(pipe);
618 }
619
620 usbd_status
621 usbd_endpoint_count(iface, count)
622 usbd_interface_handle iface;
623 u_int8_t *count;
624 {
625 *count = iface->idesc->bNumEndpoints;
626 return (USBD_NORMAL_COMPLETION);
627 }
628
629 usbd_status
630 usbd_interface_count(dev, count)
631 usbd_device_handle dev;
632 u_int8_t *count;
633 {
634 if (dev->cdesc == NULL)
635 return (USBD_NOT_CONFIGURED);
636 *count = dev->cdesc->bNumInterface;
637 return (USBD_NORMAL_COMPLETION);
638 }
639
640 usbd_status
641 usbd_interface2device_handle(iface, dev)
642 usbd_interface_handle iface;
643 usbd_device_handle *dev;
644 {
645 *dev = iface->device;
646 return (USBD_NORMAL_COMPLETION);
647 }
648
649 usbd_status
650 usbd_device2interface_handle(dev, ifaceno, iface)
651 usbd_device_handle dev;
652 u_int8_t ifaceno;
653 usbd_interface_handle *iface;
654 {
655 if (dev->cdesc == NULL)
656 return (USBD_NOT_CONFIGURED);
657 if (ifaceno >= dev->cdesc->bNumInterface)
658 return (USBD_INVAL);
659 *iface = &dev->ifaces[ifaceno];
660 return (USBD_NORMAL_COMPLETION);
661 }
662
663 usbd_device_handle
664 usbd_pipe2device_handle(pipe)
665 usbd_pipe_handle pipe;
666 {
667 return (pipe->device);
668 }
669
670 /* XXXX use altno */
671 usbd_status
672 usbd_set_interface(iface, altidx)
673 usbd_interface_handle iface;
674 int altidx;
675 {
676 usb_device_request_t req;
677 usbd_status err;
678
679 if (LIST_FIRST(&iface->pipes) != 0)
680 return (USBD_IN_USE);
681
682 if (iface->endpoints)
683 free(iface->endpoints, M_USB);
684 iface->endpoints = 0;
685 iface->idesc = 0;
686
687 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
688 if (err)
689 return (err);
690
691 req.bmRequestType = UT_WRITE_INTERFACE;
692 req.bRequest = UR_SET_INTERFACE;
693 USETW(req.wValue, iface->idesc->bAlternateSetting);
694 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
695 USETW(req.wLength, 0);
696 return (usbd_do_request(iface->device, &req, 0));
697 }
698
699 int
700 usbd_get_no_alts(cdesc, ifaceno)
701 usb_config_descriptor_t *cdesc;
702 int ifaceno;
703 {
704 char *p = (char *)cdesc;
705 char *end = p + UGETW(cdesc->wTotalLength);
706 usb_interface_descriptor_t *d;
707 int n;
708
709 for (n = 0; p < end; p += d->bLength) {
710 d = (usb_interface_descriptor_t *)p;
711 if (p + d->bLength <= end &&
712 d->bDescriptorType == UDESC_INTERFACE &&
713 d->bInterfaceNumber == ifaceno)
714 n++;
715 }
716 return (n);
717 }
718
719 int
720 usbd_get_interface_altindex(iface)
721 usbd_interface_handle iface;
722 {
723 return (iface->altindex);
724 }
725
726 usbd_status
727 usbd_get_interface(iface, aiface)
728 usbd_interface_handle iface;
729 u_int8_t *aiface;
730 {
731 usb_device_request_t req;
732
733 req.bmRequestType = UT_READ_INTERFACE;
734 req.bRequest = UR_GET_INTERFACE;
735 USETW(req.wValue, 0);
736 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
737 USETW(req.wLength, 1);
738 return (usbd_do_request(iface->device, &req, aiface));
739 }
740
741 /*** Internal routines ***/
742
743 /* Dequeue all pipe operations, called at splusb(). */
744 static usbd_status
745 usbd_ar_pipe(pipe)
746 usbd_pipe_handle pipe;
747 {
748 usbd_xfer_handle xfer;
749
750 SPLUSBCHECK;
751
752 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
753 #ifdef USB_DEBUG
754 if (usbdebug > 5)
755 usbd_dump_queue(pipe);
756 #endif
757 pipe->repeat = 0;
758 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
759 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
760 pipe, xfer, pipe->methods));
761 /* Make the HC abort it (and invoke the callback). */
762 pipe->methods->abort(xfer);
763 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
764 }
765 return (USBD_NORMAL_COMPLETION);
766 }
767
768 /* Called at splusb() */
769 void
770 usb_transfer_complete(xfer)
771 usbd_xfer_handle xfer;
772 {
773 usbd_pipe_handle pipe = xfer->pipe;
774 usb_dma_t *dmap = &xfer->dmabuf;
775 int repeat = pipe->repeat;
776 int polling;
777
778 SPLUSBCHECK;
779
780 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
781 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
782
783 #ifdef DIAGNOSTIC
784 if (pipe == NULL) {
785 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
786 return;
787 }
788 #endif
789 polling = pipe->device->bus->use_polling;
790 /* XXXX */
791 if (polling)
792 pipe->running = 0;
793
794 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
795 usbd_xfer_isread(xfer)) {
796 #ifdef DIAGNOSTIC
797 if (xfer->actlen > xfer->length) {
798 printf("usb_transfer_complete: actlen > len %d > %d\n",
799 xfer->actlen, xfer->length);
800 xfer->actlen = xfer->length;
801 }
802 #endif
803 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
804 }
805
806 /* if we allocated the buffer in usbd_transfer() we free it here. */
807 if (xfer->rqflags & URQ_AUTO_DMABUF) {
808 if (!repeat) {
809 struct usbd_bus *bus = pipe->device->bus;
810 bus->methods->freem(bus, dmap);
811 xfer->rqflags &= ~URQ_AUTO_DMABUF;
812 }
813 }
814
815 if (!repeat) {
816 /* Remove request from queue. */
817 #ifdef DIAGNOSTIC
818 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
819 printf("usb_transfer_complete: bad dequeue %p != %p\n",
820 xfer, SIMPLEQ_FIRST(&pipe->queue));
821 #endif
822 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
823 }
824 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
825 repeat, SIMPLEQ_FIRST(&pipe->queue)));
826
827 /* Count completed transfers. */
828 ++pipe->device->bus->stats.requests
829 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
830
831 xfer->done = 1;
832 if (!xfer->status && xfer->actlen < xfer->length &&
833 !(xfer->flags & USBD_SHORT_XFER_OK)) {
834 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
835 xfer->actlen, xfer->length));
836 xfer->status = USBD_SHORT_XFER;
837 }
838
839 if (xfer->callback)
840 xfer->callback(xfer, xfer->priv, xfer->status);
841
842 #ifdef DIAGNOSTIC
843 if (pipe->methods->done != NULL)
844 pipe->methods->done(xfer);
845 else
846 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
847 #else
848 pipe->methods->done(xfer);
849 #endif
850
851 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
852 wakeup(xfer);
853
854 if (!repeat) {
855 /* XXX should we stop the queue on all errors? */
856 if ((xfer->status == USBD_CANCELLED ||
857 xfer->status == USBD_TIMEOUT) &&
858 pipe->iface != NULL) /* not control pipe */
859 pipe->running = 0;
860 else
861 usbd_start_next(pipe);
862 }
863 }
864
865 usbd_status
866 usb_insert_transfer(xfer)
867 usbd_xfer_handle xfer;
868 {
869 usbd_pipe_handle pipe = xfer->pipe;
870 usbd_status err;
871 int s;
872
873 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
874 pipe, pipe->running, xfer->timeout));
875 s = splusb();
876 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
877 if (pipe->running)
878 err = USBD_IN_PROGRESS;
879 else {
880 pipe->running = 1;
881 err = USBD_NORMAL_COMPLETION;
882 }
883 splx(s);
884 return (err);
885 }
886
887 /* Called at splusb() */
888 void
889 usbd_start_next(pipe)
890 usbd_pipe_handle pipe;
891 {
892 usbd_xfer_handle xfer;
893 usbd_status err;
894
895 SPLUSBCHECK;
896
897 #ifdef DIAGNOSTIC
898 if (pipe == NULL) {
899 printf("usbd_start_next: pipe == NULL\n");
900 return;
901 }
902 if (pipe->methods == NULL || pipe->methods->start == NULL) {
903 printf("usbd_start_next: pipe=%p no start method\n", pipe);
904 return;
905 }
906 #endif
907
908 /* Get next request in queue. */
909 xfer = SIMPLEQ_FIRST(&pipe->queue);
910 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
911 if (xfer == NULL) {
912 pipe->running = 0;
913 } else {
914 err = pipe->methods->start(xfer);
915 if (err != USBD_IN_PROGRESS) {
916 printf("usbd_start_next: error=%d\n", err);
917 pipe->running = 0;
918 /* XXX do what? */
919 }
920 }
921 }
922
923 usbd_status
924 usbd_do_request(dev, req, data)
925 usbd_device_handle dev;
926 usb_device_request_t *req;
927 void *data;
928 {
929 return (usbd_do_request_flags(dev, req, data, 0, 0));
930 }
931
932 usbd_status
933 usbd_do_request_flags(dev, req, data, flags, actlen)
934 usbd_device_handle dev;
935 usb_device_request_t *req;
936 void *data;
937 u_int16_t flags;
938 int *actlen;
939 {
940 usbd_xfer_handle xfer;
941 usbd_status err;
942
943 #ifdef DIAGNOSTIC
944 #if defined(__i386__) && defined(__FreeBSD__)
945 KASSERT(intr_nesting_level == 0,
946 ("usbd_do_request: in interrupt context"));
947 #endif
948 if (dev->bus->intr_context) {
949 printf("usbd_do_request: not in process context\n");
950 return (USBD_INVAL);
951 }
952 #endif
953
954 xfer = usbd_alloc_xfer(dev);
955 if (xfer == NULL)
956 return (USBD_NOMEM);
957 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
958 data, UGETW(req->wLength), flags, 0);
959 err = usbd_sync_transfer(xfer);
960 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
961 if (xfer->actlen > xfer->length)
962 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
963 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
964 dev->address, xfer->request.bmRequestType,
965 xfer->request.bRequest, UGETW(xfer->request.wValue),
966 UGETW(xfer->request.wIndex),
967 UGETW(xfer->request.wLength),
968 xfer->length, xfer->actlen));
969 #endif
970 if (actlen != NULL)
971 *actlen = xfer->actlen;
972 if (err == USBD_STALLED) {
973 /*
974 * The control endpoint has stalled. Control endpoints
975 * should not halt, but some may do so anyway so clear
976 * any halt condition.
977 */
978 usb_device_request_t treq;
979 usb_status_t status;
980 u_int16_t s;
981 usbd_status nerr;
982
983 treq.bmRequestType = UT_READ_ENDPOINT;
984 treq.bRequest = UR_GET_STATUS;
985 USETW(treq.wValue, 0);
986 USETW(treq.wIndex, 0);
987 USETW(treq.wLength, sizeof(usb_status_t));
988 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
989 &treq, &status,sizeof(usb_status_t),
990 0, 0);
991 nerr = usbd_sync_transfer(xfer);
992 if (nerr)
993 goto bad;
994 s = UGETW(status.wStatus);
995 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
996 if (!(s & UES_HALT))
997 goto bad;
998 treq.bmRequestType = UT_WRITE_ENDPOINT;
999 treq.bRequest = UR_CLEAR_FEATURE;
1000 USETW(treq.wValue, UF_ENDPOINT_HALT);
1001 USETW(treq.wIndex, 0);
1002 USETW(treq.wLength, 0);
1003 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1004 &treq, &status, 0, 0, 0);
1005 nerr = usbd_sync_transfer(xfer);
1006 if (nerr)
1007 goto bad;
1008 }
1009
1010 bad:
1011 usbd_free_xfer(xfer);
1012 return (err);
1013 }
1014
1015 void
1016 usbd_do_request_async_cb(xfer, priv, status)
1017 usbd_xfer_handle xfer;
1018 usbd_private_handle priv;
1019 usbd_status status;
1020 {
1021 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1022 if (xfer->actlen > xfer->length)
1023 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1024 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1025 xfer->pipe->device->address,
1026 xfer->request.bmRequestType,
1027 xfer->request.bRequest, UGETW(xfer->request.wValue),
1028 UGETW(xfer->request.wIndex),
1029 UGETW(xfer->request.wLength),
1030 xfer->length, xfer->actlen));
1031 #endif
1032 usbd_free_xfer(xfer);
1033 }
1034
1035 /*
1036 * Execute a request without waiting for completion.
1037 * Can be used from interrupt context.
1038 */
1039 usbd_status
1040 usbd_do_request_async(dev, req, data)
1041 usbd_device_handle dev;
1042 usb_device_request_t *req;
1043 void *data;
1044 {
1045 usbd_xfer_handle xfer;
1046 usbd_status err;
1047
1048 xfer = usbd_alloc_xfer(dev);
1049 if (xfer == NULL)
1050 return (USBD_NOMEM);
1051 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1052 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1053 err = usbd_transfer(xfer);
1054 if (err != USBD_IN_PROGRESS) {
1055 usbd_free_xfer(xfer);
1056 return (err);
1057 }
1058 return (USBD_NORMAL_COMPLETION);
1059 }
1060
1061 struct usbd_quirks *
1062 usbd_get_quirks(dev)
1063 usbd_device_handle dev;
1064 {
1065 return (dev->quirks);
1066 }
1067
1068 /* XXX do periodic free() of free list */
1069
1070 /*
1071 * Called from keyboard driver when in polling mode.
1072 */
1073 void
1074 usbd_dopoll(iface)
1075 usbd_interface_handle iface;
1076 {
1077 iface->device->bus->methods->do_poll(iface->device->bus);
1078 }
1079
1080 void
1081 usbd_set_polling(dev, on)
1082 usbd_device_handle dev;
1083 int on;
1084 {
1085 if (on)
1086 dev->bus->use_polling++;
1087 else
1088 dev->bus->use_polling--;
1089 }
1090
1091
1092 usb_endpoint_descriptor_t *
1093 usbd_get_endpoint_descriptor(iface, address)
1094 usbd_interface_handle iface;
1095 u_int8_t address;
1096 {
1097 struct usbd_endpoint *ep;
1098 int i;
1099
1100 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1101 ep = &iface->endpoints[i];
1102 if (ep->edesc->bEndpointAddress == address)
1103 return (iface->endpoints[i].edesc);
1104 }
1105 return (0);
1106 }
1107
1108 /*
1109 * usbd_ratecheck() can limit the number of error messages that occurs.
1110 * When a device is unplugged it may take up to 0.25s for the hub driver
1111 * to notice it. If the driver continuosly tries to do I/O operations
1112 * this can generate a large number of messages.
1113 */
1114 int
1115 usbd_ratecheck(last)
1116 struct timeval *last;
1117 {
1118 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1119
1120 return (ratecheck(last, &errinterval));
1121 }
1122
1123 #if defined(__FreeBSD__)
1124 int
1125 usbd_driver_load(module_t mod, int what, void *arg)
1126 {
1127 /* XXX should implement something like a function that removes all generic devices */
1128
1129 return (0);
1130 }
1131
1132 #endif
1133