blob: 202db575d5db96f3913fcc201e5c79986b7d9c35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03004 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 */
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
33#include <linux/delay.h>
34#include <linux/proc_fs.h>
35#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050036#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040037#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/io.h>
39#include <acpi/acpi_bus.h>
40#include <acpi/acpi_drivers.h>
41#include <acpi/actypes.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_EC_DEVICE_NAME "Embedded Controller"
45#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040046
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030047#undef PREFIX
48#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040049
Denis M. Sadykov703959d2006-09-26 19:50:33 +040050/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050053#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040055
Denis M. Sadykov703959d2006-09-26 19:50:33 +040056/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030057enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030058 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030063};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040064
Denis M. Sadykov703959d2006-09-26 19:50:33 +040065/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040067 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040068 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069};
70
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030071#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040073
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040074enum {
75 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
76 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy78439322007-10-22 14:18:43 +040077 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +040078 EC_FLAGS_ONLY_IBF_GPE, /* Expect GPE only for IBF = 0 event */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040079};
80
Len Brown50526df2005-08-11 17:32:05 -040081static int acpi_ec_remove(struct acpi_device *device, int type);
82static int acpi_ec_start(struct acpi_device *device);
83static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040084static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Renninger1ba90e32007-07-23 14:44:41 +020086static const struct acpi_device_id ec_device_ids[] = {
87 {"PNP0C09", 0},
88 {"", 0},
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static struct acpi_driver acpi_ec_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050092 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040093 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020094 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040095 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040096 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040097 .remove = acpi_ec_remove,
98 .start = acpi_ec_start,
99 .stop = acpi_ec_stop,
100 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400102
103/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300104/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400105typedef int (*acpi_ec_query_func) (void *data);
106
107struct acpi_ec_query_handler {
108 struct list_head node;
109 acpi_ec_query_func func;
110 acpi_handle handle;
111 void *data;
112 u8 query_bit;
113};
114
Adrian Bunka854e082006-12-19 12:56:12 -0800115static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400116 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300117 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400118 unsigned long command_addr;
119 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400120 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400121 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300122 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400124 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400125 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300126} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* --------------------------------------------------------------------------
129 Transaction Management
130 -------------------------------------------------------------------------- */
131
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400132static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400134 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400138{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400139 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400140}
141
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400143{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400144 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400145}
146
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400147static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400150}
151
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400152static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400154 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500155 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300156 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400157 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400158 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300159 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400160 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400162 }
163
164 return 0;
165}
166
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400167static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400168{
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400169 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
170 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400171 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
172 msecs_to_jiffies(ACPI_EC_DELAY)))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400174 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
175 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400176 if (event == ACPI_EC_EVENT_OBF_1)
177 /* miss OBF = 1 GPE, don't expect it anymore */
178 set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
179 else
180 /* missing GPEs, switch back to poll mode */
181 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400182 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400183 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400184 } else {
185 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
186 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
187 while (time_before(jiffies, delay)) {
188 if (acpi_ec_check_status(ec, event))
189 return 0;
190 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300191 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400192 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
193 " status = %d, expect_event = %d\n",
194 acpi_ec_read_status(ec), event);
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500196}
197
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400198static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300199 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200200 u8 * rdata, unsigned rdata_len,
201 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400202{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300203 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400204 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400205 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400206
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300207 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400208 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300209 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300210 printk(KERN_ERR PREFIX
211 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300212 goto end;
213 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400214 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400215 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400216 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400217
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300218 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400219 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300220 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300221 printk(KERN_ERR PREFIX
222 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300223 goto end;
224 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400225 } else if (command == ACPI_EC_COMMAND_QUERY)
226 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400227
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300228 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400229 if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
230 force_poll = 1;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400231 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300232 if (result) {
233 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300234 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300235 goto end;
236 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400237 /* Don't expect GPE after last read */
238 if (rdata_len > 1)
239 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400240 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400241 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300242 end:
243 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400244}
245
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400246static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300247 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200248 u8 * rdata, unsigned rdata_len,
249 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400250{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400251 int status;
Len Brown50526df2005-08-11 17:32:05 -0400252 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400254 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300257 if (rdata)
258 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300260 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400261 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300263 if (ACPI_FAILURE(status)) {
264 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500268
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400269 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400270 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400271 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300272 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500273 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300276 status = acpi_ec_transaction_unlocked(ec, command,
277 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200278 rdata, rdata_len,
279 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400280
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300281 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400283 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300285 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300290/*
291 * Note: samsung nv5000 doesn't work with ec burst mode.
292 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
293 */
294int acpi_ec_burst_enable(struct acpi_ec *ec)
295{
296 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200297 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300298}
299
300int acpi_ec_burst_disable(struct acpi_ec *ec)
301{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200302 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300303}
304
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300305static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400306{
307 int result;
308 u8 d;
309
310 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200311 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400312 *data = d;
313 return result;
314}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400315
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400316static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
317{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300318 u8 wdata[2] = { address, data };
319 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200320 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * Externally callable EC access functions. For now, assume 1 EC only
325 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300326int ec_burst_enable(void)
327{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300328 if (!first_ec)
329 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300330 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300331}
332
333EXPORT_SYMBOL(ec_burst_enable);
334
335int ec_burst_disable(void)
336{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300337 if (!first_ec)
338 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300339 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300340}
341
342EXPORT_SYMBOL(ec_burst_disable);
343
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300344int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400347 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!first_ec)
350 return -ENODEV;
351
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300352 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!err) {
355 *val = temp_data;
356 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400357 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return err;
359}
Len Brown50526df2005-08-11 17:32:05 -0400360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361EXPORT_SYMBOL(ec_read);
362
Len Brown50526df2005-08-11 17:32:05 -0400363int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int err;
366
367 if (!first_ec)
368 return -ENODEV;
369
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300370 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return err;
373}
Len Brown50526df2005-08-11 17:32:05 -0400374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375EXPORT_SYMBOL(ec_write);
376
Randy Dunlap616362d2006-10-27 01:47:34 -0400377int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500378 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200379 u8 * rdata, unsigned rdata_len,
380 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400381{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400382 if (!first_ec)
383 return -ENODEV;
384
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300385 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200386 wdata_len, rdata, rdata_len,
387 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400388}
Luming Yu45bea152005-07-23 04:08:00 -0400389
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400390EXPORT_SYMBOL(ec_transaction);
391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400393{
394 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300395 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400396
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397 if (!ec || !data)
398 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400399
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300400 /*
401 * Query the EC to find out which _Qxx method we need to evaluate.
402 * Note that successful completion of the query causes the ACPI_EC_SCI
403 * bit to be cleared (and thus clearing the interrupt source).
404 */
Luming Yu45bea152005-07-23 04:08:00 -0400405
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200406 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300407 if (result)
408 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400409
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300410 if (!d)
411 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400412
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300413 *data = d;
414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* --------------------------------------------------------------------------
418 Event Management
419 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400420int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
421 acpi_handle handle, acpi_ec_query_func func,
422 void *data)
423{
424 struct acpi_ec_query_handler *handler =
425 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
426 if (!handler)
427 return -ENOMEM;
428
429 handler->query_bit = query_bit;
430 handler->handle = handle;
431 handler->func = func;
432 handler->data = data;
433 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400434 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400435 mutex_unlock(&ec->lock);
436 return 0;
437}
438
439EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
440
441void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
442{
443 struct acpi_ec_query_handler *handler;
444 mutex_lock(&ec->lock);
445 list_for_each_entry(handler, &ec->list, node) {
446 if (query_bit == handler->query_bit) {
447 list_del(&handler->node);
448 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400449 }
450 }
451 mutex_unlock(&ec->lock);
452}
453
454EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Len Brown50526df2005-08-11 17:32:05 -0400456static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300458 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400459 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400460 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400461
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300462 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300463 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400464 mutex_lock(&ec->lock);
465 list_for_each_entry(handler, &ec->list, node) {
466 if (value == handler->query_bit) {
467 /* have custom handler for this bit */
468 memcpy(&copy, handler, sizeof(copy));
469 mutex_unlock(&ec->lock);
470 if (copy.func) {
471 copy.func(copy.data);
472 } else if (copy.handle) {
473 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
474 }
475 return;
476 }
477 }
478 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Len Brown50526df2005-08-11 17:32:05 -0400481static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown50526df2005-08-11 17:32:05 -0400483 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300484 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200485
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400486 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400487 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300488 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500489
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400490 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
491 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
492 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
493 acpi_ec_gpe_query, ec);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400494 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags)))
495 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300496
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400497 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400498 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/* --------------------------------------------------------------------------
502 Address Space Management
503 -------------------------------------------------------------------------- */
504
505static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400506acpi_ec_space_setup(acpi_handle region_handle,
507 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 /*
510 * The EC object is in the handler context and is needed
511 * when calling the acpi_ec_space_handler.
512 */
Len Brown50526df2005-08-11 17:32:05 -0400513 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
514 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return AE_OK;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400520acpi_ec_space_handler(u32 function, acpi_physical_address address,
521 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400522 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300524 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400525 int result = 0, i = 0;
526 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400531 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400533
534 if (bits != 8 && acpi_strict)
535 return AE_BAD_PARAMETER;
536
537 while (bits - i > 0) {
538 if (function == ACPI_READ) {
539 result = acpi_ec_read(ec, address, &temp);
540 (*value) |= ((acpi_integer)temp) << i;
541 } else {
542 temp = 0xff & ((*value) >> i);
543 result = acpi_ec_write(ec, address, temp);
544 }
545 i += 8;
546 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 switch (result) {
550 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* --------------------------------------------------------------------------
565 FS Interface (/proc)
566 -------------------------------------------------------------------------- */
567
Len Brown50526df2005-08-11 17:32:05 -0400568static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Len Brown50526df2005-08-11 17:32:05 -0400570static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300572 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!ec)
575 goto end;
576
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300577 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
578 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
579 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
580 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400581 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400582 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
587{
588 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
589}
590
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400591static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400592 .open = acpi_ec_info_open_fs,
593 .read = seq_read,
594 .llseek = seq_lseek,
595 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .owner = THIS_MODULE,
597};
598
Len Brown50526df2005-08-11 17:32:05 -0400599static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Len Brown50526df2005-08-11 17:32:05 -0400601 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!acpi_device_dir(device)) {
604 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400605 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400611 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400613 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 else {
615 entry->proc_fops = &acpi_ec_info_ops;
616 entry->data = acpi_driver_data(device);
617 entry->owner = THIS_MODULE;
618 }
619
Patrick Mocheld550d982006-06-27 00:41:40 -0400620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Len Brown50526df2005-08-11 17:32:05 -0400623static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if (acpi_device_dir(device)) {
627 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
628 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
629 acpi_device_dir(device) = NULL;
630 }
631
Patrick Mocheld550d982006-06-27 00:41:40 -0400632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/* --------------------------------------------------------------------------
636 Driver Interface
637 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300638static acpi_status
639ec_parse_io_ports(struct acpi_resource *resource, void *context);
640
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300641static struct acpi_ec *make_acpi_ec(void)
642{
643 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
644 if (!ec)
645 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400646 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647 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 return ec;
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400653static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400654acpi_ec_register_query_methods(acpi_handle handle, u32 level,
655 void *context, void **return_value)
656{
657 struct acpi_namespace_node *node = handle;
658 struct acpi_ec *ec = context;
659 int value = 0;
660 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
661 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
662 }
663 return AE_OK;
664}
665
666static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400667ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400668{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400669 acpi_status status;
670
671 struct acpi_ec *ec = context;
672 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
673 ec_parse_io_ports, ec);
674 if (ACPI_FAILURE(status))
675 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400676
677 /* Get GPE bit assignment (EC events). */
678 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400679 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
680 if (ACPI_FAILURE(status))
681 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400682 /* Find and register all query methods */
683 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
684 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400685 /* Use the global lock for all EC transactions? */
686 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400687 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400688 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400689}
690
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400691static void ec_remove_handlers(struct acpi_ec *ec)
692{
693 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
694 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
695 printk(KERN_ERR PREFIX "failed to remove space handler\n");
696 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
697 &acpi_ec_gpe_handler)))
698 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
699 ec->handlers_installed = 0;
700}
701
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400702static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400704 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400707 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300708 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
709 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
710
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400711 /* Check for boot EC */
712 if (boot_ec) {
713 if (boot_ec->handle == device->handle) {
714 /* Pre-loaded EC from DSDT, just move pointer */
715 ec = boot_ec;
716 boot_ec = NULL;
717 goto end;
718 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
719 /* ECDT-based EC, time to shut it down */
720 ec_remove_handlers(boot_ec);
721 kfree(boot_ec);
722 first_ec = boot_ec = NULL;
723 }
724 }
725
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400730 if (ec_parse_device(device->handle, 0, ec, NULL) !=
731 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300732 kfree(ec);
733 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400734 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400736 end:
737 if (!first_ec)
738 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300740 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400741 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
742 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Len Brown50526df2005-08-11 17:32:05 -0400746static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300748 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200749 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400755 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200756 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400757 list_del(&handler->node);
758 kfree(handler);
759 }
760 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300763 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300764 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400765 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300770ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300772 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300774 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * The first address region returned is the data port, and
779 * the second address region returned is the status/command
780 * port.
781 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300782 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400783 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300784 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400785 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300786 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return AE_OK;
790}
791
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300792static int ec_install_handlers(struct acpi_ec *ec)
793{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300794 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400795 if (ec->handlers_installed)
796 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300797 status = acpi_install_gpe_handler(NULL, ec->gpe,
798 ACPI_GPE_EDGE_TRIGGERED,
799 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300800 if (ACPI_FAILURE(status))
801 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300802
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300803 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
804 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
805
806 status = acpi_install_address_space_handler(ec->handle,
807 ACPI_ADR_SPACE_EC,
808 &acpi_ec_space_handler,
809 &acpi_ec_space_setup, ec);
810 if (ACPI_FAILURE(status)) {
811 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
812 return -ENODEV;
813 }
814
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400815 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300816 return 0;
817}
818
Len Brown50526df2005-08-11 17:32:05 -0400819static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300821 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400822 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 ec = acpi_driver_data(device);
828
829 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400832 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300833
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400834 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400835 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Len Brown50526df2005-08-11 17:32:05 -0400839static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300841 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300845 if (!ec)
846 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400847 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400848
Patrick Mocheld550d982006-06-27 00:41:40 -0400849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Len Brown50526df2005-08-11 17:32:05 -0400852int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Len Brown50526df2005-08-11 17:32:05 -0400854 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300855 acpi_status status;
856 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300858 boot_ec = make_acpi_ec();
859 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300860 return -ENOMEM;
861 /*
862 * Generate a boot ec context
863 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300864 status = acpi_get_table(ACPI_SIG_ECDT, 1,
865 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400866 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400867 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400868 boot_ec->command_addr = ecdt_ptr->control.address;
869 boot_ec->data_addr = ecdt_ptr->data.address;
870 boot_ec->gpe = ecdt_ptr->gpe;
871 boot_ec->handle = ACPI_ROOT_OBJECT;
872 } else {
873 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
874 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
875 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400876 /* Check that acpi_get_devices actually find something */
877 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400878 goto error;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300881 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300882 if (!ret) {
883 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300884 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300885 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300886 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300887 kfree(boot_ec);
888 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENODEV;
890}
891
Len Brown50526df2005-08-11 17:32:05 -0400892static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Len Brown50526df2005-08-11 17:32:05 -0400894 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
900 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400901 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* Now register the driver for the EC */
904 result = acpi_bus_register_driver(&acpi_ec_driver);
905 if (result < 0) {
906 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Patrick Mocheld550d982006-06-27 00:41:40 -0400910 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913subsys_initcall(acpi_ec_init);
914
915/* EC driver currently not unloadable */
916#if 0
Len Brown50526df2005-08-11 17:32:05 -0400917static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 acpi_bus_unregister_driver(&acpi_ec_driver);
921
922 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
923
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400926#endif /* 0 */