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