ec_sys.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * ec_sys.c
  3. *
  4. * Copyright (C) 2010 SUSE Products GmbH/Novell
  5. * Author:
  6. * Thomas Renninger <trenn@suse.de>
  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 "internal.h"
  15. MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
  16. MODULE_DESCRIPTION("ACPI EC sysfs access driver");
  17. MODULE_LICENSE("GPL");
  18. static bool write_support;
  19. module_param(write_support, bool, 0644);
  20. MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
  21. "be needed.");
  22. #define EC_SPACE_SIZE 256
  23. static struct dentry *acpi_ec_debugfs_dir;
  24. static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
  25. size_t count, loff_t *off)
  26. {
  27. /* Use this if support reading/writing multiple ECs exists in ec.c:
  28. * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
  29. */
  30. unsigned int size = EC_SPACE_SIZE;
  31. u8 *data = (u8 *) buf;
  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. err = ec_read(*off, &data[*off - init_off]);
  43. if (err)
  44. return err;
  45. *off += 1;
  46. size--;
  47. }
  48. return count;
  49. }
  50. static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
  51. size_t count, loff_t *off)
  52. {
  53. /* Use this if support reading/writing multiple ECs exists in ec.c:
  54. * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
  55. */
  56. unsigned int size = count;
  57. loff_t init_off = *off;
  58. u8 *data = (u8 *) buf;
  59. int err = 0;
  60. if (*off >= EC_SPACE_SIZE)
  61. return 0;
  62. if (*off + count >= EC_SPACE_SIZE) {
  63. size = EC_SPACE_SIZE - *off;
  64. count = size;
  65. }
  66. while (size) {
  67. u8 byte_write = data[*off - init_off];
  68. err = ec_write(*off, byte_write);
  69. if (err)
  70. return err;
  71. *off += 1;
  72. size--;
  73. }
  74. return count;
  75. }
  76. static const struct file_operations acpi_ec_io_ops = {
  77. .owner = THIS_MODULE,
  78. .open = simple_open,
  79. .read = acpi_ec_read_io,
  80. .write = acpi_ec_write_io,
  81. .llseek = default_llseek,
  82. };
  83. int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
  84. {
  85. struct dentry *dev_dir;
  86. char name[64];
  87. umode_t mode = 0400;
  88. if (ec_device_count == 0) {
  89. acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
  90. if (!acpi_ec_debugfs_dir)
  91. return -ENOMEM;
  92. }
  93. sprintf(name, "ec%u", ec_device_count);
  94. dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
  95. if (!dev_dir) {
  96. if (ec_device_count != 0)
  97. goto error;
  98. return -ENOMEM;
  99. }
  100. if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
  101. goto error;
  102. if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
  103. (u32 *)&first_ec->global_lock))
  104. goto error;
  105. if (write_support)
  106. mode = 0600;
  107. if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
  108. goto error;
  109. return 0;
  110. error:
  111. debugfs_remove_recursive(acpi_ec_debugfs_dir);
  112. return -ENOMEM;
  113. }
  114. static int __init acpi_ec_sys_init(void)
  115. {
  116. int err = 0;
  117. if (first_ec)
  118. err = acpi_ec_add_debugfs(first_ec, 0);
  119. else
  120. err = -ENODEV;
  121. return err;
  122. }
  123. static void __exit acpi_ec_sys_exit(void)
  124. {
  125. debugfs_remove_recursive(acpi_ec_debugfs_dir);
  126. }
  127. module_init(acpi_ec_sys_init);
  128. module_exit(acpi_ec_sys_exit);