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