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