Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
  3  *
  4  *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  5  *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  6  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  9  *
 10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 11  *
 12  *  This program is free software; you can redistribute it and/or modify
 13  *  it under the terms of the GNU General Public License as published by
 14  *  the Free Software Foundation; either version 2 of the License, or (at
 15  *  your option) any later version.
 16  *
 17  *  This program is distributed in the hope that it will be useful, but
 18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
 19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 20  *  General Public License for more details.
 21  *
 22  *  You should have received a copy of the GNU General Public License along
 23  *  with this program; if not, write to the Free Software Foundation, Inc.,
 24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 25  *
 26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 27  */
 28 
 29 /* Uncomment next line to get verbose print outs*/
 30 /* #define DEBUG */
 31 
 32 #include <linux/kernel.h>
 33 #include <linux/module.h>
 34 #include <linux/init.h>
 35 #include <linux/types.h>
 36 #include <linux/delay.h>
 37 #include <linux/proc_fs.h>
 38 #include <linux/seq_file.h>
 39 #include <linux/interrupt.h>
 40 #include <linux/list.h>
 41 #include <asm/io.h>
 42 #include <acpi/acpi_bus.h>
 43 #include <acpi/acpi_drivers.h>
 44 #include <acpi/actypes.h>
 45 
 46 #define ACPI_EC_CLASS                   "embedded_controller"
 47 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
 48 #define ACPI_EC_FILE_INFO               "info"
 49 
 50 #undef PREFIX
 51 #define PREFIX                          "ACPI: EC: "
 52 
 53 /* EC status register */
 54 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
 55 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
 56 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
 57 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
 58 
 59 /* EC commands */
 60 enum ec_command {
 61         ACPI_EC_COMMAND_READ = 0x80,
 62         ACPI_EC_COMMAND_WRITE = 0x81,
 63         ACPI_EC_BURST_ENABLE = 0x82,
 64         ACPI_EC_BURST_DISABLE = 0x83,
 65         ACPI_EC_COMMAND_QUERY = 0x84,
 66 };
 67 
 68 /* EC events */
 69 enum ec_event {
 70         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
 71         ACPI_EC_EVENT_IBF_0,            /* Input buffer empty */
 72 };
 73 
 74 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
 75 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
 76 
 77 enum {
 78         EC_FLAGS_WAIT_GPE = 0,          /* Don't check status until GPE arrives */
 79         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
 80         EC_FLAGS_GPE_MODE,              /* Expect GPE to be sent for status change */
 81         EC_FLAGS_NO_ADDRESS_GPE,        /* Expect GPE only for non-address event */
 82         EC_FLAGS_ADDRESS,               /* Address is being written */
 83         EC_FLAGS_NO_WDATA_GPE,          /* Don't expect WDATA GPE event */
 84         EC_FLAGS_WDATA,                 /* Data is being written */
 85         EC_FLAGS_NO_OBF1_GPE,           /* Don't expect GPE before read */
 86 };
 87 
 88 static int acpi_ec_remove(struct acpi_device *device, int type);
 89 static int acpi_ec_start(struct acpi_device *device);
 90 static int acpi_ec_stop(struct acpi_device *device, int type);
 91 static int acpi_ec_add(struct acpi_device *device);
 92 
 93 static const struct acpi_device_id ec_device_ids[] = {
 94         {"PNP0C09", 0},
 95         {"", 0},
 96 };
 97 
 98 static struct acpi_driver acpi_ec_driver = {
 99         .name = "ec",
100         .class = ACPI_EC_CLASS,
101         .ids = ec_device_ids,
102         .ops = {
103                 .add = acpi_ec_add,
104                 .remove = acpi_ec_remove,
105                 .start = acpi_ec_start,
106                 .stop = acpi_ec_stop,
107                 },
108 };
109 
110 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
111 /* External interfaces use first EC only, so remember */
112 typedef int (*acpi_ec_query_func) (void *data);
113 
114 struct acpi_ec_query_handler {
115         struct list_head node;
116         acpi_ec_query_func func;
117         acpi_handle handle;
118         void *data;
119         u8 query_bit;
120 };
121 
122 static struct acpi_ec {
123         acpi_handle handle;
124         unsigned long gpe;
125         unsigned long command_addr;
126         unsigned long data_addr;
127         unsigned long global_lock;
128         unsigned long flags;
129         struct mutex lock;
130         wait_queue_head_t wait;
131         struct list_head list;
132         u8 handlers_installed;
133 } *boot_ec, *first_ec;
134 
135 /* --------------------------------------------------------------------------
136                              Transaction Management
137    -------------------------------------------------------------------------- */
138 
139 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
140 {
141         u8 x = inb(ec->command_addr);
142         pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
143         return x;
144 }
145 
146 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
147 {
148         u8 x = inb(ec->data_addr);
149         pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
150         return inb(ec->data_addr);
151 }
152 
153 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
154 {
155         pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
156         outb(command, ec->command_addr);
157 }
158 
159 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
160 {
161         pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
162         outb(data, ec->data_addr);
163 }
164 
165 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
166 {
167         if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
168                 return 0;
169         if (event == ACPI_EC_EVENT_OBF_1) {
170                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
171                         return 1;
172         } else if (event == ACPI_EC_EVENT_IBF_0) {
173                 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
174                         return 1;
175         }
176 
177         return 0;
178 }
179 
180 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
181 {
182         int ret = 0;
183 
184         if (unlikely(event == ACPI_EC_EVENT_OBF_1 &&
185                      test_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags)))
186                 force_poll = 1;
187         if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) &&
188                      test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags)))
189                 force_poll = 1;
190         if (unlikely(test_bit(EC_FLAGS_WDATA, &ec->flags) &&
191                      test_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags)))
192                 force_poll = 1;
193         if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
194             likely(!force_poll)) {
195                 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
196                                        msecs_to_jiffies(ACPI_EC_DELAY)))
197                         goto end;
198                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
199                 if (acpi_ec_check_status(ec, event)) {
200                         if (event == ACPI_EC_EVENT_OBF_1) {
201                                 /* miss OBF_1 GPE, don't expect it */
202                                 pr_info(PREFIX "missing OBF confirmation, "
203                                         "don't expect it any longer.\n");
204                                 set_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags);
205                         } else if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
206                                 /* miss address GPE, don't expect it anymore */
207                                 pr_info(PREFIX "missing address confirmation, "
208                                         "don't expect it any longer.\n");
209                                 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
210                         } else if (test_bit(EC_FLAGS_WDATA, &ec->flags)) {
211                                 /* miss write data GPE, don't expect it */
212                                 pr_info(PREFIX "missing write data confirmation, "
213                                         "don't expect it any longer.\n");
214                                 set_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags);
215                         } else {
216                                 /* missing GPEs, switch back to poll mode */
217                                 if (printk_ratelimit())
218                                         pr_info(PREFIX "missing confirmations, "
219                                                 "switch off interrupt mode.\n");
220                                 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
221                         }
222                         goto end;
223                 }
224         } else {
225                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
226                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
227                 while (time_before(jiffies, delay)) {
228                         if (acpi_ec_check_status(ec, event))
229                                 goto end;
230                 }
231         }
232         pr_err(PREFIX "acpi_ec_wait timeout,"
233                                " status = %d, expect_event = %d\n",
234                                acpi_ec_read_status(ec), event);
235         ret = -ETIME;
236       end:
237         clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
238         return ret;
239 }
240 
241 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
242                                         const u8 * wdata, unsigned wdata_len,
243                                         u8 * rdata, unsigned rdata_len,
244                                         int force_poll)
245 {
246         int result = 0;
247         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
248         acpi_ec_write_cmd(ec, command);
249         pr_debug(PREFIX "transaction start\n");
250         for (; wdata_len > 0; --wdata_len) {
251                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
252                 if (result) {
253                         pr_err(PREFIX
254                                "write_cmd timeout, command = %d\n", command);
255                         goto end;
256                 }
257                 /* mark the address byte written to EC */
258                 if (rdata_len + wdata_len > 1)
259                         set_bit(EC_FLAGS_ADDRESS, &ec->flags);
260                 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
261                 acpi_ec_write_data(ec, *(wdata++));
262         }
263 
264         if (!rdata_len) {
265                 set_bit(EC_FLAGS_WDATA, &ec->flags);
266                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
267                 if (result) {
268                         pr_err(PREFIX
269                                "finish-write timeout, command = %d\n", command);
270                         goto end;
271                 }
272         } else if (command == ACPI_EC_COMMAND_QUERY)
273                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
274 
275         for (; rdata_len > 0; --rdata_len) {
276                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
277                 if (result) {
278                         pr_err(PREFIX "read timeout, command = %d\n", command);
279                         goto end;
280                 }
281                 /* Don't expect GPE after last read */
282                 if (rdata_len > 1)
283                         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
284                 *(rdata++) = acpi_ec_read_data(ec);
285         }
286       end:
287         pr_debug(PREFIX "transaction end\n");
288         return result;
289 }
290 
291 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
292                                const u8 * wdata, unsigned wdata_len,
293                                u8 * rdata, unsigned rdata_len,
294                                int force_poll)
295 {
296         int status;
297         u32 glk;
298 
299         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
300                 return -EINVAL;
301 
302         if (rdata)
303                 memset(rdata, 0, rdata_len);
304 
305         mutex_lock(&ec->lock);
306         if (ec->global_lock) {
307                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
308                 if (ACPI_FAILURE(status)) {
309                         mutex_unlock(&ec->lock);
310                         return -ENODEV;
311                 }
312         }
313 
314         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
315         if (status) {
316                 pr_err(PREFIX "input buffer is not empty, "
317                                 "aborting transaction\n");
318                 goto end;
319         }
320 
321         status = acpi_ec_transaction_unlocked(ec, command,
322                                               wdata, wdata_len,
323                                               rdata, rdata_len,
324                                               force_poll);
325 
326       end:
327 
328         if (ec->global_lock)
329                 acpi_release_global_lock(glk);
330         mutex_unlock(&ec->lock);
331 
332         return status;
333 }
334 
335 /*
336  * Note: samsung nv5000 doesn't work with ec burst mode.
337  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
338  */
339 int acpi_ec_burst_enable(struct acpi_ec *ec)
340 {
341         u8 d;
342         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
343 }
344 
345 int acpi_ec_burst_disable(struct acpi_ec *ec)
346 {
347         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
348 }
349 
350 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
351 {
352         int result;
353         u8 d;
354 
355         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
356                                      &address, 1, &d, 1, 0);
357         *data = d;
358         return result;
359 }
360 
361 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
362 {
363         u8 wdata[2] = { address, data };
364         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
365                                    wdata, 2, NULL, 0, 0);
366 }
367 
368 /*
369  * Externally callable EC access functions. For now, assume 1 EC only
370  */
371 int ec_burst_enable(void)
372 {
373         if (!first_ec)
374                 return -ENODEV;
375         return acpi_ec_burst_enable(first_ec);
376 }
377 
378 EXPORT_SYMBOL(ec_burst_enable);
379 
380 int ec_burst_disable(void)
381 {
382         if (!first_ec)
383                 return -ENODEV;
384         return acpi_ec_burst_disable(first_ec);
385 }
386 
387 EXPORT_SYMBOL(ec_burst_disable);
388 
389 int ec_read(u8 addr, u8 * val)
390 {
391         int err;
392         u8 temp_data;
393 
394         if (!first_ec)
395                 return -ENODEV;
396 
397         err = acpi_ec_read(first_ec, addr, &temp_data);
398 
399         if (!err) {
400                 *val = temp_data;
401                 return 0;
402         } else
403                 return err;
404 }
405 
406 EXPORT_SYMBOL(ec_read);
407 
408 int ec_write(u8 addr, u8 val)
409 {
410         int err;
411 
412         if (!first_ec)
413                 return -ENODEV;
414 
415         err = acpi_ec_write(first_ec, addr, val);
416 
417         return err;
418 }
419 
420 EXPORT_SYMBOL(ec_write);
421 
422 int ec_transaction(u8 command,
423                    const u8 * wdata, unsigned wdata_len,
424                    u8 * rdata, unsigned rdata_len,
425                    int force_poll)
426 {
427         if (!first_ec)
428                 return -ENODEV;
429 
430         return acpi_ec_transaction(first_ec, command, wdata,
431                                    wdata_len, rdata, rdata_len,
432                                    force_poll);
433 }
434 
435 EXPORT_SYMBOL(ec_transaction);
436 
437 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
438 {
439         int result;
440         u8 d;
441 
442         if (!ec || !data)
443                 return -EINVAL;
444 
445         /*
446          * Query the EC to find out which _Qxx method we need to evaluate.
447          * Note that successful completion of the query causes the ACPI_EC_SCI
448          * bit to be cleared (and thus clearing the interrupt source).
449          */
450 
451         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
452         if (result)
453                 return result;
454 
455         if (!d)
456                 return -ENODATA;
457 
458         *data = d;
459         return 0;
460 }
461 
462 /* --------------------------------------------------------------------------
463                                 Event Management
464    -------------------------------------------------------------------------- */
465 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
466                               acpi_handle handle, acpi_ec_query_func func,
467                               void *data)
468 {
469         struct acpi_ec_query_handler *handler =
470             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
471         if (!handler)
472                 return -ENOMEM;
473 
474         handler->query_bit = query_bit;
475         handler->handle = handle;
476         handler->func = func;
477         handler->data = data;
478         mutex_lock(&ec->lock);
479         list_add(&handler->node, &ec->list);
480         mutex_unlock(&ec->lock);
481         return 0;
482 }
483 
484 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
485 
486 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
487 {
488         struct acpi_ec_query_handler *handler, *tmp;
489         mutex_lock(&ec->lock);
490         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
491                 if (query_bit == handler->query_bit) {
492                         list_del(&handler->node);
493                         kfree(handler);
494                 }
495         }
496         mutex_unlock(&ec->lock);
497 }
498 
499 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
500 
501 static void acpi_ec_gpe_query(void *ec_cxt)
502 {
503         struct acpi_ec *ec = ec_cxt;
504         u8 value = 0;
505         struct acpi_ec_query_handler *handler, copy;
506 
507         if (!ec || acpi_ec_query(ec, &value))
508                 return;
509         mutex_lock(&ec->lock);
510         list_for_each_entry(handler, &ec->list, node) {
511                 if (value == handler->query_bit) {
512                         /* have custom handler for this bit */
513                         memcpy(&copy, handler, sizeof(copy));
514                         mutex_unlock(&ec->lock);
515                         if (copy.func) {
516                                 copy.func(copy.data);
517                         } else if (copy.handle) {
518                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
519                         }
520                         return;
521                 }
522         }
523         mutex_unlock(&ec->lock);
524 }
525 
526 static u32 acpi_ec_gpe_handler(void *data)
527 {
528         acpi_status status = AE_OK;
529         struct acpi_ec *ec = data;
530 
531         pr_debug(PREFIX "~~~> interrupt\n");
532         clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
533         if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
534 #if 0
535                 wake_up(&ec->wait);
536 #else
537                 // hack ...
538                 if (waitqueue_active(&ec->wait)) {
539                         struct task_struct *task;
540 
541                         task = list_entry(ec->wait.task_list.next,
542                                           wait_queue_t, task_list)->private;
543                         if (task)
544                                 wake_up_process(task);
545                 }
546 #endif
547 
548         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
549                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
550                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
551                                 acpi_ec_gpe_query, ec);
552         } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
553                 /* this is non-query, must be confirmation */
554                 if (printk_ratelimit())
555                         pr_info(PREFIX "non-query interrupt received,"
556                                 " switching to interrupt mode\n");
557                 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
558         }
559 
560         return ACPI_SUCCESS(status) ?
561             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
562 }
563 
564 /* --------------------------------------------------------------------------
565                              Address Space Management
566    -------------------------------------------------------------------------- */
567 
568 static acpi_status
569 acpi_ec_space_setup(acpi_handle region_handle,
570                     u32 function, void *handler_context, void **return_context)
571 {
572         /*
573          * The EC object is in the handler context and is needed
574          * when calling the acpi_ec_space_handler.
575          */
576         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
577             handler_context : NULL;
578 
579         return AE_OK;
580 }
581 
582 static acpi_status
583 acpi_ec_space_handler(u32 function, acpi_physical_address address,
584                       u32 bits, acpi_integer *value,
585                       void *handler_context, void *region_context)
586 {
587         struct acpi_ec *ec = handler_context;
588         int result = 0, i;
589         u8 temp = 0;
590 
591         if ((address > 0xFF) || !value || !handler_context)
592                 return AE_BAD_PARAMETER;
593 
594         if (function != ACPI_READ && function != ACPI_WRITE)
595                 return AE_BAD_PARAMETER;
596 
597         if (bits != 8 && acpi_strict)
598                 return AE_BAD_PARAMETER;
599 
600         acpi_ec_burst_enable(ec);
601 
602         if (function == ACPI_READ) {
603                 result = acpi_ec_read(ec, address, &temp);
604                 *value = temp;
605         } else {
606                 temp = 0xff & (*value);
607                 result = acpi_ec_write(ec, address, temp);
608         }
609 
610         for (i = 8; unlikely(bits - i > 0); i += 8) {
611                 ++address;
612                 if (function == ACPI_READ) {
613                         result = acpi_ec_read(ec, address, &temp);
614                         (*value) |= ((acpi_integer)temp) << i;
615                 } else {
616                         temp = 0xff & ((*value) >> i);
617                         result = acpi_ec_write(ec, address, temp);
618                 }
619         }
620 
621         acpi_ec_burst_disable(ec);
622 
623         switch (result) {
624         case -EINVAL:
625                 return AE_BAD_PARAMETER;
626                 break;
627         case -ENODEV:
628                 return AE_NOT_FOUND;
629                 break;
630         case -ETIME:
631                 return AE_TIME;
632                 break;
633         default:
634                 return AE_OK;
635         }
636 }
637 
638 /* --------------------------------------------------------------------------
639                               FS Interface (/proc)
640    -------------------------------------------------------------------------- */
641 
642 static struct proc_dir_entry *acpi_ec_dir;
643 
644 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
645 {
646         struct acpi_ec *ec = seq->private;
647 
648         if (!ec)
649                 goto end;
650 
651         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
652         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
653                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
654         seq_printf(seq, "use global lock:\t%s\n",
655                    ec->global_lock ? "yes" : "no");
656       end:
657         return 0;
658 }
659 
660 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
661 {
662         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
663 }
664 
665 static struct file_operations acpi_ec_info_ops = {
666         .open = acpi_ec_info_open_fs,
667         .read = seq_read,
668         .llseek = seq_lseek,
669         .release = single_release,
670         .owner = THIS_MODULE,
671 };
672 
673 static int acpi_ec_add_fs(struct acpi_device *device)
674 {
675         struct proc_dir_entry *entry = NULL;
676 
677         if (!acpi_device_dir(device)) {
678                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
679                                                      acpi_ec_dir);
680                 if (!acpi_device_dir(device))
681                         return -ENODEV;
682         }
683 
684         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
685                                   acpi_device_dir(device));
686         if (!entry)
687                 return -ENODEV;
688         else {
689                 entry->proc_fops = &acpi_ec_info_ops;
690                 entry->data = acpi_driver_data(device);
691                 entry->owner = THIS_MODULE;
692         }
693 
694         return 0;
695 }
696 
697 static int acpi_ec_remove_fs(struct acpi_device *device)
698 {
699 
700         if (acpi_device_dir(device)) {
701                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
702                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
703                 acpi_device_dir(device) = NULL;
704         }
705 
706         return 0;
707 }
708 
709 /* --------------------------------------------------------------------------
710                                Driver Interface
711    -------------------------------------------------------------------------- */
712 static acpi_status
713 ec_parse_io_ports(struct acpi_resource *resource, void *context);
714 
715 static struct acpi_ec *make_acpi_ec(void)
716 {
717         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
718         if (!ec)
719                 return NULL;
720         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
721         mutex_init(&ec->lock);
722         init_waitqueue_head(&ec->wait);
723         INIT_LIST_HEAD(&ec->list);
724         return ec;
725 }
726 
727 static acpi_status
728 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
729                                void *context, void **return_value)
730 {
731         struct acpi_namespace_node *node = handle;
732         struct acpi_ec *ec = context;
733         int value = 0;
734         if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
735                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
736         }
737         return AE_OK;
738 }
739 
740 static acpi_status
741 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
742 {
743         acpi_status status;
744 
745         struct acpi_ec *ec = context;
746         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
747                                      ec_parse_io_ports, ec);
748         if (ACPI_FAILURE(status))
749                 return status;
750 
751         /* Get GPE bit assignment (EC events). */
752         /* TODO: Add support for _GPE returning a package */
753         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
754         if (ACPI_FAILURE(status))
755                 return status;
756         /* Find and register all query methods */
757         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
758                             acpi_ec_register_query_methods, ec, NULL);
759         /* Use the global lock for all EC transactions? */
760         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
761         ec->handle = handle;
762         return AE_CTRL_TERMINATE;
763 }
764 
765 static void ec_remove_handlers(struct acpi_ec *ec)
766 {
767         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
768                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
769                 pr_err(PREFIX "failed to remove space handler\n");
770         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
771                                 &acpi_ec_gpe_handler)))
772                 pr_err(PREFIX "failed to remove gpe handler\n");
773         ec->handlers_installed = 0;
774 }
775 
776 static int acpi_ec_add(struct acpi_device *device)
777 {
778         struct acpi_ec *ec = NULL;
779 
780         if (!device)
781                 return -EINVAL;
782         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
783         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
784 
785         /* Check for boot EC */
786         if (boot_ec) {
787                 if (boot_ec->handle == device->handle) {
788                         /* Pre-loaded EC from DSDT, just move pointer */
789                         ec = boot_ec;
790                         boot_ec = NULL;
791                         goto end;
792                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
793                         /* ECDT-based EC, time to shut it down */
794                         ec_remove_handlers(boot_ec);
795                         kfree(boot_ec);
796                         first_ec = boot_ec = NULL;
797                 }
798         }
799 
800         ec = make_acpi_ec();
801         if (!ec)
802                 return -ENOMEM;
803 
804         if (ec_parse_device(device->handle, 0, ec, NULL) !=
805             AE_CTRL_TERMINATE) {
806                 kfree(ec);
807                 return -EINVAL;
808         }
809         ec->handle = device->handle;
810       end:
811         if (!first_ec)
812                 first_ec = ec;
813         acpi_driver_data(device) = ec;
814         acpi_ec_add_fs(device);
815         pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
816                           ec->gpe, ec->command_addr, ec->data_addr);
817         pr_info(PREFIX "driver started in %s mode\n",
818                 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
819         return 0;
820 }
821 
822 static int acpi_ec_remove(struct acpi_device *device, int type)
823 {
824         struct acpi_ec *ec;
825         struct acpi_ec_query_handler *handler, *tmp;
826 
827         if (!device)
828                 return -EINVAL;
829 
830         ec = acpi_driver_data(device);
831         mutex_lock(&ec->lock);
832         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
833                 list_del(&handler->node);
834                 kfree(handler);
835         }
836         mutex_unlock(&ec->lock);
837         acpi_ec_remove_fs(device);
838         acpi_driver_data(device) = NULL;
839         if (ec == first_ec)
840                 first_ec = NULL;
841         kfree(ec);
842         return 0;
843 }
844 
845 static acpi_status
846 ec_parse_io_ports(struct acpi_resource *resource, void *context)
847 {
848         struct acpi_ec *ec = context;
849 
850         if (resource->type != ACPI_RESOURCE_TYPE_IO)
851                 return AE_OK;
852 
853         /*
854          * The first address region returned is the data port, and
855          * the second address region returned is the status/command
856          * port.
857          */
858         if (ec->data_addr == 0)
859                 ec->data_addr = resource->data.io.minimum;
860         else if (ec->command_addr == 0)
861                 ec->command_addr = resource->data.io.minimum;
862         else
863                 return AE_CTRL_TERMINATE;
864 
865         return AE_OK;
866 }
867 
868 static int ec_install_handlers(struct acpi_ec *ec)
869 {
870         acpi_status status;
871         if (ec->handlers_installed)
872                 return 0;
873         status = acpi_install_gpe_handler(NULL, ec->gpe,
874                                           ACPI_GPE_EDGE_TRIGGERED,
875                                           &acpi_ec_gpe_handler, ec);
876         if (ACPI_FAILURE(status))
877                 return -ENODEV;
878 
879         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
880         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
881 
882         status = acpi_install_address_space_handler(ec->handle,
883                                                     ACPI_ADR_SPACE_EC,
884                                                     &acpi_ec_space_handler,
885                                                     &acpi_ec_space_setup, ec);
886         if (ACPI_FAILURE(status)) {
887                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
888                 return -ENODEV;
889         }
890 
891         ec->handlers_installed = 1;
892         return 0;
893 }
894 
895 static int acpi_ec_start(struct acpi_device *device)
896 {
897         struct acpi_ec *ec;
898         int ret = 0;
899 
900         if (!device)
901                 return -EINVAL;
902 
903         ec = acpi_driver_data(device);
904 
905         if (!ec)
906                 return -EINVAL;
907 
908         ret = ec_install_handlers(ec);
909 
910         /* EC is fully operational, allow queries */
911         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
912         return ret;
913 }
914 
915 static int acpi_ec_stop(struct acpi_device *device, int type)
916 {
917         struct acpi_ec *ec;
918         if (!device)
919                 return -EINVAL;
920         ec = acpi_driver_data(device);
921         if (!ec)
922                 return -EINVAL;
923         ec_remove_handlers(ec);
924 
925         return 0;
926 }
927 
928 int __init acpi_boot_ec_enable(void)
929 {
930         if (!boot_ec || boot_ec->handlers_installed)
931                 return 0;
932         if (!ec_install_handlers(boot_ec)) {
933                 first_ec = boot_ec;
934                 return 0;
935         }
936         return -EFAULT;
937 }
938 
939 int __init acpi_ec_ecdt_probe(void)
940 {
941         int ret;
942         acpi_status status;
943         struct acpi_table_ecdt *ecdt_ptr;
944 
945         boot_ec = make_acpi_ec();
946         if (!boot_ec)
947                 return -ENOMEM;
948         /*
949          * Generate a boot ec context
950          */
951         status = acpi_get_table(ACPI_SIG_ECDT, 1,
952                                 (struct acpi_table_header **)&ecdt_ptr);
953         if (ACPI_SUCCESS(status)) {
954                 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
955                 boot_ec->command_addr = ecdt_ptr->control.address;
956                 boot_ec->data_addr = ecdt_ptr->data.address;
957                 boot_ec->gpe = ecdt_ptr->gpe;
958                 boot_ec->handle = ACPI_ROOT_OBJECT;
959         } else {
960                 /* This workaround is needed only on some broken machines,
961                  * which require early EC, but fail to provide ECDT */
962                 acpi_handle x;
963                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
964                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
965                                                 boot_ec, NULL);
966                 /* Check that acpi_get_devices actually find something */
967                 if (ACPI_FAILURE(status) || !boot_ec->handle)
968                         goto error;
969                 /* We really need to limit this workaround, the only ASUS,
970                  * which needs it, has fake EC._INI method, so use it as flag.
971                  * Keep boot_ec struct as it will be needed soon.
972                  */
973                 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
974                         return -ENODEV;
975         }
976 
977         ret = ec_install_handlers(boot_ec);
978         if (!ret) {
979                 first_ec = boot_ec;
980                 return 0;
981         }
982       error:
983         kfree(boot_ec);
984         boot_ec = NULL;
985         return -ENODEV;
986 }
987 
988 static int __init acpi_ec_init(void)
989 {
990         int result = 0;
991 
992         if (acpi_disabled)
993                 return 0;
994 
995         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
996         if (!acpi_ec_dir)
997                 return -ENODEV;
998 
999         /* Now register the driver for the EC */
1000         result = acpi_bus_register_driver(&acpi_ec_driver);
1001         if (result < 0) {
1002                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1003                 return -ENODEV;
1004         }
1005 
1006         return result;
1007 }
1008 
1009 subsys_initcall(acpi_ec_init);
1010 
1011 /* EC driver currently not unloadable */
1012 #if 0
1013 static void __exit acpi_ec_exit(void)
1014 {
1015 
1016         acpi_bus_unregister_driver(&acpi_ec_driver);
1017 
1018         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1019 
1020         return;
1021 }
1022 #endif  /* 0 */
1023 
  This page was automatically generated by the LXR engine.