event.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /* ACPI notifier chain */
  20. static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
  21. int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
  22. {
  23. struct acpi_bus_event event;
  24. strcpy(event.device_class, dev->pnp.device_class);
  25. strcpy(event.bus_id, dev->pnp.bus_id);
  26. event.type = type;
  27. event.data = data;
  28. return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
  29. == NOTIFY_BAD) ? -EINVAL : 0;
  30. }
  31. EXPORT_SYMBOL(acpi_notifier_call_chain);
  32. int register_acpi_notifier(struct notifier_block *nb)
  33. {
  34. return blocking_notifier_chain_register(&acpi_chain_head, nb);
  35. }
  36. EXPORT_SYMBOL(register_acpi_notifier);
  37. int unregister_acpi_notifier(struct notifier_block *nb)
  38. {
  39. return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
  40. }
  41. EXPORT_SYMBOL(unregister_acpi_notifier);
  42. #ifdef CONFIG_NET
  43. static unsigned int acpi_event_seqnum;
  44. struct acpi_genl_event {
  45. acpi_device_class device_class;
  46. char bus_id[15];
  47. u32 type;
  48. u32 data;
  49. };
  50. /* attributes of acpi_genl_family */
  51. enum {
  52. ACPI_GENL_ATTR_UNSPEC,
  53. ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
  54. __ACPI_GENL_ATTR_MAX,
  55. };
  56. #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
  57. /* commands supported by the acpi_genl_family */
  58. enum {
  59. ACPI_GENL_CMD_UNSPEC,
  60. ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
  61. __ACPI_GENL_CMD_MAX,
  62. };
  63. #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
  64. #define ACPI_GENL_FAMILY_NAME "acpi_event"
  65. #define ACPI_GENL_VERSION 0x01
  66. #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
  67. static const struct genl_multicast_group acpi_event_mcgrps[] = {
  68. { .name = ACPI_GENL_MCAST_GROUP_NAME, },
  69. };
  70. static struct genl_family acpi_event_genl_family __ro_after_init = {
  71. .module = THIS_MODULE,
  72. .name = ACPI_GENL_FAMILY_NAME,
  73. .version = ACPI_GENL_VERSION,
  74. .maxattr = ACPI_GENL_ATTR_MAX,
  75. .mcgrps = acpi_event_mcgrps,
  76. .n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
  77. };
  78. int acpi_bus_generate_netlink_event(const char *device_class,
  79. const char *bus_id,
  80. u8 type, int data)
  81. {
  82. struct sk_buff *skb;
  83. struct nlattr *attr;
  84. struct acpi_genl_event *event;
  85. void *msg_header;
  86. int size;
  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. memset(event, 0, sizeof(struct acpi_genl_event));
  111. strscpy(event->device_class, device_class, sizeof(event->device_class));
  112. strscpy(event->bus_id, bus_id, sizeof(event->bus_id));
  113. event->type = type;
  114. event->data = data;
  115. /* send multicast genetlink message */
  116. genlmsg_end(skb, msg_header);
  117. genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  121. static int __init acpi_event_genetlink_init(void)
  122. {
  123. return genl_register_family(&acpi_event_genl_family);
  124. }
  125. #else
  126. int acpi_bus_generate_netlink_event(const char *device_class,
  127. const char *bus_id,
  128. u8 type, int data)
  129. {
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  133. static int acpi_event_genetlink_init(void)
  134. {
  135. return -ENODEV;
  136. }
  137. #endif
  138. static int __init acpi_event_init(void)
  139. {
  140. int error = 0;
  141. if (acpi_disabled)
  142. return 0;
  143. /* create genetlink for acpi event */
  144. error = acpi_event_genetlink_init();
  145. if (error)
  146. printk(KERN_WARNING PREFIX
  147. "Failed to create genetlink family for ACPI event\n");
  148. return 0;
  149. }
  150. fs_initcall(acpi_event_init);