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>
22 #include "usbip_common.h"
24 static int event_handler(struct usbip_device
*ud
)
26 usbip_dbg_eh("enter\n");
29 * Events are handled by only this thread.
31 while (usbip_event_happened(ud
)) {
32 usbip_dbg_eh("pending event %lx\n", ud
->event
);
35 * NOTE: shutdown must come first.
36 * Shutdown the device.
38 if (ud
->event
& USBIP_EH_SHUTDOWN
) {
39 ud
->eh_ops
.shutdown(ud
);
40 ud
->event
&= ~USBIP_EH_SHUTDOWN
;
43 /* Reset the device. */
44 if (ud
->event
& USBIP_EH_RESET
) {
46 ud
->event
&= ~USBIP_EH_RESET
;
49 /* Mark the device as unusable. */
50 if (ud
->event
& USBIP_EH_UNUSABLE
) {
51 ud
->eh_ops
.unusable(ud
);
52 ud
->event
&= ~USBIP_EH_UNUSABLE
;
55 /* Stop the error handler. */
56 if (ud
->event
& USBIP_EH_BYE
)
63 static int event_handler_loop(void *data
)
65 struct usbip_device
*ud
= data
;
67 while (!kthread_should_stop()) {
68 wait_event_interruptible(ud
->eh_waitq
,
69 usbip_event_happened(ud
) ||
70 kthread_should_stop());
71 usbip_dbg_eh("wakeup\n");
73 if (event_handler(ud
) < 0)
80 int usbip_start_eh(struct usbip_device
*ud
)
82 init_waitqueue_head(&ud
->eh_waitq
);
85 ud
->eh
= kthread_run(event_handler_loop
, ud
, "usbip_eh");
87 pr_warning("Unable to start control thread\n");
88 return PTR_ERR(ud
->eh
);
93 EXPORT_SYMBOL_GPL(usbip_start_eh
);
95 void usbip_stop_eh(struct usbip_device
*ud
)
97 if (ud
->eh
== current
)
98 return; /* do not wait for myself */
100 kthread_stop(ud
->eh
);
101 usbip_dbg_eh("usbip_eh has finished\n");
103 EXPORT_SYMBOL_GPL(usbip_stop_eh
);
105 void usbip_event_add(struct usbip_device
*ud
, unsigned long event
)
107 spin_lock(&ud
->lock
);
109 wake_up(&ud
->eh_waitq
);
110 spin_unlock(&ud
->lock
);
112 EXPORT_SYMBOL_GPL(usbip_event_add
);
114 int usbip_event_happened(struct usbip_device
*ud
)
118 spin_lock(&ud
->lock
);
121 spin_unlock(&ud
->lock
);
125 EXPORT_SYMBOL_GPL(usbip_event_happened
);