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