2 * event.c - exporting ACPI events via procfs
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 #include <linux/spinlock.h>
10 #include <linux/proc_fs.h>
11 #include <linux/init.h>
12 #include <linux/poll.h>
13 #include <acpi/acpi_drivers.h>
14 #include <net/netlink.h>
15 #include <net/genetlink.h>
19 #define _COMPONENT ACPI_SYSTEM_COMPONENT
20 ACPI_MODULE_NAME("event");
22 #ifdef CONFIG_ACPI_PROC_EVENT
23 /* Global vars for handling event proc entry */
24 static DEFINE_SPINLOCK(acpi_system_event_lock);
25 int event_is_open = 0;
26 extern struct list_head acpi_bus_event_list;
27 extern wait_queue_head_t acpi_bus_event_queue;
29 static int acpi_system_open_event(struct inode *inode, struct file *file)
31 spin_lock_irq(&acpi_system_event_lock);
38 spin_unlock_irq(&acpi_system_event_lock);
42 spin_unlock_irq(&acpi_system_event_lock);
47 acpi_system_read_event(struct file *file, char __user * buffer, size_t count,
51 struct acpi_bus_event event;
52 static char str[ACPI_MAX_STRING];
53 static int chars_remaining = 0;
56 if (!chars_remaining) {
57 memset(&event, 0, sizeof(struct acpi_bus_event));
59 if ((file->f_flags & O_NONBLOCK)
60 && (list_empty(&acpi_bus_event_list)))
63 result = acpi_bus_receive_event(&event);
67 chars_remaining = sprintf(str, "%s %s %08x %08x\n",
68 event.device_class ? event.
69 device_class : "<unknown>",
71 bus_id : "<unknown>", event.type,
76 if (chars_remaining < count) {
77 count = chars_remaining;
80 if (copy_to_user(buffer, ptr, count))
84 chars_remaining -= count;
90 static int acpi_system_close_event(struct inode *inode, struct file *file)
92 spin_lock_irq(&acpi_system_event_lock);
94 spin_unlock_irq(&acpi_system_event_lock);
98 static unsigned int acpi_system_poll_event(struct file *file, poll_table * wait)
100 poll_wait(file, &acpi_bus_event_queue, wait);
101 if (!list_empty(&acpi_bus_event_list))
102 return POLLIN | POLLRDNORM;
106 static const struct file_operations acpi_system_event_ops = {
107 .owner = THIS_MODULE,
108 .open = acpi_system_open_event,
109 .read = acpi_system_read_event,
110 .release = acpi_system_close_event,
111 .poll = acpi_system_poll_event,
113 #endif /* CONFIG_ACPI_PROC_EVENT */
115 /* ACPI notifier chain */
116 static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
118 int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
120 struct acpi_bus_event event;
122 strcpy(event.device_class, dev->pnp.device_class);
123 strcpy(event.bus_id, dev->pnp.bus_id);
126 return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
127 == NOTIFY_BAD) ? -EINVAL : 0;
129 EXPORT_SYMBOL(acpi_notifier_call_chain);
131 int register_acpi_notifier(struct notifier_block *nb)
133 return blocking_notifier_chain_register(&acpi_chain_head, nb);
135 EXPORT_SYMBOL(register_acpi_notifier);
137 int unregister_acpi_notifier(struct notifier_block *nb)
139 return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
141 EXPORT_SYMBOL(unregister_acpi_notifier);
144 static unsigned int acpi_event_seqnum;
145 struct acpi_genl_event {
146 acpi_device_class device_class;
152 /* attributes of acpi_genl_family */
154 ACPI_GENL_ATTR_UNSPEC,
155 ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
156 __ACPI_GENL_ATTR_MAX,
158 #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
160 /* commands supported by the acpi_genl_family */
162 ACPI_GENL_CMD_UNSPEC,
163 ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
166 #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
168 #define ACPI_GENL_FAMILY_NAME "acpi_event"
169 #define ACPI_GENL_VERSION 0x01
170 #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
172 static struct genl_family acpi_event_genl_family = {
173 .id = GENL_ID_GENERATE,
174 .name = ACPI_GENL_FAMILY_NAME,
175 .version = ACPI_GENL_VERSION,
176 .maxattr = ACPI_GENL_ATTR_MAX,
179 static struct genl_multicast_group acpi_event_mcgrp = {
180 .name = ACPI_GENL_MCAST_GROUP_NAME,
183 int acpi_bus_generate_netlink_event(const char *device_class,
189 struct acpi_genl_event *event;
194 /* allocate memory */
195 size = nla_total_size(sizeof(struct acpi_genl_event)) +
198 skb = genlmsg_new(size, GFP_ATOMIC);
202 /* add the genetlink message header */
203 msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
204 &acpi_event_genl_family, 0,
205 ACPI_GENL_CMD_EVENT);
213 nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
214 sizeof(struct acpi_genl_event));
220 event = nla_data(attr);
226 memset(event, 0, sizeof(struct acpi_genl_event));
228 strcpy(event->device_class, device_class);
229 strcpy(event->bus_id, bus_id);
233 /* send multicast genetlink message */
234 result = genlmsg_end(skb, msg_header);
240 genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
244 EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
246 static int acpi_event_genetlink_init(void)
250 result = genl_register_family(&acpi_event_genl_family);
254 result = genl_register_mc_group(&acpi_event_genl_family,
257 genl_unregister_family(&acpi_event_genl_family);
263 int acpi_bus_generate_netlink_event(const char *device_class,
270 EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
272 static int acpi_event_genetlink_init(void)
278 static int __init acpi_event_init(void)
280 #ifdef CONFIG_ACPI_PROC_EVENT
281 struct proc_dir_entry *entry;
288 /* create genetlink for acpi event */
289 error = acpi_event_genetlink_init();
291 printk(KERN_WARNING PREFIX
292 "Failed to create genetlink family for ACPI event\n");
294 #ifdef CONFIG_ACPI_PROC_EVENT
296 entry = proc_create("event", S_IRUSR, acpi_root_dir,
297 &acpi_system_event_ops);
305 fs_initcall(acpi_event_init);