1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 SUSE Products GmbH/Novell
7 * Thomas Renninger <trenn@suse.de>
10 #include <linux/kernel.h>
11 #include <linux/acpi.h>
12 #include <linux/debugfs.h>
13 #include <linux/module.h>
14 #include <linux/uaccess.h>
17 MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
18 MODULE_DESCRIPTION("ACPI EC sysfs access driver");
19 MODULE_LICENSE("GPL");
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 "
26 #define EC_SPACE_SIZE 256
28 static struct dentry
*acpi_ec_debugfs_dir
;
30 static ssize_t
acpi_ec_read_io(struct file
*f
, char __user
*buf
,
31 size_t count
, loff_t
*off
)
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;
36 unsigned int size
= EC_SPACE_SIZE
;
37 loff_t init_off
= *off
;
42 if (*off
+ count
>= size
) {
50 err
= ec_read(*off
, &byte_read
);
53 if (put_user(byte_read
, buf
+ *off
- init_off
)) {
55 return *off
- init_off
; /* partial read */
64 static ssize_t
acpi_ec_write_io(struct file
*f
, const char __user
*buf
,
65 size_t count
, loff_t
*off
)
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;
71 unsigned int size
= count
;
72 loff_t init_off
= *off
;
78 if (*off
>= EC_SPACE_SIZE
)
80 if (*off
+ count
>= EC_SPACE_SIZE
) {
81 size
= EC_SPACE_SIZE
- *off
;
87 if (get_user(byte_write
, buf
+ *off
- init_off
)) {
89 return *off
- init_off
; /* partial write */
92 err
= ec_write(*off
, byte_write
);
102 static const struct file_operations acpi_ec_io_ops
= {
103 .owner
= THIS_MODULE
,
105 .read
= acpi_ec_read_io
,
106 .write
= acpi_ec_write_io
,
107 .llseek
= default_llseek
,
110 static void acpi_ec_add_debugfs(struct acpi_ec
*ec
, unsigned int ec_device_count
)
112 struct dentry
*dev_dir
;
116 if (ec_device_count
== 0)
117 acpi_ec_debugfs_dir
= debugfs_create_dir("ec", NULL
);
119 sprintf(name
, "ec%u", ec_device_count
);
120 dev_dir
= debugfs_create_dir(name
, acpi_ec_debugfs_dir
);
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
);
128 debugfs_create_file("io", mode
, dev_dir
, ec
, &acpi_ec_io_ops
);
131 static int __init
acpi_ec_sys_init(void)
134 acpi_ec_add_debugfs(first_ec
, 0);
138 static void __exit
acpi_ec_sys_exit(void)
140 debugfs_remove_recursive(acpi_ec_debugfs_dir
);
143 module_init(acpi_ec_sys_init
);
144 module_exit(acpi_ec_sys_exit
);