upload tizen1.0 source
[kernel/linux-2.6.36.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 "usbip_common.h"
21 #include <linux/kthread.h>
22
23 static int event_handler(struct usbip_device *ud)
24 {
25         usbip_dbg_eh("enter\n");
26
27         /*
28          * Events are handled by only this thread.
29          */
30         while (usbip_event_happened(ud)) {
31                 usbip_dbg_eh("pending event %lx\n", ud->event);
32
33                 /*
34                  * NOTE: shutdown must come first.
35                  * Shutdown the device.
36                  */
37                 if (ud->event & USBIP_EH_SHUTDOWN) {
38                         ud->eh_ops.shutdown(ud);
39
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
47                         ud->event &= ~USBIP_EH_RESET;
48                 }
49
50                 /* Mark the device as unusable. */
51                 if (ud->event & USBIP_EH_UNUSABLE) {
52                         ud->eh_ops.unusable(ud);
53
54                         ud->event &= ~USBIP_EH_UNUSABLE;
55                 }
56
57                 /* Stop the error handler. */
58                 if (ud->event & USBIP_EH_BYE)
59                         return -1;
60         }
61
62         return 0;
63 }
64
65 static void event_handler_loop(struct usbip_task *ut)
66 {
67         struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
68
69         while (1) {
70                 if (signal_pending(current)) {
71                         usbip_dbg_eh("signal catched!\n");
72                         break;
73                 }
74
75                 if (event_handler(ud) < 0)
76                         break;
77
78                 wait_event_interruptible(ud->eh_waitq,
79                                         usbip_event_happened(ud));
80                 usbip_dbg_eh("wakeup\n");
81         }
82 }
83
84 int usbip_start_eh(struct usbip_device *ud)
85 {
86         struct usbip_task *eh = &ud->eh;
87         struct task_struct *th;
88
89         init_waitqueue_head(&ud->eh_waitq);
90         ud->event = 0;
91
92         usbip_task_init(eh, "usbip_eh", event_handler_loop);
93
94         th = kthread_run(usbip_thread, (void *)eh, "usbip");
95         if (IS_ERR(th)) {
96                 printk(KERN_WARNING
97                         "Unable to start control thread\n");
98                 return PTR_ERR(th);
99         }
100
101         wait_for_completion(&eh->thread_done);
102         return 0;
103 }
104 EXPORT_SYMBOL_GPL(usbip_start_eh);
105
106 void usbip_stop_eh(struct usbip_device *ud)
107 {
108         struct usbip_task *eh = &ud->eh;
109
110         if (eh->thread == current)
111                 return; /* do not wait for myself */
112
113         wait_for_completion(&eh->thread_done);
114         usbip_dbg_eh("usbip_eh has finished\n");
115 }
116 EXPORT_SYMBOL_GPL(usbip_stop_eh);
117
118 void usbip_event_add(struct usbip_device *ud, unsigned long event)
119 {
120         spin_lock(&ud->lock);
121
122         ud->event |= event;
123
124         wake_up(&ud->eh_waitq);
125
126         spin_unlock(&ud->lock);
127 }
128 EXPORT_SYMBOL_GPL(usbip_event_add);
129
130 int usbip_event_happened(struct usbip_device *ud)
131 {
132         int happened = 0;
133
134         spin_lock(&ud->lock);
135
136         if (ud->event != 0)
137                 happened = 1;
138
139         spin_unlock(&ud->lock);
140
141         return happened;
142 }
143 EXPORT_SYMBOL_GPL(usbip_event_happened);