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