usbip_event.c 3.9 KB

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