custom_method.c 2.2 KB

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