blob: 7b4178393e34856e8b6525a0c476a99a37b6962e [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 Starikovskiy6ccedb12006-12-07 18:42:17 +030068 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 Starikovskiy3261ff42006-12-07 18:42:17 +030074static enum ec_mode {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030075 EC_INTR = 1, /* Output buffer full */
76 EC_POLL, /* Input buffer empty */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030077} acpi_ec_mode = EC_INTR;
Denis M. Sadykov703959d2006-09-26 19:50:33 +040078
Len Brown50526df2005-08-11 17:32:05 -040079static int acpi_ec_remove(struct acpi_device *device, int type);
80static int acpi_ec_start(struct acpi_device *device);
81static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040082static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Thomas Renninger1ba90e32007-07-23 14:44:41 +020084static const struct acpi_device_id ec_device_ids[] = {
85 {"PNP0C09", 0},
86 {"", 0},
87};
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct acpi_driver acpi_ec_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050090 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040091 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020092 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040093 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040094 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040095 .remove = acpi_ec_remove,
96 .start = acpi_ec_start,
97 .stop = acpi_ec_stop,
98 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400100
101/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300102/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400103typedef int (*acpi_ec_query_func) (void *data);
104
105struct acpi_ec_query_handler {
106 struct list_head node;
107 acpi_ec_query_func func;
108 acpi_handle handle;
109 void *data;
110 u8 query_bit;
111};
112
Adrian Bunka854e082006-12-19 12:56:12 -0800113static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400114 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300115 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400116 unsigned long command_addr;
117 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300119 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300120 atomic_t query_pending;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500121 atomic_t event_count;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400122 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400123 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400124 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300125} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* --------------------------------------------------------------------------
128 Transaction Management
129 -------------------------------------------------------------------------- */
130
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400131static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400133 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400136static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400137{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400138 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400139}
140
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400141static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400142{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400143 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400144}
145
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400146static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400147{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400148 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400149}
150
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500151static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
152 unsigned old_count)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300154 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500155 if (old_count == atomic_read(&ec->event_count))
156 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300157 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400158 if (status & ACPI_EC_FLAG_OBF)
159 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300160 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 if (!(status & ACPI_EC_FLAG_IBF))
162 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400163 }
164
165 return 0;
166}
167
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200168static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
169 unsigned count, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400170{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200171 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300172 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
173 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500174 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400175 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400176 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300177 } else {
178 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500179 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300180 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500181 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400182 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300183 } else {
184 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
185 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300186 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400187 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300188 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400189
Patrick Mocheld550d982006-06-27 00:41:40 -0400190 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500191}
192
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400193static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300194 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200195 u8 * rdata, unsigned rdata_len,
196 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400197{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300198 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500199 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400200 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400201
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300202 for (; wdata_len > 0; --wdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200203 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300204 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300205 printk(KERN_ERR PREFIX
206 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300207 goto end;
208 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500209 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400210 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400211 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400212
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300213 if (!rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200214 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300215 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300216 printk(KERN_ERR PREFIX
217 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300218 goto end;
219 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300220 } else if (command == ACPI_EC_COMMAND_QUERY) {
221 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400222 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400223
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300224 for (; rdata_len > 0; --rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200225 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300226 if (result) {
227 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300228 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300229 goto end;
230 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500231 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400232 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400233 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300234 end:
235 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400236}
237
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400238static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300239 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200240 u8 * rdata, unsigned rdata_len,
241 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400242{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400243 int status;
Len Brown50526df2005-08-11 17:32:05 -0400244 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400246 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300249 if (rdata)
250 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300252 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400253 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300255 if (ACPI_FAILURE(status)) {
256 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500260
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300261 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300262 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300263
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200264 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400265 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400266 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300267 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500268 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300271 status = acpi_ec_transaction_unlocked(ec, command,
272 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200273 rdata, rdata_len,
274 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400275
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300276 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400278 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300280 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300285/*
286 * Note: samsung nv5000 doesn't work with ec burst mode.
287 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
288 */
289int acpi_ec_burst_enable(struct acpi_ec *ec)
290{
291 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200292 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300293}
294
295int acpi_ec_burst_disable(struct acpi_ec *ec)
296{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200297 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300298}
299
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300300static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400301{
302 int result;
303 u8 d;
304
305 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200306 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400307 *data = d;
308 return result;
309}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400310
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400311static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
312{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300313 u8 wdata[2] = { address, data };
314 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200315 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Externally callable EC access functions. For now, assume 1 EC only
320 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300321int ec_burst_enable(void)
322{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300323 if (!first_ec)
324 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300325 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300326}
327
328EXPORT_SYMBOL(ec_burst_enable);
329
330int ec_burst_disable(void)
331{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300332 if (!first_ec)
333 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300334 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300335}
336
337EXPORT_SYMBOL(ec_burst_disable);
338
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300339int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400342 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!first_ec)
345 return -ENODEV;
346
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300347 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!err) {
350 *val = temp_data;
351 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400352 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
Len Brown50526df2005-08-11 17:32:05 -0400355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356EXPORT_SYMBOL(ec_read);
357
Len Brown50526df2005-08-11 17:32:05 -0400358int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int err;
361
362 if (!first_ec)
363 return -ENODEV;
364
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300365 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 return err;
368}
Len Brown50526df2005-08-11 17:32:05 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370EXPORT_SYMBOL(ec_write);
371
Randy Dunlap616362d2006-10-27 01:47:34 -0400372int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500373 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200374 u8 * rdata, unsigned rdata_len,
375 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400376{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400377 if (!first_ec)
378 return -ENODEV;
379
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300380 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200381 wdata_len, rdata, rdata_len,
382 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400383}
Luming Yu45bea152005-07-23 04:08:00 -0400384
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400385EXPORT_SYMBOL(ec_transaction);
386
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400388{
389 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300390 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392 if (!ec || !data)
393 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400394
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300395 /*
396 * Query the EC to find out which _Qxx method we need to evaluate.
397 * Note that successful completion of the query causes the ACPI_EC_SCI
398 * bit to be cleared (and thus clearing the interrupt source).
399 */
Luming Yu45bea152005-07-23 04:08:00 -0400400
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200401 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300402 if (result)
403 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400404
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300405 if (!d)
406 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400407
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300408 *data = d;
409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/* --------------------------------------------------------------------------
413 Event Management
414 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400415int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
416 acpi_handle handle, acpi_ec_query_func func,
417 void *data)
418{
419 struct acpi_ec_query_handler *handler =
420 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
421 if (!handler)
422 return -ENOMEM;
423
424 handler->query_bit = query_bit;
425 handler->handle = handle;
426 handler->func = func;
427 handler->data = data;
428 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400429 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400430 mutex_unlock(&ec->lock);
431 return 0;
432}
433
434EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
435
436void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
437{
438 struct acpi_ec_query_handler *handler;
439 mutex_lock(&ec->lock);
440 list_for_each_entry(handler, &ec->list, node) {
441 if (query_bit == handler->query_bit) {
442 list_del(&handler->node);
443 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400444 }
445 }
446 mutex_unlock(&ec->lock);
447}
448
449EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Len Brown50526df2005-08-11 17:32:05 -0400451static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300453 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400454 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400455 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400456
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300457 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300458 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400459 mutex_lock(&ec->lock);
460 list_for_each_entry(handler, &ec->list, node) {
461 if (value == handler->query_bit) {
462 /* have custom handler for this bit */
463 memcpy(&copy, handler, sizeof(copy));
464 mutex_unlock(&ec->lock);
465 if (copy.func) {
466 copy.func(copy.data);
467 } else if (copy.handle) {
468 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
469 }
470 return;
471 }
472 }
473 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown50526df2005-08-11 17:32:05 -0400476static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Len Brown50526df2005-08-11 17:32:05 -0400478 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400479 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300480 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200481
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500482 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300483
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400484 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300485 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500486 }
487
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300488 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300489 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
490 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300491 status =
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400492 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400493 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300494
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500495 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400496 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/* --------------------------------------------------------------------------
500 Address Space Management
501 -------------------------------------------------------------------------- */
502
503static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400504acpi_ec_space_setup(acpi_handle region_handle,
505 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 /*
508 * The EC object is in the handler context and is needed
509 * when calling the acpi_ec_space_handler.
510 */
Len Brown50526df2005-08-11 17:32:05 -0400511 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
512 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 return AE_OK;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400518acpi_ec_space_handler(u32 function, acpi_physical_address address,
519 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400520 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300522 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400523 int result = 0, i = 0;
524 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400529 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400530 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400531
532 if (bits != 8 && acpi_strict)
533 return AE_BAD_PARAMETER;
534
535 while (bits - i > 0) {
536 if (function == ACPI_READ) {
537 result = acpi_ec_read(ec, address, &temp);
538 (*value) |= ((acpi_integer)temp) << i;
539 } else {
540 temp = 0xff & ((*value) >> i);
541 result = acpi_ec_write(ec, address, temp);
542 }
543 i += 8;
544 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 switch (result) {
548 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/* --------------------------------------------------------------------------
563 FS Interface (/proc)
564 -------------------------------------------------------------------------- */
565
Len Brown50526df2005-08-11 17:32:05 -0400566static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Len Brown50526df2005-08-11 17:32:05 -0400568static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300570 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!ec)
573 goto end;
574
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300575 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
576 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
577 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
578 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400579 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400580 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
585{
586 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
587}
588
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400589static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400590 .open = acpi_ec_info_open_fs,
591 .read = seq_read,
592 .llseek = seq_lseek,
593 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .owner = THIS_MODULE,
595};
596
Len Brown50526df2005-08-11 17:32:05 -0400597static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Len Brown50526df2005-08-11 17:32:05 -0400599 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!acpi_device_dir(device)) {
602 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400603 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400609 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else {
613 entry->proc_fops = &acpi_ec_info_ops;
614 entry->data = acpi_driver_data(device);
615 entry->owner = THIS_MODULE;
616 }
617
Patrick Mocheld550d982006-06-27 00:41:40 -0400618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Len Brown50526df2005-08-11 17:32:05 -0400621static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (acpi_device_dir(device)) {
625 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
626 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
627 acpi_device_dir(device) = NULL;
628 }
629
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/* --------------------------------------------------------------------------
634 Driver Interface
635 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300636static acpi_status
637ec_parse_io_ports(struct acpi_resource *resource, void *context);
638
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300639static struct acpi_ec *make_acpi_ec(void)
640{
641 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
642 if (!ec)
643 return NULL;
644
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300645 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300646 atomic_set(&ec->event_count, 1);
647 mutex_init(&ec->lock);
648 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400649 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300650
651 return ec;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400654static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400655acpi_ec_register_query_methods(acpi_handle handle, u32 level,
656 void *context, void **return_value)
657{
658 struct acpi_namespace_node *node = handle;
659 struct acpi_ec *ec = context;
660 int value = 0;
661 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
662 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
663 }
664 return AE_OK;
665}
666
667static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400668ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400669{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400670 acpi_status status;
671
672 struct acpi_ec *ec = context;
673 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
674 ec_parse_io_ports, ec);
675 if (ACPI_FAILURE(status))
676 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400677
678 /* Get GPE bit assignment (EC events). */
679 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400680 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
681 if (ACPI_FAILURE(status))
682 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400683 /* Find and register all query methods */
684 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
685 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400686 /* Use the global lock for all EC transactions? */
687 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400688 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400689 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400690}
691
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400692static void ec_remove_handlers(struct acpi_ec *ec)
693{
694 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
695 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
696 printk(KERN_ERR PREFIX "failed to remove space handler\n");
697 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
698 &acpi_ec_gpe_handler)))
699 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
700 ec->handlers_installed = 0;
701}
702
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400703static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400705 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400708 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300709 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
710 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
711
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400712 /* Check for boot EC */
713 if (boot_ec) {
714 if (boot_ec->handle == device->handle) {
715 /* Pre-loaded EC from DSDT, just move pointer */
716 ec = boot_ec;
717 boot_ec = NULL;
718 goto end;
719 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
720 /* ECDT-based EC, time to shut it down */
721 ec_remove_handlers(boot_ec);
722 kfree(boot_ec);
723 first_ec = boot_ec = NULL;
724 }
725 }
726
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300727 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400729 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400731 if (ec_parse_device(device->handle, 0, ec, NULL) !=
732 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300733 kfree(ec);
734 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400735 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300736 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400737 end:
738 if (!first_ec)
739 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400742 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
743 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Len Brown50526df2005-08-11 17:32:05 -0400747static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300749 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200750 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400756 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200757 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400758 list_del(&handler->node);
759 kfree(handler);
760 }
761 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300763 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300764 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300765 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400766 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300771ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300773 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300775 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /*
779 * The first address region returned is the data port, and
780 * the second address region returned is the status/command
781 * port.
782 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300783 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400784 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300785 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400786 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300787 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return AE_OK;
791}
792
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300793static int ec_install_handlers(struct acpi_ec *ec)
794{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300795 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400796 if (ec->handlers_installed)
797 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300798 status = acpi_install_gpe_handler(NULL, ec->gpe,
799 ACPI_GPE_EDGE_TRIGGERED,
800 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300801 if (ACPI_FAILURE(status))
802 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300803
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300804 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
805 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
806
807 status = acpi_install_address_space_handler(ec->handle,
808 ACPI_ADR_SPACE_EC,
809 &acpi_ec_space_handler,
810 &acpi_ec_space_setup, ec);
811 if (ACPI_FAILURE(status)) {
812 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
813 return -ENODEV;
814 }
815
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400816 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300817 return 0;
818}
819
Len Brown50526df2005-08-11 17:32:05 -0400820static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300822 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400823 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 ec = acpi_driver_data(device);
829
830 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400831 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400833 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300834
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400835 /* EC is fully operational, allow queries */
836 atomic_set(&ec->query_pending, 0);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Len Brown50526df2005-08-11 17:32:05 -0400840static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300842 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400844 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300846 if (!ec)
847 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400848 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400849
Patrick Mocheld550d982006-06-27 00:41:40 -0400850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Len Brown50526df2005-08-11 17:32:05 -0400853int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Len Brown50526df2005-08-11 17:32:05 -0400855 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300856 acpi_status status;
857 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300859 boot_ec = make_acpi_ec();
860 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300861 return -ENOMEM;
862 /*
863 * Generate a boot ec context
864 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300865 status = acpi_get_table(ACPI_SIG_ECDT, 1,
866 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400867 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400868 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400869 boot_ec->command_addr = ecdt_ptr->control.address;
870 boot_ec->data_addr = ecdt_ptr->data.address;
871 boot_ec->gpe = ecdt_ptr->gpe;
872 boot_ec->handle = ACPI_ROOT_OBJECT;
873 } else {
874 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
875 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
876 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400877 /* Check that acpi_get_devices actually find something */
878 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400879 goto error;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300882 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300883 if (!ret) {
884 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300885 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300886 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300887 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300888 kfree(boot_ec);
889 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -ENODEV;
891}
892
Len Brown50526df2005-08-11 17:32:05 -0400893static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Len Brown50526df2005-08-11 17:32:05 -0400895 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
901 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* Now register the driver for the EC */
905 result = acpi_bus_register_driver(&acpi_ec_driver);
906 if (result < 0) {
907 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400908 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Patrick Mocheld550d982006-06-27 00:41:40 -0400911 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914subsys_initcall(acpi_ec_init);
915
916/* EC driver currently not unloadable */
917#if 0
Len Brown50526df2005-08-11 17:32:05 -0400918static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 acpi_bus_unregister_driver(&acpi_ec_driver);
922
923 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
924
Patrick Mocheld550d982006-06-27 00:41:40 -0400925 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
Len Brown50526df2005-08-11 17:32:05 -0400927#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Len Brown02b28a32005-12-05 16:33:04 -0500929static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400930{
Len Brown02b28a32005-12-05 16:33:04 -0500931 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400932
Len Brown02b28a32005-12-05 16:33:04 -0500933 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400934 return 0;
935
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300936 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
937
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500938 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400939
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800940 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400941}
Len Brown50526df2005-08-11 17:32:05 -0400942
Len Brown53f11d42005-12-05 16:46:36 -0500943__setup("ec_intr=", acpi_ec_set_intr_mode);