Zhang Rui | a25ee92 | 2010-07-15 10:46:15 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * debugfs.c - ACPI debugfs interface to userspace. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/init.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/uaccess.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <acpi/acpi_drivers.h> |
| 11 | |
| 12 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
| 13 | ACPI_MODULE_NAME("debugfs"); |
| 14 | |
| 15 | /* /sys/kernel/debug/acpi/custom_method */ |
| 16 | |
| 17 | static ssize_t cm_write(struct file *file, const char __user * user_buf, |
| 18 | size_t count, loff_t *ppos) |
| 19 | { |
| 20 | static char *buf; |
| 21 | static int uncopied_bytes; |
| 22 | struct acpi_table_header table; |
| 23 | acpi_status status; |
| 24 | |
| 25 | if (!(*ppos)) { |
| 26 | /* parse the table header to get the table length */ |
| 27 | if (count <= sizeof(struct acpi_table_header)) |
| 28 | return -EINVAL; |
| 29 | if (copy_from_user(&table, user_buf, |
| 30 | sizeof(struct acpi_table_header))) |
| 31 | return -EFAULT; |
| 32 | uncopied_bytes = table.length; |
| 33 | buf = kzalloc(uncopied_bytes, GFP_KERNEL); |
| 34 | if (!buf) |
| 35 | return -ENOMEM; |
| 36 | } |
| 37 | |
| 38 | if (uncopied_bytes < count) { |
| 39 | kfree(buf); |
| 40 | return -EINVAL; |
| 41 | } |
| 42 | |
| 43 | if (copy_from_user(buf + (*ppos), user_buf, count)) { |
| 44 | kfree(buf); |
| 45 | return -EFAULT; |
| 46 | } |
| 47 | |
| 48 | uncopied_bytes -= count; |
| 49 | *ppos += count; |
| 50 | |
| 51 | if (!uncopied_bytes) { |
| 52 | status = acpi_install_method(buf); |
| 53 | kfree(buf); |
| 54 | if (ACPI_FAILURE(status)) |
| 55 | return -EINVAL; |
| 56 | add_taint(TAINT_OVERRIDDEN_ACPI_TABLE); |
| 57 | } |
| 58 | |
| 59 | return count; |
| 60 | } |
| 61 | |
| 62 | static const struct file_operations cm_fops = { |
| 63 | .write = cm_write, |
| 64 | }; |
| 65 | |
| 66 | int __init acpi_debugfs_init(void) |
| 67 | { |
| 68 | struct dentry *acpi_dir, *cm_dentry; |
| 69 | |
| 70 | acpi_dir = debugfs_create_dir("acpi", NULL); |
| 71 | if (!acpi_dir) |
| 72 | goto err; |
| 73 | |
| 74 | cm_dentry = debugfs_create_file("custom_method", S_IWUGO, |
| 75 | acpi_dir, NULL, &cm_fops); |
| 76 | if (!cm_dentry) |
| 77 | goto err; |
| 78 | |
| 79 | return 0; |
| 80 | |
| 81 | err: |
| 82 | if (acpi_dir) |
| 83 | debugfs_remove(acpi_dir); |
| 84 | return -EINVAL; |
| 85 | } |