]> err.no Git - linux-2.6/blob - drivers/acpi/ec.c
ACPI: EC: Unify poll and interrupt gpe handlers
[linux-2.6] / drivers / acpi / ec.c
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>
34 #include <linux/interrupt.h>
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
41 ACPI_MODULE_NAME("acpi_ec")
42 #define ACPI_EC_COMPONENT               0x00100000
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_HID                     "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME             "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
47 #define ACPI_EC_FILE_INFO               "info"
48
49 /* EC status register */
50 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
51 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
52 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
53 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
54
55 /* EC commands */
56 #define ACPI_EC_COMMAND_READ    0x80
57 #define ACPI_EC_COMMAND_WRITE   0x81
58 #define ACPI_EC_BURST_ENABLE    0x82
59 #define ACPI_EC_BURST_DISABLE   0x83
60 #define ACPI_EC_COMMAND_QUERY   0x84
61
62 /* EC events */
63 enum {
64         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
65         ACPI_EC_EVENT_IBF_0,            /* Input buffer empty */
66 };
67
68 #define ACPI_EC_DELAY           50      /* Wait 50ms max. during EC ops */
69 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
70 #define ACPI_EC_UDELAY         100      /* Poll @ 100us increments */
71 #define ACPI_EC_UDELAY_COUNT   1000     /* Wait 10ms max. during EC ops */
72
73 enum {
74         EC_INTR = 1,    /* Output buffer full */
75         EC_POLL,                /* Input buffer empty */
76 };
77
78 static int acpi_ec_remove(struct acpi_device *device, int type);
79 static int acpi_ec_start(struct acpi_device *device);
80 static int acpi_ec_stop(struct acpi_device *device, int type);
81 static int acpi_ec_add(struct acpi_device *device);
82
83 static struct acpi_driver acpi_ec_driver = {
84         .name = ACPI_EC_DRIVER_NAME,
85         .class = ACPI_EC_CLASS,
86         .ids = ACPI_EC_HID,
87         .ops = {
88                 .add = acpi_ec_add,
89                 .remove = acpi_ec_remove,
90                 .start = acpi_ec_start,
91                 .stop = acpi_ec_stop,
92                 },
93 };
94 struct acpi_ec {
95         acpi_handle handle;
96         unsigned long uid;
97         unsigned long gpe_bit;
98         struct acpi_generic_address status_addr;
99         struct acpi_generic_address command_addr;
100         struct acpi_generic_address data_addr;
101         unsigned long global_lock;
102         struct semaphore sem;
103         unsigned int expect_event;
104         atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105         wait_queue_head_t wait;
106 };
107
108 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
109 static struct acpi_ec *ec_ecdt;
110
111 /* External interfaces use first EC only, so remember */
112 static struct acpi_device *first_ec;
113 static int acpi_ec_mode = EC_INTR;
114
115 /* --------------------------------------------------------------------------
116                              Transaction Management
117    -------------------------------------------------------------------------- */
118
119 static u32 acpi_ec_read_status(struct acpi_ec *ec)
120 {
121         u32 status = 0;
122
123         acpi_hw_low_level_read(8, &status, &ec->status_addr);
124         return status;
125 }
126
127 static u32 acpi_ec_read_data(struct acpi_ec *ec)
128 {
129         u32 data = 0;
130
131         acpi_hw_low_level_read(8, &data, &ec->data_addr);
132         return data;
133 }
134
135 static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
136 {
137         acpi_hw_low_level_write(8, command, &ec->command_addr);
138 }
139
140 static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
141 {
142         acpi_hw_low_level_write(8, data, &ec->data_addr);
143 }
144
145 static int acpi_ec_check_status(u32 status, u8 event) {
146
147         switch (event) {
148         case ACPI_EC_EVENT_OBF_1:
149                 if (status & ACPI_EC_FLAG_OBF)
150                         return 1;
151                 break;
152         case ACPI_EC_EVENT_IBF_0:
153                 if (!(status & ACPI_EC_FLAG_IBF))
154                         return 1;
155                 break;
156         default:
157                 break;
158         }
159
160         return 0;
161 }
162
163 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
164 {
165         int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
166         long time_left;
167
168         ec->expect_event = event;
169         if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
170                 ec->expect_event = 0;
171                 return 0;
172         }
173
174         do {
175                 if (acpi_ec_mode == EC_POLL) {
176                         udelay(ACPI_EC_UDELAY);
177                 } else {
178                         time_left = wait_event_timeout(ec->wait,
179                                     !ec->expect_event,
180                                     msecs_to_jiffies(ACPI_EC_DELAY));
181                         if (time_left > 0) {
182                                 ec->expect_event = 0;
183                                 return 0;
184                         }
185                 }
186                 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
187                         ec->expect_event = 0;
188                         return 0;
189                 }
190         } while (--i > 0);
191
192         ec->expect_event = 0;
193
194         return -ETIME;
195 }
196
197 #ifdef ACPI_FUTURE_USAGE
198 /*
199  * Note: samsung nv5000 doesn't work with ec burst mode.
200  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
201  */
202 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
203 {
204         u32 tmp = 0;
205         u32 status = 0;
206
207
208         status = acpi_ec_read_status(ec);
209         if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
210                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
211                 if (status)
212                         goto end;
213                 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
214                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
215                 tmp = acpi_ec_read_data(ec);
216                 if (tmp != 0x90) {      /* Burst ACK byte */
217                         return -EINVAL;
218                 }
219         }
220
221         atomic_set(&ec->leaving_burst, 0);
222         return 0;
223   end:
224         ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
225         return -1;
226 }
227
228 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
229 {
230         u32 status = 0;
231
232
233         status = acpi_ec_read_status(ec);
234         if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
235                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
236                 if(status)
237                         goto end;
238                 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
239                 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
240         }
241         atomic_set(&ec->leaving_burst, 1);
242         return 0;
243   end:
244         ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
245         return -1;
246 }
247 #endif /* ACPI_FUTURE_USAGE */
248
249 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
250                                         const u8 *wdata, unsigned wdata_len,
251                                         u8 *rdata, unsigned rdata_len)
252 {
253         int result;
254
255         acpi_ec_write_cmd(ec, command);
256
257         for (; wdata_len > 0; wdata_len --) {
258                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
259                 if (result)
260                         return result;
261                 acpi_ec_write_data(ec, *(wdata++));
262         }
263
264         if (command == ACPI_EC_COMMAND_WRITE) {
265                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
266                 if (result)
267                         return result;
268         }
269
270         for (; rdata_len > 0; rdata_len --) {
271                 u32 d;
272
273                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
274                 if (result)
275                         return result;
276
277                 d = acpi_ec_read_data(ec);
278                 *(rdata++) = (u8) d;
279         }
280
281         return 0;
282 }
283
284 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
285                                 const u8 *wdata, unsigned wdata_len,
286                                 u8 *rdata, unsigned rdata_len)
287 {
288         int status;
289         u32 glk;
290
291         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
292                 return -EINVAL;
293
294         if (rdata)
295                 memset(rdata, 0, rdata_len);
296
297         if (ec->global_lock) {
298                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
299                 if (ACPI_FAILURE(status))
300                         return -ENODEV;
301         }
302         down(&ec->sem);
303
304         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
305         if (status) {
306                 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
307                 goto end;
308         }
309
310         status = acpi_ec_transaction_unlocked(ec, command,
311                                               wdata, wdata_len,
312                                               rdata, rdata_len);
313
314 end:
315         up(&ec->sem);
316
317         if (ec->global_lock)
318                 acpi_release_global_lock(glk);
319
320         return status;
321 }
322
323 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
324 {
325         int result;
326         u8 d;
327
328         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
329                                      &address, 1, &d, 1);
330         *data = d;
331         return result;
332 }
333 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
334 {
335         u8 wdata[2] = { address, data };
336         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
337                                    wdata, 2, NULL, 0);
338 }
339
340 /*
341  * Externally callable EC access functions. For now, assume 1 EC only
342  */
343 int ec_read(u8 addr, u8 * val)
344 {
345         struct acpi_ec *ec;
346         int err;
347         u32 temp_data;
348
349         if (!first_ec)
350                 return -ENODEV;
351
352         ec = acpi_driver_data(first_ec);
353
354         err = acpi_ec_read(ec, addr, &temp_data);
355
356         if (!err) {
357                 *val = temp_data;
358                 return 0;
359         } else
360                 return err;
361 }
362
363 EXPORT_SYMBOL(ec_read);
364
365 int ec_write(u8 addr, u8 val)
366 {
367         struct acpi_ec *ec;
368         int err;
369
370         if (!first_ec)
371                 return -ENODEV;
372
373         ec = acpi_driver_data(first_ec);
374
375         err = acpi_ec_write(ec, addr, val);
376
377         return err;
378 }
379
380 EXPORT_SYMBOL(ec_write);
381
382 extern int ec_transaction(u8 command,
383                           const u8 *wdata, unsigned wdata_len,
384                           u8 *rdata, unsigned rdata_len)
385 {
386         struct acpi_ec *ec;
387
388         if (!first_ec)
389                 return -ENODEV;
390
391         ec = acpi_driver_data(first_ec);
392
393         return acpi_ec_transaction(ec, command, wdata,
394                                    wdata_len, rdata, rdata_len);
395 }
396
397 static int acpi_ec_query(struct acpi_ec *ec, u32 * data)
398 {
399         int result;
400         u8 d;
401
402         if (!ec || !data)
403                 return -EINVAL;
404
405         /*
406          * Query the EC to find out which _Qxx method we need to evaluate.
407          * Note that successful completion of the query causes the ACPI_EC_SCI
408          * bit to be cleared (and thus clearing the interrupt source).
409          */
410
411         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
412         if (result)
413                 return result;
414
415         if (!d)
416                 return -ENODATA;
417
418         *data = d;
419         return 0;
420 }
421
422 /* --------------------------------------------------------------------------
423                                 Event Management
424    -------------------------------------------------------------------------- */
425
426 struct acpi_ec_query_data {
427         acpi_handle handle;
428         u8 data;
429 };
430
431 static void acpi_ec_gpe_query(void *ec_cxt)
432 {
433         struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
434         u32 value = 0;
435         static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
436         const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
437                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
438         };
439
440
441         if (!ec_cxt)
442                 goto end;
443
444         value = acpi_ec_read_status(ec);
445
446         if (!(value & ACPI_EC_FLAG_SCI))
447                 goto end;
448
449         if (acpi_ec_query(ec, &value))
450                 goto end;
451
452         object_name[2] = hex[((value >> 4) & 0x0F)];
453         object_name[3] = hex[(value & 0x0F)];
454
455         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
456
457         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
458
459       end:
460         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
461 }
462
463 static u32 acpi_ec_gpe_handler(void *data)
464 {
465         acpi_status status = AE_OK;
466         u32 value;
467         u8 exec_mode;
468         struct acpi_ec *ec = (struct acpi_ec *)data;
469
470         acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
471         value = acpi_ec_read_status(ec);
472
473         if (acpi_ec_mode == EC_INTR) {
474                 if (acpi_ec_check_status(value, ec->expect_event)) {
475                         ec->expect_event = 0;
476                         wake_up(&ec->wait);
477                 }
478                 exec_mode = OSL_EC_BURST_HANDLER;
479         } else {
480                 exec_mode = OSL_EC_POLL_HANDLER;
481         }
482
483         if (value & ACPI_EC_FLAG_SCI) {
484                 status = acpi_os_execute(exec_mode, acpi_ec_gpe_query, ec);
485                 return status == AE_OK ?
486                     ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
487         }
488         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
489         return status == AE_OK ?
490             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
491 }
492
493 /* --------------------------------------------------------------------------
494                              Address Space Management
495    -------------------------------------------------------------------------- */
496
497 static acpi_status
498 acpi_ec_space_setup(acpi_handle region_handle,
499                     u32 function, void *handler_context, void **return_context)
500 {
501         /*
502          * The EC object is in the handler context and is needed
503          * when calling the acpi_ec_space_handler.
504          */
505         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
506             handler_context : NULL;
507
508         return AE_OK;
509 }
510
511 static acpi_status
512 acpi_ec_space_handler(u32 function,
513                       acpi_physical_address address,
514                       u32 bit_width,
515                       acpi_integer * value,
516                       void *handler_context, void *region_context)
517 {
518         int result = 0;
519         struct acpi_ec *ec = NULL;
520         u64 temp = *value;
521         acpi_integer f_v = 0;
522         int i = 0;
523
524
525         if ((address > 0xFF) || !value || !handler_context)
526                 return AE_BAD_PARAMETER;
527
528         if (bit_width != 8 && acpi_strict) {
529                 return AE_BAD_PARAMETER;
530         }
531
532         ec = (struct acpi_ec *)handler_context;
533
534       next_byte:
535         switch (function) {
536         case ACPI_READ:
537                 temp = 0;
538                 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
539                 break;
540         case ACPI_WRITE:
541                 result = acpi_ec_write(ec, (u8) address, (u8) temp);
542                 break;
543         default:
544                 result = -EINVAL;
545                 goto out;
546                 break;
547         }
548
549         bit_width -= 8;
550         if (bit_width) {
551                 if (function == ACPI_READ)
552                         f_v |= temp << 8 * i;
553                 if (function == ACPI_WRITE)
554                         temp >>= 8;
555                 i++;
556                 address++;
557                 goto next_byte;
558         }
559
560         if (function == ACPI_READ) {
561                 f_v |= temp << 8 * i;
562                 *value = f_v;
563         }
564
565       out:
566         switch (result) {
567         case -EINVAL:
568                 return AE_BAD_PARAMETER;
569                 break;
570         case -ENODEV:
571                 return AE_NOT_FOUND;
572                 break;
573         case -ETIME:
574                 return AE_TIME;
575                 break;
576         default:
577                 return AE_OK;
578         }
579 }
580
581 /* --------------------------------------------------------------------------
582                               FS Interface (/proc)
583    -------------------------------------------------------------------------- */
584
585 static struct proc_dir_entry *acpi_ec_dir;
586
587 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
588 {
589         struct acpi_ec *ec = (struct acpi_ec *)seq->private;
590
591
592         if (!ec)
593                 goto end;
594
595         seq_printf(seq, "gpe bit:                 0x%02x\n",
596                    (u32) ec->gpe_bit);
597         seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
598                    (u32) ec->status_addr.address,
599                    (u32) ec->data_addr.address);
600         seq_printf(seq, "use global lock:         %s\n",
601                    ec->global_lock ? "yes" : "no");
602         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
603
604       end:
605         return 0;
606 }
607
608 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
609 {
610         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
611 }
612
613 static struct file_operations acpi_ec_info_ops = {
614         .open = acpi_ec_info_open_fs,
615         .read = seq_read,
616         .llseek = seq_lseek,
617         .release = single_release,
618         .owner = THIS_MODULE,
619 };
620
621 static int acpi_ec_add_fs(struct acpi_device *device)
622 {
623         struct proc_dir_entry *entry = NULL;
624
625
626         if (!acpi_device_dir(device)) {
627                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
628                                                      acpi_ec_dir);
629                 if (!acpi_device_dir(device))
630                         return -ENODEV;
631         }
632
633         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
634                                   acpi_device_dir(device));
635         if (!entry)
636                 return -ENODEV;
637         else {
638                 entry->proc_fops = &acpi_ec_info_ops;
639                 entry->data = acpi_driver_data(device);
640                 entry->owner = THIS_MODULE;
641         }
642
643         return 0;
644 }
645
646 static int acpi_ec_remove_fs(struct acpi_device *device)
647 {
648
649         if (acpi_device_dir(device)) {
650                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
651                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
652                 acpi_device_dir(device) = NULL;
653         }
654
655         return 0;
656 }
657
658 /* --------------------------------------------------------------------------
659                                Driver Interface
660    -------------------------------------------------------------------------- */
661
662 static int acpi_ec_add(struct acpi_device *device)
663 {
664         int result = 0;
665         acpi_status status = AE_OK;
666         struct acpi_ec *ec = NULL;
667
668
669         if (!device)
670                 return -EINVAL;
671
672         ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
673         if (!ec)
674                 return -ENOMEM;
675         memset(ec, 0, sizeof(struct acpi_ec));
676
677         ec->handle = device->handle;
678         ec->uid = -1;
679         init_MUTEX(&ec->sem);
680         if (acpi_ec_mode == EC_INTR) {
681                 atomic_set(&ec->leaving_burst, 1);
682                 init_waitqueue_head(&ec->wait);
683         }
684         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
685         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
686         acpi_driver_data(device) = ec;
687
688         /* Use the global lock for all EC transactions? */
689         acpi_evaluate_integer(ec->handle, "_GLK", NULL,
690                               &ec->global_lock);
691
692         /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
693            http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
694         if (ec_ecdt) {
695                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
696                                                   ACPI_ADR_SPACE_EC,
697                                                   &acpi_ec_space_handler);
698
699                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
700                                         &acpi_ec_gpe_handler);
701
702                 kfree(ec_ecdt);
703         }
704
705         /* Get GPE bit assignment (EC events). */
706         /* TODO: Add support for _GPE returning a package */
707         status =
708             acpi_evaluate_integer(ec->handle, "_GPE", NULL,
709                                   &ec->gpe_bit);
710         if (ACPI_FAILURE(status)) {
711                 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
712                 result = -ENODEV;
713                 goto end;
714         }
715
716         result = acpi_ec_add_fs(device);
717         if (result)
718                 goto end;
719
720         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
721                acpi_device_name(device), acpi_device_bid(device),
722                (u32) ec->gpe_bit));
723
724         if (!first_ec)
725                 first_ec = device;
726
727   end:
728         if (result)
729                 kfree(ec);
730
731         return result;
732 }
733
734 static int acpi_ec_remove(struct acpi_device *device, int type)
735 {
736         struct acpi_ec *ec = NULL;
737
738
739         if (!device)
740                 return -EINVAL;
741
742         ec = acpi_driver_data(device);
743
744         acpi_ec_remove_fs(device);
745
746         kfree(ec);
747
748         return 0;
749 }
750
751 static acpi_status
752 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
753 {
754         struct acpi_ec *ec = (struct acpi_ec *)context;
755         struct acpi_generic_address *addr;
756
757         if (resource->type != ACPI_RESOURCE_TYPE_IO) {
758                 return AE_OK;
759         }
760
761         /*
762          * The first address region returned is the data port, and
763          * the second address region returned is the status/command
764          * port.
765          */
766         if (ec->data_addr.register_bit_width == 0) {
767                 addr = &ec->data_addr;
768         } else if (ec->command_addr.register_bit_width == 0) {
769                 addr = &ec->command_addr;
770         } else {
771                 return AE_CTRL_TERMINATE;
772         }
773
774         addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
775         addr->register_bit_width = 8;
776         addr->register_bit_offset = 0;
777         addr->address = resource->data.io.minimum;
778
779         return AE_OK;
780 }
781
782 static int acpi_ec_start(struct acpi_device *device)
783 {
784         acpi_status status = AE_OK;
785         struct acpi_ec *ec = NULL;
786
787
788         if (!device)
789                 return -EINVAL;
790
791         ec = acpi_driver_data(device);
792
793         if (!ec)
794                 return -EINVAL;
795
796         /*
797          * Get I/O port addresses. Convert to GAS format.
798          */
799         status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
800                                      acpi_ec_io_ports, ec);
801         if (ACPI_FAILURE(status)
802             || ec->command_addr.register_bit_width == 0) {
803                 ACPI_EXCEPTION((AE_INFO, status,
804                                 "Error getting I/O port addresses"));
805                 return -ENODEV;
806         }
807
808         ec->status_addr = ec->command_addr;
809
810         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
811                           (u32) ec->gpe_bit,
812                           (u32) ec->command_addr.address,
813                           (u32) ec->data_addr.address));
814
815         /*
816          * Install GPE handler
817          */
818         status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
819                                           ACPI_GPE_EDGE_TRIGGERED,
820                                           &acpi_ec_gpe_handler, ec);
821         if (ACPI_FAILURE(status)) {
822                 return -ENODEV;
823         }
824         acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
825         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
826
827         status = acpi_install_address_space_handler(ec->handle,
828                                                     ACPI_ADR_SPACE_EC,
829                                                     &acpi_ec_space_handler,
830                                                     &acpi_ec_space_setup, ec);
831         if (ACPI_FAILURE(status)) {
832                 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
833                                         &acpi_ec_gpe_handler);
834                 return -ENODEV;
835         }
836
837         return AE_OK;
838 }
839
840 static int acpi_ec_stop(struct acpi_device *device, int type)
841 {
842         acpi_status status = AE_OK;
843         struct acpi_ec *ec = NULL;
844
845
846         if (!device)
847                 return -EINVAL;
848
849         ec = acpi_driver_data(device);
850
851         status = acpi_remove_address_space_handler(ec->handle,
852                                                    ACPI_ADR_SPACE_EC,
853                                                    &acpi_ec_space_handler);
854         if (ACPI_FAILURE(status))
855                 return -ENODEV;
856
857         status =
858             acpi_remove_gpe_handler(NULL, ec->gpe_bit,
859                                     &acpi_ec_gpe_handler);
860         if (ACPI_FAILURE(status))
861                 return -ENODEV;
862
863         return 0;
864 }
865
866 static acpi_status __init
867 acpi_fake_ecdt_callback(acpi_handle handle,
868                         u32 Level, void *context, void **retval)
869 {
870         acpi_status status;
871
872         init_MUTEX(&ec_ecdt->sem);
873         if (acpi_ec_mode == EC_INTR) {
874                 init_waitqueue_head(&ec_ecdt->wait);
875         }
876         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
877                                      acpi_ec_io_ports, ec_ecdt);
878         if (ACPI_FAILURE(status))
879                 return status;
880         ec_ecdt->status_addr = ec_ecdt->command_addr;
881
882         ec_ecdt->uid = -1;
883         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
884
885         status =
886             acpi_evaluate_integer(handle, "_GPE", NULL,
887                                   &ec_ecdt->gpe_bit);
888         if (ACPI_FAILURE(status))
889                 return status;
890         ec_ecdt->global_lock = TRUE;
891         ec_ecdt->handle = handle;
892
893         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
894                (u32) ec_ecdt->gpe_bit,
895                (u32) ec_ecdt->command_addr.address,
896                (u32) ec_ecdt->data_addr.address));
897
898         return AE_CTRL_TERMINATE;
899 }
900
901 /*
902  * Some BIOS (such as some from Gateway laptops) access EC region very early
903  * such as in BAT0._INI or EC._INI before an EC device is found and
904  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
905  * required, but if EC regison is accessed early, it is required.
906  * The routine tries to workaround the BIOS bug by pre-scan EC device
907  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
908  * op region (since _REG isn't invoked yet). The assumption is true for
909  * all systems found.
910  */
911 static int __init acpi_ec_fake_ecdt(void)
912 {
913         acpi_status status;
914         int ret = 0;
915
916         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
917
918         ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
919         if (!ec_ecdt) {
920                 ret = -ENOMEM;
921                 goto error;
922         }
923         memset(ec_ecdt, 0, sizeof(struct acpi_ec));
924
925         status = acpi_get_devices(ACPI_EC_HID,
926                                   acpi_fake_ecdt_callback, NULL, NULL);
927         if (ACPI_FAILURE(status)) {
928                 kfree(ec_ecdt);
929                 ec_ecdt = NULL;
930                 ret = -ENODEV;
931                 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
932                 goto error;
933         }
934         return 0;
935   error:
936         return ret;
937 }
938
939 static int __init acpi_ec_get_real_ecdt(void)
940 {
941         acpi_status status;
942         struct acpi_table_ecdt *ecdt_ptr;
943
944         status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
945                                          (struct acpi_table_header **)
946                                          &ecdt_ptr);
947         if (ACPI_FAILURE(status))
948                 return -ENODEV;
949
950         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
951
952         /*
953          * Generate a temporary ec context to use until the namespace is scanned
954          */
955         ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
956         if (!ec_ecdt)
957                 return -ENOMEM;
958         memset(ec_ecdt, 0, sizeof(struct acpi_ec));
959
960         init_MUTEX(&ec_ecdt->sem);
961         if (acpi_ec_mode == EC_INTR) {
962                 init_waitqueue_head(&ec_ecdt->wait);
963         }
964         ec_ecdt->command_addr = ecdt_ptr->ec_control;
965         ec_ecdt->status_addr = ecdt_ptr->ec_control;
966         ec_ecdt->data_addr = ecdt_ptr->ec_data;
967         ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
968         /* use the GL just to be safe */
969         ec_ecdt->global_lock = TRUE;
970         ec_ecdt->uid = ecdt_ptr->uid;
971
972         status =
973             acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
974         if (ACPI_FAILURE(status)) {
975                 goto error;
976         }
977
978         return 0;
979   error:
980         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
981         kfree(ec_ecdt);
982         ec_ecdt = NULL;
983
984         return -ENODEV;
985 }
986
987 static int __initdata acpi_fake_ecdt_enabled;
988 int __init acpi_ec_ecdt_probe(void)
989 {
990         acpi_status status;
991         int ret;
992
993         ret = acpi_ec_get_real_ecdt();
994         /* Try to make a fake ECDT */
995         if (ret && acpi_fake_ecdt_enabled) {
996                 ret = acpi_ec_fake_ecdt();
997         }
998
999         if (ret)
1000                 return 0;
1001
1002         /*
1003          * Install GPE handler
1004          */
1005         status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1006                                           ACPI_GPE_EDGE_TRIGGERED,
1007                                           &acpi_ec_gpe_handler, ec_ecdt);
1008         if (ACPI_FAILURE(status)) {
1009                 goto error;
1010         }
1011         acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1012         acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1013
1014         status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1015                                                     ACPI_ADR_SPACE_EC,
1016                                                     &acpi_ec_space_handler,
1017                                                     &acpi_ec_space_setup,
1018                                                     ec_ecdt);
1019         if (ACPI_FAILURE(status)) {
1020                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1021                                         &acpi_ec_gpe_handler);
1022                 goto error;
1023         }
1024
1025         return 0;
1026
1027       error:
1028         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1029         kfree(ec_ecdt);
1030         ec_ecdt = NULL;
1031
1032         return -ENODEV;
1033 }
1034
1035 static int __init acpi_ec_init(void)
1036 {
1037         int result = 0;
1038
1039
1040         if (acpi_disabled)
1041                 return 0;
1042
1043         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1044         if (!acpi_ec_dir)
1045                 return -ENODEV;
1046
1047         /* Now register the driver for the EC */
1048         result = acpi_bus_register_driver(&acpi_ec_driver);
1049         if (result < 0) {
1050                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1051                 return -ENODEV;
1052         }
1053
1054         return result;
1055 }
1056
1057 subsys_initcall(acpi_ec_init);
1058
1059 /* EC driver currently not unloadable */
1060 #if 0
1061 static void __exit acpi_ec_exit(void)
1062 {
1063
1064         acpi_bus_unregister_driver(&acpi_ec_driver);
1065
1066         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1067
1068         return;
1069 }
1070 #endif                          /* 0 */
1071
1072 static int __init acpi_fake_ecdt_setup(char *str)
1073 {
1074         acpi_fake_ecdt_enabled = 1;
1075         return 1;
1076 }
1077
1078 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1079 static int __init acpi_ec_set_intr_mode(char *str)
1080 {
1081         int intr;
1082
1083         if (!get_option(&str, &intr))
1084                 return 0;
1085
1086         if (intr) {
1087                 acpi_ec_mode = EC_INTR;
1088         } else {
1089                 acpi_ec_mode = EC_POLL;
1090         }
1091         acpi_ec_driver.ops.add = acpi_ec_add;
1092         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1093
1094         return 1;
1095 }
1096
1097 __setup("ec_intr=", acpi_ec_set_intr_mode);