usbdi.c revision 1.134.2.12 1 /* $NetBSD: usbdi.c,v 1.134.2.12 2012/02/25 10:26:24 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, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
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.12 2012/02/25 10:26:24 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,
391 dev->bus->methods->get_lock ? CALLOUT_MPSAFE : 0);
392 cv_init(&xfer->cv, "usbxfer");
393 cv_init(&xfer->hccv, "usbhcxfer");
394 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
395 return (xfer);
396 }
397
398 usbd_status
399 usbd_free_xfer(usbd_xfer_handle xfer)
400 {
401 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
402 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
403 usbd_free_buffer(xfer);
404 #if defined(DIAGNOSTIC)
405 if (callout_pending(&xfer->timeout_handle)) {
406 callout_stop(&xfer->timeout_handle);
407 printf("usbd_free_xfer: timout_handle pending");
408 }
409 #endif
410 cv_destroy(&xfer->cv);
411 cv_destroy(&xfer->hccv);
412 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
413 return (USBD_NORMAL_COMPLETION);
414 }
415
416 void
417 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
418 usbd_private_handle priv, void *buffer, u_int32_t length,
419 u_int16_t flags, u_int32_t timeout,
420 usbd_callback callback)
421 {
422 xfer->pipe = pipe;
423 xfer->priv = priv;
424 xfer->buffer = buffer;
425 xfer->length = length;
426 xfer->actlen = 0;
427 xfer->flags = flags;
428 xfer->timeout = timeout;
429 xfer->status = USBD_NOT_STARTED;
430 xfer->callback = callback;
431 xfer->rqflags &= ~URQ_REQUEST;
432 xfer->nframes = 0;
433 }
434
435 void
436 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
437 usbd_private_handle priv, u_int32_t timeout,
438 usb_device_request_t *req, void *buffer,
439 u_int32_t length, u_int16_t flags,
440 usbd_callback callback)
441 {
442 xfer->pipe = dev->default_pipe;
443 xfer->priv = priv;
444 xfer->buffer = buffer;
445 xfer->length = length;
446 xfer->actlen = 0;
447 xfer->flags = flags;
448 xfer->timeout = timeout;
449 xfer->status = USBD_NOT_STARTED;
450 xfer->callback = callback;
451 xfer->request = *req;
452 xfer->rqflags |= URQ_REQUEST;
453 xfer->nframes = 0;
454 }
455
456 void
457 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
458 usbd_private_handle priv, u_int16_t *frlengths,
459 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
460 {
461 xfer->pipe = pipe;
462 xfer->priv = priv;
463 xfer->buffer = 0;
464 xfer->length = 0;
465 xfer->actlen = 0;
466 xfer->flags = flags;
467 xfer->timeout = USBD_NO_TIMEOUT;
468 xfer->status = USBD_NOT_STARTED;
469 xfer->callback = callback;
470 xfer->rqflags &= ~URQ_REQUEST;
471 xfer->frlengths = frlengths;
472 xfer->nframes = nframes;
473 }
474
475 void
476 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
477 void **buffer, u_int32_t *count, usbd_status *status)
478 {
479 if (priv != NULL)
480 *priv = xfer->priv;
481 if (buffer != NULL)
482 *buffer = xfer->buffer;
483 if (count != NULL)
484 *count = xfer->actlen;
485 if (status != NULL)
486 *status = xfer->status;
487 }
488
489 usb_config_descriptor_t *
490 usbd_get_config_descriptor(usbd_device_handle dev)
491 {
492 #ifdef DIAGNOSTIC
493 if (dev == NULL) {
494 printf("usbd_get_config_descriptor: dev == NULL\n");
495 return (NULL);
496 }
497 #endif
498 return (dev->cdesc);
499 }
500
501 usb_interface_descriptor_t *
502 usbd_get_interface_descriptor(usbd_interface_handle iface)
503 {
504 #ifdef DIAGNOSTIC
505 if (iface == NULL) {
506 printf("usbd_get_interface_descriptor: dev == NULL\n");
507 return (NULL);
508 }
509 #endif
510 return (iface->idesc);
511 }
512
513 usb_device_descriptor_t *
514 usbd_get_device_descriptor(usbd_device_handle dev)
515 {
516 return (&dev->ddesc);
517 }
518
519 usb_endpoint_descriptor_t *
520 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
521 {
522 if (index >= iface->idesc->bNumEndpoints)
523 return (0);
524 return (iface->endpoints[index].edesc);
525 }
526
527 /* Some drivers may wish to abort requests on the default pipe, *
528 * but there is no mechanism for getting a handle on it. */
529 usbd_status
530 usbd_abort_default_pipe(struct usbd_device *device)
531 {
532
533 return usbd_abort_pipe(device->default_pipe);
534 }
535
536 usbd_status
537 usbd_abort_pipe(usbd_pipe_handle pipe)
538 {
539 usbd_status err;
540 int s;
541 usbd_xfer_handle intrxfer = pipe->intrxfer;
542
543 #ifdef DIAGNOSTIC
544 if (pipe == NULL) {
545 printf("usbd_abort_pipe: pipe==NULL\n");
546 return (USBD_NORMAL_COMPLETION);
547 }
548 #endif
549 usbd_lock_pipe(pipe);
550 err = usbd_ar_pipe(pipe);
551 usbd_unlock_pipe(pipe);
552 if (pipe->intrxfer != intrxfer)
553 usbd_free_xfer(intrxfer);
554 return (err);
555 }
556
557 usbd_status
558 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
559 {
560 usbd_device_handle dev = pipe->device;
561 usb_device_request_t req;
562 usbd_status err;
563
564 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
565
566 /*
567 * Clearing en endpoint stall resets the endpoint toggle, so
568 * do the same to the HC toggle.
569 */
570 pipe->methods->cleartoggle(pipe);
571
572 req.bmRequestType = UT_WRITE_ENDPOINT;
573 req.bRequest = UR_CLEAR_FEATURE;
574 USETW(req.wValue, UF_ENDPOINT_HALT);
575 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
576 USETW(req.wLength, 0);
577 err = usbd_do_request(dev, &req, 0);
578 #if 0
579 XXX should we do this?
580 if (!err) {
581 pipe->state = USBD_PIPE_ACTIVE;
582 /* XXX activate pipe */
583 }
584 #endif
585 return (err);
586 }
587
588 usbd_status
589 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
590 {
591 usbd_device_handle dev = pipe->device;
592 usb_device_request_t req;
593 usbd_status err;
594
595 pipe->methods->cleartoggle(pipe);
596
597 req.bmRequestType = UT_WRITE_ENDPOINT;
598 req.bRequest = UR_CLEAR_FEATURE;
599 USETW(req.wValue, UF_ENDPOINT_HALT);
600 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
601 USETW(req.wLength, 0);
602 err = usbd_do_request_async(dev, &req, 0);
603 return (err);
604 }
605
606 void
607 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
608 {
609 pipe->methods->cleartoggle(pipe);
610 }
611
612 usbd_status
613 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
614 {
615 #ifdef DIAGNOSTIC
616 if (iface == NULL || iface->idesc == NULL) {
617 printf("usbd_endpoint_count: NULL pointer\n");
618 return (USBD_INVAL);
619 }
620 #endif
621 *count = iface->idesc->bNumEndpoints;
622 return (USBD_NORMAL_COMPLETION);
623 }
624
625 usbd_status
626 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
627 {
628 if (dev->cdesc == NULL)
629 return (USBD_NOT_CONFIGURED);
630 *count = dev->cdesc->bNumInterface;
631 return (USBD_NORMAL_COMPLETION);
632 }
633
634 void
635 usbd_interface2device_handle(usbd_interface_handle iface,
636 usbd_device_handle *dev)
637 {
638 *dev = iface->device;
639 }
640
641 usbd_status
642 usbd_device2interface_handle(usbd_device_handle dev,
643 u_int8_t ifaceno, usbd_interface_handle *iface)
644 {
645 if (dev->cdesc == NULL)
646 return (USBD_NOT_CONFIGURED);
647 if (ifaceno >= dev->cdesc->bNumInterface)
648 return (USBD_INVAL);
649 *iface = &dev->ifaces[ifaceno];
650 return (USBD_NORMAL_COMPLETION);
651 }
652
653 usbd_device_handle
654 usbd_pipe2device_handle(usbd_pipe_handle pipe)
655 {
656 return (pipe->device);
657 }
658
659 /* XXXX use altno */
660 usbd_status
661 usbd_set_interface(usbd_interface_handle iface, int altidx)
662 {
663 usb_device_request_t req;
664 usbd_status err;
665 void *endpoints;
666
667 if (LIST_FIRST(&iface->pipes) != 0)
668 return (USBD_IN_USE);
669
670 endpoints = iface->endpoints;
671 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
672 if (err)
673 return (err);
674
675 /* new setting works, we can free old endpoints */
676 if (endpoints != NULL)
677 free(endpoints, M_USB);
678
679 #ifdef DIAGNOSTIC
680 if (iface->idesc == NULL) {
681 printf("usbd_set_interface: NULL pointer\n");
682 return (USBD_INVAL);
683 }
684 #endif
685
686 req.bmRequestType = UT_WRITE_INTERFACE;
687 req.bRequest = UR_SET_INTERFACE;
688 USETW(req.wValue, iface->idesc->bAlternateSetting);
689 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
690 USETW(req.wLength, 0);
691 return (usbd_do_request(iface->device, &req, 0));
692 }
693
694 int
695 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
696 {
697 char *p = (char *)cdesc;
698 char *end = p + UGETW(cdesc->wTotalLength);
699 usb_interface_descriptor_t *d;
700 int n;
701
702 for (n = 0; p < end; p += d->bLength) {
703 d = (usb_interface_descriptor_t *)p;
704 if (p + d->bLength <= end &&
705 d->bDescriptorType == UDESC_INTERFACE &&
706 d->bInterfaceNumber == ifaceno)
707 n++;
708 }
709 return (n);
710 }
711
712 int
713 usbd_get_interface_altindex(usbd_interface_handle iface)
714 {
715 return (iface->altindex);
716 }
717
718 usbd_status
719 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
720 {
721 usb_device_request_t req;
722
723 req.bmRequestType = UT_READ_INTERFACE;
724 req.bRequest = UR_GET_INTERFACE;
725 USETW(req.wValue, 0);
726 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
727 USETW(req.wLength, 1);
728 return (usbd_do_request(iface->device, &req, aiface));
729 }
730
731 /*** Internal routines ***/
732
733 /* Dequeue all pipe operations, called at splusb(). */
734 Static usbd_status
735 usbd_ar_pipe(usbd_pipe_handle pipe)
736 {
737 usbd_xfer_handle xfer;
738
739 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
740
741 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
742 #ifdef USB_DEBUG
743 if (usbdebug > 5)
744 usbd_dump_queue(pipe);
745 #endif
746 pipe->repeat = 0;
747 pipe->aborting = 1;
748 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
749 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
750 pipe, xfer, pipe->methods));
751 /* Make the HC abort it (and invoke the callback). */
752 pipe->methods->abort(xfer);
753 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
754 }
755 pipe->aborting = 0;
756 return (USBD_NORMAL_COMPLETION);
757 }
758
759 /* Called with USB lock held. */
760 void
761 usb_transfer_complete(usbd_xfer_handle xfer)
762 {
763 usbd_pipe_handle pipe = xfer->pipe;
764 usb_dma_t *dmap = &xfer->dmabuf;
765 int sync = xfer->flags & USBD_SYNCHRONOUS;
766 int erred = xfer->status == USBD_CANCELLED ||
767 xfer->status == USBD_TIMEOUT;
768 int repeat, polling;
769
770 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
771 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
772
773 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
774
775 #ifdef DIAGNOSTIC
776 if (xfer->busy_free != XFER_ONQU) {
777 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
778 xfer, xfer->busy_free);
779 }
780 #endif
781
782 #ifdef DIAGNOSTIC
783 if (pipe == NULL) {
784 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
785 return;
786 }
787 #endif
788 repeat = pipe->repeat;
789 polling = pipe->device->bus->use_polling;
790 /* XXXX */
791 if (polling)
792 pipe->running = 0;
793
794 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
795 usbd_xfer_isread(xfer)) {
796 #ifdef DIAGNOSTIC
797 if (xfer->actlen > xfer->length) {
798 printf("usb_transfer_complete: actlen > len %d > %d\n",
799 xfer->actlen, xfer->length);
800 xfer->actlen = xfer->length;
801 }
802 #endif
803 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
804 }
805
806 /* if we allocated the buffer in usbd_transfer() we free it here. */
807 if (xfer->rqflags & URQ_AUTO_DMABUF) {
808 if (!repeat) {
809 struct usbd_bus *bus = pipe->device->bus;
810 bus->methods->freem(bus, dmap);
811 xfer->rqflags &= ~URQ_AUTO_DMABUF;
812 }
813 }
814
815 if (!repeat) {
816 /* Remove request from queue. */
817 #ifdef DIAGNOSTIC
818 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
819 printf("usb_transfer_complete: bad dequeue %p != %p\n",
820 xfer, SIMPLEQ_FIRST(&pipe->queue));
821 xfer->busy_free = XFER_BUSY;
822 #endif
823 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
824 }
825 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
826 repeat, SIMPLEQ_FIRST(&pipe->queue)));
827
828 /* Count completed transfers. */
829 ++pipe->device->bus->stats.uds_requests
830 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
831
832 xfer->done = 1;
833 if (!xfer->status && xfer->actlen < xfer->length &&
834 !(xfer->flags & USBD_SHORT_XFER_OK)) {
835 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
836 xfer->actlen, xfer->length));
837 xfer->status = USBD_SHORT_XFER;
838 }
839
840 if (repeat) {
841 if (xfer->callback) {
842 if (pipe->device->bus->lock)
843 mutex_exit(pipe->device->bus->lock);
844 xfer->callback(xfer, xfer->priv, xfer->status);
845 if (pipe->device->bus->lock)
846 mutex_enter(pipe->device->bus->lock);
847 }
848 pipe->methods->done(xfer);
849 } else {
850 pipe->methods->done(xfer);
851 if (xfer->callback) {
852 if (pipe->device->bus->lock)
853 mutex_exit(pipe->device->bus->lock);
854 xfer->callback(xfer, xfer->priv, xfer->status);
855 if (pipe->device->bus->lock)
856 mutex_enter(pipe->device->bus->lock);
857 }
858 }
859
860 if (sync && !polling) {
861 if (pipe->device->bus->lock)
862 cv_broadcast(&xfer->cv);
863 else
864 wakeup(xfer); /* XXXSMP ok */
865 }
866
867 if (!repeat) {
868 /* XXX should we stop the queue on all errors? */
869 if (erred && pipe->iface != NULL) /* not control pipe */
870 pipe->running = 0;
871 else
872 usbd_start_next(pipe);
873 }
874 }
875
876 /* Called with USB lock held. */
877 usbd_status
878 usb_insert_transfer(usbd_xfer_handle xfer)
879 {
880 usbd_pipe_handle pipe = xfer->pipe;
881 usbd_status err;
882
883 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
884 pipe, pipe->running, xfer->timeout));
885
886 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
887
888 #ifdef DIAGNOSTIC
889 if (xfer->busy_free != XFER_BUSY) {
890 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
891 xfer, xfer->busy_free);
892 return (USBD_INVAL);
893 }
894 xfer->busy_free = XFER_ONQU;
895 #endif
896 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
897 if (pipe->running)
898 err = USBD_IN_PROGRESS;
899 else {
900 pipe->running = 1;
901 err = USBD_NORMAL_COMPLETION;
902 }
903 return (err);
904 }
905
906 /* Called with USB lock held. */
907 void
908 usbd_start_next(usbd_pipe_handle pipe)
909 {
910 usbd_xfer_handle xfer;
911 usbd_status err;
912
913 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
914
915 #ifdef DIAGNOSTIC
916 if (pipe == NULL) {
917 printf("usbd_start_next: pipe == NULL\n");
918 return;
919 }
920 if (pipe->methods == NULL || pipe->methods->start == NULL) {
921 printf("usbd_start_next: pipe=%p no start method\n", pipe);
922 return;
923 }
924 #endif
925
926 /* Get next request in queue. */
927 xfer = SIMPLEQ_FIRST(&pipe->queue);
928 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
929 if (xfer == NULL) {
930 pipe->running = 0;
931 } else {
932 if (pipe->device->bus->lock)
933 mutex_exit(pipe->device->bus->lock);
934 err = pipe->methods->start(xfer);
935 if (pipe->device->bus->lock)
936 mutex_enter(pipe->device->bus->lock);
937 if (err != USBD_IN_PROGRESS) {
938 printf("usbd_start_next: error=%d\n", err);
939 pipe->running = 0;
940 /* XXX do what? */
941 }
942 }
943
944 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
945 }
946
947 usbd_status
948 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
949 {
950 return (usbd_do_request_flags(dev, req, data, 0, 0,
951 USBD_DEFAULT_TIMEOUT));
952 }
953
954 usbd_status
955 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
956 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
957 {
958 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
959 data, flags, actlen, timo));
960 }
961
962 usbd_status
963 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
964 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
965 u_int32_t timeout)
966 {
967 usbd_xfer_handle xfer;
968 usbd_status err;
969
970 #ifdef DIAGNOSTIC
971 if (dev->bus->intr_context) {
972 printf("usbd_do_request: not in process context\n");
973 return (USBD_INVAL);
974 }
975 #endif
976
977 xfer = usbd_alloc_xfer(dev);
978 if (xfer == NULL)
979 return (USBD_NOMEM);
980 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
981 data, UGETW(req->wLength), flags, 0);
982 xfer->pipe = pipe;
983 err = usbd_sync_transfer(xfer);
984 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
985 if (xfer->actlen > xfer->length) {
986 DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
987 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
988 __func__, dev->address, xfer->request.bmRequestType,
989 xfer->request.bRequest, UGETW(xfer->request.wValue),
990 UGETW(xfer->request.wIndex),
991 UGETW(xfer->request.wLength),
992 xfer->length, xfer->actlen));
993 }
994 #endif
995 if (actlen != NULL)
996 *actlen = xfer->actlen;
997 if (err == USBD_STALLED) {
998 /*
999 * The control endpoint has stalled. Control endpoints
1000 * should not halt, but some may do so anyway so clear
1001 * any halt condition.
1002 */
1003 usb_device_request_t treq;
1004 usb_status_t status;
1005 u_int16_t s;
1006 usbd_status nerr;
1007
1008 treq.bmRequestType = UT_READ_ENDPOINT;
1009 treq.bRequest = UR_GET_STATUS;
1010 USETW(treq.wValue, 0);
1011 USETW(treq.wIndex, 0);
1012 USETW(treq.wLength, sizeof(usb_status_t));
1013 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1014 &treq, &status,sizeof(usb_status_t),
1015 0, 0);
1016 nerr = usbd_sync_transfer(xfer);
1017 if (nerr)
1018 goto bad;
1019 s = UGETW(status.wStatus);
1020 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1021 if (!(s & UES_HALT))
1022 goto bad;
1023 treq.bmRequestType = UT_WRITE_ENDPOINT;
1024 treq.bRequest = UR_CLEAR_FEATURE;
1025 USETW(treq.wValue, UF_ENDPOINT_HALT);
1026 USETW(treq.wIndex, 0);
1027 USETW(treq.wLength, 0);
1028 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1029 &treq, &status, 0, 0, 0);
1030 nerr = usbd_sync_transfer(xfer);
1031 if (nerr)
1032 goto bad;
1033 }
1034
1035 bad:
1036 if (err) {
1037 DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
1038 }
1039 usbd_free_xfer(xfer);
1040 return (err);
1041 }
1042
1043 void
1044 usbd_do_request_async_cb(usbd_xfer_handle xfer,
1045 usbd_private_handle priv, usbd_status status)
1046 {
1047 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1048 if (xfer->actlen > xfer->length) {
1049 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1050 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1051 xfer->pipe->device->address,
1052 xfer->request.bmRequestType,
1053 xfer->request.bRequest, UGETW(xfer->request.wValue),
1054 UGETW(xfer->request.wIndex),
1055 UGETW(xfer->request.wLength),
1056 xfer->length, xfer->actlen));
1057 }
1058 #endif
1059 usbd_free_xfer(xfer);
1060 }
1061
1062 /*
1063 * Execute a request without waiting for completion.
1064 * Can be used from interrupt context.
1065 */
1066 usbd_status
1067 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1068 void *data)
1069 {
1070 usbd_xfer_handle xfer;
1071 usbd_status err;
1072
1073 xfer = usbd_alloc_xfer(dev);
1074 if (xfer == NULL)
1075 return (USBD_NOMEM);
1076 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1077 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1078 err = usbd_transfer(xfer);
1079 if (err != USBD_IN_PROGRESS) {
1080 usbd_free_xfer(xfer);
1081 return (err);
1082 }
1083 return (USBD_NORMAL_COMPLETION);
1084 }
1085
1086 const struct usbd_quirks *
1087 usbd_get_quirks(usbd_device_handle dev)
1088 {
1089 #ifdef DIAGNOSTIC
1090 if (dev == NULL) {
1091 printf("usbd_get_quirks: dev == NULL\n");
1092 return 0;
1093 }
1094 #endif
1095 return (dev->quirks);
1096 }
1097
1098 /* XXX do periodic free() of free list */
1099
1100 /*
1101 * Called from keyboard driver when in polling mode.
1102 */
1103 void
1104 usbd_dopoll(usbd_interface_handle iface)
1105 {
1106 iface->device->bus->methods->do_poll(iface->device->bus);
1107 }
1108
1109 /*
1110 * XXX use this more??? use_polling it touched manually all over
1111 */
1112 void
1113 usbd_set_polling(usbd_device_handle dev, int on)
1114 {
1115 if (on)
1116 dev->bus->use_polling++;
1117 else
1118 dev->bus->use_polling--;
1119
1120 /* Kick the host controller when switching modes */
1121 if (dev->bus->lock)
1122 mutex_enter(dev->bus->lock);
1123 (*dev->bus->methods->soft_intr)(dev->bus);
1124 if (dev->bus->lock)
1125 mutex_exit(dev->bus->lock);
1126
1127 }
1128
1129
1130 usb_endpoint_descriptor_t *
1131 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1132 {
1133 struct usbd_endpoint *ep;
1134 int i;
1135
1136 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1137 ep = &iface->endpoints[i];
1138 if (ep->edesc->bEndpointAddress == address)
1139 return (iface->endpoints[i].edesc);
1140 }
1141 return (0);
1142 }
1143
1144 /*
1145 * usbd_ratecheck() can limit the number of error messages that occurs.
1146 * When a device is unplugged it may take up to 0.25s for the hub driver
1147 * to notice it. If the driver continuosly tries to do I/O operations
1148 * this can generate a large number of messages.
1149 */
1150 int
1151 usbd_ratecheck(struct timeval *last)
1152 {
1153 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1154
1155 return (ratecheck(last, &errinterval));
1156 }
1157
1158 /*
1159 * Search for a vendor/product pair in an array. The item size is
1160 * given as an argument.
1161 */
1162 const struct usb_devno *
1163 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1164 u_int16_t vendor, u_int16_t product)
1165 {
1166 while (nentries-- > 0) {
1167 u_int16_t tproduct = tbl->ud_product;
1168 if (tbl->ud_vendor == vendor &&
1169 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1170 return (tbl);
1171 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1172 }
1173 return (NULL);
1174 }
1175
1176
1177 void
1178 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1179 {
1180 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1181
1182 iter->cur = (const uByte *)cd;
1183 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1184 }
1185
1186 const usb_descriptor_t *
1187 usb_desc_iter_next(usbd_desc_iter_t *iter)
1188 {
1189 const usb_descriptor_t *desc;
1190
1191 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1192 if (iter->cur != iter->end)
1193 printf("usb_desc_iter_next: bad descriptor\n");
1194 return NULL;
1195 }
1196 desc = (const usb_descriptor_t *)iter->cur;
1197 if (desc->bLength == 0) {
1198 printf("usb_desc_iter_next: descriptor length = 0\n");
1199 return NULL;
1200 }
1201 iter->cur += desc->bLength;
1202 if (iter->cur > iter->end) {
1203 printf("usb_desc_iter_next: descriptor length too large\n");
1204 return NULL;
1205 }
1206 return desc;
1207 }
1208
1209 usbd_status
1210 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1211 {
1212 return usbd_get_string0(dev, si, buf, 1);
1213 }
1214
1215 usbd_status
1216 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
1217 {
1218 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1219 usb_string_descriptor_t us;
1220 char *s;
1221 int i, n;
1222 u_int16_t c;
1223 usbd_status err;
1224 int size;
1225
1226 buf[0] = '\0';
1227 if (si == 0)
1228 return (USBD_INVAL);
1229 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1230 return (USBD_STALLED);
1231 if (dev->langid == USBD_NOLANG) {
1232 /* Set up default language */
1233 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1234 &size);
1235 if (err || size < 4) {
1236 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1237 dev->langid = 0; /* Well, just pick something then */
1238 } else {
1239 /* Pick the first language as the default. */
1240 dev->langid = UGETW(us.bString[0]);
1241 }
1242 }
1243 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1244 if (err)
1245 return (err);
1246 s = buf;
1247 n = size / 2 - 1;
1248 if (unicode) {
1249 for (i = 0; i < n; i++) {
1250 c = UGETW(us.bString[i]);
1251 if (swap)
1252 c = (c >> 8) | (c << 8);
1253 s += wput_utf8(s, 3, c);
1254 }
1255 *s++ = 0;
1256 }
1257 #ifdef COMPAT_30
1258 else {
1259 for (i = 0; i < n; i++) {
1260 c = UGETW(us.bString[i]);
1261 if (swap)
1262 c = (c >> 8) | (c << 8);
1263 *s++ = (c < 0x80) ? c : '?';
1264 }
1265 *s++ = 0;
1266 }
1267 #endif
1268 return (USBD_NORMAL_COMPLETION);
1269 }
1270