blob: 08fbe62a2db466f87b1e3adca37a2924625e2d9d [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 Starikovskiy080e4122007-10-22 14:18:30 +0400231 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400232 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400233 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300234 end:
235 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400236}
237
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400238static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300239 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200240 u8 * rdata, unsigned rdata_len,
241 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400242{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400243 int status;
Len Brown50526df2005-08-11 17:32:05 -0400244 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400246 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300249 if (rdata)
250 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300252 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400253 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300255 if (ACPI_FAILURE(status)) {
256 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500260
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300261 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300262 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300263
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400264 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400265 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400266 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300267 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500268 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300271 status = acpi_ec_transaction_unlocked(ec, command,
272 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200273 rdata, rdata_len,
274 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400275
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300276 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400278 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300280 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300285/*
286 * Note: samsung nv5000 doesn't work with ec burst mode.
287 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
288 */
289int acpi_ec_burst_enable(struct acpi_ec *ec)
290{
291 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200292 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300293}
294
295int acpi_ec_burst_disable(struct acpi_ec *ec)
296{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200297 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300298}
299
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300300static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400301{
302 int result;
303 u8 d;
304
305 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200306 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400307 *data = d;
308 return result;
309}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400310
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400311static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
312{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300313 u8 wdata[2] = { address, data };
314 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200315 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Externally callable EC access functions. For now, assume 1 EC only
320 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300321int ec_burst_enable(void)
322{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300323 if (!first_ec)
324 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300325 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300326}
327
328EXPORT_SYMBOL(ec_burst_enable);
329
330int ec_burst_disable(void)
331{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300332 if (!first_ec)
333 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300334 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300335}
336
337EXPORT_SYMBOL(ec_burst_disable);
338
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300339int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400342 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!first_ec)
345 return -ENODEV;
346
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300347 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!err) {
350 *val = temp_data;
351 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400352 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
Len Brown50526df2005-08-11 17:32:05 -0400355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356EXPORT_SYMBOL(ec_read);
357
Len Brown50526df2005-08-11 17:32:05 -0400358int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int err;
361
362 if (!first_ec)
363 return -ENODEV;
364
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300365 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 return err;
368}
Len Brown50526df2005-08-11 17:32:05 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370EXPORT_SYMBOL(ec_write);
371
Randy Dunlap616362d2006-10-27 01:47:34 -0400372int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500373 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200374 u8 * rdata, unsigned rdata_len,
375 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400376{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400377 if (!first_ec)
378 return -ENODEV;
379
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300380 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200381 wdata_len, rdata, rdata_len,
382 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400383}
Luming Yu45bea152005-07-23 04:08:00 -0400384
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400385EXPORT_SYMBOL(ec_transaction);
386
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400388{
389 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300390 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400391
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300392 if (!ec || !data)
393 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400394
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300395 /*
396 * Query the EC to find out which _Qxx method we need to evaluate.
397 * Note that successful completion of the query causes the ACPI_EC_SCI
398 * bit to be cleared (and thus clearing the interrupt source).
399 */
Luming Yu45bea152005-07-23 04:08:00 -0400400
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200401 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300402 if (result)
403 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400404
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300405 if (!d)
406 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400407
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300408 *data = d;
409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/* --------------------------------------------------------------------------
413 Event Management
414 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400415int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
416 acpi_handle handle, acpi_ec_query_func func,
417 void *data)
418{
419 struct acpi_ec_query_handler *handler =
420 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
421 if (!handler)
422 return -ENOMEM;
423
424 handler->query_bit = query_bit;
425 handler->handle = handle;
426 handler->func = func;
427 handler->data = data;
428 mutex_lock(&ec->lock);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400429 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400430 mutex_unlock(&ec->lock);
431 return 0;
432}
433
434EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
435
436void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
437{
438 struct acpi_ec_query_handler *handler;
439 mutex_lock(&ec->lock);
440 list_for_each_entry(handler, &ec->list, node) {
441 if (query_bit == handler->query_bit) {
442 list_del(&handler->node);
443 kfree(handler);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400444 }
445 }
446 mutex_unlock(&ec->lock);
447}
448
449EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Len Brown50526df2005-08-11 17:32:05 -0400451static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300453 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400454 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400455 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400456
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300457 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300458 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400459 mutex_lock(&ec->lock);
460 list_for_each_entry(handler, &ec->list, node) {
461 if (value == handler->query_bit) {
462 /* have custom handler for this bit */
463 memcpy(&copy, handler, sizeof(copy));
464 mutex_unlock(&ec->lock);
465 if (copy.func) {
466 copy.func(copy.data);
467 } else if (copy.handle) {
468 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
469 }
470 return;
471 }
472 }
473 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown50526df2005-08-11 17:32:05 -0400476static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Len Brown50526df2005-08-11 17:32:05 -0400478 acpi_status status = AE_OK;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300479 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200480
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400481 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300482
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400483 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300484 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500485 }
486
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400487 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
488 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
489 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
490 acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400491 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300492
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500493 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400494 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/* --------------------------------------------------------------------------
498 Address Space Management
499 -------------------------------------------------------------------------- */
500
501static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400502acpi_ec_space_setup(acpi_handle region_handle,
503 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 /*
506 * The EC object is in the handler context and is needed
507 * when calling the acpi_ec_space_handler.
508 */
Len Brown50526df2005-08-11 17:32:05 -0400509 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
510 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return AE_OK;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400516acpi_ec_space_handler(u32 function, acpi_physical_address address,
517 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400518 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300520 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400521 int result = 0, i = 0;
522 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400525 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400527 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400528 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400529
530 if (bits != 8 && acpi_strict)
531 return AE_BAD_PARAMETER;
532
533 while (bits - i > 0) {
534 if (function == ACPI_READ) {
535 result = acpi_ec_read(ec, address, &temp);
536 (*value) |= ((acpi_integer)temp) << i;
537 } else {
538 temp = 0xff & ((*value) >> i);
539 result = acpi_ec_write(ec, address, temp);
540 }
541 i += 8;
542 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 switch (result) {
546 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400547 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400556 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/* --------------------------------------------------------------------------
561 FS Interface (/proc)
562 -------------------------------------------------------------------------- */
563
Len Brown50526df2005-08-11 17:32:05 -0400564static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Len Brown50526df2005-08-11 17:32:05 -0400566static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300568 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!ec)
571 goto end;
572
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300573 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
574 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
575 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
576 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400577 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400578 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
583{
584 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
585}
586
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400587static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400588 .open = acpi_ec_info_open_fs,
589 .read = seq_read,
590 .llseek = seq_lseek,
591 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 .owner = THIS_MODULE,
593};
594
Len Brown50526df2005-08-11 17:32:05 -0400595static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Len Brown50526df2005-08-11 17:32:05 -0400597 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (!acpi_device_dir(device)) {
600 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400601 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400607 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else {
611 entry->proc_fops = &acpi_ec_info_ops;
612 entry->data = acpi_driver_data(device);
613 entry->owner = THIS_MODULE;
614 }
615
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Len Brown50526df2005-08-11 17:32:05 -0400619static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (acpi_device_dir(device)) {
623 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
624 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
625 acpi_device_dir(device) = NULL;
626 }
627
Patrick Mocheld550d982006-06-27 00:41:40 -0400628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/* --------------------------------------------------------------------------
632 Driver Interface
633 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300634static acpi_status
635ec_parse_io_ports(struct acpi_resource *resource, void *context);
636
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300637static struct acpi_ec *make_acpi_ec(void)
638{
639 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
640 if (!ec)
641 return NULL;
642
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400643 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300644 mutex_init(&ec->lock);
645 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400646 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647
648 return ec;
649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400651static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400652acpi_ec_register_query_methods(acpi_handle handle, u32 level,
653 void *context, void **return_value)
654{
655 struct acpi_namespace_node *node = handle;
656 struct acpi_ec *ec = context;
657 int value = 0;
658 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
659 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
660 }
661 return AE_OK;
662}
663
664static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400665ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400666{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400667 acpi_status status;
668
669 struct acpi_ec *ec = context;
670 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
671 ec_parse_io_ports, ec);
672 if (ACPI_FAILURE(status))
673 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400674
675 /* Get GPE bit assignment (EC events). */
676 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400677 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
678 if (ACPI_FAILURE(status))
679 return status;
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400680 /* Find and register all query methods */
681 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
682 acpi_ec_register_query_methods, ec, NULL);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400683 /* Use the global lock for all EC transactions? */
684 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400685 ec->handle = handle;
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400686 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400687}
688
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400689static void ec_remove_handlers(struct acpi_ec *ec)
690{
691 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
692 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
693 printk(KERN_ERR PREFIX "failed to remove space handler\n");
694 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
695 &acpi_ec_gpe_handler)))
696 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
697 ec->handlers_installed = 0;
698}
699
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400700static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400702 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400705 return -EINVAL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300706 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
707 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
708
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400709 /* Check for boot EC */
710 if (boot_ec) {
711 if (boot_ec->handle == device->handle) {
712 /* Pre-loaded EC from DSDT, just move pointer */
713 ec = boot_ec;
714 boot_ec = NULL;
715 goto end;
716 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
717 /* ECDT-based EC, time to shut it down */
718 ec_remove_handlers(boot_ec);
719 kfree(boot_ec);
720 first_ec = boot_ec = NULL;
721 }
722 }
723
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300724 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400728 if (ec_parse_device(device->handle, 0, ec, NULL) !=
729 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300730 kfree(ec);
731 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400732 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300733 ec->handle = device->handle;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400734 end:
735 if (!first_ec)
736 first_ec = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 acpi_driver_data(device) = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300738 acpi_ec_add_fs(device);
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400739 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
740 ec->gpe, ec->command_addr, ec->data_addr);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Len Brown50526df2005-08-11 17:32:05 -0400744static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300746 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200747 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400753 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200754 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400755 list_del(&handler->node);
756 kfree(handler);
757 }
758 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300760 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300761 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300762 first_ec = NULL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400763 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300768ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300770 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300772 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /*
776 * The first address region returned is the data port, and
777 * the second address region returned is the status/command
778 * port.
779 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300780 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400781 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300782 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400783 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300784 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return AE_OK;
788}
789
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300790static int ec_install_handlers(struct acpi_ec *ec)
791{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300792 acpi_status status;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400793 if (ec->handlers_installed)
794 return 0;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300795 status = acpi_install_gpe_handler(NULL, ec->gpe,
796 ACPI_GPE_EDGE_TRIGGERED,
797 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300798 if (ACPI_FAILURE(status))
799 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300800
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300801 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
802 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
803
804 status = acpi_install_address_space_handler(ec->handle,
805 ACPI_ADR_SPACE_EC,
806 &acpi_ec_space_handler,
807 &acpi_ec_space_setup, ec);
808 if (ACPI_FAILURE(status)) {
809 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
810 return -ENODEV;
811 }
812
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400813 ec->handlers_installed = 1;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300814 return 0;
815}
816
Len Brown50526df2005-08-11 17:32:05 -0400817static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300819 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400820 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 ec = acpi_driver_data(device);
826
827 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400828 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400830 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300831
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400832 /* EC is fully operational, allow queries */
Alexey Starikovskiy080e4122007-10-22 14:18:30 +0400833 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Len Brown50526df2005-08-11 17:32:05 -0400837static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300839 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300843 if (!ec)
844 return -EINVAL;
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400845 ec_remove_handlers(ec);
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400846
Patrick Mocheld550d982006-06-27 00:41:40 -0400847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Len Brown50526df2005-08-11 17:32:05 -0400850int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Len Brown50526df2005-08-11 17:32:05 -0400852 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300853 acpi_status status;
854 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300856 boot_ec = make_acpi_ec();
857 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300858 return -ENOMEM;
859 /*
860 * Generate a boot ec context
861 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300862 status = acpi_get_table(ACPI_SIG_ECDT, 1,
863 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400864 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy4c611062007-09-05 19:56:38 -0400865 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400866 boot_ec->command_addr = ecdt_ptr->control.address;
867 boot_ec->data_addr = ecdt_ptr->data.address;
868 boot_ec->gpe = ecdt_ptr->gpe;
869 boot_ec->handle = ACPI_ROOT_OBJECT;
870 } else {
871 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
872 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
873 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400874 /* Check that acpi_get_devices actually find something */
875 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400876 goto error;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300879 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300880 if (!ret) {
881 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300882 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300883 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300884 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300885 kfree(boot_ec);
886 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -ENODEV;
888}
889
Len Brown50526df2005-08-11 17:32:05 -0400890static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Len Brown50526df2005-08-11 17:32:05 -0400892 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
898 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Now register the driver for the EC */
902 result = acpi_bus_register_driver(&acpi_ec_driver);
903 if (result < 0) {
904 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400905 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
Patrick Mocheld550d982006-06-27 00:41:40 -0400908 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911subsys_initcall(acpi_ec_init);
912
913/* EC driver currently not unloadable */
914#if 0
Len Brown50526df2005-08-11 17:32:05 -0400915static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 acpi_bus_unregister_driver(&acpi_ec_driver);
919
920 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
921
Patrick Mocheld550d982006-06-27 00:41:40 -0400922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
Len Brown50526df2005-08-11 17:32:05 -0400924#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Len Brown02b28a32005-12-05 16:33:04 -0500926static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400927{
Len Brown02b28a32005-12-05 16:33:04 -0500928 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400929
Len Brown02b28a32005-12-05 16:33:04 -0500930 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400931 return 0;
932
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300933 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
934
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500935 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400936
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800937 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400938}
Len Brown50526df2005-08-11 17:32:05 -0400939
Len Brown53f11d42005-12-05 16:46:36 -0500940__setup("ec_intr=", acpi_ec_set_intr_mode);