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