usb: usbip: Fix possible deadlocks reported by lockdep
[linux/fpc-iii.git] / drivers / usb / usbip / usbip_event.c
blob2580a32bcdfff088d4e5f86af1e09f93b13c20ce
1 /*
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,
17 * USA.
20 #include <linux/kthread.h>
21 #include <linux/export.h>
23 #include "usbip_common.h"
25 static int event_handler(struct usbip_device *ud)
27 usbip_dbg_eh("enter\n");
30 * Events are handled by only this thread.
32 while (usbip_event_happened(ud)) {
33 usbip_dbg_eh("pending event %lx\n", ud->event);
36 * NOTE: shutdown must come first.
37 * Shutdown the device.
39 if (ud->event & USBIP_EH_SHUTDOWN) {
40 ud->eh_ops.shutdown(ud);
41 ud->event &= ~USBIP_EH_SHUTDOWN;
44 /* Reset the device. */
45 if (ud->event & USBIP_EH_RESET) {
46 ud->eh_ops.reset(ud);
47 ud->event &= ~USBIP_EH_RESET;
50 /* Mark the device as unusable. */
51 if (ud->event & USBIP_EH_UNUSABLE) {
52 ud->eh_ops.unusable(ud);
53 ud->event &= ~USBIP_EH_UNUSABLE;
56 /* Stop the error handler. */
57 if (ud->event & USBIP_EH_BYE)
58 return -1;
61 return 0;
64 static int event_handler_loop(void *data)
66 struct usbip_device *ud = data;
68 while (!kthread_should_stop()) {
69 wait_event_interruptible(ud->eh_waitq,
70 usbip_event_happened(ud) ||
71 kthread_should_stop());
72 usbip_dbg_eh("wakeup\n");
74 if (event_handler(ud) < 0)
75 break;
78 return 0;
81 int usbip_start_eh(struct usbip_device *ud)
83 init_waitqueue_head(&ud->eh_waitq);
84 ud->event = 0;
86 ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh");
87 if (IS_ERR(ud->eh)) {
88 pr_warn("Unable to start control thread\n");
89 return PTR_ERR(ud->eh);
92 return 0;
94 EXPORT_SYMBOL_GPL(usbip_start_eh);
96 void usbip_stop_eh(struct usbip_device *ud)
98 if (ud->eh == current)
99 return; /* do not wait for myself */
101 kthread_stop(ud->eh);
102 usbip_dbg_eh("usbip_eh has finished\n");
104 EXPORT_SYMBOL_GPL(usbip_stop_eh);
106 void usbip_event_add(struct usbip_device *ud, unsigned long event)
108 unsigned long flags;
110 spin_lock_irqsave(&ud->lock, flags);
111 ud->event |= event;
112 wake_up(&ud->eh_waitq);
113 spin_unlock_irqrestore(&ud->lock, flags);
115 EXPORT_SYMBOL_GPL(usbip_event_add);
117 int usbip_event_happened(struct usbip_device *ud)
119 int happened = 0;
120 unsigned long flags;
122 spin_lock_irqsave(&ud->lock, flags);
123 if (ud->event != 0)
124 happened = 1;
125 spin_unlock_irqrestore(&ud->lock, flags);
127 return happened;
129 EXPORT_SYMBOL_GPL(usbip_event_happened);