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