usb4bsd: Perform the usual porting on the controller, storage and core code.
[dragonfly.git] / sys / bus / u4b / usb_process.c
1 /* $FreeBSD$ */
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
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         /* in case of attach error, check for suspended */
88         /* XXX Suspend here?
89     *  USB_THREAD_SUSPEND_CHECK();
90     */
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         /* Clear the proc pointer if this is the last thread. */
177 /*
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     kprintf("Creating usb_proc: %s\n", pmesg);
202
203         up->up_lock = p_lock;
204         up->up_prio = prio;
205
206         TAILQ_INIT(&up->up_qhead);
207
208         cv_init(&up->up_cv, "-");
209         cv_init(&up->up_drain, "usbdrain");
210
211         if (USB_THREAD_CREATE(&usb_process, up,
212             &up->up_ptr, "%s", pmesg)) {
213                 DPRINTFN(0, "Unable to create USB process.");
214                 up->up_ptr = NULL;
215                 goto error;
216         }
217         usb_pcount++;
218         return (0);
219
220 error:
221         usb_proc_free(up);
222         return (ENOMEM);
223 }
224
225 /*------------------------------------------------------------------------*
226  *      usb_proc_free
227  *
228  * NOTE: If the structure pointed to by "up" is all zero, this
229  * function does nothing.
230  *
231  * NOTE: Messages that are pending on the process queue will not be
232  * removed nor called.
233  *------------------------------------------------------------------------*/
234 void
235 usb_proc_free(struct usb_process *up)
236 {
237         /* check if not initialised */
238         if (up->up_lock == NULL)
239                 return;
240
241         usb_proc_drain(up);
242
243         cv_destroy(&up->up_cv);
244         cv_destroy(&up->up_drain);
245
246         /* make sure that we do not enter here again */
247         up->up_lock = NULL;
248 }
249
250 /*------------------------------------------------------------------------*
251  *      usb_proc_msignal
252  *
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  *------------------------------------------------------------------------*/
261 void   *
262 usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
263 {
264         struct usb_proc_msg *pm0 = _pm0;
265         struct usb_proc_msg *pm1 = _pm1;
266         struct usb_proc_msg *pm2;
267         usb_size_t d;
268         uint8_t t;
269
270         /* check if gone, return dummy value */
271         if (up->up_gone)
272                 return (_pm0);
273
274     KKASSERT(lockstatus(up->up_lock, curthread) != 0);
275
276         t = 0;
277
278         if (pm0->pm_qentry.tqe_prev) {
279                 t |= 1;
280         }
281         if (pm1->pm_qentry.tqe_prev) {
282                 t |= 2;
283         }
284         if (t == 0) {
285                 /*
286                  * No entries are queued. Queue "pm0" and use the existing
287                  * message number.
288                  */
289                 pm2 = pm0;
290         } else if (t == 1) {
291                 /* Check if we need to increment the message number. */
292                 if (pm0->pm_num == up->up_msg_num) {
293                         up->up_msg_num++;
294                 }
295                 pm2 = pm1;
296         } else if (t == 2) {
297                 /* Check if we need to increment the message number. */
298                 if (pm1->pm_num == up->up_msg_num) {
299                         up->up_msg_num++;
300                 }
301                 pm2 = pm0;
302         } else if (t == 3) {
303                 /*
304                  * Both entries are queued. Re-queue the entry closest to
305                  * the end.
306                  */
307                 d = (pm1->pm_num - pm0->pm_num);
308
309                 /* Check sign after subtraction */
310                 if (d & 0x80000000) {
311                         pm2 = pm0;
312                 } else {
313                         pm2 = pm1;
314                 }
315
316                 TAILQ_REMOVE(&up->up_qhead, pm2, pm_qentry);
317         } else {
318                 pm2 = NULL;             /* panic - should not happen */
319         }
320
321         DPRINTF(" t=%u, num=%u\n", t, up->up_msg_num);
322
323         /* Put message last on queue */
324
325         pm2->pm_num = up->up_msg_num;
326         TAILQ_INSERT_TAIL(&up->up_qhead, pm2, pm_qentry);
327
328         /* Check if we need to wakeup the USB process. */
329
330         if (up->up_msleep) {
331                 up->up_msleep = 0;      /* save "cv_signal()" calls */
332                 cv_signal(&up->up_cv);
333         }
334         return (pm2);
335 }
336
337 /*------------------------------------------------------------------------*
338  *      usb_proc_is_gone
339  *
340  * Return values:
341  *    0: USB process is running
342  * Else: USB process is tearing down
343  *------------------------------------------------------------------------*/
344 uint8_t
345 usb_proc_is_gone(struct usb_process *up)
346 {
347         if (up->up_gone)
348                 return (1);
349
350         /*
351          * Allow calls when up_mtx is NULL, before the USB process
352          * structure is initialised.
353          */
354         if (up->up_lock != NULL)
355         KKASSERT(lockstatus(up->up_lock, curthread)!=0);
356
357         return (0);
358 }
359
360 /*------------------------------------------------------------------------*
361  *      usb_proc_mwait
362  *
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  *------------------------------------------------------------------------*/
367 void
368 usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
369 {
370         struct usb_proc_msg *pm0 = _pm0;
371         struct usb_proc_msg *pm1 = _pm1;
372
373         /* check if gone */
374         if (up->up_gone)
375                 return;
376
377     KKASSERT(lockstatus(up->up_lock, curthread) != 0);
378
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;
384                 }
385                 if (pm1->pm_qentry.tqe_prev) {
386                         TAILQ_REMOVE(&up->up_qhead, pm1, pm_qentry);
387                         pm1->pm_qentry.tqe_prev = NULL;
388                 }
389         } else
390                 while (pm0->pm_qentry.tqe_prev ||
391                     pm1->pm_qentry.tqe_prev) {
392                         /* check if config thread is gone */
393                         if (up->up_gone)
394                                 break;
395                         up->up_dsleep = 1;
396                         cv_wait(&up->up_drain, up->up_lock);
397                 }
398 }
399
400 /*------------------------------------------------------------------------*
401  *      usb_proc_drain
402  *
403  * This function will tear down an USB process, waiting for the
404  * currently executing command to return.
405  *
406  * NOTE: If the structure pointed to by "up" is all zero,
407  * this function does nothing.
408  *------------------------------------------------------------------------*/
409 void
410 usb_proc_drain(struct usb_process *up)
411 {
412         /* check if not initialised */
413         if (up->up_lock == NULL)
414                 return;
415         /* handle special case with Giant */
416     /* XXX
417         if (up->up_mtx != &Giant)
418                 mtx_assert(up->up_mtx, MA_NOTOWNED);
419     */
420     KKASSERT(lockstatus(up->up_lock, curthread) == 0);
421         lockmgr(up->up_lock, LK_EXCLUSIVE);
422
423         /* Set the gone flag */
424
425         up->up_gone = 1;
426
427         while (up->up_ptr) {
428
429                 /* Check if we need to wakeup the USB process */
430
431                 if (up->up_msleep || up->up_csleep) {
432                         up->up_msleep = 0;
433                         up->up_csleep = 0;
434                         cv_signal(&up->up_cv);
435                 }
436                 /* Check if we are still cold booted */
437
438                 if (cold) {
439                         USB_THREAD_SUSPEND(up->up_ptr);
440                         kprintf("WARNING: A USB process has "
441                             "been left suspended\n");
442                         break;
443                 }
444                 cv_wait(&up->up_cv, up->up_lock);
445         }
446         /* Check if someone is waiting - should not happen */
447
448         if (up->up_dsleep) {
449                 up->up_dsleep = 0;
450                 cv_broadcast(&up->up_drain);
451                 DPRINTF("WARNING: Someone is waiting "
452                     "for USB process drain!\n");
453         }
454     lockmgr(up->up_lock, LK_RELEASE);
455 }
456
457 /*------------------------------------------------------------------------*
458  *      usb_proc_rewakeup
459  *
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  *------------------------------------------------------------------------*/
465 void
466 usb_proc_rewakeup(struct usb_process *up)
467 {
468         /* check if not initialised */
469         if (up->up_lock == NULL)
470                 return;
471         /* check if gone */
472         if (up->up_gone)
473                 return;
474         KKASSERT(lockstatus(up->up_lock, curthread) != 0);
475
476         if (up->up_msleep == 0) {
477                 /* re-wakeup */
478                 cv_signal(&up->up_cv);
479         }
480 }