blob: 2dadb7f632693e0edc03c522a914a1dff5deab35 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static struct acpi_driver acpi_ec_driver = {
81 .name = ACPI_EC_DRIVER_NAME,
82 .class = ACPI_EC_CLASS,
83 .ids = ACPI_EC_HID,
84 .ops = {
Luming Yu45bea152005-07-23 04:08:00 -040085 .add = acpi_ec_burst_add,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .remove = acpi_ec_remove,
87 .start = acpi_ec_start,
88 .stop = acpi_ec_stop,
89 },
90};
Luming Yu45bea152005-07-23 04:08:00 -040091union acpi_ec {
92 struct {
93 u32 mode;
94 acpi_handle handle;
95 unsigned long uid;
96 unsigned long gpe_bit;
97 struct acpi_generic_address status_addr;
98 struct acpi_generic_address command_addr;
99 struct acpi_generic_address data_addr;
100 unsigned long global_lock;
101 } common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Luming Yu45bea152005-07-23 04:08:00 -0400103 struct {
104 u32 mode;
105 acpi_handle handle;
106 unsigned long uid;
107 unsigned long gpe_bit;
108 struct acpi_generic_address status_addr;
109 struct acpi_generic_address command_addr;
110 struct acpi_generic_address data_addr;
111 unsigned long global_lock;
112 unsigned int expect_event;
113 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
114 atomic_t pending_gpe;
115 struct semaphore sem;
116 wait_queue_head_t wait;
117 }burst;
118
119 struct {
120 u32 mode;
121 acpi_handle handle;
122 unsigned long uid;
123 unsigned long gpe_bit;
124 struct acpi_generic_address status_addr;
125 struct acpi_generic_address command_addr;
126 struct acpi_generic_address data_addr;
127 unsigned long global_lock;
128 spinlock_t lock;
129 }polling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Luming Yu45bea152005-07-23 04:08:00 -0400132static int acpi_ec_polling_wait ( union acpi_ec *ec, u8 event);
133static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
134static int acpi_ec_polling_read ( union acpi_ec *ec, u8 address, u32 *data);
135static int acpi_ec_burst_read( union acpi_ec *ec, u8 address, u32 *data);
136static int acpi_ec_polling_write ( union acpi_ec *ec, u8 address, u8 data);
137static int acpi_ec_burst_write ( union acpi_ec *ec, u8 address, u8 data);
138static int acpi_ec_polling_query ( union acpi_ec *ec, u32 *data);
139static int acpi_ec_burst_query ( union acpi_ec *ec, u32 *data);
140static void acpi_ec_gpe_polling_query ( void *ec_cxt);
141static void acpi_ec_gpe_burst_query ( void *ec_cxt);
142static u32 acpi_ec_gpe_polling_handler ( void *data);
143static u32 acpi_ec_gpe_burst_handler ( void *data);
144static acpi_status __init
145acpi_fake_ecdt_polling_callback (
146 acpi_handle handle,
147 u32 Level,
148 void *context,
149 void **retval);
150
151static acpi_status __init
152acpi_fake_ecdt_burst_callback (
153 acpi_handle handle,
154 u32 Level,
155 void *context,
156 void **retval);
157
158static int __init
159acpi_ec_polling_get_real_ecdt(void);
160static int __init
161acpi_ec_burst_get_real_ecdt(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Luming Yu45bea152005-07-23 04:08:00 -0400163static union acpi_ec *ec_ecdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* External interfaces use first EC only, so remember */
166static struct acpi_device *first_ec;
Luming Yu45bea152005-07-23 04:08:00 -0400167static int acpi_ec_polling_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* --------------------------------------------------------------------------
170 Transaction Management
171 -------------------------------------------------------------------------- */
172
Luming Yu45bea152005-07-23 04:08:00 -0400173static inline u32 acpi_ec_read_status(union acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500175 u32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Luming Yu45bea152005-07-23 04:08:00 -0400177 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500178 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Luming Yu45bea152005-07-23 04:08:00 -0400181static int
182acpi_ec_wait (
183 union acpi_ec *ec,
184 u8 event)
185{
186 if (acpi_ec_polling_mode)
187 return acpi_ec_polling_wait (ec, event);
188 else
189 return acpi_ec_burst_wait (ec, event);
190}
191
192static int
193acpi_ec_polling_wait (
194 union acpi_ec *ec,
195 u8 event)
196{
197 u32 acpi_ec_status = 0;
198 u32 i = ACPI_EC_UDELAY_COUNT;
199
200 if (!ec)
201 return -EINVAL;
202
203 /* Poll the EC status register waiting for the event to occur. */
204 switch (event) {
205 case ACPI_EC_EVENT_OBF:
206 do {
207 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
208 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
209 return 0;
210 udelay(ACPI_EC_UDELAY);
211 } while (--i>0);
212 break;
213 case ACPI_EC_EVENT_IBE:
214 do {
215 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
216 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
217 return 0;
218 udelay(ACPI_EC_UDELAY);
219 } while (--i>0);
220 break;
221 default:
222 return -EINVAL;
223 }
224
225 return -ETIME;
226}
227static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500228{
229 int result = 0;
230
231 ACPI_FUNCTION_TRACE("acpi_ec_wait");
232
Luming Yu45bea152005-07-23 04:08:00 -0400233 ec->burst.expect_event = event;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500234 smp_mb();
235
Luming Yu45bea152005-07-23 04:08:00 -0400236 result = wait_event_interruptible_timeout(ec->burst.wait,
237 !ec->burst.expect_event,
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500238 msecs_to_jiffies(ACPI_EC_DELAY));
239
Luming Yu45bea152005-07-23 04:08:00 -0400240 ec->burst.expect_event = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500241 smp_mb();
242
243 if (result < 0){
244 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
245 return_VALUE(result);
246 }
247
248 /*
249 * Verify that the event in question has actually happened by
250 * querying EC status. Do the check even if operation timed-out
251 * to make sure that we did not miss interrupt.
252 */
253 switch (event) {
254 case ACPI_EC_EVENT_OBF:
255 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
256 return_VALUE(0);
257 break;
258
259 case ACPI_EC_EVENT_IBE:
260 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
261 return_VALUE(0);
262 break;
263 }
264
265 return_VALUE(-ETIME);
266}
267
268
269
270static int
271acpi_ec_enter_burst_mode (
Luming Yu45bea152005-07-23 04:08:00 -0400272 union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500273{
274 u32 tmp = 0;
275 int status = 0;
276
277 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
278
279 status = acpi_ec_read_status(ec);
280 if (status != -EINVAL &&
281 !(status & ACPI_EC_FLAG_BURST)){
Luming Yu45bea152005-07-23 04:08:00 -0400282 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500283 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
284 if (status){
Luming Yu45bea152005-07-23 04:08:00 -0400285 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500286 return_VALUE(-EINVAL);
287 }
Luming Yu45bea152005-07-23 04:08:00 -0400288 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
289 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500290 if(tmp != 0x90 ) {/* Burst ACK byte*/
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500291 return_VALUE(-EINVAL);
292 }
Luming Yu668d74c2005-07-23 00:26:33 -0400293 }
294
Luming Yu45bea152005-07-23 04:08:00 -0400295 atomic_set(&ec->burst.leaving_burst , 0);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500296 return_VALUE(0);
297}
298
299static int
300acpi_ec_leave_burst_mode (
Luming Yu45bea152005-07-23 04:08:00 -0400301 union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500302{
303 int status =0;
304
305 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
306
Luming Yu45bea152005-07-23 04:08:00 -0400307 atomic_set(&ec->burst.leaving_burst , 1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500308 status = acpi_ec_read_status(ec);
309 if (status != -EINVAL &&
310 (status & ACPI_EC_FLAG_BURST)){
Luming Yu45bea152005-07-23 04:08:00 -0400311 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500312 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
313 if (status){
Luming Yu45bea152005-07-23 04:08:00 -0400314 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500315 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
316 return_VALUE(-EINVAL);
317 }
Luming Yu45bea152005-07-23 04:08:00 -0400318 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500319 status = acpi_ec_read_status(ec);
Luming Yu668d74c2005-07-23 00:26:33 -0400320 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500321
322 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
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500463retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *data = 0;
465
Luming Yu45bea152005-07-23 04:08:00 -0400466 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
468 if (ACPI_FAILURE(status))
469 return_VALUE(-ENODEV);
470 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500471
472 WARN_ON(in_interrupt());
Luming Yu45bea152005-07-23 04:08:00 -0400473 down(&ec->burst.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500474
475 if(acpi_ec_enter_burst_mode(ec))
476 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Luming Yu45bea152005-07-23 04:08:00 -0400478 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500479 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Luming Yu45bea152005-07-23 04:08:00 -0400480 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500481 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Luming Yu45bea152005-07-23 04:08:00 -0400485 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500486 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
487 if (status){
Luming Yu45bea152005-07-23 04:08:00 -0400488 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Luming Yu45bea152005-07-23 04:08:00 -0400492 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
493 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
496 *data, address));
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498end:
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500499 acpi_ec_leave_burst_mode(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400500 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Luming Yu45bea152005-07-23 04:08:00 -0400502 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 acpi_release_global_lock(glk);
504
Luming Yu45bea152005-07-23 04:08:00 -0400505 if(atomic_read(&ec->burst.leaving_burst) == 2){
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500506 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
Luming Yu45bea152005-07-23 04:08:00 -0400507 while(atomic_read(&ec->burst.pending_gpe)){
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500508 msleep(1);
509 }
Luming Yu45bea152005-07-23 04:08:00 -0400510 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500511 goto retry;
512 }
513
514 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517
518static int
Luming Yu45bea152005-07-23 04:08:00 -0400519acpi_ec_burst_write (
520 union acpi_ec *ec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 u8 address,
522 u8 data)
523{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500524 int status = 0;
525 u32 glk;
526 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 ACPI_FUNCTION_TRACE("acpi_ec_write");
529
530 if (!ec)
531 return_VALUE(-EINVAL);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500532retry:
Luming Yu45bea152005-07-23 04:08:00 -0400533 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
535 if (ACPI_FAILURE(status))
536 return_VALUE(-ENODEV);
537 }
538
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500539 WARN_ON(in_interrupt());
Luming Yu45bea152005-07-23 04:08:00 -0400540 down(&ec->burst.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500541
542 if(acpi_ec_enter_burst_mode(ec))
543 goto end;
544
545 status = acpi_ec_read_status(ec);
546 if (status != -EINVAL &&
547 !(status & ACPI_EC_FLAG_BURST)){
Luming Yu45bea152005-07-23 04:08:00 -0400548 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500549 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
550 if (status)
551 goto end;
Luming Yu45bea152005-07-23 04:08:00 -0400552 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500553 if(tmp != 0x90 ) /* Burst ACK byte*/
554 goto end;
555 }
556 /*Now we are in burst mode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Luming Yu45bea152005-07-23 04:08:00 -0400558 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500559 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Luming Yu45bea152005-07-23 04:08:00 -0400560 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500561 if (status){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Luming Yu45bea152005-07-23 04:08:00 -0400565 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500566 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
567 if (status){
Luming Yu45bea152005-07-23 04:08:00 -0400568 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Luming Yu45bea152005-07-23 04:08:00 -0400572 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500573 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Luming Yu45bea152005-07-23 04:08:00 -0400574 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500575 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto end;
577
578 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
579 data, address));
580
581end:
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500582 acpi_ec_leave_burst_mode(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400583 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Luming Yu45bea152005-07-23 04:08:00 -0400585 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 acpi_release_global_lock(glk);
587
Luming Yu45bea152005-07-23 04:08:00 -0400588 if(atomic_read(&ec->burst.leaving_burst) == 2){
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500589 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
Luming Yu45bea152005-07-23 04:08:00 -0400590 while(atomic_read(&ec->burst.pending_gpe)){
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500591 msleep(1);
592 }
Luming Yu45bea152005-07-23 04:08:00 -0400593 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500594 goto retry;
595 }
596
597 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
601 * Externally callable EC access functions. For now, assume 1 EC only
602 */
603int
604ec_read(u8 addr, u8 *val)
605{
Luming Yu45bea152005-07-23 04:08:00 -0400606 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int err;
608 u32 temp_data;
609
610 if (!first_ec)
611 return -ENODEV;
612
613 ec = acpi_driver_data(first_ec);
614
615 err = acpi_ec_read(ec, addr, &temp_data);
616
617 if (!err) {
618 *val = temp_data;
619 return 0;
620 }
621 else
622 return err;
623}
624EXPORT_SYMBOL(ec_read);
625
626int
627ec_write(u8 addr, u8 val)
628{
Luming Yu45bea152005-07-23 04:08:00 -0400629 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int err;
631
632 if (!first_ec)
633 return -ENODEV;
634
635 ec = acpi_driver_data(first_ec);
636
637 err = acpi_ec_write(ec, addr, val);
638
639 return err;
640}
641EXPORT_SYMBOL(ec_write);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static int
644acpi_ec_query (
Luming Yu45bea152005-07-23 04:08:00 -0400645 union acpi_ec *ec,
646 u32 *data)
647{
648 if (acpi_ec_polling_mode)
649 return acpi_ec_polling_query(ec, data);
650 else
651 return acpi_ec_burst_query(ec, data);
652}
653static int
654acpi_ec_polling_query (
655 union acpi_ec *ec,
656 u32 *data)
657{
658 int result = 0;
659 acpi_status status = AE_OK;
660 unsigned long flags = 0;
661 u32 glk = 0;
662
663 ACPI_FUNCTION_TRACE("acpi_ec_query");
664
665 if (!ec || !data)
666 return_VALUE(-EINVAL);
667
668 *data = 0;
669
670 if (ec->common.global_lock) {
671 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
672 if (ACPI_FAILURE(status))
673 return_VALUE(-ENODEV);
674 }
675
676 /*
677 * Query the EC to find out which _Qxx method we need to evaluate.
678 * Note that successful completion of the query causes the ACPI_EC_SCI
679 * bit to be cleared (and thus clearing the interrupt source).
680 */
681 spin_lock_irqsave(&ec->polling.lock, flags);
682
683 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
684 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
685 if (result)
686 goto end;
687
688 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
689 if (!*data)
690 result = -ENODATA;
691
692end:
693 spin_unlock_irqrestore(&ec->polling.lock, flags);
694
695 if (ec->common.global_lock)
696 acpi_release_global_lock(glk);
697
698 return_VALUE(result);
699}
700static int
701acpi_ec_burst_query (
702 union acpi_ec *ec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 u32 *data)
704{
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500705 int status = 0;
706 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 ACPI_FUNCTION_TRACE("acpi_ec_query");
709
710 if (!ec || !data)
711 return_VALUE(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *data = 0;
713
Luming Yu45bea152005-07-23 04:08:00 -0400714 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
716 if (ACPI_FAILURE(status))
717 return_VALUE(-ENODEV);
718 }
719
Luming Yu45bea152005-07-23 04:08:00 -0400720 down(&ec->burst.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500721 if(acpi_ec_enter_burst_mode(ec))
722 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * Query the EC to find out which _Qxx method we need to evaluate.
725 * Note that successful completion of the query causes the ACPI_EC_SCI
726 * bit to be cleared (and thus clearing the interrupt source).
727 */
Luming Yu45bea152005-07-23 04:08:00 -0400728 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500729 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
730 if (status){
Luming Yu45bea152005-07-23 04:08:00 -0400731 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500733 }
734
Luming Yu45bea152005-07-23 04:08:00 -0400735 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
736 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!*data)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500738 status = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740end:
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500741 acpi_ec_leave_burst_mode(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400742 up(&ec->burst.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Luming Yu45bea152005-07-23 04:08:00 -0400744 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 acpi_release_global_lock(glk);
746
Luming Yu45bea152005-07-23 04:08:00 -0400747 if(atomic_read(&ec->burst.leaving_burst) == 2){
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500748 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
Luming Yu45bea152005-07-23 04:08:00 -0400749 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Luming Yu17e9c782005-04-22 23:07:10 -0400750 status = -ENODATA;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500751 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500752 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755
756/* --------------------------------------------------------------------------
757 Event Management
758 -------------------------------------------------------------------------- */
759
Luming Yu45bea152005-07-23 04:08:00 -0400760union acpi_ec_query_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 acpi_handle handle;
762 u8 data;
763};
764
765static void
766acpi_ec_gpe_query (
767 void *ec_cxt)
768{
Luming Yu45bea152005-07-23 04:08:00 -0400769 if (acpi_ec_polling_mode)
770 acpi_ec_gpe_polling_query(ec_cxt);
771 else
772 acpi_ec_gpe_burst_query(ec_cxt);
773}
774
775static void
776acpi_ec_gpe_polling_query (
777 void *ec_cxt)
778{
779 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
780 u32 value = 0;
781 unsigned long flags = 0;
782 static char object_name[5] = {'_','Q','0','0','\0'};
783 const char hex[] = {'0','1','2','3','4','5','6','7',
784 '8','9','A','B','C','D','E','F'};
785
786 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
787
788 if (!ec_cxt)
789 goto end;
790
791 spin_lock_irqsave(&ec->polling.lock, flags);
792 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
793 spin_unlock_irqrestore(&ec->polling.lock, flags);
794
795 /* TBD: Implement asynch events!
796 * NOTE: All we care about are EC-SCI's. Other EC events are
797 * handled via polling (yuck!). This is because some systems
798 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
799 * a purely interrupt-driven approach (grumble, grumble).
800 */
801 if (!(value & ACPI_EC_FLAG_SCI))
802 goto end;
803
804 if (acpi_ec_query(ec, &value))
805 goto end;
806
807 object_name[2] = hex[((value >> 4) & 0x0F)];
808 object_name[3] = hex[(value & 0x0F)];
809
810 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
811
812 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
813
814end:
815 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
816}
817static void
818acpi_ec_gpe_burst_query (
819 void *ec_cxt)
820{
821 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500822 u32 value;
823 int result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 static char object_name[5] = {'_','Q','0','0','\0'};
825 const char hex[] = {'0','1','2','3','4','5','6','7',
826 '8','9','A','B','C','D','E','F'};
827
828 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
829
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500830 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
831 result = acpi_ec_query(ec, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500833 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto end;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 object_name[2] = hex[((value >> 4) & 0x0F)];
837 object_name[3] = hex[(value & 0x0F)];
838
839 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
840
Luming Yu45bea152005-07-23 04:08:00 -0400841 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500842end:
Luming Yu45bea152005-07-23 04:08:00 -0400843 atomic_dec(&ec->burst.pending_gpe);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500844 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static u32
848acpi_ec_gpe_handler (
849 void *data)
850{
Luming Yu45bea152005-07-23 04:08:00 -0400851 if (acpi_ec_polling_mode)
852 return acpi_ec_gpe_polling_handler(data);
853 else
854 return acpi_ec_gpe_burst_handler(data);
855}
856static u32
857acpi_ec_gpe_polling_handler (
858 void *data)
859{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 acpi_status status = AE_OK;
Luming Yu45bea152005-07-23 04:08:00 -0400861 union acpi_ec *ec = (union acpi_ec *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (!ec)
864 return ACPI_INTERRUPT_NOT_HANDLED;
865
Luming Yu45bea152005-07-23 04:08:00 -0400866 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
867
868 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
869 acpi_ec_gpe_query, ec);
870
871 if (status == AE_OK)
872 return ACPI_INTERRUPT_HANDLED;
873 else
874 return ACPI_INTERRUPT_NOT_HANDLED;
875}
876static u32
877acpi_ec_gpe_burst_handler (
878 void *data)
879{
880 acpi_status status = AE_OK;
881 u32 value;
882 union acpi_ec *ec = (union acpi_ec *) data;
883
884 if (!ec)
885 return ACPI_INTERRUPT_NOT_HANDLED;
886
887 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500889 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500891 if((value & ACPI_EC_FLAG_IBF) &&
892 !(value & ACPI_EC_FLAG_BURST) &&
Luming Yu45bea152005-07-23 04:08:00 -0400893 (atomic_read(&ec->burst.leaving_burst) == 0)) {
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500894 /*
895 * the embedded controller disables
896 * burst mode for any reason other
897 * than the burst disable command
898 * to process critical event.
899 */
Luming Yu45bea152005-07-23 04:08:00 -0400900 atomic_set(&ec->burst.leaving_burst , 2); /* block current pending transaction
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500901 and retry */
Luming Yu45bea152005-07-23 04:08:00 -0400902 wake_up(&ec->burst.wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500903 }else {
Luming Yu45bea152005-07-23 04:08:00 -0400904 if ((ec->burst.expect_event == ACPI_EC_EVENT_OBF &&
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500905 (value & ACPI_EC_FLAG_OBF)) ||
Luming Yu45bea152005-07-23 04:08:00 -0400906 (ec->burst.expect_event == ACPI_EC_EVENT_IBE &&
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500907 !(value & ACPI_EC_FLAG_IBF))) {
Luming Yu45bea152005-07-23 04:08:00 -0400908 ec->burst.expect_event = 0;
909 wake_up(&ec->burst.wait);
Luming Yu17e9c782005-04-22 23:07:10 -0400910 return ACPI_INTERRUPT_HANDLED;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500911 }
912 }
913
914 if (value & ACPI_EC_FLAG_SCI){
Luming Yu45bea152005-07-23 04:08:00 -0400915 atomic_add(1, &ec->burst.pending_gpe) ;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500916 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
917 acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400918 return status == AE_OK ?
919 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500920 }
Luming Yu45bea152005-07-23 04:08:00 -0400921 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500922 return status == AE_OK ?
923 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/* --------------------------------------------------------------------------
927 Address Space Management
928 -------------------------------------------------------------------------- */
929
930static acpi_status
931acpi_ec_space_setup (
932 acpi_handle region_handle,
933 u32 function,
934 void *handler_context,
935 void **return_context)
936{
937 /*
938 * The EC object is in the handler context and is needed
939 * when calling the acpi_ec_space_handler.
940 */
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500941 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
942 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return AE_OK;
945}
946
947
948static acpi_status
949acpi_ec_space_handler (
950 u32 function,
951 acpi_physical_address address,
952 u32 bit_width,
953 acpi_integer *value,
954 void *handler_context,
955 void *region_context)
956{
957 int result = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400958 union acpi_ec *ec = NULL;
Luming Yufa9cd542005-03-19 01:54:47 -0500959 u64 temp = *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 acpi_integer f_v = 0;
961 int i = 0;
962
963 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
964
965 if ((address > 0xFF) || !value || !handler_context)
966 return_VALUE(AE_BAD_PARAMETER);
967
Luming Yufa9cd542005-03-19 01:54:47 -0500968 if (bit_width != 8 && acpi_strict) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
Luming Yufa9cd542005-03-19 01:54:47 -0500970 return_VALUE(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Luming Yu45bea152005-07-23 04:08:00 -0400973 ec = (union acpi_ec *) handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975next_byte:
976 switch (function) {
977 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500978 temp = 0;
979 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500982 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 default:
985 result = -EINVAL;
986 goto out;
987 break;
988 }
989
990 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500991 if (bit_width) {
992 if (function == ACPI_READ)
993 f_v |= temp << 8 * i;
994 if (function == ACPI_WRITE)
995 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500997 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto next_byte;
999 }
1000
Luming Yufa9cd542005-03-19 01:54:47 -05001001 if (function == ACPI_READ) {
1002 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *value = f_v;
1004 }
1005
1006
1007out:
1008 switch (result) {
1009 case -EINVAL:
1010 return_VALUE(AE_BAD_PARAMETER);
1011 break;
1012 case -ENODEV:
1013 return_VALUE(AE_NOT_FOUND);
1014 break;
1015 case -ETIME:
1016 return_VALUE(AE_TIME);
1017 break;
1018 default:
1019 return_VALUE(AE_OK);
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023
1024/* --------------------------------------------------------------------------
1025 FS Interface (/proc)
1026 -------------------------------------------------------------------------- */
1027
1028static struct proc_dir_entry *acpi_ec_dir;
1029
1030
1031static int
1032acpi_ec_read_info (struct seq_file *seq, void *offset)
1033{
Luming Yu45bea152005-07-23 04:08:00 -04001034 union acpi_ec *ec = (union acpi_ec *) seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
1037
1038 if (!ec)
1039 goto end;
1040
1041 seq_printf(seq, "gpe bit: 0x%02x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001042 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001044 (u32) ec->common.status_addr.address, (u32) ec->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 seq_printf(seq, "use global lock: %s\n",
Luming Yu45bea152005-07-23 04:08:00 -04001046 ec->common.global_lock?"yes":"no");
1047 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049end:
1050 return_VALUE(0);
1051}
1052
1053static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
1054{
1055 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
1056}
1057
1058static struct file_operations acpi_ec_info_ops = {
1059 .open = acpi_ec_info_open_fs,
1060 .read = seq_read,
1061 .llseek = seq_lseek,
1062 .release = single_release,
1063 .owner = THIS_MODULE,
1064};
1065
1066static int
1067acpi_ec_add_fs (
1068 struct acpi_device *device)
1069{
Luming Yu45bea152005-07-23 04:08:00 -04001070 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
1073
1074 if (!acpi_device_dir(device)) {
1075 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1076 acpi_ec_dir);
1077 if (!acpi_device_dir(device))
1078 return_VALUE(-ENODEV);
1079 }
1080
1081 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
1082 acpi_device_dir(device));
1083 if (!entry)
1084 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1085 "Unable to create '%s' fs entry\n",
1086 ACPI_EC_FILE_INFO));
1087 else {
1088 entry->proc_fops = &acpi_ec_info_ops;
1089 entry->data = acpi_driver_data(device);
1090 entry->owner = THIS_MODULE;
1091 }
1092
1093 return_VALUE(0);
1094}
1095
1096
1097static int
1098acpi_ec_remove_fs (
1099 struct acpi_device *device)
1100{
1101 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
1102
1103 if (acpi_device_dir(device)) {
1104 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
1105 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
1106 acpi_device_dir(device) = NULL;
1107 }
1108
1109 return_VALUE(0);
1110}
1111
1112
1113/* --------------------------------------------------------------------------
1114 Driver Interface
1115 -------------------------------------------------------------------------- */
1116
Luming Yu45bea152005-07-23 04:08:00 -04001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static int
Luming Yu45bea152005-07-23 04:08:00 -04001119acpi_ec_polling_add (
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct acpi_device *device)
1121{
Luming Yu45bea152005-07-23 04:08:00 -04001122 int result = 0;
1123 acpi_status status = AE_OK;
1124 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 unsigned long uid;
1126
1127 ACPI_FUNCTION_TRACE("acpi_ec_add");
1128
1129 if (!device)
1130 return_VALUE(-EINVAL);
1131
Luming Yu45bea152005-07-23 04:08:00 -04001132 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!ec)
1134 return_VALUE(-ENOMEM);
Luming Yu45bea152005-07-23 04:08:00 -04001135 memset(ec, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Luming Yu45bea152005-07-23 04:08:00 -04001137 ec->common.handle = device->handle;
1138 ec->common.uid = -1;
1139 spin_lock_init(&ec->polling.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1141 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1142 acpi_driver_data(device) = ec;
1143
1144 /* Use the global lock for all EC transactions? */
Luming Yu45bea152005-07-23 04:08:00 -04001145 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* If our UID matches the UID for the ECDT-enumerated EC,
1148 we now have the *real* EC info, so kill the makeshift one.*/
Luming Yu45bea152005-07-23 04:08:00 -04001149 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1150 if (ec_ecdt && ec_ecdt->common.uid == uid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1152 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
Luming Yu45bea152005-07-23 04:08:00 -04001153
1154 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 kfree(ec_ecdt);
1157 }
1158
1159 /* Get GPE bit assignment (EC events). */
1160 /* TODO: Add support for _GPE returning a package */
Luming Yu45bea152005-07-23 04:08:00 -04001161 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (ACPI_FAILURE(status)) {
1163 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1164 "Error obtaining GPE bit assignment\n"));
1165 result = -ENODEV;
1166 goto end;
1167 }
1168
1169 result = acpi_ec_add_fs(device);
1170 if (result)
1171 goto end;
1172
1173 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1174 acpi_device_name(device), acpi_device_bid(device),
Luming Yu45bea152005-07-23 04:08:00 -04001175 (u32) ec->common.gpe_bit);
1176
1177 if (!first_ec)
1178 first_ec = device;
1179
1180end:
1181 if (result)
1182 kfree(ec);
1183
1184 return_VALUE(result);
1185}
1186static int
1187acpi_ec_burst_add (
1188 struct acpi_device *device)
1189{
1190 int result = 0;
1191 acpi_status status = AE_OK;
1192 union acpi_ec *ec = NULL;
1193 unsigned long uid;
1194
1195 ACPI_FUNCTION_TRACE("acpi_ec_add");
1196
1197 if (!device)
1198 return_VALUE(-EINVAL);
1199
1200 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1201 if (!ec)
1202 return_VALUE(-ENOMEM);
1203 memset(ec, 0, sizeof(union acpi_ec));
1204
1205 ec->common.handle = device->handle;
1206 ec->common.uid = -1;
1207 atomic_set(&ec->burst.pending_gpe, 0);
1208 atomic_set(&ec->burst.leaving_burst , 1);
1209 init_MUTEX(&ec->burst.sem);
1210 init_waitqueue_head(&ec->burst.wait);
1211 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1212 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1213 acpi_driver_data(device) = ec;
1214
1215 /* Use the global lock for all EC transactions? */
1216 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1217
1218 /* If our UID matches the UID for the ECDT-enumerated EC,
1219 we now have the *real* EC info, so kill the makeshift one.*/
1220 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1221 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1222 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1223 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1224
1225 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1226
1227 kfree(ec_ecdt);
1228 }
1229
1230 /* Get GPE bit assignment (EC events). */
1231 /* TODO: Add support for _GPE returning a package */
1232 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1233 if (ACPI_FAILURE(status)) {
1234 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1235 "Error obtaining GPE bit assignment\n"));
1236 result = -ENODEV;
1237 goto end;
1238 }
1239
1240 result = acpi_ec_add_fs(device);
1241 if (result)
1242 goto end;
1243
1244 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1245 acpi_device_name(device), acpi_device_bid(device),
1246 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (!first_ec)
1249 first_ec = device;
1250
1251end:
1252 if (result)
1253 kfree(ec);
1254
1255 return_VALUE(result);
1256}
1257
1258
1259static int
1260acpi_ec_remove (
1261 struct acpi_device *device,
1262 int type)
1263{
Luming Yu45bea152005-07-23 04:08:00 -04001264 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1267
1268 if (!device)
1269 return_VALUE(-EINVAL);
1270
1271 ec = acpi_driver_data(device);
1272
1273 acpi_ec_remove_fs(device);
1274
1275 kfree(ec);
1276
1277 return_VALUE(0);
1278}
1279
1280
1281static acpi_status
1282acpi_ec_io_ports (
1283 struct acpi_resource *resource,
1284 void *context)
1285{
Luming Yu45bea152005-07-23 04:08:00 -04001286 union acpi_ec *ec = (union acpi_ec *) context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct acpi_generic_address *addr;
1288
1289 if (resource->id != ACPI_RSTYPE_IO) {
1290 return AE_OK;
1291 }
1292
1293 /*
1294 * The first address region returned is the data port, and
1295 * the second address region returned is the status/command
1296 * port.
1297 */
Luming Yu45bea152005-07-23 04:08:00 -04001298 if (ec->common.data_addr.register_bit_width == 0) {
1299 addr = &ec->common.data_addr;
1300 } else if (ec->common.command_addr.register_bit_width == 0) {
1301 addr = &ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 } else {
1303 return AE_CTRL_TERMINATE;
1304 }
1305
1306 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1307 addr->register_bit_width = 8;
1308 addr->register_bit_offset = 0;
1309 addr->address = resource->data.io.min_base_address;
1310
1311 return AE_OK;
1312}
1313
1314
1315static int
1316acpi_ec_start (
1317 struct acpi_device *device)
1318{
Luming Yu45bea152005-07-23 04:08:00 -04001319 acpi_status status = AE_OK;
1320 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 ACPI_FUNCTION_TRACE("acpi_ec_start");
1323
1324 if (!device)
1325 return_VALUE(-EINVAL);
1326
1327 ec = acpi_driver_data(device);
1328
1329 if (!ec)
1330 return_VALUE(-EINVAL);
1331
1332 /*
1333 * Get I/O port addresses. Convert to GAS format.
1334 */
Luming Yu45bea152005-07-23 04:08:00 -04001335 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 acpi_ec_io_ports, ec);
Luming Yu45bea152005-07-23 04:08:00 -04001337 if (ACPI_FAILURE(status) || ec->common.command_addr.register_bit_width == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
1339 return_VALUE(-ENODEV);
1340 }
1341
Luming Yu45bea152005-07-23 04:08:00 -04001342 ec->common.status_addr = ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001345 (u32) ec->common.gpe_bit, (u32) ec->common.command_addr.address,
1346 (u32) ec->common.data_addr.address));
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /*
1350 * Install GPE handler
1351 */
Luming Yu45bea152005-07-23 04:08:00 -04001352 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
1354 if (ACPI_FAILURE(status)) {
1355 return_VALUE(-ENODEV);
1356 }
Luming Yu45bea152005-07-23 04:08:00 -04001357 acpi_set_gpe_type (NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1358 acpi_enable_gpe (NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Luming Yu45bea152005-07-23 04:08:00 -04001360 status = acpi_install_address_space_handler (ec->common.handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1362 &acpi_ec_space_setup, ec);
1363 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001364 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return_VALUE(-ENODEV);
1366 }
1367
1368 return_VALUE(AE_OK);
1369}
1370
1371
1372static int
1373acpi_ec_stop (
1374 struct acpi_device *device,
1375 int type)
1376{
Luming Yu45bea152005-07-23 04:08:00 -04001377 acpi_status status = AE_OK;
1378 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1381
1382 if (!device)
1383 return_VALUE(-EINVAL);
1384
1385 ec = acpi_driver_data(device);
1386
Luming Yu45bea152005-07-23 04:08:00 -04001387 status = acpi_remove_address_space_handler(ec->common.handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1389 if (ACPI_FAILURE(status))
1390 return_VALUE(-ENODEV);
1391
Luming Yu45bea152005-07-23 04:08:00 -04001392 status = acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (ACPI_FAILURE(status))
1394 return_VALUE(-ENODEV);
1395
1396 return_VALUE(0);
1397}
1398
1399static acpi_status __init
1400acpi_fake_ecdt_callback (
1401 acpi_handle handle,
1402 u32 Level,
1403 void *context,
1404 void **retval)
1405{
Luming Yu45bea152005-07-23 04:08:00 -04001406
1407 if (acpi_ec_polling_mode)
1408 return acpi_fake_ecdt_polling_callback(handle,
1409 Level, context, retval);
1410 else
1411 return acpi_fake_ecdt_burst_callback(handle,
1412 Level, context, retval);
1413}
1414
1415static acpi_status __init
1416acpi_fake_ecdt_polling_callback (
1417 acpi_handle handle,
1418 u32 Level,
1419 void *context,
1420 void **retval)
1421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 acpi_status status;
1423
1424 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1425 acpi_ec_io_ports, ec_ecdt);
1426 if (ACPI_FAILURE(status))
1427 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001428 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Luming Yu45bea152005-07-23 04:08:00 -04001430 ec_ecdt->common.uid = -1;
1431 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Luming Yu45bea152005-07-23 04:08:00 -04001433 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (ACPI_FAILURE(status))
1435 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001436 spin_lock_init(&ec_ecdt->polling.lock);
1437 ec_ecdt->common.global_lock = TRUE;
1438 ec_ecdt->common.handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
Luming Yu45bea152005-07-23 04:08:00 -04001441 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1442 (u32) ec_ecdt->common.data_addr.address);
1443
1444 return AE_CTRL_TERMINATE;
1445}
1446
1447static acpi_status __init
1448acpi_fake_ecdt_burst_callback (
1449 acpi_handle handle,
1450 u32 Level,
1451 void *context,
1452 void **retval)
1453{
1454 acpi_status status;
1455
1456 init_MUTEX(&ec_ecdt->burst.sem);
1457 init_waitqueue_head(&ec_ecdt->burst.wait);
1458 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1459 acpi_ec_io_ports, ec_ecdt);
1460 if (ACPI_FAILURE(status))
1461 return status;
1462 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1463
1464 ec_ecdt->common.uid = -1;
1465 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1466
1467 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1468 if (ACPI_FAILURE(status))
1469 return status;
1470 ec_ecdt->common.global_lock = TRUE;
1471 ec_ecdt->common.handle = handle;
1472
1473 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1474 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1475 (u32) ec_ecdt->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 return AE_CTRL_TERMINATE;
1478}
1479
1480/*
1481 * Some BIOS (such as some from Gateway laptops) access EC region very early
1482 * such as in BAT0._INI or EC._INI before an EC device is found and
1483 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1484 * required, but if EC regison is accessed early, it is required.
1485 * The routine tries to workaround the BIOS bug by pre-scan EC device
1486 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1487 * op region (since _REG isn't invoked yet). The assumption is true for
1488 * all systems found.
1489 */
1490static int __init
1491acpi_ec_fake_ecdt(void)
1492{
1493 acpi_status status;
1494 int ret = 0;
1495
1496 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1497
Luming Yu45bea152005-07-23 04:08:00 -04001498 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!ec_ecdt) {
1500 ret = -ENOMEM;
1501 goto error;
1502 }
Luming Yu45bea152005-07-23 04:08:00 -04001503 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 status = acpi_get_devices (ACPI_EC_HID,
1506 acpi_fake_ecdt_callback,
1507 NULL,
1508 NULL);
1509 if (ACPI_FAILURE(status)) {
1510 kfree(ec_ecdt);
1511 ec_ecdt = NULL;
1512 ret = -ENODEV;
1513 goto error;
1514 }
1515 return 0;
1516error:
1517 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1518 return ret;
1519}
1520
1521static int __init
1522acpi_ec_get_real_ecdt(void)
1523{
Luming Yu45bea152005-07-23 04:08:00 -04001524 if (acpi_ec_polling_mode)
1525 return acpi_ec_polling_get_real_ecdt();
1526 else
1527 return acpi_ec_burst_get_real_ecdt();
1528}
1529
1530static int __init
1531acpi_ec_polling_get_real_ecdt(void)
1532{
1533 acpi_status status;
1534 struct acpi_table_ecdt *ecdt_ptr;
1535
1536 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1537 (struct acpi_table_header **) &ecdt_ptr);
1538 if (ACPI_FAILURE(status))
1539 return -ENODEV;
1540
1541 printk(KERN_INFO PREFIX "Found ECDT\n");
1542
1543 /*
1544 * Generate a temporary ec context to use until the namespace is scanned
1545 */
1546 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1547 if (!ec_ecdt)
1548 return -ENOMEM;
1549 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1550
1551 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1552 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1553 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1554 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1555 spin_lock_init(&ec_ecdt->polling.lock);
1556 /* use the GL just to be safe */
1557 ec_ecdt->common.global_lock = TRUE;
1558 ec_ecdt->common.uid = ecdt_ptr->uid;
1559
1560 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1561 if (ACPI_FAILURE(status)) {
1562 goto error;
1563 }
1564
1565 return 0;
1566error:
1567 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1568 kfree(ec_ecdt);
1569 ec_ecdt = NULL;
1570
1571 return -ENODEV;
1572}
1573
1574
1575static int __init
1576acpi_ec_burst_get_real_ecdt(void)
1577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 acpi_status status;
1579 struct acpi_table_ecdt *ecdt_ptr;
1580
Dmitry Torokhov451566f2005-03-19 01:10:05 -05001581 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 (struct acpi_table_header **) &ecdt_ptr);
1583 if (ACPI_FAILURE(status))
1584 return -ENODEV;
1585
1586 printk(KERN_INFO PREFIX "Found ECDT\n");
1587
1588 /*
1589 * Generate a temporary ec context to use until the namespace is scanned
1590 */
Luming Yu45bea152005-07-23 04:08:00 -04001591 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (!ec_ecdt)
1593 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -04001594 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Luming Yu45bea152005-07-23 04:08:00 -04001596 init_MUTEX(&ec_ecdt->burst.sem);
1597 init_waitqueue_head(&ec_ecdt->burst.wait);
1598 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1599 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1600 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1601 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* use the GL just to be safe */
Luming Yu45bea152005-07-23 04:08:00 -04001603 ec_ecdt->common.global_lock = TRUE;
1604 ec_ecdt->common.uid = ecdt_ptr->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Luming Yu45bea152005-07-23 04:08:00 -04001606 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (ACPI_FAILURE(status)) {
1608 goto error;
1609 }
1610
1611 return 0;
1612error:
1613 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1614 kfree(ec_ecdt);
1615 ec_ecdt = NULL;
1616
1617 return -ENODEV;
1618}
1619
1620static int __initdata acpi_fake_ecdt_enabled;
1621int __init
1622acpi_ec_ecdt_probe (void)
1623{
1624 acpi_status status;
1625 int ret;
1626
1627 ret = acpi_ec_get_real_ecdt();
1628 /* Try to make a fake ECDT */
1629 if (ret && acpi_fake_ecdt_enabled) {
1630 ret = acpi_ec_fake_ecdt();
1631 }
1632
1633 if (ret)
1634 return 0;
1635
1636 /*
1637 * Install GPE handler
1638 */
Luming Yu45bea152005-07-23 04:08:00 -04001639 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1641 ec_ecdt);
1642 if (ACPI_FAILURE(status)) {
1643 goto error;
1644 }
Luming Yu45bea152005-07-23 04:08:00 -04001645 acpi_set_gpe_type (NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1646 acpi_enable_gpe (NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1649 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1650 &acpi_ec_space_setup, ec_ecdt);
1651 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001652 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 &acpi_ec_gpe_handler);
1654 goto error;
1655 }
1656
1657 return 0;
1658
1659error:
1660 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1661 kfree(ec_ecdt);
1662 ec_ecdt = NULL;
1663
1664 return -ENODEV;
1665}
1666
1667
1668static int __init acpi_ec_init (void)
1669{
Luming Yu45bea152005-07-23 04:08:00 -04001670 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 ACPI_FUNCTION_TRACE("acpi_ec_init");
1673
1674 if (acpi_disabled)
1675 return_VALUE(0);
1676
1677 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1678 if (!acpi_ec_dir)
1679 return_VALUE(-ENODEV);
1680
1681 /* Now register the driver for the EC */
1682 result = acpi_bus_register_driver(&acpi_ec_driver);
1683 if (result < 0) {
1684 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1685 return_VALUE(-ENODEV);
1686 }
1687
1688 return_VALUE(result);
1689}
1690
1691subsys_initcall(acpi_ec_init);
1692
1693/* EC driver currently not unloadable */
1694#if 0
1695static void __exit
1696acpi_ec_exit (void)
1697{
1698 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1699
1700 acpi_bus_unregister_driver(&acpi_ec_driver);
1701
1702 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1703
1704 return_VOID;
1705}
1706#endif /* 0 */
1707
1708static int __init acpi_fake_ecdt_setup(char *str)
1709{
1710 acpi_fake_ecdt_enabled = 1;
1711 return 0;
1712}
1713__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Luming Yu45bea152005-07-23 04:08:00 -04001714static int __init acpi_ec_set_polling_mode(char *str)
1715{
1716 acpi_ec_polling_mode = EC_POLLING;
1717 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1718 return 0;
1719}
1720__setup("ec_polling", acpi_ec_set_polling_mode);