blob: 56bee9e065cf3e395e6680b4667cdaad5da45d84 [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 Starikovskiy6ccedb12006-12-07 18:42:17 +030068 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
Len Brown50526df2005-08-11 17:32:05 -040079static int acpi_ec_remove(struct acpi_device *device, int type);
80static int acpi_ec_start(struct acpi_device *device);
81static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040082static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Thomas Renninger1ba90e32007-07-23 14:44:41 +020084static const struct acpi_device_id ec_device_ids[] = {
85 {"PNP0C09", 0},
86 {"", 0},
87};
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050090 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040091 .class = ACPI_EC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020092 .ids = ec_device_ids,
Len Brown50526df2005-08-11 17:32:05 -040093 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040094 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040095 .remove = acpi_ec_remove,
96 .start = acpi_ec_start,
97 .stop = acpi_ec_stop,
98 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400100
101/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300102/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400103typedef int (*acpi_ec_query_func) (void *data);
104
105struct acpi_ec_query_handler {
106 struct list_head node;
107 acpi_ec_query_func func;
108 acpi_handle handle;
109 void *data;
110 u8 query_bit;
111};
112
Adrian Bunka854e082006-12-19 12:56:12 -0800113static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400114 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300115 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400116 unsigned long command_addr;
117 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300119 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300120 atomic_t query_pending;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500121 atomic_t event_count;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400122 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400123 struct list_head list;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300124} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
129
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400130static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400132 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400136{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400138}
139
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400140static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400141{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400142 outb(command, ec->command_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_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400146{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400147 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148}
149
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500150static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
151 unsigned old_count)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400152{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300153 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500154 if (old_count == atomic_read(&ec->event_count))
155 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300156 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400157 if (status & ACPI_EC_FLAG_OBF)
158 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300159 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400160 if (!(status & ACPI_EC_FLAG_IBF))
161 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400162 }
163
164 return 0;
165}
166
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200167static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
168 unsigned count, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400169{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200170 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300171 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
172 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500173 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400174 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400175 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300176 } else {
177 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500178 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300179 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500180 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400181 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300182 } else {
183 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
184 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300185 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400186 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300187 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400188
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500190}
191
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400192static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300193 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200194 u8 * rdata, unsigned rdata_len,
195 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400196{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300197 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500198 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400199 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400200
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300201 for (; wdata_len > 0; --wdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200202 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300203 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300204 printk(KERN_ERR PREFIX
205 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300206 goto end;
207 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500208 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400209 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400210 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400211
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300212 if (!rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200213 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300214 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300215 printk(KERN_ERR PREFIX
216 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300217 goto end;
218 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300219 } else if (command == ACPI_EC_COMMAND_QUERY) {
220 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400221 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400222
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300223 for (; rdata_len > 0; --rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200224 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300225 if (result) {
226 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300227 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300228 goto end;
229 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500230 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400231 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400232 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300233 end:
234 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400235}
236
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400237static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300238 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200239 u8 * rdata, unsigned rdata_len,
240 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400241{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400242 int status;
Len Brown50526df2005-08-11 17:32:05 -0400243 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400245 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300248 if (rdata)
249 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300251 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400252 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300254 if (ACPI_FAILURE(status)) {
255 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500259
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300260 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300261 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300262
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200263 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400264 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400265 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300266 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500267 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300270 status = acpi_ec_transaction_unlocked(ec, command,
271 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200272 rdata, rdata_len,
273 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400274
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300275 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400277 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300279 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300284/*
285 * Note: samsung nv5000 doesn't work with ec burst mode.
286 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
287 */
288int acpi_ec_burst_enable(struct acpi_ec *ec)
289{
290 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200291 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300292}
293
294int acpi_ec_burst_disable(struct acpi_ec *ec)
295{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200296 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300297}
298
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300299static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400300{
301 int result;
302 u8 d;
303
304 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200305 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400306 *data = d;
307 return result;
308}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400309
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400310static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
311{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300312 u8 wdata[2] = { address, data };
313 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200314 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * Externally callable EC access functions. For now, assume 1 EC only
319 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300320int ec_burst_enable(void)
321{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300322 if (!first_ec)
323 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300324 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300325}
326
327EXPORT_SYMBOL(ec_burst_enable);
328
329int ec_burst_disable(void)
330{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300331 if (!first_ec)
332 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300333 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300334}
335
336EXPORT_SYMBOL(ec_burst_disable);
337
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300338int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400341 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!first_ec)
344 return -ENODEV;
345
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300346 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (!err) {
349 *val = temp_data;
350 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400351 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return err;
353}
Len Brown50526df2005-08-11 17:32:05 -0400354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355EXPORT_SYMBOL(ec_read);
356
Len Brown50526df2005-08-11 17:32:05 -0400357int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int err;
360
361 if (!first_ec)
362 return -ENODEV;
363
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300364 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return err;
367}
Len Brown50526df2005-08-11 17:32:05 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369EXPORT_SYMBOL(ec_write);
370
Randy Dunlap616362d2006-10-27 01:47:34 -0400371int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500372 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200373 u8 * rdata, unsigned rdata_len,
374 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400375{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400376 if (!first_ec)
377 return -ENODEV;
378
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300379 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200380 wdata_len, rdata, rdata_len,
381 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400382}
Luming Yu45bea152005-07-23 04:08:00 -0400383
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400384EXPORT_SYMBOL(ec_transaction);
385
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300386static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400387{
388 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300389 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400390
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300391 if (!ec || !data)
392 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400393
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300394 /*
395 * Query the EC to find out which _Qxx method we need to evaluate.
396 * Note that successful completion of the query causes the ACPI_EC_SCI
397 * bit to be cleared (and thus clearing the interrupt source).
398 */
Luming Yu45bea152005-07-23 04:08:00 -0400399
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200400 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300401 if (result)
402 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400403
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300404 if (!d)
405 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400406
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300407 *data = d;
408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* --------------------------------------------------------------------------
412 Event Management
413 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400414int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
415 acpi_handle handle, acpi_ec_query_func func,
416 void *data)
417{
418 struct acpi_ec_query_handler *handler =
419 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
420 if (!handler)
421 return -ENOMEM;
422
423 handler->query_bit = query_bit;
424 handler->handle = handle;
425 handler->func = func;
426 handler->data = data;
427 mutex_lock(&ec->lock);
428 list_add_tail(&handler->node, &ec->list);
429 mutex_unlock(&ec->lock);
430 return 0;
431}
432
433EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
434
435void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
436{
437 struct acpi_ec_query_handler *handler;
438 mutex_lock(&ec->lock);
439 list_for_each_entry(handler, &ec->list, node) {
440 if (query_bit == handler->query_bit) {
441 list_del(&handler->node);
442 kfree(handler);
443 break;
444 }
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;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400479 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300480 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200481
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500482 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300483
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400484 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300485 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500486 }
487
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300488 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300489 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
490 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300491 status =
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400492 acpi_os_execute(OSL_EC_BURST_HANDLER, 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 Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300645 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300646 atomic_set(&ec->event_count, 1);
647 mutex_init(&ec->lock);
648 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400649 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300650
651 return ec;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400654static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400655acpi_ec_register_query_methods(acpi_handle handle, u32 level,
656 void *context, void **return_value)
657{
658 struct acpi_namespace_node *node = handle;
659 struct acpi_ec *ec = context;
660 int value = 0;
661 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
662 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
663 }
664 return AE_OK;
665}
666
667static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400668ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400669{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400670 acpi_status status;
671
672 struct acpi_ec *ec = context;
673 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
674 ec_parse_io_ports, ec);
675 if (ACPI_FAILURE(status))
676 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400677
678 /* Get GPE bit assignment (EC events). */
679 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400680 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
681 if (ACPI_FAILURE(status))
682 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400683
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400684 /* Find and register all query methods */
685 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
686 acpi_ec_register_query_methods, ec, NULL);
687
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400688 /* Use the global lock for all EC transactions? */
689 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
690
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400691 ec->handle = handle;
692
Meelis Roos0a5245092007-07-26 12:56:55 +0300693 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400694 ec->gpe, ec->command_addr, ec->data_addr);
695
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400696 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400697}
698
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400699static void ec_remove_handlers(struct acpi_ec *ec)
700{
701 acpi_remove_address_space_handler(ec->handle,
702 ACPI_ADR_SPACE_EC,
703 &acpi_ec_space_handler);
704 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
705}
706
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400707static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400709 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400712 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300714 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
715 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
716
717 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400721 if (ec_parse_device(device->handle, 0, ec, NULL) !=
722 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300723 kfree(ec);
724 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400725 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726
727 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300728 if (boot_ec) {
729 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400730 ec_remove_handlers(boot_ec);
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400731 mutex_destroy(&boot_ec->lock);
732 kfree(boot_ec);
733 first_ec = boot_ec = NULL;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300734 }
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400735 }
736 if (!first_ec)
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300737 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300738 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300741 acpi_ec_add_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Len Brown50526df2005-08-11 17:32:05 -0400745static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300747 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200748 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400754 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200755 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400756 list_del(&handler->node);
757 kfree(handler);
758 }
759 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300761 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300762 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300763 first_ec = NULL;
764
Patrick Mocheld550d982006-06-27 00:41:40 -0400765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300769ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300771 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300773 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /*
777 * The first address region returned is the data port, and
778 * the second address region returned is the status/command
779 * port.
780 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300781 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400782 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300783 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400784 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300785 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return AE_OK;
789}
790
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300791static int ec_install_handlers(struct acpi_ec *ec)
792{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300793 acpi_status status;
794 status = acpi_install_gpe_handler(NULL, ec->gpe,
795 ACPI_GPE_EDGE_TRIGGERED,
796 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300797 if (ACPI_FAILURE(status))
798 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300799
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300800 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
801 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
802
803 status = acpi_install_address_space_handler(ec->handle,
804 ACPI_ADR_SPACE_EC,
805 &acpi_ec_space_handler,
806 &acpi_ec_space_setup, ec);
807 if (ACPI_FAILURE(status)) {
808 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
809 return -ENODEV;
810 }
811
812 return 0;
813}
814
Len Brown50526df2005-08-11 17:32:05 -0400815static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300817 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400818 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 ec = acpi_driver_data(device);
824
825 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400828 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300829
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400830 /* EC is fully operational, allow queries */
831 atomic_set(&ec->query_pending, 0);
832
833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Len Brown50526df2005-08-11 17:32:05 -0400836static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300838 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
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
843 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300844 if (!ec)
845 return -EINVAL;
Alexey Starikovskiy7c010de2007-08-03 17:57:53 -0400846 ec_remove_handlers(ec);
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)) {
865 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n\n");
866 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);
874 if (ACPI_FAILURE(status))
875 goto error;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300878 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300879 if (!ret) {
880 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300881 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300882 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300883 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300884 kfree(boot_ec);
885 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 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);