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