blob: 8dfcbb8aff7398a5b75061b52eab15298726dd0e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040049
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030050#undef PREFIX
51#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040052
Denis M. Sadykov703959d2006-09-26 19:50:33 +040053/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050056#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040058
Denis M. Sadykov703959d2006-09-26 19:50:33 +040059/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030060enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030061 ACPI_EC_COMMAND_READ = 0x80,
62 ACPI_EC_COMMAND_WRITE = 0x81,
63 ACPI_EC_BURST_ENABLE = 0x82,
64 ACPI_EC_BURST_DISABLE = 0x83,
65 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040067
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030068#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Alexey Starikovskiye6e82a32008-03-21 17:06:57 +030070#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040071
Alexey Starikovskiy06cf7d32008-11-09 19:01:06 +030072#define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040073 per one transaction */
74
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040075enum {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040076 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040077 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
78 * for status change */
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +030079 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040080 EC_FLAGS_GPE_STORM, /* GPE storm detected */
81 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
82 * OpReg are installed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040084
85/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030086/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040087typedef int (*acpi_ec_query_func) (void *data);
88
89struct acpi_ec_query_handler {
90 struct list_head node;
91 acpi_ec_query_func func;
92 acpi_handle handle;
93 void *data;
94 u8 query_bit;
95};
96
Alexey Starikovskiy84632002008-09-26 00:54:28 +040097struct transaction {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +040098 const u8 *wdata;
99 u8 *rdata;
100 unsigned short irq_count;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400101 u8 command;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300102 u8 wi;
103 u8 ri;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400104 u8 wlen;
105 u8 rlen;
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300106 bool done;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400107};
108
Adrian Bunka854e082006-12-19 12:56:12 -0800109static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400110 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300111 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400112 unsigned long command_addr;
113 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400114 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400115 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300116 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400117 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400118 struct list_head list;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400119 struct transaction *curr;
120 spinlock_t curr_lock;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300121} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400122
Zhao Yakui25008222008-08-12 10:40:10 +0800123/*
124 * Some Asus system have exchanged ECDT data/command IO addresses.
125 */
126static int print_ecdt_error(const struct dmi_system_id *id)
127{
128 printk(KERN_NOTICE PREFIX "%s detected - "
129 "ECDT has exchanged control/data I/O address\n",
130 id->ident);
131 return 0;
132}
133
134static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
135 {
136 print_ecdt_error, "Asus L4R", {
137 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
138 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
139 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
140 {
141 print_ecdt_error, "Asus M6R", {
142 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
143 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
144 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
145 {},
146};
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* --------------------------------------------------------------------------
149 Transaction Management
150 -------------------------------------------------------------------------- */
151
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300154 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500155 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300156 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400159static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400160{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300161 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500162 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400163 return x;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400164}
165
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400166static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400167{
Márton Németh86dae012008-01-23 22:33:06 -0500168 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400169 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400170}
171
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400172static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173{
Márton Németh86dae012008-01-23 22:33:06 -0500174 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400175 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400176}
177
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400178static int ec_transaction_done(struct acpi_ec *ec)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400179{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400180 unsigned long flags;
181 int ret = 0;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400182 spin_lock_irqsave(&ec->curr_lock, flags);
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300183 if (!ec->curr || ec->curr->done)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400184 ret = 1;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400185 spin_unlock_irqrestore(&ec->curr_lock, flags);
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400186 return ret;
187}
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400188
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300189static void start_transaction(struct acpi_ec *ec)
190{
191 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
192 ec->curr->done = false;
193 acpi_ec_write_cmd(ec, ec->curr->command);
194}
195
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400196static void gpe_transaction(struct acpi_ec *ec, u8 status)
197{
198 unsigned long flags;
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400199 spin_lock_irqsave(&ec->curr_lock, flags);
200 if (!ec->curr)
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400201 goto unlock;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300202 if (ec->curr->wlen > ec->curr->wi) {
203 if ((status & ACPI_EC_FLAG_IBF) == 0)
204 acpi_ec_write_data(ec,
205 ec->curr->wdata[ec->curr->wi++]);
206 else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300207 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300208 } else if (ec->curr->rlen > ec->curr->ri) {
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400209 if ((status & ACPI_EC_FLAG_OBF) == 1) {
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300210 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
211 if (ec->curr->rlen == ec->curr->ri)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300212 ec->curr->done = true;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400213 } else
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300214 goto err;
Alexey Starikovskiya2f93ae2008-11-12 01:40:19 +0300215 } else if (ec->curr->wlen == ec->curr->wi &&
216 (status & ACPI_EC_FLAG_IBF) == 0)
Alexey Starikovskiydd15f8c2008-11-08 21:42:30 +0300217 ec->curr->done = true;
218 goto unlock;
219err:
220 /* false interrupt, state didn't change */
Alexey Starikovskiy7b4d4692008-11-13 12:00:03 +0300221 if (in_interrupt())
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 */
Roel Kluin8a383ef2008-12-09 20:45:30 +0100372static int acpi_ec_burst_enable(struct acpi_ec *ec)
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300373{
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
Roel Kluin8a383ef2008-12-09 20:45:30 +0100382static int acpi_ec_burst_disable(struct acpi_ec *ec)
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300383{
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
Alexey Starikovskiy85179342008-11-11 12:54:11 +0300584 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
585 gpe_transaction(ec, status);
586 if (ec_transaction_done(ec) &&
587 (status & ACPI_EC_FLAG_IBF) == 0)
588 wake_up(&ec->wait);
589 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500590
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400591 ec_check_sci(ec, status);
592 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
593 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400594 /* this is non-query, must be confirmation */
Alan Jenkinsf8248432008-11-01 11:05:26 +0000595 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
596 if (printk_ratelimit())
597 pr_info(PREFIX "non-query interrupt received,"
598 " switching to interrupt mode\n");
599 } else {
600 /* hush, STORM switches the mode every transaction */
601 pr_debug(PREFIX "non-query interrupt received,"
Márton Németh3ebe08a2007-11-21 03:23:26 +0300602 " switching to interrupt mode\n");
Alan Jenkinsf8248432008-11-01 11:05:26 +0000603 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400604 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400605 }
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400606 return ACPI_INTERRUPT_HANDLED;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* --------------------------------------------------------------------------
610 Address Space Management
611 -------------------------------------------------------------------------- */
612
613static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400614acpi_ec_space_handler(u32 function, acpi_physical_address address,
615 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400616 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300618 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300619 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400620 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400625 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400627
628 if (bits != 8 && acpi_strict)
629 return AE_BAD_PARAMETER;
630
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300631 acpi_ec_burst_enable(ec);
632
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300633 if (function == ACPI_READ) {
634 result = acpi_ec_read(ec, address, &temp);
635 *value = temp;
636 } else {
637 temp = 0xff & (*value);
638 result = acpi_ec_write(ec, address, temp);
639 }
640
641 for (i = 8; unlikely(bits - i > 0); i += 8) {
642 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400643 if (function == ACPI_READ) {
644 result = acpi_ec_read(ec, address, &temp);
645 (*value) |= ((acpi_integer)temp) << i;
646 } else {
647 temp = 0xff & ((*value) >> i);
648 result = acpi_ec_write(ec, address, temp);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300652 acpi_ec_burst_disable(ec);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 switch (result) {
655 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
664 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400665 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/* --------------------------------------------------------------------------
670 FS Interface (/proc)
671 -------------------------------------------------------------------------- */
672
Len Brown50526df2005-08-11 17:32:05 -0400673static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Len Brown50526df2005-08-11 17:32:05 -0400675static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300677 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!ec)
680 goto end;
681
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300682 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
683 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
684 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
685 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400686 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400687 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
692{
693 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
694}
695
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400696static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400697 .open = acpi_ec_info_open_fs,
698 .read = seq_read,
699 .llseek = seq_lseek,
700 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 .owner = THIS_MODULE,
702};
703
Len Brown50526df2005-08-11 17:32:05 -0400704static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Len Brown50526df2005-08-11 17:32:05 -0400706 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!acpi_device_dir(device)) {
709 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400710 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400712 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700715 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
716 acpi_device_dir(device),
717 &acpi_ec_info_ops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Len Brown50526df2005-08-11 17:32:05 -0400723static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (acpi_device_dir(device)) {
727 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
728 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
729 acpi_device_dir(device) = NULL;
730 }
731
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/* --------------------------------------------------------------------------
736 Driver Interface
737 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300738static acpi_status
739ec_parse_io_ports(struct acpi_resource *resource, void *context);
740
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741static struct acpi_ec *make_acpi_ec(void)
742{
743 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
744 if (!ec)
745 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400746 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300747 mutex_init(&ec->lock);
748 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400749 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy84632002008-09-26 00:54:28 +0400750 spin_lock_init(&ec->curr_lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300751 return ec;
752}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400754static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400755acpi_ec_register_query_methods(acpi_handle handle, u32 level,
756 void *context, void **return_value)
757{
Lin Ming0175d562008-12-16 16:46:12 +0800758 char node_name[5];
759 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400760 struct acpi_ec *ec = context;
761 int value = 0;
Lin Ming0175d562008-12-16 16:46:12 +0800762 acpi_status status;
763
764 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
765
766 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400767 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
768 }
769 return AE_OK;
770}
771
772static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400773ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400774{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400775 acpi_status status;
Alexey Starikovskiyd21cf3c2008-11-03 14:26:40 -0500776 unsigned long long tmp = 0;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400777
778 struct acpi_ec *ec = context;
779 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
780 ec_parse_io_ports, ec);
781 if (ACPI_FAILURE(status))
782 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400783
784 /* Get GPE bit assignment (EC events). */
785 /* TODO: Add support for _GPE returning a package */
Matthew Wilcox27663c52008-10-10 02:22:59 -0400786 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400787 if (ACPI_FAILURE(status))
788 return status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400789 ec->gpe = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400790 /* Use the global lock for all EC transactions? */
Alexey Starikovskiyd21cf3c2008-11-03 14:26:40 -0500791 tmp = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400792 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
793 ec->global_lock = tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400794 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400795 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400796}
797
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400798static void ec_remove_handlers(struct acpi_ec *ec)
799{
800 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
801 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300802 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400803 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
804 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300805 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400806 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400807}
808
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400809static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400811 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400814 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300815 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
816 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
817
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400818 /* Check for boot EC */
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300819 if (boot_ec &&
820 (boot_ec->handle == device->handle ||
821 boot_ec->handle == ACPI_ROOT_OBJECT)) {
822 ec = boot_ec;
823 boot_ec = NULL;
824 } else {
825 ec = make_acpi_ec();
826 if (!ec)
827 return -ENOMEM;
828 if (ec_parse_device(device->handle, 0, ec, NULL) !=
829 AE_CTRL_TERMINATE) {
830 kfree(ec);
831 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400832 }
833 }
834
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300835 ec->handle = device->handle;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300836
837 /* Find and register all query methods */
838 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
839 acpi_ec_register_query_methods, ec, NULL);
840
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400841 if (!first_ec)
842 first_ec = ec;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700843 device->driver_data = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300844 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300845 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400846 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300847 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400848 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Len Brown50526df2005-08-11 17:32:05 -0400852static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300854 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200855 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400861 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200862 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400863 list_del(&handler->node);
864 kfree(handler);
865 }
866 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 acpi_ec_remove_fs(device);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700868 device->driver_data = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300869 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300870 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400871 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300876ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300878 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300880 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /*
884 * The first address region returned is the data port, and
885 * the second address region returned is the status/command
886 * port.
887 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300888 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400889 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300890 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400891 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300892 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return AE_OK;
896}
897
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300898static int ec_install_handlers(struct acpi_ec *ec)
899{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300900 acpi_status status;
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400901 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400902 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300903 status = acpi_install_gpe_handler(NULL, ec->gpe,
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400904 ACPI_GPE_EDGE_TRIGGERED,
905 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300906 if (ACPI_FAILURE(status))
907 return -ENODEV;
908 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400909 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300910 status = acpi_install_address_space_handler(ec->handle,
911 ACPI_ADR_SPACE_EC,
912 &acpi_ec_space_handler,
Alexey Starikovskiy6d9e1122008-03-24 23:22:29 +0300913 NULL, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300914 if (ACPI_FAILURE(status)) {
Zhao Yakui20edd742008-10-17 02:02:44 -0400915 if (status == AE_NOT_FOUND) {
916 /*
917 * Maybe OS fails in evaluating the _REG object.
918 * The AE_NOT_FOUND error will be ignored and OS
919 * continue to initialize EC.
920 */
921 printk(KERN_ERR "Fail in evaluating the _REG object"
922 " of EC device. Broken bios is suspected.\n");
923 } else {
924 acpi_remove_gpe_handler(NULL, ec->gpe,
925 &acpi_ec_gpe_handler);
926 return -ENODEV;
927 }
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300928 }
929
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400930 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300931 return 0;
932}
933
Len Brown50526df2005-08-11 17:32:05 -0400934static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300936 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400937 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 ec = acpi_driver_data(device);
943
944 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400947 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300948
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400949 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400950 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Len Brown50526df2005-08-11 17:32:05 -0400954static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300956 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300960 if (!ec)
961 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400962 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400963
Patrick Mocheld550d982006-06-27 00:41:40 -0400964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500967int __init acpi_boot_ec_enable(void)
968{
Alexey Starikovskiy7c6db4e2008-09-25 21:00:31 +0400969 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500970 return 0;
971 if (!ec_install_handlers(boot_ec)) {
972 first_ec = boot_ec;
973 return 0;
974 }
975 return -EFAULT;
976}
977
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300978static const struct acpi_device_id ec_device_ids[] = {
979 {"PNP0C09", 0},
980 {"", 0},
981};
982
Len Brown50526df2005-08-11 17:32:05 -0400983int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300985 acpi_status status;
986 struct acpi_table_ecdt *ecdt_ptr;
Hannes Eder3e540482008-11-29 07:21:29 +0100987 acpi_handle dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300989 boot_ec = make_acpi_ec();
990 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300991 return -ENOMEM;
992 /*
993 * Generate a boot ec context
994 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300995 status = acpi_get_table(ACPI_SIG_ECDT, 1,
996 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400997 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300998 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400999 boot_ec->command_addr = ecdt_ptr->control.address;
1000 boot_ec->data_addr = ecdt_ptr->data.address;
Zhao Yakui25008222008-08-12 10:40:10 +08001001 if (dmi_check_system(ec_dmi_table)) {
1002 /*
1003 * If the board falls into ec_dmi_table, it means
1004 * that ECDT table gives the incorrect command/status
1005 * & data I/O address. Just fix it.
1006 */
1007 boot_ec->data_addr = ecdt_ptr->control.address;
1008 boot_ec->command_addr = ecdt_ptr->data.address;
1009 }
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001010 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -04001011 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +03001012 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001013 /* Add some basic check against completely broken table */
1014 if (boot_ec->data_addr != boot_ec->command_addr)
1015 goto install;
1016 /* fall through */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001017 }
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001018 /* This workaround is needed only on some broken machines,
1019 * which require early EC, but fail to provide ECDT */
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001020 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1021 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1022 boot_ec, NULL);
1023 /* Check that acpi_get_devices actually find something */
1024 if (ACPI_FAILURE(status) || !boot_ec->handle)
1025 goto error;
1026 /* We really need to limit this workaround, the only ASUS,
1027 * which needs it, has fake EC._INI method, so use it as flag.
1028 * Keep boot_ec struct as it will be needed soon.
1029 */
Hannes Eder3e540482008-11-29 07:21:29 +01001030 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &dummy)))
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001031 return -ENODEV;
1032install:
1033 if (!ec_install_handlers(boot_ec)) {
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001034 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +03001035 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001036 }
Alexey Starikovskiyc5279de2008-11-26 17:11:53 -05001037error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001038 kfree(boot_ec);
1039 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -ENODEV;
1041}
1042
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001043static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1044{
1045 struct acpi_ec *ec = acpi_driver_data(device);
1046 /* Stop using GPE */
1047 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1048 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001049 acpi_disable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001050 return 0;
1051}
1052
1053static int acpi_ec_resume(struct acpi_device *device)
1054{
1055 struct acpi_ec *ec = acpi_driver_data(device);
1056 /* Enable use of GPE back */
1057 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +04001058 acpi_enable_gpe(NULL, ec->gpe);
Alexey Starikovskiy223883b2008-03-21 17:07:21 +03001059 return 0;
1060}
1061
1062static struct acpi_driver acpi_ec_driver = {
1063 .name = "ec",
1064 .class = ACPI_EC_CLASS,
1065 .ids = ec_device_ids,
1066 .ops = {
1067 .add = acpi_ec_add,
1068 .remove = acpi_ec_remove,
1069 .start = acpi_ec_start,
1070 .stop = acpi_ec_stop,
1071 .suspend = acpi_ec_suspend,
1072 .resume = acpi_ec_resume,
1073 },
1074};
1075
Len Brown50526df2005-08-11 17:32:05 -04001076static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Len Brown50526df2005-08-11 17:32:05 -04001078 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1084 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001085 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /* Now register the driver for the EC */
1088 result = acpi_bus_register_driver(&acpi_ec_driver);
1089 if (result < 0) {
1090 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001091 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Patrick Mocheld550d982006-06-27 00:41:40 -04001094 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097subsys_initcall(acpi_ec_init);
1098
1099/* EC driver currently not unloadable */
1100#if 0
Len Brown50526df2005-08-11 17:32:05 -04001101static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 acpi_bus_unregister_driver(&acpi_ec_driver);
1105
1106 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1107
Patrick Mocheld550d982006-06-27 00:41:40 -04001108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
Alexey Starikovskiy78439322007-10-22 14:18:43 +04001110#endif /* 0 */