event.c 6.8 KB

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