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