ec_sys.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * ec_sys.c
  3. *
  4. * Copyright (C) 2010 SUSE Products GmbH/Novell
  5. * Author:
  6. * Thomas Renninger <[email protected]>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/acpi.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/module.h>
  14. #include <linux/uaccess.h>
  15. #include "internal.h"
  16. MODULE_AUTHOR("Thomas Renninger <[email protected]>");
  17. MODULE_DESCRIPTION("ACPI EC sysfs access driver");
  18. MODULE_LICENSE("GPL");
  19. static bool write_support;
  20. module_param(write_support, bool, 0644);
  21. MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
  22. "be needed.");
  23. #define EC_SPACE_SIZE 256
  24. static struct dentry *acpi_ec_debugfs_dir;
  25. static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
  26. size_t count, loff_t *off)
  27. {
  28. /* Use this if support reading/writing multiple ECs exists in ec.c:
  29. * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
  30. */
  31. unsigned int size = EC_SPACE_SIZE;
  32. loff_t init_off = *off;
  33. int err = 0;
  34. if (*off >= size)
  35. return 0;
  36. if (*off + count >= size) {
  37. size -= *off;
  38. count = size;
  39. } else
  40. size = count;
  41. while (size) {
  42. u8 byte_read;
  43. err = ec_read(*off, &byte_read);
  44. if (err)
  45. return err;
  46. if (put_user(byte_read, buf + *off - init_off)) {
  47. if (*off - init_off)
  48. return *off - init_off; /* partial read */
  49. return -EFAULT;
  50. }
  51. *off += 1;
  52. size--;
  53. }
  54. return count;
  55. }
  56. static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
  57. size_t count, loff_t *off)
  58. {
  59. /* Use this if support reading/writing multiple ECs exists in ec.c:
  60. * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
  61. */
  62. unsigned int size = count;
  63. loff_t init_off = *off;
  64. int err = 0;
  65. if (!write_support)
  66. return -EINVAL;
  67. if (*off >= EC_SPACE_SIZE)
  68. return 0;
  69. if (*off + count >= EC_SPACE_SIZE) {
  70. size = EC_SPACE_SIZE - *off;
  71. count = size;
  72. }
  73. while (size) {
  74. u8 byte_write;
  75. if (get_user(byte_write, buf + *off - init_off)) {
  76. if (*off - init_off)
  77. return *off - init_off; /* partial write */
  78. return -EFAULT;
  79. }
  80. err = ec_write(*off, byte_write);
  81. if (err)
  82. return err;
  83. *off += 1;
  84. size--;
  85. }
  86. return count;
  87. }
  88. static const struct file_operations acpi_ec_io_ops = {
  89. .owner = THIS_MODULE,
  90. .open = simple_open,
  91. .read = acpi_ec_read_io,
  92. .write = acpi_ec_write_io,
  93. .llseek = default_llseek,
  94. };
  95. static int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
  96. {
  97. struct dentry *dev_dir;
  98. char name[64];
  99. umode_t mode = 0400;
  100. if (ec_device_count == 0) {
  101. acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
  102. if (!acpi_ec_debugfs_dir)
  103. return -ENOMEM;
  104. }
  105. sprintf(name, "ec%u", ec_device_count);
  106. dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
  107. if (!dev_dir) {
  108. if (ec_device_count != 0)
  109. goto error;
  110. return -ENOMEM;
  111. }
  112. if (!debugfs_create_x32("gpe", 0444, dev_dir, &first_ec->gpe))
  113. goto error;
  114. if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
  115. &first_ec->global_lock))
  116. goto error;
  117. if (write_support)
  118. mode = 0600;
  119. if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
  120. goto error;
  121. return 0;
  122. error:
  123. debugfs_remove_recursive(acpi_ec_debugfs_dir);
  124. return -ENOMEM;
  125. }
  126. static int __init acpi_ec_sys_init(void)
  127. {
  128. int err = 0;
  129. if (first_ec)
  130. err = acpi_ec_add_debugfs(first_ec, 0);
  131. else
  132. err = -ENODEV;
  133. return err;
  134. }
  135. static void __exit acpi_ec_sys_exit(void)
  136. {
  137. debugfs_remove_recursive(acpi_ec_debugfs_dir);
  138. }
  139. module_init(acpi_ec_sys_init);
  140. module_exit(acpi_ec_sys_exit);