blob: c434f6571ab8b04f98762f209945412b51c886c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +04002 * ec.c - ACPI Embedded Controller Driver (v2.1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +04004 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03005 * 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
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040029/* Uncomment next line to get verbose printout */
Márton Némethd772b3b2008-01-23 22:34:09 -050030/* #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>
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040041#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
Len Browneb27cae2009-07-06 23:40:19 -040045#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_EC_DEVICE_NAME "Embedded Controller"
49#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040050
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030051#undef PREFIX
52#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040053
Denis M. Sadykov703959d2006-09-26 19:50:33 +040054/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
56#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050057#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040059
Denis M. Sadykov703959d2006-09-26 19:50:33 +040060/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030061enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030062 ACPI_EC_COMMAND_READ = 0x80,
63 ACPI_EC_COMMAND_WRITE = 0x81,
64 ACPI_EC_BURST_ENABLE = 0x82,
65 ACPI_EC_BURST_DISABLE = 0x83,
66 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030067};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040068
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030069#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Alexey Starikovskiy34ff4db2009-04-01 00:25:10 -040071#define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072
Alexey Starikovskiy06cf7d32008-11-09 19:01:06 +030073#define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040074 per one transaction */
75
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040076enum {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040077 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040078 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
79 * for status change */
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +030080 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040081 EC_FLAGS_GPE_STORM, /* GPE storm detected */
82 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
83 * OpReg are installed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040085
86/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030087/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040088typedef int (*acpi_ec_query_func) (void *data);
89
90struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
93 acpi_handle handle;
94 void *data;
95 u8 query_bit;
96};
97
Alexey Starikovskiy84632002008-09-26 00:54:28 +040098struct transaction {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040099 const u8 *wdata;
100 u8 *rdata;
101 unsigned short irq_count;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400102 u8 command;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300103 u8 wi;
104 u8 ri;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400105 u8 wlen;
106 u8 rlen;
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300107 bool done;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400108};
109
Adrian Bunka854e082006-12-19 12:56:12 -0800110static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400111 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300112 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400113 unsigned long command_addr;
114 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400115 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400116 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300117 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400119 struct list_head list;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400120 struct transaction *curr;
121 spinlock_t curr_lock;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300122} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123
Alexey Starikovskiy5423a0c2009-02-21 12:18:13 -0500124static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
129
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400130static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300132 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500133 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300134 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400138{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300139 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500140 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400141 return x;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400142}
143
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400144static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400145{
Márton Németh86dae012008-01-23 22:33:06 -0500146 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400147 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148}
149
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400150static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400151{
Márton Németh86dae012008-01-23 22:33:06 -0500152 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400154}
155
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400156static int ec_transaction_done(struct acpi_ec *ec)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400157{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400158 unsigned long flags;
159 int ret = 0;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400160 spin_lock_irqsave(&ec->curr_lock, flags);
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300161 if (!ec->curr || ec->curr->done)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400162 ret = 1;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400163 spin_unlock_irqrestore(&ec->curr_lock, flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400164 return ret;
165}
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400166
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300167static void start_transaction(struct acpi_ec *ec)
168{
169 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
170 ec->curr->done = false;
171 acpi_ec_write_cmd(ec, ec->curr->command);
172}
173
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400174static void gpe_transaction(struct acpi_ec *ec, u8 status)
175{
176 unsigned long flags;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400177 spin_lock_irqsave(&ec->curr_lock, flags);
178 if (!ec->curr)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400179 goto unlock;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300180 if (ec->curr->wlen > ec->curr->wi) {
181 if ((status & ACPI_EC_FLAG_IBF) == 0)
182 acpi_ec_write_data(ec,
183 ec->curr->wdata[ec->curr->wi++]);
184 else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300185 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300186 } else if (ec->curr->rlen > ec->curr->ri) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400187 if ((status & ACPI_EC_FLAG_OBF) == 1) {
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300188 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
189 if (ec->curr->rlen == ec->curr->ri)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300190 ec->curr->done = true;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400191 } else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300192 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300193 } else if (ec->curr->wlen == ec->curr->wi &&
194 (status & ACPI_EC_FLAG_IBF) == 0)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300195 ec->curr->done = true;
196 goto unlock;
197err:
198 /* false interrupt, state didn't change */
Alexey Starikovskiy7b4d4692008-11-13 12:00:03 +0300199 if (in_interrupt())
200 ++ec->curr->irq_count;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400201unlock:
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400202 spin_unlock_irqrestore(&ec->curr_lock, flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400203}
204
205static int acpi_ec_wait(struct acpi_ec *ec)
206{
207 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
208 msecs_to_jiffies(ACPI_EC_DELAY)))
209 return 0;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300210 /* try restart command if we get any false interrupts */
211 if (ec->curr->irq_count &&
212 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
213 pr_debug(PREFIX "controller reset, restart transaction\n");
214 start_transaction(ec);
215 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
216 msecs_to_jiffies(ACPI_EC_DELAY)))
217 return 0;
218 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400219 /* missing GPEs, switch back to poll mode */
220 if (printk_ratelimit())
221 pr_info(PREFIX "missing confirmations, "
222 "switch off interrupt mode.\n");
223 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
224 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
225 return 1;
226}
227
228static void acpi_ec_gpe_query(void *ec_cxt);
229
230static int ec_check_sci(struct acpi_ec *ec, u8 state)
231{
232 if (state & ACPI_EC_FLAG_SCI) {
233 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
234 return acpi_os_execute(OSL_EC_BURST_HANDLER,
235 acpi_ec_gpe_query, ec);
236 }
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400237 return 0;
238}
239
Alexey Starikovskiy34ff4db2009-04-01 00:25:10 -0400240static void ec_delay(void)
241{
242 /* EC in MSI notebooks don't tolerate delays other than 550 usec */
243 if (EC_FLAGS_MSI)
244 udelay(ACPI_EC_DELAY);
245 else
246 /* Use shortest sleep available */
247 msleep(1);
248}
249
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400250static int ec_poll(struct acpi_ec *ec)
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300251{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400252 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
Alexey Starikovskiy34ff4db2009-04-01 00:25:10 -0400253 udelay(ACPI_EC_CDELAY);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400254 while (time_before(jiffies, delay)) {
255 gpe_transaction(ec, acpi_ec_read_status(ec));
Alexey Starikovskiy34ff4db2009-04-01 00:25:10 -0400256 ec_delay();
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400257 if (ec_transaction_done(ec))
Zhao Yakui9d699ed2008-08-11 10:33:31 +0800258 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300259 }
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300260 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500261}
262
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400263static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
264 struct transaction *t,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200265 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400266{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400267 unsigned long tmp;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400268 int ret = 0;
Márton Németh3ebe08a2007-11-21 03:23:26 +0300269 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400270 /* disable GPE during transaction if storm is detected */
271 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
272 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400273 acpi_disable_gpe(NULL, ec->gpe);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400274 }
Alexey Starikovskiy5423a0c2009-02-21 12:18:13 -0500275 if (EC_FLAGS_MSI)
276 udelay(ACPI_EC_DELAY);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400277 /* start transaction */
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400278 spin_lock_irqsave(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400279 /* following two actions should be kept atomic */
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400280 ec->curr = t;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300281 start_transaction(ec);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400282 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400283 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400284 spin_unlock_irqrestore(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400285 /* if we selected poll mode or failed in GPE-mode do a poll loop */
286 if (force_poll ||
287 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
288 acpi_ec_wait(ec))
289 ret = ec_poll(ec);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300290 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400291 spin_lock_irqsave(&ec->curr_lock, tmp);
292 ec->curr = NULL;
293 spin_unlock_irqrestore(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400294 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
295 /* check if we received SCI during transaction */
296 ec_check_sci(ec, acpi_ec_read_status(ec));
297 /* it is safe to enable GPE outside of transaction */
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400298 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400299 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400300 t->irq_count > ACPI_EC_STORM_THRESHOLD) {
Alan Jenkinsf8248432008-11-01 11:05:26 +0000301 pr_info(PREFIX "GPE storm detected, "
302 "transactions will use polling mode\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400303 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
304 }
305 return ret;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400306}
307
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400308static int ec_check_ibf0(struct acpi_ec *ec)
309{
310 u8 status = acpi_ec_read_status(ec);
311 return (status & ACPI_EC_FLAG_IBF) == 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400312}
313
Alexey Starikovskiyc0ff1772008-10-16 02:02:33 +0400314static int ec_wait_ibf0(struct acpi_ec *ec)
315{
316 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
317 /* interrupt wait manually if GPE mode is not active */
318 unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
319 msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
320 while (time_before(jiffies, delay))
321 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
322 return 0;
323 return -ETIME;
324}
325
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400326static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200327 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400328{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400329 int status;
Len Brown50526df2005-08-11 17:32:05 -0400330 u32 glk;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400331 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return -EINVAL;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400333 if (t->rdata)
334 memset(t->rdata, 0, t->rlen);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300335 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400336 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300338 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400339 status = -ENODEV;
340 goto unlock;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Alexey Starikovskiyc0ff1772008-10-16 02:02:33 +0400343 if (ec_wait_ibf0(ec)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300344 pr_err(PREFIX "input buffer is not empty, "
345 "aborting transaction\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400346 status = -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500347 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400348 }
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400349 status = acpi_ec_transaction_unlocked(ec, t, force_poll);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400350end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400351 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 acpi_release_global_lock(glk);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400353unlock:
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300354 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300358/*
359 * Note: samsung nv5000 doesn't work with ec burst mode.
360 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
361 */
Roel Kluin8a383ef2008-12-09 20:45:30 +0100362static int acpi_ec_burst_enable(struct acpi_ec *ec)
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300363{
364 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400365 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
366 .wdata = NULL, .rdata = &d,
367 .wlen = 0, .rlen = 1};
368
369 return acpi_ec_transaction(ec, &t, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300370}
371
Roel Kluin8a383ef2008-12-09 20:45:30 +0100372static int acpi_ec_burst_disable(struct acpi_ec *ec)
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300373{
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400374 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
375 .wdata = NULL, .rdata = NULL,
376 .wlen = 0, .rlen = 0};
377
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400378 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400379 acpi_ec_transaction(ec, &t, 0) : 0;
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300380}
381
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300382static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400383{
384 int result;
385 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400386 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
387 .wdata = &address, .rdata = &d,
388 .wlen = 1, .rlen = 1};
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400389
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400390 result = acpi_ec_transaction(ec, &t, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400391 *data = d;
392 return result;
393}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400394
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400395static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
396{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397 u8 wdata[2] = { address, data };
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400398 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
399 .wdata = wdata, .rdata = NULL,
400 .wlen = 2, .rlen = 0};
401
402 return acpi_ec_transaction(ec, &t, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
406 * Externally callable EC access functions. For now, assume 1 EC only
407 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300408int ec_burst_enable(void)
409{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300410 if (!first_ec)
411 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300412 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300413}
414
415EXPORT_SYMBOL(ec_burst_enable);
416
417int ec_burst_disable(void)
418{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300419 if (!first_ec)
420 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300421 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300422}
423
424EXPORT_SYMBOL(ec_burst_disable);
425
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300426int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400429 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (!first_ec)
432 return -ENODEV;
433
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300434 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!err) {
437 *val = temp_data;
438 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400439 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return err;
441}
Len Brown50526df2005-08-11 17:32:05 -0400442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443EXPORT_SYMBOL(ec_read);
444
Len Brown50526df2005-08-11 17:32:05 -0400445int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int err;
448
449 if (!first_ec)
450 return -ENODEV;
451
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300452 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 return err;
455}
Len Brown50526df2005-08-11 17:32:05 -0400456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457EXPORT_SYMBOL(ec_write);
458
Randy Dunlap616362d2006-10-27 01:47:34 -0400459int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500460 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200461 u8 * rdata, unsigned rdata_len,
462 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400463{
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400464 struct transaction t = {.command = command,
465 .wdata = wdata, .rdata = rdata,
466 .wlen = wdata_len, .rlen = rdata_len};
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400467 if (!first_ec)
468 return -ENODEV;
469
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400470 return acpi_ec_transaction(first_ec, &t, force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400471}
Luming Yu45bea152005-07-23 04:08:00 -0400472
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400473EXPORT_SYMBOL(ec_transaction);
474
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300475static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400476{
477 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300478 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400479 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
480 .wdata = NULL, .rdata = &d,
481 .wlen = 0, .rlen = 1};
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300482 if (!ec || !data)
483 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400484
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300485 /*
486 * Query the EC to find out which _Qxx method we need to evaluate.
487 * Note that successful completion of the query causes the ACPI_EC_SCI
488 * bit to be cleared (and thus clearing the interrupt source).
489 */
Luming Yu45bea152005-07-23 04:08:00 -0400490
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400491 result = acpi_ec_transaction(ec, &t, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300492 if (result)
493 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400494
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300495 if (!d)
496 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400497
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300498 *data = d;
499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/* --------------------------------------------------------------------------
503 Event Management
504 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400505int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
506 acpi_handle handle, acpi_ec_query_func func,
507 void *data)
508{
509 struct acpi_ec_query_handler *handler =
510 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
511 if (!handler)
512 return -ENOMEM;
513
514 handler->query_bit = query_bit;
515 handler->handle = handle;
516 handler->func = func;
517 handler->data = data;
518 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400519 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400520 mutex_unlock(&ec->lock);
521 return 0;
522}
523
524EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
525
526void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
527{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200528 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400529 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200530 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400531 if (query_bit == handler->query_bit) {
532 list_del(&handler->node);
533 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400534 }
535 }
536 mutex_unlock(&ec->lock);
537}
538
539EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Len Brown50526df2005-08-11 17:32:05 -0400541static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300543 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400544 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400545 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400546
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300547 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300548 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400549 mutex_lock(&ec->lock);
550 list_for_each_entry(handler, &ec->list, node) {
551 if (value == handler->query_bit) {
552 /* have custom handler for this bit */
553 memcpy(&copy, handler, sizeof(copy));
554 mutex_unlock(&ec->lock);
555 if (copy.func) {
556 copy.func(copy.data);
557 } else if (copy.handle) {
558 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
559 }
560 return;
561 }
562 }
563 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Len Brown50526df2005-08-11 17:32:05 -0400566static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300568 struct acpi_ec *ec = data;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400569 u8 status;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200570
Márton Németh3ebe08a2007-11-21 03:23:26 +0300571 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400572 status = acpi_ec_read_status(ec);
573
Alexey Starikovskiy85179342008-11-11 12:54:11 +0300574 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
575 gpe_transaction(ec, status);
576 if (ec_transaction_done(ec) &&
577 (status & ACPI_EC_FLAG_IBF) == 0)
578 wake_up(&ec->wait);
579 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500580
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400581 ec_check_sci(ec, status);
582 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
583 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400584 /* this is non-query, must be confirmation */
Alan Jenkinsf8248432008-11-01 11:05:26 +0000585 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
586 if (printk_ratelimit())
587 pr_info(PREFIX "non-query interrupt received,"
588 " switching to interrupt mode\n");
589 } else {
590 /* hush, STORM switches the mode every transaction */
591 pr_debug(PREFIX "non-query interrupt received,"
Márton Németh3ebe08a2007-11-21 03:23:26 +0300592 " switching to interrupt mode\n");
Alan Jenkinsf8248432008-11-01 11:05:26 +0000593 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400594 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400595 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400596 return ACPI_INTERRUPT_HANDLED;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* --------------------------------------------------------------------------
600 Address Space Management
601 -------------------------------------------------------------------------- */
602
603static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400604acpi_ec_space_handler(u32 function, acpi_physical_address address,
605 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400606 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300608 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300609 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400610 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400613 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400615 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400617
618 if (bits != 8 && acpi_strict)
619 return AE_BAD_PARAMETER;
620
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300621 acpi_ec_burst_enable(ec);
622
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300623 if (function == ACPI_READ) {
624 result = acpi_ec_read(ec, address, &temp);
625 *value = temp;
626 } else {
627 temp = 0xff & (*value);
628 result = acpi_ec_write(ec, address, temp);
629 }
630
631 for (i = 8; unlikely(bits - i > 0); i += 8) {
632 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400633 if (function == ACPI_READ) {
634 result = acpi_ec_read(ec, address, &temp);
635 (*value) |= ((acpi_integer)temp) << i;
636 } else {
637 temp = 0xff & ((*value) >> i);
638 result = acpi_ec_write(ec, address, temp);
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300642 acpi_ec_burst_disable(ec);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 switch (result) {
645 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400646 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400649 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400655 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/* --------------------------------------------------------------------------
660 FS Interface (/proc)
661 -------------------------------------------------------------------------- */
662
Len Brown50526df2005-08-11 17:32:05 -0400663static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Len Brown50526df2005-08-11 17:32:05 -0400665static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300667 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!ec)
670 goto end;
671
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300672 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
673 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
674 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
675 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400676 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400677 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
682{
683 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
684}
685
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100686static const struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400687 .open = acpi_ec_info_open_fs,
688 .read = seq_read,
689 .llseek = seq_lseek,
690 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 .owner = THIS_MODULE,
692};
693
Len Brown50526df2005-08-11 17:32:05 -0400694static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Len Brown50526df2005-08-11 17:32:05 -0400696 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (!acpi_device_dir(device)) {
699 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400700 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400702 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700705 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
706 acpi_device_dir(device),
707 &acpi_ec_info_ops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -0400710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Len Brown50526df2005-08-11 17:32:05 -0400713static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (acpi_device_dir(device)) {
717 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
718 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
719 acpi_device_dir(device) = NULL;
720 }
721
Patrick Mocheld550d982006-06-27 00:41:40 -0400722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/* --------------------------------------------------------------------------
726 Driver Interface
727 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300728static acpi_status
729ec_parse_io_ports(struct acpi_resource *resource, void *context);
730
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300731static struct acpi_ec *make_acpi_ec(void)
732{
733 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
734 if (!ec)
735 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400736 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300737 mutex_init(&ec->lock);
738 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400739 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400740 spin_lock_init(&ec->curr_lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 return ec;
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400744static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400745acpi_ec_register_query_methods(acpi_handle handle, u32 level,
746 void *context, void **return_value)
747{
Lin Ming0175d562008-12-16 16:46:12 +0800748 char node_name[5];
749 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400750 struct acpi_ec *ec = context;
751 int value = 0;
Lin Ming0175d562008-12-16 16:46:12 +0800752 acpi_status status;
753
754 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
755
756 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400757 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
758 }
759 return AE_OK;
760}
761
762static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400763ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400764{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400765 acpi_status status;
Alexey Starikovskiyd21cf3c2008-11-03 14:26:40 -0500766 unsigned long long tmp = 0;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400767
768 struct acpi_ec *ec = context;
Alexey Starikovskiya5032bf2009-04-01 01:33:15 -0400769
770 /* clear addr values, ec_parse_io_ports depend on it */
771 ec->command_addr = ec->data_addr = 0;
772
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400773 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
774 ec_parse_io_ports, ec);
775 if (ACPI_FAILURE(status))
776 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400777
778 /* Get GPE bit assignment (EC events). */
779 /* TODO: Add support for _GPE returning a package */
Matthew Wilcox27663c52008-10-10 02:22:59 -0400780 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400781 if (ACPI_FAILURE(status))
782 return status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400783 ec->gpe = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400784 /* Use the global lock for all EC transactions? */
Alexey Starikovskiyd21cf3c2008-11-03 14:26:40 -0500785 tmp = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400786 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
787 ec->global_lock = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400788 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400789 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400790}
791
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400792static void ec_remove_handlers(struct acpi_ec *ec)
793{
794 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
795 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300796 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400797 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
798 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300799 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400800 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400801}
802
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400803static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400805 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300809 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
810 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
811
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400812 /* Check for boot EC */
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300813 if (boot_ec &&
814 (boot_ec->handle == device->handle ||
815 boot_ec->handle == ACPI_ROOT_OBJECT)) {
816 ec = boot_ec;
817 boot_ec = NULL;
818 } else {
819 ec = make_acpi_ec();
820 if (!ec)
821 return -ENOMEM;
Alexey Starikovskiya5032bf2009-04-01 01:33:15 -0400822 }
823 if (ec_parse_device(device->handle, 0, ec, NULL) !=
824 AE_CTRL_TERMINATE) {
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300825 kfree(ec);
826 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400827 }
828
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300829 ec->handle = device->handle;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300830
831 /* Find and register all query methods */
832 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
833 acpi_ec_register_query_methods, ec, NULL);
834
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400835 if (!first_ec)
836 first_ec = ec;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700837 device->driver_data = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300838 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300839 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400840 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300841 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400842 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Len Brown50526df2005-08-11 17:32:05 -0400846static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300848 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200849 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400855 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200856 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400857 list_del(&handler->node);
858 kfree(handler);
859 }
860 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 acpi_ec_remove_fs(device);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700862 device->driver_data = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300863 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300864 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400865 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300870ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300872 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300874 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
878 * The first address region returned is the data port, and
879 * the second address region returned is the status/command
880 * port.
881 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300882 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400883 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300884 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400885 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300886 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return AE_OK;
890}
891
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300892static int ec_install_handlers(struct acpi_ec *ec)
893{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300894 acpi_status status;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400895 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400896 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300897 status = acpi_install_gpe_handler(NULL, ec->gpe,
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400898 ACPI_GPE_EDGE_TRIGGERED,
899 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300900 if (ACPI_FAILURE(status))
901 return -ENODEV;
902 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400903 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300904 status = acpi_install_address_space_handler(ec->handle,
905 ACPI_ADR_SPACE_EC,
906 &acpi_ec_space_handler,
Alexey Starikovskiy6d9e1122008-03-24 23:22:29 +0300907 NULL, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300908 if (ACPI_FAILURE(status)) {
Zhao Yakui20edd742008-10-17 02:02:44 -0400909 if (status == AE_NOT_FOUND) {
910 /*
911 * Maybe OS fails in evaluating the _REG object.
912 * The AE_NOT_FOUND error will be ignored and OS
913 * continue to initialize EC.
914 */
915 printk(KERN_ERR "Fail in evaluating the _REG object"
916 " of EC device. Broken bios is suspected.\n");
917 } else {
918 acpi_remove_gpe_handler(NULL, ec->gpe,
919 &acpi_ec_gpe_handler);
920 return -ENODEV;
921 }
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300922 }
923
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400924 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300925 return 0;
926}
927
Len Brown50526df2005-08-11 17:32:05 -0400928static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300930 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400931 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 ec = acpi_driver_data(device);
937
938 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400941 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300942
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400943 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400944 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400945 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Len Brown50526df2005-08-11 17:32:05 -0400948static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300950 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300954 if (!ec)
955 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400956 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400957
Patrick Mocheld550d982006-06-27 00:41:40 -0400958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500961int __init acpi_boot_ec_enable(void)
962{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400963 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500964 return 0;
965 if (!ec_install_handlers(boot_ec)) {
966 first_ec = boot_ec;
967 return 0;
968 }
969 return -EFAULT;
970}
971
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300972static const struct acpi_device_id ec_device_ids[] = {
973 {"PNP0C09", 0},
974 {"", 0},
975};
976
Len Brown50526df2005-08-11 17:32:05 -0400977int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300979 acpi_status status;
Alexey Starikovskiyc6cb0e82009-01-14 02:57:53 +0300980 struct acpi_ec *saved_ec = NULL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300981 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300983 boot_ec = make_acpi_ec();
984 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300985 return -ENOMEM;
986 /*
987 * Generate a boot ec context
988 */
Alexey Starikovskiy5423a0c2009-02-21 12:18:13 -0500989 if (dmi_name_in_vendors("Micro-Star") ||
990 dmi_name_in_vendors("Notebook")) {
991 pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
992 EC_FLAGS_MSI = 1;
993 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300994 status = acpi_get_table(ACPI_SIG_ECDT, 1,
995 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400996 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300997 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400998 boot_ec->command_addr = ecdt_ptr->control.address;
999 boot_ec->data_addr = ecdt_ptr->data.address;
1000 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -04001001 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +03001002 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
Alexey Starikovskiyc6cb0e82009-01-14 02:57:53 +03001003 /* Don't trust ECDT, which comes from ASUSTek */
Alexey Starikovskiya5032bf2009-04-01 01:33:15 -04001004 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001005 goto install;
Alexey Starikovskiyc6cb0e82009-01-14 02:57:53 +03001006 saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1007 if (!saved_ec)
1008 return -ENOMEM;
Alexey Starikovskiya5032bf2009-04-01 01:33:15 -04001009 memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001010 /* fall through */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001011 }
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001012 /* This workaround is needed only on some broken machines,
1013 * which require early EC, but fail to provide ECDT */
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001014 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1015 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1016 boot_ec, NULL);
1017 /* Check that acpi_get_devices actually find something */
1018 if (ACPI_FAILURE(status) || !boot_ec->handle)
1019 goto error;
Alexey Starikovskiyc6cb0e82009-01-14 02:57:53 +03001020 if (saved_ec) {
1021 /* try to find good ECDT from ASUSTek */
1022 if (saved_ec->command_addr != boot_ec->command_addr ||
1023 saved_ec->data_addr != boot_ec->data_addr ||
1024 saved_ec->gpe != boot_ec->gpe ||
1025 saved_ec->handle != boot_ec->handle)
1026 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
1027 "ECDT tables, which are very hard to workaround. "
1028 "Trying to use DSDT EC info instead. Please send "
1029 "output of acpidump to linux-acpi@vger.kernel.org\n");
1030 kfree(saved_ec);
1031 saved_ec = NULL;
1032 } else {
1033 /* We really need to limit this workaround, the only ASUS,
1034 * which needs it, has fake EC._INI method, so use it as flag.
1035 * Keep boot_ec struct as it will be needed soon.
1036 */
1037 acpi_handle dummy;
1038 if (!dmi_name_in_vendors("ASUS") ||
1039 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1040 &dummy)))
1041 return -ENODEV;
1042 }
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001043install:
1044 if (!ec_install_handlers(boot_ec)) {
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001045 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +03001046 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001047 }
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001048error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001049 kfree(boot_ec);
1050 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -ENODEV;
1052}
1053
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001054static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1055{
1056 struct acpi_ec *ec = acpi_driver_data(device);
1057 /* Stop using GPE */
1058 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1059 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001060 acpi_disable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001061 return 0;
1062}
1063
1064static int acpi_ec_resume(struct acpi_device *device)
1065{
1066 struct acpi_ec *ec = acpi_driver_data(device);
1067 /* Enable use of GPE back */
1068 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
Almer S. Tigelaar5aa63f02009-04-12 11:26:29 +00001069 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001070 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001071 return 0;
1072}
1073
1074static struct acpi_driver acpi_ec_driver = {
1075 .name = "ec",
1076 .class = ACPI_EC_CLASS,
1077 .ids = ec_device_ids,
1078 .ops = {
1079 .add = acpi_ec_add,
1080 .remove = acpi_ec_remove,
1081 .start = acpi_ec_start,
1082 .stop = acpi_ec_stop,
1083 .suspend = acpi_ec_suspend,
1084 .resume = acpi_ec_resume,
1085 },
1086};
1087
Bjorn Helgaasa5f820f2009-03-24 16:49:48 -06001088int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Len Brown50526df2005-08-11 17:32:05 -04001090 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1093 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001094 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* Now register the driver for the EC */
1097 result = acpi_bus_register_driver(&acpi_ec_driver);
1098 if (result < 0) {
1099 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001100 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Patrick Mocheld550d982006-06-27 00:41:40 -04001103 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/* EC driver currently not unloadable */
1107#if 0
Len Brown50526df2005-08-11 17:32:05 -04001108static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 acpi_bus_unregister_driver(&acpi_ec_driver);
1112
1113 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1114
Patrick Mocheld550d982006-06-27 00:41:40 -04001115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
Alexey Starikovskiy78439322007-10-22 14:18:43 +04001117#endif /* 0 */