GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / usb / usbip / usbip_event.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  * Copyright (C) 2015 Nobuo Iwata
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18  * USA.
19  */
20
21 #include <linux/kthread.h>
22 #include <linux/export.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25
26 #include "usbip_common.h"
27
28 struct usbip_event {
29         struct list_head node;
30         struct usbip_device *ud;
31 };
32
33 static DEFINE_SPINLOCK(event_lock);
34 static LIST_HEAD(event_list);
35
36 static void set_event(struct usbip_device *ud, unsigned long event)
37 {
38         unsigned long flags;
39
40         spin_lock_irqsave(&ud->lock, flags);
41         ud->event |= event;
42         spin_unlock_irqrestore(&ud->lock, flags);
43 }
44
45 static void unset_event(struct usbip_device *ud, unsigned long event)
46 {
47         unsigned long flags;
48
49         spin_lock_irqsave(&ud->lock, flags);
50         ud->event &= ~event;
51         spin_unlock_irqrestore(&ud->lock, flags);
52 }
53
54 static struct usbip_device *get_event(void)
55 {
56         struct usbip_event *ue = NULL;
57         struct usbip_device *ud = NULL;
58         unsigned long flags;
59
60         spin_lock_irqsave(&event_lock, flags);
61         if (!list_empty(&event_list)) {
62                 ue = list_first_entry(&event_list, struct usbip_event, node);
63                 list_del(&ue->node);
64         }
65         spin_unlock_irqrestore(&event_lock, flags);
66
67         if (ue) {
68                 ud = ue->ud;
69                 kfree(ue);
70         }
71         return ud;
72 }
73
74 static struct task_struct *worker_context;
75
76 static void event_handler(struct work_struct *work)
77 {
78         struct usbip_device *ud;
79
80         if (worker_context == NULL) {
81                 worker_context = current;
82         }
83
84         while ((ud = get_event()) != NULL) {
85                 usbip_dbg_eh("pending event %lx\n", ud->event);
86
87                 mutex_lock(&ud->sysfs_lock);
88                 /*
89                  * NOTE: shutdown must come first.
90                  * Shutdown the device.
91                  */
92                 if (ud->event & USBIP_EH_SHUTDOWN) {
93                         ud->eh_ops.shutdown(ud);
94                         unset_event(ud, USBIP_EH_SHUTDOWN);
95                 }
96
97                 /* Reset the device. */
98                 if (ud->event & USBIP_EH_RESET) {
99                         ud->eh_ops.reset(ud);
100                         unset_event(ud, USBIP_EH_RESET);
101                 }
102
103                 /* Mark the device as unusable. */
104                 if (ud->event & USBIP_EH_UNUSABLE) {
105                         ud->eh_ops.unusable(ud);
106                         unset_event(ud, USBIP_EH_UNUSABLE);
107                 }
108                 mutex_unlock(&ud->sysfs_lock);
109
110                 wake_up(&ud->eh_waitq);
111         }
112 }
113
114 int usbip_start_eh(struct usbip_device *ud)
115 {
116         init_waitqueue_head(&ud->eh_waitq);
117         ud->event = 0;
118         return 0;
119 }
120 EXPORT_SYMBOL_GPL(usbip_start_eh);
121
122 void usbip_stop_eh(struct usbip_device *ud)
123 {
124         unsigned long pending = ud->event & ~USBIP_EH_BYE;
125
126         if (!(ud->event & USBIP_EH_BYE))
127                 usbip_dbg_eh("usbip_eh stopping but not removed\n");
128
129         if (pending)
130                 usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending);
131
132         wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE));
133         usbip_dbg_eh("usbip_eh has stopped\n");
134 }
135 EXPORT_SYMBOL_GPL(usbip_stop_eh);
136
137 #define WORK_QUEUE_NAME "usbip_event"
138
139 static struct workqueue_struct *usbip_queue;
140 static DECLARE_WORK(usbip_work, event_handler);
141
142 int usbip_init_eh(void)
143 {
144         usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME);
145         if (usbip_queue == NULL) {
146                 pr_err("failed to create usbip_event\n");
147                 return -ENOMEM;
148         }
149         return 0;
150 }
151
152 void usbip_finish_eh(void)
153 {
154         flush_workqueue(usbip_queue);
155         destroy_workqueue(usbip_queue);
156         usbip_queue = NULL;
157 }
158
159 void usbip_event_add(struct usbip_device *ud, unsigned long event)
160 {
161         struct usbip_event *ue;
162         unsigned long flags;
163
164         if (ud->event & USBIP_EH_BYE)
165                 return;
166
167         set_event(ud, event);
168
169         spin_lock_irqsave(&event_lock, flags);
170
171         list_for_each_entry_reverse(ue, &event_list, node) {
172                 if (ue->ud == ud)
173                         goto out;
174         }
175
176         ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC);
177         if (ue == NULL)
178                 goto out;
179
180         ue->ud = ud;
181
182         list_add_tail(&ue->node, &event_list);
183         queue_work(usbip_queue, &usbip_work);
184
185 out:
186         spin_unlock_irqrestore(&event_lock, flags);
187 }
188 EXPORT_SYMBOL_GPL(usbip_event_add);
189
190 int usbip_event_happened(struct usbip_device *ud)
191 {
192         int happened = 0;
193         unsigned long flags;
194
195         spin_lock_irqsave(&ud->lock, flags);
196         if (ud->event != 0)
197                 happened = 1;
198         spin_unlock_irqrestore(&ud->lock, flags);
199
200         return happened;
201 }
202 EXPORT_SYMBOL_GPL(usbip_event_happened);
203
204 int usbip_in_eh(struct task_struct *task)
205 {
206         if (task == worker_context)
207                 return 1;
208
209         return 0;
210 }
211 EXPORT_SYMBOL_GPL(usbip_in_eh);