blob: fd39c14493ab870dd7d915dce32d70857302cc9c [file] [log] [blame]
Thomas Gleixner7a338472019-06-04 10:11:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Thomas Renninger98278862010-07-16 13:11:32 +02002/*
3 * ec_sys.c
4 *
5 * Copyright (C) 2010 SUSE Products GmbH/Novell
6 * Author:
7 * Thomas Renninger <trenn@suse.de>
Thomas Renninger98278862010-07-16 13:11:32 +02008 */
9
Thomas Renninger1195a092010-07-16 13:11:31 +020010#include <linux/kernel.h>
11#include <linux/acpi.h>
12#include <linux/debugfs.h>
Paul Gortmakercc4b8592011-07-01 14:30:49 -040013#include <linux/module.h>
Vasiliy Kulikovecde3002013-05-22 14:59:11 +020014#include <linux/uaccess.h>
Thomas Renninger1195a092010-07-16 13:11:31 +020015#include "internal.h"
16
17MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
18MODULE_DESCRIPTION("ACPI EC sysfs access driver");
19MODULE_LICENSE("GPL");
20
Thomas Renninger500de3dd2010-07-29 22:30:24 +020021static bool write_support;
22module_param(write_support, bool, 0644);
23MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
24 "be needed.");
25
Thomas Renninger98278862010-07-16 13:11:32 +020026#define EC_SPACE_SIZE 256
27
Thomas Renninger1195a092010-07-16 13:11:31 +020028static struct dentry *acpi_ec_debugfs_dir;
29
Thomas Renninger98278862010-07-16 13:11:32 +020030static 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 Renninger98278862010-07-16 13:11:32 +020037 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 Kulikovecde3002013-05-22 14:59:11 +020049 u8 byte_read;
50 err = ec_read(*off, &byte_read);
Thomas Renninger98278862010-07-16 13:11:32 +020051 if (err)
52 return err;
Vasiliy Kulikovecde3002013-05-22 14:59:11 +020053 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 Renninger98278862010-07-16 13:11:32 +020058 *off += 1;
59 size--;
60 }
61 return count;
62}
63
64static 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 Renninger98278862010-07-16 13:11:32 +020073 int err = 0;
74
Oleg Drokin1b6e75e2016-02-06 02:08:08 -050075 if (!write_support)
76 return -EINVAL;
77
Thomas Renninger98278862010-07-16 13:11:32 +020078 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 Kulikovecde3002013-05-22 14:59:11 +020086 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 Renninger98278862010-07-16 13:11:32 +020092 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 Kulikov9c8b04b2011-06-25 21:07:52 +0400102static const struct file_operations acpi_ec_io_ops = {
Thomas Renninger98278862010-07-16 13:11:32 +0200103 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700104 .open = simple_open,
Thomas Renninger98278862010-07-16 13:11:32 +0200105 .read = acpi_ec_read_io,
106 .write = acpi_ec_write_io,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200107 .llseek = default_llseek,
Thomas Renninger98278862010-07-16 13:11:32 +0200108};
109
Greg Kroah-Hartman9ec6dbf2019-01-22 16:21:03 +0100110static void acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
Thomas Renninger1195a092010-07-16 13:11:31 +0200111{
112 struct dentry *dev_dir;
113 char name[64];
Al Virof4ae40a62011-07-24 04:33:43 -0400114 umode_t mode = 0400;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200115
Greg Kroah-Hartman9ec6dbf2019-01-22 16:21:03 +0100116 if (ec_device_count == 0)
Thomas Renninger1195a092010-07-16 13:11:31 +0200117 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
Thomas Renninger1195a092010-07-16 13:11:31 +0200118
119 sprintf(name, "ec%u", ec_device_count);
120 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
Thomas Renninger1195a092010-07-16 13:11:31 +0200121
Greg Kroah-Hartman9ec6dbf2019-01-22 16:21:03 +0100122 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 Renninger500de3dd2010-07-29 22:30:24 +0200125
126 if (write_support)
127 mode = 0600;
Greg Kroah-Hartman9ec6dbf2019-01-22 16:21:03 +0100128 debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops);
Thomas Renninger1195a092010-07-16 13:11:31 +0200129}
130
131static int __init acpi_ec_sys_init(void)
132{
Thomas Renninger1195a092010-07-16 13:11:31 +0200133 if (first_ec)
Greg Kroah-Hartman9ec6dbf2019-01-22 16:21:03 +0100134 acpi_ec_add_debugfs(first_ec, 0);
135 return 0;
Thomas Renninger1195a092010-07-16 13:11:31 +0200136}
137
138static void __exit acpi_ec_sys_exit(void)
139{
140 debugfs_remove_recursive(acpi_ec_debugfs_dir);
141}
142
143module_init(acpi_ec_sys_init);
144module_exit(acpi_ec_sys_exit);