inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / usb / usbip / usbip_event.c
blobf8f7f3803a99c8ce551e7b935400f0f884891c67
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015 Nobuo Iwata
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.
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.
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.
21 #include <linux/kthread.h>
22 #include <linux/export.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
26 #include "usbip_common.h"
28 struct usbip_event {
29 struct list_head node;
30 struct usbip_device *ud;
33 static DEFINE_SPINLOCK(event_lock);
34 static LIST_HEAD(event_list);
36 static void set_event(struct usbip_device *ud, unsigned long event)
38 unsigned long flags;
40 spin_lock_irqsave(&ud->lock, flags);
41 ud->event |= event;
42 spin_unlock_irqrestore(&ud->lock, flags);
45 static void unset_event(struct usbip_device *ud, unsigned long event)
47 unsigned long flags;
49 spin_lock_irqsave(&ud->lock, flags);
50 ud->event &= ~event;
51 spin_unlock_irqrestore(&ud->lock, flags);
54 static struct usbip_device *get_event(void)
56 struct usbip_event *ue = NULL;
57 struct usbip_device *ud = NULL;
58 unsigned long flags;
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);
65 spin_unlock_irqrestore(&event_lock, flags);
67 if (ue) {
68 ud = ue->ud;
69 kfree(ue);
71 return ud;
74 static struct task_struct *worker_context;
76 static void event_handler(struct work_struct *work)
78 struct usbip_device *ud;
80 if (worker_context == NULL) {
81 worker_context = current;
84 while ((ud = get_event()) != NULL) {
85 usbip_dbg_eh("pending event %lx\n", ud->event);
88 * NOTE: shutdown must come first.
89 * Shutdown the device.
91 if (ud->event & USBIP_EH_SHUTDOWN) {
92 ud->eh_ops.shutdown(ud);
93 unset_event(ud, USBIP_EH_SHUTDOWN);
96 /* Reset the device. */
97 if (ud->event & USBIP_EH_RESET) {
98 ud->eh_ops.reset(ud);
99 unset_event(ud, USBIP_EH_RESET);
102 /* Mark the device as unusable. */
103 if (ud->event & USBIP_EH_UNUSABLE) {
104 ud->eh_ops.unusable(ud);
105 unset_event(ud, USBIP_EH_UNUSABLE);
108 wake_up(&ud->eh_waitq);
112 int usbip_start_eh(struct usbip_device *ud)
114 init_waitqueue_head(&ud->eh_waitq);
115 ud->event = 0;
116 return 0;
118 EXPORT_SYMBOL_GPL(usbip_start_eh);
120 void usbip_stop_eh(struct usbip_device *ud)
122 unsigned long pending = ud->event & ~USBIP_EH_BYE;
124 if (!(ud->event & USBIP_EH_BYE))
125 usbip_dbg_eh("usbip_eh stopping but not removed\n");
127 if (pending)
128 usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending);
130 wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE));
131 usbip_dbg_eh("usbip_eh has stopped\n");
133 EXPORT_SYMBOL_GPL(usbip_stop_eh);
135 #define WORK_QUEUE_NAME "usbip_event"
137 static struct workqueue_struct *usbip_queue;
138 static DECLARE_WORK(usbip_work, event_handler);
140 int usbip_init_eh(void)
142 usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME);
143 if (usbip_queue == NULL) {
144 pr_err("failed to create usbip_event\n");
145 return -ENOMEM;
147 return 0;
150 void usbip_finish_eh(void)
152 flush_workqueue(usbip_queue);
153 destroy_workqueue(usbip_queue);
154 usbip_queue = NULL;
157 void usbip_event_add(struct usbip_device *ud, unsigned long event)
159 struct usbip_event *ue;
160 unsigned long flags;
162 if (ud->event & USBIP_EH_BYE)
163 return;
165 set_event(ud, event);
167 spin_lock_irqsave(&event_lock, flags);
169 list_for_each_entry_reverse(ue, &event_list, node) {
170 if (ue->ud == ud)
171 goto out;
174 ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC);
175 if (ue == NULL)
176 goto out;
178 ue->ud = ud;
180 list_add_tail(&ue->node, &event_list);
181 queue_work(usbip_queue, &usbip_work);
183 out:
184 spin_unlock_irqrestore(&event_lock, flags);
186 EXPORT_SYMBOL_GPL(usbip_event_add);
188 int usbip_event_happened(struct usbip_device *ud)
190 int happened = 0;
191 unsigned long flags;
193 spin_lock_irqsave(&ud->lock, flags);
194 if (ud->event != 0)
195 happened = 1;
196 spin_unlock_irqrestore(&ud->lock, flags);
198 return happened;
200 EXPORT_SYMBOL_GPL(usbip_event_happened);
202 int usbip_in_eh(struct task_struct *task)
204 if (task == worker_context)
205 return 1;
207 return 0;
209 EXPORT_SYMBOL_GPL(usbip_in_eh);