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