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