[PATCH] Simplify proc/devices and fix early termination regression
[linux-2.6-block.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"
1da177e4
LT
48#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
49#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
451566f4 50#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
1da177e4 51#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
1da177e4
LT
52#define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
53#define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
451566f4 54#define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
1da177e4 55#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
50526df6
LB
56#define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
57#define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
1da177e4
LT
58#define ACPI_EC_COMMAND_READ 0x80
59#define ACPI_EC_COMMAND_WRITE 0x81
451566f4
DT
60#define ACPI_EC_BURST_ENABLE 0x82
61#define ACPI_EC_BURST_DISABLE 0x83
1da177e4 62#define ACPI_EC_COMMAND_QUERY 0x84
02b28a33
LB
63#define EC_POLL 0xFF
64#define EC_INTR 0x00
50526df6
LB
65static int acpi_ec_remove(struct acpi_device *device, int type);
66static int acpi_ec_start(struct acpi_device *device);
67static int acpi_ec_stop(struct acpi_device *device, int type);
02b28a33
LB
68static int acpi_ec_intr_add(struct acpi_device *device);
69static int acpi_ec_poll_add(struct acpi_device *device);
1da177e4
LT
70
71static struct acpi_driver acpi_ec_driver = {
50526df6
LB
72 .name = ACPI_EC_DRIVER_NAME,
73 .class = ACPI_EC_CLASS,
74 .ids = ACPI_EC_HID,
75 .ops = {
53f11d4f 76 .add = acpi_ec_intr_add,
50526df6
LB
77 .remove = acpi_ec_remove,
78 .start = acpi_ec_start,
79 .stop = acpi_ec_stop,
80 },
1da177e4 81};
45bea155
LY
82union acpi_ec {
83 struct {
50526df6
LB
84 u32 mode;
85 acpi_handle handle;
86 unsigned long uid;
87 unsigned long gpe_bit;
88 struct acpi_generic_address status_addr;
89 struct acpi_generic_address command_addr;
90 struct acpi_generic_address data_addr;
91 unsigned long global_lock;
45bea155
LY
92 } common;
93
94 struct {
50526df6
LB
95 u32 mode;
96 acpi_handle handle;
97 unsigned long uid;
98 unsigned long gpe_bit;
99 struct acpi_generic_address status_addr;
100 struct acpi_generic_address command_addr;
101 struct acpi_generic_address data_addr;
102 unsigned long global_lock;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 atomic_t pending_gpe;
106 struct semaphore sem;
107 wait_queue_head_t wait;
02b28a33 108 } intr;
45bea155
LY
109
110 struct {
50526df6
LB
111 u32 mode;
112 acpi_handle handle;
113 unsigned long uid;
114 unsigned long gpe_bit;
115 struct acpi_generic_address status_addr;
116 struct acpi_generic_address command_addr;
117 struct acpi_generic_address data_addr;
118 unsigned long global_lock;
119 spinlock_t lock;
02b28a33 120 } poll;
1da177e4
LT
121};
122
02b28a33
LB
123static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131static void acpi_ec_gpe_poll_query(void *ec_cxt);
132static void acpi_ec_gpe_intr_query(void *ec_cxt);
133static u32 acpi_ec_gpe_poll_handler(void *data);
134static u32 acpi_ec_gpe_intr_handler(void *data);
45bea155 135static acpi_status __init
02b28a33 136acpi_fake_ecdt_poll_callback(acpi_handle handle,
50526df6 137 u32 Level, void *context, void **retval);
45bea155
LY
138
139static acpi_status __init
02b28a33 140acpi_fake_ecdt_intr_callback(acpi_handle handle,
50526df6
LB
141 u32 Level, void *context, void **retval);
142
02b28a33
LB
143static int __init acpi_ec_poll_get_real_ecdt(void);
144static int __init acpi_ec_intr_get_real_ecdt(void);
1da177e4 145/* If we find an EC via the ECDT, we need to keep a ptr to its context */
50526df6 146static union acpi_ec *ec_ecdt;
1da177e4
LT
147
148/* External interfaces use first EC only, so remember */
149static struct acpi_device *first_ec;
53f11d4f 150static int acpi_ec_poll_mode = EC_INTR;
1da177e4
LT
151
152/* --------------------------------------------------------------------------
153 Transaction Management
154 -------------------------------------------------------------------------- */
155
858119e1 156static u32 acpi_ec_read_status(union acpi_ec *ec)
1da177e4 157{
50526df6 158 u32 status = 0;
1da177e4 159
45bea155 160 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
451566f4
DT
161 return status;
162}
163
50526df6 164static int acpi_ec_wait(union acpi_ec *ec, u8 event)
45bea155 165{
02b28a33
LB
166 if (acpi_ec_poll_mode)
167 return acpi_ec_poll_wait(ec, event);
45bea155 168 else
02b28a33 169 return acpi_ec_intr_wait(ec, event);
45bea155
LY
170}
171
02b28a33 172static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
45bea155 173{
50526df6
LB
174 u32 acpi_ec_status = 0;
175 u32 i = ACPI_EC_UDELAY_COUNT;
45bea155
LY
176
177 if (!ec)
178 return -EINVAL;
179
180 /* Poll the EC status register waiting for the event to occur. */
181 switch (event) {
182 case ACPI_EC_EVENT_OBF:
183 do {
50526df6
LB
184 acpi_hw_low_level_read(8, &acpi_ec_status,
185 &ec->common.status_addr);
45bea155
LY
186 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
187 return 0;
188 udelay(ACPI_EC_UDELAY);
50526df6 189 } while (--i > 0);
45bea155
LY
190 break;
191 case ACPI_EC_EVENT_IBE:
192 do {
50526df6
LB
193 acpi_hw_low_level_read(8, &acpi_ec_status,
194 &ec->common.status_addr);
45bea155
LY
195 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
196 return 0;
197 udelay(ACPI_EC_UDELAY);
50526df6 198 } while (--i > 0);
45bea155
LY
199 break;
200 default:
201 return -EINVAL;
202 }
203
204 return -ETIME;
205}
02b28a33 206static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
451566f4 207{
50526df6 208 int result = 0;
451566f4
DT
209
210 ACPI_FUNCTION_TRACE("acpi_ec_wait");
1da177e4 211
02b28a33 212 ec->intr.expect_event = event;
451566f4
DT
213 smp_mb();
214
716e084e 215 switch (event) {
716e084e
LY
216 case ACPI_EC_EVENT_IBE:
217 if (~acpi_ec_read_status(ec) & event) {
02b28a33 218 ec->intr.expect_event = 0;
716e084e
LY
219 return_VALUE(0);
220 }
221 break;
06a2a385
LY
222 default:
223 break;
716e084e
LY
224 }
225
02b28a33
LB
226 result = wait_event_timeout(ec->intr.wait,
227 !ec->intr.expect_event,
50526df6
LB
228 msecs_to_jiffies(ACPI_EC_DELAY));
229
02b28a33 230 ec->intr.expect_event = 0;
451566f4
DT
231 smp_mb();
232
451566f4
DT
233 /*
234 * Verify that the event in question has actually happened by
235 * querying EC status. Do the check even if operation timed-out
236 * to make sure that we did not miss interrupt.
237 */
1da177e4
LT
238 switch (event) {
239 case ACPI_EC_EVENT_OBF:
451566f4
DT
240 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
241 return_VALUE(0);
1da177e4 242 break;
451566f4 243
1da177e4 244 case ACPI_EC_EVENT_IBE:
451566f4
DT
245 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
246 return_VALUE(0);
1da177e4 247 break;
1da177e4
LT
248 }
249
451566f4 250 return_VALUE(-ETIME);
1da177e4
LT
251}
252
02b28a33 253#ifdef ACPI_FUTURE_USAGE
06a2a385
LY
254/*
255 * Note: samsung nv5000 doesn't work with ec burst mode.
256 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
257 */
258int acpi_ec_enter_burst_mode(union acpi_ec *ec)
451566f4 259{
50526df6
LB
260 u32 tmp = 0;
261 int status = 0;
451566f4
DT
262
263 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
264
265 status = acpi_ec_read_status(ec);
50526df6 266 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
716e084e 267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
50526df6 268 if (status)
716e084e 269 goto end;
50526df6
LB
270 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
271 &ec->common.command_addr);
451566f4 272 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
45bea155 273 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
50526df6 274 if (tmp != 0x90) { /* Burst ACK byte */
451566f4
DT
275 return_VALUE(-EINVAL);
276 }
668d74c0
LY
277 }
278
02b28a33 279 atomic_set(&ec->intr.leaving_burst, 0);
451566f4 280 return_VALUE(0);
50526df6 281 end:
1e8df53c 282 printk(KERN_WARNING PREFIX "Error in acpi_ec_wait\n");
716e084e 283 return_VALUE(-1);
451566f4
DT
284}
285
06a2a385 286int acpi_ec_leave_burst_mode(union acpi_ec *ec)
451566f4 287{
06a2a385 288 int status = 0;
451566f4
DT
289
290 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
291
06a2a385
LY
292 status = acpi_ec_read_status(ec);
293 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
294 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
295 if(status)
296 goto end;
297 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
298 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
299 }
02b28a33 300 atomic_set(&ec->intr.leaving_burst, 1);
451566f4 301 return_VALUE(0);
06a2a385 302end:
1e8df53c 303 printk(KERN_WARNING PREFIX "leave burst_mode:error\n");
06a2a385 304 return_VALUE(-1);
451566f4 305}
02b28a33 306#endif /* ACPI_FUTURE_USAGE */
451566f4 307
50526df6 308static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
45bea155 309{
02b28a33
LB
310 if (acpi_ec_poll_mode)
311 return acpi_ec_poll_read(ec, address, data);
45bea155 312 else
02b28a33 313 return acpi_ec_intr_read(ec, address, data);
45bea155 314}
50526df6 315static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
45bea155 316{
02b28a33
LB
317 if (acpi_ec_poll_mode)
318 return acpi_ec_poll_write(ec, address, data);
45bea155 319 else
02b28a33 320 return acpi_ec_intr_write(ec, address, data);
45bea155 321}
02b28a33 322static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
45bea155 323{
50526df6
LB
324 acpi_status status = AE_OK;
325 int result = 0;
326 unsigned long flags = 0;
327 u32 glk = 0;
45bea155
LY
328
329 ACPI_FUNCTION_TRACE("acpi_ec_read");
330
331 if (!ec || !data)
332 return_VALUE(-EINVAL);
333
334 *data = 0;
335
336 if (ec->common.global_lock) {
337 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
338 if (ACPI_FAILURE(status))
339 return_VALUE(-ENODEV);
340 }
341
02b28a33 342 spin_lock_irqsave(&ec->poll.lock, flags);
45bea155 343
50526df6
LB
344 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
345 &ec->common.command_addr);
45bea155
LY
346 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
347 if (result)
348 goto end;
349
350 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
351 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
352 if (result)
353 goto end;
354
355 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
356
357 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
50526df6
LB
358 *data, address));
359
360 end:
02b28a33 361 spin_unlock_irqrestore(&ec->poll.lock, flags);
45bea155
LY
362
363 if (ec->common.global_lock)
364 acpi_release_global_lock(glk);
365
366 return_VALUE(result);
367}
368
02b28a33 369static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
45bea155 370{
50526df6
LB
371 int result = 0;
372 acpi_status status = AE_OK;
373 unsigned long flags = 0;
374 u32 glk = 0;
45bea155
LY
375
376 ACPI_FUNCTION_TRACE("acpi_ec_write");
377
378 if (!ec)
379 return_VALUE(-EINVAL);
380
381 if (ec->common.global_lock) {
382 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
383 if (ACPI_FAILURE(status))
384 return_VALUE(-ENODEV);
385 }
386
02b28a33 387 spin_lock_irqsave(&ec->poll.lock, flags);
45bea155 388
50526df6
LB
389 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
390 &ec->common.command_addr);
45bea155
LY
391 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
392 if (result)
393 goto end;
394
395 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
396 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
397 if (result)
398 goto end;
399
400 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
401 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
402 if (result)
403 goto end;
404
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
50526df6 406 data, address));
45bea155 407
50526df6 408 end:
02b28a33 409 spin_unlock_irqrestore(&ec->poll.lock, flags);
45bea155
LY
410
411 if (ec->common.global_lock)
412 acpi_release_global_lock(glk);
413
414 return_VALUE(result);
415}
416
02b28a33 417static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
1da177e4 418{
50526df6
LB
419 int status = 0;
420 u32 glk;
1da177e4
LT
421
422 ACPI_FUNCTION_TRACE("acpi_ec_read");
423
424 if (!ec || !data)
425 return_VALUE(-EINVAL);
426
427 *data = 0;
428
45bea155 429 if (ec->common.global_lock) {
1da177e4
LT
430 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
431 if (ACPI_FAILURE(status))
432 return_VALUE(-ENODEV);
433 }
451566f4
DT
434
435 WARN_ON(in_interrupt());
02b28a33 436 down(&ec->intr.sem);
451566f4 437
716e084e
LY
438 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
439 if (status) {
1e8df53c 440 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
451566f4 441 goto end;
716e084e 442 }
50526df6
LB
443 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
444 &ec->common.command_addr);
451566f4 445 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
451566f4 446 if (status) {
1e8df53c 447 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
451566f4 448 }
1da177e4 449
45bea155 450 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
50526df6
LB
451 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
452 if (status) {
1e8df53c 453 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
1da177e4 454 goto end;
451566f4 455 }
45bea155 456 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
1da177e4 457 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
50526df6
LB
458 *data, address));
459
460 end:
02b28a33 461 up(&ec->intr.sem);
1da177e4 462
45bea155 463 if (ec->common.global_lock)
1da177e4
LT
464 acpi_release_global_lock(glk);
465
451566f4 466 return_VALUE(status);
1da177e4
LT
467}
468
02b28a33 469static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
1da177e4 470{
50526df6
LB
471 int status = 0;
472 u32 glk;
1da177e4
LT
473
474 ACPI_FUNCTION_TRACE("acpi_ec_write");
475
476 if (!ec)
477 return_VALUE(-EINVAL);
716e084e 478
45bea155 479 if (ec->common.global_lock) {
1da177e4
LT
480 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
481 if (ACPI_FAILURE(status))
482 return_VALUE(-ENODEV);
483 }
484
451566f4 485 WARN_ON(in_interrupt());
02b28a33 486 down(&ec->intr.sem);
451566f4 487
716e084e 488 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
50526df6 489 if (status) {
1e8df53c 490 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
451566f4 491 }
50526df6
LB
492 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
493 &ec->common.command_addr);
451566f4 494 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
716e084e 495 if (status) {
1e8df53c 496 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
451566f4 497 }
1da177e4 498
45bea155 499 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
451566f4 500 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
50526df6 501 if (status) {
1e8df53c 502 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
451566f4 503 }
1da177e4 504
45bea155 505 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
1da177e4
LT
506
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
50526df6 508 data, address));
1da177e4 509
02b28a33 510 up(&ec->intr.sem);
1da177e4 511
45bea155 512 if (ec->common.global_lock)
1da177e4
LT
513 acpi_release_global_lock(glk);
514
451566f4 515 return_VALUE(status);
1da177e4
LT
516}
517
518/*
519 * Externally callable EC access functions. For now, assume 1 EC only
520 */
50526df6 521int ec_read(u8 addr, u8 * val)
1da177e4 522{
45bea155 523 union acpi_ec *ec;
1da177e4
LT
524 int err;
525 u32 temp_data;
526
527 if (!first_ec)
528 return -ENODEV;
529
530 ec = acpi_driver_data(first_ec);
531
532 err = acpi_ec_read(ec, addr, &temp_data);
533
534 if (!err) {
535 *val = temp_data;
536 return 0;
50526df6 537 } else
1da177e4
LT
538 return err;
539}
50526df6 540
1da177e4
LT
541EXPORT_SYMBOL(ec_read);
542
50526df6 543int ec_write(u8 addr, u8 val)
1da177e4 544{
45bea155 545 union acpi_ec *ec;
1da177e4
LT
546 int err;
547
548 if (!first_ec)
549 return -ENODEV;
550
551 ec = acpi_driver_data(first_ec);
552
553 err = acpi_ec_write(ec, addr, val);
554
555 return err;
556}
50526df6 557
1da177e4
LT
558EXPORT_SYMBOL(ec_write);
559
50526df6 560static int acpi_ec_query(union acpi_ec *ec, u32 * data)
45bea155 561{
02b28a33
LB
562 if (acpi_ec_poll_mode)
563 return acpi_ec_poll_query(ec, data);
45bea155 564 else
02b28a33 565 return acpi_ec_intr_query(ec, data);
45bea155 566}
02b28a33 567static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
45bea155 568{
50526df6
LB
569 int result = 0;
570 acpi_status status = AE_OK;
571 unsigned long flags = 0;
572 u32 glk = 0;
45bea155
LY
573
574 ACPI_FUNCTION_TRACE("acpi_ec_query");
575
576 if (!ec || !data)
577 return_VALUE(-EINVAL);
578
579 *data = 0;
580
581 if (ec->common.global_lock) {
582 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
583 if (ACPI_FAILURE(status))
584 return_VALUE(-ENODEV);
585 }
586
587 /*
588 * Query the EC to find out which _Qxx method we need to evaluate.
589 * Note that successful completion of the query causes the ACPI_EC_SCI
590 * bit to be cleared (and thus clearing the interrupt source).
591 */
02b28a33 592 spin_lock_irqsave(&ec->poll.lock, flags);
45bea155 593
50526df6
LB
594 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
595 &ec->common.command_addr);
45bea155
LY
596 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
597 if (result)
598 goto end;
599
600 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
601 if (!*data)
602 result = -ENODATA;
603
50526df6 604 end:
02b28a33 605 spin_unlock_irqrestore(&ec->poll.lock, flags);
45bea155
LY
606
607 if (ec->common.global_lock)
608 acpi_release_global_lock(glk);
609
610 return_VALUE(result);
611}
02b28a33 612static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
1da177e4 613{
50526df6
LB
614 int status = 0;
615 u32 glk;
1da177e4
LT
616
617 ACPI_FUNCTION_TRACE("acpi_ec_query");
618
619 if (!ec || !data)
620 return_VALUE(-EINVAL);
1da177e4
LT
621 *data = 0;
622
45bea155 623 if (ec->common.global_lock) {
1da177e4
LT
624 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
625 if (ACPI_FAILURE(status))
626 return_VALUE(-ENODEV);
627 }
628
02b28a33 629 down(&ec->intr.sem);
716e084e
LY
630
631 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
632 if (status) {
1e8df53c 633 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
451566f4 634 goto end;
716e084e 635 }
1da177e4
LT
636 /*
637 * Query the EC to find out which _Qxx method we need to evaluate.
638 * Note that successful completion of the query causes the ACPI_EC_SCI
639 * bit to be cleared (and thus clearing the interrupt source).
640 */
50526df6
LB
641 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
642 &ec->common.command_addr);
451566f4 643 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
50526df6 644 if (status) {
1e8df53c 645 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
1da177e4 646 goto end;
451566f4
DT
647 }
648
45bea155 649 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
1da177e4 650 if (!*data)
451566f4 651 status = -ENODATA;
1da177e4 652
50526df6 653 end:
02b28a33 654 up(&ec->intr.sem);
1da177e4 655
45bea155 656 if (ec->common.global_lock)
1da177e4
LT
657 acpi_release_global_lock(glk);
658
451566f4 659 return_VALUE(status);
1da177e4
LT
660}
661
1da177e4
LT
662/* --------------------------------------------------------------------------
663 Event Management
664 -------------------------------------------------------------------------- */
665
45bea155 666union acpi_ec_query_data {
50526df6
LB
667 acpi_handle handle;
668 u8 data;
1da177e4
LT
669};
670
50526df6 671static void acpi_ec_gpe_query(void *ec_cxt)
1da177e4 672{
02b28a33
LB
673 if (acpi_ec_poll_mode)
674 acpi_ec_gpe_poll_query(ec_cxt);
45bea155 675 else
02b28a33 676 acpi_ec_gpe_intr_query(ec_cxt);
45bea155
LY
677}
678
02b28a33 679static void acpi_ec_gpe_poll_query(void *ec_cxt)
45bea155 680{
50526df6
LB
681 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
682 u32 value = 0;
683 unsigned long flags = 0;
684 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
685 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
686 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
687 };
45bea155
LY
688
689 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
690
691 if (!ec_cxt)
692 goto end;
693
02b28a33 694 spin_lock_irqsave(&ec->poll.lock, flags);
45bea155 695 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
02b28a33 696 spin_unlock_irqrestore(&ec->poll.lock, flags);
45bea155
LY
697
698 /* TBD: Implement asynch events!
699 * NOTE: All we care about are EC-SCI's. Other EC events are
700 * handled via polling (yuck!). This is because some systems
701 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
702 * a purely interrupt-driven approach (grumble, grumble).
703 */
704 if (!(value & ACPI_EC_FLAG_SCI))
705 goto end;
706
707 if (acpi_ec_query(ec, &value))
708 goto end;
709
710 object_name[2] = hex[((value >> 4) & 0x0F)];
711 object_name[3] = hex[(value & 0x0F)];
712
713 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
714
715 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
716
50526df6 717 end:
45bea155
LY
718 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
719}
02b28a33 720static void acpi_ec_gpe_intr_query(void *ec_cxt)
45bea155 721{
50526df6
LB
722 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
723 u32 value;
724 int result = -ENODATA;
725 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
726 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
727 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
728 };
1da177e4
LT
729
730 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
731
451566f4
DT
732 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
733 result = acpi_ec_query(ec, &value);
1da177e4 734
451566f4 735 if (result)
1da177e4
LT
736 goto end;
737
1da177e4
LT
738 object_name[2] = hex[((value >> 4) & 0x0F)];
739 object_name[3] = hex[(value & 0x0F)];
740
741 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
742
45bea155 743 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
50526df6 744 end:
02b28a33 745 atomic_dec(&ec->intr.pending_gpe);
451566f4 746 return;
1da177e4
LT
747}
748
50526df6 749static u32 acpi_ec_gpe_handler(void *data)
45bea155 750{
02b28a33
LB
751 if (acpi_ec_poll_mode)
752 return acpi_ec_gpe_poll_handler(data);
45bea155 753 else
02b28a33 754 return acpi_ec_gpe_intr_handler(data);
45bea155 755}
02b28a33 756static u32 acpi_ec_gpe_poll_handler(void *data)
45bea155 757{
50526df6
LB
758 acpi_status status = AE_OK;
759 union acpi_ec *ec = (union acpi_ec *)data;
45bea155
LY
760
761 if (!ec)
762 return ACPI_INTERRUPT_NOT_HANDLED;
763
764 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
765
766 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
50526df6 767 acpi_ec_gpe_query, ec);
45bea155
LY
768
769 if (status == AE_OK)
770 return ACPI_INTERRUPT_HANDLED;
771 else
772 return ACPI_INTERRUPT_NOT_HANDLED;
773}
02b28a33 774static u32 acpi_ec_gpe_intr_handler(void *data)
1da177e4 775{
50526df6
LB
776 acpi_status status = AE_OK;
777 u32 value;
778 union acpi_ec *ec = (union acpi_ec *)data;
1da177e4
LT
779
780 if (!ec)
781 return ACPI_INTERRUPT_NOT_HANDLED;
782
716e084e 783 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
451566f4 784 value = acpi_ec_read_status(ec);
1da177e4 785
02b28a33 786 switch (ec->intr.expect_event) {
716e084e
LY
787 case ACPI_EC_EVENT_OBF:
788 if (!(value & ACPI_EC_FLAG_OBF))
789 break;
790 case ACPI_EC_EVENT_IBE:
791 if ((value & ACPI_EC_FLAG_IBF))
792 break;
02b28a33
LB
793 ec->intr.expect_event = 0;
794 wake_up(&ec->intr.wait);
716e084e
LY
795 return ACPI_INTERRUPT_HANDLED;
796 default:
797 break;
451566f4
DT
798 }
799
50526df6 800 if (value & ACPI_EC_FLAG_SCI) {
02b28a33 801 atomic_add(1, &ec->intr.pending_gpe);
451566f4 802 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
50526df6 803 acpi_ec_gpe_query, ec);
17e9c78a 804 return status == AE_OK ?
50526df6
LB
805 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
806 }
45bea155 807 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
451566f4 808 return status == AE_OK ?
50526df6 809 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
1da177e4
LT
810}
811
812/* --------------------------------------------------------------------------
813 Address Space Management
814 -------------------------------------------------------------------------- */
815
816static acpi_status
50526df6
LB
817acpi_ec_space_setup(acpi_handle region_handle,
818 u32 function, void *handler_context, void **return_context)
1da177e4
LT
819{
820 /*
821 * The EC object is in the handler context and is needed
822 * when calling the acpi_ec_space_handler.
823 */
50526df6
LB
824 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
825 handler_context : NULL;
1da177e4
LT
826
827 return AE_OK;
828}
829
1da177e4 830static acpi_status
50526df6
LB
831acpi_ec_space_handler(u32 function,
832 acpi_physical_address address,
833 u32 bit_width,
834 acpi_integer * value,
835 void *handler_context, void *region_context)
1da177e4 836{
50526df6
LB
837 int result = 0;
838 union acpi_ec *ec = NULL;
839 u64 temp = *value;
840 acpi_integer f_v = 0;
841 int i = 0;
1da177e4
LT
842
843 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
844
845 if ((address > 0xFF) || !value || !handler_context)
846 return_VALUE(AE_BAD_PARAMETER);
847
fa9cd547 848 if (bit_width != 8 && acpi_strict) {
50526df6
LB
849 printk(KERN_WARNING PREFIX
850 "acpi_ec_space_handler: bit_width should be 8\n");
fa9cd547 851 return_VALUE(AE_BAD_PARAMETER);
1da177e4
LT
852 }
853
50526df6 854 ec = (union acpi_ec *)handler_context;
1da177e4 855
50526df6 856 next_byte:
1da177e4
LT
857 switch (function) {
858 case ACPI_READ:
fa9cd547 859 temp = 0;
50526df6 860 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
1da177e4
LT
861 break;
862 case ACPI_WRITE:
fa9cd547 863 result = acpi_ec_write(ec, (u8) address, (u8) temp);
1da177e4
LT
864 break;
865 default:
866 result = -EINVAL;
867 goto out;
868 break;
869 }
870
871 bit_width -= 8;
fa9cd547
LY
872 if (bit_width) {
873 if (function == ACPI_READ)
874 f_v |= temp << 8 * i;
875 if (function == ACPI_WRITE)
876 temp >>= 8;
1da177e4 877 i++;
83ea7445 878 address++;
1da177e4
LT
879 goto next_byte;
880 }
881
fa9cd547
LY
882 if (function == ACPI_READ) {
883 f_v |= temp << 8 * i;
1da177e4
LT
884 *value = f_v;
885 }
886
50526df6 887 out:
1da177e4
LT
888 switch (result) {
889 case -EINVAL:
890 return_VALUE(AE_BAD_PARAMETER);
891 break;
892 case -ENODEV:
893 return_VALUE(AE_NOT_FOUND);
894 break;
895 case -ETIME:
896 return_VALUE(AE_TIME);
897 break;
898 default:
899 return_VALUE(AE_OK);
900 }
1da177e4
LT
901}
902
1da177e4
LT
903/* --------------------------------------------------------------------------
904 FS Interface (/proc)
905 -------------------------------------------------------------------------- */
906
50526df6 907static struct proc_dir_entry *acpi_ec_dir;
1da177e4 908
50526df6 909static int acpi_ec_read_info(struct seq_file *seq, void *offset)
1da177e4 910{
50526df6 911 union acpi_ec *ec = (union acpi_ec *)seq->private;
1da177e4
LT
912
913 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
914
915 if (!ec)
916 goto end;
917
918 seq_printf(seq, "gpe bit: 0x%02x\n",
50526df6 919 (u32) ec->common.gpe_bit);
1da177e4 920 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
50526df6
LB
921 (u32) ec->common.status_addr.address,
922 (u32) ec->common.data_addr.address);
1da177e4 923 seq_printf(seq, "use global lock: %s\n",
50526df6 924 ec->common.global_lock ? "yes" : "no");
45bea155 925 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1da177e4 926
50526df6 927 end:
1da177e4
LT
928 return_VALUE(0);
929}
930
931static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
932{
933 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
934}
935
936static struct file_operations acpi_ec_info_ops = {
50526df6
LB
937 .open = acpi_ec_info_open_fs,
938 .read = seq_read,
939 .llseek = seq_lseek,
940 .release = single_release,
1da177e4
LT
941 .owner = THIS_MODULE,
942};
943
50526df6 944static int acpi_ec_add_fs(struct acpi_device *device)
1da177e4 945{
50526df6 946 struct proc_dir_entry *entry = NULL;
1da177e4
LT
947
948 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
949
950 if (!acpi_device_dir(device)) {
951 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
50526df6 952 acpi_ec_dir);
1da177e4
LT
953 if (!acpi_device_dir(device))
954 return_VALUE(-ENODEV);
955 }
956
957 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
50526df6 958 acpi_device_dir(device));
1da177e4
LT
959 if (!entry)
960 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
50526df6
LB
961 "Unable to create '%s' fs entry\n",
962 ACPI_EC_FILE_INFO));
1da177e4
LT
963 else {
964 entry->proc_fops = &acpi_ec_info_ops;
965 entry->data = acpi_driver_data(device);
966 entry->owner = THIS_MODULE;
967 }
968
969 return_VALUE(0);
970}
971
50526df6 972static int acpi_ec_remove_fs(struct acpi_device *device)
1da177e4
LT
973{
974 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
975
976 if (acpi_device_dir(device)) {
977 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
978 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
979 acpi_device_dir(device) = NULL;
980 }
981
982 return_VALUE(0);
983}
984
1da177e4
LT
985/* --------------------------------------------------------------------------
986 Driver Interface
987 -------------------------------------------------------------------------- */
988
02b28a33 989static int acpi_ec_poll_add(struct acpi_device *device)
1da177e4 990{
50526df6
LB
991 int result = 0;
992 acpi_status status = AE_OK;
993 union acpi_ec *ec = NULL;
994 unsigned long uid;
45bea155
LY
995
996 ACPI_FUNCTION_TRACE("acpi_ec_add");
997
998 if (!device)
999 return_VALUE(-EINVAL);
1000
1001 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1002 if (!ec)
1003 return_VALUE(-ENOMEM);
1004 memset(ec, 0, sizeof(union acpi_ec));
1005
1006 ec->common.handle = device->handle;
1007 ec->common.uid = -1;
02b28a33 1008 spin_lock_init(&ec->poll.lock);
45bea155
LY
1009 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1010 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1011 acpi_driver_data(device) = ec;
1012
1013 /* Use the global lock for all EC transactions? */
50526df6
LB
1014 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1015 &ec->common.global_lock);
45bea155
LY
1016
1017 /* If our UID matches the UID for the ECDT-enumerated EC,
50526df6 1018 we now have the *real* EC info, so kill the makeshift one. */
45bea155
LY
1019 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1020 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1021 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
50526df6
LB
1022 ACPI_ADR_SPACE_EC,
1023 &acpi_ec_space_handler);
1024
1025 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1026 &acpi_ec_gpe_handler);
45bea155
LY
1027
1028 kfree(ec_ecdt);
1029 }
1030
1031 /* Get GPE bit assignment (EC events). */
1032 /* TODO: Add support for _GPE returning a package */
50526df6
LB
1033 status =
1034 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1035 &ec->common.gpe_bit);
45bea155
LY
1036 if (ACPI_FAILURE(status)) {
1037 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
50526df6 1038 "Error obtaining GPE bit assignment\n"));
45bea155
LY
1039 result = -ENODEV;
1040 goto end;
1041 }
1042
1043 result = acpi_ec_add_fs(device);
1044 if (result)
1045 goto end;
1046
02b28a33 1047 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
50526df6
LB
1048 acpi_device_name(device), acpi_device_bid(device),
1049 (u32) ec->common.gpe_bit);
45bea155
LY
1050
1051 if (!first_ec)
1052 first_ec = device;
1053
50526df6 1054 end:
45bea155
LY
1055 if (result)
1056 kfree(ec);
1057
1058 return_VALUE(result);
1059}
02b28a33 1060static int acpi_ec_intr_add(struct acpi_device *device)
45bea155 1061{
50526df6
LB
1062 int result = 0;
1063 acpi_status status = AE_OK;
1064 union acpi_ec *ec = NULL;
1065 unsigned long uid;
1da177e4
LT
1066
1067 ACPI_FUNCTION_TRACE("acpi_ec_add");
1068
1069 if (!device)
1070 return_VALUE(-EINVAL);
1071
45bea155 1072 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1da177e4
LT
1073 if (!ec)
1074 return_VALUE(-ENOMEM);
45bea155
LY
1075 memset(ec, 0, sizeof(union acpi_ec));
1076
1077 ec->common.handle = device->handle;
1078 ec->common.uid = -1;
02b28a33
LB
1079 atomic_set(&ec->intr.pending_gpe, 0);
1080 atomic_set(&ec->intr.leaving_burst, 1);
1081 init_MUTEX(&ec->intr.sem);
1082 init_waitqueue_head(&ec->intr.wait);
1da177e4
LT
1083 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1084 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1085 acpi_driver_data(device) = ec;
1086
1087 /* Use the global lock for all EC transactions? */
50526df6
LB
1088 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1089 &ec->common.global_lock);
1da177e4
LT
1090
1091 /* If our UID matches the UID for the ECDT-enumerated EC,
50526df6 1092 we now have the *real* EC info, so kill the makeshift one. */
45bea155
LY
1093 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1094 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1da177e4 1095 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
50526df6
LB
1096 ACPI_ADR_SPACE_EC,
1097 &acpi_ec_space_handler);
451566f4 1098
50526df6
LB
1099 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1100 &acpi_ec_gpe_handler);
1da177e4
LT
1101
1102 kfree(ec_ecdt);
1103 }
1104
1105 /* Get GPE bit assignment (EC events). */
1106 /* TODO: Add support for _GPE returning a package */
50526df6
LB
1107 status =
1108 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1109 &ec->common.gpe_bit);
1da177e4
LT
1110 if (ACPI_FAILURE(status)) {
1111 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
50526df6 1112 "Error obtaining GPE bit assignment\n"));
1da177e4
LT
1113 result = -ENODEV;
1114 goto end;
1115 }
1116
1117 result = acpi_ec_add_fs(device);
1118 if (result)
1119 goto end;
1120
02b28a33 1121 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
50526df6
LB
1122 acpi_device_name(device), acpi_device_bid(device),
1123 (u32) ec->common.gpe_bit);
1da177e4
LT
1124
1125 if (!first_ec)
1126 first_ec = device;
1127
50526df6 1128 end:
1da177e4
LT
1129 if (result)
1130 kfree(ec);
1131
1132 return_VALUE(result);
1133}
1134
50526df6 1135static int acpi_ec_remove(struct acpi_device *device, int type)
1da177e4 1136{
50526df6 1137 union acpi_ec *ec = NULL;
1da177e4
LT
1138
1139 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1140
1141 if (!device)
1142 return_VALUE(-EINVAL);
1143
1144 ec = acpi_driver_data(device);
1145
1146 acpi_ec_remove_fs(device);
1147
1148 kfree(ec);
1149
1150 return_VALUE(0);
1151}
1152
1da177e4 1153static acpi_status
50526df6 1154acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1da177e4 1155{
50526df6 1156 union acpi_ec *ec = (union acpi_ec *)context;
1da177e4
LT
1157 struct acpi_generic_address *addr;
1158
50eca3eb 1159 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1da177e4
LT
1160 return AE_OK;
1161 }
1162
1163 /*
1164 * The first address region returned is the data port, and
1165 * the second address region returned is the status/command
1166 * port.
1167 */
45bea155
LY
1168 if (ec->common.data_addr.register_bit_width == 0) {
1169 addr = &ec->common.data_addr;
1170 } else if (ec->common.command_addr.register_bit_width == 0) {
1171 addr = &ec->common.command_addr;
1da177e4
LT
1172 } else {
1173 return AE_CTRL_TERMINATE;
1174 }
1175
1176 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1177 addr->register_bit_width = 8;
1178 addr->register_bit_offset = 0;
50eca3eb 1179 addr->address = resource->data.io.minimum;
1da177e4
LT
1180
1181 return AE_OK;
1182}
1183
50526df6 1184static int acpi_ec_start(struct acpi_device *device)
1da177e4 1185{
50526df6
LB
1186 acpi_status status = AE_OK;
1187 union acpi_ec *ec = NULL;
1da177e4
LT
1188
1189 ACPI_FUNCTION_TRACE("acpi_ec_start");
1190
1191 if (!device)
1192 return_VALUE(-EINVAL);
1193
1194 ec = acpi_driver_data(device);
1195
1196 if (!ec)
1197 return_VALUE(-EINVAL);
1198
1199 /*
1200 * Get I/O port addresses. Convert to GAS format.
1201 */
45bea155 1202 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
50526df6
LB
1203 acpi_ec_io_ports, ec);
1204 if (ACPI_FAILURE(status)
1205 || ec->common.command_addr.register_bit_width == 0) {
1206 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1207 "Error getting I/O port addresses"));
1da177e4
LT
1208 return_VALUE(-ENODEV);
1209 }
1210
45bea155 1211 ec->common.status_addr = ec->common.command_addr;
1da177e4
LT
1212
1213 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
50526df6
LB
1214 (u32) ec->common.gpe_bit,
1215 (u32) ec->common.command_addr.address,
1216 (u32) ec->common.data_addr.address));
1da177e4
LT
1217
1218 /*
1219 * Install GPE handler
1220 */
45bea155 1221 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
50526df6
LB
1222 ACPI_GPE_EDGE_TRIGGERED,
1223 &acpi_ec_gpe_handler, ec);
1da177e4
LT
1224 if (ACPI_FAILURE(status)) {
1225 return_VALUE(-ENODEV);
1226 }
50526df6
LB
1227 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1228 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1da177e4 1229
50526df6
LB
1230 status = acpi_install_address_space_handler(ec->common.handle,
1231 ACPI_ADR_SPACE_EC,
1232 &acpi_ec_space_handler,
1233 &acpi_ec_space_setup, ec);
1da177e4 1234 if (ACPI_FAILURE(status)) {
50526df6
LB
1235 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1236 &acpi_ec_gpe_handler);
1da177e4
LT
1237 return_VALUE(-ENODEV);
1238 }
1239
1240 return_VALUE(AE_OK);
1241}
1242
50526df6 1243static int acpi_ec_stop(struct acpi_device *device, int type)
1da177e4 1244{
50526df6
LB
1245 acpi_status status = AE_OK;
1246 union acpi_ec *ec = NULL;
1da177e4
LT
1247
1248 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1249
1250 if (!device)
1251 return_VALUE(-EINVAL);
1252
1253 ec = acpi_driver_data(device);
1254
45bea155 1255 status = acpi_remove_address_space_handler(ec->common.handle,
50526df6
LB
1256 ACPI_ADR_SPACE_EC,
1257 &acpi_ec_space_handler);
1da177e4
LT
1258 if (ACPI_FAILURE(status))
1259 return_VALUE(-ENODEV);
1260
50526df6
LB
1261 status =
1262 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1263 &acpi_ec_gpe_handler);
1da177e4
LT
1264 if (ACPI_FAILURE(status))
1265 return_VALUE(-ENODEV);
1266
1267 return_VALUE(0);
1268}
1269
1270static acpi_status __init
50526df6
LB
1271acpi_fake_ecdt_callback(acpi_handle handle,
1272 u32 Level, void *context, void **retval)
1da177e4 1273{
45bea155 1274
02b28a33
LB
1275 if (acpi_ec_poll_mode)
1276 return acpi_fake_ecdt_poll_callback(handle,
50526df6 1277 Level, context, retval);
45bea155 1278 else
02b28a33 1279 return acpi_fake_ecdt_intr_callback(handle,
50526df6 1280 Level, context, retval);
45bea155
LY
1281}
1282
1283static acpi_status __init
02b28a33 1284acpi_fake_ecdt_poll_callback(acpi_handle handle,
50526df6 1285 u32 Level, void *context, void **retval)
45bea155 1286{
50526df6 1287 acpi_status status;
45bea155
LY
1288
1289 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
50526df6 1290 acpi_ec_io_ports, ec_ecdt);
45bea155
LY
1291 if (ACPI_FAILURE(status))
1292 return status;
1293 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1294
1295 ec_ecdt->common.uid = -1;
1296 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1297
50526df6
LB
1298 status =
1299 acpi_evaluate_integer(handle, "_GPE", NULL,
1300 &ec_ecdt->common.gpe_bit);
45bea155
LY
1301 if (ACPI_FAILURE(status))
1302 return status;
02b28a33 1303 spin_lock_init(&ec_ecdt->poll.lock);
45bea155
LY
1304 ec_ecdt->common.global_lock = TRUE;
1305 ec_ecdt->common.handle = handle;
1306
50526df6
LB
1307 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1308 (u32) ec_ecdt->common.gpe_bit,
1309 (u32) ec_ecdt->common.command_addr.address,
1310 (u32) ec_ecdt->common.data_addr.address);
45bea155
LY
1311
1312 return AE_CTRL_TERMINATE;
1313}
1314
1315static acpi_status __init
02b28a33 1316acpi_fake_ecdt_intr_callback(acpi_handle handle,
50526df6 1317 u32 Level, void *context, void **retval)
45bea155 1318{
50526df6 1319 acpi_status status;
1da177e4 1320
02b28a33
LB
1321 init_MUTEX(&ec_ecdt->intr.sem);
1322 init_waitqueue_head(&ec_ecdt->intr.wait);
1da177e4 1323 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
50526df6 1324 acpi_ec_io_ports, ec_ecdt);
1da177e4
LT
1325 if (ACPI_FAILURE(status))
1326 return status;
45bea155 1327 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1da177e4 1328
45bea155
LY
1329 ec_ecdt->common.uid = -1;
1330 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1da177e4 1331
50526df6
LB
1332 status =
1333 acpi_evaluate_integer(handle, "_GPE", NULL,
1334 &ec_ecdt->common.gpe_bit);
1da177e4
LT
1335 if (ACPI_FAILURE(status))
1336 return status;
45bea155
LY
1337 ec_ecdt->common.global_lock = TRUE;
1338 ec_ecdt->common.handle = handle;
1da177e4 1339
50526df6
LB
1340 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1341 (u32) ec_ecdt->common.gpe_bit,
1342 (u32) ec_ecdt->common.command_addr.address,
1343 (u32) ec_ecdt->common.data_addr.address);
1da177e4
LT
1344
1345 return AE_CTRL_TERMINATE;
1346}
1347
1348/*
1349 * Some BIOS (such as some from Gateway laptops) access EC region very early
1350 * such as in BAT0._INI or EC._INI before an EC device is found and
1351 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1352 * required, but if EC regison is accessed early, it is required.
1353 * The routine tries to workaround the BIOS bug by pre-scan EC device
1354 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1355 * op region (since _REG isn't invoked yet). The assumption is true for
1356 * all systems found.
1357 */
50526df6 1358static int __init acpi_ec_fake_ecdt(void)
1da177e4 1359{
50526df6
LB
1360 acpi_status status;
1361 int ret = 0;
1da177e4
LT
1362
1363 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1364
45bea155 1365 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1da177e4
LT
1366 if (!ec_ecdt) {
1367 ret = -ENOMEM;
1368 goto error;
1369 }
45bea155 1370 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1da177e4 1371
50526df6
LB
1372 status = acpi_get_devices(ACPI_EC_HID,
1373 acpi_fake_ecdt_callback, NULL, NULL);
1da177e4
LT
1374 if (ACPI_FAILURE(status)) {
1375 kfree(ec_ecdt);
1376 ec_ecdt = NULL;
1377 ret = -ENODEV;
1378 goto error;
1379 }
1380 return 0;
50526df6 1381 error:
1da177e4
LT
1382 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1383 return ret;
1384}
1385
50526df6 1386static int __init acpi_ec_get_real_ecdt(void)
45bea155 1387{
02b28a33
LB
1388 if (acpi_ec_poll_mode)
1389 return acpi_ec_poll_get_real_ecdt();
45bea155 1390 else
02b28a33 1391 return acpi_ec_intr_get_real_ecdt();
45bea155
LY
1392}
1393
02b28a33 1394static int __init acpi_ec_poll_get_real_ecdt(void)
45bea155 1395{
50526df6
LB
1396 acpi_status status;
1397 struct acpi_table_ecdt *ecdt_ptr;
45bea155 1398
50526df6
LB
1399 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1400 (struct acpi_table_header **)
1401 &ecdt_ptr);
45bea155
LY
1402 if (ACPI_FAILURE(status))
1403 return -ENODEV;
1404
1405 printk(KERN_INFO PREFIX "Found ECDT\n");
1406
1407 /*
1408 * Generate a temporary ec context to use until the namespace is scanned
1409 */
1410 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1411 if (!ec_ecdt)
1412 return -ENOMEM;
1413 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1414
1415 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1416 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1417 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1418 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
02b28a33 1419 spin_lock_init(&ec_ecdt->poll.lock);
45bea155
LY
1420 /* use the GL just to be safe */
1421 ec_ecdt->common.global_lock = TRUE;
1422 ec_ecdt->common.uid = ecdt_ptr->uid;
1423
50526df6
LB
1424 status =
1425 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
45bea155
LY
1426 if (ACPI_FAILURE(status)) {
1427 goto error;
1428 }
1429
1430 return 0;
50526df6 1431 error:
45bea155
LY
1432 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1433 kfree(ec_ecdt);
1434 ec_ecdt = NULL;
1435
1436 return -ENODEV;
1437}
1438
02b28a33 1439static int __init acpi_ec_intr_get_real_ecdt(void)
1da177e4 1440{
50526df6
LB
1441 acpi_status status;
1442 struct acpi_table_ecdt *ecdt_ptr;
1da177e4 1443
451566f4 1444 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
50526df6
LB
1445 (struct acpi_table_header **)
1446 &ecdt_ptr);
1da177e4
LT
1447 if (ACPI_FAILURE(status))
1448 return -ENODEV;
1449
1450 printk(KERN_INFO PREFIX "Found ECDT\n");
1451
1452 /*
1453 * Generate a temporary ec context to use until the namespace is scanned
1454 */
45bea155 1455 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1da177e4
LT
1456 if (!ec_ecdt)
1457 return -ENOMEM;
45bea155
LY
1458 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1459
02b28a33
LB
1460 init_MUTEX(&ec_ecdt->intr.sem);
1461 init_waitqueue_head(&ec_ecdt->intr.wait);
45bea155
LY
1462 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1463 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1464 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1465 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1da177e4 1466 /* use the GL just to be safe */
45bea155
LY
1467 ec_ecdt->common.global_lock = TRUE;
1468 ec_ecdt->common.uid = ecdt_ptr->uid;
1da177e4 1469
50526df6
LB
1470 status =
1471 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1da177e4
LT
1472 if (ACPI_FAILURE(status)) {
1473 goto error;
1474 }
1475
1476 return 0;
50526df6 1477 error:
1da177e4
LT
1478 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1479 kfree(ec_ecdt);
1480 ec_ecdt = NULL;
1481
1482 return -ENODEV;
1483}
1484
1485static int __initdata acpi_fake_ecdt_enabled;
50526df6 1486int __init acpi_ec_ecdt_probe(void)
1da177e4 1487{
50526df6
LB
1488 acpi_status status;
1489 int ret;
1da177e4
LT
1490
1491 ret = acpi_ec_get_real_ecdt();
1492 /* Try to make a fake ECDT */
1493 if (ret && acpi_fake_ecdt_enabled) {
1494 ret = acpi_ec_fake_ecdt();
1495 }
1496
1497 if (ret)
1498 return 0;
1499
1500 /*
1501 * Install GPE handler
1502 */
45bea155 1503 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
50526df6
LB
1504 ACPI_GPE_EDGE_TRIGGERED,
1505 &acpi_ec_gpe_handler, ec_ecdt);
1da177e4
LT
1506 if (ACPI_FAILURE(status)) {
1507 goto error;
1508 }
50526df6
LB
1509 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1510 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1511
1512 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1513 ACPI_ADR_SPACE_EC,
1514 &acpi_ec_space_handler,
1515 &acpi_ec_space_setup,
1516 ec_ecdt);
1da177e4 1517 if (ACPI_FAILURE(status)) {
45bea155 1518 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
50526df6 1519 &acpi_ec_gpe_handler);
1da177e4
LT
1520 goto error;
1521 }
1522
1523 return 0;
1524
50526df6 1525 error:
1da177e4
LT
1526 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1527 kfree(ec_ecdt);
1528 ec_ecdt = NULL;
1529
1530 return -ENODEV;
1531}
1532
50526df6 1533static int __init acpi_ec_init(void)
1da177e4 1534{
50526df6 1535 int result = 0;
1da177e4
LT
1536
1537 ACPI_FUNCTION_TRACE("acpi_ec_init");
1538
1539 if (acpi_disabled)
1540 return_VALUE(0);
1541
1542 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1543 if (!acpi_ec_dir)
1544 return_VALUE(-ENODEV);
1545
1546 /* Now register the driver for the EC */
1547 result = acpi_bus_register_driver(&acpi_ec_driver);
1548 if (result < 0) {
1549 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1550 return_VALUE(-ENODEV);
1551 }
1552
1553 return_VALUE(result);
1554}
1555
1556subsys_initcall(acpi_ec_init);
1557
1558/* EC driver currently not unloadable */
1559#if 0
50526df6 1560static void __exit acpi_ec_exit(void)
1da177e4
LT
1561{
1562 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1563
1564 acpi_bus_unregister_driver(&acpi_ec_driver);
1565
1566 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1567
1568 return_VOID;
1569}
50526df6 1570#endif /* 0 */
1da177e4
LT
1571
1572static int __init acpi_fake_ecdt_setup(char *str)
1573{
1574 acpi_fake_ecdt_enabled = 1;
1575 return 0;
1576}
7b15f5e7 1577
1da177e4 1578__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
02b28a33 1579static int __init acpi_ec_set_intr_mode(char *str)
45bea155 1580{
02b28a33 1581 int intr;
7b15f5e7 1582
02b28a33 1583 if (!get_option(&str, &intr))
7b15f5e7
LY
1584 return 0;
1585
02b28a33
LB
1586 if (intr) {
1587 acpi_ec_poll_mode = EC_INTR;
1588 acpi_ec_driver.ops.add = acpi_ec_intr_add;
7b15f5e7 1589 } else {
02b28a33
LB
1590 acpi_ec_poll_mode = EC_POLL;
1591 acpi_ec_driver.ops.add = acpi_ec_poll_add;
7b15f5e7 1592 }
02b28a33 1593 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
45bea155
LY
1594 return 0;
1595}
50526df6 1596
53f11d4f 1597__setup("ec_intr=", acpi_ec_set_intr_mode);