blob: 31067a0a36715de5ce6c6673344f7e090d8864f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
41ACPI_MODULE_NAME ("acpi_ec")
42
43#define ACPI_EC_COMPONENT 0x00100000
44#define ACPI_EC_CLASS "embedded_controller"
45#define ACPI_EC_HID "PNP0C09"
46#define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
47#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
49
50
51#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050053#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
55
56#define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
57#define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
58
Dmitry Torokhov451566f2005-03-19 01:10:05 -050059#define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
61
Luming Yu45bea152005-07-23 04:08:00 -040062#define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
63#define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define ACPI_EC_COMMAND_READ 0x80
66#define ACPI_EC_COMMAND_WRITE 0x81
Dmitry Torokhov451566f2005-03-19 01:10:05 -050067#define ACPI_EC_BURST_ENABLE 0x82
68#define ACPI_EC_BURST_DISABLE 0x83
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define ACPI_EC_COMMAND_QUERY 0x84
70
Luming Yu45bea152005-07-23 04:08:00 -040071#define EC_POLLING 0xFF
72#define EC_BURST 0x00
73
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int acpi_ec_remove (struct acpi_device *device, int type);
76static int acpi_ec_start (struct acpi_device *device);
77static int acpi_ec_stop (struct acpi_device *device, int type);
Luming Yu45bea152005-07-23 04:08:00 -040078static int acpi_ec_burst_add ( struct acpi_device *device);
Luming Yu7b15f5e2005-08-03 17:38:04 -040079static int acpi_ec_polling_add ( struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct acpi_driver acpi_ec_driver = {
82 .name = ACPI_EC_DRIVER_NAME,
83 .class = ACPI_EC_CLASS,
84 .ids = ACPI_EC_HID,
85 .ops = {
Luming Yu7b15f5e2005-08-03 17:38:04 -040086 .add = acpi_ec_polling_add,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
89 .stop = acpi_ec_stop,
90 },
91};
Luming Yu45bea152005-07-23 04:08:00 -040092union acpi_ec {
93 struct {
94 u32 mode;
95 acpi_handle handle;
96 unsigned long uid;
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
102 } common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Luming Yu45bea152005-07-23 04:08:00 -0400104 struct {
105 u32 mode;
106 acpi_handle handle;
107 unsigned long uid;
108 unsigned long gpe_bit;
109 struct acpi_generic_address status_addr;
110 struct acpi_generic_address command_addr;
111 struct acpi_generic_address data_addr;
112 unsigned long global_lock;
113 unsigned int expect_event;
114 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
115 atomic_t pending_gpe;
116 struct semaphore sem;
117 wait_queue_head_t wait;
118 }burst;
119
120 struct {
121 u32 mode;
122 acpi_handle handle;
123 unsigned long uid;
124 unsigned long gpe_bit;
125 struct acpi_generic_address status_addr;
126 struct acpi_generic_address command_addr;
127 struct acpi_generic_address data_addr;
128 unsigned long global_lock;
129 spinlock_t lock;
130 }polling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Luming Yu45bea152005-07-23 04:08:00 -0400133static int acpi_ec_polling_wait ( union acpi_ec *ec, u8 event);
134static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
135static int acpi_ec_polling_read ( union acpi_ec *ec, u8 address, u32 *data);
136static int acpi_ec_burst_read( union acpi_ec *ec, u8 address, u32 *data);
137static int acpi_ec_polling_write ( union acpi_ec *ec, u8 address, u8 data);
138static int acpi_ec_burst_write ( union acpi_ec *ec, u8 address, u8 data);
139static int acpi_ec_polling_query ( union acpi_ec *ec, u32 *data);
140static int acpi_ec_burst_query ( union acpi_ec *ec, u32 *data);
141static void acpi_ec_gpe_polling_query ( void *ec_cxt);
142static void acpi_ec_gpe_burst_query ( void *ec_cxt);
143static u32 acpi_ec_gpe_polling_handler ( void *data);
144static u32 acpi_ec_gpe_burst_handler ( void *data);
145static acpi_status __init
146acpi_fake_ecdt_polling_callback (
147 acpi_handle handle,
148 u32 Level,
149 void *context,
150 void **retval);
151
152static acpi_status __init
153acpi_fake_ecdt_burst_callback (
154 acpi_handle handle,
155 u32 Level,
156 void *context,
157 void **retval);
158
159static int __init
160acpi_ec_polling_get_real_ecdt(void);
161static int __init
162acpi_ec_burst_get_real_ecdt(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Luming Yu45bea152005-07-23 04:08:00 -0400164static union acpi_ec *ec_ecdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/* External interfaces use first EC only, so remember */
167static struct acpi_device *first_ec;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400168static int acpi_ec_polling_mode = EC_POLLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/* --------------------------------------------------------------------------
171 Transaction Management
172 -------------------------------------------------------------------------- */
173
Luming Yu45bea152005-07-23 04:08:00 -0400174static inline u32 acpi_ec_read_status(union acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500176 u32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Luming Yu45bea152005-07-23 04:08:00 -0400178 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500179 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Luming Yu45bea152005-07-23 04:08:00 -0400182static int
183acpi_ec_wait (
184 union acpi_ec *ec,
185 u8 event)
186{
187 if (acpi_ec_polling_mode)
188 return acpi_ec_polling_wait (ec, event);
189 else
190 return acpi_ec_burst_wait (ec, event);
191}
192
193static int
194acpi_ec_polling_wait (
195 union acpi_ec *ec,
196 u8 event)
197{
198 u32 acpi_ec_status = 0;
199 u32 i = ACPI_EC_UDELAY_COUNT;
200
201 if (!ec)
202 return -EINVAL;
203
204 /* Poll the EC status register waiting for the event to occur. */
205 switch (event) {
206 case ACPI_EC_EVENT_OBF:
207 do {
208 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
209 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
210 return 0;
211 udelay(ACPI_EC_UDELAY);
212 } while (--i>0);
213 break;
214 case ACPI_EC_EVENT_IBE:
215 do {
216 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
217 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
218 return 0;
219 udelay(ACPI_EC_UDELAY);
220 } while (--i>0);
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 return -ETIME;
227}
228static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500229{
230 int result = 0;
231
232 ACPI_FUNCTION_TRACE("acpi_ec_wait");
233
Luming Yu45bea152005-07-23 04:08:00 -0400234 ec->burst.expect_event = event;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500235 smp_mb();
236
Luming Yu716e0842005-08-10 01:40:00 -0400237 switch (event) {
238 case ACPI_EC_EVENT_OBF:
239 if (acpi_ec_read_status(ec) & event) {
240 ec->burst.expect_event = 0;
241 return_VALUE(0);
242 }
243 break;
244
245 case ACPI_EC_EVENT_IBE:
246 if (~acpi_ec_read_status(ec) & event) {
247 ec->burst.expect_event = 0;
248 return_VALUE(0);
249 }
250 break;
251 }
252
253 result = wait_event_timeout(ec->burst.wait,
Luming Yu45bea152005-07-23 04:08:00 -0400254 !ec->burst.expect_event,
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500255 msecs_to_jiffies(ACPI_EC_DELAY));
256
Luming Yu45bea152005-07-23 04:08:00 -0400257 ec->burst.expect_event = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500258 smp_mb();
259
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500260 /*
261 * Verify that the event in question has actually happened by
262 * querying EC status. Do the check even if operation timed-out
263 * to make sure that we did not miss interrupt.
264 */
265 switch (event) {
266 case ACPI_EC_EVENT_OBF:
267 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
268 return_VALUE(0);
269 break;
270
271 case ACPI_EC_EVENT_IBE:
272 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
273 return_VALUE(0);
274 break;
275 }
276
277 return_VALUE(-ETIME);
278}
279
280
281
282static int
283acpi_ec_enter_burst_mode (
Luming Yu45bea152005-07-23 04:08:00 -0400284 union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500285{
286 u32 tmp = 0;
287 int status = 0;
288
289 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
290
291 status = acpi_ec_read_status(ec);
292 if (status != -EINVAL &&
293 !(status & ACPI_EC_FLAG_BURST)){
Luming Yu716e0842005-08-10 01:40:00 -0400294 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
295 if(status)
296 goto end;
Luming Yu45bea152005-07-23 04:08:00 -0400297 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500298 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
Luming Yu716e0842005-08-10 01:40:00 -0400299 if (status)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500300 return_VALUE(-EINVAL);
Luming Yu45bea152005-07-23 04:08:00 -0400301 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500302 if(tmp != 0x90 ) {/* Burst ACK byte*/
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500303 return_VALUE(-EINVAL);
304 }
Luming Yu668d74c2005-07-23 00:26:33 -0400305 }
306
Luming Yu45bea152005-07-23 04:08:00 -0400307 atomic_set(&ec->burst.leaving_burst , 0);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500308 return_VALUE(0);
Luming Yu716e0842005-08-10 01:40:00 -0400309end:
310 printk("Error in acpi_ec_wait\n");
311 return_VALUE(-1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500312}
313
314static int
315acpi_ec_leave_burst_mode (
Luming Yu45bea152005-07-23 04:08:00 -0400316 union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500317{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500318
319 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
320
Luming Yu716e0842005-08-10 01:40:00 -0400321 atomic_set(&ec->burst.leaving_burst, 1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500322 return_VALUE(0);
323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325static int
326acpi_ec_read (
Luming Yu45bea152005-07-23 04:08:00 -0400327 union acpi_ec *ec,
328 u8 address,
329 u32 *data)
330{
331 if (acpi_ec_polling_mode)
332 return acpi_ec_polling_read(ec, address, data);
333 else
334 return acpi_ec_burst_read(ec, address, data);
335}
336static int
337acpi_ec_write (
338 union acpi_ec *ec,
339 u8 address,
340 u8 data)
341{
342 if (acpi_ec_polling_mode)
343 return acpi_ec_polling_write(ec, address, data);
344 else
345 return acpi_ec_burst_write(ec, address, data);
346}
347static int
348acpi_ec_polling_read (
349 union acpi_ec *ec,
350 u8 address,
351 u32 *data)
352{
353 acpi_status status = AE_OK;
354 int result = 0;
355 unsigned long flags = 0;
356 u32 glk = 0;
357
358 ACPI_FUNCTION_TRACE("acpi_ec_read");
359
360 if (!ec || !data)
361 return_VALUE(-EINVAL);
362
363 *data = 0;
364
365 if (ec->common.global_lock) {
366 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
367 if (ACPI_FAILURE(status))
368 return_VALUE(-ENODEV);
369 }
370
371 spin_lock_irqsave(&ec->polling.lock, flags);
372
373 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
374 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
375 if (result)
376 goto end;
377
378 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
379 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
380 if (result)
381 goto end;
382
383 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
384
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
386 *data, address));
387
388end:
389 spin_unlock_irqrestore(&ec->polling.lock, flags);
390
391 if (ec->common.global_lock)
392 acpi_release_global_lock(glk);
393
394 return_VALUE(result);
395}
396
397
398static int
399acpi_ec_polling_write (
400 union acpi_ec *ec,
401 u8 address,
402 u8 data)
403{
404 int result = 0;
405 acpi_status status = AE_OK;
406 unsigned long flags = 0;
407 u32 glk = 0;
408
409 ACPI_FUNCTION_TRACE("acpi_ec_write");
410
411 if (!ec)
412 return_VALUE(-EINVAL);
413
414 if (ec->common.global_lock) {
415 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
416 if (ACPI_FAILURE(status))
417 return_VALUE(-ENODEV);
418 }
419
420 spin_lock_irqsave(&ec->polling.lock, flags);
421
422 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
423 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
424 if (result)
425 goto end;
426
427 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
428 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
429 if (result)
430 goto end;
431
432 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
433 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
434 if (result)
435 goto end;
436
437 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
438 data, address));
439
440end:
441 spin_unlock_irqrestore(&ec->polling.lock, flags);
442
443 if (ec->common.global_lock)
444 acpi_release_global_lock(glk);
445
446 return_VALUE(result);
447}
448
449static int
450acpi_ec_burst_read (
451 union acpi_ec *ec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 u8 address,
453 u32 *data)
454{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500455 int status = 0;
456 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 ACPI_FUNCTION_TRACE("acpi_ec_read");
459
460 if (!ec || !data)
461 return_VALUE(-EINVAL);
462
463 *data = 0;
464
Luming Yu45bea152005-07-23 04:08:00 -0400465 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
467 if (ACPI_FAILURE(status))
468 return_VALUE(-ENODEV);
469 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500470
471 WARN_ON(in_interrupt());
Luming Yu45bea152005-07-23 04:08:00 -0400472 down(&ec->burst.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500473
Luming Yu716e0842005-08-10 01:40:00 -0400474 acpi_ec_enter_burst_mode(ec);
475 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
476 if (status) {
477 printk("read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500478 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400479 }
Luming Yu45bea152005-07-23 04:08:00 -0400480 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500481 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500482 if (status) {
Luming Yu716e0842005-08-10 01:40:00 -0400483 printk("read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Luming Yu45bea152005-07-23 04:08:00 -0400486 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500487 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
488 if (status){
Luming Yu716e0842005-08-10 01:40:00 -0400489 printk("read EC, OB not full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500491 }
Luming Yu45bea152005-07-23 04:08:00 -0400492 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
494 *data, address));
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496end:
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500497 acpi_ec_leave_burst_mode(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400498 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Luming Yu45bea152005-07-23 04:08:00 -0400500 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 acpi_release_global_lock(glk);
502
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500503 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506
507static int
Luming Yu45bea152005-07-23 04:08:00 -0400508acpi_ec_burst_write (
509 union acpi_ec *ec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 u8 address,
511 u8 data)
512{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500513 int status = 0;
514 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 ACPI_FUNCTION_TRACE("acpi_ec_write");
517
518 if (!ec)
519 return_VALUE(-EINVAL);
Luming Yu716e0842005-08-10 01:40:00 -0400520
Luming Yu45bea152005-07-23 04:08:00 -0400521 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
523 if (ACPI_FAILURE(status))
524 return_VALUE(-ENODEV);
525 }
526
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500527 WARN_ON(in_interrupt());
Luming Yu45bea152005-07-23 04:08:00 -0400528 down(&ec->burst.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500529
Luming Yu716e0842005-08-10 01:40:00 -0400530 acpi_ec_enter_burst_mode(ec);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500531
Luming Yu716e0842005-08-10 01:40:00 -0400532 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
533 if ( status) {
534 printk("write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500535 }
Luming Yu45bea152005-07-23 04:08:00 -0400536 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500537 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Luming Yu716e0842005-08-10 01:40:00 -0400538 if (status) {
539 printk ("write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Luming Yu45bea152005-07-23 04:08:00 -0400542 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500543 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
544 if (status){
Luming Yu716e0842005-08-10 01:40:00 -0400545 printk("write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Luming Yu45bea152005-07-23 04:08:00 -0400548 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
551 data, address));
552
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500553 acpi_ec_leave_burst_mode(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400554 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Luming Yu45bea152005-07-23 04:08:00 -0400556 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 acpi_release_global_lock(glk);
558
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500559 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Externally callable EC access functions. For now, assume 1 EC only
564 */
565int
566ec_read(u8 addr, u8 *val)
567{
Luming Yu45bea152005-07-23 04:08:00 -0400568 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int err;
570 u32 temp_data;
571
572 if (!first_ec)
573 return -ENODEV;
574
575 ec = acpi_driver_data(first_ec);
576
577 err = acpi_ec_read(ec, addr, &temp_data);
578
579 if (!err) {
580 *val = temp_data;
581 return 0;
582 }
583 else
584 return err;
585}
586EXPORT_SYMBOL(ec_read);
587
588int
589ec_write(u8 addr, u8 val)
590{
Luming Yu45bea152005-07-23 04:08:00 -0400591 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int err;
593
594 if (!first_ec)
595 return -ENODEV;
596
597 ec = acpi_driver_data(first_ec);
598
599 err = acpi_ec_write(ec, addr, val);
600
601 return err;
602}
603EXPORT_SYMBOL(ec_write);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605static int
606acpi_ec_query (
Luming Yu45bea152005-07-23 04:08:00 -0400607 union acpi_ec *ec,
608 u32 *data)
609{
610 if (acpi_ec_polling_mode)
611 return acpi_ec_polling_query(ec, data);
612 else
613 return acpi_ec_burst_query(ec, data);
614}
615static int
616acpi_ec_polling_query (
617 union acpi_ec *ec,
618 u32 *data)
619{
620 int result = 0;
621 acpi_status status = AE_OK;
622 unsigned long flags = 0;
623 u32 glk = 0;
624
625 ACPI_FUNCTION_TRACE("acpi_ec_query");
626
627 if (!ec || !data)
628 return_VALUE(-EINVAL);
629
630 *data = 0;
631
632 if (ec->common.global_lock) {
633 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
634 if (ACPI_FAILURE(status))
635 return_VALUE(-ENODEV);
636 }
637
638 /*
639 * Query the EC to find out which _Qxx method we need to evaluate.
640 * Note that successful completion of the query causes the ACPI_EC_SCI
641 * bit to be cleared (and thus clearing the interrupt source).
642 */
643 spin_lock_irqsave(&ec->polling.lock, flags);
644
645 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
646 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
647 if (result)
648 goto end;
649
650 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
651 if (!*data)
652 result = -ENODATA;
653
654end:
655 spin_unlock_irqrestore(&ec->polling.lock, flags);
656
657 if (ec->common.global_lock)
658 acpi_release_global_lock(glk);
659
660 return_VALUE(result);
661}
662static int
663acpi_ec_burst_query (
664 union acpi_ec *ec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 u32 *data)
666{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500667 int status = 0;
668 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 ACPI_FUNCTION_TRACE("acpi_ec_query");
671
672 if (!ec || !data)
673 return_VALUE(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *data = 0;
675
Luming Yu45bea152005-07-23 04:08:00 -0400676 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
678 if (ACPI_FAILURE(status))
679 return_VALUE(-ENODEV);
680 }
681
Luming Yu45bea152005-07-23 04:08:00 -0400682 down(&ec->burst.sem);
Luming Yu716e0842005-08-10 01:40:00 -0400683
684 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
685 if (status) {
686 printk("query EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500687 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * Query the EC to find out which _Qxx method we need to evaluate.
691 * Note that successful completion of the query causes the ACPI_EC_SCI
692 * bit to be cleared (and thus clearing the interrupt source).
693 */
Luming Yu45bea152005-07-23 04:08:00 -0400694 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500695 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
696 if (status){
Luming Yu716e0842005-08-10 01:40:00 -0400697 printk("query EC, OB not full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500699 }
700
Luming Yu45bea152005-07-23 04:08:00 -0400701 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!*data)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500703 status = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705end:
Luming Yu45bea152005-07-23 04:08:00 -0400706 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Luming Yu45bea152005-07-23 04:08:00 -0400708 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 acpi_release_global_lock(glk);
710
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500711 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714
715/* --------------------------------------------------------------------------
716 Event Management
717 -------------------------------------------------------------------------- */
718
Luming Yu45bea152005-07-23 04:08:00 -0400719union acpi_ec_query_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 acpi_handle handle;
721 u8 data;
722};
723
724static void
725acpi_ec_gpe_query (
726 void *ec_cxt)
727{
Luming Yu45bea152005-07-23 04:08:00 -0400728 if (acpi_ec_polling_mode)
729 acpi_ec_gpe_polling_query(ec_cxt);
730 else
731 acpi_ec_gpe_burst_query(ec_cxt);
732}
733
734static void
735acpi_ec_gpe_polling_query (
736 void *ec_cxt)
737{
738 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
739 u32 value = 0;
740 unsigned long flags = 0;
741 static char object_name[5] = {'_','Q','0','0','\0'};
742 const char hex[] = {'0','1','2','3','4','5','6','7',
743 '8','9','A','B','C','D','E','F'};
744
745 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
746
747 if (!ec_cxt)
748 goto end;
749
750 spin_lock_irqsave(&ec->polling.lock, flags);
751 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
752 spin_unlock_irqrestore(&ec->polling.lock, flags);
753
754 /* TBD: Implement asynch events!
755 * NOTE: All we care about are EC-SCI's. Other EC events are
756 * handled via polling (yuck!). This is because some systems
757 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
758 * a purely interrupt-driven approach (grumble, grumble).
759 */
760 if (!(value & ACPI_EC_FLAG_SCI))
761 goto end;
762
763 if (acpi_ec_query(ec, &value))
764 goto end;
765
766 object_name[2] = hex[((value >> 4) & 0x0F)];
767 object_name[3] = hex[(value & 0x0F)];
768
769 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
770
771 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
772
773end:
774 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
775}
776static void
777acpi_ec_gpe_burst_query (
778 void *ec_cxt)
779{
780 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500781 u32 value;
782 int result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 static char object_name[5] = {'_','Q','0','0','\0'};
784 const char hex[] = {'0','1','2','3','4','5','6','7',
785 '8','9','A','B','C','D','E','F'};
786
787 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
788
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500789 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
790 result = acpi_ec_query(ec, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500792 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto end;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 object_name[2] = hex[((value >> 4) & 0x0F)];
796 object_name[3] = hex[(value & 0x0F)];
797
798 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
799
Luming Yu45bea152005-07-23 04:08:00 -0400800 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500801end:
Luming Yu45bea152005-07-23 04:08:00 -0400802 atomic_dec(&ec->burst.pending_gpe);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500803 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806static u32
807acpi_ec_gpe_handler (
808 void *data)
809{
Luming Yu45bea152005-07-23 04:08:00 -0400810 if (acpi_ec_polling_mode)
811 return acpi_ec_gpe_polling_handler(data);
812 else
813 return acpi_ec_gpe_burst_handler(data);
814}
815static u32
816acpi_ec_gpe_polling_handler (
817 void *data)
818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 acpi_status status = AE_OK;
Luming Yu45bea152005-07-23 04:08:00 -0400820 union acpi_ec *ec = (union acpi_ec *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!ec)
823 return ACPI_INTERRUPT_NOT_HANDLED;
824
Luming Yu45bea152005-07-23 04:08:00 -0400825 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
826
827 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
828 acpi_ec_gpe_query, ec);
829
830 if (status == AE_OK)
831 return ACPI_INTERRUPT_HANDLED;
832 else
833 return ACPI_INTERRUPT_NOT_HANDLED;
834}
835static u32
836acpi_ec_gpe_burst_handler (
837 void *data)
838{
839 acpi_status status = AE_OK;
840 u32 value;
841 union acpi_ec *ec = (union acpi_ec *) data;
842
843 if (!ec)
844 return ACPI_INTERRUPT_NOT_HANDLED;
845
Luming Yu716e0842005-08-10 01:40:00 -0400846 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500847 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Luming Yu716e0842005-08-10 01:40:00 -0400849 switch ( ec->burst.expect_event) {
850 case ACPI_EC_EVENT_OBF:
851 if (!(value & ACPI_EC_FLAG_OBF))
852 break;
853 case ACPI_EC_EVENT_IBE:
854 if ((value & ACPI_EC_FLAG_IBF))
855 break;
856 ec->burst.expect_event = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400857 wake_up(&ec->burst.wait);
Luming Yu716e0842005-08-10 01:40:00 -0400858 return ACPI_INTERRUPT_HANDLED;
859 default:
860 break;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500861 }
862
863 if (value & ACPI_EC_FLAG_SCI){
Luming Yu45bea152005-07-23 04:08:00 -0400864 atomic_add(1, &ec->burst.pending_gpe) ;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500865 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
866 acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400867 return status == AE_OK ?
868 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500869 }
Luming Yu45bea152005-07-23 04:08:00 -0400870 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500871 return status == AE_OK ?
872 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/* --------------------------------------------------------------------------
876 Address Space Management
877 -------------------------------------------------------------------------- */
878
879static acpi_status
880acpi_ec_space_setup (
881 acpi_handle region_handle,
882 u32 function,
883 void *handler_context,
884 void **return_context)
885{
886 /*
887 * The EC object is in the handler context and is needed
888 * when calling the acpi_ec_space_handler.
889 */
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500890 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
891 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 return AE_OK;
894}
895
896
897static acpi_status
898acpi_ec_space_handler (
899 u32 function,
900 acpi_physical_address address,
901 u32 bit_width,
902 acpi_integer *value,
903 void *handler_context,
904 void *region_context)
905{
906 int result = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400907 union acpi_ec *ec = NULL;
Luming Yufa9cd542005-03-19 01:54:47 -0500908 u64 temp = *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 acpi_integer f_v = 0;
910 int i = 0;
911
912 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
913
914 if ((address > 0xFF) || !value || !handler_context)
915 return_VALUE(AE_BAD_PARAMETER);
916
Luming Yufa9cd542005-03-19 01:54:47 -0500917 if (bit_width != 8 && acpi_strict) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
Luming Yufa9cd542005-03-19 01:54:47 -0500919 return_VALUE(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Luming Yu45bea152005-07-23 04:08:00 -0400922 ec = (union acpi_ec *) handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924next_byte:
925 switch (function) {
926 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500927 temp = 0;
928 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500931 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 default:
934 result = -EINVAL;
935 goto out;
936 break;
937 }
938
939 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500940 if (bit_width) {
941 if (function == ACPI_READ)
942 f_v |= temp << 8 * i;
943 if (function == ACPI_WRITE)
944 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500946 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto next_byte;
948 }
949
Luming Yufa9cd542005-03-19 01:54:47 -0500950 if (function == ACPI_READ) {
951 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *value = f_v;
953 }
954
955
956out:
957 switch (result) {
958 case -EINVAL:
959 return_VALUE(AE_BAD_PARAMETER);
960 break;
961 case -ENODEV:
962 return_VALUE(AE_NOT_FOUND);
963 break;
964 case -ETIME:
965 return_VALUE(AE_TIME);
966 break;
967 default:
968 return_VALUE(AE_OK);
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972
973/* --------------------------------------------------------------------------
974 FS Interface (/proc)
975 -------------------------------------------------------------------------- */
976
977static struct proc_dir_entry *acpi_ec_dir;
978
979
980static int
981acpi_ec_read_info (struct seq_file *seq, void *offset)
982{
Luming Yu45bea152005-07-23 04:08:00 -0400983 union acpi_ec *ec = (union acpi_ec *) seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
986
987 if (!ec)
988 goto end;
989
990 seq_printf(seq, "gpe bit: 0x%02x\n",
Luming Yu45bea152005-07-23 04:08:00 -0400991 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Luming Yu45bea152005-07-23 04:08:00 -0400993 (u32) ec->common.status_addr.address, (u32) ec->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 seq_printf(seq, "use global lock: %s\n",
Luming Yu45bea152005-07-23 04:08:00 -0400995 ec->common.global_lock?"yes":"no");
996 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998end:
999 return_VALUE(0);
1000}
1001
1002static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
1003{
1004 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
1005}
1006
1007static struct file_operations acpi_ec_info_ops = {
1008 .open = acpi_ec_info_open_fs,
1009 .read = seq_read,
1010 .llseek = seq_lseek,
1011 .release = single_release,
1012 .owner = THIS_MODULE,
1013};
1014
1015static int
1016acpi_ec_add_fs (
1017 struct acpi_device *device)
1018{
Luming Yu45bea152005-07-23 04:08:00 -04001019 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
1022
1023 if (!acpi_device_dir(device)) {
1024 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1025 acpi_ec_dir);
1026 if (!acpi_device_dir(device))
1027 return_VALUE(-ENODEV);
1028 }
1029
1030 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
1031 acpi_device_dir(device));
1032 if (!entry)
1033 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1034 "Unable to create '%s' fs entry\n",
1035 ACPI_EC_FILE_INFO));
1036 else {
1037 entry->proc_fops = &acpi_ec_info_ops;
1038 entry->data = acpi_driver_data(device);
1039 entry->owner = THIS_MODULE;
1040 }
1041
1042 return_VALUE(0);
1043}
1044
1045
1046static int
1047acpi_ec_remove_fs (
1048 struct acpi_device *device)
1049{
1050 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
1051
1052 if (acpi_device_dir(device)) {
1053 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
1054 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
1055 acpi_device_dir(device) = NULL;
1056 }
1057
1058 return_VALUE(0);
1059}
1060
1061
1062/* --------------------------------------------------------------------------
1063 Driver Interface
1064 -------------------------------------------------------------------------- */
1065
Luming Yu45bea152005-07-23 04:08:00 -04001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static int
Luming Yu45bea152005-07-23 04:08:00 -04001068acpi_ec_polling_add (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct acpi_device *device)
1070{
Luming Yu45bea152005-07-23 04:08:00 -04001071 int result = 0;
1072 acpi_status status = AE_OK;
1073 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned long uid;
1075
1076 ACPI_FUNCTION_TRACE("acpi_ec_add");
1077
1078 if (!device)
1079 return_VALUE(-EINVAL);
1080
Luming Yu45bea152005-07-23 04:08:00 -04001081 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!ec)
1083 return_VALUE(-ENOMEM);
Luming Yu45bea152005-07-23 04:08:00 -04001084 memset(ec, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Luming Yu45bea152005-07-23 04:08:00 -04001086 ec->common.handle = device->handle;
1087 ec->common.uid = -1;
1088 spin_lock_init(&ec->polling.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1090 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1091 acpi_driver_data(device) = ec;
1092
1093 /* Use the global lock for all EC transactions? */
Luming Yu45bea152005-07-23 04:08:00 -04001094 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* If our UID matches the UID for the ECDT-enumerated EC,
1097 we now have the *real* EC info, so kill the makeshift one.*/
Luming Yu45bea152005-07-23 04:08:00 -04001098 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1099 if (ec_ecdt && ec_ecdt->common.uid == uid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1101 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
Luming Yu45bea152005-07-23 04:08:00 -04001102
1103 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 kfree(ec_ecdt);
1106 }
1107
1108 /* Get GPE bit assignment (EC events). */
1109 /* TODO: Add support for _GPE returning a package */
Luming Yu45bea152005-07-23 04:08:00 -04001110 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (ACPI_FAILURE(status)) {
1112 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1113 "Error obtaining GPE bit assignment\n"));
1114 result = -ENODEV;
1115 goto end;
1116 }
1117
1118 result = acpi_ec_add_fs(device);
1119 if (result)
1120 goto end;
1121
1122 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1123 acpi_device_name(device), acpi_device_bid(device),
Luming Yu45bea152005-07-23 04:08:00 -04001124 (u32) ec->common.gpe_bit);
1125
1126 if (!first_ec)
1127 first_ec = device;
1128
1129end:
1130 if (result)
1131 kfree(ec);
1132
1133 return_VALUE(result);
1134}
1135static int
1136acpi_ec_burst_add (
1137 struct acpi_device *device)
1138{
1139 int result = 0;
1140 acpi_status status = AE_OK;
1141 union acpi_ec *ec = NULL;
1142 unsigned long uid;
1143
1144 ACPI_FUNCTION_TRACE("acpi_ec_add");
1145
1146 if (!device)
1147 return_VALUE(-EINVAL);
1148
1149 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1150 if (!ec)
1151 return_VALUE(-ENOMEM);
1152 memset(ec, 0, sizeof(union acpi_ec));
1153
1154 ec->common.handle = device->handle;
1155 ec->common.uid = -1;
1156 atomic_set(&ec->burst.pending_gpe, 0);
1157 atomic_set(&ec->burst.leaving_burst , 1);
1158 init_MUTEX(&ec->burst.sem);
1159 init_waitqueue_head(&ec->burst.wait);
1160 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1161 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1162 acpi_driver_data(device) = ec;
1163
1164 /* Use the global lock for all EC transactions? */
1165 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1166
1167 /* If our UID matches the UID for the ECDT-enumerated EC,
1168 we now have the *real* EC info, so kill the makeshift one.*/
1169 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1170 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1171 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1172 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1173
1174 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1175
1176 kfree(ec_ecdt);
1177 }
1178
1179 /* Get GPE bit assignment (EC events). */
1180 /* TODO: Add support for _GPE returning a package */
1181 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1182 if (ACPI_FAILURE(status)) {
1183 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1184 "Error obtaining GPE bit assignment\n"));
1185 result = -ENODEV;
1186 goto end;
1187 }
1188
1189 result = acpi_ec_add_fs(device);
1190 if (result)
1191 goto end;
1192
Luming Yu716e0842005-08-10 01:40:00 -04001193 printk("burst-mode-ec-10-Aug\n");
Luming Yu45bea152005-07-23 04:08:00 -04001194 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1195 acpi_device_name(device), acpi_device_bid(device),
1196 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (!first_ec)
1199 first_ec = device;
1200
1201end:
1202 if (result)
1203 kfree(ec);
1204
1205 return_VALUE(result);
1206}
1207
1208
1209static int
1210acpi_ec_remove (
1211 struct acpi_device *device,
1212 int type)
1213{
Luming Yu45bea152005-07-23 04:08:00 -04001214 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1217
1218 if (!device)
1219 return_VALUE(-EINVAL);
1220
1221 ec = acpi_driver_data(device);
1222
1223 acpi_ec_remove_fs(device);
1224
1225 kfree(ec);
1226
1227 return_VALUE(0);
1228}
1229
1230
1231static acpi_status
1232acpi_ec_io_ports (
1233 struct acpi_resource *resource,
1234 void *context)
1235{
Luming Yu45bea152005-07-23 04:08:00 -04001236 union acpi_ec *ec = (union acpi_ec *) context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct acpi_generic_address *addr;
1238
1239 if (resource->id != ACPI_RSTYPE_IO) {
1240 return AE_OK;
1241 }
1242
1243 /*
1244 * The first address region returned is the data port, and
1245 * the second address region returned is the status/command
1246 * port.
1247 */
Luming Yu45bea152005-07-23 04:08:00 -04001248 if (ec->common.data_addr.register_bit_width == 0) {
1249 addr = &ec->common.data_addr;
1250 } else if (ec->common.command_addr.register_bit_width == 0) {
1251 addr = &ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else {
1253 return AE_CTRL_TERMINATE;
1254 }
1255
1256 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1257 addr->register_bit_width = 8;
1258 addr->register_bit_offset = 0;
1259 addr->address = resource->data.io.min_base_address;
1260
1261 return AE_OK;
1262}
1263
1264
1265static int
1266acpi_ec_start (
1267 struct acpi_device *device)
1268{
Luming Yu45bea152005-07-23 04:08:00 -04001269 acpi_status status = AE_OK;
1270 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 ACPI_FUNCTION_TRACE("acpi_ec_start");
1273
1274 if (!device)
1275 return_VALUE(-EINVAL);
1276
1277 ec = acpi_driver_data(device);
1278
1279 if (!ec)
1280 return_VALUE(-EINVAL);
1281
1282 /*
1283 * Get I/O port addresses. Convert to GAS format.
1284 */
Luming Yu45bea152005-07-23 04:08:00 -04001285 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 acpi_ec_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -04001287 if (ACPI_FAILURE(status) || ec->common.command_addr.register_bit_width == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
1289 return_VALUE(-ENODEV);
1290 }
1291
Luming Yu45bea152005-07-23 04:08:00 -04001292 ec->common.status_addr = ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001295 (u32) ec->common.gpe_bit, (u32) ec->common.command_addr.address,
1296 (u32) ec->common.data_addr.address));
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * Install GPE handler
1301 */
Luming Yu45bea152005-07-23 04:08:00 -04001302 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
1304 if (ACPI_FAILURE(status)) {
1305 return_VALUE(-ENODEV);
1306 }
Luming Yu45bea152005-07-23 04:08:00 -04001307 acpi_set_gpe_type (NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1308 acpi_enable_gpe (NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Luming Yu45bea152005-07-23 04:08:00 -04001310 status = acpi_install_address_space_handler (ec->common.handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1312 &acpi_ec_space_setup, ec);
1313 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001314 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return_VALUE(-ENODEV);
1316 }
1317
1318 return_VALUE(AE_OK);
1319}
1320
1321
1322static int
1323acpi_ec_stop (
1324 struct acpi_device *device,
1325 int type)
1326{
Luming Yu45bea152005-07-23 04:08:00 -04001327 acpi_status status = AE_OK;
1328 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1331
1332 if (!device)
1333 return_VALUE(-EINVAL);
1334
1335 ec = acpi_driver_data(device);
1336
Luming Yu45bea152005-07-23 04:08:00 -04001337 status = acpi_remove_address_space_handler(ec->common.handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1339 if (ACPI_FAILURE(status))
1340 return_VALUE(-ENODEV);
1341
Luming Yu45bea152005-07-23 04:08:00 -04001342 status = acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (ACPI_FAILURE(status))
1344 return_VALUE(-ENODEV);
1345
1346 return_VALUE(0);
1347}
1348
1349static acpi_status __init
1350acpi_fake_ecdt_callback (
1351 acpi_handle handle,
1352 u32 Level,
1353 void *context,
1354 void **retval)
1355{
Luming Yu45bea152005-07-23 04:08:00 -04001356
1357 if (acpi_ec_polling_mode)
1358 return acpi_fake_ecdt_polling_callback(handle,
1359 Level, context, retval);
1360 else
1361 return acpi_fake_ecdt_burst_callback(handle,
1362 Level, context, retval);
1363}
1364
1365static acpi_status __init
1366acpi_fake_ecdt_polling_callback (
1367 acpi_handle handle,
1368 u32 Level,
1369 void *context,
1370 void **retval)
1371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 acpi_status status;
1373
1374 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1375 acpi_ec_io_ports, ec_ecdt);
1376 if (ACPI_FAILURE(status))
1377 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001378 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Luming Yu45bea152005-07-23 04:08:00 -04001380 ec_ecdt->common.uid = -1;
1381 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Luming Yu45bea152005-07-23 04:08:00 -04001383 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (ACPI_FAILURE(status))
1385 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001386 spin_lock_init(&ec_ecdt->polling.lock);
1387 ec_ecdt->common.global_lock = TRUE;
1388 ec_ecdt->common.handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001391 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1392 (u32) ec_ecdt->common.data_addr.address);
1393
1394 return AE_CTRL_TERMINATE;
1395}
1396
1397static acpi_status __init
1398acpi_fake_ecdt_burst_callback (
1399 acpi_handle handle,
1400 u32 Level,
1401 void *context,
1402 void **retval)
1403{
1404 acpi_status status;
1405
1406 init_MUTEX(&ec_ecdt->burst.sem);
1407 init_waitqueue_head(&ec_ecdt->burst.wait);
1408 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1409 acpi_ec_io_ports, ec_ecdt);
1410 if (ACPI_FAILURE(status))
1411 return status;
1412 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1413
1414 ec_ecdt->common.uid = -1;
1415 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1416
1417 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1418 if (ACPI_FAILURE(status))
1419 return status;
1420 ec_ecdt->common.global_lock = TRUE;
1421 ec_ecdt->common.handle = handle;
1422
1423 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1424 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1425 (u32) ec_ecdt->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 return AE_CTRL_TERMINATE;
1428}
1429
1430/*
1431 * Some BIOS (such as some from Gateway laptops) access EC region very early
1432 * such as in BAT0._INI or EC._INI before an EC device is found and
1433 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1434 * required, but if EC regison is accessed early, it is required.
1435 * The routine tries to workaround the BIOS bug by pre-scan EC device
1436 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1437 * op region (since _REG isn't invoked yet). The assumption is true for
1438 * all systems found.
1439 */
1440static int __init
1441acpi_ec_fake_ecdt(void)
1442{
1443 acpi_status status;
1444 int ret = 0;
1445
1446 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1447
Luming Yu45bea152005-07-23 04:08:00 -04001448 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!ec_ecdt) {
1450 ret = -ENOMEM;
1451 goto error;
1452 }
Luming Yu45bea152005-07-23 04:08:00 -04001453 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 status = acpi_get_devices (ACPI_EC_HID,
1456 acpi_fake_ecdt_callback,
1457 NULL,
1458 NULL);
1459 if (ACPI_FAILURE(status)) {
1460 kfree(ec_ecdt);
1461 ec_ecdt = NULL;
1462 ret = -ENODEV;
1463 goto error;
1464 }
1465 return 0;
1466error:
1467 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1468 return ret;
1469}
1470
1471static int __init
1472acpi_ec_get_real_ecdt(void)
1473{
Luming Yu45bea152005-07-23 04:08:00 -04001474 if (acpi_ec_polling_mode)
1475 return acpi_ec_polling_get_real_ecdt();
1476 else
1477 return acpi_ec_burst_get_real_ecdt();
1478}
1479
1480static int __init
1481acpi_ec_polling_get_real_ecdt(void)
1482{
1483 acpi_status status;
1484 struct acpi_table_ecdt *ecdt_ptr;
1485
1486 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1487 (struct acpi_table_header **) &ecdt_ptr);
1488 if (ACPI_FAILURE(status))
1489 return -ENODEV;
1490
1491 printk(KERN_INFO PREFIX "Found ECDT\n");
1492
1493 /*
1494 * Generate a temporary ec context to use until the namespace is scanned
1495 */
1496 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1497 if (!ec_ecdt)
1498 return -ENOMEM;
1499 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1500
1501 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1502 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1503 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1504 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1505 spin_lock_init(&ec_ecdt->polling.lock);
1506 /* use the GL just to be safe */
1507 ec_ecdt->common.global_lock = TRUE;
1508 ec_ecdt->common.uid = ecdt_ptr->uid;
1509
1510 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1511 if (ACPI_FAILURE(status)) {
1512 goto error;
1513 }
1514
1515 return 0;
1516error:
1517 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1518 kfree(ec_ecdt);
1519 ec_ecdt = NULL;
1520
1521 return -ENODEV;
1522}
1523
1524
1525static int __init
1526acpi_ec_burst_get_real_ecdt(void)
1527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 acpi_status status;
1529 struct acpi_table_ecdt *ecdt_ptr;
1530
Dmitry Torokhov451566f2005-03-19 01:10:05 -05001531 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 (struct acpi_table_header **) &ecdt_ptr);
1533 if (ACPI_FAILURE(status))
1534 return -ENODEV;
1535
1536 printk(KERN_INFO PREFIX "Found ECDT\n");
1537
1538 /*
1539 * Generate a temporary ec context to use until the namespace is scanned
1540 */
Luming Yu45bea152005-07-23 04:08:00 -04001541 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (!ec_ecdt)
1543 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -04001544 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Luming Yu45bea152005-07-23 04:08:00 -04001546 init_MUTEX(&ec_ecdt->burst.sem);
1547 init_waitqueue_head(&ec_ecdt->burst.wait);
1548 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1549 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1550 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1551 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* use the GL just to be safe */
Luming Yu45bea152005-07-23 04:08:00 -04001553 ec_ecdt->common.global_lock = TRUE;
1554 ec_ecdt->common.uid = ecdt_ptr->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Luming Yu45bea152005-07-23 04:08:00 -04001556 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (ACPI_FAILURE(status)) {
1558 goto error;
1559 }
1560
1561 return 0;
1562error:
1563 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1564 kfree(ec_ecdt);
1565 ec_ecdt = NULL;
1566
1567 return -ENODEV;
1568}
1569
1570static int __initdata acpi_fake_ecdt_enabled;
1571int __init
1572acpi_ec_ecdt_probe (void)
1573{
1574 acpi_status status;
1575 int ret;
1576
1577 ret = acpi_ec_get_real_ecdt();
1578 /* Try to make a fake ECDT */
1579 if (ret && acpi_fake_ecdt_enabled) {
1580 ret = acpi_ec_fake_ecdt();
1581 }
1582
1583 if (ret)
1584 return 0;
1585
1586 /*
1587 * Install GPE handler
1588 */
Luming Yu45bea152005-07-23 04:08:00 -04001589 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1591 ec_ecdt);
1592 if (ACPI_FAILURE(status)) {
1593 goto error;
1594 }
Luming Yu45bea152005-07-23 04:08:00 -04001595 acpi_set_gpe_type (NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1596 acpi_enable_gpe (NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1599 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1600 &acpi_ec_space_setup, ec_ecdt);
1601 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001602 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 &acpi_ec_gpe_handler);
1604 goto error;
1605 }
1606
1607 return 0;
1608
1609error:
1610 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1611 kfree(ec_ecdt);
1612 ec_ecdt = NULL;
1613
1614 return -ENODEV;
1615}
1616
1617
1618static int __init acpi_ec_init (void)
1619{
Luming Yu45bea152005-07-23 04:08:00 -04001620 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 ACPI_FUNCTION_TRACE("acpi_ec_init");
1623
1624 if (acpi_disabled)
1625 return_VALUE(0);
1626
1627 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1628 if (!acpi_ec_dir)
1629 return_VALUE(-ENODEV);
1630
1631 /* Now register the driver for the EC */
1632 result = acpi_bus_register_driver(&acpi_ec_driver);
1633 if (result < 0) {
1634 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1635 return_VALUE(-ENODEV);
1636 }
1637
1638 return_VALUE(result);
1639}
1640
1641subsys_initcall(acpi_ec_init);
1642
1643/* EC driver currently not unloadable */
1644#if 0
1645static void __exit
1646acpi_ec_exit (void)
1647{
1648 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1649
1650 acpi_bus_unregister_driver(&acpi_ec_driver);
1651
1652 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1653
1654 return_VOID;
1655}
1656#endif /* 0 */
1657
1658static int __init acpi_fake_ecdt_setup(char *str)
1659{
1660 acpi_fake_ecdt_enabled = 1;
1661 return 0;
1662}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Luming Yu45bea152005-07-23 04:08:00 -04001665static int __init acpi_ec_set_polling_mode(char *str)
1666{
Luming Yu7b15f5e2005-08-03 17:38:04 -04001667 int burst;
1668
1669 if (!get_option(&str, &burst))
1670 return 0;
1671
1672 if (burst) {
1673 acpi_ec_polling_mode = EC_BURST;
1674 acpi_ec_driver.ops.add = acpi_ec_burst_add;
1675 } else {
1676 acpi_ec_polling_mode = EC_POLLING;
1677 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1678 }
1679 printk(KERN_INFO PREFIX "EC %s mode.\n",
1680 burst ? "burst": "polling");
Luming Yu45bea152005-07-23 04:08:00 -04001681 return 0;
1682}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001683__setup("ec_burst=", acpi_ec_set_polling_mode);