Merge branch 'processor-procfs-2.6.32' into release
[deliverable/linux.git] / drivers / acpi / ec.c
1 /*
2 * ec.c - ACPI Embedded Controller Driver (v2.1)
3 *
4 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
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 printout */
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 <linux/spinlock.h>
42 #include <asm/io.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.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 #define PREFIX "ACPI: EC: "
51
52 /* EC status register */
53 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
54 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
55 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
56 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
57
58 /* EC commands */
59 enum ec_command {
60 ACPI_EC_COMMAND_READ = 0x80,
61 ACPI_EC_COMMAND_WRITE = 0x81,
62 ACPI_EC_BURST_ENABLE = 0x82,
63 ACPI_EC_BURST_DISABLE = 0x83,
64 ACPI_EC_COMMAND_QUERY = 0x84,
65 };
66
67 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
68 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
69 #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
70 #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
71
72 #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
73 per one transaction */
74
75 enum {
76 EC_FLAGS_QUERY_PENDING, /* Query is pending */
77 EC_FLAGS_GPE_STORM, /* GPE storm detected */
78 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
79 * OpReg are installed */
80 };
81
82 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
83 /* External interfaces use first EC only, so remember */
84 typedef int (*acpi_ec_query_func) (void *data);
85
86 struct acpi_ec_query_handler {
87 struct list_head node;
88 acpi_ec_query_func func;
89 acpi_handle handle;
90 void *data;
91 u8 query_bit;
92 };
93
94 struct transaction {
95 const u8 *wdata;
96 u8 *rdata;
97 unsigned short irq_count;
98 u8 command;
99 u8 wi;
100 u8 ri;
101 u8 wlen;
102 u8 rlen;
103 bool done;
104 };
105
106 static struct acpi_ec {
107 acpi_handle handle;
108 unsigned long gpe;
109 unsigned long command_addr;
110 unsigned long data_addr;
111 unsigned long global_lock;
112 unsigned long flags;
113 struct mutex lock;
114 wait_queue_head_t wait;
115 struct list_head list;
116 struct transaction *curr;
117 spinlock_t curr_lock;
118 } *boot_ec, *first_ec;
119
120 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
121
122 /* --------------------------------------------------------------------------
123 Transaction Management
124 -------------------------------------------------------------------------- */
125
126 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
127 {
128 u8 x = inb(ec->command_addr);
129 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
130 return x;
131 }
132
133 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
134 {
135 u8 x = inb(ec->data_addr);
136 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
137 return x;
138 }
139
140 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
141 {
142 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
143 outb(command, ec->command_addr);
144 }
145
146 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
147 {
148 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
149 outb(data, ec->data_addr);
150 }
151
152 static int ec_transaction_done(struct acpi_ec *ec)
153 {
154 unsigned long flags;
155 int ret = 0;
156 spin_lock_irqsave(&ec->curr_lock, flags);
157 if (!ec->curr || ec->curr->done)
158 ret = 1;
159 spin_unlock_irqrestore(&ec->curr_lock, flags);
160 return ret;
161 }
162
163 static void start_transaction(struct acpi_ec *ec)
164 {
165 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
166 ec->curr->done = false;
167 acpi_ec_write_cmd(ec, ec->curr->command);
168 }
169
170 static void advance_transaction(struct acpi_ec *ec, u8 status)
171 {
172 unsigned long flags;
173 spin_lock_irqsave(&ec->curr_lock, flags);
174 if (!ec->curr)
175 goto unlock;
176 if (ec->curr->wlen > ec->curr->wi) {
177 if ((status & ACPI_EC_FLAG_IBF) == 0)
178 acpi_ec_write_data(ec,
179 ec->curr->wdata[ec->curr->wi++]);
180 else
181 goto err;
182 } else if (ec->curr->rlen > ec->curr->ri) {
183 if ((status & ACPI_EC_FLAG_OBF) == 1) {
184 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
185 if (ec->curr->rlen == ec->curr->ri)
186 ec->curr->done = true;
187 } else
188 goto err;
189 } else if (ec->curr->wlen == ec->curr->wi &&
190 (status & ACPI_EC_FLAG_IBF) == 0)
191 ec->curr->done = true;
192 goto unlock;
193 err:
194 /* false interrupt, state didn't change */
195 if (in_interrupt())
196 ++ec->curr->irq_count;
197 unlock:
198 spin_unlock_irqrestore(&ec->curr_lock, flags);
199 }
200
201 static void acpi_ec_gpe_query(void *ec_cxt);
202
203 static int ec_check_sci(struct acpi_ec *ec, u8 state)
204 {
205 if (state & ACPI_EC_FLAG_SCI) {
206 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
207 return acpi_os_execute(OSL_EC_BURST_HANDLER,
208 acpi_ec_gpe_query, ec);
209 }
210 return 0;
211 }
212
213 static int ec_poll(struct acpi_ec *ec)
214 {
215 unsigned long flags;
216 int repeat = 2; /* number of command restarts */
217 while (repeat--) {
218 unsigned long delay = jiffies +
219 msecs_to_jiffies(ACPI_EC_DELAY);
220 do {
221 /* don't sleep with disabled interrupts */
222 if (EC_FLAGS_MSI || irqs_disabled()) {
223 udelay(ACPI_EC_MSI_UDELAY);
224 if (ec_transaction_done(ec))
225 return 0;
226 } else {
227 if (wait_event_timeout(ec->wait,
228 ec_transaction_done(ec),
229 msecs_to_jiffies(1)))
230 return 0;
231 }
232 advance_transaction(ec, acpi_ec_read_status(ec));
233 } while (time_before(jiffies, delay));
234 if (!ec->curr->irq_count ||
235 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
236 break;
237 /* try restart command if we get any false interrupts */
238 pr_debug(PREFIX "controller reset, restart transaction\n");
239 spin_lock_irqsave(&ec->curr_lock, flags);
240 start_transaction(ec);
241 spin_unlock_irqrestore(&ec->curr_lock, flags);
242 }
243 return -ETIME;
244 }
245
246 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
247 struct transaction *t)
248 {
249 unsigned long tmp;
250 int ret = 0;
251 pr_debug(PREFIX "transaction start\n");
252 /* disable GPE during transaction if storm is detected */
253 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
254 acpi_disable_gpe(NULL, ec->gpe);
255 }
256 if (EC_FLAGS_MSI)
257 udelay(ACPI_EC_MSI_UDELAY);
258 /* start transaction */
259 spin_lock_irqsave(&ec->curr_lock, tmp);
260 /* following two actions should be kept atomic */
261 ec->curr = t;
262 start_transaction(ec);
263 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
264 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
265 spin_unlock_irqrestore(&ec->curr_lock, tmp);
266 ret = ec_poll(ec);
267 pr_debug(PREFIX "transaction end\n");
268 spin_lock_irqsave(&ec->curr_lock, tmp);
269 ec->curr = NULL;
270 spin_unlock_irqrestore(&ec->curr_lock, tmp);
271 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
272 /* check if we received SCI during transaction */
273 ec_check_sci(ec, acpi_ec_read_status(ec));
274 /* it is safe to enable GPE outside of transaction */
275 acpi_enable_gpe(NULL, ec->gpe);
276 } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
277 pr_info(PREFIX "GPE storm detected, "
278 "transactions will use polling mode\n");
279 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
280 }
281 return ret;
282 }
283
284 static int ec_check_ibf0(struct acpi_ec *ec)
285 {
286 u8 status = acpi_ec_read_status(ec);
287 return (status & ACPI_EC_FLAG_IBF) == 0;
288 }
289
290 static int ec_wait_ibf0(struct acpi_ec *ec)
291 {
292 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
293 /* interrupt wait manually if GPE mode is not active */
294 while (time_before(jiffies, delay))
295 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
296 msecs_to_jiffies(1)))
297 return 0;
298 return -ETIME;
299 }
300
301 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
302 {
303 int status;
304 u32 glk;
305 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
306 return -EINVAL;
307 if (t->rdata)
308 memset(t->rdata, 0, t->rlen);
309 mutex_lock(&ec->lock);
310 if (ec->global_lock) {
311 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
312 if (ACPI_FAILURE(status)) {
313 status = -ENODEV;
314 goto unlock;
315 }
316 }
317 if (ec_wait_ibf0(ec)) {
318 pr_err(PREFIX "input buffer is not empty, "
319 "aborting transaction\n");
320 status = -ETIME;
321 goto end;
322 }
323 status = acpi_ec_transaction_unlocked(ec, t);
324 end:
325 if (ec->global_lock)
326 acpi_release_global_lock(glk);
327 unlock:
328 mutex_unlock(&ec->lock);
329 return status;
330 }
331
332 static int acpi_ec_burst_enable(struct acpi_ec *ec)
333 {
334 u8 d;
335 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
336 .wdata = NULL, .rdata = &d,
337 .wlen = 0, .rlen = 1};
338
339 return acpi_ec_transaction(ec, &t);
340 }
341
342 static int acpi_ec_burst_disable(struct acpi_ec *ec)
343 {
344 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
345 .wdata = NULL, .rdata = NULL,
346 .wlen = 0, .rlen = 0};
347
348 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
349 acpi_ec_transaction(ec, &t) : 0;
350 }
351
352 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
353 {
354 int result;
355 u8 d;
356 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
357 .wdata = &address, .rdata = &d,
358 .wlen = 1, .rlen = 1};
359
360 result = acpi_ec_transaction(ec, &t);
361 *data = d;
362 return result;
363 }
364
365 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
366 {
367 u8 wdata[2] = { address, data };
368 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
369 .wdata = wdata, .rdata = NULL,
370 .wlen = 2, .rlen = 0};
371
372 return acpi_ec_transaction(ec, &t);
373 }
374
375 /*
376 * Externally callable EC access functions. For now, assume 1 EC only
377 */
378 int ec_burst_enable(void)
379 {
380 if (!first_ec)
381 return -ENODEV;
382 return acpi_ec_burst_enable(first_ec);
383 }
384
385 EXPORT_SYMBOL(ec_burst_enable);
386
387 int ec_burst_disable(void)
388 {
389 if (!first_ec)
390 return -ENODEV;
391 return acpi_ec_burst_disable(first_ec);
392 }
393
394 EXPORT_SYMBOL(ec_burst_disable);
395
396 int ec_read(u8 addr, u8 * val)
397 {
398 int err;
399 u8 temp_data;
400
401 if (!first_ec)
402 return -ENODEV;
403
404 err = acpi_ec_read(first_ec, addr, &temp_data);
405
406 if (!err) {
407 *val = temp_data;
408 return 0;
409 } else
410 return err;
411 }
412
413 EXPORT_SYMBOL(ec_read);
414
415 int ec_write(u8 addr, u8 val)
416 {
417 int err;
418
419 if (!first_ec)
420 return -ENODEV;
421
422 err = acpi_ec_write(first_ec, addr, val);
423
424 return err;
425 }
426
427 EXPORT_SYMBOL(ec_write);
428
429 int ec_transaction(u8 command,
430 const u8 * wdata, unsigned wdata_len,
431 u8 * rdata, unsigned rdata_len,
432 int force_poll)
433 {
434 struct transaction t = {.command = command,
435 .wdata = wdata, .rdata = rdata,
436 .wlen = wdata_len, .rlen = rdata_len};
437 if (!first_ec)
438 return -ENODEV;
439
440 return acpi_ec_transaction(first_ec, &t);
441 }
442
443 EXPORT_SYMBOL(ec_transaction);
444
445 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
446 {
447 int result;
448 u8 d;
449 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
450 .wdata = NULL, .rdata = &d,
451 .wlen = 0, .rlen = 1};
452 if (!ec || !data)
453 return -EINVAL;
454
455 /*
456 * Query the EC to find out which _Qxx method we need to evaluate.
457 * Note that successful completion of the query causes the ACPI_EC_SCI
458 * bit to be cleared (and thus clearing the interrupt source).
459 */
460
461 result = acpi_ec_transaction(ec, &t);
462 if (result)
463 return result;
464
465 if (!d)
466 return -ENODATA;
467
468 *data = d;
469 return 0;
470 }
471
472 /* --------------------------------------------------------------------------
473 Event Management
474 -------------------------------------------------------------------------- */
475 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
476 acpi_handle handle, acpi_ec_query_func func,
477 void *data)
478 {
479 struct acpi_ec_query_handler *handler =
480 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
481 if (!handler)
482 return -ENOMEM;
483
484 handler->query_bit = query_bit;
485 handler->handle = handle;
486 handler->func = func;
487 handler->data = data;
488 mutex_lock(&ec->lock);
489 list_add(&handler->node, &ec->list);
490 mutex_unlock(&ec->lock);
491 return 0;
492 }
493
494 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
495
496 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
497 {
498 struct acpi_ec_query_handler *handler, *tmp;
499 mutex_lock(&ec->lock);
500 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
501 if (query_bit == handler->query_bit) {
502 list_del(&handler->node);
503 kfree(handler);
504 }
505 }
506 mutex_unlock(&ec->lock);
507 }
508
509 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
510
511 static void acpi_ec_gpe_query(void *ec_cxt)
512 {
513 struct acpi_ec *ec = ec_cxt;
514 u8 value = 0;
515 struct acpi_ec_query_handler *handler, copy;
516
517 if (!ec || acpi_ec_query(ec, &value))
518 return;
519 mutex_lock(&ec->lock);
520 list_for_each_entry(handler, &ec->list, node) {
521 if (value == handler->query_bit) {
522 /* have custom handler for this bit */
523 memcpy(&copy, handler, sizeof(copy));
524 mutex_unlock(&ec->lock);
525 if (copy.func) {
526 copy.func(copy.data);
527 } else if (copy.handle) {
528 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
529 }
530 return;
531 }
532 }
533 mutex_unlock(&ec->lock);
534 }
535
536 static u32 acpi_ec_gpe_handler(void *data)
537 {
538 struct acpi_ec *ec = data;
539 u8 status;
540
541 pr_debug(PREFIX "~~~> interrupt\n");
542 status = acpi_ec_read_status(ec);
543
544 advance_transaction(ec, status);
545 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
546 wake_up(&ec->wait);
547 ec_check_sci(ec, status);
548 return ACPI_INTERRUPT_HANDLED;
549 }
550
551 /* --------------------------------------------------------------------------
552 Address Space Management
553 -------------------------------------------------------------------------- */
554
555 static acpi_status
556 acpi_ec_space_handler(u32 function, acpi_physical_address address,
557 u32 bits, acpi_integer *value,
558 void *handler_context, void *region_context)
559 {
560 struct acpi_ec *ec = handler_context;
561 int result = 0, i;
562 u8 temp = 0;
563
564 if ((address > 0xFF) || !value || !handler_context)
565 return AE_BAD_PARAMETER;
566
567 if (function != ACPI_READ && function != ACPI_WRITE)
568 return AE_BAD_PARAMETER;
569
570 if (bits != 8 && acpi_strict)
571 return AE_BAD_PARAMETER;
572
573 if (EC_FLAGS_MSI)
574 acpi_ec_burst_enable(ec);
575
576 if (function == ACPI_READ) {
577 result = acpi_ec_read(ec, address, &temp);
578 *value = temp;
579 } else {
580 temp = 0xff & (*value);
581 result = acpi_ec_write(ec, address, temp);
582 }
583
584 for (i = 8; unlikely(bits - i > 0); i += 8) {
585 ++address;
586 if (function == ACPI_READ) {
587 result = acpi_ec_read(ec, address, &temp);
588 (*value) |= ((acpi_integer)temp) << i;
589 } else {
590 temp = 0xff & ((*value) >> i);
591 result = acpi_ec_write(ec, address, temp);
592 }
593 }
594
595 if (EC_FLAGS_MSI)
596 acpi_ec_burst_disable(ec);
597
598 switch (result) {
599 case -EINVAL:
600 return AE_BAD_PARAMETER;
601 break;
602 case -ENODEV:
603 return AE_NOT_FOUND;
604 break;
605 case -ETIME:
606 return AE_TIME;
607 break;
608 default:
609 return AE_OK;
610 }
611 }
612
613 /* --------------------------------------------------------------------------
614 FS Interface (/proc)
615 -------------------------------------------------------------------------- */
616
617 static struct proc_dir_entry *acpi_ec_dir;
618
619 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
620 {
621 struct acpi_ec *ec = seq->private;
622
623 if (!ec)
624 goto end;
625
626 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
627 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
628 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
629 seq_printf(seq, "use global lock:\t%s\n",
630 ec->global_lock ? "yes" : "no");
631 end:
632 return 0;
633 }
634
635 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
636 {
637 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
638 }
639
640 static const struct file_operations acpi_ec_info_ops = {
641 .open = acpi_ec_info_open_fs,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = single_release,
645 .owner = THIS_MODULE,
646 };
647
648 static int acpi_ec_add_fs(struct acpi_device *device)
649 {
650 struct proc_dir_entry *entry = NULL;
651
652 if (!acpi_device_dir(device)) {
653 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
654 acpi_ec_dir);
655 if (!acpi_device_dir(device))
656 return -ENODEV;
657 }
658
659 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
660 acpi_device_dir(device),
661 &acpi_ec_info_ops, acpi_driver_data(device));
662 if (!entry)
663 return -ENODEV;
664 return 0;
665 }
666
667 static int acpi_ec_remove_fs(struct acpi_device *device)
668 {
669
670 if (acpi_device_dir(device)) {
671 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
672 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
673 acpi_device_dir(device) = NULL;
674 }
675
676 return 0;
677 }
678
679 /* --------------------------------------------------------------------------
680 Driver Interface
681 -------------------------------------------------------------------------- */
682 static acpi_status
683 ec_parse_io_ports(struct acpi_resource *resource, void *context);
684
685 static struct acpi_ec *make_acpi_ec(void)
686 {
687 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
688 if (!ec)
689 return NULL;
690 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
691 mutex_init(&ec->lock);
692 init_waitqueue_head(&ec->wait);
693 INIT_LIST_HEAD(&ec->list);
694 spin_lock_init(&ec->curr_lock);
695 return ec;
696 }
697
698 static acpi_status
699 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
700 void *context, void **return_value)
701 {
702 char node_name[5];
703 struct acpi_buffer buffer = { sizeof(node_name), node_name };
704 struct acpi_ec *ec = context;
705 int value = 0;
706 acpi_status status;
707
708 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
709
710 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
711 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
712 }
713 return AE_OK;
714 }
715
716 static acpi_status
717 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
718 {
719 acpi_status status;
720 unsigned long long tmp = 0;
721
722 struct acpi_ec *ec = context;
723
724 /* clear addr values, ec_parse_io_ports depend on it */
725 ec->command_addr = ec->data_addr = 0;
726
727 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
728 ec_parse_io_ports, ec);
729 if (ACPI_FAILURE(status))
730 return status;
731
732 /* Get GPE bit assignment (EC events). */
733 /* TODO: Add support for _GPE returning a package */
734 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
735 if (ACPI_FAILURE(status))
736 return status;
737 ec->gpe = tmp;
738 /* Use the global lock for all EC transactions? */
739 tmp = 0;
740 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
741 ec->global_lock = tmp;
742 ec->handle = handle;
743 return AE_CTRL_TERMINATE;
744 }
745
746 static int ec_install_handlers(struct acpi_ec *ec)
747 {
748 acpi_status status;
749 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
750 return 0;
751 status = acpi_install_gpe_handler(NULL, ec->gpe,
752 ACPI_GPE_EDGE_TRIGGERED,
753 &acpi_ec_gpe_handler, ec);
754 if (ACPI_FAILURE(status))
755 return -ENODEV;
756 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
757 acpi_enable_gpe(NULL, ec->gpe);
758 status = acpi_install_address_space_handler(ec->handle,
759 ACPI_ADR_SPACE_EC,
760 &acpi_ec_space_handler,
761 NULL, ec);
762 if (ACPI_FAILURE(status)) {
763 if (status == AE_NOT_FOUND) {
764 /*
765 * Maybe OS fails in evaluating the _REG object.
766 * The AE_NOT_FOUND error will be ignored and OS
767 * continue to initialize EC.
768 */
769 printk(KERN_ERR "Fail in evaluating the _REG object"
770 " of EC device. Broken bios is suspected.\n");
771 } else {
772 acpi_remove_gpe_handler(NULL, ec->gpe,
773 &acpi_ec_gpe_handler);
774 return -ENODEV;
775 }
776 }
777
778 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
779 return 0;
780 }
781
782 static void ec_remove_handlers(struct acpi_ec *ec)
783 {
784 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
785 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
786 pr_err(PREFIX "failed to remove space handler\n");
787 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
788 &acpi_ec_gpe_handler)))
789 pr_err(PREFIX "failed to remove gpe handler\n");
790 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
791 }
792
793 static int acpi_ec_add(struct acpi_device *device)
794 {
795 struct acpi_ec *ec = NULL;
796 int ret;
797
798 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
799 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
800
801 /* Check for boot EC */
802 if (boot_ec &&
803 (boot_ec->handle == device->handle ||
804 boot_ec->handle == ACPI_ROOT_OBJECT)) {
805 ec = boot_ec;
806 boot_ec = NULL;
807 } else {
808 ec = make_acpi_ec();
809 if (!ec)
810 return -ENOMEM;
811 }
812 if (ec_parse_device(device->handle, 0, ec, NULL) !=
813 AE_CTRL_TERMINATE) {
814 kfree(ec);
815 return -EINVAL;
816 }
817
818 ec->handle = device->handle;
819
820 /* Find and register all query methods */
821 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
822 acpi_ec_register_query_methods, ec, NULL);
823
824 if (!first_ec)
825 first_ec = ec;
826 device->driver_data = ec;
827 acpi_ec_add_fs(device);
828 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
829 ec->gpe, ec->command_addr, ec->data_addr);
830
831 ret = ec_install_handlers(ec);
832
833 /* EC is fully operational, allow queries */
834 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
835 return ret;
836 }
837
838 static int acpi_ec_remove(struct acpi_device *device, int type)
839 {
840 struct acpi_ec *ec;
841 struct acpi_ec_query_handler *handler, *tmp;
842
843 if (!device)
844 return -EINVAL;
845
846 ec = acpi_driver_data(device);
847 ec_remove_handlers(ec);
848 mutex_lock(&ec->lock);
849 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
850 list_del(&handler->node);
851 kfree(handler);
852 }
853 mutex_unlock(&ec->lock);
854 acpi_ec_remove_fs(device);
855 device->driver_data = NULL;
856 if (ec == first_ec)
857 first_ec = NULL;
858 kfree(ec);
859 return 0;
860 }
861
862 static acpi_status
863 ec_parse_io_ports(struct acpi_resource *resource, void *context)
864 {
865 struct acpi_ec *ec = context;
866
867 if (resource->type != ACPI_RESOURCE_TYPE_IO)
868 return AE_OK;
869
870 /*
871 * The first address region returned is the data port, and
872 * the second address region returned is the status/command
873 * port.
874 */
875 if (ec->data_addr == 0)
876 ec->data_addr = resource->data.io.minimum;
877 else if (ec->command_addr == 0)
878 ec->command_addr = resource->data.io.minimum;
879 else
880 return AE_CTRL_TERMINATE;
881
882 return AE_OK;
883 }
884
885 int __init acpi_boot_ec_enable(void)
886 {
887 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
888 return 0;
889 if (!ec_install_handlers(boot_ec)) {
890 first_ec = boot_ec;
891 return 0;
892 }
893 return -EFAULT;
894 }
895
896 static const struct acpi_device_id ec_device_ids[] = {
897 {"PNP0C09", 0},
898 {"", 0},
899 };
900
901 int __init acpi_ec_ecdt_probe(void)
902 {
903 acpi_status status;
904 struct acpi_ec *saved_ec = NULL;
905 struct acpi_table_ecdt *ecdt_ptr;
906
907 boot_ec = make_acpi_ec();
908 if (!boot_ec)
909 return -ENOMEM;
910 /*
911 * Generate a boot ec context
912 */
913 if (dmi_name_in_vendors("Micro-Star") ||
914 dmi_name_in_vendors("Notebook")) {
915 pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
916 EC_FLAGS_MSI = 1;
917 }
918 status = acpi_get_table(ACPI_SIG_ECDT, 1,
919 (struct acpi_table_header **)&ecdt_ptr);
920 if (ACPI_SUCCESS(status)) {
921 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
922 boot_ec->command_addr = ecdt_ptr->control.address;
923 boot_ec->data_addr = ecdt_ptr->data.address;
924 boot_ec->gpe = ecdt_ptr->gpe;
925 boot_ec->handle = ACPI_ROOT_OBJECT;
926 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
927 /* Don't trust ECDT, which comes from ASUSTek */
928 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
929 goto install;
930 saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
931 if (!saved_ec)
932 return -ENOMEM;
933 memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
934 /* fall through */
935 }
936 /* This workaround is needed only on some broken machines,
937 * which require early EC, but fail to provide ECDT */
938 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
939 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
940 boot_ec, NULL);
941 /* Check that acpi_get_devices actually find something */
942 if (ACPI_FAILURE(status) || !boot_ec->handle)
943 goto error;
944 if (saved_ec) {
945 /* try to find good ECDT from ASUSTek */
946 if (saved_ec->command_addr != boot_ec->command_addr ||
947 saved_ec->data_addr != boot_ec->data_addr ||
948 saved_ec->gpe != boot_ec->gpe ||
949 saved_ec->handle != boot_ec->handle)
950 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
951 "ECDT tables, which are very hard to workaround. "
952 "Trying to use DSDT EC info instead. Please send "
953 "output of acpidump to linux-acpi@vger.kernel.org\n");
954 kfree(saved_ec);
955 saved_ec = NULL;
956 } else {
957 /* We really need to limit this workaround, the only ASUS,
958 * which needs it, has fake EC._INI method, so use it as flag.
959 * Keep boot_ec struct as it will be needed soon.
960 */
961 acpi_handle dummy;
962 if (!dmi_name_in_vendors("ASUS") ||
963 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
964 &dummy)))
965 return -ENODEV;
966 }
967 install:
968 if (!ec_install_handlers(boot_ec)) {
969 first_ec = boot_ec;
970 return 0;
971 }
972 error:
973 kfree(boot_ec);
974 boot_ec = NULL;
975 return -ENODEV;
976 }
977
978 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
979 {
980 struct acpi_ec *ec = acpi_driver_data(device);
981 /* Stop using GPE */
982 acpi_disable_gpe(NULL, ec->gpe);
983 return 0;
984 }
985
986 static int acpi_ec_resume(struct acpi_device *device)
987 {
988 struct acpi_ec *ec = acpi_driver_data(device);
989 /* Enable use of GPE back */
990 acpi_enable_gpe(NULL, ec->gpe);
991 return 0;
992 }
993
994 static struct acpi_driver acpi_ec_driver = {
995 .name = "ec",
996 .class = ACPI_EC_CLASS,
997 .ids = ec_device_ids,
998 .ops = {
999 .add = acpi_ec_add,
1000 .remove = acpi_ec_remove,
1001 .suspend = acpi_ec_suspend,
1002 .resume = acpi_ec_resume,
1003 },
1004 };
1005
1006 int __init acpi_ec_init(void)
1007 {
1008 int result = 0;
1009
1010 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1011 if (!acpi_ec_dir)
1012 return -ENODEV;
1013
1014 /* Now register the driver for the EC */
1015 result = acpi_bus_register_driver(&acpi_ec_driver);
1016 if (result < 0) {
1017 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1018 return -ENODEV;
1019 }
1020
1021 return result;
1022 }
1023
1024 /* EC driver currently not unloadable */
1025 #if 0
1026 static void __exit acpi_ec_exit(void)
1027 {
1028
1029 acpi_bus_unregister_driver(&acpi_ec_driver);
1030
1031 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1032
1033 return;
1034 }
1035 #endif /* 0 */
This page took 0.052536 seconds and 5 git commands to generate.