event.c 6.8 KB

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