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