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