usbip_event.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <linux/kthread.h>
  20. #include <linux/export.h>
  21. #include "usbip_common.h"
  22. static int event_handler(struct usbip_device *ud)
  23. {
  24. usbip_dbg_eh("enter\n");
  25. /*
  26. * Events are handled by only this thread.
  27. */
  28. while (usbip_event_happened(ud)) {
  29. usbip_dbg_eh("pending event %lx\n", ud->event);
  30. /*
  31. * NOTE: shutdown must come first.
  32. * Shutdown the device.
  33. */
  34. if (ud->event & USBIP_EH_SHUTDOWN) {
  35. ud->eh_ops.shutdown(ud);
  36. ud->event &= ~USBIP_EH_SHUTDOWN;
  37. }
  38. /* Reset the device. */
  39. if (ud->event & USBIP_EH_RESET) {
  40. ud->eh_ops.reset(ud);
  41. ud->event &= ~USBIP_EH_RESET;
  42. }
  43. /* Mark the device as unusable. */
  44. if (ud->event & USBIP_EH_UNUSABLE) {
  45. ud->eh_ops.unusable(ud);
  46. ud->event &= ~USBIP_EH_UNUSABLE;
  47. }
  48. /* Stop the error handler. */
  49. if (ud->event & USBIP_EH_BYE)
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. static int event_handler_loop(void *data)
  55. {
  56. struct usbip_device *ud = data;
  57. while (!kthread_should_stop()) {
  58. wait_event_interruptible(ud->eh_waitq,
  59. usbip_event_happened(ud) ||
  60. kthread_should_stop());
  61. usbip_dbg_eh("wakeup\n");
  62. if (event_handler(ud) < 0)
  63. break;
  64. }
  65. return 0;
  66. }
  67. int usbip_start_eh(struct usbip_device *ud)
  68. {
  69. init_waitqueue_head(&ud->eh_waitq);
  70. ud->event = 0;
  71. ud->eh = kthread_run(event_handler_loop, ud, "usbip_eh");
  72. if (IS_ERR(ud->eh)) {
  73. pr_warn("Unable to start control thread\n");
  74. return PTR_ERR(ud->eh);
  75. }
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(usbip_start_eh);
  79. void usbip_stop_eh(struct usbip_device *ud)
  80. {
  81. if (ud->eh == current)
  82. return; /* do not wait for myself */
  83. kthread_stop(ud->eh);
  84. usbip_dbg_eh("usbip_eh has finished\n");
  85. }
  86. EXPORT_SYMBOL_GPL(usbip_stop_eh);
  87. void usbip_event_add(struct usbip_device *ud, unsigned long event)
  88. {
  89. unsigned long flags;
  90. spin_lock_irqsave(&ud->lock, flags);
  91. ud->event |= event;
  92. wake_up(&ud->eh_waitq);
  93. spin_unlock_irqrestore(&ud->lock, flags);
  94. }
  95. EXPORT_SYMBOL_GPL(usbip_event_add);
  96. int usbip_event_happened(struct usbip_device *ud)
  97. {
  98. int happened = 0;
  99. unsigned long flags;
  100. spin_lock_irqsave(&ud->lock, flags);
  101. if (ud->event != 0)
  102. happened = 1;
  103. spin_unlock_irqrestore(&ud->lock, flags);
  104. return happened;
  105. }
  106. EXPORT_SYMBOL_GPL(usbip_event_happened);