[MTD] NAND: Fix missing NULL pointer check
[linux-2.6] / 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 <asm/io.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37 #include <acpi/actypes.h>
38
39 #define _COMPONENT              ACPI_EC_COMPONENT
40 ACPI_MODULE_NAME                ("acpi_ec")
41
42 #define ACPI_EC_COMPONENT               0x00100000
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_HID                     "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME             "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
47 #define ACPI_EC_FILE_INFO               "info"
48
49
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_SCI        0x20    /* EC-SCI occurred */
53
54 #define ACPI_EC_EVENT_OBF       0x01    /* Output buffer full */
55 #define ACPI_EC_EVENT_IBE       0x02    /* Input buffer empty */
56
57 #define ACPI_EC_UDELAY          100     /* Poll @ 100us increments */
58 #define ACPI_EC_UDELAY_COUNT    1000    /* Wait 10ms max. during EC ops */
59 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
60
61 #define ACPI_EC_COMMAND_READ    0x80
62 #define ACPI_EC_COMMAND_WRITE   0x81
63 #define ACPI_EC_COMMAND_QUERY   0x84
64
65 static int acpi_ec_add (struct acpi_device *device);
66 static int acpi_ec_remove (struct acpi_device *device, int type);
67 static int acpi_ec_start (struct acpi_device *device);
68 static int acpi_ec_stop (struct acpi_device *device, int type);
69
70 static struct acpi_driver acpi_ec_driver = {
71         .name =         ACPI_EC_DRIVER_NAME,
72         .class =        ACPI_EC_CLASS,
73         .ids =          ACPI_EC_HID,
74         .ops =          {
75                                 .add =          acpi_ec_add,
76                                 .remove =       acpi_ec_remove,
77                                 .start =        acpi_ec_start,
78                                 .stop =         acpi_ec_stop,
79                         },
80 };
81
82 struct acpi_ec {
83         acpi_handle                     handle;
84         unsigned long                   uid;
85         unsigned long                   gpe_bit;
86         struct acpi_generic_address     status_addr;
87         struct acpi_generic_address     command_addr;
88         struct acpi_generic_address     data_addr;
89         unsigned long                   global_lock;
90         spinlock_t                      lock;
91 };
92
93 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
94 static struct acpi_ec   *ec_ecdt;
95
96 /* External interfaces use first EC only, so remember */
97 static struct acpi_device *first_ec;
98
99 /* --------------------------------------------------------------------------
100                              Transaction Management
101    -------------------------------------------------------------------------- */
102
103 static int
104 acpi_ec_wait (
105         struct acpi_ec          *ec,
106         u8                      event)
107 {
108         u32                     acpi_ec_status = 0;
109         u32                     i = ACPI_EC_UDELAY_COUNT;
110
111         if (!ec)
112                 return -EINVAL;
113
114         /* Poll the EC status register waiting for the event to occur. */
115         switch (event) {
116         case ACPI_EC_EVENT_OBF:
117                 do {
118                         acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
119                         if (acpi_ec_status & ACPI_EC_FLAG_OBF)
120                                 return 0;
121                         udelay(ACPI_EC_UDELAY);
122                 } while (--i>0);
123                 break;
124         case ACPI_EC_EVENT_IBE:
125                 do {
126                         acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
127                         if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
128                                 return 0;
129                         udelay(ACPI_EC_UDELAY);
130                 } while (--i>0);
131                 break;
132         default:
133                 return -EINVAL;
134         }
135
136         return -ETIME;
137 }
138
139
140 static int
141 acpi_ec_read (
142         struct acpi_ec          *ec,
143         u8                      address,
144         u32                     *data)
145 {
146         acpi_status             status = AE_OK;
147         int                     result = 0;
148         unsigned long           flags = 0;
149         u32                     glk = 0;
150
151         ACPI_FUNCTION_TRACE("acpi_ec_read");
152
153         if (!ec || !data)
154                 return_VALUE(-EINVAL);
155
156         *data = 0;
157
158         if (ec->global_lock) {
159                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
160                 if (ACPI_FAILURE(status))
161                         return_VALUE(-ENODEV);
162         }
163         
164         spin_lock_irqsave(&ec->lock, flags);
165
166         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
167         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
168         if (result)
169                 goto end;
170
171         acpi_hw_low_level_write(8, address, &ec->data_addr);
172         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
173         if (result)
174                 goto end;
175
176
177         acpi_hw_low_level_read(8, data, &ec->data_addr);
178
179         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
180                 *data, address));
181
182 end:
183         spin_unlock_irqrestore(&ec->lock, flags);
184
185         if (ec->global_lock)
186                 acpi_release_global_lock(glk);
187
188         return_VALUE(result);
189 }
190
191
192 static int
193 acpi_ec_write (
194         struct acpi_ec          *ec,
195         u8                      address,
196         u8                      data)
197 {
198         int                     result = 0;
199         acpi_status             status = AE_OK;
200         unsigned long           flags = 0;
201         u32                     glk = 0;
202
203         ACPI_FUNCTION_TRACE("acpi_ec_write");
204
205         if (!ec)
206                 return_VALUE(-EINVAL);
207
208         if (ec->global_lock) {
209                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
210                 if (ACPI_FAILURE(status))
211                         return_VALUE(-ENODEV);
212         }
213
214         spin_lock_irqsave(&ec->lock, flags);
215
216         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
217         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
218         if (result)
219                 goto end;
220
221         acpi_hw_low_level_write(8, address, &ec->data_addr);
222         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
223         if (result)
224                 goto end;
225
226         acpi_hw_low_level_write(8, data, &ec->data_addr);
227         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
228         if (result)
229                 goto end;
230
231         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
232                 data, address));
233
234 end:
235         spin_unlock_irqrestore(&ec->lock, flags);
236
237         if (ec->global_lock)
238                 acpi_release_global_lock(glk);
239
240         return_VALUE(result);
241 }
242
243 /*
244  * Externally callable EC access functions. For now, assume 1 EC only
245  */
246 int
247 ec_read(u8 addr, u8 *val)
248 {
249         struct acpi_ec *ec;
250         int err;
251         u32 temp_data;
252
253         if (!first_ec)
254                 return -ENODEV;
255
256         ec = acpi_driver_data(first_ec);
257
258         err = acpi_ec_read(ec, addr, &temp_data);
259
260         if (!err) {
261                 *val = temp_data;
262                 return 0;
263         }
264         else
265                 return err;
266 }
267 EXPORT_SYMBOL(ec_read);
268
269 int
270 ec_write(u8 addr, u8 val)
271 {
272         struct acpi_ec *ec;
273         int err;
274
275         if (!first_ec)
276                 return -ENODEV;
277
278         ec = acpi_driver_data(first_ec);
279
280         err = acpi_ec_write(ec, addr, val);
281
282         return err;
283 }
284 EXPORT_SYMBOL(ec_write);
285
286
287 static int
288 acpi_ec_query (
289         struct acpi_ec          *ec,
290         u32                     *data)
291 {
292         int                     result = 0;
293         acpi_status             status = AE_OK;
294         unsigned long           flags = 0;
295         u32                     glk = 0;
296
297         ACPI_FUNCTION_TRACE("acpi_ec_query");
298
299         if (!ec || !data)
300                 return_VALUE(-EINVAL);
301
302         *data = 0;
303
304         if (ec->global_lock) {
305                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
306                 if (ACPI_FAILURE(status))
307                         return_VALUE(-ENODEV);
308         }
309
310         /*
311          * Query the EC to find out which _Qxx method we need to evaluate.
312          * Note that successful completion of the query causes the ACPI_EC_SCI
313          * bit to be cleared (and thus clearing the interrupt source).
314          */
315         spin_lock_irqsave(&ec->lock, flags);
316
317         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
318         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
319         if (result)
320                 goto end;
321         
322         acpi_hw_low_level_read(8, data, &ec->data_addr);
323         if (!*data)
324                 result = -ENODATA;
325
326 end:
327         spin_unlock_irqrestore(&ec->lock, flags);
328
329         if (ec->global_lock)
330                 acpi_release_global_lock(glk);
331
332         return_VALUE(result);
333 }
334
335
336 /* --------------------------------------------------------------------------
337                                 Event Management
338    -------------------------------------------------------------------------- */
339
340 struct acpi_ec_query_data {
341         acpi_handle             handle;
342         u8                      data;
343 };
344
345 static void
346 acpi_ec_gpe_query (
347         void                    *ec_cxt)
348 {
349         struct acpi_ec          *ec = (struct acpi_ec *) ec_cxt;
350         u32                     value = 0;
351         unsigned long           flags = 0;
352         static char             object_name[5] = {'_','Q','0','0','\0'};
353         const char              hex[] = {'0','1','2','3','4','5','6','7',
354                                          '8','9','A','B','C','D','E','F'};
355
356         ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
357
358         if (!ec_cxt)
359                 goto end;       
360
361         spin_lock_irqsave(&ec->lock, flags);
362         acpi_hw_low_level_read(8, &value, &ec->command_addr);
363         spin_unlock_irqrestore(&ec->lock, flags);
364
365         /* TBD: Implement asynch events!
366          * NOTE: All we care about are EC-SCI's.  Other EC events are
367          * handled via polling (yuck!).  This is because some systems
368          * treat EC-SCIs as level (versus EDGE!) triggered, preventing
369          *  a purely interrupt-driven approach (grumble, grumble).
370          */
371         if (!(value & ACPI_EC_FLAG_SCI))
372                 goto end;
373
374         if (acpi_ec_query(ec, &value))
375                 goto end;
376         
377         object_name[2] = hex[((value >> 4) & 0x0F)];
378         object_name[3] = hex[(value & 0x0F)];
379
380         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
381
382         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
383
384 end:
385         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
386 }
387
388 static u32
389 acpi_ec_gpe_handler (
390         void                    *data)
391 {
392         acpi_status             status = AE_OK;
393         struct acpi_ec          *ec = (struct acpi_ec *) data;
394
395         if (!ec)
396                 return ACPI_INTERRUPT_NOT_HANDLED;
397
398         acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
399
400         status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
401                 acpi_ec_gpe_query, ec);
402
403         if (status == AE_OK)
404                 return ACPI_INTERRUPT_HANDLED;
405         else
406                 return ACPI_INTERRUPT_NOT_HANDLED;
407 }
408
409 /* --------------------------------------------------------------------------
410                              Address Space Management
411    -------------------------------------------------------------------------- */
412
413 static acpi_status
414 acpi_ec_space_setup (
415         acpi_handle             region_handle,
416         u32                     function,
417         void                    *handler_context,
418         void                    **return_context)
419 {
420         /*
421          * The EC object is in the handler context and is needed
422          * when calling the acpi_ec_space_handler.
423          */
424         if(function == ACPI_REGION_DEACTIVATE) 
425                 *return_context = NULL;
426         else 
427                 *return_context = handler_context;
428
429         return AE_OK;
430 }
431
432
433 static acpi_status
434 acpi_ec_space_handler (
435         u32                     function,
436         acpi_physical_address   address,
437         u32                     bit_width,
438         acpi_integer            *value,
439         void                    *handler_context,
440         void                    *region_context)
441 {
442         int                     result = 0;
443         struct acpi_ec          *ec = NULL;
444         u32                     temp = 0;
445         acpi_integer            f_v = 0;
446         int                     i = 0;
447
448         ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
449
450         if ((address > 0xFF) || !value || !handler_context)
451                 return_VALUE(AE_BAD_PARAMETER);
452
453         if(bit_width != 8) {
454                 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
455                 if (acpi_strict)
456                         return_VALUE(AE_BAD_PARAMETER);
457         }
458
459         ec = (struct acpi_ec *) handler_context;
460
461 next_byte:
462         switch (function) {
463         case ACPI_READ:
464                 result = acpi_ec_read(ec, (u8) address, &temp);
465                 *value = (acpi_integer) temp;
466                 break;
467         case ACPI_WRITE:
468                 result = acpi_ec_write(ec, (u8) address, (u8) *value);
469                 break;
470         default:
471                 result = -EINVAL;
472                 goto out;
473                 break;
474         }
475
476         bit_width -= 8;
477         if(bit_width){
478
479                 if(function == ACPI_READ)
480                         f_v |= (acpi_integer) (*value) << 8*i;
481                 if(function == ACPI_WRITE)
482                         (*value) >>=8; 
483                 i++;
484                 goto next_byte;
485         }
486
487
488         if(function == ACPI_READ){
489                 f_v |= (acpi_integer) (*value) << 8*i;
490                 *value = f_v;
491         }
492
493                 
494 out:
495         switch (result) {
496         case -EINVAL:
497                 return_VALUE(AE_BAD_PARAMETER);
498                 break;
499         case -ENODEV:
500                 return_VALUE(AE_NOT_FOUND);
501                 break;
502         case -ETIME:
503                 return_VALUE(AE_TIME);
504                 break;
505         default:
506                 return_VALUE(AE_OK);
507         }
508         
509
510 }
511
512
513 /* --------------------------------------------------------------------------
514                               FS Interface (/proc)
515    -------------------------------------------------------------------------- */
516
517 static struct proc_dir_entry    *acpi_ec_dir;
518
519
520 static int
521 acpi_ec_read_info (struct seq_file *seq, void *offset)
522 {
523         struct acpi_ec          *ec = (struct acpi_ec *) seq->private;
524
525         ACPI_FUNCTION_TRACE("acpi_ec_read_info");
526
527         if (!ec)
528                 goto end;
529
530         seq_printf(seq, "gpe bit:                 0x%02x\n",
531                 (u32) ec->gpe_bit);
532         seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
533                 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
534         seq_printf(seq, "use global lock:         %s\n",
535                 ec->global_lock?"yes":"no");
536
537 end:
538         return_VALUE(0);
539 }
540
541 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
542 {
543         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
544 }
545
546 static struct file_operations acpi_ec_info_ops = {
547         .open           = acpi_ec_info_open_fs,
548         .read           = seq_read,
549         .llseek         = seq_lseek,
550         .release        = single_release,
551         .owner = THIS_MODULE,
552 };
553
554 static int
555 acpi_ec_add_fs (
556         struct acpi_device      *device)
557 {
558         struct proc_dir_entry   *entry = NULL;
559
560         ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
561
562         if (!acpi_device_dir(device)) {
563                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
564                         acpi_ec_dir);
565                 if (!acpi_device_dir(device))
566                         return_VALUE(-ENODEV);
567         }
568
569         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
570                 acpi_device_dir(device));
571         if (!entry)
572                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
573                         "Unable to create '%s' fs entry\n",
574                         ACPI_EC_FILE_INFO));
575         else {
576                 entry->proc_fops = &acpi_ec_info_ops;
577                 entry->data = acpi_driver_data(device);
578                 entry->owner = THIS_MODULE;
579         }
580
581         return_VALUE(0);
582 }
583
584
585 static int
586 acpi_ec_remove_fs (
587         struct acpi_device      *device)
588 {
589         ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
590
591         if (acpi_device_dir(device)) {
592                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
593                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
594                 acpi_device_dir(device) = NULL;
595         }
596
597         return_VALUE(0);
598 }
599
600
601 /* --------------------------------------------------------------------------
602                                Driver Interface
603    -------------------------------------------------------------------------- */
604
605 static int
606 acpi_ec_add (
607         struct acpi_device      *device)
608 {
609         int                     result = 0;
610         acpi_status             status = AE_OK;
611         struct acpi_ec          *ec = NULL;
612         unsigned long           uid;
613
614         ACPI_FUNCTION_TRACE("acpi_ec_add");
615
616         if (!device)
617                 return_VALUE(-EINVAL);
618
619         ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
620         if (!ec)
621                 return_VALUE(-ENOMEM);
622         memset(ec, 0, sizeof(struct acpi_ec));
623
624         ec->handle = device->handle;
625         ec->uid = -1;
626         spin_lock_init(&ec->lock);
627         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
628         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
629         acpi_driver_data(device) = ec;
630
631         /* Use the global lock for all EC transactions? */
632         acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
633
634         /* If our UID matches the UID for the ECDT-enumerated EC,
635            we now have the *real* EC info, so kill the makeshift one.*/
636         acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
637         if (ec_ecdt && ec_ecdt->uid == uid) {
638                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
639                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
640         
641                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
642
643                 kfree(ec_ecdt);
644         }
645
646         /* Get GPE bit assignment (EC events). */
647         /* TODO: Add support for _GPE returning a package */
648         status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
649         if (ACPI_FAILURE(status)) {
650                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
651                         "Error obtaining GPE bit assignment\n"));
652                 result = -ENODEV;
653                 goto end;
654         }
655
656         result = acpi_ec_add_fs(device);
657         if (result)
658                 goto end;
659
660         printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
661                 acpi_device_name(device), acpi_device_bid(device),
662                 (u32) ec->gpe_bit);
663
664         if (!first_ec)
665                 first_ec = device;
666
667 end:
668         if (result)
669                 kfree(ec);
670
671         return_VALUE(result);
672 }
673
674
675 static int
676 acpi_ec_remove (
677         struct acpi_device      *device,
678         int                     type)
679 {
680         struct acpi_ec          *ec = NULL;
681
682         ACPI_FUNCTION_TRACE("acpi_ec_remove");
683
684         if (!device)
685                 return_VALUE(-EINVAL);
686
687         ec = acpi_driver_data(device);
688
689         acpi_ec_remove_fs(device);
690
691         kfree(ec);
692
693         return_VALUE(0);
694 }
695
696
697 static acpi_status
698 acpi_ec_io_ports (
699         struct acpi_resource    *resource,
700         void                    *context)
701 {
702         struct acpi_ec          *ec = (struct acpi_ec *) context;
703         struct acpi_generic_address *addr;
704
705         if (resource->id != ACPI_RSTYPE_IO) {
706                 return AE_OK;
707         }
708
709         /*
710          * The first address region returned is the data port, and
711          * the second address region returned is the status/command
712          * port.
713          */
714         if (ec->data_addr.register_bit_width == 0) {
715                 addr = &ec->data_addr;
716         } else if (ec->command_addr.register_bit_width == 0) {
717                 addr = &ec->command_addr;
718         } else {
719                 return AE_CTRL_TERMINATE;
720         }
721
722         addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
723         addr->register_bit_width = 8;
724         addr->register_bit_offset = 0;
725         addr->address = resource->data.io.min_base_address;
726
727         return AE_OK;
728 }
729
730
731 static int
732 acpi_ec_start (
733         struct acpi_device      *device)
734 {
735         acpi_status             status = AE_OK;
736         struct acpi_ec          *ec = NULL;
737
738         ACPI_FUNCTION_TRACE("acpi_ec_start");
739
740         if (!device)
741                 return_VALUE(-EINVAL);
742
743         ec = acpi_driver_data(device);
744
745         if (!ec)
746                 return_VALUE(-EINVAL);
747
748         /*
749          * Get I/O port addresses. Convert to GAS format.
750          */
751         status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
752                 acpi_ec_io_ports, ec);
753         if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
754                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
755                 return_VALUE(-ENODEV);
756         }
757
758         ec->status_addr = ec->command_addr;
759
760         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
761                 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
762                 (u32) ec->data_addr.address));
763
764         /*
765          * Install GPE handler
766          */
767         status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
768                 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
769         if (ACPI_FAILURE(status)) {
770                 return_VALUE(-ENODEV);
771         }
772         acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
773         acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
774
775         status = acpi_install_address_space_handler (ec->handle,
776                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
777                         &acpi_ec_space_setup, ec);
778         if (ACPI_FAILURE(status)) {
779                 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
780                 return_VALUE(-ENODEV);
781         }
782
783         return_VALUE(AE_OK);
784 }
785
786
787 static int
788 acpi_ec_stop (
789         struct acpi_device      *device,
790         int                     type)
791 {
792         acpi_status             status = AE_OK;
793         struct acpi_ec          *ec = NULL;
794
795         ACPI_FUNCTION_TRACE("acpi_ec_stop");
796
797         if (!device)
798                 return_VALUE(-EINVAL);
799
800         ec = acpi_driver_data(device);
801
802         status = acpi_remove_address_space_handler(ec->handle,
803                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
804         if (ACPI_FAILURE(status))
805                 return_VALUE(-ENODEV);
806
807         status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
808         if (ACPI_FAILURE(status))
809                 return_VALUE(-ENODEV);
810
811         return_VALUE(0);
812 }
813
814 static acpi_status __init
815 acpi_fake_ecdt_callback (
816         acpi_handle     handle,
817         u32             Level,
818         void            *context,
819         void            **retval)
820 {
821         acpi_status     status;
822
823         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
824                 acpi_ec_io_ports, ec_ecdt);
825         if (ACPI_FAILURE(status))
826                 return status;
827         ec_ecdt->status_addr = ec_ecdt->command_addr;
828
829         ec_ecdt->uid = -1;
830         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
831
832         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
833         if (ACPI_FAILURE(status))
834                 return status;
835         spin_lock_init(&ec_ecdt->lock);
836         ec_ecdt->global_lock = TRUE;
837         ec_ecdt->handle = handle;
838
839         printk(KERN_INFO PREFIX  "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
840                 (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
841                 (u32) ec_ecdt->data_addr.address);
842
843         return AE_CTRL_TERMINATE;
844 }
845
846 /*
847  * Some BIOS (such as some from Gateway laptops) access EC region very early
848  * such as in BAT0._INI or EC._INI before an EC device is found and
849  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
850  * required, but if EC regison is accessed early, it is required.
851  * The routine tries to workaround the BIOS bug by pre-scan EC device
852  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
853  * op region (since _REG isn't invoked yet). The assumption is true for
854  * all systems found.
855  */
856 static int __init
857 acpi_ec_fake_ecdt(void)
858 {
859         acpi_status     status;
860         int             ret = 0;
861
862         printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
863
864         ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
865         if (!ec_ecdt) {
866                 ret = -ENOMEM;
867                 goto error;
868         }
869         memset(ec_ecdt, 0, sizeof(struct acpi_ec));
870
871         status = acpi_get_devices (ACPI_EC_HID,
872                                 acpi_fake_ecdt_callback,
873                                 NULL,
874                                 NULL);
875         if (ACPI_FAILURE(status)) {
876                 kfree(ec_ecdt);
877                 ec_ecdt = NULL;
878                 ret = -ENODEV;
879                 goto error;
880         }
881         return 0;
882 error:
883         printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
884         return ret;
885 }
886
887 static int __init
888 acpi_ec_get_real_ecdt(void)
889 {
890         acpi_status             status;
891         struct acpi_table_ecdt  *ecdt_ptr;
892
893         status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING, 
894                 (struct acpi_table_header **) &ecdt_ptr);
895         if (ACPI_FAILURE(status))
896                 return -ENODEV;
897
898         printk(KERN_INFO PREFIX "Found ECDT\n");
899
900         /*
901          * Generate a temporary ec context to use until the namespace is scanned
902          */
903         ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
904         if (!ec_ecdt)
905                 return -ENOMEM;
906         memset(ec_ecdt, 0, sizeof(struct acpi_ec));
907
908         ec_ecdt->command_addr = ecdt_ptr->ec_control;
909         ec_ecdt->status_addr = ecdt_ptr->ec_control;
910         ec_ecdt->data_addr = ecdt_ptr->ec_data;
911         ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
912         spin_lock_init(&ec_ecdt->lock);
913         /* use the GL just to be safe */
914         ec_ecdt->global_lock = TRUE;
915         ec_ecdt->uid = ecdt_ptr->uid;
916
917         status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
918         if (ACPI_FAILURE(status)) {
919                 goto error;
920         }
921
922         return 0;
923 error:
924         printk(KERN_ERR PREFIX "Could not use ECDT\n");
925         kfree(ec_ecdt);
926         ec_ecdt = NULL;
927
928         return -ENODEV;
929 }
930
931 static int __initdata acpi_fake_ecdt_enabled;
932 int __init
933 acpi_ec_ecdt_probe (void)
934 {
935         acpi_status             status;
936         int                     ret;
937
938         ret = acpi_ec_get_real_ecdt();
939         /* Try to make a fake ECDT */
940         if (ret && acpi_fake_ecdt_enabled) {
941                 ret = acpi_ec_fake_ecdt();
942         }
943
944         if (ret)
945                 return 0;
946
947         /*
948          * Install GPE handler
949          */
950         status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
951                 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
952                 ec_ecdt);
953         if (ACPI_FAILURE(status)) {
954                 goto error;
955         }
956         acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
957         acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
958
959         status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
960                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
961                         &acpi_ec_space_setup, ec_ecdt);
962         if (ACPI_FAILURE(status)) {
963                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
964                         &acpi_ec_gpe_handler);
965                 goto error;
966         }
967
968         return 0;
969
970 error:
971         printk(KERN_ERR PREFIX "Could not use ECDT\n");
972         kfree(ec_ecdt);
973         ec_ecdt = NULL;
974
975         return -ENODEV;
976 }
977
978
979 static int __init acpi_ec_init (void)
980 {
981         int                     result = 0;
982
983         ACPI_FUNCTION_TRACE("acpi_ec_init");
984
985         if (acpi_disabled)
986                 return_VALUE(0);
987
988         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
989         if (!acpi_ec_dir)
990                 return_VALUE(-ENODEV);
991
992         /* Now register the driver for the EC */
993         result = acpi_bus_register_driver(&acpi_ec_driver);
994         if (result < 0) {
995                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
996                 return_VALUE(-ENODEV);
997         }
998
999         return_VALUE(result);
1000 }
1001
1002 subsys_initcall(acpi_ec_init);
1003
1004 /* EC driver currently not unloadable */
1005 #if 0
1006 static void __exit
1007 acpi_ec_exit (void)
1008 {
1009         ACPI_FUNCTION_TRACE("acpi_ec_exit");
1010
1011         acpi_bus_unregister_driver(&acpi_ec_driver);
1012
1013         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1014
1015         return_VOID;
1016 }
1017 #endif /* 0 */
1018
1019 static int __init acpi_fake_ecdt_setup(char *str)
1020 {
1021         acpi_fake_ecdt_enabled = 1;
1022         return 0;
1023 }
1024 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);