event.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <linux/acpi.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. /* allocate memory */
  89. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  90. nla_total_size(0);
  91. skb = genlmsg_new(size, GFP_ATOMIC);
  92. if (!skb)
  93. return -ENOMEM;
  94. /* add the genetlink message header */
  95. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  96. &acpi_event_genl_family, 0,
  97. ACPI_GENL_CMD_EVENT);
  98. if (!msg_header) {
  99. nlmsg_free(skb);
  100. return -ENOMEM;
  101. }
  102. /* fill the data */
  103. attr =
  104. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  105. sizeof(struct acpi_genl_event));
  106. if (!attr) {
  107. nlmsg_free(skb);
  108. return -EINVAL;
  109. }
  110. event = nla_data(attr);
  111. memset(event, 0, sizeof(struct acpi_genl_event));
  112. strcpy(event->device_class, device_class);
  113. strcpy(event->bus_id, bus_id);
  114. event->type = type;
  115. event->data = data;
  116. /* send multicast genetlink message */
  117. genlmsg_end(skb, msg_header);
  118. genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  122. static int acpi_event_genetlink_init(void)
  123. {
  124. return genl_register_family(&acpi_event_genl_family);
  125. }
  126. #else
  127. int acpi_bus_generate_netlink_event(const char *device_class,
  128. const char *bus_id,
  129. u8 type, int data)
  130. {
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  134. static int acpi_event_genetlink_init(void)
  135. {
  136. return -ENODEV;
  137. }
  138. #endif
  139. static int __init acpi_event_init(void)
  140. {
  141. int error = 0;
  142. if (acpi_disabled)
  143. return 0;
  144. /* create genetlink for acpi event */
  145. error = acpi_event_genetlink_init();
  146. if (error)
  147. printk(KERN_WARNING PREFIX
  148. "Failed to create genetlink family for ACPI event\n");
  149. return 0;
  150. }
  151. fs_initcall(acpi_event_init);