Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
01f22462 | 2 | * ec.c - ACPI Embedded Controller Driver (v2.0) |
1da177e4 | 3 | * |
01f22462 AS |
4 | * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
5 | * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com> | |
1da177e4 LT |
6 | * Copyright (C) 2004 Luming Yu <luming.yu@intel.com> |
7 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
8 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
9 | * | |
10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
25 | * | |
26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
27 | */ | |
28 | ||
29 | #include <linux/kernel.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/types.h> | |
33 | #include <linux/delay.h> | |
34 | #include <linux/proc_fs.h> | |
35 | #include <linux/seq_file.h> | |
451566f4 | 36 | #include <linux/interrupt.h> |
837012ed | 37 | #include <linux/list.h> |
1da177e4 LT |
38 | #include <asm/io.h> |
39 | #include <acpi/acpi_bus.h> | |
40 | #include <acpi/acpi_drivers.h> | |
41 | #include <acpi/actypes.h> | |
42 | ||
1da177e4 | 43 | #define ACPI_EC_CLASS "embedded_controller" |
1da177e4 LT |
44 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
45 | #define ACPI_EC_FILE_INFO "info" | |
837012ed | 46 | |
af3fd140 AS |
47 | #undef PREFIX |
48 | #define PREFIX "ACPI: EC: " | |
4350933a | 49 | |
703959d4 | 50 | /* EC status register */ |
1da177e4 LT |
51 | #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ |
52 | #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ | |
451566f4 | 53 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
1da177e4 | 54 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
4350933a | 55 | |
703959d4 | 56 | /* EC commands */ |
3261ff4d | 57 | enum ec_command { |
6ccedb10 AS |
58 | ACPI_EC_COMMAND_READ = 0x80, |
59 | ACPI_EC_COMMAND_WRITE = 0x81, | |
60 | ACPI_EC_BURST_ENABLE = 0x82, | |
61 | ACPI_EC_BURST_DISABLE = 0x83, | |
62 | ACPI_EC_COMMAND_QUERY = 0x84, | |
3261ff4d | 63 | }; |
837012ed | 64 | |
703959d4 | 65 | /* EC events */ |
3261ff4d | 66 | enum ec_event { |
703959d4 | 67 | ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */ |
080e412c | 68 | ACPI_EC_EVENT_IBF_0, /* Input buffer empty */ |
703959d4 DS |
69 | }; |
70 | ||
5c406412 | 71 | #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ |
703959d4 | 72 | #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ |
703959d4 | 73 | |
080e412c AS |
74 | enum { |
75 | EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */ | |
76 | EC_FLAGS_QUERY_PENDING, /* Query is pending */ | |
7843932a | 77 | EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */ |
080e412c AS |
78 | }; |
79 | ||
50526df6 LB |
80 | static int acpi_ec_remove(struct acpi_device *device, int type); |
81 | static int acpi_ec_start(struct acpi_device *device); | |
82 | static int acpi_ec_stop(struct acpi_device *device, int type); | |
703959d4 | 83 | static int acpi_ec_add(struct acpi_device *device); |
1da177e4 | 84 | |
1ba90e3a TR |
85 | static const struct acpi_device_id ec_device_ids[] = { |
86 | {"PNP0C09", 0}, | |
87 | {"", 0}, | |
88 | }; | |
89 | ||
1da177e4 | 90 | static struct acpi_driver acpi_ec_driver = { |
c2b6705b | 91 | .name = "ec", |
50526df6 | 92 | .class = ACPI_EC_CLASS, |
1ba90e3a | 93 | .ids = ec_device_ids, |
50526df6 | 94 | .ops = { |
703959d4 | 95 | .add = acpi_ec_add, |
50526df6 LB |
96 | .remove = acpi_ec_remove, |
97 | .start = acpi_ec_start, | |
98 | .stop = acpi_ec_stop, | |
99 | }, | |
1da177e4 | 100 | }; |
6ffb221a DS |
101 | |
102 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ | |
d033879c | 103 | /* External interfaces use first EC only, so remember */ |
837012ed AS |
104 | typedef int (*acpi_ec_query_func) (void *data); |
105 | ||
106 | struct acpi_ec_query_handler { | |
107 | struct list_head node; | |
108 | acpi_ec_query_func func; | |
109 | acpi_handle handle; | |
110 | void *data; | |
111 | u8 query_bit; | |
112 | }; | |
113 | ||
a854e08a | 114 | static struct acpi_ec { |
703959d4 | 115 | acpi_handle handle; |
a86e2772 | 116 | unsigned long gpe; |
6ffb221a DS |
117 | unsigned long command_addr; |
118 | unsigned long data_addr; | |
703959d4 | 119 | unsigned long global_lock; |
080e412c | 120 | unsigned long flags; |
c787a855 | 121 | struct mutex lock; |
703959d4 | 122 | wait_queue_head_t wait; |
837012ed | 123 | struct list_head list; |
4c611060 | 124 | u8 handlers_installed; |
d033879c | 125 | } *boot_ec, *first_ec; |
703959d4 | 126 | |
1da177e4 LT |
127 | /* -------------------------------------------------------------------------- |
128 | Transaction Management | |
129 | -------------------------------------------------------------------------- */ | |
130 | ||
6ffb221a | 131 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 132 | { |
6ffb221a | 133 | return inb(ec->command_addr); |
451566f4 DT |
134 | } |
135 | ||
6ffb221a | 136 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 137 | { |
6ffb221a | 138 | return inb(ec->data_addr); |
7c6db5e5 DS |
139 | } |
140 | ||
6ffb221a | 141 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 142 | { |
6ffb221a | 143 | outb(command, ec->command_addr); |
45bea155 LY |
144 | } |
145 | ||
6ffb221a | 146 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 147 | { |
6ffb221a | 148 | outb(data, ec->data_addr); |
703959d4 | 149 | } |
45bea155 | 150 | |
080e412c | 151 | static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event) |
6ffb221a | 152 | { |
080e412c | 153 | if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags)) |
9e197219 | 154 | return 0; |
78d0af33 | 155 | if (event == ACPI_EC_EVENT_OBF_1) { |
080e412c | 156 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF) |
703959d4 | 157 | return 1; |
78d0af33 | 158 | } else if (event == ACPI_EC_EVENT_IBF_0) { |
080e412c | 159 | if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)) |
703959d4 | 160 | return 1; |
45bea155 LY |
161 | } |
162 | ||
703959d4 | 163 | return 0; |
45bea155 | 164 | } |
451566f4 | 165 | |
080e412c | 166 | static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll) |
7c6db5e5 | 167 | { |
7843932a AS |
168 | if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) && |
169 | likely(!force_poll)) { | |
080e412c AS |
170 | if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event), |
171 | msecs_to_jiffies(ACPI_EC_DELAY))) | |
172 | return 0; | |
173 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); | |
174 | if (acpi_ec_check_status(ec, event)) { | |
7843932a | 175 | clear_bit(EC_FLAGS_GPE_MODE, &ec->flags); |
703959d4 DS |
176 | return 0; |
177 | } | |
7843932a AS |
178 | } else { |
179 | unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); | |
180 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); | |
181 | while (time_before(jiffies, delay)) { | |
182 | if (acpi_ec_check_status(ec, event)) | |
183 | return 0; | |
184 | } | |
af3fd140 | 185 | } |
080e412c AS |
186 | printk(KERN_ERR PREFIX "acpi_ec_wait timeout," |
187 | " status = %d, expect_event = %d\n", | |
188 | acpi_ec_read_status(ec), event); | |
d550d98d | 189 | return -ETIME; |
1da177e4 LT |
190 | } |
191 | ||
703959d4 | 192 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, |
6ccedb10 | 193 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
194 | u8 * rdata, unsigned rdata_len, |
195 | int force_poll) | |
45bea155 | 196 | { |
af3fd140 | 197 | int result = 0; |
080e412c | 198 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
703959d4 | 199 | acpi_ec_write_cmd(ec, command); |
45bea155 | 200 | |
78d0af33 | 201 | for (; wdata_len > 0; --wdata_len) { |
080e412c | 202 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
af3fd140 | 203 | if (result) { |
6ccedb10 AS |
204 | printk(KERN_ERR PREFIX |
205 | "write_cmd timeout, command = %d\n", command); | |
af3fd140 AS |
206 | goto end; |
207 | } | |
080e412c | 208 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
703959d4 | 209 | acpi_ec_write_data(ec, *(wdata++)); |
3576cf61 | 210 | } |
45bea155 | 211 | |
d91df1aa | 212 | if (!rdata_len) { |
080e412c | 213 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
af3fd140 | 214 | if (result) { |
6ccedb10 AS |
215 | printk(KERN_ERR PREFIX |
216 | "finish-write timeout, command = %d\n", command); | |
af3fd140 AS |
217 | goto end; |
218 | } | |
080e412c AS |
219 | } else if (command == ACPI_EC_COMMAND_QUERY) |
220 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); | |
45bea155 | 221 | |
78d0af33 | 222 | for (; rdata_len > 0; --rdata_len) { |
080e412c | 223 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll); |
af3fd140 AS |
224 | if (result) { |
225 | printk(KERN_ERR PREFIX "read timeout, command = %d\n", | |
6ccedb10 | 226 | command); |
af3fd140 AS |
227 | goto end; |
228 | } | |
0c5d31f4 AS |
229 | /* Don't expect GPE after last read */ |
230 | if (rdata_len > 1) | |
231 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); | |
6ffb221a | 232 | *(rdata++) = acpi_ec_read_data(ec); |
7c6db5e5 | 233 | } |
af3fd140 AS |
234 | end: |
235 | return result; | |
45bea155 LY |
236 | } |
237 | ||
3576cf61 | 238 | static int acpi_ec_transaction(struct acpi_ec *ec, u8 command, |
6ccedb10 | 239 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
240 | u8 * rdata, unsigned rdata_len, |
241 | int force_poll) | |
1da177e4 | 242 | { |
d7a76e4c | 243 | int status; |
50526df6 | 244 | u32 glk; |
1da177e4 | 245 | |
d7a76e4c | 246 | if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata)) |
d550d98d | 247 | return -EINVAL; |
1da177e4 | 248 | |
6ccedb10 AS |
249 | if (rdata) |
250 | memset(rdata, 0, rdata_len); | |
1da177e4 | 251 | |
523953b4 | 252 | mutex_lock(&ec->lock); |
703959d4 | 253 | if (ec->global_lock) { |
1da177e4 | 254 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b AS |
255 | if (ACPI_FAILURE(status)) { |
256 | mutex_unlock(&ec->lock); | |
d550d98d | 257 | return -ENODEV; |
c24e912b | 258 | } |
1da177e4 | 259 | } |
451566f4 | 260 | |
080e412c | 261 | status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0); |
716e084e | 262 | if (status) { |
4350933a | 263 | printk(KERN_ERR PREFIX |
6ccedb10 | 264 | "input buffer is not empty, aborting transaction\n"); |
451566f4 | 265 | goto end; |
716e084e | 266 | } |
1da177e4 | 267 | |
6ccedb10 AS |
268 | status = acpi_ec_transaction_unlocked(ec, command, |
269 | wdata, wdata_len, | |
00eb43a1 LP |
270 | rdata, rdata_len, |
271 | force_poll); | |
1da177e4 | 272 | |
6ccedb10 | 273 | end: |
1da177e4 | 274 | |
703959d4 | 275 | if (ec->global_lock) |
1da177e4 | 276 | acpi_release_global_lock(glk); |
523953b4 | 277 | mutex_unlock(&ec->lock); |
1da177e4 | 278 | |
d550d98d | 279 | return status; |
1da177e4 LT |
280 | } |
281 | ||
c45aac43 AS |
282 | /* |
283 | * Note: samsung nv5000 doesn't work with ec burst mode. | |
284 | * http://bugzilla.kernel.org/show_bug.cgi?id=4980 | |
285 | */ | |
286 | int acpi_ec_burst_enable(struct acpi_ec *ec) | |
287 | { | |
288 | u8 d; | |
00eb43a1 | 289 | return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0); |
c45aac43 AS |
290 | } |
291 | ||
292 | int acpi_ec_burst_disable(struct acpi_ec *ec) | |
293 | { | |
00eb43a1 | 294 | return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0); |
c45aac43 AS |
295 | } |
296 | ||
6ccedb10 | 297 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
3576cf61 DS |
298 | { |
299 | int result; | |
300 | u8 d; | |
301 | ||
302 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, | |
00eb43a1 | 303 | &address, 1, &d, 1, 0); |
3576cf61 DS |
304 | *data = d; |
305 | return result; | |
306 | } | |
6ffb221a | 307 | |
3576cf61 DS |
308 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
309 | { | |
6ccedb10 AS |
310 | u8 wdata[2] = { address, data }; |
311 | return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, | |
00eb43a1 | 312 | wdata, 2, NULL, 0, 0); |
3576cf61 DS |
313 | } |
314 | ||
1da177e4 LT |
315 | /* |
316 | * Externally callable EC access functions. For now, assume 1 EC only | |
317 | */ | |
c45aac43 AS |
318 | int ec_burst_enable(void) |
319 | { | |
c45aac43 AS |
320 | if (!first_ec) |
321 | return -ENODEV; | |
d033879c | 322 | return acpi_ec_burst_enable(first_ec); |
c45aac43 AS |
323 | } |
324 | ||
325 | EXPORT_SYMBOL(ec_burst_enable); | |
326 | ||
327 | int ec_burst_disable(void) | |
328 | { | |
c45aac43 AS |
329 | if (!first_ec) |
330 | return -ENODEV; | |
d033879c | 331 | return acpi_ec_burst_disable(first_ec); |
c45aac43 AS |
332 | } |
333 | ||
334 | EXPORT_SYMBOL(ec_burst_disable); | |
335 | ||
6ccedb10 | 336 | int ec_read(u8 addr, u8 * val) |
1da177e4 | 337 | { |
1da177e4 | 338 | int err; |
6ffb221a | 339 | u8 temp_data; |
1da177e4 LT |
340 | |
341 | if (!first_ec) | |
342 | return -ENODEV; | |
343 | ||
d033879c | 344 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
345 | |
346 | if (!err) { | |
347 | *val = temp_data; | |
348 | return 0; | |
50526df6 | 349 | } else |
1da177e4 LT |
350 | return err; |
351 | } | |
50526df6 | 352 | |
1da177e4 LT |
353 | EXPORT_SYMBOL(ec_read); |
354 | ||
50526df6 | 355 | int ec_write(u8 addr, u8 val) |
1da177e4 | 356 | { |
1da177e4 LT |
357 | int err; |
358 | ||
359 | if (!first_ec) | |
360 | return -ENODEV; | |
361 | ||
d033879c | 362 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
363 | |
364 | return err; | |
365 | } | |
50526df6 | 366 | |
1da177e4 LT |
367 | EXPORT_SYMBOL(ec_write); |
368 | ||
616362de | 369 | int ec_transaction(u8 command, |
9e197219 | 370 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
371 | u8 * rdata, unsigned rdata_len, |
372 | int force_poll) | |
45bea155 | 373 | { |
d7a76e4c LP |
374 | if (!first_ec) |
375 | return -ENODEV; | |
45bea155 | 376 | |
d033879c | 377 | return acpi_ec_transaction(first_ec, command, wdata, |
00eb43a1 LP |
378 | wdata_len, rdata, rdata_len, |
379 | force_poll); | |
45bea155 | 380 | } |
1da177e4 | 381 | |
ab9e43c6 LP |
382 | EXPORT_SYMBOL(ec_transaction); |
383 | ||
6ccedb10 | 384 | static int acpi_ec_query(struct acpi_ec *ec, u8 * data) |
3576cf61 DS |
385 | { |
386 | int result; | |
6ccedb10 | 387 | u8 d; |
1da177e4 | 388 | |
6ccedb10 AS |
389 | if (!ec || !data) |
390 | return -EINVAL; | |
1da177e4 | 391 | |
6ccedb10 AS |
392 | /* |
393 | * Query the EC to find out which _Qxx method we need to evaluate. | |
394 | * Note that successful completion of the query causes the ACPI_EC_SCI | |
395 | * bit to be cleared (and thus clearing the interrupt source). | |
396 | */ | |
716e084e | 397 | |
00eb43a1 | 398 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0); |
6ccedb10 AS |
399 | if (result) |
400 | return result; | |
1da177e4 | 401 | |
6ccedb10 AS |
402 | if (!d) |
403 | return -ENODATA; | |
1da177e4 | 404 | |
6ccedb10 AS |
405 | *data = d; |
406 | return 0; | |
1da177e4 LT |
407 | } |
408 | ||
1da177e4 LT |
409 | /* -------------------------------------------------------------------------- |
410 | Event Management | |
411 | -------------------------------------------------------------------------- */ | |
837012ed AS |
412 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
413 | acpi_handle handle, acpi_ec_query_func func, | |
414 | void *data) | |
415 | { | |
416 | struct acpi_ec_query_handler *handler = | |
417 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); | |
418 | if (!handler) | |
419 | return -ENOMEM; | |
420 | ||
421 | handler->query_bit = query_bit; | |
422 | handler->handle = handle; | |
423 | handler->func = func; | |
424 | handler->data = data; | |
425 | mutex_lock(&ec->lock); | |
30c08574 | 426 | list_add(&handler->node, &ec->list); |
837012ed AS |
427 | mutex_unlock(&ec->lock); |
428 | return 0; | |
429 | } | |
430 | ||
431 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); | |
432 | ||
433 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) | |
434 | { | |
435 | struct acpi_ec_query_handler *handler; | |
436 | mutex_lock(&ec->lock); | |
437 | list_for_each_entry(handler, &ec->list, node) { | |
438 | if (query_bit == handler->query_bit) { | |
439 | list_del(&handler->node); | |
440 | kfree(handler); | |
837012ed AS |
441 | } |
442 | } | |
443 | mutex_unlock(&ec->lock); | |
444 | } | |
445 | ||
446 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); | |
1da177e4 | 447 | |
50526df6 | 448 | static void acpi_ec_gpe_query(void *ec_cxt) |
45bea155 | 449 | { |
3d02b90b | 450 | struct acpi_ec *ec = ec_cxt; |
6ffb221a | 451 | u8 value = 0; |
837012ed | 452 | struct acpi_ec_query_handler *handler, copy; |
45bea155 | 453 | |
5d0c288b | 454 | if (!ec || acpi_ec_query(ec, &value)) |
e41334c0 | 455 | return; |
837012ed AS |
456 | mutex_lock(&ec->lock); |
457 | list_for_each_entry(handler, &ec->list, node) { | |
458 | if (value == handler->query_bit) { | |
459 | /* have custom handler for this bit */ | |
460 | memcpy(©, handler, sizeof(copy)); | |
461 | mutex_unlock(&ec->lock); | |
462 | if (copy.func) { | |
463 | copy.func(copy.data); | |
464 | } else if (copy.handle) { | |
465 | acpi_evaluate_object(copy.handle, NULL, NULL, NULL); | |
466 | } | |
467 | return; | |
468 | } | |
469 | } | |
470 | mutex_unlock(&ec->lock); | |
45bea155 | 471 | } |
1da177e4 | 472 | |
50526df6 | 473 | static u32 acpi_ec_gpe_handler(void *data) |
1da177e4 | 474 | { |
50526df6 | 475 | acpi_status status = AE_OK; |
3d02b90b | 476 | struct acpi_ec *ec = data; |
00eb43a1 | 477 | |
080e412c | 478 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
7843932a | 479 | if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) |
af3fd140 | 480 | wake_up(&ec->wait); |
451566f4 | 481 | |
080e412c AS |
482 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) { |
483 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) | |
484 | status = acpi_os_execute(OSL_EC_BURST_HANDLER, | |
485 | acpi_ec_gpe_query, ec); | |
7843932a AS |
486 | } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) |
487 | set_bit(EC_FLAGS_GPE_MODE, &ec->flags); | |
e41334c0 | 488 | |
7843932a | 489 | return ACPI_SUCCESS(status) ? |
50526df6 | 490 | ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED; |
1da177e4 LT |
491 | } |
492 | ||
493 | /* -------------------------------------------------------------------------- | |
494 | Address Space Management | |
495 | -------------------------------------------------------------------------- */ | |
496 | ||
497 | static acpi_status | |
50526df6 LB |
498 | acpi_ec_space_setup(acpi_handle region_handle, |
499 | u32 function, void *handler_context, void **return_context) | |
1da177e4 LT |
500 | { |
501 | /* | |
502 | * The EC object is in the handler context and is needed | |
503 | * when calling the acpi_ec_space_handler. | |
504 | */ | |
50526df6 LB |
505 | *return_context = (function != ACPI_REGION_DEACTIVATE) ? |
506 | handler_context : NULL; | |
1da177e4 LT |
507 | |
508 | return AE_OK; | |
509 | } | |
510 | ||
1da177e4 | 511 | static acpi_status |
5b7734b4 AS |
512 | acpi_ec_space_handler(u32 function, acpi_physical_address address, |
513 | u32 bits, acpi_integer *value, | |
50526df6 | 514 | void *handler_context, void *region_context) |
1da177e4 | 515 | { |
3d02b90b | 516 | struct acpi_ec *ec = handler_context; |
5b7734b4 AS |
517 | int result = 0, i = 0; |
518 | u8 temp = 0; | |
1da177e4 | 519 | |
1da177e4 | 520 | if ((address > 0xFF) || !value || !handler_context) |
d550d98d | 521 | return AE_BAD_PARAMETER; |
1da177e4 | 522 | |
5b7734b4 | 523 | if (function != ACPI_READ && function != ACPI_WRITE) |
d550d98d | 524 | return AE_BAD_PARAMETER; |
1da177e4 | 525 | |
5b7734b4 AS |
526 | if (bits != 8 && acpi_strict) |
527 | return AE_BAD_PARAMETER; | |
1da177e4 | 528 | |
5b7734b4 AS |
529 | while (bits - i > 0) { |
530 | if (function == ACPI_READ) { | |
531 | result = acpi_ec_read(ec, address, &temp); | |
532 | (*value) |= ((acpi_integer)temp) << i; | |
533 | } else { | |
534 | temp = 0xff & ((*value) >> i); | |
535 | result = acpi_ec_write(ec, address, temp); | |
536 | } | |
537 | i += 8; | |
538 | ++address; | |
1da177e4 LT |
539 | } |
540 | ||
1da177e4 LT |
541 | switch (result) { |
542 | case -EINVAL: | |
d550d98d | 543 | return AE_BAD_PARAMETER; |
1da177e4 LT |
544 | break; |
545 | case -ENODEV: | |
d550d98d | 546 | return AE_NOT_FOUND; |
1da177e4 LT |
547 | break; |
548 | case -ETIME: | |
d550d98d | 549 | return AE_TIME; |
1da177e4 LT |
550 | break; |
551 | default: | |
d550d98d | 552 | return AE_OK; |
1da177e4 | 553 | } |
1da177e4 LT |
554 | } |
555 | ||
1da177e4 LT |
556 | /* -------------------------------------------------------------------------- |
557 | FS Interface (/proc) | |
558 | -------------------------------------------------------------------------- */ | |
559 | ||
50526df6 | 560 | static struct proc_dir_entry *acpi_ec_dir; |
1da177e4 | 561 | |
50526df6 | 562 | static int acpi_ec_read_info(struct seq_file *seq, void *offset) |
1da177e4 | 563 | { |
3d02b90b | 564 | struct acpi_ec *ec = seq->private; |
1da177e4 | 565 | |
1da177e4 LT |
566 | if (!ec) |
567 | goto end; | |
568 | ||
01f22462 AS |
569 | seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe); |
570 | seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n", | |
571 | (unsigned)ec->command_addr, (unsigned)ec->data_addr); | |
572 | seq_printf(seq, "use global lock:\t%s\n", | |
703959d4 | 573 | ec->global_lock ? "yes" : "no"); |
50526df6 | 574 | end: |
d550d98d | 575 | return 0; |
1da177e4 LT |
576 | } |
577 | ||
578 | static int acpi_ec_info_open_fs(struct inode *inode, struct file *file) | |
579 | { | |
580 | return single_open(file, acpi_ec_read_info, PDE(inode)->data); | |
581 | } | |
582 | ||
703959d4 | 583 | static struct file_operations acpi_ec_info_ops = { |
50526df6 LB |
584 | .open = acpi_ec_info_open_fs, |
585 | .read = seq_read, | |
586 | .llseek = seq_lseek, | |
587 | .release = single_release, | |
1da177e4 LT |
588 | .owner = THIS_MODULE, |
589 | }; | |
590 | ||
50526df6 | 591 | static int acpi_ec_add_fs(struct acpi_device *device) |
1da177e4 | 592 | { |
50526df6 | 593 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 594 | |
1da177e4 LT |
595 | if (!acpi_device_dir(device)) { |
596 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
50526df6 | 597 | acpi_ec_dir); |
1da177e4 | 598 | if (!acpi_device_dir(device)) |
d550d98d | 599 | return -ENODEV; |
1da177e4 LT |
600 | } |
601 | ||
602 | entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO, | |
50526df6 | 603 | acpi_device_dir(device)); |
1da177e4 | 604 | if (!entry) |
d550d98d | 605 | return -ENODEV; |
1da177e4 LT |
606 | else { |
607 | entry->proc_fops = &acpi_ec_info_ops; | |
608 | entry->data = acpi_driver_data(device); | |
609 | entry->owner = THIS_MODULE; | |
610 | } | |
611 | ||
d550d98d | 612 | return 0; |
1da177e4 LT |
613 | } |
614 | ||
50526df6 | 615 | static int acpi_ec_remove_fs(struct acpi_device *device) |
1da177e4 | 616 | { |
1da177e4 LT |
617 | |
618 | if (acpi_device_dir(device)) { | |
619 | remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device)); | |
620 | remove_proc_entry(acpi_device_bid(device), acpi_ec_dir); | |
621 | acpi_device_dir(device) = NULL; | |
622 | } | |
623 | ||
d550d98d | 624 | return 0; |
1da177e4 LT |
625 | } |
626 | ||
1da177e4 LT |
627 | /* -------------------------------------------------------------------------- |
628 | Driver Interface | |
629 | -------------------------------------------------------------------------- */ | |
c0900c35 AS |
630 | static acpi_status |
631 | ec_parse_io_ports(struct acpi_resource *resource, void *context); | |
632 | ||
c0900c35 AS |
633 | static struct acpi_ec *make_acpi_ec(void) |
634 | { | |
635 | struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); | |
636 | if (!ec) | |
637 | return NULL; | |
080e412c | 638 | ec->flags = 1 << EC_FLAGS_QUERY_PENDING; |
c0900c35 AS |
639 | mutex_init(&ec->lock); |
640 | init_waitqueue_head(&ec->wait); | |
837012ed | 641 | INIT_LIST_HEAD(&ec->list); |
c0900c35 AS |
642 | return ec; |
643 | } | |
837012ed | 644 | |
c019b193 AS |
645 | static acpi_status |
646 | acpi_ec_register_query_methods(acpi_handle handle, u32 level, | |
647 | void *context, void **return_value) | |
648 | { | |
649 | struct acpi_namespace_node *node = handle; | |
650 | struct acpi_ec *ec = context; | |
651 | int value = 0; | |
652 | if (sscanf(node->name.ascii, "_Q%x", &value) == 1) { | |
653 | acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); | |
654 | } | |
655 | return AE_OK; | |
656 | } | |
657 | ||
cd8c93a4 AS |
658 | static acpi_status |
659 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
837012ed | 660 | { |
cd8c93a4 AS |
661 | acpi_status status; |
662 | ||
663 | struct acpi_ec *ec = context; | |
664 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, | |
665 | ec_parse_io_ports, ec); | |
666 | if (ACPI_FAILURE(status)) | |
667 | return status; | |
837012ed AS |
668 | |
669 | /* Get GPE bit assignment (EC events). */ | |
670 | /* TODO: Add support for _GPE returning a package */ | |
cd8c93a4 AS |
671 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe); |
672 | if (ACPI_FAILURE(status)) | |
673 | return status; | |
c019b193 AS |
674 | /* Find and register all query methods */ |
675 | acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1, | |
676 | acpi_ec_register_query_methods, ec, NULL); | |
837012ed AS |
677 | /* Use the global lock for all EC transactions? */ |
678 | acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); | |
837012ed | 679 | ec->handle = handle; |
cd8c93a4 | 680 | return AE_CTRL_TERMINATE; |
837012ed AS |
681 | } |
682 | ||
4c611060 AS |
683 | static void ec_remove_handlers(struct acpi_ec *ec) |
684 | { | |
685 | if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, | |
686 | ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) | |
687 | printk(KERN_ERR PREFIX "failed to remove space handler\n"); | |
688 | if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, | |
689 | &acpi_ec_gpe_handler))) | |
690 | printk(KERN_ERR PREFIX "failed to remove gpe handler\n"); | |
691 | ec->handlers_installed = 0; | |
692 | } | |
693 | ||
703959d4 | 694 | static int acpi_ec_add(struct acpi_device *device) |
1da177e4 | 695 | { |
703959d4 | 696 | struct acpi_ec *ec = NULL; |
45bea155 | 697 | |
45bea155 | 698 | if (!device) |
d550d98d | 699 | return -EINVAL; |
c0900c35 AS |
700 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
701 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
702 | ||
4c611060 AS |
703 | /* Check for boot EC */ |
704 | if (boot_ec) { | |
705 | if (boot_ec->handle == device->handle) { | |
706 | /* Pre-loaded EC from DSDT, just move pointer */ | |
707 | ec = boot_ec; | |
708 | boot_ec = NULL; | |
709 | goto end; | |
710 | } else if (boot_ec->handle == ACPI_ROOT_OBJECT) { | |
711 | /* ECDT-based EC, time to shut it down */ | |
712 | ec_remove_handlers(boot_ec); | |
713 | kfree(boot_ec); | |
714 | first_ec = boot_ec = NULL; | |
715 | } | |
716 | } | |
717 | ||
c0900c35 | 718 | ec = make_acpi_ec(); |
45bea155 | 719 | if (!ec) |
d550d98d | 720 | return -ENOMEM; |
703959d4 | 721 | |
cd8c93a4 AS |
722 | if (ec_parse_device(device->handle, 0, ec, NULL) != |
723 | AE_CTRL_TERMINATE) { | |
c0900c35 AS |
724 | kfree(ec); |
725 | return -EINVAL; | |
45bea155 | 726 | } |
c0900c35 | 727 | ec->handle = device->handle; |
4c611060 AS |
728 | end: |
729 | if (!first_ec) | |
730 | first_ec = ec; | |
c0900c35 | 731 | acpi_driver_data(device) = ec; |
c0900c35 | 732 | acpi_ec_add_fs(device); |
4c611060 AS |
733 | printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n", |
734 | ec->gpe, ec->command_addr, ec->data_addr); | |
c0900c35 | 735 | return 0; |
1da177e4 LT |
736 | } |
737 | ||
50526df6 | 738 | static int acpi_ec_remove(struct acpi_device *device, int type) |
1da177e4 | 739 | { |
01f22462 | 740 | struct acpi_ec *ec; |
07ddf768 | 741 | struct acpi_ec_query_handler *handler, *tmp; |
1da177e4 | 742 | |
1da177e4 | 743 | if (!device) |
d550d98d | 744 | return -EINVAL; |
1da177e4 LT |
745 | |
746 | ec = acpi_driver_data(device); | |
837012ed | 747 | mutex_lock(&ec->lock); |
07ddf768 | 748 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
837012ed AS |
749 | list_del(&handler->node); |
750 | kfree(handler); | |
751 | } | |
752 | mutex_unlock(&ec->lock); | |
1da177e4 | 753 | acpi_ec_remove_fs(device); |
c0900c35 | 754 | acpi_driver_data(device) = NULL; |
d033879c | 755 | if (ec == first_ec) |
c0900c35 | 756 | first_ec = NULL; |
4c611060 | 757 | kfree(ec); |
d550d98d | 758 | return 0; |
1da177e4 LT |
759 | } |
760 | ||
1da177e4 | 761 | static acpi_status |
c0900c35 | 762 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 763 | { |
3d02b90b | 764 | struct acpi_ec *ec = context; |
1da177e4 | 765 | |
01f22462 | 766 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 767 | return AE_OK; |
1da177e4 LT |
768 | |
769 | /* | |
770 | * The first address region returned is the data port, and | |
771 | * the second address region returned is the status/command | |
772 | * port. | |
773 | */ | |
01f22462 | 774 | if (ec->data_addr == 0) |
6ffb221a | 775 | ec->data_addr = resource->data.io.minimum; |
01f22462 | 776 | else if (ec->command_addr == 0) |
6ffb221a | 777 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 778 | else |
1da177e4 | 779 | return AE_CTRL_TERMINATE; |
1da177e4 | 780 | |
1da177e4 LT |
781 | return AE_OK; |
782 | } | |
783 | ||
e8284321 AS |
784 | static int ec_install_handlers(struct acpi_ec *ec) |
785 | { | |
c0900c35 | 786 | acpi_status status; |
4c611060 AS |
787 | if (ec->handlers_installed) |
788 | return 0; | |
c0900c35 AS |
789 | status = acpi_install_gpe_handler(NULL, ec->gpe, |
790 | ACPI_GPE_EDGE_TRIGGERED, | |
791 | &acpi_ec_gpe_handler, ec); | |
e8284321 AS |
792 | if (ACPI_FAILURE(status)) |
793 | return -ENODEV; | |
01f22462 | 794 | |
e8284321 AS |
795 | acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME); |
796 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); | |
797 | ||
798 | status = acpi_install_address_space_handler(ec->handle, | |
799 | ACPI_ADR_SPACE_EC, | |
800 | &acpi_ec_space_handler, | |
801 | &acpi_ec_space_setup, ec); | |
802 | if (ACPI_FAILURE(status)) { | |
803 | acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); | |
804 | return -ENODEV; | |
805 | } | |
806 | ||
4c611060 | 807 | ec->handlers_installed = 1; |
e8284321 AS |
808 | return 0; |
809 | } | |
810 | ||
50526df6 | 811 | static int acpi_ec_start(struct acpi_device *device) |
1da177e4 | 812 | { |
c0900c35 | 813 | struct acpi_ec *ec; |
837012ed | 814 | int ret = 0; |
1da177e4 | 815 | |
1da177e4 | 816 | if (!device) |
d550d98d | 817 | return -EINVAL; |
1da177e4 LT |
818 | |
819 | ec = acpi_driver_data(device); | |
820 | ||
821 | if (!ec) | |
d550d98d | 822 | return -EINVAL; |
1da177e4 | 823 | |
4c611060 | 824 | ret = ec_install_handlers(ec); |
837012ed AS |
825 | |
826 | /* EC is fully operational, allow queries */ | |
080e412c | 827 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); |
837012ed | 828 | return ret; |
1da177e4 LT |
829 | } |
830 | ||
50526df6 | 831 | static int acpi_ec_stop(struct acpi_device *device, int type) |
1da177e4 | 832 | { |
c0900c35 | 833 | struct acpi_ec *ec; |
1da177e4 | 834 | if (!device) |
d550d98d | 835 | return -EINVAL; |
1da177e4 | 836 | ec = acpi_driver_data(device); |
c0900c35 AS |
837 | if (!ec) |
838 | return -EINVAL; | |
4c611060 | 839 | ec_remove_handlers(ec); |
f9319f90 | 840 | |
d550d98d | 841 | return 0; |
1da177e4 LT |
842 | } |
843 | ||
c0900c35 AS |
844 | int __init acpi_ec_ecdt_probe(void) |
845 | { | |
846 | int ret; | |
847 | acpi_status status; | |
50526df6 | 848 | struct acpi_table_ecdt *ecdt_ptr; |
45bea155 | 849 | |
d66d969d AS |
850 | boot_ec = make_acpi_ec(); |
851 | if (!boot_ec) | |
c0900c35 AS |
852 | return -ENOMEM; |
853 | /* | |
854 | * Generate a boot ec context | |
855 | */ | |
15a58ed1 AS |
856 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
857 | (struct acpi_table_header **)&ecdt_ptr); | |
cd8c93a4 | 858 | if (ACPI_SUCCESS(status)) { |
4c611060 | 859 | printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n"); |
cd8c93a4 AS |
860 | boot_ec->command_addr = ecdt_ptr->control.address; |
861 | boot_ec->data_addr = ecdt_ptr->data.address; | |
862 | boot_ec->gpe = ecdt_ptr->gpe; | |
863 | boot_ec->handle = ACPI_ROOT_OBJECT; | |
864 | } else { | |
865 | printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n"); | |
866 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, | |
867 | boot_ec, NULL); | |
2d8348b4 AS |
868 | /* Check that acpi_get_devices actually find something */ |
869 | if (ACPI_FAILURE(status) || !boot_ec->handle) | |
cd8c93a4 AS |
870 | goto error; |
871 | } | |
1da177e4 | 872 | |
d66d969d | 873 | ret = ec_install_handlers(boot_ec); |
d033879c AS |
874 | if (!ret) { |
875 | first_ec = boot_ec; | |
e8284321 | 876 | return 0; |
d033879c | 877 | } |
c0900c35 | 878 | error: |
d66d969d AS |
879 | kfree(boot_ec); |
880 | boot_ec = NULL; | |
1da177e4 LT |
881 | return -ENODEV; |
882 | } | |
883 | ||
50526df6 | 884 | static int __init acpi_ec_init(void) |
1da177e4 | 885 | { |
50526df6 | 886 | int result = 0; |
1da177e4 | 887 | |
1da177e4 | 888 | if (acpi_disabled) |
d550d98d | 889 | return 0; |
1da177e4 LT |
890 | |
891 | acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir); | |
892 | if (!acpi_ec_dir) | |
d550d98d | 893 | return -ENODEV; |
1da177e4 LT |
894 | |
895 | /* Now register the driver for the EC */ | |
896 | result = acpi_bus_register_driver(&acpi_ec_driver); | |
897 | if (result < 0) { | |
898 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
d550d98d | 899 | return -ENODEV; |
1da177e4 LT |
900 | } |
901 | ||
d550d98d | 902 | return result; |
1da177e4 LT |
903 | } |
904 | ||
905 | subsys_initcall(acpi_ec_init); | |
906 | ||
907 | /* EC driver currently not unloadable */ | |
908 | #if 0 | |
50526df6 | 909 | static void __exit acpi_ec_exit(void) |
1da177e4 | 910 | { |
1da177e4 LT |
911 | |
912 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
913 | ||
914 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
915 | ||
d550d98d | 916 | return; |
1da177e4 | 917 | } |
7843932a | 918 | #endif /* 0 */ |