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