usbip_event.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /*
  58. * NOTE: shutdown must come first.
  59. * Shutdown the device.
  60. */
  61. if (ud->event & USBIP_EH_SHUTDOWN) {
  62. ud->eh_ops.shutdown(ud);
  63. unset_event(ud, USBIP_EH_SHUTDOWN);
  64. }
  65. /* Reset the device. */
  66. if (ud->event & USBIP_EH_RESET) {
  67. ud->eh_ops.reset(ud);
  68. unset_event(ud, USBIP_EH_RESET);
  69. }
  70. /* Mark the device as unusable. */
  71. if (ud->event & USBIP_EH_UNUSABLE) {
  72. ud->eh_ops.unusable(ud);
  73. unset_event(ud, USBIP_EH_UNUSABLE);
  74. }
  75. wake_up(&ud->eh_waitq);
  76. }
  77. }
  78. int usbip_start_eh(struct usbip_device *ud)
  79. {
  80. init_waitqueue_head(&ud->eh_waitq);
  81. ud->event = 0;
  82. return 0;
  83. }
  84. EXPORT_SYMBOL_GPL(usbip_start_eh);
  85. void usbip_stop_eh(struct usbip_device *ud)
  86. {
  87. unsigned long pending = ud->event & ~USBIP_EH_BYE;
  88. if (!(ud->event & USBIP_EH_BYE))
  89. usbip_dbg_eh("usbip_eh stopping but not removed\n");
  90. if (pending)
  91. usbip_dbg_eh("usbip_eh waiting completion %lx\n", pending);
  92. wait_event_interruptible(ud->eh_waitq, !(ud->event & ~USBIP_EH_BYE));
  93. usbip_dbg_eh("usbip_eh has stopped\n");
  94. }
  95. EXPORT_SYMBOL_GPL(usbip_stop_eh);
  96. #define WORK_QUEUE_NAME "usbip_event"
  97. static struct workqueue_struct *usbip_queue;
  98. static DECLARE_WORK(usbip_work, event_handler);
  99. int usbip_init_eh(void)
  100. {
  101. usbip_queue = create_singlethread_workqueue(WORK_QUEUE_NAME);
  102. if (usbip_queue == NULL) {
  103. pr_err("failed to create usbip_event\n");
  104. return -ENOMEM;
  105. }
  106. return 0;
  107. }
  108. void usbip_finish_eh(void)
  109. {
  110. flush_workqueue(usbip_queue);
  111. destroy_workqueue(usbip_queue);
  112. usbip_queue = NULL;
  113. }
  114. void usbip_event_add(struct usbip_device *ud, unsigned long event)
  115. {
  116. struct usbip_event *ue;
  117. unsigned long flags;
  118. if (ud->event & USBIP_EH_BYE)
  119. return;
  120. set_event(ud, event);
  121. spin_lock_irqsave(&event_lock, flags);
  122. list_for_each_entry_reverse(ue, &event_list, node) {
  123. if (ue->ud == ud)
  124. goto out;
  125. }
  126. ue = kmalloc(sizeof(struct usbip_event), GFP_ATOMIC);
  127. if (ue == NULL)
  128. goto out;
  129. ue->ud = ud;
  130. list_add_tail(&ue->node, &event_list);
  131. queue_work(usbip_queue, &usbip_work);
  132. out:
  133. spin_unlock_irqrestore(&event_lock, flags);
  134. }
  135. EXPORT_SYMBOL_GPL(usbip_event_add);
  136. int usbip_event_happened(struct usbip_device *ud)
  137. {
  138. int happened = 0;
  139. unsigned long flags;
  140. spin_lock_irqsave(&ud->lock, flags);
  141. if (ud->event != 0)
  142. happened = 1;
  143. spin_unlock_irqrestore(&ud->lock, flags);
  144. return happened;
  145. }
  146. EXPORT_SYMBOL_GPL(usbip_event_happened);
  147. int usbip_in_eh(struct task_struct *task)
  148. {
  149. if (task == worker_context)
  150. return 1;
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(usbip_in_eh);