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