Linux 2.6.21-rc3
[linux-2.6-block.git] / drivers / acpi / ec.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
451566f4 34#include <linux/interrupt.h>
1da177e4
LT
35#include <asm/io.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38#include <acpi/actypes.h>
39
40#define _COMPONENT ACPI_EC_COMPONENT
f52fd66d 41ACPI_MODULE_NAME("ec");
1da177e4
LT
42#define ACPI_EC_COMPONENT 0x00100000
43#define ACPI_EC_CLASS "embedded_controller"
44#define ACPI_EC_HID "PNP0C09"
1da177e4
LT
45#define ACPI_EC_DEVICE_NAME "Embedded Controller"
46#define ACPI_EC_FILE_INFO "info"
af3fd140
AS
47#undef PREFIX
48#define PREFIX "ACPI: EC: "
703959d4 49/* EC status register */
1da177e4
LT
50#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
451566f4 52#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
1da177e4 53#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
703959d4 54/* EC commands */
3261ff4d 55enum ec_command {
6ccedb10
AS
56 ACPI_EC_COMMAND_READ = 0x80,
57 ACPI_EC_COMMAND_WRITE = 0x81,
58 ACPI_EC_BURST_ENABLE = 0x82,
59 ACPI_EC_BURST_DISABLE = 0x83,
60 ACPI_EC_COMMAND_QUERY = 0x84,
3261ff4d 61};
703959d4 62/* EC events */
3261ff4d 63enum ec_event {
703959d4 64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
6ccedb10 65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
703959d4
DS
66};
67
5c406412 68#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
703959d4 69#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
703959d4 70
3261ff4d 71static enum ec_mode {
6ccedb10
AS
72 EC_INTR = 1, /* Output buffer full */
73 EC_POLL, /* Input buffer empty */
3261ff4d 74} acpi_ec_mode = EC_INTR;
703959d4 75
50526df6
LB
76static int acpi_ec_remove(struct acpi_device *device, int type);
77static int acpi_ec_start(struct acpi_device *device);
78static int acpi_ec_stop(struct acpi_device *device, int type);
703959d4 79static int acpi_ec_add(struct acpi_device *device);
1da177e4
LT
80
81static struct acpi_driver acpi_ec_driver = {
c2b6705b 82 .name = "ec",
50526df6
LB
83 .class = ACPI_EC_CLASS,
84 .ids = ACPI_EC_HID,
85 .ops = {
703959d4 86 .add = acpi_ec_add,
50526df6
LB
87 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
89 .stop = acpi_ec_stop,
90 },
1da177e4 91};
6ffb221a
DS
92
93/* If we find an EC via the ECDT, we need to keep a ptr to its context */
a854e08a 94static struct acpi_ec {
703959d4
DS
95 acpi_handle handle;
96 unsigned long uid;
a86e2772 97 unsigned long gpe;
6ffb221a
DS
98 unsigned long command_addr;
99 unsigned long data_addr;
703959d4 100 unsigned long global_lock;
c787a855 101 struct mutex lock;
5d0c288b 102 atomic_t query_pending;
703959d4
DS
103 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
104 wait_queue_head_t wait;
6ffb221a 105} *ec_ecdt;
703959d4
DS
106
107/* External interfaces use first EC only, so remember */
108static struct acpi_device *first_ec;
703959d4 109
1da177e4
LT
110/* --------------------------------------------------------------------------
111 Transaction Management
112 -------------------------------------------------------------------------- */
113
6ffb221a 114static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
1da177e4 115{
6ffb221a 116 return inb(ec->command_addr);
451566f4
DT
117}
118
6ffb221a 119static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
703959d4 120{
6ffb221a 121 return inb(ec->data_addr);
7c6db5e5
DS
122}
123
6ffb221a 124static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
45bea155 125{
6ffb221a 126 outb(command, ec->command_addr);
45bea155
LY
127}
128
6ffb221a 129static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
45bea155 130{
6ffb221a 131 outb(data, ec->data_addr);
703959d4 132}
45bea155 133
3261ff4d 134static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
6ffb221a 135{
bec5a1e0 136 u8 status = acpi_ec_read_status(ec);
78d0af33
AS
137
138 if (event == ACPI_EC_EVENT_OBF_1) {
703959d4
DS
139 if (status & ACPI_EC_FLAG_OBF)
140 return 1;
78d0af33 141 } else if (event == ACPI_EC_EVENT_IBF_0) {
703959d4
DS
142 if (!(status & ACPI_EC_FLAG_IBF))
143 return 1;
45bea155
LY
144 }
145
703959d4 146 return 0;
45bea155 147}
451566f4 148
3261ff4d 149static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event)
7c6db5e5 150{
af3fd140 151 if (acpi_ec_mode == EC_POLL) {
50c1e113
AS
152 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
153 while (time_before(jiffies, delay)) {
af3fd140 154 if (acpi_ec_check_status(ec, event))
7c6db5e5
DS
155 return 0;
156 }
af3fd140
AS
157 } else {
158 if (wait_event_timeout(ec->wait,
159 acpi_ec_check_status(ec, event),
160 msecs_to_jiffies(ACPI_EC_DELAY)) ||
161 acpi_ec_check_status(ec, event)) {
703959d4 162 return 0;
af3fd140
AS
163 } else {
164 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
165 " status = %d, expect_event = %d\n",
6ccedb10 166 acpi_ec_read_status(ec), event);
703959d4 167 }
af3fd140 168 }
1da177e4 169
d550d98d 170 return -ETIME;
1da177e4
LT
171}
172
02b28a33 173#ifdef ACPI_FUTURE_USAGE
06a2a385
LY
174/*
175 * Note: samsung nv5000 doesn't work with ec burst mode.
176 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
177 */
703959d4 178int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
451566f4 179{
6ffb221a
DS
180 u8 tmp = 0;
181 u8 status = 0;
451566f4 182
451566f4 183 status = acpi_ec_read_status(ec);
50526df6 184 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
703959d4 185 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
50526df6 186 if (status)
716e084e 187 goto end;
703959d4
DS
188 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
189 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
190 tmp = acpi_ec_read_data(ec);
50526df6 191 if (tmp != 0x90) { /* Burst ACK byte */
d550d98d 192 return -EINVAL;
451566f4 193 }
668d74c0
LY
194 }
195
703959d4 196 atomic_set(&ec->leaving_burst, 0);
d550d98d 197 return 0;
6ccedb10 198 end:
703959d4 199 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
d550d98d 200 return -1;
451566f4
DT
201}
202
703959d4 203int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
451566f4 204{
6ffb221a 205 u8 status = 0;
451566f4 206
06a2a385 207 status = acpi_ec_read_status(ec);
6ccedb10 208 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)) {
703959d4 209 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
6ccedb10 210 if (status)
06a2a385 211 goto end;
703959d4
DS
212 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
213 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
7c6db5e5 214 }
703959d4 215 atomic_set(&ec->leaving_burst, 1);
d550d98d 216 return 0;
6ccedb10 217 end:
703959d4 218 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
d550d98d 219 return -1;
451566f4 220}
6ccedb10 221#endif /* ACPI_FUTURE_USAGE */
451566f4 222
703959d4 223static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
6ccedb10
AS
224 const u8 * wdata, unsigned wdata_len,
225 u8 * rdata, unsigned rdata_len)
45bea155 226{
af3fd140 227 int result = 0;
45bea155 228
703959d4 229 acpi_ec_write_cmd(ec, command);
45bea155 230
78d0af33 231 for (; wdata_len > 0; --wdata_len) {
703959d4 232 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
af3fd140 233 if (result) {
6ccedb10
AS
234 printk(KERN_ERR PREFIX
235 "write_cmd timeout, command = %d\n", command);
af3fd140
AS
236 goto end;
237 }
703959d4 238 acpi_ec_write_data(ec, *(wdata++));
3576cf61 239 }
45bea155 240
d91df1aa 241 if (!rdata_len) {
703959d4 242 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
af3fd140 243 if (result) {
6ccedb10
AS
244 printk(KERN_ERR PREFIX
245 "finish-write timeout, command = %d\n", command);
af3fd140
AS
246 goto end;
247 }
5d0c288b
AS
248 } else if (command == ACPI_EC_COMMAND_QUERY) {
249 atomic_set(&ec->query_pending, 0);
7c6db5e5 250 }
45bea155 251
78d0af33 252 for (; rdata_len > 0; --rdata_len) {
703959d4 253 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
af3fd140
AS
254 if (result) {
255 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
6ccedb10 256 command);
af3fd140
AS
257 goto end;
258 }
50526df6 259
6ffb221a 260 *(rdata++) = acpi_ec_read_data(ec);
7c6db5e5 261 }
af3fd140
AS
262 end:
263 return result;
45bea155
LY
264}
265
3576cf61 266static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
6ccedb10
AS
267 const u8 * wdata, unsigned wdata_len,
268 u8 * rdata, unsigned rdata_len)
1da177e4 269{
d7a76e4c 270 int status;
50526df6 271 u32 glk;
1da177e4 272
d7a76e4c 273 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
d550d98d 274 return -EINVAL;
1da177e4 275
6ccedb10
AS
276 if (rdata)
277 memset(rdata, 0, rdata_len);
1da177e4 278
523953b4 279 mutex_lock(&ec->lock);
703959d4 280 if (ec->global_lock) {
1da177e4 281 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
c24e912b
AS
282 if (ACPI_FAILURE(status)) {
283 mutex_unlock(&ec->lock);
d550d98d 284 return -ENODEV;
c24e912b 285 }
1da177e4 286 }
451566f4 287
5d57a6a5 288 /* Make sure GPE is enabled before doing transaction */
a86e2772 289 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
5d57a6a5 290
703959d4 291 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
716e084e 292 if (status) {
6ccedb10
AS
293 printk(KERN_DEBUG PREFIX
294 "input buffer is not empty, aborting transaction\n");
451566f4 295 goto end;
716e084e 296 }
1da177e4 297
6ccedb10
AS
298 status = acpi_ec_transaction_unlocked(ec, command,
299 wdata, wdata_len,
300 rdata, rdata_len);
1da177e4 301
6ccedb10 302 end:
1da177e4 303
703959d4 304 if (ec->global_lock)
1da177e4 305 acpi_release_global_lock(glk);
523953b4 306 mutex_unlock(&ec->lock);
1da177e4 307
d550d98d 308 return status;
1da177e4
LT
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,
317 &address, 1, &d, 1);
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,
3576cf61
DS
326 wdata, 2, NULL, 0);
327}
328
1da177e4
LT
329/*
330 * Externally callable EC access functions. For now, assume 1 EC only
331 */
6ccedb10 332int ec_read(u8 addr, u8 * val)
1da177e4 333{
703959d4 334 struct acpi_ec *ec;
1da177e4 335 int err;
6ffb221a 336 u8 temp_data;
1da177e4
LT
337
338 if (!first_ec)
339 return -ENODEV;
340
341 ec = acpi_driver_data(first_ec);
342
343 err = acpi_ec_read(ec, addr, &temp_data);
344
345 if (!err) {
346 *val = temp_data;
347 return 0;
50526df6 348 } else
1da177e4
LT
349 return err;
350}
50526df6 351
1da177e4
LT
352EXPORT_SYMBOL(ec_read);
353
50526df6 354int ec_write(u8 addr, u8 val)
1da177e4 355{
703959d4 356 struct acpi_ec *ec;
1da177e4
LT
357 int err;
358
359 if (!first_ec)
360 return -ENODEV;
361
362 ec = acpi_driver_data(first_ec);
363
364 err = acpi_ec_write(ec, addr, val);
365
366 return err;
367}
50526df6 368
1da177e4
LT
369EXPORT_SYMBOL(ec_write);
370
616362de 371int ec_transaction(u8 command,
6ccedb10
AS
372 const u8 * wdata, unsigned wdata_len,
373 u8 * rdata, unsigned rdata_len)
45bea155 374{
703959d4 375 struct acpi_ec *ec;
45bea155 376
d7a76e4c
LP
377 if (!first_ec)
378 return -ENODEV;
45bea155 379
d7a76e4c 380 ec = acpi_driver_data(first_ec);
45bea155 381
3576cf61
DS
382 return acpi_ec_transaction(ec, command, wdata,
383 wdata_len, rdata, rdata_len);
45bea155 384}
1da177e4 385
ab9e43c6
LP
386EXPORT_SYMBOL(ec_transaction);
387
6ccedb10 388static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
3576cf61
DS
389{
390 int result;
6ccedb10 391 u8 d;
1da177e4 392
6ccedb10
AS
393 if (!ec || !data)
394 return -EINVAL;
1da177e4 395
6ccedb10
AS
396 /*
397 * Query the EC to find out which _Qxx method we need to evaluate.
398 * Note that successful completion of the query causes the ACPI_EC_SCI
399 * bit to be cleared (and thus clearing the interrupt source).
400 */
716e084e 401
6ccedb10
AS
402 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
403 if (result)
404 return result;
1da177e4 405
6ccedb10
AS
406 if (!d)
407 return -ENODATA;
1da177e4 408
6ccedb10
AS
409 *data = d;
410 return 0;
1da177e4
LT
411}
412
1da177e4
LT
413/* --------------------------------------------------------------------------
414 Event Management
415 -------------------------------------------------------------------------- */
416
50526df6 417static void acpi_ec_gpe_query(void *ec_cxt)
45bea155 418{
703959d4 419 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
6ffb221a 420 u8 value = 0;
5d0c288b 421 char object_name[8];
45bea155 422
5d0c288b 423 if (!ec || acpi_ec_query(ec, &value))
e41334c0 424 return;
45bea155 425
6ffb221a 426 snprintf(object_name, 8, "_Q%2.2X", value);
45bea155 427
c6e19194 428 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
45bea155 429
703959d4 430 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
45bea155 431}
1da177e4 432
50526df6 433static u32 acpi_ec_gpe_handler(void *data)
1da177e4 434{
50526df6 435 acpi_status status = AE_OK;
6ffb221a 436 u8 value;
703959d4 437 struct acpi_ec *ec = (struct acpi_ec *)data;
1da177e4 438
8e0341ba 439 if (acpi_ec_mode == EC_INTR) {
af3fd140 440 wake_up(&ec->wait);
451566f4
DT
441 }
442
bec5a1e0 443 value = acpi_ec_read_status(ec);
5d0c288b
AS
444 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
445 atomic_set(&ec->query_pending, 1);
6ccedb10
AS
446 status =
447 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
448 ec);
50526df6 449 }
e41334c0 450
451566f4 451 return status == AE_OK ?
50526df6 452 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
1da177e4
LT
453}
454
455/* --------------------------------------------------------------------------
456 Address Space Management
457 -------------------------------------------------------------------------- */
458
459static acpi_status
50526df6
LB
460acpi_ec_space_setup(acpi_handle region_handle,
461 u32 function, void *handler_context, void **return_context)
1da177e4
LT
462{
463 /*
464 * The EC object is in the handler context and is needed
465 * when calling the acpi_ec_space_handler.
466 */
50526df6
LB
467 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
468 handler_context : NULL;
1da177e4
LT
469
470 return AE_OK;
471}
472
1da177e4 473static acpi_status
50526df6
LB
474acpi_ec_space_handler(u32 function,
475 acpi_physical_address address,
476 u32 bit_width,
477 acpi_integer * value,
478 void *handler_context, void *region_context)
1da177e4 479{
50526df6 480 int result = 0;
703959d4 481 struct acpi_ec *ec = NULL;
50526df6
LB
482 u64 temp = *value;
483 acpi_integer f_v = 0;
484 int i = 0;
1da177e4 485
1da177e4 486 if ((address > 0xFF) || !value || !handler_context)
d550d98d 487 return AE_BAD_PARAMETER;
1da177e4 488
fa9cd547 489 if (bit_width != 8 && acpi_strict) {
d550d98d 490 return AE_BAD_PARAMETER;
1da177e4
LT
491 }
492
703959d4 493 ec = (struct acpi_ec *)handler_context;
1da177e4 494
50526df6 495 next_byte:
1da177e4
LT
496 switch (function) {
497 case ACPI_READ:
fa9cd547 498 temp = 0;
6ccedb10 499 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
1da177e4
LT
500 break;
501 case ACPI_WRITE:
fa9cd547 502 result = acpi_ec_write(ec, (u8) address, (u8) temp);
1da177e4
LT
503 break;
504 default:
505 result = -EINVAL;
506 goto out;
507 break;
508 }
509
510 bit_width -= 8;
fa9cd547
LY
511 if (bit_width) {
512 if (function == ACPI_READ)
513 f_v |= temp << 8 * i;
514 if (function == ACPI_WRITE)
515 temp >>= 8;
1da177e4 516 i++;
83ea7445 517 address++;
1da177e4
LT
518 goto next_byte;
519 }
520
fa9cd547
LY
521 if (function == ACPI_READ) {
522 f_v |= temp << 8 * i;
1da177e4
LT
523 *value = f_v;
524 }
525
50526df6 526 out:
1da177e4
LT
527 switch (result) {
528 case -EINVAL:
d550d98d 529 return AE_BAD_PARAMETER;
1da177e4
LT
530 break;
531 case -ENODEV:
d550d98d 532 return AE_NOT_FOUND;
1da177e4
LT
533 break;
534 case -ETIME:
d550d98d 535 return AE_TIME;
1da177e4
LT
536 break;
537 default:
d550d98d 538 return AE_OK;
1da177e4 539 }
1da177e4
LT
540}
541
1da177e4
LT
542/* --------------------------------------------------------------------------
543 FS Interface (/proc)
544 -------------------------------------------------------------------------- */
545
50526df6 546static struct proc_dir_entry *acpi_ec_dir;
1da177e4 547
50526df6 548static int acpi_ec_read_info(struct seq_file *seq, void *offset)
1da177e4 549{
703959d4 550 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
1da177e4 551
1da177e4
LT
552 if (!ec)
553 goto end;
554
6ccedb10 555 seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
1da177e4 556 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
6ccedb10 557 (u32) ec->command_addr, (u32) ec->data_addr);
1da177e4 558 seq_printf(seq, "use global lock: %s\n",
703959d4 559 ec->global_lock ? "yes" : "no");
a86e2772 560 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
1da177e4 561
50526df6 562 end:
d550d98d 563 return 0;
1da177e4
LT
564}
565
566static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
567{
568 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
569}
570
703959d4 571static struct file_operations acpi_ec_info_ops = {
50526df6
LB
572 .open = acpi_ec_info_open_fs,
573 .read = seq_read,
574 .llseek = seq_lseek,
575 .release = single_release,
1da177e4
LT
576 .owner = THIS_MODULE,
577};
578
50526df6 579static int acpi_ec_add_fs(struct acpi_device *device)
1da177e4 580{
50526df6 581 struct proc_dir_entry *entry = NULL;
1da177e4 582
1da177e4
LT
583 if (!acpi_device_dir(device)) {
584 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
50526df6 585 acpi_ec_dir);
1da177e4 586 if (!acpi_device_dir(device))
d550d98d 587 return -ENODEV;
1da177e4
LT
588 }
589
590 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
50526df6 591 acpi_device_dir(device));
1da177e4 592 if (!entry)
d550d98d 593 return -ENODEV;
1da177e4
LT
594 else {
595 entry->proc_fops = &acpi_ec_info_ops;
596 entry->data = acpi_driver_data(device);
597 entry->owner = THIS_MODULE;
598 }
599
d550d98d 600 return 0;
1da177e4
LT
601}
602
50526df6 603static int acpi_ec_remove_fs(struct acpi_device *device)
1da177e4 604{
1da177e4
LT
605
606 if (acpi_device_dir(device)) {
607 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
608 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
609 acpi_device_dir(device) = NULL;
610 }
611
d550d98d 612 return 0;
1da177e4
LT
613}
614
1da177e4
LT
615/* --------------------------------------------------------------------------
616 Driver Interface
617 -------------------------------------------------------------------------- */
618
703959d4 619static int acpi_ec_add(struct acpi_device *device)
1da177e4 620{
50526df6
LB
621 int result = 0;
622 acpi_status status = AE_OK;
703959d4 623 struct acpi_ec *ec = NULL;
45bea155 624
45bea155 625 if (!device)
d550d98d 626 return -EINVAL;
45bea155 627
36bcbec7 628 ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
45bea155 629 if (!ec)
d550d98d 630 return -ENOMEM;
703959d4
DS
631
632 ec->handle = device->handle;
633 ec->uid = -1;
c787a855 634 mutex_init(&ec->lock);
5d0c288b 635 atomic_set(&ec->query_pending, 0);
703959d4
DS
636 if (acpi_ec_mode == EC_INTR) {
637 atomic_set(&ec->leaving_burst, 1);
638 init_waitqueue_head(&ec->wait);
45bea155 639 }
1da177e4
LT
640 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
641 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
642 acpi_driver_data(device) = ec;
643
644 /* Use the global lock for all EC transactions? */
6ccedb10 645 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
1da177e4 646
ff2fc3e9
JS
647 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
648 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
649 if (ec_ecdt) {
1da177e4 650 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
50526df6
LB
651 ACPI_ADR_SPACE_EC,
652 &acpi_ec_space_handler);
451566f4 653
a86e2772 654 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
50526df6 655 &acpi_ec_gpe_handler);
1da177e4
LT
656
657 kfree(ec_ecdt);
658 }
659
660 /* Get GPE bit assignment (EC events). */
661 /* TODO: Add support for _GPE returning a package */
6ccedb10 662 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
1da177e4 663 if (ACPI_FAILURE(status)) {
6ccedb10
AS
664 ACPI_EXCEPTION((AE_INFO, status,
665 "Obtaining GPE bit assignment"));
1da177e4
LT
666 result = -ENODEV;
667 goto end;
668 }
669
670 result = acpi_ec_add_fs(device);
671 if (result)
672 goto end;
673
703959d4 674 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
6ccedb10
AS
675 acpi_device_name(device), acpi_device_bid(device),
676 (u32) ec->gpe));
1da177e4
LT
677
678 if (!first_ec)
679 first_ec = device;
680
6ccedb10 681 end:
1da177e4
LT
682 if (result)
683 kfree(ec);
684
d550d98d 685 return result;
1da177e4
LT
686}
687
50526df6 688static int acpi_ec_remove(struct acpi_device *device, int type)
1da177e4 689{
703959d4 690 struct acpi_ec *ec = NULL;
1da177e4 691
1da177e4 692 if (!device)
d550d98d 693 return -EINVAL;
1da177e4
LT
694
695 ec = acpi_driver_data(device);
696
697 acpi_ec_remove_fs(device);
698
699 kfree(ec);
700
d550d98d 701 return 0;
1da177e4
LT
702}
703
1da177e4 704static acpi_status
50526df6 705acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1da177e4 706{
703959d4 707 struct acpi_ec *ec = (struct acpi_ec *)context;
1da177e4 708
50eca3eb 709 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1da177e4
LT
710 return AE_OK;
711 }
712
713 /*
714 * The first address region returned is the data port, and
715 * the second address region returned is the status/command
716 * port.
717 */
6ffb221a
DS
718 if (ec->data_addr == 0) {
719 ec->data_addr = resource->data.io.minimum;
720 } else if (ec->command_addr == 0) {
721 ec->command_addr = resource->data.io.minimum;
1da177e4
LT
722 } else {
723 return AE_CTRL_TERMINATE;
724 }
725
1da177e4
LT
726 return AE_OK;
727}
728
50526df6 729static int acpi_ec_start(struct acpi_device *device)
1da177e4 730{
50526df6 731 acpi_status status = AE_OK;
703959d4 732 struct acpi_ec *ec = NULL;
1da177e4 733
1da177e4 734 if (!device)
d550d98d 735 return -EINVAL;
1da177e4
LT
736
737 ec = acpi_driver_data(device);
738
739 if (!ec)
d550d98d 740 return -EINVAL;
1da177e4
LT
741
742 /*
743 * Get I/O port addresses. Convert to GAS format.
744 */
703959d4 745 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
50526df6 746 acpi_ec_io_ports, ec);
6ffb221a 747 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
703959d4
DS
748 ACPI_EXCEPTION((AE_INFO, status,
749 "Error getting I/O port addresses"));
d550d98d 750 return -ENODEV;
1da177e4
LT
751 }
752
6ffb221a 753 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
a86e2772 754 ec->gpe, ec->command_addr, ec->data_addr));
1da177e4
LT
755
756 /*
757 * Install GPE handler
758 */
a86e2772 759 status = acpi_install_gpe_handler(NULL, ec->gpe,
50526df6
LB
760 ACPI_GPE_EDGE_TRIGGERED,
761 &acpi_ec_gpe_handler, ec);
1da177e4 762 if (ACPI_FAILURE(status)) {
d550d98d 763 return -ENODEV;
1da177e4 764 }
a86e2772
AS
765 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
766 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
1da177e4 767
703959d4 768 status = acpi_install_address_space_handler(ec->handle,
50526df6
LB
769 ACPI_ADR_SPACE_EC,
770 &acpi_ec_space_handler,
771 &acpi_ec_space_setup, ec);
1da177e4 772 if (ACPI_FAILURE(status)) {
6ccedb10 773 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
d550d98d 774 return -ENODEV;
1da177e4
LT
775 }
776
d550d98d 777 return AE_OK;
1da177e4
LT
778}
779
50526df6 780static int acpi_ec_stop(struct acpi_device *device, int type)
1da177e4 781{
50526df6 782 acpi_status status = AE_OK;
703959d4 783 struct acpi_ec *ec = NULL;
1da177e4 784
1da177e4 785 if (!device)
d550d98d 786 return -EINVAL;
1da177e4
LT
787
788 ec = acpi_driver_data(device);
789
703959d4 790 status = acpi_remove_address_space_handler(ec->handle,
50526df6
LB
791 ACPI_ADR_SPACE_EC,
792 &acpi_ec_space_handler);
1da177e4 793 if (ACPI_FAILURE(status))
d550d98d 794 return -ENODEV;
1da177e4 795
6ccedb10 796 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
1da177e4 797 if (ACPI_FAILURE(status))
d550d98d 798 return -ENODEV;
1da177e4 799
d550d98d 800 return 0;
1da177e4
LT
801}
802
803static acpi_status __init
50526df6
LB
804acpi_fake_ecdt_callback(acpi_handle handle,
805 u32 Level, void *context, void **retval)
1da177e4 806{
50526df6 807 acpi_status status;
1da177e4 808
c787a855 809 mutex_init(&ec_ecdt->lock);
703959d4
DS
810 if (acpi_ec_mode == EC_INTR) {
811 init_waitqueue_head(&ec_ecdt->wait);
812 }
1da177e4 813 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
50526df6 814 acpi_ec_io_ports, ec_ecdt);
1da177e4
LT
815 if (ACPI_FAILURE(status))
816 return status;
1da177e4 817
703959d4
DS
818 ec_ecdt->uid = -1;
819 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
1da177e4 820
6ccedb10 821 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe);
1da177e4
LT
822 if (ACPI_FAILURE(status))
823 return status;
703959d4
DS
824 ec_ecdt->global_lock = TRUE;
825 ec_ecdt->handle = handle;
1da177e4 826
6ffb221a 827 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
6ccedb10
AS
828 ec_ecdt->gpe, ec_ecdt->command_addr,
829 ec_ecdt->data_addr));
1da177e4
LT
830
831 return AE_CTRL_TERMINATE;
832}
833
834/*
835 * Some BIOS (such as some from Gateway laptops) access EC region very early
836 * such as in BAT0._INI or EC._INI before an EC device is found and
837 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
838 * required, but if EC regison is accessed early, it is required.
839 * The routine tries to workaround the BIOS bug by pre-scan EC device
840 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
841 * op region (since _REG isn't invoked yet). The assumption is true for
842 * all systems found.
843 */
50526df6 844static int __init acpi_ec_fake_ecdt(void)
1da177e4 845{
50526df6
LB
846 acpi_status status;
847 int ret = 0;
1da177e4 848
703959d4 849 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
1da177e4 850
36bcbec7 851 ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1da177e4
LT
852 if (!ec_ecdt) {
853 ret = -ENOMEM;
854 goto error;
855 }
1da177e4 856
50526df6
LB
857 status = acpi_get_devices(ACPI_EC_HID,
858 acpi_fake_ecdt_callback, NULL, NULL);
1da177e4
LT
859 if (ACPI_FAILURE(status)) {
860 kfree(ec_ecdt);
861 ec_ecdt = NULL;
862 ret = -ENODEV;
703959d4 863 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
1da177e4
LT
864 goto error;
865 }
866 return 0;
6ccedb10 867 error:
1da177e4
LT
868 return ret;
869}
870
50526df6 871static int __init acpi_ec_get_real_ecdt(void)
45bea155 872{
50526df6
LB
873 acpi_status status;
874 struct acpi_table_ecdt *ecdt_ptr;
45bea155 875
15a58ed1
AS
876 status = acpi_get_table(ACPI_SIG_ECDT, 1,
877 (struct acpi_table_header **)&ecdt_ptr);
45bea155
LY
878 if (ACPI_FAILURE(status))
879 return -ENODEV;
880
703959d4 881 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
45bea155
LY
882
883 /*
884 * Generate a temporary ec context to use until the namespace is scanned
885 */
36bcbec7 886 ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
45bea155
LY
887 if (!ec_ecdt)
888 return -ENOMEM;
45bea155 889
c787a855 890 mutex_init(&ec_ecdt->lock);
703959d4
DS
891 if (acpi_ec_mode == EC_INTR) {
892 init_waitqueue_head(&ec_ecdt->wait);
45bea155 893 }
ad363f80
AS
894 ec_ecdt->command_addr = ecdt_ptr->control.address;
895 ec_ecdt->data_addr = ecdt_ptr->data.address;
896 ec_ecdt->gpe = ecdt_ptr->gpe;
1da177e4 897 /* use the GL just to be safe */
703959d4
DS
898 ec_ecdt->global_lock = TRUE;
899 ec_ecdt->uid = ecdt_ptr->uid;
1da177e4 900
ad363f80 901 status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
1da177e4
LT
902 if (ACPI_FAILURE(status)) {
903 goto error;
904 }
905
906 return 0;
6ccedb10 907 error:
703959d4 908 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1da177e4
LT
909 kfree(ec_ecdt);
910 ec_ecdt = NULL;
911
912 return -ENODEV;
913}
914
915static int __initdata acpi_fake_ecdt_enabled;
50526df6 916int __init acpi_ec_ecdt_probe(void)
1da177e4 917{
50526df6
LB
918 acpi_status status;
919 int ret;
1da177e4
LT
920
921 ret = acpi_ec_get_real_ecdt();
922 /* Try to make a fake ECDT */
923 if (ret && acpi_fake_ecdt_enabled) {
924 ret = acpi_ec_fake_ecdt();
925 }
926
927 if (ret)
928 return 0;
929
930 /*
931 * Install GPE handler
932 */
a86e2772 933 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
50526df6
LB
934 ACPI_GPE_EDGE_TRIGGERED,
935 &acpi_ec_gpe_handler, ec_ecdt);
1da177e4
LT
936 if (ACPI_FAILURE(status)) {
937 goto error;
938 }
a86e2772
AS
939 acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
940 acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
50526df6
LB
941
942 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
943 ACPI_ADR_SPACE_EC,
944 &acpi_ec_space_handler,
945 &acpi_ec_space_setup,
946 ec_ecdt);
1da177e4 947 if (ACPI_FAILURE(status)) {
a86e2772 948 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
50526df6 949 &acpi_ec_gpe_handler);
1da177e4
LT
950 goto error;
951 }
952
953 return 0;
954
50526df6 955 error:
703959d4 956 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1da177e4
LT
957 kfree(ec_ecdt);
958 ec_ecdt = NULL;
959
960 return -ENODEV;
961}
962
50526df6 963static int __init acpi_ec_init(void)
1da177e4 964{
50526df6 965 int result = 0;
1da177e4 966
1da177e4 967 if (acpi_disabled)
d550d98d 968 return 0;
1da177e4
LT
969
970 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
971 if (!acpi_ec_dir)
d550d98d 972 return -ENODEV;
1da177e4
LT
973
974 /* Now register the driver for the EC */
975 result = acpi_bus_register_driver(&acpi_ec_driver);
976 if (result < 0) {
977 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
d550d98d 978 return -ENODEV;
1da177e4
LT
979 }
980
d550d98d 981 return result;
1da177e4
LT
982}
983
984subsys_initcall(acpi_ec_init);
985
986/* EC driver currently not unloadable */
987#if 0
50526df6 988static void __exit acpi_ec_exit(void)
1da177e4 989{
1da177e4
LT
990
991 acpi_bus_unregister_driver(&acpi_ec_driver);
992
993 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
994
d550d98d 995 return;
1da177e4 996}
50526df6 997#endif /* 0 */
1da177e4
LT
998
999static int __init acpi_fake_ecdt_setup(char *str)
1000{
1001 acpi_fake_ecdt_enabled = 1;
9b41046c 1002 return 1;
1da177e4 1003}
7b15f5e7 1004
1da177e4 1005__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
02b28a33 1006static int __init acpi_ec_set_intr_mode(char *str)
45bea155 1007{
02b28a33 1008 int intr;
7b15f5e7 1009
02b28a33 1010 if (!get_option(&str, &intr))
7b15f5e7
LY
1011 return 0;
1012
02b28a33 1013 if (intr) {
703959d4 1014 acpi_ec_mode = EC_INTR;
7b15f5e7 1015 } else {
703959d4 1016 acpi_ec_mode = EC_POLL;
7b15f5e7 1017 }
703959d4 1018 acpi_ec_driver.ops.add = acpi_ec_add;
723fe2ca
LB
1019 printk(KERN_NOTICE PREFIX "%s mode.\n",
1020 intr ? "interrupt" : "polling");
703959d4 1021
9b41046c 1022 return 1;
45bea155 1023}
50526df6 1024
53f11d4f 1025__setup("ec_intr=", acpi_ec_set_intr_mode);