custom_method.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. kfree(buf);
  43. return -EINVAL;
  44. }
  45. if (copy_from_user(buf + (*ppos), user_buf, count)) {
  46. kfree(buf);
  47. buf = NULL;
  48. return -EFAULT;
  49. }
  50. uncopied_bytes -= count;
  51. *ppos += count;
  52. if (!uncopied_bytes) {
  53. status = acpi_install_method(buf);
  54. kfree(buf);
  55. buf = NULL;
  56. if (ACPI_FAILURE(status))
  57. return -EINVAL;
  58. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
  59. }
  60. kfree(buf);
  61. return count;
  62. }
  63. static const struct file_operations cm_fops = {
  64. .write = cm_write,
  65. .llseek = default_llseek,
  66. };
  67. static int __init acpi_custom_method_init(void)
  68. {
  69. if (acpi_debugfs_dir == NULL)
  70. return -ENOENT;
  71. cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
  72. acpi_debugfs_dir, NULL, &cm_fops);
  73. if (cm_dentry == NULL)
  74. return -ENODEV;
  75. return 0;
  76. }
  77. static void __exit acpi_custom_method_exit(void)
  78. {
  79. if (cm_dentry)
  80. debugfs_remove(cm_dentry);
  81. }
  82. module_init(acpi_custom_method_init);
  83. module_exit(acpi_custom_method_exit);