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