event.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * event.c - exporting ACPI events via procfs
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. */
  8. #include <linux/spinlock.h>
  9. #include <linux/export.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/init.h>
  12. #include <linux/poll.h>
  13. #include <linux/gfp.h>
  14. #include <acpi/acpi_drivers.h>
  15. #include <net/netlink.h>
  16. #include <net/genetlink.h>
  17. #include "internal.h"
  18. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  19. ACPI_MODULE_NAME("event");
  20. /* ACPI notifier chain */
  21. static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
  22. int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
  23. {
  24. struct acpi_bus_event event;
  25. strcpy(event.device_class, dev->pnp.device_class);
  26. strcpy(event.bus_id, dev->pnp.bus_id);
  27. event.type = type;
  28. event.data = data;
  29. return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
  30. == NOTIFY_BAD) ? -EINVAL : 0;
  31. }
  32. EXPORT_SYMBOL(acpi_notifier_call_chain);
  33. int register_acpi_notifier(struct notifier_block *nb)
  34. {
  35. return blocking_notifier_chain_register(&acpi_chain_head, nb);
  36. }
  37. EXPORT_SYMBOL(register_acpi_notifier);
  38. int unregister_acpi_notifier(struct notifier_block *nb)
  39. {
  40. return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
  41. }
  42. EXPORT_SYMBOL(unregister_acpi_notifier);
  43. #ifdef CONFIG_NET
  44. static unsigned int acpi_event_seqnum;
  45. struct acpi_genl_event {
  46. acpi_device_class device_class;
  47. char bus_id[15];
  48. u32 type;
  49. u32 data;
  50. };
  51. /* attributes of acpi_genl_family */
  52. enum {
  53. ACPI_GENL_ATTR_UNSPEC,
  54. ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
  55. __ACPI_GENL_ATTR_MAX,
  56. };
  57. #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
  58. /* commands supported by the acpi_genl_family */
  59. enum {
  60. ACPI_GENL_CMD_UNSPEC,
  61. ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
  62. __ACPI_GENL_CMD_MAX,
  63. };
  64. #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
  65. #define ACPI_GENL_FAMILY_NAME "acpi_event"
  66. #define ACPI_GENL_VERSION 0x01
  67. #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
  68. static const struct genl_multicast_group acpi_event_mcgrps[] = {
  69. { .name = ACPI_GENL_MCAST_GROUP_NAME, },
  70. };
  71. static struct genl_family acpi_event_genl_family = {
  72. .id = GENL_ID_GENERATE,
  73. .name = ACPI_GENL_FAMILY_NAME,
  74. .version = ACPI_GENL_VERSION,
  75. .maxattr = ACPI_GENL_ATTR_MAX,
  76. .mcgrps = acpi_event_mcgrps,
  77. .n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
  78. };
  79. int acpi_bus_generate_netlink_event(const char *device_class,
  80. const char *bus_id,
  81. u8 type, int data)
  82. {
  83. struct sk_buff *skb;
  84. struct nlattr *attr;
  85. struct acpi_genl_event *event;
  86. void *msg_header;
  87. int size;
  88. int result;
  89. /* allocate memory */
  90. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  91. nla_total_size(0);
  92. skb = genlmsg_new(size, GFP_ATOMIC);
  93. if (!skb)
  94. return -ENOMEM;
  95. /* add the genetlink message header */
  96. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  97. &acpi_event_genl_family, 0,
  98. ACPI_GENL_CMD_EVENT);
  99. if (!msg_header) {
  100. nlmsg_free(skb);
  101. return -ENOMEM;
  102. }
  103. /* fill the data */
  104. attr =
  105. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  106. sizeof(struct acpi_genl_event));
  107. if (!attr) {
  108. nlmsg_free(skb);
  109. return -EINVAL;
  110. }
  111. event = nla_data(attr);
  112. if (!event) {
  113. nlmsg_free(skb);
  114. return -EINVAL;
  115. }
  116. memset(event, 0, sizeof(struct acpi_genl_event));
  117. strcpy(event->device_class, device_class);
  118. strcpy(event->bus_id, bus_id);
  119. event->type = type;
  120. event->data = data;
  121. /* send multicast genetlink message */
  122. result = genlmsg_end(skb, msg_header);
  123. if (result < 0) {
  124. nlmsg_free(skb);
  125. return result;
  126. }
  127. genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  131. static int acpi_event_genetlink_init(void)
  132. {
  133. return genl_register_family(&acpi_event_genl_family);
  134. }
  135. #else
  136. int acpi_bus_generate_netlink_event(const char *device_class,
  137. const char *bus_id,
  138. u8 type, int data)
  139. {
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  143. static int acpi_event_genetlink_init(void)
  144. {
  145. return -ENODEV;
  146. }
  147. #endif
  148. static int __init acpi_event_init(void)
  149. {
  150. int error = 0;
  151. if (acpi_disabled)
  152. return 0;
  153. /* create genetlink for acpi event */
  154. error = acpi_event_genetlink_init();
  155. if (error)
  156. printk(KERN_WARNING PREFIX
  157. "Failed to create genetlink family for ACPI event\n");
  158. return 0;
  159. }
  160. fs_initcall(acpi_event_init);