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