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