Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / bus / usb / usbdi.c
... / ...
CommitLineData
1/*
2 * $NetBSD: usbdi.c,v 1.103 2002/09/27 15:37:38 provos Exp $
3 * $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.84 2003/11/09 23:56:19 joe Exp $
4 * $DragonFly: src/sys/bus/usb/usbdi.c,v 1.11 2006/10/25 20:55:52 dillon Exp $
5 */
6
7/*
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/module.h>
47#include <sys/bus.h>
48#include "usb_if.h"
49#if defined(DIAGNOSTIC) && defined(__i386__)
50#include <machine/cpu.h>
51#endif
52#include <sys/malloc.h>
53#include <sys/proc.h>
54#include <sys/thread2.h>
55
56#include "usb.h"
57#include "usbdi.h"
58#include "usbdi_util.h"
59#include "usbdivar.h"
60#include "usb_mem.h"
61
62#if defined(__FreeBSD__) || defined(__DragonFly__)
63#include "usb_if.h"
64#include <machine/clock.h>
65#define delay(d) DELAY(d)
66#endif
67
68#ifdef USB_DEBUG
69#define DPRINTF(x) if (usbdebug) logprintf x
70#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
71extern int usbdebug;
72#else
73#define DPRINTF(x)
74#define DPRINTFN(n,x)
75#endif
76
77Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
78Static void usbd_do_request_async_cb
79 (usbd_xfer_handle, usbd_private_handle, usbd_status);
80Static void usbd_start_next(usbd_pipe_handle pipe);
81Static usbd_status usbd_open_pipe_ival
82 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
83Static int usbd_xfer_isread(usbd_xfer_handle xfer);
84
85Static int usbd_nbuses = 0;
86
87void
88usbd_init(void)
89{
90 usbd_nbuses++;
91}
92
93void
94usbd_finish(void)
95{
96 --usbd_nbuses;
97}
98
99static __inline int
100usbd_xfer_isread(usbd_xfer_handle xfer)
101{
102 if (xfer->rqflags & URQ_REQUEST)
103 return (xfer->request.bmRequestType & UT_READ);
104 else
105 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
106 UE_DIR_IN);
107}
108
109#ifdef USB_DEBUG
110void
111usbd_dump_iface(struct usbd_interface *iface)
112{
113 printf("usbd_dump_iface: iface=%p\n", iface);
114 if (iface == NULL)
115 return;
116 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
117 iface->device, iface->idesc, iface->index, iface->altindex,
118 iface->priv);
119}
120
121void
122usbd_dump_device(struct usbd_device *dev)
123{
124 printf("usbd_dump_device: dev=%p\n", dev);
125 if (dev == NULL)
126 return;
127 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
128 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
129 "power=%d langid=%d\n",
130 dev->address, dev->config, dev->depth, dev->speed,
131 dev->self_powered, dev->power, dev->langid);
132}
133
134void
135usbd_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
146void
147usbd_dump_queue(usbd_pipe_handle pipe)
148{
149 usbd_xfer_handle xfer;
150
151 printf("usbd_dump_queue: pipe=%p\n", pipe);
152 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
153 printf(" xfer=%p\n", xfer);
154 }
155}
156
157void
158usbd_dump_pipe(usbd_pipe_handle pipe)
159{
160 printf("usbd_dump_pipe: pipe=%p\n", pipe);
161 if (pipe == NULL)
162 return;
163 usbd_dump_iface(pipe->iface);
164 usbd_dump_device(pipe->device);
165 usbd_dump_endpoint(pipe->endpoint);
166 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
167 pipe->refcnt, pipe->running, pipe->aborting);
168 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
169 pipe->intrxfer, pipe->repeat, pipe->interval);
170}
171#endif
172
173usbd_status
174usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
175 u_int8_t flags, usbd_pipe_handle *pipe)
176{
177 return (usbd_open_pipe_ival(iface, address, flags, pipe,
178 USBD_DEFAULT_INTERVAL));
179}
180
181usbd_status
182usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
183 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
184{
185 usbd_pipe_handle p;
186 struct usbd_endpoint *ep;
187 usbd_status err;
188 int i;
189
190 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
191 iface, address, flags));
192
193 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
194 ep = &iface->endpoints[i];
195 if (ep->edesc == NULL)
196 return (USBD_IOERROR);
197 if (ep->edesc->bEndpointAddress == address)
198 goto found;
199 }
200 return (USBD_BAD_ADDRESS);
201 found:
202 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
203 return (USBD_IN_USE);
204 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
205 if (err)
206 return (err);
207 LIST_INSERT_HEAD(&iface->pipes, p, next);
208 *pipe = p;
209 return (USBD_NORMAL_COMPLETION);
210}
211
212usbd_status
213usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
214 u_int8_t flags, usbd_pipe_handle *pipe,
215 usbd_private_handle priv, void *buffer, u_int32_t len,
216 usbd_callback cb, int ival)
217{
218 usbd_status err;
219 usbd_xfer_handle xfer;
220 usbd_pipe_handle ipipe;
221
222 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
223 address, flags, len));
224
225 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
226 &ipipe, ival);
227 if (err)
228 return (err);
229 xfer = usbd_alloc_xfer(iface->device);
230 if (xfer == NULL) {
231 err = USBD_NOMEM;
232 goto bad1;
233 }
234 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
235 USBD_NO_TIMEOUT, cb);
236 ipipe->intrxfer = xfer;
237 ipipe->repeat = 1;
238 err = usbd_transfer(xfer);
239 *pipe = ipipe;
240 if (err != USBD_IN_PROGRESS)
241 goto bad2;
242 return (USBD_NORMAL_COMPLETION);
243
244 bad2:
245 ipipe->intrxfer = NULL;
246 ipipe->repeat = 0;
247 usbd_free_xfer(xfer);
248 bad1:
249 usbd_close_pipe(ipipe);
250 return (err);
251}
252
253usbd_status
254usbd_close_pipe(usbd_pipe_handle pipe)
255{
256#ifdef DIAGNOSTIC
257 if (pipe == NULL) {
258 printf("usbd_close_pipe: pipe==NULL\n");
259 return (USBD_NORMAL_COMPLETION);
260 }
261#endif
262
263 if (--pipe->refcnt != 0)
264 return (USBD_NORMAL_COMPLETION);
265 if (! SIMPLEQ_EMPTY(&pipe->queue))
266 return (USBD_PENDING_REQUESTS);
267 LIST_REMOVE(pipe, next);
268 pipe->endpoint->refcnt--;
269 pipe->methods->close(pipe);
270 if (pipe->intrxfer != NULL)
271 usbd_free_xfer(pipe->intrxfer);
272 kfree(pipe, M_USB);
273 return (USBD_NORMAL_COMPLETION);
274}
275
276usbd_status
277usbd_transfer(usbd_xfer_handle xfer)
278{
279 usbd_pipe_handle pipe = xfer->pipe;
280 usb_dma_t *dmap = &xfer->dmabuf;
281 usbd_status err;
282 u_int size;
283
284 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
285 xfer, xfer->flags, pipe, pipe->running));
286#ifdef USB_DEBUG
287 if (usbdebug > 5)
288 usbd_dump_queue(pipe);
289#endif
290 xfer->done = 0;
291
292 if (pipe->aborting)
293 return (USBD_CANCELLED);
294
295 size = xfer->length;
296 /* If there is no buffer, allocate one. */
297 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
298 struct usbd_bus *bus = pipe->device->bus;
299
300#ifdef DIAGNOSTIC
301 if (xfer->rqflags & URQ_AUTO_DMABUF)
302 printf("usbd_transfer: has old buffer!\n");
303#endif
304 err = bus->methods->allocm(bus, dmap, size);
305 if (err)
306 return (err);
307 xfer->rqflags |= URQ_AUTO_DMABUF;
308 }
309
310 /* Copy data if going out. */
311 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
312 !usbd_xfer_isread(xfer))
313 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
314
315 err = pipe->methods->transfer(xfer);
316
317 if (err != USBD_IN_PROGRESS && err) {
318 /* The transfer has not been queued, so free buffer. */
319 if (xfer->rqflags & URQ_AUTO_DMABUF) {
320 struct usbd_bus *bus = pipe->device->bus;
321
322 bus->methods->freem(bus, &xfer->dmabuf);
323 xfer->rqflags &= ~URQ_AUTO_DMABUF;
324 }
325 }
326
327 if (!(xfer->flags & USBD_SYNCHRONOUS))
328 return (err);
329
330 /* Sync transfer, wait for completion. */
331 if (err != USBD_IN_PROGRESS)
332 return (err);
333 crit_enter();
334 if (!xfer->done) {
335 if (pipe->device->bus->use_polling)
336 panic("usbd_transfer: not done\n");
337 tsleep(xfer, 0, "usbsyn", 0);
338 }
339 crit_exit();
340 return (xfer->status);
341}
342
343/* Like usbd_transfer(), but waits for completion. */
344usbd_status
345usbd_sync_transfer(usbd_xfer_handle xfer)
346{
347 xfer->flags |= USBD_SYNCHRONOUS;
348 return (usbd_transfer(xfer));
349}
350
351void *
352usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
353{
354 struct usbd_bus *bus = xfer->device->bus;
355 usbd_status err;
356
357#ifdef DIAGNOSTIC
358 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
359 printf("usbd_alloc_buffer: xfer already has a buffer\n");
360#endif
361 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
362 if (err)
363 return (NULL);
364 xfer->rqflags |= URQ_DEV_DMABUF;
365 return (KERNADDR(&xfer->dmabuf, 0));
366}
367
368void
369usbd_free_buffer(usbd_xfer_handle xfer)
370{
371#ifdef DIAGNOSTIC
372 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
373 printf("usbd_free_buffer: no buffer\n");
374 return;
375 }
376#endif
377 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
378 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
379}
380
381void *
382usbd_get_buffer(usbd_xfer_handle xfer)
383{
384 if (!(xfer->rqflags & URQ_DEV_DMABUF))
385 return (0);
386 return (KERNADDR(&xfer->dmabuf, 0));
387}
388
389usbd_xfer_handle
390usbd_alloc_xfer(usbd_device_handle dev)
391{
392 usbd_xfer_handle xfer;
393
394 xfer = dev->bus->methods->allocx(dev->bus);
395 if (xfer == NULL)
396 return (NULL);
397 xfer->device = dev;
398 usb_callout_init(xfer->timeout_handle);
399 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
400 return (xfer);
401}
402
403usbd_status
404usbd_free_xfer(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
419void
420usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
421 usbd_private_handle priv, void *buffer, u_int32_t length,
422 u_int16_t flags, u_int32_t timeout,
423 usbd_callback callback)
424{
425 xfer->pipe = pipe;
426 xfer->priv = priv;
427 xfer->buffer = buffer;
428 xfer->length = length;
429 xfer->actlen = 0;
430 xfer->flags = flags;
431 xfer->timeout = timeout;
432 xfer->status = USBD_NOT_STARTED;
433 xfer->callback = callback;
434 xfer->rqflags &= ~URQ_REQUEST;
435 xfer->nframes = 0;
436}
437
438void
439usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
440 usbd_private_handle priv, u_int32_t timeout,
441 usb_device_request_t *req, void *buffer,
442 u_int32_t length, u_int16_t flags,
443 usbd_callback callback)
444{
445 xfer->pipe = dev->default_pipe;
446 xfer->priv = priv;
447 xfer->buffer = buffer;
448 xfer->length = length;
449 xfer->actlen = 0;
450 xfer->flags = flags;
451 xfer->timeout = timeout;
452 xfer->status = USBD_NOT_STARTED;
453 xfer->callback = callback;
454 xfer->request = *req;
455 xfer->rqflags |= URQ_REQUEST;
456 xfer->nframes = 0;
457}
458
459void
460usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
461 usbd_private_handle priv, u_int16_t *frlengths,
462 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
463{
464 xfer->pipe = pipe;
465 xfer->priv = priv;
466 xfer->buffer = 0;
467 xfer->length = 0;
468 xfer->actlen = 0;
469 xfer->flags = flags;
470 xfer->timeout = USBD_NO_TIMEOUT;
471 xfer->status = USBD_NOT_STARTED;
472 xfer->callback = callback;
473 xfer->rqflags &= ~URQ_REQUEST;
474 xfer->frlengths = frlengths;
475 xfer->nframes = nframes;
476}
477
478void
479usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
480 void **buffer, u_int32_t *count, usbd_status *status)
481{
482 if (priv != NULL)
483 *priv = xfer->priv;
484 if (buffer != NULL)
485 *buffer = xfer->buffer;
486 if (count != NULL)
487 *count = xfer->actlen;
488 if (status != NULL)
489 *status = xfer->status;
490}
491
492usb_config_descriptor_t *
493usbd_get_config_descriptor(usbd_device_handle dev)
494{
495#ifdef DIAGNOSTIC
496 if (dev == NULL) {
497 printf("usbd_get_config_descriptor: dev == NULL\n");
498 return (NULL);
499 }
500#endif
501 return (dev->cdesc);
502}
503
504usb_interface_descriptor_t *
505usbd_get_interface_descriptor(usbd_interface_handle iface)
506{
507#ifdef DIAGNOSTIC
508 if (iface == NULL) {
509 printf("usbd_get_interface_descriptor: dev == NULL\n");
510 return (NULL);
511 }
512#endif
513 return (iface->idesc);
514}
515
516usb_device_descriptor_t *
517usbd_get_device_descriptor(usbd_device_handle dev)
518{
519 return (&dev->ddesc);
520}
521
522usb_endpoint_descriptor_t *
523usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
524{
525 if (index >= iface->idesc->bNumEndpoints)
526 return (0);
527 return (iface->endpoints[index].edesc);
528}
529
530usbd_status
531usbd_abort_pipe(usbd_pipe_handle pipe)
532{
533 usbd_status err;
534
535#ifdef DIAGNOSTIC
536 if (pipe == NULL) {
537 printf("usbd_close_pipe: pipe==NULL\n");
538 return (USBD_NORMAL_COMPLETION);
539 }
540#endif
541 crit_enter();
542 err = usbd_ar_pipe(pipe);
543 crit_exit();
544 return (err);
545}
546
547usbd_status
548usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
549{
550 usbd_device_handle dev = pipe->device;
551 usb_device_request_t req;
552 usbd_status err;
553
554 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
555
556 /*
557 * Clearing en endpoint stall resets the endpoint toggle, so
558 * do the same to the HC toggle.
559 */
560 pipe->methods->cleartoggle(pipe);
561
562 req.bmRequestType = UT_WRITE_ENDPOINT;
563 req.bRequest = UR_CLEAR_FEATURE;
564 USETW(req.wValue, UF_ENDPOINT_HALT);
565 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
566 USETW(req.wLength, 0);
567 err = usbd_do_request(dev, &req, 0);
568#if 0
569XXX should we do this?
570 if (!err) {
571 pipe->state = USBD_PIPE_ACTIVE;
572 /* XXX activate pipe */
573 }
574#endif
575 return (err);
576}
577
578usbd_status
579usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
580{
581 usbd_device_handle dev = pipe->device;
582 usb_device_request_t req;
583 usbd_status err;
584
585 pipe->methods->cleartoggle(pipe);
586
587 req.bmRequestType = UT_WRITE_ENDPOINT;
588 req.bRequest = UR_CLEAR_FEATURE;
589 USETW(req.wValue, UF_ENDPOINT_HALT);
590 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
591 USETW(req.wLength, 0);
592 err = usbd_do_request_async(dev, &req, 0);
593 return (err);
594}
595
596void
597usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
598{
599 pipe->methods->cleartoggle(pipe);
600}
601
602usbd_status
603usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
604{
605#ifdef DIAGNOSTIC
606 if (iface == NULL || iface->idesc == NULL) {
607 printf("usbd_endpoint_count: NULL pointer\n");
608 return (USBD_INVAL);
609 }
610#endif
611 *count = iface->idesc->bNumEndpoints;
612 return (USBD_NORMAL_COMPLETION);
613}
614
615usbd_status
616usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
617{
618 if (dev->cdesc == NULL)
619 return (USBD_NOT_CONFIGURED);
620 *count = dev->cdesc->bNumInterface;
621 return (USBD_NORMAL_COMPLETION);
622}
623
624void
625usbd_interface2device_handle(usbd_interface_handle iface,
626 usbd_device_handle *dev)
627{
628 *dev = iface->device;
629}
630
631usbd_status
632usbd_device2interface_handle(usbd_device_handle dev,
633 u_int8_t ifaceno, usbd_interface_handle *iface)
634{
635 if (dev->cdesc == NULL)
636 return (USBD_NOT_CONFIGURED);
637 if (ifaceno >= dev->cdesc->bNumInterface)
638 return (USBD_INVAL);
639 *iface = &dev->ifaces[ifaceno];
640 return (USBD_NORMAL_COMPLETION);
641}
642
643usbd_device_handle
644usbd_pipe2device_handle(usbd_pipe_handle pipe)
645{
646 return (pipe->device);
647}
648
649/* XXXX use altno */
650usbd_status
651usbd_set_interface(usbd_interface_handle iface, int altidx)
652{
653 usb_device_request_t req;
654 usbd_status err;
655 void *endpoints;
656
657 if (LIST_FIRST(&iface->pipes) != 0)
658 return (USBD_IN_USE);
659
660 endpoints = iface->endpoints;
661 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
662 if (err)
663 return (err);
664
665 /* new setting works, we can free old endpoints */
666 if (endpoints != NULL)
667 kfree(endpoints, M_USB);
668
669#ifdef DIAGNOSTIC
670 if (iface->idesc == NULL) {
671 printf("usbd_set_interface: NULL pointer\n");
672 return (USBD_INVAL);
673 }
674#endif
675
676 req.bmRequestType = UT_WRITE_INTERFACE;
677 req.bRequest = UR_SET_INTERFACE;
678 USETW(req.wValue, iface->idesc->bAlternateSetting);
679 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
680 USETW(req.wLength, 0);
681 return (usbd_do_request(iface->device, &req, 0));
682}
683
684int
685usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
686{
687 char *p = (char *)cdesc;
688 char *end = p + UGETW(cdesc->wTotalLength);
689 usb_interface_descriptor_t *d;
690 int n;
691
692 for (n = 0; p < end; p += d->bLength) {
693 d = (usb_interface_descriptor_t *)p;
694 if (p + d->bLength <= end &&
695 d->bDescriptorType == UDESC_INTERFACE &&
696 d->bInterfaceNumber == ifaceno)
697 n++;
698 }
699 return (n);
700}
701
702int
703usbd_get_interface_altindex(usbd_interface_handle iface)
704{
705 return (iface->altindex);
706}
707
708usbd_status
709usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
710{
711 usb_device_request_t req;
712
713 req.bmRequestType = UT_READ_INTERFACE;
714 req.bRequest = UR_GET_INTERFACE;
715 USETW(req.wValue, 0);
716 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
717 USETW(req.wLength, 1);
718 return (usbd_do_request(iface->device, &req, aiface));
719}
720
721/*** Internal routines ***/
722
723/* Dequeue all pipe operations, called from a critical section. */
724Static usbd_status
725usbd_ar_pipe(usbd_pipe_handle pipe)
726{
727 usbd_xfer_handle xfer;
728
729 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
730#ifdef USB_DEBUG
731 if (usbdebug > 5)
732 usbd_dump_queue(pipe);
733#endif
734 pipe->repeat = 0;
735 pipe->aborting = 1;
736 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
737 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
738 pipe, xfer, pipe->methods));
739 /* Make the HC abort it (and invoke the callback). */
740 pipe->methods->abort(xfer);
741 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
742 }
743 pipe->aborting = 0;
744 return (USBD_NORMAL_COMPLETION);
745}
746
747/* Called from a critical section. */
748void
749usb_transfer_complete(usbd_xfer_handle xfer)
750{
751 usbd_pipe_handle pipe = xfer->pipe;
752 usb_dma_t *dmap = &xfer->dmabuf;
753 int repeat = pipe->repeat;
754 int polling;
755
756 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
757 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
758#ifdef DIAGNOSTIC
759 if (xfer->busy_free != XFER_ONQU) {
760 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
761 xfer, xfer->busy_free);
762 return;
763 }
764#endif
765
766#ifdef DIAGNOSTIC
767 if (pipe == NULL) {
768 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
769 return;
770 }
771#endif
772 polling = pipe->device->bus->use_polling;
773 /* XXXX */
774 if (polling)
775 pipe->running = 0;
776
777 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
778 usbd_xfer_isread(xfer)) {
779#ifdef DIAGNOSTIC
780 if (xfer->actlen > xfer->length) {
781 printf("usb_transfer_complete: actlen > len %d > %d\n",
782 xfer->actlen, xfer->length);
783 xfer->actlen = xfer->length;
784 }
785#endif
786 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
787 }
788
789 /* if we allocated the buffer in usbd_transfer() we free it here. */
790 if (xfer->rqflags & URQ_AUTO_DMABUF) {
791 if (!repeat) {
792 struct usbd_bus *bus = pipe->device->bus;
793 bus->methods->freem(bus, dmap);
794 xfer->rqflags &= ~URQ_AUTO_DMABUF;
795 }
796 }
797
798 if (!repeat) {
799 /* Remove request from queue. */
800#ifdef DIAGNOSTIC
801 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
802 printf("usb_transfer_complete: bad dequeue %p != %p\n",
803 xfer, SIMPLEQ_FIRST(&pipe->queue));
804 xfer->busy_free = XFER_BUSY;
805#endif
806 SIMPLEQ_REMOVE_HEAD(&pipe->queue, 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.uds_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
849usbd_status
850usb_insert_transfer(usbd_xfer_handle xfer)
851{
852 usbd_pipe_handle pipe = xfer->pipe;
853 usbd_status err;
854
855 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
856 pipe, pipe->running, xfer->timeout));
857#ifdef DIAGNOSTIC
858 if (xfer->busy_free != XFER_BUSY) {
859 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
860 xfer, xfer->busy_free);
861 return (USBD_INVAL);
862 }
863 xfer->busy_free = XFER_ONQU;
864#endif
865 crit_enter();
866 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
867 if (pipe->running)
868 err = USBD_IN_PROGRESS;
869 else {
870 pipe->running = 1;
871 err = USBD_NORMAL_COMPLETION;
872 }
873 crit_exit();
874 return (err);
875}
876
877/* Called from a critical section. */
878void
879usbd_start_next(usbd_pipe_handle pipe)
880{
881 usbd_xfer_handle xfer;
882 usbd_status err;
883
884#ifdef DIAGNOSTIC
885 if (pipe == NULL) {
886 printf("usbd_start_next: pipe == NULL\n");
887 return;
888 }
889 if (pipe->methods == NULL || pipe->methods->start == NULL) {
890 printf("usbd_start_next: pipe=%p no start method\n", pipe);
891 return;
892 }
893#endif
894
895 /* Get next request in queue. */
896 xfer = SIMPLEQ_FIRST(&pipe->queue);
897 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
898 if (xfer == NULL) {
899 pipe->running = 0;
900 } else {
901 err = pipe->methods->start(xfer);
902 if (err != USBD_IN_PROGRESS) {
903 printf("usbd_start_next: error=%d\n", err);
904 pipe->running = 0;
905 /* XXX do what? */
906 }
907 }
908}
909
910
911usbd_status
912usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
913{
914 return (usbd_do_request_flags(dev, req, data, 0, 0,
915 USBD_DEFAULT_TIMEOUT));
916}
917
918usbd_status
919usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
920 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
921{
922 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
923 data, flags, actlen, timo));
924}
925
926usbd_status
927usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
928 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
929 u_int32_t timeout)
930{
931 usbd_xfer_handle xfer;
932 usbd_status err;
933
934#ifdef DIAGNOSTIC
935#if defined(__i386__) && (defined(__FreeBSD__) || defined(__DragonFly__))
936 KASSERT(mycpu->gd_intr_nesting_level == 0,
937 ("usbd_do_request: in interrupt context"));
938#endif
939 if (dev->bus->intr_context) {
940 printf("usbd_do_request: not in process context\n");
941 return (USBD_INVAL);
942 }
943#endif
944
945 xfer = usbd_alloc_xfer(dev);
946 if (xfer == NULL)
947 return (USBD_NOMEM);
948 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
949 data, UGETW(req->wLength), flags, 0);
950 xfer->pipe = pipe;
951 err = usbd_sync_transfer(xfer);
952#if defined(USB_DEBUG) || defined(DIAGNOSTIC)
953 if (xfer->actlen > xfer->length)
954 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
955 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
956 dev->address, xfer->request.bmRequestType,
957 xfer->request.bRequest, UGETW(xfer->request.wValue),
958 UGETW(xfer->request.wIndex),
959 UGETW(xfer->request.wLength),
960 xfer->length, xfer->actlen));
961#endif
962 if (actlen != NULL)
963 *actlen = xfer->actlen;
964 if (err == USBD_STALLED) {
965 /*
966 * The control endpoint has stalled. Control endpoints
967 * should not halt, but some may do so anyway so clear
968 * any halt condition.
969 */
970 usb_device_request_t treq;
971 usb_status_t status;
972 u_int16_t s;
973 usbd_status nerr;
974
975 treq.bmRequestType = UT_READ_ENDPOINT;
976 treq.bRequest = UR_GET_STATUS;
977 USETW(treq.wValue, 0);
978 USETW(treq.wIndex, 0);
979 USETW(treq.wLength, sizeof(usb_status_t));
980 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
981 &treq, &status,sizeof(usb_status_t),
982 0, 0);
983 nerr = usbd_sync_transfer(xfer);
984 if (nerr)
985 goto bad;
986 s = UGETW(status.wStatus);
987 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
988 if (!(s & UES_HALT))
989 goto bad;
990 treq.bmRequestType = UT_WRITE_ENDPOINT;
991 treq.bRequest = UR_CLEAR_FEATURE;
992 USETW(treq.wValue, UF_ENDPOINT_HALT);
993 USETW(treq.wIndex, 0);
994 USETW(treq.wLength, 0);
995 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
996 &treq, &status, 0, 0, 0);
997 nerr = usbd_sync_transfer(xfer);
998 if (nerr)
999 goto bad;
1000 }
1001
1002 bad:
1003 usbd_free_xfer(xfer);
1004 return (err);
1005}
1006
1007void
1008usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1009 usbd_status status)
1010{
1011#if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1012 if (xfer->actlen > xfer->length)
1013 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1014 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1015 xfer->pipe->device->address,
1016 xfer->request.bmRequestType,
1017 xfer->request.bRequest, UGETW(xfer->request.wValue),
1018 UGETW(xfer->request.wIndex),
1019 UGETW(xfer->request.wLength),
1020 xfer->length, xfer->actlen));
1021#endif
1022 usbd_free_xfer(xfer);
1023}
1024
1025/*
1026 * Execute a request without waiting for completion.
1027 * Can be used from interrupt context.
1028 */
1029usbd_status
1030usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1031 void *data)
1032{
1033 usbd_xfer_handle xfer;
1034 usbd_status err;
1035
1036 xfer = usbd_alloc_xfer(dev);
1037 if (xfer == NULL)
1038 return (USBD_NOMEM);
1039 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1040 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1041 err = usbd_transfer(xfer);
1042 if (err != USBD_IN_PROGRESS) {
1043 usbd_free_xfer(xfer);
1044 return (err);
1045 }
1046 return (USBD_NORMAL_COMPLETION);
1047}
1048
1049const struct usbd_quirks *
1050usbd_get_quirks(usbd_device_handle dev)
1051{
1052#ifdef DIAGNOSTIC
1053 if (dev == NULL) {
1054 printf("usbd_get_quirks: dev == NULL\n");
1055 return 0;
1056 }
1057#endif
1058 return (dev->quirks);
1059}
1060
1061/* XXX do periodic free() of free list */
1062
1063/*
1064 * Called from keyboard driver when in polling mode.
1065 */
1066void
1067usbd_dopoll(usbd_interface_handle iface)
1068{
1069 iface->device->bus->methods->do_poll(iface->device->bus);
1070}
1071
1072void
1073usbd_set_polling(usbd_device_handle dev, int on)
1074{
1075 if (on)
1076 dev->bus->use_polling++;
1077 else
1078 dev->bus->use_polling--;
1079 /* When polling we need to make sure there is nothing pending to do. */
1080 if (dev->bus->use_polling)
1081 dev->bus->methods->soft_intr(dev->bus);
1082}
1083
1084
1085usb_endpoint_descriptor_t *
1086usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1087{
1088 struct usbd_endpoint *ep;
1089 int i;
1090
1091 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1092 ep = &iface->endpoints[i];
1093 if (ep->edesc->bEndpointAddress == address)
1094 return (iface->endpoints[i].edesc);
1095 }
1096 return (0);
1097}
1098
1099/*
1100 * usbd_ratecheck() can limit the number of error messages that occurs.
1101 * When a device is unplugged it may take up to 0.25s for the hub driver
1102 * to notice it. If the driver continuosly tries to do I/O operations
1103 * this can generate a large number of messages.
1104 */
1105int
1106usbd_ratecheck(struct timeval *last)
1107{
1108#if 0
1109 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1110
1111 return (ratecheck(last, &errinterval));
1112#endif
1113 return (1);
1114}
1115
1116/*
1117 * Search for a vendor/product pair in an array. The item size is
1118 * given as an argument.
1119 */
1120const struct usb_devno *
1121usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1122 u_int16_t vendor, u_int16_t product)
1123{
1124 while (nentries-- > 0) {
1125 u_int16_t tproduct = tbl->ud_product;
1126 if (tbl->ud_vendor == vendor &&
1127 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1128 return (tbl);
1129 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1130 }
1131 return (NULL);
1132}
1133
1134#if defined(__FreeBSD__) || defined(__DragonFly__)
1135int
1136usbd_driver_load(module_t mod, int what, void *arg)
1137{
1138 /* XXX should implement something like a function that removes all generic devices */
1139
1140 return (0);
1141}
1142
1143#endif