event.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(result);
  50. chars_remaining = sprintf(str, "%s %s %08x %08x\n",
  51. event.device_class ? event.
  52. device_class : "<unknown>",
  53. event.bus_id ? event.
  54. bus_id : "<unknown>", event.type,
  55. event.data);
  56. ptr = str;
  57. }
  58. if (chars_remaining < count) {
  59. count = chars_remaining;
  60. }
  61. if (copy_to_user(buffer, ptr, count))
  62. return_VALUE(-EFAULT);
  63. *ppos += count;
  64. chars_remaining -= count;
  65. ptr += count;
  66. return_VALUE(count);
  67. }
  68. static int acpi_system_close_event(struct inode *inode, struct file *file)
  69. {
  70. spin_lock_irq(&acpi_system_event_lock);
  71. event_is_open = 0;
  72. spin_unlock_irq(&acpi_system_event_lock);
  73. return 0;
  74. }
  75. static unsigned int acpi_system_poll_event(struct file *file, poll_table * wait)
  76. {
  77. poll_wait(file, &acpi_bus_event_queue, wait);
  78. if (!list_empty(&acpi_bus_event_list))
  79. return POLLIN | POLLRDNORM;
  80. return 0;
  81. }
  82. static struct file_operations acpi_system_event_ops = {
  83. .open = acpi_system_open_event,
  84. .read = acpi_system_read_event,
  85. .release = acpi_system_close_event,
  86. .poll = acpi_system_poll_event,
  87. };
  88. static int __init acpi_event_init(void)
  89. {
  90. struct proc_dir_entry *entry;
  91. int error = 0;
  92. ACPI_FUNCTION_TRACE("acpi_event_init");
  93. if (acpi_disabled)
  94. return_VALUE(0);
  95. /* 'event' [R] */
  96. entry = create_proc_entry("event", S_IRUSR, acpi_root_dir);
  97. if (entry)
  98. entry->proc_fops = &acpi_system_event_ops;
  99. else {
  100. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  101. "Unable to create '%s' proc fs entry\n",
  102. "event"));
  103. error = -EFAULT;
  104. }
  105. return_VALUE(error);
  106. }
  107. subsys_initcall(acpi_event_init);