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