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 usb_proc_debug
29 #include <sys/stdint.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <bus/u4b/usb.h>
47 #include <bus/u4b/usbdi.h>
48 #include <bus/u4b/usbdi_util.h>
49 #include <bus/u4b/usb_process.h>
50 #include <bus/u4b/usb_debug.h>
51 #include <bus/u4b/usb_util.h>
54 #include <sys/kthread.h>
55 #include <sys/sched.h>
58 static int usb_pcount;
59 #define USB_THREAD_CREATE(f, s, p, ...) \
60 kthread_create((f), (s), (p), __VA_ARGS__)
61 #define USB_THREAD_SUSPEND_CHECK() kthread_suspend_check(curproc)
62 #define USB_THREAD_SUSPEND(p) suspend_kproc(p,0)
63 #define USB_THREAD_EXIT(err) kthread_exit()
66 static int usb_proc_debug;
68 static SYSCTL_NODE(_hw_usb, OID_AUTO, proc, CTLFLAG_RW, 0, "USB process");
69 SYSCTL_INT(_hw_usb_proc, OID_AUTO, debug, CTLFLAG_RW, &usb_proc_debug, 0,
72 TUNABLE_INT("hw.usb.proc.debug", &usb_proc_debug);
75 /*------------------------------------------------------------------------*
78 * This function is the USB process dispatcher.
79 *------------------------------------------------------------------------*/
81 usb_process(void *arg)
83 struct usb_process *up = arg;
84 struct usb_proc_msg *pm;
87 /* in case of attach error, check for suspended */
89 * USB_THREAD_SUSPEND_CHECK();
94 lwkt_setpri(td, up->up_prio);
95 lockmgr(up->up_lock, LK_EXCLUSIVE);
105 * NOTE to reimplementors: dequeueing a command from the
106 * "used" queue and executing it must be atomic, with regard
107 * to the "up_mtx" mutex. That means any attempt to queue a
108 * command by another thread must be blocked until either:
110 * 1) the command sleeps
112 * 2) the command returns
114 * Here is a practical example that shows how this helps
117 * Assume that you want to set the baud rate on a USB serial
118 * device. During the programming of the device you don't
119 * want to receive nor transmit any data, because it will be
120 * garbage most likely anyway. The programming of our USB
121 * device takes 20 milliseconds and it needs to call
122 * functions that sleep.
124 * Non-working solution: Before we queue the programming
125 * command, we stop transmission and reception of data. Then
126 * we queue a programming command. At the end of the
127 * programming command we enable transmission and reception
130 * Problem: If a second programming command is queued while the
131 * first one is sleeping, we end up enabling transmission
132 * and reception of data too early.
134 * Working solution: Before we queue the programming command,
135 * we stop transmission and reception of data. Then we queue
136 * a programming command. Then we queue a second command
137 * that only enables transmission and reception of data.
139 * Why it works: If a second programming command is queued
140 * while the first one is sleeping, then the queueing of a
141 * second command to enable the data transfers, will cause
142 * the previous one, which is still on the queue, to be
143 * removed from the queue, and re-inserted after the last
144 * baud rate programming command, which then gives the
147 pm = TAILQ_FIRST(&up->up_qhead);
150 DPRINTF("Message pm=%p, cb=%p (enter)\n",
151 pm, pm->pm_callback);
153 (pm->pm_callback) (pm);
155 if (pm == TAILQ_FIRST(&up->up_qhead)) {
156 /* nothing changed */
157 TAILQ_REMOVE(&up->up_qhead, pm, pm_qentry);
158 pm->pm_qentry.tqe_prev = NULL;
160 DPRINTF("Message pm=%p (leave)\n", pm);
164 /* end if messages - check if anyone is waiting for sync */
167 cv_broadcast(&up->up_drain);
170 cv_wait(&up->up_cv, up->up_lock);
174 cv_signal(&up->up_cv);
175 lockmgr(up->up_lock, LK_RELEASE);
176 /* Clear the proc pointer if this is the last thread. */
178 if (--usb_pcount == 0)
184 /*------------------------------------------------------------------------*
187 * This function will create a process using the given "prio" that can
188 * execute callbacks. The mutex pointed to by "p_mtx" will be applied
189 * before calling the callbacks and released after that the callback
190 * has returned. The structure pointed to by "up" is assumed to be
191 * zeroed before this function is called.
196 *------------------------------------------------------------------------*/
198 usb_proc_create(struct usb_process *up, struct lock *p_lock,
199 const char *pmesg, uint8_t prio)
201 kprintf("Creating usb_proc: %s\n", pmesg);
203 up->up_lock = p_lock;
206 TAILQ_INIT(&up->up_qhead);
208 cv_init(&up->up_cv, "-");
209 cv_init(&up->up_drain, "usbdrain");
211 if (USB_THREAD_CREATE(&usb_process, up,
212 &up->up_ptr, "%s", pmesg)) {
213 DPRINTFN(0, "Unable to create USB process.");
225 /*------------------------------------------------------------------------*
228 * NOTE: If the structure pointed to by "up" is all zero, this
229 * function does nothing.
231 * NOTE: Messages that are pending on the process queue will not be
232 * removed nor called.
233 *------------------------------------------------------------------------*/
235 usb_proc_free(struct usb_process *up)
237 /* check if not initialised */
238 if (up->up_lock == NULL)
243 cv_destroy(&up->up_cv);
244 cv_destroy(&up->up_drain);
246 /* make sure that we do not enter here again */
250 /*------------------------------------------------------------------------*
253 * This function will queue one of the passed USB process messages on
254 * the USB process queue. The first message that is not already queued
255 * will get queued. If both messages are already queued the one queued
256 * last will be removed from the queue and queued in the end. The USB
257 * process mutex must be locked when calling this function. This
258 * function exploits the fact that a process can only do one callback
259 * at a time. The message that was queued is returned.
260 *------------------------------------------------------------------------*/
262 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
264 struct usb_proc_msg *pm0 = _pm0;
265 struct usb_proc_msg *pm1 = _pm1;
266 struct usb_proc_msg *pm2;
270 /* check if gone, return dummy value */
274 KKASSERT(lockstatus(up->up_lock, curthread) != 0);
278 if (pm0->pm_qentry.tqe_prev) {
281 if (pm1->pm_qentry.tqe_prev) {
286 * No entries are queued. Queue "pm0" and use the existing
291 /* Check if we need to increment the message number. */
292 if (pm0->pm_num == up->up_msg_num) {
297 /* Check if we need to increment the message number. */
298 if (pm1->pm_num == up->up_msg_num) {
304 * Both entries are queued. Re-queue the entry closest to
307 d = (pm1->pm_num - pm0->pm_num);
309 /* Check sign after subtraction */
310 if (d & 0x80000000) {
316 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
318 pm2 = NULL; /* panic - should not happen */
321 DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
323 /* Put message last on queue */
325 pm2->pm_num = up->up_msg_num;
326 TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
328 /* Check if we need to wakeup the USB process. */
331 up->up_msleep = 0; /* save "cv_signal()" calls */
332 cv_signal(&up->up_cv);
337 /*------------------------------------------------------------------------*
341 * 0: USB process is running
342 * Else: USB process is tearing down
343 *------------------------------------------------------------------------*/
345 usb_proc_is_gone(struct usb_process *up)
351 * Allow calls when up_mtx is NULL, before the USB process
352 * structure is initialised.
354 if (up->up_lock != NULL)
355 KKASSERT(lockstatus(up->up_lock, curthread)!=0);
360 /*------------------------------------------------------------------------*
363 * This function will return when the USB process message pointed to
364 * by "pm" is no longer on a queue. This function must be called
365 * having "up->up_mtx" locked.
366 *------------------------------------------------------------------------*/
368 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
370 struct usb_proc_msg *pm0 = _pm0;
371 struct usb_proc_msg *pm1 = _pm1;
377 KKASSERT(lockstatus(up->up_lock, curthread) != 0);
379 if (up->up_curtd == curthread) {
380 /* Just remove the messages from the queue. */
381 if (pm0->pm_qentry.tqe_prev) {
382 TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
383 pm0->pm_qentry.tqe_prev = NULL;
385 if (pm1->pm_qentry.tqe_prev) {
386 TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
387 pm1->pm_qentry.tqe_prev = NULL;
390 while (pm0->pm_qentry.tqe_prev ||
391 pm1->pm_qentry.tqe_prev) {
392 /* check if config thread is gone */
396 cv_wait(&up->up_drain, up->up_lock);
400 /*------------------------------------------------------------------------*
403 * This function will tear down an USB process, waiting for the
404 * currently executing command to return.
406 * NOTE: If the structure pointed to by "up" is all zero,
407 * this function does nothing.
408 *------------------------------------------------------------------------*/
410 usb_proc_drain(struct usb_process *up)
412 /* check if not initialised */
413 if (up->up_lock == NULL)
415 /* handle special case with Giant */
417 if (up->up_mtx != &Giant)
418 mtx_assert(up->up_mtx, MA_NOTOWNED);
420 KKASSERT(lockstatus(up->up_lock, curthread) == 0);
421 lockmgr(up->up_lock, LK_EXCLUSIVE);
423 /* Set the gone flag */
429 /* Check if we need to wakeup the USB process */
431 if (up->up_msleep || up->up_csleep) {
434 cv_signal(&up->up_cv);
436 /* Check if we are still cold booted */
439 USB_THREAD_SUSPEND(up->up_ptr);
440 kprintf("WARNING: A USB process has "
441 "been left suspended\n");
444 cv_wait(&up->up_cv, up->up_lock);
446 /* Check if someone is waiting - should not happen */
450 cv_broadcast(&up->up_drain);
451 DPRINTF("WARNING: Someone is waiting "
452 "for USB process drain!\n");
454 lockmgr(up->up_lock, LK_RELEASE);
457 /*------------------------------------------------------------------------*
460 * This function is called to re-wakeup the given USB
461 * process. This usually happens after that the USB system has been in
462 * polling mode, like during a panic. This function must be called
463 * having "up->up_lock" locked.
464 *------------------------------------------------------------------------*/
466 usb_proc_rewakeup(struct usb_process *up)
468 /* check if not initialised */
469 if (up->up_lock == NULL)
474 KKASSERT(lockstatus(up->up_lock, curthread) != 0);
476 if (up->up_msleep == 0) {
478 cv_signal(&up->up_cv);