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