blob: 6c7dd7af789e453ce3d16ae80b849ef38bc4a3c0 [file] [log] [blame]
Thomas Renninger98278862010-07-16 13:11:32 +02001/*
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 Renninger1195a092010-07-16 13:11:31 +020011#include <linux/kernel.h>
12#include <linux/acpi.h>
13#include <linux/debugfs.h>
Paul Gortmakercc4b8592011-07-01 14:30:49 -040014#include <linux/module.h>
Vasiliy Kulikovecde3002013-05-22 14:59:11 +020015#include <linux/uaccess.h>
Thomas Renninger1195a092010-07-16 13:11:31 +020016#include "internal.h"
17
18MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
19MODULE_DESCRIPTION("ACPI EC sysfs access driver");
20MODULE_LICENSE("GPL");
21
Thomas Renninger500de3dd2010-07-29 22:30:24 +020022static bool write_support;
23module_param(write_support, bool, 0644);
24MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
25 "be needed.");
26
Thomas Renninger98278862010-07-16 13:11:32 +020027#define EC_SPACE_SIZE 256
28
Thomas Renninger1195a092010-07-16 13:11:31 +020029static struct dentry *acpi_ec_debugfs_dir;
30
Thomas Renninger98278862010-07-16 13:11:32 +020031static 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 Renninger98278862010-07-16 13:11:32 +020038 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 Kulikovecde3002013-05-22 14:59:11 +020050 u8 byte_read;
51 err = ec_read(*off, &byte_read);
Thomas Renninger98278862010-07-16 13:11:32 +020052 if (err)
53 return err;
Vasiliy Kulikovecde3002013-05-22 14:59:11 +020054 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 Renninger98278862010-07-16 13:11:32 +020059 *off += 1;
60 size--;
61 }
62 return count;
63}
64
65static 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 Renninger98278862010-07-16 13:11:32 +020074 int err = 0;
75
Oleg Drokin1b6e75e2016-02-06 02:08:08 -050076 if (!write_support)
77 return -EINVAL;
78
Thomas Renninger98278862010-07-16 13:11:32 +020079 if (*off >= EC_SPACE_SIZE)
80 return 0;
81 if (*off + count >= EC_SPACE_SIZE) {
82 size = EC_SPACE_SIZE - *off;
83 count = size;
84 }
85
86 while (size) {
Vasiliy Kulikovecde3002013-05-22 14:59:11 +020087 u8 byte_write;
88 if (get_user(byte_write, buf + *off - init_off)) {
89 if (*off - init_off)
90 return *off - init_off; /* partial write */
91 return -EFAULT;
92 }
Thomas Renninger98278862010-07-16 13:11:32 +020093 err = ec_write(*off, byte_write);
94 if (err)
95 return err;
96
97 *off += 1;
98 size--;
99 }
100 return count;
101}
102
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400103static const struct file_operations acpi_ec_io_ops = {
Thomas Renninger98278862010-07-16 13:11:32 +0200104 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700105 .open = simple_open,
Thomas Renninger98278862010-07-16 13:11:32 +0200106 .read = acpi_ec_read_io,
107 .write = acpi_ec_write_io,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200108 .llseek = default_llseek,
Thomas Renninger98278862010-07-16 13:11:32 +0200109};
110
Rashika49894d82013-12-17 14:46:41 +0530111static int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
Thomas Renninger1195a092010-07-16 13:11:31 +0200112{
113 struct dentry *dev_dir;
114 char name[64];
Al Virof4ae40a2011-07-24 04:33:43 -0400115 umode_t mode = 0400;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200116
Thomas Renninger1195a092010-07-16 13:11:31 +0200117 if (ec_device_count == 0) {
118 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
119 if (!acpi_ec_debugfs_dir)
120 return -ENOMEM;
121 }
122
123 sprintf(name, "ec%u", ec_device_count);
124 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
125 if (!dev_dir) {
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200126 if (ec_device_count != 0)
127 goto error;
Thomas Renninger1195a092010-07-16 13:11:31 +0200128 return -ENOMEM;
129 }
130
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200131 if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
132 goto error;
133 if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
Viresh Kumar6e58f752015-09-26 15:04:06 -0700134 &first_ec->global_lock))
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200135 goto error;
136
137 if (write_support)
138 mode = 0600;
139 if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
140 goto error;
141
Thomas Renninger1195a092010-07-16 13:11:31 +0200142 return 0;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200143
144error:
145 debugfs_remove_recursive(acpi_ec_debugfs_dir);
146 return -ENOMEM;
Thomas Renninger1195a092010-07-16 13:11:31 +0200147}
148
149static int __init acpi_ec_sys_init(void)
150{
151 int err = 0;
152 if (first_ec)
153 err = acpi_ec_add_debugfs(first_ec, 0);
154 else
155 err = -ENODEV;
156 return err;
157}
158
159static void __exit acpi_ec_sys_exit(void)
160{
161 debugfs_remove_recursive(acpi_ec_debugfs_dir);
162}
163
164module_init(acpi_ec_sys_init);
165module_exit(acpi_ec_sys_exit);