Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7c6db4e0 | 2 | * ec.c - ACPI Embedded Controller Driver (v2.1) |
1da177e4 | 3 | * |
7c6db4e0 | 4 | * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de> |
01f22462 | 5 | * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com> |
1da177e4 LT |
6 | * Copyright (C) 2004 Luming Yu <luming.yu@intel.com> |
7 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
8 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
9 | * | |
10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
25 | * | |
26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
27 | */ | |
28 | ||
7c6db4e0 | 29 | /* Uncomment next line to get verbose printout */ |
d772b3b3 MN |
30 | /* #define DEBUG */ |
31 | ||
1da177e4 LT |
32 | #include <linux/kernel.h> |
33 | #include <linux/module.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/types.h> | |
36 | #include <linux/delay.h> | |
451566f4 | 37 | #include <linux/interrupt.h> |
837012ed | 38 | #include <linux/list.h> |
7c6db4e0 | 39 | #include <linux/spinlock.h> |
5a0e3ad6 | 40 | #include <linux/slab.h> |
1da177e4 LT |
41 | #include <asm/io.h> |
42 | #include <acpi/acpi_bus.h> | |
43 | #include <acpi/acpi_drivers.h> | |
eb27cae8 | 44 | #include <linux/dmi.h> |
1da177e4 | 45 | |
1195a098 TR |
46 | #include "internal.h" |
47 | ||
1da177e4 | 48 | #define ACPI_EC_CLASS "embedded_controller" |
1da177e4 LT |
49 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
50 | #define ACPI_EC_FILE_INFO "info" | |
837012ed | 51 | |
1195a098 | 52 | #undef PREFIX |
af3fd140 | 53 | #define PREFIX "ACPI: EC: " |
4350933a | 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 */ | |
451566f4 | 58 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
1da177e4 | 59 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
4350933a | 60 | |
703959d4 | 61 | /* EC commands */ |
3261ff4d | 62 | enum ec_command { |
6ccedb10 AS |
63 | ACPI_EC_COMMAND_READ = 0x80, |
64 | ACPI_EC_COMMAND_WRITE = 0x81, | |
65 | ACPI_EC_BURST_ENABLE = 0x82, | |
66 | ACPI_EC_BURST_DISABLE = 0x83, | |
67 | ACPI_EC_COMMAND_QUERY = 0x84, | |
3261ff4d | 68 | }; |
837012ed | 69 | |
5c406412 | 70 | #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ |
703959d4 | 71 | #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ |
2a84cb98 | 72 | #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */ |
703959d4 | 73 | |
080e412c | 74 | enum { |
080e412c | 75 | EC_FLAGS_QUERY_PENDING, /* Query is pending */ |
7c6db4e0 | 76 | EC_FLAGS_GPE_STORM, /* GPE storm detected */ |
f6bb13aa | 77 | EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and |
7c6db4e0 | 78 | * OpReg are installed */ |
fe955682 | 79 | EC_FLAGS_BLOCKED, /* Transactions are blocked */ |
1da177e4 | 80 | }; |
6ffb221a | 81 | |
7a18e96d TR |
82 | /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */ |
83 | static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY; | |
84 | module_param(ec_delay, uint, 0644); | |
85 | MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes"); | |
86 | ||
a520d52e FT |
87 | /* |
88 | * If the number of false interrupts per one transaction exceeds | |
89 | * this threshold, will think there is a GPE storm happened and | |
90 | * will disable the GPE for normal transaction. | |
91 | */ | |
92 | static unsigned int ec_storm_threshold __read_mostly = 8; | |
93 | module_param(ec_storm_threshold, uint, 0644); | |
94 | MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm"); | |
95 | ||
6ffb221a | 96 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ |
d033879c | 97 | /* External interfaces use first EC only, so remember */ |
837012ed AS |
98 | typedef int (*acpi_ec_query_func) (void *data); |
99 | ||
100 | struct acpi_ec_query_handler { | |
101 | struct list_head node; | |
102 | acpi_ec_query_func func; | |
103 | acpi_handle handle; | |
104 | void *data; | |
105 | u8 query_bit; | |
106 | }; | |
107 | ||
8463200a | 108 | struct transaction { |
7c6db4e0 AS |
109 | const u8 *wdata; |
110 | u8 *rdata; | |
111 | unsigned short irq_count; | |
8463200a | 112 | u8 command; |
a2f93aea AS |
113 | u8 wi; |
114 | u8 ri; | |
7c6db4e0 AS |
115 | u8 wlen; |
116 | u8 rlen; | |
dd15f8c4 | 117 | bool done; |
7c6db4e0 AS |
118 | }; |
119 | ||
1195a098 TR |
120 | struct acpi_ec *boot_ec, *first_ec; |
121 | EXPORT_SYMBOL(first_ec); | |
703959d4 | 122 | |
5423a0cb | 123 | static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */ |
0adf3c74 | 124 | static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */ |
478fa03b | 125 | static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */ |
5423a0cb | 126 | |
1da177e4 LT |
127 | /* -------------------------------------------------------------------------- |
128 | Transaction Management | |
129 | -------------------------------------------------------------------------- */ | |
130 | ||
6ffb221a | 131 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 132 | { |
3ebe08a7 | 133 | u8 x = inb(ec->command_addr); |
86dae015 | 134 | pr_debug(PREFIX "---> status = 0x%2.2x\n", x); |
3ebe08a7 | 135 | return x; |
451566f4 DT |
136 | } |
137 | ||
6ffb221a | 138 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 139 | { |
3ebe08a7 | 140 | u8 x = inb(ec->data_addr); |
86dae015 | 141 | pr_debug(PREFIX "---> data = 0x%2.2x\n", x); |
7c6db4e0 | 142 | return x; |
7c6db5e5 DS |
143 | } |
144 | ||
6ffb221a | 145 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 146 | { |
86dae015 | 147 | pr_debug(PREFIX "<--- command = 0x%2.2x\n", command); |
6ffb221a | 148 | outb(command, ec->command_addr); |
45bea155 LY |
149 | } |
150 | ||
6ffb221a | 151 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 152 | { |
86dae015 | 153 | pr_debug(PREFIX "<--- data = 0x%2.2x\n", data); |
6ffb221a | 154 | outb(data, ec->data_addr); |
703959d4 | 155 | } |
45bea155 | 156 | |
7c6db4e0 | 157 | static int ec_transaction_done(struct acpi_ec *ec) |
6ffb221a | 158 | { |
7c6db4e0 AS |
159 | unsigned long flags; |
160 | int ret = 0; | |
f351d027 | 161 | spin_lock_irqsave(&ec->lock, flags); |
dd15f8c4 | 162 | if (!ec->curr || ec->curr->done) |
7c6db4e0 | 163 | ret = 1; |
f351d027 | 164 | spin_unlock_irqrestore(&ec->lock, flags); |
7c6db4e0 | 165 | return ret; |
45bea155 | 166 | } |
451566f4 | 167 | |
a2f93aea AS |
168 | static void start_transaction(struct acpi_ec *ec) |
169 | { | |
170 | ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0; | |
171 | ec->curr->done = false; | |
172 | acpi_ec_write_cmd(ec, ec->curr->command); | |
173 | } | |
174 | ||
2a84cb98 | 175 | static void advance_transaction(struct acpi_ec *ec, u8 status) |
7c6db5e5 | 176 | { |
7c6db4e0 | 177 | unsigned long flags; |
968f56cc | 178 | struct transaction *t; |
b76b51ba | 179 | |
f351d027 | 180 | spin_lock_irqsave(&ec->lock, flags); |
968f56cc | 181 | t = ec->curr; |
b76b51ba | 182 | if (!t) |
7c6db4e0 | 183 | goto unlock; |
b76b51ba | 184 | if (t->wlen > t->wi) { |
a2f93aea AS |
185 | if ((status & ACPI_EC_FLAG_IBF) == 0) |
186 | acpi_ec_write_data(ec, | |
b76b51ba | 187 | t->wdata[t->wi++]); |
a2f93aea | 188 | else |
dd15f8c4 | 189 | goto err; |
b76b51ba | 190 | } else if (t->rlen > t->ri) { |
7c6db4e0 | 191 | if ((status & ACPI_EC_FLAG_OBF) == 1) { |
b76b51ba FT |
192 | t->rdata[t->ri++] = acpi_ec_read_data(ec); |
193 | if (t->rlen == t->ri) | |
194 | t->done = true; | |
7c6db4e0 | 195 | } else |
dd15f8c4 | 196 | goto err; |
b76b51ba | 197 | } else if (t->wlen == t->wi && |
a2f93aea | 198 | (status & ACPI_EC_FLAG_IBF) == 0) |
b76b51ba | 199 | t->done = true; |
dd15f8c4 AS |
200 | goto unlock; |
201 | err: | |
a3cd8d27 FT |
202 | /* |
203 | * If SCI bit is set, then don't think it's a false IRQ | |
204 | * otherwise will take a not handled IRQ as a false one. | |
205 | */ | |
206 | if (in_interrupt() && !(status & ACPI_EC_FLAG_SCI)) | |
b76b51ba | 207 | ++t->irq_count; |
a3cd8d27 | 208 | |
7c6db4e0 | 209 | unlock: |
f351d027 | 210 | spin_unlock_irqrestore(&ec->lock, flags); |
845625cd | 211 | } |
03d1d99c | 212 | |
a62e8f19 | 213 | static int acpi_ec_sync_query(struct acpi_ec *ec); |
7c6db4e0 | 214 | |
a62e8f19 | 215 | static int ec_check_sci_sync(struct acpi_ec *ec, u8 state) |
7c6db5e5 | 216 | { |
7c6db4e0 AS |
217 | if (state & ACPI_EC_FLAG_SCI) { |
218 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) | |
a62e8f19 | 219 | return acpi_ec_sync_query(ec); |
7c6db4e0 AS |
220 | } |
221 | return 0; | |
222 | } | |
223 | ||
224 | static int ec_poll(struct acpi_ec *ec) | |
225 | { | |
2a84cb98 | 226 | unsigned long flags; |
28fe5c82 | 227 | int repeat = 5; /* number of command restarts */ |
2a84cb98 AS |
228 | while (repeat--) { |
229 | unsigned long delay = jiffies + | |
7a18e96d | 230 | msecs_to_jiffies(ec_delay); |
2a84cb98 AS |
231 | do { |
232 | /* don't sleep with disabled interrupts */ | |
233 | if (EC_FLAGS_MSI || irqs_disabled()) { | |
234 | udelay(ACPI_EC_MSI_UDELAY); | |
235 | if (ec_transaction_done(ec)) | |
236 | return 0; | |
237 | } else { | |
238 | if (wait_event_timeout(ec->wait, | |
239 | ec_transaction_done(ec), | |
240 | msecs_to_jiffies(1))) | |
241 | return 0; | |
242 | } | |
243 | advance_transaction(ec, acpi_ec_read_status(ec)); | |
244 | } while (time_before(jiffies, delay)); | |
2a84cb98 | 245 | pr_debug(PREFIX "controller reset, restart transaction\n"); |
f351d027 | 246 | spin_lock_irqsave(&ec->lock, flags); |
2a84cb98 | 247 | start_transaction(ec); |
f351d027 | 248 | spin_unlock_irqrestore(&ec->lock, flags); |
af3fd140 | 249 | } |
b77d81b2 | 250 | return -ETIME; |
1da177e4 LT |
251 | } |
252 | ||
8463200a | 253 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, |
2a84cb98 | 254 | struct transaction *t) |
45bea155 | 255 | { |
7c6db4e0 | 256 | unsigned long tmp; |
7c6db4e0 | 257 | int ret = 0; |
5423a0cb | 258 | if (EC_FLAGS_MSI) |
2a84cb98 | 259 | udelay(ACPI_EC_MSI_UDELAY); |
7c6db4e0 | 260 | /* start transaction */ |
f351d027 | 261 | spin_lock_irqsave(&ec->lock, tmp); |
7c6db4e0 | 262 | /* following two actions should be kept atomic */ |
8463200a | 263 | ec->curr = t; |
a2f93aea | 264 | start_transaction(ec); |
8463200a | 265 | if (ec->curr->command == ACPI_EC_COMMAND_QUERY) |
080e412c | 266 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); |
f351d027 | 267 | spin_unlock_irqrestore(&ec->lock, tmp); |
2a84cb98 | 268 | ret = ec_poll(ec); |
f351d027 | 269 | spin_lock_irqsave(&ec->lock, tmp); |
8463200a | 270 | ec->curr = NULL; |
f351d027 | 271 | spin_unlock_irqrestore(&ec->lock, tmp); |
7c6db4e0 AS |
272 | return ret; |
273 | } | |
274 | ||
275 | static int ec_check_ibf0(struct acpi_ec *ec) | |
276 | { | |
277 | u8 status = acpi_ec_read_status(ec); | |
278 | return (status & ACPI_EC_FLAG_IBF) == 0; | |
45bea155 LY |
279 | } |
280 | ||
c0ff1772 AS |
281 | static int ec_wait_ibf0(struct acpi_ec *ec) |
282 | { | |
7a18e96d | 283 | unsigned long delay = jiffies + msecs_to_jiffies(ec_delay); |
c0ff1772 | 284 | /* interrupt wait manually if GPE mode is not active */ |
c0ff1772 | 285 | while (time_before(jiffies, delay)) |
2a84cb98 AS |
286 | if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), |
287 | msecs_to_jiffies(1))) | |
c0ff1772 AS |
288 | return 0; |
289 | return -ETIME; | |
45bea155 LY |
290 | } |
291 | ||
2a84cb98 | 292 | static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t) |
1da177e4 | 293 | { |
d7a76e4c | 294 | int status; |
50526df6 | 295 | u32 glk; |
8463200a | 296 | if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata)) |
d550d98d | 297 | return -EINVAL; |
8463200a AS |
298 | if (t->rdata) |
299 | memset(t->rdata, 0, t->rlen); | |
f351d027 | 300 | mutex_lock(&ec->mutex); |
fe955682 | 301 | if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) { |
f6bb13aa RW |
302 | status = -EINVAL; |
303 | goto unlock; | |
304 | } | |
703959d4 | 305 | if (ec->global_lock) { |
1da177e4 | 306 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b | 307 | if (ACPI_FAILURE(status)) { |
7c6db4e0 AS |
308 | status = -ENODEV; |
309 | goto unlock; | |
c24e912b | 310 | } |
1da177e4 | 311 | } |
c0ff1772 | 312 | if (ec_wait_ibf0(ec)) { |
3ebe08a7 MN |
313 | pr_err(PREFIX "input buffer is not empty, " |
314 | "aborting transaction\n"); | |
7c6db4e0 | 315 | status = -ETIME; |
451566f4 | 316 | goto end; |
716e084e | 317 | } |
b76b51ba FT |
318 | pr_debug(PREFIX "transaction start (cmd=0x%02x, addr=0x%02x)\n", |
319 | t->command, t->wdata ? t->wdata[0] : 0); | |
a62e8f19 AS |
320 | /* disable GPE during transaction if storm is detected */ |
321 | if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) { | |
3784730b RW |
322 | /* It has to be disabled, so that it doesn't trigger. */ |
323 | acpi_disable_gpe(NULL, ec->gpe); | |
a62e8f19 AS |
324 | } |
325 | ||
2a84cb98 | 326 | status = acpi_ec_transaction_unlocked(ec, t); |
a62e8f19 AS |
327 | |
328 | /* check if we received SCI during transaction */ | |
329 | ec_check_sci_sync(ec, acpi_ec_read_status(ec)); | |
330 | if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) { | |
54070101 | 331 | msleep(1); |
3784730b RW |
332 | /* It is safe to enable the GPE outside of the transaction. */ |
333 | acpi_enable_gpe(NULL, ec->gpe); | |
a520d52e | 334 | } else if (t->irq_count > ec_storm_threshold) { |
b76b51ba FT |
335 | pr_info(PREFIX "GPE storm detected(%d GPEs), " |
336 | "transactions will use polling mode\n", | |
337 | t->irq_count); | |
a62e8f19 AS |
338 | set_bit(EC_FLAGS_GPE_STORM, &ec->flags); |
339 | } | |
54070101 | 340 | pr_debug(PREFIX "transaction end\n"); |
7c6db4e0 | 341 | end: |
703959d4 | 342 | if (ec->global_lock) |
1da177e4 | 343 | acpi_release_global_lock(glk); |
7c6db4e0 | 344 | unlock: |
f351d027 | 345 | mutex_unlock(&ec->mutex); |
d550d98d | 346 | return status; |
1da177e4 LT |
347 | } |
348 | ||
8a383ef0 | 349 | static int acpi_ec_burst_enable(struct acpi_ec *ec) |
c45aac43 AS |
350 | { |
351 | u8 d; | |
8463200a AS |
352 | struct transaction t = {.command = ACPI_EC_BURST_ENABLE, |
353 | .wdata = NULL, .rdata = &d, | |
354 | .wlen = 0, .rlen = 1}; | |
355 | ||
2a84cb98 | 356 | return acpi_ec_transaction(ec, &t); |
c45aac43 AS |
357 | } |
358 | ||
8a383ef0 | 359 | static int acpi_ec_burst_disable(struct acpi_ec *ec) |
c45aac43 | 360 | { |
8463200a AS |
361 | struct transaction t = {.command = ACPI_EC_BURST_DISABLE, |
362 | .wdata = NULL, .rdata = NULL, | |
363 | .wlen = 0, .rlen = 0}; | |
364 | ||
7c6db4e0 | 365 | return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ? |
2a84cb98 | 366 | acpi_ec_transaction(ec, &t) : 0; |
c45aac43 AS |
367 | } |
368 | ||
6ccedb10 | 369 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
3576cf61 DS |
370 | { |
371 | int result; | |
372 | u8 d; | |
8463200a AS |
373 | struct transaction t = {.command = ACPI_EC_COMMAND_READ, |
374 | .wdata = &address, .rdata = &d, | |
375 | .wlen = 1, .rlen = 1}; | |
3576cf61 | 376 | |
2a84cb98 | 377 | result = acpi_ec_transaction(ec, &t); |
3576cf61 DS |
378 | *data = d; |
379 | return result; | |
380 | } | |
6ffb221a | 381 | |
3576cf61 DS |
382 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
383 | { | |
6ccedb10 | 384 | u8 wdata[2] = { address, data }; |
8463200a AS |
385 | struct transaction t = {.command = ACPI_EC_COMMAND_WRITE, |
386 | .wdata = wdata, .rdata = NULL, | |
387 | .wlen = 2, .rlen = 0}; | |
388 | ||
2a84cb98 | 389 | return acpi_ec_transaction(ec, &t); |
3576cf61 DS |
390 | } |
391 | ||
1da177e4 LT |
392 | /* |
393 | * Externally callable EC access functions. For now, assume 1 EC only | |
394 | */ | |
c45aac43 AS |
395 | int ec_burst_enable(void) |
396 | { | |
c45aac43 AS |
397 | if (!first_ec) |
398 | return -ENODEV; | |
d033879c | 399 | return acpi_ec_burst_enable(first_ec); |
c45aac43 AS |
400 | } |
401 | ||
402 | EXPORT_SYMBOL(ec_burst_enable); | |
403 | ||
404 | int ec_burst_disable(void) | |
405 | { | |
c45aac43 AS |
406 | if (!first_ec) |
407 | return -ENODEV; | |
d033879c | 408 | return acpi_ec_burst_disable(first_ec); |
c45aac43 AS |
409 | } |
410 | ||
411 | EXPORT_SYMBOL(ec_burst_disable); | |
412 | ||
b76b51ba | 413 | int ec_read(u8 addr, u8 *val) |
1da177e4 | 414 | { |
1da177e4 | 415 | int err; |
6ffb221a | 416 | u8 temp_data; |
1da177e4 LT |
417 | |
418 | if (!first_ec) | |
419 | return -ENODEV; | |
420 | ||
d033879c | 421 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
422 | |
423 | if (!err) { | |
424 | *val = temp_data; | |
425 | return 0; | |
50526df6 | 426 | } else |
1da177e4 LT |
427 | return err; |
428 | } | |
50526df6 | 429 | |
1da177e4 LT |
430 | EXPORT_SYMBOL(ec_read); |
431 | ||
50526df6 | 432 | int ec_write(u8 addr, u8 val) |
1da177e4 | 433 | { |
1da177e4 LT |
434 | int err; |
435 | ||
436 | if (!first_ec) | |
437 | return -ENODEV; | |
438 | ||
d033879c | 439 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
440 | |
441 | return err; | |
442 | } | |
50526df6 | 443 | |
1da177e4 LT |
444 | EXPORT_SYMBOL(ec_write); |
445 | ||
616362de | 446 | int ec_transaction(u8 command, |
9e197219 | 447 | const u8 * wdata, unsigned wdata_len, |
1cb7b1e0 | 448 | u8 * rdata, unsigned rdata_len) |
45bea155 | 449 | { |
8463200a AS |
450 | struct transaction t = {.command = command, |
451 | .wdata = wdata, .rdata = rdata, | |
452 | .wlen = wdata_len, .rlen = rdata_len}; | |
d7a76e4c LP |
453 | if (!first_ec) |
454 | return -ENODEV; | |
45bea155 | 455 | |
2a84cb98 | 456 | return acpi_ec_transaction(first_ec, &t); |
45bea155 | 457 | } |
1da177e4 | 458 | |
ab9e43c6 LP |
459 | EXPORT_SYMBOL(ec_transaction); |
460 | ||
3e2abc5a SF |
461 | /* Get the handle to the EC device */ |
462 | acpi_handle ec_get_handle(void) | |
463 | { | |
464 | if (!first_ec) | |
465 | return NULL; | |
466 | return first_ec->handle; | |
467 | } | |
468 | ||
469 | EXPORT_SYMBOL(ec_get_handle); | |
470 | ||
fe955682 | 471 | void acpi_ec_block_transactions(void) |
f6bb13aa RW |
472 | { |
473 | struct acpi_ec *ec = first_ec; | |
474 | ||
475 | if (!ec) | |
476 | return; | |
477 | ||
f351d027 | 478 | mutex_lock(&ec->mutex); |
f6bb13aa | 479 | /* Prevent transactions from being carried out */ |
fe955682 | 480 | set_bit(EC_FLAGS_BLOCKED, &ec->flags); |
f351d027 | 481 | mutex_unlock(&ec->mutex); |
f6bb13aa RW |
482 | } |
483 | ||
fe955682 | 484 | void acpi_ec_unblock_transactions(void) |
f6bb13aa RW |
485 | { |
486 | struct acpi_ec *ec = first_ec; | |
487 | ||
488 | if (!ec) | |
489 | return; | |
490 | ||
f351d027 | 491 | mutex_lock(&ec->mutex); |
f6bb13aa | 492 | /* Allow transactions to be carried out again */ |
fe955682 | 493 | clear_bit(EC_FLAGS_BLOCKED, &ec->flags); |
f351d027 | 494 | mutex_unlock(&ec->mutex); |
f6bb13aa RW |
495 | } |
496 | ||
fe955682 | 497 | void acpi_ec_unblock_transactions_early(void) |
d5a64513 RW |
498 | { |
499 | /* | |
500 | * Allow transactions to happen again (this function is called from | |
501 | * atomic context during wakeup, so we don't need to acquire the mutex). | |
502 | */ | |
503 | if (first_ec) | |
fe955682 | 504 | clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags); |
d5a64513 RW |
505 | } |
506 | ||
a62e8f19 | 507 | static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data) |
3576cf61 DS |
508 | { |
509 | int result; | |
6ccedb10 | 510 | u8 d; |
8463200a AS |
511 | struct transaction t = {.command = ACPI_EC_COMMAND_QUERY, |
512 | .wdata = NULL, .rdata = &d, | |
513 | .wlen = 0, .rlen = 1}; | |
6ccedb10 AS |
514 | if (!ec || !data) |
515 | return -EINVAL; | |
6ccedb10 AS |
516 | /* |
517 | * Query the EC to find out which _Qxx method we need to evaluate. | |
518 | * Note that successful completion of the query causes the ACPI_EC_SCI | |
519 | * bit to be cleared (and thus clearing the interrupt source). | |
520 | */ | |
a62e8f19 | 521 | result = acpi_ec_transaction_unlocked(ec, &t); |
6ccedb10 AS |
522 | if (result) |
523 | return result; | |
6ccedb10 AS |
524 | if (!d) |
525 | return -ENODATA; | |
6ccedb10 AS |
526 | *data = d; |
527 | return 0; | |
1da177e4 LT |
528 | } |
529 | ||
1da177e4 LT |
530 | /* -------------------------------------------------------------------------- |
531 | Event Management | |
532 | -------------------------------------------------------------------------- */ | |
837012ed AS |
533 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
534 | acpi_handle handle, acpi_ec_query_func func, | |
535 | void *data) | |
536 | { | |
537 | struct acpi_ec_query_handler *handler = | |
538 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); | |
539 | if (!handler) | |
540 | return -ENOMEM; | |
541 | ||
542 | handler->query_bit = query_bit; | |
543 | handler->handle = handle; | |
544 | handler->func = func; | |
545 | handler->data = data; | |
f351d027 | 546 | mutex_lock(&ec->mutex); |
30c08574 | 547 | list_add(&handler->node, &ec->list); |
f351d027 | 548 | mutex_unlock(&ec->mutex); |
837012ed AS |
549 | return 0; |
550 | } | |
551 | ||
552 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); | |
553 | ||
554 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) | |
555 | { | |
1544fdbc | 556 | struct acpi_ec_query_handler *handler, *tmp; |
f351d027 | 557 | mutex_lock(&ec->mutex); |
1544fdbc | 558 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
837012ed AS |
559 | if (query_bit == handler->query_bit) { |
560 | list_del(&handler->node); | |
561 | kfree(handler); | |
837012ed AS |
562 | } |
563 | } | |
f351d027 | 564 | mutex_unlock(&ec->mutex); |
837012ed AS |
565 | } |
566 | ||
567 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); | |
1da177e4 | 568 | |
a62e8f19 | 569 | static void acpi_ec_run(void *cxt) |
45bea155 | 570 | { |
a62e8f19 AS |
571 | struct acpi_ec_query_handler *handler = cxt; |
572 | if (!handler) | |
e41334c0 | 573 | return; |
a62e8f19 AS |
574 | pr_debug(PREFIX "start query execution\n"); |
575 | if (handler->func) | |
576 | handler->func(handler->data); | |
577 | else if (handler->handle) | |
578 | acpi_evaluate_object(handler->handle, NULL, NULL, NULL); | |
579 | pr_debug(PREFIX "stop query execution\n"); | |
580 | kfree(handler); | |
581 | } | |
582 | ||
583 | static int acpi_ec_sync_query(struct acpi_ec *ec) | |
584 | { | |
585 | u8 value = 0; | |
586 | int status; | |
587 | struct acpi_ec_query_handler *handler, *copy; | |
588 | if ((status = acpi_ec_query_unlocked(ec, &value))) | |
589 | return status; | |
837012ed AS |
590 | list_for_each_entry(handler, &ec->list, node) { |
591 | if (value == handler->query_bit) { | |
592 | /* have custom handler for this bit */ | |
a62e8f19 AS |
593 | copy = kmalloc(sizeof(*handler), GFP_KERNEL); |
594 | if (!copy) | |
595 | return -ENOMEM; | |
596 | memcpy(copy, handler, sizeof(*copy)); | |
597 | pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value); | |
f5347867 AS |
598 | return acpi_os_execute((copy->func) ? |
599 | OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER, | |
a62e8f19 | 600 | acpi_ec_run, copy); |
837012ed AS |
601 | } |
602 | } | |
a62e8f19 AS |
603 | return 0; |
604 | } | |
605 | ||
606 | static void acpi_ec_gpe_query(void *ec_cxt) | |
607 | { | |
608 | struct acpi_ec *ec = ec_cxt; | |
609 | if (!ec) | |
610 | return; | |
f351d027 | 611 | mutex_lock(&ec->mutex); |
a62e8f19 | 612 | acpi_ec_sync_query(ec); |
f351d027 | 613 | mutex_unlock(&ec->mutex); |
45bea155 | 614 | } |
1da177e4 | 615 | |
a62e8f19 AS |
616 | static int ec_check_sci(struct acpi_ec *ec, u8 state) |
617 | { | |
618 | if (state & ACPI_EC_FLAG_SCI) { | |
619 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) { | |
620 | pr_debug(PREFIX "push gpe query to the queue\n"); | |
621 | return acpi_os_execute(OSL_NOTIFY_HANDLER, | |
622 | acpi_ec_gpe_query, ec); | |
623 | } | |
624 | } | |
625 | return 0; | |
626 | } | |
627 | ||
8b6cd8ad LM |
628 | static u32 acpi_ec_gpe_handler(acpi_handle gpe_device, |
629 | u32 gpe_number, void *data) | |
1da177e4 | 630 | { |
3d02b90b | 631 | struct acpi_ec *ec = data; |
b76b51ba | 632 | u8 status = acpi_ec_read_status(ec); |
00eb43a1 | 633 | |
b76b51ba | 634 | pr_debug(PREFIX "~~~> interrupt, status:0x%02x\n", status); |
7c6db4e0 | 635 | |
b76b51ba | 636 | advance_transaction(ec, status); |
a62e8f19 AS |
637 | if (ec_transaction_done(ec) && |
638 | (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) { | |
2a84cb98 | 639 | wake_up(&ec->wait); |
a62e8f19 AS |
640 | ec_check_sci(ec, acpi_ec_read_status(ec)); |
641 | } | |
bba63a29 | 642 | return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE; |
845625cd AS |
643 | } |
644 | ||
1da177e4 LT |
645 | /* -------------------------------------------------------------------------- |
646 | Address Space Management | |
647 | -------------------------------------------------------------------------- */ | |
648 | ||
1da177e4 | 649 | static acpi_status |
5b7734b4 | 650 | acpi_ec_space_handler(u32 function, acpi_physical_address address, |
dadf28a1 | 651 | u32 bits, u64 *value64, |
50526df6 | 652 | void *handler_context, void *region_context) |
1da177e4 | 653 | { |
3d02b90b | 654 | struct acpi_ec *ec = handler_context; |
dadf28a1 AS |
655 | int result = 0, i, bytes = bits / 8; |
656 | u8 *value = (u8 *)value64; | |
1da177e4 | 657 | |
1da177e4 | 658 | if ((address > 0xFF) || !value || !handler_context) |
d550d98d | 659 | return AE_BAD_PARAMETER; |
1da177e4 | 660 | |
5b7734b4 | 661 | if (function != ACPI_READ && function != ACPI_WRITE) |
d550d98d | 662 | return AE_BAD_PARAMETER; |
1da177e4 | 663 | |
dadf28a1 | 664 | if (EC_FLAGS_MSI || bits > 8) |
6a63b06f | 665 | acpi_ec_burst_enable(ec); |
b3b233c7 | 666 | |
dadf28a1 AS |
667 | for (i = 0; i < bytes; ++i, ++address, ++value) |
668 | result = (function == ACPI_READ) ? | |
669 | acpi_ec_read(ec, address, value) : | |
670 | acpi_ec_write(ec, address, *value); | |
1da177e4 | 671 | |
dadf28a1 | 672 | if (EC_FLAGS_MSI || bits > 8) |
6a63b06f | 673 | acpi_ec_burst_disable(ec); |
b3b233c7 | 674 | |
1da177e4 LT |
675 | switch (result) { |
676 | case -EINVAL: | |
d550d98d | 677 | return AE_BAD_PARAMETER; |
1da177e4 LT |
678 | break; |
679 | case -ENODEV: | |
d550d98d | 680 | return AE_NOT_FOUND; |
1da177e4 LT |
681 | break; |
682 | case -ETIME: | |
d550d98d | 683 | return AE_TIME; |
1da177e4 LT |
684 | break; |
685 | default: | |
d550d98d | 686 | return AE_OK; |
1da177e4 | 687 | } |
1da177e4 LT |
688 | } |
689 | ||
1da177e4 LT |
690 | /* -------------------------------------------------------------------------- |
691 | Driver Interface | |
692 | -------------------------------------------------------------------------- */ | |
c0900c35 AS |
693 | static acpi_status |
694 | ec_parse_io_ports(struct acpi_resource *resource, void *context); | |
695 | ||
c0900c35 AS |
696 | static struct acpi_ec *make_acpi_ec(void) |
697 | { | |
698 | struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); | |
699 | if (!ec) | |
700 | return NULL; | |
080e412c | 701 | ec->flags = 1 << EC_FLAGS_QUERY_PENDING; |
f351d027 | 702 | mutex_init(&ec->mutex); |
c0900c35 | 703 | init_waitqueue_head(&ec->wait); |
837012ed | 704 | INIT_LIST_HEAD(&ec->list); |
f351d027 | 705 | spin_lock_init(&ec->lock); |
c0900c35 AS |
706 | return ec; |
707 | } | |
837012ed | 708 | |
c019b193 AS |
709 | static acpi_status |
710 | acpi_ec_register_query_methods(acpi_handle handle, u32 level, | |
711 | void *context, void **return_value) | |
712 | { | |
0175d562 LM |
713 | char node_name[5]; |
714 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; | |
c019b193 AS |
715 | struct acpi_ec *ec = context; |
716 | int value = 0; | |
0175d562 LM |
717 | acpi_status status; |
718 | ||
719 | status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); | |
720 | ||
721 | if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) { | |
c019b193 AS |
722 | acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); |
723 | } | |
724 | return AE_OK; | |
725 | } | |
726 | ||
cd8c93a4 AS |
727 | static acpi_status |
728 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
837012ed | 729 | { |
cd8c93a4 | 730 | acpi_status status; |
d21cf3c1 | 731 | unsigned long long tmp = 0; |
cd8c93a4 AS |
732 | |
733 | struct acpi_ec *ec = context; | |
a5032bfd AS |
734 | |
735 | /* clear addr values, ec_parse_io_ports depend on it */ | |
736 | ec->command_addr = ec->data_addr = 0; | |
737 | ||
cd8c93a4 AS |
738 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, |
739 | ec_parse_io_ports, ec); | |
740 | if (ACPI_FAILURE(status)) | |
741 | return status; | |
837012ed AS |
742 | |
743 | /* Get GPE bit assignment (EC events). */ | |
744 | /* TODO: Add support for _GPE returning a package */ | |
27663c58 | 745 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp); |
cd8c93a4 AS |
746 | if (ACPI_FAILURE(status)) |
747 | return status; | |
27663c58 | 748 | ec->gpe = tmp; |
837012ed | 749 | /* Use the global lock for all EC transactions? */ |
d21cf3c1 | 750 | tmp = 0; |
27663c58 MW |
751 | acpi_evaluate_integer(handle, "_GLK", NULL, &tmp); |
752 | ec->global_lock = tmp; | |
837012ed | 753 | ec->handle = handle; |
cd8c93a4 | 754 | return AE_CTRL_TERMINATE; |
837012ed AS |
755 | } |
756 | ||
5efc5476 BH |
757 | static int ec_install_handlers(struct acpi_ec *ec) |
758 | { | |
759 | acpi_status status; | |
760 | if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags)) | |
761 | return 0; | |
762 | status = acpi_install_gpe_handler(NULL, ec->gpe, | |
763 | ACPI_GPE_EDGE_TRIGGERED, | |
764 | &acpi_ec_gpe_handler, ec); | |
765 | if (ACPI_FAILURE(status)) | |
766 | return -ENODEV; | |
9630bdd9 | 767 | |
a44061aa | 768 | acpi_enable_gpe(NULL, ec->gpe); |
5efc5476 BH |
769 | status = acpi_install_address_space_handler(ec->handle, |
770 | ACPI_ADR_SPACE_EC, | |
771 | &acpi_ec_space_handler, | |
772 | NULL, ec); | |
773 | if (ACPI_FAILURE(status)) { | |
774 | if (status == AE_NOT_FOUND) { | |
775 | /* | |
776 | * Maybe OS fails in evaluating the _REG object. | |
777 | * The AE_NOT_FOUND error will be ignored and OS | |
778 | * continue to initialize EC. | |
779 | */ | |
780 | printk(KERN_ERR "Fail in evaluating the _REG object" | |
781 | " of EC device. Broken bios is suspected.\n"); | |
782 | } else { | |
783 | acpi_remove_gpe_handler(NULL, ec->gpe, | |
784 | &acpi_ec_gpe_handler); | |
a44061aa | 785 | acpi_disable_gpe(NULL, ec->gpe); |
5efc5476 BH |
786 | return -ENODEV; |
787 | } | |
788 | } | |
789 | ||
790 | set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags); | |
791 | return 0; | |
792 | } | |
793 | ||
4c611060 AS |
794 | static void ec_remove_handlers(struct acpi_ec *ec) |
795 | { | |
a44061aa | 796 | acpi_disable_gpe(NULL, ec->gpe); |
4c611060 AS |
797 | if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, |
798 | ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) | |
3ebe08a7 | 799 | pr_err(PREFIX "failed to remove space handler\n"); |
4c611060 AS |
800 | if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, |
801 | &acpi_ec_gpe_handler))) | |
3ebe08a7 | 802 | pr_err(PREFIX "failed to remove gpe handler\n"); |
7c6db4e0 | 803 | clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags); |
4c611060 AS |
804 | } |
805 | ||
703959d4 | 806 | static int acpi_ec_add(struct acpi_device *device) |
1da177e4 | 807 | { |
703959d4 | 808 | struct acpi_ec *ec = NULL; |
d02be047 | 809 | int ret; |
45bea155 | 810 | |
c0900c35 AS |
811 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
812 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
813 | ||
4c611060 | 814 | /* Check for boot EC */ |
ce52ddf5 AS |
815 | if (boot_ec && |
816 | (boot_ec->handle == device->handle || | |
817 | boot_ec->handle == ACPI_ROOT_OBJECT)) { | |
818 | ec = boot_ec; | |
819 | boot_ec = NULL; | |
820 | } else { | |
821 | ec = make_acpi_ec(); | |
822 | if (!ec) | |
823 | return -ENOMEM; | |
a5032bfd AS |
824 | } |
825 | if (ec_parse_device(device->handle, 0, ec, NULL) != | |
826 | AE_CTRL_TERMINATE) { | |
ce52ddf5 AS |
827 | kfree(ec); |
828 | return -EINVAL; | |
4c611060 AS |
829 | } |
830 | ||
ce52ddf5 AS |
831 | /* Find and register all query methods */ |
832 | acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1, | |
2263576c | 833 | acpi_ec_register_query_methods, NULL, ec, NULL); |
ce52ddf5 | 834 | |
4c611060 AS |
835 | if (!first_ec) |
836 | first_ec = ec; | |
db89b4f0 | 837 | device->driver_data = ec; |
de4f1046 | 838 | |
d6795fe3 AK |
839 | ret = !!request_region(ec->data_addr, 1, "EC data"); |
840 | WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr); | |
841 | ret = !!request_region(ec->command_addr, 1, "EC cmd"); | |
842 | WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr); | |
de4f1046 | 843 | |
3ebe08a7 | 844 | pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n", |
4c611060 | 845 | ec->gpe, ec->command_addr, ec->data_addr); |
5efc5476 BH |
846 | |
847 | ret = ec_install_handlers(ec); | |
848 | ||
849 | /* EC is fully operational, allow queries */ | |
850 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); | |
851 | return ret; | |
1da177e4 LT |
852 | } |
853 | ||
51fac838 | 854 | static int acpi_ec_remove(struct acpi_device *device) |
1da177e4 | 855 | { |
01f22462 | 856 | struct acpi_ec *ec; |
07ddf768 | 857 | struct acpi_ec_query_handler *handler, *tmp; |
1da177e4 | 858 | |
1da177e4 | 859 | if (!device) |
d550d98d | 860 | return -EINVAL; |
1da177e4 LT |
861 | |
862 | ec = acpi_driver_data(device); | |
cf745ec7 | 863 | ec_remove_handlers(ec); |
f351d027 | 864 | mutex_lock(&ec->mutex); |
07ddf768 | 865 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
837012ed AS |
866 | list_del(&handler->node); |
867 | kfree(handler); | |
868 | } | |
f351d027 | 869 | mutex_unlock(&ec->mutex); |
de4f1046 TR |
870 | release_region(ec->data_addr, 1); |
871 | release_region(ec->command_addr, 1); | |
db89b4f0 | 872 | device->driver_data = NULL; |
d033879c | 873 | if (ec == first_ec) |
c0900c35 | 874 | first_ec = NULL; |
4c611060 | 875 | kfree(ec); |
d550d98d | 876 | return 0; |
1da177e4 LT |
877 | } |
878 | ||
1da177e4 | 879 | static acpi_status |
c0900c35 | 880 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 881 | { |
3d02b90b | 882 | struct acpi_ec *ec = context; |
1da177e4 | 883 | |
01f22462 | 884 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 885 | return AE_OK; |
1da177e4 LT |
886 | |
887 | /* | |
888 | * The first address region returned is the data port, and | |
889 | * the second address region returned is the status/command | |
890 | * port. | |
891 | */ | |
de4f1046 | 892 | if (ec->data_addr == 0) |
6ffb221a | 893 | ec->data_addr = resource->data.io.minimum; |
de4f1046 | 894 | else if (ec->command_addr == 0) |
6ffb221a | 895 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 896 | else |
1da177e4 | 897 | return AE_CTRL_TERMINATE; |
1da177e4 | 898 | |
1da177e4 LT |
899 | return AE_OK; |
900 | } | |
901 | ||
c04209a7 AS |
902 | int __init acpi_boot_ec_enable(void) |
903 | { | |
7c6db4e0 | 904 | if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags)) |
c04209a7 AS |
905 | return 0; |
906 | if (!ec_install_handlers(boot_ec)) { | |
907 | first_ec = boot_ec; | |
908 | return 0; | |
909 | } | |
910 | return -EFAULT; | |
911 | } | |
912 | ||
223883b7 AS |
913 | static const struct acpi_device_id ec_device_ids[] = { |
914 | {"PNP0C09", 0}, | |
915 | {"", 0}, | |
916 | }; | |
917 | ||
478fa03b AS |
918 | /* Some BIOS do not survive early DSDT scan, skip it */ |
919 | static int ec_skip_dsdt_scan(const struct dmi_system_id *id) | |
920 | { | |
921 | EC_FLAGS_SKIP_DSDT_SCAN = 1; | |
922 | return 0; | |
923 | } | |
924 | ||
0adf3c74 AS |
925 | /* ASUStek often supplies us with broken ECDT, validate it */ |
926 | static int ec_validate_ecdt(const struct dmi_system_id *id) | |
927 | { | |
928 | EC_FLAGS_VALIDATE_ECDT = 1; | |
929 | return 0; | |
930 | } | |
931 | ||
932 | /* MSI EC needs special treatment, enable it */ | |
933 | static int ec_flag_msi(const struct dmi_system_id *id) | |
934 | { | |
55b313f2 | 935 | printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n"); |
0adf3c74 AS |
936 | EC_FLAGS_MSI = 1; |
937 | EC_FLAGS_VALIDATE_ECDT = 1; | |
938 | return 0; | |
939 | } | |
940 | ||
67bfa9b6 FT |
941 | /* |
942 | * Clevo M720 notebook actually works ok with IRQ mode, if we lifted | |
943 | * the GPE storm threshold back to 20 | |
944 | */ | |
945 | static int ec_enlarge_storm_threshold(const struct dmi_system_id *id) | |
946 | { | |
947 | pr_debug("Setting the EC GPE storm threshold to 20\n"); | |
948 | ec_storm_threshold = 20; | |
949 | return 0; | |
950 | } | |
951 | ||
0adf3c74 | 952 | static struct dmi_system_id __initdata ec_dmi_table[] = { |
478fa03b AS |
953 | { |
954 | ec_skip_dsdt_scan, "Compal JFL92", { | |
955 | DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"), | |
956 | DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL}, | |
0adf3c74 AS |
957 | { |
958 | ec_flag_msi, "MSI hardware", { | |
55b313f2 AS |
959 | DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL}, |
960 | { | |
961 | ec_flag_msi, "MSI hardware", { | |
962 | DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL}, | |
963 | { | |
964 | ec_flag_msi, "MSI hardware", { | |
965 | DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL}, | |
0adf3c74 | 966 | { |
a5dc4f89 AS |
967 | ec_flag_msi, "MSI hardware", { |
968 | DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL}, | |
969 | { | |
534bc4e3 ZR |
970 | ec_flag_msi, "Quanta hardware", { |
971 | DMI_MATCH(DMI_SYS_VENDOR, "Quanta"), | |
972 | DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL}, | |
973 | { | |
974 | ec_flag_msi, "Quanta hardware", { | |
975 | DMI_MATCH(DMI_SYS_VENDOR, "Quanta"), | |
976 | DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL}, | |
977 | { | |
0adf3c74 AS |
978 | ec_validate_ecdt, "ASUS hardware", { |
979 | DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL}, | |
af986d10 PC |
980 | { |
981 | ec_validate_ecdt, "ASUS hardware", { | |
982 | DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL}, | |
67bfa9b6 FT |
983 | { |
984 | ec_enlarge_storm_threshold, "CLEVO hardware", { | |
985 | DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."), | |
986 | DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL}, | |
93ede471 LT |
987 | { |
988 | ec_skip_dsdt_scan, "HP Folio 13", { | |
989 | DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), | |
990 | DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL}, | |
2880e37d LT |
991 | { |
992 | ec_validate_ecdt, "ASUS hardware", { | |
993 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."), | |
994 | DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL}, | |
0adf3c74 AS |
995 | {}, |
996 | }; | |
997 | ||
c0900c35 AS |
998 | int __init acpi_ec_ecdt_probe(void) |
999 | { | |
c0900c35 | 1000 | acpi_status status; |
c6cb0e87 | 1001 | struct acpi_ec *saved_ec = NULL; |
50526df6 | 1002 | struct acpi_table_ecdt *ecdt_ptr; |
45bea155 | 1003 | |
d66d969d AS |
1004 | boot_ec = make_acpi_ec(); |
1005 | if (!boot_ec) | |
c0900c35 AS |
1006 | return -ENOMEM; |
1007 | /* | |
1008 | * Generate a boot ec context | |
1009 | */ | |
0adf3c74 | 1010 | dmi_check_system(ec_dmi_table); |
15a58ed1 AS |
1011 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
1012 | (struct acpi_table_header **)&ecdt_ptr); | |
cd8c93a4 | 1013 | if (ACPI_SUCCESS(status)) { |
3ebe08a7 | 1014 | pr_info(PREFIX "EC description table is found, configuring boot EC\n"); |
cd8c93a4 AS |
1015 | boot_ec->command_addr = ecdt_ptr->control.address; |
1016 | boot_ec->data_addr = ecdt_ptr->data.address; | |
1017 | boot_ec->gpe = ecdt_ptr->gpe; | |
4af8e10a | 1018 | boot_ec->handle = ACPI_ROOT_OBJECT; |
ce52ddf5 | 1019 | acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle); |
c6cb0e87 | 1020 | /* Don't trust ECDT, which comes from ASUSTek */ |
0adf3c74 | 1021 | if (!EC_FLAGS_VALIDATE_ECDT) |
c5279dee | 1022 | goto install; |
d6bd535d | 1023 | saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL); |
c6cb0e87 AS |
1024 | if (!saved_ec) |
1025 | return -ENOMEM; | |
c5279dee | 1026 | /* fall through */ |
cd8c93a4 | 1027 | } |
0adf3c74 | 1028 | |
478fa03b AS |
1029 | if (EC_FLAGS_SKIP_DSDT_SCAN) |
1030 | return -ENODEV; | |
1031 | ||
c5279dee AS |
1032 | /* This workaround is needed only on some broken machines, |
1033 | * which require early EC, but fail to provide ECDT */ | |
c5279dee AS |
1034 | printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n"); |
1035 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, | |
1036 | boot_ec, NULL); | |
1037 | /* Check that acpi_get_devices actually find something */ | |
1038 | if (ACPI_FAILURE(status) || !boot_ec->handle) | |
1039 | goto error; | |
c6cb0e87 AS |
1040 | if (saved_ec) { |
1041 | /* try to find good ECDT from ASUSTek */ | |
1042 | if (saved_ec->command_addr != boot_ec->command_addr || | |
1043 | saved_ec->data_addr != boot_ec->data_addr || | |
1044 | saved_ec->gpe != boot_ec->gpe || | |
1045 | saved_ec->handle != boot_ec->handle) | |
1046 | pr_info(PREFIX "ASUSTek keeps feeding us with broken " | |
1047 | "ECDT tables, which are very hard to workaround. " | |
1048 | "Trying to use DSDT EC info instead. Please send " | |
1049 | "output of acpidump to linux-acpi@vger.kernel.org\n"); | |
1050 | kfree(saved_ec); | |
1051 | saved_ec = NULL; | |
1052 | } else { | |
1053 | /* We really need to limit this workaround, the only ASUS, | |
1054 | * which needs it, has fake EC._INI method, so use it as flag. | |
1055 | * Keep boot_ec struct as it will be needed soon. | |
1056 | */ | |
1057 | acpi_handle dummy; | |
1058 | if (!dmi_name_in_vendors("ASUS") || | |
1059 | ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", | |
1060 | &dummy))) | |
1061 | return -ENODEV; | |
1062 | } | |
c5279dee AS |
1063 | install: |
1064 | if (!ec_install_handlers(boot_ec)) { | |
d033879c | 1065 | first_ec = boot_ec; |
e8284321 | 1066 | return 0; |
d033879c | 1067 | } |
c5279dee | 1068 | error: |
d66d969d AS |
1069 | kfree(boot_ec); |
1070 | boot_ec = NULL; | |
1da177e4 LT |
1071 | return -ENODEV; |
1072 | } | |
1073 | ||
223883b7 AS |
1074 | static struct acpi_driver acpi_ec_driver = { |
1075 | .name = "ec", | |
1076 | .class = ACPI_EC_CLASS, | |
1077 | .ids = ec_device_ids, | |
1078 | .ops = { | |
1079 | .add = acpi_ec_add, | |
1080 | .remove = acpi_ec_remove, | |
223883b7 AS |
1081 | }, |
1082 | }; | |
1083 | ||
a5f820fe | 1084 | int __init acpi_ec_init(void) |
1da177e4 | 1085 | { |
50526df6 | 1086 | int result = 0; |
1da177e4 | 1087 | |
1da177e4 LT |
1088 | /* Now register the driver for the EC */ |
1089 | result = acpi_bus_register_driver(&acpi_ec_driver); | |
49c6c5ff | 1090 | if (result < 0) |
d550d98d | 1091 | return -ENODEV; |
1da177e4 | 1092 | |
d550d98d | 1093 | return result; |
1da177e4 LT |
1094 | } |
1095 | ||
1da177e4 LT |
1096 | /* EC driver currently not unloadable */ |
1097 | #if 0 | |
50526df6 | 1098 | static void __exit acpi_ec_exit(void) |
1da177e4 | 1099 | { |
1da177e4 LT |
1100 | |
1101 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
d550d98d | 1102 | return; |
1da177e4 | 1103 | } |
7843932a | 1104 | #endif /* 0 */ |