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