event.c 3.0 KB

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