notify.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * All the USB notify logic
  3. *
  4. * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
  5. *
  6. * notifier functions originally based on those in kernel/sys.c
  7. * but fixed up to not be so broken.
  8. *
  9. */
  10. #include <linux/config.h>
  11. #include <linux/kernel.h>
  12. #include <linux/notifier.h>
  13. #include <linux/usb.h>
  14. #include "usb.h"
  15. static struct notifier_block *usb_notifier_list;
  16. static DECLARE_MUTEX(usb_notifier_lock);
  17. static void usb_notifier_chain_register(struct notifier_block **list,
  18. struct notifier_block *n)
  19. {
  20. down(&usb_notifier_lock);
  21. while (*list) {
  22. if (n->priority > (*list)->priority)
  23. break;
  24. list = &((*list)->next);
  25. }
  26. n->next = *list;
  27. *list = n;
  28. up(&usb_notifier_lock);
  29. }
  30. static void usb_notifier_chain_unregister(struct notifier_block **nl,
  31. struct notifier_block *n)
  32. {
  33. down(&usb_notifier_lock);
  34. while ((*nl)!=NULL) {
  35. if ((*nl)==n) {
  36. *nl = n->next;
  37. goto exit;
  38. }
  39. nl=&((*nl)->next);
  40. }
  41. exit:
  42. up(&usb_notifier_lock);
  43. }
  44. static int usb_notifier_call_chain(struct notifier_block **n,
  45. unsigned long val, void *v)
  46. {
  47. int ret=NOTIFY_DONE;
  48. struct notifier_block *nb = *n;
  49. down(&usb_notifier_lock);
  50. while (nb) {
  51. ret = nb->notifier_call(nb,val,v);
  52. if (ret&NOTIFY_STOP_MASK) {
  53. goto exit;
  54. }
  55. nb = nb->next;
  56. }
  57. exit:
  58. up(&usb_notifier_lock);
  59. return ret;
  60. }
  61. /**
  62. * usb_register_notify - register a notifier callback whenever a usb change happens
  63. * @nb: pointer to the notifier block for the callback events.
  64. *
  65. * These changes are either USB devices or busses being added or removed.
  66. */
  67. void usb_register_notify(struct notifier_block *nb)
  68. {
  69. usb_notifier_chain_register(&usb_notifier_list, nb);
  70. }
  71. EXPORT_SYMBOL_GPL(usb_register_notify);
  72. /**
  73. * usb_unregister_notify - unregister a notifier callback
  74. * @nb: pointer to the notifier block for the callback events.
  75. *
  76. * usb_register_notifier() must have been previously called for this function
  77. * to work properly.
  78. */
  79. void usb_unregister_notify(struct notifier_block *nb)
  80. {
  81. usb_notifier_chain_unregister(&usb_notifier_list, nb);
  82. }
  83. EXPORT_SYMBOL_GPL(usb_unregister_notify);
  84. void usb_notify_add_device(struct usb_device *udev)
  85. {
  86. usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev);
  87. }
  88. void usb_notify_remove_device(struct usb_device *udev)
  89. {
  90. usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_REMOVE, udev);
  91. }
  92. void usb_notify_add_bus(struct usb_bus *ubus)
  93. {
  94. usb_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus);
  95. }
  96. void usb_notify_remove_bus(struct usb_bus *ubus)
  97. {
  98. usb_notifier_call_chain(&usb_notifier_list, USB_BUS_REMOVE, ubus);
  99. }