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