blob: 41a21fcdbcb85b9d3ae7def1a112dd508de305b6 [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 Brownc2b67052007-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 Starikovskiy080e4122007-10-22 14:18:30 +0400261 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400262 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400263 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300264 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500265 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300268 status = acpi_ec_transaction_unlocked(ec, command,
269 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200270 rdata, rdata_len,
271 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400272
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300273 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400275 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300277 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Patrick Mocheld550d982006-06-27 00:41:40 -0400279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300282/*
283 * Note: samsung nv5000 doesn't work with ec burst mode.
284 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
285 */
286int acpi_ec_burst_enable(struct acpi_ec *ec)
287{
288 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200289 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300290}
291
292int acpi_ec_burst_disable(struct acpi_ec *ec)
293{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200294 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300295}
296
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300297static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400298{
299 int result;
300 u8 d;
301
302 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200303 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400304 *data = d;
305 return result;
306}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400307
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400308static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
309{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300310 u8 wdata[2] = { address, data };
311 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200312 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * Externally callable EC access functions. For now, assume 1 EC only
317 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300318int ec_burst_enable(void)
319{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300320 if (!first_ec)
321 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300322 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300323}
324
325EXPORT_SYMBOL(ec_burst_enable);
326
327int ec_burst_disable(void)
328{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300329 if (!first_ec)
330 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300331 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300332}
333
334EXPORT_SYMBOL(ec_burst_disable);
335
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300336int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400339 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (!first_ec)
342 return -ENODEV;
343
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300344 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (!err) {
347 *val = temp_data;
348 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400349 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return err;
351}
Len Brown50526df2005-08-11 17:32:05 -0400352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353EXPORT_SYMBOL(ec_read);
354
Len Brown50526df2005-08-11 17:32:05 -0400355int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int err;
358
359 if (!first_ec)
360 return -ENODEV;
361
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300362 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return err;
365}
Len Brown50526df2005-08-11 17:32:05 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367EXPORT_SYMBOL(ec_write);
368
Randy Dunlap616362d2006-10-27 01:47:34 -0400369int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500370 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200371 u8 * rdata, unsigned rdata_len,
372 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400373{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400374 if (!first_ec)
375 return -ENODEV;
376
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300377 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200378 wdata_len, rdata, rdata_len,
379 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400380}
Luming Yu45bea152005-07-23 04:08:00 -0400381
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400382EXPORT_SYMBOL(ec_transaction);
383
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300384static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400385{
386 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400388
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300389 if (!ec || !data)
390 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392 /*
393 * Query the EC to find out which _Qxx method we need to evaluate.
394 * Note that successful completion of the query causes the ACPI_EC_SCI
395 * bit to be cleared (and thus clearing the interrupt source).
396 */
Luming Yu45bea152005-07-23 04:08:00 -0400397
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200398 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300399 if (result)
400 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400401
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300402 if (!d)
403 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400404
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300405 *data = d;
406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* --------------------------------------------------------------------------
410 Event Management
411 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400412int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
413 acpi_handle handle, acpi_ec_query_func func,
414 void *data)
415{
416 struct acpi_ec_query_handler *handler =
417 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
418 if (!handler)
419 return -ENOMEM;
420
421 handler->query_bit = query_bit;
422 handler->handle = handle;
423 handler->func = func;
424 handler->data = data;
425 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400426 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400427 mutex_unlock(&ec->lock);
428 return 0;
429}
430
431EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
432
433void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
434{
435 struct acpi_ec_query_handler *handler;
436 mutex_lock(&ec->lock);
437 list_for_each_entry(handler, &ec->list, node) {
438 if (query_bit == handler->query_bit) {
439 list_del(&handler->node);
440 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400441 }
442 }
443 mutex_unlock(&ec->lock);
444}
445
446EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Len Brown50526df2005-08-11 17:32:05 -0400448static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300450 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400451 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400452 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400453
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300454 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300455 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400456 mutex_lock(&ec->lock);
457 list_for_each_entry(handler, &ec->list, node) {
458 if (value == handler->query_bit) {
459 /* have custom handler for this bit */
460 memcpy(&copy, handler, sizeof(copy));
461 mutex_unlock(&ec->lock);
462 if (copy.func) {
463 copy.func(copy.data);
464 } else if (copy.handle) {
465 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
466 }
467 return;
468 }
469 }
470 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Len Brown50526df2005-08-11 17:32:05 -0400473static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Len Brown50526df2005-08-11 17:32:05 -0400475 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300476 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200477
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400478 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400479 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300480 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500481
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400482 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
483 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
484 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
485 acpi_ec_gpe_query, ec);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400486 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags)))
487 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300488
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400489 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400490 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/* --------------------------------------------------------------------------
494 Address Space Management
495 -------------------------------------------------------------------------- */
496
497static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400498acpi_ec_space_setup(acpi_handle region_handle,
499 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 /*
502 * The EC object is in the handler context and is needed
503 * when calling the acpi_ec_space_handler.
504 */
Len Brown50526df2005-08-11 17:32:05 -0400505 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
506 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 return AE_OK;
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400512acpi_ec_space_handler(u32 function, acpi_physical_address address,
513 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400514 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300516 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400517 int result = 0, i = 0;
518 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400521 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400523 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400524 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400525
526 if (bits != 8 && acpi_strict)
527 return AE_BAD_PARAMETER;
528
529 while (bits - i > 0) {
530 if (function == ACPI_READ) {
531 result = acpi_ec_read(ec, address, &temp);
532 (*value) |= ((acpi_integer)temp) << i;
533 } else {
534 temp = 0xff & ((*value) >> i);
535 result = acpi_ec_write(ec, address, temp);
536 }
537 i += 8;
538 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 switch (result) {
542 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400543 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400546 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/* --------------------------------------------------------------------------
557 FS Interface (/proc)
558 -------------------------------------------------------------------------- */
559
Len Brown50526df2005-08-11 17:32:05 -0400560static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Len Brown50526df2005-08-11 17:32:05 -0400562static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300564 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!ec)
567 goto end;
568
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300569 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
570 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
571 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
572 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400573 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400574 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
579{
580 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
581}
582
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400583static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400584 .open = acpi_ec_info_open_fs,
585 .read = seq_read,
586 .llseek = seq_lseek,
587 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 .owner = THIS_MODULE,
589};
590
Len Brown50526df2005-08-11 17:32:05 -0400591static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Len Brown50526df2005-08-11 17:32:05 -0400593 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!acpi_device_dir(device)) {
596 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400597 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400599 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400603 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else {
607 entry->proc_fops = &acpi_ec_info_ops;
608 entry->data = acpi_driver_data(device);
609 entry->owner = THIS_MODULE;
610 }
611
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Len Brown50526df2005-08-11 17:32:05 -0400615static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (acpi_device_dir(device)) {
619 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
620 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
621 acpi_device_dir(device) = NULL;
622 }
623
Patrick Mocheld550d982006-06-27 00:41:40 -0400624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/* --------------------------------------------------------------------------
628 Driver Interface
629 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300630static acpi_status
631ec_parse_io_ports(struct acpi_resource *resource, void *context);
632
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300633static struct acpi_ec *make_acpi_ec(void)
634{
635 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
636 if (!ec)
637 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400638 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300639 mutex_init(&ec->lock);
640 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400641 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300642 return ec;
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400645static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400646acpi_ec_register_query_methods(acpi_handle handle, u32 level,
647 void *context, void **return_value)
648{
649 struct acpi_namespace_node *node = handle;
650 struct acpi_ec *ec = context;
651 int value = 0;
652 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
653 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
654 }
655 return AE_OK;
656}
657
658static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400659ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400660{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400661 acpi_status status;
662
663 struct acpi_ec *ec = context;
664 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
665 ec_parse_io_ports, ec);
666 if (ACPI_FAILURE(status))
667 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400668
669 /* Get GPE bit assignment (EC events). */
670 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400671 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
672 if (ACPI_FAILURE(status))
673 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400674 /* Find and register all query methods */
675 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
676 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400677 /* Use the global lock for all EC transactions? */
678 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400679 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400680 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400681}
682
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400683static void ec_remove_handlers(struct acpi_ec *ec)
684{
685 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
686 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
687 printk(KERN_ERR PREFIX "failed to remove space handler\n");
688 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
689 &acpi_ec_gpe_handler)))
690 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
691 ec->handlers_installed = 0;
692}
693
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400694static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400696 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400699 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300700 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
701 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
702
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400703 /* Check for boot EC */
704 if (boot_ec) {
705 if (boot_ec->handle == device->handle) {
706 /* Pre-loaded EC from DSDT, just move pointer */
707 ec = boot_ec;
708 boot_ec = NULL;
709 goto end;
710 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
711 /* ECDT-based EC, time to shut it down */
712 ec_remove_handlers(boot_ec);
713 kfree(boot_ec);
714 first_ec = boot_ec = NULL;
715 }
716 }
717
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300718 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400722 if (ec_parse_device(device->handle, 0, ec, NULL) !=
723 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300724 kfree(ec);
725 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400726 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300727 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400728 end:
729 if (!first_ec)
730 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300732 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400733 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
734 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Len Brown50526df2005-08-11 17:32:05 -0400738static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300740 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200741 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400747 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200748 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400749 list_del(&handler->node);
750 kfree(handler);
751 }
752 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300754 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300755 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300756 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400757 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300764 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300766 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /*
770 * The first address region returned is the data port, and
771 * the second address region returned is the status/command
772 * port.
773 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300774 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400775 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300776 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400777 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300778 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return AE_OK;
782}
783
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300784static int ec_install_handlers(struct acpi_ec *ec)
785{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300786 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400787 if (ec->handlers_installed)
788 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300789 status = acpi_install_gpe_handler(NULL, ec->gpe,
790 ACPI_GPE_EDGE_TRIGGERED,
791 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300792 if (ACPI_FAILURE(status))
793 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300794
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300795 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
796 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
797
798 status = acpi_install_address_space_handler(ec->handle,
799 ACPI_ADR_SPACE_EC,
800 &acpi_ec_space_handler,
801 &acpi_ec_space_setup, ec);
802 if (ACPI_FAILURE(status)) {
803 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
804 return -ENODEV;
805 }
806
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400807 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300808 return 0;
809}
810
Len Brown50526df2005-08-11 17:32:05 -0400811static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300813 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400814 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400817 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 ec = acpi_driver_data(device);
820
821 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400824 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300825
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400826 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400827 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Len Brown50526df2005-08-11 17:32:05 -0400831static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300833 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300837 if (!ec)
838 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400839 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400840
Patrick Mocheld550d982006-06-27 00:41:40 -0400841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Len Brown50526df2005-08-11 17:32:05 -0400844int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Len Brown50526df2005-08-11 17:32:05 -0400846 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300847 acpi_status status;
848 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300850 boot_ec = make_acpi_ec();
851 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300852 return -ENOMEM;
853 /*
854 * Generate a boot ec context
855 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300856 status = acpi_get_table(ACPI_SIG_ECDT, 1,
857 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400858 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400859 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400860 boot_ec->command_addr = ecdt_ptr->control.address;
861 boot_ec->data_addr = ecdt_ptr->data.address;
862 boot_ec->gpe = ecdt_ptr->gpe;
863 boot_ec->handle = ACPI_ROOT_OBJECT;
864 } else {
865 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
866 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
867 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400868 /* Check that acpi_get_devices actually find something */
869 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400870 goto error;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300873 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300874 if (!ret) {
875 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300876 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300877 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300878 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300879 kfree(boot_ec);
880 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENODEV;
882}
883
Len Brown50526df2005-08-11 17:32:05 -0400884static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Len Brown50526df2005-08-11 17:32:05 -0400886 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
892 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Now register the driver for the EC */
896 result = acpi_bus_register_driver(&acpi_ec_driver);
897 if (result < 0) {
898 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905subsys_initcall(acpi_ec_init);
906
907/* EC driver currently not unloadable */
908#if 0
Len Brown50526df2005-08-11 17:32:05 -0400909static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 acpi_bus_unregister_driver(&acpi_ec_driver);
913
914 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
915
Patrick Mocheld550d982006-06-27 00:41:40 -0400916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400918#endif /* 0 */