event.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 struct genl_family acpi_event_genl_family = {
  69. .id = GENL_ID_GENERATE,
  70. .name = ACPI_GENL_FAMILY_NAME,
  71. .version = ACPI_GENL_VERSION,
  72. .maxattr = ACPI_GENL_ATTR_MAX,
  73. };
  74. static struct genl_multicast_group acpi_event_mcgrp = {
  75. .name = ACPI_GENL_MCAST_GROUP_NAME,
  76. };
  77. int acpi_bus_generate_netlink_event(const char *device_class,
  78. const char *bus_id,
  79. u8 type, int data)
  80. {
  81. struct sk_buff *skb;
  82. struct nlattr *attr;
  83. struct acpi_genl_event *event;
  84. void *msg_header;
  85. int size;
  86. int result;
  87. /* allocate memory */
  88. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  89. nla_total_size(0);
  90. skb = genlmsg_new(size, GFP_ATOMIC);
  91. if (!skb)
  92. return -ENOMEM;
  93. /* add the genetlink message header */
  94. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  95. &acpi_event_genl_family, 0,
  96. ACPI_GENL_CMD_EVENT);
  97. if (!msg_header) {
  98. nlmsg_free(skb);
  99. return -ENOMEM;
  100. }
  101. /* fill the data */
  102. attr =
  103. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  104. sizeof(struct acpi_genl_event));
  105. if (!attr) {
  106. nlmsg_free(skb);
  107. return -EINVAL;
  108. }
  109. event = nla_data(attr);
  110. if (!event) {
  111. nlmsg_free(skb);
  112. return -EINVAL;
  113. }
  114. memset(event, 0, sizeof(struct acpi_genl_event));
  115. strcpy(event->device_class, device_class);
  116. strcpy(event->bus_id, bus_id);
  117. event->type = type;
  118. event->data = data;
  119. /* send multicast genetlink message */
  120. result = genlmsg_end(skb, msg_header);
  121. if (result < 0) {
  122. nlmsg_free(skb);
  123. return result;
  124. }
  125. genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  129. static int acpi_event_genetlink_init(void)
  130. {
  131. int result;
  132. result = genl_register_family(&acpi_event_genl_family);
  133. if (result)
  134. return result;
  135. result = genl_register_mc_group(&acpi_event_genl_family,
  136. &acpi_event_mcgrp);
  137. if (result)
  138. genl_unregister_family(&acpi_event_genl_family);
  139. return result;
  140. }
  141. #else
  142. int acpi_bus_generate_netlink_event(const char *device_class,
  143. const char *bus_id,
  144. u8 type, int data)
  145. {
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  149. static int acpi_event_genetlink_init(void)
  150. {
  151. return -ENODEV;
  152. }
  153. #endif
  154. static int __init acpi_event_init(void)
  155. {
  156. int error = 0;
  157. if (acpi_disabled)
  158. return 0;
  159. /* create genetlink for acpi event */
  160. error = acpi_event_genetlink_init();
  161. if (error)
  162. printk(KERN_WARNING PREFIX
  163. "Failed to create genetlink family for ACPI event\n");
  164. return 0;
  165. }
  166. fs_initcall(acpi_event_init);