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