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