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