blob: 89d6d2868e8c7359f2e9d7c6f06c4efdda798f1f [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>
45#include <acpi/actypes.h>
46
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 Starikovskiye6e82a32008-03-21 17:06:57 +030071#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040073#define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts
74 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
Zhao Yakui25008222008-08-12 10:40:10 +0800124/*
125 * Some Asus system have exchanged ECDT data/command IO addresses.
126 */
127static int print_ecdt_error(const struct dmi_system_id *id)
128{
129 printk(KERN_NOTICE PREFIX "%s detected - "
130 "ECDT has exchanged control/data I/O address\n",
131 id->ident);
132 return 0;
133}
134
135static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
136 {
137 print_ecdt_error, "Asus L4R", {
138 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
139 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
140 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
141 {
142 print_ecdt_error, "Asus M6R", {
143 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
144 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
145 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
146 {},
147};
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* --------------------------------------------------------------------------
150 Transaction Management
151 -------------------------------------------------------------------------- */
152
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300155 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500156 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300157 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400160static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400161{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300162 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500163 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400164 return x;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400165}
166
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400167static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400168{
Márton Németh86dae012008-01-23 22:33:06 -0500169 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400170 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400171}
172
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400173static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400174{
Márton Németh86dae012008-01-23 22:33:06 -0500175 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400176 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400177}
178
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400179static int ec_transaction_done(struct acpi_ec *ec)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400180{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400181 unsigned long flags;
182 int ret = 0;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400183 spin_lock_irqsave(&ec->curr_lock, flags);
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300184 if (!ec->curr || ec->curr->done)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400185 ret = 1;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400186 spin_unlock_irqrestore(&ec->curr_lock, flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400187 return ret;
188}
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400189
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300190static void start_transaction(struct acpi_ec *ec)
191{
192 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
193 ec->curr->done = false;
194 acpi_ec_write_cmd(ec, ec->curr->command);
195}
196
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400197static void gpe_transaction(struct acpi_ec *ec, u8 status)
198{
199 unsigned long flags;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400200 spin_lock_irqsave(&ec->curr_lock, flags);
201 if (!ec->curr)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400202 goto unlock;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300203 if (ec->curr->wlen > ec->curr->wi) {
204 if ((status & ACPI_EC_FLAG_IBF) == 0)
205 acpi_ec_write_data(ec,
206 ec->curr->wdata[ec->curr->wi++]);
207 else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300208 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300209 } else if (ec->curr->rlen > ec->curr->ri) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400210 if ((status & ACPI_EC_FLAG_OBF) == 1) {
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300211 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
212 if (ec->curr->rlen == ec->curr->ri)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300213 ec->curr->done = true;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400214 } else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300215 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300216 } else if (ec->curr->wlen == ec->curr->wi &&
217 (status & ACPI_EC_FLAG_IBF) == 0)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300218 ec->curr->done = true;
219 goto unlock;
220err:
221 /* false interrupt, state didn't change */
222 ++ec->curr->irq_count;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400223unlock:
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400224 spin_unlock_irqrestore(&ec->curr_lock, flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400225}
226
227static int acpi_ec_wait(struct acpi_ec *ec)
228{
229 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
230 msecs_to_jiffies(ACPI_EC_DELAY)))
231 return 0;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300232 /* try restart command if we get any false interrupts */
233 if (ec->curr->irq_count &&
234 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
235 pr_debug(PREFIX "controller reset, restart transaction\n");
236 start_transaction(ec);
237 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
238 msecs_to_jiffies(ACPI_EC_DELAY)))
239 return 0;
240 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400241 /* missing GPEs, switch back to poll mode */
242 if (printk_ratelimit())
243 pr_info(PREFIX "missing confirmations, "
244 "switch off interrupt mode.\n");
245 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
246 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
247 return 1;
248}
249
250static void acpi_ec_gpe_query(void *ec_cxt);
251
252static int ec_check_sci(struct acpi_ec *ec, u8 state)
253{
254 if (state & ACPI_EC_FLAG_SCI) {
255 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
256 return acpi_os_execute(OSL_EC_BURST_HANDLER,
257 acpi_ec_gpe_query, ec);
258 }
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400259 return 0;
260}
261
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400262static int ec_poll(struct acpi_ec *ec)
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300263{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400264 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
Alexey Starikovskiy1cfe62c2008-10-28 00:35:30 +0300265 udelay(ACPI_EC_UDELAY);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400266 while (time_before(jiffies, delay)) {
267 gpe_transaction(ec, acpi_ec_read_status(ec));
Alexey Starikovskiy1cfe62c2008-10-28 00:35:30 +0300268 udelay(ACPI_EC_UDELAY);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400269 if (ec_transaction_done(ec))
Zhao Yakui9d699ed2008-08-11 10:33:31 +0800270 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300271 }
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300272 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500273}
274
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400275static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
276 struct transaction *t,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200277 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400278{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400279 unsigned long tmp;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400280 int ret = 0;
Márton Németh3ebe08a2007-11-21 03:23:26 +0300281 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400282 /* disable GPE during transaction if storm is detected */
283 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
284 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400285 acpi_disable_gpe(NULL, ec->gpe);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400286 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400287 /* start transaction */
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400288 spin_lock_irqsave(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400289 /* following two actions should be kept atomic */
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400290 ec->curr = t;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300291 start_transaction(ec);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400292 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400293 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400294 spin_unlock_irqrestore(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400295 /* if we selected poll mode or failed in GPE-mode do a poll loop */
296 if (force_poll ||
297 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
298 acpi_ec_wait(ec))
299 ret = ec_poll(ec);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300300 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400301 spin_lock_irqsave(&ec->curr_lock, tmp);
302 ec->curr = NULL;
303 spin_unlock_irqrestore(&ec->curr_lock, tmp);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400304 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
305 /* check if we received SCI during transaction */
306 ec_check_sci(ec, acpi_ec_read_status(ec));
307 /* it is safe to enable GPE outside of transaction */
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400308 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400309 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400310 t->irq_count > ACPI_EC_STORM_THRESHOLD) {
Alan Jenkinsf8248432008-11-01 11:05:26 +0000311 pr_info(PREFIX "GPE storm detected, "
312 "transactions will use polling mode\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400313 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
314 }
315 return ret;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400316}
317
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400318static int ec_check_ibf0(struct acpi_ec *ec)
319{
320 u8 status = acpi_ec_read_status(ec);
321 return (status & ACPI_EC_FLAG_IBF) == 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400322}
323
Alexey Starikovskiyc0ff1772008-10-16 02:02:33 +0400324static int ec_wait_ibf0(struct acpi_ec *ec)
325{
326 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
327 /* interrupt wait manually if GPE mode is not active */
328 unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
329 msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
330 while (time_before(jiffies, delay))
331 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
332 return 0;
333 return -ETIME;
334}
335
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400336static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200337 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400338{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400339 int status;
Len Brown50526df2005-08-11 17:32:05 -0400340 u32 glk;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400341 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return -EINVAL;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400343 if (t->rdata)
344 memset(t->rdata, 0, t->rlen);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300345 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400346 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300348 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400349 status = -ENODEV;
350 goto unlock;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Alexey Starikovskiyc0ff1772008-10-16 02:02:33 +0400353 if (ec_wait_ibf0(ec)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300354 pr_err(PREFIX "input buffer is not empty, "
355 "aborting transaction\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400356 status = -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500357 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400358 }
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400359 status = acpi_ec_transaction_unlocked(ec, t, force_poll);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400360end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400361 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 acpi_release_global_lock(glk);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400363unlock:
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300364 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400365 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300368/*
369 * Note: samsung nv5000 doesn't work with ec burst mode.
370 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
371 */
372int acpi_ec_burst_enable(struct acpi_ec *ec)
373{
374 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400375 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
376 .wdata = NULL, .rdata = &d,
377 .wlen = 0, .rlen = 1};
378
379 return acpi_ec_transaction(ec, &t, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300380}
381
382int acpi_ec_burst_disable(struct acpi_ec *ec)
383{
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400384 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
385 .wdata = NULL, .rdata = NULL,
386 .wlen = 0, .rlen = 0};
387
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400388 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400389 acpi_ec_transaction(ec, &t, 0) : 0;
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300390}
391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400393{
394 int result;
395 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400396 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
397 .wdata = &address, .rdata = &d,
398 .wlen = 1, .rlen = 1};
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400399
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400400 result = acpi_ec_transaction(ec, &t, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400401 *data = d;
402 return result;
403}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400404
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400405static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
406{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300407 u8 wdata[2] = { address, data };
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400408 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
409 .wdata = wdata, .rdata = NULL,
410 .wlen = 2, .rlen = 0};
411
412 return acpi_ec_transaction(ec, &t, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
416 * Externally callable EC access functions. For now, assume 1 EC only
417 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300418int ec_burst_enable(void)
419{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300420 if (!first_ec)
421 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300422 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300423}
424
425EXPORT_SYMBOL(ec_burst_enable);
426
427int ec_burst_disable(void)
428{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300429 if (!first_ec)
430 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300431 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300432}
433
434EXPORT_SYMBOL(ec_burst_disable);
435
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300436int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400439 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!first_ec)
442 return -ENODEV;
443
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300444 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!err) {
447 *val = temp_data;
448 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400449 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return err;
451}
Len Brown50526df2005-08-11 17:32:05 -0400452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453EXPORT_SYMBOL(ec_read);
454
Len Brown50526df2005-08-11 17:32:05 -0400455int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int err;
458
459 if (!first_ec)
460 return -ENODEV;
461
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300462 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 return err;
465}
Len Brown50526df2005-08-11 17:32:05 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467EXPORT_SYMBOL(ec_write);
468
Randy Dunlap616362d2006-10-27 01:47:34 -0400469int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500470 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200471 u8 * rdata, unsigned rdata_len,
472 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400473{
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400474 struct transaction t = {.command = command,
475 .wdata = wdata, .rdata = rdata,
476 .wlen = wdata_len, .rlen = rdata_len};
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400477 if (!first_ec)
478 return -ENODEV;
479
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400480 return acpi_ec_transaction(first_ec, &t, force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400481}
Luming Yu45bea152005-07-23 04:08:00 -0400482
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400483EXPORT_SYMBOL(ec_transaction);
484
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300485static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400486{
487 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300488 u8 d;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400489 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
490 .wdata = NULL, .rdata = &d,
491 .wlen = 0, .rlen = 1};
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300492 if (!ec || !data)
493 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400494
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300495 /*
496 * Query the EC to find out which _Qxx method we need to evaluate.
497 * Note that successful completion of the query causes the ACPI_EC_SCI
498 * bit to be cleared (and thus clearing the interrupt source).
499 */
Luming Yu45bea152005-07-23 04:08:00 -0400500
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400501 result = acpi_ec_transaction(ec, &t, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300502 if (result)
503 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400504
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300505 if (!d)
506 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400507
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300508 *data = d;
509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/* --------------------------------------------------------------------------
513 Event Management
514 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400515int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
516 acpi_handle handle, acpi_ec_query_func func,
517 void *data)
518{
519 struct acpi_ec_query_handler *handler =
520 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
521 if (!handler)
522 return -ENOMEM;
523
524 handler->query_bit = query_bit;
525 handler->handle = handle;
526 handler->func = func;
527 handler->data = data;
528 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400529 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400530 mutex_unlock(&ec->lock);
531 return 0;
532}
533
534EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
535
536void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
537{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200538 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400539 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200540 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400541 if (query_bit == handler->query_bit) {
542 list_del(&handler->node);
543 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400544 }
545 }
546 mutex_unlock(&ec->lock);
547}
548
549EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Len Brown50526df2005-08-11 17:32:05 -0400551static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300553 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400554 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400555 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400556
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300557 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300558 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400559 mutex_lock(&ec->lock);
560 list_for_each_entry(handler, &ec->list, node) {
561 if (value == handler->query_bit) {
562 /* have custom handler for this bit */
563 memcpy(&copy, handler, sizeof(copy));
564 mutex_unlock(&ec->lock);
565 if (copy.func) {
566 copy.func(copy.data);
567 } else if (copy.handle) {
568 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
569 }
570 return;
571 }
572 }
573 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400574}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Len Brown50526df2005-08-11 17:32:05 -0400576static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300578 struct acpi_ec *ec = data;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400579 u8 status;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200580
Márton Németh3ebe08a2007-11-21 03:23:26 +0300581 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400582 status = acpi_ec_read_status(ec);
583
584 gpe_transaction(ec, status);
585 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300586 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500587
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400588 ec_check_sci(ec, status);
589 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
590 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400591 /* this is non-query, must be confirmation */
Alan Jenkinsf8248432008-11-01 11:05:26 +0000592 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
593 if (printk_ratelimit())
594 pr_info(PREFIX "non-query interrupt received,"
595 " switching to interrupt mode\n");
596 } else {
597 /* hush, STORM switches the mode every transaction */
598 pr_debug(PREFIX "non-query interrupt received,"
Márton Németh3ebe08a2007-11-21 03:23:26 +0300599 " switching to interrupt mode\n");
Alan Jenkinsf8248432008-11-01 11:05:26 +0000600 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400601 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400602 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400603 return ACPI_INTERRUPT_HANDLED;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/* --------------------------------------------------------------------------
607 Address Space Management
608 -------------------------------------------------------------------------- */
609
610static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400611acpi_ec_space_handler(u32 function, acpi_physical_address address,
612 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400613 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300615 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300616 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400617 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400620 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400622 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400624
625 if (bits != 8 && acpi_strict)
626 return AE_BAD_PARAMETER;
627
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300628 acpi_ec_burst_enable(ec);
629
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300630 if (function == ACPI_READ) {
631 result = acpi_ec_read(ec, address, &temp);
632 *value = temp;
633 } else {
634 temp = 0xff & (*value);
635 result = acpi_ec_write(ec, address, temp);
636 }
637
638 for (i = 8; unlikely(bits - i > 0); i += 8) {
639 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400640 if (function == ACPI_READ) {
641 result = acpi_ec_read(ec, address, &temp);
642 (*value) |= ((acpi_integer)temp) << i;
643 } else {
644 temp = 0xff & ((*value) >> i);
645 result = acpi_ec_write(ec, address, temp);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300649 acpi_ec_burst_disable(ec);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 switch (result) {
652 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400653 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/* --------------------------------------------------------------------------
667 FS Interface (/proc)
668 -------------------------------------------------------------------------- */
669
Len Brown50526df2005-08-11 17:32:05 -0400670static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Len Brown50526df2005-08-11 17:32:05 -0400672static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300674 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!ec)
677 goto end;
678
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300679 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
680 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
681 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
682 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400683 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400684 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
689{
690 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
691}
692
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400693static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400694 .open = acpi_ec_info_open_fs,
695 .read = seq_read,
696 .llseek = seq_lseek,
697 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 .owner = THIS_MODULE,
699};
700
Len Brown50526df2005-08-11 17:32:05 -0400701static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Len Brown50526df2005-08-11 17:32:05 -0400703 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!acpi_device_dir(device)) {
706 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400707 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700712 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
713 acpi_device_dir(device),
714 &acpi_ec_info_ops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -0400717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Len Brown50526df2005-08-11 17:32:05 -0400720static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (acpi_device_dir(device)) {
724 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
725 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
726 acpi_device_dir(device) = NULL;
727 }
728
Patrick Mocheld550d982006-06-27 00:41:40 -0400729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/* --------------------------------------------------------------------------
733 Driver Interface
734 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735static acpi_status
736ec_parse_io_ports(struct acpi_resource *resource, void *context);
737
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300738static struct acpi_ec *make_acpi_ec(void)
739{
740 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
741 if (!ec)
742 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400743 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300744 mutex_init(&ec->lock);
745 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400746 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400747 spin_lock_init(&ec->curr_lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300748 return ec;
749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400751static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400752acpi_ec_register_query_methods(acpi_handle handle, u32 level,
753 void *context, void **return_value)
754{
755 struct acpi_namespace_node *node = handle;
756 struct acpi_ec *ec = context;
757 int value = 0;
758 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
759 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
760 }
761 return AE_OK;
762}
763
764static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400765ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400766{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400767 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400768 unsigned long long tmp;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400769
770 struct acpi_ec *ec = context;
771 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
772 ec_parse_io_ports, ec);
773 if (ACPI_FAILURE(status))
774 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400775
776 /* Get GPE bit assignment (EC events). */
777 /* TODO: Add support for _GPE returning a package */
Matthew Wilcox27663c52008-10-10 02:22:59 -0400778 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400779 if (ACPI_FAILURE(status))
780 return status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400781 ec->gpe = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400782 /* Use the global lock for all EC transactions? */
Matthew Wilcox27663c52008-10-10 02:22:59 -0400783 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
784 ec->global_lock = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400785 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400786 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400787}
788
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400789static void ec_remove_handlers(struct acpi_ec *ec)
790{
791 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
792 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300793 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400794 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
795 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300796 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400797 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400798}
799
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400800static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400802 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300806 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
807 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
808
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400809 /* Check for boot EC */
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300810 if (boot_ec &&
811 (boot_ec->handle == device->handle ||
812 boot_ec->handle == ACPI_ROOT_OBJECT)) {
813 ec = boot_ec;
814 boot_ec = NULL;
815 } else {
816 ec = make_acpi_ec();
817 if (!ec)
818 return -ENOMEM;
819 if (ec_parse_device(device->handle, 0, ec, NULL) !=
820 AE_CTRL_TERMINATE) {
821 kfree(ec);
822 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400823 }
824 }
825
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300826 ec->handle = device->handle;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300827
828 /* Find and register all query methods */
829 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
830 acpi_ec_register_query_methods, ec, NULL);
831
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400832 if (!first_ec)
833 first_ec = ec;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700834 device->driver_data = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300835 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300836 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400837 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300838 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400839 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Len Brown50526df2005-08-11 17:32:05 -0400843static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300845 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200846 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400852 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200853 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400854 list_del(&handler->node);
855 kfree(handler);
856 }
857 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 acpi_ec_remove_fs(device);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700859 device->driver_data = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300860 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300861 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400862 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300867ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300869 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300871 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /*
875 * The first address region returned is the data port, and
876 * the second address region returned is the status/command
877 * port.
878 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300879 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400880 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300881 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400882 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300883 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return AE_OK;
887}
888
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300889static int ec_install_handlers(struct acpi_ec *ec)
890{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300891 acpi_status status;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400892 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400893 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300894 status = acpi_install_gpe_handler(NULL, ec->gpe,
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400895 ACPI_GPE_EDGE_TRIGGERED,
896 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300897 if (ACPI_FAILURE(status))
898 return -ENODEV;
899 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400900 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300901 status = acpi_install_address_space_handler(ec->handle,
902 ACPI_ADR_SPACE_EC,
903 &acpi_ec_space_handler,
Alexey Starikovskiy6d9e1122008-03-24 23:22:29 +0300904 NULL, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300905 if (ACPI_FAILURE(status)) {
Zhao Yakui20edd742008-10-17 02:02:44 -0400906 if (status == AE_NOT_FOUND) {
907 /*
908 * Maybe OS fails in evaluating the _REG object.
909 * The AE_NOT_FOUND error will be ignored and OS
910 * continue to initialize EC.
911 */
912 printk(KERN_ERR "Fail in evaluating the _REG object"
913 " of EC device. Broken bios is suspected.\n");
914 } else {
915 acpi_remove_gpe_handler(NULL, ec->gpe,
916 &acpi_ec_gpe_handler);
917 return -ENODEV;
918 }
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300919 }
920
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400921 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300922 return 0;
923}
924
Len Brown50526df2005-08-11 17:32:05 -0400925static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300927 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400928 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 ec = acpi_driver_data(device);
934
935 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400936 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400938 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300939
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400940 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400941 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400942 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Len Brown50526df2005-08-11 17:32:05 -0400945static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300947 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300951 if (!ec)
952 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400953 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400954
Patrick Mocheld550d982006-06-27 00:41:40 -0400955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500958int __init acpi_boot_ec_enable(void)
959{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400960 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500961 return 0;
962 if (!ec_install_handlers(boot_ec)) {
963 first_ec = boot_ec;
964 return 0;
965 }
966 return -EFAULT;
967}
968
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300969static const struct acpi_device_id ec_device_ids[] = {
970 {"PNP0C09", 0},
971 {"", 0},
972};
973
Len Brown50526df2005-08-11 17:32:05 -0400974int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Len Brown50526df2005-08-11 17:32:05 -0400976 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300977 acpi_status status;
978 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300980 boot_ec = make_acpi_ec();
981 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300982 return -ENOMEM;
983 /*
984 * Generate a boot ec context
985 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300986 status = acpi_get_table(ACPI_SIG_ECDT, 1,
987 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400988 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300989 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400990 boot_ec->command_addr = ecdt_ptr->control.address;
991 boot_ec->data_addr = ecdt_ptr->data.address;
Zhao Yakui25008222008-08-12 10:40:10 +0800992 if (dmi_check_system(ec_dmi_table)) {
993 /*
994 * If the board falls into ec_dmi_table, it means
995 * that ECDT table gives the incorrect command/status
996 * & data I/O address. Just fix it.
997 */
998 boot_ec->data_addr = ecdt_ptr->control.address;
999 boot_ec->command_addr = ecdt_ptr->data.address;
1000 }
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001001 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -04001002 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +03001003 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001004 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +03001005 /* This workaround is needed only on some broken machines,
1006 * which require early EC, but fail to provide ECDT */
1007 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001008 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1009 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1010 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +04001011 /* Check that acpi_get_devices actually find something */
1012 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001013 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +03001014 /* We really need to limit this workaround, the only ASUS,
1015 * which needs it, has fake EC._INI method, so use it as flag.
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001016 * Keep boot_ec struct as it will be needed soon.
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +03001017 */
1018 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001019 return -ENODEV;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001022 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001023 if (!ret) {
1024 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +03001025 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001026 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +03001027 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001028 kfree(boot_ec);
1029 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -ENODEV;
1031}
1032
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001033static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1034{
1035 struct acpi_ec *ec = acpi_driver_data(device);
1036 /* Stop using GPE */
1037 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1038 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001039 acpi_disable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001040 return 0;
1041}
1042
1043static int acpi_ec_resume(struct acpi_device *device)
1044{
1045 struct acpi_ec *ec = acpi_driver_data(device);
1046 /* Enable use of GPE back */
1047 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001048 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001049 return 0;
1050}
1051
1052static struct acpi_driver acpi_ec_driver = {
1053 .name = "ec",
1054 .class = ACPI_EC_CLASS,
1055 .ids = ec_device_ids,
1056 .ops = {
1057 .add = acpi_ec_add,
1058 .remove = acpi_ec_remove,
1059 .start = acpi_ec_start,
1060 .stop = acpi_ec_stop,
1061 .suspend = acpi_ec_suspend,
1062 .resume = acpi_ec_resume,
1063 },
1064};
1065
Len Brown50526df2005-08-11 17:32:05 -04001066static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Len Brown50526df2005-08-11 17:32:05 -04001068 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1074 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001075 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* Now register the driver for the EC */
1078 result = acpi_bus_register_driver(&acpi_ec_driver);
1079 if (result < 0) {
1080 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Patrick Mocheld550d982006-06-27 00:41:40 -04001084 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087subsys_initcall(acpi_ec_init);
1088
1089/* EC driver currently not unloadable */
1090#if 0
Len Brown50526df2005-08-11 17:32:05 -04001091static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 acpi_bus_unregister_driver(&acpi_ec_driver);
1095
1096 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1097
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
Alexey Starikovskiy78439322007-10-22 14:18:43 +04001100#endif /* 0 */