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