xref: /dragonfly/sys/bus/u4b/usb_process.c (revision c69bf40f)
1 /* $FreeBSD: head/sys/dev/usb/usb_process.c 267992 2014-06-28 03:56:17Z hselasky $ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #define	USB_DEBUG_VAR usb_proc_debug
28 
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>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/lock.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>
44 #include <sys/priv.h>
45 
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>
52 
53 #include <sys/proc.h>
54 #include <sys/kthread.h>
55 #include <sys/sched.h>
56 
57 static struct proc *usbproc;
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()
64 
65 #ifdef USB_DEBUG
66 static int usb_proc_debug;
67 
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,
70     "Debug level");
71 
72 TUNABLE_INT("hw.usb.proc.debug", &usb_proc_debug);
73 #endif
74 
75 /*------------------------------------------------------------------------*
76  *	usb_process
77  *
78  * This function is the USB process dispatcher.
79  *------------------------------------------------------------------------*/
80 static void
81 usb_process(void *arg)
82 {
83 	struct usb_process *up = arg;
84 	struct usb_proc_msg *pm;
85 	struct thread *td;
86 
87 #if 0 /* XXX Suspend here? */
88 	/* in case of attach error, check for suspended */
89 	USB_THREAD_SUSPEND_CHECK();
90 #endif
91 
92 	/* adjust priority */
93 	td = curthread;
94 	lwkt_setpri(td, up->up_prio);
95 	lockmgr(up->up_lock, LK_EXCLUSIVE);
96 
97 	up->up_curtd = td;
98 
99 	while (1) {
100 
101 		if (up->up_gone)
102 			break;
103 
104 		/*
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:
109 		 *
110 		 * 1) the command sleeps
111 		 *
112 		 * 2) the command returns
113 		 *
114 		 * Here is a practical example that shows how this helps
115 		 * solving a problem:
116 		 *
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.
123 		 *
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
128 		 * of data.
129 		 *
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.
133 		 *
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.
138 		 *
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
145 		 * desired result.
146 		 */
147 		pm = TAILQ_FIRST(&up->up_qhead);
148 
149 		if (pm) {
150 			DPRINTF("Message pm=%p, cb=%p (enter)\n",
151 			    pm, pm->pm_callback);
152 
153 			(pm->pm_callback) (pm);
154 
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;
159 			}
160 			DPRINTF("Message pm=%p (leave)\n", pm);
161 
162 			continue;
163 		}
164 		/* end if messages - check if anyone is waiting for sync */
165 		if (up->up_dsleep) {
166 			up->up_dsleep = 0;
167 			cv_broadcast(&up->up_drain);
168 		}
169 		up->up_msleep = 1;
170 		cv_wait(&up->up_cv, up->up_lock);
171 	}
172 
173 	up->up_ptr = NULL;
174 	cv_signal(&up->up_cv);
175 	lockmgr(up->up_lock, LK_RELEASE);
176 
177 	/* Clear the proc pointer if this is the last thread. */
178 	if (--usb_pcount == 0)
179 		usbproc = NULL;
180 
181 	USB_THREAD_EXIT(0);
182 }
183 
184 /*------------------------------------------------------------------------*
185  *	usb_proc_create
186  *
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.
192  *
193  * Return values:
194  *    0: success
195  * Else: failure
196  *------------------------------------------------------------------------*/
197 int
198 usb_proc_create(struct usb_process *up, struct lock *p_lock,
199     const char *pmesg, uint8_t prio)
200 {
201 	up->up_lock = p_lock;
202 	up->up_prio = prio;
203 
204 	TAILQ_INIT(&up->up_qhead);
205 
206 	cv_init(&up->up_cv, "-");
207 	cv_init(&up->up_drain, "usbdrain");
208 
209 	if (USB_THREAD_CREATE(&usb_process, up,
210 	    &up->up_ptr, "%s", pmesg)) {
211 		DPRINTFN(0, "Unable to create USB process.");
212 		up->up_ptr = NULL;
213 		goto error;
214 	}
215 	usb_pcount++;
216 	return (0);
217 
218 error:
219 	usb_proc_free(up);
220 	return (ENOMEM);
221 }
222 
223 /*------------------------------------------------------------------------*
224  *	usb_proc_free
225  *
226  * NOTE: If the structure pointed to by "up" is all zero, this
227  * function does nothing.
228  *
229  * NOTE: Messages that are pending on the process queue will not be
230  * removed nor called.
231  *------------------------------------------------------------------------*/
232 void
233 usb_proc_free(struct usb_process *up)
234 {
235 	/* check if not initialised */
236 	if (up->up_lock == NULL)
237 		return;
238 
239 	usb_proc_drain(up);
240 
241 	cv_destroy(&up->up_cv);
242 	cv_destroy(&up->up_drain);
243 
244 	/* make sure that we do not enter here again */
245 	up->up_lock = NULL;
246 }
247 
248 /*------------------------------------------------------------------------*
249  *	usb_proc_msignal
250  *
251  * This function will queue one of the passed USB process messages on
252  * the USB process queue. The first message that is not already queued
253  * will get queued. If both messages are already queued the one queued
254  * last will be removed from the queue and queued in the end. The USB
255  * process mutex must be locked when calling this function. This
256  * function exploits the fact that a process can only do one callback
257  * at a time. The message that was queued is returned.
258  *------------------------------------------------------------------------*/
259 void   *
260 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
261 {
262 	struct usb_proc_msg *pm0 = _pm0;
263 	struct usb_proc_msg *pm1 = _pm1;
264 	struct usb_proc_msg *pm2;
265 	usb_size_t d;
266 	uint8_t t;
267 
268 	/* check if gone, return dummy value */
269 	if (up->up_gone)
270 		return (_pm0);
271 
272 	KKASSERT(lockowned(up->up_lock));
273 
274 	t = 0;
275 
276 	if (pm0->pm_qentry.tqe_prev) {
277 		t |= 1;
278 	}
279 	if (pm1->pm_qentry.tqe_prev) {
280 		t |= 2;
281 	}
282 	if (t == 0) {
283 		/*
284 		 * No entries are queued. Queue "pm0" and use the existing
285 		 * message number.
286 		 */
287 		pm2 = pm0;
288 	} else if (t == 1) {
289 		/* Check if we need to increment the message number. */
290 		if (pm0->pm_num == up->up_msg_num) {
291 			up->up_msg_num++;
292 		}
293 		pm2 = pm1;
294 	} else if (t == 2) {
295 		/* Check if we need to increment the message number. */
296 		if (pm1->pm_num == up->up_msg_num) {
297 			up->up_msg_num++;
298 		}
299 		pm2 = pm0;
300 	} else if (t == 3) {
301 		/*
302 		 * Both entries are queued. Re-queue the entry closest to
303 		 * the end.
304 		 */
305 		d = (pm1->pm_num - pm0->pm_num);
306 
307 		/* Check sign after subtraction */
308 		if (d & 0x80000000) {
309 			pm2 = pm0;
310 		} else {
311 			pm2 = pm1;
312 		}
313 
314 		TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
315 	} else {
316 		pm2 = NULL;		/* panic - should not happen */
317 	}
318 
319 	DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
320 
321 	/* Put message last on queue */
322 
323 	pm2->pm_num = up->up_msg_num;
324 	TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
325 
326 	/* Check if we need to wakeup the USB process. */
327 
328 	if (up->up_msleep) {
329 		up->up_msleep = 0;	/* save "cv_signal()" calls */
330 		cv_signal(&up->up_cv);
331 	}
332 	return (pm2);
333 }
334 
335 /*------------------------------------------------------------------------*
336  *	usb_proc_is_gone
337  *
338  * Return values:
339  *    0: USB process is running
340  * Else: USB process is tearing down
341  *------------------------------------------------------------------------*/
342 uint8_t
343 usb_proc_is_gone(struct usb_process *up)
344 {
345 	if (up->up_gone)
346 		return (1);
347 
348 	/*
349 	 * Allow calls when up_mtx is NULL, before the USB process
350 	 * structure is initialised.
351 	 */
352 	if (up->up_lock != NULL)
353 		KKASSERT(lockowned(up->up_lock));
354 	return (0);
355 }
356 
357 /*------------------------------------------------------------------------*
358  *	usb_proc_mwait
359  *
360  * This function will return when the USB process message pointed to
361  * by "pm" is no longer on a queue. This function must be called
362  * having "up->up_mtx" locked.
363  *------------------------------------------------------------------------*/
364 void
365 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
366 {
367 	struct usb_proc_msg *pm0 = _pm0;
368 	struct usb_proc_msg *pm1 = _pm1;
369 
370 	/* check if gone */
371 	if (up->up_gone)
372 		return;
373 
374 	KKASSERT(lockowned(up->up_lock));
375 
376 	if (up->up_curtd == curthread) {
377 		/* Just remove the messages from the queue. */
378 		if (pm0->pm_qentry.tqe_prev) {
379 			TAILQ_REMOVE(&up->up_qhead, pm0, pm_qentry);
380 			pm0->pm_qentry.tqe_prev = NULL;
381 		}
382 		if (pm1->pm_qentry.tqe_prev) {
383 			TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
384 			pm1->pm_qentry.tqe_prev = NULL;
385 		}
386 	} else
387 		while (pm0->pm_qentry.tqe_prev ||
388 		    pm1->pm_qentry.tqe_prev) {
389 			/* check if config thread is gone */
390 			if (up->up_gone)
391 				break;
392 			up->up_dsleep = 1;
393 			cv_wait(&up->up_drain, up->up_lock);
394 		}
395 }
396 
397 /*------------------------------------------------------------------------*
398  *	usb_proc_drain
399  *
400  * This function will tear down an USB process, waiting for the
401  * currently executing command to return.
402  *
403  * NOTE: If the structure pointed to by "up" is all zero,
404  * this function does nothing.
405  *------------------------------------------------------------------------*/
406 void
407 usb_proc_drain(struct usb_process *up)
408 {
409 	/* check if not initialised */
410 	if (up->up_lock == NULL)
411 		return;
412 #if 0 /* XXX */
413 	/* handle special case with Giant */
414 	if (up->up_mtx != &Giant)
415 		mtx_assert(up->up_mtx, MA_NOTOWNED);
416 #else
417 	KKASSERT(!lockowned(up->up_lock));
418 	lockmgr(up->up_lock, LK_EXCLUSIVE);
419 #endif
420 
421 	/* Set the gone flag */
422 
423 	up->up_gone = 1;
424 
425 	while (up->up_ptr) {
426 
427 		/* Check if we need to wakeup the USB process */
428 
429 		if (up->up_msleep || up->up_csleep) {
430 			up->up_msleep = 0;
431 			up->up_csleep = 0;
432 			cv_signal(&up->up_cv);
433 		}
434 		/* Check if we are still cold booted */
435 
436 		if (cold) {
437 			USB_THREAD_SUSPEND(up->up_ptr);
438 			kprintf("WARNING: A USB process has "
439 			    "been left suspended\n");
440 			break;
441 		}
442 		cv_wait(&up->up_cv, up->up_lock);
443 	}
444 	/* Check if someone is waiting - should not happen */
445 
446 	if (up->up_dsleep) {
447 		up->up_dsleep = 0;
448 		cv_broadcast(&up->up_drain);
449 		DPRINTF("WARNING: Someone is waiting "
450 		    "for USB process drain!\n");
451 	}
452 	lockmgr(up->up_lock, LK_RELEASE);
453 }
454 
455 /*------------------------------------------------------------------------*
456  *	usb_proc_rewakeup
457  *
458  * This function is called to re-wakeup the given USB
459  * process. This usually happens after that the USB system has been in
460  * polling mode, like during a panic. This function must be called
461  * having "up->up_lock" locked.
462  *------------------------------------------------------------------------*/
463 void
464 usb_proc_rewakeup(struct usb_process *up)
465 {
466 	/* check if not initialised */
467 	if (up->up_lock == NULL)
468 		return;
469 	/* check if gone */
470 	if (up->up_gone)
471 		return;
472 
473 	KKASSERT(lockowned(up->up_lock));
474 
475 	if (up->up_msleep == 0) {
476 		/* re-wakeup */
477 		cv_signal(&up->up_cv);
478 	}
479 }
480 
481 /*------------------------------------------------------------------------*
482  *	usb_proc_is_called_from
483  *
484  * This function will return non-zero if called from inside the USB
485  * process passed as first argument. Else this function returns zero.
486  *------------------------------------------------------------------------*/
487 int
488 usb_proc_is_called_from(struct usb_process *up)
489 {
490 	return (up->up_curtd == curthread);
491 }
492