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