usbdi.c revision 1.31 1 /* $NetBSD: usbdi.c,v 1.31 1999/08/22 20:12:39 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #if defined(__NetBSD__) || defined(__OpenBSD__)
44 #include <sys/device.h>
45 #else
46 #include <sys/module.h>
47 #include <sys/bus.h>
48 #include <sys/conf.h>
49 #endif
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52
53 #include <dev/usb/usb.h>
54
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdivar.h>
58
59 #if defined(__FreeBSD__)
60 #include "usb_if.h"
61 #endif
62
63 #ifdef USB_DEBUG
64 #define DPRINTF(x) if (usbdebug) logprintf x
65 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
66 extern int usbdebug;
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71
72 static usbd_status usbd_ar_pipe __P((usbd_pipe_handle pipe));
73 void usbd_do_request_async_cb
74 __P((usbd_request_handle, usbd_private_handle, usbd_status));
75 void usbd_start_next __P((usbd_pipe_handle pipe));
76
77 static SIMPLEQ_HEAD(, usbd_request) usbd_free_requests;
78
79 #if defined(__FreeBSD__)
80 #define USB_CDEV_MAJOR 108
81
82 extern struct cdevsw usb_cdevsw;
83 #endif
84
85 #ifdef USB_DEBUG
86 void usbd_dump_queue __P((usbd_pipe_handle));
87
88 void
89 usbd_dump_queue(pipe)
90 usbd_pipe_handle pipe;
91 {
92 usbd_request_handle reqh;
93
94 printf("usbd_dump_queue: pipe=%p\n", pipe);
95 for (reqh = SIMPLEQ_FIRST(&pipe->queue);
96 reqh;
97 reqh = SIMPLEQ_NEXT(reqh, next)) {
98 printf(" reqh=%p\n", reqh);
99 }
100 }
101 #endif
102
103 usbd_status
104 usbd_open_pipe(iface, address, flags, pipe)
105 usbd_interface_handle iface;
106 u_int8_t address;
107 u_int8_t flags;
108 usbd_pipe_handle *pipe;
109 {
110 usbd_pipe_handle p;
111 struct usbd_endpoint *ep;
112 usbd_status r;
113 int i;
114
115 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
116 ep = &iface->endpoints[i];
117 if (ep->edesc->bEndpointAddress == address)
118 goto found;
119 }
120 return (USBD_BAD_ADDRESS);
121 found:
122 if ((flags & USBD_EXCLUSIVE_USE) &&
123 ep->refcnt != 0)
124 return (USBD_IN_USE);
125 r = usbd_setup_pipe(iface->device, iface, ep, &p);
126 if (r != USBD_NORMAL_COMPLETION)
127 return (r);
128 LIST_INSERT_HEAD(&iface->pipes, p, next);
129 *pipe = p;
130 return (USBD_NORMAL_COMPLETION);
131 }
132
133 usbd_status
134 usbd_open_pipe_intr(iface, address, flags, pipe, priv, buffer, length, cb)
135 usbd_interface_handle iface;
136 u_int8_t address;
137 u_int8_t flags;
138 usbd_pipe_handle *pipe;
139 usbd_private_handle priv;
140 void *buffer;
141 u_int32_t length;
142 usbd_callback cb;
143 {
144 usbd_status r;
145 usbd_request_handle reqh;
146 usbd_pipe_handle ipipe;
147
148 reqh = usbd_alloc_request();
149 if (reqh == 0)
150 return (USBD_NOMEM);
151 r = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &ipipe);
152 if (r != USBD_NORMAL_COMPLETION)
153 goto bad1;
154 r = usbd_setup_request(reqh, ipipe, priv, buffer, length,
155 USBD_XFER_IN | flags, USBD_NO_TIMEOUT, cb);
156 if (r != USBD_NORMAL_COMPLETION)
157 goto bad2;
158 ipipe->intrreqh = reqh;
159 ipipe->repeat = 1;
160 r = usbd_transfer(reqh);
161 *pipe = ipipe;
162 if (r != USBD_IN_PROGRESS)
163 goto bad3;
164 return (USBD_NORMAL_COMPLETION);
165
166 bad3:
167 ipipe->intrreqh = 0;
168 ipipe->repeat = 0;
169 bad2:
170 usbd_close_pipe(ipipe);
171 bad1:
172 usbd_free_request(reqh);
173 return r;
174 }
175
176 usbd_status
177 usbd_open_pipe_iso(iface, address, flags, pipe, priv, bufsize, nbuf)
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 u_int32_t bufsize;
184 u_int32_t nbuf;
185 {
186 usbd_status r;
187 usbd_pipe_handle p;
188
189 r = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &p);
190 if (r != USBD_NORMAL_COMPLETION)
191 return (r);
192 if (!p->methods->isobuf) {
193 usbd_close_pipe(p);
194 return (USBD_INVAL);
195 }
196 r = p->methods->isobuf(p, bufsize, nbuf);
197 if (r != USBD_NORMAL_COMPLETION) {
198 usbd_close_pipe(p);
199 return (r);
200 }
201 *pipe = p;
202 return r;
203 }
204
205 usbd_status
206 usbd_close_pipe(pipe)
207 usbd_pipe_handle pipe;
208 {
209 #ifdef DIAGNOSTIC
210 if (pipe == 0) {
211 printf("usbd_close_pipe: pipe==NULL\n");
212 return (USBD_NORMAL_COMPLETION);
213 }
214 #endif
215
216 if (--pipe->refcnt != 0)
217 return (USBD_NORMAL_COMPLETION);
218 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
219 return (USBD_PENDING_REQUESTS);
220 LIST_REMOVE(pipe, next);
221 pipe->endpoint->refcnt--;
222 pipe->methods->close(pipe);
223 if (pipe->intrreqh)
224 usbd_free_request(pipe->intrreqh);
225 free(pipe, M_USB);
226 return (USBD_NORMAL_COMPLETION);
227 }
228
229 usbd_status
230 usbd_transfer(reqh)
231 usbd_request_handle reqh;
232 {
233 usbd_pipe_handle pipe = reqh->pipe;
234 usbd_status r;
235 int s;
236
237 DPRINTFN(5,("usbd_transfer: reqh=%p, flags=%d, pipe=%p, running=%d\n",
238 reqh, reqh->flags, pipe, pipe->running));
239 #ifdef USB_DEBUG
240 if (usbdebug > 5)
241 usbd_dump_queue(pipe);
242 #endif
243 reqh->done = 0;
244
245 r = pipe->methods->transfer(reqh);
246 if (!(reqh->flags & USBD_SYNCHRONOUS))
247 return r;
248
249 /* Sync transfer, wait for completion. */
250 if (r != USBD_IN_PROGRESS)
251 return (r);
252 s = splusb();
253 if (!reqh->done) {
254 if (reqh->pipe->device->bus->use_polling)
255 panic("usbd_transfer: not done\n");
256 tsleep(reqh, PRIBIO, "usbsyn", 0);
257 }
258 splx(s);
259 return (reqh->status);
260 }
261
262 /* Like usbd_transfer(), but waits for completion. */
263 usbd_status
264 usbd_sync_transfer(reqh)
265 usbd_request_handle reqh;
266 {
267 reqh->flags |= USBD_SYNCHRONOUS;
268 return (usbd_transfer(reqh));
269 }
270
271 usbd_request_handle
272 usbd_alloc_request()
273 {
274 usbd_request_handle reqh;
275
276 reqh = SIMPLEQ_FIRST(&usbd_free_requests);
277 if (reqh)
278 SIMPLEQ_REMOVE_HEAD(&usbd_free_requests, reqh, next);
279 else
280 reqh = malloc(sizeof(*reqh), M_USB, M_NOWAIT);
281 if (!reqh)
282 return (0);
283 memset(reqh, 0, sizeof *reqh);
284 DPRINTFN(1,("usbd_alloc_request() = %p\n", reqh));
285 return (reqh);
286 }
287
288 usbd_status
289 usbd_free_request(reqh)
290 usbd_request_handle reqh;
291 {
292 DPRINTFN(1,("usbd_free_request: %p\n", reqh));
293 SIMPLEQ_INSERT_HEAD(&usbd_free_requests, reqh, next);
294 return (USBD_NORMAL_COMPLETION);
295 }
296
297 usbd_status
298 usbd_setup_request(reqh, pipe, priv, buffer, length, flags, timeout, callback)
299 usbd_request_handle reqh;
300 usbd_pipe_handle pipe;
301 usbd_private_handle priv;
302 void *buffer;
303 u_int32_t length;
304 u_int16_t flags;
305 u_int32_t timeout;
306 void (*callback) __P((usbd_request_handle,
307 usbd_private_handle,
308 usbd_status));
309 {
310 reqh->pipe = pipe;
311 reqh->priv = priv;
312 reqh->buffer = buffer;
313 reqh->length = length;
314 reqh->actlen = 0;
315 reqh->flags = flags;
316 reqh->timeout = timeout;
317 reqh->status = USBD_NOT_STARTED;
318 reqh->callback = callback;
319 reqh->isreq = 0;
320 return (USBD_NORMAL_COMPLETION);
321 }
322
323 usbd_status
324 usbd_setup_default_request(reqh, dev, priv, timeout, req, buffer,
325 length, flags, callback)
326 usbd_request_handle reqh;
327 usbd_device_handle dev;
328 usbd_private_handle priv;
329 u_int32_t timeout;
330 usb_device_request_t *req;
331 void *buffer;
332 u_int32_t length;
333 u_int16_t flags;
334 void (*callback) __P((usbd_request_handle,
335 usbd_private_handle,
336 usbd_status));
337 {
338 reqh->pipe = dev->default_pipe;
339 reqh->priv = priv;
340 reqh->buffer = buffer;
341 reqh->length = length;
342 reqh->actlen = 0;
343 reqh->flags = flags;
344 reqh->timeout = timeout;
345 reqh->status = USBD_NOT_STARTED;
346 reqh->callback = callback;
347 reqh->request = *req;
348 reqh->isreq = 1;
349 return (USBD_NORMAL_COMPLETION);
350 }
351
352 void
353 usbd_get_request_status(reqh, priv, buffer, count, status)
354 usbd_request_handle reqh;
355 usbd_private_handle *priv;
356 void **buffer;
357 u_int32_t *count;
358 usbd_status *status;
359 {
360 if (priv)
361 *priv = reqh->priv;
362 if (buffer)
363 *buffer = reqh->buffer;
364 if (count)
365 *count = reqh->actlen;
366 if (status)
367 *status = reqh->status;
368 }
369
370 usb_config_descriptor_t *
371 usbd_get_config_descriptor(dev)
372 usbd_device_handle dev;
373 {
374 return (dev->cdesc);
375 }
376
377 usb_interface_descriptor_t *
378 usbd_get_interface_descriptor(iface)
379 usbd_interface_handle iface;
380 {
381 return (iface->idesc);
382 }
383
384 usb_device_descriptor_t *
385 usbd_get_device_descriptor(dev)
386 usbd_device_handle dev;
387 {
388 return (&dev->ddesc);
389 }
390
391 usb_endpoint_descriptor_t *
392 usbd_interface2endpoint_descriptor(iface, index)
393 usbd_interface_handle iface;
394 u_int8_t index;
395 {
396 if (index >= iface->idesc->bNumEndpoints)
397 return (0);
398 return (iface->endpoints[index].edesc);
399 }
400
401 usbd_status
402 usbd_abort_pipe(pipe)
403 usbd_pipe_handle pipe;
404 {
405 usbd_status r;
406 int s;
407
408 #ifdef DIAGNOSTIC
409 if (pipe == 0) {
410 printf("usbd_close_pipe: pipe==NULL\n");
411 return (USBD_NORMAL_COMPLETION);
412 }
413 #endif
414 s = splusb();
415 r = usbd_ar_pipe(pipe);
416 splx(s);
417 return (r);
418 }
419
420 usbd_status
421 usbd_clear_endpoint_stall(pipe)
422 usbd_pipe_handle pipe;
423 {
424 usbd_device_handle dev = pipe->device;
425 usb_device_request_t req;
426 usbd_status r;
427
428 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
429 pipe->methods->cleartoggle(pipe);
430 req.bmRequestType = UT_WRITE_ENDPOINT;
431 req.bRequest = UR_CLEAR_FEATURE;
432 USETW(req.wValue, UF_ENDPOINT_HALT);
433 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
434 USETW(req.wLength, 0);
435 r = usbd_do_request(dev, &req, 0);
436 #if 0
437 XXX should we do this?
438 if (r == USBD_NORMAL_COMPLETION) {
439 pipe->state = USBD_PIPE_ACTIVE;
440 /* XXX activate pipe */
441 }
442 #endif
443 return (r);
444 }
445
446 usbd_status
447 usbd_clear_endpoint_stall_async(pipe)
448 usbd_pipe_handle pipe;
449 {
450 usbd_device_handle dev = pipe->device;
451 usb_device_request_t req;
452 usbd_status r;
453
454 pipe->methods->cleartoggle(pipe);
455 req.bmRequestType = UT_WRITE_ENDPOINT;
456 req.bRequest = UR_CLEAR_FEATURE;
457 USETW(req.wValue, UF_ENDPOINT_HALT);
458 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
459 USETW(req.wLength, 0);
460 r = usbd_do_request_async(dev, &req, 0);
461 return (r);
462 }
463
464 usbd_status
465 usbd_endpoint_count(iface, count)
466 usbd_interface_handle iface;
467 u_int8_t *count;
468 {
469 *count = iface->idesc->bNumEndpoints;
470 return (USBD_NORMAL_COMPLETION);
471 }
472
473 usbd_status
474 usbd_interface_count(dev, count)
475 usbd_device_handle dev;
476 u_int8_t *count;
477 {
478 if (!dev->cdesc)
479 return (USBD_NOT_CONFIGURED);
480 *count = dev->cdesc->bNumInterface;
481 return (USBD_NORMAL_COMPLETION);
482 }
483
484 usbd_status
485 usbd_interface2device_handle(iface, dev)
486 usbd_interface_handle iface;
487 usbd_device_handle *dev;
488 {
489 *dev = iface->device;
490 return (USBD_NORMAL_COMPLETION);
491 }
492
493 usbd_status
494 usbd_device2interface_handle(dev, ifaceno, iface)
495 usbd_device_handle dev;
496 u_int8_t ifaceno;
497 usbd_interface_handle *iface;
498 {
499 if (!dev->cdesc)
500 return (USBD_NOT_CONFIGURED);
501 if (ifaceno >= dev->cdesc->bNumInterface)
502 return (USBD_INVAL);
503 *iface = &dev->ifaces[ifaceno];
504 return (USBD_NORMAL_COMPLETION);
505 }
506
507 /* XXXX use altno */
508 usbd_status
509 usbd_set_interface(iface, altidx)
510 usbd_interface_handle iface;
511 int altidx;
512 {
513 usb_device_request_t req;
514 usbd_status r;
515
516 if (LIST_FIRST(&iface->pipes) != 0)
517 return (USBD_IN_USE);
518
519 if (iface->endpoints)
520 free(iface->endpoints, M_USB);
521 iface->endpoints = 0;
522 iface->idesc = 0;
523
524 r = usbd_fill_iface_data(iface->device, iface->index, altidx);
525 if (r != USBD_NORMAL_COMPLETION)
526 return (r);
527
528 req.bmRequestType = UT_WRITE_INTERFACE;
529 req.bRequest = UR_SET_INTERFACE;
530 USETW(req.wValue, iface->idesc->bAlternateSetting);
531 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
532 USETW(req.wLength, 0);
533 return usbd_do_request(iface->device, &req, 0);
534 }
535
536 int
537 usbd_get_no_alts(cdesc, ifaceno)
538 usb_config_descriptor_t *cdesc;
539 int ifaceno;
540 {
541 char *p = (char *)cdesc;
542 char *end = p + UGETW(cdesc->wTotalLength);
543 usb_interface_descriptor_t *d;
544 int n;
545
546 for (n = 0; p < end; p += d->bLength) {
547 d = (usb_interface_descriptor_t *)p;
548 if (p + d->bLength <= end &&
549 d->bDescriptorType == UDESC_INTERFACE &&
550 d->bInterfaceNumber == ifaceno)
551 n++;
552 }
553 return (n);
554 }
555
556 int
557 usbd_get_interface_altindex(iface)
558 usbd_interface_handle iface;
559 {
560 return (iface->altindex);
561 }
562
563 usbd_status
564 usbd_get_interface(iface, aiface)
565 usbd_interface_handle iface;
566 u_int8_t *aiface;
567 {
568 usb_device_request_t req;
569
570 req.bmRequestType = UT_READ_INTERFACE;
571 req.bRequest = UR_GET_INTERFACE;
572 USETW(req.wValue, 0);
573 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
574 USETW(req.wLength, 1);
575 return usbd_do_request(iface->device, &req, aiface);
576 }
577
578 /*** Internal routines ***/
579
580 /* Dequeue all pipe operations, called at splusb(). */
581 static usbd_status
582 usbd_ar_pipe(pipe)
583 usbd_pipe_handle pipe;
584 {
585 usbd_request_handle reqh;
586
587 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
588 #ifdef USB_DEBUG
589 if (usbdebug > 5)
590 usbd_dump_queue(pipe);
591 #endif
592 while ((reqh = SIMPLEQ_FIRST(&pipe->queue))) {
593 DPRINTFN(2,("usbd_ar_pipe: pipe=%p reqh=%p (methods=%p)\n",
594 pipe, reqh, pipe->methods));
595 /* Make the HC abort it (and invoke the callback). */
596 pipe->methods->abort(reqh);
597 }
598 return (USBD_NORMAL_COMPLETION);
599 }
600
601 static int usbd_global_init_done = 0;
602
603 void
604 usbd_init()
605 {
606 #if defined(__FreeBSD__)
607 dev_t dev;
608 #endif
609
610 if (!usbd_global_init_done) {
611 usbd_global_init_done = 1;
612 SIMPLEQ_INIT(&usbd_free_requests);
613
614 #if defined(__FreeBSD__)
615 dev = makedev(USB_CDEV_MAJOR, 0);
616 cdevsw_add(&dev, &usb_cdevsw, NULL);
617 #endif
618 }
619 }
620
621 void
622 usb_transfer_complete(reqh)
623 usbd_request_handle reqh;
624 {
625 usbd_pipe_handle pipe = reqh->pipe;
626 int polling;
627
628 DPRINTFN(5, ("usb_transfer_complete: pipe=%p reqh=%p\n", pipe, reqh));
629
630 #ifdef DIAGNOSTIC
631 if (!pipe) {
632 printf("usbd_transfer_cb: pipe==0, reqh=%p\n", reqh);
633 return;
634 }
635 #endif
636 polling = reqh->pipe->device->bus->use_polling;
637 /* XXXX */
638 if (polling)
639 pipe->running = 0;
640
641 if (reqh->pipe->methods->done)
642 reqh->pipe->methods->done(reqh);
643
644 /* Remove request from queue. */
645 SIMPLEQ_REMOVE_HEAD(&pipe->queue, reqh, next);
646
647 /* Count completed transfers. */
648 ++pipe->device->bus->stats.requests
649 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
650
651 reqh->done = 1;
652 if (reqh->status == USBD_NORMAL_COMPLETION &&
653 reqh->actlen < reqh->length &&
654 !(reqh->flags & USBD_SHORT_XFER_OK)) {
655 DPRINTFN(-1, ("usbd_transfer_cb: short xfer %d<%d (bytes)\n",
656 reqh->actlen, reqh->length));
657 reqh->status = USBD_SHORT_XFER;
658 }
659
660 if (reqh->callback)
661 reqh->callback(reqh, reqh->priv, reqh->status);
662
663 if ((reqh->flags & USBD_SYNCHRONOUS) && !polling)
664 wakeup(reqh);
665
666 if (!pipe->repeat &&
667 reqh->status != USBD_CANCELLED && reqh->status != USBD_TIMEOUT)
668 usbd_start_next(pipe);
669 }
670
671 usbd_status
672 usb_insert_transfer(reqh)
673 usbd_request_handle reqh;
674 {
675 usbd_pipe_handle pipe = reqh->pipe;
676
677 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d\n", pipe,
678 pipe->running));
679 SIMPLEQ_INSERT_TAIL(&pipe->queue, reqh, next);
680 if (pipe->running)
681 return (USBD_IN_PROGRESS);
682 pipe->running = 1;
683 return (USBD_NORMAL_COMPLETION);
684 }
685
686 void
687 usbd_start_next(pipe)
688 usbd_pipe_handle pipe;
689 {
690 usbd_request_handle reqh;
691 usbd_status r;
692
693 DPRINTFN(10, ("usbd_start_next: pipe=%p\n", pipe));
694
695 #ifdef DIAGNOSTIC
696 if (!pipe) {
697 printf("usbd_start_next: pipe == 0\n");
698 return;
699 }
700 if (!pipe->methods || !pipe->methods->start) {
701 printf("usbd_start_next: no start method\n");
702 return;
703 }
704 #endif
705
706 /* Get next request in queue. */
707 reqh = SIMPLEQ_FIRST(&pipe->queue);
708 DPRINTFN(5, ("usbd_start_next: pipe=%p start reqh=%p\n", pipe, reqh));
709 if (!reqh)
710 pipe->running = 0;
711 else {
712 r = pipe->methods->start(reqh);
713 if (r != USBD_IN_PROGRESS) {
714 printf("usbd_start_next: error=%d\n", r);
715 pipe->running = 0;
716 /* XXX do what? */
717 }
718 }
719 }
720
721 usbd_status
722 usbd_do_request(dev, req, data)
723 usbd_device_handle dev;
724 usb_device_request_t *req;
725 void *data;
726 {
727 return (usbd_do_request_flags(dev, req, data, 0, 0));
728 }
729
730 usbd_status
731 usbd_do_request_flags(dev, req, data, flags, actlen)
732 usbd_device_handle dev;
733 usb_device_request_t *req;
734 void *data;
735 u_int16_t flags;
736 int *actlen;
737 {
738 usbd_request_handle reqh;
739 usbd_status r;
740
741 #ifdef DIAGNOSTIC
742 if (!curproc) {
743 printf("usbd_do_request: not in process context\n");
744 return (USBD_XXX);
745 }
746 #endif
747
748 reqh = usbd_alloc_request();
749 if (reqh == 0)
750 return (USBD_NOMEM);
751 r = usbd_setup_default_request(
752 reqh, dev, 0, USBD_DEFAULT_TIMEOUT, req, data,
753 UGETW(req->wLength), flags, 0);
754 if (r != USBD_NORMAL_COMPLETION)
755 goto bad;
756 r = usbd_sync_transfer(reqh);
757 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
758 if (reqh->actlen > reqh->length)
759 printf("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
760 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
761 dev->address, reqh->request.bmRequestType,
762 reqh->request.bRequest, UGETW(reqh->request.wValue),
763 UGETW(reqh->request.wIndex),
764 UGETW(reqh->request.wLength),
765 reqh->length, reqh->actlen);
766 #endif
767 if (actlen)
768 *actlen = reqh->actlen;
769 if (r == USBD_STALLED) {
770 /*
771 * The control endpoint has stalled. Control endpoints
772 * should not halt, but some may do so anyway so clear
773 * any halt condition.
774 */
775 usb_device_request_t treq;
776 usb_status_t status;
777 u_int16_t s;
778 usbd_status nr;
779
780 treq.bmRequestType = UT_READ_ENDPOINT;
781 treq.bRequest = UR_GET_STATUS;
782 USETW(treq.wValue, 0);
783 USETW(treq.wIndex, 0);
784 USETW(treq.wLength, sizeof(usb_status_t));
785 nr = usbd_setup_default_request(
786 reqh, dev, 0, USBD_DEFAULT_TIMEOUT, &treq, &status,
787 sizeof(usb_status_t), 0, 0);
788 if (nr != USBD_NORMAL_COMPLETION)
789 goto bad;
790 nr = usbd_sync_transfer(reqh);
791 if (nr != USBD_NORMAL_COMPLETION)
792 goto bad;
793 s = UGETW(status.wStatus);
794 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
795 if (!(s & UES_HALT))
796 goto bad;
797 treq.bmRequestType = UT_WRITE_ENDPOINT;
798 treq.bRequest = UR_CLEAR_FEATURE;
799 USETW(treq.wValue, UF_ENDPOINT_HALT);
800 USETW(treq.wIndex, 0);
801 USETW(treq.wLength, 0);
802 nr = usbd_setup_default_request(
803 reqh, dev, 0, USBD_DEFAULT_TIMEOUT, &treq, &status,
804 0, 0, 0);
805 if (nr != USBD_NORMAL_COMPLETION)
806 goto bad;
807 nr = usbd_sync_transfer(reqh);
808 if (nr != USBD_NORMAL_COMPLETION)
809 goto bad;
810 }
811
812 bad:
813 usbd_free_request(reqh);
814 return (r);
815 }
816
817 void
818 usbd_do_request_async_cb(reqh, priv, status)
819 usbd_request_handle reqh;
820 usbd_private_handle priv;
821 usbd_status status;
822 {
823 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
824 if (reqh->actlen > reqh->length)
825 printf("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
826 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
827 reqh->pipe->device->address,
828 reqh->request.bmRequestType,
829 reqh->request.bRequest, UGETW(reqh->request.wValue),
830 UGETW(reqh->request.wIndex),
831 UGETW(reqh->request.wLength),
832 reqh->length, reqh->actlen);
833 #endif
834 usbd_free_request(reqh);
835 }
836
837 /*
838 * Execute a request without waiting for completion.
839 * Can be used from interrupt context.
840 */
841 usbd_status
842 usbd_do_request_async(dev, req, data)
843 usbd_device_handle dev;
844 usb_device_request_t *req;
845 void *data;
846 {
847 usbd_request_handle reqh;
848 usbd_status r;
849
850 reqh = usbd_alloc_request();
851 if (reqh == 0)
852 return (USBD_NOMEM);
853 r = usbd_setup_default_request(
854 reqh, dev, 0, USBD_DEFAULT_TIMEOUT, req, data,
855 UGETW(req->wLength), 0, usbd_do_request_async_cb);
856 if (r != USBD_NORMAL_COMPLETION) {
857 usbd_free_request(reqh);
858 return (r);
859 }
860 r = usbd_transfer(reqh);
861 if (r != USBD_IN_PROGRESS)
862 return (r);
863 return (USBD_NORMAL_COMPLETION);
864 }
865
866 struct usbd_quirks *
867 usbd_get_quirks(dev)
868 usbd_device_handle dev;
869 {
870 return (dev->quirks);
871 }
872
873 /* XXX do periodic free() of free list */
874
875 /*
876 * Called from keyboard driver when in polling mode.
877 */
878 void
879 usbd_dopoll(iface)
880 usbd_interface_handle iface;
881 {
882 iface->device->bus->do_poll(iface->device->bus);
883 }
884
885 void
886 usbd_set_polling(iface, on)
887 usbd_interface_handle iface;
888 int on;
889 {
890 iface->device->bus->use_polling = on;
891 }
892
893
894 usb_endpoint_descriptor_t *
895 usbd_get_endpoint_descriptor(iface, address)
896 usbd_interface_handle iface;
897 u_int8_t address;
898 {
899 struct usbd_endpoint *ep;
900 int i;
901
902 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
903 ep = &iface->endpoints[i];
904 if (ep->edesc->bEndpointAddress == address)
905 return (iface->endpoints[i].edesc);
906 }
907 return (0);
908 }
909
910 #if defined(__FreeBSD__)
911 void
912 usbd_print_child(device_t parent, device_t child)
913 {
914 /*
915 struct usb_softc *sc = device_get_softc(child);
916 */
917
918 printf(" at %s%d", device_get_name(parent), device_get_unit(parent));
919
920 /* XXX How do we get to the usbd_device_handle???
921 usbd_device_handle dev = invalidadosch;
922
923 printf(" addr %d", dev->addr);
924
925 if (bootverbose) {
926 if (dev->lowspeed)
927 printf(", lowspeed");
928 if (dev->self_powered)
929 printf(", self powered");
930 else
931 printf(", %dmA", dev->power);
932 printf(", config %d", dev->config);
933 }
934 */
935 }
936
937 /* Reconfigure all the USB busses in the system. */
938 int
939 usbd_driver_load(module_t mod, int what, void *arg)
940 {
941 devclass_t usb_devclass = devclass_find("usb");
942 devclass_t ugen_devclass = devclass_find("ugen");
943 device_t *devlist;
944 int devcount;
945 int error;
946
947 switch (what) {
948 case MOD_LOAD:
949 case MOD_UNLOAD:
950 if (!usb_devclass)
951 return 0; /* just ignore call */
952
953 if (ugen_devclass) {
954 /* detach devices from generic driver if possible */
955 error = devclass_get_devices(ugen_devclass, &devlist,
956 &devcount);
957 if (!error)
958 for (devcount--; devcount >= 0; devcount--)
959 (void)DEVICE_DETACH(devlist[devcount]);
960 }
961
962 error = devclass_get_devices(usb_devclass, &devlist, &devcount);
963 if (error)
964 return 0; /* XXX maybe transient, or error? */
965
966 for (devcount--; devcount >= 0; devcount--)
967 USB_RECONFIGURE(devlist[devcount]);
968
969 free(devlist, M_TEMP);
970 return 0;
971 }
972
973 return 0; /* nothing to do by us */
974 }
975
976 /* Set the description of the device including a malloc and copy. */
977 void
978 usbd_device_set_desc(device_t device, char *devinfo)
979 {
980 size_t l;
981 char *desc;
982
983 if ( devinfo ) {
984 l = strlen(devinfo);
985 desc = malloc(l+1, M_USB, M_NOWAIT);
986 if (desc)
987 memcpy(desc, devinfo, l+1);
988 } else
989 desc = NULL;
990
991 device_set_desc(device, desc);
992 }
993
994 char *
995 usbd_devname(bdevice *bdev)
996 {
997 static char buf[20];
998 /*
999 * A static buffer is a loss if this routine is used from an interrupt,
1000 * but it's not fatal.
1001 */
1002
1003 sprintf(buf, "%s%d", device_get_name(*bdev), device_get_unit(*bdev));
1004 return (buf);
1005 }
1006
1007 #endif
1008