Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / staging / usbip / usbip_event.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  *
4  * This is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  */
19
20 #include <linux/kthread.h>
21
22 #include "usbip_common.h"
23
24 static int event_handler(struct usbip_device *ud)
25 {
26         usbip_dbg_eh("enter\n");
27
28         /*
29          * Events are handled by only this thread.
30          */
31         while (usbip_event_happened(ud)) {
32                 usbip_dbg_eh("pending event %lx\n", ud->event);
33
34                 /*
35                  * NOTE: shutdown must come first.
36                  * Shutdown the device.
37                  */
38                 if (ud->event & USBIP_EH_SHUTDOWN) {
39                         ud->eh_ops.shutdown(ud);
40                         ud->event &= ~USBIP_EH_SHUTDOWN;
41                 }
42
43                 /* Reset the device. */
44                 if (ud->event & USBIP_EH_RESET) {
45                         ud->eh_ops.reset(ud);
46                         ud->event &= ~USBIP_EH_RESET;
47                 }
48
49                 /* Mark the device as unusable. */
50                 if (ud->event & USBIP_EH_UNUSABLE) {
51                         ud->eh_ops.unusable(ud);
52                         ud->event &= ~USBIP_EH_UNUSABLE;
53                 }
54
55                 /* Stop the error handler. */
56                 if (ud->event & USBIP_EH_BYE)
57                         return -1;
58         }
59
60         return 0;
61 }
62
63 static int event_handler_loop(void *data)
64 {
65         struct usbip_device *ud = data;
66
67         while (!kthread_should_stop()) {
68                 wait_event_interruptible(ud->eh_waitq,
69                                          usbip_event_happened(ud) ||
70                                          kthread_should_stop());
71                 usbip_dbg_eh("wakeup\n");
72
73                 if (event_handler(ud) < 0)
74                         break;
75         }
76
77         return 0;
78 }
79
80 int usbip_start_eh(struct usbip_device *ud)
81 {
82         init_waitqueue_head(&ud->eh_waitq);
83         ud->event = 0;
84
85         ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh");
86         if (IS_ERR(ud->eh)) {
87                 pr_warning("Unable to start control thread\n");
88                 return PTR_ERR(ud->eh);
89         }
90
91         return 0;
92 }
93 EXPORT_SYMBOL_GPL(usbip_start_eh);
94
95 void usbip_stop_eh(struct usbip_device *ud)
96 {
97         if (ud->eh == current)
98                 return; /* do not wait for myself */
99
100         kthread_stop(ud->eh);
101         usbip_dbg_eh("usbip_eh has finished\n");
102 }
103 EXPORT_SYMBOL_GPL(usbip_stop_eh);
104
105 void usbip_event_add(struct usbip_device *ud, unsigned long event)
106 {
107         spin_lock(&ud->lock);
108         ud->event |= event;
109         wake_up(&ud->eh_waitq);
110         spin_unlock(&ud->lock);
111 }
112 EXPORT_SYMBOL_GPL(usbip_event_add);
113
114 int usbip_event_happened(struct usbip_device *ud)
115 {
116         int happened = 0;
117
118         spin_lock(&ud->lock);
119         if (ud->event != 0)
120                 happened = 1;
121         spin_unlock(&ud->lock);
122
123         return happened;
124 }
125 EXPORT_SYMBOL_GPL(usbip_event_happened);