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 <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. .open = acpi_system_open_event,
  86. .read = acpi_system_read_event,
  87. .release = acpi_system_close_event,
  88. .poll = acpi_system_poll_event,
  89. };
  90. #endif /* CONFIG_ACPI_PROC_EVENT */
  91. /* ACPI notifier chain */
  92. static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
  93. int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
  94. {
  95. struct acpi_bus_event event;
  96. strcpy(event.device_class, dev->pnp.device_class);
  97. strcpy(event.bus_id, dev->pnp.bus_id);
  98. event.type = type;
  99. event.data = data;
  100. return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
  101. == NOTIFY_BAD) ? -EINVAL : 0;
  102. }
  103. EXPORT_SYMBOL(acpi_notifier_call_chain);
  104. int register_acpi_notifier(struct notifier_block *nb)
  105. {
  106. return blocking_notifier_chain_register(&acpi_chain_head, nb);
  107. }
  108. EXPORT_SYMBOL(register_acpi_notifier);
  109. int unregister_acpi_notifier(struct notifier_block *nb)
  110. {
  111. return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
  112. }
  113. EXPORT_SYMBOL(unregister_acpi_notifier);
  114. #ifdef CONFIG_NET
  115. static unsigned int acpi_event_seqnum;
  116. struct acpi_genl_event {
  117. acpi_device_class device_class;
  118. char bus_id[15];
  119. u32 type;
  120. u32 data;
  121. };
  122. /* attributes of acpi_genl_family */
  123. enum {
  124. ACPI_GENL_ATTR_UNSPEC,
  125. ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
  126. __ACPI_GENL_ATTR_MAX,
  127. };
  128. #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
  129. /* commands supported by the acpi_genl_family */
  130. enum {
  131. ACPI_GENL_CMD_UNSPEC,
  132. ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
  133. __ACPI_GENL_CMD_MAX,
  134. };
  135. #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
  136. #define ACPI_GENL_FAMILY_NAME "acpi_event"
  137. #define ACPI_GENL_VERSION 0x01
  138. #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
  139. static struct genl_family acpi_event_genl_family = {
  140. .id = GENL_ID_GENERATE,
  141. .name = ACPI_GENL_FAMILY_NAME,
  142. .version = ACPI_GENL_VERSION,
  143. .maxattr = ACPI_GENL_ATTR_MAX,
  144. };
  145. static struct genl_multicast_group acpi_event_mcgrp = {
  146. .name = ACPI_GENL_MCAST_GROUP_NAME,
  147. };
  148. int acpi_bus_generate_netlink_event(const char *device_class,
  149. const char *bus_id,
  150. u8 type, int data)
  151. {
  152. struct sk_buff *skb;
  153. struct nlattr *attr;
  154. struct acpi_genl_event *event;
  155. void *msg_header;
  156. int size;
  157. int result;
  158. /* allocate memory */
  159. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  160. nla_total_size(0);
  161. skb = genlmsg_new(size, GFP_ATOMIC);
  162. if (!skb)
  163. return -ENOMEM;
  164. /* add the genetlink message header */
  165. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  166. &acpi_event_genl_family, 0,
  167. ACPI_GENL_CMD_EVENT);
  168. if (!msg_header) {
  169. nlmsg_free(skb);
  170. return -ENOMEM;
  171. }
  172. /* fill the data */
  173. attr =
  174. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  175. sizeof(struct acpi_genl_event));
  176. if (!attr) {
  177. nlmsg_free(skb);
  178. return -EINVAL;
  179. }
  180. event = nla_data(attr);
  181. if (!event) {
  182. nlmsg_free(skb);
  183. return -EINVAL;
  184. }
  185. memset(event, 0, sizeof(struct acpi_genl_event));
  186. strcpy(event->device_class, device_class);
  187. strcpy(event->bus_id, bus_id);
  188. event->type = type;
  189. event->data = data;
  190. /* send multicast genetlink message */
  191. result = genlmsg_end(skb, msg_header);
  192. if (result < 0) {
  193. nlmsg_free(skb);
  194. return result;
  195. }
  196. result =
  197. genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
  198. if (result)
  199. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  200. "Failed to send a Genetlink message!\n"));
  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 = create_proc_entry("event", S_IRUSR, acpi_root_dir);
  245. if (entry)
  246. entry->proc_fops = &acpi_system_event_ops;
  247. else
  248. return -ENODEV;
  249. #endif
  250. return 0;
  251. }
  252. fs_initcall(acpi_event_init);