forked from mkottman/acpi_call
/
acpi_call.c
232 lines (196 loc) · 6.38 KB
/
acpi_call.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright (c) 2010: Michal Kottman */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <acpi/acpi.h>
MODULE_LICENSE("GPL");
extern struct proc_dir_entry *acpi_root_dir;
static char result_buffer[256];
static u8 buffer_data[256];
/**
@param method The full name of ACPI method to call
@param argc The number of parameters
@param argv A pre-allocated array of arguments of type acpi_object
*/
static void do_acpi_call(const char * method, int argc, union acpi_object *argv)
{
acpi_status status;
acpi_handle handle;
struct acpi_object_list atpx_arg;
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
union acpi_object *result;
printk(KERN_INFO "acpi_call: Calling %s\n", method);
// get the handle of the method, must be a fully qualified path
status = acpi_get_handle(NULL, (acpi_string) method, &handle);
if (ACPI_FAILURE(status))
{
sprintf(result_buffer, "Error: %s", acpi_format_exception(status));
printk(KERN_ERR "acpi_call: Cannot get handle: %s\n", result_buffer);
return;
}
// prepare parameters
atpx_arg.count = argc;
atpx_arg.pointer = argv;
// call the method
status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
if (ACPI_FAILURE(status))
{
sprintf(result_buffer, "Error: %s", acpi_format_exception(status));
printk(KERN_ERR "acpi_call: ATPX method call failed: %s\n", result_buffer);
return;
}
result = buffer.pointer;
if (result->type == ACPI_TYPE_INTEGER) {
sprintf(result_buffer, "0x%x", (int)result->integer.value);
} else if (result->type == ACPI_TYPE_STRING) {
sprintf(result_buffer, "\"%*s\"", result->string.length, result->string.pointer);
} else if (result->type == ACPI_TYPE_BUFFER) {
int i;
sprintf(result_buffer, "{");
for (i = 0; i < result->buffer.length; i++)
sprintf(result_buffer + strlen(result_buffer),
i == 0 ? "0x%02x" : ", 0x%02x", result->buffer.pointer[i]);
strcpy(result_buffer + strlen(result_buffer), "}");
} else {
sprintf(result_buffer, "Object type 0x%x\n", result->type);
}
kfree(buffer.pointer);
printk(KERN_INFO "acpi_call: Call successful: %s\n", result_buffer);
}
/** Parses method name and arguments
@param input Input string to be parsed. Modified in the process.
@param nargs Set to number of arguments parsed (output)
@param args
*/
static char *parse_acpi_args(char *input, int *nargs, union acpi_object **args)
{
char *s = input;
u8 *buf = buffer_data;
*nargs = 0;
*args = NULL;
// the method name is separated from the arguments by a space
while (*s && *s != ' ')
s++;
// if no space is found, return 0 arguments
if (*s == 0)
return input;
*args = (union acpi_object *) kmalloc(16 * sizeof(union acpi_object), GFP_KERNEL);
while (*s) {
if (*s == ' ') {
if (*nargs == 0)
*s = 0; // change first space to nul
++ *nargs;
++ s;
} else {
union acpi_object *arg = (*args) + (*nargs - 1);
if (*s == '"') {
// decode string
arg->type = ACPI_TYPE_STRING;
arg->string.pointer = ++s;
arg->string.length = 0;
while (*s && *s++ != '"') {
arg->string.length ++;
}
} else if (*s == '{') {
// decode buffer
arg->type = ACPI_TYPE_BUFFER;
arg->buffer.pointer = buf;
arg->buffer.length = 0;
while (*s && *s++ != '}') {
if (buf >= buffer_data + sizeof(buffer_data))
printk(KERN_INFO "buffer full\n");
else if (*s >= '0' && *s <= '9') {
// decode integer into buffer
arg->buffer.length ++;
if (s[0] == '0' && s[1] == 'x')
*buf++ = simple_strtol(s+2, 0, 16);
else
*buf++ = simple_strtol(s, 0, 10);
printk(KERN_INFO "Buffer [%d] = 0x%x\n",
arg->buffer.length-1, buf[-1]);
}
// skip until space or comma or '}'
while (*s && *s != ' ' && *s != ',' && *s != '}')
++s;
}
} else {
// decode integer, N or 0xN
arg->type = ACPI_TYPE_INTEGER;
if (s[0] == '0' && s[1] == 'x') {
arg->integer.value = simple_strtol(s+2, 0, 16);
} else {
arg->integer.value = simple_strtol(s, 0, 10);
}
while (*s && *s != ' ') {
++s;
}
}
}
}
return input;
}
/** procfs write callback. Called when writing into /proc/acpi/call.
*/
static int acpi_proc_write( struct file *filp, const char __user *buff,
unsigned long len, void *data )
{
char input[512] = { '\0' };
union acpi_object *args;
int nargs;
char *method;
if (len > sizeof(input) - 1) {
printk(KERN_ERR "acpi_call: Input too long! (%lu)\n", len);
return -ENOSPC;
}
if (copy_from_user( input, buff, len )) {
return -EFAULT;
}
input[len] = '\0';
if (input[len-1] == '\n')
input[len-1] = '\0';
method = parse_acpi_args(input, &nargs, &args);
do_acpi_call(method, nargs, args);
kfree(args);
return len;
}
/** procfs 'call' read callback. Called when reading the content of /proc/acpi/call.
Returns the last call status:
- "not called" when no call was previously issued
- "failed" if the call failed
- "ok" if the call succeeded
*/
static int acpi_proc_read(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
int len = 0;
if (off > 0) {
*eof = 1;
return 0;
}
len = strlen(result_buffer);
memcpy(page, result_buffer, len + 1);
strcpy(result_buffer, "not called");
return len;
}
/** module initialization function */
static int __init init_acpi_call(void)
{
struct proc_dir_entry *acpi_entry = create_proc_entry("call", 0666, acpi_root_dir);
strcpy(result_buffer, "not called");
if (acpi_entry == NULL) {
printk(KERN_ERR "acpi_call: Couldn't create proc entry\n");
return -ENOMEM;
}
acpi_entry->write_proc = acpi_proc_write;
acpi_entry->read_proc = acpi_proc_read;
printk(KERN_INFO "acpi_call: Module loaded successfully\n");
return 0;
}
static void unload_acpi_call(void)
{
remove_proc_entry("call", acpi_root_dir);
}
module_init(init_acpi_call);
module_exit(unload_acpi_call);