usbip_event.c 4.0 KB

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