blob: 5ce90ce22b5849b211e13086fd71fb892aee4a8d [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
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
33#include <linux/delay.h>
34#include <linux/proc_fs.h>
35#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050036#include <linux/interrupt.h>
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040037#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/io.h>
39#include <acpi/acpi_bus.h>
40#include <acpi/acpi_drivers.h>
41#include <acpi/actypes.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_EC_CLASS "embedded_controller"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_EC_DEVICE_NAME "Embedded Controller"
45#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040046
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030047#undef PREFIX
48#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040049
Denis M. Sadykov703959d2006-09-26 19:50:33 +040050/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050053#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040055
Denis M. Sadykov703959d2006-09-26 19:50:33 +040056/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030057enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030058 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030063};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040064
Denis M. Sadykov703959d2006-09-26 19:50:33 +040065/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030066enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040067 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040068 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069};
70
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030071#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040073
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030074static enum ec_mode {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030075 EC_INTR = 1, /* Output buffer full */
76 EC_POLL, /* Input buffer empty */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030077} acpi_ec_mode = EC_INTR;
Denis M. Sadykov703959d2006-09-26 19:50:33 +040078
Alexey Starikovskiy080e4122007-10-22 14:18:30 +040079enum {
80 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
81 EC_FLAGS_QUERY_PENDING, /* Query is pending */
82};
83
Len Brown50526df2005-08-11 17:32:05 -040084static int acpi_ec_remove(struct acpi_device *device, int type);
85static int acpi_ec_start(struct acpi_device *device);
86static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040087static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089static const struct acpi_device_id ec_device_ids[] = {
90 {"PNP0C09", 0},
91 {"", 0},
92};
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050095 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040096 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020097 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040098 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040099 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -0400100 .remove = acpi_ec_remove,
101 .start = acpi_ec_start,
102 .stop = acpi_ec_stop,
103 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400105
106/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300107/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400108typedef int (*acpi_ec_query_func) (void *data);
109
110struct acpi_ec_query_handler {
111 struct list_head node;
112 acpi_ec_query_func func;
113 acpi_handle handle;
114 void *data;
115 u8 query_bit;
116};
117
Adrian Bunka854e082006-12-19 12:56:12 -0800118static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400119 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300120 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400121 unsigned long command_addr;
122 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123 unsigned long global_lock;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400124 unsigned long flags;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300125 struct mutex lock;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400127 struct list_head list;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400128 u8 handlers_installed;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300129} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* --------------------------------------------------------------------------
132 Transaction Management
133 -------------------------------------------------------------------------- */
134
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400140static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400141{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400143}
144
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400145static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400146{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400147 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148}
149
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400150static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400151{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400153}
154
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400155static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400156{
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400157 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500158 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300159 if (event == ACPI_EC_EVENT_OBF_1) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400160 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300162 } else if (event == ACPI_EC_EVENT_IBF_0) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400163 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400164 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400165 }
166
167 return 0;
168}
169
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400170static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400171{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200172 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300173 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400174 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300175 while (time_before(jiffies, delay)) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400176 if (acpi_ec_check_status(ec, event))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400177 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400178 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300179 } else {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400180 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
181 msecs_to_jiffies(ACPI_EC_DELAY)))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400182 return 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400183 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
184 if (acpi_ec_check_status(ec, event)) {
185 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400186 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300187 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400188 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
189 " status = %d, expect_event = %d\n",
190 acpi_ec_read_status(ec), event);
Patrick Mocheld550d982006-06-27 00:41:40 -0400191 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500192}
193
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400194static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300195 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200196 u8 * rdata, unsigned rdata_len,
197 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400198{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300199 int result = 0;
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400200 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400201 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400202
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300203 for (; wdata_len > 0; --wdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400204 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300205 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300206 printk(KERN_ERR PREFIX
207 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300208 goto end;
209 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400210 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400211 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400212 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400213
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300214 if (!rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400215 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300216 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300217 printk(KERN_ERR PREFIX
218 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300219 goto end;
220 }
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400221 } else if (command == ACPI_EC_COMMAND_QUERY)
222 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400223
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300224 for (; rdata_len > 0; --rdata_len) {
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400225 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300226 if (result) {
227 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300228 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300229 goto end;
230 }
Alexey Starikovskiy0c5d31f2007-10-22 14:18:36 +0400231 /* Don't expect GPE after last read */
232 if (rdata_len > 1)
233 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400234 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400235 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300236 end:
237 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400238}
239
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400240static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300241 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200242 u8 * rdata, unsigned rdata_len,
243 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400244{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400245 int status;
Len Brown50526df2005-08-11 17:32:05 -0400246 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400248 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300251 if (rdata)
252 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300254 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400255 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300257 if (ACPI_FAILURE(status)) {
258 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500262
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300263 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300264 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300265
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400266 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400267 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400268 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300269 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500270 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300273 status = acpi_ec_transaction_unlocked(ec, command,
274 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200275 rdata, rdata_len,
276 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400277
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300278 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400280 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300282 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300287/*
288 * Note: samsung nv5000 doesn't work with ec burst mode.
289 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
290 */
291int acpi_ec_burst_enable(struct acpi_ec *ec)
292{
293 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200294 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300295}
296
297int acpi_ec_burst_disable(struct acpi_ec *ec)
298{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200299 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300300}
301
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300302static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400303{
304 int result;
305 u8 d;
306
307 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200308 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400309 *data = d;
310 return result;
311}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400312
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400313static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
314{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300315 u8 wdata[2] = { address, data };
316 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200317 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/*
321 * Externally callable EC access functions. For now, assume 1 EC only
322 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300323int ec_burst_enable(void)
324{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300325 if (!first_ec)
326 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300327 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300328}
329
330EXPORT_SYMBOL(ec_burst_enable);
331
332int ec_burst_disable(void)
333{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300334 if (!first_ec)
335 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300336 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300337}
338
339EXPORT_SYMBOL(ec_burst_disable);
340
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300341int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400344 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (!first_ec)
347 return -ENODEV;
348
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300349 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (!err) {
352 *val = temp_data;
353 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400354 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return err;
356}
Len Brown50526df2005-08-11 17:32:05 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358EXPORT_SYMBOL(ec_read);
359
Len Brown50526df2005-08-11 17:32:05 -0400360int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int err;
363
364 if (!first_ec)
365 return -ENODEV;
366
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300367 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 return err;
370}
Len Brown50526df2005-08-11 17:32:05 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372EXPORT_SYMBOL(ec_write);
373
Randy Dunlap616362d2006-10-27 01:47:34 -0400374int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500375 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200376 u8 * rdata, unsigned rdata_len,
377 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400378{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400379 if (!first_ec)
380 return -ENODEV;
381
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300382 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200383 wdata_len, rdata, rdata_len,
384 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400385}
Luming Yu45bea152005-07-23 04:08:00 -0400386
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400387EXPORT_SYMBOL(ec_transaction);
388
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300389static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400390{
391 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400393
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300394 if (!ec || !data)
395 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400396
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397 /*
398 * Query the EC to find out which _Qxx method we need to evaluate.
399 * Note that successful completion of the query causes the ACPI_EC_SCI
400 * bit to be cleared (and thus clearing the interrupt source).
401 */
Luming Yu45bea152005-07-23 04:08:00 -0400402
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200403 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300404 if (result)
405 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400406
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300407 if (!d)
408 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400409
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300410 *data = d;
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* --------------------------------------------------------------------------
415 Event Management
416 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400417int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
418 acpi_handle handle, acpi_ec_query_func func,
419 void *data)
420{
421 struct acpi_ec_query_handler *handler =
422 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
423 if (!handler)
424 return -ENOMEM;
425
426 handler->query_bit = query_bit;
427 handler->handle = handle;
428 handler->func = func;
429 handler->data = data;
430 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400431 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400432 mutex_unlock(&ec->lock);
433 return 0;
434}
435
436EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
437
438void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
439{
440 struct acpi_ec_query_handler *handler;
441 mutex_lock(&ec->lock);
442 list_for_each_entry(handler, &ec->list, node) {
443 if (query_bit == handler->query_bit) {
444 list_del(&handler->node);
445 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400446 }
447 }
448 mutex_unlock(&ec->lock);
449}
450
451EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Len Brown50526df2005-08-11 17:32:05 -0400453static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300455 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400456 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400457 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400458
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300459 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300460 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400461 mutex_lock(&ec->lock);
462 list_for_each_entry(handler, &ec->list, node) {
463 if (value == handler->query_bit) {
464 /* have custom handler for this bit */
465 memcpy(&copy, handler, sizeof(copy));
466 mutex_unlock(&ec->lock);
467 if (copy.func) {
468 copy.func(copy.data);
469 } else if (copy.handle) {
470 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
471 }
472 return;
473 }
474 }
475 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Len Brown50526df2005-08-11 17:32:05 -0400478static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Len Brown50526df2005-08-11 17:32:05 -0400480 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300481 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200482
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400483 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300484
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400485 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300486 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500487 }
488
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400489 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
490 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
491 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
492 acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400493 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300494
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500495 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400496 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/* --------------------------------------------------------------------------
500 Address Space Management
501 -------------------------------------------------------------------------- */
502
503static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400504acpi_ec_space_setup(acpi_handle region_handle,
505 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 /*
508 * The EC object is in the handler context and is needed
509 * when calling the acpi_ec_space_handler.
510 */
Len Brown50526df2005-08-11 17:32:05 -0400511 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
512 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 return AE_OK;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400518acpi_ec_space_handler(u32 function, acpi_physical_address address,
519 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400520 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300522 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400523 int result = 0, i = 0;
524 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400529 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400530 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400531
532 if (bits != 8 && acpi_strict)
533 return AE_BAD_PARAMETER;
534
535 while (bits - i > 0) {
536 if (function == ACPI_READ) {
537 result = acpi_ec_read(ec, address, &temp);
538 (*value) |= ((acpi_integer)temp) << i;
539 } else {
540 temp = 0xff & ((*value) >> i);
541 result = acpi_ec_write(ec, address, temp);
542 }
543 i += 8;
544 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 switch (result) {
548 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/* --------------------------------------------------------------------------
563 FS Interface (/proc)
564 -------------------------------------------------------------------------- */
565
Len Brown50526df2005-08-11 17:32:05 -0400566static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Len Brown50526df2005-08-11 17:32:05 -0400568static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300570 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!ec)
573 goto end;
574
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300575 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
576 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
577 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
578 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400579 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400580 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
585{
586 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
587}
588
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400589static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400590 .open = acpi_ec_info_open_fs,
591 .read = seq_read,
592 .llseek = seq_lseek,
593 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .owner = THIS_MODULE,
595};
596
Len Brown50526df2005-08-11 17:32:05 -0400597static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Len Brown50526df2005-08-11 17:32:05 -0400599 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!acpi_device_dir(device)) {
602 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400603 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400609 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else {
613 entry->proc_fops = &acpi_ec_info_ops;
614 entry->data = acpi_driver_data(device);
615 entry->owner = THIS_MODULE;
616 }
617
Patrick Mocheld550d982006-06-27 00:41:40 -0400618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Len Brown50526df2005-08-11 17:32:05 -0400621static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (acpi_device_dir(device)) {
625 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
626 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
627 acpi_device_dir(device) = NULL;
628 }
629
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/* --------------------------------------------------------------------------
634 Driver Interface
635 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300636static acpi_status
637ec_parse_io_ports(struct acpi_resource *resource, void *context);
638
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300639static struct acpi_ec *make_acpi_ec(void)
640{
641 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
642 if (!ec)
643 return NULL;
644
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400645 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300646 mutex_init(&ec->lock);
647 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400648 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300649
650 return ec;
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400653static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400654acpi_ec_register_query_methods(acpi_handle handle, u32 level,
655 void *context, void **return_value)
656{
657 struct acpi_namespace_node *node = handle;
658 struct acpi_ec *ec = context;
659 int value = 0;
660 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
661 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
662 }
663 return AE_OK;
664}
665
666static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400667ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400668{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400669 acpi_status status;
670
671 struct acpi_ec *ec = context;
672 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
673 ec_parse_io_ports, ec);
674 if (ACPI_FAILURE(status))
675 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400676
677 /* Get GPE bit assignment (EC events). */
678 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400679 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
680 if (ACPI_FAILURE(status))
681 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400682 /* Find and register all query methods */
683 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
684 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400685 /* Use the global lock for all EC transactions? */
686 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400687 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400688 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400689}
690
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400691static void ec_remove_handlers(struct acpi_ec *ec)
692{
693 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
694 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
695 printk(KERN_ERR PREFIX "failed to remove space handler\n");
696 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
697 &acpi_ec_gpe_handler)))
698 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
699 ec->handlers_installed = 0;
700}
701
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400702static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400704 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400707 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300708 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
709 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
710
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400711 /* Check for boot EC */
712 if (boot_ec) {
713 if (boot_ec->handle == device->handle) {
714 /* Pre-loaded EC from DSDT, just move pointer */
715 ec = boot_ec;
716 boot_ec = NULL;
717 goto end;
718 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
719 /* ECDT-based EC, time to shut it down */
720 ec_remove_handlers(boot_ec);
721 kfree(boot_ec);
722 first_ec = boot_ec = NULL;
723 }
724 }
725
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400730 if (ec_parse_device(device->handle, 0, ec, NULL) !=
731 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300732 kfree(ec);
733 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400734 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400736 end:
737 if (!first_ec)
738 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300740 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400741 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
742 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Len Brown50526df2005-08-11 17:32:05 -0400746static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300748 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200749 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400755 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200756 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400757 list_del(&handler->node);
758 kfree(handler);
759 }
760 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300763 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300764 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400765 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300770ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300772 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300774 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * The first address region returned is the data port, and
779 * the second address region returned is the status/command
780 * port.
781 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300782 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400783 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300784 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400785 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300786 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return AE_OK;
790}
791
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300792static int ec_install_handlers(struct acpi_ec *ec)
793{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300794 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400795 if (ec->handlers_installed)
796 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300797 status = acpi_install_gpe_handler(NULL, ec->gpe,
798 ACPI_GPE_EDGE_TRIGGERED,
799 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300800 if (ACPI_FAILURE(status))
801 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300802
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300803 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
804 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
805
806 status = acpi_install_address_space_handler(ec->handle,
807 ACPI_ADR_SPACE_EC,
808 &acpi_ec_space_handler,
809 &acpi_ec_space_setup, ec);
810 if (ACPI_FAILURE(status)) {
811 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
812 return -ENODEV;
813 }
814
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400815 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300816 return 0;
817}
818
Len Brown50526df2005-08-11 17:32:05 -0400819static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300821 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400822 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 ec = acpi_driver_data(device);
828
829 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400832 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300833
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400834 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400835 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Len Brown50526df2005-08-11 17:32:05 -0400839static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300841 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300845 if (!ec)
846 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400847 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400848
Patrick Mocheld550d982006-06-27 00:41:40 -0400849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Len Brown50526df2005-08-11 17:32:05 -0400852int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Len Brown50526df2005-08-11 17:32:05 -0400854 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300855 acpi_status status;
856 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300858 boot_ec = make_acpi_ec();
859 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300860 return -ENOMEM;
861 /*
862 * Generate a boot ec context
863 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300864 status = acpi_get_table(ACPI_SIG_ECDT, 1,
865 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400866 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400867 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400868 boot_ec->command_addr = ecdt_ptr->control.address;
869 boot_ec->data_addr = ecdt_ptr->data.address;
870 boot_ec->gpe = ecdt_ptr->gpe;
871 boot_ec->handle = ACPI_ROOT_OBJECT;
872 } else {
873 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
874 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
875 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400876 /* Check that acpi_get_devices actually find something */
877 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400878 goto error;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300881 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300882 if (!ret) {
883 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300884 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300885 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300886 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300887 kfree(boot_ec);
888 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENODEV;
890}
891
Len Brown50526df2005-08-11 17:32:05 -0400892static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Len Brown50526df2005-08-11 17:32:05 -0400894 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
900 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400901 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* Now register the driver for the EC */
904 result = acpi_bus_register_driver(&acpi_ec_driver);
905 if (result < 0) {
906 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Patrick Mocheld550d982006-06-27 00:41:40 -0400910 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913subsys_initcall(acpi_ec_init);
914
915/* EC driver currently not unloadable */
916#if 0
Len Brown50526df2005-08-11 17:32:05 -0400917static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 acpi_bus_unregister_driver(&acpi_ec_driver);
921
922 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
923
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
Len Brown50526df2005-08-11 17:32:05 -0400926#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Len Brown02b28a32005-12-05 16:33:04 -0500928static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400929{
Len Brown02b28a32005-12-05 16:33:04 -0500930 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400931
Len Brown02b28a32005-12-05 16:33:04 -0500932 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400933 return 0;
934
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300935 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
936
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500937 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400938
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800939 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400940}
Len Brown50526df2005-08-11 17:32:05 -0400941
Len Brown53f11d42005-12-05 16:46:36 -0500942__setup("ec_intr=", acpi_ec_set_intr_mode);