blob: 22f918bacd35b0186af2328ec68d19b0567b08b9 [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>
14#include "internal.h"
15
16MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
17MODULE_DESCRIPTION("ACPI EC sysfs access driver");
18MODULE_LICENSE("GPL");
19
Thomas Renninger500de3dd2010-07-29 22:30:24 +020020static bool write_support;
21module_param(write_support, bool, 0644);
22MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
23 "be needed.");
24
Thomas Renninger98278862010-07-16 13:11:32 +020025#define EC_SPACE_SIZE 256
26
Thomas Renninger1195a092010-07-16 13:11:31 +020027static struct dentry *acpi_ec_debugfs_dir;
28
Thomas Renninger98278862010-07-16 13:11:32 +020029static int acpi_ec_open_io(struct inode *i, struct file *f)
30{
31 f->private_data = i->i_private;
32 return 0;
33}
34
35static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
36 size_t count, loff_t *off)
37{
38 /* Use this if support reading/writing multiple ECs exists in ec.c:
39 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
40 */
41 unsigned int size = EC_SPACE_SIZE;
42 u8 *data = (u8 *) buf;
43 loff_t init_off = *off;
44 int err = 0;
45
46 if (*off >= size)
47 return 0;
48 if (*off + count >= size) {
49 size -= *off;
50 count = size;
51 } else
52 size = count;
53
54 while (size) {
55 err = ec_read(*off, &data[*off - init_off]);
56 if (err)
57 return err;
58 *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;
73 u8 *data = (u8 *) buf;
74 int err = 0;
75
76 if (*off >= EC_SPACE_SIZE)
77 return 0;
78 if (*off + count >= EC_SPACE_SIZE) {
79 size = EC_SPACE_SIZE - *off;
80 count = size;
81 }
82
83 while (size) {
84 u8 byte_write = data[*off - init_off];
85 err = ec_write(*off, byte_write);
86 if (err)
87 return err;
88
89 *off += 1;
90 size--;
91 }
92 return count;
93}
94
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +040095static const struct file_operations acpi_ec_io_ops = {
Thomas Renninger98278862010-07-16 13:11:32 +020096 .owner = THIS_MODULE,
97 .open = acpi_ec_open_io,
98 .read = acpi_ec_read_io,
99 .write = acpi_ec_write_io,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200100 .llseek = default_llseek,
Thomas Renninger98278862010-07-16 13:11:32 +0200101};
102
Thomas Renninger1195a092010-07-16 13:11:31 +0200103int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
104{
105 struct dentry *dev_dir;
106 char name[64];
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200107 mode_t mode = 0400;
108
Thomas Renninger1195a092010-07-16 13:11:31 +0200109 if (ec_device_count == 0) {
110 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
111 if (!acpi_ec_debugfs_dir)
112 return -ENOMEM;
113 }
114
115 sprintf(name, "ec%u", ec_device_count);
116 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
117 if (!dev_dir) {
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200118 if (ec_device_count != 0)
119 goto error;
Thomas Renninger1195a092010-07-16 13:11:31 +0200120 return -ENOMEM;
121 }
122
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200123 if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
124 goto error;
125 if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
126 (u32 *)&first_ec->global_lock))
127 goto error;
128
129 if (write_support)
130 mode = 0600;
131 if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
132 goto error;
133
Thomas Renninger1195a092010-07-16 13:11:31 +0200134 return 0;
Thomas Renninger500de3dd2010-07-29 22:30:24 +0200135
136error:
137 debugfs_remove_recursive(acpi_ec_debugfs_dir);
138 return -ENOMEM;
Thomas Renninger1195a092010-07-16 13:11:31 +0200139}
140
141static int __init acpi_ec_sys_init(void)
142{
143 int err = 0;
144 if (first_ec)
145 err = acpi_ec_add_debugfs(first_ec, 0);
146 else
147 err = -ENODEV;
148 return err;
149}
150
151static void __exit acpi_ec_sys_exit(void)
152{
153 debugfs_remove_recursive(acpi_ec_debugfs_dir);
154}
155
156module_init(acpi_ec_sys_init);
157module_exit(acpi_ec_sys_exit);