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