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