blob: 76784ae7e6a16ffe272392ce1543db2a14909a96 [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 Starikovskiyb77d81b2008-03-21 17:07:15 +030082 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
Alexey Starikovskiy845625c2008-03-21 17:07:03 +030083 EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040085
86/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030087/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040088typedef int (*acpi_ec_query_func) (void *data);
89
90struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
93 acpi_handle handle;
94 void *data;
95 u8 query_bit;
96};
97
Adrian Bunka854e082006-12-19 12:56:12 -080098static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040099 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300100 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400101 unsigned long command_addr;
102 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400103 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400104 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300105 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400106 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400107 struct list_head list;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300108 struct delayed_work work;
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300109 atomic_t irq_count;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400110 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300111} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400112
Zhao Yakui25008222008-08-12 10:40:10 +0800113/*
114 * Some Asus system have exchanged ECDT data/command IO addresses.
115 */
116static int print_ecdt_error(const struct dmi_system_id *id)
117{
118 printk(KERN_NOTICE PREFIX "%s detected - "
119 "ECDT has exchanged control/data I/O address\n",
120 id->ident);
121 return 0;
122}
123
124static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
125 {
126 print_ecdt_error, "Asus L4R", {
127 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
128 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
129 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
130 {
131 print_ecdt_error, "Asus M6R", {
132 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
133 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
134 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
135 {},
136};
137
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{
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300192 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300193 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
194 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
195 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
196}
197
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400198static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400199{
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300200 atomic_set(&ec->irq_count, 0);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400201 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
202 likely(!force_poll)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400203 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
204 msecs_to_jiffies(ACPI_EC_DELAY)))
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300205 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400206 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
207 if (acpi_ec_check_status(ec, event)) {
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300208 /* missing GPEs, switch back to poll mode */
209 if (printk_ratelimit())
210 pr_info(PREFIX "missing confirmations, "
Márton Németh3ebe08a2007-11-21 03:23:26 +0300211 "switch off interrupt mode.\n");
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300212 ec_switch_to_poll_mode(ec);
213 ec_schedule_ec_poll(ec);
214 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400215 }
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400216 } else {
217 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
218 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
219 while (time_before(jiffies, delay)) {
220 if (acpi_ec_check_status(ec, event))
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300221 return 0;
Alexey Starikovskiy1b7fc5a2008-06-06 11:49:33 -0400222 msleep(1);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400223 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300224 }
Alexey Starikovskiydc0e8492008-03-21 17:07:09 +0300225 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
226 acpi_ec_read_status(ec),
227 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300228 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500229}
230
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400231static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300232 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200233 u8 * rdata, unsigned rdata_len,
234 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400235{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300236 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400237 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300238 pr_debug(PREFIX "transaction start\n");
Alexey Starikovskiydc0e8492008-03-21 17:07:09 +0300239 acpi_ec_write_cmd(ec, command);
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300240 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400241 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300242 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300243 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300244 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300245 goto end;
246 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400247 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400249 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400250
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300251 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400252 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300253 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300254 pr_err(PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300255 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300256 goto end;
257 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400258 } else if (command == ACPI_EC_COMMAND_QUERY)
259 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400260
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300261 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400262 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300263 if (result) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300264 pr_err(PREFIX "read timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300265 goto end;
266 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400267 /* Don't expect GPE after last read */
268 if (rdata_len > 1)
269 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400270 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400271 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300272 end:
Márton Németh3ebe08a2007-11-21 03:23:26 +0300273 pr_debug(PREFIX "transaction end\n");
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300274 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400275}
276
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400277static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300278 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200279 u8 * rdata, unsigned rdata_len,
280 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400281{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400282 int status;
Len Brown50526df2005-08-11 17:32:05 -0400283 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400285 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300288 if (rdata)
289 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300291 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400292 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300294 if (ACPI_FAILURE(status)) {
295 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400296 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500299
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400300 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400301 if (status) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300302 pr_err(PREFIX "input buffer is not empty, "
303 "aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500304 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300307 status = acpi_ec_transaction_unlocked(ec, command,
308 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200309 rdata, rdata_len,
310 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400311
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300312 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400314 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300316 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300321/*
322 * Note: samsung nv5000 doesn't work with ec burst mode.
323 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
324 */
325int acpi_ec_burst_enable(struct acpi_ec *ec)
326{
327 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200328 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300329}
330
331int acpi_ec_burst_disable(struct acpi_ec *ec)
332{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200333 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300334}
335
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300336static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400337{
338 int result;
339 u8 d;
340
341 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200342 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400343 *data = d;
344 return result;
345}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400346
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400347static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
348{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300349 u8 wdata[2] = { address, data };
350 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200351 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * Externally callable EC access functions. For now, assume 1 EC only
356 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300357int ec_burst_enable(void)
358{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300359 if (!first_ec)
360 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300361 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300362}
363
364EXPORT_SYMBOL(ec_burst_enable);
365
366int ec_burst_disable(void)
367{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300368 if (!first_ec)
369 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300370 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300371}
372
373EXPORT_SYMBOL(ec_burst_disable);
374
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300375int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400378 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!first_ec)
381 return -ENODEV;
382
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300383 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (!err) {
386 *val = temp_data;
387 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400388 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return err;
390}
Len Brown50526df2005-08-11 17:32:05 -0400391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392EXPORT_SYMBOL(ec_read);
393
Len Brown50526df2005-08-11 17:32:05 -0400394int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int err;
397
398 if (!first_ec)
399 return -ENODEV;
400
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300401 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return err;
404}
Len Brown50526df2005-08-11 17:32:05 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406EXPORT_SYMBOL(ec_write);
407
Randy Dunlap616362d2006-10-27 01:47:34 -0400408int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500409 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200410 u8 * rdata, unsigned rdata_len,
411 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400412{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400413 if (!first_ec)
414 return -ENODEV;
415
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300416 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200417 wdata_len, rdata, rdata_len,
418 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400419}
Luming Yu45bea152005-07-23 04:08:00 -0400420
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400421EXPORT_SYMBOL(ec_transaction);
422
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300423static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400424{
425 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300426 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400427
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300428 if (!ec || !data)
429 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400430
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300431 /*
432 * Query the EC to find out which _Qxx method we need to evaluate.
433 * Note that successful completion of the query causes the ACPI_EC_SCI
434 * bit to be cleared (and thus clearing the interrupt source).
435 */
Luming Yu45bea152005-07-23 04:08:00 -0400436
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200437 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300438 if (result)
439 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400440
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300441 if (!d)
442 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400443
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300444 *data = d;
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/* --------------------------------------------------------------------------
449 Event Management
450 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400451int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
452 acpi_handle handle, acpi_ec_query_func func,
453 void *data)
454{
455 struct acpi_ec_query_handler *handler =
456 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
457 if (!handler)
458 return -ENOMEM;
459
460 handler->query_bit = query_bit;
461 handler->handle = handle;
462 handler->func = func;
463 handler->data = data;
464 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400465 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400466 mutex_unlock(&ec->lock);
467 return 0;
468}
469
470EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
471
472void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
473{
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200474 struct acpi_ec_query_handler *handler, *tmp;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400475 mutex_lock(&ec->lock);
Adrian Bunk1544fdb2007-10-24 18:26:00 +0200476 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400477 if (query_bit == handler->query_bit) {
478 list_del(&handler->node);
479 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400480 }
481 }
482 mutex_unlock(&ec->lock);
483}
484
485EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Len Brown50526df2005-08-11 17:32:05 -0400487static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300489 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400490 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400491 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400492
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300493 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300494 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400495 mutex_lock(&ec->lock);
496 list_for_each_entry(handler, &ec->list, node) {
497 if (value == handler->query_bit) {
498 /* have custom handler for this bit */
499 memcpy(&copy, handler, sizeof(copy));
500 mutex_unlock(&ec->lock);
501 if (copy.func) {
502 copy.func(copy.data);
503 } else if (copy.handle) {
504 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
505 }
506 return;
507 }
508 }
509 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400510}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Len Brown50526df2005-08-11 17:32:05 -0400512static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Len Brown50526df2005-08-11 17:32:05 -0400514 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300515 struct acpi_ec *ec = data;
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300516 u8 state = acpi_ec_read_status(ec);
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200517
Márton Németh3ebe08a2007-11-21 03:23:26 +0300518 pr_debug(PREFIX "~~~> interrupt\n");
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300519 atomic_inc(&ec->irq_count);
520 if (atomic_read(&ec->irq_count) > 5) {
521 pr_err(PREFIX "GPE storm detected, disabling EC GPE\n");
522 ec_switch_to_poll_mode(ec);
523 goto end;
524 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400525 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400526 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300527 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500528
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300529 if (state & ACPI_EC_FLAG_SCI) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400530 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
531 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
532 acpi_ec_gpe_query, ec);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300533 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
Alexey Starikovskiyb77d81b2008-03-21 17:07:15 +0300534 !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300535 in_interrupt()) {
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400536 /* this is non-query, must be confirmation */
Márton Németh3ebe08a2007-11-21 03:23:26 +0300537 if (printk_ratelimit())
538 pr_info(PREFIX "non-query interrupt received,"
539 " switching to interrupt mode\n");
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400540 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300541 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400542 }
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300543end:
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300544 ec_schedule_ec_poll(ec);
Alexey Starikovskiy78439322007-10-22 14:18:43 +0400545 return ACPI_SUCCESS(status) ?
Len Brown50526df2005-08-11 17:32:05 -0400546 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300549static void do_ec_poll(struct work_struct *work)
550{
551 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300552 atomic_set(&ec->irq_count, 0);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300553 (void)acpi_ec_gpe_handler(ec);
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/* --------------------------------------------------------------------------
557 Address Space Management
558 -------------------------------------------------------------------------- */
559
560static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400561acpi_ec_space_handler(u32 function, acpi_physical_address address,
562 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400563 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300565 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300566 int result = 0, i;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400567 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400572 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400574
575 if (bits != 8 && acpi_strict)
576 return AE_BAD_PARAMETER;
577
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300578 acpi_ec_burst_enable(ec);
579
Alexey Starikovskiy3e71a872008-01-11 02:42:51 +0300580 if (function == ACPI_READ) {
581 result = acpi_ec_read(ec, address, &temp);
582 *value = temp;
583 } else {
584 temp = 0xff & (*value);
585 result = acpi_ec_write(ec, address, temp);
586 }
587
588 for (i = 8; unlikely(bits - i > 0); i += 8) {
589 ++address;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400590 if (function == ACPI_READ) {
591 result = acpi_ec_read(ec, address, &temp);
592 (*value) |= ((acpi_integer)temp) << i;
593 } else {
594 temp = 0xff & ((*value) >> i);
595 result = acpi_ec_write(ec, address, temp);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Alexey Starikovskiyb3b233c2008-01-11 02:42:57 +0300599 acpi_ec_burst_disable(ec);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 switch (result) {
602 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* --------------------------------------------------------------------------
617 FS Interface (/proc)
618 -------------------------------------------------------------------------- */
619
Len Brown50526df2005-08-11 17:32:05 -0400620static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Len Brown50526df2005-08-11 17:32:05 -0400622static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300624 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!ec)
627 goto end;
628
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300629 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
630 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
631 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
632 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400633 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400634 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
639{
640 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
641}
642
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400643static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400644 .open = acpi_ec_info_open_fs,
645 .read = seq_read,
646 .llseek = seq_lseek,
647 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 .owner = THIS_MODULE,
649};
650
Len Brown50526df2005-08-11 17:32:05 -0400651static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Len Brown50526df2005-08-11 17:32:05 -0400653 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!acpi_device_dir(device)) {
656 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400657 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700662 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
663 acpi_device_dir(device),
664 &acpi_ec_info_ops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Len Brown50526df2005-08-11 17:32:05 -0400670static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (acpi_device_dir(device)) {
674 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
675 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
676 acpi_device_dir(device) = NULL;
677 }
678
Patrick Mocheld550d982006-06-27 00:41:40 -0400679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/* --------------------------------------------------------------------------
683 Driver Interface
684 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300685static acpi_status
686ec_parse_io_ports(struct acpi_resource *resource, void *context);
687
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300688static struct acpi_ec *make_acpi_ec(void)
689{
690 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
691 if (!ec)
692 return NULL;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400693 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300694 mutex_init(&ec->lock);
695 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400696 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300697 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
Alexey Starikovskiyfa95ba02008-03-21 19:36:02 +0300698 atomic_set(&ec->irq_count, 0);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300699 return ec;
700}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400702static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400703acpi_ec_register_query_methods(acpi_handle handle, u32 level,
704 void *context, void **return_value)
705{
706 struct acpi_namespace_node *node = handle;
707 struct acpi_ec *ec = context;
708 int value = 0;
709 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
710 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
711 }
712 return AE_OK;
713}
714
715static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400716ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400717{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400718 acpi_status status;
719
720 struct acpi_ec *ec = context;
721 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
722 ec_parse_io_ports, ec);
723 if (ACPI_FAILURE(status))
724 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400725
726 /* Get GPE bit assignment (EC events). */
727 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400728 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
729 if (ACPI_FAILURE(status))
730 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400731 /* Use the global lock for all EC transactions? */
732 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400733 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400734 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400735}
736
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300737static void ec_poll_stop(struct acpi_ec *ec)
738{
739 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
740 cancel_delayed_work(&ec->work);
741}
742
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400743static void ec_remove_handlers(struct acpi_ec *ec)
744{
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300745 ec_poll_stop(ec);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400746 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
747 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300748 pr_err(PREFIX "failed to remove space handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400749 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
750 &acpi_ec_gpe_handler)))
Márton Németh3ebe08a2007-11-21 03:23:26 +0300751 pr_err(PREFIX "failed to remove gpe handler\n");
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400752 ec->handlers_installed = 0;
753}
754
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400755static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400757 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400760 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300761 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
762 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
763
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400764 /* Check for boot EC */
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300765 if (boot_ec &&
766 (boot_ec->handle == device->handle ||
767 boot_ec->handle == ACPI_ROOT_OBJECT)) {
768 ec = boot_ec;
769 boot_ec = NULL;
770 } else {
771 ec = make_acpi_ec();
772 if (!ec)
773 return -ENOMEM;
774 if (ec_parse_device(device->handle, 0, ec, NULL) !=
775 AE_CTRL_TERMINATE) {
776 kfree(ec);
777 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400778 }
779 }
780
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300781 ec->handle = device->handle;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300782
783 /* Find and register all query methods */
784 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
785 acpi_ec_register_query_methods, ec, NULL);
786
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400787 if (!first_ec)
788 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300790 acpi_ec_add_fs(device);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300791 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400792 ec->gpe, ec->command_addr, ec->data_addr);
Márton Németh3ebe08a2007-11-21 03:23:26 +0300793 pr_info(PREFIX "driver started in %s mode\n",
Alexey Starikovskiy95b937e2007-10-22 14:19:03 +0400794 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Len Brown50526df2005-08-11 17:32:05 -0400798static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300800 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200801 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400804 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400807 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200808 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400809 list_del(&handler->node);
810 kfree(handler);
811 }
812 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300814 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300815 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300816 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400817 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300822ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300824 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300826 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /*
830 * The first address region returned is the data port, and
831 * the second address region returned is the status/command
832 * port.
833 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300834 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400835 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300836 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400837 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300838 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return AE_OK;
842}
843
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300844static int ec_install_handlers(struct acpi_ec *ec)
845{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300846 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400847 if (ec->handlers_installed)
848 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300849 status = acpi_install_gpe_handler(NULL, ec->gpe,
850 ACPI_GPE_EDGE_TRIGGERED,
851 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300852 if (ACPI_FAILURE(status))
853 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300854
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300855 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
856 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
857
858 status = acpi_install_address_space_handler(ec->handle,
859 ACPI_ADR_SPACE_EC,
860 &acpi_ec_space_handler,
Alexey Starikovskiy6d9e1122008-03-24 23:22:29 +0300861 NULL, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300862 if (ACPI_FAILURE(status)) {
863 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
864 return -ENODEV;
865 }
866
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400867 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300868 return 0;
869}
870
Len Brown50526df2005-08-11 17:32:05 -0400871static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300873 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400874 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400877 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 ec = acpi_driver_data(device);
880
881 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400884 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300885
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400886 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400887 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy845625c2008-03-21 17:07:03 +0300888 ec_schedule_ec_poll(ec);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Len Brown50526df2005-08-11 17:32:05 -0400892static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300894 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300898 if (!ec)
899 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400900 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400901
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500905int __init acpi_boot_ec_enable(void)
906{
907 if (!boot_ec || boot_ec->handlers_installed)
908 return 0;
909 if (!ec_install_handlers(boot_ec)) {
910 first_ec = boot_ec;
911 return 0;
912 }
913 return -EFAULT;
914}
915
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300916static const struct acpi_device_id ec_device_ids[] = {
917 {"PNP0C09", 0},
918 {"", 0},
919};
920
Len Brown50526df2005-08-11 17:32:05 -0400921int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Len Brown50526df2005-08-11 17:32:05 -0400923 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300924 acpi_status status;
925 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300927 boot_ec = make_acpi_ec();
928 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300929 return -ENOMEM;
930 /*
931 * Generate a boot ec context
932 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300933 status = acpi_get_table(ACPI_SIG_ECDT, 1,
934 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400935 if (ACPI_SUCCESS(status)) {
Márton Németh3ebe08a2007-11-21 03:23:26 +0300936 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400937 boot_ec->command_addr = ecdt_ptr->control.address;
938 boot_ec->data_addr = ecdt_ptr->data.address;
Zhao Yakui25008222008-08-12 10:40:10 +0800939 if (dmi_check_system(ec_dmi_table)) {
940 /*
941 * If the board falls into ec_dmi_table, it means
942 * that ECDT table gives the incorrect command/status
943 * & data I/O address. Just fix it.
944 */
945 boot_ec->data_addr = ecdt_ptr->control.address;
946 boot_ec->command_addr = ecdt_ptr->data.address;
947 }
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400948 boot_ec->gpe = ecdt_ptr->gpe;
Len Brown4af8e102008-03-11 00:27:16 -0400949 boot_ec->handle = ACPI_ROOT_OBJECT;
Alexey Starikovskiyce52ddf2008-03-24 23:22:36 +0300950 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400951 } else {
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300952 /* This workaround is needed only on some broken machines,
953 * which require early EC, but fail to provide ECDT */
954 acpi_handle x;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400955 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
956 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
957 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400958 /* Check that acpi_get_devices actually find something */
959 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400960 goto error;
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300961 /* We really need to limit this workaround, the only ASUS,
962 * which needs it, has fake EC._INI method, so use it as flag.
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500963 * Keep boot_ec struct as it will be needed soon.
Alexey Starikovskiy5870a8c2007-11-15 21:52:47 +0300964 */
965 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -0500966 return -ENODEV;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300969 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300970 if (!ret) {
971 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300972 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300973 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300974 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300975 kfree(boot_ec);
976 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENODEV;
978}
979
Alexey Starikovskiy223883b2008-03-21 17:07:21 +0300980static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
981{
982 struct acpi_ec *ec = acpi_driver_data(device);
983 /* Stop using GPE */
984 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
985 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
986 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
987 return 0;
988}
989
990static int acpi_ec_resume(struct acpi_device *device)
991{
992 struct acpi_ec *ec = acpi_driver_data(device);
993 /* Enable use of GPE back */
994 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
995 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
996 return 0;
997}
998
999static struct acpi_driver acpi_ec_driver = {
1000 .name = "ec",
1001 .class = ACPI_EC_CLASS,
1002 .ids = ec_device_ids,
1003 .ops = {
1004 .add = acpi_ec_add,
1005 .remove = acpi_ec_remove,
1006 .start = acpi_ec_start,
1007 .stop = acpi_ec_stop,
1008 .suspend = acpi_ec_suspend,
1009 .resume = acpi_ec_resume,
1010 },
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 */