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