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