Linux Audio

Check our new training course

Loading...
v5.9
  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
 
 
 10#include <linux/spinlock.h>
 11#include <linux/export.h>
 12#include <linux/proc_fs.h>
 13#include <linux/init.h>
 14#include <linux/poll.h>
 15#include <linux/gfp.h>
 16#include <linux/acpi.h>
 17#include <net/netlink.h>
 18#include <net/genetlink.h>
 19
 20#include "internal.h"
 21
 22#define _COMPONENT		ACPI_SYSTEM_COMPONENT
 23ACPI_MODULE_NAME("event");
 24
 25/* ACPI notifier chain */
 26static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
 27
 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
 53#ifdef CONFIG_NET
 54static unsigned int acpi_event_seqnum;
 55struct 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
 78#define ACPI_GENL_FAMILY_NAME		"acpi_event"
 79#define ACPI_GENL_VERSION		0x01
 80#define ACPI_GENL_MCAST_GROUP_NAME 	"acpi_mc_group"
 81
 82static const struct genl_multicast_group acpi_event_mcgrps[] = {
 83	{ .name = ACPI_GENL_MCAST_GROUP_NAME, },
 84};
 85
 86static struct genl_family acpi_event_genl_family __ro_after_init = {
 87	.module = THIS_MODULE,
 88	.name = ACPI_GENL_FAMILY_NAME,
 89	.version = ACPI_GENL_VERSION,
 90	.maxattr = ACPI_GENL_ATTR_MAX,
 91	.mcgrps = acpi_event_mcgrps,
 92	.n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
 93};
 94
 95int acpi_bus_generate_netlink_event(const char *device_class,
 96				      const char *bus_id,
 97				      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;
104
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);
132	memset(event, 0, sizeof(struct acpi_genl_event));
133
134	strscpy(event->device_class, device_class, sizeof(event->device_class));
135	strscpy(event->bus_id, bus_id, sizeof(event->bus_id));
136	event->type = type;
137	event->data = data;
138
139	/* send multicast genetlink message */
140	genlmsg_end(skb, msg_header);
141
142	genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
143	return 0;
144}
145
146EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
147
148static int __init acpi_event_genetlink_init(void)
149{
150	return genl_register_family(&acpi_event_genl_family);
151}
152
153#else
154int acpi_bus_generate_netlink_event(const char *device_class,
155				      const char *bus_id,
156				      u8 type, int data)
157{
158	return 0;
159}
160
161EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
162
163static int acpi_event_genetlink_init(void)
164{
165	return -ENODEV;
166}
167#endif
168
169static int __init acpi_event_init(void)
170{
171	int error = 0;
172
173	if (acpi_disabled)
174		return 0;
175
176	/* 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");
181	return 0;
182}
183
184fs_initcall(acpi_event_init);
v6.8
  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
 10#define pr_fmt(fmt) "ACPI: " fmt
 11
 12#include <linux/spinlock.h>
 13#include <linux/export.h>
 14#include <linux/proc_fs.h>
 15#include <linux/init.h>
 16#include <linux/poll.h>
 17#include <linux/gfp.h>
 18#include <linux/acpi.h>
 19#include <net/netlink.h>
 20#include <net/genetlink.h>
 21
 22#include "internal.h"
 23
 
 
 
 24/* ACPI notifier chain */
 25static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
 26
 27int 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}
 38EXPORT_SYMBOL(acpi_notifier_call_chain);
 39
 40int register_acpi_notifier(struct notifier_block *nb)
 41{
 42	return blocking_notifier_chain_register(&acpi_chain_head, nb);
 43}
 44EXPORT_SYMBOL(register_acpi_notifier);
 45
 46int unregister_acpi_notifier(struct notifier_block *nb)
 47{
 48	return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
 49}
 50EXPORT_SYMBOL(unregister_acpi_notifier);
 51
 52#ifdef CONFIG_NET
 53static unsigned int acpi_event_seqnum;
 54struct 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 */
 62enum {
 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 */
 70enum {
 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
 77#define ACPI_GENL_FAMILY_NAME		"acpi_event"
 78#define ACPI_GENL_VERSION		0x01
 79#define ACPI_GENL_MCAST_GROUP_NAME 	"acpi_mc_group"
 80
 81static const struct genl_multicast_group acpi_event_mcgrps[] = {
 82	{ .name = ACPI_GENL_MCAST_GROUP_NAME, },
 83};
 84
 85static struct genl_family acpi_event_genl_family __ro_after_init = {
 86	.module = THIS_MODULE,
 87	.name = ACPI_GENL_FAMILY_NAME,
 88	.version = ACPI_GENL_VERSION,
 89	.maxattr = ACPI_GENL_ATTR_MAX,
 90	.mcgrps = acpi_event_mcgrps,
 91	.n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
 92};
 93
 94int acpi_bus_generate_netlink_event(const char *device_class,
 95				      const char *bus_id,
 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;
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);
131	memset(event, 0, sizeof(struct acpi_genl_event));
132
133	strscpy(event->device_class, device_class, sizeof(event->device_class));
134	strscpy(event->bus_id, bus_id, sizeof(event->bus_id));
135	event->type = type;
136	event->data = data;
137
138	/* send multicast genetlink message */
139	genlmsg_end(skb, msg_header);
140
141	genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
142	return 0;
143}
144
145EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
146
147static int __init acpi_event_genetlink_init(void)
148{
149	return genl_register_family(&acpi_event_genl_family);
150}
151
152#else
153int acpi_bus_generate_netlink_event(const char *device_class,
154				      const char *bus_id,
155				      u8 type, int data)
156{
157	return 0;
158}
159
160EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
161
162static int acpi_event_genetlink_init(void)
163{
164	return -ENODEV;
165}
166#endif
167
168static int __init acpi_event_init(void)
169{
170	int error;
171
172	if (acpi_disabled)
173		return 0;
174
175	/* create genetlink for acpi event */
176	error = acpi_event_genetlink_init();
177	if (error)
178		pr_warn("Failed to create genetlink family for ACPI event\n");
179
180	return 0;
181}
182
183fs_initcall(acpi_event_init);