^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2003-2008 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2015 Nobuo Iwata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct usbip_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct usbip_device *ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static DEFINE_SPINLOCK(event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static LIST_HEAD(event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void set_event(struct usbip_device *ud, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) spin_lock_irqsave(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ud->event |= event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) spin_unlock_irqrestore(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void unset_event(struct usbip_device *ud, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_lock_irqsave(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ud->event &= ~event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) spin_unlock_irqrestore(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static struct usbip_device *get_event(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct usbip_event *ue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct usbip_device *ud = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) spin_lock_irqsave(&event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!list_empty(&event_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ue = list_first_entry(&event_list, struct usbip_event, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) list_del(&ue->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) spin_unlock_irqrestore(&event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (ue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ud = ue->ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfree(ue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct task_struct *worker_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static void event_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct usbip_device *ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (worker_context == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) worker_context = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) while ((ud = get_event()) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) usbip_dbg_eh("pending event %lx\n", ud->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) mutex_lock(&ud->sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * NOTE: shutdown must come first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * Shutdown the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ud->event & USBIP_EH_SHUTDOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ud->eh_ops.shutdown(ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unset_event(ud, USBIP_EH_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Reset the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ud->event & USBIP_EH_RESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ud->eh_ops.reset(ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unset_event(ud, USBIP_EH_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Mark the device as unusable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (ud->event & USBIP_EH_UNUSABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ud->eh_ops.unusable(ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unset_event(ud, USBIP_EH_UNUSABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mutex_unlock(&ud->sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) wake_up(&ud->eh_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int usbip_start_eh(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) init_waitqueue_head(&ud->eh_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ud->event = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) EXPORT_SYMBOL_GPL(usbip_start_eh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void usbip_stop_eh(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned long pending = ud->event & ~USBIP_EH_BYE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!(ud->event & USBIP_EH_BYE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) usbip_dbg_eh("usbip_eh stopping but not removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) usbip_dbg_eh("usbip_eh has stopped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL_GPL(usbip_stop_eh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #define WORK_QUEUE_NAME "usbip_event"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct workqueue_struct *usbip_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static DECLARE_WORK(usbip_work, event_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int usbip_init_eh(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (usbip_queue == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_err("failed to create usbip_event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void usbip_finish_eh(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) flush_workqueue(usbip_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) destroy_workqueue(usbip_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) usbip_queue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) void usbip_event_add(struct usbip_device *ud, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct usbip_event *ue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ud->event & USBIP_EH_BYE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) set_event(ud, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_lock_irqsave(&event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) list_for_each_entry_reverse(ue, &event_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ue->ud == ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ue == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ue->ud = ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) list_add_tail(&ue->node, &event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) queue_work(usbip_queue, &usbip_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) spin_unlock_irqrestore(&event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL_GPL(usbip_event_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int usbip_event_happened(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int happened = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) spin_lock_irqsave(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ud->event != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) happened = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) spin_unlock_irqrestore(&ud->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return happened;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) EXPORT_SYMBOL_GPL(usbip_event_happened);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int usbip_in_eh(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (task == worker_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL_GPL(usbip_in_eh);