blob: b258cab9061ced6765fc7dda2f81c70eb9511a83 [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>
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 int acpi_ec_open_io(struct inode *i, struct file *f)
31{
32 f->private_data = i->i_private;
33 return 0;
34}
35
36static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
37 size_t count, loff_t *off)
38{
39 /* Use this if support reading/writing multiple ECs exists in ec.c:
40 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
41 */
42 unsigned int size = EC_SPACE_SIZE;
43 u8 *data = (u8 *) buf;
44 loff_t init_off = *off;
45 int err = 0;
46
47 if (*off >= size)
48 return 0;
49 if (*off + count >= size) {
50 size -= *off;
51 count = size;
52 } else
53 size = count;
54
55 while (size) {
56 err = ec_read(*off, &data[*off - init_off]);
57 if (err)
58 return err;
59 *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;
74 u8 *data = (u8 *) buf;
75 int err = 0;
76
77 if (*off >= EC_SPACE_SIZE)
78 return 0;
79 if (*off + count >= EC_SPACE_SIZE) {
80 size = EC_SPACE_SIZE - *off;
81 count = size;
82 }
83
84 while (size) {
85 u8 byte_write = data[*off - init_off];
86 err = ec_write(*off, byte_write);
87 if (err)
88 return err;
89
90 *off += 1;
91 size--;
92 }
93 return count;
94}
95
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +040096static const struct file_operations acpi_ec_io_ops = {
Thomas Renninger98278862010-07-16 13:11:32 +020097 .owner = THIS_MODULE,
98 .open = acpi_ec_open_io,
99 .read = acpi_ec_read_io,
100 .write = acpi_ec_write_io,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200101 .llseek = default_llseek,
Thomas Renninger98278862010-07-16 13:11:32 +0200102};
103
Thomas Renninger1195a092010-07-16 13:11:31 +0200104int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
105{
106 struct dentry *dev_dir;
107 char name[64];
Al Virof4ae40a2011-07-24 04:33:43 -0400108 umode_t mode = 0400;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200109
Thomas Renninger1195a092010-07-16 13:11:31 +0200110 if (ec_device_count == 0) {
111 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
112 if (!acpi_ec_debugfs_dir)
113 return -ENOMEM;
114 }
115
116 sprintf(name, "ec%u", ec_device_count);
117 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
118 if (!dev_dir) {
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200119 if (ec_device_count != 0)
120 goto error;
Thomas Renninger1195a092010-07-16 13:11:31 +0200121 return -ENOMEM;
122 }
123
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200124 if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
125 goto error;
126 if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
127 (u32 *)&first_ec->global_lock))
128 goto error;
129
130 if (write_support)
131 mode = 0600;
132 if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
133 goto error;
134
Thomas Renninger1195a092010-07-16 13:11:31 +0200135 return 0;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200136
137error:
138 debugfs_remove_recursive(acpi_ec_debugfs_dir);
139 return -ENOMEM;
Thomas Renninger1195a092010-07-16 13:11:31 +0200140}
141
142static int __init acpi_ec_sys_init(void)
143{
144 int err = 0;
145 if (first_ec)
146 err = acpi_ec_add_debugfs(first_ec, 0);
147 else
148 err = -ENODEV;
149 return err;
150}
151
152static void __exit acpi_ec_sys_exit(void)
153{
154 debugfs_remove_recursive(acpi_ec_debugfs_dir);
155}
156
157module_init(acpi_ec_sys_init);
158module_exit(acpi_ec_sys_exit);