usbdi.c revision 1.85 1 /* $NetBSD: usbdi.c,v 1.85 2001/11/15 15:15:59 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.85 2001/11/15 15:15:59 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 lowspeed=%d self_powered=%d power=%d langid=%d\n",
133 dev->address, dev->config, dev->depth, dev->lowspeed,
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 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
156 xfer;
157 xfer = SIMPLEQ_NEXT(xfer, next)) {
158 printf(" xfer=%p\n", xfer);
159 }
160 }
161
162 void
163 usbd_dump_pipe(usbd_pipe_handle pipe)
164 {
165 printf("usbd_dump_pipe: pipe=%p\n", pipe);
166 if (pipe == NULL)
167 return;
168 usbd_dump_iface(pipe->iface);
169 usbd_dump_device(pipe->device);
170 usbd_dump_endpoint(pipe->endpoint);
171 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
172 pipe->refcnt, pipe->running, pipe->aborting);
173 printf("intrxfer=%p, repeat=%d, interval=%d\n",
174 pipe->intrxfer, pipe->repeat, pipe->interval);
175 }
176 #endif
177
178 usbd_status
179 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
180 u_int8_t flags, usbd_pipe_handle *pipe)
181 {
182 return (usbd_open_pipe_ival(iface, address, flags, pipe,
183 USBD_DEFAULT_INTERVAL));
184 }
185
186 usbd_status
187 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
188 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
189 {
190 usbd_pipe_handle p;
191 struct usbd_endpoint *ep;
192 usbd_status err;
193 int i;
194
195 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
196 iface, address, flags));
197
198 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
199 ep = &iface->endpoints[i];
200 if (ep->edesc == NULL)
201 return (USBD_IOERROR);
202 if (ep->edesc->bEndpointAddress == address)
203 goto found;
204 }
205 return (USBD_BAD_ADDRESS);
206 found:
207 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
208 return (USBD_IN_USE);
209 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
210 if (err)
211 return (err);
212 LIST_INSERT_HEAD(&iface->pipes, p, next);
213 *pipe = p;
214 return (USBD_NORMAL_COMPLETION);
215 }
216
217 usbd_status
218 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
219 u_int8_t flags, usbd_pipe_handle *pipe,
220 usbd_private_handle priv, void *buffer, u_int32_t len,
221 usbd_callback cb, int ival)
222 {
223 usbd_status err;
224 usbd_xfer_handle xfer;
225 usbd_pipe_handle ipipe;
226
227 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
228 address, flags, len));
229
230 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
231 &ipipe, ival);
232 if (err)
233 return (err);
234 xfer = usbd_alloc_xfer(iface->device);
235 if (xfer == NULL) {
236 err = USBD_NOMEM;
237 goto bad1;
238 }
239 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
240 USBD_NO_TIMEOUT, cb);
241 ipipe->intrxfer = xfer;
242 ipipe->repeat = 1;
243 err = usbd_transfer(xfer);
244 *pipe = ipipe;
245 if (err != USBD_IN_PROGRESS)
246 goto bad2;
247 return (USBD_NORMAL_COMPLETION);
248
249 bad2:
250 ipipe->intrxfer = NULL;
251 ipipe->repeat = 0;
252 usbd_free_xfer(xfer);
253 bad1:
254 usbd_close_pipe(ipipe);
255 return (err);
256 }
257
258 usbd_status
259 usbd_close_pipe(usbd_pipe_handle pipe)
260 {
261 #ifdef DIAGNOSTIC
262 if (pipe == NULL) {
263 printf("usbd_close_pipe: pipe==NULL\n");
264 return (USBD_NORMAL_COMPLETION);
265 }
266 #endif
267
268 if (--pipe->refcnt != 0)
269 return (USBD_NORMAL_COMPLETION);
270 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
271 return (USBD_PENDING_REQUESTS);
272 LIST_REMOVE(pipe, next);
273 pipe->endpoint->refcnt--;
274 pipe->methods->close(pipe);
275 if (pipe->intrxfer != NULL)
276 usbd_free_xfer(pipe->intrxfer);
277 free(pipe, M_USB);
278 return (USBD_NORMAL_COMPLETION);
279 }
280
281 usbd_status
282 usbd_transfer(usbd_xfer_handle xfer)
283 {
284 usbd_pipe_handle pipe = xfer->pipe;
285 usb_dma_t *dmap = &xfer->dmabuf;
286 usbd_status err;
287 u_int size;
288 int s;
289
290 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
291 xfer, xfer->flags, pipe, pipe->running));
292 #ifdef USB_DEBUG
293 if (usbdebug > 5)
294 usbd_dump_queue(pipe);
295 #endif
296 xfer->done = 0;
297
298 if (pipe->aborting)
299 return (USBD_CANCELLED);
300
301 size = xfer->length;
302 /* If there is no buffer, allocate one. */
303 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
304 struct usbd_bus *bus = pipe->device->bus;
305
306 #ifdef DIAGNOSTIC
307 if (xfer->rqflags & URQ_AUTO_DMABUF)
308 printf("usbd_transfer: has old buffer!\n");
309 #endif
310 err = bus->methods->allocm(bus, dmap, size);
311 if (err)
312 return (err);
313 xfer->rqflags |= URQ_AUTO_DMABUF;
314 }
315
316 /* Copy data if going out. */
317 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
318 !usbd_xfer_isread(xfer))
319 memcpy(KERNADDR(dmap), xfer->buffer, size);
320
321 err = pipe->methods->transfer(xfer);
322
323 if (err != USBD_IN_PROGRESS && err) {
324 /* The transfer has not been queued, so free buffer. */
325 if (xfer->rqflags & URQ_AUTO_DMABUF) {
326 struct usbd_bus *bus = pipe->device->bus;
327
328 bus->methods->freem(bus, &xfer->dmabuf);
329 xfer->rqflags &= ~URQ_AUTO_DMABUF;
330 }
331 }
332
333 if (!(xfer->flags & USBD_SYNCHRONOUS))
334 return (err);
335
336 /* Sync transfer, wait for completion. */
337 if (err != USBD_IN_PROGRESS)
338 return (err);
339 s = splusb();
340 if (!xfer->done) {
341 if (pipe->device->bus->use_polling)
342 panic("usbd_transfer: not done\n");
343 tsleep(xfer, PRIBIO, "usbsyn", 0);
344 }
345 splx(s);
346 return (xfer->status);
347 }
348
349 /* Like usbd_transfer(), but waits for completion. */
350 usbd_status
351 usbd_sync_transfer(usbd_xfer_handle xfer)
352 {
353 xfer->flags |= USBD_SYNCHRONOUS;
354 return (usbd_transfer(xfer));
355 }
356
357 void *
358 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
359 {
360 struct usbd_bus *bus = xfer->device->bus;
361 usbd_status err;
362
363 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
364 if (err)
365 return (0);
366 xfer->rqflags |= URQ_DEV_DMABUF;
367 return (KERNADDR(&xfer->dmabuf));
368 }
369
370 void
371 usbd_free_buffer(usbd_xfer_handle xfer)
372 {
373 #ifdef DIAGNOSTIC
374 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
375 printf("usbd_free_buffer: no buffer\n");
376 return;
377 }
378 #endif
379 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
380 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
381 }
382
383 void *
384 usbd_get_buffer(usbd_xfer_handle xfer)
385 {
386 if (!(xfer->rqflags & URQ_DEV_DMABUF))
387 return (0);
388 return (KERNADDR(&xfer->dmabuf));
389 }
390
391 usbd_xfer_handle
392 usbd_alloc_xfer(usbd_device_handle dev)
393 {
394 usbd_xfer_handle xfer;
395
396 xfer = dev->bus->methods->allocx(dev->bus);
397 if (xfer == NULL)
398 return (NULL);
399 xfer->device = dev;
400 usb_callout_init(xfer->timeout_handle);
401 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
402 return (xfer);
403 }
404
405 usbd_status
406 usbd_free_xfer(usbd_xfer_handle xfer)
407 {
408 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
409 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
410 usbd_free_buffer(xfer);
411 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
412 if (callout_pending(&xfer->timeout_handle)) {
413 callout_stop(&xfer->timeout_handle);
414 printf("usbd_free_xfer: timout_handle pending");
415 }
416 #endif
417 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
418 return (USBD_NORMAL_COMPLETION);
419 }
420
421 void
422 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
423 usbd_private_handle priv, void *buffer, u_int32_t length,
424 u_int16_t flags, u_int32_t timeout,
425 usbd_callback callback)
426 {
427 xfer->pipe = pipe;
428 xfer->priv = priv;
429 xfer->buffer = buffer;
430 xfer->length = length;
431 xfer->actlen = 0;
432 xfer->flags = flags;
433 xfer->timeout = timeout;
434 xfer->status = USBD_NOT_STARTED;
435 xfer->callback = callback;
436 xfer->rqflags &= ~URQ_REQUEST;
437 xfer->nframes = 0;
438 }
439
440 void
441 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
442 usbd_private_handle priv, u_int32_t timeout,
443 usb_device_request_t *req, void *buffer,
444 u_int32_t length, u_int16_t flags,
445 usbd_callback callback)
446 {
447 xfer->pipe = dev->default_pipe;
448 xfer->priv = priv;
449 xfer->buffer = buffer;
450 xfer->length = length;
451 xfer->actlen = 0;
452 xfer->flags = flags;
453 xfer->timeout = timeout;
454 xfer->status = USBD_NOT_STARTED;
455 xfer->callback = callback;
456 xfer->request = *req;
457 xfer->rqflags |= URQ_REQUEST;
458 xfer->nframes = 0;
459 }
460
461 void
462 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
463 usbd_private_handle priv, u_int16_t *frlengths,
464 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
465 {
466 xfer->pipe = pipe;
467 xfer->priv = priv;
468 xfer->buffer = 0;
469 xfer->length = 0;
470 xfer->actlen = 0;
471 xfer->flags = flags;
472 xfer->timeout = USBD_NO_TIMEOUT;
473 xfer->status = USBD_NOT_STARTED;
474 xfer->callback = callback;
475 xfer->rqflags &= ~URQ_REQUEST;
476 xfer->frlengths = frlengths;
477 xfer->nframes = nframes;
478 }
479
480 void
481 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
482 void **buffer, u_int32_t *count, usbd_status *status)
483 {
484 if (priv != NULL)
485 *priv = xfer->priv;
486 if (buffer != NULL)
487 *buffer = xfer->buffer;
488 if (count != NULL)
489 *count = xfer->actlen;
490 if (status != NULL)
491 *status = xfer->status;
492 }
493
494 usb_config_descriptor_t *
495 usbd_get_config_descriptor(usbd_device_handle dev)
496 {
497 #ifdef DIAGNOSTIC
498 if (dev == NULL) {
499 printf("usbd_get_config_descriptor: dev == NULL\n");
500 return (NULL);
501 }
502 #endif
503 return (dev->cdesc);
504 }
505
506 usb_interface_descriptor_t *
507 usbd_get_interface_descriptor(usbd_interface_handle iface)
508 {
509 #ifdef DIAGNOSTIC
510 if (iface == NULL) {
511 printf("usbd_get_interface_descriptor: dev == NULL\n");
512 return (NULL);
513 }
514 #endif
515 return (iface->idesc);
516 }
517
518 usb_device_descriptor_t *
519 usbd_get_device_descriptor(usbd_device_handle dev)
520 {
521 return (&dev->ddesc);
522 }
523
524 usb_endpoint_descriptor_t *
525 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
526 {
527 if (index >= iface->idesc->bNumEndpoints)
528 return (0);
529 return (iface->endpoints[index].edesc);
530 }
531
532 usbd_status
533 usbd_abort_pipe(usbd_pipe_handle pipe)
534 {
535 usbd_status err;
536 int s;
537
538 #ifdef DIAGNOSTIC
539 if (pipe == NULL) {
540 printf("usbd_close_pipe: pipe==NULL\n");
541 return (USBD_NORMAL_COMPLETION);
542 }
543 #endif
544 s = splusb();
545 err = usbd_ar_pipe(pipe);
546 splx(s);
547 return (err);
548 }
549
550 usbd_status
551 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
552 {
553 usbd_device_handle dev = pipe->device;
554 usb_device_request_t req;
555 usbd_status err;
556
557 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
558
559 /*
560 * Clearing en endpoint stall resets the enpoint toggle, so
561 * do the same to the HC toggle.
562 */
563 pipe->methods->cleartoggle(pipe);
564
565 req.bmRequestType = UT_WRITE_ENDPOINT;
566 req.bRequest = UR_CLEAR_FEATURE;
567 USETW(req.wValue, UF_ENDPOINT_HALT);
568 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
569 USETW(req.wLength, 0);
570 err = usbd_do_request(dev, &req, 0);
571 #if 0
572 XXX should we do this?
573 if (!err) {
574 pipe->state = USBD_PIPE_ACTIVE;
575 /* XXX activate pipe */
576 }
577 #endif
578 return (err);
579 }
580
581 usbd_status
582 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
583 {
584 usbd_device_handle dev = pipe->device;
585 usb_device_request_t req;
586 usbd_status err;
587
588 pipe->methods->cleartoggle(pipe);
589
590 req.bmRequestType = UT_WRITE_ENDPOINT;
591 req.bRequest = UR_CLEAR_FEATURE;
592 USETW(req.wValue, UF_ENDPOINT_HALT);
593 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
594 USETW(req.wLength, 0);
595 err = usbd_do_request_async(dev, &req, 0);
596 return (err);
597 }
598
599 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
600 void
601 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
602 {
603 pipe->methods->cleartoggle(pipe);
604 }
605
606 usbd_status
607 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
608 {
609 #ifdef DIAGNOSTIC
610 if (iface == NULL || iface->idesc == NULL) {
611 printf("usbd_endpoint_count: NULL pointer\n");
612 return (USBD_INVAL);
613 }
614 #endif
615 *count = iface->idesc->bNumEndpoints;
616 return (USBD_NORMAL_COMPLETION);
617 }
618
619 usbd_status
620 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
621 {
622 if (dev->cdesc == NULL)
623 return (USBD_NOT_CONFIGURED);
624 *count = dev->cdesc->bNumInterface;
625 return (USBD_NORMAL_COMPLETION);
626 }
627
628 void
629 usbd_interface2device_handle(usbd_interface_handle iface,
630 usbd_device_handle *dev)
631 {
632 *dev = iface->device;
633 }
634
635 usbd_status
636 usbd_device2interface_handle(usbd_device_handle dev,
637 u_int8_t ifaceno, usbd_interface_handle *iface)
638 {
639 if (dev->cdesc == NULL)
640 return (USBD_NOT_CONFIGURED);
641 if (ifaceno >= dev->cdesc->bNumInterface)
642 return (USBD_INVAL);
643 *iface = &dev->ifaces[ifaceno];
644 return (USBD_NORMAL_COMPLETION);
645 }
646
647 usbd_device_handle
648 usbd_pipe2device_handle(usbd_pipe_handle pipe)
649 {
650 return (pipe->device);
651 }
652
653 /* XXXX use altno */
654 usbd_status
655 usbd_set_interface(usbd_interface_handle iface, int altidx)
656 {
657 usb_device_request_t req;
658 usbd_status err;
659 void *endpoints;
660
661 if (LIST_FIRST(&iface->pipes) != 0)
662 return (USBD_IN_USE);
663
664 endpoints = iface->endpoints;
665 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
666 if (err)
667 return (err);
668
669 /* new setting works, we can free old endpoints */
670 if (endpoints != NULL)
671 free(endpoints, M_USB);
672
673 #ifdef DIAGNOSTIC
674 if (iface->idesc == NULL) {
675 printf("usbd_set_interface: NULL pointer\n");
676 return (USBD_INVAL);
677 }
678 #endif
679
680 req.bmRequestType = UT_WRITE_INTERFACE;
681 req.bRequest = UR_SET_INTERFACE;
682 USETW(req.wValue, iface->idesc->bAlternateSetting);
683 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
684 USETW(req.wLength, 0);
685 return (usbd_do_request(iface->device, &req, 0));
686 }
687
688 int
689 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
690 {
691 char *p = (char *)cdesc;
692 char *end = p + UGETW(cdesc->wTotalLength);
693 usb_interface_descriptor_t *d;
694 int n;
695
696 for (n = 0; p < end; p += d->bLength) {
697 d = (usb_interface_descriptor_t *)p;
698 if (p + d->bLength <= end &&
699 d->bDescriptorType == UDESC_INTERFACE &&
700 d->bInterfaceNumber == ifaceno)
701 n++;
702 }
703 return (n);
704 }
705
706 int
707 usbd_get_interface_altindex(usbd_interface_handle iface)
708 {
709 return (iface->altindex);
710 }
711
712 usbd_status
713 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
714 {
715 usb_device_request_t req;
716
717 req.bmRequestType = UT_READ_INTERFACE;
718 req.bRequest = UR_GET_INTERFACE;
719 USETW(req.wValue, 0);
720 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
721 USETW(req.wLength, 1);
722 return (usbd_do_request(iface->device, &req, aiface));
723 }
724
725 /*** Internal routines ***/
726
727 /* Dequeue all pipe operations, called at splusb(). */
728 Static usbd_status
729 usbd_ar_pipe(usbd_pipe_handle pipe)
730 {
731 usbd_xfer_handle xfer;
732
733 SPLUSBCHECK;
734
735 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
736 #ifdef USB_DEBUG
737 if (usbdebug > 5)
738 usbd_dump_queue(pipe);
739 #endif
740 pipe->repeat = 0;
741 pipe->aborting = 1;
742 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
743 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
744 pipe, xfer, pipe->methods));
745 /* Make the HC abort it (and invoke the callback). */
746 pipe->methods->abort(xfer);
747 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
748 }
749 pipe->aborting = 0;
750 return (USBD_NORMAL_COMPLETION);
751 }
752
753 /* Called at splusb() */
754 void
755 usb_transfer_complete(usbd_xfer_handle xfer)
756 {
757 usbd_pipe_handle pipe = xfer->pipe;
758 usb_dma_t *dmap = &xfer->dmabuf;
759 int repeat = pipe->repeat;
760 int polling;
761
762 SPLUSBCHECK;
763
764 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
765 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
766
767 #ifdef DIAGNOSTIC
768 if (pipe == NULL) {
769 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
770 return;
771 }
772 #endif
773 polling = pipe->device->bus->use_polling;
774 /* XXXX */
775 if (polling)
776 pipe->running = 0;
777
778 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
779 usbd_xfer_isread(xfer)) {
780 #ifdef DIAGNOSTIC
781 if (xfer->actlen > xfer->length) {
782 printf("usb_transfer_complete: actlen > len %d > %d\n",
783 xfer->actlen, xfer->length);
784 xfer->actlen = xfer->length;
785 }
786 #endif
787 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
788 }
789
790 /* if we allocated the buffer in usbd_transfer() we free it here. */
791 if (xfer->rqflags & URQ_AUTO_DMABUF) {
792 if (!repeat) {
793 struct usbd_bus *bus = pipe->device->bus;
794 bus->methods->freem(bus, dmap);
795 xfer->rqflags &= ~URQ_AUTO_DMABUF;
796 }
797 }
798
799 if (!repeat) {
800 /* Remove request from queue. */
801 #ifdef DIAGNOSTIC
802 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
803 printf("usb_transfer_complete: bad dequeue %p != %p\n",
804 xfer, SIMPLEQ_FIRST(&pipe->queue));
805 #endif
806 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, 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.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 ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
836 wakeup(xfer);
837
838 if (!repeat) {
839 /* XXX should we stop the queue on all errors? */
840 if ((xfer->status == USBD_CANCELLED ||
841 xfer->status == USBD_TIMEOUT) &&
842 pipe->iface != NULL) /* not control pipe */
843 pipe->running = 0;
844 else
845 usbd_start_next(pipe);
846 }
847 }
848
849 usbd_status
850 usb_insert_transfer(usbd_xfer_handle xfer)
851 {
852 usbd_pipe_handle pipe = xfer->pipe;
853 usbd_status err;
854 int s;
855
856 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
857 pipe, pipe->running, xfer->timeout));
858 s = splusb();
859 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
860 if (pipe->running)
861 err = USBD_IN_PROGRESS;
862 else {
863 pipe->running = 1;
864 err = USBD_NORMAL_COMPLETION;
865 }
866 splx(s);
867 return (err);
868 }
869
870 /* Called at splusb() */
871 void
872 usbd_start_next(usbd_pipe_handle pipe)
873 {
874 usbd_xfer_handle xfer;
875 usbd_status err;
876
877 SPLUSBCHECK;
878
879 #ifdef DIAGNOSTIC
880 if (pipe == NULL) {
881 printf("usbd_start_next: pipe == NULL\n");
882 return;
883 }
884 if (pipe->methods == NULL || pipe->methods->start == NULL) {
885 printf("usbd_start_next: pipe=%p no start method\n", pipe);
886 return;
887 }
888 #endif
889
890 /* Get next request in queue. */
891 xfer = SIMPLEQ_FIRST(&pipe->queue);
892 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
893 if (xfer == NULL) {
894 pipe->running = 0;
895 } else {
896 err = pipe->methods->start(xfer);
897 if (err != USBD_IN_PROGRESS) {
898 printf("usbd_start_next: error=%d\n", err);
899 pipe->running = 0;
900 /* XXX do what? */
901 }
902 }
903 }
904
905 usbd_status
906 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
907 {
908 return (usbd_do_request_flags(dev, req, data, 0, 0));
909 }
910
911 usbd_status
912 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
913 void *data, u_int16_t flags, int *actlen)
914 {
915 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
916 data, flags, actlen));
917 }
918
919 usbd_status
920 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
921 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
922 {
923 usbd_xfer_handle xfer;
924 usbd_status err;
925
926 #ifdef DIAGNOSTIC
927 #if defined(__i386__) && defined(__FreeBSD__)
928 KASSERT(intr_nesting_level == 0,
929 ("usbd_do_request: in interrupt context"));
930 #endif
931 if (dev->bus->intr_context) {
932 printf("usbd_do_request: not in process context\n");
933 return (USBD_INVAL);
934 }
935 #endif
936
937 xfer = usbd_alloc_xfer(dev);
938 if (xfer == NULL)
939 return (USBD_NOMEM);
940 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
941 data, UGETW(req->wLength), flags, 0);
942 xfer->pipe = pipe;
943 err = usbd_sync_transfer(xfer);
944 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
945 if (xfer->actlen > xfer->length)
946 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
947 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
948 dev->address, xfer->request.bmRequestType,
949 xfer->request.bRequest, UGETW(xfer->request.wValue),
950 UGETW(xfer->request.wIndex),
951 UGETW(xfer->request.wLength),
952 xfer->length, xfer->actlen));
953 #endif
954 if (actlen != NULL)
955 *actlen = xfer->actlen;
956 if (err == USBD_STALLED) {
957 /*
958 * The control endpoint has stalled. Control endpoints
959 * should not halt, but some may do so anyway so clear
960 * any halt condition.
961 */
962 usb_device_request_t treq;
963 usb_status_t status;
964 u_int16_t s;
965 usbd_status nerr;
966
967 treq.bmRequestType = UT_READ_ENDPOINT;
968 treq.bRequest = UR_GET_STATUS;
969 USETW(treq.wValue, 0);
970 USETW(treq.wIndex, 0);
971 USETW(treq.wLength, sizeof(usb_status_t));
972 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
973 &treq, &status,sizeof(usb_status_t),
974 0, 0);
975 nerr = usbd_sync_transfer(xfer);
976 if (nerr)
977 goto bad;
978 s = UGETW(status.wStatus);
979 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
980 if (!(s & UES_HALT))
981 goto bad;
982 treq.bmRequestType = UT_WRITE_ENDPOINT;
983 treq.bRequest = UR_CLEAR_FEATURE;
984 USETW(treq.wValue, UF_ENDPOINT_HALT);
985 USETW(treq.wIndex, 0);
986 USETW(treq.wLength, 0);
987 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
988 &treq, &status, 0, 0, 0);
989 nerr = usbd_sync_transfer(xfer);
990 if (nerr)
991 goto bad;
992 }
993
994 bad:
995 usbd_free_xfer(xfer);
996 return (err);
997 }
998
999 void
1000 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1001 usbd_status status)
1002 {
1003 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1004 if (xfer->actlen > xfer->length)
1005 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1006 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1007 xfer->pipe->device->address,
1008 xfer->request.bmRequestType,
1009 xfer->request.bRequest, UGETW(xfer->request.wValue),
1010 UGETW(xfer->request.wIndex),
1011 UGETW(xfer->request.wLength),
1012 xfer->length, xfer->actlen));
1013 #endif
1014 usbd_free_xfer(xfer);
1015 }
1016
1017 /*
1018 * Execute a request without waiting for completion.
1019 * Can be used from interrupt context.
1020 */
1021 usbd_status
1022 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1023 void *data)
1024 {
1025 usbd_xfer_handle xfer;
1026 usbd_status err;
1027
1028 xfer = usbd_alloc_xfer(dev);
1029 if (xfer == NULL)
1030 return (USBD_NOMEM);
1031 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1032 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1033 err = usbd_transfer(xfer);
1034 if (err != USBD_IN_PROGRESS) {
1035 usbd_free_xfer(xfer);
1036 return (err);
1037 }
1038 return (USBD_NORMAL_COMPLETION);
1039 }
1040
1041 const struct usbd_quirks *
1042 usbd_get_quirks(usbd_device_handle dev)
1043 {
1044 #ifdef DIAGNOSTIC
1045 if (dev == NULL) {
1046 printf("usbd_get_quirks: dev == NULL\n");
1047 return 0;
1048 }
1049 #endif
1050 return (dev->quirks);
1051 }
1052
1053 /* XXX do periodic free() of free list */
1054
1055 /*
1056 * Called from keyboard driver when in polling mode.
1057 */
1058 void
1059 usbd_dopoll(usbd_interface_handle iface)
1060 {
1061 iface->device->bus->methods->do_poll(iface->device->bus);
1062 }
1063
1064 void
1065 usbd_set_polling(usbd_device_handle dev, int on)
1066 {
1067 if (on)
1068 dev->bus->use_polling++;
1069 else
1070 dev->bus->use_polling--;
1071 }
1072
1073
1074 usb_endpoint_descriptor_t *
1075 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1076 {
1077 struct usbd_endpoint *ep;
1078 int i;
1079
1080 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1081 ep = &iface->endpoints[i];
1082 if (ep->edesc->bEndpointAddress == address)
1083 return (iface->endpoints[i].edesc);
1084 }
1085 return (0);
1086 }
1087
1088 /*
1089 * usbd_ratecheck() can limit the number of error messages that occurs.
1090 * When a device is unplugged it may take up to 0.25s for the hub driver
1091 * to notice it. If the driver continuosly tries to do I/O operations
1092 * this can generate a large number of messages.
1093 */
1094 int
1095 usbd_ratecheck(struct timeval *last)
1096 {
1097 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1098
1099 return (ratecheck(last, &errinterval));
1100 }
1101
1102 #if defined(__FreeBSD__)
1103 int
1104 usbd_driver_load(module_t mod, int what, void *arg)
1105 {
1106 /* XXX should implement something like a function that removes all generic devices */
1107
1108 return (0);
1109 }
1110
1111 #endif
1112