event.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #ifdef CONFIG_NET
  92. static unsigned int acpi_event_seqnum;
  93. struct acpi_genl_event {
  94. acpi_device_class device_class;
  95. char bus_id[15];
  96. u32 type;
  97. u32 data;
  98. };
  99. /* attributes of acpi_genl_family */
  100. enum {
  101. ACPI_GENL_ATTR_UNSPEC,
  102. ACPI_GENL_ATTR_EVENT, /* ACPI event info needed by user space */
  103. __ACPI_GENL_ATTR_MAX,
  104. };
  105. #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
  106. /* commands supported by the acpi_genl_family */
  107. enum {
  108. ACPI_GENL_CMD_UNSPEC,
  109. ACPI_GENL_CMD_EVENT, /* kernel->user notifications for ACPI events */
  110. __ACPI_GENL_CMD_MAX,
  111. };
  112. #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
  113. #define ACPI_GENL_FAMILY_NAME "acpi_event"
  114. #define ACPI_GENL_VERSION 0x01
  115. #define ACPI_GENL_MCAST_GROUP_NAME "acpi_mc_group"
  116. static struct genl_family acpi_event_genl_family = {
  117. .id = GENL_ID_GENERATE,
  118. .name = ACPI_GENL_FAMILY_NAME,
  119. .version = ACPI_GENL_VERSION,
  120. .maxattr = ACPI_GENL_ATTR_MAX,
  121. };
  122. static struct genl_multicast_group acpi_event_mcgrp = {
  123. .name = ACPI_GENL_MCAST_GROUP_NAME,
  124. };
  125. int acpi_bus_generate_netlink_event(const char *device_class,
  126. const char *bus_id,
  127. u8 type, int data)
  128. {
  129. struct sk_buff *skb;
  130. struct nlattr *attr;
  131. struct acpi_genl_event *event;
  132. void *msg_header;
  133. int size;
  134. int result;
  135. /* allocate memory */
  136. size = nla_total_size(sizeof(struct acpi_genl_event)) +
  137. nla_total_size(0);
  138. skb = genlmsg_new(size, GFP_ATOMIC);
  139. if (!skb)
  140. return -ENOMEM;
  141. /* add the genetlink message header */
  142. msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
  143. &acpi_event_genl_family, 0,
  144. ACPI_GENL_CMD_EVENT);
  145. if (!msg_header) {
  146. nlmsg_free(skb);
  147. return -ENOMEM;
  148. }
  149. /* fill the data */
  150. attr =
  151. nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
  152. sizeof(struct acpi_genl_event));
  153. if (!attr) {
  154. nlmsg_free(skb);
  155. return -EINVAL;
  156. }
  157. event = nla_data(attr);
  158. if (!event) {
  159. nlmsg_free(skb);
  160. return -EINVAL;
  161. }
  162. memset(event, 0, sizeof(struct acpi_genl_event));
  163. strcpy(event->device_class, device_class);
  164. strcpy(event->bus_id, bus_id);
  165. event->type = type;
  166. event->data = data;
  167. /* send multicast genetlink message */
  168. result = genlmsg_end(skb, msg_header);
  169. if (result < 0) {
  170. nlmsg_free(skb);
  171. return result;
  172. }
  173. result =
  174. genlmsg_multicast(skb, 0, acpi_event_mcgrp.id, GFP_ATOMIC);
  175. if (result)
  176. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  177. "Failed to send a Genetlink message!\n"));
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  181. static int acpi_event_genetlink_init(void)
  182. {
  183. int result;
  184. result = genl_register_family(&acpi_event_genl_family);
  185. if (result)
  186. return result;
  187. result = genl_register_mc_group(&acpi_event_genl_family,
  188. &acpi_event_mcgrp);
  189. if (result)
  190. genl_unregister_family(&acpi_event_genl_family);
  191. return result;
  192. }
  193. #else
  194. int acpi_bus_generate_netlink_event(const char *device_class,
  195. const char *bus_id,
  196. u8 type, int data)
  197. {
  198. return 0;
  199. }
  200. EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
  201. static int acpi_event_genetlink_init(void)
  202. {
  203. return -ENODEV;
  204. }
  205. #endif
  206. static int __init acpi_event_init(void)
  207. {
  208. #ifdef CONFIG_ACPI_PROC_EVENT
  209. struct proc_dir_entry *entry;
  210. #endif
  211. int error = 0;
  212. if (acpi_disabled)
  213. return 0;
  214. /* create genetlink for acpi event */
  215. error = acpi_event_genetlink_init();
  216. if (error)
  217. printk(KERN_WARNING PREFIX
  218. "Failed to create genetlink family for ACPI event\n");
  219. #ifdef CONFIG_ACPI_PROC_EVENT
  220. /* 'event' [R] */
  221. entry = create_proc_entry("event", S_IRUSR, acpi_root_dir);
  222. if (entry)
  223. entry->proc_fops = &acpi_system_event_ops;
  224. else
  225. return -ENODEV;
  226. #endif
  227. return 0;
  228. }
  229. fs_initcall(acpi_event_init);