blob: 1a7949c757b32ad92f194a6e2843b779a7c7e257 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03002 * ec.c - ACPI Embedded Controller Driver (v2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiy01f22462007-03-07 22:28:00 +03004 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 */
28
Márton Némethd772b3b2008-01-23 22:34:09 -050029/* Uncomment next line to get verbose print outs*/
30/* #define DEBUG */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/delay.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050039#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040040#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44#include <acpi/actypes.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_EC_DEVICE_NAME "Embedded Controller"
48#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040049
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030050#undef PREFIX
51#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040052
Denis M. Sadykov703959d2006-09-26 19:50:33 +040053/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050056#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040058
Denis M. Sadykov703959d2006-09-26 19:50:33 +040059/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030060enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030061 ACPI_EC_COMMAND_READ = 0x80,
62 ACPI_EC_COMMAND_WRITE = 0x81,
63 ACPI_EC_BURST_ENABLE = 0x82,
64 ACPI_EC_BURST_DISABLE = 0x83,
65 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040067
Denis M. Sadykov703959d2006-09-26 19:50:33 +040068/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030069enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040071 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072};
73
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030074#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040075#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Alexey Starikovskiye6e82a32008-03-21 17:06:57 +030076#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040077
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040078enum {
79 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
80 EC_FLAGS_QUERY_PENDING, /* Query is pending */
Alexey Starikovskiy78439322007-10-22 14:18:43 +040081 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +030082 EC_FLAGS_NO_ADDRESS_GPE, /* Expect GPE only for non-address event */
83 EC_FLAGS_ADDRESS, /* Address is being written */
Alexey Starikovskiye790cc82007-11-21 03:23:32 +030084 EC_FLAGS_NO_WDATA_GPE, /* Don't expect WDATA GPE event */
85 EC_FLAGS_WDATA, /* Data is being written */
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -050086 EC_FLAGS_NO_OBF1_GPE, /* Don't expect GPE before read */
Alexey Starikovskiy845625c2008-03-21 17:07:03 +030087 EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040088};
89
Len Brown50526df2005-08-11 17:32:05 -040090static int acpi_ec_remove(struct acpi_device *device, int type);
91static int acpi_ec_start(struct acpi_device *device);
92static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040093static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Renninger1ba90e32007-07-23 14:44:41 +020095static const struct acpi_device_id ec_device_ids[] = {
96 {"PNP0C09", 0},
97 {"", 0},
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -0500101 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -0400102 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200103 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -0400104 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400105 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -0400106 .remove = acpi_ec_remove,
107 .start = acpi_ec_start,
108 .stop = acpi_ec_stop,
109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400111
112/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300113/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400114typedef int (*acpi_ec_query_func) (void *data);
115
116struct acpi_ec_query_handler {
117 struct list_head node;
118 acpi_ec_query_func func;
119 acpi_handle handle;
120 void *data;
121 u8 query_bit;
122};
123
Adrian Bunka854e082006-12-19 12:56:12 -0800124static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400125 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300126 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400127 unsigned long command_addr;
128 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400129 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400130 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300131 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400132 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400133 struct list_head list;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300134 struct delayed_work work;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400135 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300136} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* --------------------------------------------------------------------------
139 Transaction Management
140 -------------------------------------------------------------------------- */
141
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300144 u8 x = inb(ec->command_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500145 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300146 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400149static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400150{
Márton Németh3ebe08a2007-11-21 03:23:26 +0300151 u8 x = inb(ec->data_addr);
Márton Németh86dae012008-01-23 22:33:06 -0500152 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400153 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400154}
155
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400156static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157{
Márton Németh86dae012008-01-23 22:33:06 -0500158 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400159 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400160}
161
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400162static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400163{
Márton Németh86dae012008-01-23 22:33:06 -0500164 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400165 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400166}
167
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400168static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400169{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400170 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500171 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300172 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400173 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400174 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300175 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400176 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400177 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400178 }
179
180 return 0;
181}
182
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300183static void ec_schedule_ec_poll(struct acpi_ec *ec)
184{
185 if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
186 schedule_delayed_work(&ec->work,
187 msecs_to_jiffies(ACPI_EC_DELAY));
188}
189
190static void ec_switch_to_poll_mode(struct acpi_ec *ec)
191{
192 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
193 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
194 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
195}
196
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400197static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400198{
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300199 int ret = 0;
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -0500200
201 if (unlikely(event == ACPI_EC_EVENT_OBF_1 &&
202 test_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags)))
203 force_poll = 1;
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300204 if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) &&
205 test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags)))
206 force_poll = 1;
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300207 if (unlikely(test_bit(EC_FLAGS_WDATA, &ec->flags) &&
208 test_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags)))
209 force_poll = 1;
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400210 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
211 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400212 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
213 msecs_to_jiffies(ACPI_EC_DELAY)))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300214 goto end;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400215 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
216 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiy03d1d992008-01-23 22:28:34 -0500217 if (event == ACPI_EC_EVENT_OBF_1) {
218 /* miss OBF_1 GPE, don't expect it */
219 pr_info(PREFIX "missing OBF confirmation, "
220 "don't expect it any longer.\n");
221 set_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags);
222 } else if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300223 /* miss address GPE, don't expect it anymore */
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300224 pr_info(PREFIX "missing address confirmation, "
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300225 "don't expect it any longer.\n");
226 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300227 } else if (test_bit(EC_FLAGS_WDATA, &ec->flags)) {
228 /* miss write data GPE, don't expect it */
229 pr_info(PREFIX "missing write data confirmation, "
230 "don't expect it any longer.\n");
231 set_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400232 } else {
Alexey Starikovskiy66c5f4e2007-10-22 14:18:56 +0400233 /* missing GPEs, switch back to poll mode */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300234 if (printk_ratelimit())
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300235 pr_info(PREFIX "missing confirmations, "
Márton Németh3ebe08a2007-11-21 03:23:26 +0300236 "switch off interrupt mode.\n");
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300237 ec_switch_to_poll_mode(ec);
238 ec_schedule_ec_poll(ec);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400239 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300240 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400241 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400242 } else {
243 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
244 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
245 while (time_before(jiffies, delay)) {
246 if (acpi_ec_check_status(ec, event))
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300247 goto end;
Alexey Starikovskiye6e82a32008-03-21 17:06:57 +0300248 udelay(ACPI_EC_UDELAY);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400249 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300250 }
Alexey Starikovskiydc0e8492008-03-21 17:07:09 +0300251 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
252 acpi_ec_read_status(ec),
253 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300254 ret = -ETIME;
255 end:
256 clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
257 return ret;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500258}
259
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400260static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300261 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200262 u8 * rdata, unsigned rdata_len,
263 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400264{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300265 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400266 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300267 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiydc0e8492008-03-21 17:07:09 +0300268 acpi_ec_write_cmd(ec, command);
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300269 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400270 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300271 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300272 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300273 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300274 goto end;
275 }
Alexey Starikovskiyf2d68932007-11-19 01:37:03 +0300276 /* mark the address byte written to EC */
277 if (rdata_len + wdata_len > 1)
278 set_bit(EC_FLAGS_ADDRESS, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400279 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400280 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400281 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400282
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300283 if (!rdata_len) {
Alexey Starikovskiye790cc82007-11-21 03:23:32 +0300284 set_bit(EC_FLAGS_WDATA, &ec->flags);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400285 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300286 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300287 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300288 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300289 goto end;
290 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400291 } else if (command == ACPI_EC_COMMAND_QUERY)
292 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400293
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300294 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400295 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300296 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300297 pr_err(PREFIX "read timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300298 goto end;
299 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400300 /* Don't expect GPE after last read */
301 if (rdata_len > 1)
302 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400303 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400304 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300305 end:
Márton Németh3ebe08a2007-11-21 03:23:26 +0300306 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300307 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400308}
309
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400310static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300311 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200312 u8 * rdata, unsigned rdata_len,
313 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400314{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400315 int status;
Len Brown50526df2005-08-11 17:32:05 -0400316 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400318 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300321 if (rdata)
322 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300324 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400325 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300327 if (ACPI_FAILURE(status)) {
328 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500332
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400333 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400334 if (status) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300335 pr_err(PREFIX "input buffer is not empty, "
336 "aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500337 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300340 status = acpi_ec_transaction_unlocked(ec, command,
341 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200342 rdata, rdata_len,
343 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400344
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300345 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400347 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300349 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300354/*
355 * Note: samsung nv5000 doesn't work with ec burst mode.
356 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
357 */
358int acpi_ec_burst_enable(struct acpi_ec *ec)
359{
360 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200361 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300362}
363
364int acpi_ec_burst_disable(struct acpi_ec *ec)
365{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200366 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300367}
368
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300369static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400370{
371 int result;
372 u8 d;
373
374 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200375 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400376 *data = d;
377 return result;
378}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400379
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400380static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
381{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300382 u8 wdata[2] = { address, data };
383 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200384 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * Externally callable EC access functions. For now, assume 1 EC only
389 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300390int ec_burst_enable(void)
391{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300392 if (!first_ec)
393 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300394 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300395}
396
397EXPORT_SYMBOL(ec_burst_enable);
398
399int ec_burst_disable(void)
400{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300401 if (!first_ec)
402 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300403 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300404}
405
406EXPORT_SYMBOL(ec_burst_disable);
407
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300408int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400411 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (!first_ec)
414 return -ENODEV;
415
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300416 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (!err) {
419 *val = temp_data;
420 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400421 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return err;
423}
Len Brown50526df2005-08-11 17:32:05 -0400424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425EXPORT_SYMBOL(ec_read);
426
Len Brown50526df2005-08-11 17:32:05 -0400427int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int err;
430
431 if (!first_ec)
432 return -ENODEV;
433
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300434 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return err;
437}
Len Brown50526df2005-08-11 17:32:05 -0400438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439EXPORT_SYMBOL(ec_write);
440
Randy Dunlap616362d2006-10-27 01:47:34 -0400441int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500442 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200443 u8 * rdata, unsigned rdata_len,
444 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400445{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400446 if (!first_ec)
447 return -ENODEV;
448
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300449 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200450 wdata_len, rdata, rdata_len,
451 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400452}
Luming Yu45bea152005-07-23 04:08:00 -0400453
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400454EXPORT_SYMBOL(ec_transaction);
455
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300456static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400457{
458 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300459 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400460
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300461 if (!ec || !data)
462 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400463
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300464 /*
465 * Query the EC to find out which _Qxx method we need to evaluate.
466 * Note that successful completion of the query causes the ACPI_EC_SCI
467 * bit to be cleared (and thus clearing the interrupt source).
468 */
Luming Yu45bea152005-07-23 04:08:00 -0400469
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200470 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300471 if (result)
472 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400473
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300474 if (!d)
475 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400476
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300477 *data = d;
478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/* --------------------------------------------------------------------------
482 Event Management
483 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400484int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
485 acpi_handle handle, acpi_ec_query_func func,
486 void *data)
487{
488 struct acpi_ec_query_handler *handler =
489 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
490 if (!handler)
491 return -ENOMEM;
492
493 handler->query_bit = query_bit;
494 handler->handle = handle;
495 handler->func = func;
496 handler->data = data;
497 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400498 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400499 mutex_unlock(&ec->lock);
500 return 0;
501}
502
503EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
504
505void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
506{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200507 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400508 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200509 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400510 if (query_bit == handler->query_bit) {
511 list_del(&handler->node);
512 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400513 }
514 }
515 mutex_unlock(&ec->lock);
516}
517
518EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Len Brown50526df2005-08-11 17:32:05 -0400520static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300522 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400523 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400524 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400525
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300526 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300527 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400528 mutex_lock(&ec->lock);
529 list_for_each_entry(handler, &ec->list, node) {
530 if (value == handler->query_bit) {
531 /* have custom handler for this bit */
532 memcpy(&copy, handler, sizeof(copy));
533 mutex_unlock(&ec->lock);
534 if (copy.func) {
535 copy.func(copy.data);
536 } else if (copy.handle) {
537 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
538 }
539 return;
540 }
541 }
542 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Len Brown50526df2005-08-11 17:32:05 -0400545static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Len Brown50526df2005-08-11 17:32:05 -0400547 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300548 struct acpi_ec *ec = data;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300549 u8 state = acpi_ec_read_status(ec);
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200550
Márton Németh3ebe08a2007-11-21 03:23:26 +0300551 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400552 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400553 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300554 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500555
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300556 if (state & ACPI_EC_FLAG_SCI) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400557 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
558 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
559 acpi_ec_gpe_query, ec);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300560 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
561 in_interrupt()) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400562 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300563 if (printk_ratelimit())
564 pr_info(PREFIX "non-query interrupt received,"
565 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400566 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300567 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400568 }
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300569 ec_schedule_ec_poll(ec);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400570 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400571 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300574static void do_ec_poll(struct work_struct *work)
575{
576 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
577 (void)acpi_ec_gpe_handler(ec);
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/* --------------------------------------------------------------------------
581 Address Space Management
582 -------------------------------------------------------------------------- */
583
584static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400585acpi_ec_space_setup(acpi_handle region_handle,
586 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 /*
589 * The EC object is in the handler context and is needed
590 * when calling the acpi_ec_space_handler.
591 */
Len Brown50526df2005-08-11 17:32:05 -0400592 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
593 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 return AE_OK;
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400599acpi_ec_space_handler(u32 function, acpi_physical_address address,
600 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400601 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300603 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300604 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400605 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400610 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400612
613 if (bits != 8 && acpi_strict)
614 return AE_BAD_PARAMETER;
615
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300616 acpi_ec_burst_enable(ec);
617
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300618 if (function == ACPI_READ) {
619 result = acpi_ec_read(ec, address, &temp);
620 *value = temp;
621 } else {
622 temp = 0xff & (*value);
623 result = acpi_ec_write(ec, address, temp);
624 }
625
626 for (i = 8; unlikely(bits - i > 0); i += 8) {
627 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400628 if (function == ACPI_READ) {
629 result = acpi_ec_read(ec, address, &temp);
630 (*value) |= ((acpi_integer)temp) << i;
631 } else {
632 temp = 0xff & ((*value) >> i);
633 result = acpi_ec_write(ec, address, temp);
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300637 acpi_ec_burst_disable(ec);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 switch (result) {
640 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400641 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400644 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400647 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/* --------------------------------------------------------------------------
655 FS Interface (/proc)
656 -------------------------------------------------------------------------- */
657
Len Brown50526df2005-08-11 17:32:05 -0400658static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Len Brown50526df2005-08-11 17:32:05 -0400660static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300662 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!ec)
665 goto end;
666
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300667 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
668 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
669 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
670 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400671 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400672 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
677{
678 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
679}
680
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400681static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400682 .open = acpi_ec_info_open_fs,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .owner = THIS_MODULE,
687};
688
Len Brown50526df2005-08-11 17:32:05 -0400689static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Len Brown50526df2005-08-11 17:32:05 -0400691 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!acpi_device_dir(device)) {
694 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400695 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400701 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400703 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else {
705 entry->proc_fops = &acpi_ec_info_ops;
706 entry->data = acpi_driver_data(device);
707 entry->owner = THIS_MODULE;
708 }
709
Patrick Mocheld550d982006-06-27 00:41:40 -0400710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Len Brown50526df2005-08-11 17:32:05 -0400713static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (acpi_device_dir(device)) {
717 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
718 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
719 acpi_device_dir(device) = NULL;
720 }
721
Patrick Mocheld550d982006-06-27 00:41:40 -0400722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/* --------------------------------------------------------------------------
726 Driver Interface
727 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300728static acpi_status
729ec_parse_io_ports(struct acpi_resource *resource, void *context);
730
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300731static struct acpi_ec *make_acpi_ec(void)
732{
733 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
734 if (!ec)
735 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400736 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300737 mutex_init(&ec->lock);
738 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400739 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300740 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 return ec;
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400744static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400745acpi_ec_register_query_methods(acpi_handle handle, u32 level,
746 void *context, void **return_value)
747{
748 struct acpi_namespace_node *node = handle;
749 struct acpi_ec *ec = context;
750 int value = 0;
751 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
752 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
753 }
754 return AE_OK;
755}
756
757static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400758ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400759{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400760 acpi_status status;
761
762 struct acpi_ec *ec = context;
763 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
764 ec_parse_io_ports, ec);
765 if (ACPI_FAILURE(status))
766 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400767
768 /* Get GPE bit assignment (EC events). */
769 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400770 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
771 if (ACPI_FAILURE(status))
772 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400773 /* Find and register all query methods */
774 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
775 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400776 /* Use the global lock for all EC transactions? */
777 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400778 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400779 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400780}
781
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300782static void ec_poll_stop(struct acpi_ec *ec)
783{
784 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
785 cancel_delayed_work(&ec->work);
786}
787
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400788static void ec_remove_handlers(struct acpi_ec *ec)
789{
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300790 ec_poll_stop(ec);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400791 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 Starikovskiy4c611062007-09-05 19:56:38 -0400797 ec->handlers_installed = 0;
798}
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 */
810 if (boot_ec) {
811 if (boot_ec->handle == device->handle) {
812 /* Pre-loaded EC from DSDT, just move pointer */
813 ec = boot_ec;
814 boot_ec = NULL;
815 goto end;
816 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
817 /* ECDT-based EC, time to shut it down */
818 ec_remove_handlers(boot_ec);
819 kfree(boot_ec);
820 first_ec = boot_ec = NULL;
821 }
822 }
823
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300824 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400826 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400828 if (ec_parse_device(device->handle, 0, ec, NULL) !=
829 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300830 kfree(ec);
831 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400832 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300833 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400834 end:
835 if (!first_ec)
836 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300838 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300839 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400840 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300841 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400842 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Len Brown50526df2005-08-11 17:32:05 -0400846static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300848 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200849 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400855 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200856 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400857 list_del(&handler->node);
858 kfree(handler);
859 }
860 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300862 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300863 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300864 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400865 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300870ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300872 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300874 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
878 * The first address region returned is the data port, and
879 * the second address region returned is the status/command
880 * port.
881 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300882 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400883 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300884 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400885 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300886 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return AE_OK;
890}
891
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300892static int ec_install_handlers(struct acpi_ec *ec)
893{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300894 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400895 if (ec->handlers_installed)
896 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300897 status = acpi_install_gpe_handler(NULL, ec->gpe,
898 ACPI_GPE_EDGE_TRIGGERED,
899 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300900 if (ACPI_FAILURE(status))
901 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300902
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300903 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
904 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
905
906 status = acpi_install_address_space_handler(ec->handle,
907 ACPI_ADR_SPACE_EC,
908 &acpi_ec_space_handler,
909 &acpi_ec_space_setup, ec);
910 if (ACPI_FAILURE(status)) {
911 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
912 return -ENODEV;
913 }
914
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400915 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300916 return 0;
917}
918
Len Brown50526df2005-08-11 17:32:05 -0400919static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300921 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400922 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400925 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 ec = acpi_driver_data(device);
928
929 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400932 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300933
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400934 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400935 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300936 ec_schedule_ec_poll(ec);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Len Brown50526df2005-08-11 17:32:05 -0400940static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300942 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300946 if (!ec)
947 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400948 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400949
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500953int __init acpi_boot_ec_enable(void)
954{
955 if (!boot_ec || boot_ec->handlers_installed)
956 return 0;
957 if (!ec_install_handlers(boot_ec)) {
958 first_ec = boot_ec;
959 return 0;
960 }
961 return -EFAULT;
962}
963
Len Brown50526df2005-08-11 17:32:05 -0400964int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Len Brown50526df2005-08-11 17:32:05 -0400966 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300967 acpi_status status;
968 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300970 boot_ec = make_acpi_ec();
971 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300972 return -ENOMEM;
973 /*
974 * Generate a boot ec context
975 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300976 status = acpi_get_table(ACPI_SIG_ECDT, 1,
977 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400978 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300979 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400980 boot_ec->command_addr = ecdt_ptr->control.address;
981 boot_ec->data_addr = ecdt_ptr->data.address;
982 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -0400983 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400984 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300985 /* This workaround is needed only on some broken machines,
986 * which require early EC, but fail to provide ECDT */
987 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400988 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
989 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
990 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400991 /* Check that acpi_get_devices actually find something */
992 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400993 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300994 /* We really need to limit this workaround, the only ASUS,
995 * which needs it, has fake EC._INI method, so use it as flag.
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500996 * Keep boot_ec struct as it will be needed soon.
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300997 */
998 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500999 return -ENODEV;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -04001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001002 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001003 if (!ret) {
1004 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +03001005 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +03001006 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +03001007 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +03001008 kfree(boot_ec);
1009 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -ENODEV;
1011}
1012
Len Brown50526df2005-08-11 17:32:05 -04001013static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Len Brown50526df2005-08-11 17:32:05 -04001015 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1021 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* Now register the driver for the EC */
1025 result = acpi_bus_register_driver(&acpi_ec_driver);
1026 if (result < 0) {
1027 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001028 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034subsys_initcall(acpi_ec_init);
1035
1036/* EC driver currently not unloadable */
1037#if 0
Len Brown50526df2005-08-11 17:32:05 -04001038static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 acpi_bus_unregister_driver(&acpi_ec_driver);
1042
1043 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1044
Patrick Mocheld550d982006-06-27 00:41:40 -04001045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Alexey Starikovskiy78439322007-10-22 14:18:43 +04001047#endif /* 0 */