blob: 50d55fe71a30e624bb46d144b785e95bc7e10df2 [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 Starikovskiy080e4122007-10-22 14:18:30 +040078};
79
Len Brown50526df2005-08-11 17:32:05 -040080static int acpi_ec_remove(struct acpi_device *device, int type);
81static int acpi_ec_start(struct acpi_device *device);
82static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040083static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Thomas Renninger1ba90e32007-07-23 14:44:41 +020085static const struct acpi_device_id ec_device_ids[] = {
86 {"PNP0C09", 0},
87 {"", 0},
88};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050091 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040092 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020093 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040094 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040095 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040096 .remove = acpi_ec_remove,
97 .start = acpi_ec_start,
98 .stop = acpi_ec_stop,
99 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400101
102/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300103/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400104typedef int (*acpi_ec_query_func) (void *data);
105
106struct acpi_ec_query_handler {
107 struct list_head node;
108 acpi_ec_query_func func;
109 acpi_handle handle;
110 void *data;
111 u8 query_bit;
112};
113
Adrian Bunka854e082006-12-19 12:56:12 -0800114static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400115 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300116 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400117 unsigned long command_addr;
118 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400119 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400120 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300121 struct mutex lock;
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 Starikovskiy080e4122007-10-22 14:18:30 +0400151static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400153 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500154 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300155 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400156 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400157 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300158 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400159 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400160 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 }
162
163 return 0;
164}
165
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400166static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400167{
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400168 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
169 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400170 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
171 msecs_to_jiffies(ACPI_EC_DELAY)))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400172 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400173 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
174 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400175 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400176 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400177 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400178 } else {
179 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
180 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
181 while (time_before(jiffies, delay)) {
182 if (acpi_ec_check_status(ec, event))
183 return 0;
184 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300185 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400186 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
187 " status = %d, expect_event = %d\n",
188 acpi_ec_read_status(ec), event);
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500190}
191
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400192static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300193 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200194 u8 * rdata, unsigned rdata_len,
195 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400196{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300197 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400198 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400199 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400200
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300201 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400202 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300203 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300204 printk(KERN_ERR PREFIX
205 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300206 goto end;
207 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400208 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400209 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400210 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400211
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300212 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400213 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300214 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300215 printk(KERN_ERR PREFIX
216 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300217 goto end;
218 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400219 } else if (command == ACPI_EC_COMMAND_QUERY)
220 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400221
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300222 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400223 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300224 if (result) {
225 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300226 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300227 goto end;
228 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400229 /* Don't expect GPE after last read */
230 if (rdata_len > 1)
231 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
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
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400264 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400265 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400266 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300267 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500268 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300271 status = acpi_ec_transaction_unlocked(ec, command,
272 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200273 rdata, rdata_len,
274 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400275
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300276 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400278 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300280 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300285/*
286 * Note: samsung nv5000 doesn't work with ec burst mode.
287 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
288 */
289int acpi_ec_burst_enable(struct acpi_ec *ec)
290{
291 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200292 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300293}
294
295int acpi_ec_burst_disable(struct acpi_ec *ec)
296{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200297 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300298}
299
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300300static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400301{
302 int result;
303 u8 d;
304
305 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200306 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400307 *data = d;
308 return result;
309}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400310
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400311static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
312{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300313 u8 wdata[2] = { address, data };
314 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200315 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Externally callable EC access functions. For now, assume 1 EC only
320 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300321int ec_burst_enable(void)
322{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300323 if (!first_ec)
324 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300325 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300326}
327
328EXPORT_SYMBOL(ec_burst_enable);
329
330int ec_burst_disable(void)
331{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300332 if (!first_ec)
333 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300334 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300335}
336
337EXPORT_SYMBOL(ec_burst_disable);
338
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300339int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400342 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!first_ec)
345 return -ENODEV;
346
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300347 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!err) {
350 *val = temp_data;
351 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400352 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
Len Brown50526df2005-08-11 17:32:05 -0400355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356EXPORT_SYMBOL(ec_read);
357
Len Brown50526df2005-08-11 17:32:05 -0400358int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int err;
361
362 if (!first_ec)
363 return -ENODEV;
364
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300365 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 return err;
368}
Len Brown50526df2005-08-11 17:32:05 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370EXPORT_SYMBOL(ec_write);
371
Randy Dunlap616362d2006-10-27 01:47:34 -0400372int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500373 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200374 u8 * rdata, unsigned rdata_len,
375 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400376{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400377 if (!first_ec)
378 return -ENODEV;
379
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300380 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200381 wdata_len, rdata, rdata_len,
382 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400383}
Luming Yu45bea152005-07-23 04:08:00 -0400384
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400385EXPORT_SYMBOL(ec_transaction);
386
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400388{
389 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300390 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392 if (!ec || !data)
393 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400394
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300395 /*
396 * Query the EC to find out which _Qxx method we need to evaluate.
397 * Note that successful completion of the query causes the ACPI_EC_SCI
398 * bit to be cleared (and thus clearing the interrupt source).
399 */
Luming Yu45bea152005-07-23 04:08:00 -0400400
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200401 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300402 if (result)
403 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400404
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300405 if (!d)
406 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400407
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300408 *data = d;
409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/* --------------------------------------------------------------------------
413 Event Management
414 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400415int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
416 acpi_handle handle, acpi_ec_query_func func,
417 void *data)
418{
419 struct acpi_ec_query_handler *handler =
420 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
421 if (!handler)
422 return -ENOMEM;
423
424 handler->query_bit = query_bit;
425 handler->handle = handle;
426 handler->func = func;
427 handler->data = data;
428 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400429 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400430 mutex_unlock(&ec->lock);
431 return 0;
432}
433
434EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
435
436void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
437{
438 struct acpi_ec_query_handler *handler;
439 mutex_lock(&ec->lock);
440 list_for_each_entry(handler, &ec->list, node) {
441 if (query_bit == handler->query_bit) {
442 list_del(&handler->node);
443 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400444 }
445 }
446 mutex_unlock(&ec->lock);
447}
448
449EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Len Brown50526df2005-08-11 17:32:05 -0400451static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300453 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400454 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400455 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400456
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300457 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300458 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400459 mutex_lock(&ec->lock);
460 list_for_each_entry(handler, &ec->list, node) {
461 if (value == handler->query_bit) {
462 /* have custom handler for this bit */
463 memcpy(&copy, handler, sizeof(copy));
464 mutex_unlock(&ec->lock);
465 if (copy.func) {
466 copy.func(copy.data);
467 } else if (copy.handle) {
468 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
469 }
470 return;
471 }
472 }
473 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown50526df2005-08-11 17:32:05 -0400476static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Len Brown50526df2005-08-11 17:32:05 -0400478 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300479 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200480
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400481 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400482 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300483 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500484
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400485 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
486 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
487 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
488 acpi_ec_gpe_query, ec);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400489 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags)))
490 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300491
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400492 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400493 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/* --------------------------------------------------------------------------
497 Address Space Management
498 -------------------------------------------------------------------------- */
499
500static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400501acpi_ec_space_setup(acpi_handle region_handle,
502 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 /*
505 * The EC object is in the handler context and is needed
506 * when calling the acpi_ec_space_handler.
507 */
Len Brown50526df2005-08-11 17:32:05 -0400508 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
509 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 return AE_OK;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400515acpi_ec_space_handler(u32 function, acpi_physical_address address,
516 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400517 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300519 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400520 int result = 0, i = 0;
521 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400524 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400526 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400528
529 if (bits != 8 && acpi_strict)
530 return AE_BAD_PARAMETER;
531
532 while (bits - i > 0) {
533 if (function == ACPI_READ) {
534 result = acpi_ec_read(ec, address, &temp);
535 (*value) |= ((acpi_integer)temp) << i;
536 } else {
537 temp = 0xff & ((*value) >> i);
538 result = acpi_ec_write(ec, address, temp);
539 }
540 i += 8;
541 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (result) {
545 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400546 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/* --------------------------------------------------------------------------
560 FS Interface (/proc)
561 -------------------------------------------------------------------------- */
562
Len Brown50526df2005-08-11 17:32:05 -0400563static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Len Brown50526df2005-08-11 17:32:05 -0400565static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300567 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!ec)
570 goto end;
571
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300572 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
573 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
574 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
575 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400576 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400577 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
582{
583 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
584}
585
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400586static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400587 .open = acpi_ec_info_open_fs,
588 .read = seq_read,
589 .llseek = seq_lseek,
590 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .owner = THIS_MODULE,
592};
593
Len Brown50526df2005-08-11 17:32:05 -0400594static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Len Brown50526df2005-08-11 17:32:05 -0400596 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!acpi_device_dir(device)) {
599 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400600 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400606 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else {
610 entry->proc_fops = &acpi_ec_info_ops;
611 entry->data = acpi_driver_data(device);
612 entry->owner = THIS_MODULE;
613 }
614
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Len Brown50526df2005-08-11 17:32:05 -0400618static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (acpi_device_dir(device)) {
622 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
623 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
624 acpi_device_dir(device) = NULL;
625 }
626
Patrick Mocheld550d982006-06-27 00:41:40 -0400627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* --------------------------------------------------------------------------
631 Driver Interface
632 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300633static acpi_status
634ec_parse_io_ports(struct acpi_resource *resource, void *context);
635
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300636static struct acpi_ec *make_acpi_ec(void)
637{
638 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
639 if (!ec)
640 return NULL;
641
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400642 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300643 mutex_init(&ec->lock);
644 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400645 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300646
647 return ec;
648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400650static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400651acpi_ec_register_query_methods(acpi_handle handle, u32 level,
652 void *context, void **return_value)
653{
654 struct acpi_namespace_node *node = handle;
655 struct acpi_ec *ec = context;
656 int value = 0;
657 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
658 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
659 }
660 return AE_OK;
661}
662
663static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400664ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400665{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400666 acpi_status status;
667
668 struct acpi_ec *ec = context;
669 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
670 ec_parse_io_ports, ec);
671 if (ACPI_FAILURE(status))
672 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400673
674 /* Get GPE bit assignment (EC events). */
675 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400676 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
677 if (ACPI_FAILURE(status))
678 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400679 /* Find and register all query methods */
680 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
681 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400682 /* Use the global lock for all EC transactions? */
683 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400684 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400685 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400686}
687
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400688static void ec_remove_handlers(struct acpi_ec *ec)
689{
690 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
691 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
692 printk(KERN_ERR PREFIX "failed to remove space handler\n");
693 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
694 &acpi_ec_gpe_handler)))
695 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
696 ec->handlers_installed = 0;
697}
698
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400699static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400701 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400704 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300705 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
706 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
707
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400708 /* Check for boot EC */
709 if (boot_ec) {
710 if (boot_ec->handle == device->handle) {
711 /* Pre-loaded EC from DSDT, just move pointer */
712 ec = boot_ec;
713 boot_ec = NULL;
714 goto end;
715 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
716 /* ECDT-based EC, time to shut it down */
717 ec_remove_handlers(boot_ec);
718 kfree(boot_ec);
719 first_ec = boot_ec = NULL;
720 }
721 }
722
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300723 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400727 if (ec_parse_device(device->handle, 0, ec, NULL) !=
728 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300729 kfree(ec);
730 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400731 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300732 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400733 end:
734 if (!first_ec)
735 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300737 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400738 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
739 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Len Brown50526df2005-08-11 17:32:05 -0400743static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300745 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200746 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400749 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400752 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200753 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400754 list_del(&handler->node);
755 kfree(handler);
756 }
757 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300759 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300760 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300761 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400762 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300767ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300769 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300771 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * The first address region returned is the data port, and
776 * the second address region returned is the status/command
777 * port.
778 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300779 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400780 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300781 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400782 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300783 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return AE_OK;
787}
788
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300789static int ec_install_handlers(struct acpi_ec *ec)
790{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300791 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400792 if (ec->handlers_installed)
793 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300794 status = acpi_install_gpe_handler(NULL, ec->gpe,
795 ACPI_GPE_EDGE_TRIGGERED,
796 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300797 if (ACPI_FAILURE(status))
798 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300799
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300800 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
801 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
802
803 status = acpi_install_address_space_handler(ec->handle,
804 ACPI_ADR_SPACE_EC,
805 &acpi_ec_space_handler,
806 &acpi_ec_space_setup, ec);
807 if (ACPI_FAILURE(status)) {
808 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
809 return -ENODEV;
810 }
811
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400812 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300813 return 0;
814}
815
Len Brown50526df2005-08-11 17:32:05 -0400816static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300818 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400819 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 ec = acpi_driver_data(device);
825
826 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400829 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300830
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400831 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400832 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Len Brown50526df2005-08-11 17:32:05 -0400836static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300838 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400840 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300842 if (!ec)
843 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400844 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400845
Patrick Mocheld550d982006-06-27 00:41:40 -0400846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Len Brown50526df2005-08-11 17:32:05 -0400849int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Len Brown50526df2005-08-11 17:32:05 -0400851 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300852 acpi_status status;
853 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300855 boot_ec = make_acpi_ec();
856 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300857 return -ENOMEM;
858 /*
859 * Generate a boot ec context
860 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300861 status = acpi_get_table(ACPI_SIG_ECDT, 1,
862 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400863 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400864 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400865 boot_ec->command_addr = ecdt_ptr->control.address;
866 boot_ec->data_addr = ecdt_ptr->data.address;
867 boot_ec->gpe = ecdt_ptr->gpe;
868 boot_ec->handle = ACPI_ROOT_OBJECT;
869 } else {
870 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
871 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
872 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400873 /* Check that acpi_get_devices actually find something */
874 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400875 goto error;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300878 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300879 if (!ret) {
880 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300881 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300882 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300883 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300884 kfree(boot_ec);
885 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -ENODEV;
887}
888
Len Brown50526df2005-08-11 17:32:05 -0400889static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Len Brown50526df2005-08-11 17:32:05 -0400891 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
897 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400898 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Now register the driver for the EC */
901 result = acpi_bus_register_driver(&acpi_ec_driver);
902 if (result < 0) {
903 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400904 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910subsys_initcall(acpi_ec_init);
911
912/* EC driver currently not unloadable */
913#if 0
Len Brown50526df2005-08-11 17:32:05 -0400914static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 acpi_bus_unregister_driver(&acpi_ec_driver);
918
919 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
920
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400923#endif /* 0 */