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