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