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