Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 3 | * Copyright (C) 2015 Nobuo Iwata |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 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 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/workqueue.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 25 | |
matt mooney | 0f79847 | 2011-05-06 03:47:47 -0700 | [diff] [blame] | 26 | #include "usbip_common.h" |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 27 | |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 28 | struct usbip_event { |
| 29 | struct list_head node; |
| 30 | struct usbip_device *ud; |
| 31 | }; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 32 | |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 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) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 85 | usbip_dbg_eh("pending event %lx\n", ud->event); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * NOTE: shutdown must come first. |
| 89 | * Shutdown the device. |
| 90 | */ |
| 91 | if (ud->event & USBIP_EH_SHUTDOWN) { |
| 92 | ud->eh_ops.shutdown(ud); |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 93 | unset_event(ud, USBIP_EH_SHUTDOWN); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 94 | } |
| 95 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 96 | /* Reset the device. */ |
| 97 | if (ud->event & USBIP_EH_RESET) { |
| 98 | ud->eh_ops.reset(ud); |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 99 | unset_event(ud, USBIP_EH_RESET); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Mark the device as unusable. */ |
| 103 | if (ud->event & USBIP_EH_UNUSABLE) { |
| 104 | ud->eh_ops.unusable(ud); |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 105 | unset_event(ud, USBIP_EH_UNUSABLE); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Max Vozeler | 584c5b7 | 2010-09-21 17:43:30 +0200 | [diff] [blame] | 108 | /* Stop the error handler. */ |
| 109 | if (ud->event & USBIP_EH_BYE) |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 110 | usbip_dbg_eh("removed %p\n", ud); |
| 111 | |
| 112 | wake_up(&ud->eh_waitq); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 113 | } |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 114 | } |
| 115 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 116 | int usbip_start_eh(struct usbip_device *ud) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 117 | { |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 118 | init_waitqueue_head(&ud->eh_waitq); |
| 119 | ud->event = 0; |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 120 | return 0; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL_GPL(usbip_start_eh); |
| 123 | |
| 124 | void usbip_stop_eh(struct usbip_device *ud) |
| 125 | { |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 126 | unsigned long pending = ud->event & ~USBIP_EH_BYE; |
Eric Lescouet | d01f42a | 2010-04-24 02:55:24 +0200 | [diff] [blame] | 127 | |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 128 | if (!(ud->event & USBIP_EH_BYE)) |
| 129 | usbip_dbg_eh("usbip_eh stopping but not removed\n"); |
| 130 | |
| 131 | if (pending) |
| 132 | usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending); |
| 133 | |
| 134 | wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE)); |
| 135 | usbip_dbg_eh("usbip_eh has stopped\n"); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(usbip_stop_eh); |
| 138 | |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 139 | #define WORK_QUEUE_NAME "usbip_event" |
| 140 | |
| 141 | static struct workqueue_struct *usbip_queue; |
| 142 | static DECLARE_WORK(usbip_work, event_handler); |
| 143 | |
| 144 | int usbip_init_eh(void) |
| 145 | { |
| 146 | usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME); |
| 147 | if (usbip_queue == NULL) { |
| 148 | pr_err("failed to create usbip_event\n"); |
| 149 | return -ENOMEM; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void usbip_finish_eh(void) |
| 155 | { |
| 156 | flush_workqueue(usbip_queue); |
| 157 | destroy_workqueue(usbip_queue); |
| 158 | usbip_queue = NULL; |
| 159 | } |
| 160 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 161 | void usbip_event_add(struct usbip_device *ud, unsigned long event) |
| 162 | { |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 163 | struct usbip_event *ue; |
Harvey Yang | dcf14779 | 2013-01-22 13:31:30 +0800 | [diff] [blame] | 164 | unsigned long flags; |
| 165 | |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 166 | if (ud->event & USBIP_EH_BYE) |
| 167 | return; |
| 168 | |
| 169 | set_event(ud, event); |
| 170 | |
| 171 | spin_lock_irqsave(&event_lock, flags); |
| 172 | |
| 173 | list_for_each_entry_reverse(ue, &event_list, node) { |
| 174 | if (ue->ud == ud) |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC); |
| 179 | if (ue == NULL) |
| 180 | goto out; |
| 181 | |
| 182 | ue->ud = ud; |
| 183 | |
| 184 | list_add_tail(&ue->node, &event_list); |
| 185 | queue_work(usbip_queue, &usbip_work); |
| 186 | |
| 187 | out: |
| 188 | spin_unlock_irqrestore(&event_lock, flags); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 189 | } |
| 190 | EXPORT_SYMBOL_GPL(usbip_event_add); |
| 191 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 192 | int usbip_event_happened(struct usbip_device *ud) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 193 | { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 194 | int happened = 0; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 195 | unsigned long flags; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 196 | |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 197 | spin_lock_irqsave(&ud->lock, flags); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 198 | if (ud->event != 0) |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 199 | happened = 1; |
Andrew Goodbody | 2161979 | 2016-02-02 17:36:39 +0000 | [diff] [blame] | 200 | spin_unlock_irqrestore(&ud->lock, flags); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 201 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 202 | return happened; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 203 | } |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 204 | EXPORT_SYMBOL_GPL(usbip_event_happened); |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 205 | |
| 206 | int usbip_in_eh(struct task_struct *task) |
| 207 | { |
| 208 | if (task == worker_context) |
| 209 | return 1; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(usbip_in_eh); |