blob: 06b78e5e33a11b8a13a0b0874311f2bb7faf075a [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
Denis M. Sadykov703959d2006-09-26 19:50:33 +040050/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050053#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040055
Denis M. Sadykov703959d2006-09-26 19:50:33 +040056/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030057enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030058 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030063};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040064
Denis M. Sadykov703959d2006-09-26 19:50:33 +040065/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040067 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040068 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069};
70
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030071#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040073
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040074enum {
75 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
76 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy78439322007-10-22 14:18:43 +040077 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +040078 EC_FLAGS_ONLY_IBF_GPE, /* Expect GPE only for IBF = 0 event */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040079};
80
Len Brown50526df2005-08-11 17:32:05 -040081static int acpi_ec_remove(struct acpi_device *device, int type);
82static int acpi_ec_start(struct acpi_device *device);
83static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040084static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Renninger1ba90e32007-07-23 14:44:41 +020086static const struct acpi_device_id ec_device_ids[] = {
87 {"PNP0C09", 0},
88 {"", 0},
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050092 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040093 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020094 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040095 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040096 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040097 .remove = acpi_ec_remove,
98 .start = acpi_ec_start,
99 .stop = acpi_ec_stop,
100 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400102
103/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300104/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400105typedef int (*acpi_ec_query_func) (void *data);
106
107struct acpi_ec_query_handler {
108 struct list_head node;
109 acpi_ec_query_func func;
110 acpi_handle handle;
111 void *data;
112 u8 query_bit;
113};
114
Adrian Bunka854e082006-12-19 12:56:12 -0800115static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400116 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300117 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400118 unsigned long command_addr;
119 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400120 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400121 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300122 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400124 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400125 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300126} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* --------------------------------------------------------------------------
129 Transaction Management
130 -------------------------------------------------------------------------- */
131
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400132static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400134 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400138{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400139 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400140}
141
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400143{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400144 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400145}
146
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400147static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400150}
151
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400152static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400154 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500155 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300156 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400157 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400158 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300159 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400160 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400162 }
163
164 return 0;
165}
166
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400167static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400168{
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400169 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
170 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400171 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
172 msecs_to_jiffies(ACPI_EC_DELAY)))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400174 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
175 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400176 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400177 /* miss OBF = 1 GPE, don't expect it anymore */
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400178 printk(KERN_INFO PREFIX "missing OBF_1 confirmation,"
179 "switching to degraded mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400180 set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400181 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400182 /* missing GPEs, switch back to poll mode */
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400183 printk(KERN_INFO PREFIX "missing IBF_1 confirmations,"
184 "switch off interrupt mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400185 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400186 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400187 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400188 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400189 } else {
190 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
191 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
192 while (time_before(jiffies, delay)) {
193 if (acpi_ec_check_status(ec, event))
194 return 0;
195 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300196 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400197 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
198 " status = %d, expect_event = %d\n",
199 acpi_ec_read_status(ec), event);
Patrick Mocheld550d982006-06-27 00:41:40 -0400200 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500201}
202
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400203static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300204 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200205 u8 * rdata, unsigned rdata_len,
206 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400207{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300208 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400209 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400210 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400211
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300212 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400213 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300214 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300215 printk(KERN_ERR PREFIX
216 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300217 goto end;
218 }
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_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400221 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400222
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300223 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400224 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300225 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300226 printk(KERN_ERR PREFIX
227 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300228 goto end;
229 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400230 } else if (command == ACPI_EC_COMMAND_QUERY)
231 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400232
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300233 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400234 if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
235 force_poll = 1;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400236 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300237 if (result) {
238 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300239 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300240 goto end;
241 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400242 /* Don't expect GPE after last read */
243 if (rdata_len > 1)
244 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400245 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400246 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300247 end:
248 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400249}
250
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400251static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300252 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200253 u8 * rdata, unsigned rdata_len,
254 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400255{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400256 int status;
Len Brown50526df2005-08-11 17:32:05 -0400257 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400259 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300262 if (rdata)
263 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300265 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400266 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300268 if (ACPI_FAILURE(status)) {
269 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400270 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500273
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400274 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400275 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400276 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300277 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500278 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300281 status = acpi_ec_transaction_unlocked(ec, command,
282 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200283 rdata, rdata_len,
284 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400285
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300286 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400288 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300290 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300295/*
296 * Note: samsung nv5000 doesn't work with ec burst mode.
297 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
298 */
299int acpi_ec_burst_enable(struct acpi_ec *ec)
300{
301 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200302 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300303}
304
305int acpi_ec_burst_disable(struct acpi_ec *ec)
306{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200307 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300308}
309
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300310static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400311{
312 int result;
313 u8 d;
314
315 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200316 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400317 *data = d;
318 return result;
319}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400320
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400321static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
322{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300323 u8 wdata[2] = { address, data };
324 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200325 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 * Externally callable EC access functions. For now, assume 1 EC only
330 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300331int ec_burst_enable(void)
332{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300333 if (!first_ec)
334 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300335 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300336}
337
338EXPORT_SYMBOL(ec_burst_enable);
339
340int ec_burst_disable(void)
341{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300342 if (!first_ec)
343 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300344 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300345}
346
347EXPORT_SYMBOL(ec_burst_disable);
348
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300349int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400352 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!first_ec)
355 return -ENODEV;
356
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300357 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (!err) {
360 *val = temp_data;
361 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400362 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return err;
364}
Len Brown50526df2005-08-11 17:32:05 -0400365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366EXPORT_SYMBOL(ec_read);
367
Len Brown50526df2005-08-11 17:32:05 -0400368int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int err;
371
372 if (!first_ec)
373 return -ENODEV;
374
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300375 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return err;
378}
Len Brown50526df2005-08-11 17:32:05 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380EXPORT_SYMBOL(ec_write);
381
Randy Dunlap616362d2006-10-27 01:47:34 -0400382int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500383 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200384 u8 * rdata, unsigned rdata_len,
385 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400386{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400387 if (!first_ec)
388 return -ENODEV;
389
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300390 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200391 wdata_len, rdata, rdata_len,
392 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400393}
Luming Yu45bea152005-07-23 04:08:00 -0400394
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400395EXPORT_SYMBOL(ec_transaction);
396
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400398{
399 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300400 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400401
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300402 if (!ec || !data)
403 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400404
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300405 /*
406 * Query the EC to find out which _Qxx method we need to evaluate.
407 * Note that successful completion of the query causes the ACPI_EC_SCI
408 * bit to be cleared (and thus clearing the interrupt source).
409 */
Luming Yu45bea152005-07-23 04:08:00 -0400410
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200411 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300412 if (result)
413 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400414
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300415 if (!d)
416 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400417
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300418 *data = d;
419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* --------------------------------------------------------------------------
423 Event Management
424 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400425int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
426 acpi_handle handle, acpi_ec_query_func func,
427 void *data)
428{
429 struct acpi_ec_query_handler *handler =
430 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
431 if (!handler)
432 return -ENOMEM;
433
434 handler->query_bit = query_bit;
435 handler->handle = handle;
436 handler->func = func;
437 handler->data = data;
438 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400439 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400440 mutex_unlock(&ec->lock);
441 return 0;
442}
443
444EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
445
446void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
447{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200448 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400449 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200450 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400451 if (query_bit == handler->query_bit) {
452 list_del(&handler->node);
453 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400454 }
455 }
456 mutex_unlock(&ec->lock);
457}
458
459EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Len Brown50526df2005-08-11 17:32:05 -0400461static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300463 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400464 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400465 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400466
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300467 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300468 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400469 mutex_lock(&ec->lock);
470 list_for_each_entry(handler, &ec->list, node) {
471 if (value == handler->query_bit) {
472 /* have custom handler for this bit */
473 memcpy(&copy, handler, sizeof(copy));
474 mutex_unlock(&ec->lock);
475 if (copy.func) {
476 copy.func(copy.data);
477 } else if (copy.handle) {
478 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
479 }
480 return;
481 }
482 }
483 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Len Brown50526df2005-08-11 17:32:05 -0400486static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Len Brown50526df2005-08-11 17:32:05 -0400488 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300489 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200490
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400491 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400492 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300493 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500494
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400495 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
496 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
497 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
498 acpi_ec_gpe_query, ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400499 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
500 /* this is non-query, must be confirmation */
501 printk(KERN_INFO PREFIX "non-query interrupt received,"
502 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400503 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400504 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300505
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400506 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400507 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/* --------------------------------------------------------------------------
511 Address Space Management
512 -------------------------------------------------------------------------- */
513
514static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400515acpi_ec_space_setup(acpi_handle region_handle,
516 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 /*
519 * The EC object is in the handler context and is needed
520 * when calling the acpi_ec_space_handler.
521 */
Len Brown50526df2005-08-11 17:32:05 -0400522 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
523 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return AE_OK;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400529acpi_ec_space_handler(u32 function, acpi_physical_address address,
530 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400531 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300533 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400534 int result = 0, i = 0;
535 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400538 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400540 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400541 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400542
543 if (bits != 8 && acpi_strict)
544 return AE_BAD_PARAMETER;
545
546 while (bits - i > 0) {
547 if (function == ACPI_READ) {
548 result = acpi_ec_read(ec, address, &temp);
549 (*value) |= ((acpi_integer)temp) << i;
550 } else {
551 temp = 0xff & ((*value) >> i);
552 result = acpi_ec_write(ec, address, temp);
553 }
554 i += 8;
555 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 switch (result) {
559 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400563 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* --------------------------------------------------------------------------
574 FS Interface (/proc)
575 -------------------------------------------------------------------------- */
576
Len Brown50526df2005-08-11 17:32:05 -0400577static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Len Brown50526df2005-08-11 17:32:05 -0400579static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300581 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!ec)
584 goto end;
585
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300586 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
587 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
588 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
589 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400590 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400591 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
596{
597 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
598}
599
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400600static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400601 .open = acpi_ec_info_open_fs,
602 .read = seq_read,
603 .llseek = seq_lseek,
604 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 .owner = THIS_MODULE,
606};
607
Len Brown50526df2005-08-11 17:32:05 -0400608static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Len Brown50526df2005-08-11 17:32:05 -0400610 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!acpi_device_dir(device)) {
613 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400614 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400620 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400622 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else {
624 entry->proc_fops = &acpi_ec_info_ops;
625 entry->data = acpi_driver_data(device);
626 entry->owner = THIS_MODULE;
627 }
628
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Len Brown50526df2005-08-11 17:32:05 -0400632static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (acpi_device_dir(device)) {
636 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
637 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
638 acpi_device_dir(device) = NULL;
639 }
640
Patrick Mocheld550d982006-06-27 00:41:40 -0400641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/* --------------------------------------------------------------------------
645 Driver Interface
646 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647static acpi_status
648ec_parse_io_ports(struct acpi_resource *resource, void *context);
649
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300650static struct acpi_ec *make_acpi_ec(void)
651{
652 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
653 if (!ec)
654 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400655 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300656 mutex_init(&ec->lock);
657 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400658 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300659 return ec;
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400662static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400663acpi_ec_register_query_methods(acpi_handle handle, u32 level,
664 void *context, void **return_value)
665{
666 struct acpi_namespace_node *node = handle;
667 struct acpi_ec *ec = context;
668 int value = 0;
669 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
670 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
671 }
672 return AE_OK;
673}
674
675static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400676ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400677{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400678 acpi_status status;
679
680 struct acpi_ec *ec = context;
681 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
682 ec_parse_io_ports, ec);
683 if (ACPI_FAILURE(status))
684 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400685
686 /* Get GPE bit assignment (EC events). */
687 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400688 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
689 if (ACPI_FAILURE(status))
690 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400691 /* Find and register all query methods */
692 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
693 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400694 /* Use the global lock for all EC transactions? */
695 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400696 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400697 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400698}
699
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400700static void ec_remove_handlers(struct acpi_ec *ec)
701{
702 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
703 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
704 printk(KERN_ERR PREFIX "failed to remove space handler\n");
705 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
706 &acpi_ec_gpe_handler)))
707 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
708 ec->handlers_installed = 0;
709}
710
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400711static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400713 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300717 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
718 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
719
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400720 /* Check for boot EC */
721 if (boot_ec) {
722 if (boot_ec->handle == device->handle) {
723 /* Pre-loaded EC from DSDT, just move pointer */
724 ec = boot_ec;
725 boot_ec = NULL;
726 goto end;
727 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
728 /* ECDT-based EC, time to shut it down */
729 ec_remove_handlers(boot_ec);
730 kfree(boot_ec);
731 first_ec = boot_ec = NULL;
732 }
733 }
734
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400737 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400739 if (ec_parse_device(device->handle, 0, ec, NULL) !=
740 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 kfree(ec);
742 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400743 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300744 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400745 end:
746 if (!first_ec)
747 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300749 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400750 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
751 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400752 printk(KERN_INFO PREFIX "driver started in %s mode\n",
753 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Len Brown50526df2005-08-11 17:32:05 -0400757static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300759 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200760 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400766 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200767 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400768 list_del(&handler->node);
769 kfree(handler);
770 }
771 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300773 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300774 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300775 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400776 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300781ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300783 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300785 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /*
789 * The first address region returned is the data port, and
790 * the second address region returned is the status/command
791 * port.
792 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300793 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400794 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300795 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400796 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300797 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return AE_OK;
801}
802
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300803static int ec_install_handlers(struct acpi_ec *ec)
804{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300805 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400806 if (ec->handlers_installed)
807 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300808 status = acpi_install_gpe_handler(NULL, ec->gpe,
809 ACPI_GPE_EDGE_TRIGGERED,
810 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300811 if (ACPI_FAILURE(status))
812 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300813
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300814 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
815 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
816
817 status = acpi_install_address_space_handler(ec->handle,
818 ACPI_ADR_SPACE_EC,
819 &acpi_ec_space_handler,
820 &acpi_ec_space_setup, ec);
821 if (ACPI_FAILURE(status)) {
822 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
823 return -ENODEV;
824 }
825
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400826 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300827 return 0;
828}
829
Len Brown50526df2005-08-11 17:32:05 -0400830static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300832 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400833 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400836 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 ec = acpi_driver_data(device);
839
840 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400843 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300844
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400845 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400846 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Len Brown50526df2005-08-11 17:32:05 -0400850static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300852 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300856 if (!ec)
857 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400858 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400859
Patrick Mocheld550d982006-06-27 00:41:40 -0400860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Len Brown50526df2005-08-11 17:32:05 -0400863int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Len Brown50526df2005-08-11 17:32:05 -0400865 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300866 acpi_status status;
867 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300869 boot_ec = make_acpi_ec();
870 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300871 return -ENOMEM;
872 /*
873 * Generate a boot ec context
874 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300875 status = acpi_get_table(ACPI_SIG_ECDT, 1,
876 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400877 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400878 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400879 boot_ec->command_addr = ecdt_ptr->control.address;
880 boot_ec->data_addr = ecdt_ptr->data.address;
881 boot_ec->gpe = ecdt_ptr->gpe;
882 boot_ec->handle = ACPI_ROOT_OBJECT;
883 } else {
884 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
885 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
886 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400887 /* Check that acpi_get_devices actually find something */
888 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400889 goto error;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300892 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300893 if (!ret) {
894 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300895 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300896 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300897 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300898 kfree(boot_ec);
899 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -ENODEV;
901}
902
Len Brown50526df2005-08-11 17:32:05 -0400903static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Len Brown50526df2005-08-11 17:32:05 -0400905 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
911 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Now register the driver for the EC */
915 result = acpi_bus_register_driver(&acpi_ec_driver);
916 if (result < 0) {
917 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400918 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924subsys_initcall(acpi_ec_init);
925
926/* EC driver currently not unloadable */
927#if 0
Len Brown50526df2005-08-11 17:32:05 -0400928static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 acpi_bus_unregister_driver(&acpi_ec_driver);
932
933 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
934
Patrick Mocheld550d982006-06-27 00:41:40 -0400935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400937#endif /* 0 */