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 <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
) {
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
)
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)
81 int usbip_start_eh(struct usbip_device
*ud
)
83 init_waitqueue_head(&ud
->eh_waitq
);
86 ud
->eh
= kthread_run(event_handler_loop
, ud
, "usbip_eh");
88 pr_warn("Unable to start control thread\n");
89 return PTR_ERR(ud
->eh
);
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
)
110 spin_lock_irqsave(&ud
->lock
, flags
);
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
)
122 spin_lock_irqsave(&ud
->lock
, flags
);
125 spin_unlock_irqrestore(&ud
->lock
, flags
);
129 EXPORT_SYMBOL_GPL(usbip_event_happened
);