FreeBSD kernel usb device Code
usb_process.c
Go to the documentation of this file.
1/* $FreeBSD$ */
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifdef USB_GLOBAL_INCLUDE_FILE
30#include USB_GLOBAL_INCLUDE_FILE
31#else
32#include <sys/stdint.h>
33#include <sys/stddef.h>
34#include <sys/param.h>
35#include <sys/queue.h>
36#include <sys/types.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40#include <sys/module.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/condvar.h>
44#include <sys/sysctl.h>
45#include <sys/sx.h>
46#include <sys/unistd.h>
47#include <sys/callout.h>
48#include <sys/malloc.h>
49#include <sys/priv.h>
50
51#include <dev/usb/usb.h>
52#include <dev/usb/usbdi.h>
53#include <dev/usb/usbdi_util.h>
54#include <dev/usb/usb_process.h>
55
56#define USB_DEBUG_VAR usb_proc_debug
57#include <dev/usb/usb_debug.h>
58#include <dev/usb/usb_util.h>
59
60#include <sys/proc.h>
61#include <sys/kthread.h>
62#include <sys/sched.h>
63#endif /* USB_GLOBAL_INCLUDE_FILE */
64
65#if (__FreeBSD_version < 700000)
66#define thread_lock(td) mtx_lock_spin(&sched_lock)
67#define thread_unlock(td) mtx_unlock_spin(&sched_lock)
68#endif
69
70#if (__FreeBSD_version >= 800000)
71static struct proc *usbproc;
72static int usb_pcount;
73#define USB_THREAD_CREATE(f, s, p, ...) \
74 kproc_kthread_add((f), (s), &usbproc, (p), RFHIGHPID, \
75 0, "usb", __VA_ARGS__)
76#if (__FreeBSD_version >= 900000)
77#define USB_THREAD_SUSPEND_CHECK() kthread_suspend_check()
78#else
79#define USB_THREAD_SUSPEND_CHECK() kthread_suspend_check(curthread)
80#endif
81#define USB_THREAD_SUSPEND(p) kthread_suspend(p,0)
82#define USB_THREAD_EXIT(err) kthread_exit()
83#else
84#define USB_THREAD_CREATE(f, s, p, ...) \
85 kthread_create((f), (s), (p), RFHIGHPID, 0, __VA_ARGS__)
86#define USB_THREAD_SUSPEND_CHECK() kthread_suspend_check(curproc)
87#define USB_THREAD_SUSPEND(p) kthread_suspend(p,0)
88#define USB_THREAD_EXIT(err) kthread_exit(err)
89#endif
90
91#ifdef USB_DEBUG
92static int usb_proc_debug;
93
94static SYSCTL_NODE(_hw_usb, OID_AUTO, proc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 "USB process");
96SYSCTL_INT(_hw_usb_proc, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_proc_debug, 0,
97 "Debug level");
98#endif
99
100/*------------------------------------------------------------------------*
101 * usb_process
102 *
103 * This function is the USB process dispatcher.
104 *------------------------------------------------------------------------*/
105static void
106usb_process(void *arg)
107{
108 struct usb_process *up = arg;
109 struct usb_proc_msg *pm;
110 struct thread *td;
111
112 /* in case of attach error, check for suspended */
114
115 /* adjust priority */
116 td = curthread;
117 thread_lock(td);
118 sched_prio(td, up->up_prio);
119 thread_unlock(td);
120
121 USB_MTX_LOCK(up->up_mtx);
122
123 up->up_curtd = td;
124
125 while (1) {
126 if (up->up_gone)
127 break;
128
129 /*
130 * NOTE to reimplementors: dequeueing a command from the
131 * "used" queue and executing it must be atomic, with regard
132 * to the "up_mtx" mutex. That means any attempt to queue a
133 * command by another thread must be blocked until either:
134 *
135 * 1) the command sleeps
136 *
137 * 2) the command returns
138 *
139 * Here is a practical example that shows how this helps
140 * solving a problem:
141 *
142 * Assume that you want to set the baud rate on a USB serial
143 * device. During the programming of the device you don't
144 * want to receive nor transmit any data, because it will be
145 * garbage most likely anyway. The programming of our USB
146 * device takes 20 milliseconds and it needs to call
147 * functions that sleep.
148 *
149 * Non-working solution: Before we queue the programming
150 * command, we stop transmission and reception of data. Then
151 * we queue a programming command. At the end of the
152 * programming command we enable transmission and reception
153 * of data.
154 *
155 * Problem: If a second programming command is queued while the
156 * first one is sleeping, we end up enabling transmission
157 * and reception of data too early.
158 *
159 * Working solution: Before we queue the programming command,
160 * we stop transmission and reception of data. Then we queue
161 * a programming command. Then we queue a second command
162 * that only enables transmission and reception of data.
163 *
164 * Why it works: If a second programming command is queued
165 * while the first one is sleeping, then the queueing of a
166 * second command to enable the data transfers, will cause
167 * the previous one, which is still on the queue, to be
168 * removed from the queue, and re-inserted after the last
169 * baud rate programming command, which then gives the
170 * desired result.
171 */
172 pm = TAILQ_FIRST(&up->up_qhead);
173
174 if (pm) {
175 DPRINTF("Message pm=%p, cb=%p (enter)\n",
176 pm, pm->pm_callback);
177
178 (pm->pm_callback) (pm);
179
180 if (pm == TAILQ_FIRST(&up->up_qhead)) {
181 /* nothing changed */
182 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
183 pm->pm_qentry.tqe_prev = NULL;
184 }
185 DPRINTF("Message pm=%p (leave)\n", pm);
186
187 continue;
188 }
189 /* end of messages - check if anyone is waiting for sync */
190 if (up->up_dsleep) {
191 up->up_dsleep = 0;
192 cv_broadcast(&up->up_drain);
193 }
194 up->up_msleep = 1;
195 cv_wait(&up->up_cv, up->up_mtx);
196 }
197
198 up->up_ptr = NULL;
199 cv_signal(&up->up_cv);
201#if (__FreeBSD_version >= 800000)
202 /* Clear the proc pointer if this is the last thread. */
203 if (--usb_pcount == 0)
204 usbproc = NULL;
205#endif
206
208}
209
210/*------------------------------------------------------------------------*
211 * usb_proc_create
212 *
213 * This function will create a process using the given "prio" that can
214 * execute callbacks. The mutex pointed to by "p_mtx" will be applied
215 * before calling the callbacks and released after that the callback
216 * has returned. The structure pointed to by "up" is assumed to be
217 * zeroed before this function is called.
218 *
219 * Return values:
220 * 0: success
221 * Else: failure
222 *------------------------------------------------------------------------*/
223int
224usb_proc_create(struct usb_process *up, struct mtx *p_mtx,
225 const char *pmesg, uint8_t prio)
226{
227 up->up_mtx = p_mtx;
228 up->up_prio = prio;
229
230 TAILQ_INIT(&up->up_qhead);
231
232 cv_init(&up->up_cv, "-");
233 cv_init(&up->up_drain, "usbdrain");
234
236 &up->up_ptr, "%s", pmesg)) {
237 DPRINTFN(0, "Unable to create USB process.");
238 up->up_ptr = NULL;
239 goto error;
240 }
241#if (__FreeBSD_version >= 800000)
242 usb_pcount++;
243#endif
244 return (0);
245
246error:
247 usb_proc_free(up);
248 return (ENOMEM);
249}
250
251/*------------------------------------------------------------------------*
252 * usb_proc_free
253 *
254 * NOTE: If the structure pointed to by "up" is all zero, this
255 * function does nothing.
256 *
257 * NOTE: Messages that are pending on the process queue will not be
258 * removed nor called.
259 *------------------------------------------------------------------------*/
260void
262{
263 /* check if not initialised */
264 if (up->up_mtx == NULL)
265 return;
266
267 usb_proc_drain(up);
268
269 cv_destroy(&up->up_cv);
270 cv_destroy(&up->up_drain);
271
272 /* make sure that we do not enter here again */
273 up->up_mtx = NULL;
274}
275
276/*------------------------------------------------------------------------*
277 * usb_proc_msignal
278 *
279 * This function will queue one of the passed USB process messages on
280 * the USB process queue. The first message that is not already queued
281 * will get queued. If both messages are already queued the one queued
282 * last will be removed from the queue and queued in the end. The USB
283 * process mutex must be locked when calling this function. This
284 * function exploits the fact that a process can only do one callback
285 * at a time. The message that was queued is returned.
286 *------------------------------------------------------------------------*/
287void *
288usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
289{
290 struct usb_proc_msg *pm0 = _pm0;
291 struct usb_proc_msg *pm1 = _pm1;
292 struct usb_proc_msg *pm2;
293 usb_size_t d;
294 uint8_t t;
295
296 /* check if gone or in polling mode, return dummy value */
297 if (up->up_gone != 0 ||
299 return (_pm0);
300
301 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
302
303 t = 0;
304
305 if (pm0->pm_qentry.tqe_prev) {
306 t |= 1;
307 }
308 if (pm1->pm_qentry.tqe_prev) {
309 t |= 2;
310 }
311 if (t == 0) {
312 /*
313 * No entries are queued. Queue "pm0" and use the existing
314 * message number.
315 */
316 pm2 = pm0;
317 } else if (t == 1) {
318 /* Check if we need to increment the message number. */
319 if (pm0->pm_num == up->up_msg_num) {
320 up->up_msg_num++;
321 }
322 pm2 = pm1;
323 } else if (t == 2) {
324 /* Check if we need to increment the message number. */
325 if (pm1->pm_num == up->up_msg_num) {
326 up->up_msg_num++;
327 }
328 pm2 = pm0;
329 } else if (t == 3) {
330 /*
331 * Both entries are queued. Re-queue the entry closest to
332 * the end.
333 */
334 d = (pm1->pm_num - pm0->pm_num);
335
336 /* Check sign after subtraction */
337 if (d & 0x80000000) {
338 pm2 = pm0;
339 } else {
340 pm2 = pm1;
341 }
342
343 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
344 } else {
345 pm2 = NULL; /* panic - should not happen */
346 }
347
348 DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
349
350 /* Put message last on queue */
351
352 pm2->pm_num = up->up_msg_num;
353 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
354
355 /* Check if we need to wakeup the USB process. */
356
357 if (up->up_msleep) {
358 up->up_msleep = 0; /* save "cv_signal()" calls */
359 cv_signal(&up->up_cv);
360 }
361 return (pm2);
362}
363
364/*------------------------------------------------------------------------*
365 * usb_proc_is_gone
366 *
367 * Return values:
368 * 0: USB process is running
369 * Else: USB process is tearing down
370 *------------------------------------------------------------------------*/
371uint8_t
373{
374 if (up->up_gone)
375 return (1);
376
377 /*
378 * Allow calls when up_mtx is NULL, before the USB process
379 * structure is initialised.
380 */
381 if (up->up_mtx != NULL)
382 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
383 return (0);
384}
385
386/*------------------------------------------------------------------------*
387 * usb_proc_mwait
388 *
389 * This function will return when the USB process message pointed to
390 * by "pm" is no longer on a queue. This function must be called
391 * having "up->up_mtx" locked.
392 *------------------------------------------------------------------------*/
393void
394usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
395{
396 struct usb_proc_msg *pm0 = _pm0;
397 struct usb_proc_msg *pm1 = _pm1;
398
399 /* check if gone */
400 if (up->up_gone)
401 return;
402
403 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
404
405 if (up->up_curtd == curthread) {
406 /* Just remove the messages from the queue. */
407 if (pm0->pm_qentry.tqe_prev) {
408 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
409 pm0->pm_qentry.tqe_prev = NULL;
410 }
411 if (pm1->pm_qentry.tqe_prev) {
412 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
413 pm1->pm_qentry.tqe_prev = NULL;
414 }
415 } else
416 while (pm0->pm_qentry.tqe_prev ||
417 pm1->pm_qentry.tqe_prev) {
418 /* check if config thread is gone */
419 if (up->up_gone)
420 break;
421 up->up_dsleep = 1;
422 cv_wait(&up->up_drain, up->up_mtx);
423 }
424}
425
426/*------------------------------------------------------------------------*
427 * usb_proc_drain
428 *
429 * This function will tear down an USB process, waiting for the
430 * currently executing command to return.
431 *
432 * NOTE: If the structure pointed to by "up" is all zero,
433 * this function does nothing.
434 *------------------------------------------------------------------------*/
435void
437{
438 /* check if not initialised */
439 if (up->up_mtx == NULL)
440 return;
441 /* handle special case with Giant */
442 if (up->up_mtx != &Giant)
443 USB_MTX_ASSERT(up->up_mtx, MA_NOTOWNED);
444
445 USB_MTX_LOCK(up->up_mtx);
446
447 /* Set the gone flag */
448
449 up->up_gone = 1;
450
451 while (up->up_ptr) {
452 /* Check if we need to wakeup the USB process */
453
454 if (up->up_msleep || up->up_csleep) {
455 up->up_msleep = 0;
456 up->up_csleep = 0;
457 cv_signal(&up->up_cv);
458 }
459#ifndef EARLY_AP_STARTUP
460 /* Check if we are still cold booted */
461 if (cold) {
463 printf("WARNING: A USB process has "
464 "been left suspended\n");
465 break;
466 }
467#endif
468 cv_wait(&up->up_cv, up->up_mtx);
469 }
470 /* Check if someone is waiting - should not happen */
471
472 if (up->up_dsleep) {
473 up->up_dsleep = 0;
474 cv_broadcast(&up->up_drain);
475 DPRINTF("WARNING: Someone is waiting "
476 "for USB process drain!\n");
477 }
479}
480
481/*------------------------------------------------------------------------*
482 * usb_proc_rewakeup
483 *
484 * This function is called to re-wakeup the given USB
485 * process. This usually happens after that the USB system has been in
486 * polling mode, like during a panic. This function must be called
487 * having "up->up_mtx" locked.
488 *------------------------------------------------------------------------*/
489void
491{
492 /* check if not initialised */
493 if (up->up_mtx == NULL)
494 return;
495 /* check if gone */
496 if (up->up_gone)
497 return;
498
499 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
500
501 if (up->up_msleep == 0) {
502 /* re-wakeup */
503 cv_signal(&up->up_cv);
504 }
505}
506
507/*------------------------------------------------------------------------*
508 * usb_proc_is_called_from
509 *
510 * This function will return non-zero if called from inside the USB
511 * process passed as first argument. Else this function returns zero.
512 *------------------------------------------------------------------------*/
513int
515{
516 return (up->up_curtd == curthread);
517}
static int debug
Definition: cfumass.c:73
static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "USB DWC OTG")
SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN, &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2/3 - ULPI/HSIC/INTERNAL/UTMI+")
struct @109 error
usb_size_t pm_num
Definition: usbdi.h:523
usb_proc_callback_t * pm_callback
Definition: usbdi.h:522
struct thread * up_curtd
Definition: usb_process.h:65
struct cv up_drain
Definition: usb_process.h:58
struct proc * up_ptr
Definition: usb_process.h:63
usb_size_t up_msg_num
Definition: usb_process.h:68
uint8_t up_prio
Definition: usb_process.h:70
struct mtx * up_mtx
Definition: usb_process.h:66
struct cv up_cv
Definition: usb_process.h:57
uint8_t up_gone
Definition: usb_process.h:71
uint8_t up_msleep
Definition: usb_process.h:72
uint8_t up_csleep
Definition: usb_process.h:73
uint8_t up_dsleep
Definition: usb_process.h:74
#define DPRINTF(...)
Definition: umass.c:179
#define USB_IN_POLLING_MODE_FUNC()
Definition: usb_freebsd.h:96
uint32_t usb_size_t
Definition: usb_freebsd.h:102
int usb_proc_create(struct usb_process *up, struct mtx *p_mtx, const char *pmesg, uint8_t prio)
Definition: usb_process.c:224
#define USB_THREAD_EXIT(err)
Definition: usb_process.c:88
uint8_t usb_proc_is_gone(struct usb_process *up)
Definition: usb_process.c:372
void usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
Definition: usb_process.c:394
#define thread_unlock(td)
Definition: usb_process.c:67
static void usb_process(void *arg)
Definition: usb_process.c:106
void usb_proc_drain(struct usb_process *up)
Definition: usb_process.c:436
int usb_proc_is_called_from(struct usb_process *up)
Definition: usb_process.c:514
#define USB_THREAD_CREATE(f, s, p,...)
Definition: usb_process.c:84
void * usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
Definition: usb_process.c:288
#define USB_THREAD_SUSPEND(p)
Definition: usb_process.c:87
void usb_proc_rewakeup(struct usb_process *up)
Definition: usb_process.c:490
void usb_proc_free(struct usb_process *up)
Definition: usb_process.c:261
#define thread_lock(td)
Definition: usb_process.c:66
#define USB_THREAD_SUSPEND_CHECK()
Definition: usb_process.c:86
#define USB_MTX_UNLOCK(_m)
Definition: usbdi.h:458
#define USB_MTX_ASSERT(_m, _t)
Definition: usbdi.h:450
#define USB_MTX_LOCK(_m)
Definition: usbdi.h:453