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