ec_sys.c 3.3 KB

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