event.c 6.7 KB

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