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