usbip_event.c 4.6 KB

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