blob: 64933b993d7a5f0a8ff64a82979ed20bb9890534 [file] [log] [blame]
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06001/*
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
Brian G. Merrellb8868e42009-07-21 00:46:13 -060020#include <linux/kthread.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -040021#include <linux/export.h>
matt mooney7aaacb42011-05-11 22:33:43 -070022
matt mooney0f798472011-05-06 03:47:47 -070023#include "usbip_common.h"
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060024
25static int event_handler(struct usbip_device *ud)
26{
Brian G. Merrellb8868e42009-07-21 00:46:13 -060027 usbip_dbg_eh("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060028
29 /*
30 * Events are handled by only this thread.
31 */
Brian G. Merrellb8868e42009-07-21 00:46:13 -060032 while (usbip_event_happened(ud)) {
33 usbip_dbg_eh("pending event %lx\n", ud->event);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060034
35 /*
36 * NOTE: shutdown must come first.
37 * Shutdown the device.
38 */
39 if (ud->event & USBIP_EH_SHUTDOWN) {
40 ud->eh_ops.shutdown(ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060041 ud->event &= ~USBIP_EH_SHUTDOWN;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060042 }
43
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060044 /* Reset the device. */
45 if (ud->event & USBIP_EH_RESET) {
46 ud->eh_ops.reset(ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060047 ud->event &= ~USBIP_EH_RESET;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060048 }
49
50 /* Mark the device as unusable. */
51 if (ud->event & USBIP_EH_UNUSABLE) {
52 ud->eh_ops.unusable(ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060053 ud->event &= ~USBIP_EH_UNUSABLE;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060054 }
55
Max Vozeler584c5b72010-09-21 17:43:30 +020056 /* Stop the error handler. */
57 if (ud->event & USBIP_EH_BYE)
58 return -1;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060059 }
60
61 return 0;
62}
63
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010064static int event_handler_loop(void *data)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060065{
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010066 struct usbip_device *ud = data;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060067
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010068 while (!kthread_should_stop()) {
69 wait_event_interruptible(ud->eh_waitq,
matt mooney0f798472011-05-06 03:47:47 -070070 usbip_event_happened(ud) ||
71 kthread_should_stop());
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010072 usbip_dbg_eh("wakeup\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060073
74 if (event_handler(ud) < 0)
75 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060076 }
matt mooney0f798472011-05-06 03:47:47 -070077
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010078 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060079}
80
Brian G. Merrellb8868e42009-07-21 00:46:13 -060081int usbip_start_eh(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060082{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060083 init_waitqueue_head(&ud->eh_waitq);
84 ud->event = 0;
85
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010086 ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh");
87 if (IS_ERR(ud->eh)) {
Lisa Nguyenff451372013-05-05 16:28:37 -070088 pr_warn("Unable to start control thread\n");
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010089 return PTR_ERR(ud->eh);
Brian G. Merrellb8868e42009-07-21 00:46:13 -060090 }
matt mooney0f798472011-05-06 03:47:47 -070091
Brian G. Merrellb8868e42009-07-21 00:46:13 -060092 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060093}
94EXPORT_SYMBOL_GPL(usbip_start_eh);
95
96void usbip_stop_eh(struct usbip_device *ud)
97{
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010098 if (ud->eh == current)
Eric Lescouetd01f42a2010-04-24 02:55:24 +020099 return; /* do not wait for myself */
100
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100101 kthread_stop(ud->eh);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600102 usbip_dbg_eh("usbip_eh has finished\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600103}
104EXPORT_SYMBOL_GPL(usbip_stop_eh);
105
106void usbip_event_add(struct usbip_device *ud, unsigned long event)
107{
Harvey Yangdcf147792013-01-22 13:31:30 +0800108 unsigned long flags;
109
110 spin_lock_irqsave(&ud->lock, flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600111 ud->event |= event;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600112 wake_up(&ud->eh_waitq);
Harvey Yangdcf147792013-01-22 13:31:30 +0800113 spin_unlock_irqrestore(&ud->lock, flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600114}
115EXPORT_SYMBOL_GPL(usbip_event_add);
116
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600117int usbip_event_happened(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600118{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600119 int happened = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600120
121 spin_lock(&ud->lock);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600122 if (ud->event != 0)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600123 happened = 1;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600124 spin_unlock(&ud->lock);
125
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600126 return happened;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600127}
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600128EXPORT_SYMBOL_GPL(usbip_event_happened);