usbip_event.c 4.5 KB

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