event.c 4.3 KB

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