blob: d5a5958d4b201982300695ca0809ab1173836ab3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03004 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050036#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040037#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/io.h>
39#include <acpi/acpi_bus.h>
40#include <acpi/acpi_drivers.h>
41#include <acpi/actypes.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_EC_DEVICE_NAME "Embedded Controller"
45#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040046
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030047#undef PREFIX
48#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040049
Márton Németh3ebe08a2007-11-21 03:23:26 +030050/* Uncomment next line to get verbose print outs*/
51/* #define DEBUG */
52
Denis M. Sadykov703959d2006-09-26 19:50:33 +040053/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050056#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040058
Denis M. Sadykov703959d2006-09-26 19:50:33 +040059/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030060enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030061 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,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040067
Denis M. Sadykov703959d2006-09-26 19:50:33 +040068/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030069enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040071 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072};
73
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030074#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040075#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040076
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040077enum {
78 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
79 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy78439322007-10-22 14:18:43 +040080 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
Len Brown0af2f652007-11-20 19:59:08 -050081 EC_FLAGS_ONLY_IBF_GPE, /* Expect GPE only for IBF = 0 event */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040082};
83
Len Brown50526df2005-08-11 17:32:05 -040084static int acpi_ec_remove(struct acpi_device *device, int type);
85static int acpi_ec_start(struct acpi_device *device);
86static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040087static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089static const struct acpi_device_id ec_device_ids[] = {
90 {"PNP0C09", 0},
91 {"", 0},
92};
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050095 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040096 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020097 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040098 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040099 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -0400100 .remove = acpi_ec_remove,
101 .start = acpi_ec_start,
102 .stop = acpi_ec_stop,
103 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400105
106/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300107/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400108typedef int (*acpi_ec_query_func) (void *data);
109
110struct acpi_ec_query_handler {
111 struct list_head node;
112 acpi_ec_query_func func;
113 acpi_handle handle;
114 void *data;
115 u8 query_bit;
116};
117
Adrian Bunka854e082006-12-19 12:56:12 -0800118static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400119 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300120 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400121 unsigned long command_addr;
122 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400124 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300125 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400127 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400128 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300129} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* --------------------------------------------------------------------------
132 Transaction Management
133 -------------------------------------------------------------------------- */
134
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300137 u8 x = inb(ec->command_addr);
138 pr_debug(PREFIX "---> status = 0x%2x\n", x);
139 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400143{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300144 u8 x = inb(ec->data_addr);
145 pr_debug(PREFIX "---> data = 0x%2x\n", x);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400146 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400147}
148
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400150{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300151 pr_debug(PREFIX "<--- command = 0x%2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400153}
154
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400155static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400156{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300157 pr_debug(PREFIX "<--- data = 0x%2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400158 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400159}
160
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400161static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400162{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400163 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500164 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300165 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400166 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400167 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300168 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400169 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400170 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400171 }
172
173 return 0;
174}
175
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400176static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400177{
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400178 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
179 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400180 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
181 msecs_to_jiffies(ACPI_EC_DELAY)))
Len Brown0af2f652007-11-20 19:59:08 -0500182 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400183 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
184 if (acpi_ec_check_status(ec, event)) {
Len Brown0af2f652007-11-20 19:59:08 -0500185 if (event == ACPI_EC_EVENT_OBF_1) {
186 /* miss OBF = 1 GPE, don't expect it anymore */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300187 pr_info(PREFIX "missing OBF_1 confirmation,"
Len Brown0af2f652007-11-20 19:59:08 -0500188 "switching to degraded mode.\n");
189 set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400190 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400191 /* missing GPEs, switch back to poll mode */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300192 if (printk_ratelimit())
193 pr_info(PREFIX "missing IBF_1 confirmations,"
194 "switch off interrupt mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400195 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400196 }
Len Brown0af2f652007-11-20 19:59:08 -0500197 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400198 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400199 } else {
200 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
201 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
202 while (time_before(jiffies, delay)) {
203 if (acpi_ec_check_status(ec, event))
Len Brown0af2f652007-11-20 19:59:08 -0500204 return 0;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400205 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300206 }
Márton Németh3ebe08a2007-11-21 03:23:26 +0300207 pr_err(PREFIX "acpi_ec_wait timeout,"
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400208 " status = %d, expect_event = %d\n",
209 acpi_ec_read_status(ec), event);
Len Brown0af2f652007-11-20 19:59:08 -0500210 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500211}
212
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400213static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300214 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200215 u8 * rdata, unsigned rdata_len,
216 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400217{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300218 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400219 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400220 acpi_ec_write_cmd(ec, command);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300221 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300222 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400223 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300224 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300225 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300226 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300227 goto end;
228 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400229 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400230 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400231 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400232
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300233 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400234 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300235 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300236 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300237 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300238 goto end;
239 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400240 } else if (command == ACPI_EC_COMMAND_QUERY)
241 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400242
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300243 for (; rdata_len > 0; --rdata_len) {
Len Brown0af2f652007-11-20 19:59:08 -0500244 if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
245 force_poll = 1;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400246 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300247 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300248 pr_err(PREFIX "read timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300249 goto end;
250 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400251 /* Don't expect GPE after last read */
252 if (rdata_len > 1)
253 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400254 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400255 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300256 end:
Márton Németh3ebe08a2007-11-21 03:23:26 +0300257 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300258 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400259}
260
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400261static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300262 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200263 u8 * rdata, unsigned rdata_len,
264 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400265{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400266 int status;
Len Brown50526df2005-08-11 17:32:05 -0400267 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400269 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300272 if (rdata)
273 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300275 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400276 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300278 if (ACPI_FAILURE(status)) {
279 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500283
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400284 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400285 if (status) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300286 pr_err(PREFIX "input buffer is not empty, "
287 "aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500288 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300291 status = acpi_ec_transaction_unlocked(ec, command,
292 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200293 rdata, rdata_len,
294 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400295
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300296 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400298 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300300 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Patrick Mocheld550d982006-06-27 00:41:40 -0400302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300305/*
306 * Note: samsung nv5000 doesn't work with ec burst mode.
307 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
308 */
309int acpi_ec_burst_enable(struct acpi_ec *ec)
310{
311 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200312 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300313}
314
315int acpi_ec_burst_disable(struct acpi_ec *ec)
316{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200317 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300318}
319
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300320static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400321{
322 int result;
323 u8 d;
324
325 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200326 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400327 *data = d;
328 return result;
329}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400330
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400331static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
332{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300333 u8 wdata[2] = { address, data };
334 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200335 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
339 * Externally callable EC access functions. For now, assume 1 EC only
340 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300341int ec_burst_enable(void)
342{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300343 if (!first_ec)
344 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300345 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300346}
347
348EXPORT_SYMBOL(ec_burst_enable);
349
350int ec_burst_disable(void)
351{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300352 if (!first_ec)
353 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300354 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300355}
356
357EXPORT_SYMBOL(ec_burst_disable);
358
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300359int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400362 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (!first_ec)
365 return -ENODEV;
366
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300367 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (!err) {
370 *val = temp_data;
371 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400372 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return err;
374}
Len Brown50526df2005-08-11 17:32:05 -0400375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376EXPORT_SYMBOL(ec_read);
377
Len Brown50526df2005-08-11 17:32:05 -0400378int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err;
381
382 if (!first_ec)
383 return -ENODEV;
384
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300385 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return err;
388}
Len Brown50526df2005-08-11 17:32:05 -0400389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390EXPORT_SYMBOL(ec_write);
391
Randy Dunlap616362d2006-10-27 01:47:34 -0400392int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500393 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200394 u8 * rdata, unsigned rdata_len,
395 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400396{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400397 if (!first_ec)
398 return -ENODEV;
399
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300400 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200401 wdata_len, rdata, rdata_len,
402 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400403}
Luming Yu45bea152005-07-23 04:08:00 -0400404
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400405EXPORT_SYMBOL(ec_transaction);
406
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300407static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400408{
409 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300410 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400411
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300412 if (!ec || !data)
413 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400414
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300415 /*
416 * Query the EC to find out which _Qxx method we need to evaluate.
417 * Note that successful completion of the query causes the ACPI_EC_SCI
418 * bit to be cleared (and thus clearing the interrupt source).
419 */
Luming Yu45bea152005-07-23 04:08:00 -0400420
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200421 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300422 if (result)
423 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400424
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300425 if (!d)
426 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400427
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300428 *data = d;
429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* --------------------------------------------------------------------------
433 Event Management
434 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400435int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
436 acpi_handle handle, acpi_ec_query_func func,
437 void *data)
438{
439 struct acpi_ec_query_handler *handler =
440 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
441 if (!handler)
442 return -ENOMEM;
443
444 handler->query_bit = query_bit;
445 handler->handle = handle;
446 handler->func = func;
447 handler->data = data;
448 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400449 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400450 mutex_unlock(&ec->lock);
451 return 0;
452}
453
454EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
455
456void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
457{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200458 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400459 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200460 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400461 if (query_bit == handler->query_bit) {
462 list_del(&handler->node);
463 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400464 }
465 }
466 mutex_unlock(&ec->lock);
467}
468
469EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Len Brown50526df2005-08-11 17:32:05 -0400471static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300473 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400474 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400475 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400476
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300477 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300478 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400479 mutex_lock(&ec->lock);
480 list_for_each_entry(handler, &ec->list, node) {
481 if (value == handler->query_bit) {
482 /* have custom handler for this bit */
483 memcpy(&copy, handler, sizeof(copy));
484 mutex_unlock(&ec->lock);
485 if (copy.func) {
486 copy.func(copy.data);
487 } else if (copy.handle) {
488 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
489 }
490 return;
491 }
492 }
493 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Len Brown50526df2005-08-11 17:32:05 -0400496static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown50526df2005-08-11 17:32:05 -0400498 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300499 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200500
Márton Németh3ebe08a2007-11-21 03:23:26 +0300501 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400502 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400503 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300504 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500505
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400506 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
507 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
508 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
509 acpi_ec_gpe_query, ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400510 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
511 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300512 if (printk_ratelimit())
513 pr_info(PREFIX "non-query interrupt received,"
514 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400515 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400516 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300517
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400518 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400519 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/* --------------------------------------------------------------------------
523 Address Space Management
524 -------------------------------------------------------------------------- */
525
526static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400527acpi_ec_space_setup(acpi_handle region_handle,
528 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 /*
531 * The EC object is in the handler context and is needed
532 * when calling the acpi_ec_space_handler.
533 */
Len Brown50526df2005-08-11 17:32:05 -0400534 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
535 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return AE_OK;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400541acpi_ec_space_handler(u32 function, acpi_physical_address address,
542 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400543 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300545 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400546 int result = 0, i = 0;
547 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400552 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400554
555 if (bits != 8 && acpi_strict)
556 return AE_BAD_PARAMETER;
557
558 while (bits - i > 0) {
559 if (function == ACPI_READ) {
560 result = acpi_ec_read(ec, address, &temp);
561 (*value) |= ((acpi_integer)temp) << i;
562 } else {
563 temp = 0xff & ((*value) >> i);
564 result = acpi_ec_write(ec, address, temp);
565 }
566 i += 8;
567 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (result) {
571 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* --------------------------------------------------------------------------
586 FS Interface (/proc)
587 -------------------------------------------------------------------------- */
588
Len Brown50526df2005-08-11 17:32:05 -0400589static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Len Brown50526df2005-08-11 17:32:05 -0400591static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300593 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!ec)
596 goto end;
597
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300598 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
599 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
600 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
601 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400602 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400603 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
608{
609 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
610}
611
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400612static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400613 .open = acpi_ec_info_open_fs,
614 .read = seq_read,
615 .llseek = seq_lseek,
616 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 .owner = THIS_MODULE,
618};
619
Len Brown50526df2005-08-11 17:32:05 -0400620static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Len Brown50526df2005-08-11 17:32:05 -0400622 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!acpi_device_dir(device)) {
625 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400626 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400628 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
631 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400632 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 else {
636 entry->proc_fops = &acpi_ec_info_ops;
637 entry->data = acpi_driver_data(device);
638 entry->owner = THIS_MODULE;
639 }
640
Patrick Mocheld550d982006-06-27 00:41:40 -0400641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Len Brown50526df2005-08-11 17:32:05 -0400644static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (acpi_device_dir(device)) {
648 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
649 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
650 acpi_device_dir(device) = NULL;
651 }
652
Patrick Mocheld550d982006-06-27 00:41:40 -0400653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/* --------------------------------------------------------------------------
657 Driver Interface
658 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300659static acpi_status
660ec_parse_io_ports(struct acpi_resource *resource, void *context);
661
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300662static struct acpi_ec *make_acpi_ec(void)
663{
664 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
665 if (!ec)
666 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400667 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300668 mutex_init(&ec->lock);
669 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400670 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300671 return ec;
672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400674static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400675acpi_ec_register_query_methods(acpi_handle handle, u32 level,
676 void *context, void **return_value)
677{
678 struct acpi_namespace_node *node = handle;
679 struct acpi_ec *ec = context;
680 int value = 0;
681 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
682 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
683 }
684 return AE_OK;
685}
686
687static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400688ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400689{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400690 acpi_status status;
691
692 struct acpi_ec *ec = context;
693 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
694 ec_parse_io_ports, ec);
695 if (ACPI_FAILURE(status))
696 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400697
698 /* Get GPE bit assignment (EC events). */
699 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400700 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
701 if (ACPI_FAILURE(status))
702 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400703 /* Find and register all query methods */
704 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
705 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400706 /* Use the global lock for all EC transactions? */
707 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400708 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400709 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400710}
711
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400712static void ec_remove_handlers(struct acpi_ec *ec)
713{
714 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
715 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300716 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400717 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
718 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300719 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400720 ec->handlers_installed = 0;
721}
722
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400723static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400725 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300729 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
730 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
731
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400732 /* Check for boot EC */
733 if (boot_ec) {
734 if (boot_ec->handle == device->handle) {
735 /* Pre-loaded EC from DSDT, just move pointer */
736 ec = boot_ec;
737 boot_ec = NULL;
738 goto end;
739 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
740 /* ECDT-based EC, time to shut it down */
741 ec_remove_handlers(boot_ec);
742 kfree(boot_ec);
743 first_ec = boot_ec = NULL;
744 }
745 }
746
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300747 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400749 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400751 if (ec_parse_device(device->handle, 0, ec, NULL) !=
752 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300753 kfree(ec);
754 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400755 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300756 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400757 end:
758 if (!first_ec)
759 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300761 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300762 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400763 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300764 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400765 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Len Brown50526df2005-08-11 17:32:05 -0400769static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300771 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200772 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400778 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200779 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400780 list_del(&handler->node);
781 kfree(handler);
782 }
783 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300785 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300786 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300787 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400788 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300793ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300795 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300797 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /*
801 * The first address region returned is the data port, and
802 * the second address region returned is the status/command
803 * port.
804 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300805 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400806 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300807 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400808 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300809 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return AE_OK;
813}
814
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300815static int ec_install_handlers(struct acpi_ec *ec)
816{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300817 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400818 if (ec->handlers_installed)
819 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300820 status = acpi_install_gpe_handler(NULL, ec->gpe,
821 ACPI_GPE_EDGE_TRIGGERED,
822 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300823 if (ACPI_FAILURE(status))
824 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300825
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300826 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
827 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
828
829 status = acpi_install_address_space_handler(ec->handle,
830 ACPI_ADR_SPACE_EC,
831 &acpi_ec_space_handler,
832 &acpi_ec_space_setup, ec);
833 if (ACPI_FAILURE(status)) {
834 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
835 return -ENODEV;
836 }
837
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400838 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300839 return 0;
840}
841
Len Brown50526df2005-08-11 17:32:05 -0400842static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300844 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400845 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 ec = acpi_driver_data(device);
851
852 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400855 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300856
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400857 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400858 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400859 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Len Brown50526df2005-08-11 17:32:05 -0400862static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300864 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300868 if (!ec)
869 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400870 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400871
Patrick Mocheld550d982006-06-27 00:41:40 -0400872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Len Brown50526df2005-08-11 17:32:05 -0400875int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Len Brown50526df2005-08-11 17:32:05 -0400877 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300878 acpi_status status;
879 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300881 boot_ec = make_acpi_ec();
882 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300883 return -ENOMEM;
884 /*
885 * Generate a boot ec context
886 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300887 status = acpi_get_table(ACPI_SIG_ECDT, 1,
888 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400889 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300890 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400891 boot_ec->command_addr = ecdt_ptr->control.address;
892 boot_ec->data_addr = ecdt_ptr->data.address;
893 boot_ec->gpe = ecdt_ptr->gpe;
894 boot_ec->handle = ACPI_ROOT_OBJECT;
895 } else {
896 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
897 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
898 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400899 /* Check that acpi_get_devices actually find something */
900 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400901 goto error;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300904 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300905 if (!ret) {
906 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300907 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300908 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300909 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300910 kfree(boot_ec);
911 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENODEV;
913}
914
Len Brown50526df2005-08-11 17:32:05 -0400915static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Len Brown50526df2005-08-11 17:32:05 -0400917 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
923 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* Now register the driver for the EC */
927 result = acpi_bus_register_driver(&acpi_ec_driver);
928 if (result < 0) {
929 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Patrick Mocheld550d982006-06-27 00:41:40 -0400933 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936subsys_initcall(acpi_ec_init);
937
938/* EC driver currently not unloadable */
939#if 0
Len Brown50526df2005-08-11 17:32:05 -0400940static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 acpi_bus_unregister_driver(&acpi_ec_driver);
944
945 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
946
Patrick Mocheld550d982006-06-27 00:41:40 -0400947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400949#endif /* 0 */