blob: 8247fcdde0795b37377bdec44d6271ca16ad225d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Gortmaker214f2c92011-10-26 16:22:14 -040010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/proc_fs.h>
12#include <linux/init.h>
13#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <acpi/acpi_drivers.h>
Zhang Rui864bdfb2007-06-19 11:40:03 +080016#include <net/netlink.h>
17#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Len Browna192a952009-07-28 16:45:54 -040019#include "internal.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050022ACPI_MODULE_NAME("event");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Zhang Rui9ee85242008-01-25 14:48:06 +080024/* ACPI notifier chain */
Adrian Bunkc8e773f2008-02-13 23:29:57 +020025static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
Zhang Rui9ee85242008-01-25 14:48:06 +080026
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
Zhang Rui864bdfb2007-06-19 11:40:03 +080052#ifdef CONFIG_NET
Adrian Bunke13d8742007-08-10 13:45:18 -070053static unsigned int acpi_event_seqnum;
Zhang Rui864bdfb2007-06-19 11:40:03 +080054struct 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
Zhang Rui9c977a42007-07-20 16:41:14 +080077#define ACPI_GENL_FAMILY_NAME "acpi_event"
78#define ACPI_GENL_VERSION 0x01
79#define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
Zhang Rui864bdfb2007-06-19 11:40:03 +080080
81static struct genl_family acpi_event_genl_family = {
82 .id = GENL_ID_GENERATE,
Zhang Rui9c977a42007-07-20 16:41:14 +080083 .name = ACPI_GENL_FAMILY_NAME,
Zhang Rui864bdfb2007-06-19 11:40:03 +080084 .version = ACPI_GENL_VERSION,
85 .maxattr = ACPI_GENL_ATTR_MAX,
86};
87
Zhang Rui9c977a42007-07-20 16:41:14 +080088static struct genl_multicast_group acpi_event_mcgrp = {
89 .name = ACPI_GENL_MCAST_GROUP_NAME,
Zhang Rui864bdfb2007-06-19 11:40:03 +080090};
91
Zhang Rui962ce8c2007-08-23 01:24:31 +080092int acpi_bus_generate_netlink_event(const char *device_class,
93 const char *bus_id,
Zhang Rui864bdfb2007-06-19 11:40:03 +080094 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;
101 int result;
102
103 /* allocate memory */
104 size = nla_total_size(sizeof(struct acpi_genl_event)) +
105 nla_total_size(0);
106
107 skb = genlmsg_new(size, GFP_ATOMIC);
108 if (!skb)
109 return -ENOMEM;
110
111 /* add the genetlink message header */
112 msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
113 &acpi_event_genl_family, 0,
114 ACPI_GENL_CMD_EVENT);
115 if (!msg_header) {
116 nlmsg_free(skb);
117 return -ENOMEM;
118 }
119
120 /* fill the data */
121 attr =
122 nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
123 sizeof(struct acpi_genl_event));
124 if (!attr) {
125 nlmsg_free(skb);
126 return -EINVAL;
127 }
128
129 event = nla_data(attr);
130 if (!event) {
131 nlmsg_free(skb);
132 return -EINVAL;
133 }
134
135 memset(event, 0, sizeof(struct acpi_genl_event));
136
Zhang Rui962ce8c2007-08-23 01:24:31 +0800137 strcpy(event->device_class, device_class);
138 strcpy(event->bus_id, bus_id);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800139 event->type = type;
140 event->data = data;
141
142 /* send multicast genetlink message */
143 result = genlmsg_end(skb, msg_header);
144 if (result < 0) {
145 nlmsg_free(skb);
146 return result;
147 }
148
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800149 genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800150 return 0;
151}
Zhang Rui864bdfb2007-06-19 11:40:03 +0800152
Zhang Rui962ce8c2007-08-23 01:24:31 +0800153EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
154
Zhang Rui864bdfb2007-06-19 11:40:03 +0800155static int acpi_event_genetlink_init(void)
156{
157 int result;
158
159 result = genl_register_family(&acpi_event_genl_family);
160 if (result)
161 return result;
162
Zhang Rui9c977a42007-07-20 16:41:14 +0800163 result = genl_register_mc_group(&acpi_event_genl_family,
164 &acpi_event_mcgrp);
Zhang Rui864bdfb2007-06-19 11:40:03 +0800165 if (result)
166 genl_unregister_family(&acpi_event_genl_family);
167
168 return result;
169}
170
171#else
Len Brown3e069ee2007-08-24 03:06:33 -0400172int acpi_bus_generate_netlink_event(const char *device_class,
173 const char *bus_id,
174 u8 type, int data)
Zhang Rui864bdfb2007-06-19 11:40:03 +0800175{
176 return 0;
177}
Zhang Rui864bdfb2007-06-19 11:40:03 +0800178
Henrique de Moraes Holschuh66baf322007-09-03 00:03:35 -0300179EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800180
Zhang Rui864bdfb2007-06-19 11:40:03 +0800181static int acpi_event_genetlink_init(void)
182{
183 return -ENODEV;
184}
185#endif
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int __init acpi_event_init(void)
188{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int error = 0;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Zhang Rui864bdfb2007-06-19 11:40:03 +0800194 /* create genetlink for acpi event */
195 error = acpi_event_genetlink_init();
196 if (error)
197 printk(KERN_WARNING PREFIX
198 "Failed to create genetlink family for ACPI event\n");
Zhang Rui864bdfb2007-06-19 11:40:03 +0800199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Zhang Rui864bdfb2007-06-19 11:40:03 +0800202fs_initcall(acpi_event_init);