Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
01f22462 | 2 | * ec.c - ACPI Embedded Controller Driver (v2.0) |
1da177e4 | 3 | * |
01f22462 AS |
4 | * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
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 | ||
29 | #include <linux/kernel.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/types.h> | |
33 | #include <linux/delay.h> | |
34 | #include <linux/proc_fs.h> | |
35 | #include <linux/seq_file.h> | |
451566f4 | 36 | #include <linux/interrupt.h> |
837012ed | 37 | #include <linux/list.h> |
1da177e4 LT |
38 | #include <asm/io.h> |
39 | #include <acpi/acpi_bus.h> | |
40 | #include <acpi/acpi_drivers.h> | |
41 | #include <acpi/actypes.h> | |
42 | ||
1da177e4 | 43 | #define ACPI_EC_CLASS "embedded_controller" |
1da177e4 LT |
44 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
45 | #define ACPI_EC_FILE_INFO "info" | |
837012ed | 46 | |
af3fd140 AS |
47 | #undef PREFIX |
48 | #define PREFIX "ACPI: EC: " | |
4350933a | 49 | |
703959d4 | 50 | /* EC status register */ |
1da177e4 LT |
51 | #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ |
52 | #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ | |
451566f4 | 53 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
1da177e4 | 54 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
4350933a | 55 | |
703959d4 | 56 | /* EC commands */ |
3261ff4d | 57 | enum ec_command { |
6ccedb10 AS |
58 | ACPI_EC_COMMAND_READ = 0x80, |
59 | ACPI_EC_COMMAND_WRITE = 0x81, | |
60 | ACPI_EC_BURST_ENABLE = 0x82, | |
61 | ACPI_EC_BURST_DISABLE = 0x83, | |
62 | ACPI_EC_COMMAND_QUERY = 0x84, | |
3261ff4d | 63 | }; |
837012ed | 64 | |
703959d4 | 65 | /* EC events */ |
3261ff4d | 66 | enum ec_event { |
703959d4 | 67 | ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */ |
080e412c | 68 | ACPI_EC_EVENT_IBF_0, /* Input buffer empty */ |
703959d4 DS |
69 | }; |
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 */ |
703959d4 | 73 | |
3261ff4d | 74 | static enum ec_mode { |
6ccedb10 AS |
75 | EC_INTR = 1, /* Output buffer full */ |
76 | EC_POLL, /* Input buffer empty */ | |
3261ff4d | 77 | } acpi_ec_mode = EC_INTR; |
703959d4 | 78 | |
080e412c AS |
79 | enum { |
80 | EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */ | |
81 | EC_FLAGS_QUERY_PENDING, /* Query is pending */ | |
82 | }; | |
83 | ||
50526df6 LB |
84 | static int acpi_ec_remove(struct acpi_device *device, int type); |
85 | static int acpi_ec_start(struct acpi_device *device); | |
86 | static int acpi_ec_stop(struct acpi_device *device, int type); | |
703959d4 | 87 | static int acpi_ec_add(struct acpi_device *device); |
1da177e4 | 88 | |
1ba90e3a TR |
89 | static const struct acpi_device_id ec_device_ids[] = { |
90 | {"PNP0C09", 0}, | |
91 | {"", 0}, | |
92 | }; | |
93 | ||
1da177e4 | 94 | static struct acpi_driver acpi_ec_driver = { |
c2b6705b | 95 | .name = "ec", |
50526df6 | 96 | .class = ACPI_EC_CLASS, |
1ba90e3a | 97 | .ids = ec_device_ids, |
50526df6 | 98 | .ops = { |
703959d4 | 99 | .add = acpi_ec_add, |
50526df6 LB |
100 | .remove = acpi_ec_remove, |
101 | .start = acpi_ec_start, | |
102 | .stop = acpi_ec_stop, | |
103 | }, | |
1da177e4 | 104 | }; |
6ffb221a DS |
105 | |
106 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ | |
d033879c | 107 | /* External interfaces use first EC only, so remember */ |
837012ed AS |
108 | typedef int (*acpi_ec_query_func) (void *data); |
109 | ||
110 | struct acpi_ec_query_handler { | |
111 | struct list_head node; | |
112 | acpi_ec_query_func func; | |
113 | acpi_handle handle; | |
114 | void *data; | |
115 | u8 query_bit; | |
116 | }; | |
117 | ||
a854e08a | 118 | static struct acpi_ec { |
703959d4 | 119 | acpi_handle handle; |
a86e2772 | 120 | unsigned long gpe; |
6ffb221a DS |
121 | unsigned long command_addr; |
122 | unsigned long data_addr; | |
703959d4 | 123 | unsigned long global_lock; |
080e412c | 124 | unsigned long flags; |
c787a855 | 125 | struct mutex lock; |
703959d4 | 126 | wait_queue_head_t wait; |
837012ed | 127 | struct list_head list; |
4c611060 | 128 | u8 handlers_installed; |
d033879c | 129 | } *boot_ec, *first_ec; |
703959d4 | 130 | |
1da177e4 LT |
131 | /* -------------------------------------------------------------------------- |
132 | Transaction Management | |
133 | -------------------------------------------------------------------------- */ | |
134 | ||
6ffb221a | 135 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 136 | { |
6ffb221a | 137 | return inb(ec->command_addr); |
451566f4 DT |
138 | } |
139 | ||
6ffb221a | 140 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 141 | { |
6ffb221a | 142 | return inb(ec->data_addr); |
7c6db5e5 DS |
143 | } |
144 | ||
6ffb221a | 145 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 146 | { |
6ffb221a | 147 | outb(command, ec->command_addr); |
45bea155 LY |
148 | } |
149 | ||
6ffb221a | 150 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 151 | { |
6ffb221a | 152 | outb(data, ec->data_addr); |
703959d4 | 153 | } |
45bea155 | 154 | |
080e412c | 155 | static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event) |
6ffb221a | 156 | { |
080e412c | 157 | if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags)) |
9e197219 | 158 | return 0; |
78d0af33 | 159 | if (event == ACPI_EC_EVENT_OBF_1) { |
080e412c | 160 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF) |
703959d4 | 161 | return 1; |
78d0af33 | 162 | } else if (event == ACPI_EC_EVENT_IBF_0) { |
080e412c | 163 | if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)) |
703959d4 | 164 | return 1; |
45bea155 LY |
165 | } |
166 | ||
703959d4 | 167 | return 0; |
45bea155 | 168 | } |
451566f4 | 169 | |
080e412c | 170 | static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll) |
7c6db5e5 | 171 | { |
00eb43a1 | 172 | if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) { |
50c1e113 | 173 | unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); |
080e412c | 174 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
50c1e113 | 175 | while (time_before(jiffies, delay)) { |
080e412c | 176 | if (acpi_ec_check_status(ec, event)) |
7c6db5e5 DS |
177 | return 0; |
178 | } | |
af3fd140 | 179 | } else { |
080e412c AS |
180 | if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event), |
181 | msecs_to_jiffies(ACPI_EC_DELAY))) | |
182 | return 0; | |
183 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); | |
184 | if (acpi_ec_check_status(ec, event)) { | |
703959d4 DS |
185 | return 0; |
186 | } | |
af3fd140 | 187 | } |
080e412c AS |
188 | printk(KERN_ERR PREFIX "acpi_ec_wait timeout," |
189 | " status = %d, expect_event = %d\n", | |
190 | acpi_ec_read_status(ec), event); | |
d550d98d | 191 | return -ETIME; |
1da177e4 LT |
192 | } |
193 | ||
703959d4 | 194 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, |
6ccedb10 | 195 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
196 | u8 * rdata, unsigned rdata_len, |
197 | int force_poll) | |
45bea155 | 198 | { |
af3fd140 | 199 | int result = 0; |
080e412c | 200 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
703959d4 | 201 | acpi_ec_write_cmd(ec, command); |
45bea155 | 202 | |
78d0af33 | 203 | for (; wdata_len > 0; --wdata_len) { |
080e412c | 204 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
af3fd140 | 205 | if (result) { |
6ccedb10 AS |
206 | printk(KERN_ERR PREFIX |
207 | "write_cmd timeout, command = %d\n", command); | |
af3fd140 AS |
208 | goto end; |
209 | } | |
080e412c | 210 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
703959d4 | 211 | acpi_ec_write_data(ec, *(wdata++)); |
3576cf61 | 212 | } |
45bea155 | 213 | |
d91df1aa | 214 | if (!rdata_len) { |
080e412c | 215 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
af3fd140 | 216 | if (result) { |
6ccedb10 AS |
217 | printk(KERN_ERR PREFIX |
218 | "finish-write timeout, command = %d\n", command); | |
af3fd140 AS |
219 | goto end; |
220 | } | |
080e412c AS |
221 | } else if (command == ACPI_EC_COMMAND_QUERY) |
222 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); | |
45bea155 | 223 | |
78d0af33 | 224 | for (; rdata_len > 0; --rdata_len) { |
080e412c | 225 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll); |
af3fd140 AS |
226 | if (result) { |
227 | printk(KERN_ERR PREFIX "read timeout, command = %d\n", | |
6ccedb10 | 228 | command); |
af3fd140 AS |
229 | goto end; |
230 | } | |
0c5d31f4 AS |
231 | /* Don't expect GPE after last read */ |
232 | if (rdata_len > 1) | |
233 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); | |
6ffb221a | 234 | *(rdata++) = acpi_ec_read_data(ec); |
7c6db5e5 | 235 | } |
af3fd140 AS |
236 | end: |
237 | return result; | |
45bea155 LY |
238 | } |
239 | ||
3576cf61 | 240 | static int acpi_ec_transaction(struct acpi_ec *ec, u8 command, |
6ccedb10 | 241 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
242 | u8 * rdata, unsigned rdata_len, |
243 | int force_poll) | |
1da177e4 | 244 | { |
d7a76e4c | 245 | int status; |
50526df6 | 246 | u32 glk; |
1da177e4 | 247 | |
d7a76e4c | 248 | if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata)) |
d550d98d | 249 | return -EINVAL; |
1da177e4 | 250 | |
6ccedb10 AS |
251 | if (rdata) |
252 | memset(rdata, 0, rdata_len); | |
1da177e4 | 253 | |
523953b4 | 254 | mutex_lock(&ec->lock); |
703959d4 | 255 | if (ec->global_lock) { |
1da177e4 | 256 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b AS |
257 | if (ACPI_FAILURE(status)) { |
258 | mutex_unlock(&ec->lock); | |
d550d98d | 259 | return -ENODEV; |
c24e912b | 260 | } |
1da177e4 | 261 | } |
451566f4 | 262 | |
5d57a6a5 | 263 | /* Make sure GPE is enabled before doing transaction */ |
a86e2772 | 264 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); |
5d57a6a5 | 265 | |
080e412c | 266 | status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0); |
716e084e | 267 | if (status) { |
4350933a | 268 | printk(KERN_ERR PREFIX |
6ccedb10 | 269 | "input buffer is not empty, aborting transaction\n"); |
451566f4 | 270 | goto end; |
716e084e | 271 | } |
1da177e4 | 272 | |
6ccedb10 AS |
273 | status = acpi_ec_transaction_unlocked(ec, command, |
274 | wdata, wdata_len, | |
00eb43a1 LP |
275 | rdata, rdata_len, |
276 | force_poll); | |
1da177e4 | 277 | |
6ccedb10 | 278 | end: |
1da177e4 | 279 | |
703959d4 | 280 | if (ec->global_lock) |
1da177e4 | 281 | acpi_release_global_lock(glk); |
523953b4 | 282 | mutex_unlock(&ec->lock); |
1da177e4 | 283 | |
d550d98d | 284 | return status; |
1da177e4 LT |
285 | } |
286 | ||
c45aac43 AS |
287 | /* |
288 | * Note: samsung nv5000 doesn't work with ec burst mode. | |
289 | * http://bugzilla.kernel.org/show_bug.cgi?id=4980 | |
290 | */ | |
291 | int acpi_ec_burst_enable(struct acpi_ec *ec) | |
292 | { | |
293 | u8 d; | |
00eb43a1 | 294 | return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0); |
c45aac43 AS |
295 | } |
296 | ||
297 | int acpi_ec_burst_disable(struct acpi_ec *ec) | |
298 | { | |
00eb43a1 | 299 | return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0); |
c45aac43 AS |
300 | } |
301 | ||
6ccedb10 | 302 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
3576cf61 DS |
303 | { |
304 | int result; | |
305 | u8 d; | |
306 | ||
307 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, | |
00eb43a1 | 308 | &address, 1, &d, 1, 0); |
3576cf61 DS |
309 | *data = d; |
310 | return result; | |
311 | } | |
6ffb221a | 312 | |
3576cf61 DS |
313 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
314 | { | |
6ccedb10 AS |
315 | u8 wdata[2] = { address, data }; |
316 | return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, | |
00eb43a1 | 317 | wdata, 2, NULL, 0, 0); |
3576cf61 DS |
318 | } |
319 | ||
1da177e4 LT |
320 | /* |
321 | * Externally callable EC access functions. For now, assume 1 EC only | |
322 | */ | |
c45aac43 AS |
323 | int ec_burst_enable(void) |
324 | { | |
c45aac43 AS |
325 | if (!first_ec) |
326 | return -ENODEV; | |
d033879c | 327 | return acpi_ec_burst_enable(first_ec); |
c45aac43 AS |
328 | } |
329 | ||
330 | EXPORT_SYMBOL(ec_burst_enable); | |
331 | ||
332 | int ec_burst_disable(void) | |
333 | { | |
c45aac43 AS |
334 | if (!first_ec) |
335 | return -ENODEV; | |
d033879c | 336 | return acpi_ec_burst_disable(first_ec); |
c45aac43 AS |
337 | } |
338 | ||
339 | EXPORT_SYMBOL(ec_burst_disable); | |
340 | ||
6ccedb10 | 341 | int ec_read(u8 addr, u8 * val) |
1da177e4 | 342 | { |
1da177e4 | 343 | int err; |
6ffb221a | 344 | u8 temp_data; |
1da177e4 LT |
345 | |
346 | if (!first_ec) | |
347 | return -ENODEV; | |
348 | ||
d033879c | 349 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
350 | |
351 | if (!err) { | |
352 | *val = temp_data; | |
353 | return 0; | |
50526df6 | 354 | } else |
1da177e4 LT |
355 | return err; |
356 | } | |
50526df6 | 357 | |
1da177e4 LT |
358 | EXPORT_SYMBOL(ec_read); |
359 | ||
50526df6 | 360 | int ec_write(u8 addr, u8 val) |
1da177e4 | 361 | { |
1da177e4 LT |
362 | int err; |
363 | ||
364 | if (!first_ec) | |
365 | return -ENODEV; | |
366 | ||
d033879c | 367 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
368 | |
369 | return err; | |
370 | } | |
50526df6 | 371 | |
1da177e4 LT |
372 | EXPORT_SYMBOL(ec_write); |
373 | ||
616362de | 374 | int ec_transaction(u8 command, |
9e197219 | 375 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
376 | u8 * rdata, unsigned rdata_len, |
377 | int force_poll) | |
45bea155 | 378 | { |
d7a76e4c LP |
379 | if (!first_ec) |
380 | return -ENODEV; | |
45bea155 | 381 | |
d033879c | 382 | return acpi_ec_transaction(first_ec, command, wdata, |
00eb43a1 LP |
383 | wdata_len, rdata, rdata_len, |
384 | force_poll); | |
45bea155 | 385 | } |
1da177e4 | 386 | |
ab9e43c6 LP |
387 | EXPORT_SYMBOL(ec_transaction); |
388 | ||
6ccedb10 | 389 | static int acpi_ec_query(struct acpi_ec *ec, u8 * data) |
3576cf61 DS |
390 | { |
391 | int result; | |
6ccedb10 | 392 | u8 d; |
1da177e4 | 393 | |
6ccedb10 AS |
394 | if (!ec || !data) |
395 | return -EINVAL; | |
1da177e4 | 396 | |
6ccedb10 AS |
397 | /* |
398 | * Query the EC to find out which _Qxx method we need to evaluate. | |
399 | * Note that successful completion of the query causes the ACPI_EC_SCI | |
400 | * bit to be cleared (and thus clearing the interrupt source). | |
401 | */ | |
716e084e | 402 | |
00eb43a1 | 403 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0); |
6ccedb10 AS |
404 | if (result) |
405 | return result; | |
1da177e4 | 406 | |
6ccedb10 AS |
407 | if (!d) |
408 | return -ENODATA; | |
1da177e4 | 409 | |
6ccedb10 AS |
410 | *data = d; |
411 | return 0; | |
1da177e4 LT |
412 | } |
413 | ||
1da177e4 LT |
414 | /* -------------------------------------------------------------------------- |
415 | Event Management | |
416 | -------------------------------------------------------------------------- */ | |
837012ed AS |
417 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
418 | acpi_handle handle, acpi_ec_query_func func, | |
419 | void *data) | |
420 | { | |
421 | struct acpi_ec_query_handler *handler = | |
422 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); | |
423 | if (!handler) | |
424 | return -ENOMEM; | |
425 | ||
426 | handler->query_bit = query_bit; | |
427 | handler->handle = handle; | |
428 | handler->func = func; | |
429 | handler->data = data; | |
430 | mutex_lock(&ec->lock); | |
30c08574 | 431 | list_add(&handler->node, &ec->list); |
837012ed AS |
432 | mutex_unlock(&ec->lock); |
433 | return 0; | |
434 | } | |
435 | ||
436 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); | |
437 | ||
438 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) | |
439 | { | |
440 | struct acpi_ec_query_handler *handler; | |
441 | mutex_lock(&ec->lock); | |
442 | list_for_each_entry(handler, &ec->list, node) { | |
443 | if (query_bit == handler->query_bit) { | |
444 | list_del(&handler->node); | |
445 | kfree(handler); | |
837012ed AS |
446 | } |
447 | } | |
448 | mutex_unlock(&ec->lock); | |
449 | } | |
450 | ||
451 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); | |
1da177e4 | 452 | |
50526df6 | 453 | static void acpi_ec_gpe_query(void *ec_cxt) |
45bea155 | 454 | { |
3d02b90b | 455 | struct acpi_ec *ec = ec_cxt; |
6ffb221a | 456 | u8 value = 0; |
837012ed | 457 | struct acpi_ec_query_handler *handler, copy; |
45bea155 | 458 | |
5d0c288b | 459 | if (!ec || acpi_ec_query(ec, &value)) |
e41334c0 | 460 | return; |
837012ed AS |
461 | mutex_lock(&ec->lock); |
462 | list_for_each_entry(handler, &ec->list, node) { | |
463 | if (value == handler->query_bit) { | |
464 | /* have custom handler for this bit */ | |
465 | memcpy(©, handler, sizeof(copy)); | |
466 | mutex_unlock(&ec->lock); | |
467 | if (copy.func) { | |
468 | copy.func(copy.data); | |
469 | } else if (copy.handle) { | |
470 | acpi_evaluate_object(copy.handle, NULL, NULL, NULL); | |
471 | } | |
472 | return; | |
473 | } | |
474 | } | |
475 | mutex_unlock(&ec->lock); | |
45bea155 | 476 | } |
1da177e4 | 477 | |
50526df6 | 478 | static u32 acpi_ec_gpe_handler(void *data) |
1da177e4 | 479 | { |
50526df6 | 480 | acpi_status status = AE_OK; |
3d02b90b | 481 | struct acpi_ec *ec = data; |
00eb43a1 | 482 | |
080e412c | 483 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
3d02b90b | 484 | |
8e0341ba | 485 | if (acpi_ec_mode == EC_INTR) { |
af3fd140 | 486 | wake_up(&ec->wait); |
451566f4 DT |
487 | } |
488 | ||
080e412c AS |
489 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) { |
490 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) | |
491 | status = acpi_os_execute(OSL_EC_BURST_HANDLER, | |
492 | acpi_ec_gpe_query, ec); | |
50526df6 | 493 | } |
e41334c0 | 494 | |
451566f4 | 495 | return status == AE_OK ? |
50526df6 | 496 | ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED; |
1da177e4 LT |
497 | } |
498 | ||
499 | /* -------------------------------------------------------------------------- | |
500 | Address Space Management | |
501 | -------------------------------------------------------------------------- */ | |
502 | ||
503 | static acpi_status | |
50526df6 LB |
504 | acpi_ec_space_setup(acpi_handle region_handle, |
505 | u32 function, void *handler_context, void **return_context) | |
1da177e4 LT |
506 | { |
507 | /* | |
508 | * The EC object is in the handler context and is needed | |
509 | * when calling the acpi_ec_space_handler. | |
510 | */ | |
50526df6 LB |
511 | *return_context = (function != ACPI_REGION_DEACTIVATE) ? |
512 | handler_context : NULL; | |
1da177e4 LT |
513 | |
514 | return AE_OK; | |
515 | } | |
516 | ||
1da177e4 | 517 | static acpi_status |
5b7734b4 AS |
518 | acpi_ec_space_handler(u32 function, acpi_physical_address address, |
519 | u32 bits, acpi_integer *value, | |
50526df6 | 520 | void *handler_context, void *region_context) |
1da177e4 | 521 | { |
3d02b90b | 522 | struct acpi_ec *ec = handler_context; |
5b7734b4 AS |
523 | int result = 0, i = 0; |
524 | u8 temp = 0; | |
1da177e4 | 525 | |
1da177e4 | 526 | if ((address > 0xFF) || !value || !handler_context) |
d550d98d | 527 | return AE_BAD_PARAMETER; |
1da177e4 | 528 | |
5b7734b4 | 529 | if (function != ACPI_READ && function != ACPI_WRITE) |
d550d98d | 530 | return AE_BAD_PARAMETER; |
1da177e4 | 531 | |
5b7734b4 AS |
532 | if (bits != 8 && acpi_strict) |
533 | return AE_BAD_PARAMETER; | |
1da177e4 | 534 | |
5b7734b4 AS |
535 | while (bits - i > 0) { |
536 | if (function == ACPI_READ) { | |
537 | result = acpi_ec_read(ec, address, &temp); | |
538 | (*value) |= ((acpi_integer)temp) << i; | |
539 | } else { | |
540 | temp = 0xff & ((*value) >> i); | |
541 | result = acpi_ec_write(ec, address, temp); | |
542 | } | |
543 | i += 8; | |
544 | ++address; | |
1da177e4 LT |
545 | } |
546 | ||
1da177e4 LT |
547 | switch (result) { |
548 | case -EINVAL: | |
d550d98d | 549 | return AE_BAD_PARAMETER; |
1da177e4 LT |
550 | break; |
551 | case -ENODEV: | |
d550d98d | 552 | return AE_NOT_FOUND; |
1da177e4 LT |
553 | break; |
554 | case -ETIME: | |
d550d98d | 555 | return AE_TIME; |
1da177e4 LT |
556 | break; |
557 | default: | |
d550d98d | 558 | return AE_OK; |
1da177e4 | 559 | } |
1da177e4 LT |
560 | } |
561 | ||
1da177e4 LT |
562 | /* -------------------------------------------------------------------------- |
563 | FS Interface (/proc) | |
564 | -------------------------------------------------------------------------- */ | |
565 | ||
50526df6 | 566 | static struct proc_dir_entry *acpi_ec_dir; |
1da177e4 | 567 | |
50526df6 | 568 | static int acpi_ec_read_info(struct seq_file *seq, void *offset) |
1da177e4 | 569 | { |
3d02b90b | 570 | struct acpi_ec *ec = seq->private; |
1da177e4 | 571 | |
1da177e4 LT |
572 | if (!ec) |
573 | goto end; | |
574 | ||
01f22462 AS |
575 | seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe); |
576 | seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n", | |
577 | (unsigned)ec->command_addr, (unsigned)ec->data_addr); | |
578 | seq_printf(seq, "use global lock:\t%s\n", | |
703959d4 | 579 | ec->global_lock ? "yes" : "no"); |
50526df6 | 580 | end: |
d550d98d | 581 | return 0; |
1da177e4 LT |
582 | } |
583 | ||
584 | static int acpi_ec_info_open_fs(struct inode *inode, struct file *file) | |
585 | { | |
586 | return single_open(file, acpi_ec_read_info, PDE(inode)->data); | |
587 | } | |
588 | ||
703959d4 | 589 | static struct file_operations acpi_ec_info_ops = { |
50526df6 LB |
590 | .open = acpi_ec_info_open_fs, |
591 | .read = seq_read, | |
592 | .llseek = seq_lseek, | |
593 | .release = single_release, | |
1da177e4 LT |
594 | .owner = THIS_MODULE, |
595 | }; | |
596 | ||
50526df6 | 597 | static int acpi_ec_add_fs(struct acpi_device *device) |
1da177e4 | 598 | { |
50526df6 | 599 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 600 | |
1da177e4 LT |
601 | if (!acpi_device_dir(device)) { |
602 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
50526df6 | 603 | acpi_ec_dir); |
1da177e4 | 604 | if (!acpi_device_dir(device)) |
d550d98d | 605 | return -ENODEV; |
1da177e4 LT |
606 | } |
607 | ||
608 | entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO, | |
50526df6 | 609 | acpi_device_dir(device)); |
1da177e4 | 610 | if (!entry) |
d550d98d | 611 | return -ENODEV; |
1da177e4 LT |
612 | else { |
613 | entry->proc_fops = &acpi_ec_info_ops; | |
614 | entry->data = acpi_driver_data(device); | |
615 | entry->owner = THIS_MODULE; | |
616 | } | |
617 | ||
d550d98d | 618 | return 0; |
1da177e4 LT |
619 | } |
620 | ||
50526df6 | 621 | static int acpi_ec_remove_fs(struct acpi_device *device) |
1da177e4 | 622 | { |
1da177e4 LT |
623 | |
624 | if (acpi_device_dir(device)) { | |
625 | remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device)); | |
626 | remove_proc_entry(acpi_device_bid(device), acpi_ec_dir); | |
627 | acpi_device_dir(device) = NULL; | |
628 | } | |
629 | ||
d550d98d | 630 | return 0; |
1da177e4 LT |
631 | } |
632 | ||
1da177e4 LT |
633 | /* -------------------------------------------------------------------------- |
634 | Driver Interface | |
635 | -------------------------------------------------------------------------- */ | |
c0900c35 AS |
636 | static acpi_status |
637 | ec_parse_io_ports(struct acpi_resource *resource, void *context); | |
638 | ||
c0900c35 AS |
639 | static struct acpi_ec *make_acpi_ec(void) |
640 | { | |
641 | struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); | |
642 | if (!ec) | |
643 | return NULL; | |
644 | ||
080e412c | 645 | ec->flags = 1 << EC_FLAGS_QUERY_PENDING; |
c0900c35 AS |
646 | mutex_init(&ec->lock); |
647 | init_waitqueue_head(&ec->wait); | |
837012ed | 648 | INIT_LIST_HEAD(&ec->list); |
c0900c35 AS |
649 | |
650 | return ec; | |
651 | } | |
837012ed | 652 | |
c019b193 AS |
653 | static acpi_status |
654 | acpi_ec_register_query_methods(acpi_handle handle, u32 level, | |
655 | void *context, void **return_value) | |
656 | { | |
657 | struct acpi_namespace_node *node = handle; | |
658 | struct acpi_ec *ec = context; | |
659 | int value = 0; | |
660 | if (sscanf(node->name.ascii, "_Q%x", &value) == 1) { | |
661 | acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); | |
662 | } | |
663 | return AE_OK; | |
664 | } | |
665 | ||
cd8c93a4 AS |
666 | static acpi_status |
667 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
837012ed | 668 | { |
cd8c93a4 AS |
669 | acpi_status status; |
670 | ||
671 | struct acpi_ec *ec = context; | |
672 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, | |
673 | ec_parse_io_ports, ec); | |
674 | if (ACPI_FAILURE(status)) | |
675 | return status; | |
837012ed AS |
676 | |
677 | /* Get GPE bit assignment (EC events). */ | |
678 | /* TODO: Add support for _GPE returning a package */ | |
cd8c93a4 AS |
679 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe); |
680 | if (ACPI_FAILURE(status)) | |
681 | return status; | |
c019b193 AS |
682 | /* Find and register all query methods */ |
683 | acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1, | |
684 | acpi_ec_register_query_methods, ec, NULL); | |
837012ed AS |
685 | /* Use the global lock for all EC transactions? */ |
686 | acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); | |
837012ed | 687 | ec->handle = handle; |
cd8c93a4 | 688 | return AE_CTRL_TERMINATE; |
837012ed AS |
689 | } |
690 | ||
4c611060 AS |
691 | static void ec_remove_handlers(struct acpi_ec *ec) |
692 | { | |
693 | if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, | |
694 | ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) | |
695 | printk(KERN_ERR PREFIX "failed to remove space handler\n"); | |
696 | if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, | |
697 | &acpi_ec_gpe_handler))) | |
698 | printk(KERN_ERR PREFIX "failed to remove gpe handler\n"); | |
699 | ec->handlers_installed = 0; | |
700 | } | |
701 | ||
703959d4 | 702 | static int acpi_ec_add(struct acpi_device *device) |
1da177e4 | 703 | { |
703959d4 | 704 | struct acpi_ec *ec = NULL; |
45bea155 | 705 | |
45bea155 | 706 | if (!device) |
d550d98d | 707 | return -EINVAL; |
c0900c35 AS |
708 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
709 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
710 | ||
4c611060 AS |
711 | /* Check for boot EC */ |
712 | if (boot_ec) { | |
713 | if (boot_ec->handle == device->handle) { | |
714 | /* Pre-loaded EC from DSDT, just move pointer */ | |
715 | ec = boot_ec; | |
716 | boot_ec = NULL; | |
717 | goto end; | |
718 | } else if (boot_ec->handle == ACPI_ROOT_OBJECT) { | |
719 | /* ECDT-based EC, time to shut it down */ | |
720 | ec_remove_handlers(boot_ec); | |
721 | kfree(boot_ec); | |
722 | first_ec = boot_ec = NULL; | |
723 | } | |
724 | } | |
725 | ||
c0900c35 | 726 | ec = make_acpi_ec(); |
45bea155 | 727 | if (!ec) |
d550d98d | 728 | return -ENOMEM; |
703959d4 | 729 | |
cd8c93a4 AS |
730 | if (ec_parse_device(device->handle, 0, ec, NULL) != |
731 | AE_CTRL_TERMINATE) { | |
c0900c35 AS |
732 | kfree(ec); |
733 | return -EINVAL; | |
45bea155 | 734 | } |
c0900c35 | 735 | ec->handle = device->handle; |
4c611060 AS |
736 | end: |
737 | if (!first_ec) | |
738 | first_ec = ec; | |
c0900c35 | 739 | acpi_driver_data(device) = ec; |
c0900c35 | 740 | acpi_ec_add_fs(device); |
4c611060 AS |
741 | printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n", |
742 | ec->gpe, ec->command_addr, ec->data_addr); | |
c0900c35 | 743 | return 0; |
1da177e4 LT |
744 | } |
745 | ||
50526df6 | 746 | static int acpi_ec_remove(struct acpi_device *device, int type) |
1da177e4 | 747 | { |
01f22462 | 748 | struct acpi_ec *ec; |
07ddf768 | 749 | struct acpi_ec_query_handler *handler, *tmp; |
1da177e4 | 750 | |
1da177e4 | 751 | if (!device) |
d550d98d | 752 | return -EINVAL; |
1da177e4 LT |
753 | |
754 | ec = acpi_driver_data(device); | |
837012ed | 755 | mutex_lock(&ec->lock); |
07ddf768 | 756 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
837012ed AS |
757 | list_del(&handler->node); |
758 | kfree(handler); | |
759 | } | |
760 | mutex_unlock(&ec->lock); | |
1da177e4 | 761 | acpi_ec_remove_fs(device); |
c0900c35 | 762 | acpi_driver_data(device) = NULL; |
d033879c | 763 | if (ec == first_ec) |
c0900c35 | 764 | first_ec = NULL; |
4c611060 | 765 | kfree(ec); |
d550d98d | 766 | return 0; |
1da177e4 LT |
767 | } |
768 | ||
1da177e4 | 769 | static acpi_status |
c0900c35 | 770 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 771 | { |
3d02b90b | 772 | struct acpi_ec *ec = context; |
1da177e4 | 773 | |
01f22462 | 774 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 775 | return AE_OK; |
1da177e4 LT |
776 | |
777 | /* | |
778 | * The first address region returned is the data port, and | |
779 | * the second address region returned is the status/command | |
780 | * port. | |
781 | */ | |
01f22462 | 782 | if (ec->data_addr == 0) |
6ffb221a | 783 | ec->data_addr = resource->data.io.minimum; |
01f22462 | 784 | else if (ec->command_addr == 0) |
6ffb221a | 785 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 786 | else |
1da177e4 | 787 | return AE_CTRL_TERMINATE; |
1da177e4 | 788 | |
1da177e4 LT |
789 | return AE_OK; |
790 | } | |
791 | ||
e8284321 AS |
792 | static int ec_install_handlers(struct acpi_ec *ec) |
793 | { | |
c0900c35 | 794 | acpi_status status; |
4c611060 AS |
795 | if (ec->handlers_installed) |
796 | return 0; | |
c0900c35 AS |
797 | status = acpi_install_gpe_handler(NULL, ec->gpe, |
798 | ACPI_GPE_EDGE_TRIGGERED, | |
799 | &acpi_ec_gpe_handler, ec); | |
e8284321 AS |
800 | if (ACPI_FAILURE(status)) |
801 | return -ENODEV; | |
01f22462 | 802 | |
e8284321 AS |
803 | acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME); |
804 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); | |
805 | ||
806 | status = acpi_install_address_space_handler(ec->handle, | |
807 | ACPI_ADR_SPACE_EC, | |
808 | &acpi_ec_space_handler, | |
809 | &acpi_ec_space_setup, ec); | |
810 | if (ACPI_FAILURE(status)) { | |
811 | acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); | |
812 | return -ENODEV; | |
813 | } | |
814 | ||
4c611060 | 815 | ec->handlers_installed = 1; |
e8284321 AS |
816 | return 0; |
817 | } | |
818 | ||
50526df6 | 819 | static int acpi_ec_start(struct acpi_device *device) |
1da177e4 | 820 | { |
c0900c35 | 821 | struct acpi_ec *ec; |
837012ed | 822 | int ret = 0; |
1da177e4 | 823 | |
1da177e4 | 824 | if (!device) |
d550d98d | 825 | return -EINVAL; |
1da177e4 LT |
826 | |
827 | ec = acpi_driver_data(device); | |
828 | ||
829 | if (!ec) | |
d550d98d | 830 | return -EINVAL; |
1da177e4 | 831 | |
4c611060 | 832 | ret = ec_install_handlers(ec); |
837012ed AS |
833 | |
834 | /* EC is fully operational, allow queries */ | |
080e412c | 835 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); |
837012ed | 836 | return ret; |
1da177e4 LT |
837 | } |
838 | ||
50526df6 | 839 | static int acpi_ec_stop(struct acpi_device *device, int type) |
1da177e4 | 840 | { |
c0900c35 | 841 | struct acpi_ec *ec; |
1da177e4 | 842 | if (!device) |
d550d98d | 843 | return -EINVAL; |
1da177e4 | 844 | ec = acpi_driver_data(device); |
c0900c35 AS |
845 | if (!ec) |
846 | return -EINVAL; | |
4c611060 | 847 | ec_remove_handlers(ec); |
f9319f90 | 848 | |
d550d98d | 849 | return 0; |
1da177e4 LT |
850 | } |
851 | ||
c0900c35 AS |
852 | int __init acpi_ec_ecdt_probe(void) |
853 | { | |
854 | int ret; | |
855 | acpi_status status; | |
50526df6 | 856 | struct acpi_table_ecdt *ecdt_ptr; |
45bea155 | 857 | |
d66d969d AS |
858 | boot_ec = make_acpi_ec(); |
859 | if (!boot_ec) | |
c0900c35 AS |
860 | return -ENOMEM; |
861 | /* | |
862 | * Generate a boot ec context | |
863 | */ | |
15a58ed1 AS |
864 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
865 | (struct acpi_table_header **)&ecdt_ptr); | |
cd8c93a4 | 866 | if (ACPI_SUCCESS(status)) { |
4c611060 | 867 | printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n"); |
cd8c93a4 AS |
868 | boot_ec->command_addr = ecdt_ptr->control.address; |
869 | boot_ec->data_addr = ecdt_ptr->data.address; | |
870 | boot_ec->gpe = ecdt_ptr->gpe; | |
871 | boot_ec->handle = ACPI_ROOT_OBJECT; | |
872 | } else { | |
873 | printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n"); | |
874 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, | |
875 | boot_ec, NULL); | |
2d8348b4 AS |
876 | /* Check that acpi_get_devices actually find something */ |
877 | if (ACPI_FAILURE(status) || !boot_ec->handle) | |
cd8c93a4 AS |
878 | goto error; |
879 | } | |
1da177e4 | 880 | |
d66d969d | 881 | ret = ec_install_handlers(boot_ec); |
d033879c AS |
882 | if (!ret) { |
883 | first_ec = boot_ec; | |
e8284321 | 884 | return 0; |
d033879c | 885 | } |
c0900c35 | 886 | error: |
d66d969d AS |
887 | kfree(boot_ec); |
888 | boot_ec = NULL; | |
1da177e4 LT |
889 | return -ENODEV; |
890 | } | |
891 | ||
50526df6 | 892 | static int __init acpi_ec_init(void) |
1da177e4 | 893 | { |
50526df6 | 894 | int result = 0; |
1da177e4 | 895 | |
1da177e4 | 896 | if (acpi_disabled) |
d550d98d | 897 | return 0; |
1da177e4 LT |
898 | |
899 | acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir); | |
900 | if (!acpi_ec_dir) | |
d550d98d | 901 | return -ENODEV; |
1da177e4 LT |
902 | |
903 | /* Now register the driver for the EC */ | |
904 | result = acpi_bus_register_driver(&acpi_ec_driver); | |
905 | if (result < 0) { | |
906 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
d550d98d | 907 | return -ENODEV; |
1da177e4 LT |
908 | } |
909 | ||
d550d98d | 910 | return result; |
1da177e4 LT |
911 | } |
912 | ||
913 | subsys_initcall(acpi_ec_init); | |
914 | ||
915 | /* EC driver currently not unloadable */ | |
916 | #if 0 | |
50526df6 | 917 | static void __exit acpi_ec_exit(void) |
1da177e4 | 918 | { |
1da177e4 LT |
919 | |
920 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
921 | ||
922 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
923 | ||
d550d98d | 924 | return; |
1da177e4 | 925 | } |
50526df6 | 926 | #endif /* 0 */ |
1da177e4 | 927 | |
02b28a33 | 928 | static int __init acpi_ec_set_intr_mode(char *str) |
45bea155 | 929 | { |
02b28a33 | 930 | int intr; |
7b15f5e7 | 931 | |
02b28a33 | 932 | if (!get_option(&str, &intr)) |
7b15f5e7 LY |
933 | return 0; |
934 | ||
c0900c35 AS |
935 | acpi_ec_mode = (intr) ? EC_INTR : EC_POLL; |
936 | ||
9e197219 | 937 | printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling"); |
703959d4 | 938 | |
9b41046c | 939 | return 1; |
45bea155 | 940 | } |
50526df6 | 941 | |
53f11d4f | 942 | __setup("ec_intr=", acpi_ec_set_intr_mode); |