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