3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
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>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
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>
56 #define USB_DEBUG_VAR usb_proc_debug
57 #include <dev/usb/usb_debug.h>
58 #include <dev/usb/usb_util.h>
61 #include <sys/kthread.h>
62 #include <sys/sched.h>
63 #endif /* USB_GLOBAL_INCLUDE_FILE */
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)
70 #if (__FreeBSD_version >= 800000)
71 static struct proc *usbproc;
72 static 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()
79 #define USB_THREAD_SUSPEND_CHECK() kthread_suspend_check(curthread)
81 #define USB_THREAD_SUSPEND(p) kthread_suspend(p,0)
82 #define USB_THREAD_EXIT(err) kthread_exit()
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)
92 static int usb_proc_debug;
94 static SYSCTL_NODE(_hw_usb, OID_AUTO, proc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
96 SYSCTL_INT(_hw_usb_proc, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_proc_debug, 0,
100 /*------------------------------------------------------------------------*
103 * This function is the USB process dispatcher.
104 *------------------------------------------------------------------------*/
106 usb_process(void *arg)
108 struct usb_process *up = arg;
109 struct usb_proc_msg *pm;
112 /* in case of attach error, check for suspended */
113 USB_THREAD_SUSPEND_CHECK();
115 /* adjust priority */
118 sched_prio(td, up->up_prio);
121 USB_MTX_LOCK(up->up_mtx);
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:
135 * 1) the command sleeps
137 * 2) the command returns
139 * Here is a practical example that shows how this helps
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.
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
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.
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.
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
172 pm = TAILQ_FIRST(&up->up_qhead);
175 DPRINTF("Message pm=%p, cb=%p (enter)\n",
176 pm, pm->pm_callback);
178 (pm->pm_callback) (pm);
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;
185 DPRINTF("Message pm=%p (leave)\n", pm);
189 /* end of messages - check if anyone is waiting for sync */
192 cv_broadcast(&up->up_drain);
195 cv_wait(&up->up_cv, up->up_mtx);
199 cv_signal(&up->up_cv);
200 USB_MTX_UNLOCK(up->up_mtx);
201 #if (__FreeBSD_version >= 800000)
202 /* Clear the proc pointer if this is the last thread. */
203 if (--usb_pcount == 0)
210 /*------------------------------------------------------------------------*
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.
222 *------------------------------------------------------------------------*/
224 usb_proc_create(struct usb_process *up, struct mtx *p_mtx,
225 const char *pmesg, uint8_t prio)
230 TAILQ_INIT(&up->up_qhead);
232 cv_init(&up->up_cv, "-");
233 cv_init(&up->up_drain, "usbdrain");
235 if (USB_THREAD_CREATE(&usb_process, up,
236 &up->up_ptr, "%s", pmesg)) {
237 DPRINTFN(0, "Unable to create USB process.");
241 #if (__FreeBSD_version >= 800000)
251 /*------------------------------------------------------------------------*
254 * NOTE: If the structure pointed to by "up" is all zero, this
255 * function does nothing.
257 * NOTE: Messages that are pending on the process queue will not be
258 * removed nor called.
259 *------------------------------------------------------------------------*/
261 usb_proc_free(struct usb_process *up)
263 /* check if not initialised */
264 if (up->up_mtx == NULL)
269 cv_destroy(&up->up_cv);
270 cv_destroy(&up->up_drain);
272 /* make sure that we do not enter here again */
276 /*------------------------------------------------------------------------*
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 *------------------------------------------------------------------------*/
288 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
290 struct usb_proc_msg *pm0 = _pm0;
291 struct usb_proc_msg *pm1 = _pm1;
292 struct usb_proc_msg *pm2;
296 /* check if gone or in polling mode, return dummy value */
297 if (up->up_gone != 0 ||
298 USB_IN_POLLING_MODE_FUNC() != 0)
301 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
305 if (pm0->pm_qentry.tqe_prev) {
308 if (pm1->pm_qentry.tqe_prev) {
313 * No entries are queued. Queue "pm0" and use the existing
318 /* Check if we need to increment the message number. */
319 if (pm0->pm_num == up->up_msg_num) {
324 /* Check if we need to increment the message number. */
325 if (pm1->pm_num == up->up_msg_num) {
331 * Both entries are queued. Re-queue the entry closest to
334 d = (pm1->pm_num - pm0->pm_num);
336 /* Check sign after subtraction */
337 if (d & 0x80000000) {
343 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
345 pm2 = NULL; /* panic - should not happen */
348 DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
350 /* Put message last on queue */
352 pm2->pm_num = up->up_msg_num;
353 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
355 /* Check if we need to wakeup the USB process. */
358 up->up_msleep = 0; /* save "cv_signal()" calls */
359 cv_signal(&up->up_cv);
364 /*------------------------------------------------------------------------*
368 * 0: USB process is running
369 * Else: USB process is tearing down
370 *------------------------------------------------------------------------*/
372 usb_proc_is_gone(struct usb_process *up)
378 * Allow calls when up_mtx is NULL, before the USB process
379 * structure is initialised.
381 if (up->up_mtx != NULL)
382 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
386 /*------------------------------------------------------------------------*
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 *------------------------------------------------------------------------*/
394 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
396 struct usb_proc_msg *pm0 = _pm0;
397 struct usb_proc_msg *pm1 = _pm1;
403 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
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;
411 if (pm1->pm_qentry.tqe_prev) {
412 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
413 pm1->pm_qentry.tqe_prev = NULL;
416 while (pm0->pm_qentry.tqe_prev ||
417 pm1->pm_qentry.tqe_prev) {
418 /* check if config thread is gone */
422 cv_wait(&up->up_drain, up->up_mtx);
426 /*------------------------------------------------------------------------*
429 * This function will tear down an USB process, waiting for the
430 * currently executing command to return.
432 * NOTE: If the structure pointed to by "up" is all zero,
433 * this function does nothing.
434 *------------------------------------------------------------------------*/
436 usb_proc_drain(struct usb_process *up)
438 /* check if not initialised */
439 if (up->up_mtx == NULL)
441 /* handle special case with Giant */
442 if (up->up_mtx != &Giant)
443 USB_MTX_ASSERT(up->up_mtx, MA_NOTOWNED);
445 USB_MTX_LOCK(up->up_mtx);
447 /* Set the gone flag */
452 /* Check if we need to wakeup the USB process */
454 if (up->up_msleep || up->up_csleep) {
457 cv_signal(&up->up_cv);
459 #ifndef EARLY_AP_STARTUP
460 /* Check if we are still cold booted */
462 USB_THREAD_SUSPEND(up->up_ptr);
463 printf("WARNING: A USB process has "
464 "been left suspended\n");
468 cv_wait(&up->up_cv, up->up_mtx);
470 /* Check if someone is waiting - should not happen */
474 cv_broadcast(&up->up_drain);
475 DPRINTF("WARNING: Someone is waiting "
476 "for USB process drain!\n");
478 USB_MTX_UNLOCK(up->up_mtx);
481 /*------------------------------------------------------------------------*
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 *------------------------------------------------------------------------*/
490 usb_proc_rewakeup(struct usb_process *up)
492 /* check if not initialised */
493 if (up->up_mtx == NULL)
499 USB_MTX_ASSERT(up->up_mtx, MA_OWNED);
501 if (up->up_msleep == 0) {
503 cv_signal(&up->up_cv);
507 /*------------------------------------------------------------------------*
508 * usb_proc_is_called_from
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 *------------------------------------------------------------------------*/
514 usb_proc_is_called_from(struct usb_process *up)
516 return (up->up_curtd == curthread);