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