Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 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 | |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/acpi.h> |
| 13 | #include <linux/debugfs.h> |
Paul Gortmaker | cc4b859 | 2011-07-01 14:30:49 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Vasiliy Kulikov | ecde300 | 2013-05-22 14:59:11 +0200 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
| 18 | MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>"); |
| 19 | MODULE_DESCRIPTION("ACPI EC sysfs access driver"); |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 22 | static bool write_support; |
| 23 | module_param(write_support, bool, 0644); |
| 24 | MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may " |
| 25 | "be needed."); |
| 26 | |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 27 | #define EC_SPACE_SIZE 256 |
| 28 | |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 29 | static struct dentry *acpi_ec_debugfs_dir; |
| 30 | |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 31 | static ssize_t acpi_ec_read_io(struct file *f, char __user *buf, |
| 32 | size_t count, loff_t *off) |
| 33 | { |
| 34 | /* Use this if support reading/writing multiple ECs exists in ec.c: |
| 35 | * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private; |
| 36 | */ |
| 37 | unsigned int size = EC_SPACE_SIZE; |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 38 | loff_t init_off = *off; |
| 39 | int err = 0; |
| 40 | |
| 41 | if (*off >= size) |
| 42 | return 0; |
| 43 | if (*off + count >= size) { |
| 44 | size -= *off; |
| 45 | count = size; |
| 46 | } else |
| 47 | size = count; |
| 48 | |
| 49 | while (size) { |
Vasiliy Kulikov | ecde300 | 2013-05-22 14:59:11 +0200 | [diff] [blame] | 50 | u8 byte_read; |
| 51 | err = ec_read(*off, &byte_read); |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 52 | if (err) |
| 53 | return err; |
Vasiliy Kulikov | ecde300 | 2013-05-22 14:59:11 +0200 | [diff] [blame] | 54 | if (put_user(byte_read, buf + *off - init_off)) { |
| 55 | if (*off - init_off) |
| 56 | return *off - init_off; /* partial read */ |
| 57 | return -EFAULT; |
| 58 | } |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 59 | *off += 1; |
| 60 | size--; |
| 61 | } |
| 62 | return count; |
| 63 | } |
| 64 | |
| 65 | static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf, |
| 66 | size_t count, loff_t *off) |
| 67 | { |
| 68 | /* Use this if support reading/writing multiple ECs exists in ec.c: |
| 69 | * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private; |
| 70 | */ |
| 71 | |
| 72 | unsigned int size = count; |
| 73 | loff_t init_off = *off; |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 74 | int err = 0; |
| 75 | |
| 76 | if (*off >= EC_SPACE_SIZE) |
| 77 | return 0; |
| 78 | if (*off + count >= EC_SPACE_SIZE) { |
| 79 | size = EC_SPACE_SIZE - *off; |
| 80 | count = size; |
| 81 | } |
| 82 | |
| 83 | while (size) { |
Vasiliy Kulikov | ecde300 | 2013-05-22 14:59:11 +0200 | [diff] [blame] | 84 | u8 byte_write; |
| 85 | if (get_user(byte_write, buf + *off - init_off)) { |
| 86 | if (*off - init_off) |
| 87 | return *off - init_off; /* partial write */ |
| 88 | return -EFAULT; |
| 89 | } |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 90 | err = ec_write(*off, byte_write); |
| 91 | if (err) |
| 92 | return err; |
| 93 | |
| 94 | *off += 1; |
| 95 | size--; |
| 96 | } |
| 97 | return count; |
| 98 | } |
| 99 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 100 | static const struct file_operations acpi_ec_io_ops = { |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 101 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 102 | .open = simple_open, |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 103 | .read = acpi_ec_read_io, |
| 104 | .write = acpi_ec_write_io, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 105 | .llseek = default_llseek, |
Thomas Renninger | 9827886 | 2010-07-16 13:11:32 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
Rashika | 49894d8 | 2013-12-17 14:46:41 +0530 | [diff] [blame] | 108 | static int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count) |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 109 | { |
| 110 | struct dentry *dev_dir; |
| 111 | char name[64]; |
Al Viro | f4ae40a | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 112 | umode_t mode = 0400; |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 113 | |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 114 | if (ec_device_count == 0) { |
| 115 | acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL); |
| 116 | if (!acpi_ec_debugfs_dir) |
| 117 | return -ENOMEM; |
| 118 | } |
| 119 | |
| 120 | sprintf(name, "ec%u", ec_device_count); |
| 121 | dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir); |
| 122 | if (!dev_dir) { |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 123 | if (ec_device_count != 0) |
| 124 | goto error; |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 125 | return -ENOMEM; |
| 126 | } |
| 127 | |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 128 | if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe)) |
| 129 | goto error; |
| 130 | if (!debugfs_create_bool("use_global_lock", 0444, dev_dir, |
Viresh Kumar | 6e58f75 | 2015-09-26 15:04:06 -0700 | [diff] [blame] | 131 | &first_ec->global_lock)) |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 132 | goto error; |
| 133 | |
| 134 | if (write_support) |
| 135 | mode = 0600; |
| 136 | if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops)) |
| 137 | goto error; |
| 138 | |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 139 | return 0; |
Thomas Renninger | 500de3dd | 2010-07-29 22:30:24 +0200 | [diff] [blame] | 140 | |
| 141 | error: |
| 142 | debugfs_remove_recursive(acpi_ec_debugfs_dir); |
| 143 | return -ENOMEM; |
Thomas Renninger | 1195a09 | 2010-07-16 13:11:31 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int __init acpi_ec_sys_init(void) |
| 147 | { |
| 148 | int err = 0; |
| 149 | if (first_ec) |
| 150 | err = acpi_ec_add_debugfs(first_ec, 0); |
| 151 | else |
| 152 | err = -ENODEV; |
| 153 | return err; |
| 154 | } |
| 155 | |
| 156 | static void __exit acpi_ec_sys_exit(void) |
| 157 | { |
| 158 | debugfs_remove_recursive(acpi_ec_debugfs_dir); |
| 159 | } |
| 160 | |
| 161 | module_init(acpi_ec_sys_init); |
| 162 | module_exit(acpi_ec_sys_exit); |