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 */ |
6ccedb10 | 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 | |
50526df6 LB |
79 | static int acpi_ec_remove(struct acpi_device *device, int type); |
80 | static int acpi_ec_start(struct acpi_device *device); | |
81 | static int acpi_ec_stop(struct acpi_device *device, int type); | |
703959d4 | 82 | static int acpi_ec_add(struct acpi_device *device); |
1da177e4 | 83 | |
1ba90e3a TR |
84 | static const struct acpi_device_id ec_device_ids[] = { |
85 | {"PNP0C09", 0}, | |
86 | {"", 0}, | |
87 | }; | |
88 | ||
1da177e4 | 89 | static struct acpi_driver acpi_ec_driver = { |
c2b6705b | 90 | .name = "ec", |
50526df6 | 91 | .class = ACPI_EC_CLASS, |
1ba90e3a | 92 | .ids = ec_device_ids, |
50526df6 | 93 | .ops = { |
703959d4 | 94 | .add = acpi_ec_add, |
50526df6 LB |
95 | .remove = acpi_ec_remove, |
96 | .start = acpi_ec_start, | |
97 | .stop = acpi_ec_stop, | |
98 | }, | |
1da177e4 | 99 | }; |
6ffb221a DS |
100 | |
101 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ | |
d033879c | 102 | /* External interfaces use first EC only, so remember */ |
837012ed AS |
103 | typedef int (*acpi_ec_query_func) (void *data); |
104 | ||
105 | struct acpi_ec_query_handler { | |
106 | struct list_head node; | |
107 | acpi_ec_query_func func; | |
108 | acpi_handle handle; | |
109 | void *data; | |
110 | u8 query_bit; | |
111 | }; | |
112 | ||
a854e08a | 113 | static struct acpi_ec { |
703959d4 | 114 | acpi_handle handle; |
a86e2772 | 115 | unsigned long gpe; |
6ffb221a DS |
116 | unsigned long command_addr; |
117 | unsigned long data_addr; | |
703959d4 | 118 | unsigned long global_lock; |
c787a855 | 119 | struct mutex lock; |
5d0c288b | 120 | atomic_t query_pending; |
9e197219 | 121 | atomic_t event_count; |
703959d4 | 122 | wait_queue_head_t wait; |
837012ed | 123 | struct list_head list; |
d033879c | 124 | } *boot_ec, *first_ec; |
703959d4 | 125 | |
1da177e4 LT |
126 | /* -------------------------------------------------------------------------- |
127 | Transaction Management | |
128 | -------------------------------------------------------------------------- */ | |
129 | ||
6ffb221a | 130 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 131 | { |
6ffb221a | 132 | return inb(ec->command_addr); |
451566f4 DT |
133 | } |
134 | ||
6ffb221a | 135 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 136 | { |
6ffb221a | 137 | return inb(ec->data_addr); |
7c6db5e5 DS |
138 | } |
139 | ||
6ffb221a | 140 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 141 | { |
6ffb221a | 142 | outb(command, ec->command_addr); |
45bea155 LY |
143 | } |
144 | ||
6ffb221a | 145 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 146 | { |
6ffb221a | 147 | outb(data, ec->data_addr); |
703959d4 | 148 | } |
45bea155 | 149 | |
9e197219 AS |
150 | static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event, |
151 | unsigned old_count) | |
6ffb221a | 152 | { |
bec5a1e0 | 153 | u8 status = acpi_ec_read_status(ec); |
9e197219 AS |
154 | if (old_count == atomic_read(&ec->event_count)) |
155 | return 0; | |
78d0af33 | 156 | if (event == ACPI_EC_EVENT_OBF_1) { |
703959d4 DS |
157 | if (status & ACPI_EC_FLAG_OBF) |
158 | return 1; | |
78d0af33 | 159 | } else if (event == ACPI_EC_EVENT_IBF_0) { |
703959d4 DS |
160 | if (!(status & ACPI_EC_FLAG_IBF)) |
161 | return 1; | |
45bea155 LY |
162 | } |
163 | ||
703959d4 | 164 | return 0; |
45bea155 | 165 | } |
451566f4 | 166 | |
00eb43a1 LP |
167 | static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, |
168 | unsigned count, int force_poll) | |
7c6db5e5 | 169 | { |
00eb43a1 | 170 | if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) { |
50c1e113 AS |
171 | unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); |
172 | while (time_before(jiffies, delay)) { | |
9e197219 | 173 | if (acpi_ec_check_status(ec, event, 0)) |
7c6db5e5 DS |
174 | return 0; |
175 | } | |
af3fd140 AS |
176 | } else { |
177 | if (wait_event_timeout(ec->wait, | |
9e197219 | 178 | acpi_ec_check_status(ec, event, count), |
af3fd140 | 179 | msecs_to_jiffies(ACPI_EC_DELAY)) || |
9e197219 | 180 | acpi_ec_check_status(ec, event, 0)) { |
703959d4 | 181 | return 0; |
af3fd140 AS |
182 | } else { |
183 | printk(KERN_ERR PREFIX "acpi_ec_wait timeout," | |
184 | " status = %d, expect_event = %d\n", | |
6ccedb10 | 185 | acpi_ec_read_status(ec), event); |
703959d4 | 186 | } |
af3fd140 | 187 | } |
1da177e4 | 188 | |
d550d98d | 189 | return -ETIME; |
1da177e4 LT |
190 | } |
191 | ||
703959d4 | 192 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, |
6ccedb10 | 193 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
194 | u8 * rdata, unsigned rdata_len, |
195 | int force_poll) | |
45bea155 | 196 | { |
af3fd140 | 197 | int result = 0; |
9e197219 | 198 | unsigned count = atomic_read(&ec->event_count); |
703959d4 | 199 | acpi_ec_write_cmd(ec, command); |
45bea155 | 200 | |
78d0af33 | 201 | for (; wdata_len > 0; --wdata_len) { |
00eb43a1 | 202 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll); |
af3fd140 | 203 | if (result) { |
6ccedb10 AS |
204 | printk(KERN_ERR PREFIX |
205 | "write_cmd timeout, command = %d\n", command); | |
af3fd140 AS |
206 | goto end; |
207 | } | |
9e197219 | 208 | count = atomic_read(&ec->event_count); |
703959d4 | 209 | acpi_ec_write_data(ec, *(wdata++)); |
3576cf61 | 210 | } |
45bea155 | 211 | |
d91df1aa | 212 | if (!rdata_len) { |
00eb43a1 | 213 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll); |
af3fd140 | 214 | if (result) { |
6ccedb10 AS |
215 | printk(KERN_ERR PREFIX |
216 | "finish-write timeout, command = %d\n", command); | |
af3fd140 AS |
217 | goto end; |
218 | } | |
5d0c288b AS |
219 | } else if (command == ACPI_EC_COMMAND_QUERY) { |
220 | atomic_set(&ec->query_pending, 0); | |
7c6db5e5 | 221 | } |
45bea155 | 222 | |
78d0af33 | 223 | for (; rdata_len > 0; --rdata_len) { |
00eb43a1 | 224 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll); |
af3fd140 AS |
225 | if (result) { |
226 | printk(KERN_ERR PREFIX "read timeout, command = %d\n", | |
6ccedb10 | 227 | command); |
af3fd140 AS |
228 | goto end; |
229 | } | |
9e197219 | 230 | count = atomic_read(&ec->event_count); |
6ffb221a | 231 | *(rdata++) = acpi_ec_read_data(ec); |
7c6db5e5 | 232 | } |
af3fd140 AS |
233 | end: |
234 | return result; | |
45bea155 LY |
235 | } |
236 | ||
3576cf61 | 237 | static int acpi_ec_transaction(struct acpi_ec *ec, u8 command, |
6ccedb10 | 238 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
239 | u8 * rdata, unsigned rdata_len, |
240 | int force_poll) | |
1da177e4 | 241 | { |
d7a76e4c | 242 | int status; |
50526df6 | 243 | u32 glk; |
1da177e4 | 244 | |
d7a76e4c | 245 | if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata)) |
d550d98d | 246 | return -EINVAL; |
1da177e4 | 247 | |
6ccedb10 AS |
248 | if (rdata) |
249 | memset(rdata, 0, rdata_len); | |
1da177e4 | 250 | |
523953b4 | 251 | mutex_lock(&ec->lock); |
703959d4 | 252 | if (ec->global_lock) { |
1da177e4 | 253 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b AS |
254 | if (ACPI_FAILURE(status)) { |
255 | mutex_unlock(&ec->lock); | |
d550d98d | 256 | return -ENODEV; |
c24e912b | 257 | } |
1da177e4 | 258 | } |
451566f4 | 259 | |
5d57a6a5 | 260 | /* Make sure GPE is enabled before doing transaction */ |
a86e2772 | 261 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); |
5d57a6a5 | 262 | |
00eb43a1 | 263 | status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0); |
716e084e | 264 | if (status) { |
4350933a | 265 | printk(KERN_ERR PREFIX |
6ccedb10 | 266 | "input buffer is not empty, aborting transaction\n"); |
451566f4 | 267 | goto end; |
716e084e | 268 | } |
1da177e4 | 269 | |
6ccedb10 AS |
270 | status = acpi_ec_transaction_unlocked(ec, command, |
271 | wdata, wdata_len, | |
00eb43a1 LP |
272 | rdata, rdata_len, |
273 | force_poll); | |
1da177e4 | 274 | |
6ccedb10 | 275 | end: |
1da177e4 | 276 | |
703959d4 | 277 | if (ec->global_lock) |
1da177e4 | 278 | acpi_release_global_lock(glk); |
523953b4 | 279 | mutex_unlock(&ec->lock); |
1da177e4 | 280 | |
d550d98d | 281 | return status; |
1da177e4 LT |
282 | } |
283 | ||
c45aac43 AS |
284 | /* |
285 | * Note: samsung nv5000 doesn't work with ec burst mode. | |
286 | * http://bugzilla.kernel.org/show_bug.cgi?id=4980 | |
287 | */ | |
288 | int acpi_ec_burst_enable(struct acpi_ec *ec) | |
289 | { | |
290 | u8 d; | |
00eb43a1 | 291 | return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0); |
c45aac43 AS |
292 | } |
293 | ||
294 | int acpi_ec_burst_disable(struct acpi_ec *ec) | |
295 | { | |
00eb43a1 | 296 | return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0); |
c45aac43 AS |
297 | } |
298 | ||
6ccedb10 | 299 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
3576cf61 DS |
300 | { |
301 | int result; | |
302 | u8 d; | |
303 | ||
304 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, | |
00eb43a1 | 305 | &address, 1, &d, 1, 0); |
3576cf61 DS |
306 | *data = d; |
307 | return result; | |
308 | } | |
6ffb221a | 309 | |
3576cf61 DS |
310 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
311 | { | |
6ccedb10 AS |
312 | u8 wdata[2] = { address, data }; |
313 | return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, | |
00eb43a1 | 314 | wdata, 2, NULL, 0, 0); |
3576cf61 DS |
315 | } |
316 | ||
1da177e4 LT |
317 | /* |
318 | * Externally callable EC access functions. For now, assume 1 EC only | |
319 | */ | |
c45aac43 AS |
320 | int ec_burst_enable(void) |
321 | { | |
c45aac43 AS |
322 | if (!first_ec) |
323 | return -ENODEV; | |
d033879c | 324 | return acpi_ec_burst_enable(first_ec); |
c45aac43 AS |
325 | } |
326 | ||
327 | EXPORT_SYMBOL(ec_burst_enable); | |
328 | ||
329 | int ec_burst_disable(void) | |
330 | { | |
c45aac43 AS |
331 | if (!first_ec) |
332 | return -ENODEV; | |
d033879c | 333 | return acpi_ec_burst_disable(first_ec); |
c45aac43 AS |
334 | } |
335 | ||
336 | EXPORT_SYMBOL(ec_burst_disable); | |
337 | ||
6ccedb10 | 338 | int ec_read(u8 addr, u8 * val) |
1da177e4 | 339 | { |
1da177e4 | 340 | int err; |
6ffb221a | 341 | u8 temp_data; |
1da177e4 LT |
342 | |
343 | if (!first_ec) | |
344 | return -ENODEV; | |
345 | ||
d033879c | 346 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
347 | |
348 | if (!err) { | |
349 | *val = temp_data; | |
350 | return 0; | |
50526df6 | 351 | } else |
1da177e4 LT |
352 | return err; |
353 | } | |
50526df6 | 354 | |
1da177e4 LT |
355 | EXPORT_SYMBOL(ec_read); |
356 | ||
50526df6 | 357 | int ec_write(u8 addr, u8 val) |
1da177e4 | 358 | { |
1da177e4 LT |
359 | int err; |
360 | ||
361 | if (!first_ec) | |
362 | return -ENODEV; | |
363 | ||
d033879c | 364 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
365 | |
366 | return err; | |
367 | } | |
50526df6 | 368 | |
1da177e4 LT |
369 | EXPORT_SYMBOL(ec_write); |
370 | ||
616362de | 371 | int ec_transaction(u8 command, |
9e197219 | 372 | const u8 * wdata, unsigned wdata_len, |
00eb43a1 LP |
373 | u8 * rdata, unsigned rdata_len, |
374 | int force_poll) | |
45bea155 | 375 | { |
d7a76e4c LP |
376 | if (!first_ec) |
377 | return -ENODEV; | |
45bea155 | 378 | |
d033879c | 379 | return acpi_ec_transaction(first_ec, command, wdata, |
00eb43a1 LP |
380 | wdata_len, rdata, rdata_len, |
381 | force_poll); | |
45bea155 | 382 | } |
1da177e4 | 383 | |
ab9e43c6 LP |
384 | EXPORT_SYMBOL(ec_transaction); |
385 | ||
6ccedb10 | 386 | static int acpi_ec_query(struct acpi_ec *ec, u8 * data) |
3576cf61 DS |
387 | { |
388 | int result; | |
6ccedb10 | 389 | u8 d; |
1da177e4 | 390 | |
6ccedb10 AS |
391 | if (!ec || !data) |
392 | return -EINVAL; | |
1da177e4 | 393 | |
6ccedb10 AS |
394 | /* |
395 | * Query the EC to find out which _Qxx method we need to evaluate. | |
396 | * Note that successful completion of the query causes the ACPI_EC_SCI | |
397 | * bit to be cleared (and thus clearing the interrupt source). | |
398 | */ | |
716e084e | 399 | |
00eb43a1 | 400 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0); |
6ccedb10 AS |
401 | if (result) |
402 | return result; | |
1da177e4 | 403 | |
6ccedb10 AS |
404 | if (!d) |
405 | return -ENODATA; | |
1da177e4 | 406 | |
6ccedb10 AS |
407 | *data = d; |
408 | return 0; | |
1da177e4 LT |
409 | } |
410 | ||
1da177e4 LT |
411 | /* -------------------------------------------------------------------------- |
412 | Event Management | |
413 | -------------------------------------------------------------------------- */ | |
837012ed AS |
414 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
415 | acpi_handle handle, acpi_ec_query_func func, | |
416 | void *data) | |
417 | { | |
418 | struct acpi_ec_query_handler *handler = | |
419 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); | |
420 | if (!handler) | |
421 | return -ENOMEM; | |
422 | ||
423 | handler->query_bit = query_bit; | |
424 | handler->handle = handle; | |
425 | handler->func = func; | |
426 | handler->data = data; | |
427 | mutex_lock(&ec->lock); | |
428 | list_add_tail(&handler->node, &ec->list); | |
429 | mutex_unlock(&ec->lock); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); | |
434 | ||
435 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) | |
436 | { | |
437 | struct acpi_ec_query_handler *handler; | |
438 | mutex_lock(&ec->lock); | |
439 | list_for_each_entry(handler, &ec->list, node) { | |
440 | if (query_bit == handler->query_bit) { | |
441 | list_del(&handler->node); | |
442 | kfree(handler); | |
443 | break; | |
444 | } | |
445 | } | |
446 | mutex_unlock(&ec->lock); | |
447 | } | |
448 | ||
449 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); | |
1da177e4 | 450 | |
50526df6 | 451 | static void acpi_ec_gpe_query(void *ec_cxt) |
45bea155 | 452 | { |
3d02b90b | 453 | struct acpi_ec *ec = ec_cxt; |
6ffb221a | 454 | u8 value = 0; |
837012ed | 455 | struct acpi_ec_query_handler *handler, copy; |
45bea155 | 456 | |
5d0c288b | 457 | if (!ec || acpi_ec_query(ec, &value)) |
e41334c0 | 458 | return; |
837012ed AS |
459 | mutex_lock(&ec->lock); |
460 | list_for_each_entry(handler, &ec->list, node) { | |
461 | if (value == handler->query_bit) { | |
462 | /* have custom handler for this bit */ | |
463 | memcpy(©, handler, sizeof(copy)); | |
464 | mutex_unlock(&ec->lock); | |
465 | if (copy.func) { | |
466 | copy.func(copy.data); | |
467 | } else if (copy.handle) { | |
468 | acpi_evaluate_object(copy.handle, NULL, NULL, NULL); | |
469 | } | |
470 | return; | |
471 | } | |
472 | } | |
473 | mutex_unlock(&ec->lock); | |
45bea155 | 474 | } |
1da177e4 | 475 | |
50526df6 | 476 | static u32 acpi_ec_gpe_handler(void *data) |
1da177e4 | 477 | { |
50526df6 | 478 | acpi_status status = AE_OK; |
6ffb221a | 479 | u8 value; |
3d02b90b | 480 | struct acpi_ec *ec = data; |
00eb43a1 | 481 | |
9e197219 | 482 | atomic_inc(&ec->event_count); |
3d02b90b | 483 | |
8e0341ba | 484 | if (acpi_ec_mode == EC_INTR) { |
af3fd140 | 485 | wake_up(&ec->wait); |
451566f4 DT |
486 | } |
487 | ||
bec5a1e0 | 488 | value = acpi_ec_read_status(ec); |
5d0c288b AS |
489 | if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) { |
490 | atomic_set(&ec->query_pending, 1); | |
6ccedb10 | 491 | status = |
837012ed | 492 | acpi_os_execute(OSL_EC_BURST_HANDLER, 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 | ||
9fd9f8e8 | 645 | atomic_set(&ec->query_pending, 1); |
c0900c35 AS |
646 | atomic_set(&ec->event_count, 1); |
647 | mutex_init(&ec->lock); | |
648 | init_waitqueue_head(&ec->wait); | |
837012ed | 649 | INIT_LIST_HEAD(&ec->list); |
c0900c35 AS |
650 | |
651 | return ec; | |
652 | } | |
837012ed | 653 | |
cd8c93a4 AS |
654 | static acpi_status |
655 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
837012ed | 656 | { |
cd8c93a4 AS |
657 | acpi_status status; |
658 | ||
659 | struct acpi_ec *ec = context; | |
660 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, | |
661 | ec_parse_io_ports, ec); | |
662 | if (ACPI_FAILURE(status)) | |
663 | return status; | |
837012ed AS |
664 | |
665 | /* Get GPE bit assignment (EC events). */ | |
666 | /* TODO: Add support for _GPE returning a package */ | |
cd8c93a4 AS |
667 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe); |
668 | if (ACPI_FAILURE(status)) | |
669 | return status; | |
837012ed AS |
670 | |
671 | /* Use the global lock for all EC transactions? */ | |
672 | acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); | |
673 | ||
837012ed AS |
674 | ec->handle = handle; |
675 | ||
0a524509 | 676 | printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n", |
837012ed AS |
677 | ec->gpe, ec->command_addr, ec->data_addr); |
678 | ||
cd8c93a4 | 679 | return AE_CTRL_TERMINATE; |
837012ed AS |
680 | } |
681 | ||
7c010de7 AS |
682 | static void ec_remove_handlers(struct acpi_ec *ec) |
683 | { | |
684 | acpi_remove_address_space_handler(ec->handle, | |
685 | ACPI_ADR_SPACE_EC, | |
686 | &acpi_ec_space_handler); | |
687 | acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); | |
688 | } | |
689 | ||
703959d4 | 690 | static int acpi_ec_add(struct acpi_device *device) |
1da177e4 | 691 | { |
703959d4 | 692 | struct acpi_ec *ec = NULL; |
45bea155 | 693 | |
45bea155 | 694 | if (!device) |
d550d98d | 695 | return -EINVAL; |
45bea155 | 696 | |
c0900c35 AS |
697 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
698 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
699 | ||
700 | ec = make_acpi_ec(); | |
45bea155 | 701 | if (!ec) |
d550d98d | 702 | return -ENOMEM; |
703959d4 | 703 | |
cd8c93a4 AS |
704 | if (ec_parse_device(device->handle, 0, ec, NULL) != |
705 | AE_CTRL_TERMINATE) { | |
c0900c35 AS |
706 | kfree(ec); |
707 | return -EINVAL; | |
45bea155 | 708 | } |
1da177e4 | 709 | |
c0900c35 | 710 | /* Check if we found the boot EC */ |
d66d969d AS |
711 | if (boot_ec) { |
712 | if (boot_ec->gpe == ec->gpe) { | |
7c010de7 | 713 | ec_remove_handlers(boot_ec); |
7c010de7 AS |
714 | mutex_destroy(&boot_ec->lock); |
715 | kfree(boot_ec); | |
716 | first_ec = boot_ec = NULL; | |
c0900c35 | 717 | } |
7c010de7 AS |
718 | } |
719 | if (!first_ec) | |
d033879c | 720 | first_ec = ec; |
c0900c35 | 721 | ec->handle = device->handle; |
c0900c35 | 722 | acpi_driver_data(device) = ec; |
1da177e4 | 723 | |
c0900c35 | 724 | acpi_ec_add_fs(device); |
c0900c35 | 725 | return 0; |
1da177e4 LT |
726 | } |
727 | ||
50526df6 | 728 | static int acpi_ec_remove(struct acpi_device *device, int type) |
1da177e4 | 729 | { |
01f22462 | 730 | struct acpi_ec *ec; |
07ddf768 | 731 | struct acpi_ec_query_handler *handler, *tmp; |
1da177e4 | 732 | |
1da177e4 | 733 | if (!device) |
d550d98d | 734 | return -EINVAL; |
1da177e4 LT |
735 | |
736 | ec = acpi_driver_data(device); | |
837012ed | 737 | mutex_lock(&ec->lock); |
07ddf768 | 738 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
837012ed AS |
739 | list_del(&handler->node); |
740 | kfree(handler); | |
741 | } | |
742 | mutex_unlock(&ec->lock); | |
1da177e4 | 743 | acpi_ec_remove_fs(device); |
c0900c35 | 744 | acpi_driver_data(device) = NULL; |
d033879c | 745 | if (ec == first_ec) |
c0900c35 AS |
746 | first_ec = NULL; |
747 | ||
d550d98d | 748 | return 0; |
1da177e4 LT |
749 | } |
750 | ||
1da177e4 | 751 | static acpi_status |
c0900c35 | 752 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 753 | { |
3d02b90b | 754 | struct acpi_ec *ec = context; |
1da177e4 | 755 | |
01f22462 | 756 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 757 | return AE_OK; |
1da177e4 LT |
758 | |
759 | /* | |
760 | * The first address region returned is the data port, and | |
761 | * the second address region returned is the status/command | |
762 | * port. | |
763 | */ | |
01f22462 | 764 | if (ec->data_addr == 0) |
6ffb221a | 765 | ec->data_addr = resource->data.io.minimum; |
01f22462 | 766 | else if (ec->command_addr == 0) |
6ffb221a | 767 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 768 | else |
1da177e4 | 769 | return AE_CTRL_TERMINATE; |
1da177e4 | 770 | |
1da177e4 LT |
771 | return AE_OK; |
772 | } | |
773 | ||
e8284321 AS |
774 | static int ec_install_handlers(struct acpi_ec *ec) |
775 | { | |
c0900c35 AS |
776 | acpi_status status; |
777 | status = acpi_install_gpe_handler(NULL, ec->gpe, | |
778 | ACPI_GPE_EDGE_TRIGGERED, | |
779 | &acpi_ec_gpe_handler, ec); | |
e8284321 AS |
780 | if (ACPI_FAILURE(status)) |
781 | return -ENODEV; | |
01f22462 | 782 | |
e8284321 AS |
783 | acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME); |
784 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); | |
785 | ||
786 | status = acpi_install_address_space_handler(ec->handle, | |
787 | ACPI_ADR_SPACE_EC, | |
788 | &acpi_ec_space_handler, | |
789 | &acpi_ec_space_setup, ec); | |
790 | if (ACPI_FAILURE(status)) { | |
791 | acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); | |
792 | return -ENODEV; | |
793 | } | |
794 | ||
795 | return 0; | |
796 | } | |
797 | ||
50526df6 | 798 | static int acpi_ec_start(struct acpi_device *device) |
1da177e4 | 799 | { |
c0900c35 | 800 | struct acpi_ec *ec; |
837012ed | 801 | int ret = 0; |
1da177e4 | 802 | |
1da177e4 | 803 | if (!device) |
d550d98d | 804 | return -EINVAL; |
1da177e4 LT |
805 | |
806 | ec = acpi_driver_data(device); | |
807 | ||
808 | if (!ec) | |
d550d98d | 809 | return -EINVAL; |
1da177e4 | 810 | |
7c010de7 | 811 | ret = ec_install_handlers(ec); |
837012ed AS |
812 | |
813 | /* EC is fully operational, allow queries */ | |
814 | atomic_set(&ec->query_pending, 0); | |
c0900c35 | 815 | |
837012ed | 816 | return ret; |
1da177e4 LT |
817 | } |
818 | ||
50526df6 | 819 | static int acpi_ec_stop(struct acpi_device *device, int type) |
1da177e4 | 820 | { |
c0900c35 | 821 | struct acpi_ec *ec; |
1da177e4 | 822 | |
1da177e4 | 823 | if (!device) |
d550d98d | 824 | return -EINVAL; |
1da177e4 LT |
825 | |
826 | ec = acpi_driver_data(device); | |
c0900c35 AS |
827 | if (!ec) |
828 | return -EINVAL; | |
7c010de7 | 829 | ec_remove_handlers(ec); |
d550d98d | 830 | return 0; |
1da177e4 LT |
831 | } |
832 | ||
c0900c35 AS |
833 | int __init acpi_ec_ecdt_probe(void) |
834 | { | |
835 | int ret; | |
836 | acpi_status status; | |
50526df6 | 837 | struct acpi_table_ecdt *ecdt_ptr; |
45bea155 | 838 | |
d66d969d AS |
839 | boot_ec = make_acpi_ec(); |
840 | if (!boot_ec) | |
c0900c35 AS |
841 | return -ENOMEM; |
842 | /* | |
843 | * Generate a boot ec context | |
844 | */ | |
15a58ed1 AS |
845 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
846 | (struct acpi_table_header **)&ecdt_ptr); | |
cd8c93a4 AS |
847 | if (ACPI_SUCCESS(status)) { |
848 | printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n\n"); | |
849 | boot_ec->command_addr = ecdt_ptr->control.address; | |
850 | boot_ec->data_addr = ecdt_ptr->data.address; | |
851 | boot_ec->gpe = ecdt_ptr->gpe; | |
852 | boot_ec->handle = ACPI_ROOT_OBJECT; | |
853 | } else { | |
854 | printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n"); | |
855 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, | |
856 | boot_ec, NULL); | |
857 | if (ACPI_FAILURE(status)) | |
858 | goto error; | |
859 | } | |
1da177e4 | 860 | |
d66d969d | 861 | ret = ec_install_handlers(boot_ec); |
d033879c AS |
862 | if (!ret) { |
863 | first_ec = boot_ec; | |
e8284321 | 864 | return 0; |
d033879c | 865 | } |
c0900c35 | 866 | error: |
d66d969d AS |
867 | kfree(boot_ec); |
868 | boot_ec = NULL; | |
1da177e4 LT |
869 | |
870 | return -ENODEV; | |
871 | } | |
872 | ||
50526df6 | 873 | static int __init acpi_ec_init(void) |
1da177e4 | 874 | { |
50526df6 | 875 | int result = 0; |
1da177e4 | 876 | |
1da177e4 | 877 | if (acpi_disabled) |
d550d98d | 878 | return 0; |
1da177e4 LT |
879 | |
880 | acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir); | |
881 | if (!acpi_ec_dir) | |
d550d98d | 882 | return -ENODEV; |
1da177e4 LT |
883 | |
884 | /* Now register the driver for the EC */ | |
885 | result = acpi_bus_register_driver(&acpi_ec_driver); | |
886 | if (result < 0) { | |
887 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
d550d98d | 888 | return -ENODEV; |
1da177e4 LT |
889 | } |
890 | ||
d550d98d | 891 | return result; |
1da177e4 LT |
892 | } |
893 | ||
894 | subsys_initcall(acpi_ec_init); | |
895 | ||
896 | /* EC driver currently not unloadable */ | |
897 | #if 0 | |
50526df6 | 898 | static void __exit acpi_ec_exit(void) |
1da177e4 | 899 | { |
1da177e4 LT |
900 | |
901 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
902 | ||
903 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); | |
904 | ||
d550d98d | 905 | return; |
1da177e4 | 906 | } |
50526df6 | 907 | #endif /* 0 */ |
1da177e4 | 908 | |
02b28a33 | 909 | static int __init acpi_ec_set_intr_mode(char *str) |
45bea155 | 910 | { |
02b28a33 | 911 | int intr; |
7b15f5e7 | 912 | |
02b28a33 | 913 | if (!get_option(&str, &intr)) |
7b15f5e7 LY |
914 | return 0; |
915 | ||
c0900c35 AS |
916 | acpi_ec_mode = (intr) ? EC_INTR : EC_POLL; |
917 | ||
9e197219 | 918 | printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling"); |
703959d4 | 919 | |
9b41046c | 920 | return 1; |
45bea155 | 921 | } |
50526df6 | 922 | |
53f11d4f | 923 | __setup("ec_intr=", acpi_ec_set_intr_mode); |