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