ACPI: EC: Detect irq storm
[safe/jmp/linux-2.6] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
3  *
4  *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5  *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or (at
15  *  your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28
29 /* Uncomment next line to get verbose print outs*/
30 /* #define DEBUG */
31
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <acpi/actypes.h>
45
46 #define ACPI_EC_CLASS                   "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
48 #define ACPI_EC_FILE_INFO               "info"
49
50 #undef PREFIX
51 #define PREFIX                          "ACPI: EC: "
52
53 /* EC status register */
54 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
55 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
56 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
57 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
58
59 /* EC commands */
60 enum ec_command {
61         ACPI_EC_COMMAND_READ = 0x80,
62         ACPI_EC_COMMAND_WRITE = 0x81,
63         ACPI_EC_BURST_ENABLE = 0x82,
64         ACPI_EC_BURST_DISABLE = 0x83,
65         ACPI_EC_COMMAND_QUERY = 0x84,
66 };
67
68 /* EC events */
69 enum ec_event {
70         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
71         ACPI_EC_EVENT_IBF_0,            /* Input buffer empty */
72 };
73
74 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
75 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
76 #define ACPI_EC_UDELAY          100     /* Wait 100us before polling EC again */
77
78 enum {
79         EC_FLAGS_WAIT_GPE = 0,          /* Don't check status until GPE arrives */
80         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
81         EC_FLAGS_GPE_MODE,              /* Expect GPE to be sent for status change */
82         EC_FLAGS_NO_GPE,                /* Don't use GPE mode */
83         EC_FLAGS_RESCHEDULE_POLL        /* Re-schedule poll */
84 };
85
86 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
87 /* External interfaces use first EC only, so remember */
88 typedef int (*acpi_ec_query_func) (void *data);
89
90 struct acpi_ec_query_handler {
91         struct list_head node;
92         acpi_ec_query_func func;
93         acpi_handle handle;
94         void *data;
95         u8 query_bit;
96 };
97
98 static struct acpi_ec {
99         acpi_handle handle;
100         unsigned long gpe;
101         unsigned long command_addr;
102         unsigned long data_addr;
103         unsigned long global_lock;
104         unsigned long flags;
105         struct mutex lock;
106         wait_queue_head_t wait;
107         struct list_head list;
108         struct delayed_work work;
109         atomic_t irq_count;
110         u8 handlers_installed;
111 } *boot_ec, *first_ec;
112
113 /* --------------------------------------------------------------------------
114                              Transaction Management
115    -------------------------------------------------------------------------- */
116
117 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
118 {
119         u8 x = inb(ec->command_addr);
120         pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
121         return x;
122 }
123
124 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
125 {
126         u8 x = inb(ec->data_addr);
127         pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
128         return inb(ec->data_addr);
129 }
130
131 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
132 {
133         pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
134         outb(command, ec->command_addr);
135 }
136
137 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
138 {
139         pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
140         outb(data, ec->data_addr);
141 }
142
143 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
144 {
145         if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
146                 return 0;
147         if (event == ACPI_EC_EVENT_OBF_1) {
148                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
149                         return 1;
150         } else if (event == ACPI_EC_EVENT_IBF_0) {
151                 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
152                         return 1;
153         }
154
155         return 0;
156 }
157
158 static void ec_schedule_ec_poll(struct acpi_ec *ec)
159 {
160         if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
161                 schedule_delayed_work(&ec->work,
162                                       msecs_to_jiffies(ACPI_EC_DELAY));
163 }
164
165 static void ec_switch_to_poll_mode(struct acpi_ec *ec)
166 {
167         set_bit(EC_FLAGS_NO_GPE, &ec->flags);
168         clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
169         acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
170         set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
171 }
172
173 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
174 {
175         atomic_set(&ec->irq_count, 0);
176         if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
177             likely(!force_poll)) {
178                 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
179                                        msecs_to_jiffies(ACPI_EC_DELAY)))
180                         return 0;
181                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
182                 if (acpi_ec_check_status(ec, event)) {
183                         /* missing GPEs, switch back to poll mode */
184                         if (printk_ratelimit())
185                                 pr_info(PREFIX "missing confirmations, "
186                                                 "switch off interrupt mode.\n");
187                         ec_switch_to_poll_mode(ec);
188                         ec_schedule_ec_poll(ec);
189                         return 0;
190                 }
191         } else {
192                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
193                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
194                 while (time_before(jiffies, delay)) {
195                         if (acpi_ec_check_status(ec, event))
196                                 return 0;
197                         udelay(ACPI_EC_UDELAY);
198                 }
199         }
200         pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
201                 acpi_ec_read_status(ec),
202                 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
203         return -ETIME;
204 }
205
206 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
207                                         const u8 * wdata, unsigned wdata_len,
208                                         u8 * rdata, unsigned rdata_len,
209                                         int force_poll)
210 {
211         int result = 0;
212         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
213         pr_debug(PREFIX "transaction start\n");
214         acpi_ec_write_cmd(ec, command);
215         for (; wdata_len > 0; --wdata_len) {
216                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
217                 if (result) {
218                         pr_err(PREFIX
219                                "write_cmd timeout, command = %d\n", command);
220                         goto end;
221                 }
222                 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
223                 acpi_ec_write_data(ec, *(wdata++));
224         }
225
226         if (!rdata_len) {
227                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
228                 if (result) {
229                         pr_err(PREFIX
230                                "finish-write timeout, command = %d\n", command);
231                         goto end;
232                 }
233         } else if (command == ACPI_EC_COMMAND_QUERY)
234                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
235
236         for (; rdata_len > 0; --rdata_len) {
237                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
238                 if (result) {
239                         pr_err(PREFIX "read timeout, command = %d\n", command);
240                         goto end;
241                 }
242                 /* Don't expect GPE after last read */
243                 if (rdata_len > 1)
244                         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
245                 *(rdata++) = acpi_ec_read_data(ec);
246         }
247       end:
248         pr_debug(PREFIX "transaction end\n");
249         return result;
250 }
251
252 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
253                                const u8 * wdata, unsigned wdata_len,
254                                u8 * rdata, unsigned rdata_len,
255                                int force_poll)
256 {
257         int status;
258         u32 glk;
259
260         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
261                 return -EINVAL;
262
263         if (rdata)
264                 memset(rdata, 0, rdata_len);
265
266         mutex_lock(&ec->lock);
267         if (ec->global_lock) {
268                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
269                 if (ACPI_FAILURE(status)) {
270                         mutex_unlock(&ec->lock);
271                         return -ENODEV;
272                 }
273         }
274
275         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
276         if (status) {
277                 pr_err(PREFIX "input buffer is not empty, "
278                                 "aborting transaction\n");
279                 goto end;
280         }
281
282         status = acpi_ec_transaction_unlocked(ec, command,
283                                               wdata, wdata_len,
284                                               rdata, rdata_len,
285                                               force_poll);
286
287       end:
288
289         if (ec->global_lock)
290                 acpi_release_global_lock(glk);
291         mutex_unlock(&ec->lock);
292
293         return status;
294 }
295
296 /*
297  * Note: samsung nv5000 doesn't work with ec burst mode.
298  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
299  */
300 int acpi_ec_burst_enable(struct acpi_ec *ec)
301 {
302         u8 d;
303         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
304 }
305
306 int acpi_ec_burst_disable(struct acpi_ec *ec)
307 {
308         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
309 }
310
311 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
312 {
313         int result;
314         u8 d;
315
316         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
317                                      &address, 1, &d, 1, 0);
318         *data = d;
319         return result;
320 }
321
322 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
323 {
324         u8 wdata[2] = { address, data };
325         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
326                                    wdata, 2, NULL, 0, 0);
327 }
328
329 /*
330  * Externally callable EC access functions. For now, assume 1 EC only
331  */
332 int ec_burst_enable(void)
333 {
334         if (!first_ec)
335                 return -ENODEV;
336         return acpi_ec_burst_enable(first_ec);
337 }
338
339 EXPORT_SYMBOL(ec_burst_enable);
340
341 int ec_burst_disable(void)
342 {
343         if (!first_ec)
344                 return -ENODEV;
345         return acpi_ec_burst_disable(first_ec);
346 }
347
348 EXPORT_SYMBOL(ec_burst_disable);
349
350 int ec_read(u8 addr, u8 * val)
351 {
352         int err;
353         u8 temp_data;
354
355         if (!first_ec)
356                 return -ENODEV;
357
358         err = acpi_ec_read(first_ec, addr, &temp_data);
359
360         if (!err) {
361                 *val = temp_data;
362                 return 0;
363         } else
364                 return err;
365 }
366
367 EXPORT_SYMBOL(ec_read);
368
369 int ec_write(u8 addr, u8 val)
370 {
371         int err;
372
373         if (!first_ec)
374                 return -ENODEV;
375
376         err = acpi_ec_write(first_ec, addr, val);
377
378         return err;
379 }
380
381 EXPORT_SYMBOL(ec_write);
382
383 int ec_transaction(u8 command,
384                    const u8 * wdata, unsigned wdata_len,
385                    u8 * rdata, unsigned rdata_len,
386                    int force_poll)
387 {
388         if (!first_ec)
389                 return -ENODEV;
390
391         return acpi_ec_transaction(first_ec, command, wdata,
392                                    wdata_len, rdata, rdata_len,
393                                    force_poll);
394 }
395
396 EXPORT_SYMBOL(ec_transaction);
397
398 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
399 {
400         int result;
401         u8 d;
402
403         if (!ec || !data)
404                 return -EINVAL;
405
406         /*
407          * Query the EC to find out which _Qxx method we need to evaluate.
408          * Note that successful completion of the query causes the ACPI_EC_SCI
409          * bit to be cleared (and thus clearing the interrupt source).
410          */
411
412         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
413         if (result)
414                 return result;
415
416         if (!d)
417                 return -ENODATA;
418
419         *data = d;
420         return 0;
421 }
422
423 /* --------------------------------------------------------------------------
424                                 Event Management
425    -------------------------------------------------------------------------- */
426 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
427                               acpi_handle handle, acpi_ec_query_func func,
428                               void *data)
429 {
430         struct acpi_ec_query_handler *handler =
431             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
432         if (!handler)
433                 return -ENOMEM;
434
435         handler->query_bit = query_bit;
436         handler->handle = handle;
437         handler->func = func;
438         handler->data = data;
439         mutex_lock(&ec->lock);
440         list_add(&handler->node, &ec->list);
441         mutex_unlock(&ec->lock);
442         return 0;
443 }
444
445 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
446
447 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
448 {
449         struct acpi_ec_query_handler *handler, *tmp;
450         mutex_lock(&ec->lock);
451         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
452                 if (query_bit == handler->query_bit) {
453                         list_del(&handler->node);
454                         kfree(handler);
455                 }
456         }
457         mutex_unlock(&ec->lock);
458 }
459
460 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
461
462 static void acpi_ec_gpe_query(void *ec_cxt)
463 {
464         struct acpi_ec *ec = ec_cxt;
465         u8 value = 0;
466         struct acpi_ec_query_handler *handler, copy;
467
468         if (!ec || acpi_ec_query(ec, &value))
469                 return;
470         mutex_lock(&ec->lock);
471         list_for_each_entry(handler, &ec->list, node) {
472                 if (value == handler->query_bit) {
473                         /* have custom handler for this bit */
474                         memcpy(&copy, handler, sizeof(copy));
475                         mutex_unlock(&ec->lock);
476                         if (copy.func) {
477                                 copy.func(copy.data);
478                         } else if (copy.handle) {
479                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
480                         }
481                         return;
482                 }
483         }
484         mutex_unlock(&ec->lock);
485 }
486
487 static u32 acpi_ec_gpe_handler(void *data)
488 {
489         acpi_status status = AE_OK;
490         struct acpi_ec *ec = data;
491         u8 state = acpi_ec_read_status(ec);
492
493         pr_debug(PREFIX "~~~> interrupt\n");
494         atomic_inc(&ec->irq_count);
495         if (atomic_read(&ec->irq_count) > 5) {
496                 pr_err(PREFIX "GPE storm detected, disabling EC GPE\n");
497                 ec_switch_to_poll_mode(ec);
498                 goto end;
499         }
500         clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
501         if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
502                 wake_up(&ec->wait);
503
504         if (state & ACPI_EC_FLAG_SCI) {
505                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
506                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
507                                 acpi_ec_gpe_query, ec);
508         } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
509                    !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
510                    in_interrupt()) {
511                 /* this is non-query, must be confirmation */
512                 if (printk_ratelimit())
513                         pr_info(PREFIX "non-query interrupt received,"
514                                 " switching to interrupt mode\n");
515                 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
516                 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
517         }
518 end:
519         ec_schedule_ec_poll(ec);
520         return ACPI_SUCCESS(status) ?
521             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
522 }
523
524 static void do_ec_poll(struct work_struct *work)
525 {
526         struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
527         atomic_set(&ec->irq_count, 0);
528         (void)acpi_ec_gpe_handler(ec);
529 }
530
531 /* --------------------------------------------------------------------------
532                              Address Space Management
533    -------------------------------------------------------------------------- */
534
535 static acpi_status
536 acpi_ec_space_setup(acpi_handle region_handle,
537                     u32 function, void *handler_context, void **return_context)
538 {
539         /*
540          * The EC object is in the handler context and is needed
541          * when calling the acpi_ec_space_handler.
542          */
543         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
544             handler_context : NULL;
545
546         return AE_OK;
547 }
548
549 static acpi_status
550 acpi_ec_space_handler(u32 function, acpi_physical_address address,
551                       u32 bits, acpi_integer *value,
552                       void *handler_context, void *region_context)
553 {
554         struct acpi_ec *ec = handler_context;
555         int result = 0, i;
556         u8 temp = 0;
557
558         if ((address > 0xFF) || !value || !handler_context)
559                 return AE_BAD_PARAMETER;
560
561         if (function != ACPI_READ && function != ACPI_WRITE)
562                 return AE_BAD_PARAMETER;
563
564         if (bits != 8 && acpi_strict)
565                 return AE_BAD_PARAMETER;
566
567         acpi_ec_burst_enable(ec);
568
569         if (function == ACPI_READ) {
570                 result = acpi_ec_read(ec, address, &temp);
571                 *value = temp;
572         } else {
573                 temp = 0xff & (*value);
574                 result = acpi_ec_write(ec, address, temp);
575         }
576
577         for (i = 8; unlikely(bits - i > 0); i += 8) {
578                 ++address;
579                 if (function == ACPI_READ) {
580                         result = acpi_ec_read(ec, address, &temp);
581                         (*value) |= ((acpi_integer)temp) << i;
582                 } else {
583                         temp = 0xff & ((*value) >> i);
584                         result = acpi_ec_write(ec, address, temp);
585                 }
586         }
587
588         acpi_ec_burst_disable(ec);
589
590         switch (result) {
591         case -EINVAL:
592                 return AE_BAD_PARAMETER;
593                 break;
594         case -ENODEV:
595                 return AE_NOT_FOUND;
596                 break;
597         case -ETIME:
598                 return AE_TIME;
599                 break;
600         default:
601                 return AE_OK;
602         }
603 }
604
605 /* --------------------------------------------------------------------------
606                               FS Interface (/proc)
607    -------------------------------------------------------------------------- */
608
609 static struct proc_dir_entry *acpi_ec_dir;
610
611 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
612 {
613         struct acpi_ec *ec = seq->private;
614
615         if (!ec)
616                 goto end;
617
618         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
619         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
620                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
621         seq_printf(seq, "use global lock:\t%s\n",
622                    ec->global_lock ? "yes" : "no");
623       end:
624         return 0;
625 }
626
627 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
628 {
629         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
630 }
631
632 static struct file_operations acpi_ec_info_ops = {
633         .open = acpi_ec_info_open_fs,
634         .read = seq_read,
635         .llseek = seq_lseek,
636         .release = single_release,
637         .owner = THIS_MODULE,
638 };
639
640 static int acpi_ec_add_fs(struct acpi_device *device)
641 {
642         struct proc_dir_entry *entry = NULL;
643
644         if (!acpi_device_dir(device)) {
645                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
646                                                      acpi_ec_dir);
647                 if (!acpi_device_dir(device))
648                         return -ENODEV;
649         }
650
651         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
652                                   acpi_device_dir(device));
653         if (!entry)
654                 return -ENODEV;
655         else {
656                 entry->proc_fops = &acpi_ec_info_ops;
657                 entry->data = acpi_driver_data(device);
658                 entry->owner = THIS_MODULE;
659         }
660
661         return 0;
662 }
663
664 static int acpi_ec_remove_fs(struct acpi_device *device)
665 {
666
667         if (acpi_device_dir(device)) {
668                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
669                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
670                 acpi_device_dir(device) = NULL;
671         }
672
673         return 0;
674 }
675
676 /* --------------------------------------------------------------------------
677                                Driver Interface
678    -------------------------------------------------------------------------- */
679 static acpi_status
680 ec_parse_io_ports(struct acpi_resource *resource, void *context);
681
682 static struct acpi_ec *make_acpi_ec(void)
683 {
684         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
685         if (!ec)
686                 return NULL;
687         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
688         mutex_init(&ec->lock);
689         init_waitqueue_head(&ec->wait);
690         INIT_LIST_HEAD(&ec->list);
691         INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
692         atomic_set(&ec->irq_count, 0);
693         return ec;
694 }
695
696 static acpi_status
697 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
698                                void *context, void **return_value)
699 {
700         struct acpi_namespace_node *node = handle;
701         struct acpi_ec *ec = context;
702         int value = 0;
703         if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
704                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
705         }
706         return AE_OK;
707 }
708
709 static acpi_status
710 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
711 {
712         acpi_status status;
713
714         struct acpi_ec *ec = context;
715         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
716                                      ec_parse_io_ports, ec);
717         if (ACPI_FAILURE(status))
718                 return status;
719
720         /* Get GPE bit assignment (EC events). */
721         /* TODO: Add support for _GPE returning a package */
722         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
723         if (ACPI_FAILURE(status))
724                 return status;
725         /* Find and register all query methods */
726         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
727                             acpi_ec_register_query_methods, ec, NULL);
728         /* Use the global lock for all EC transactions? */
729         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
730         ec->handle = handle;
731         return AE_CTRL_TERMINATE;
732 }
733
734 static void ec_poll_stop(struct acpi_ec *ec)
735 {
736         clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
737         cancel_delayed_work(&ec->work);
738 }
739
740 static void ec_remove_handlers(struct acpi_ec *ec)
741 {
742         ec_poll_stop(ec);
743         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
744                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
745                 pr_err(PREFIX "failed to remove space handler\n");
746         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
747                                 &acpi_ec_gpe_handler)))
748                 pr_err(PREFIX "failed to remove gpe handler\n");
749         ec->handlers_installed = 0;
750 }
751
752 static int acpi_ec_add(struct acpi_device *device)
753 {
754         struct acpi_ec *ec = NULL;
755
756         if (!device)
757                 return -EINVAL;
758         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
759         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
760
761         /* Check for boot EC */
762         if (boot_ec) {
763                 if (boot_ec->handle == device->handle) {
764                         /* Pre-loaded EC from DSDT, just move pointer */
765                         ec = boot_ec;
766                         boot_ec = NULL;
767                         goto end;
768                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
769                         /* ECDT-based EC, time to shut it down */
770                         ec_remove_handlers(boot_ec);
771                         kfree(boot_ec);
772                         first_ec = boot_ec = NULL;
773                 }
774         }
775
776         ec = make_acpi_ec();
777         if (!ec)
778                 return -ENOMEM;
779
780         if (ec_parse_device(device->handle, 0, ec, NULL) !=
781             AE_CTRL_TERMINATE) {
782                 kfree(ec);
783                 return -EINVAL;
784         }
785         ec->handle = device->handle;
786       end:
787         if (!first_ec)
788                 first_ec = ec;
789         acpi_driver_data(device) = ec;
790         acpi_ec_add_fs(device);
791         pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
792                           ec->gpe, ec->command_addr, ec->data_addr);
793         pr_info(PREFIX "driver started in %s mode\n",
794                 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
795         return 0;
796 }
797
798 static int acpi_ec_remove(struct acpi_device *device, int type)
799 {
800         struct acpi_ec *ec;
801         struct acpi_ec_query_handler *handler, *tmp;
802
803         if (!device)
804                 return -EINVAL;
805
806         ec = acpi_driver_data(device);
807         mutex_lock(&ec->lock);
808         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
809                 list_del(&handler->node);
810                 kfree(handler);
811         }
812         mutex_unlock(&ec->lock);
813         acpi_ec_remove_fs(device);
814         acpi_driver_data(device) = NULL;
815         if (ec == first_ec)
816                 first_ec = NULL;
817         kfree(ec);
818         return 0;
819 }
820
821 static acpi_status
822 ec_parse_io_ports(struct acpi_resource *resource, void *context)
823 {
824         struct acpi_ec *ec = context;
825
826         if (resource->type != ACPI_RESOURCE_TYPE_IO)
827                 return AE_OK;
828
829         /*
830          * The first address region returned is the data port, and
831          * the second address region returned is the status/command
832          * port.
833          */
834         if (ec->data_addr == 0)
835                 ec->data_addr = resource->data.io.minimum;
836         else if (ec->command_addr == 0)
837                 ec->command_addr = resource->data.io.minimum;
838         else
839                 return AE_CTRL_TERMINATE;
840
841         return AE_OK;
842 }
843
844 static int ec_install_handlers(struct acpi_ec *ec)
845 {
846         acpi_status status;
847         if (ec->handlers_installed)
848                 return 0;
849         status = acpi_install_gpe_handler(NULL, ec->gpe,
850                                           ACPI_GPE_EDGE_TRIGGERED,
851                                           &acpi_ec_gpe_handler, ec);
852         if (ACPI_FAILURE(status))
853                 return -ENODEV;
854
855         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
856         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
857
858         status = acpi_install_address_space_handler(ec->handle,
859                                                     ACPI_ADR_SPACE_EC,
860                                                     &acpi_ec_space_handler,
861                                                     &acpi_ec_space_setup, ec);
862         if (ACPI_FAILURE(status)) {
863                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
864                 return -ENODEV;
865         }
866
867         ec->handlers_installed = 1;
868         return 0;
869 }
870
871 static int acpi_ec_start(struct acpi_device *device)
872 {
873         struct acpi_ec *ec;
874         int ret = 0;
875
876         if (!device)
877                 return -EINVAL;
878
879         ec = acpi_driver_data(device);
880
881         if (!ec)
882                 return -EINVAL;
883
884         ret = ec_install_handlers(ec);
885
886         /* EC is fully operational, allow queries */
887         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
888         ec_schedule_ec_poll(ec);
889         return ret;
890 }
891
892 static int acpi_ec_stop(struct acpi_device *device, int type)
893 {
894         struct acpi_ec *ec;
895         if (!device)
896                 return -EINVAL;
897         ec = acpi_driver_data(device);
898         if (!ec)
899                 return -EINVAL;
900         ec_remove_handlers(ec);
901
902         return 0;
903 }
904
905 int __init acpi_boot_ec_enable(void)
906 {
907         if (!boot_ec || boot_ec->handlers_installed)
908                 return 0;
909         if (!ec_install_handlers(boot_ec)) {
910                 first_ec = boot_ec;
911                 return 0;
912         }
913         return -EFAULT;
914 }
915
916 static const struct acpi_device_id ec_device_ids[] = {
917         {"PNP0C09", 0},
918         {"", 0},
919 };
920
921 int __init acpi_ec_ecdt_probe(void)
922 {
923         int ret;
924         acpi_status status;
925         struct acpi_table_ecdt *ecdt_ptr;
926
927         boot_ec = make_acpi_ec();
928         if (!boot_ec)
929                 return -ENOMEM;
930         /*
931          * Generate a boot ec context
932          */
933         status = acpi_get_table(ACPI_SIG_ECDT, 1,
934                                 (struct acpi_table_header **)&ecdt_ptr);
935         if (ACPI_SUCCESS(status)) {
936                 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
937                 boot_ec->command_addr = ecdt_ptr->control.address;
938                 boot_ec->data_addr = ecdt_ptr->data.address;
939                 boot_ec->gpe = ecdt_ptr->gpe;
940                 boot_ec->handle = ACPI_ROOT_OBJECT;
941         } else {
942                 /* This workaround is needed only on some broken machines,
943                  * which require early EC, but fail to provide ECDT */
944                 acpi_handle x;
945                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
946                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
947                                                 boot_ec, NULL);
948                 /* Check that acpi_get_devices actually find something */
949                 if (ACPI_FAILURE(status) || !boot_ec->handle)
950                         goto error;
951                 /* We really need to limit this workaround, the only ASUS,
952                  * which needs it, has fake EC._INI method, so use it as flag.
953                  * Keep boot_ec struct as it will be needed soon.
954                  */
955                 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
956                         return -ENODEV;
957         }
958
959         ret = ec_install_handlers(boot_ec);
960         if (!ret) {
961                 first_ec = boot_ec;
962                 return 0;
963         }
964       error:
965         kfree(boot_ec);
966         boot_ec = NULL;
967         return -ENODEV;
968 }
969
970 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
971 {
972         struct acpi_ec *ec = acpi_driver_data(device);
973         /* Stop using GPE */
974         set_bit(EC_FLAGS_NO_GPE, &ec->flags);
975         clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
976         acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
977         return 0;
978 }
979
980 static int acpi_ec_resume(struct acpi_device *device)
981 {
982         struct acpi_ec *ec = acpi_driver_data(device);
983         /* Enable use of GPE back */
984         clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
985         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
986         return 0;
987 }
988
989 static struct acpi_driver acpi_ec_driver = {
990         .name = "ec",
991         .class = ACPI_EC_CLASS,
992         .ids = ec_device_ids,
993         .ops = {
994                 .add = acpi_ec_add,
995                 .remove = acpi_ec_remove,
996                 .start = acpi_ec_start,
997                 .stop = acpi_ec_stop,
998                 .suspend = acpi_ec_suspend,
999                 .resume = acpi_ec_resume,
1000                 },
1001 };
1002
1003 static int __init acpi_ec_init(void)
1004 {
1005         int result = 0;
1006
1007         if (acpi_disabled)
1008                 return 0;
1009
1010         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1011         if (!acpi_ec_dir)
1012                 return -ENODEV;
1013
1014         /* Now register the driver for the EC */
1015         result = acpi_bus_register_driver(&acpi_ec_driver);
1016         if (result < 0) {
1017                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1018                 return -ENODEV;
1019         }
1020
1021         return result;
1022 }
1023
1024 subsys_initcall(acpi_ec_init);
1025
1026 /* EC driver currently not unloadable */
1027 #if 0
1028 static void __exit acpi_ec_exit(void)
1029 {
1030
1031         acpi_bus_unregister_driver(&acpi_ec_driver);
1032
1033         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1034
1035         return;
1036 }
1037 #endif  /* 0 */