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