blob: 9cd7997312e044df40d2e5d561a9322eef2a5f6b [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);
429 list_add_tail(&handler->node, &ec->list);
430 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);
444 break;
445 }
446 }
447 mutex_unlock(&ec->lock);
448}
449
450EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Len Brown50526df2005-08-11 17:32:05 -0400452static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300454 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400455 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400456 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400457
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300458 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300459 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400460 mutex_lock(&ec->lock);
461 list_for_each_entry(handler, &ec->list, node) {
462 if (value == handler->query_bit) {
463 /* have custom handler for this bit */
464 memcpy(&copy, handler, sizeof(copy));
465 mutex_unlock(&ec->lock);
466 if (copy.func) {
467 copy.func(copy.data);
468 } else if (copy.handle) {
469 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
470 }
471 return;
472 }
473 }
474 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Len Brown50526df2005-08-11 17:32:05 -0400477static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Len Brown50526df2005-08-11 17:32:05 -0400479 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400480 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300481 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200482
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500483 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300484
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400485 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300486 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500487 }
488
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300489 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300490 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
491 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300492 status =
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400493 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400494 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300495
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500496 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400497 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/* --------------------------------------------------------------------------
501 Address Space Management
502 -------------------------------------------------------------------------- */
503
504static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400505acpi_ec_space_setup(acpi_handle region_handle,
506 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 /*
509 * The EC object is in the handler context and is needed
510 * when calling the acpi_ec_space_handler.
511 */
Len Brown50526df2005-08-11 17:32:05 -0400512 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
513 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return AE_OK;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400519acpi_ec_space_handler(u32 function, acpi_physical_address address,
520 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400521 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300523 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400524 int result = 0, i = 0;
525 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400528 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400530 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400531 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400532
533 if (bits != 8 && acpi_strict)
534 return AE_BAD_PARAMETER;
535
536 while (bits - i > 0) {
537 if (function == ACPI_READ) {
538 result = acpi_ec_read(ec, address, &temp);
539 (*value) |= ((acpi_integer)temp) << i;
540 } else {
541 temp = 0xff & ((*value) >> i);
542 result = acpi_ec_write(ec, address, temp);
543 }
544 i += 8;
545 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 switch (result) {
549 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400556 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* --------------------------------------------------------------------------
564 FS Interface (/proc)
565 -------------------------------------------------------------------------- */
566
Len Brown50526df2005-08-11 17:32:05 -0400567static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Len Brown50526df2005-08-11 17:32:05 -0400569static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300571 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!ec)
574 goto end;
575
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300576 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
577 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
578 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
579 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400580 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400581 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
586{
587 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
588}
589
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400590static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400591 .open = acpi_ec_info_open_fs,
592 .read = seq_read,
593 .llseek = seq_lseek,
594 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .owner = THIS_MODULE,
596};
597
Len Brown50526df2005-08-11 17:32:05 -0400598static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Len Brown50526df2005-08-11 17:32:05 -0400600 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!acpi_device_dir(device)) {
603 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400604 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400610 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else {
614 entry->proc_fops = &acpi_ec_info_ops;
615 entry->data = acpi_driver_data(device);
616 entry->owner = THIS_MODULE;
617 }
618
Patrick Mocheld550d982006-06-27 00:41:40 -0400619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Len Brown50526df2005-08-11 17:32:05 -0400622static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (acpi_device_dir(device)) {
626 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
627 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
628 acpi_device_dir(device) = NULL;
629 }
630
Patrick Mocheld550d982006-06-27 00:41:40 -0400631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/* --------------------------------------------------------------------------
635 Driver Interface
636 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300637static acpi_status
638ec_parse_io_ports(struct acpi_resource *resource, void *context);
639
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300640static struct acpi_ec *make_acpi_ec(void)
641{
642 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
643 if (!ec)
644 return NULL;
645
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300646 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647 atomic_set(&ec->event_count, 1);
648 mutex_init(&ec->lock);
649 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400650 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300651
652 return ec;
653}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400655static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400656acpi_ec_register_query_methods(acpi_handle handle, u32 level,
657 void *context, void **return_value)
658{
659 struct acpi_namespace_node *node = handle;
660 struct acpi_ec *ec = context;
661 int value = 0;
662 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
663 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
664 }
665 return AE_OK;
666}
667
668static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400669ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400670{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400671 acpi_status status;
672
673 struct acpi_ec *ec = context;
674 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
675 ec_parse_io_ports, ec);
676 if (ACPI_FAILURE(status))
677 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400678
679 /* Get GPE bit assignment (EC events). */
680 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400681 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
682 if (ACPI_FAILURE(status))
683 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400684 /* Find and register all query methods */
685 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
686 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400687 /* Use the global lock for all EC transactions? */
688 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400689 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400690 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400691}
692
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400693static void ec_remove_handlers(struct acpi_ec *ec)
694{
695 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
696 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
697 printk(KERN_ERR PREFIX "failed to remove space handler\n");
698 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
699 &acpi_ec_gpe_handler)))
700 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
701 ec->handlers_installed = 0;
702}
703
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400704static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400706 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300710 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
711 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
712
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400713 /* Check for boot EC */
714 if (boot_ec) {
715 if (boot_ec->handle == device->handle) {
716 /* Pre-loaded EC from DSDT, just move pointer */
717 ec = boot_ec;
718 boot_ec = NULL;
719 goto end;
720 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
721 /* ECDT-based EC, time to shut it down */
722 ec_remove_handlers(boot_ec);
723 kfree(boot_ec);
724 first_ec = boot_ec = NULL;
725 }
726 }
727
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300728 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400732 if (ec_parse_device(device->handle, 0, ec, NULL) !=
733 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300734 kfree(ec);
735 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400736 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300737 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400738 end:
739 if (!first_ec)
740 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300742 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400743 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
744 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Len Brown50526df2005-08-11 17:32:05 -0400748static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300750 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200751 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400757 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200758 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400759 list_del(&handler->node);
760 kfree(handler);
761 }
762 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300764 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300765 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300766 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400767 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300772ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300774 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300776 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /*
780 * The first address region returned is the data port, and
781 * the second address region returned is the status/command
782 * port.
783 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300784 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400785 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300786 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400787 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300788 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return AE_OK;
792}
793
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300794static int ec_install_handlers(struct acpi_ec *ec)
795{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300796 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400797 if (ec->handlers_installed)
798 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300799 status = acpi_install_gpe_handler(NULL, ec->gpe,
800 ACPI_GPE_EDGE_TRIGGERED,
801 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300802 if (ACPI_FAILURE(status))
803 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300804
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300805 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
806 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
807
808 status = acpi_install_address_space_handler(ec->handle,
809 ACPI_ADR_SPACE_EC,
810 &acpi_ec_space_handler,
811 &acpi_ec_space_setup, ec);
812 if (ACPI_FAILURE(status)) {
813 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
814 return -ENODEV;
815 }
816
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400817 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300818 return 0;
819}
820
Len Brown50526df2005-08-11 17:32:05 -0400821static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300823 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400824 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 ec = acpi_driver_data(device);
830
831 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400834 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300835
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400836 /* EC is fully operational, allow queries */
837 atomic_set(&ec->query_pending, 0);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Len Brown50526df2005-08-11 17:32:05 -0400841static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300843 struct acpi_ec *ec;
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 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300847 if (!ec)
848 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400849 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400850
Patrick Mocheld550d982006-06-27 00:41:40 -0400851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Len Brown50526df2005-08-11 17:32:05 -0400854int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Len Brown50526df2005-08-11 17:32:05 -0400856 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300857 acpi_status status;
858 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300860 boot_ec = make_acpi_ec();
861 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300862 return -ENOMEM;
863 /*
864 * Generate a boot ec context
865 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300866 status = acpi_get_table(ACPI_SIG_ECDT, 1,
867 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400868 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400869 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400870 boot_ec->command_addr = ecdt_ptr->control.address;
871 boot_ec->data_addr = ecdt_ptr->data.address;
872 boot_ec->gpe = ecdt_ptr->gpe;
873 boot_ec->handle = ACPI_ROOT_OBJECT;
874 } else {
875 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
876 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
877 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400878 /* Check that acpi_get_devices actually find something */
879 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400880 goto error;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300883 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300884 if (!ret) {
885 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300886 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300887 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300888 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300889 kfree(boot_ec);
890 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENODEV;
892}
893
Len Brown50526df2005-08-11 17:32:05 -0400894static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Len Brown50526df2005-08-11 17:32:05 -0400896 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
902 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* Now register the driver for the EC */
906 result = acpi_bus_register_driver(&acpi_ec_driver);
907 if (result < 0) {
908 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400909 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915subsys_initcall(acpi_ec_init);
916
917/* EC driver currently not unloadable */
918#if 0
Len Brown50526df2005-08-11 17:32:05 -0400919static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 acpi_bus_unregister_driver(&acpi_ec_driver);
923
924 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
925
Patrick Mocheld550d982006-06-27 00:41:40 -0400926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
Len Brown50526df2005-08-11 17:32:05 -0400928#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Len Brown02b28a32005-12-05 16:33:04 -0500930static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400931{
Len Brown02b28a32005-12-05 16:33:04 -0500932 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400933
Len Brown02b28a32005-12-05 16:33:04 -0500934 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400935 return 0;
936
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300937 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
938
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500939 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400940
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800941 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400942}
Len Brown50526df2005-08-11 17:32:05 -0400943
Len Brown53f11d42005-12-05 16:46:36 -0500944__setup("ec_intr=", acpi_ec_set_intr_mode);