ACPI / EC: Convert event handling work queue into loop style.
[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
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)) {
9d8993be
LZ
387 ec_dbg_evt("Command(%s) submitted/blocked",
388 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
389 ec->nr_pending_queries++;
74443bbe
LZ
390 schedule_work(&ec->work);
391 }
392}
393
37d11391 394static void acpi_ec_complete_query(struct acpi_ec *ec)
74443bbe 395{
f8b8eb71 396 if (test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
37d11391 397 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
9d8993be
LZ
398 ec_dbg_evt("Command(%s) unblocked",
399 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
74443bbe
LZ
400 }
401}
402
d8d031a6
LZ
403static int ec_transaction_polled(struct acpi_ec *ec)
404{
405 unsigned long flags;
406 int ret = 0;
407
408 spin_lock_irqsave(&ec->lock, flags);
409 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
410 ret = 1;
411 spin_unlock_irqrestore(&ec->lock, flags);
412 return ret;
413}
414
f92fca00 415static int ec_transaction_completed(struct acpi_ec *ec)
6ffb221a 416{
7c6db4e0
AS
417 unsigned long flags;
418 int ret = 0;
7a73e60e 419
f351d027 420 spin_lock_irqsave(&ec->lock, flags);
c0d65341 421 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
7c6db4e0 422 ret = 1;
f351d027 423 spin_unlock_irqrestore(&ec->lock, flags);
7c6db4e0 424 return ret;
45bea155 425}
451566f4 426
f8b8eb71
LZ
427static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag)
428{
429 ec->curr->flags |= flag;
430 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
431 if (flag == ACPI_EC_COMMAND_POLL)
432 acpi_ec_complete_query(ec);
433 }
434}
435
0c78808f 436static void advance_transaction(struct acpi_ec *ec)
7c6db5e5 437{
36b15875 438 struct transaction *t;
66b42b78 439 u8 status;
c0d65341 440 bool wakeup = false;
b76b51ba 441
3535a3c1
LZ
442 ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
443 smp_processor_id());
ca37bfdf
LZ
444 /*
445 * By always clearing STS before handling all indications, we can
446 * ensure a hardware STS 0->1 change after this clearing can always
447 * trigger a GPE interrupt.
448 */
449 acpi_ec_clear_gpe(ec);
66b42b78 450 status = acpi_ec_read_status(ec);
36b15875 451 t = ec->curr;
b76b51ba 452 if (!t)
f92fca00
LZ
453 goto err;
454 if (t->flags & ACPI_EC_COMMAND_POLL) {
455 if (t->wlen > t->wi) {
456 if ((status & ACPI_EC_FLAG_IBF) == 0)
457 acpi_ec_write_data(ec, t->wdata[t->wi++]);
458 else
459 goto err;
460 } else if (t->rlen > t->ri) {
461 if ((status & ACPI_EC_FLAG_OBF) == 1) {
462 t->rdata[t->ri++] = acpi_ec_read_data(ec);
c0d65341 463 if (t->rlen == t->ri) {
f8b8eb71 464 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
3afcf2ec 465 if (t->command == ACPI_EC_COMMAND_QUERY)
9d8993be
LZ
466 ec_dbg_evt("Command(%s) completed by hardware",
467 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
c0d65341
LZ
468 wakeup = true;
469 }
f92fca00
LZ
470 } else
471 goto err;
472 } else if (t->wlen == t->wi &&
c0d65341 473 (status & ACPI_EC_FLAG_IBF) == 0) {
f8b8eb71 474 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
c0d65341
LZ
475 wakeup = true;
476 }
0c78808f 477 goto out;
f92fca00 478 } else {
79149001
LZ
479 if (EC_FLAGS_QUERY_HANDSHAKE &&
480 !(status & ACPI_EC_FLAG_SCI) &&
3afcf2ec 481 (t->command == ACPI_EC_COMMAND_QUERY)) {
f8b8eb71 482 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
3afcf2ec 483 t->rdata[t->ri++] = 0x00;
f8b8eb71 484 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
9d8993be
LZ
485 ec_dbg_evt("Command(%s) completed by software",
486 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
3afcf2ec
LZ
487 wakeup = true;
488 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
f92fca00 489 acpi_ec_write_cmd(ec, t->command);
f8b8eb71 490 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
7c6db4e0 491 } else
dd15f8c4 492 goto err;
0c78808f 493 goto out;
f92fca00 494 }
dd15f8c4 495err:
a3cd8d27
FT
496 /*
497 * If SCI bit is set, then don't think it's a false IRQ
498 * otherwise will take a not handled IRQ as a false one.
499 */
f92fca00 500 if (!(status & ACPI_EC_FLAG_SCI)) {
e1d4d90f
LZ
501 if (in_interrupt() && t) {
502 if (t->irq_count < ec_storm_threshold)
503 ++t->irq_count;
504 /* Allow triggering on 0 threshold */
505 if (t->irq_count == ec_storm_threshold)
506 acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
507 }
f92fca00 508 }
0c78808f 509out:
74443bbe 510 if (status & ACPI_EC_FLAG_SCI)
37d11391 511 acpi_ec_submit_query(ec);
0c78808f
LZ
512 if (wakeup && in_interrupt())
513 wake_up(&ec->wait);
f92fca00 514}
a3cd8d27 515
f92fca00
LZ
516static void start_transaction(struct acpi_ec *ec)
517{
518 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
519 ec->curr->flags = 0;
d8d031a6
LZ
520}
521
522static int ec_guard(struct acpi_ec *ec)
523{
15de603b 524 unsigned long guard = usecs_to_jiffies(ec_polling_guard);
d8d031a6
LZ
525 unsigned long timeout = ec->timestamp + guard;
526
527 do {
15de603b 528 if (ec_busy_polling) {
d8d031a6
LZ
529 /* Perform busy polling */
530 if (ec_transaction_completed(ec))
531 return 0;
532 udelay(jiffies_to_usecs(guard));
533 } else {
534 /*
535 * Perform wait polling
536 *
537 * The following check is there to keep the old
538 * logic - no inter-transaction guarding for the
539 * wait polling mode.
540 */
541 if (!ec_transaction_polled(ec))
542 break;
543 if (wait_event_timeout(ec->wait,
544 ec_transaction_completed(ec),
545 guard))
546 return 0;
547 }
548 /* Guard the register accesses for the polling modes */
549 } while (time_before(jiffies, timeout));
550 return -ETIME;
845625cd 551}
03d1d99c 552
7c6db4e0
AS
553static int ec_poll(struct acpi_ec *ec)
554{
2a84cb98 555 unsigned long flags;
28fe5c82 556 int repeat = 5; /* number of command restarts */
7a73e60e 557
2a84cb98
AS
558 while (repeat--) {
559 unsigned long delay = jiffies +
7a18e96d 560 msecs_to_jiffies(ec_delay);
2a84cb98 561 do {
d8d031a6
LZ
562 if (!ec_guard(ec))
563 return 0;
f92fca00 564 spin_lock_irqsave(&ec->lock, flags);
d8d031a6 565 advance_transaction(ec);
f92fca00 566 spin_unlock_irqrestore(&ec->lock, flags);
2a84cb98 567 } while (time_before(jiffies, delay));
16a26e85 568 pr_debug("controller reset, restart transaction\n");
f351d027 569 spin_lock_irqsave(&ec->lock, flags);
2a84cb98 570 start_transaction(ec);
f351d027 571 spin_unlock_irqrestore(&ec->lock, flags);
af3fd140 572 }
b77d81b2 573 return -ETIME;
1da177e4
LT
574}
575
8463200a 576static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
2a84cb98 577 struct transaction *t)
45bea155 578{
7c6db4e0 579 unsigned long tmp;
7c6db4e0 580 int ret = 0;
7a73e60e 581
7c6db4e0 582 /* start transaction */
f351d027 583 spin_lock_irqsave(&ec->lock, tmp);
9887d22a 584 /* Enable GPE for command processing (IBF=0/OBF=1) */
37d11391 585 if (!acpi_ec_submit_flushable_request(ec)) {
ad479e7f
LZ
586 ret = -EINVAL;
587 goto unlock;
588 }
770970f0 589 ec_dbg_ref(ec, "Increase command");
7c6db4e0 590 /* following two actions should be kept atomic */
8463200a 591 ec->curr = t;
3535a3c1 592 ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
a2f93aea 593 start_transaction(ec);
df9ff918 594 spin_unlock_irqrestore(&ec->lock, tmp);
d8d031a6 595
df9ff918 596 ret = ec_poll(ec);
d8d031a6 597
df9ff918 598 spin_lock_irqsave(&ec->lock, tmp);
e1d4d90f
LZ
599 if (t->irq_count == ec_storm_threshold)
600 acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
3535a3c1 601 ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
8463200a 602 ec->curr = NULL;
9887d22a
LZ
603 /* Disable GPE for command processing (IBF=0/OBF=1) */
604 acpi_ec_complete_request(ec);
770970f0 605 ec_dbg_ref(ec, "Decrease command");
ad479e7f 606unlock:
f351d027 607 spin_unlock_irqrestore(&ec->lock, tmp);
7c6db4e0
AS
608 return ret;
609}
610
2a84cb98 611static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
1da177e4 612{
d7a76e4c 613 int status;
50526df6 614 u32 glk;
7a73e60e 615
8463200a 616 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
d550d98d 617 return -EINVAL;
8463200a
AS
618 if (t->rdata)
619 memset(t->rdata, 0, t->rlen);
d8d031a6 620
f351d027 621 mutex_lock(&ec->mutex);
703959d4 622 if (ec->global_lock) {
1da177e4 623 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
c24e912b 624 if (ACPI_FAILURE(status)) {
7c6db4e0
AS
625 status = -ENODEV;
626 goto unlock;
c24e912b 627 }
1da177e4 628 }
a62e8f19 629
2a84cb98 630 status = acpi_ec_transaction_unlocked(ec, t);
a62e8f19 631
703959d4 632 if (ec->global_lock)
1da177e4 633 acpi_release_global_lock(glk);
7c6db4e0 634unlock:
f351d027 635 mutex_unlock(&ec->mutex);
d550d98d 636 return status;
1da177e4
LT
637}
638
8a383ef0 639static int acpi_ec_burst_enable(struct acpi_ec *ec)
c45aac43
AS
640{
641 u8 d;
8463200a
AS
642 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
643 .wdata = NULL, .rdata = &d,
644 .wlen = 0, .rlen = 1};
645
2a84cb98 646 return acpi_ec_transaction(ec, &t);
c45aac43
AS
647}
648
8a383ef0 649static int acpi_ec_burst_disable(struct acpi_ec *ec)
c45aac43 650{
8463200a
AS
651 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
652 .wdata = NULL, .rdata = NULL,
653 .wlen = 0, .rlen = 0};
654
7c6db4e0 655 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
2a84cb98 656 acpi_ec_transaction(ec, &t) : 0;
c45aac43
AS
657}
658
7a73e60e 659static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
3576cf61
DS
660{
661 int result;
662 u8 d;
8463200a
AS
663 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
664 .wdata = &address, .rdata = &d,
665 .wlen = 1, .rlen = 1};
3576cf61 666
2a84cb98 667 result = acpi_ec_transaction(ec, &t);
3576cf61
DS
668 *data = d;
669 return result;
670}
6ffb221a 671
3576cf61
DS
672static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
673{
6ccedb10 674 u8 wdata[2] = { address, data };
8463200a
AS
675 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
676 .wdata = wdata, .rdata = NULL,
677 .wlen = 2, .rlen = 0};
678
2a84cb98 679 return acpi_ec_transaction(ec, &t);
3576cf61
DS
680}
681
b76b51ba 682int ec_read(u8 addr, u8 *val)
1da177e4 683{
1da177e4 684 int err;
6ffb221a 685 u8 temp_data;
1da177e4
LT
686
687 if (!first_ec)
688 return -ENODEV;
689
d033879c 690 err = acpi_ec_read(first_ec, addr, &temp_data);
1da177e4
LT
691
692 if (!err) {
693 *val = temp_data;
694 return 0;
7a73e60e
LZ
695 }
696 return err;
1da177e4
LT
697}
698EXPORT_SYMBOL(ec_read);
699
50526df6 700int ec_write(u8 addr, u8 val)
1da177e4 701{
1da177e4
LT
702 int err;
703
704 if (!first_ec)
705 return -ENODEV;
706
d033879c 707 err = acpi_ec_write(first_ec, addr, val);
1da177e4
LT
708
709 return err;
710}
711EXPORT_SYMBOL(ec_write);
712
616362de 713int ec_transaction(u8 command,
7a73e60e
LZ
714 const u8 *wdata, unsigned wdata_len,
715 u8 *rdata, unsigned rdata_len)
45bea155 716{
8463200a
AS
717 struct transaction t = {.command = command,
718 .wdata = wdata, .rdata = rdata,
719 .wlen = wdata_len, .rlen = rdata_len};
7a73e60e 720
d7a76e4c
LP
721 if (!first_ec)
722 return -ENODEV;
45bea155 723
2a84cb98 724 return acpi_ec_transaction(first_ec, &t);
45bea155 725}
ab9e43c6
LP
726EXPORT_SYMBOL(ec_transaction);
727
3e2abc5a
SF
728/* Get the handle to the EC device */
729acpi_handle ec_get_handle(void)
730{
731 if (!first_ec)
732 return NULL;
733 return first_ec->handle;
734}
3e2abc5a
SF
735EXPORT_SYMBOL(ec_get_handle);
736
ad332c8a 737/*
3eba563e 738 * Process _Q events that might have accumulated in the EC.
ad332c8a
KC
739 * Run with locked ec mutex.
740 */
741static void acpi_ec_clear(struct acpi_ec *ec)
742{
743 int i, status;
744 u8 value = 0;
745
746 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
550b3aac 747 status = acpi_ec_query(ec, &value);
ad332c8a
KC
748 if (status || !value)
749 break;
750 }
751
752 if (unlikely(i == ACPI_EC_CLEAR_MAX))
753 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
754 else
755 pr_info("%d stale EC events cleared\n", i);
756}
757
ad479e7f
LZ
758static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&ec->lock, flags);
763 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
3535a3c1 764 ec_dbg_drv("Starting EC");
9887d22a 765 /* Enable GPE for event processing (SCI_EVT=1) */
770970f0 766 if (!resuming) {
9887d22a 767 acpi_ec_submit_request(ec);
770970f0
LZ
768 ec_dbg_ref(ec, "Increase driver");
769 }
3535a3c1 770 ec_log_drv("EC started");
ad479e7f
LZ
771 }
772 spin_unlock_irqrestore(&ec->lock, flags);
773}
774
9887d22a
LZ
775static bool acpi_ec_stopped(struct acpi_ec *ec)
776{
777 unsigned long flags;
778 bool flushed;
779
780 spin_lock_irqsave(&ec->lock, flags);
781 flushed = acpi_ec_flushed(ec);
782 spin_unlock_irqrestore(&ec->lock, flags);
783 return flushed;
784}
785
ad479e7f
LZ
786static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
787{
788 unsigned long flags;
789
790 spin_lock_irqsave(&ec->lock, flags);
791 if (acpi_ec_started(ec)) {
3535a3c1 792 ec_dbg_drv("Stopping EC");
ad479e7f 793 set_bit(EC_FLAGS_STOPPED, &ec->flags);
9887d22a
LZ
794 spin_unlock_irqrestore(&ec->lock, flags);
795 wait_event(ec->wait, acpi_ec_stopped(ec));
796 spin_lock_irqsave(&ec->lock, flags);
797 /* Disable GPE for event processing (SCI_EVT=1) */
770970f0 798 if (!suspending) {
9887d22a 799 acpi_ec_complete_request(ec);
770970f0
LZ
800 ec_dbg_ref(ec, "Decrease driver");
801 }
ad479e7f
LZ
802 clear_bit(EC_FLAGS_STARTED, &ec->flags);
803 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
3535a3c1 804 ec_log_drv("EC stopped");
ad479e7f
LZ
805 }
806 spin_unlock_irqrestore(&ec->lock, flags);
807}
808
fe955682 809void acpi_ec_block_transactions(void)
f6bb13aa
RW
810{
811 struct acpi_ec *ec = first_ec;
812
813 if (!ec)
814 return;
815
f351d027 816 mutex_lock(&ec->mutex);
f6bb13aa 817 /* Prevent transactions from being carried out */
ad479e7f 818 acpi_ec_stop(ec, true);
f351d027 819 mutex_unlock(&ec->mutex);
f6bb13aa
RW
820}
821
fe955682 822void acpi_ec_unblock_transactions(void)
f6bb13aa
RW
823{
824 struct acpi_ec *ec = first_ec;
825
826 if (!ec)
827 return;
828
f6bb13aa 829 /* Allow transactions to be carried out again */
ad479e7f 830 acpi_ec_start(ec, true);
ad332c8a
KC
831
832 if (EC_FLAGS_CLEAR_ON_RESUME)
833 acpi_ec_clear(ec);
f6bb13aa
RW
834}
835
fe955682 836void acpi_ec_unblock_transactions_early(void)
d5a64513
RW
837{
838 /*
839 * Allow transactions to happen again (this function is called from
840 * atomic context during wakeup, so we don't need to acquire the mutex).
841 */
842 if (first_ec)
ad479e7f 843 acpi_ec_start(first_ec, true);
d5a64513
RW
844}
845
1da177e4
LT
846/* --------------------------------------------------------------------------
847 Event Management
848 -------------------------------------------------------------------------- */
01305d41
LZ
849static struct acpi_ec_query_handler *
850acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
851{
852 if (handler)
853 kref_get(&handler->kref);
854 return handler;
855}
856
857static void acpi_ec_query_handler_release(struct kref *kref)
858{
859 struct acpi_ec_query_handler *handler =
860 container_of(kref, struct acpi_ec_query_handler, kref);
861
862 kfree(handler);
863}
864
865static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
866{
867 kref_put(&handler->kref, acpi_ec_query_handler_release);
868}
869
837012ed
AS
870int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
871 acpi_handle handle, acpi_ec_query_func func,
872 void *data)
873{
874 struct acpi_ec_query_handler *handler =
875 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
7a73e60e 876
837012ed
AS
877 if (!handler)
878 return -ENOMEM;
879
880 handler->query_bit = query_bit;
881 handler->handle = handle;
882 handler->func = func;
883 handler->data = data;
f351d027 884 mutex_lock(&ec->mutex);
01305d41 885 kref_init(&handler->kref);
30c08574 886 list_add(&handler->node, &ec->list);
f351d027 887 mutex_unlock(&ec->mutex);
837012ed
AS
888 return 0;
889}
837012ed
AS
890EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
891
892void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
893{
1544fdbc 894 struct acpi_ec_query_handler *handler, *tmp;
01305d41 895 LIST_HEAD(free_list);
7a73e60e 896
f351d027 897 mutex_lock(&ec->mutex);
1544fdbc 898 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed 899 if (query_bit == handler->query_bit) {
01305d41
LZ
900 list_del_init(&handler->node);
901 list_add(&handler->node, &free_list);
837012ed
AS
902 }
903 }
f351d027 904 mutex_unlock(&ec->mutex);
6b5eab54 905 list_for_each_entry_safe(handler, tmp, &free_list, node)
01305d41 906 acpi_ec_put_query_handler(handler);
837012ed 907}
837012ed 908EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
1da177e4 909
a62e8f19 910static void acpi_ec_run(void *cxt)
45bea155 911{
a62e8f19 912 struct acpi_ec_query_handler *handler = cxt;
7a73e60e 913
a62e8f19 914 if (!handler)
e41334c0 915 return;
3535a3c1 916 ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
a62e8f19
AS
917 if (handler->func)
918 handler->func(handler->data);
919 else if (handler->handle)
920 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
3535a3c1 921 ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
01305d41 922 acpi_ec_put_query_handler(handler);
a62e8f19
AS
923}
924
550b3aac 925static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
a62e8f19
AS
926{
927 u8 value = 0;
c2cf5769
LZ
928 int result;
929 acpi_status status;
01305d41 930 struct acpi_ec_query_handler *handler;
550b3aac
LZ
931 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
932 .wdata = NULL, .rdata = &value,
933 .wlen = 0, .rlen = 1};
3eba563e 934
550b3aac
LZ
935 /*
936 * Query the EC to find out which _Qxx method we need to evaluate.
937 * Note that successful completion of the query causes the ACPI_EC_SCI
938 * bit to be cleared (and thus clearing the interrupt source).
939 */
940 result = acpi_ec_transaction(ec, &t);
c2cf5769
LZ
941 if (result)
942 return result;
550b3aac
LZ
943 if (data)
944 *data = value;
945 if (!value)
946 return -ENODATA;
3eba563e 947
550b3aac 948 mutex_lock(&ec->mutex);
837012ed
AS
949 list_for_each_entry(handler, &ec->list, node) {
950 if (value == handler->query_bit) {
951 /* have custom handler for this bit */
01305d41 952 handler = acpi_ec_get_query_handler(handler);
3535a3c1
LZ
953 ec_dbg_evt("Query(0x%02x) scheduled",
954 handler->query_bit);
c2cf5769 955 status = acpi_os_execute((handler->func) ?
f5347867 956 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
01305d41 957 acpi_ec_run, handler);
c2cf5769
LZ
958 if (ACPI_FAILURE(status))
959 result = -EBUSY;
960 break;
837012ed
AS
961 }
962 }
550b3aac 963 mutex_unlock(&ec->mutex);
c2cf5769 964 return result;
a62e8f19
AS
965}
966
9d8993be 967static void acpi_ec_event_handler(struct work_struct *work)
a62e8f19 968{
9d8993be 969 unsigned long flags;
74443bbe 970 struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
7a73e60e 971
9d8993be
LZ
972 ec_dbg_evt("Event started");
973
974 spin_lock_irqsave(&ec->lock, flags);
975 while (ec->nr_pending_queries) {
976 spin_unlock_irqrestore(&ec->lock, flags);
977 (void)acpi_ec_query(ec, NULL);
978 spin_lock_irqsave(&ec->lock, flags);
979 ec->nr_pending_queries--;
980 }
981 spin_unlock_irqrestore(&ec->lock, flags);
982
983 ec_dbg_evt("Event stopped");
45bea155 984}
1da177e4 985
8b6cd8ad
LM
986static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
987 u32 gpe_number, void *data)
1da177e4 988{
f92fca00 989 unsigned long flags;
3d02b90b 990 struct acpi_ec *ec = data;
7c6db4e0 991
f92fca00 992 spin_lock_irqsave(&ec->lock, flags);
0c78808f 993 advance_transaction(ec);
c0d65341 994 spin_unlock_irqrestore(&ec->lock, flags);
ca37bfdf 995 return ACPI_INTERRUPT_HANDLED;
845625cd
AS
996}
997
1da177e4 998/* --------------------------------------------------------------------------
7a73e60e
LZ
999 * Address Space Management
1000 * -------------------------------------------------------------------------- */
1da177e4 1001
1da177e4 1002static acpi_status
5b7734b4 1003acpi_ec_space_handler(u32 function, acpi_physical_address address,
dadf28a1 1004 u32 bits, u64 *value64,
50526df6 1005 void *handler_context, void *region_context)
1da177e4 1006{
3d02b90b 1007 struct acpi_ec *ec = handler_context;
dadf28a1
AS
1008 int result = 0, i, bytes = bits / 8;
1009 u8 *value = (u8 *)value64;
1da177e4 1010
1da177e4 1011 if ((address > 0xFF) || !value || !handler_context)
d550d98d 1012 return AE_BAD_PARAMETER;
1da177e4 1013
5b7734b4 1014 if (function != ACPI_READ && function != ACPI_WRITE)
d550d98d 1015 return AE_BAD_PARAMETER;
1da177e4 1016
15de603b 1017 if (ec_busy_polling || bits > 8)
6a63b06f 1018 acpi_ec_burst_enable(ec);
b3b233c7 1019
dadf28a1
AS
1020 for (i = 0; i < bytes; ++i, ++address, ++value)
1021 result = (function == ACPI_READ) ?
1022 acpi_ec_read(ec, address, value) :
1023 acpi_ec_write(ec, address, *value);
1da177e4 1024
15de603b 1025 if (ec_busy_polling || bits > 8)
6a63b06f 1026 acpi_ec_burst_disable(ec);
b3b233c7 1027
1da177e4
LT
1028 switch (result) {
1029 case -EINVAL:
d550d98d 1030 return AE_BAD_PARAMETER;
1da177e4 1031 case -ENODEV:
d550d98d 1032 return AE_NOT_FOUND;
1da177e4 1033 case -ETIME:
d550d98d 1034 return AE_TIME;
1da177e4 1035 default:
d550d98d 1036 return AE_OK;
1da177e4 1037 }
1da177e4
LT
1038}
1039
1da177e4 1040/* --------------------------------------------------------------------------
7a73e60e
LZ
1041 * Driver Interface
1042 * -------------------------------------------------------------------------- */
1043
c0900c35
AS
1044static acpi_status
1045ec_parse_io_ports(struct acpi_resource *resource, void *context);
1046
c0900c35
AS
1047static struct acpi_ec *make_acpi_ec(void)
1048{
1049 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
7a73e60e 1050
c0900c35
AS
1051 if (!ec)
1052 return NULL;
37d11391 1053 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
f351d027 1054 mutex_init(&ec->mutex);
c0900c35 1055 init_waitqueue_head(&ec->wait);
837012ed 1056 INIT_LIST_HEAD(&ec->list);
f351d027 1057 spin_lock_init(&ec->lock);
9d8993be 1058 INIT_WORK(&ec->work, acpi_ec_event_handler);
d8d031a6 1059 ec->timestamp = jiffies;
c0900c35
AS
1060 return ec;
1061}
837012ed 1062
c019b193
AS
1063static acpi_status
1064acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1065 void *context, void **return_value)
1066{
0175d562
LM
1067 char node_name[5];
1068 struct acpi_buffer buffer = { sizeof(node_name), node_name };
c019b193
AS
1069 struct acpi_ec *ec = context;
1070 int value = 0;
0175d562
LM
1071 acpi_status status;
1072
1073 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1074
7a73e60e 1075 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
c019b193 1076 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
c019b193
AS
1077 return AE_OK;
1078}
1079
cd8c93a4
AS
1080static acpi_status
1081ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
837012ed 1082{
cd8c93a4 1083 acpi_status status;
d21cf3c1 1084 unsigned long long tmp = 0;
cd8c93a4 1085 struct acpi_ec *ec = context;
a5032bfd
AS
1086
1087 /* clear addr values, ec_parse_io_ports depend on it */
1088 ec->command_addr = ec->data_addr = 0;
1089
cd8c93a4
AS
1090 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1091 ec_parse_io_ports, ec);
1092 if (ACPI_FAILURE(status))
1093 return status;
837012ed
AS
1094
1095 /* Get GPE bit assignment (EC events). */
1096 /* TODO: Add support for _GPE returning a package */
27663c58 1097 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
cd8c93a4
AS
1098 if (ACPI_FAILURE(status))
1099 return status;
27663c58 1100 ec->gpe = tmp;
837012ed 1101 /* Use the global lock for all EC transactions? */
d21cf3c1 1102 tmp = 0;
27663c58
MW
1103 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1104 ec->global_lock = tmp;
837012ed 1105 ec->handle = handle;
cd8c93a4 1106 return AE_CTRL_TERMINATE;
837012ed
AS
1107}
1108
5efc5476
BH
1109static int ec_install_handlers(struct acpi_ec *ec)
1110{
1111 acpi_status status;
7a73e60e 1112
5efc5476
BH
1113 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1114 return 0;
ca37bfdf 1115 status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
5efc5476
BH
1116 ACPI_GPE_EDGE_TRIGGERED,
1117 &acpi_ec_gpe_handler, ec);
1118 if (ACPI_FAILURE(status))
1119 return -ENODEV;
9630bdd9 1120
ad479e7f 1121 acpi_ec_start(ec, false);
5efc5476
BH
1122 status = acpi_install_address_space_handler(ec->handle,
1123 ACPI_ADR_SPACE_EC,
1124 &acpi_ec_space_handler,
1125 NULL, ec);
1126 if (ACPI_FAILURE(status)) {
1127 if (status == AE_NOT_FOUND) {
1128 /*
1129 * Maybe OS fails in evaluating the _REG object.
1130 * The AE_NOT_FOUND error will be ignored and OS
1131 * continue to initialize EC.
1132 */
16a26e85 1133 pr_err("Fail in evaluating the _REG object"
5efc5476
BH
1134 " of EC device. Broken bios is suspected.\n");
1135 } else {
ad479e7f 1136 acpi_ec_stop(ec, false);
5efc5476
BH
1137 acpi_remove_gpe_handler(NULL, ec->gpe,
1138 &acpi_ec_gpe_handler);
1139 return -ENODEV;
1140 }
1141 }
1142
1143 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1144 return 0;
1145}
1146
4c611060
AS
1147static void ec_remove_handlers(struct acpi_ec *ec)
1148{
1741acea
LZ
1149 if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1150 return;
ad479e7f 1151 acpi_ec_stop(ec, false);
4c611060
AS
1152 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1153 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
16a26e85 1154 pr_err("failed to remove space handler\n");
4c611060
AS
1155 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1156 &acpi_ec_gpe_handler)))
16a26e85 1157 pr_err("failed to remove gpe handler\n");
7c6db4e0 1158 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
4c611060
AS
1159}
1160
703959d4 1161static int acpi_ec_add(struct acpi_device *device)
1da177e4 1162{
703959d4 1163 struct acpi_ec *ec = NULL;
d02be047 1164 int ret;
45bea155 1165
c0900c35
AS
1166 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1167 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1168
4c611060 1169 /* Check for boot EC */
ce52ddf5
AS
1170 if (boot_ec &&
1171 (boot_ec->handle == device->handle ||
1172 boot_ec->handle == ACPI_ROOT_OBJECT)) {
1173 ec = boot_ec;
1174 boot_ec = NULL;
1175 } else {
1176 ec = make_acpi_ec();
1177 if (!ec)
1178 return -ENOMEM;
a5032bfd
AS
1179 }
1180 if (ec_parse_device(device->handle, 0, ec, NULL) !=
1181 AE_CTRL_TERMINATE) {
ce52ddf5
AS
1182 kfree(ec);
1183 return -EINVAL;
4c611060
AS
1184 }
1185
ce52ddf5
AS
1186 /* Find and register all query methods */
1187 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
2263576c 1188 acpi_ec_register_query_methods, NULL, ec, NULL);
ce52ddf5 1189
4c611060
AS
1190 if (!first_ec)
1191 first_ec = ec;
db89b4f0 1192 device->driver_data = ec;
de4f1046 1193
d6795fe3
AK
1194 ret = !!request_region(ec->data_addr, 1, "EC data");
1195 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1196 ret = !!request_region(ec->command_addr, 1, "EC cmd");
1197 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
de4f1046 1198
16a26e85 1199 pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
4c611060 1200 ec->gpe, ec->command_addr, ec->data_addr);
5efc5476
BH
1201
1202 ret = ec_install_handlers(ec);
1203
1c832b3e
LT
1204 /* Reprobe devices depending on the EC */
1205 acpi_walk_dep_device_list(ec->handle);
1206
5efc5476 1207 /* EC is fully operational, allow queries */
37d11391 1208 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
ad332c8a
KC
1209
1210 /* Clear stale _Q events if hardware might require that */
550b3aac 1211 if (EC_FLAGS_CLEAR_ON_RESUME)
ad332c8a 1212 acpi_ec_clear(ec);
5efc5476 1213 return ret;
1da177e4
LT
1214}
1215
51fac838 1216static int acpi_ec_remove(struct acpi_device *device)
1da177e4 1217{
01f22462 1218 struct acpi_ec *ec;
07ddf768 1219 struct acpi_ec_query_handler *handler, *tmp;
1da177e4 1220
1da177e4 1221 if (!device)
d550d98d 1222 return -EINVAL;
1da177e4
LT
1223
1224 ec = acpi_driver_data(device);
cf745ec7 1225 ec_remove_handlers(ec);
f351d027 1226 mutex_lock(&ec->mutex);
07ddf768 1227 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
837012ed
AS
1228 list_del(&handler->node);
1229 kfree(handler);
1230 }
f351d027 1231 mutex_unlock(&ec->mutex);
de4f1046
TR
1232 release_region(ec->data_addr, 1);
1233 release_region(ec->command_addr, 1);
db89b4f0 1234 device->driver_data = NULL;
d033879c 1235 if (ec == first_ec)
c0900c35 1236 first_ec = NULL;
4c611060 1237 kfree(ec);
d550d98d 1238 return 0;
1da177e4
LT
1239}
1240
1da177e4 1241static acpi_status
c0900c35 1242ec_parse_io_ports(struct acpi_resource *resource, void *context)
1da177e4 1243{
3d02b90b 1244 struct acpi_ec *ec = context;
1da177e4 1245
01f22462 1246 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1da177e4 1247 return AE_OK;
1da177e4
LT
1248
1249 /*
1250 * The first address region returned is the data port, and
1251 * the second address region returned is the status/command
1252 * port.
1253 */
de4f1046 1254 if (ec->data_addr == 0)
6ffb221a 1255 ec->data_addr = resource->data.io.minimum;
de4f1046 1256 else if (ec->command_addr == 0)
6ffb221a 1257 ec->command_addr = resource->data.io.minimum;
01f22462 1258 else
1da177e4 1259 return AE_CTRL_TERMINATE;
1da177e4 1260
1da177e4
LT
1261 return AE_OK;
1262}
1263
c04209a7
AS
1264int __init acpi_boot_ec_enable(void)
1265{
7c6db4e0 1266 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
c04209a7
AS
1267 return 0;
1268 if (!ec_install_handlers(boot_ec)) {
1269 first_ec = boot_ec;
1270 return 0;
1271 }
1272 return -EFAULT;
1273}
1274
223883b7
AS
1275static const struct acpi_device_id ec_device_ids[] = {
1276 {"PNP0C09", 0},
1277 {"", 0},
1278};
1279
478fa03b
AS
1280/* Some BIOS do not survive early DSDT scan, skip it */
1281static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
1282{
1283 EC_FLAGS_SKIP_DSDT_SCAN = 1;
1284 return 0;
1285}
1286
0adf3c74
AS
1287/* ASUStek often supplies us with broken ECDT, validate it */
1288static int ec_validate_ecdt(const struct dmi_system_id *id)
1289{
1290 EC_FLAGS_VALIDATE_ECDT = 1;
1291 return 0;
1292}
1293
79149001
LZ
1294/*
1295 * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
1296 * which case, we complete the QR_EC without issuing it to the firmware.
1297 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1298 */
1299static int ec_flag_query_handshake(const struct dmi_system_id *id)
1300{
1301 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1302 EC_FLAGS_QUERY_HANDSHAKE = 1;
1303 return 0;
1304}
1305
ad332c8a
KC
1306/*
1307 * On some hardware it is necessary to clear events accumulated by the EC during
1308 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1309 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1310 *
1311 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1312 *
1313 * Ideally, the EC should also be instructed NOT to accumulate events during
1314 * sleep (which Windows seems to do somehow), but the interface to control this
1315 * behaviour is not known at this time.
1316 *
1317 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1318 * however it is very likely that other Samsung models are affected.
1319 *
1320 * On systems which don't accumulate _Q events during sleep, this extra check
1321 * should be harmless.
1322 */
1323static int ec_clear_on_resume(const struct dmi_system_id *id)
1324{
1325 pr_debug("Detected system needing EC poll on resume.\n");
1326 EC_FLAGS_CLEAR_ON_RESUME = 1;
1327 return 0;
1328}
1329
6cef7497 1330static struct dmi_system_id ec_dmi_table[] __initdata = {
478fa03b
AS
1331 {
1332 ec_skip_dsdt_scan, "Compal JFL92", {
1333 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1334 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
0adf3c74 1335 {
3174abcf
LZ
1336 ec_validate_ecdt, "MSI MS-171F", {
1337 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"),
1338 DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL},
777cb382 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.104324 seconds and 5 git commands to generate.