blob: 5b4c0864ad92ae6ac40e19b5b10f30667bc09207 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06002/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Nobuo Iwatabb7871a2016-03-24 10:50:59 +09004 * Copyright (C) 2015 Nobuo Iwata
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06005 */
6
Brian G. Merrellb8868e42009-07-21 00:46:13 -06007#include <linux/kthread.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -04008#include <linux/export.h>
Nobuo Iwatabb7871a2016-03-24 10:50:59 +09009#include <linux/slab.h>
10#include <linux/workqueue.h>
matt mooney7aaacb42011-05-11 22:33:43 -070011
matt mooney0f798472011-05-06 03:47:47 -070012#include "usbip_common.h"
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060013
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090014struct usbip_event {
15 struct list_head node;
16 struct usbip_device *ud;
17};
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060018
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090019static DEFINE_SPINLOCK(event_lock);
20static LIST_HEAD(event_list);
21
22static void set_event(struct usbip_device *ud, unsigned long event)
23{
24 unsigned long flags;
25
26 spin_lock_irqsave(&ud->lock, flags);
27 ud->event |= event;
28 spin_unlock_irqrestore(&ud->lock, flags);
29}
30
31static void unset_event(struct usbip_device *ud, unsigned long event)
32{
33 unsigned long flags;
34
35 spin_lock_irqsave(&ud->lock, flags);
36 ud->event &= ~event;
37 spin_unlock_irqrestore(&ud->lock, flags);
38}
39
40static struct usbip_device *get_event(void)
41{
42 struct usbip_event *ue = NULL;
43 struct usbip_device *ud = NULL;
44 unsigned long flags;
45
46 spin_lock_irqsave(&event_lock, flags);
47 if (!list_empty(&event_list)) {
48 ue = list_first_entry(&event_list, struct usbip_event, node);
49 list_del(&ue->node);
50 }
51 spin_unlock_irqrestore(&event_lock, flags);
52
53 if (ue) {
54 ud = ue->ud;
55 kfree(ue);
56 }
57 return ud;
58}
59
60static struct task_struct *worker_context;
61
62static void event_handler(struct work_struct *work)
63{
64 struct usbip_device *ud;
65
66 if (worker_context == NULL) {
67 worker_context = current;
68 }
69
70 while ((ud = get_event()) != NULL) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -060071 usbip_dbg_eh("pending event %lx\n", ud->event);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060072
73 /*
74 * NOTE: shutdown must come first.
75 * Shutdown the device.
76 */
77 if (ud->event & USBIP_EH_SHUTDOWN) {
78 ud->eh_ops.shutdown(ud);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090079 unset_event(ud, USBIP_EH_SHUTDOWN);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060080 }
81
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060082 /* Reset the device. */
83 if (ud->event & USBIP_EH_RESET) {
84 ud->eh_ops.reset(ud);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090085 unset_event(ud, USBIP_EH_RESET);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060086 }
87
88 /* Mark the device as unusable. */
89 if (ud->event & USBIP_EH_UNUSABLE) {
90 ud->eh_ops.unusable(ud);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090091 unset_event(ud, USBIP_EH_UNUSABLE);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060092 }
93
Max Vozeler584c5b72010-09-21 17:43:30 +020094 /* Stop the error handler. */
95 if (ud->event & USBIP_EH_BYE)
Nobuo Iwatabb7871a2016-03-24 10:50:59 +090096 usbip_dbg_eh("removed %p\n", ud);
97
98 wake_up(&ud->eh_waitq);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060099 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600100}
101
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600102int usbip_start_eh(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600103{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600104 init_waitqueue_head(&ud->eh_waitq);
105 ud->event = 0;
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600106 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600107}
108EXPORT_SYMBOL_GPL(usbip_start_eh);
109
110void usbip_stop_eh(struct usbip_device *ud)
111{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900112 unsigned long pending = ud->event & ~USBIP_EH_BYE;
Eric Lescouetd01f42a2010-04-24 02:55:24 +0200113
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900114 if (!(ud->event & USBIP_EH_BYE))
115 usbip_dbg_eh("usbip_eh stopping but not removed\n");
116
117 if (pending)
118 usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending);
119
120 wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE));
121 usbip_dbg_eh("usbip_eh has stopped\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600122}
123EXPORT_SYMBOL_GPL(usbip_stop_eh);
124
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900125#define WORK_QUEUE_NAME "usbip_event"
126
127static struct workqueue_struct *usbip_queue;
128static DECLARE_WORK(usbip_work, event_handler);
129
130int usbip_init_eh(void)
131{
132 usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME);
133 if (usbip_queue == NULL) {
134 pr_err("failed to create usbip_event\n");
135 return -ENOMEM;
136 }
137 return 0;
138}
139
140void usbip_finish_eh(void)
141{
142 flush_workqueue(usbip_queue);
143 destroy_workqueue(usbip_queue);
144 usbip_queue = NULL;
145}
146
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600147void usbip_event_add(struct usbip_device *ud, unsigned long event)
148{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900149 struct usbip_event *ue;
Harvey Yangdcf147792013-01-22 13:31:30 +0800150 unsigned long flags;
151
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900152 if (ud->event & USBIP_EH_BYE)
153 return;
154
155 set_event(ud, event);
156
157 spin_lock_irqsave(&event_lock, flags);
158
159 list_for_each_entry_reverse(ue, &event_list, node) {
160 if (ue->ud == ud)
161 goto out;
162 }
163
164 ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC);
165 if (ue == NULL)
166 goto out;
167
168 ue->ud = ud;
169
170 list_add_tail(&ue->node, &event_list);
171 queue_work(usbip_queue, &usbip_work);
172
173out:
174 spin_unlock_irqrestore(&event_lock, flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600175}
176EXPORT_SYMBOL_GPL(usbip_event_add);
177
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600178int usbip_event_happened(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600179{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600180 int happened = 0;
Andrew Goodbody21619792016-02-02 17:36:39 +0000181 unsigned long flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600182
Andrew Goodbody21619792016-02-02 17:36:39 +0000183 spin_lock_irqsave(&ud->lock, flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600184 if (ud->event != 0)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600185 happened = 1;
Andrew Goodbody21619792016-02-02 17:36:39 +0000186 spin_unlock_irqrestore(&ud->lock, flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600187
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600188 return happened;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600189}
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600190EXPORT_SYMBOL_GPL(usbip_event_happened);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900191
192int usbip_in_eh(struct task_struct *task)
193{
194 if (task == worker_context)
195 return 1;
196
197 return 0;
198}
199EXPORT_SYMBOL_GPL(usbip_in_eh);