usbdi.c revision 1.203 1 1.203 mrg /* $NetBSD: usbdi.c,v 1.203 2020/07/13 05:43:38 mrg Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.169 skrll * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.11 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.72 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.169 skrll * Carlstedt Research & Technology, Matthew R. Green (mrg (at) eterna.com.au),
10 1.169 skrll * and Nick Hudson.
11 1.1 augustss *
12 1.1 augustss * Redistribution and use in source and binary forms, with or without
13 1.1 augustss * modification, are permitted provided that the following conditions
14 1.1 augustss * are met:
15 1.1 augustss * 1. Redistributions of source code must retain the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer.
17 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 augustss * notice, this list of conditions and the following disclaimer in the
19 1.1 augustss * documentation and/or other materials provided with the distribution.
20 1.1 augustss *
21 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
32 1.1 augustss */
33 1.84 lukem
34 1.84 lukem #include <sys/cdefs.h>
35 1.203 mrg __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.203 2020/07/13 05:43:38 mrg Exp $");
36 1.115 pavel
37 1.141 christos #ifdef _KERNEL_OPT
38 1.162 skrll #include "opt_usb.h"
39 1.115 pavel #include "opt_compat_netbsd.h"
40 1.169 skrll #include "usb_dma.h"
41 1.141 christos #endif
42 1.1 augustss
43 1.1 augustss #include <sys/param.h>
44 1.1 augustss #include <sys/systm.h>
45 1.1 augustss #include <sys/kernel.h>
46 1.15 augustss #include <sys/device.h>
47 1.169 skrll #include <sys/kmem.h>
48 1.1 augustss #include <sys/proc.h>
49 1.121 ad #include <sys/bus.h>
50 1.136 mrg #include <sys/cpu.h>
51 1.36 augustss
52 1.1 augustss #include <dev/usb/usb.h>
53 1.1 augustss #include <dev/usb/usbdi.h>
54 1.1 augustss #include <dev/usb/usbdi_util.h>
55 1.1 augustss #include <dev/usb/usbdivar.h>
56 1.36 augustss #include <dev/usb/usb_mem.h>
57 1.105 augustss #include <dev/usb/usb_quirks.h>
58 1.195 riastrad #include <dev/usb/usb_sdt.h>
59 1.162 skrll #include <dev/usb/usbhist.h>
60 1.1 augustss
61 1.119 drochner /* UTF-8 encoding stuff */
62 1.119 drochner #include <fs/unicode.h>
63 1.119 drochner
64 1.1 augustss extern int usbdebug;
65 1.1 augustss
66 1.195 riastrad SDT_PROBE_DEFINE5(usb, device, pipe, open,
67 1.195 riastrad "struct usbd_interface *"/*iface*/,
68 1.195 riastrad "uint8_t"/*address*/,
69 1.195 riastrad "uint8_t"/*flags*/,
70 1.195 riastrad "int"/*ival*/,
71 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
72 1.195 riastrad
73 1.195 riastrad SDT_PROBE_DEFINE7(usb, device, pipe, open__intr,
74 1.195 riastrad "struct usbd_interface *"/*iface*/,
75 1.195 riastrad "uint8_t"/*address*/,
76 1.195 riastrad "uint8_t"/*flags*/,
77 1.195 riastrad "int"/*ival*/,
78 1.195 riastrad "usbd_callback"/*cb*/,
79 1.195 riastrad "void *"/*cookie*/,
80 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
81 1.195 riastrad
82 1.195 riastrad SDT_PROBE_DEFINE2(usb, device, pipe, transfer__start,
83 1.195 riastrad "struct usbd_pipe *"/*pipe*/,
84 1.195 riastrad "struct usbd_xfer *"/*xfer*/);
85 1.195 riastrad SDT_PROBE_DEFINE3(usb, device, pipe, transfer__done,
86 1.195 riastrad "struct usbd_pipe *"/*pipe*/,
87 1.195 riastrad "struct usbd_xfer *"/*xfer*/,
88 1.195 riastrad "usbd_status"/*err*/);
89 1.195 riastrad SDT_PROBE_DEFINE2(usb, device, pipe, start,
90 1.195 riastrad "struct usbd_pipe *"/*pipe*/,
91 1.195 riastrad "struct usbd_xfer *"/*xfer*/);
92 1.195 riastrad
93 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, pipe, close, "struct usbd_pipe *"/*pipe*/);
94 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, pipe, abort__start,
95 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
96 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, pipe, abort__done,
97 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
98 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, pipe, clear__endpoint__stall,
99 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
100 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, pipe, clear__endpoint__toggle,
101 1.195 riastrad "struct usbd_pipe *"/*pipe*/);
102 1.195 riastrad
103 1.195 riastrad SDT_PROBE_DEFINE5(usb, device, xfer, create,
104 1.195 riastrad "struct usbd_xfer *"/*xfer*/,
105 1.195 riastrad "struct usbd_pipe *"/*pipe*/,
106 1.195 riastrad "size_t"/*len*/,
107 1.195 riastrad "unsigned int"/*flags*/,
108 1.195 riastrad "unsigned int"/*nframes*/);
109 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, xfer, start, "struct usbd_xfer *"/*xfer*/);
110 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, xfer, preabort, "struct usbd_xfer *"/*xfer*/);
111 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, xfer, abort, "struct usbd_xfer *"/*xfer*/);
112 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, xfer, timeout, "struct usbd_xfer *"/*xfer*/);
113 1.195 riastrad SDT_PROBE_DEFINE2(usb, device, xfer, done,
114 1.195 riastrad "struct usbd_xfer *"/*xfer*/,
115 1.195 riastrad "usbd_status"/*status*/);
116 1.195 riastrad SDT_PROBE_DEFINE1(usb, device, xfer, destroy, "struct usbd_xfer *"/*xfer*/);
117 1.195 riastrad
118 1.169 skrll Static usbd_status usbd_ar_pipe(struct usbd_pipe *);
119 1.169 skrll Static void usbd_start_next(struct usbd_pipe *);
120 1.69 augustss Static usbd_status usbd_open_pipe_ival
121 1.169 skrll (struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
122 1.169 skrll static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
123 1.169 skrll static void usbd_free_buffer(struct usbd_xfer *);
124 1.169 skrll static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
125 1.169 skrll static usbd_status usbd_free_xfer(struct usbd_xfer *);
126 1.176 khorben static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
127 1.192 riastrad static void usbd_xfer_timeout(void *);
128 1.192 riastrad static void usbd_xfer_timeout_task(void *);
129 1.192 riastrad static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
130 1.192 riastrad static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
131 1.1 augustss
132 1.162 skrll #if defined(USB_DEBUG)
133 1.82 augustss void
134 1.82 augustss usbd_dump_iface(struct usbd_interface *iface)
135 1.82 augustss {
136 1.183 mrg USBHIST_FUNC();
137 1.183 mrg USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
138 1.162 skrll
139 1.82 augustss if (iface == NULL)
140 1.82 augustss return;
141 1.196 christos USBHIST_LOG(usbdebug, " device = %#jx idesc = %#jx index = %jd",
142 1.175 pgoyette (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
143 1.175 pgoyette iface->ui_index, 0);
144 1.196 christos USBHIST_LOG(usbdebug, " altindex=%jd priv=%#jx",
145 1.175 pgoyette iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
146 1.82 augustss }
147 1.82 augustss
148 1.82 augustss void
149 1.82 augustss usbd_dump_device(struct usbd_device *dev)
150 1.82 augustss {
151 1.183 mrg USBHIST_FUNC();
152 1.183 mrg USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
153 1.162 skrll
154 1.82 augustss if (dev == NULL)
155 1.82 augustss return;
156 1.175 pgoyette USBHIST_LOG(usbdebug, " bus = %#jx default_pipe = %#jx",
157 1.175 pgoyette (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
158 1.175 pgoyette USBHIST_LOG(usbdebug, " address = %jd config = %jd depth = %jd ",
159 1.169 skrll dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
160 1.175 pgoyette USBHIST_LOG(usbdebug, " speed = %jd self_powered = %jd "
161 1.175 pgoyette "power = %jd langid = %jd",
162 1.169 skrll dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
163 1.82 augustss }
164 1.82 augustss
165 1.82 augustss void
166 1.82 augustss usbd_dump_endpoint(struct usbd_endpoint *endp)
167 1.82 augustss {
168 1.183 mrg USBHIST_FUNC();
169 1.183 mrg USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
170 1.162 skrll
171 1.82 augustss if (endp == NULL)
172 1.82 augustss return;
173 1.175 pgoyette USBHIST_LOG(usbdebug, " edesc = %#jx refcnt = %jd",
174 1.175 pgoyette (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
175 1.169 skrll if (endp->ue_edesc)
176 1.198 christos USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02jx",
177 1.169 skrll endp->ue_edesc->bEndpointAddress, 0, 0, 0);
178 1.82 augustss }
179 1.82 augustss
180 1.28 augustss void
181 1.169 skrll usbd_dump_queue(struct usbd_pipe *pipe)
182 1.28 augustss {
183 1.169 skrll struct usbd_xfer *xfer;
184 1.28 augustss
185 1.183 mrg USBHIST_FUNC();
186 1.183 mrg USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
187 1.162 skrll
188 1.169 skrll SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
189 1.175 pgoyette USBHIST_LOG(usbdebug, " xfer = %#jx", (uintptr_t)xfer,
190 1.175 pgoyette 0, 0, 0);
191 1.28 augustss }
192 1.82 augustss }
193 1.82 augustss
194 1.82 augustss void
195 1.169 skrll usbd_dump_pipe(struct usbd_pipe *pipe)
196 1.82 augustss {
197 1.183 mrg USBHIST_FUNC();
198 1.183 mrg USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
199 1.162 skrll
200 1.82 augustss if (pipe == NULL)
201 1.82 augustss return;
202 1.169 skrll usbd_dump_iface(pipe->up_iface);
203 1.169 skrll usbd_dump_device(pipe->up_dev);
204 1.169 skrll usbd_dump_endpoint(pipe->up_endpoint);
205 1.162 skrll USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
206 1.175 pgoyette USBHIST_LOG(usbdebug, " running = %jd aborting = %jd",
207 1.169 skrll pipe->up_running, pipe->up_aborting, 0, 0);
208 1.175 pgoyette USBHIST_LOG(usbdebug, " intrxfer = %#jx, repeat = %jd, "
209 1.175 pgoyette "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
210 1.175 pgoyette pipe->up_interval, 0);
211 1.28 augustss }
212 1.28 augustss #endif
213 1.28 augustss
214 1.102 augustss usbd_status
215 1.169 skrll usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
216 1.169 skrll uint8_t flags, struct usbd_pipe **pipe)
217 1.102 augustss {
218 1.102 augustss return (usbd_open_pipe_ival(iface, address, flags, pipe,
219 1.60 augustss USBD_DEFAULT_INTERVAL));
220 1.60 augustss }
221 1.60 augustss
222 1.102 augustss usbd_status
223 1.169 skrll usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
224 1.169 skrll uint8_t flags, struct usbd_pipe **pipe, int ival)
225 1.102 augustss {
226 1.169 skrll struct usbd_pipe *p;
227 1.1 augustss struct usbd_endpoint *ep;
228 1.48 augustss usbd_status err;
229 1.12 augustss int i;
230 1.1 augustss
231 1.183 mrg USBHIST_FUNC();
232 1.196 christos USBHIST_CALLARGS(usbdebug, "iface = %#jx address = %#jx flags = %#jx",
233 1.175 pgoyette (uintptr_t)iface, address, flags, 0);
234 1.53 augustss
235 1.169 skrll for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
236 1.169 skrll ep = &iface->ui_endpoints[i];
237 1.169 skrll if (ep->ue_edesc == NULL)
238 1.169 skrll return USBD_IOERROR;
239 1.169 skrll if (ep->ue_edesc->bEndpointAddress == address)
240 1.1 augustss goto found;
241 1.1 augustss }
242 1.169 skrll return USBD_BAD_ADDRESS;
243 1.1 augustss found:
244 1.169 skrll if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
245 1.169 skrll return USBD_IN_USE;
246 1.169 skrll err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
247 1.48 augustss if (err)
248 1.169 skrll return err;
249 1.169 skrll LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
250 1.1 augustss *pipe = p;
251 1.195 riastrad SDT_PROBE5(usb, device, pipe, open,
252 1.195 riastrad iface, address, flags, ival, p);
253 1.169 skrll return USBD_NORMAL_COMPLETION;
254 1.1 augustss }
255 1.1 augustss
256 1.102 augustss usbd_status
257 1.169 skrll usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
258 1.169 skrll uint8_t flags, struct usbd_pipe **pipe,
259 1.169 skrll void *priv, void *buffer, uint32_t len,
260 1.74 augustss usbd_callback cb, int ival)
261 1.1 augustss {
262 1.48 augustss usbd_status err;
263 1.169 skrll struct usbd_xfer *xfer;
264 1.169 skrll struct usbd_pipe *ipipe;
265 1.1 augustss
266 1.183 mrg USBHIST_FUNC();
267 1.196 christos USBHIST_CALLARGS(usbdebug, "address = %#jx flags = %#jx len = %jd",
268 1.162 skrll address, flags, len, 0);
269 1.53 augustss
270 1.150 jmcneill err = usbd_open_pipe_ival(iface, address,
271 1.150 jmcneill USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
272 1.60 augustss &ipipe, ival);
273 1.48 augustss if (err)
274 1.169 skrll return err;
275 1.169 skrll err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
276 1.169 skrll if (err)
277 1.1 augustss goto bad1;
278 1.169 skrll
279 1.169 skrll usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
280 1.169 skrll ipipe->up_intrxfer = xfer;
281 1.169 skrll ipipe->up_repeat = 1;
282 1.48 augustss err = usbd_transfer(xfer);
283 1.1 augustss *pipe = ipipe;
284 1.48 augustss if (err != USBD_IN_PROGRESS)
285 1.169 skrll goto bad3;
286 1.195 riastrad SDT_PROBE7(usb, device, pipe, open__intr,
287 1.195 riastrad iface, address, flags, ival, cb, priv, ipipe);
288 1.169 skrll return USBD_NORMAL_COMPLETION;
289 1.169 skrll
290 1.169 skrll bad3:
291 1.169 skrll ipipe->up_intrxfer = NULL;
292 1.169 skrll ipipe->up_repeat = 0;
293 1.1 augustss
294 1.169 skrll usbd_destroy_xfer(xfer);
295 1.36 augustss bad1:
296 1.1 augustss usbd_close_pipe(ipipe);
297 1.169 skrll return err;
298 1.9 augustss }
299 1.9 augustss
300 1.1 augustss usbd_status
301 1.169 skrll usbd_close_pipe(struct usbd_pipe *pipe)
302 1.1 augustss {
303 1.162 skrll USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
304 1.162 skrll
305 1.169 skrll KASSERT(pipe != NULL);
306 1.26 augustss
307 1.138 mrg usbd_lock_pipe(pipe);
308 1.195 riastrad SDT_PROBE1(usb, device, pipe, close, pipe);
309 1.169 skrll
310 1.169 skrll if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
311 1.169 skrll printf("WARNING: pipe closed with active xfers on addr %d\n",
312 1.169 skrll pipe->up_dev->ud_addr);
313 1.169 skrll usbd_ar_pipe(pipe);
314 1.138 mrg }
315 1.169 skrll
316 1.169 skrll KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
317 1.169 skrll
318 1.169 skrll LIST_REMOVE(pipe, up_next);
319 1.169 skrll pipe->up_endpoint->ue_refcnt--;
320 1.169 skrll
321 1.181 skrll pipe->up_methods->upm_close(pipe);
322 1.181 skrll
323 1.169 skrll if (pipe->up_intrxfer != NULL) {
324 1.169 skrll usbd_unlock_pipe(pipe);
325 1.169 skrll usbd_destroy_xfer(pipe->up_intrxfer);
326 1.169 skrll usbd_lock_pipe(pipe);
327 1.138 mrg }
328 1.169 skrll
329 1.138 mrg usbd_unlock_pipe(pipe);
330 1.169 skrll kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
331 1.169 skrll
332 1.169 skrll return USBD_NORMAL_COMPLETION;
333 1.1 augustss }
334 1.1 augustss
335 1.31 augustss usbd_status
336 1.169 skrll usbd_transfer(struct usbd_xfer *xfer)
337 1.1 augustss {
338 1.169 skrll struct usbd_pipe *pipe = xfer->ux_pipe;
339 1.48 augustss usbd_status err;
340 1.120 kiyohara unsigned int size, flags;
341 1.16 augustss
342 1.183 mrg USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
343 1.175 pgoyette "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
344 1.175 pgoyette (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
345 1.177 mrg KASSERT(xfer->ux_status == USBD_NOT_STARTED);
346 1.195 riastrad SDT_PROBE1(usb, device, xfer, start, xfer);
347 1.134 jmcneill
348 1.28 augustss #ifdef USB_DEBUG
349 1.28 augustss if (usbdebug > 5)
350 1.28 augustss usbd_dump_queue(pipe);
351 1.28 augustss #endif
352 1.169 skrll xfer->ux_done = 0;
353 1.31 augustss
354 1.169 skrll if (pipe->up_aborting) {
355 1.175 pgoyette USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
356 1.175 pgoyette (uintptr_t)xfer, 0, 0, 0);
357 1.195 riastrad SDT_PROBE2(usb, device, xfer, done, xfer, USBD_CANCELLED);
358 1.169 skrll return USBD_CANCELLED;
359 1.162 skrll }
360 1.71 augustss
361 1.169 skrll KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
362 1.169 skrll
363 1.169 skrll size = xfer->ux_length;
364 1.169 skrll flags = xfer->ux_flags;
365 1.37 augustss
366 1.169 skrll if (size != 0) {
367 1.169 skrll /*
368 1.169 skrll * Use the xfer buffer if none specified in transfer setup.
369 1.169 skrll * isoc transfers always use the xfer buffer, i.e.
370 1.169 skrll * ux_buffer is always NULL for isoc.
371 1.169 skrll */
372 1.169 skrll if (xfer->ux_buffer == NULL) {
373 1.169 skrll xfer->ux_buffer = xfer->ux_buf;
374 1.169 skrll }
375 1.169 skrll
376 1.169 skrll /*
377 1.169 skrll * If not using the xfer buffer copy data to the
378 1.169 skrll * xfer buffer for OUT transfers of >0 length
379 1.169 skrll */
380 1.169 skrll if (xfer->ux_buffer != xfer->ux_buf) {
381 1.169 skrll KASSERT(xfer->ux_buf);
382 1.169 skrll if (!usbd_xfer_isread(xfer)) {
383 1.169 skrll memcpy(xfer->ux_buf, xfer->ux_buffer, size);
384 1.169 skrll }
385 1.162 skrll }
386 1.38 augustss }
387 1.37 augustss
388 1.120 kiyohara /* xfer is not valid after the transfer method unless synchronous */
389 1.195 riastrad SDT_PROBE2(usb, device, pipe, transfer__start, pipe, xfer);
390 1.169 skrll err = pipe->up_methods->upm_transfer(xfer);
391 1.195 riastrad SDT_PROBE3(usb, device, pipe, transfer__done, pipe, xfer, err);
392 1.37 augustss
393 1.51 augustss if (err != USBD_IN_PROGRESS && err) {
394 1.168 skrll /*
395 1.168 skrll * The transfer made it onto the pipe queue, but didn't get
396 1.168 skrll * accepted by the HCD for some reason. It needs removing
397 1.168 skrll * from the pipe queue.
398 1.168 skrll */
399 1.196 christos USBHIST_LOG(usbdebug, "xfer failed: %jd, reinserting",
400 1.186 mrg err, 0, 0, 0);
401 1.168 skrll usbd_lock_pipe(pipe);
402 1.195 riastrad SDT_PROBE1(usb, device, xfer, preabort, xfer);
403 1.203 mrg #ifdef DIAGNOSTIC
404 1.203 mrg xfer->ux_state = XFER_BUSY;
405 1.203 mrg #endif
406 1.169 skrll SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
407 1.169 skrll if (pipe->up_serialise)
408 1.169 skrll usbd_start_next(pipe);
409 1.168 skrll usbd_unlock_pipe(pipe);
410 1.37 augustss }
411 1.37 augustss
412 1.162 skrll if (!(flags & USBD_SYNCHRONOUS)) {
413 1.175 pgoyette USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
414 1.175 pgoyette (uintptr_t)xfer, err, 0, 0);
415 1.195 riastrad if (err != USBD_IN_PROGRESS) /* XXX Possible? */
416 1.195 riastrad SDT_PROBE2(usb, device, xfer, done, xfer, err);
417 1.169 skrll return err;
418 1.162 skrll }
419 1.31 augustss
420 1.162 skrll if (err != USBD_IN_PROGRESS) {
421 1.186 mrg USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
422 1.186 mrg (uintptr_t)xfer, err, 0, 0);
423 1.195 riastrad SDT_PROBE2(usb, device, xfer, done, xfer, err);
424 1.169 skrll return err;
425 1.162 skrll }
426 1.167 skrll
427 1.167 skrll /* Sync transfer, wait for completion. */
428 1.138 mrg usbd_lock_pipe(pipe);
429 1.169 skrll while (!xfer->ux_done) {
430 1.169 skrll if (pipe->up_dev->ud_bus->ub_usepolling)
431 1.103 provos panic("usbd_transfer: not done");
432 1.175 pgoyette USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
433 1.175 pgoyette (uintptr_t)xfer, 0, 0, 0);
434 1.138 mrg
435 1.144 christos err = 0;
436 1.138 mrg if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
437 1.169 skrll err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
438 1.138 mrg } else {
439 1.169 skrll cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
440 1.138 mrg }
441 1.154 aymeric if (err) {
442 1.195 riastrad if (!xfer->ux_done) {
443 1.195 riastrad SDT_PROBE1(usb, device, xfer, abort, xfer);
444 1.169 skrll pipe->up_methods->upm_abort(xfer);
445 1.195 riastrad }
446 1.144 christos break;
447 1.154 aymeric }
448 1.31 augustss }
449 1.195 riastrad SDT_PROBE2(usb, device, xfer, done, xfer, xfer->ux_status);
450 1.195 riastrad /* XXX Race to read xfer->ux_status? */
451 1.138 mrg usbd_unlock_pipe(pipe);
452 1.169 skrll return xfer->ux_status;
453 1.31 augustss }
454 1.31 augustss
455 1.31 augustss /* Like usbd_transfer(), but waits for completion. */
456 1.31 augustss usbd_status
457 1.169 skrll usbd_sync_transfer(struct usbd_xfer *xfer)
458 1.31 augustss {
459 1.169 skrll xfer->ux_flags |= USBD_SYNCHRONOUS;
460 1.169 skrll return usbd_transfer(xfer);
461 1.16 augustss }
462 1.16 augustss
463 1.138 mrg /* Like usbd_transfer(), but waits for completion and listens for signals. */
464 1.138 mrg usbd_status
465 1.169 skrll usbd_sync_transfer_sig(struct usbd_xfer *xfer)
466 1.138 mrg {
467 1.169 skrll xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
468 1.169 skrll return usbd_transfer(xfer);
469 1.138 mrg }
470 1.138 mrg
471 1.169 skrll static void *
472 1.169 skrll usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
473 1.36 augustss {
474 1.169 skrll KASSERT(xfer->ux_buf == NULL);
475 1.169 skrll KASSERT(size != 0);
476 1.169 skrll
477 1.169 skrll xfer->ux_bufsize = 0;
478 1.169 skrll #if NUSB_DMA > 0
479 1.169 skrll struct usbd_bus *bus = xfer->ux_bus;
480 1.169 skrll
481 1.169 skrll if (bus->ub_usedma) {
482 1.169 skrll usb_dma_t *dmap = &xfer->ux_dmabuf;
483 1.169 skrll
484 1.200 skrll KASSERT((bus->ub_dmaflags & USBMALLOC_COHERENT) == 0);
485 1.200 skrll int err = usb_allocmem(bus, size, 0, bus->ub_dmaflags, dmap);
486 1.169 skrll if (err) {
487 1.169 skrll return NULL;
488 1.169 skrll }
489 1.169 skrll xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
490 1.169 skrll xfer->ux_bufsize = size;
491 1.37 augustss
492 1.169 skrll return xfer->ux_buf;
493 1.169 skrll }
494 1.92 augustss #endif
495 1.169 skrll KASSERT(xfer->ux_bus->ub_usedma == false);
496 1.169 skrll xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
497 1.173 chs xfer->ux_bufsize = size;
498 1.169 skrll return xfer->ux_buf;
499 1.36 augustss }
500 1.36 augustss
501 1.169 skrll static void
502 1.169 skrll usbd_free_buffer(struct usbd_xfer *xfer)
503 1.36 augustss {
504 1.169 skrll KASSERT(xfer->ux_buf != NULL);
505 1.169 skrll KASSERT(xfer->ux_bufsize != 0);
506 1.169 skrll
507 1.169 skrll void *buf = xfer->ux_buf;
508 1.169 skrll uint32_t size = xfer->ux_bufsize;
509 1.169 skrll
510 1.169 skrll xfer->ux_buf = NULL;
511 1.169 skrll xfer->ux_bufsize = 0;
512 1.169 skrll
513 1.169 skrll #if NUSB_DMA > 0
514 1.169 skrll struct usbd_bus *bus = xfer->ux_bus;
515 1.169 skrll
516 1.169 skrll if (bus->ub_usedma) {
517 1.169 skrll usb_dma_t *dmap = &xfer->ux_dmabuf;
518 1.169 skrll
519 1.169 skrll usb_freemem(bus, dmap);
520 1.37 augustss return;
521 1.37 augustss }
522 1.37 augustss #endif
523 1.169 skrll KASSERT(xfer->ux_bus->ub_usedma == false);
524 1.169 skrll
525 1.169 skrll kmem_free(buf, size);
526 1.36 augustss }
527 1.36 augustss
528 1.38 augustss void *
529 1.169 skrll usbd_get_buffer(struct usbd_xfer *xfer)
530 1.38 augustss {
531 1.169 skrll return xfer->ux_buf;
532 1.38 augustss }
533 1.38 augustss
534 1.169 skrll struct usbd_pipe *
535 1.169 skrll usbd_get_pipe0(struct usbd_device *dev)
536 1.1 augustss {
537 1.169 skrll
538 1.169 skrll return dev->ud_pipe0;
539 1.169 skrll }
540 1.169 skrll
541 1.169 skrll static struct usbd_xfer *
542 1.169 skrll usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
543 1.169 skrll {
544 1.169 skrll struct usbd_xfer *xfer;
545 1.1 augustss
546 1.183 mrg USBHIST_FUNC();
547 1.162 skrll
548 1.169 skrll ASSERT_SLEEPABLE();
549 1.169 skrll
550 1.169 skrll xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
551 1.48 augustss if (xfer == NULL)
552 1.183 mrg goto out;
553 1.169 skrll xfer->ux_bus = dev->ud_bus;
554 1.169 skrll callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
555 1.192 riastrad callout_setfunc(&xfer->ux_callout, usbd_xfer_timeout, xfer);
556 1.169 skrll cv_init(&xfer->ux_cv, "usbxfer");
557 1.192 riastrad usb_init_task(&xfer->ux_aborttask, usbd_xfer_timeout_task, xfer,
558 1.192 riastrad USB_TASKQ_MPSAFE);
559 1.162 skrll
560 1.183 mrg out:
561 1.183 mrg USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
562 1.162 skrll
563 1.169 skrll return xfer;
564 1.1 augustss }
565 1.1 augustss
566 1.169 skrll static usbd_status
567 1.169 skrll usbd_free_xfer(struct usbd_xfer *xfer)
568 1.1 augustss {
569 1.183 mrg USBHIST_FUNC();
570 1.183 mrg USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
571 1.162 skrll
572 1.169 skrll if (xfer->ux_buf) {
573 1.48 augustss usbd_free_buffer(xfer);
574 1.169 skrll }
575 1.190 riastrad
576 1.190 riastrad /* Wait for any straggling timeout to complete. */
577 1.190 riastrad mutex_enter(xfer->ux_bus->ub_lock);
578 1.191 riastrad xfer->ux_timeout_reset = false; /* do not resuscitate */
579 1.190 riastrad callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
580 1.190 riastrad usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
581 1.190 riastrad USB_TASKQ_HC, xfer->ux_bus->ub_lock);
582 1.190 riastrad mutex_exit(xfer->ux_bus->ub_lock);
583 1.190 riastrad
584 1.169 skrll cv_destroy(&xfer->ux_cv);
585 1.169 skrll xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
586 1.169 skrll return USBD_NORMAL_COMPLETION;
587 1.169 skrll }
588 1.169 skrll
589 1.169 skrll int
590 1.169 skrll usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
591 1.169 skrll unsigned int nframes, struct usbd_xfer **xp)
592 1.169 skrll {
593 1.169 skrll KASSERT(xp != NULL);
594 1.171 pooka void *buf = NULL;
595 1.169 skrll
596 1.169 skrll struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
597 1.169 skrll if (xfer == NULL)
598 1.169 skrll return ENOMEM;
599 1.169 skrll
600 1.199 skrll xfer->ux_pipe = pipe;
601 1.199 skrll xfer->ux_flags = flags;
602 1.199 skrll xfer->ux_nframes = nframes;
603 1.199 skrll xfer->ux_methods = pipe->up_methods;
604 1.199 skrll
605 1.169 skrll if (len) {
606 1.169 skrll buf = usbd_alloc_buffer(xfer, len);
607 1.169 skrll if (!buf) {
608 1.169 skrll usbd_free_xfer(xfer);
609 1.169 skrll return ENOMEM;
610 1.169 skrll }
611 1.169 skrll }
612 1.169 skrll
613 1.169 skrll if (xfer->ux_methods->upm_init) {
614 1.169 skrll int err = xfer->ux_methods->upm_init(xfer);
615 1.169 skrll if (err) {
616 1.169 skrll usbd_free_xfer(xfer);
617 1.169 skrll return err;
618 1.169 skrll }
619 1.169 skrll }
620 1.169 skrll
621 1.169 skrll *xp = xfer;
622 1.195 riastrad SDT_PROBE5(usb, device, xfer, create,
623 1.195 riastrad xfer, pipe, len, flags, nframes);
624 1.169 skrll return 0;
625 1.1 augustss }
626 1.1 augustss
627 1.36 augustss void
628 1.169 skrll usbd_destroy_xfer(struct usbd_xfer *xfer)
629 1.169 skrll {
630 1.169 skrll
631 1.195 riastrad SDT_PROBE1(usb, device, xfer, destroy, xfer);
632 1.195 riastrad if (xfer->ux_methods->upm_fini)
633 1.169 skrll xfer->ux_methods->upm_fini(xfer);
634 1.169 skrll
635 1.169 skrll usbd_free_xfer(xfer);
636 1.1 augustss }
637 1.1 augustss
638 1.36 augustss void
639 1.169 skrll usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
640 1.169 skrll uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
641 1.169 skrll {
642 1.169 skrll KASSERT(xfer->ux_pipe);
643 1.169 skrll
644 1.169 skrll xfer->ux_priv = priv;
645 1.169 skrll xfer->ux_buffer = buffer;
646 1.169 skrll xfer->ux_length = length;
647 1.169 skrll xfer->ux_actlen = 0;
648 1.169 skrll xfer->ux_flags = flags;
649 1.169 skrll xfer->ux_timeout = timeout;
650 1.169 skrll xfer->ux_status = USBD_NOT_STARTED;
651 1.169 skrll xfer->ux_callback = callback;
652 1.169 skrll xfer->ux_rqflags &= ~URQ_REQUEST;
653 1.169 skrll xfer->ux_nframes = 0;
654 1.36 augustss }
655 1.36 augustss
656 1.36 augustss void
657 1.169 skrll usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
658 1.169 skrll void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
659 1.169 skrll uint32_t length, uint16_t flags, usbd_callback callback)
660 1.169 skrll {
661 1.169 skrll KASSERT(xfer->ux_pipe == dev->ud_pipe0);
662 1.169 skrll
663 1.169 skrll xfer->ux_priv = priv;
664 1.169 skrll xfer->ux_buffer = buffer;
665 1.169 skrll xfer->ux_length = length;
666 1.169 skrll xfer->ux_actlen = 0;
667 1.169 skrll xfer->ux_flags = flags;
668 1.169 skrll xfer->ux_timeout = timeout;
669 1.169 skrll xfer->ux_status = USBD_NOT_STARTED;
670 1.169 skrll xfer->ux_callback = callback;
671 1.169 skrll xfer->ux_request = *req;
672 1.169 skrll xfer->ux_rqflags |= URQ_REQUEST;
673 1.169 skrll xfer->ux_nframes = 0;
674 1.169 skrll }
675 1.169 skrll
676 1.169 skrll void
677 1.169 skrll usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
678 1.169 skrll uint32_t nframes, uint16_t flags, usbd_callback callback)
679 1.169 skrll {
680 1.169 skrll xfer->ux_priv = priv;
681 1.169 skrll xfer->ux_buffer = NULL;
682 1.169 skrll xfer->ux_length = 0;
683 1.169 skrll xfer->ux_actlen = 0;
684 1.169 skrll xfer->ux_flags = flags;
685 1.169 skrll xfer->ux_timeout = USBD_NO_TIMEOUT;
686 1.169 skrll xfer->ux_status = USBD_NOT_STARTED;
687 1.169 skrll xfer->ux_callback = callback;
688 1.169 skrll xfer->ux_rqflags &= ~URQ_REQUEST;
689 1.169 skrll xfer->ux_frlengths = frlengths;
690 1.169 skrll xfer->ux_nframes = nframes;
691 1.201 skrll
692 1.201 skrll for (size_t i = 0; i < xfer->ux_nframes; i++)
693 1.201 skrll xfer->ux_length += xfer->ux_frlengths[i];
694 1.1 augustss }
695 1.1 augustss
696 1.31 augustss void
697 1.169 skrll usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
698 1.169 skrll void **buffer, uint32_t *count, usbd_status *status)
699 1.1 augustss {
700 1.48 augustss if (priv != NULL)
701 1.169 skrll *priv = xfer->ux_priv;
702 1.48 augustss if (buffer != NULL)
703 1.169 skrll *buffer = xfer->ux_buffer;
704 1.48 augustss if (count != NULL)
705 1.169 skrll *count = xfer->ux_actlen;
706 1.48 augustss if (status != NULL)
707 1.169 skrll *status = xfer->ux_status;
708 1.1 augustss }
709 1.1 augustss
710 1.1 augustss usb_config_descriptor_t *
711 1.169 skrll usbd_get_config_descriptor(struct usbd_device *dev)
712 1.1 augustss {
713 1.169 skrll KASSERT(dev != NULL);
714 1.169 skrll
715 1.169 skrll return dev->ud_cdesc;
716 1.1 augustss }
717 1.1 augustss
718 1.1 augustss usb_interface_descriptor_t *
719 1.169 skrll usbd_get_interface_descriptor(struct usbd_interface *iface)
720 1.1 augustss {
721 1.169 skrll KASSERT(iface != NULL);
722 1.169 skrll
723 1.169 skrll return iface->ui_idesc;
724 1.1 augustss }
725 1.1 augustss
726 1.1 augustss usb_device_descriptor_t *
727 1.169 skrll usbd_get_device_descriptor(struct usbd_device *dev)
728 1.1 augustss {
729 1.169 skrll KASSERT(dev != NULL);
730 1.169 skrll
731 1.169 skrll return &dev->ud_ddesc;
732 1.1 augustss }
733 1.1 augustss
734 1.1 augustss usb_endpoint_descriptor_t *
735 1.169 skrll usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
736 1.1 augustss {
737 1.169 skrll
738 1.169 skrll if (index >= iface->ui_idesc->bNumEndpoints)
739 1.169 skrll return NULL;
740 1.169 skrll return iface->ui_endpoints[index].ue_edesc;
741 1.1 augustss }
742 1.1 augustss
743 1.125 jmorse /* Some drivers may wish to abort requests on the default pipe, *
744 1.125 jmorse * but there is no mechanism for getting a handle on it. */
745 1.125 jmorse usbd_status
746 1.125 jmorse usbd_abort_default_pipe(struct usbd_device *device)
747 1.125 jmorse {
748 1.169 skrll return usbd_abort_pipe(device->ud_pipe0);
749 1.125 jmorse }
750 1.125 jmorse
751 1.102 augustss usbd_status
752 1.169 skrll usbd_abort_pipe(struct usbd_pipe *pipe)
753 1.1 augustss {
754 1.48 augustss usbd_status err;
755 1.1 augustss
756 1.169 skrll KASSERT(pipe != NULL);
757 1.169 skrll
758 1.138 mrg usbd_lock_pipe(pipe);
759 1.48 augustss err = usbd_ar_pipe(pipe);
760 1.138 mrg usbd_unlock_pipe(pipe);
761 1.169 skrll return err;
762 1.1 augustss }
763 1.102 augustss
764 1.102 augustss usbd_status
765 1.169 skrll usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
766 1.1 augustss {
767 1.169 skrll struct usbd_device *dev = pipe->up_dev;
768 1.48 augustss usbd_status err;
769 1.1 augustss
770 1.162 skrll USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
771 1.195 riastrad SDT_PROBE1(usb, device, pipe, clear__endpoint__stall, pipe);
772 1.33 augustss
773 1.102 augustss /*
774 1.87 augustss * Clearing en endpoint stall resets the endpoint toggle, so
775 1.33 augustss * do the same to the HC toggle.
776 1.33 augustss */
777 1.195 riastrad SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
778 1.169 skrll pipe->up_methods->upm_cleartoggle(pipe);
779 1.33 augustss
780 1.188 maxv err = usbd_clear_endpoint_feature(dev,
781 1.188 maxv pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
782 1.1 augustss #if 0
783 1.1 augustss XXX should we do this?
784 1.51 augustss if (!err) {
785 1.1 augustss pipe->state = USBD_PIPE_ACTIVE;
786 1.1 augustss /* XXX activate pipe */
787 1.1 augustss }
788 1.1 augustss #endif
789 1.169 skrll return err;
790 1.1 augustss }
791 1.1 augustss
792 1.139 mrg void
793 1.156 skrll usbd_clear_endpoint_stall_task(void *arg)
794 1.7 augustss {
795 1.169 skrll struct usbd_pipe *pipe = arg;
796 1.169 skrll struct usbd_device *dev = pipe->up_dev;
797 1.7 augustss
798 1.195 riastrad SDT_PROBE1(usb, device, pipe, clear__endpoint__stall, pipe);
799 1.195 riastrad SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
800 1.169 skrll pipe->up_methods->upm_cleartoggle(pipe);
801 1.33 augustss
802 1.188 maxv (void)usbd_clear_endpoint_feature(dev,
803 1.188 maxv pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
804 1.139 mrg }
805 1.139 mrg
806 1.139 mrg void
807 1.169 skrll usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
808 1.139 mrg {
809 1.169 skrll usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
810 1.7 augustss }
811 1.7 augustss
812 1.61 augustss void
813 1.169 skrll usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
814 1.61 augustss {
815 1.169 skrll
816 1.195 riastrad SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
817 1.169 skrll pipe->up_methods->upm_cleartoggle(pipe);
818 1.61 augustss }
819 1.61 augustss
820 1.102 augustss usbd_status
821 1.169 skrll usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
822 1.1 augustss {
823 1.169 skrll KASSERT(iface != NULL);
824 1.169 skrll KASSERT(iface->ui_idesc != NULL);
825 1.169 skrll
826 1.169 skrll *count = iface->ui_idesc->bNumEndpoints;
827 1.169 skrll return USBD_NORMAL_COMPLETION;
828 1.1 augustss }
829 1.1 augustss
830 1.102 augustss usbd_status
831 1.169 skrll usbd_interface_count(struct usbd_device *dev, uint8_t *count)
832 1.1 augustss {
833 1.169 skrll
834 1.169 skrll if (dev->ud_cdesc == NULL)
835 1.169 skrll return USBD_NOT_CONFIGURED;
836 1.169 skrll *count = dev->ud_cdesc->bNumInterface;
837 1.169 skrll return USBD_NORMAL_COMPLETION;
838 1.1 augustss }
839 1.1 augustss
840 1.85 augustss void
841 1.169 skrll usbd_interface2device_handle(struct usbd_interface *iface,
842 1.169 skrll struct usbd_device **dev)
843 1.1 augustss {
844 1.169 skrll
845 1.169 skrll *dev = iface->ui_dev;
846 1.1 augustss }
847 1.1 augustss
848 1.102 augustss usbd_status
849 1.169 skrll usbd_device2interface_handle(struct usbd_device *dev,
850 1.169 skrll uint8_t ifaceno, struct usbd_interface **iface)
851 1.1 augustss {
852 1.169 skrll
853 1.169 skrll if (dev->ud_cdesc == NULL)
854 1.169 skrll return USBD_NOT_CONFIGURED;
855 1.169 skrll if (ifaceno >= dev->ud_cdesc->bNumInterface)
856 1.169 skrll return USBD_INVAL;
857 1.169 skrll *iface = &dev->ud_ifaces[ifaceno];
858 1.169 skrll return USBD_NORMAL_COMPLETION;
859 1.1 augustss }
860 1.1 augustss
861 1.169 skrll struct usbd_device *
862 1.169 skrll usbd_pipe2device_handle(struct usbd_pipe *pipe)
863 1.36 augustss {
864 1.169 skrll KASSERT(pipe != NULL);
865 1.169 skrll
866 1.169 skrll return pipe->up_dev;
867 1.36 augustss }
868 1.36 augustss
869 1.13 augustss /* XXXX use altno */
870 1.3 augustss usbd_status
871 1.169 skrll usbd_set_interface(struct usbd_interface *iface, int altidx)
872 1.3 augustss {
873 1.3 augustss usb_device_request_t req;
874 1.48 augustss usbd_status err;
875 1.73 augustss void *endpoints;
876 1.13 augustss
877 1.183 mrg USBHIST_FUNC();
878 1.169 skrll
879 1.169 skrll if (LIST_FIRST(&iface->ui_pipes) != NULL)
880 1.169 skrll return USBD_IN_USE;
881 1.13 augustss
882 1.169 skrll endpoints = iface->ui_endpoints;
883 1.169 skrll int nendpt = iface->ui_idesc->bNumEndpoints;
884 1.183 mrg USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
885 1.175 pgoyette (uintptr_t)iface, (uintptr_t)endpoints,
886 1.175 pgoyette iface->ui_idesc->bNumEndpoints, 0);
887 1.169 skrll err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
888 1.48 augustss if (err)
889 1.169 skrll return err;
890 1.73 augustss
891 1.76 augustss /* new setting works, we can free old endpoints */
892 1.169 skrll if (endpoints != NULL) {
893 1.175 pgoyette USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
894 1.175 pgoyette (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
895 1.169 skrll kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
896 1.73 augustss }
897 1.169 skrll KASSERT(iface->ui_idesc != NULL);
898 1.3 augustss
899 1.3 augustss req.bmRequestType = UT_WRITE_INTERFACE;
900 1.3 augustss req.bRequest = UR_SET_INTERFACE;
901 1.169 skrll USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
902 1.169 skrll USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
903 1.3 augustss USETW(req.wLength, 0);
904 1.169 skrll return usbd_do_request(iface->ui_dev, &req, 0);
905 1.12 augustss }
906 1.12 augustss
907 1.12 augustss int
908 1.74 augustss usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
909 1.12 augustss {
910 1.13 augustss char *p = (char *)cdesc;
911 1.13 augustss char *end = p + UGETW(cdesc->wTotalLength);
912 1.12 augustss usb_interface_descriptor_t *d;
913 1.12 augustss int n;
914 1.12 augustss
915 1.12 augustss for (n = 0; p < end; p += d->bLength) {
916 1.12 augustss d = (usb_interface_descriptor_t *)p;
917 1.102 augustss if (p + d->bLength <= end &&
918 1.12 augustss d->bDescriptorType == UDESC_INTERFACE &&
919 1.13 augustss d->bInterfaceNumber == ifaceno)
920 1.12 augustss n++;
921 1.12 augustss }
922 1.169 skrll return n;
923 1.13 augustss }
924 1.13 augustss
925 1.13 augustss int
926 1.169 skrll usbd_get_interface_altindex(struct usbd_interface *iface)
927 1.13 augustss {
928 1.169 skrll return iface->ui_altindex;
929 1.12 augustss }
930 1.12 augustss
931 1.12 augustss usbd_status
932 1.169 skrll usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
933 1.12 augustss {
934 1.12 augustss usb_device_request_t req;
935 1.12 augustss
936 1.12 augustss req.bmRequestType = UT_READ_INTERFACE;
937 1.12 augustss req.bRequest = UR_GET_INTERFACE;
938 1.12 augustss USETW(req.wValue, 0);
939 1.169 skrll USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
940 1.12 augustss USETW(req.wLength, 1);
941 1.169 skrll return usbd_do_request(iface->ui_dev, &req, aiface);
942 1.3 augustss }
943 1.1 augustss
944 1.1 augustss /*** Internal routines ***/
945 1.1 augustss
946 1.174 skrll /* Dequeue all pipe operations, called with bus lock held. */
947 1.69 augustss Static usbd_status
948 1.169 skrll usbd_ar_pipe(struct usbd_pipe *pipe)
949 1.1 augustss {
950 1.169 skrll struct usbd_xfer *xfer;
951 1.1 augustss
952 1.183 mrg USBHIST_FUNC();
953 1.183 mrg USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
954 1.195 riastrad SDT_PROBE1(usb, device, pipe, abort__start, pipe);
955 1.162 skrll
956 1.169 skrll KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
957 1.138 mrg
958 1.28 augustss #ifdef USB_DEBUG
959 1.28 augustss if (usbdebug > 5)
960 1.28 augustss usbd_dump_queue(pipe);
961 1.28 augustss #endif
962 1.169 skrll pipe->up_repeat = 0;
963 1.194 riastrad pipe->up_running = 0;
964 1.169 skrll pipe->up_aborting = 1;
965 1.169 skrll while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
966 1.175 pgoyette USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
967 1.175 pgoyette "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
968 1.175 pgoyette (uintptr_t)pipe->up_methods, 0);
969 1.182 rin if (xfer->ux_status == USBD_NOT_STARTED) {
970 1.195 riastrad SDT_PROBE1(usb, device, xfer, preabort, xfer);
971 1.203 mrg #ifdef DIAGNOSTIC
972 1.203 mrg xfer->ux_state = XFER_BUSY;
973 1.203 mrg #endif
974 1.182 rin SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
975 1.182 rin } else {
976 1.182 rin /* Make the HC abort it (and invoke the callback). */
977 1.195 riastrad SDT_PROBE1(usb, device, xfer, abort, xfer);
978 1.182 rin pipe->up_methods->upm_abort(xfer);
979 1.182 rin /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
980 1.182 rin }
981 1.20 augustss }
982 1.169 skrll pipe->up_aborting = 0;
983 1.195 riastrad SDT_PROBE1(usb, device, pipe, abort__done, pipe);
984 1.169 skrll return USBD_NORMAL_COMPLETION;
985 1.1 augustss }
986 1.1 augustss
987 1.138 mrg /* Called with USB lock held. */
988 1.31 augustss void
989 1.169 skrll usb_transfer_complete(struct usbd_xfer *xfer)
990 1.1 augustss {
991 1.169 skrll struct usbd_pipe *pipe = xfer->ux_pipe;
992 1.172 skrll struct usbd_bus *bus = pipe->up_dev->ud_bus;
993 1.169 skrll int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
994 1.179 manu int erred;
995 1.169 skrll int polling = bus->ub_usepolling;
996 1.169 skrll int repeat = pipe->up_repeat;
997 1.31 augustss
998 1.183 mrg USBHIST_FUNC();
999 1.183 mrg USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
1000 1.175 pgoyette "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
1001 1.175 pgoyette xfer->ux_actlen);
1002 1.134 jmcneill
1003 1.169 skrll KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1004 1.177 mrg KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
1005 1.177 mrg xfer->ux_state);
1006 1.169 skrll KASSERT(pipe != NULL);
1007 1.5 augustss
1008 1.179 manu /*
1009 1.179 manu * If device is known to miss out ack, then pretend that
1010 1.179 manu * output timeout is a success. Userland should handle
1011 1.179 manu * the logic to verify that the operation succeeded.
1012 1.179 manu */
1013 1.179 manu if (pipe->up_dev->ud_quirks &&
1014 1.179 manu pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
1015 1.179 manu xfer->ux_status == USBD_TIMEOUT &&
1016 1.179 manu !usbd_xfer_isread(xfer)) {
1017 1.179 manu USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
1018 1.197 christos "hiding write timeout to %jd.%jd for %ju bytes written",
1019 1.180 manu (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
1020 1.179 manu xfer->ux_length);
1021 1.179 manu
1022 1.179 manu xfer->ux_status = USBD_NORMAL_COMPLETION;
1023 1.179 manu xfer->ux_actlen = xfer->ux_length;
1024 1.179 manu }
1025 1.179 manu
1026 1.179 manu erred = xfer->ux_status == USBD_CANCELLED ||
1027 1.179 manu xfer->ux_status == USBD_TIMEOUT;
1028 1.179 manu
1029 1.47 augustss if (!repeat) {
1030 1.47 augustss /* Remove request from queue. */
1031 1.153 jakllsch
1032 1.169 skrll KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
1033 1.145 skrll "pipe %p is empty, but xfer %p wants to complete", pipe,
1034 1.145 skrll xfer);
1035 1.169 skrll KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
1036 1.169 skrll "xfer %p is not start of queue (%p is at start)", xfer,
1037 1.169 skrll SIMPLEQ_FIRST(&pipe->up_queue));
1038 1.169 skrll
1039 1.47 augustss #ifdef DIAGNOSTIC
1040 1.169 skrll xfer->ux_state = XFER_BUSY;
1041 1.45 augustss #endif
1042 1.169 skrll SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
1043 1.47 augustss }
1044 1.175 pgoyette USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
1045 1.175 pgoyette (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
1046 1.175 pgoyette 0);
1047 1.31 augustss
1048 1.31 augustss /* Count completed transfers. */
1049 1.169 skrll ++pipe->up_dev->ud_bus->ub_stats.uds_requests
1050 1.169 skrll [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
1051 1.1 augustss
1052 1.169 skrll xfer->ux_done = 1;
1053 1.169 skrll if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
1054 1.169 skrll !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
1055 1.175 pgoyette USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
1056 1.169 skrll xfer->ux_actlen, xfer->ux_length, 0, 0);
1057 1.169 skrll xfer->ux_status = USBD_SHORT_XFER;
1058 1.1 augustss }
1059 1.31 augustss
1060 1.175 pgoyette USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
1061 1.175 pgoyette (uintptr_t)pipe->up_methods->upm_done, 0, 0);
1062 1.195 riastrad SDT_PROBE2(usb, device, xfer, done, xfer, xfer->ux_status);
1063 1.169 skrll pipe->up_methods->upm_done(xfer);
1064 1.169 skrll
1065 1.169 skrll if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
1066 1.169 skrll KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
1067 1.169 skrll "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
1068 1.148 jmcneill
1069 1.169 skrll /* Only if IN transfer */
1070 1.169 skrll if (usbd_xfer_isread(xfer)) {
1071 1.169 skrll memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
1072 1.138 mrg }
1073 1.169 skrll }
1074 1.169 skrll
1075 1.175 pgoyette USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
1076 1.175 pgoyette (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
1077 1.169 skrll
1078 1.169 skrll if (xfer->ux_callback) {
1079 1.178 mrg if (!polling) {
1080 1.169 skrll mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1081 1.178 mrg if (!(pipe->up_flags & USBD_MPSAFE))
1082 1.178 mrg KERNEL_LOCK(1, curlwp);
1083 1.178 mrg }
1084 1.169 skrll
1085 1.169 skrll xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
1086 1.169 skrll
1087 1.178 mrg if (!polling) {
1088 1.178 mrg if (!(pipe->up_flags & USBD_MPSAFE))
1089 1.178 mrg KERNEL_UNLOCK_ONE(curlwp);
1090 1.169 skrll mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1091 1.178 mrg }
1092 1.124 jmcneill }
1093 1.31 augustss
1094 1.138 mrg if (sync && !polling) {
1095 1.175 pgoyette USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
1096 1.175 pgoyette (uintptr_t)xfer, 0, 0, 0);
1097 1.169 skrll cv_broadcast(&xfer->ux_cv);
1098 1.138 mrg }
1099 1.31 augustss
1100 1.169 skrll if (repeat) {
1101 1.169 skrll xfer->ux_actlen = 0;
1102 1.169 skrll xfer->ux_status = USBD_NOT_STARTED;
1103 1.169 skrll } else {
1104 1.40 augustss /* XXX should we stop the queue on all errors? */
1105 1.169 skrll if (erred && pipe->up_iface != NULL) /* not control pipe */
1106 1.169 skrll pipe->up_running = 0;
1107 1.40 augustss }
1108 1.169 skrll if (pipe->up_running && pipe->up_serialise)
1109 1.169 skrll usbd_start_next(pipe);
1110 1.1 augustss }
1111 1.1 augustss
1112 1.138 mrg /* Called with USB lock held. */
1113 1.31 augustss usbd_status
1114 1.169 skrll usb_insert_transfer(struct usbd_xfer *xfer)
1115 1.1 augustss {
1116 1.169 skrll struct usbd_pipe *pipe = xfer->ux_pipe;
1117 1.48 augustss usbd_status err;
1118 1.31 augustss
1119 1.183 mrg USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
1120 1.183 mrg "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
1121 1.183 mrg (uintptr_t)xfer, (uintptr_t)pipe,
1122 1.175 pgoyette pipe->up_running, xfer->ux_timeout);
1123 1.134 jmcneill
1124 1.169 skrll KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1125 1.174 skrll KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1126 1.174 skrll xfer->ux_state);
1127 1.134 jmcneill
1128 1.93 augustss #ifdef DIAGNOSTIC
1129 1.169 skrll xfer->ux_state = XFER_ONQU;
1130 1.93 augustss #endif
1131 1.169 skrll SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1132 1.169 skrll if (pipe->up_running && pipe->up_serialise)
1133 1.48 augustss err = USBD_IN_PROGRESS;
1134 1.37 augustss else {
1135 1.169 skrll pipe->up_running = 1;
1136 1.48 augustss err = USBD_NORMAL_COMPLETION;
1137 1.37 augustss }
1138 1.175 pgoyette USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1139 1.175 pgoyette err, 0, 0);
1140 1.169 skrll return err;
1141 1.1 augustss }
1142 1.1 augustss
1143 1.138 mrg /* Called with USB lock held. */
1144 1.31 augustss void
1145 1.169 skrll usbd_start_next(struct usbd_pipe *pipe)
1146 1.31 augustss {
1147 1.169 skrll struct usbd_xfer *xfer;
1148 1.48 augustss usbd_status err;
1149 1.40 augustss
1150 1.183 mrg USBHIST_FUNC();
1151 1.162 skrll
1152 1.169 skrll KASSERT(pipe != NULL);
1153 1.169 skrll KASSERT(pipe->up_methods != NULL);
1154 1.169 skrll KASSERT(pipe->up_methods->upm_start != NULL);
1155 1.169 skrll KASSERT(pipe->up_serialise == true);
1156 1.31 augustss
1157 1.169 skrll int polling = pipe->up_dev->ud_bus->ub_usepolling;
1158 1.169 skrll KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1159 1.138 mrg
1160 1.31 augustss /* Get next request in queue. */
1161 1.169 skrll xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1162 1.183 mrg USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1163 1.175 pgoyette (uintptr_t)xfer, 0, 0);
1164 1.51 augustss if (xfer == NULL) {
1165 1.169 skrll pipe->up_running = 0;
1166 1.51 augustss } else {
1167 1.165 skrll if (!polling)
1168 1.169 skrll mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1169 1.195 riastrad SDT_PROBE2(usb, device, pipe, start, pipe, xfer);
1170 1.169 skrll err = pipe->up_methods->upm_start(xfer);
1171 1.165 skrll if (!polling)
1172 1.169 skrll mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1173 1.158 skrll
1174 1.48 augustss if (err != USBD_IN_PROGRESS) {
1175 1.175 pgoyette USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1176 1.169 skrll pipe->up_running = 0;
1177 1.31 augustss /* XXX do what? */
1178 1.31 augustss }
1179 1.1 augustss }
1180 1.138 mrg
1181 1.169 skrll KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1182 1.1 augustss }
1183 1.1 augustss
1184 1.1 augustss usbd_status
1185 1.169 skrll usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1186 1.1 augustss {
1187 1.17 augustss
1188 1.169 skrll return usbd_do_request_flags(dev, req, data, 0, 0,
1189 1.169 skrll USBD_DEFAULT_TIMEOUT);
1190 1.77 augustss }
1191 1.77 augustss
1192 1.77 augustss usbd_status
1193 1.169 skrll usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1194 1.169 skrll void *data, uint16_t flags, int *actlen, uint32_t timeout)
1195 1.77 augustss {
1196 1.186 mrg size_t len = UGETW(req->wLength);
1197 1.186 mrg
1198 1.186 mrg return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
1199 1.186 mrg }
1200 1.186 mrg
1201 1.186 mrg usbd_status
1202 1.186 mrg usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
1203 1.186 mrg size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
1204 1.186 mrg {
1205 1.169 skrll struct usbd_xfer *xfer;
1206 1.48 augustss usbd_status err;
1207 1.186 mrg
1208 1.186 mrg KASSERT(len >= UGETW(req->wLength));
1209 1.1 augustss
1210 1.183 mrg USBHIST_FUNC();
1211 1.186 mrg USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
1212 1.185 mrg (uintptr_t)dev, (uintptr_t)req, flags, len);
1213 1.183 mrg
1214 1.169 skrll ASSERT_SLEEPABLE();
1215 1.7 augustss
1216 1.169 skrll int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1217 1.169 skrll if (error)
1218 1.169 skrll return error;
1219 1.169 skrll
1220 1.169 skrll usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1221 1.169 skrll UGETW(req->wLength), flags, NULL);
1222 1.169 skrll KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1223 1.48 augustss err = usbd_sync_transfer(xfer);
1224 1.1 augustss #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1225 1.169 skrll if (xfer->ux_actlen > xfer->ux_length) {
1226 1.198 christos USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1227 1.169 skrll dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1228 1.198 christos USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1229 1.175 pgoyette "index = %jd",
1230 1.169 skrll xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1231 1.169 skrll UGETW(xfer->ux_request.wIndex), 0);
1232 1.175 pgoyette USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1233 1.175 pgoyette "actlen = %jd",
1234 1.169 skrll UGETW(xfer->ux_request.wLength),
1235 1.169 skrll xfer->ux_length, xfer->ux_actlen, 0);
1236 1.112 dogcow }
1237 1.1 augustss #endif
1238 1.48 augustss if (actlen != NULL)
1239 1.169 skrll *actlen = xfer->ux_actlen;
1240 1.169 skrll
1241 1.169 skrll usbd_destroy_xfer(xfer);
1242 1.15 augustss
1243 1.132 matt if (err) {
1244 1.175 pgoyette USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1245 1.132 matt }
1246 1.169 skrll return err;
1247 1.7 augustss }
1248 1.7 augustss
1249 1.176 khorben static void
1250 1.176 khorben usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1251 1.176 khorben {
1252 1.202 jakllsch usbd_destroy_xfer(xfer);
1253 1.176 khorben }
1254 1.176 khorben
1255 1.176 khorben /*
1256 1.176 khorben * Execute a request without waiting for completion.
1257 1.176 khorben * Can be used from interrupt context.
1258 1.176 khorben */
1259 1.176 khorben usbd_status
1260 1.176 khorben usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1261 1.176 khorben usb_device_request_t *req, void *priv, usbd_callback callback)
1262 1.176 khorben {
1263 1.176 khorben usbd_status err;
1264 1.176 khorben
1265 1.176 khorben if (callback == NULL)
1266 1.176 khorben callback = usbd_request_async_cb;
1267 1.176 khorben
1268 1.176 khorben usbd_setup_default_xfer(xfer, dev, priv,
1269 1.176 khorben USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1270 1.176 khorben callback);
1271 1.176 khorben err = usbd_transfer(xfer);
1272 1.176 khorben if (err != USBD_IN_PROGRESS) {
1273 1.202 jakllsch usbd_destroy_xfer(xfer);
1274 1.176 khorben return (err);
1275 1.176 khorben }
1276 1.176 khorben return (USBD_NORMAL_COMPLETION);
1277 1.176 khorben }
1278 1.176 khorben
1279 1.78 augustss const struct usbd_quirks *
1280 1.169 skrll usbd_get_quirks(struct usbd_device *dev)
1281 1.1 augustss {
1282 1.81 augustss #ifdef DIAGNOSTIC
1283 1.81 augustss if (dev == NULL) {
1284 1.81 augustss printf("usbd_get_quirks: dev == NULL\n");
1285 1.81 augustss return 0;
1286 1.81 augustss }
1287 1.81 augustss #endif
1288 1.169 skrll return dev->ud_quirks;
1289 1.1 augustss }
1290 1.1 augustss
1291 1.1 augustss /* XXX do periodic free() of free list */
1292 1.1 augustss
1293 1.6 augustss /*
1294 1.6 augustss * Called from keyboard driver when in polling mode.
1295 1.6 augustss */
1296 1.6 augustss void
1297 1.169 skrll usbd_dopoll(struct usbd_interface *iface)
1298 1.6 augustss {
1299 1.169 skrll iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1300 1.8 augustss }
1301 1.8 augustss
1302 1.138 mrg /*
1303 1.178 mrg * This is for keyboard driver as well, which only operates in polling
1304 1.178 mrg * mode from the ask root, etc., prompt and from DDB.
1305 1.138 mrg */
1306 1.8 augustss void
1307 1.169 skrll usbd_set_polling(struct usbd_device *dev, int on)
1308 1.8 augustss {
1309 1.42 augustss if (on)
1310 1.169 skrll dev->ud_bus->ub_usepolling++;
1311 1.42 augustss else
1312 1.169 skrll dev->ud_bus->ub_usepolling--;
1313 1.133 jmcneill
1314 1.133 jmcneill /* Kick the host controller when switching modes */
1315 1.169 skrll mutex_enter(dev->ud_bus->ub_lock);
1316 1.169 skrll dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1317 1.169 skrll mutex_exit(dev->ud_bus->ub_lock);
1318 1.6 augustss }
1319 1.12 augustss
1320 1.12 augustss
1321 1.12 augustss usb_endpoint_descriptor_t *
1322 1.169 skrll usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1323 1.12 augustss {
1324 1.12 augustss struct usbd_endpoint *ep;
1325 1.12 augustss int i;
1326 1.12 augustss
1327 1.169 skrll for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1328 1.169 skrll ep = &iface->ui_endpoints[i];
1329 1.169 skrll if (ep->ue_edesc->bEndpointAddress == address)
1330 1.169 skrll return iface->ui_endpoints[i].ue_edesc;
1331 1.12 augustss }
1332 1.169 skrll return NULL;
1333 1.63 augustss }
1334 1.63 augustss
1335 1.63 augustss /*
1336 1.63 augustss * usbd_ratecheck() can limit the number of error messages that occurs.
1337 1.63 augustss * When a device is unplugged it may take up to 0.25s for the hub driver
1338 1.169 skrll * to notice it. If the driver continuously tries to do I/O operations
1339 1.63 augustss * this can generate a large number of messages.
1340 1.63 augustss */
1341 1.63 augustss int
1342 1.74 augustss usbd_ratecheck(struct timeval *last)
1343 1.63 augustss {
1344 1.64 augustss static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1345 1.63 augustss
1346 1.169 skrll return ratecheck(last, &errinterval);
1347 1.89 augustss }
1348 1.89 augustss
1349 1.89 augustss /*
1350 1.89 augustss * Search for a vendor/product pair in an array. The item size is
1351 1.89 augustss * given as an argument.
1352 1.89 augustss */
1353 1.90 augustss const struct usb_devno *
1354 1.90 augustss usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1355 1.169 skrll uint16_t vendor, uint16_t product)
1356 1.89 augustss {
1357 1.89 augustss while (nentries-- > 0) {
1358 1.169 skrll uint16_t tproduct = tbl->ud_product;
1359 1.93 augustss if (tbl->ud_vendor == vendor &&
1360 1.93 augustss (tproduct == product || tproduct == USB_PRODUCT_ANY))
1361 1.169 skrll return tbl;
1362 1.95 augustss tbl = (const struct usb_devno *)((const char *)tbl + sz);
1363 1.89 augustss }
1364 1.169 skrll return NULL;
1365 1.12 augustss }
1366 1.12 augustss
1367 1.105 augustss usbd_status
1368 1.169 skrll usbd_get_string(struct usbd_device *dev, int si, char *buf)
1369 1.105 augustss {
1370 1.115 pavel return usbd_get_string0(dev, si, buf, 1);
1371 1.115 pavel }
1372 1.115 pavel
1373 1.115 pavel usbd_status
1374 1.169 skrll usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1375 1.115 pavel {
1376 1.169 skrll int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1377 1.105 augustss usb_string_descriptor_t us;
1378 1.105 augustss char *s;
1379 1.116 macallan int i, n;
1380 1.169 skrll uint16_t c;
1381 1.105 augustss usbd_status err;
1382 1.105 augustss int size;
1383 1.105 augustss
1384 1.162 skrll USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1385 1.162 skrll
1386 1.105 augustss buf[0] = '\0';
1387 1.105 augustss if (si == 0)
1388 1.169 skrll return USBD_INVAL;
1389 1.169 skrll if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1390 1.169 skrll return USBD_STALLED;
1391 1.169 skrll if (dev->ud_langid == USBD_NOLANG) {
1392 1.105 augustss /* Set up default language */
1393 1.105 augustss err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1394 1.105 augustss &size);
1395 1.105 augustss if (err || size < 4) {
1396 1.162 skrll USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1397 1.162 skrll 0, 0, 0, 0);
1398 1.169 skrll dev->ud_langid = 0; /* Well, just pick something then */
1399 1.105 augustss } else {
1400 1.105 augustss /* Pick the first language as the default. */
1401 1.169 skrll dev->ud_langid = UGETW(us.bString[0]);
1402 1.105 augustss }
1403 1.105 augustss }
1404 1.169 skrll err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1405 1.105 augustss if (err)
1406 1.169 skrll return err;
1407 1.105 augustss s = buf;
1408 1.105 augustss n = size / 2 - 1;
1409 1.115 pavel if (unicode) {
1410 1.115 pavel for (i = 0; i < n; i++) {
1411 1.115 pavel c = UGETW(us.bString[i]);
1412 1.115 pavel if (swap)
1413 1.115 pavel c = (c >> 8) | (c << 8);
1414 1.119 drochner s += wput_utf8(s, 3, c);
1415 1.115 pavel }
1416 1.115 pavel *s++ = 0;
1417 1.115 pavel }
1418 1.115 pavel #ifdef COMPAT_30
1419 1.115 pavel else {
1420 1.119 drochner for (i = 0; i < n; i++) {
1421 1.115 pavel c = UGETW(us.bString[i]);
1422 1.115 pavel if (swap)
1423 1.115 pavel c = (c >> 8) | (c << 8);
1424 1.119 drochner *s++ = (c < 0x80) ? c : '?';
1425 1.107 augustss }
1426 1.119 drochner *s++ = 0;
1427 1.105 augustss }
1428 1.115 pavel #endif
1429 1.169 skrll return USBD_NORMAL_COMPLETION;
1430 1.105 augustss }
1431 1.192 riastrad
1432 1.192 riastrad /*
1433 1.192 riastrad * usbd_xfer_trycomplete(xfer)
1434 1.192 riastrad *
1435 1.192 riastrad * Try to claim xfer for completion. Return true if successful,
1436 1.192 riastrad * false if the xfer has been synchronously aborted or has timed
1437 1.192 riastrad * out.
1438 1.192 riastrad *
1439 1.192 riastrad * If this returns true, caller is responsible for setting
1440 1.192 riastrad * xfer->ux_status and calling usb_transfer_complete. To be used
1441 1.192 riastrad * in a host controller interrupt handler.
1442 1.192 riastrad *
1443 1.192 riastrad * Caller must either hold the bus lock or have the bus in polling
1444 1.192 riastrad * mode.
1445 1.192 riastrad */
1446 1.192 riastrad bool
1447 1.192 riastrad usbd_xfer_trycomplete(struct usbd_xfer *xfer)
1448 1.192 riastrad {
1449 1.192 riastrad struct usbd_bus *bus __diagused = xfer->ux_bus;
1450 1.192 riastrad
1451 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1452 1.192 riastrad
1453 1.192 riastrad /*
1454 1.192 riastrad * If software has completed it, either by synchronous abort or
1455 1.192 riastrad * by timeout, too late.
1456 1.192 riastrad */
1457 1.192 riastrad if (xfer->ux_status != USBD_IN_PROGRESS)
1458 1.192 riastrad return false;
1459 1.192 riastrad
1460 1.192 riastrad /*
1461 1.192 riastrad * We are completing the xfer. Cancel the timeout if we can,
1462 1.192 riastrad * but only asynchronously. See usbd_xfer_cancel_timeout_async
1463 1.192 riastrad * for why we need not wait for the callout or task here.
1464 1.192 riastrad */
1465 1.192 riastrad usbd_xfer_cancel_timeout_async(xfer);
1466 1.192 riastrad
1467 1.192 riastrad /* Success! Note: Caller must set xfer->ux_status afterwar. */
1468 1.192 riastrad return true;
1469 1.192 riastrad }
1470 1.192 riastrad
1471 1.192 riastrad /*
1472 1.192 riastrad * usbd_xfer_abort(xfer)
1473 1.192 riastrad *
1474 1.192 riastrad * Try to claim xfer to abort. If successful, mark it completed
1475 1.192 riastrad * with USBD_CANCELLED and call the bus-specific method to abort
1476 1.192 riastrad * at the hardware level.
1477 1.192 riastrad *
1478 1.192 riastrad * To be called in thread context from struct
1479 1.192 riastrad * usbd_pipe_methods::upm_abort.
1480 1.192 riastrad *
1481 1.192 riastrad * Caller must hold the bus lock.
1482 1.192 riastrad */
1483 1.192 riastrad void
1484 1.192 riastrad usbd_xfer_abort(struct usbd_xfer *xfer)
1485 1.192 riastrad {
1486 1.192 riastrad struct usbd_bus *bus = xfer->ux_bus;
1487 1.192 riastrad
1488 1.192 riastrad KASSERT(mutex_owned(bus->ub_lock));
1489 1.192 riastrad
1490 1.192 riastrad /*
1491 1.192 riastrad * If host controller interrupt or timer interrupt has
1492 1.192 riastrad * completed it, too late. But the xfer cannot be
1493 1.192 riastrad * cancelled already -- only one caller can synchronously
1494 1.192 riastrad * abort.
1495 1.192 riastrad */
1496 1.192 riastrad KASSERT(xfer->ux_status != USBD_CANCELLED);
1497 1.192 riastrad if (xfer->ux_status != USBD_IN_PROGRESS)
1498 1.192 riastrad return;
1499 1.192 riastrad
1500 1.192 riastrad /*
1501 1.192 riastrad * Cancel the timeout if we can, but only asynchronously; see
1502 1.192 riastrad * usbd_xfer_cancel_timeout_async for why we need not wait for
1503 1.192 riastrad * the callout or task here.
1504 1.192 riastrad */
1505 1.192 riastrad usbd_xfer_cancel_timeout_async(xfer);
1506 1.192 riastrad
1507 1.192 riastrad /*
1508 1.192 riastrad * We beat everyone else. Claim the status as cancelled and do
1509 1.192 riastrad * the bus-specific dance to abort the hardware.
1510 1.192 riastrad */
1511 1.192 riastrad xfer->ux_status = USBD_CANCELLED;
1512 1.192 riastrad bus->ub_methods->ubm_abortx(xfer);
1513 1.192 riastrad }
1514 1.192 riastrad
1515 1.192 riastrad /*
1516 1.192 riastrad * usbd_xfer_timeout(xfer)
1517 1.192 riastrad *
1518 1.192 riastrad * Called at IPL_SOFTCLOCK when too much time has elapsed waiting
1519 1.192 riastrad * for xfer to complete. Since we can't abort the xfer at
1520 1.192 riastrad * IPL_SOFTCLOCK, defer to a usb_task to run it in thread context,
1521 1.192 riastrad * unless the xfer has completed or aborted concurrently -- and if
1522 1.192 riastrad * the xfer has also been resubmitted, take care of rescheduling
1523 1.192 riastrad * the callout.
1524 1.192 riastrad */
1525 1.192 riastrad static void
1526 1.192 riastrad usbd_xfer_timeout(void *cookie)
1527 1.192 riastrad {
1528 1.192 riastrad struct usbd_xfer *xfer = cookie;
1529 1.192 riastrad struct usbd_bus *bus = xfer->ux_bus;
1530 1.192 riastrad struct usbd_device *dev = xfer->ux_pipe->up_dev;
1531 1.192 riastrad
1532 1.192 riastrad /* Acquire the lock so we can transition the timeout state. */
1533 1.192 riastrad mutex_enter(bus->ub_lock);
1534 1.192 riastrad
1535 1.192 riastrad /*
1536 1.192 riastrad * Use usbd_xfer_probe_timeout to check whether the timeout is
1537 1.192 riastrad * still valid, or to reschedule the callout if necessary. If
1538 1.192 riastrad * it is still valid, schedule the task.
1539 1.192 riastrad */
1540 1.192 riastrad if (usbd_xfer_probe_timeout(xfer))
1541 1.192 riastrad usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
1542 1.192 riastrad
1543 1.192 riastrad /*
1544 1.192 riastrad * Notify usbd_xfer_cancel_timeout_async that we may have
1545 1.192 riastrad * scheduled the task. This causes callout_invoking to return
1546 1.192 riastrad * false in usbd_xfer_cancel_timeout_async so that it can tell
1547 1.192 riastrad * which stage in the callout->task->abort process we're at.
1548 1.192 riastrad */
1549 1.192 riastrad callout_ack(&xfer->ux_callout);
1550 1.192 riastrad
1551 1.192 riastrad /* All done -- release the lock. */
1552 1.192 riastrad mutex_exit(bus->ub_lock);
1553 1.192 riastrad }
1554 1.192 riastrad
1555 1.192 riastrad /*
1556 1.192 riastrad * usbd_xfer_timeout_task(xfer)
1557 1.192 riastrad *
1558 1.192 riastrad * Called in thread context when too much time has elapsed waiting
1559 1.192 riastrad * for xfer to complete. Abort the xfer with USBD_TIMEOUT, unless
1560 1.192 riastrad * it has completed or aborted concurrently -- and if the xfer has
1561 1.192 riastrad * also been resubmitted, take care of rescheduling the callout.
1562 1.192 riastrad */
1563 1.192 riastrad static void
1564 1.192 riastrad usbd_xfer_timeout_task(void *cookie)
1565 1.192 riastrad {
1566 1.192 riastrad struct usbd_xfer *xfer = cookie;
1567 1.192 riastrad struct usbd_bus *bus = xfer->ux_bus;
1568 1.192 riastrad
1569 1.192 riastrad /* Acquire the lock so we can transition the timeout state. */
1570 1.192 riastrad mutex_enter(bus->ub_lock);
1571 1.192 riastrad
1572 1.192 riastrad /*
1573 1.192 riastrad * Use usbd_xfer_probe_timeout to check whether the timeout is
1574 1.192 riastrad * still valid, or to reschedule the callout if necessary. If
1575 1.192 riastrad * it is not valid -- the timeout has been asynchronously
1576 1.192 riastrad * cancelled, or the xfer has already been resubmitted -- then
1577 1.192 riastrad * we're done here.
1578 1.192 riastrad */
1579 1.192 riastrad if (!usbd_xfer_probe_timeout(xfer))
1580 1.192 riastrad goto out;
1581 1.192 riastrad
1582 1.192 riastrad /*
1583 1.192 riastrad * May have completed or been aborted, but we're the only one
1584 1.192 riastrad * who can time it out. If it has completed or been aborted,
1585 1.192 riastrad * no need to timeout.
1586 1.192 riastrad */
1587 1.192 riastrad KASSERT(xfer->ux_status != USBD_TIMEOUT);
1588 1.192 riastrad if (xfer->ux_status != USBD_IN_PROGRESS)
1589 1.192 riastrad goto out;
1590 1.192 riastrad
1591 1.192 riastrad /*
1592 1.192 riastrad * We beat everyone else. Claim the status as timed out and do
1593 1.192 riastrad * the bus-specific dance to abort the hardware.
1594 1.192 riastrad */
1595 1.192 riastrad xfer->ux_status = USBD_TIMEOUT;
1596 1.192 riastrad bus->ub_methods->ubm_abortx(xfer);
1597 1.192 riastrad
1598 1.192 riastrad out: /* All done -- release the lock. */
1599 1.192 riastrad mutex_exit(bus->ub_lock);
1600 1.192 riastrad }
1601 1.192 riastrad
1602 1.192 riastrad /*
1603 1.192 riastrad * usbd_xfer_probe_timeout(xfer)
1604 1.192 riastrad *
1605 1.192 riastrad * Probe the status of xfer's timeout. Acknowledge and process a
1606 1.192 riastrad * request to reschedule. Return true if the timeout is still
1607 1.192 riastrad * valid and the caller should take further action (queueing a
1608 1.192 riastrad * task or aborting the xfer), false if it must stop here.
1609 1.192 riastrad */
1610 1.192 riastrad static bool
1611 1.192 riastrad usbd_xfer_probe_timeout(struct usbd_xfer *xfer)
1612 1.192 riastrad {
1613 1.192 riastrad struct usbd_bus *bus = xfer->ux_bus;
1614 1.192 riastrad bool valid;
1615 1.192 riastrad
1616 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1617 1.192 riastrad
1618 1.192 riastrad /* The timeout must be set. */
1619 1.192 riastrad KASSERT(xfer->ux_timeout_set);
1620 1.192 riastrad
1621 1.192 riastrad /*
1622 1.192 riastrad * Neither callout nor task may be pending; they execute
1623 1.192 riastrad * alternately in lock step.
1624 1.192 riastrad */
1625 1.192 riastrad KASSERT(!callout_pending(&xfer->ux_callout));
1626 1.192 riastrad KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1627 1.192 riastrad
1628 1.192 riastrad /* There are a few cases... */
1629 1.192 riastrad if (bus->ub_methods->ubm_dying(bus)) {
1630 1.192 riastrad /* Host controller dying. Drop it all on the floor. */
1631 1.192 riastrad xfer->ux_timeout_set = false;
1632 1.192 riastrad xfer->ux_timeout_reset = false;
1633 1.192 riastrad valid = false;
1634 1.192 riastrad } else if (xfer->ux_timeout_reset) {
1635 1.192 riastrad /*
1636 1.192 riastrad * The xfer completed _and_ got resubmitted while we
1637 1.192 riastrad * waited for the lock. Acknowledge the request to
1638 1.192 riastrad * reschedule, and reschedule it if there is a timeout
1639 1.192 riastrad * and the bus is not polling.
1640 1.192 riastrad */
1641 1.192 riastrad xfer->ux_timeout_reset = false;
1642 1.192 riastrad if (xfer->ux_timeout && !bus->ub_usepolling) {
1643 1.192 riastrad KASSERT(xfer->ux_timeout_set);
1644 1.192 riastrad callout_schedule(&xfer->ux_callout,
1645 1.192 riastrad mstohz(xfer->ux_timeout));
1646 1.192 riastrad } else {
1647 1.192 riastrad /* No more callout or task scheduled. */
1648 1.192 riastrad xfer->ux_timeout_set = false;
1649 1.192 riastrad }
1650 1.192 riastrad valid = false;
1651 1.192 riastrad } else if (xfer->ux_status != USBD_IN_PROGRESS) {
1652 1.192 riastrad /*
1653 1.192 riastrad * The xfer has completed by hardware completion or by
1654 1.192 riastrad * software abort, and has not been resubmitted, so the
1655 1.192 riastrad * timeout must be unset, and is no longer valid for
1656 1.192 riastrad * the caller.
1657 1.192 riastrad */
1658 1.192 riastrad xfer->ux_timeout_set = false;
1659 1.192 riastrad valid = false;
1660 1.192 riastrad } else {
1661 1.192 riastrad /*
1662 1.192 riastrad * The xfer has not yet completed, so the timeout is
1663 1.192 riastrad * valid.
1664 1.192 riastrad */
1665 1.192 riastrad valid = true;
1666 1.192 riastrad }
1667 1.192 riastrad
1668 1.192 riastrad /* Any reset must have been processed. */
1669 1.192 riastrad KASSERT(!xfer->ux_timeout_reset);
1670 1.192 riastrad
1671 1.192 riastrad /*
1672 1.192 riastrad * Either we claim the timeout is set, or the callout is idle.
1673 1.192 riastrad * If the timeout is still set, we may be handing off to the
1674 1.192 riastrad * task instead, so this is an if but not an iff.
1675 1.192 riastrad */
1676 1.192 riastrad KASSERT(xfer->ux_timeout_set || !callout_pending(&xfer->ux_callout));
1677 1.192 riastrad
1678 1.192 riastrad /*
1679 1.192 riastrad * The task must be idle now.
1680 1.192 riastrad *
1681 1.192 riastrad * - If the caller is the callout, _and_ the timeout is still
1682 1.192 riastrad * valid, the caller will schedule it, but it hasn't been
1683 1.192 riastrad * scheduled yet. (If the timeout is not valid, the task
1684 1.192 riastrad * should not be scheduled.)
1685 1.192 riastrad *
1686 1.192 riastrad * - If the caller is the task, it cannot be scheduled again
1687 1.192 riastrad * until the callout runs again, which won't happen until we
1688 1.192 riastrad * next release the lock.
1689 1.192 riastrad */
1690 1.192 riastrad KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1691 1.192 riastrad
1692 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1693 1.192 riastrad
1694 1.192 riastrad return valid;
1695 1.192 riastrad }
1696 1.192 riastrad
1697 1.192 riastrad /*
1698 1.192 riastrad * usbd_xfer_schedule_timeout(xfer)
1699 1.192 riastrad *
1700 1.192 riastrad * Ensure that xfer has a timeout. If the callout is already
1701 1.192 riastrad * queued or the task is already running, request that they
1702 1.192 riastrad * reschedule the callout. If not, and if we're not polling,
1703 1.192 riastrad * schedule the callout anew.
1704 1.192 riastrad *
1705 1.192 riastrad * To be called in thread context from struct
1706 1.192 riastrad * usbd_pipe_methods::upm_start.
1707 1.192 riastrad */
1708 1.192 riastrad void
1709 1.192 riastrad usbd_xfer_schedule_timeout(struct usbd_xfer *xfer)
1710 1.192 riastrad {
1711 1.192 riastrad struct usbd_bus *bus = xfer->ux_bus;
1712 1.192 riastrad
1713 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1714 1.192 riastrad
1715 1.192 riastrad if (xfer->ux_timeout_set) {
1716 1.192 riastrad /*
1717 1.192 riastrad * Callout or task has fired from a prior completed
1718 1.192 riastrad * xfer but has not yet noticed that the xfer is done.
1719 1.192 riastrad * Ask it to reschedule itself to ux_timeout.
1720 1.192 riastrad */
1721 1.192 riastrad xfer->ux_timeout_reset = true;
1722 1.192 riastrad } else if (xfer->ux_timeout && !bus->ub_usepolling) {
1723 1.192 riastrad /* Callout is not scheduled. Schedule it. */
1724 1.192 riastrad KASSERT(!callout_pending(&xfer->ux_callout));
1725 1.192 riastrad callout_schedule(&xfer->ux_callout, mstohz(xfer->ux_timeout));
1726 1.192 riastrad xfer->ux_timeout_set = true;
1727 1.192 riastrad }
1728 1.192 riastrad
1729 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1730 1.192 riastrad }
1731 1.192 riastrad
1732 1.192 riastrad /*
1733 1.192 riastrad * usbd_xfer_cancel_timeout_async(xfer)
1734 1.192 riastrad *
1735 1.192 riastrad * Cancel the callout and the task of xfer, which have not yet run
1736 1.192 riastrad * to completion, but don't wait for the callout or task to finish
1737 1.192 riastrad * running.
1738 1.192 riastrad *
1739 1.192 riastrad * If they have already fired, at worst they are waiting for the
1740 1.192 riastrad * bus lock. They will see that the xfer is no longer in progress
1741 1.192 riastrad * and give up, or they will see that the xfer has been
1742 1.192 riastrad * resubmitted with a new timeout and reschedule the callout.
1743 1.192 riastrad *
1744 1.192 riastrad * If a resubmitted request completed so fast that the callout
1745 1.192 riastrad * didn't have time to process a timer reset, just cancel the
1746 1.192 riastrad * timer reset.
1747 1.192 riastrad */
1748 1.192 riastrad static void
1749 1.192 riastrad usbd_xfer_cancel_timeout_async(struct usbd_xfer *xfer)
1750 1.192 riastrad {
1751 1.192 riastrad struct usbd_bus *bus __diagused = xfer->ux_bus;
1752 1.192 riastrad
1753 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1754 1.192 riastrad
1755 1.192 riastrad /*
1756 1.192 riastrad * If the timer wasn't running anyway, forget about it. This
1757 1.192 riastrad * can happen if we are completing an isochronous transfer
1758 1.192 riastrad * which doesn't use the same timeout logic.
1759 1.192 riastrad */
1760 1.192 riastrad if (!xfer->ux_timeout_set)
1761 1.192 riastrad return;
1762 1.192 riastrad
1763 1.192 riastrad xfer->ux_timeout_reset = false;
1764 1.192 riastrad if (!callout_stop(&xfer->ux_callout)) {
1765 1.192 riastrad /*
1766 1.192 riastrad * We stopped the callout before it ran. The timeout
1767 1.192 riastrad * is no longer set.
1768 1.192 riastrad */
1769 1.192 riastrad xfer->ux_timeout_set = false;
1770 1.192 riastrad } else if (callout_invoking(&xfer->ux_callout)) {
1771 1.192 riastrad /*
1772 1.192 riastrad * The callout has begun to run but it has not yet
1773 1.192 riastrad * acquired the lock and called callout_ack. The task
1774 1.192 riastrad * cannot be queued yet, and the callout cannot have
1775 1.192 riastrad * been rescheduled yet.
1776 1.192 riastrad *
1777 1.192 riastrad * By the time the callout acquires the lock, we will
1778 1.192 riastrad * have transitioned from USBD_IN_PROGRESS to a
1779 1.192 riastrad * completed status, and possibly also resubmitted the
1780 1.192 riastrad * xfer and set xfer->ux_timeout_reset = true. In both
1781 1.192 riastrad * cases, the callout will DTRT, so no further action
1782 1.192 riastrad * is needed here.
1783 1.192 riastrad */
1784 1.192 riastrad } else if (usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask)) {
1785 1.192 riastrad /*
1786 1.192 riastrad * The callout had fired and scheduled the task, but we
1787 1.192 riastrad * stopped the task before it could run. The timeout
1788 1.192 riastrad * is therefore no longer set -- the next resubmission
1789 1.192 riastrad * of the xfer must schedule a new timeout.
1790 1.192 riastrad *
1791 1.192 riastrad * The callout should not be be pending at this point:
1792 1.192 riastrad * it is scheduled only under the lock, and only when
1793 1.192 riastrad * xfer->ux_timeout_set is false, or by the callout or
1794 1.192 riastrad * task itself when xfer->ux_timeout_reset is true.
1795 1.192 riastrad */
1796 1.192 riastrad xfer->ux_timeout_set = false;
1797 1.192 riastrad }
1798 1.192 riastrad
1799 1.192 riastrad /*
1800 1.192 riastrad * The callout cannot be scheduled and the task cannot be
1801 1.192 riastrad * queued at this point. Either we cancelled them, or they are
1802 1.192 riastrad * already running and waiting for the bus lock.
1803 1.192 riastrad */
1804 1.192 riastrad KASSERT(!callout_pending(&xfer->ux_callout));
1805 1.192 riastrad KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1806 1.192 riastrad
1807 1.192 riastrad KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1808 1.192 riastrad }
1809