blob: 445ecbabbe7686c3a2a00a548a96821cd02d46be [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
Márton Németh3ebe08a2007-11-21 03:23:26 +030050/* Uncomment next line to get verbose print outs*/
51/* #define DEBUG */
52
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 Starikovskiy080e4122007-10-22 14:18:30 +040085};
86
Len Brown50526df2005-08-11 17:32:05 -040087static int acpi_ec_remove(struct acpi_device *device, int type);
88static int acpi_ec_start(struct acpi_device *device);
89static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040090static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Thomas Renninger1ba90e32007-07-23 14:44:41 +020092static const struct acpi_device_id ec_device_ids[] = {
93 {"PNP0C09", 0},
94 {"", 0},
95};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static struct acpi_driver acpi_ec_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050098 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040099 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200100 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -0400101 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400102 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -0400103 .remove = acpi_ec_remove,
104 .start = acpi_ec_start,
105 .stop = acpi_ec_stop,
106 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400108
109/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300110/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400111typedef int (*acpi_ec_query_func) (void *data);
112
113struct acpi_ec_query_handler {
114 struct list_head node;
115 acpi_ec_query_func func;
116 acpi_handle handle;
117 void *data;
118 u8 query_bit;
119};
120
Adrian Bunka854e082006-12-19 12:56:12 -0800121static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400122 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300123 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400124 unsigned long command_addr;
125 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400127 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300128 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400129 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400130 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400131 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300132} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* --------------------------------------------------------------------------
135 Transaction Management
136 -------------------------------------------------------------------------- */
137
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400138static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300140 u8 x = inb(ec->command_addr);
141 pr_debug(PREFIX "---> status = 0x%2x\n", x);
142 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400145static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400146{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300147 u8 x = inb(ec->data_addr);
148 pr_debug(PREFIX "---> data = 0x%2x\n", x);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400150}
151
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400153{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300154 pr_debug(PREFIX "<--- command = 0x%2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400155 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400156}
157
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400158static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400159{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300160 pr_debug(PREFIX "<--- data = 0x%2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400161 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400162}
163
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400164static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400165{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400166 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500167 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300168 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400169 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400170 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300171 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400172 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400173 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400174 }
175
176 return 0;
177}
178
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400179static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400180{
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300181 int ret = 0;
182 if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) &&
183 test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags)))
184 force_poll = 1;
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300185 if (unlikely(test_bit(EC_FLAGS_WDATA, &ec->flags) &&
186 test_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags)))
187 force_poll = 1;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400188 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
189 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400190 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
191 msecs_to_jiffies(ACPI_EC_DELAY)))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300192 goto end;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400193 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
194 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300195 if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
196 /* miss address GPE, don't expect it anymore */
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300197 pr_info(PREFIX "missing address confirmation, "
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300198 "don't expect it any longer.\n");
199 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300200 } else if (test_bit(EC_FLAGS_WDATA, &ec->flags)) {
201 /* miss write data GPE, don't expect it */
202 pr_info(PREFIX "missing write data confirmation, "
203 "don't expect it any longer.\n");
204 set_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400205 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400206 /* missing GPEs, switch back to poll mode */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300207 if (printk_ratelimit())
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300208 pr_info(PREFIX "missing confirmations, "
Márton Németh3ebe08a2007-11-21 03:23:26 +0300209 "switch off interrupt mode.\n");
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400210 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400211 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300212 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400213 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400214 } else {
215 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
216 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
217 while (time_before(jiffies, delay)) {
218 if (acpi_ec_check_status(ec, event))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300219 goto end;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400220 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300221 }
Márton Németh3ebe08a2007-11-21 03:23:26 +0300222 pr_err(PREFIX "acpi_ec_wait timeout,"
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400223 " status = %d, expect_event = %d\n",
224 acpi_ec_read_status(ec), event);
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300225 ret = -ETIME;
226 end:
227 clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
228 return ret;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500229}
230
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400231static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300232 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200233 u8 * rdata, unsigned rdata_len,
234 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400235{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300236 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400237 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400238 acpi_ec_write_cmd(ec, command);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300239 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300240 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400241 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300242 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300243 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300244 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300245 goto end;
246 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300247 /* mark the address byte written to EC */
248 if (rdata_len + wdata_len > 1)
249 set_bit(EC_FLAGS_ADDRESS, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400250 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400251 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400252 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400253
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300254 if (!rdata_len) {
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300255 set_bit(EC_FLAGS_WDATA, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400256 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300257 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300258 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300259 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300260 goto end;
261 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400262 } else if (command == ACPI_EC_COMMAND_QUERY)
263 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400264
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300265 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400266 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, 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 "read timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300269 goto end;
270 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400271 /* Don't expect GPE after last read */
272 if (rdata_len > 1)
273 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400274 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400275 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300276 end:
Márton Németh3ebe08a2007-11-21 03:23:26 +0300277 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300278 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400279}
280
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400281static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300282 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200283 u8 * rdata, unsigned rdata_len,
284 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400285{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400286 int status;
Len Brown50526df2005-08-11 17:32:05 -0400287 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400289 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300292 if (rdata)
293 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300295 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400296 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300298 if (ACPI_FAILURE(status)) {
299 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500303
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400304 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400305 if (status) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300306 pr_err(PREFIX "input buffer is not empty, "
307 "aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500308 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300311 status = acpi_ec_transaction_unlocked(ec, command,
312 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200313 rdata, rdata_len,
314 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400315
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300316 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400318 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300320 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300325/*
326 * Note: samsung nv5000 doesn't work with ec burst mode.
327 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
328 */
329int acpi_ec_burst_enable(struct acpi_ec *ec)
330{
331 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200332 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300333}
334
335int acpi_ec_burst_disable(struct acpi_ec *ec)
336{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200337 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300338}
339
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300340static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400341{
342 int result;
343 u8 d;
344
345 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200346 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400347 *data = d;
348 return result;
349}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400350
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400351static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
352{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300353 u8 wdata[2] = { address, data };
354 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200355 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * Externally callable EC access functions. For now, assume 1 EC only
360 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300361int ec_burst_enable(void)
362{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300363 if (!first_ec)
364 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300365 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300366}
367
368EXPORT_SYMBOL(ec_burst_enable);
369
370int ec_burst_disable(void)
371{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300372 if (!first_ec)
373 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300374 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300375}
376
377EXPORT_SYMBOL(ec_burst_disable);
378
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300379int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400382 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!first_ec)
385 return -ENODEV;
386
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300387 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (!err) {
390 *val = temp_data;
391 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400392 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return err;
394}
Len Brown50526df2005-08-11 17:32:05 -0400395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396EXPORT_SYMBOL(ec_read);
397
Len Brown50526df2005-08-11 17:32:05 -0400398int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int err;
401
402 if (!first_ec)
403 return -ENODEV;
404
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300405 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 return err;
408}
Len Brown50526df2005-08-11 17:32:05 -0400409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410EXPORT_SYMBOL(ec_write);
411
Randy Dunlap616362d2006-10-27 01:47:34 -0400412int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500413 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200414 u8 * rdata, unsigned rdata_len,
415 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400416{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400417 if (!first_ec)
418 return -ENODEV;
419
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300420 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200421 wdata_len, rdata, rdata_len,
422 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400423}
Luming Yu45bea152005-07-23 04:08:00 -0400424
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400425EXPORT_SYMBOL(ec_transaction);
426
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300427static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400428{
429 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300430 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400431
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300432 if (!ec || !data)
433 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400434
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300435 /*
436 * Query the EC to find out which _Qxx method we need to evaluate.
437 * Note that successful completion of the query causes the ACPI_EC_SCI
438 * bit to be cleared (and thus clearing the interrupt source).
439 */
Luming Yu45bea152005-07-23 04:08:00 -0400440
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200441 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300442 if (result)
443 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400444
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300445 if (!d)
446 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400447
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300448 *data = d;
449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* --------------------------------------------------------------------------
453 Event Management
454 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400455int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
456 acpi_handle handle, acpi_ec_query_func func,
457 void *data)
458{
459 struct acpi_ec_query_handler *handler =
460 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
461 if (!handler)
462 return -ENOMEM;
463
464 handler->query_bit = query_bit;
465 handler->handle = handle;
466 handler->func = func;
467 handler->data = data;
468 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400469 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400470 mutex_unlock(&ec->lock);
471 return 0;
472}
473
474EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
475
476void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
477{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200478 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400479 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200480 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400481 if (query_bit == handler->query_bit) {
482 list_del(&handler->node);
483 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400484 }
485 }
486 mutex_unlock(&ec->lock);
487}
488
489EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Len Brown50526df2005-08-11 17:32:05 -0400491static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300493 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400494 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400495 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400496
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300497 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300498 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400499 mutex_lock(&ec->lock);
500 list_for_each_entry(handler, &ec->list, node) {
501 if (value == handler->query_bit) {
502 /* have custom handler for this bit */
503 memcpy(&copy, handler, sizeof(copy));
504 mutex_unlock(&ec->lock);
505 if (copy.func) {
506 copy.func(copy.data);
507 } else if (copy.handle) {
508 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
509 }
510 return;
511 }
512 }
513 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Len Brown50526df2005-08-11 17:32:05 -0400516static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Len Brown50526df2005-08-11 17:32:05 -0400518 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300519 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200520
Márton Németh3ebe08a2007-11-21 03:23:26 +0300521 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400522 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400523 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300524 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500525
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400526 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
527 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
528 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
529 acpi_ec_gpe_query, ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400530 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
531 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300532 if (printk_ratelimit())
533 pr_info(PREFIX "non-query interrupt received,"
534 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400535 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400536 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300537
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400538 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400539 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/* --------------------------------------------------------------------------
543 Address Space Management
544 -------------------------------------------------------------------------- */
545
546static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400547acpi_ec_space_setup(acpi_handle region_handle,
548 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 /*
551 * The EC object is in the handler context and is needed
552 * when calling the acpi_ec_space_handler.
553 */
Len Brown50526df2005-08-11 17:32:05 -0400554 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
555 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 return AE_OK;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400561acpi_ec_space_handler(u32 function, acpi_physical_address address,
562 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400563 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300565 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300566 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400567 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400572 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400574
575 if (bits != 8 && acpi_strict)
576 return AE_BAD_PARAMETER;
577
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300578 acpi_ec_burst_enable(ec);
579
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300580 if (function == ACPI_READ) {
581 result = acpi_ec_read(ec, address, &temp);
582 *value = temp;
583 } else {
584 temp = 0xff & (*value);
585 result = acpi_ec_write(ec, address, temp);
586 }
587
588 for (i = 8; unlikely(bits - i > 0); i += 8) {
589 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400590 if (function == ACPI_READ) {
591 result = acpi_ec_read(ec, address, &temp);
592 (*value) |= ((acpi_integer)temp) << i;
593 } else {
594 temp = 0xff & ((*value) >> i);
595 result = acpi_ec_write(ec, address, temp);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300599 acpi_ec_burst_disable(ec);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 switch (result) {
602 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* --------------------------------------------------------------------------
617 FS Interface (/proc)
618 -------------------------------------------------------------------------- */
619
Len Brown50526df2005-08-11 17:32:05 -0400620static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Len Brown50526df2005-08-11 17:32:05 -0400622static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300624 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!ec)
627 goto end;
628
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300629 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
630 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
631 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
632 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400633 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400634 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
639{
640 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
641}
642
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400643static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400644 .open = acpi_ec_info_open_fs,
645 .read = seq_read,
646 .llseek = seq_lseek,
647 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 .owner = THIS_MODULE,
649};
650
Len Brown50526df2005-08-11 17:32:05 -0400651static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Len Brown50526df2005-08-11 17:32:05 -0400653 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!acpi_device_dir(device)) {
656 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400657 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400663 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400665 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 else {
667 entry->proc_fops = &acpi_ec_info_ops;
668 entry->data = acpi_driver_data(device);
669 entry->owner = THIS_MODULE;
670 }
671
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Len Brown50526df2005-08-11 17:32:05 -0400675static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (acpi_device_dir(device)) {
679 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
680 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
681 acpi_device_dir(device) = NULL;
682 }
683
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/* --------------------------------------------------------------------------
688 Driver Interface
689 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300690static acpi_status
691ec_parse_io_ports(struct acpi_resource *resource, void *context);
692
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300693static struct acpi_ec *make_acpi_ec(void)
694{
695 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
696 if (!ec)
697 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400698 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300699 mutex_init(&ec->lock);
700 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400701 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300702 return ec;
703}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400705static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400706acpi_ec_register_query_methods(acpi_handle handle, u32 level,
707 void *context, void **return_value)
708{
709 struct acpi_namespace_node *node = handle;
710 struct acpi_ec *ec = context;
711 int value = 0;
712 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
713 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
714 }
715 return AE_OK;
716}
717
718static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400719ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400720{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400721 acpi_status status;
722
723 struct acpi_ec *ec = context;
724 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
725 ec_parse_io_ports, ec);
726 if (ACPI_FAILURE(status))
727 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400728
729 /* Get GPE bit assignment (EC events). */
730 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400731 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
732 if (ACPI_FAILURE(status))
733 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400734 /* Find and register all query methods */
735 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
736 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400737 /* Use the global lock for all EC transactions? */
738 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400739 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400740 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400741}
742
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400743static void ec_remove_handlers(struct acpi_ec *ec)
744{
745 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
746 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300747 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400748 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
749 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300750 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400751 ec->handlers_installed = 0;
752}
753
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400754static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400756 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400759 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300760 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
761 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
762
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400763 /* Check for boot EC */
764 if (boot_ec) {
765 if (boot_ec->handle == device->handle) {
766 /* Pre-loaded EC from DSDT, just move pointer */
767 ec = boot_ec;
768 boot_ec = NULL;
769 goto end;
770 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
771 /* ECDT-based EC, time to shut it down */
772 ec_remove_handlers(boot_ec);
773 kfree(boot_ec);
774 first_ec = boot_ec = NULL;
775 }
776 }
777
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300778 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400780 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400782 if (ec_parse_device(device->handle, 0, ec, NULL) !=
783 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300784 kfree(ec);
785 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400786 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300787 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400788 end:
789 if (!first_ec)
790 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300792 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300793 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400794 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300795 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400796 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Len Brown50526df2005-08-11 17:32:05 -0400800static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300802 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200803 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400809 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200810 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400811 list_del(&handler->node);
812 kfree(handler);
813 }
814 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300816 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300817 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300818 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400819 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300824ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300826 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300828 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * The first address region returned is the data port, and
833 * the second address region returned is the status/command
834 * port.
835 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300836 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400837 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300838 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400839 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300840 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return AE_OK;
844}
845
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300846static int ec_install_handlers(struct acpi_ec *ec)
847{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300848 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400849 if (ec->handlers_installed)
850 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300851 status = acpi_install_gpe_handler(NULL, ec->gpe,
852 ACPI_GPE_EDGE_TRIGGERED,
853 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300854 if (ACPI_FAILURE(status))
855 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300856
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300857 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
858 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
859
860 status = acpi_install_address_space_handler(ec->handle,
861 ACPI_ADR_SPACE_EC,
862 &acpi_ec_space_handler,
863 &acpi_ec_space_setup, ec);
864 if (ACPI_FAILURE(status)) {
865 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
866 return -ENODEV;
867 }
868
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400869 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300870 return 0;
871}
872
Len Brown50526df2005-08-11 17:32:05 -0400873static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300875 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400876 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 ec = acpi_driver_data(device);
882
883 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400884 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400886 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300887
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400888 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400889 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Len Brown50526df2005-08-11 17:32:05 -0400893static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300895 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300899 if (!ec)
900 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400901 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400902
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Len Brown50526df2005-08-11 17:32:05 -0400906int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Len Brown50526df2005-08-11 17:32:05 -0400908 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300909 acpi_status status;
910 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300912 boot_ec = make_acpi_ec();
913 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300914 return -ENOMEM;
915 /*
916 * Generate a boot ec context
917 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300918 status = acpi_get_table(ACPI_SIG_ECDT, 1,
919 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400920 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300921 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400922 boot_ec->command_addr = ecdt_ptr->control.address;
923 boot_ec->data_addr = ecdt_ptr->data.address;
924 boot_ec->gpe = ecdt_ptr->gpe;
925 boot_ec->handle = ACPI_ROOT_OBJECT;
926 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300927 /* This workaround is needed only on some broken machines,
928 * which require early EC, but fail to provide ECDT */
929 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400930 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
931 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
932 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400933 /* Check that acpi_get_devices actually find something */
934 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400935 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300936 /* We really need to limit this workaround, the only ASUS,
937 * which needs it, has fake EC._INI method, so use it as flag.
938 */
939 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
940 goto error;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300943 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300944 if (!ret) {
945 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300946 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300947 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300948 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300949 kfree(boot_ec);
950 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -ENODEV;
952}
953
Len Brown50526df2005-08-11 17:32:05 -0400954static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Len Brown50526df2005-08-11 17:32:05 -0400956 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
962 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400963 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* Now register the driver for the EC */
966 result = acpi_bus_register_driver(&acpi_ec_driver);
967 if (result < 0) {
968 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Patrick Mocheld550d982006-06-27 00:41:40 -0400972 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975subsys_initcall(acpi_ec_init);
976
977/* EC driver currently not unloadable */
978#if 0
Len Brown50526df2005-08-11 17:32:05 -0400979static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 acpi_bus_unregister_driver(&acpi_ec_driver);
983
984 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
985
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400988#endif /* 0 */