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