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