ffb8361bc1bf97d902bcb1c6e38dc0061889cda2
[cascardo/linux.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 #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 <asm/io.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/actypes.h>
41
42 #define ACPI_EC_CLASS                   "embedded_controller"
43 #define ACPI_EC_HID                     "PNP0C09"
44 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
45 #define ACPI_EC_FILE_INFO               "info"
46 #undef PREFIX
47 #define PREFIX                          "ACPI: EC: "
48
49 /* EC status register */
50 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
51 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
52 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
53 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
54
55 /* EC commands */
56 enum ec_command {
57         ACPI_EC_COMMAND_READ = 0x80,
58         ACPI_EC_COMMAND_WRITE = 0x81,
59         ACPI_EC_BURST_ENABLE = 0x82,
60         ACPI_EC_BURST_DISABLE = 0x83,
61         ACPI_EC_COMMAND_QUERY = 0x84,
62 };
63 /* EC events */
64 enum ec_event {
65         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
66         ACPI_EC_EVENT_IBF_0,    /* Input buffer empty */
67 };
68
69 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
70 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
71
72 static enum ec_mode {
73         EC_INTR = 1,            /* Output buffer full */
74         EC_POLL,                /* Input buffer empty */
75 } acpi_ec_mode = EC_INTR;
76
77 static int acpi_ec_remove(struct acpi_device *device, int type);
78 static int acpi_ec_start(struct acpi_device *device);
79 static int acpi_ec_stop(struct acpi_device *device, int type);
80 static int acpi_ec_add(struct acpi_device *device);
81
82 static struct acpi_driver acpi_ec_driver = {
83         .name = "ec",
84         .class = ACPI_EC_CLASS,
85         .ids = ACPI_EC_HID,
86         .ops = {
87                 .add = acpi_ec_add,
88                 .remove = acpi_ec_remove,
89                 .start = acpi_ec_start,
90                 .stop = acpi_ec_stop,
91                 },
92 };
93
94 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
95 /* External interfaces use first EC only, so remember */
96 static struct acpi_ec {
97         acpi_handle handle;
98         unsigned long gpe;
99         unsigned long command_addr;
100         unsigned long data_addr;
101         unsigned long global_lock;
102         struct mutex lock;
103         atomic_t query_pending;
104         atomic_t event_count;
105         wait_queue_head_t wait;
106 } *boot_ec, *first_ec;
107
108 /* --------------------------------------------------------------------------
109                              Transaction Management
110    -------------------------------------------------------------------------- */
111
112 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
113 {
114         return inb(ec->command_addr);
115 }
116
117 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
118 {
119         return inb(ec->data_addr);
120 }
121
122 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
123 {
124         outb(command, ec->command_addr);
125 }
126
127 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
128 {
129         outb(data, ec->data_addr);
130 }
131
132 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
133                                        unsigned old_count)
134 {
135         u8 status = acpi_ec_read_status(ec);
136         if (old_count == atomic_read(&ec->event_count))
137                 return 0;
138         if (event == ACPI_EC_EVENT_OBF_1) {
139                 if (status & ACPI_EC_FLAG_OBF)
140                         return 1;
141         } else if (event == ACPI_EC_EVENT_IBF_0) {
142                 if (!(status & ACPI_EC_FLAG_IBF))
143                         return 1;
144         }
145
146         return 0;
147 }
148
149 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
150                         unsigned count, int force_poll)
151 {
152         if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
153                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
154                 while (time_before(jiffies, delay)) {
155                         if (acpi_ec_check_status(ec, event, 0))
156                                 return 0;
157                 }
158         } else {
159                 if (wait_event_timeout(ec->wait,
160                                        acpi_ec_check_status(ec, event, count),
161                                        msecs_to_jiffies(ACPI_EC_DELAY)) ||
162                     acpi_ec_check_status(ec, event, 0)) {
163                         return 0;
164                 } else {
165                         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
166                                " status = %d, expect_event = %d\n",
167                                acpi_ec_read_status(ec), event);
168                 }
169         }
170
171         return -ETIME;
172 }
173
174 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
175                                         const u8 * wdata, unsigned wdata_len,
176                                         u8 * rdata, unsigned rdata_len,
177                                         int force_poll)
178 {
179         int result = 0;
180         unsigned count = atomic_read(&ec->event_count);
181         acpi_ec_write_cmd(ec, command);
182
183         for (; wdata_len > 0; --wdata_len) {
184                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
185                 if (result) {
186                         printk(KERN_ERR PREFIX
187                                "write_cmd timeout, command = %d\n", command);
188                         goto end;
189                 }
190                 count = atomic_read(&ec->event_count);
191                 acpi_ec_write_data(ec, *(wdata++));
192         }
193
194         if (!rdata_len) {
195                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
196                 if (result) {
197                         printk(KERN_ERR PREFIX
198                                "finish-write timeout, command = %d\n", command);
199                         goto end;
200                 }
201         } else if (command == ACPI_EC_COMMAND_QUERY) {
202                 atomic_set(&ec->query_pending, 0);
203         }
204
205         for (; rdata_len > 0; --rdata_len) {
206                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
207                 if (result) {
208                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
209                                command);
210                         goto end;
211                 }
212                 count = atomic_read(&ec->event_count);
213                 *(rdata++) = acpi_ec_read_data(ec);
214         }
215       end:
216         return result;
217 }
218
219 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
220                                const u8 * wdata, unsigned wdata_len,
221                                u8 * rdata, unsigned rdata_len,
222                                int force_poll)
223 {
224         int status;
225         u32 glk;
226
227         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
228                 return -EINVAL;
229
230         if (rdata)
231                 memset(rdata, 0, rdata_len);
232
233         mutex_lock(&ec->lock);
234         if (ec->global_lock) {
235                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
236                 if (ACPI_FAILURE(status)) {
237                         mutex_unlock(&ec->lock);
238                         return -ENODEV;
239                 }
240         }
241
242         /* Make sure GPE is enabled before doing transaction */
243         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
244
245         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
246         if (status) {
247                 printk(KERN_ERR PREFIX
248                        "input buffer is not empty, aborting transaction\n");
249                 goto end;
250         }
251
252         status = acpi_ec_transaction_unlocked(ec, command,
253                                               wdata, wdata_len,
254                                               rdata, rdata_len,
255                                               force_poll);
256
257       end:
258
259         if (ec->global_lock)
260                 acpi_release_global_lock(glk);
261         mutex_unlock(&ec->lock);
262
263         return status;
264 }
265
266 /*
267  * Note: samsung nv5000 doesn't work with ec burst mode.
268  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
269  */
270 int acpi_ec_burst_enable(struct acpi_ec *ec)
271 {
272         u8 d;
273         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
274 }
275
276 int acpi_ec_burst_disable(struct acpi_ec *ec)
277 {
278         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
279 }
280
281 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
282 {
283         int result;
284         u8 d;
285
286         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
287                                      &address, 1, &d, 1, 0);
288         *data = d;
289         return result;
290 }
291
292 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
293 {
294         u8 wdata[2] = { address, data };
295         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
296                                    wdata, 2, NULL, 0, 0);
297 }
298
299 /*
300  * Externally callable EC access functions. For now, assume 1 EC only
301  */
302 int ec_burst_enable(void)
303 {
304         if (!first_ec)
305                 return -ENODEV;
306         return acpi_ec_burst_enable(first_ec);
307 }
308
309 EXPORT_SYMBOL(ec_burst_enable);
310
311 int ec_burst_disable(void)
312 {
313         if (!first_ec)
314                 return -ENODEV;
315         return acpi_ec_burst_disable(first_ec);
316 }
317
318 EXPORT_SYMBOL(ec_burst_disable);
319
320 int ec_read(u8 addr, u8 * val)
321 {
322         int err;
323         u8 temp_data;
324
325         if (!first_ec)
326                 return -ENODEV;
327
328         err = acpi_ec_read(first_ec, addr, &temp_data);
329
330         if (!err) {
331                 *val = temp_data;
332                 return 0;
333         } else
334                 return err;
335 }
336
337 EXPORT_SYMBOL(ec_read);
338
339 int ec_write(u8 addr, u8 val)
340 {
341         int err;
342
343         if (!first_ec)
344                 return -ENODEV;
345
346         err = acpi_ec_write(first_ec, addr, val);
347
348         return err;
349 }
350
351 EXPORT_SYMBOL(ec_write);
352
353 int ec_transaction(u8 command,
354                    const u8 * wdata, unsigned wdata_len,
355                    u8 * rdata, unsigned rdata_len,
356                    int force_poll)
357 {
358         if (!first_ec)
359                 return -ENODEV;
360
361         return acpi_ec_transaction(first_ec, command, wdata,
362                                    wdata_len, rdata, rdata_len,
363                                    force_poll);
364 }
365
366 EXPORT_SYMBOL(ec_transaction);
367
368 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
369 {
370         int result;
371         u8 d;
372
373         if (!ec || !data)
374                 return -EINVAL;
375
376         /*
377          * Query the EC to find out which _Qxx method we need to evaluate.
378          * Note that successful completion of the query causes the ACPI_EC_SCI
379          * bit to be cleared (and thus clearing the interrupt source).
380          */
381
382         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
383         if (result)
384                 return result;
385
386         if (!d)
387                 return -ENODATA;
388
389         *data = d;
390         return 0;
391 }
392
393 /* --------------------------------------------------------------------------
394                                 Event Management
395    -------------------------------------------------------------------------- */
396
397 static void acpi_ec_gpe_query(void *ec_cxt)
398 {
399         struct acpi_ec *ec = ec_cxt;
400         u8 value = 0;
401         char object_name[8];
402
403         if (!ec || acpi_ec_query(ec, &value))
404                 return;
405
406         snprintf(object_name, 8, "_Q%2.2X", value);
407
408         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
409
410         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
411 }
412
413 static u32 acpi_ec_gpe_handler(void *data)
414 {
415         acpi_status status = AE_OK;
416         u8 value;
417         struct acpi_ec *ec = data;
418
419         atomic_inc(&ec->event_count);
420
421         if (acpi_ec_mode == EC_INTR) {
422                 wake_up(&ec->wait);
423         }
424
425         value = acpi_ec_read_status(ec);
426         if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
427                 atomic_set(&ec->query_pending, 1);
428                 status =
429                     acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
430                                     ec);
431         }
432
433         return status == AE_OK ?
434             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
435 }
436
437 /* --------------------------------------------------------------------------
438                              Address Space Management
439    -------------------------------------------------------------------------- */
440
441 static acpi_status
442 acpi_ec_space_setup(acpi_handle region_handle,
443                     u32 function, void *handler_context, void **return_context)
444 {
445         /*
446          * The EC object is in the handler context and is needed
447          * when calling the acpi_ec_space_handler.
448          */
449         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
450             handler_context : NULL;
451
452         return AE_OK;
453 }
454
455 static acpi_status
456 acpi_ec_space_handler(u32 function, acpi_physical_address address,
457                       u32 bits, acpi_integer *value,
458                       void *handler_context, void *region_context)
459 {
460         struct acpi_ec *ec = handler_context;
461         int result = 0, i = 0;
462         u8 temp = 0;
463
464         if ((address > 0xFF) || !value || !handler_context)
465                 return AE_BAD_PARAMETER;
466
467         if (function != ACPI_READ && function != ACPI_WRITE)
468                 return AE_BAD_PARAMETER;
469
470         if (bits != 8 && acpi_strict)
471                 return AE_BAD_PARAMETER;
472
473         while (bits - i > 0) {
474                 if (function == ACPI_READ) {
475                         result = acpi_ec_read(ec, address, &temp);
476                         (*value) |= ((acpi_integer)temp) << i;
477                 } else {
478                         temp = 0xff & ((*value) >> i);
479                         result = acpi_ec_write(ec, address, temp);
480                 }
481                 i += 8;
482                 ++address;
483         }
484
485         switch (result) {
486         case -EINVAL:
487                 return AE_BAD_PARAMETER;
488                 break;
489         case -ENODEV:
490                 return AE_NOT_FOUND;
491                 break;
492         case -ETIME:
493                 return AE_TIME;
494                 break;
495         default:
496                 return AE_OK;
497         }
498 }
499
500 /* --------------------------------------------------------------------------
501                               FS Interface (/proc)
502    -------------------------------------------------------------------------- */
503
504 static struct proc_dir_entry *acpi_ec_dir;
505
506 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
507 {
508         struct acpi_ec *ec = seq->private;
509
510         if (!ec)
511                 goto end;
512
513         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
514         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
515                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
516         seq_printf(seq, "use global lock:\t%s\n",
517                    ec->global_lock ? "yes" : "no");
518       end:
519         return 0;
520 }
521
522 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
523 {
524         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
525 }
526
527 static struct file_operations acpi_ec_info_ops = {
528         .open = acpi_ec_info_open_fs,
529         .read = seq_read,
530         .llseek = seq_lseek,
531         .release = single_release,
532         .owner = THIS_MODULE,
533 };
534
535 static int acpi_ec_add_fs(struct acpi_device *device)
536 {
537         struct proc_dir_entry *entry = NULL;
538
539         if (!acpi_device_dir(device)) {
540                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
541                                                      acpi_ec_dir);
542                 if (!acpi_device_dir(device))
543                         return -ENODEV;
544         }
545
546         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
547                                   acpi_device_dir(device));
548         if (!entry)
549                 return -ENODEV;
550         else {
551                 entry->proc_fops = &acpi_ec_info_ops;
552                 entry->data = acpi_driver_data(device);
553                 entry->owner = THIS_MODULE;
554         }
555
556         return 0;
557 }
558
559 static int acpi_ec_remove_fs(struct acpi_device *device)
560 {
561
562         if (acpi_device_dir(device)) {
563                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
564                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
565                 acpi_device_dir(device) = NULL;
566         }
567
568         return 0;
569 }
570
571 /* --------------------------------------------------------------------------
572                                Driver Interface
573    -------------------------------------------------------------------------- */
574 static acpi_status
575 ec_parse_io_ports(struct acpi_resource *resource, void *context);
576
577 static acpi_status
578 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
579
580 static struct acpi_ec *make_acpi_ec(void)
581 {
582         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
583         if (!ec)
584                 return NULL;
585
586         atomic_set(&ec->query_pending, 1);
587         atomic_set(&ec->event_count, 1);
588         mutex_init(&ec->lock);
589         init_waitqueue_head(&ec->wait);
590
591         return ec;
592 }
593
594 static int acpi_ec_add(struct acpi_device *device)
595 {
596         acpi_status status = AE_OK;
597         struct acpi_ec *ec = NULL;
598
599         if (!device)
600                 return -EINVAL;
601
602         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
603         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
604
605         ec = make_acpi_ec();
606         if (!ec)
607                 return -ENOMEM;
608
609         status = ec_parse_device(device->handle, 0, ec, NULL);
610         if (status != AE_CTRL_TERMINATE) {
611                 kfree(ec);
612                 return -EINVAL;
613         }
614
615         /* Check if we found the boot EC */
616         if (boot_ec) {
617                 if (boot_ec->gpe == ec->gpe) {
618                         /* We might have incorrect info for GL at boot time */
619                         mutex_lock(&boot_ec->lock);
620                         boot_ec->global_lock = ec->global_lock;
621                         mutex_unlock(&boot_ec->lock);
622                         kfree(ec);
623                         ec = boot_ec;
624                 }
625         } else
626                 first_ec = ec;
627         ec->handle = device->handle;
628         acpi_driver_data(device) = ec;
629
630         acpi_ec_add_fs(device);
631
632         return 0;
633 }
634
635 static int acpi_ec_remove(struct acpi_device *device, int type)
636 {
637         struct acpi_ec *ec;
638
639         if (!device)
640                 return -EINVAL;
641
642         ec = acpi_driver_data(device);
643         acpi_ec_remove_fs(device);
644         acpi_driver_data(device) = NULL;
645         if (ec == first_ec)
646                 first_ec = NULL;
647
648         /* Don't touch boot EC */
649         if (boot_ec != ec)
650                 kfree(ec);
651         return 0;
652 }
653
654 static acpi_status
655 ec_parse_io_ports(struct acpi_resource *resource, void *context)
656 {
657         struct acpi_ec *ec = context;
658
659         if (resource->type != ACPI_RESOURCE_TYPE_IO)
660                 return AE_OK;
661
662         /*
663          * The first address region returned is the data port, and
664          * the second address region returned is the status/command
665          * port.
666          */
667         if (ec->data_addr == 0)
668                 ec->data_addr = resource->data.io.minimum;
669         else if (ec->command_addr == 0)
670                 ec->command_addr = resource->data.io.minimum;
671         else
672                 return AE_CTRL_TERMINATE;
673
674         return AE_OK;
675 }
676
677 static int ec_install_handlers(struct acpi_ec *ec)
678 {
679         acpi_status status;
680         status = acpi_install_gpe_handler(NULL, ec->gpe,
681                                           ACPI_GPE_EDGE_TRIGGERED,
682                                           &acpi_ec_gpe_handler, ec);
683         if (ACPI_FAILURE(status))
684                 return -ENODEV;
685
686         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
687         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
688
689         status = acpi_install_address_space_handler(ec->handle,
690                                                     ACPI_ADR_SPACE_EC,
691                                                     &acpi_ec_space_handler,
692                                                     &acpi_ec_space_setup, ec);
693         if (ACPI_FAILURE(status)) {
694                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
695                 return -ENODEV;
696         }
697
698         /* EC is fully operational, allow queries */
699         atomic_set(&ec->query_pending, 0);
700
701         return 0;
702 }
703
704 static int acpi_ec_start(struct acpi_device *device)
705 {
706         struct acpi_ec *ec;
707
708         if (!device)
709                 return -EINVAL;
710
711         ec = acpi_driver_data(device);
712
713         if (!ec)
714                 return -EINVAL;
715
716         /* Boot EC is already working */
717         if (ec == boot_ec)
718                 return 0;
719
720         return ec_install_handlers(ec);
721 }
722
723 static int acpi_ec_stop(struct acpi_device *device, int type)
724 {
725         acpi_status status;
726         struct acpi_ec *ec;
727
728         if (!device)
729                 return -EINVAL;
730
731         ec = acpi_driver_data(device);
732         if (!ec)
733                 return -EINVAL;
734
735         /* Don't touch boot EC */
736         if (ec == boot_ec)
737                 return 0;
738
739         status = acpi_remove_address_space_handler(ec->handle,
740                                                    ACPI_ADR_SPACE_EC,
741                                                    &acpi_ec_space_handler);
742         if (ACPI_FAILURE(status))
743                 return -ENODEV;
744
745         status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
746         if (ACPI_FAILURE(status))
747                 return -ENODEV;
748
749         return 0;
750 }
751
752 static acpi_status
753 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
754 {
755         acpi_status status;
756
757         struct acpi_ec *ec = context;
758         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
759                                      ec_parse_io_ports, ec);
760         if (ACPI_FAILURE(status))
761                 return status;
762
763         /* Get GPE bit assignment (EC events). */
764         /* TODO: Add support for _GPE returning a package */
765         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
766         if (ACPI_FAILURE(status))
767                 return status;
768
769         /* Use the global lock for all EC transactions? */
770         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
771
772         ec->handle = handle;
773
774         printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
775                           ec->gpe, ec->command_addr, ec->data_addr);
776
777         return AE_CTRL_TERMINATE;
778 }
779
780 int __init acpi_ec_ecdt_probe(void)
781 {
782         int ret;
783         acpi_status status;
784         struct acpi_table_ecdt *ecdt_ptr;
785
786         boot_ec = make_acpi_ec();
787         if (!boot_ec)
788                 return -ENOMEM;
789         /*
790          * Generate a boot ec context
791          */
792
793         status = acpi_get_table(ACPI_SIG_ECDT, 1,
794                                 (struct acpi_table_header **)&ecdt_ptr);
795         if (ACPI_FAILURE(status))
796                 goto error;
797
798         printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
799
800         boot_ec->command_addr = ecdt_ptr->control.address;
801         boot_ec->data_addr = ecdt_ptr->data.address;
802         boot_ec->gpe = ecdt_ptr->gpe;
803         boot_ec->handle = ACPI_ROOT_OBJECT;
804
805         ret = ec_install_handlers(boot_ec);
806         if (!ret) {
807                 first_ec = boot_ec;
808                 return 0;
809         }
810       error:
811         kfree(boot_ec);
812         boot_ec = NULL;
813
814         return -ENODEV;
815 }
816
817 static int __init acpi_ec_init(void)
818 {
819         int result = 0;
820
821         if (acpi_disabled)
822                 return 0;
823
824         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
825         if (!acpi_ec_dir)
826                 return -ENODEV;
827
828         /* Now register the driver for the EC */
829         result = acpi_bus_register_driver(&acpi_ec_driver);
830         if (result < 0) {
831                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
832                 return -ENODEV;
833         }
834
835         return result;
836 }
837
838 subsys_initcall(acpi_ec_init);
839
840 /* EC driver currently not unloadable */
841 #if 0
842 static void __exit acpi_ec_exit(void)
843 {
844
845         acpi_bus_unregister_driver(&acpi_ec_driver);
846
847         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
848
849         return;
850 }
851 #endif                          /* 0 */
852
853 static int __init acpi_ec_set_intr_mode(char *str)
854 {
855         int intr;
856
857         if (!get_option(&str, &intr))
858                 return 0;
859
860         acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
861
862         printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
863
864         return 1;
865 }
866
867 __setup("ec_intr=", acpi_ec_set_intr_mode);