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