[ACPI] increase MAX_IO_APICS to 64 on i386
[linux-2.6-block.git] / drivers / acpi / ec.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
451566f4 34#include <linux/interrupt.h>
1da177e4
LT
35#include <asm/io.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38#include <acpi/actypes.h>
39
40#define _COMPONENT ACPI_EC_COMPONENT
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 */
451566f4 53#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
1da177e4
LT
54#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
451566f4 59#define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
1da177e4
LT
60#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
61
62#define ACPI_EC_COMMAND_READ 0x80
63#define ACPI_EC_COMMAND_WRITE 0x81
451566f4
DT
64#define ACPI_EC_BURST_ENABLE 0x82
65#define ACPI_EC_BURST_DISABLE 0x83
1da177e4
LT
66#define ACPI_EC_COMMAND_QUERY 0x84
67
68static int acpi_ec_add (struct acpi_device *device);
69static int acpi_ec_remove (struct acpi_device *device, int type);
70static int acpi_ec_start (struct acpi_device *device);
71static int acpi_ec_stop (struct acpi_device *device, int type);
72
73static struct acpi_driver acpi_ec_driver = {
74 .name = ACPI_EC_DRIVER_NAME,
75 .class = ACPI_EC_CLASS,
76 .ids = ACPI_EC_HID,
77 .ops = {
78 .add = acpi_ec_add,
79 .remove = acpi_ec_remove,
80 .start = acpi_ec_start,
81 .stop = acpi_ec_stop,
82 },
83};
84
85struct acpi_ec {
86 acpi_handle handle;
87 unsigned long uid;
88 unsigned long gpe_bit;
89 struct acpi_generic_address status_addr;
90 struct acpi_generic_address command_addr;
91 struct acpi_generic_address data_addr;
92 unsigned long global_lock;
451566f4
DT
93 unsigned int expect_event;
94 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
95 atomic_t pending_gpe;
96 struct semaphore sem;
97 wait_queue_head_t wait;
1da177e4
LT
98};
99
100/* If we find an EC via the ECDT, we need to keep a ptr to its context */
101static struct acpi_ec *ec_ecdt;
102
103/* External interfaces use first EC only, so remember */
104static struct acpi_device *first_ec;
105
106/* --------------------------------------------------------------------------
107 Transaction Management
108 -------------------------------------------------------------------------- */
109
451566f4 110static inline u32 acpi_ec_read_status(struct acpi_ec *ec)
1da177e4 111{
451566f4 112 u32 status = 0;
1da177e4 113
451566f4
DT
114 acpi_hw_low_level_read(8, &status, &ec->status_addr);
115 return status;
116}
117
118static int acpi_ec_wait(struct acpi_ec *ec, unsigned int event)
119{
120 int result = 0;
121
122 ACPI_FUNCTION_TRACE("acpi_ec_wait");
1da177e4 123
451566f4
DT
124 ec->expect_event = event;
125 smp_mb();
126
127 result = wait_event_interruptible_timeout(ec->wait,
128 !ec->expect_event,
129 msecs_to_jiffies(ACPI_EC_DELAY));
130
131 ec->expect_event = 0;
132 smp_mb();
133
134 if (result < 0){
135 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
136 return_VALUE(result);
137 }
138
139 /*
140 * Verify that the event in question has actually happened by
141 * querying EC status. Do the check even if operation timed-out
142 * to make sure that we did not miss interrupt.
143 */
1da177e4
LT
144 switch (event) {
145 case ACPI_EC_EVENT_OBF:
451566f4
DT
146 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
147 return_VALUE(0);
1da177e4 148 break;
451566f4 149
1da177e4 150 case ACPI_EC_EVENT_IBE:
451566f4
DT
151 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
152 return_VALUE(0);
1da177e4 153 break;
1da177e4
LT
154 }
155
451566f4 156 return_VALUE(-ETIME);
1da177e4
LT
157}
158
159
451566f4
DT
160
161static int
162acpi_ec_enter_burst_mode (
163 struct acpi_ec *ec)
164{
165 u32 tmp = 0;
166 int status = 0;
167
168 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
169
170 status = acpi_ec_read_status(ec);
171 if (status != -EINVAL &&
172 !(status & ACPI_EC_FLAG_BURST)){
173 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"entering burst mode \n"));
174 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
175 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
176 if (status){
177 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
178 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," status = %d\n", status));
179 return_VALUE(-EINVAL);
180 }
181 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
182 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
183 if(tmp != 0x90 ) {/* Burst ACK byte*/
184 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"Ack failed \n"));
185 return_VALUE(-EINVAL);
186 }
187 } else
188 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in burst mode \n"));
189 atomic_set(&ec->leaving_burst , 0);
190 return_VALUE(0);
191}
192
193static int
194acpi_ec_leave_burst_mode (
195 struct acpi_ec *ec)
196{
197 int status =0;
198
199 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
200
201 atomic_set(&ec->leaving_burst , 1);
202 status = acpi_ec_read_status(ec);
203 if (status != -EINVAL &&
204 (status & ACPI_EC_FLAG_BURST)){
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
206 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->command_addr);
207 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
208 if (status){
209 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
210 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
211 return_VALUE(-EINVAL);
212 }
213 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
214 status = acpi_ec_read_status(ec);
215 if (status != -EINVAL &&
216 (status & ACPI_EC_FLAG_BURST)) {
217 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->status fail\n"));
218 return_VALUE(-EINVAL);
219 }
220 }else
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in Non-burst mode \n"));
222 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
223
224 return_VALUE(0);
225}
226
1da177e4
LT
227static int
228acpi_ec_read (
229 struct acpi_ec *ec,
230 u8 address,
231 u32 *data)
232{
451566f4
DT
233 int status = 0;
234 u32 glk;
1da177e4
LT
235
236 ACPI_FUNCTION_TRACE("acpi_ec_read");
237
238 if (!ec || !data)
239 return_VALUE(-EINVAL);
240
451566f4 241retry:
1da177e4
LT
242 *data = 0;
243
244 if (ec->global_lock) {
245 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
246 if (ACPI_FAILURE(status))
247 return_VALUE(-ENODEV);
248 }
451566f4
DT
249
250 WARN_ON(in_interrupt());
251 down(&ec->sem);
252
253 if(acpi_ec_enter_burst_mode(ec))
254 goto end;
1da177e4
LT
255
256 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
451566f4
DT
257 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
258 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
259 if (status) {
1da177e4 260 goto end;
451566f4 261 }
1da177e4
LT
262
263 acpi_hw_low_level_write(8, address, &ec->data_addr);
451566f4
DT
264 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
265 if (status){
266 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4 267 goto end;
451566f4 268 }
1da177e4
LT
269
270 acpi_hw_low_level_read(8, data, &ec->data_addr);
451566f4 271 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4
LT
272
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
274 *data, address));
451566f4 275
1da177e4 276end:
451566f4
DT
277 acpi_ec_leave_burst_mode(ec);
278 up(&ec->sem);
1da177e4
LT
279
280 if (ec->global_lock)
281 acpi_release_global_lock(glk);
282
451566f4
DT
283 if(atomic_read(&ec->leaving_burst) == 2){
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
17e9c78a 285 while(atomic_read(&ec->pending_gpe)){
451566f4
DT
286 msleep(1);
287 }
288 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
289 goto retry;
290 }
291
292 return_VALUE(status);
1da177e4
LT
293}
294
295
296static int
297acpi_ec_write (
298 struct acpi_ec *ec,
299 u8 address,
300 u8 data)
301{
451566f4
DT
302 int status = 0;
303 u32 glk;
304 u32 tmp;
1da177e4
LT
305
306 ACPI_FUNCTION_TRACE("acpi_ec_write");
307
308 if (!ec)
309 return_VALUE(-EINVAL);
451566f4 310retry:
1da177e4
LT
311 if (ec->global_lock) {
312 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
313 if (ACPI_FAILURE(status))
314 return_VALUE(-ENODEV);
315 }
316
451566f4
DT
317 WARN_ON(in_interrupt());
318 down(&ec->sem);
319
320 if(acpi_ec_enter_burst_mode(ec))
321 goto end;
322
323 status = acpi_ec_read_status(ec);
324 if (status != -EINVAL &&
325 !(status & ACPI_EC_FLAG_BURST)){
326 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
327 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
328 if (status)
329 goto end;
330 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
331 if(tmp != 0x90 ) /* Burst ACK byte*/
332 goto end;
333 }
334 /*Now we are in burst mode*/
1da177e4
LT
335
336 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
451566f4
DT
337 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
338 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
339 if (status){
1da177e4 340 goto end;
451566f4 341 }
1da177e4
LT
342
343 acpi_hw_low_level_write(8, address, &ec->data_addr);
451566f4
DT
344 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
345 if (status){
346 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4 347 goto end;
451566f4 348 }
1da177e4
LT
349
350 acpi_hw_low_level_write(8, data, &ec->data_addr);
451566f4
DT
351 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
352 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
353 if (status)
1da177e4
LT
354 goto end;
355
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
357 data, address));
358
359end:
451566f4
DT
360 acpi_ec_leave_burst_mode(ec);
361 up(&ec->sem);
1da177e4
LT
362
363 if (ec->global_lock)
364 acpi_release_global_lock(glk);
365
451566f4
DT
366 if(atomic_read(&ec->leaving_burst) == 2){
367 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
17e9c78a 368 while(atomic_read(&ec->pending_gpe)){
451566f4
DT
369 msleep(1);
370 }
371 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
372 goto retry;
373 }
374
375 return_VALUE(status);
1da177e4
LT
376}
377
378/*
379 * Externally callable EC access functions. For now, assume 1 EC only
380 */
381int
382ec_read(u8 addr, u8 *val)
383{
384 struct acpi_ec *ec;
385 int err;
386 u32 temp_data;
387
388 if (!first_ec)
389 return -ENODEV;
390
391 ec = acpi_driver_data(first_ec);
392
393 err = acpi_ec_read(ec, addr, &temp_data);
394
395 if (!err) {
396 *val = temp_data;
397 return 0;
398 }
399 else
400 return err;
401}
402EXPORT_SYMBOL(ec_read);
403
404int
405ec_write(u8 addr, u8 val)
406{
407 struct acpi_ec *ec;
408 int err;
409
410 if (!first_ec)
411 return -ENODEV;
412
413 ec = acpi_driver_data(first_ec);
414
415 err = acpi_ec_write(ec, addr, val);
416
417 return err;
418}
419EXPORT_SYMBOL(ec_write);
420
421
422static int
423acpi_ec_query (
424 struct acpi_ec *ec,
425 u32 *data)
426{
451566f4
DT
427 int status = 0;
428 u32 glk;
1da177e4
LT
429
430 ACPI_FUNCTION_TRACE("acpi_ec_query");
431
432 if (!ec || !data)
433 return_VALUE(-EINVAL);
1da177e4
LT
434 *data = 0;
435
436 if (ec->global_lock) {
437 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
438 if (ACPI_FAILURE(status))
439 return_VALUE(-ENODEV);
440 }
441
451566f4
DT
442 down(&ec->sem);
443 if(acpi_ec_enter_burst_mode(ec))
444 goto end;
1da177e4
LT
445 /*
446 * Query the EC to find out which _Qxx method we need to evaluate.
447 * Note that successful completion of the query causes the ACPI_EC_SCI
448 * bit to be cleared (and thus clearing the interrupt source).
449 */
1da177e4 450 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
451566f4
DT
451 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
452 if (status){
453 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4 454 goto end;
451566f4
DT
455 }
456
1da177e4 457 acpi_hw_low_level_read(8, data, &ec->data_addr);
451566f4 458 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4 459 if (!*data)
451566f4 460 status = -ENODATA;
1da177e4
LT
461
462end:
451566f4
DT
463 acpi_ec_leave_burst_mode(ec);
464 up(&ec->sem);
1da177e4
LT
465
466 if (ec->global_lock)
467 acpi_release_global_lock(glk);
468
451566f4
DT
469 if(atomic_read(&ec->leaving_burst) == 2){
470 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
451566f4 471 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
17e9c78a 472 status = -ENODATA;
451566f4 473 }
451566f4 474 return_VALUE(status);
1da177e4
LT
475}
476
477
478/* --------------------------------------------------------------------------
479 Event Management
480 -------------------------------------------------------------------------- */
481
482struct acpi_ec_query_data {
483 acpi_handle handle;
484 u8 data;
485};
486
487static void
488acpi_ec_gpe_query (
489 void *ec_cxt)
490{
491 struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
451566f4
DT
492 u32 value;
493 int result = -ENODATA;
1da177e4
LT
494 static char object_name[5] = {'_','Q','0','0','\0'};
495 const char hex[] = {'0','1','2','3','4','5','6','7',
496 '8','9','A','B','C','D','E','F'};
497
498 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
499
451566f4
DT
500 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
501 result = acpi_ec_query(ec, &value);
1da177e4 502
451566f4 503 if (result)
1da177e4
LT
504 goto end;
505
1da177e4
LT
506 object_name[2] = hex[((value >> 4) & 0x0F)];
507 object_name[3] = hex[(value & 0x0F)];
508
509 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
510
511 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
451566f4 512end:
17e9c78a 513 atomic_dec(&ec->pending_gpe);
451566f4 514 return;
1da177e4
LT
515}
516
517static u32
518acpi_ec_gpe_handler (
519 void *data)
520{
521 acpi_status status = AE_OK;
451566f4 522 u32 value;
1da177e4
LT
523 struct acpi_ec *ec = (struct acpi_ec *) data;
524
525 if (!ec)
526 return ACPI_INTERRUPT_NOT_HANDLED;
527
528 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
529
451566f4 530 value = acpi_ec_read_status(ec);
1da177e4 531
451566f4
DT
532 if((value & ACPI_EC_FLAG_IBF) &&
533 !(value & ACPI_EC_FLAG_BURST) &&
534 (atomic_read(&ec->leaving_burst) == 0)) {
535 /*
536 * the embedded controller disables
537 * burst mode for any reason other
538 * than the burst disable command
539 * to process critical event.
540 */
541 atomic_set(&ec->leaving_burst , 2); /* block current pending transaction
542 and retry */
543 wake_up(&ec->wait);
544 }else {
545 if ((ec->expect_event == ACPI_EC_EVENT_OBF &&
546 (value & ACPI_EC_FLAG_OBF)) ||
547 (ec->expect_event == ACPI_EC_EVENT_IBE &&
548 !(value & ACPI_EC_FLAG_IBF))) {
549 ec->expect_event = 0;
550 wake_up(&ec->wait);
17e9c78a 551 return ACPI_INTERRUPT_HANDLED;
451566f4
DT
552 }
553 }
554
555 if (value & ACPI_EC_FLAG_SCI){
556 atomic_add(1, &ec->pending_gpe) ;
557 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
558 acpi_ec_gpe_query, ec);
17e9c78a
LY
559 return status == AE_OK ?
560 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
451566f4 561 }
17e9c78a 562 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
451566f4
DT
563 return status == AE_OK ?
564 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
1da177e4
LT
565}
566
567/* --------------------------------------------------------------------------
568 Address Space Management
569 -------------------------------------------------------------------------- */
570
571static acpi_status
572acpi_ec_space_setup (
573 acpi_handle region_handle,
574 u32 function,
575 void *handler_context,
576 void **return_context)
577{
578 /*
579 * The EC object is in the handler context and is needed
580 * when calling the acpi_ec_space_handler.
581 */
451566f4
DT
582 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
583 handler_context : NULL;
1da177e4
LT
584
585 return AE_OK;
586}
587
588
589static acpi_status
590acpi_ec_space_handler (
591 u32 function,
592 acpi_physical_address address,
593 u32 bit_width,
594 acpi_integer *value,
595 void *handler_context,
596 void *region_context)
597{
598 int result = 0;
599 struct acpi_ec *ec = NULL;
fa9cd547 600 u64 temp = *value;
1da177e4
LT
601 acpi_integer f_v = 0;
602 int i = 0;
603
604 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
605
606 if ((address > 0xFF) || !value || !handler_context)
607 return_VALUE(AE_BAD_PARAMETER);
608
fa9cd547 609 if (bit_width != 8 && acpi_strict) {
1da177e4 610 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
fa9cd547 611 return_VALUE(AE_BAD_PARAMETER);
1da177e4
LT
612 }
613
614 ec = (struct acpi_ec *) handler_context;
615
616next_byte:
617 switch (function) {
618 case ACPI_READ:
fa9cd547
LY
619 temp = 0;
620 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
1da177e4
LT
621 break;
622 case ACPI_WRITE:
fa9cd547 623 result = acpi_ec_write(ec, (u8) address, (u8) temp);
1da177e4
LT
624 break;
625 default:
626 result = -EINVAL;
627 goto out;
628 break;
629 }
630
631 bit_width -= 8;
fa9cd547
LY
632 if (bit_width) {
633 if (function == ACPI_READ)
634 f_v |= temp << 8 * i;
635 if (function == ACPI_WRITE)
636 temp >>= 8;
1da177e4 637 i++;
83ea7445 638 address++;
1da177e4
LT
639 goto next_byte;
640 }
641
fa9cd547
LY
642 if (function == ACPI_READ) {
643 f_v |= temp << 8 * i;
1da177e4
LT
644 *value = f_v;
645 }
646
647
648out:
649 switch (result) {
650 case -EINVAL:
651 return_VALUE(AE_BAD_PARAMETER);
652 break;
653 case -ENODEV:
654 return_VALUE(AE_NOT_FOUND);
655 break;
656 case -ETIME:
657 return_VALUE(AE_TIME);
658 break;
659 default:
660 return_VALUE(AE_OK);
661 }
1da177e4
LT
662}
663
664
665/* --------------------------------------------------------------------------
666 FS Interface (/proc)
667 -------------------------------------------------------------------------- */
668
669static struct proc_dir_entry *acpi_ec_dir;
670
671
672static int
673acpi_ec_read_info (struct seq_file *seq, void *offset)
674{
675 struct acpi_ec *ec = (struct acpi_ec *) seq->private;
676
677 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
678
679 if (!ec)
680 goto end;
681
682 seq_printf(seq, "gpe bit: 0x%02x\n",
683 (u32) ec->gpe_bit);
684 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
685 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
686 seq_printf(seq, "use global lock: %s\n",
687 ec->global_lock?"yes":"no");
17e9c78a 688 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
1da177e4
LT
689
690end:
691 return_VALUE(0);
692}
693
694static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
695{
696 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
697}
698
699static struct file_operations acpi_ec_info_ops = {
700 .open = acpi_ec_info_open_fs,
701 .read = seq_read,
702 .llseek = seq_lseek,
703 .release = single_release,
704 .owner = THIS_MODULE,
705};
706
707static int
708acpi_ec_add_fs (
709 struct acpi_device *device)
710{
451566f4 711 struct proc_dir_entry *entry;
1da177e4
LT
712
713 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
714
715 if (!acpi_device_dir(device)) {
716 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
717 acpi_ec_dir);
718 if (!acpi_device_dir(device))
719 return_VALUE(-ENODEV);
720 }
721
722 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
723 acpi_device_dir(device));
724 if (!entry)
725 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
726 "Unable to create '%s' fs entry\n",
727 ACPI_EC_FILE_INFO));
728 else {
729 entry->proc_fops = &acpi_ec_info_ops;
730 entry->data = acpi_driver_data(device);
731 entry->owner = THIS_MODULE;
732 }
733
734 return_VALUE(0);
735}
736
737
738static int
739acpi_ec_remove_fs (
740 struct acpi_device *device)
741{
742 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
743
744 if (acpi_device_dir(device)) {
745 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
746 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
747 acpi_device_dir(device) = NULL;
748 }
749
750 return_VALUE(0);
751}
752
753
754/* --------------------------------------------------------------------------
755 Driver Interface
756 -------------------------------------------------------------------------- */
757
758static int
759acpi_ec_add (
760 struct acpi_device *device)
761{
451566f4
DT
762 int result;
763 acpi_status status;
764 struct acpi_ec *ec;
1da177e4
LT
765 unsigned long uid;
766
767 ACPI_FUNCTION_TRACE("acpi_ec_add");
768
769 if (!device)
770 return_VALUE(-EINVAL);
771
772 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
773 if (!ec)
774 return_VALUE(-ENOMEM);
775 memset(ec, 0, sizeof(struct acpi_ec));
776
777 ec->handle = device->handle;
778 ec->uid = -1;
451566f4
DT
779 atomic_set(&ec->pending_gpe, 0);
780 atomic_set(&ec->leaving_burst , 1);
781 init_MUTEX(&ec->sem);
782 init_waitqueue_head(&ec->wait);
1da177e4
LT
783 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
784 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
785 acpi_driver_data(device) = ec;
786
787 /* Use the global lock for all EC transactions? */
788 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
789
790 /* If our UID matches the UID for the ECDT-enumerated EC,
791 we now have the *real* EC info, so kill the makeshift one.*/
792 acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
793 if (ec_ecdt && ec_ecdt->uid == uid) {
794 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
795 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
451566f4 796
1da177e4
LT
797 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
798
799 kfree(ec_ecdt);
800 }
801
802 /* Get GPE bit assignment (EC events). */
803 /* TODO: Add support for _GPE returning a package */
804 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
805 if (ACPI_FAILURE(status)) {
806 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
807 "Error obtaining GPE bit assignment\n"));
808 result = -ENODEV;
809 goto end;
810 }
811
812 result = acpi_ec_add_fs(device);
813 if (result)
814 goto end;
815
816 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
817 acpi_device_name(device), acpi_device_bid(device),
818 (u32) ec->gpe_bit);
819
820 if (!first_ec)
821 first_ec = device;
822
823end:
824 if (result)
825 kfree(ec);
826
827 return_VALUE(result);
828}
829
830
831static int
832acpi_ec_remove (
833 struct acpi_device *device,
834 int type)
835{
451566f4 836 struct acpi_ec *ec;
1da177e4
LT
837
838 ACPI_FUNCTION_TRACE("acpi_ec_remove");
839
840 if (!device)
841 return_VALUE(-EINVAL);
842
843 ec = acpi_driver_data(device);
844
845 acpi_ec_remove_fs(device);
846
847 kfree(ec);
848
849 return_VALUE(0);
850}
851
852
853static acpi_status
854acpi_ec_io_ports (
855 struct acpi_resource *resource,
856 void *context)
857{
858 struct acpi_ec *ec = (struct acpi_ec *) context;
859 struct acpi_generic_address *addr;
860
861 if (resource->id != ACPI_RSTYPE_IO) {
862 return AE_OK;
863 }
864
865 /*
866 * The first address region returned is the data port, and
867 * the second address region returned is the status/command
868 * port.
869 */
870 if (ec->data_addr.register_bit_width == 0) {
871 addr = &ec->data_addr;
872 } else if (ec->command_addr.register_bit_width == 0) {
873 addr = &ec->command_addr;
874 } else {
875 return AE_CTRL_TERMINATE;
876 }
877
878 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
879 addr->register_bit_width = 8;
880 addr->register_bit_offset = 0;
881 addr->address = resource->data.io.min_base_address;
882
883 return AE_OK;
884}
885
886
887static int
888acpi_ec_start (
889 struct acpi_device *device)
890{
451566f4
DT
891 acpi_status status;
892 struct acpi_ec *ec;
1da177e4
LT
893
894 ACPI_FUNCTION_TRACE("acpi_ec_start");
895
896 if (!device)
897 return_VALUE(-EINVAL);
898
899 ec = acpi_driver_data(device);
900
901 if (!ec)
902 return_VALUE(-EINVAL);
903
904 /*
905 * Get I/O port addresses. Convert to GAS format.
906 */
907 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
908 acpi_ec_io_ports, ec);
909 if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
910 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
911 return_VALUE(-ENODEV);
912 }
913
914 ec->status_addr = ec->command_addr;
915
916 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
917 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
918 (u32) ec->data_addr.address));
919
920 /*
921 * Install GPE handler
922 */
923 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
924 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
925 if (ACPI_FAILURE(status)) {
926 return_VALUE(-ENODEV);
927 }
928 acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
929 acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
930
931 status = acpi_install_address_space_handler (ec->handle,
932 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
933 &acpi_ec_space_setup, ec);
934 if (ACPI_FAILURE(status)) {
935 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
936 return_VALUE(-ENODEV);
937 }
938
939 return_VALUE(AE_OK);
940}
941
942
943static int
944acpi_ec_stop (
945 struct acpi_device *device,
946 int type)
947{
451566f4
DT
948 acpi_status status;
949 struct acpi_ec *ec;
1da177e4
LT
950
951 ACPI_FUNCTION_TRACE("acpi_ec_stop");
952
953 if (!device)
954 return_VALUE(-EINVAL);
955
956 ec = acpi_driver_data(device);
957
958 status = acpi_remove_address_space_handler(ec->handle,
959 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
960 if (ACPI_FAILURE(status))
961 return_VALUE(-ENODEV);
962
963 status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
964 if (ACPI_FAILURE(status))
965 return_VALUE(-ENODEV);
966
967 return_VALUE(0);
968}
969
970static acpi_status __init
971acpi_fake_ecdt_callback (
972 acpi_handle handle,
973 u32 Level,
974 void *context,
975 void **retval)
976{
977 acpi_status status;
978
979 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
980 acpi_ec_io_ports, ec_ecdt);
981 if (ACPI_FAILURE(status))
982 return status;
983 ec_ecdt->status_addr = ec_ecdt->command_addr;
984
985 ec_ecdt->uid = -1;
986 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
987
988 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
989 if (ACPI_FAILURE(status))
990 return status;
1da177e4
LT
991 ec_ecdt->global_lock = TRUE;
992 ec_ecdt->handle = handle;
993
994 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
995 (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
996 (u32) ec_ecdt->data_addr.address);
997
998 return AE_CTRL_TERMINATE;
999}
1000
1001/*
1002 * Some BIOS (such as some from Gateway laptops) access EC region very early
1003 * such as in BAT0._INI or EC._INI before an EC device is found and
1004 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1005 * required, but if EC regison is accessed early, it is required.
1006 * The routine tries to workaround the BIOS bug by pre-scan EC device
1007 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1008 * op region (since _REG isn't invoked yet). The assumption is true for
1009 * all systems found.
1010 */
1011static int __init
1012acpi_ec_fake_ecdt(void)
1013{
1014 acpi_status status;
1015 int ret = 0;
1016
1017 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1018
1019 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1020 if (!ec_ecdt) {
1021 ret = -ENOMEM;
1022 goto error;
1023 }
1024 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1025
1026 status = acpi_get_devices (ACPI_EC_HID,
1027 acpi_fake_ecdt_callback,
1028 NULL,
1029 NULL);
1030 if (ACPI_FAILURE(status)) {
1031 kfree(ec_ecdt);
1032 ec_ecdt = NULL;
1033 ret = -ENODEV;
1034 goto error;
1035 }
1036 return 0;
1037error:
1038 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1039 return ret;
1040}
1041
1042static int __init
1043acpi_ec_get_real_ecdt(void)
1044{
1045 acpi_status status;
1046 struct acpi_table_ecdt *ecdt_ptr;
1047
451566f4 1048 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1da177e4
LT
1049 (struct acpi_table_header **) &ecdt_ptr);
1050 if (ACPI_FAILURE(status))
1051 return -ENODEV;
1052
1053 printk(KERN_INFO PREFIX "Found ECDT\n");
1054
1055 /*
1056 * Generate a temporary ec context to use until the namespace is scanned
1057 */
1058 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1059 if (!ec_ecdt)
1060 return -ENOMEM;
1061 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1062
451566f4
DT
1063 init_MUTEX(&ec_ecdt->sem);
1064 init_waitqueue_head(&ec_ecdt->wait);
1da177e4
LT
1065 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1066 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1067 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1068 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
1da177e4
LT
1069 /* use the GL just to be safe */
1070 ec_ecdt->global_lock = TRUE;
1071 ec_ecdt->uid = ecdt_ptr->uid;
1072
1073 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
1074 if (ACPI_FAILURE(status)) {
1075 goto error;
1076 }
1077
1078 return 0;
1079error:
1080 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1081 kfree(ec_ecdt);
1082 ec_ecdt = NULL;
1083
1084 return -ENODEV;
1085}
1086
1087static int __initdata acpi_fake_ecdt_enabled;
1088int __init
1089acpi_ec_ecdt_probe (void)
1090{
1091 acpi_status status;
1092 int ret;
1093
1094 ret = acpi_ec_get_real_ecdt();
1095 /* Try to make a fake ECDT */
1096 if (ret && acpi_fake_ecdt_enabled) {
1097 ret = acpi_ec_fake_ecdt();
1098 }
1099
1100 if (ret)
1101 return 0;
1102
1103 /*
1104 * Install GPE handler
1105 */
1106 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1107 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1108 ec_ecdt);
1109 if (ACPI_FAILURE(status)) {
1110 goto error;
1111 }
1112 acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1113 acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1114
1115 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1116 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1117 &acpi_ec_space_setup, ec_ecdt);
1118 if (ACPI_FAILURE(status)) {
1119 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1120 &acpi_ec_gpe_handler);
1121 goto error;
1122 }
1123
1124 return 0;
1125
1126error:
1127 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1128 kfree(ec_ecdt);
1129 ec_ecdt = NULL;
1130
1131 return -ENODEV;
1132}
1133
1134
1135static int __init acpi_ec_init (void)
1136{
451566f4 1137 int result;
1da177e4
LT
1138
1139 ACPI_FUNCTION_TRACE("acpi_ec_init");
1140
1141 if (acpi_disabled)
1142 return_VALUE(0);
1143
1144 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1145 if (!acpi_ec_dir)
1146 return_VALUE(-ENODEV);
1147
1148 /* Now register the driver for the EC */
1149 result = acpi_bus_register_driver(&acpi_ec_driver);
1150 if (result < 0) {
1151 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1152 return_VALUE(-ENODEV);
1153 }
1154
1155 return_VALUE(result);
1156}
1157
1158subsys_initcall(acpi_ec_init);
1159
1160/* EC driver currently not unloadable */
1161#if 0
1162static void __exit
1163acpi_ec_exit (void)
1164{
1165 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1166
1167 acpi_bus_unregister_driver(&acpi_ec_driver);
1168
1169 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1170
1171 return_VOID;
1172}
1173#endif /* 0 */
1174
1175static int __init acpi_fake_ecdt_setup(char *str)
1176{
1177 acpi_fake_ecdt_enabled = 1;
1178 return 0;
1179}
1180__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);