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