3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #define USB_DEBUG_VAR usb2_proc_debug
29 #include <dev/usb/usb_core.h>
30 #include <dev/usb/usb_process.h>
31 #include <dev/usb/usb_debug.h>
32 #include <dev/usb/usb_util.h>
35 #include <sys/kthread.h>
36 #include <sys/sched.h>
38 #if (__FreeBSD_version < 700000)
39 #define thread_lock(td) mtx_lock_spin(&sched_lock)
40 #define thread_unlock(td) mtx_unlock_spin(&sched_lock)
43 #if (__FreeBSD_version >= 800000)
44 #define USB_THREAD_CREATE(f, s, p, ...) \
45 kproc_create((f), (s), (p), RFHIGHPID, 0, __VA_ARGS__)
46 #define USB_THREAD_SUSPEND(p) kproc_suspend(p,0)
47 #define USB_THREAD_EXIT(err) kproc_exit(err)
49 #define USB_THREAD_CREATE(f, s, p, ...) \
50 kthread_create((f), (s), (p), RFHIGHPID, 0, __VA_ARGS__)
51 #define USB_THREAD_SUSPEND(p) kthread_suspend(p,0)
52 #define USB_THREAD_EXIT(err) kthread_exit(err)
56 static int usb2_proc_debug;
58 SYSCTL_NODE(_hw_usb, OID_AUTO, proc, CTLFLAG_RW, 0, "USB process");
59 SYSCTL_INT(_hw_usb_proc, OID_AUTO, debug, CTLFLAG_RW, &usb2_proc_debug, 0,
63 /*------------------------------------------------------------------------*
66 * This function is the USB process dispatcher.
67 *------------------------------------------------------------------------*/
69 usb2_process(void *arg)
71 struct usb2_process *up = arg;
72 struct usb2_proc_msg *pm;
78 sched_prio(td, up->up_prio);
91 * NOTE to reimplementors: dequeueing a command from the
92 * "used" queue and executing it must be atomic, with regard
93 * to the "up_mtx" mutex. That means any attempt to queue a
94 * command by another thread must be blocked until either:
96 * 1) the command sleeps
98 * 2) the command returns
100 * Here is a practical example that shows how this helps
103 * Assume that you want to set the baud rate on a USB serial
104 * device. During the programming of the device you don't
105 * want to receive nor transmit any data, because it will be
106 * garbage most likely anyway. The programming of our USB
107 * device takes 20 milliseconds and it needs to call
108 * functions that sleep.
110 * Non-working solution: Before we queue the programming
111 * command, we stop transmission and reception of data. Then
112 * we queue a programming command. At the end of the
113 * programming command we enable transmission and reception
116 * Problem: If a second programming command is queued while the
117 * first one is sleeping, we end up enabling transmission
118 * and reception of data too early.
120 * Working solution: Before we queue the programming command,
121 * we stop transmission and reception of data. Then we queue
122 * a programming command. Then we queue a second command
123 * that only enables transmission and reception of data.
125 * Why it works: If a second programming command is queued
126 * while the first one is sleeping, then the queueing of a
127 * second command to enable the data transfers, will cause
128 * the previous one, which is still on the queue, to be
129 * removed from the queue, and re-inserted after the last
130 * baud rate programming command, which then gives the
133 pm = TAILQ_FIRST(&up->up_qhead);
136 DPRINTF("Message pm=%p, cb=%p (enter)\n",
137 pm, pm->pm_callback);
139 (pm->pm_callback) (pm);
141 if (pm == TAILQ_FIRST(&up->up_qhead)) {
142 /* nothing changed */
143 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
144 pm->pm_qentry.tqe_prev = NULL;
146 DPRINTF("Message pm=%p (leave)\n", pm);
150 /* end if messages - check if anyone is waiting for sync */
153 usb2_cv_broadcast(&up->up_drain);
156 usb2_cv_wait(&up->up_cv, up->up_mtx);
160 usb2_cv_signal(&up->up_cv);
161 mtx_unlock(up->up_mtx);
166 /*------------------------------------------------------------------------*
169 * This function will create a process using the given "prio" that can
170 * execute callbacks. The mutex pointed to by "p_mtx" will be applied
171 * before calling the callbacks and released after that the callback
172 * has returned. The structure pointed to by "up" is assumed to be
173 * zeroed before this function is called.
178 *------------------------------------------------------------------------*/
180 usb2_proc_create(struct usb2_process *up, struct mtx *p_mtx,
181 const char *pmesg, uint8_t prio)
186 TAILQ_INIT(&up->up_qhead);
188 usb2_cv_init(&up->up_cv, "wmsg");
189 usb2_cv_init(&up->up_drain, "dmsg");
191 if (USB_THREAD_CREATE(&usb2_process, up,
192 &up->up_ptr, pmesg)) {
193 DPRINTFN(0, "Unable to create USB process.");
204 /*------------------------------------------------------------------------*
207 * NOTE: If the structure pointed to by "up" is all zero, this
208 * function does nothing.
210 * NOTE: Messages that are pending on the process queue will not be
211 * removed nor called.
212 *------------------------------------------------------------------------*/
214 usb2_proc_free(struct usb2_process *up)
216 /* check if not initialised */
217 if (up->up_mtx == NULL)
222 usb2_cv_destroy(&up->up_cv);
223 usb2_cv_destroy(&up->up_drain);
225 /* make sure that we do not enter here again */
229 /*------------------------------------------------------------------------*
232 * This function will queue one of the passed USB process messages on
233 * the USB process queue. The first message that is not already queued
234 * will get queued. If both messages are already queued the one queued
235 * last will be removed from the queue and queued in the end. The USB
236 * process mutex must be locked when calling this function. This
237 * function exploits the fact that a process can only do one callback
238 * at a time. The message that was queued is returned.
239 *------------------------------------------------------------------------*/
241 usb2_proc_msignal(struct usb2_process *up, void *_pm0, void *_pm1)
243 struct usb2_proc_msg *pm0 = _pm0;
244 struct usb2_proc_msg *pm1 = _pm1;
245 struct usb2_proc_msg *pm2;
249 /* check if gone, return dummy value */
253 mtx_assert(up->up_mtx, MA_OWNED);
257 if (pm0->pm_qentry.tqe_prev) {
260 if (pm1->pm_qentry.tqe_prev) {
265 * No entries are queued. Queue "pm0" and use the existing
270 /* Check if we need to increment the message number. */
271 if (pm0->pm_num == up->up_msg_num) {
276 /* Check if we need to increment the message number. */
277 if (pm1->pm_num == up->up_msg_num) {
283 * Both entries are queued. Re-queue the entry closest to
286 d = (pm1->pm_num - pm0->pm_num);
288 /* Check sign after subtraction */
289 if (d & 0x80000000) {
295 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
297 pm2 = NULL; /* panic - should not happen */
300 DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
302 /* Put message last on queue */
304 pm2->pm_num = up->up_msg_num;
305 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
307 /* Check if we need to wakeup the USB process. */
310 up->up_msleep = 0; /* save "cv_signal()" calls */
311 usb2_cv_signal(&up->up_cv);
316 /*------------------------------------------------------------------------*
320 * 0: USB process is running
321 * Else: USB process is tearing down
322 *------------------------------------------------------------------------*/
324 usb2_proc_is_gone(struct usb2_process *up)
329 mtx_assert(up->up_mtx, MA_OWNED);
333 /*------------------------------------------------------------------------*
336 * This function will return when the USB process message pointed to
337 * by "pm" is no longer on a queue. This function must be called
338 * having "up->up_mtx" locked.
339 *------------------------------------------------------------------------*/
341 usb2_proc_mwait(struct usb2_process *up, void *_pm0, void *_pm1)
343 struct usb2_proc_msg *pm0 = _pm0;
344 struct usb2_proc_msg *pm1 = _pm1;
350 mtx_assert(up->up_mtx, MA_OWNED);
352 if (up->up_curtd == curthread) {
353 /* Just remove the messages from the queue. */
354 if (pm0->pm_qentry.tqe_prev) {
355 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
356 pm0->pm_qentry.tqe_prev = NULL;
358 if (pm1->pm_qentry.tqe_prev) {
359 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
360 pm1->pm_qentry.tqe_prev = NULL;
363 while (pm0->pm_qentry.tqe_prev ||
364 pm1->pm_qentry.tqe_prev) {
365 /* check if config thread is gone */
369 usb2_cv_wait(&up->up_drain, up->up_mtx);
373 /*------------------------------------------------------------------------*
376 * This function will tear down an USB process, waiting for the
377 * currently executing command to return.
379 * NOTE: If the structure pointed to by "up" is all zero,
380 * this function does nothing.
381 *------------------------------------------------------------------------*/
383 usb2_proc_drain(struct usb2_process *up)
385 /* check if not initialised */
386 if (up->up_mtx == NULL)
388 /* handle special case with Giant */
389 if (up->up_mtx != &Giant)
390 mtx_assert(up->up_mtx, MA_NOTOWNED);
392 mtx_lock(up->up_mtx);
394 /* Set the gone flag */
400 /* Check if we need to wakeup the USB process */
402 if (up->up_msleep || up->up_csleep) {
405 usb2_cv_signal(&up->up_cv);
407 /* Check if we are still cold booted */
410 USB_THREAD_SUSPEND(up->up_ptr);
411 printf("WARNING: A USB process has "
412 "been left suspended!\n");
415 usb2_cv_wait(&up->up_cv, up->up_mtx);
417 /* Check if someone is waiting - should not happen */
421 usb2_cv_broadcast(&up->up_drain);
422 DPRINTF("WARNING: Someone is waiting "
423 "for USB process drain!\n");
425 mtx_unlock(up->up_mtx);