blob: 987b967c74670728831af322571e4a182fb84d7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f224622007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f224622007-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
Márton Némethd772b3b2008-01-23 22:34:09 -050029/* Uncomment next line to get verbose print outs*/
30/* #define DEBUG */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/delay.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050039#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040040#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44#include <acpi/actypes.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040049
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030050#undef PREFIX
51#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040052
Denis M. Sadykov703959d2006-09-26 19:50:33 +040053/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050056#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040058
Denis M. Sadykov703959d2006-09-26 19:50:33 +040059/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030060enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030061 ACPI_EC_COMMAND_READ = 0x80,
62 ACPI_EC_COMMAND_WRITE = 0x81,
63 ACPI_EC_BURST_ENABLE = 0x82,
64 ACPI_EC_BURST_DISABLE = 0x83,
65 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040067
Denis M. Sadykov703959d2006-09-26 19:50:33 +040068/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030069enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040071 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072};
73
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030074#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040075#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040076
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040077enum {
78 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
79 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy78439322007-10-22 14:18:43 +040080 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +030081 EC_FLAGS_NO_ADDRESS_GPE, /* Expect GPE only for non-address event */
82 EC_FLAGS_ADDRESS, /* Address is being written */
Alexey Starikovskiye790cc82007-11-21 03:23:32 +030083 EC_FLAGS_NO_WDATA_GPE, /* Don't expect WDATA GPE event */
84 EC_FLAGS_WDATA, /* Data is being written */
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -050085 EC_FLAGS_NO_OBF1_GPE, /* Don't expect GPE before read */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040086};
87
Len Brown50526df2005-08-11 17:32:05 -040088static int acpi_ec_remove(struct acpi_device *device, int type);
89static int acpi_ec_start(struct acpi_device *device);
90static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040091static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Thomas Renninger1ba90e32007-07-23 14:44:41 +020093static const struct acpi_device_id ec_device_ids[] = {
94 {"PNP0C09", 0},
95 {"", 0},
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct acpi_driver acpi_ec_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050099 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -0400100 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200101 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -0400102 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400103 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -0400104 .remove = acpi_ec_remove,
105 .start = acpi_ec_start,
106 .stop = acpi_ec_stop,
107 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400109
110/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300111/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400112typedef int (*acpi_ec_query_func) (void *data);
113
114struct acpi_ec_query_handler {
115 struct list_head node;
116 acpi_ec_query_func func;
117 acpi_handle handle;
118 void *data;
119 u8 query_bit;
120};
121
Adrian Bunka854e082006-12-19 12:56:12 -0800122static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300124 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400125 unsigned long command_addr;
126 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400127 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400128 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300129 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400131 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400132 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300133} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* --------------------------------------------------------------------------
136 Transaction Management
137 -------------------------------------------------------------------------- */
138
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400139static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300141 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500142 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300143 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400146static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400147{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300148 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500149 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400150 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400151}
152
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400154{
Márton Németh86dae012008-01-23 22:33:06 -0500155 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400156 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157}
158
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400159static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400160{
Márton Németh86dae012008-01-23 22:33:06 -0500161 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400162 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400163}
164
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400165static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400166{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400167 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500168 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300169 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400170 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400171 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300172 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400173 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400174 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400175 }
176
177 return 0;
178}
179
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400180static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400181{
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300182 int ret = 0;
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -0500183
184 if (unlikely(event == ACPI_EC_EVENT_OBF_1 &&
185 test_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags)))
186 force_poll = 1;
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300187 if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) &&
188 test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags)))
189 force_poll = 1;
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300190 if (unlikely(test_bit(EC_FLAGS_WDATA, &ec->flags) &&
191 test_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags)))
192 force_poll = 1;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400193 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
194 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400195 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
196 msecs_to_jiffies(ACPI_EC_DELAY)))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300197 goto end;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400198 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
199 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -0500200 if (event == ACPI_EC_EVENT_OBF_1) {
201 /* miss OBF_1 GPE, don't expect it */
202 pr_info(PREFIX "missing OBF confirmation, "
203 "don't expect it any longer.\n");
204 set_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags);
205 } else if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300206 /* miss address GPE, don't expect it anymore */
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300207 pr_info(PREFIX "missing address confirmation, "
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300208 "don't expect it any longer.\n");
209 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300210 } else if (test_bit(EC_FLAGS_WDATA, &ec->flags)) {
211 /* miss write data GPE, don't expect it */
212 pr_info(PREFIX "missing write data confirmation, "
213 "don't expect it any longer.\n");
214 set_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400215 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400216 /* missing GPEs, switch back to poll mode */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300217 if (printk_ratelimit())
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300218 pr_info(PREFIX "missing confirmations, "
Márton Németh3ebe08a2007-11-21 03:23:26 +0300219 "switch off interrupt mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400220 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400221 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300222 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400223 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400224 } else {
225 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
226 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
227 while (time_before(jiffies, delay)) {
228 if (acpi_ec_check_status(ec, event))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300229 goto end;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400230 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300231 }
Márton Németh3ebe08a2007-11-21 03:23:26 +0300232 pr_err(PREFIX "acpi_ec_wait timeout,"
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400233 " status = %d, expect_event = %d\n",
234 acpi_ec_read_status(ec), event);
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300235 ret = -ETIME;
236 end:
237 clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
238 return ret;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500239}
240
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400241static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300242 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200243 u8 * rdata, unsigned rdata_len,
244 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400245{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300246 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400247 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 acpi_ec_write_cmd(ec, command);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300249 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300250 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400251 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300252 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300253 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300254 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300255 goto end;
256 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300257 /* mark the address byte written to EC */
258 if (rdata_len + wdata_len > 1)
259 set_bit(EC_FLAGS_ADDRESS, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400260 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400261 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400262 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400263
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300264 if (!rdata_len) {
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300265 set_bit(EC_FLAGS_WDATA, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400266 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300267 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300268 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300269 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300270 goto end;
271 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400272 } else if (command == ACPI_EC_COMMAND_QUERY)
273 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400274
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300275 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400276 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300277 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300278 pr_err(PREFIX "read timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300279 goto end;
280 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400281 /* Don't expect GPE after last read */
282 if (rdata_len > 1)
283 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400284 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400285 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300286 end:
Márton Németh3ebe08a2007-11-21 03:23:26 +0300287 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300288 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400289}
290
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400291static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300292 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200293 u8 * rdata, unsigned rdata_len,
294 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400295{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400296 int status;
Len Brown50526df2005-08-11 17:32:05 -0400297 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400299 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300302 if (rdata)
303 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300305 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400306 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300308 if (ACPI_FAILURE(status)) {
309 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500313
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400314 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400315 if (status) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300316 pr_err(PREFIX "input buffer is not empty, "
317 "aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500318 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300321 status = acpi_ec_transaction_unlocked(ec, command,
322 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200323 rdata, rdata_len,
324 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400325
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300326 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400328 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300330 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300335/*
336 * Note: samsung nv5000 doesn't work with ec burst mode.
337 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
338 */
339int acpi_ec_burst_enable(struct acpi_ec *ec)
340{
341 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200342 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300343}
344
345int acpi_ec_burst_disable(struct acpi_ec *ec)
346{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200347 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300348}
349
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300350static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400351{
352 int result;
353 u8 d;
354
355 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200356 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400357 *data = d;
358 return result;
359}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400360
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400361static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
362{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300363 u8 wdata[2] = { address, data };
364 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200365 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * Externally callable EC access functions. For now, assume 1 EC only
370 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300371int ec_burst_enable(void)
372{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300373 if (!first_ec)
374 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300375 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300376}
377
378EXPORT_SYMBOL(ec_burst_enable);
379
380int ec_burst_disable(void)
381{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300382 if (!first_ec)
383 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300384 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300385}
386
387EXPORT_SYMBOL(ec_burst_disable);
388
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300389int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400392 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (!first_ec)
395 return -ENODEV;
396
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300397 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!err) {
400 *val = temp_data;
401 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400402 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return err;
404}
Len Brown50526df2005-08-11 17:32:05 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406EXPORT_SYMBOL(ec_read);
407
Len Brown50526df2005-08-11 17:32:05 -0400408int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err;
411
412 if (!first_ec)
413 return -ENODEV;
414
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300415 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 return err;
418}
Len Brown50526df2005-08-11 17:32:05 -0400419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420EXPORT_SYMBOL(ec_write);
421
Randy Dunlap616362d2006-10-27 01:47:34 -0400422int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500423 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200424 u8 * rdata, unsigned rdata_len,
425 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400426{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400427 if (!first_ec)
428 return -ENODEV;
429
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300430 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200431 wdata_len, rdata, rdata_len,
432 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400433}
Luming Yu45bea152005-07-23 04:08:00 -0400434
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400435EXPORT_SYMBOL(ec_transaction);
436
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300437static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400438{
439 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300440 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400441
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300442 if (!ec || !data)
443 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400444
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300445 /*
446 * Query the EC to find out which _Qxx method we need to evaluate.
447 * Note that successful completion of the query causes the ACPI_EC_SCI
448 * bit to be cleared (and thus clearing the interrupt source).
449 */
Luming Yu45bea152005-07-23 04:08:00 -0400450
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200451 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300452 if (result)
453 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400454
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300455 if (!d)
456 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400457
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300458 *data = d;
459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/* --------------------------------------------------------------------------
463 Event Management
464 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400465int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
466 acpi_handle handle, acpi_ec_query_func func,
467 void *data)
468{
469 struct acpi_ec_query_handler *handler =
470 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
471 if (!handler)
472 return -ENOMEM;
473
474 handler->query_bit = query_bit;
475 handler->handle = handle;
476 handler->func = func;
477 handler->data = data;
478 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400479 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400480 mutex_unlock(&ec->lock);
481 return 0;
482}
483
484EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
485
486void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
487{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200488 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400489 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200490 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400491 if (query_bit == handler->query_bit) {
492 list_del(&handler->node);
493 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400494 }
495 }
496 mutex_unlock(&ec->lock);
497}
498
499EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Len Brown50526df2005-08-11 17:32:05 -0400501static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300503 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400504 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400505 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400506
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300507 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300508 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400509 mutex_lock(&ec->lock);
510 list_for_each_entry(handler, &ec->list, node) {
511 if (value == handler->query_bit) {
512 /* have custom handler for this bit */
513 memcpy(&copy, handler, sizeof(copy));
514 mutex_unlock(&ec->lock);
515 if (copy.func) {
516 copy.func(copy.data);
517 } else if (copy.handle) {
518 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
519 }
520 return;
521 }
522 }
523 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Len Brown50526df2005-08-11 17:32:05 -0400526static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Len Brown50526df2005-08-11 17:32:05 -0400528 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300529 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200530
Márton Németh3ebe08a2007-11-21 03:23:26 +0300531 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400532 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400533 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300534 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500535
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400536 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
537 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
538 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
539 acpi_ec_gpe_query, ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400540 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
541 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300542 if (printk_ratelimit())
543 pr_info(PREFIX "non-query interrupt received,"
544 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400545 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400546 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300547
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400548 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400549 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/* --------------------------------------------------------------------------
553 Address Space Management
554 -------------------------------------------------------------------------- */
555
556static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400557acpi_ec_space_setup(acpi_handle region_handle,
558 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 /*
561 * The EC object is in the handler context and is needed
562 * when calling the acpi_ec_space_handler.
563 */
Len Brown50526df2005-08-11 17:32:05 -0400564 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
565 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return AE_OK;
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400571acpi_ec_space_handler(u32 function, acpi_physical_address address,
572 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400573 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300575 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400576 int result = 0, i = 0;
577 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400582 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400584
585 if (bits != 8 && acpi_strict)
586 return AE_BAD_PARAMETER;
587
588 while (bits - i > 0) {
589 if (function == ACPI_READ) {
590 result = acpi_ec_read(ec, address, &temp);
591 (*value) |= ((acpi_integer)temp) << i;
592 } else {
593 temp = 0xff & ((*value) >> i);
594 result = acpi_ec_write(ec, address, temp);
595 }
596 i += 8;
597 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 switch (result) {
601 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* --------------------------------------------------------------------------
616 FS Interface (/proc)
617 -------------------------------------------------------------------------- */
618
Len Brown50526df2005-08-11 17:32:05 -0400619static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Len Brown50526df2005-08-11 17:32:05 -0400621static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300623 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!ec)
626 goto end;
627
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300628 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
629 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
630 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
631 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400632 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400633 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
638{
639 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
640}
641
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400642static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400643 .open = acpi_ec_info_open_fs,
644 .read = seq_read,
645 .llseek = seq_lseek,
646 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 .owner = THIS_MODULE,
648};
649
Len Brown50526df2005-08-11 17:32:05 -0400650static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Len Brown50526df2005-08-11 17:32:05 -0400652 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!acpi_device_dir(device)) {
655 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400656 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400658 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400662 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else {
666 entry->proc_fops = &acpi_ec_info_ops;
667 entry->data = acpi_driver_data(device);
668 entry->owner = THIS_MODULE;
669 }
670
Patrick Mocheld550d982006-06-27 00:41:40 -0400671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Len Brown50526df2005-08-11 17:32:05 -0400674static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (acpi_device_dir(device)) {
678 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
679 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
680 acpi_device_dir(device) = NULL;
681 }
682
Patrick Mocheld550d982006-06-27 00:41:40 -0400683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/* --------------------------------------------------------------------------
687 Driver Interface
688 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300689static acpi_status
690ec_parse_io_ports(struct acpi_resource *resource, void *context);
691
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300692static struct acpi_ec *make_acpi_ec(void)
693{
694 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
695 if (!ec)
696 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400697 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300698 mutex_init(&ec->lock);
699 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400700 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300701 return ec;
702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400704static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400705acpi_ec_register_query_methods(acpi_handle handle, u32 level,
706 void *context, void **return_value)
707{
708 struct acpi_namespace_node *node = handle;
709 struct acpi_ec *ec = context;
710 int value = 0;
711 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
712 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
713 }
714 return AE_OK;
715}
716
717static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400718ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400719{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400720 acpi_status status;
721
722 struct acpi_ec *ec = context;
723 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
724 ec_parse_io_ports, ec);
725 if (ACPI_FAILURE(status))
726 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400727
728 /* Get GPE bit assignment (EC events). */
729 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400730 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
731 if (ACPI_FAILURE(status))
732 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400733 /* Find and register all query methods */
734 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
735 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400736 /* Use the global lock for all EC transactions? */
737 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400738 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400739 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400740}
741
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400742static void ec_remove_handlers(struct acpi_ec *ec)
743{
744 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
745 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300746 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400747 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
748 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300749 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400750 ec->handlers_installed = 0;
751}
752
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400753static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400755 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400758 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300759 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
760 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
761
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400762 /* Check for boot EC */
763 if (boot_ec) {
764 if (boot_ec->handle == device->handle) {
765 /* Pre-loaded EC from DSDT, just move pointer */
766 ec = boot_ec;
767 boot_ec = NULL;
768 goto end;
769 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
770 /* ECDT-based EC, time to shut it down */
771 ec_remove_handlers(boot_ec);
772 kfree(boot_ec);
773 first_ec = boot_ec = NULL;
774 }
775 }
776
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300777 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400781 if (ec_parse_device(device->handle, 0, ec, NULL) !=
782 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300783 kfree(ec);
784 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400785 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300786 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400787 end:
788 if (!first_ec)
789 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300791 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300792 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400793 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300794 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400795 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Len Brown50526df2005-08-11 17:32:05 -0400799static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300801 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200802 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400808 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200809 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400810 list_del(&handler->node);
811 kfree(handler);
812 }
813 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300815 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300816 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300817 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400818 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300823ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300825 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300827 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /*
831 * The first address region returned is the data port, and
832 * the second address region returned is the status/command
833 * port.
834 */
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300835 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400836 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300837 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400838 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300839 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return AE_OK;
843}
844
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300845static int ec_install_handlers(struct acpi_ec *ec)
846{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300847 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400848 if (ec->handlers_installed)
849 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300850 status = acpi_install_gpe_handler(NULL, ec->gpe,
851 ACPI_GPE_EDGE_TRIGGERED,
852 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300853 if (ACPI_FAILURE(status))
854 return -ENODEV;
Alexey Starikovskiy01f224622007-03-07 22:28:00 +0300855
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300856 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
857 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
858
859 status = acpi_install_address_space_handler(ec->handle,
860 ACPI_ADR_SPACE_EC,
861 &acpi_ec_space_handler,
862 &acpi_ec_space_setup, ec);
863 if (ACPI_FAILURE(status)) {
864 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
865 return -ENODEV;
866 }
867
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400868 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300869 return 0;
870}
871
Len Brown50526df2005-08-11 17:32:05 -0400872static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300874 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400875 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 ec = acpi_driver_data(device);
881
882 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400885 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300886
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400887 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400888 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Len Brown50526df2005-08-11 17:32:05 -0400892static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300894 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300898 if (!ec)
899 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400900 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400901
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500905int __init acpi_boot_ec_enable(void)
906{
907 if (!boot_ec || boot_ec->handlers_installed)
908 return 0;
909 if (!ec_install_handlers(boot_ec)) {
910 first_ec = boot_ec;
911 return 0;
912 }
913 return -EFAULT;
914}
915
Len Brown50526df2005-08-11 17:32:05 -0400916int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Len Brown50526df2005-08-11 17:32:05 -0400918 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300919 acpi_status status;
920 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300922 boot_ec = make_acpi_ec();
923 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300924 return -ENOMEM;
925 /*
926 * Generate a boot ec context
927 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300928 status = acpi_get_table(ACPI_SIG_ECDT, 1,
929 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400930 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300931 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400932 boot_ec->command_addr = ecdt_ptr->control.address;
933 boot_ec->data_addr = ecdt_ptr->data.address;
934 boot_ec->gpe = ecdt_ptr->gpe;
935 boot_ec->handle = ACPI_ROOT_OBJECT;
936 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300937 /* This workaround is needed only on some broken machines,
938 * which require early EC, but fail to provide ECDT */
939 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400940 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
941 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
942 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400943 /* Check that acpi_get_devices actually find something */
944 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400945 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300946 /* We really need to limit this workaround, the only ASUS,
947 * which needs it, has fake EC._INI method, so use it as flag.
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500948 * Keep boot_ec struct as it will be needed soon.
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300949 */
950 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500951 return -ENODEV;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300954 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300955 if (!ret) {
956 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300957 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300958 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300959 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300960 kfree(boot_ec);
961 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -ENODEV;
963}
964
Len Brown50526df2005-08-11 17:32:05 -0400965static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Len Brown50526df2005-08-11 17:32:05 -0400967 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
973 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400974 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /* Now register the driver for the EC */
977 result = acpi_bus_register_driver(&acpi_ec_driver);
978 if (result < 0) {
979 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400980 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Patrick Mocheld550d982006-06-27 00:41:40 -0400983 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986subsys_initcall(acpi_ec_init);
987
988/* EC driver currently not unloadable */
989#if 0
Len Brown50526df2005-08-11 17:32:05 -0400990static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 acpi_bus_unregister_driver(&acpi_ec_driver);
994
995 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
996
Patrick Mocheld550d982006-06-27 00:41:40 -0400997 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400999#endif /* 0 */