event.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include <linux/spinlock.h>
  9. #include <linux/export.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/init.h>
  12. #include <linux/poll.h>
  13. #include <linux/gfp.h>
  14. #include <acpi/acpi_drivers.h>
  15. #include <net/netlink.h>
  16. #include <net/genetlink.h>
  17. #include "internal.h"
  18. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  19. ACPI_MODULE_NAME("event");
  20. #ifdef CONFIG_ACPI_PROC_EVENT
  21. /* Global vars for handling event proc entry */
  22. static DEFINE_SPINLOCK(acpi_system_event_lock);
  23. int event_is_open = 0;
  24. extern struct list_head acpi_bus_event_list;
  25. extern wait_queue_head_t acpi_bus_event_queue;
  26. static int acpi_system_open_event(struct inode *inode, struct file *file)
  27. {
  28. spin_lock_irq(&acpi_system_event_lock);
  29. if (event_is_open)
  30. goto out_busy;
  31. event_is_open = 1;
  32. spin_unlock_irq(&acpi_system_event_lock);
  33. return 0;
  34. out_busy:
  35. spin_unlock_irq(&acpi_system_event_lock);
  36. return -EBUSY;
  37. }
  38. static ssize_t
  39. acpi_system_read_event(struct file *file, char __user * buffer, size_t count,
  40. loff_t * ppos)
  41. {
  42. int result = 0;
  43. struct acpi_bus_event event;
  44. static char str[ACPI_MAX_STRING];
  45. static int chars_remaining = 0;
  46. static char *ptr;
  47. if (!chars_remaining) {
  48. memset(&event, 0, sizeof(struct acpi_bus_event));
  49. if ((file->f_flags & O_NONBLOCK)
  50. && (list_empty(&acpi_bus_event_list)))
  51. return -EAGAIN;
  52. result = acpi_bus_receive_event(&event);
  53. if (result)
  54. return result;
  55. chars_remaining = sprintf(str, "%s %s %08x %08x\n",
  56. event.device_class ? event.
  57. device_class : "<unknown>",
  58. event.bus_id ? event.
  59. bus_id : "<unknown>", event.type,
  60. event.data);
  61. ptr = str;
  62. }
  63. if (chars_remaining < count) {
  64. count = chars_remaining;
  65. }
  66. if (copy_to_user(buffer, ptr, count))
  67. return -EFAULT;
  68. *ppos += count;
  69. chars_remaining -= count;
  70. ptr += count;
  71. return count;
  72. }
  73. static int acpi_system_close_event(struct inode *inode, struct file *file)
  74. {
  75. spin_lock_irq(&acpi_system_event_lock);
  76. event_is_open = 0;
  77. spin_unlock_irq(&acpi_system_event_lock);
  78. return 0;
  79. }
  80. static unsigned int acpi_system_poll_event(struct file *file, poll_table * wait)
  81. {
  82. poll_wait(file, &acpi_bus_event_queue, wait);
  83. if (!list_empty(&acpi_bus_event_list))
  84. return POLLIN | POLLRDNORM;
  85. return 0;
  86. }
  87. static const struct file_operations acpi_system_event_ops = {
  88. .owner = THIS_MODULE,
  89. .open = acpi_system_open_event,
  90. .read = acpi_system_read_event,
  91. .release = acpi_system_close_event,
  92. .poll = acpi_system_poll_event,
  93. .llseek = default_llseek,
  94. };
  95. #endif /* CONFIG_ACPI_PROC_EVENT */
  96. /* ACPI notifier chain */
  97. static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
  98. int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
  99. {
  100. struct acpi_bus_event event;
  101. strcpy(event.device_class, dev->pnp.device_class);
  102. strcpy(event.bus_id, dev->pnp.bus_id);
  103. event.type = type;
  104. event.data = data;
  105. return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
  106. == NOTIFY_BAD) ? -EINVAL : 0;
  107. }
  108. EXPORT_SYMBOL(acpi_notifier_call_chain);
  109. int register_acpi_notifier(struct notifier_block *nb)
  110. {
  111. return blocking_notifier_chain_register(&acpi_chain_head, nb);
  112. }
  113. EXPORT_SYMBOL(register_acpi_notifier);
  114. int unregister_acpi_notifier(struct notifier_block *nb)
  115. {
  116. return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
  117. }
  118. EXPORT_SYMBOL(unregister_acpi_notifier);
  119. #ifdef CONFIG_NET
  120. static unsigned int acpi_event_seqnum;
  121. struct acpi_genl_event {
  122. acpi_device_class device_class;
  123. char bus_id[15];
  124. u32 type;
  125. u32 data;
  126. };
  127. /* attributes of acpi_genl_family */
  128. enum {
  129. ACPI_GENL_ATTR_UNSPEC,
  130. ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
  131. __ACPI_GENL_ATTR_MAX,
  132. };
  133. #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
  134. /* commands supported by the acpi_genl_family */
  135. enum {
  136. ACPI_GENL_CMD_UNSPEC,
  137. ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
  138. __ACPI_GENL_CMD_MAX,
  139. };
  140. #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
  141. #define ACPI_GENL_FAMILY_NAME "acpi_event"
  142. #define ACPI_GENL_VERSION 0x01
  143. #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
  144. static struct genl_family acpi_event_genl_family = {
  145. .id = GENL_ID_GENERATE,
  146. .name = ACPI_GENL_FAMILY_NAME,
  147. .version = ACPI_GENL_VERSION,
  148. .maxattr = ACPI_GENL_ATTR_MAX,
  149. };
  150. static struct genl_multicast_group acpi_event_mcgrp = {
  151. .name = ACPI_GENL_MCAST_GROUP_NAME,
  152. };
  153. int acpi_bus_generate_netlink_event(const char *device_class,
  154. const char *bus_id,
  155. u8 type, int data)
  156. {
  157. struct sk_buff *skb;
  158. struct nlattr *attr;
  159. struct acpi_genl_event *event;
  160. void *msg_header;
  161. int size;
  162. int result;
  163. /* allocate memory */
  164. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  165. nla_total_size(0);
  166. skb = genlmsg_new(size, GFP_ATOMIC);
  167. if (!skb)
  168. return -ENOMEM;
  169. /* add the genetlink message header */
  170. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  171. &acpi_event_genl_family, 0,
  172. ACPI_GENL_CMD_EVENT);
  173. if (!msg_header) {
  174. nlmsg_free(skb);
  175. return -ENOMEM;
  176. }
  177. /* fill the data */
  178. attr =
  179. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  180. sizeof(struct acpi_genl_event));
  181. if (!attr) {
  182. nlmsg_free(skb);
  183. return -EINVAL;
  184. }
  185. event = nla_data(attr);
  186. if (!event) {
  187. nlmsg_free(skb);
  188. return -EINVAL;
  189. }
  190. memset(event, 0, sizeof(struct acpi_genl_event));
  191. strcpy(event->device_class, device_class);
  192. strcpy(event->bus_id, bus_id);
  193. event->type = type;
  194. event->data = data;
  195. /* send multicast genetlink message */
  196. result = genlmsg_end(skb, msg_header);
  197. if (result < 0) {
  198. nlmsg_free(skb);
  199. return result;
  200. }
  201. genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  205. static int acpi_event_genetlink_init(void)
  206. {
  207. int result;
  208. result = genl_register_family(&acpi_event_genl_family);
  209. if (result)
  210. return result;
  211. result = genl_register_mc_group(&acpi_event_genl_family,
  212. &acpi_event_mcgrp);
  213. if (result)
  214. genl_unregister_family(&acpi_event_genl_family);
  215. return result;
  216. }
  217. #else
  218. int acpi_bus_generate_netlink_event(const char *device_class,
  219. const char *bus_id,
  220. u8 type, int data)
  221. {
  222. return 0;
  223. }
  224. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  225. static int acpi_event_genetlink_init(void)
  226. {
  227. return -ENODEV;
  228. }
  229. #endif
  230. static int __init acpi_event_init(void)
  231. {
  232. #ifdef CONFIG_ACPI_PROC_EVENT
  233. struct proc_dir_entry *entry;
  234. #endif
  235. int error = 0;
  236. if (acpi_disabled)
  237. return 0;
  238. /* create genetlink for acpi event */
  239. error = acpi_event_genetlink_init();
  240. if (error)
  241. printk(KERN_WARNING PREFIX
  242. "Failed to create genetlink family for ACPI event\n");
  243. #ifdef CONFIG_ACPI_PROC_EVENT
  244. /* 'event' [R] */
  245. entry = proc_create("event", S_IRUSR, acpi_root_dir,
  246. &acpi_system_event_ops);
  247. if (!entry)
  248. return -ENODEV;
  249. #endif
  250. return 0;
  251. }
  252. fs_initcall(acpi_event_init);