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