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