2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include "usbip_common.h"
22 static int event_handler(struct usbip_device *ud)
27 * Events are handled by only this thread.
29 while (usbip_event_happend(ud)) {
30 dbg_eh("pending event %lx\n", ud->event);
33 * NOTE: shutdown must come first.
34 * Shutdown the device.
36 if (ud->event & USBIP_EH_SHUTDOWN) {
37 ud->eh_ops.shutdown(ud);
39 ud->event &= ~USBIP_EH_SHUTDOWN;
44 /* Stop the error handler. */
45 if (ud->event & USBIP_EH_BYE)
48 /* Reset the device. */
49 if (ud->event & USBIP_EH_RESET) {
52 ud->event &= ~USBIP_EH_RESET;
57 /* Mark the device as unusable. */
58 if (ud->event & USBIP_EH_UNUSABLE) {
59 ud->eh_ops.unusable(ud);
61 ud->event &= ~USBIP_EH_UNUSABLE;
67 printk(KERN_ERR "%s: unknown event\n", __func__);
74 static void event_handler_loop(struct usbip_task *ut)
76 struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
79 if (signal_pending(current)) {
80 dbg_eh("signal catched!\n");
84 if (event_handler(ud) < 0)
87 wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
92 void usbip_start_eh(struct usbip_device *ud)
94 struct usbip_task *eh = &ud->eh;
96 init_waitqueue_head(&ud->eh_waitq);
99 usbip_task_init(eh, "usbip_eh", event_handler_loop);
101 kernel_thread(usbip_thread, (void *)eh, 0);
103 wait_for_completion(&eh->thread_done);
105 EXPORT_SYMBOL_GPL(usbip_start_eh);
107 void usbip_stop_eh(struct usbip_device *ud)
109 struct usbip_task *eh = &ud->eh;
111 wait_for_completion(&eh->thread_done);
112 dbg_eh("usbip_eh has finished\n");
114 EXPORT_SYMBOL_GPL(usbip_stop_eh);
116 void usbip_event_add(struct usbip_device *ud, unsigned long event)
118 spin_lock(&ud->lock);
122 wake_up(&ud->eh_waitq);
124 spin_unlock(&ud->lock);
126 EXPORT_SYMBOL_GPL(usbip_event_add);
128 int usbip_event_happend(struct usbip_device *ud)
132 spin_lock(&ud->lock);
137 spin_unlock(&ud->lock);
141 EXPORT_SYMBOL_GPL(usbip_event_happend);