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