Merge branch 'akpm' (patches from Andrew)
[deliverable/linux.git] / drivers / acpi / ec.c
1 /*
2 * ec.c - ACPI Embedded Controller Driver (v3)
3 *
4 * Copyright (C) 2001-2015 Intel Corporation
5 * Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com>
6 * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
7 * 2006 Denis Sadykov <denis.m.sadykov@intel.com>
8 * 2004 Luming Yu <luming.yu@intel.com>
9 * 2001, 2002 Andy Grover <andrew.grover@intel.com>
10 * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
11 * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
12 *
13 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 *
29 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 */
31
32 /* Uncomment next line to get verbose printout */
33 /* #define DEBUG */
34 #define DEBUG_REF 0
35 #define pr_fmt(fmt) "ACPI : EC: " fmt
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/delay.h>
42 #include <linux/interrupt.h>
43 #include <linux/list.h>
44 #include <linux/spinlock.h>
45 #include <linux/slab.h>
46 #include <linux/acpi.h>
47 #include <linux/dmi.h>
48 #include <asm/io.h>
49
50 #include "internal.h"
51
52 #define ACPI_EC_CLASS "embedded_controller"
53 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
54 #define ACPI_EC_FILE_INFO "info"
55
56 /* EC status register */
57 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
58 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
59 #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
60 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
61 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
62
63 /* EC commands */
64 enum ec_command {
65 ACPI_EC_COMMAND_READ = 0x80,
66 ACPI_EC_COMMAND_WRITE = 0x81,
67 ACPI_EC_BURST_ENABLE = 0x82,
68 ACPI_EC_BURST_DISABLE = 0x83,
69 ACPI_EC_COMMAND_QUERY = 0x84,
70 };
71
72 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
73 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
74 #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
75 #define ACPI_EC_UDELAY_POLL 1000 /* Wait 1ms for EC transaction polling */
76 #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
77 * when trying to clear the EC */
78
79 enum {
80 EC_FLAGS_EVENT_ENABLED, /* Event is enabled */
81 EC_FLAGS_EVENT_PENDING, /* Event is pending */
82 EC_FLAGS_EVENT_DETECTED, /* Event is detected */
83 EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
84 * OpReg are installed */
85 EC_FLAGS_STARTED, /* Driver is started */
86 EC_FLAGS_STOPPED, /* Driver is stopped */
87 EC_FLAGS_COMMAND_STORM, /* GPE storms occurred to the
88 * current command processing */
89 };
90
91 #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
92 #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
93
94 #define ec_debug_ref(ec, fmt, ...) \
95 do { \
96 if (DEBUG_REF) \
97 pr_debug("%lu: " fmt, ec->reference_count, \
98 ## __VA_ARGS__); \
99 } while (0)
100
101 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
102 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
103 module_param(ec_delay, uint, 0644);
104 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
105
106 /*
107 * If the number of false interrupts per one transaction exceeds
108 * this threshold, will think there is a GPE storm happened and
109 * will disable the GPE for normal transaction.
110 */
111 static unsigned int ec_storm_threshold __read_mostly = 8;
112 module_param(ec_storm_threshold, uint, 0644);
113 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
114
115 struct acpi_ec_query_handler {
116 struct list_head node;
117 acpi_ec_query_func func;
118 acpi_handle handle;
119 void *data;
120 u8 query_bit;
121 struct kref kref;
122 };
123
124 struct transaction {
125 const u8 *wdata;
126 u8 *rdata;
127 unsigned short irq_count;
128 u8 command;
129 u8 wi;
130 u8 ri;
131 u8 wlen;
132 u8 rlen;
133 u8 flags;
134 unsigned long timestamp;
135 };
136
137 static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
138 static void advance_transaction(struct acpi_ec *ec);
139
140 struct acpi_ec *boot_ec, *first_ec;
141 EXPORT_SYMBOL(first_ec);
142
143 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
144 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
145 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
146 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
147 static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
148
149 /* --------------------------------------------------------------------------
150 * Device Flags
151 * -------------------------------------------------------------------------- */
152
153 static bool acpi_ec_started(struct acpi_ec *ec)
154 {
155 return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
156 !test_bit(EC_FLAGS_STOPPED, &ec->flags);
157 }
158
159 static bool acpi_ec_flushed(struct acpi_ec *ec)
160 {
161 return ec->reference_count == 1;
162 }
163
164 static bool acpi_ec_has_pending_event(struct acpi_ec *ec)
165 {
166 return test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
167 test_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
168 }
169
170 /* --------------------------------------------------------------------------
171 * EC Registers
172 * -------------------------------------------------------------------------- */
173
174 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
175 {
176 u8 x = inb(ec->command_addr);
177
178 pr_debug("EC_SC(R) = 0x%2.2x "
179 "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d\n",
180 x,
181 !!(x & ACPI_EC_FLAG_SCI),
182 !!(x & ACPI_EC_FLAG_BURST),
183 !!(x & ACPI_EC_FLAG_CMD),
184 !!(x & ACPI_EC_FLAG_IBF),
185 !!(x & ACPI_EC_FLAG_OBF));
186 return x;
187 }
188
189 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
190 {
191 u8 x = inb(ec->data_addr);
192
193 ec->curr->timestamp = jiffies;
194 pr_debug("EC_DATA(R) = 0x%2.2x\n", x);
195 return x;
196 }
197
198 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
199 {
200 pr_debug("EC_SC(W) = 0x%2.2x\n", command);
201 outb(command, ec->command_addr);
202 ec->curr->timestamp = jiffies;
203 }
204
205 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
206 {
207 pr_debug("EC_DATA(W) = 0x%2.2x\n", data);
208 outb(data, ec->data_addr);
209 ec->curr->timestamp = jiffies;
210 }
211
212 #ifdef DEBUG
213 static const char *acpi_ec_cmd_string(u8 cmd)
214 {
215 switch (cmd) {
216 case 0x80:
217 return "RD_EC";
218 case 0x81:
219 return "WR_EC";
220 case 0x82:
221 return "BE_EC";
222 case 0x83:
223 return "BD_EC";
224 case 0x84:
225 return "QR_EC";
226 }
227 return "UNKNOWN";
228 }
229 #else
230 #define acpi_ec_cmd_string(cmd) "UNDEF"
231 #endif
232
233 /* --------------------------------------------------------------------------
234 * GPE Registers
235 * -------------------------------------------------------------------------- */
236
237 static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
238 {
239 acpi_event_status gpe_status = 0;
240
241 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
242 return (gpe_status & ACPI_EVENT_FLAG_SET) ? true : false;
243 }
244
245 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
246 {
247 if (open)
248 acpi_enable_gpe(NULL, ec->gpe);
249 else {
250 BUG_ON(ec->reference_count < 1);
251 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
252 }
253 if (acpi_ec_is_gpe_raised(ec)) {
254 /*
255 * On some platforms, EN=1 writes cannot trigger GPE. So
256 * software need to manually trigger a pseudo GPE event on
257 * EN=1 writes.
258 */
259 pr_debug("***** Polling quirk *****\n");
260 advance_transaction(ec);
261 }
262 }
263
264 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
265 {
266 if (close)
267 acpi_disable_gpe(NULL, ec->gpe);
268 else {
269 BUG_ON(ec->reference_count < 1);
270 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
271 }
272 }
273
274 static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
275 {
276 /*
277 * GPE STS is a W1C register, which means:
278 * 1. Software can clear it without worrying about clearing other
279 * GPEs' STS bits when the hardware sets them in parallel.
280 * 2. As long as software can ensure only clearing it when it is
281 * set, hardware won't set it in parallel.
282 * So software can clear GPE in any contexts.
283 * Warning: do not move the check into advance_transaction() as the
284 * EC commands will be sent without GPE raised.
285 */
286 if (!acpi_ec_is_gpe_raised(ec))
287 return;
288 acpi_clear_gpe(NULL, ec->gpe);
289 }
290
291 /* --------------------------------------------------------------------------
292 * Transaction Management
293 * -------------------------------------------------------------------------- */
294
295 static void acpi_ec_submit_request(struct acpi_ec *ec)
296 {
297 ec->reference_count++;
298 if (ec->reference_count == 1)
299 acpi_ec_enable_gpe(ec, true);
300 }
301
302 static void acpi_ec_complete_request(struct acpi_ec *ec)
303 {
304 bool flushed = false;
305
306 ec->reference_count--;
307 if (ec->reference_count == 0)
308 acpi_ec_disable_gpe(ec, true);
309 flushed = acpi_ec_flushed(ec);
310 if (flushed)
311 wake_up(&ec->wait);
312 }
313
314 static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
315 {
316 if (!test_bit(flag, &ec->flags)) {
317 acpi_ec_disable_gpe(ec, false);
318 pr_debug("+++++ Polling enabled +++++\n");
319 set_bit(flag, &ec->flags);
320 }
321 }
322
323 static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
324 {
325 if (test_bit(flag, &ec->flags)) {
326 clear_bit(flag, &ec->flags);
327 acpi_ec_enable_gpe(ec, false);
328 pr_debug("+++++ Polling disabled +++++\n");
329 }
330 }
331
332 /*
333 * acpi_ec_submit_flushable_request() - Increase the reference count unless
334 * the flush operation is not in
335 * progress
336 * @ec: the EC device
337 * @allow_event: whether event should be handled
338 *
339 * This function must be used before taking a new action that should hold
340 * the reference count. If this function returns false, then the action
341 * must be discarded or it will prevent the flush operation from being
342 * completed.
343 *
344 * During flushing, QR_EC command need to pass this check when there is a
345 * pending event, so that the reference count held for the pending event
346 * can be decreased by the completion of the QR_EC command.
347 */
348 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec,
349 bool allow_event)
350 {
351 if (!acpi_ec_started(ec)) {
352 if (!allow_event || !acpi_ec_has_pending_event(ec))
353 return false;
354 }
355 acpi_ec_submit_request(ec);
356 return true;
357 }
358
359 static void acpi_ec_submit_event(struct acpi_ec *ec)
360 {
361 if (!test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
362 !test_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags))
363 return;
364 /* Hold reference for pending event */
365 if (!acpi_ec_submit_flushable_request(ec, true))
366 return;
367 ec_debug_ref(ec, "Increase event\n");
368 if (!test_and_set_bit(EC_FLAGS_EVENT_PENDING, &ec->flags)) {
369 pr_debug("***** Event query started *****\n");
370 schedule_work(&ec->work);
371 return;
372 }
373 acpi_ec_complete_request(ec);
374 ec_debug_ref(ec, "Decrease event\n");
375 }
376
377 static void acpi_ec_complete_event(struct acpi_ec *ec)
378 {
379 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
380 clear_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
381 pr_debug("***** Event query stopped *****\n");
382 /* Unhold reference for pending event */
383 acpi_ec_complete_request(ec);
384 ec_debug_ref(ec, "Decrease event\n");
385 /* Check if there is another SCI_EVT detected */
386 acpi_ec_submit_event(ec);
387 }
388 }
389
390 static void acpi_ec_submit_detection(struct acpi_ec *ec)
391 {
392 /* Hold reference for query submission */
393 if (!acpi_ec_submit_flushable_request(ec, false))
394 return;
395 ec_debug_ref(ec, "Increase query\n");
396 if (!test_and_set_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags)) {
397 pr_debug("***** Event detection blocked *****\n");
398 acpi_ec_submit_event(ec);
399 return;
400 }
401 acpi_ec_complete_request(ec);
402 ec_debug_ref(ec, "Decrease query\n");
403 }
404
405 static void acpi_ec_complete_detection(struct acpi_ec *ec)
406 {
407 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
408 clear_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags);
409 pr_debug("***** Event detetion unblocked *****\n");
410 /* Unhold reference for query submission */
411 acpi_ec_complete_request(ec);
412 ec_debug_ref(ec, "Decrease query\n");
413 }
414 }
415
416 static void acpi_ec_enable_event(struct acpi_ec *ec)
417 {
418 unsigned long flags;
419
420 spin_lock_irqsave(&ec->lock, flags);
421 set_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags);
422 /*
423 * An event may be pending even with SCI_EVT=0, so QR_EC should
424 * always be issued right after started.
425 */
426 acpi_ec_submit_detection(ec);
427 spin_unlock_irqrestore(&ec->lock, flags);
428 }
429
430 static int ec_transaction_completed(struct acpi_ec *ec)
431 {
432 unsigned long flags;
433 int ret = 0;
434
435 spin_lock_irqsave(&ec->lock, flags);
436 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
437 ret = 1;
438 spin_unlock_irqrestore(&ec->lock, flags);
439 return ret;
440 }
441
442 static void advance_transaction(struct acpi_ec *ec)
443 {
444 struct transaction *t;
445 u8 status;
446 bool wakeup = false;
447
448 pr_debug("===== %s (%d) =====\n",
449 in_interrupt() ? "IRQ" : "TASK", smp_processor_id());
450 /*
451 * By always clearing STS before handling all indications, we can
452 * ensure a hardware STS 0->1 change after this clearing can always
453 * trigger a GPE interrupt.
454 */
455 acpi_ec_clear_gpe(ec);
456 status = acpi_ec_read_status(ec);
457 t = ec->curr;
458 if (!t)
459 goto err;
460 if (t->flags & ACPI_EC_COMMAND_POLL) {
461 if (t->wlen > t->wi) {
462 if ((status & ACPI_EC_FLAG_IBF) == 0)
463 acpi_ec_write_data(ec, t->wdata[t->wi++]);
464 else
465 goto err;
466 } else if (t->rlen > t->ri) {
467 if ((status & ACPI_EC_FLAG_OBF) == 1) {
468 t->rdata[t->ri++] = acpi_ec_read_data(ec);
469 if (t->rlen == t->ri) {
470 t->flags |= ACPI_EC_COMMAND_COMPLETE;
471 acpi_ec_complete_event(ec);
472 if (t->command == ACPI_EC_COMMAND_QUERY)
473 pr_debug("***** Command(%s) hardware completion *****\n",
474 acpi_ec_cmd_string(t->command));
475 wakeup = true;
476 }
477 } else
478 goto err;
479 } else if (t->wlen == t->wi &&
480 (status & ACPI_EC_FLAG_IBF) == 0) {
481 t->flags |= ACPI_EC_COMMAND_COMPLETE;
482 acpi_ec_complete_event(ec);
483 wakeup = true;
484 }
485 goto out;
486 } else {
487 if (EC_FLAGS_QUERY_HANDSHAKE &&
488 !(status & ACPI_EC_FLAG_SCI) &&
489 (t->command == ACPI_EC_COMMAND_QUERY)) {
490 t->flags |= ACPI_EC_COMMAND_POLL;
491 acpi_ec_complete_detection(ec);
492 t->rdata[t->ri++] = 0x00;
493 t->flags |= ACPI_EC_COMMAND_COMPLETE;
494 acpi_ec_complete_event(ec);
495 pr_debug("***** Command(%s) software completion *****\n",
496 acpi_ec_cmd_string(t->command));
497 wakeup = true;
498 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
499 acpi_ec_write_cmd(ec, t->command);
500 t->flags |= ACPI_EC_COMMAND_POLL;
501 acpi_ec_complete_detection(ec);
502 } else
503 goto err;
504 goto out;
505 }
506 err:
507 /*
508 * If SCI bit is set, then don't think it's a false IRQ
509 * otherwise will take a not handled IRQ as a false one.
510 */
511 if (!(status & ACPI_EC_FLAG_SCI)) {
512 if (in_interrupt() && t) {
513 if (t->irq_count < ec_storm_threshold)
514 ++t->irq_count;
515 /* Allow triggering on 0 threshold */
516 if (t->irq_count == ec_storm_threshold)
517 acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
518 }
519 }
520 out:
521 if (status & ACPI_EC_FLAG_SCI)
522 acpi_ec_submit_detection(ec);
523 if (wakeup && in_interrupt())
524 wake_up(&ec->wait);
525 }
526
527 static void start_transaction(struct acpi_ec *ec)
528 {
529 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
530 ec->curr->flags = 0;
531 ec->curr->timestamp = jiffies;
532 advance_transaction(ec);
533 }
534
535 static int ec_poll(struct acpi_ec *ec)
536 {
537 unsigned long flags;
538 int repeat = 5; /* number of command restarts */
539
540 while (repeat--) {
541 unsigned long delay = jiffies +
542 msecs_to_jiffies(ec_delay);
543 unsigned long usecs = ACPI_EC_UDELAY_POLL;
544 do {
545 /* don't sleep with disabled interrupts */
546 if (EC_FLAGS_MSI || irqs_disabled()) {
547 usecs = ACPI_EC_MSI_UDELAY;
548 udelay(usecs);
549 if (ec_transaction_completed(ec))
550 return 0;
551 } else {
552 if (wait_event_timeout(ec->wait,
553 ec_transaction_completed(ec),
554 usecs_to_jiffies(usecs)))
555 return 0;
556 }
557 spin_lock_irqsave(&ec->lock, flags);
558 if (time_after(jiffies,
559 ec->curr->timestamp +
560 usecs_to_jiffies(usecs)))
561 advance_transaction(ec);
562 spin_unlock_irqrestore(&ec->lock, flags);
563 } while (time_before(jiffies, delay));
564 pr_debug("controller reset, restart transaction\n");
565 spin_lock_irqsave(&ec->lock, flags);
566 start_transaction(ec);
567 spin_unlock_irqrestore(&ec->lock, flags);
568 }
569 return -ETIME;
570 }
571
572 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
573 struct transaction *t)
574 {
575 unsigned long tmp;
576 int ret = 0;
577
578 if (EC_FLAGS_MSI)
579 udelay(ACPI_EC_MSI_UDELAY);
580 /* start transaction */
581 spin_lock_irqsave(&ec->lock, tmp);
582 /* Enable GPE for command processing (IBF=0/OBF=1) */
583 if (!acpi_ec_submit_flushable_request(ec, true)) {
584 ret = -EINVAL;
585 goto unlock;
586 }
587 ec_debug_ref(ec, "Increase command\n");
588 /* following two actions should be kept atomic */
589 ec->curr = t;
590 pr_debug("***** Command(%s) started *****\n",
591 acpi_ec_cmd_string(t->command));
592 start_transaction(ec);
593 spin_unlock_irqrestore(&ec->lock, tmp);
594 ret = ec_poll(ec);
595 spin_lock_irqsave(&ec->lock, tmp);
596 if (t->irq_count == ec_storm_threshold)
597 acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
598 pr_debug("***** Command(%s) stopped *****\n",
599 acpi_ec_cmd_string(t->command));
600 ec->curr = NULL;
601 /* Disable GPE for command processing (IBF=0/OBF=1) */
602 acpi_ec_complete_request(ec);
603 ec_debug_ref(ec, "Decrease command\n");
604 unlock:
605 spin_unlock_irqrestore(&ec->lock, tmp);
606 return ret;
607 }
608
609 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
610 {
611 int status;
612 u32 glk;
613
614 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
615 return -EINVAL;
616 if (t->rdata)
617 memset(t->rdata, 0, t->rlen);
618 mutex_lock(&ec->mutex);
619 if (ec->global_lock) {
620 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
621 if (ACPI_FAILURE(status)) {
622 status = -ENODEV;
623 goto unlock;
624 }
625 }
626
627 status = acpi_ec_transaction_unlocked(ec, t);
628
629 if (test_bit(EC_FLAGS_COMMAND_STORM, &ec->flags))
630 msleep(1);
631 if (ec->global_lock)
632 acpi_release_global_lock(glk);
633 unlock:
634 mutex_unlock(&ec->mutex);
635 return status;
636 }
637
638 static int acpi_ec_burst_enable(struct acpi_ec *ec)
639 {
640 u8 d;
641 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
642 .wdata = NULL, .rdata = &d,
643 .wlen = 0, .rlen = 1};
644
645 return acpi_ec_transaction(ec, &t);
646 }
647
648 static int acpi_ec_burst_disable(struct acpi_ec *ec)
649 {
650 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
651 .wdata = NULL, .rdata = NULL,
652 .wlen = 0, .rlen = 0};
653
654 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
655 acpi_ec_transaction(ec, &t) : 0;
656 }
657
658 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
659 {
660 int result;
661 u8 d;
662 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
663 .wdata = &address, .rdata = &d,
664 .wlen = 1, .rlen = 1};
665
666 result = acpi_ec_transaction(ec, &t);
667 *data = d;
668 return result;
669 }
670
671 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
672 {
673 u8 wdata[2] = { address, data };
674 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
675 .wdata = wdata, .rdata = NULL,
676 .wlen = 2, .rlen = 0};
677
678 return acpi_ec_transaction(ec, &t);
679 }
680
681 int ec_read(u8 addr, u8 *val)
682 {
683 int err;
684 u8 temp_data;
685
686 if (!first_ec)
687 return -ENODEV;
688
689 err = acpi_ec_read(first_ec, addr, &temp_data);
690
691 if (!err) {
692 *val = temp_data;
693 return 0;
694 }
695 return err;
696 }
697 EXPORT_SYMBOL(ec_read);
698
699 int ec_write(u8 addr, u8 val)
700 {
701 int err;
702
703 if (!first_ec)
704 return -ENODEV;
705
706 err = acpi_ec_write(first_ec, addr, val);
707
708 return err;
709 }
710 EXPORT_SYMBOL(ec_write);
711
712 int ec_transaction(u8 command,
713 const u8 *wdata, unsigned wdata_len,
714 u8 *rdata, unsigned rdata_len)
715 {
716 struct transaction t = {.command = command,
717 .wdata = wdata, .rdata = rdata,
718 .wlen = wdata_len, .rlen = rdata_len};
719
720 if (!first_ec)
721 return -ENODEV;
722
723 return acpi_ec_transaction(first_ec, &t);
724 }
725 EXPORT_SYMBOL(ec_transaction);
726
727 /* Get the handle to the EC device */
728 acpi_handle ec_get_handle(void)
729 {
730 if (!first_ec)
731 return NULL;
732 return first_ec->handle;
733 }
734 EXPORT_SYMBOL(ec_get_handle);
735
736 /*
737 * Process _Q events that might have accumulated in the EC.
738 * Run with locked ec mutex.
739 */
740 static void acpi_ec_clear(struct acpi_ec *ec)
741 {
742 int i, status;
743 u8 value = 0;
744
745 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
746 status = acpi_ec_query(ec, &value);
747 if (status || !value)
748 break;
749 }
750
751 if (unlikely(i == ACPI_EC_CLEAR_MAX))
752 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
753 else
754 pr_info("%d stale EC events cleared\n", i);
755 }
756
757 static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
758 {
759 unsigned long flags;
760
761 spin_lock_irqsave(&ec->lock, flags);
762 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
763 pr_debug("+++++ Starting EC +++++\n");
764 /* Enable GPE for event processing (SCI_EVT=1) */
765 if (!resuming) {
766 acpi_ec_submit_request(ec);
767 ec_debug_ref(ec, "Increase driver\n");
768 }
769 pr_info("+++++ EC started +++++\n");
770 }
771 spin_unlock_irqrestore(&ec->lock, flags);
772 }
773
774 static bool acpi_ec_stopped(struct acpi_ec *ec)
775 {
776 unsigned long flags;
777 bool flushed;
778
779 spin_lock_irqsave(&ec->lock, flags);
780 flushed = acpi_ec_flushed(ec);
781 spin_unlock_irqrestore(&ec->lock, flags);
782 return flushed;
783 }
784
785 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
786 {
787 unsigned long flags;
788
789 spin_lock_irqsave(&ec->lock, flags);
790 if (acpi_ec_started(ec)) {
791 pr_debug("+++++ Stopping EC +++++\n");
792 set_bit(EC_FLAGS_STOPPED, &ec->flags);
793 spin_unlock_irqrestore(&ec->lock, flags);
794 wait_event(ec->wait, acpi_ec_stopped(ec));
795 spin_lock_irqsave(&ec->lock, flags);
796 /* Disable GPE for event processing (SCI_EVT=1) */
797 if (!suspending) {
798 acpi_ec_complete_request(ec);
799 ec_debug_ref(ec, "Decrease driver\n");
800 }
801 clear_bit(EC_FLAGS_STARTED, &ec->flags);
802 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
803 pr_info("+++++ EC stopped +++++\n");
804 }
805 spin_unlock_irqrestore(&ec->lock, flags);
806 }
807
808 void acpi_ec_block_transactions(void)
809 {
810 struct acpi_ec *ec = first_ec;
811
812 if (!ec)
813 return;
814
815 mutex_lock(&ec->mutex);
816 /* Prevent transactions from being carried out */
817 acpi_ec_stop(ec, true);
818 mutex_unlock(&ec->mutex);
819 }
820
821 void acpi_ec_unblock_transactions(void)
822 {
823 struct acpi_ec *ec = first_ec;
824
825 if (!ec)
826 return;
827
828 /* Allow transactions to be carried out again */
829 acpi_ec_start(ec, true);
830
831 if (EC_FLAGS_CLEAR_ON_RESUME)
832 acpi_ec_clear(ec);
833 }
834
835 void acpi_ec_unblock_transactions_early(void)
836 {
837 /*
838 * Allow transactions to happen again (this function is called from
839 * atomic context during wakeup, so we don't need to acquire the mutex).
840 */
841 if (first_ec)
842 acpi_ec_start(first_ec, true);
843 }
844
845 /* --------------------------------------------------------------------------
846 Event Management
847 -------------------------------------------------------------------------- */
848 static struct acpi_ec_query_handler *
849 acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
850 {
851 if (handler)
852 kref_get(&handler->kref);
853 return handler;
854 }
855
856 static void acpi_ec_query_handler_release(struct kref *kref)
857 {
858 struct acpi_ec_query_handler *handler =
859 container_of(kref, struct acpi_ec_query_handler, kref);
860
861 kfree(handler);
862 }
863
864 static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
865 {
866 kref_put(&handler->kref, acpi_ec_query_handler_release);
867 }
868
869 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
870 acpi_handle handle, acpi_ec_query_func func,
871 void *data)
872 {
873 struct acpi_ec_query_handler *handler =
874 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
875
876 if (!handler)
877 return -ENOMEM;
878
879 handler->query_bit = query_bit;
880 handler->handle = handle;
881 handler->func = func;
882 handler->data = data;
883 mutex_lock(&ec->mutex);
884 kref_init(&handler->kref);
885 list_add(&handler->node, &ec->list);
886 mutex_unlock(&ec->mutex);
887 return 0;
888 }
889 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
890
891 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
892 {
893 struct acpi_ec_query_handler *handler, *tmp;
894 LIST_HEAD(free_list);
895
896 mutex_lock(&ec->mutex);
897 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
898 if (query_bit == handler->query_bit) {
899 list_del_init(&handler->node);
900 list_add(&handler->node, &free_list);
901 }
902 }
903 mutex_unlock(&ec->mutex);
904 list_for_each_entry(handler, &free_list, node)
905 acpi_ec_put_query_handler(handler);
906 }
907 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
908
909 static void acpi_ec_run(void *cxt)
910 {
911 struct acpi_ec_query_handler *handler = cxt;
912
913 if (!handler)
914 return;
915 pr_debug("##### Query(0x%02x) started #####\n", handler->query_bit);
916 if (handler->func)
917 handler->func(handler->data);
918 else if (handler->handle)
919 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
920 pr_debug("##### Query(0x%02x) stopped #####\n", handler->query_bit);
921 acpi_ec_put_query_handler(handler);
922 }
923
924 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
925 {
926 u8 value = 0;
927 int result;
928 acpi_status status;
929 struct acpi_ec_query_handler *handler;
930 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
931 .wdata = NULL, .rdata = &value,
932 .wlen = 0, .rlen = 1};
933
934 /*
935 * Query the EC to find out which _Qxx method we need to evaluate.
936 * Note that successful completion of the query causes the ACPI_EC_SCI
937 * bit to be cleared (and thus clearing the interrupt source).
938 */
939 result = acpi_ec_transaction(ec, &t);
940 if (result)
941 return result;
942 if (data)
943 *data = value;
944 if (!value)
945 return -ENODATA;
946
947 mutex_lock(&ec->mutex);
948 list_for_each_entry(handler, &ec->list, node) {
949 if (value == handler->query_bit) {
950 /* have custom handler for this bit */
951 handler = acpi_ec_get_query_handler(handler);
952 pr_debug("##### Query(0x%02x) scheduled #####\n",
953 handler->query_bit);
954 status = acpi_os_execute((handler->func) ?
955 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
956 acpi_ec_run, handler);
957 if (ACPI_FAILURE(status))
958 result = -EBUSY;
959 break;
960 }
961 }
962 mutex_unlock(&ec->mutex);
963 return result;
964 }
965
966 static void acpi_ec_gpe_poller(struct work_struct *work)
967 {
968 struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
969
970 pr_debug("***** Event poller started *****\n");
971 acpi_ec_query(ec, NULL);
972 pr_debug("***** Event poller stopped *****\n");
973 }
974
975 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
976 u32 gpe_number, void *data)
977 {
978 unsigned long flags;
979 struct acpi_ec *ec = data;
980
981 spin_lock_irqsave(&ec->lock, flags);
982 advance_transaction(ec);
983 spin_unlock_irqrestore(&ec->lock, flags);
984 return ACPI_INTERRUPT_HANDLED;
985 }
986
987 /* --------------------------------------------------------------------------
988 * Address Space Management
989 * -------------------------------------------------------------------------- */
990
991 static acpi_status
992 acpi_ec_space_handler(u32 function, acpi_physical_address address,
993 u32 bits, u64 *value64,
994 void *handler_context, void *region_context)
995 {
996 struct acpi_ec *ec = handler_context;
997 int result = 0, i, bytes = bits / 8;
998 u8 *value = (u8 *)value64;
999
1000 if ((address > 0xFF) || !value || !handler_context)
1001 return AE_BAD_PARAMETER;
1002
1003 if (function != ACPI_READ && function != ACPI_WRITE)
1004 return AE_BAD_PARAMETER;
1005
1006 if (EC_FLAGS_MSI || bits > 8)
1007 acpi_ec_burst_enable(ec);
1008
1009 for (i = 0; i < bytes; ++i, ++address, ++value)
1010 result = (function == ACPI_READ) ?
1011 acpi_ec_read(ec, address, value) :
1012 acpi_ec_write(ec, address, *value);
1013
1014 if (EC_FLAGS_MSI || bits > 8)
1015 acpi_ec_burst_disable(ec);
1016
1017 switch (result) {
1018 case -EINVAL:
1019 return AE_BAD_PARAMETER;
1020 case -ENODEV:
1021 return AE_NOT_FOUND;
1022 case -ETIME:
1023 return AE_TIME;
1024 default:
1025 return AE_OK;
1026 }
1027 }
1028
1029 /* --------------------------------------------------------------------------
1030 * Driver Interface
1031 * -------------------------------------------------------------------------- */
1032
1033 static acpi_status
1034 ec_parse_io_ports(struct acpi_resource *resource, void *context);
1035
1036 static struct acpi_ec *make_acpi_ec(void)
1037 {
1038 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1039
1040 if (!ec)
1041 return NULL;
1042 mutex_init(&ec->mutex);
1043 init_waitqueue_head(&ec->wait);
1044 INIT_LIST_HEAD(&ec->list);
1045 spin_lock_init(&ec->lock);
1046 INIT_WORK(&ec->work, acpi_ec_gpe_poller);
1047 return ec;
1048 }
1049
1050 static acpi_status
1051 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1052 void *context, void **return_value)
1053 {
1054 char node_name[5];
1055 struct acpi_buffer buffer = { sizeof(node_name), node_name };
1056 struct acpi_ec *ec = context;
1057 int value = 0;
1058 acpi_status status;
1059
1060 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1061
1062 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
1063 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
1064 return AE_OK;
1065 }
1066
1067 static acpi_status
1068 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
1069 {
1070 acpi_status status;
1071 unsigned long long tmp = 0;
1072 struct acpi_ec *ec = context;
1073
1074 /* clear addr values, ec_parse_io_ports depend on it */
1075 ec->command_addr = ec->data_addr = 0;
1076
1077 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1078 ec_parse_io_ports, ec);
1079 if (ACPI_FAILURE(status))
1080 return status;
1081
1082 /* Get GPE bit assignment (EC events). */
1083 /* TODO: Add support for _GPE returning a package */
1084 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
1085 if (ACPI_FAILURE(status))
1086 return status;
1087 ec->gpe = tmp;
1088 /* Use the global lock for all EC transactions? */
1089 tmp = 0;
1090 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1091 ec->global_lock = tmp;
1092 ec->handle = handle;
1093 return AE_CTRL_TERMINATE;
1094 }
1095
1096 static int ec_install_handlers(struct acpi_ec *ec)
1097 {
1098 acpi_status status;
1099
1100 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1101 return 0;
1102 status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1103 ACPI_GPE_EDGE_TRIGGERED,
1104 &acpi_ec_gpe_handler, ec);
1105 if (ACPI_FAILURE(status))
1106 return -ENODEV;
1107
1108 acpi_ec_start(ec, false);
1109 status = acpi_install_address_space_handler(ec->handle,
1110 ACPI_ADR_SPACE_EC,
1111 &acpi_ec_space_handler,
1112 NULL, ec);
1113 if (ACPI_FAILURE(status)) {
1114 if (status == AE_NOT_FOUND) {
1115 /*
1116 * Maybe OS fails in evaluating the _REG object.
1117 * The AE_NOT_FOUND error will be ignored and OS
1118 * continue to initialize EC.
1119 */
1120 pr_err("Fail in evaluating the _REG object"
1121 " of EC device. Broken bios is suspected.\n");
1122 } else {
1123 acpi_ec_stop(ec, false);
1124 acpi_remove_gpe_handler(NULL, ec->gpe,
1125 &acpi_ec_gpe_handler);
1126 return -ENODEV;
1127 }
1128 }
1129
1130 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1131 return 0;
1132 }
1133
1134 static void ec_remove_handlers(struct acpi_ec *ec)
1135 {
1136 if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1137 return;
1138 acpi_ec_stop(ec, false);
1139 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1140 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
1141 pr_err("failed to remove space handler\n");
1142 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1143 &acpi_ec_gpe_handler)))
1144 pr_err("failed to remove gpe handler\n");
1145 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1146 }
1147
1148 static int acpi_ec_add(struct acpi_device *device)
1149 {
1150 struct acpi_ec *ec = NULL;
1151 int ret;
1152
1153 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1154 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1155
1156 /* Check for boot EC */
1157 if (boot_ec &&
1158 (boot_ec->handle == device->handle ||
1159 boot_ec->handle == ACPI_ROOT_OBJECT)) {
1160 ec = boot_ec;
1161 boot_ec = NULL;
1162 } else {
1163 ec = make_acpi_ec();
1164 if (!ec)
1165 return -ENOMEM;
1166 }
1167 if (ec_parse_device(device->handle, 0, ec, NULL) !=
1168 AE_CTRL_TERMINATE) {
1169 kfree(ec);
1170 return -EINVAL;
1171 }
1172
1173 /* Find and register all query methods */
1174 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
1175 acpi_ec_register_query_methods, NULL, ec, NULL);
1176
1177 if (!first_ec)
1178 first_ec = ec;
1179 device->driver_data = ec;
1180
1181 ret = !!request_region(ec->data_addr, 1, "EC data");
1182 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1183 ret = !!request_region(ec->command_addr, 1, "EC cmd");
1184 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
1185
1186 pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
1187 ec->gpe, ec->command_addr, ec->data_addr);
1188
1189 ret = ec_install_handlers(ec);
1190
1191 /* EC is fully operational, allow queries */
1192 acpi_ec_enable_event(ec);
1193
1194 /* Clear stale _Q events if hardware might require that */
1195 if (EC_FLAGS_CLEAR_ON_RESUME)
1196 acpi_ec_clear(ec);
1197 return ret;
1198 }
1199
1200 static int acpi_ec_remove(struct acpi_device *device)
1201 {
1202 struct acpi_ec *ec;
1203 struct acpi_ec_query_handler *handler, *tmp;
1204
1205 if (!device)
1206 return -EINVAL;
1207
1208 ec = acpi_driver_data(device);
1209 ec_remove_handlers(ec);
1210 mutex_lock(&ec->mutex);
1211 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
1212 list_del(&handler->node);
1213 kfree(handler);
1214 }
1215 mutex_unlock(&ec->mutex);
1216 release_region(ec->data_addr, 1);
1217 release_region(ec->command_addr, 1);
1218 device->driver_data = NULL;
1219 if (ec == first_ec)
1220 first_ec = NULL;
1221 kfree(ec);
1222 return 0;
1223 }
1224
1225 static acpi_status
1226 ec_parse_io_ports(struct acpi_resource *resource, void *context)
1227 {
1228 struct acpi_ec *ec = context;
1229
1230 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1231 return AE_OK;
1232
1233 /*
1234 * The first address region returned is the data port, and
1235 * the second address region returned is the status/command
1236 * port.
1237 */
1238 if (ec->data_addr == 0)
1239 ec->data_addr = resource->data.io.minimum;
1240 else if (ec->command_addr == 0)
1241 ec->command_addr = resource->data.io.minimum;
1242 else
1243 return AE_CTRL_TERMINATE;
1244
1245 return AE_OK;
1246 }
1247
1248 int __init acpi_boot_ec_enable(void)
1249 {
1250 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
1251 return 0;
1252 if (!ec_install_handlers(boot_ec)) {
1253 first_ec = boot_ec;
1254 return 0;
1255 }
1256 return -EFAULT;
1257 }
1258
1259 static const struct acpi_device_id ec_device_ids[] = {
1260 {"PNP0C09", 0},
1261 {"", 0},
1262 };
1263
1264 /* Some BIOS do not survive early DSDT scan, skip it */
1265 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
1266 {
1267 EC_FLAGS_SKIP_DSDT_SCAN = 1;
1268 return 0;
1269 }
1270
1271 /* ASUStek often supplies us with broken ECDT, validate it */
1272 static int ec_validate_ecdt(const struct dmi_system_id *id)
1273 {
1274 EC_FLAGS_VALIDATE_ECDT = 1;
1275 return 0;
1276 }
1277
1278 /* MSI EC needs special treatment, enable it */
1279 static int ec_flag_msi(const struct dmi_system_id *id)
1280 {
1281 pr_debug("Detected MSI hardware, enabling workarounds.\n");
1282 EC_FLAGS_MSI = 1;
1283 EC_FLAGS_VALIDATE_ECDT = 1;
1284 return 0;
1285 }
1286
1287 /*
1288 * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
1289 * the GPE storm threshold back to 20
1290 */
1291 static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
1292 {
1293 pr_debug("Setting the EC GPE storm threshold to 20\n");
1294 ec_storm_threshold = 20;
1295 return 0;
1296 }
1297
1298 /*
1299 * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
1300 * which case, we complete the QR_EC without issuing it to the firmware.
1301 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1302 */
1303 static int ec_flag_query_handshake(const struct dmi_system_id *id)
1304 {
1305 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1306 EC_FLAGS_QUERY_HANDSHAKE = 1;
1307 return 0;
1308 }
1309
1310 /*
1311 * On some hardware it is necessary to clear events accumulated by the EC during
1312 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1313 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1314 *
1315 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1316 *
1317 * Ideally, the EC should also be instructed NOT to accumulate events during
1318 * sleep (which Windows seems to do somehow), but the interface to control this
1319 * behaviour is not known at this time.
1320 *
1321 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1322 * however it is very likely that other Samsung models are affected.
1323 *
1324 * On systems which don't accumulate _Q events during sleep, this extra check
1325 * should be harmless.
1326 */
1327 static int ec_clear_on_resume(const struct dmi_system_id *id)
1328 {
1329 pr_debug("Detected system needing EC poll on resume.\n");
1330 EC_FLAGS_CLEAR_ON_RESUME = 1;
1331 return 0;
1332 }
1333
1334 static struct dmi_system_id ec_dmi_table[] __initdata = {
1335 {
1336 ec_skip_dsdt_scan, "Compal JFL92", {
1337 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1338 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1339 {
1340 ec_flag_msi, "MSI hardware", {
1341 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1342 {
1343 ec_flag_msi, "MSI hardware", {
1344 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1345 {
1346 ec_flag_msi, "MSI hardware", {
1347 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1348 {
1349 ec_flag_msi, "MSI hardware", {
1350 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1351 {
1352 ec_flag_msi, "Quanta hardware", {
1353 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1354 DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1355 {
1356 ec_flag_msi, "Quanta hardware", {
1357 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1358 DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1359 {
1360 ec_flag_msi, "Clevo W350etq", {
1361 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
1362 DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
1363 {
1364 ec_validate_ecdt, "ASUS hardware", {
1365 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1366 {
1367 ec_validate_ecdt, "ASUS hardware", {
1368 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1369 {
1370 ec_enlarge_storm_threshold, "CLEVO hardware", {
1371 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
1372 DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
1373 {
1374 ec_skip_dsdt_scan, "HP Folio 13", {
1375 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1376 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
1377 {
1378 ec_validate_ecdt, "ASUS hardware", {
1379 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1380 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1381 {
1382 ec_clear_on_resume, "Samsung hardware", {
1383 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1384 {
1385 ec_flag_query_handshake, "Acer hardware", {
1386 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
1387 {},
1388 };
1389
1390 int __init acpi_ec_ecdt_probe(void)
1391 {
1392 acpi_status status;
1393 struct acpi_ec *saved_ec = NULL;
1394 struct acpi_table_ecdt *ecdt_ptr;
1395
1396 boot_ec = make_acpi_ec();
1397 if (!boot_ec)
1398 return -ENOMEM;
1399 /*
1400 * Generate a boot ec context
1401 */
1402 dmi_check_system(ec_dmi_table);
1403 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1404 (struct acpi_table_header **)&ecdt_ptr);
1405 if (ACPI_SUCCESS(status)) {
1406 pr_info("EC description table is found, configuring boot EC\n");
1407 boot_ec->command_addr = ecdt_ptr->control.address;
1408 boot_ec->data_addr = ecdt_ptr->data.address;
1409 boot_ec->gpe = ecdt_ptr->gpe;
1410 boot_ec->handle = ACPI_ROOT_OBJECT;
1411 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
1412 &boot_ec->handle);
1413 /* Don't trust ECDT, which comes from ASUSTek */
1414 if (!EC_FLAGS_VALIDATE_ECDT)
1415 goto install;
1416 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1417 if (!saved_ec)
1418 return -ENOMEM;
1419 /* fall through */
1420 }
1421
1422 if (EC_FLAGS_SKIP_DSDT_SCAN) {
1423 kfree(saved_ec);
1424 return -ENODEV;
1425 }
1426
1427 /* This workaround is needed only on some broken machines,
1428 * which require early EC, but fail to provide ECDT */
1429 pr_debug("Look up EC in DSDT\n");
1430 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1431 boot_ec, NULL);
1432 /* Check that acpi_get_devices actually find something */
1433 if (ACPI_FAILURE(status) || !boot_ec->handle)
1434 goto error;
1435 if (saved_ec) {
1436 /* try to find good ECDT from ASUSTek */
1437 if (saved_ec->command_addr != boot_ec->command_addr ||
1438 saved_ec->data_addr != boot_ec->data_addr ||
1439 saved_ec->gpe != boot_ec->gpe ||
1440 saved_ec->handle != boot_ec->handle)
1441 pr_info("ASUSTek keeps feeding us with broken "
1442 "ECDT tables, which are very hard to workaround. "
1443 "Trying to use DSDT EC info instead. Please send "
1444 "output of acpidump to linux-acpi@vger.kernel.org\n");
1445 kfree(saved_ec);
1446 saved_ec = NULL;
1447 } else {
1448 /* We really need to limit this workaround, the only ASUS,
1449 * which needs it, has fake EC._INI method, so use it as flag.
1450 * Keep boot_ec struct as it will be needed soon.
1451 */
1452 if (!dmi_name_in_vendors("ASUS") ||
1453 !acpi_has_method(boot_ec->handle, "_INI"))
1454 return -ENODEV;
1455 }
1456 install:
1457 if (!ec_install_handlers(boot_ec)) {
1458 first_ec = boot_ec;
1459 return 0;
1460 }
1461 error:
1462 kfree(boot_ec);
1463 kfree(saved_ec);
1464 boot_ec = NULL;
1465 return -ENODEV;
1466 }
1467
1468 static struct acpi_driver acpi_ec_driver = {
1469 .name = "ec",
1470 .class = ACPI_EC_CLASS,
1471 .ids = ec_device_ids,
1472 .ops = {
1473 .add = acpi_ec_add,
1474 .remove = acpi_ec_remove,
1475 },
1476 };
1477
1478 int __init acpi_ec_init(void)
1479 {
1480 int result = 0;
1481
1482 /* Now register the driver for the EC */
1483 result = acpi_bus_register_driver(&acpi_ec_driver);
1484 if (result < 0)
1485 return -ENODEV;
1486
1487 return result;
1488 }
1489
1490 /* EC driver currently not unloadable */
1491 #if 0
1492 static void __exit acpi_ec_exit(void)
1493 {
1494
1495 acpi_bus_unregister_driver(&acpi_ec_driver);
1496 }
1497 #endif /* 0 */
This page took 0.063533 seconds and 6 git commands to generate.