custom_method.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * custom_method.c - debugfs interface for customizing ACPI control method
  3. */
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/acpi.h>
  10. #include "internal.h"
  11. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  12. ACPI_MODULE_NAME("custom_method");
  13. MODULE_LICENSE("GPL");
  14. static struct dentry *cm_dentry;
  15. /* /sys/kernel/debug/acpi/custom_method */
  16. static ssize_t cm_write(struct file *file, const char __user * user_buf,
  17. size_t count, loff_t *ppos)
  18. {
  19. static char *buf;
  20. static u32 max_size;
  21. static u32 uncopied_bytes;
  22. struct acpi_table_header table;
  23. acpi_status status;
  24. if (!(*ppos)) {
  25. /* parse the table header to get the table length */
  26. if (count <= sizeof(struct acpi_table_header))
  27. return -EINVAL;
  28. if (copy_from_user(&table, user_buf,
  29. sizeof(struct acpi_table_header)))
  30. return -EFAULT;
  31. uncopied_bytes = max_size = table.length;
  32. buf = kzalloc(max_size, GFP_KERNEL);
  33. if (!buf)
  34. return -ENOMEM;
  35. }
  36. if (buf == NULL)
  37. return -EINVAL;
  38. if ((*ppos > max_size) ||
  39. (*ppos + count > max_size) ||
  40. (*ppos + count < count) ||
  41. (count > uncopied_bytes))
  42. return -EINVAL;
  43. if (copy_from_user(buf + (*ppos), user_buf, count)) {
  44. kfree(buf);
  45. buf = NULL;
  46. return -EFAULT;
  47. }
  48. uncopied_bytes -= count;
  49. *ppos += count;
  50. if (!uncopied_bytes) {
  51. status = acpi_install_method(buf);
  52. kfree(buf);
  53. buf = NULL;
  54. if (ACPI_FAILURE(status))
  55. return -EINVAL;
  56. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
  57. }
  58. return count;
  59. }
  60. static const struct file_operations cm_fops = {
  61. .write = cm_write,
  62. .llseek = default_llseek,
  63. };
  64. static int __init acpi_custom_method_init(void)
  65. {
  66. if (acpi_debugfs_dir == NULL)
  67. return -ENOENT;
  68. cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
  69. acpi_debugfs_dir, NULL, &cm_fops);
  70. if (cm_dentry == NULL)
  71. return -ENODEV;
  72. return 0;
  73. }
  74. static void __exit acpi_custom_method_exit(void)
  75. {
  76. if (cm_dentry)
  77. debugfs_remove(cm_dentry);
  78. }
  79. module_init(acpi_custom_method_init);
  80. module_exit(acpi_custom_method_exit);