]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/usbip/usbip_event.c
Merge git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[linux-2.6-omap-h63xx.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
22 static int event_handler(struct usbip_device *ud)
23 {
24         dbg_eh("enter\n");
25
26         /*
27          * Events are handled by only this thread.
28          */
29         while (usbip_event_happend(ud)) {
30                 dbg_eh("pending event %lx\n", ud->event);
31
32                 /*
33                  * NOTE: shutdown must come first.
34                  * Shutdown the device.
35                  */
36                 if (ud->event & USBIP_EH_SHUTDOWN) {
37                         ud->eh_ops.shutdown(ud);
38
39                         ud->event &= ~USBIP_EH_SHUTDOWN;
40
41                         break;
42                 }
43
44                 /* Stop the error handler. */
45                 if (ud->event & USBIP_EH_BYE)
46                         return -1;
47
48                 /* Reset the device. */
49                 if (ud->event & USBIP_EH_RESET) {
50                         ud->eh_ops.reset(ud);
51
52                         ud->event &= ~USBIP_EH_RESET;
53
54                         break;
55                 }
56
57                 /* Mark the device as unusable. */
58                 if (ud->event & USBIP_EH_UNUSABLE) {
59                         ud->eh_ops.unusable(ud);
60
61                         ud->event &= ~USBIP_EH_UNUSABLE;
62
63                         break;
64                 }
65
66                 /* NOTREACHED */
67                 printk(KERN_ERR "%s: unknown event\n", __func__);
68                 return -1;
69         }
70
71         return 0;
72 }
73
74 static void event_handler_loop(struct usbip_task *ut)
75 {
76         struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
77
78         while (1) {
79                 if (signal_pending(current)) {
80                         dbg_eh("signal catched!\n");
81                         break;
82                 }
83
84                 if (event_handler(ud) < 0)
85                         break;
86
87                 wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
88                 dbg_eh("wakeup\n");
89         }
90 }
91
92 void usbip_start_eh(struct usbip_device *ud)
93 {
94         struct usbip_task *eh = &ud->eh;
95
96         init_waitqueue_head(&ud->eh_waitq);
97         ud->event = 0;
98
99         usbip_task_init(eh, "usbip_eh", event_handler_loop);
100
101         kernel_thread(usbip_thread, (void *)eh, 0);
102
103         wait_for_completion(&eh->thread_done);
104 }
105 EXPORT_SYMBOL_GPL(usbip_start_eh);
106
107 void usbip_stop_eh(struct usbip_device *ud)
108 {
109         struct usbip_task *eh = &ud->eh;
110
111         wait_for_completion(&eh->thread_done);
112         dbg_eh("usbip_eh has finished\n");
113 }
114 EXPORT_SYMBOL_GPL(usbip_stop_eh);
115
116 void usbip_event_add(struct usbip_device *ud, unsigned long event)
117 {
118         spin_lock(&ud->lock);
119
120         ud->event |= event;
121
122         wake_up(&ud->eh_waitq);
123
124         spin_unlock(&ud->lock);
125 }
126 EXPORT_SYMBOL_GPL(usbip_event_add);
127
128 int usbip_event_happend(struct usbip_device *ud)
129 {
130         int happend = 0;
131
132         spin_lock(&ud->lock);
133
134         if (ud->event != 0)
135                 happend = 1;
136
137         spin_unlock(&ud->lock);
138
139         return happend;
140 }
141 EXPORT_SYMBOL_GPL(usbip_event_happend);