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