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