event.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  14. ACPI_MODULE_NAME("event")
  15. /* Global vars for handling event proc entry */
  16. static DEFINE_SPINLOCK(acpi_system_event_lock);
  17. int event_is_open = 0;
  18. extern struct list_head acpi_bus_event_list;
  19. extern wait_queue_head_t acpi_bus_event_queue;
  20. static int acpi_system_open_event(struct inode *inode, struct file *file)
  21. {
  22. spin_lock_irq(&acpi_system_event_lock);
  23. if (event_is_open)
  24. goto out_busy;
  25. event_is_open = 1;
  26. spin_unlock_irq(&acpi_system_event_lock);
  27. return 0;
  28. out_busy:
  29. spin_unlock_irq(&acpi_system_event_lock);
  30. return -EBUSY;
  31. }
  32. static ssize_t
  33. acpi_system_read_event(struct file *file, char __user * buffer, size_t count,
  34. loff_t * ppos)
  35. {
  36. int result = 0;
  37. struct acpi_bus_event event;
  38. static char str[ACPI_MAX_STRING];
  39. static int chars_remaining = 0;
  40. static char *ptr;
  41. if (!chars_remaining) {
  42. memset(&event, 0, sizeof(struct acpi_bus_event));
  43. if ((file->f_flags & O_NONBLOCK)
  44. && (list_empty(&acpi_bus_event_list)))
  45. return -EAGAIN;
  46. result = acpi_bus_receive_event(&event);
  47. if (result)
  48. return result;
  49. chars_remaining = sprintf(str, "%s %s %08x %08x\n",
  50. event.device_class ? event.
  51. device_class : "<unknown>",
  52. event.bus_id ? event.
  53. bus_id : "<unknown>", event.type,
  54. event.data);
  55. ptr = str;
  56. }
  57. if (chars_remaining < count) {
  58. count = chars_remaining;
  59. }
  60. if (copy_to_user(buffer, ptr, count))
  61. return -EFAULT;
  62. *ppos += count;
  63. chars_remaining -= count;
  64. ptr += count;
  65. return count;
  66. }
  67. static int acpi_system_close_event(struct inode *inode, struct file *file)
  68. {
  69. spin_lock_irq(&acpi_system_event_lock);
  70. event_is_open = 0;
  71. spin_unlock_irq(&acpi_system_event_lock);
  72. return 0;
  73. }
  74. static unsigned int acpi_system_poll_event(struct file *file, poll_table * wait)
  75. {
  76. poll_wait(file, &acpi_bus_event_queue, wait);
  77. if (!list_empty(&acpi_bus_event_list))
  78. return POLLIN | POLLRDNORM;
  79. return 0;
  80. }
  81. static const struct file_operations acpi_system_event_ops = {
  82. .open = acpi_system_open_event,
  83. .read = acpi_system_read_event,
  84. .release = acpi_system_close_event,
  85. .poll = acpi_system_poll_event,
  86. };
  87. static int __init acpi_event_init(void)
  88. {
  89. struct proc_dir_entry *entry;
  90. int error = 0;
  91. if (acpi_disabled)
  92. return 0;
  93. /* 'event' [R] */
  94. entry = create_proc_entry("event", S_IRUSR, acpi_root_dir);
  95. if (entry)
  96. entry->proc_fops = &acpi_system_event_ops;
  97. else {
  98. error = -ENODEV;
  99. }
  100. return error;
  101. }
  102. subsys_initcall(acpi_event_init);