event.c 5.8 KB

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