Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 10 | #include <linux/spinlock.h> |
Paul Gortmaker | 214f2c9 | 2011-10-26 16:22:14 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/proc_fs.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 16 | #include <linux/acpi.h> |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 17 | #include <net/netlink.h> |
| 18 | #include <net/genetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
Zhang Rui | 9ee8524 | 2008-01-25 14:48:06 +0800 | [diff] [blame] | 22 | /* ACPI notifier chain */ |
Adrian Bunk | c8e773f | 2008-02-13 23:29:57 +0200 | [diff] [blame] | 23 | static BLOCKING_NOTIFIER_HEAD(acpi_chain_head); |
Zhang Rui | 9ee8524 | 2008-01-25 14:48:06 +0800 | [diff] [blame] | 24 | |
| 25 | int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data) |
| 26 | { |
| 27 | struct acpi_bus_event event; |
| 28 | |
| 29 | strcpy(event.device_class, dev->pnp.device_class); |
| 30 | strcpy(event.bus_id, dev->pnp.bus_id); |
| 31 | event.type = type; |
| 32 | event.data = data; |
| 33 | return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event) |
Maximilian Luz | c6237b2 | 2020-11-05 03:06:00 +0100 | [diff] [blame] | 34 | == NOTIFY_BAD) ? -EINVAL : 0; |
Zhang Rui | 9ee8524 | 2008-01-25 14:48:06 +0800 | [diff] [blame] | 35 | } |
| 36 | EXPORT_SYMBOL(acpi_notifier_call_chain); |
| 37 | |
| 38 | int register_acpi_notifier(struct notifier_block *nb) |
| 39 | { |
| 40 | return blocking_notifier_chain_register(&acpi_chain_head, nb); |
| 41 | } |
| 42 | EXPORT_SYMBOL(register_acpi_notifier); |
| 43 | |
| 44 | int unregister_acpi_notifier(struct notifier_block *nb) |
| 45 | { |
| 46 | return blocking_notifier_chain_unregister(&acpi_chain_head, nb); |
| 47 | } |
| 48 | EXPORT_SYMBOL(unregister_acpi_notifier); |
| 49 | |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 50 | #ifdef CONFIG_NET |
Adrian Bunk | e13d874 | 2007-08-10 13:45:18 -0700 | [diff] [blame] | 51 | static unsigned int acpi_event_seqnum; |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 52 | struct acpi_genl_event { |
| 53 | acpi_device_class device_class; |
| 54 | char bus_id[15]; |
| 55 | u32 type; |
| 56 | u32 data; |
| 57 | }; |
| 58 | |
| 59 | /* attributes of acpi_genl_family */ |
| 60 | enum { |
| 61 | ACPI_GENL_ATTR_UNSPEC, |
| 62 | ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */ |
| 63 | __ACPI_GENL_ATTR_MAX, |
| 64 | }; |
| 65 | #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1) |
| 66 | |
| 67 | /* commands supported by the acpi_genl_family */ |
| 68 | enum { |
| 69 | ACPI_GENL_CMD_UNSPEC, |
| 70 | ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */ |
| 71 | __ACPI_GENL_CMD_MAX, |
| 72 | }; |
| 73 | #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1) |
| 74 | |
Zhang Rui | 9c977a4 | 2007-07-20 16:41:14 +0800 | [diff] [blame] | 75 | #define ACPI_GENL_FAMILY_NAME "acpi_event" |
| 76 | #define ACPI_GENL_VERSION 0x01 |
| 77 | #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group" |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 78 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 79 | static const struct genl_multicast_group acpi_event_mcgrps[] = { |
| 80 | { .name = ACPI_GENL_MCAST_GROUP_NAME, }, |
| 81 | }; |
| 82 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 83 | static struct genl_family acpi_event_genl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 84 | .module = THIS_MODULE, |
Zhang Rui | 9c977a4 | 2007-07-20 16:41:14 +0800 | [diff] [blame] | 85 | .name = ACPI_GENL_FAMILY_NAME, |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 86 | .version = ACPI_GENL_VERSION, |
| 87 | .maxattr = ACPI_GENL_ATTR_MAX, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 88 | .mcgrps = acpi_event_mcgrps, |
| 89 | .n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps), |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
Zhang Rui | 962ce8c | 2007-08-23 01:24:31 +0800 | [diff] [blame] | 92 | int acpi_bus_generate_netlink_event(const char *device_class, |
| 93 | const char *bus_id, |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 94 | u8 type, int data) |
| 95 | { |
| 96 | struct sk_buff *skb; |
| 97 | struct nlattr *attr; |
| 98 | struct acpi_genl_event *event; |
| 99 | void *msg_header; |
| 100 | int size; |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 101 | |
| 102 | /* allocate memory */ |
| 103 | size = nla_total_size(sizeof(struct acpi_genl_event)) + |
| 104 | nla_total_size(0); |
| 105 | |
| 106 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 107 | if (!skb) |
| 108 | return -ENOMEM; |
| 109 | |
| 110 | /* add the genetlink message header */ |
| 111 | msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++, |
| 112 | &acpi_event_genl_family, 0, |
| 113 | ACPI_GENL_CMD_EVENT); |
| 114 | if (!msg_header) { |
| 115 | nlmsg_free(skb); |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | /* fill the data */ |
| 120 | attr = |
| 121 | nla_reserve(skb, ACPI_GENL_ATTR_EVENT, |
| 122 | sizeof(struct acpi_genl_event)); |
| 123 | if (!attr) { |
| 124 | nlmsg_free(skb); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | event = nla_data(attr); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 129 | memset(event, 0, sizeof(struct acpi_genl_event)); |
| 130 | |
Gustavo A. R. Silva | c7d5f21 | 2019-04-22 11:39:34 -0500 | [diff] [blame] | 131 | strscpy(event->device_class, device_class, sizeof(event->device_class)); |
| 132 | strscpy(event->bus_id, bus_id, sizeof(event->bus_id)); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 133 | event->type = type; |
| 134 | event->data = data; |
| 135 | |
| 136 | /* send multicast genetlink message */ |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 137 | genlmsg_end(skb, msg_header); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 138 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 139 | genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 140 | return 0; |
| 141 | } |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 142 | |
Zhang Rui | 962ce8c | 2007-08-23 01:24:31 +0800 | [diff] [blame] | 143 | EXPORT_SYMBOL(acpi_bus_generate_netlink_event); |
| 144 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 145 | static int __init acpi_event_genetlink_init(void) |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 146 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 147 | return genl_register_family(&acpi_event_genl_family); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #else |
Len Brown | 3e069ee | 2007-08-24 03:06:33 -0400 | [diff] [blame] | 151 | int acpi_bus_generate_netlink_event(const char *device_class, |
| 152 | const char *bus_id, |
| 153 | u8 type, int data) |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 154 | { |
| 155 | return 0; |
| 156 | } |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 157 | |
Henrique de Moraes Holschuh | 66baf32 | 2007-09-03 00:03:35 -0300 | [diff] [blame] | 158 | EXPORT_SYMBOL(acpi_bus_generate_netlink_event); |
Zhang Rui | 962ce8c | 2007-08-23 01:24:31 +0800 | [diff] [blame] | 159 | |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 160 | static int acpi_event_genetlink_init(void) |
| 161 | { |
| 162 | return -ENODEV; |
| 163 | } |
| 164 | #endif |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static int __init acpi_event_init(void) |
| 167 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int error = 0; |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 173 | /* create genetlink for acpi event */ |
| 174 | error = acpi_event_genetlink_init(); |
| 175 | if (error) |
| 176 | printk(KERN_WARNING PREFIX |
| 177 | "Failed to create genetlink family for ACPI event\n"); |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Zhang Rui | 864bdfb | 2007-06-19 11:40:03 +0800 | [diff] [blame] | 181 | fs_initcall(acpi_event_init); |