blob: 390f8f8666daee289e7d7c76979ffd228056b00f [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 Starikovskiyf2d68932007-11-19 01:37:03 +030078 EC_FLAGS_NO_ADDRESS_GPE, /* Expect GPE only for non-address event */
79 EC_FLAGS_ADDRESS, /* Address is being written */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040080};
81
Len Brown50526df2005-08-11 17:32:05 -040082static int acpi_ec_remove(struct acpi_device *device, int type);
83static int acpi_ec_start(struct acpi_device *device);
84static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040085static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Renninger1ba90e32007-07-23 14:44:41 +020087static const struct acpi_device_id ec_device_ids[] = {
88 {"PNP0C09", 0},
89 {"", 0},
90};
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050093 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040094 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020095 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040096 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040097 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040098 .remove = acpi_ec_remove,
99 .start = acpi_ec_start,
100 .stop = acpi_ec_stop,
101 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400103
104/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300105/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400106typedef int (*acpi_ec_query_func) (void *data);
107
108struct acpi_ec_query_handler {
109 struct list_head node;
110 acpi_ec_query_func func;
111 acpi_handle handle;
112 void *data;
113 u8 query_bit;
114};
115
Adrian Bunka854e082006-12-19 12:56:12 -0800116static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400117 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300118 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400119 unsigned long command_addr;
120 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400121 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400122 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300123 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400124 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400125 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400126 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300127} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* --------------------------------------------------------------------------
130 Transaction Management
131 -------------------------------------------------------------------------- */
132
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400133static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400138static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400139{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400140 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400141}
142
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400143static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400144{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400145 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400146}
147
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400148static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400149{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400150 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400151}
152
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400153static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400154{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400155 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500156 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300157 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400158 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400159 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300160 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400161 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400162 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400163 }
164
165 return 0;
166}
167
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400168static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400169{
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300170 int ret = 0;
171 if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) &&
172 test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags)))
173 force_poll = 1;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400174 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
175 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400176 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
177 msecs_to_jiffies(ACPI_EC_DELAY)))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300178 goto end;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400179 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
180 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300181 if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
182 /* miss address GPE, don't expect it anymore */
183 printk(KERN_INFO PREFIX "missing address confirmation,"
184 "don't expect it any longer.\n");
185 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400186 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400187 /* missing GPEs, switch back to poll mode */
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300188 printk(KERN_INFO PREFIX "missing confirmations,"
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400189 "switch off interrupt mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400190 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400191 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300192 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400193 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400194 } else {
195 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
196 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
197 while (time_before(jiffies, delay)) {
198 if (acpi_ec_check_status(ec, event))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300199 goto end;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400200 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300201 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400202 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
203 " status = %d, expect_event = %d\n",
204 acpi_ec_read_status(ec), event);
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300205 ret = -ETIME;
206 end:
207 clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
208 return ret;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500209}
210
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400211static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300212 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200213 u8 * rdata, unsigned rdata_len,
214 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400215{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300216 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400217 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400218 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400219
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300220 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400221 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300222 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300223 printk(KERN_ERR PREFIX
224 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300225 goto end;
226 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300227 /* mark the address byte written to EC */
228 if (rdata_len + wdata_len > 1)
229 set_bit(EC_FLAGS_ADDRESS, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400230 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400231 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400232 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400233
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300234 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400235 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300236 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300237 printk(KERN_ERR PREFIX
238 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300239 goto end;
240 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400241 } else if (command == ACPI_EC_COMMAND_QUERY)
242 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400243
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300244 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400245 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300246 if (result) {
247 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300248 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:
257 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400258}
259
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400260static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300261 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200262 u8 * rdata, unsigned rdata_len,
263 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400264{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400265 int status;
Len Brown50526df2005-08-11 17:32:05 -0400266 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400268 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300271 if (rdata)
272 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300274 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400275 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300277 if (ACPI_FAILURE(status)) {
278 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400279 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500282
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400283 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400284 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400285 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300286 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500287 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300290 status = acpi_ec_transaction_unlocked(ec, command,
291 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200292 rdata, rdata_len,
293 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400294
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300295 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400297 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300299 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300304/*
305 * Note: samsung nv5000 doesn't work with ec burst mode.
306 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
307 */
308int acpi_ec_burst_enable(struct acpi_ec *ec)
309{
310 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200311 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300312}
313
314int acpi_ec_burst_disable(struct acpi_ec *ec)
315{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200316 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300317}
318
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300319static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400320{
321 int result;
322 u8 d;
323
324 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200325 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400326 *data = d;
327 return result;
328}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400329
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400330static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
331{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300332 u8 wdata[2] = { address, data };
333 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200334 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * Externally callable EC access functions. For now, assume 1 EC only
339 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300340int ec_burst_enable(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_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300345}
346
347EXPORT_SYMBOL(ec_burst_enable);
348
349int ec_burst_disable(void)
350{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300351 if (!first_ec)
352 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300353 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300354}
355
356EXPORT_SYMBOL(ec_burst_disable);
357
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300358int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400361 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!first_ec)
364 return -ENODEV;
365
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300366 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!err) {
369 *val = temp_data;
370 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400371 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return err;
373}
Len Brown50526df2005-08-11 17:32:05 -0400374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375EXPORT_SYMBOL(ec_read);
376
Len Brown50526df2005-08-11 17:32:05 -0400377int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int err;
380
381 if (!first_ec)
382 return -ENODEV;
383
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300384 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 return err;
387}
Len Brown50526df2005-08-11 17:32:05 -0400388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389EXPORT_SYMBOL(ec_write);
390
Randy Dunlap616362d2006-10-27 01:47:34 -0400391int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500392 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200393 u8 * rdata, unsigned rdata_len,
394 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400395{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400396 if (!first_ec)
397 return -ENODEV;
398
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300399 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200400 wdata_len, rdata, rdata_len,
401 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400402}
Luming Yu45bea152005-07-23 04:08:00 -0400403
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400404EXPORT_SYMBOL(ec_transaction);
405
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300406static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400407{
408 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300409 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400410
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300411 if (!ec || !data)
412 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400413
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300414 /*
415 * Query the EC to find out which _Qxx method we need to evaluate.
416 * Note that successful completion of the query causes the ACPI_EC_SCI
417 * bit to be cleared (and thus clearing the interrupt source).
418 */
Luming Yu45bea152005-07-23 04:08:00 -0400419
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200420 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300421 if (result)
422 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400423
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300424 if (!d)
425 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400426
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300427 *data = d;
428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/* --------------------------------------------------------------------------
432 Event Management
433 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400434int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
435 acpi_handle handle, acpi_ec_query_func func,
436 void *data)
437{
438 struct acpi_ec_query_handler *handler =
439 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
440 if (!handler)
441 return -ENOMEM;
442
443 handler->query_bit = query_bit;
444 handler->handle = handle;
445 handler->func = func;
446 handler->data = data;
447 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400448 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400449 mutex_unlock(&ec->lock);
450 return 0;
451}
452
453EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
454
455void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
456{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200457 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400458 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200459 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400460 if (query_bit == handler->query_bit) {
461 list_del(&handler->node);
462 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400463 }
464 }
465 mutex_unlock(&ec->lock);
466}
467
468EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Len Brown50526df2005-08-11 17:32:05 -0400470static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300472 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400473 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400474 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400475
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300476 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300477 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400478 mutex_lock(&ec->lock);
479 list_for_each_entry(handler, &ec->list, node) {
480 if (value == handler->query_bit) {
481 /* have custom handler for this bit */
482 memcpy(&copy, handler, sizeof(copy));
483 mutex_unlock(&ec->lock);
484 if (copy.func) {
485 copy.func(copy.data);
486 } else if (copy.handle) {
487 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
488 }
489 return;
490 }
491 }
492 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Len Brown50526df2005-08-11 17:32:05 -0400495static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Len Brown50526df2005-08-11 17:32:05 -0400497 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300498 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200499
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400500 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400501 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300502 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500503
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400504 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
505 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
506 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
507 acpi_ec_gpe_query, ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400508 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
509 /* this is non-query, must be confirmation */
510 printk(KERN_INFO PREFIX "non-query interrupt received,"
511 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400512 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400513 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300514
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400515 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400516 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/* --------------------------------------------------------------------------
520 Address Space Management
521 -------------------------------------------------------------------------- */
522
523static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400524acpi_ec_space_setup(acpi_handle region_handle,
525 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 /*
528 * The EC object is in the handler context and is needed
529 * when calling the acpi_ec_space_handler.
530 */
Len Brown50526df2005-08-11 17:32:05 -0400531 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
532 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 return AE_OK;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400538acpi_ec_space_handler(u32 function, acpi_physical_address address,
539 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400540 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300542 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400543 int result = 0, i = 0;
544 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400547 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400549 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400551
552 if (bits != 8 && acpi_strict)
553 return AE_BAD_PARAMETER;
554
555 while (bits - i > 0) {
556 if (function == ACPI_READ) {
557 result = acpi_ec_read(ec, address, &temp);
558 (*value) |= ((acpi_integer)temp) << i;
559 } else {
560 temp = 0xff & ((*value) >> i);
561 result = acpi_ec_write(ec, address, temp);
562 }
563 i += 8;
564 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 switch (result) {
568 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/* --------------------------------------------------------------------------
583 FS Interface (/proc)
584 -------------------------------------------------------------------------- */
585
Len Brown50526df2005-08-11 17:32:05 -0400586static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Len Brown50526df2005-08-11 17:32:05 -0400588static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300590 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!ec)
593 goto end;
594
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300595 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
596 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
597 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
598 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400599 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400600 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
605{
606 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
607}
608
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400609static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400610 .open = acpi_ec_info_open_fs,
611 .read = seq_read,
612 .llseek = seq_lseek,
613 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .owner = THIS_MODULE,
615};
616
Len Brown50526df2005-08-11 17:32:05 -0400617static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Len Brown50526df2005-08-11 17:32:05 -0400619 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!acpi_device_dir(device)) {
622 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400623 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400629 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400631 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 else {
633 entry->proc_fops = &acpi_ec_info_ops;
634 entry->data = acpi_driver_data(device);
635 entry->owner = THIS_MODULE;
636 }
637
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Len Brown50526df2005-08-11 17:32:05 -0400641static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (acpi_device_dir(device)) {
645 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
646 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
647 acpi_device_dir(device) = NULL;
648 }
649
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/* --------------------------------------------------------------------------
654 Driver Interface
655 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300656static acpi_status
657ec_parse_io_ports(struct acpi_resource *resource, void *context);
658
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300659static struct acpi_ec *make_acpi_ec(void)
660{
661 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
662 if (!ec)
663 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400664 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300665 mutex_init(&ec->lock);
666 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400667 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300668 return ec;
669}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400671static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400672acpi_ec_register_query_methods(acpi_handle handle, u32 level,
673 void *context, void **return_value)
674{
675 struct acpi_namespace_node *node = handle;
676 struct acpi_ec *ec = context;
677 int value = 0;
678 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
679 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
680 }
681 return AE_OK;
682}
683
684static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400685ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400686{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400687 acpi_status status;
688
689 struct acpi_ec *ec = context;
690 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
691 ec_parse_io_ports, ec);
692 if (ACPI_FAILURE(status))
693 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400694
695 /* Get GPE bit assignment (EC events). */
696 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400697 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
698 if (ACPI_FAILURE(status))
699 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400700 /* Find and register all query methods */
701 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
702 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400703 /* Use the global lock for all EC transactions? */
704 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400705 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400706 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400707}
708
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400709static void ec_remove_handlers(struct acpi_ec *ec)
710{
711 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
712 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
713 printk(KERN_ERR PREFIX "failed to remove space handler\n");
714 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
715 &acpi_ec_gpe_handler)))
716 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
717 ec->handlers_installed = 0;
718}
719
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400720static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400722 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400725 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
727 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
728
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400729 /* Check for boot EC */
730 if (boot_ec) {
731 if (boot_ec->handle == device->handle) {
732 /* Pre-loaded EC from DSDT, just move pointer */
733 ec = boot_ec;
734 boot_ec = NULL;
735 goto end;
736 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
737 /* ECDT-based EC, time to shut it down */
738 ec_remove_handlers(boot_ec);
739 kfree(boot_ec);
740 first_ec = boot_ec = NULL;
741 }
742 }
743
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300744 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400746 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400748 if (ec_parse_device(device->handle, 0, ec, NULL) !=
749 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300750 kfree(ec);
751 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400752 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300753 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400754 end:
755 if (!first_ec)
756 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300758 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400759 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
760 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400761 printk(KERN_INFO PREFIX "driver started in %s mode\n",
762 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Len Brown50526df2005-08-11 17:32:05 -0400766static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300768 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200769 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400775 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200776 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400777 list_del(&handler->node);
778 kfree(handler);
779 }
780 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300782 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300783 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300784 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400785 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300790ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300792 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300794 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * The first address region returned is the data port, and
799 * the second address region returned is the status/command
800 * port.
801 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300802 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400803 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300804 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400805 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300806 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return AE_OK;
810}
811
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300812static int ec_install_handlers(struct acpi_ec *ec)
813{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300814 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400815 if (ec->handlers_installed)
816 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300817 status = acpi_install_gpe_handler(NULL, ec->gpe,
818 ACPI_GPE_EDGE_TRIGGERED,
819 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300820 if (ACPI_FAILURE(status))
821 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300822
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300823 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
824 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
825
826 status = acpi_install_address_space_handler(ec->handle,
827 ACPI_ADR_SPACE_EC,
828 &acpi_ec_space_handler,
829 &acpi_ec_space_setup, ec);
830 if (ACPI_FAILURE(status)) {
831 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
832 return -ENODEV;
833 }
834
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400835 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300836 return 0;
837}
838
Len Brown50526df2005-08-11 17:32:05 -0400839static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300841 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400842 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 ec = acpi_driver_data(device);
848
849 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400852 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300853
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400854 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400855 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400856 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Len Brown50526df2005-08-11 17:32:05 -0400859static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300861 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300865 if (!ec)
866 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400867 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400868
Patrick Mocheld550d982006-06-27 00:41:40 -0400869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Len Brown50526df2005-08-11 17:32:05 -0400872int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Len Brown50526df2005-08-11 17:32:05 -0400874 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300875 acpi_status status;
876 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300878 boot_ec = make_acpi_ec();
879 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300880 return -ENOMEM;
881 /*
882 * Generate a boot ec context
883 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300884 status = acpi_get_table(ACPI_SIG_ECDT, 1,
885 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400886 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400887 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400888 boot_ec->command_addr = ecdt_ptr->control.address;
889 boot_ec->data_addr = ecdt_ptr->data.address;
890 boot_ec->gpe = ecdt_ptr->gpe;
891 boot_ec->handle = ACPI_ROOT_OBJECT;
892 } else {
893 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
894 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
895 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400896 /* Check that acpi_get_devices actually find something */
897 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400898 goto error;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300901 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300902 if (!ret) {
903 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300904 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300905 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300906 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300907 kfree(boot_ec);
908 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ENODEV;
910}
911
Len Brown50526df2005-08-11 17:32:05 -0400912static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Len Brown50526df2005-08-11 17:32:05 -0400914 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
920 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Now register the driver for the EC */
924 result = acpi_bus_register_driver(&acpi_ec_driver);
925 if (result < 0) {
926 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933subsys_initcall(acpi_ec_init);
934
935/* EC driver currently not unloadable */
936#if 0
Len Brown50526df2005-08-11 17:32:05 -0400937static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 acpi_bus_unregister_driver(&acpi_ec_driver);
941
942 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
943
Patrick Mocheld550d982006-06-27 00:41:40 -0400944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400946#endif /* 0 */