blob: 47f21599f2ab59aabc05731a6e90a94f566a56eb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Gortmaker214f2c92011-10-26 16:22:14 -040011#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/proc_fs.h>
13#include <linux/init.h>
14#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Lv Zheng8b484632013-12-03 08:49:16 +080016#include <linux/acpi.h>
Zhang Rui864bdfb2007-06-19 11:40:03 +080017#include <net/netlink.h>
18#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Len Browna192a952009-07-28 16:45:54 -040020#include "internal.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050023ACPI_MODULE_NAME("event");
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Zhang Rui9ee85242008-01-25 14:48:06 +080025/* ACPI notifier chain */
Adrian Bunkc8e773f2008-02-13 23:29:57 +020026static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
Zhang Rui9ee85242008-01-25 14:48:06 +080027
28int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
29{
30 struct acpi_bus_event event;
31
32 strcpy(event.device_class, dev->pnp.device_class);
33 strcpy(event.bus_id, dev->pnp.bus_id);
34 event.type = type;
35 event.data = data;
36 return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
37 == NOTIFY_BAD) ? -EINVAL : 0;
38}
39EXPORT_SYMBOL(acpi_notifier_call_chain);
40
41int register_acpi_notifier(struct notifier_block *nb)
42{
43 return blocking_notifier_chain_register(&acpi_chain_head, nb);
44}
45EXPORT_SYMBOL(register_acpi_notifier);
46
47int unregister_acpi_notifier(struct notifier_block *nb)
48{
49 return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
50}
51EXPORT_SYMBOL(unregister_acpi_notifier);
52
Zhang Rui864bdfb2007-06-19 11:40:03 +080053#ifdef CONFIG_NET
Adrian Bunke13d8742007-08-10 13:45:18 -070054static unsigned int acpi_event_seqnum;
Zhang Rui864bdfb2007-06-19 11:40:03 +080055struct acpi_genl_event {
56 acpi_device_class device_class;
57 char bus_id[15];
58 u32 type;
59 u32 data;
60};
61
62/* attributes of acpi_genl_family */
63enum {
64 ACPI_GENL_ATTR_UNSPEC,
65 ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
66 __ACPI_GENL_ATTR_MAX,
67};
68#define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
69
70/* commands supported by the acpi_genl_family */
71enum {
72 ACPI_GENL_CMD_UNSPEC,
73 ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
74 __ACPI_GENL_CMD_MAX,
75};
76#define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
77
Zhang Rui9c977a42007-07-20 16:41:14 +080078#define ACPI_GENL_FAMILY_NAME "acpi_event"
79#define ACPI_GENL_VERSION 0x01
80#define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
Zhang Rui864bdfb2007-06-19 11:40:03 +080081
Johannes Berg2a94fe42013-11-19 15:19:39 +010082static const struct genl_multicast_group acpi_event_mcgrps[] = {
83 { .name = ACPI_GENL_MCAST_GROUP_NAME, },
84};
85
Johannes Berg56989f62016-10-24 14:40:05 +020086static struct genl_family acpi_event_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +020087 .module = THIS_MODULE,
Zhang Rui9c977a42007-07-20 16:41:14 +080088 .name = ACPI_GENL_FAMILY_NAME,
Zhang Rui864bdfb2007-06-19 11:40:03 +080089 .version = ACPI_GENL_VERSION,
90 .maxattr = ACPI_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +010091 .mcgrps = acpi_event_mcgrps,
92 .n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
Zhang Rui864bdfb2007-06-19 11:40:03 +080093};
94
Zhang Rui962ce8c2007-08-23 01:24:31 +080095int acpi_bus_generate_netlink_event(const char *device_class,
96 const char *bus_id,
Zhang Rui864bdfb2007-06-19 11:40:03 +080097 u8 type, int data)
98{
99 struct sk_buff *skb;
100 struct nlattr *attr;
101 struct acpi_genl_event *event;
102 void *msg_header;
103 int size;
Zhang Rui864bdfb2007-06-19 11:40:03 +0800104
105 /* allocate memory */
106 size = nla_total_size(sizeof(struct acpi_genl_event)) +
107 nla_total_size(0);
108
109 skb = genlmsg_new(size, GFP_ATOMIC);
110 if (!skb)
111 return -ENOMEM;
112
113 /* add the genetlink message header */
114 msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
115 &acpi_event_genl_family, 0,
116 ACPI_GENL_CMD_EVENT);
117 if (!msg_header) {
118 nlmsg_free(skb);
119 return -ENOMEM;
120 }
121
122 /* fill the data */
123 attr =
124 nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
125 sizeof(struct acpi_genl_event));
126 if (!attr) {
127 nlmsg_free(skb);
128 return -EINVAL;
129 }
130
131 event = nla_data(attr);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800132 memset(event, 0, sizeof(struct acpi_genl_event));
133
Gustavo A. R. Silvac7d5f212019-04-22 11:39:34 -0500134 strscpy(event->device_class, device_class, sizeof(event->device_class));
135 strscpy(event->bus_id, bus_id, sizeof(event->bus_id));
Zhang Rui864bdfb2007-06-19 11:40:03 +0800136 event->type = type;
137 event->data = data;
138
139 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +0100140 genlmsg_end(skb, msg_header);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800141
Johannes Berg2a94fe42013-11-19 15:19:39 +0100142 genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800143 return 0;
144}
Zhang Rui864bdfb2007-06-19 11:40:03 +0800145
Zhang Rui962ce8c2007-08-23 01:24:31 +0800146EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
147
Johannes Berg56989f62016-10-24 14:40:05 +0200148static int __init acpi_event_genetlink_init(void)
Zhang Rui864bdfb2007-06-19 11:40:03 +0800149{
Johannes Berg2a94fe42013-11-19 15:19:39 +0100150 return genl_register_family(&acpi_event_genl_family);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800151}
152
153#else
Len Brown3e069ee2007-08-24 03:06:33 -0400154int acpi_bus_generate_netlink_event(const char *device_class,
155 const char *bus_id,
156 u8 type, int data)
Zhang Rui864bdfb2007-06-19 11:40:03 +0800157{
158 return 0;
159}
Zhang Rui864bdfb2007-06-19 11:40:03 +0800160
Henrique de Moraes Holschuh66baf322007-09-03 00:03:35 -0300161EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800162
Zhang Rui864bdfb2007-06-19 11:40:03 +0800163static int acpi_event_genetlink_init(void)
164{
165 return -ENODEV;
166}
167#endif
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int __init acpi_event_init(void)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int error = 0;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Zhang Rui864bdfb2007-06-19 11:40:03 +0800176 /* create genetlink for acpi event */
177 error = acpi_event_genetlink_init();
178 if (error)
179 printk(KERN_WARNING PREFIX
180 "Failed to create genetlink family for ACPI event\n");
Zhang Rui864bdfb2007-06-19 11:40:03 +0800181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Zhang Rui864bdfb2007-06-19 11:40:03 +0800184fs_initcall(acpi_event_init);