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