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