usbdi.c revision 1.90 1 /* $NetBSD: usbdi.c,v 1.90 2001/12/03 01:47:12 augustss 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.90 2001/12/03 01:47:12 augustss Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #elif defined(__FreeBSD__)
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/conf.h>
53 #include "usb_if.h"
54 #if defined(DIAGNOSTIC) && defined(__i386__)
55 #include <machine/cpu.h>
56 #endif
57 #endif
58 #include <sys/malloc.h>
59 #include <sys/proc.h>
60
61 #include <machine/bus.h>
62
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdivar.h>
67 #include <dev/usb/usb_mem.h>
68
69 #if defined(__FreeBSD__)
70 #include "usb_if.h"
71 #endif
72
73 #ifdef USB_DEBUG
74 #define DPRINTF(x) if (usbdebug) logprintf x
75 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
76 extern int usbdebug;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n,x)
80 #endif
81
82 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
83 Static void usbd_do_request_async_cb
84 (usbd_xfer_handle, usbd_private_handle, usbd_status);
85 Static void usbd_start_next(usbd_pipe_handle pipe);
86 Static usbd_status usbd_open_pipe_ival
87 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
88
89 Static int usbd_nbuses = 0;
90
91 void
92 usbd_init(void)
93 {
94 usbd_nbuses++;
95 }
96
97 void
98 usbd_finish(void)
99 {
100 --usbd_nbuses;
101 }
102
103 static __inline int
104 usbd_xfer_isread(usbd_xfer_handle xfer)
105 {
106 if (xfer->rqflags & URQ_REQUEST)
107 return (xfer->request.bmRequestType & UT_READ);
108 else
109 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
110 UE_DIR_IN);
111 }
112
113 #ifdef USB_DEBUG
114 void
115 usbd_dump_iface(struct usbd_interface *iface)
116 {
117 printf("usbd_dump_iface: iface=%p\n", iface);
118 if (iface == NULL)
119 return;
120 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
121 iface->device, iface->idesc, iface->index, iface->altindex,
122 iface->priv);
123 }
124
125 void
126 usbd_dump_device(struct usbd_device *dev)
127 {
128 printf("usbd_dump_device: dev=%p\n", dev);
129 if (dev == NULL)
130 return;
131 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
132 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
133 "power=%d langid=%d\n",
134 dev->address, dev->config, dev->depth, dev->speed,
135 dev->self_powered, dev->power, dev->langid);
136 }
137
138 void
139 usbd_dump_endpoint(struct usbd_endpoint *endp)
140 {
141 printf("usbd_dump_endpoint: endp=%p\n", endp);
142 if (endp == NULL)
143 return;
144 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
145 if (endp->edesc)
146 printf(" bEndpointAddress=0x%02x\n",
147 endp->edesc->bEndpointAddress);
148 }
149
150 void
151 usbd_dump_queue(usbd_pipe_handle pipe)
152 {
153 usbd_xfer_handle xfer;
154
155 printf("usbd_dump_queue: pipe=%p\n", pipe);
156 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
157 xfer;
158 xfer = SIMPLEQ_NEXT(xfer, next)) {
159 printf(" xfer=%p\n", xfer);
160 }
161 }
162
163 void
164 usbd_dump_pipe(usbd_pipe_handle pipe)
165 {
166 printf("usbd_dump_pipe: pipe=%p\n", pipe);
167 if (pipe == NULL)
168 return;
169 usbd_dump_iface(pipe->iface);
170 usbd_dump_device(pipe->device);
171 usbd_dump_endpoint(pipe->endpoint);
172 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
173 pipe->refcnt, pipe->running, pipe->aborting);
174 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
175 pipe->intrxfer, pipe->repeat, pipe->interval);
176 }
177 #endif
178
179 usbd_status
180 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
181 u_int8_t flags, usbd_pipe_handle *pipe)
182 {
183 return (usbd_open_pipe_ival(iface, address, flags, pipe,
184 USBD_DEFAULT_INTERVAL));
185 }
186
187 usbd_status
188 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
189 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
190 {
191 usbd_pipe_handle p;
192 struct usbd_endpoint *ep;
193 usbd_status err;
194 int i;
195
196 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
197 iface, address, flags));
198
199 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
200 ep = &iface->endpoints[i];
201 if (ep->edesc == NULL)
202 return (USBD_IOERROR);
203 if (ep->edesc->bEndpointAddress == address)
204 goto found;
205 }
206 return (USBD_BAD_ADDRESS);
207 found:
208 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
209 return (USBD_IN_USE);
210 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
211 if (err)
212 return (err);
213 LIST_INSERT_HEAD(&iface->pipes, p, next);
214 *pipe = p;
215 return (USBD_NORMAL_COMPLETION);
216 }
217
218 usbd_status
219 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
220 u_int8_t flags, usbd_pipe_handle *pipe,
221 usbd_private_handle priv, void *buffer, u_int32_t len,
222 usbd_callback cb, int ival)
223 {
224 usbd_status err;
225 usbd_xfer_handle xfer;
226 usbd_pipe_handle ipipe;
227
228 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
229 address, flags, len));
230
231 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
232 &ipipe, ival);
233 if (err)
234 return (err);
235 xfer = usbd_alloc_xfer(iface->device);
236 if (xfer == NULL) {
237 err = USBD_NOMEM;
238 goto bad1;
239 }
240 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
241 USBD_NO_TIMEOUT, cb);
242 ipipe->intrxfer = xfer;
243 ipipe->repeat = 1;
244 err = usbd_transfer(xfer);
245 *pipe = ipipe;
246 if (err != USBD_IN_PROGRESS)
247 goto bad2;
248 return (USBD_NORMAL_COMPLETION);
249
250 bad2:
251 ipipe->intrxfer = NULL;
252 ipipe->repeat = 0;
253 usbd_free_xfer(xfer);
254 bad1:
255 usbd_close_pipe(ipipe);
256 return (err);
257 }
258
259 usbd_status
260 usbd_close_pipe(usbd_pipe_handle pipe)
261 {
262 #ifdef DIAGNOSTIC
263 if (pipe == NULL) {
264 printf("usbd_close_pipe: pipe==NULL\n");
265 return (USBD_NORMAL_COMPLETION);
266 }
267 #endif
268
269 if (--pipe->refcnt != 0)
270 return (USBD_NORMAL_COMPLETION);
271 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
272 return (USBD_PENDING_REQUESTS);
273 LIST_REMOVE(pipe, next);
274 pipe->endpoint->refcnt--;
275 pipe->methods->close(pipe);
276 if (pipe->intrxfer != NULL)
277 usbd_free_xfer(pipe->intrxfer);
278 free(pipe, M_USB);
279 return (USBD_NORMAL_COMPLETION);
280 }
281
282 usbd_status
283 usbd_transfer(usbd_xfer_handle xfer)
284 {
285 usbd_pipe_handle pipe = xfer->pipe;
286 usb_dma_t *dmap = &xfer->dmabuf;
287 usbd_status err;
288 u_int size;
289 int s;
290
291 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
292 xfer, xfer->flags, pipe, pipe->running));
293 #ifdef USB_DEBUG
294 if (usbdebug > 5)
295 usbd_dump_queue(pipe);
296 #endif
297 xfer->done = 0;
298
299 if (pipe->aborting)
300 return (USBD_CANCELLED);
301
302 size = xfer->length;
303 /* If there is no buffer, allocate one. */
304 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
305 struct usbd_bus *bus = pipe->device->bus;
306
307 #ifdef DIAGNOSTIC
308 if (xfer->rqflags & URQ_AUTO_DMABUF)
309 printf("usbd_transfer: has old buffer!\n");
310 #endif
311 err = bus->methods->allocm(bus, dmap, size);
312 if (err)
313 return (err);
314 xfer->rqflags |= URQ_AUTO_DMABUF;
315 }
316
317 /* Copy data if going out. */
318 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
319 !usbd_xfer_isread(xfer))
320 memcpy(KERNADDR(dmap), xfer->buffer, size);
321
322 err = pipe->methods->transfer(xfer);
323
324 if (err != USBD_IN_PROGRESS && err) {
325 /* The transfer has not been queued, so free buffer. */
326 if (xfer->rqflags & URQ_AUTO_DMABUF) {
327 struct usbd_bus *bus = pipe->device->bus;
328
329 bus->methods->freem(bus, &xfer->dmabuf);
330 xfer->rqflags &= ~URQ_AUTO_DMABUF;
331 }
332 }
333
334 if (!(xfer->flags & USBD_SYNCHRONOUS))
335 return (err);
336
337 /* Sync transfer, wait for completion. */
338 if (err != USBD_IN_PROGRESS)
339 return (err);
340 s = splusb();
341 if (!xfer->done) {
342 if (pipe->device->bus->use_polling)
343 panic("usbd_transfer: not done\n");
344 tsleep(xfer, PRIBIO, "usbsyn", 0);
345 }
346 splx(s);
347 return (xfer->status);
348 }
349
350 /* Like usbd_transfer(), but waits for completion. */
351 usbd_status
352 usbd_sync_transfer(usbd_xfer_handle xfer)
353 {
354 xfer->flags |= USBD_SYNCHRONOUS;
355 return (usbd_transfer(xfer));
356 }
357
358 void *
359 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
360 {
361 struct usbd_bus *bus = xfer->device->bus;
362 usbd_status err;
363
364 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
365 if (err)
366 return (0);
367 xfer->rqflags |= URQ_DEV_DMABUF;
368 return (KERNADDR(&xfer->dmabuf));
369 }
370
371 void
372 usbd_free_buffer(usbd_xfer_handle xfer)
373 {
374 #ifdef DIAGNOSTIC
375 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
376 printf("usbd_free_buffer: no buffer\n");
377 return;
378 }
379 #endif
380 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
381 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
382 }
383
384 void *
385 usbd_get_buffer(usbd_xfer_handle xfer)
386 {
387 if (!(xfer->rqflags & URQ_DEV_DMABUF))
388 return (0);
389 return (KERNADDR(&xfer->dmabuf));
390 }
391
392 usbd_xfer_handle
393 usbd_alloc_xfer(usbd_device_handle dev)
394 {
395 usbd_xfer_handle xfer;
396
397 xfer = dev->bus->methods->allocx(dev->bus);
398 if (xfer == NULL)
399 return (NULL);
400 xfer->device = dev;
401 usb_callout_init(xfer->timeout_handle);
402 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
403 return (xfer);
404 }
405
406 usbd_status
407 usbd_free_xfer(usbd_xfer_handle xfer)
408 {
409 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
410 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
411 usbd_free_buffer(xfer);
412 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
413 if (callout_pending(&xfer->timeout_handle)) {
414 callout_stop(&xfer->timeout_handle);
415 printf("usbd_free_xfer: timout_handle pending");
416 }
417 #endif
418 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
419 return (USBD_NORMAL_COMPLETION);
420 }
421
422 void
423 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
424 usbd_private_handle priv, void *buffer, u_int32_t length,
425 u_int16_t flags, u_int32_t timeout,
426 usbd_callback callback)
427 {
428 xfer->pipe = pipe;
429 xfer->priv = priv;
430 xfer->buffer = buffer;
431 xfer->length = length;
432 xfer->actlen = 0;
433 xfer->flags = flags;
434 xfer->timeout = timeout;
435 xfer->status = USBD_NOT_STARTED;
436 xfer->callback = callback;
437 xfer->rqflags &= ~URQ_REQUEST;
438 xfer->nframes = 0;
439 }
440
441 void
442 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
443 usbd_private_handle priv, u_int32_t timeout,
444 usb_device_request_t *req, void *buffer,
445 u_int32_t length, u_int16_t flags,
446 usbd_callback callback)
447 {
448 xfer->pipe = dev->default_pipe;
449 xfer->priv = priv;
450 xfer->buffer = buffer;
451 xfer->length = length;
452 xfer->actlen = 0;
453 xfer->flags = flags;
454 xfer->timeout = timeout;
455 xfer->status = USBD_NOT_STARTED;
456 xfer->callback = callback;
457 xfer->request = *req;
458 xfer->rqflags |= URQ_REQUEST;
459 xfer->nframes = 0;
460 }
461
462 void
463 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
464 usbd_private_handle priv, u_int16_t *frlengths,
465 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
466 {
467 xfer->pipe = pipe;
468 xfer->priv = priv;
469 xfer->buffer = 0;
470 xfer->length = 0;
471 xfer->actlen = 0;
472 xfer->flags = flags;
473 xfer->timeout = USBD_NO_TIMEOUT;
474 xfer->status = USBD_NOT_STARTED;
475 xfer->callback = callback;
476 xfer->rqflags &= ~URQ_REQUEST;
477 xfer->frlengths = frlengths;
478 xfer->nframes = nframes;
479 }
480
481 void
482 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
483 void **buffer, u_int32_t *count, usbd_status *status)
484 {
485 if (priv != NULL)
486 *priv = xfer->priv;
487 if (buffer != NULL)
488 *buffer = xfer->buffer;
489 if (count != NULL)
490 *count = xfer->actlen;
491 if (status != NULL)
492 *status = xfer->status;
493 }
494
495 usb_config_descriptor_t *
496 usbd_get_config_descriptor(usbd_device_handle dev)
497 {
498 #ifdef DIAGNOSTIC
499 if (dev == NULL) {
500 printf("usbd_get_config_descriptor: dev == NULL\n");
501 return (NULL);
502 }
503 #endif
504 return (dev->cdesc);
505 }
506
507 usb_interface_descriptor_t *
508 usbd_get_interface_descriptor(usbd_interface_handle iface)
509 {
510 #ifdef DIAGNOSTIC
511 if (iface == NULL) {
512 printf("usbd_get_interface_descriptor: dev == NULL\n");
513 return (NULL);
514 }
515 #endif
516 return (iface->idesc);
517 }
518
519 usb_device_descriptor_t *
520 usbd_get_device_descriptor(usbd_device_handle dev)
521 {
522 return (&dev->ddesc);
523 }
524
525 usb_endpoint_descriptor_t *
526 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
527 {
528 if (index >= iface->idesc->bNumEndpoints)
529 return (0);
530 return (iface->endpoints[index].edesc);
531 }
532
533 usbd_status
534 usbd_abort_pipe(usbd_pipe_handle pipe)
535 {
536 usbd_status err;
537 int s;
538
539 #ifdef DIAGNOSTIC
540 if (pipe == NULL) {
541 printf("usbd_close_pipe: pipe==NULL\n");
542 return (USBD_NORMAL_COMPLETION);
543 }
544 #endif
545 s = splusb();
546 err = usbd_ar_pipe(pipe);
547 splx(s);
548 return (err);
549 }
550
551 usbd_status
552 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
553 {
554 usbd_device_handle dev = pipe->device;
555 usb_device_request_t req;
556 usbd_status err;
557
558 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
559
560 /*
561 * Clearing en endpoint stall resets the endpoint toggle, so
562 * do the same to the HC toggle.
563 */
564 pipe->methods->cleartoggle(pipe);
565
566 req.bmRequestType = UT_WRITE_ENDPOINT;
567 req.bRequest = UR_CLEAR_FEATURE;
568 USETW(req.wValue, UF_ENDPOINT_HALT);
569 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
570 USETW(req.wLength, 0);
571 err = usbd_do_request(dev, &req, 0);
572 #if 0
573 XXX should we do this?
574 if (!err) {
575 pipe->state = USBD_PIPE_ACTIVE;
576 /* XXX activate pipe */
577 }
578 #endif
579 return (err);
580 }
581
582 usbd_status
583 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
584 {
585 usbd_device_handle dev = pipe->device;
586 usb_device_request_t req;
587 usbd_status err;
588
589 pipe->methods->cleartoggle(pipe);
590
591 req.bmRequestType = UT_WRITE_ENDPOINT;
592 req.bRequest = UR_CLEAR_FEATURE;
593 USETW(req.wValue, UF_ENDPOINT_HALT);
594 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
595 USETW(req.wLength, 0);
596 err = usbd_do_request_async(dev, &req, 0);
597 return (err);
598 }
599
600 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
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 repeat = pipe->repeat;
761 int polling;
762
763 SPLUSBCHECK;
764
765 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
766 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
767
768 #ifdef DIAGNOSTIC
769 if (pipe == NULL) {
770 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
771 return;
772 }
773 #endif
774 polling = pipe->device->bus->use_polling;
775 /* XXXX */
776 if (polling)
777 pipe->running = 0;
778
779 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
780 usbd_xfer_isread(xfer)) {
781 #ifdef DIAGNOSTIC
782 if (xfer->actlen > xfer->length) {
783 printf("usb_transfer_complete: actlen > len %d > %d\n",
784 xfer->actlen, xfer->length);
785 xfer->actlen = xfer->length;
786 }
787 #endif
788 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
789 }
790
791 /* if we allocated the buffer in usbd_transfer() we free it here. */
792 if (xfer->rqflags & URQ_AUTO_DMABUF) {
793 if (!repeat) {
794 struct usbd_bus *bus = pipe->device->bus;
795 bus->methods->freem(bus, dmap);
796 xfer->rqflags &= ~URQ_AUTO_DMABUF;
797 }
798 }
799
800 if (!repeat) {
801 /* Remove request from queue. */
802 #ifdef DIAGNOSTIC
803 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
804 printf("usb_transfer_complete: bad dequeue %p != %p\n",
805 xfer, SIMPLEQ_FIRST(&pipe->queue));
806 #endif
807 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
808 }
809 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
810 repeat, SIMPLEQ_FIRST(&pipe->queue)));
811
812 /* Count completed transfers. */
813 ++pipe->device->bus->stats.requests
814 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
815
816 xfer->done = 1;
817 if (!xfer->status && xfer->actlen < xfer->length &&
818 !(xfer->flags & USBD_SHORT_XFER_OK)) {
819 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
820 xfer->actlen, xfer->length));
821 xfer->status = USBD_SHORT_XFER;
822 }
823
824 if (xfer->callback)
825 xfer->callback(xfer, xfer->priv, xfer->status);
826
827 #ifdef DIAGNOSTIC
828 if (pipe->methods->done != NULL)
829 pipe->methods->done(xfer);
830 else
831 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
832 #else
833 pipe->methods->done(xfer);
834 #endif
835
836 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
837 wakeup(xfer);
838
839 if (!repeat) {
840 /* XXX should we stop the queue on all errors? */
841 if ((xfer->status == USBD_CANCELLED ||
842 xfer->status == USBD_TIMEOUT) &&
843 pipe->iface != NULL) /* not control pipe */
844 pipe->running = 0;
845 else
846 usbd_start_next(pipe);
847 }
848 }
849
850 usbd_status
851 usb_insert_transfer(usbd_xfer_handle xfer)
852 {
853 usbd_pipe_handle pipe = xfer->pipe;
854 usbd_status err;
855 int s;
856
857 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
858 pipe, pipe->running, xfer->timeout));
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 }
911
912 usbd_status
913 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
914 void *data, u_int16_t flags, int *actlen)
915 {
916 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
917 data, flags, actlen));
918 }
919
920 usbd_status
921 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
922 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
923 {
924 usbd_xfer_handle xfer;
925 usbd_status err;
926
927 #ifdef DIAGNOSTIC
928 #if defined(__i386__) && defined(__FreeBSD__)
929 KASSERT(intr_nesting_level == 0,
930 ("usbd_do_request: in interrupt context"));
931 #endif
932 if (dev->bus->intr_context) {
933 printf("usbd_do_request: not in process context\n");
934 return (USBD_INVAL);
935 }
936 #endif
937
938 xfer = usbd_alloc_xfer(dev);
939 if (xfer == NULL)
940 return (USBD_NOMEM);
941 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
942 data, UGETW(req->wLength), flags, 0);
943 xfer->pipe = pipe;
944 err = usbd_sync_transfer(xfer);
945 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
946 if (xfer->actlen > xfer->length)
947 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
948 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
949 dev->address, xfer->request.bmRequestType,
950 xfer->request.bRequest, UGETW(xfer->request.wValue),
951 UGETW(xfer->request.wIndex),
952 UGETW(xfer->request.wLength),
953 xfer->length, xfer->actlen));
954 #endif
955 if (actlen != NULL)
956 *actlen = xfer->actlen;
957 if (err == USBD_STALLED) {
958 /*
959 * The control endpoint has stalled. Control endpoints
960 * should not halt, but some may do so anyway so clear
961 * any halt condition.
962 */
963 usb_device_request_t treq;
964 usb_status_t status;
965 u_int16_t s;
966 usbd_status nerr;
967
968 treq.bmRequestType = UT_READ_ENDPOINT;
969 treq.bRequest = UR_GET_STATUS;
970 USETW(treq.wValue, 0);
971 USETW(treq.wIndex, 0);
972 USETW(treq.wLength, sizeof(usb_status_t));
973 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
974 &treq, &status,sizeof(usb_status_t),
975 0, 0);
976 nerr = usbd_sync_transfer(xfer);
977 if (nerr)
978 goto bad;
979 s = UGETW(status.wStatus);
980 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
981 if (!(s & UES_HALT))
982 goto bad;
983 treq.bmRequestType = UT_WRITE_ENDPOINT;
984 treq.bRequest = UR_CLEAR_FEATURE;
985 USETW(treq.wValue, UF_ENDPOINT_HALT);
986 USETW(treq.wIndex, 0);
987 USETW(treq.wLength, 0);
988 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
989 &treq, &status, 0, 0, 0);
990 nerr = usbd_sync_transfer(xfer);
991 if (nerr)
992 goto bad;
993 }
994
995 bad:
996 usbd_free_xfer(xfer);
997 return (err);
998 }
999
1000 void
1001 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1002 usbd_status status)
1003 {
1004 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1005 if (xfer->actlen > xfer->length)
1006 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1007 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1008 xfer->pipe->device->address,
1009 xfer->request.bmRequestType,
1010 xfer->request.bRequest, UGETW(xfer->request.wValue),
1011 UGETW(xfer->request.wIndex),
1012 UGETW(xfer->request.wLength),
1013 xfer->length, xfer->actlen));
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 }
1073
1074
1075 usb_endpoint_descriptor_t *
1076 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1077 {
1078 struct usbd_endpoint *ep;
1079 int i;
1080
1081 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1082 ep = &iface->endpoints[i];
1083 if (ep->edesc->bEndpointAddress == address)
1084 return (iface->endpoints[i].edesc);
1085 }
1086 return (0);
1087 }
1088
1089 /*
1090 * usbd_ratecheck() can limit the number of error messages that occurs.
1091 * When a device is unplugged it may take up to 0.25s for the hub driver
1092 * to notice it. If the driver continuosly tries to do I/O operations
1093 * this can generate a large number of messages.
1094 */
1095 int
1096 usbd_ratecheck(struct timeval *last)
1097 {
1098 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1099
1100 return (ratecheck(last, &errinterval));
1101 }
1102
1103 /*
1104 * Search for a vendor/product pair in an array. The item size is
1105 * given as an argument.
1106 */
1107 const struct usb_devno *
1108 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1109 u_int16_t vendor, u_int16_t product)
1110 {
1111 while (nentries-- > 0) {
1112 if (tbl->ud_vendor == vendor && tbl->ud_product == product)
1113 return (tbl);
1114 tbl = (struct usb_devno *)((char *)tbl + sz);
1115 }
1116 return (NULL);
1117 }
1118
1119 #if defined(__FreeBSD__)
1120 int
1121 usbd_driver_load(module_t mod, int what, void *arg)
1122 {
1123 /* XXX should implement something like a function that removes all generic devices */
1124
1125 return (0);
1126 }
1127
1128 #endif
1129