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