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