ACPI: EC: do transaction from interrupt context
[deliverable/linux.git] / drivers / acpi / ec.c
CommitLineData
1da177e4 1/*
7c6db4e0 2 * ec.c - ACPI Embedded Controller Driver (v2.1)
1da177e4 3 *
7c6db4e0 4 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
01f22462 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
7c6db4e0 29/* Uncomment next line to get verbose printout */
d772b3b3
MN
30/* #define DEBUG */
31
1da177e4
LT
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/delay.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
451566f4 39#include <linux/interrupt.h>
837012ed 40#include <linux/list.h>
7c6db4e0 41#include <linux/spinlock.h>
1da177e4
LT
42#include <asm/io.h>
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45#include <acpi/actypes.h>
46
1da177e4 47#define ACPI_EC_CLASS "embedded_controller"
1da177e4
LT
48#define ACPI_EC_DEVICE_NAME "Embedded Controller"
49#define ACPI_EC_FILE_INFO "info"
837012ed 50
af3fd140
AS
51#undef PREFIX
52#define PREFIX "ACPI: EC: "
4350933a 53
703959d4 54/* EC status register */
1da177e4
LT
55#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
56#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
451566f4 57#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
1da177e4 58#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
4350933a 59
703959d4 60/* EC commands */
3261ff4d 61enum ec_command {
6ccedb10
AS
62 ACPI_EC_COMMAND_READ = 0x80,
63 ACPI_EC_COMMAND_WRITE = 0x81,
64 ACPI_EC_BURST_ENABLE = 0x82,
65 ACPI_EC_BURST_DISABLE = 0x83,
66 ACPI_EC_COMMAND_QUERY = 0x84,
3261ff4d 67};
837012ed 68
5c406412 69#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
703959d4 70#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
e6e82a30 71#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
703959d4 72
7c6db4e0
AS
73#define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts
74 per one transaction */
75
080e412c 76enum {
080e412c 77 EC_FLAGS_QUERY_PENDING, /* Query is pending */
7c6db4e0
AS
78 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
79 * for status change */
b77d81b2 80 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
7c6db4e0
AS
81 EC_FLAGS_GPE_STORM, /* GPE storm detected */
82 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
83 * OpReg are installed */
1da177e4 84};
6ffb221a
DS
85
86/* If we find an EC via the ECDT, we need to keep a ptr to its context */
d033879c 87/* External interfaces use first EC only, so remember */
837012ed
AS
88typedef int (*acpi_ec_query_func) (void *data);
89
90struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
93 acpi_handle handle;
94 void *data;
95 u8 query_bit;
96};
97
7c6db4e0
AS
98struct transaction_data {
99 const u8 *wdata;
100 u8 *rdata;
101 unsigned short irq_count;
102 u8 wlen;
103 u8 rlen;
104};
105
a854e08a 106static struct acpi_ec {
703959d4 107 acpi_handle handle;
a86e2772 108 unsigned long gpe;
6ffb221a
DS
109 unsigned long command_addr;
110 unsigned long data_addr;
703959d4 111 unsigned long global_lock;
080e412c 112 unsigned long flags;
c787a855 113 struct mutex lock;
703959d4 114 wait_queue_head_t wait;
837012ed 115 struct list_head list;
7c6db4e0
AS
116 struct transaction_data *t;
117 spinlock_t t_lock;
d033879c 118} *boot_ec, *first_ec;
703959d4 119
2500822b
ZY
120/*
121 * Some Asus system have exchanged ECDT data/command IO addresses.
122 */
123static int print_ecdt_error(const struct dmi_system_id *id)
124{
125 printk(KERN_NOTICE PREFIX "%s detected - "
126 "ECDT has exchanged control/data I/O address\n",
127 id->ident);
128 return 0;
129}
130
131static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
132 {
133 print_ecdt_error, "Asus L4R", {
134 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
135 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
136 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
137 {
138 print_ecdt_error, "Asus M6R", {
139 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
141 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
142 {},
143};
144
1da177e4
LT
145/* --------------------------------------------------------------------------
146 Transaction Management
147 -------------------------------------------------------------------------- */
148
6ffb221a 149static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
1da177e4 150{
3ebe08a7 151 u8 x = inb(ec->command_addr);
86dae015 152 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
3ebe08a7 153 return x;
451566f4
DT
154}
155
6ffb221a 156static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
703959d4 157{
3ebe08a7 158 u8 x = inb(ec->data_addr);
86dae015 159 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
7c6db4e0 160 return x;
7c6db5e5
DS
161}
162
6ffb221a 163static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
45bea155 164{
86dae015 165 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
6ffb221a 166 outb(command, ec->command_addr);
45bea155
LY
167}
168
6ffb221a 169static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
45bea155 170{
86dae015 171 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
6ffb221a 172 outb(data, ec->data_addr);
703959d4 173}
45bea155 174
7c6db4e0 175static int ec_transaction_done(struct acpi_ec *ec)
6ffb221a 176{
7c6db4e0
AS
177 unsigned long flags;
178 int ret = 0;
179 spin_lock_irqsave(&ec->t_lock, flags);
180 if (!ec->t || (!ec->t->wlen && !ec->t->rlen))
181 ret = 1;
182 spin_unlock_irqrestore(&ec->t_lock, flags);
183 return ret;
45bea155 184}
451566f4 185
7c6db4e0 186static void gpe_transaction(struct acpi_ec *ec, u8 status)
7c6db5e5 187{
7c6db4e0
AS
188 unsigned long flags;
189 spin_lock_irqsave(&ec->t_lock, flags);
190 if (!ec->t)
191 goto unlock;
192 if (ec->t->wlen > 0) {
193 if ((status & ACPI_EC_FLAG_IBF) == 0) {
194 acpi_ec_write_data(ec, *(ec->t->wdata++));
195 --ec->t->wlen;
196 } else
197 /* false interrupt, state didn't change */
198 ++ec->t->irq_count;
199
200 } else if (ec->t->rlen > 0) {
201 if ((status & ACPI_EC_FLAG_OBF) == 1) {
202 *(ec->t->rdata++) = acpi_ec_read_data(ec);
203 --ec->t->rlen;
204 } else
205 /* false interrupt, state didn't change */
206 ++ec->t->irq_count;
207 }
208unlock:
209 spin_unlock_irqrestore(&ec->t_lock, flags);
845625cd 210}
03d1d99c 211
7c6db4e0 212static int acpi_ec_wait(struct acpi_ec *ec)
845625cd 213{
7c6db4e0
AS
214 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
215 msecs_to_jiffies(ACPI_EC_DELAY)))
216 return 0;
217 /* missing GPEs, switch back to poll mode */
218 if (printk_ratelimit())
219 pr_info(PREFIX "missing confirmations, "
220 "switch off interrupt mode.\n");
b77d81b2 221 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
845625cd 222 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
7c6db4e0 223 return 1;
845625cd
AS
224}
225
7c6db4e0
AS
226static void acpi_ec_gpe_query(void *ec_cxt);
227
228static int ec_check_sci(struct acpi_ec *ec, u8 state)
7c6db5e5 229{
7c6db4e0
AS
230 if (state & ACPI_EC_FLAG_SCI) {
231 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
232 return acpi_os_execute(OSL_EC_BURST_HANDLER,
233 acpi_ec_gpe_query, ec);
234 }
235 return 0;
236}
237
238static int ec_poll(struct acpi_ec *ec)
239{
240 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
241 msleep(1);
242 while (time_before(jiffies, delay)) {
243 gpe_transaction(ec, acpi_ec_read_status(ec));
244 msleep(1);
245 if (ec_transaction_done(ec))
9d699ed9 246 return 0;
af3fd140 247 }
b77d81b2 248 return -ETIME;
1da177e4
LT
249}
250
703959d4 251static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
6ccedb10 252 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
253 u8 * rdata, unsigned rdata_len,
254 int force_poll)
45bea155 255{
7c6db4e0
AS
256 unsigned long tmp;
257 struct transaction_data t = {.wdata = wdata, .rdata = rdata,
258 .wlen = wdata_len, .rlen = rdata_len,
259 .irq_count = 0};
260 int ret = 0;
3ebe08a7 261 pr_debug(PREFIX "transaction start\n");
7c6db4e0
AS
262 /* disable GPE during transaction if storm is detected */
263 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
264 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
265 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
3576cf61 266 }
7c6db4e0
AS
267 /* start transaction */
268 spin_lock_irqsave(&ec->t_lock, tmp);
269 /* following two actions should be kept atomic */
270 ec->t = &t;
271 acpi_ec_write_cmd(ec, command);
272 if (command == ACPI_EC_COMMAND_QUERY)
080e412c 273 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
7c6db4e0
AS
274 spin_unlock_irqrestore(&ec->t_lock, tmp);
275 /* if we selected poll mode or failed in GPE-mode do a poll loop */
276 if (force_poll ||
277 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
278 acpi_ec_wait(ec))
279 ret = ec_poll(ec);
3ebe08a7 280 pr_debug(PREFIX "transaction end\n");
7c6db4e0
AS
281 spin_lock_irqsave(&ec->t_lock, tmp);
282 ec->t = NULL;
283 spin_unlock_irqrestore(&ec->t_lock, tmp);
284 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
285 /* check if we received SCI during transaction */
286 ec_check_sci(ec, acpi_ec_read_status(ec));
287 /* it is safe to enable GPE outside of transaction */
288 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
289 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
290 t.irq_count > ACPI_EC_STORM_THRESHOLD) {
291 pr_debug(PREFIX "GPE storm detected\n");
292 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
293 }
294 return ret;
295}
296
297static int ec_check_ibf0(struct acpi_ec *ec)
298{
299 u8 status = acpi_ec_read_status(ec);
300 return (status & ACPI_EC_FLAG_IBF) == 0;
45bea155
LY
301}
302
3576cf61 303static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
6ccedb10 304 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
305 u8 * rdata, unsigned rdata_len,
306 int force_poll)
1da177e4 307{
d7a76e4c 308 int status;
50526df6 309 u32 glk;
d7a76e4c 310 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
d550d98d 311 return -EINVAL;
6ccedb10
AS
312 if (rdata)
313 memset(rdata, 0, rdata_len);
523953b4 314 mutex_lock(&ec->lock);
703959d4 315 if (ec->global_lock) {
1da177e4 316 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
c24e912b 317 if (ACPI_FAILURE(status)) {
7c6db4e0
AS
318 status = -ENODEV;
319 goto unlock;
c24e912b 320 }
1da177e4 321 }
7c6db4e0
AS
322 if (!wait_event_timeout(ec->wait, ec_check_ibf0(ec),
323 msecs_to_jiffies(ACPI_EC_DELAY))) {
3ebe08a7
MN
324 pr_err(PREFIX "input buffer is not empty, "
325 "aborting transaction\n");
7c6db4e0 326 status = -ETIME;
451566f4 327 goto end;
716e084e 328 }
6ccedb10
AS
329 status = acpi_ec_transaction_unlocked(ec, command,
330 wdata, wdata_len,
00eb43a1
LP
331 rdata, rdata_len,
332 force_poll);
7c6db4e0 333end:
703959d4 334 if (ec->global_lock)
1da177e4 335 acpi_release_global_lock(glk);
7c6db4e0 336unlock:
523953b4 337 mutex_unlock(&ec->lock);
d550d98d 338 return status;
1da177e4
LT
339}
340
c45aac43
AS
341/*
342 * Note: samsung nv5000 doesn't work with ec burst mode.
343 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
344 */
345int acpi_ec_burst_enable(struct acpi_ec *ec)
346{
347 u8 d;
00eb43a1 348 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
c45aac43
AS
349}
350
351int acpi_ec_burst_disable(struct acpi_ec *ec)
352{
7c6db4e0
AS
353 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
354 acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE,
355 NULL, 0, NULL, 0, 0) : 0;
c45aac43
AS
356}
357
6ccedb10 358static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
3576cf61
DS
359{
360 int result;
361 u8 d;
362
363 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
00eb43a1 364 &address, 1, &d, 1, 0);
3576cf61
DS
365 *data = d;
366 return result;
367}
6ffb221a 368
3576cf61
DS
369static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
370{
6ccedb10
AS
371 u8 wdata[2] = { address, data };
372 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
00eb43a1 373 wdata, 2, NULL, 0, 0);
3576cf61
DS
374}
375
1da177e4
LT
376/*
377 * Externally callable EC access functions. For now, assume 1 EC only
378 */
c45aac43
AS
379int ec_burst_enable(void)
380{
c45aac43
AS
381 if (!first_ec)
382 return -ENODEV;
d033879c 383 return acpi_ec_burst_enable(first_ec);
c45aac43
AS
384}
385
386EXPORT_SYMBOL(ec_burst_enable);
387
388int ec_burst_disable(void)
389{
c45aac43
AS
390 if (!first_ec)
391 return -ENODEV;
d033879c 392 return acpi_ec_burst_disable(first_ec);
c45aac43
AS
393}
394
395EXPORT_SYMBOL(ec_burst_disable);
396
6ccedb10 397int ec_read(u8 addr, u8 * val)
1da177e4 398{
1da177e4 399 int err;
6ffb221a 400 u8 temp_data;
1da177e4
LT
401
402 if (!first_ec)
403 return -ENODEV;
404
d033879c 405 err = acpi_ec_read(first_ec, addr, &temp_data);
1da177e4
LT
406
407 if (!err) {
408 *val = temp_data;
409 return 0;
50526df6 410 } else
1da177e4
LT
411 return err;
412}
50526df6 413
1da177e4
LT
414EXPORT_SYMBOL(ec_read);
415
50526df6 416int ec_write(u8 addr, u8 val)
1da177e4 417{
1da177e4
LT
418 int err;
419
420 if (!first_ec)
421 return -ENODEV;
422
d033879c 423 err = acpi_ec_write(first_ec, addr, val);
1da177e4
LT
424
425 return err;
426}
50526df6 427
1da177e4
LT
428EXPORT_SYMBOL(ec_write);
429
616362de 430int ec_transaction(u8 command,
9e197219 431 const u8 * wdata, unsigned wdata_len,
00eb43a1
LP
432 u8 * rdata, unsigned rdata_len,
433 int force_poll)
45bea155 434{
d7a76e4c
LP
435 if (!first_ec)
436 return -ENODEV;
45bea155 437
d033879c 438 return acpi_ec_transaction(first_ec, command, wdata,
00eb43a1
LP
439 wdata_len, rdata, rdata_len,
440 force_poll);
45bea155 441}
1da177e4 442
ab9e43c6
LP
443EXPORT_SYMBOL(ec_transaction);
444
6ccedb10 445static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
3576cf61
DS
446{
447 int result;
6ccedb10 448 u8 d;
1da177e4 449
6ccedb10
AS
450 if (!ec || !data)
451 return -EINVAL;
1da177e4 452
6ccedb10
AS
453 /*
454 * Query the EC to find out which _Qxx method we need to evaluate.
455 * Note that successful completion of the query causes the ACPI_EC_SCI
456 * bit to be cleared (and thus clearing the interrupt source).
457 */
716e084e 458
00eb43a1 459 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
6ccedb10
AS
460 if (result)
461 return result;
1da177e4 462
6ccedb10
AS
463 if (!d)
464 return -ENODATA;
1da177e4 465
6ccedb10
AS
466 *data = d;
467 return 0;
1da177e4
LT
468}
469
1da177e4
LT
470/* --------------------------------------------------------------------------
471 Event Management
472 -------------------------------------------------------------------------- */
837012ed
AS
473int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
474 acpi_handle handle, acpi_ec_query_func func,
475 void *data)
476{
477 struct acpi_ec_query_handler *handler =
478 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
479 if (!handler)
480 return -ENOMEM;
481
482 handler->query_bit = query_bit;
483 handler->handle = handle;
484 handler->func = func;
485 handler->data = data;
486 mutex_lock(&ec->lock);
30c08574 487 list_add(&handler->node, &ec->list);
837012ed
AS
488 mutex_unlock(&ec->lock);
489 return 0;
490}
491
492EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
493
494void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
495{
1544fdbc 496 struct acpi_ec_query_handler *handler, *tmp;
837012ed 497 mutex_lock(&ec->lock);
1544fdbc 498 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed
AS
499 if (query_bit == handler->query_bit) {
500 list_del(&handler->node);
501 kfree(handler);
837012ed
AS
502 }
503 }
504 mutex_unlock(&ec->lock);
505}
506
507EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
1da177e4 508
50526df6 509static void acpi_ec_gpe_query(void *ec_cxt)
45bea155 510{
3d02b90b 511 struct acpi_ec *ec = ec_cxt;
6ffb221a 512 u8 value = 0;
837012ed 513 struct acpi_ec_query_handler *handler, copy;
45bea155 514
5d0c288b 515 if (!ec || acpi_ec_query(ec, &value))
e41334c0 516 return;
837012ed
AS
517 mutex_lock(&ec->lock);
518 list_for_each_entry(handler, &ec->list, node) {
519 if (value == handler->query_bit) {
520 /* have custom handler for this bit */
521 memcpy(&copy, handler, sizeof(copy));
522 mutex_unlock(&ec->lock);
523 if (copy.func) {
524 copy.func(copy.data);
525 } else if (copy.handle) {
526 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
527 }
528 return;
529 }
530 }
531 mutex_unlock(&ec->lock);
45bea155 532}
1da177e4 533
50526df6 534static u32 acpi_ec_gpe_handler(void *data)
1da177e4 535{
3d02b90b 536 struct acpi_ec *ec = data;
7c6db4e0 537 u8 status;
00eb43a1 538
3ebe08a7 539 pr_debug(PREFIX "~~~> interrupt\n");
7c6db4e0
AS
540 status = acpi_ec_read_status(ec);
541
542 gpe_transaction(ec, status);
543 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
af3fd140 544 wake_up(&ec->wait);
451566f4 545
7c6db4e0
AS
546 ec_check_sci(ec, status);
547 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
548 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
95b937e3 549 /* this is non-query, must be confirmation */
3ebe08a7
MN
550 if (printk_ratelimit())
551 pr_info(PREFIX "non-query interrupt received,"
552 " switching to interrupt mode\n");
7843932a 553 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
95b937e3 554 }
7c6db4e0 555 return ACPI_INTERRUPT_HANDLED;
845625cd
AS
556}
557
1da177e4
LT
558/* --------------------------------------------------------------------------
559 Address Space Management
560 -------------------------------------------------------------------------- */
561
1da177e4 562static acpi_status
5b7734b4
AS
563acpi_ec_space_handler(u32 function, acpi_physical_address address,
564 u32 bits, acpi_integer *value,
50526df6 565 void *handler_context, void *region_context)
1da177e4 566{
3d02b90b 567 struct acpi_ec *ec = handler_context;
3e71a87d 568 int result = 0, i;
5b7734b4 569 u8 temp = 0;
1da177e4 570
1da177e4 571 if ((address > 0xFF) || !value || !handler_context)
d550d98d 572 return AE_BAD_PARAMETER;
1da177e4 573
5b7734b4 574 if (function != ACPI_READ && function != ACPI_WRITE)
d550d98d 575 return AE_BAD_PARAMETER;
1da177e4 576
5b7734b4
AS
577 if (bits != 8 && acpi_strict)
578 return AE_BAD_PARAMETER;
1da177e4 579
b3b233c7
AS
580 acpi_ec_burst_enable(ec);
581
3e71a87d
AS
582 if (function == ACPI_READ) {
583 result = acpi_ec_read(ec, address, &temp);
584 *value = temp;
585 } else {
586 temp = 0xff & (*value);
587 result = acpi_ec_write(ec, address, temp);
588 }
589
590 for (i = 8; unlikely(bits - i > 0); i += 8) {
591 ++address;
5b7734b4
AS
592 if (function == ACPI_READ) {
593 result = acpi_ec_read(ec, address, &temp);
594 (*value) |= ((acpi_integer)temp) << i;
595 } else {
596 temp = 0xff & ((*value) >> i);
597 result = acpi_ec_write(ec, address, temp);
598 }
1da177e4
LT
599 }
600
b3b233c7
AS
601 acpi_ec_burst_disable(ec);
602
1da177e4
LT
603 switch (result) {
604 case -EINVAL:
d550d98d 605 return AE_BAD_PARAMETER;
1da177e4
LT
606 break;
607 case -ENODEV:
d550d98d 608 return AE_NOT_FOUND;
1da177e4
LT
609 break;
610 case -ETIME:
d550d98d 611 return AE_TIME;
1da177e4
LT
612 break;
613 default:
d550d98d 614 return AE_OK;
1da177e4 615 }
1da177e4
LT
616}
617
1da177e4
LT
618/* --------------------------------------------------------------------------
619 FS Interface (/proc)
620 -------------------------------------------------------------------------- */
621
50526df6 622static struct proc_dir_entry *acpi_ec_dir;
1da177e4 623
50526df6 624static int acpi_ec_read_info(struct seq_file *seq, void *offset)
1da177e4 625{
3d02b90b 626 struct acpi_ec *ec = seq->private;
1da177e4 627
1da177e4
LT
628 if (!ec)
629 goto end;
630
01f22462
AS
631 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
632 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
633 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
634 seq_printf(seq, "use global lock:\t%s\n",
703959d4 635 ec->global_lock ? "yes" : "no");
50526df6 636 end:
d550d98d 637 return 0;
1da177e4
LT
638}
639
640static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
641{
642 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
643}
644
703959d4 645static struct file_operations acpi_ec_info_ops = {
50526df6
LB
646 .open = acpi_ec_info_open_fs,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = single_release,
1da177e4
LT
650 .owner = THIS_MODULE,
651};
652
50526df6 653static int acpi_ec_add_fs(struct acpi_device *device)
1da177e4 654{
50526df6 655 struct proc_dir_entry *entry = NULL;
1da177e4 656
1da177e4
LT
657 if (!acpi_device_dir(device)) {
658 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
50526df6 659 acpi_ec_dir);
1da177e4 660 if (!acpi_device_dir(device))
d550d98d 661 return -ENODEV;
1da177e4
LT
662 }
663
cf7acfab
DL
664 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
665 acpi_device_dir(device),
666 &acpi_ec_info_ops, acpi_driver_data(device));
1da177e4 667 if (!entry)
d550d98d 668 return -ENODEV;
d550d98d 669 return 0;
1da177e4
LT
670}
671
50526df6 672static int acpi_ec_remove_fs(struct acpi_device *device)
1da177e4 673{
1da177e4
LT
674
675 if (acpi_device_dir(device)) {
676 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
677 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
678 acpi_device_dir(device) = NULL;
679 }
680
d550d98d 681 return 0;
1da177e4
LT
682}
683
1da177e4
LT
684/* --------------------------------------------------------------------------
685 Driver Interface
686 -------------------------------------------------------------------------- */
c0900c35
AS
687static acpi_status
688ec_parse_io_ports(struct acpi_resource *resource, void *context);
689
c0900c35
AS
690static struct acpi_ec *make_acpi_ec(void)
691{
692 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
693 if (!ec)
694 return NULL;
080e412c 695 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
c0900c35
AS
696 mutex_init(&ec->lock);
697 init_waitqueue_head(&ec->wait);
837012ed 698 INIT_LIST_HEAD(&ec->list);
7c6db4e0 699 spin_lock_init(&ec->t_lock);
c0900c35
AS
700 return ec;
701}
837012ed 702
c019b193
AS
703static acpi_status
704acpi_ec_register_query_methods(acpi_handle handle, u32 level,
705 void *context, void **return_value)
706{
707 struct acpi_namespace_node *node = handle;
708 struct acpi_ec *ec = context;
709 int value = 0;
710 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
711 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
712 }
713 return AE_OK;
714}
715
cd8c93a4
AS
716static acpi_status
717ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
837012ed 718{
cd8c93a4
AS
719 acpi_status status;
720
721 struct acpi_ec *ec = context;
722 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
723 ec_parse_io_ports, ec);
724 if (ACPI_FAILURE(status))
725 return status;
837012ed
AS
726
727 /* Get GPE bit assignment (EC events). */
728 /* TODO: Add support for _GPE returning a package */
cd8c93a4
AS
729 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
730 if (ACPI_FAILURE(status))
731 return status;
837012ed
AS
732 /* Use the global lock for all EC transactions? */
733 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
837012ed 734 ec->handle = handle;
cd8c93a4 735 return AE_CTRL_TERMINATE;
837012ed
AS
736}
737
4c611060
AS
738static void ec_remove_handlers(struct acpi_ec *ec)
739{
740 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
741 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
3ebe08a7 742 pr_err(PREFIX "failed to remove space handler\n");
4c611060
AS
743 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
744 &acpi_ec_gpe_handler)))
3ebe08a7 745 pr_err(PREFIX "failed to remove gpe handler\n");
7c6db4e0 746 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
4c611060
AS
747}
748
703959d4 749static int acpi_ec_add(struct acpi_device *device)
1da177e4 750{
703959d4 751 struct acpi_ec *ec = NULL;
45bea155 752
45bea155 753 if (!device)
d550d98d 754 return -EINVAL;
c0900c35
AS
755 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
756 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
757
4c611060 758 /* Check for boot EC */
ce52ddf5
AS
759 if (boot_ec &&
760 (boot_ec->handle == device->handle ||
761 boot_ec->handle == ACPI_ROOT_OBJECT)) {
762 ec = boot_ec;
763 boot_ec = NULL;
764 } else {
765 ec = make_acpi_ec();
766 if (!ec)
767 return -ENOMEM;
768 if (ec_parse_device(device->handle, 0, ec, NULL) !=
769 AE_CTRL_TERMINATE) {
770 kfree(ec);
771 return -EINVAL;
4c611060
AS
772 }
773 }
774
c0900c35 775 ec->handle = device->handle;
ce52ddf5
AS
776
777 /* Find and register all query methods */
778 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
779 acpi_ec_register_query_methods, ec, NULL);
780
4c611060
AS
781 if (!first_ec)
782 first_ec = ec;
c0900c35 783 acpi_driver_data(device) = ec;
c0900c35 784 acpi_ec_add_fs(device);
3ebe08a7 785 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
4c611060 786 ec->gpe, ec->command_addr, ec->data_addr);
3ebe08a7 787 pr_info(PREFIX "driver started in %s mode\n",
95b937e3 788 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
c0900c35 789 return 0;
1da177e4
LT
790}
791
50526df6 792static int acpi_ec_remove(struct acpi_device *device, int type)
1da177e4 793{
01f22462 794 struct acpi_ec *ec;
07ddf768 795 struct acpi_ec_query_handler *handler, *tmp;
1da177e4 796
1da177e4 797 if (!device)
d550d98d 798 return -EINVAL;
1da177e4
LT
799
800 ec = acpi_driver_data(device);
837012ed 801 mutex_lock(&ec->lock);
07ddf768 802 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed
AS
803 list_del(&handler->node);
804 kfree(handler);
805 }
806 mutex_unlock(&ec->lock);
1da177e4 807 acpi_ec_remove_fs(device);
c0900c35 808 acpi_driver_data(device) = NULL;
d033879c 809 if (ec == first_ec)
c0900c35 810 first_ec = NULL;
4c611060 811 kfree(ec);
d550d98d 812 return 0;
1da177e4
LT
813}
814
1da177e4 815static acpi_status
c0900c35 816ec_parse_io_ports(struct acpi_resource *resource, void *context)
1da177e4 817{
3d02b90b 818 struct acpi_ec *ec = context;
1da177e4 819
01f22462 820 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1da177e4 821 return AE_OK;
1da177e4
LT
822
823 /*
824 * The first address region returned is the data port, and
825 * the second address region returned is the status/command
826 * port.
827 */
01f22462 828 if (ec->data_addr == 0)
6ffb221a 829 ec->data_addr = resource->data.io.minimum;
01f22462 830 else if (ec->command_addr == 0)
6ffb221a 831 ec->command_addr = resource->data.io.minimum;
01f22462 832 else
1da177e4 833 return AE_CTRL_TERMINATE;
1da177e4 834
1da177e4
LT
835 return AE_OK;
836}
837
e8284321
AS
838static int ec_install_handlers(struct acpi_ec *ec)
839{
c0900c35 840 acpi_status status;
7c6db4e0 841 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
4c611060 842 return 0;
c0900c35 843 status = acpi_install_gpe_handler(NULL, ec->gpe,
7c6db4e0
AS
844 ACPI_GPE_EDGE_TRIGGERED,
845 &acpi_ec_gpe_handler, ec);
e8284321
AS
846 if (ACPI_FAILURE(status))
847 return -ENODEV;
848 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
849 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
e8284321
AS
850 status = acpi_install_address_space_handler(ec->handle,
851 ACPI_ADR_SPACE_EC,
852 &acpi_ec_space_handler,
6d9e1120 853 NULL, ec);
e8284321
AS
854 if (ACPI_FAILURE(status)) {
855 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
856 return -ENODEV;
857 }
858
7c6db4e0 859 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
e8284321
AS
860 return 0;
861}
862
50526df6 863static int acpi_ec_start(struct acpi_device *device)
1da177e4 864{
c0900c35 865 struct acpi_ec *ec;
837012ed 866 int ret = 0;
1da177e4 867
1da177e4 868 if (!device)
d550d98d 869 return -EINVAL;
1da177e4
LT
870
871 ec = acpi_driver_data(device);
872
873 if (!ec)
d550d98d 874 return -EINVAL;
1da177e4 875
4c611060 876 ret = ec_install_handlers(ec);
837012ed
AS
877
878 /* EC is fully operational, allow queries */
080e412c 879 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
837012ed 880 return ret;
1da177e4
LT
881}
882
50526df6 883static int acpi_ec_stop(struct acpi_device *device, int type)
1da177e4 884{
c0900c35 885 struct acpi_ec *ec;
1da177e4 886 if (!device)
d550d98d 887 return -EINVAL;
1da177e4 888 ec = acpi_driver_data(device);
c0900c35
AS
889 if (!ec)
890 return -EINVAL;
4c611060 891 ec_remove_handlers(ec);
f9319f90 892
d550d98d 893 return 0;
1da177e4
LT
894}
895
c04209a7
AS
896int __init acpi_boot_ec_enable(void)
897{
7c6db4e0 898 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
c04209a7
AS
899 return 0;
900 if (!ec_install_handlers(boot_ec)) {
901 first_ec = boot_ec;
902 return 0;
903 }
904 return -EFAULT;
905}
906
223883b7
AS
907static const struct acpi_device_id ec_device_ids[] = {
908 {"PNP0C09", 0},
909 {"", 0},
910};
911
c0900c35
AS
912int __init acpi_ec_ecdt_probe(void)
913{
914 int ret;
915 acpi_status status;
50526df6 916 struct acpi_table_ecdt *ecdt_ptr;
45bea155 917
d66d969d
AS
918 boot_ec = make_acpi_ec();
919 if (!boot_ec)
c0900c35
AS
920 return -ENOMEM;
921 /*
922 * Generate a boot ec context
923 */
15a58ed1
AS
924 status = acpi_get_table(ACPI_SIG_ECDT, 1,
925 (struct acpi_table_header **)&ecdt_ptr);
cd8c93a4 926 if (ACPI_SUCCESS(status)) {
3ebe08a7 927 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
cd8c93a4
AS
928 boot_ec->command_addr = ecdt_ptr->control.address;
929 boot_ec->data_addr = ecdt_ptr->data.address;
2500822b
ZY
930 if (dmi_check_system(ec_dmi_table)) {
931 /*
932 * If the board falls into ec_dmi_table, it means
933 * that ECDT table gives the incorrect command/status
934 * & data I/O address. Just fix it.
935 */
936 boot_ec->data_addr = ecdt_ptr->control.address;
937 boot_ec->command_addr = ecdt_ptr->data.address;
938 }
cd8c93a4 939 boot_ec->gpe = ecdt_ptr->gpe;
4af8e10a 940 boot_ec->handle = ACPI_ROOT_OBJECT;
ce52ddf5 941 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
cd8c93a4 942 } else {
5870a8cd
AS
943 /* This workaround is needed only on some broken machines,
944 * which require early EC, but fail to provide ECDT */
945 acpi_handle x;
cd8c93a4
AS
946 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
947 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
948 boot_ec, NULL);
2d8348b4
AS
949 /* Check that acpi_get_devices actually find something */
950 if (ACPI_FAILURE(status) || !boot_ec->handle)
cd8c93a4 951 goto error;
5870a8cd
AS
952 /* We really need to limit this workaround, the only ASUS,
953 * which needs it, has fake EC._INI method, so use it as flag.
c04209a7 954 * Keep boot_ec struct as it will be needed soon.
5870a8cd
AS
955 */
956 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
c04209a7 957 return -ENODEV;
cd8c93a4 958 }
1da177e4 959
d66d969d 960 ret = ec_install_handlers(boot_ec);
d033879c
AS
961 if (!ret) {
962 first_ec = boot_ec;
e8284321 963 return 0;
d033879c 964 }
c0900c35 965 error:
d66d969d
AS
966 kfree(boot_ec);
967 boot_ec = NULL;
1da177e4
LT
968 return -ENODEV;
969}
970
223883b7
AS
971static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
972{
973 struct acpi_ec *ec = acpi_driver_data(device);
974 /* Stop using GPE */
975 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
976 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
977 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
978 return 0;
979}
980
981static int acpi_ec_resume(struct acpi_device *device)
982{
983 struct acpi_ec *ec = acpi_driver_data(device);
984 /* Enable use of GPE back */
985 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
986 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
987 return 0;
988}
989
990static struct acpi_driver acpi_ec_driver = {
991 .name = "ec",
992 .class = ACPI_EC_CLASS,
993 .ids = ec_device_ids,
994 .ops = {
995 .add = acpi_ec_add,
996 .remove = acpi_ec_remove,
997 .start = acpi_ec_start,
998 .stop = acpi_ec_stop,
999 .suspend = acpi_ec_suspend,
1000 .resume = acpi_ec_resume,
1001 },
1002};
1003
50526df6 1004static int __init acpi_ec_init(void)
1da177e4 1005{
50526df6 1006 int result = 0;
1da177e4 1007
1da177e4 1008 if (acpi_disabled)
d550d98d 1009 return 0;
1da177e4
LT
1010
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1012 if (!acpi_ec_dir)
d550d98d 1013 return -ENODEV;
1da177e4
LT
1014
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1017 if (result < 0) {
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
d550d98d 1019 return -ENODEV;
1da177e4
LT
1020 }
1021
d550d98d 1022 return result;
1da177e4
LT
1023}
1024
1025subsys_initcall(acpi_ec_init);
1026
1027/* EC driver currently not unloadable */
1028#if 0
50526df6 1029static void __exit acpi_ec_exit(void)
1da177e4 1030{
1da177e4
LT
1031
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1033
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1035
d550d98d 1036 return;
1da177e4 1037}
7843932a 1038#endif /* 0 */
This page took 0.907282 seconds and 5 git commands to generate.