event.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ACPI_FUNCTION_TRACE("acpi_system_read_event");
  42. if (!chars_remaining) {
  43. memset(&event, 0, sizeof(struct acpi_bus_event));
  44. if ((file->f_flags & O_NONBLOCK)
  45. && (list_empty(&acpi_bus_event_list)))
  46. return_VALUE(-EAGAIN);
  47. result = acpi_bus_receive_event(&event);
  48. if (result) {
  49. return_VALUE(-EIO);
  50. }
  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_VALUE(-EFAULT);
  64. *ppos += count;
  65. chars_remaining -= count;
  66. ptr += count;
  67. return_VALUE(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 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. static int __init acpi_event_init(void)
  90. {
  91. struct proc_dir_entry *entry;
  92. int error = 0;
  93. ACPI_FUNCTION_TRACE("acpi_event_init");
  94. if (acpi_disabled)
  95. return_VALUE(0);
  96. /* 'event' [R] */
  97. entry = create_proc_entry("event", S_IRUSR, acpi_root_dir);
  98. if (entry)
  99. entry->proc_fops = &acpi_system_event_ops;
  100. else {
  101. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  102. "Unable to create '%s' proc fs entry\n",
  103. "event"));
  104. error = -EFAULT;
  105. }
  106. return_VALUE(error);
  107. }
  108. subsys_initcall(acpi_event_init);