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