blob: 3f7935ab0cf53c0d6ca21b89096ba3c727e0e7b6 [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 Brownc2b67052007-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
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400699static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400701 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
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
709 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400711 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400713 if (ec_parse_device(device->handle, 0, ec, NULL) !=
714 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300715 kfree(ec);
716 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400717 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300718
719 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300720 if (boot_ec) {
721 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400722 /* We might have incorrect info for GL at boot time */
723 mutex_lock(&boot_ec->lock);
724 boot_ec->global_lock = ec->global_lock;
725 /* Copy handlers from new ec into boot ec */
726 list_splice(&ec->list, &boot_ec->list);
727 mutex_unlock(&boot_ec->lock);
728 kfree(ec);
729 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300730 }
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400731 } else
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300732 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300733 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300736 acpi_ec_add_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Len Brown50526df2005-08-11 17:32:05 -0400740static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300742 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200743 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400746 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400749 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200750 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400751 list_del(&handler->node);
752 kfree(handler);
753 }
754 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300756 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300757 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300758 first_ec = NULL;
759
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400760 /* Don't touch boot EC */
761 if (boot_ec != ec)
762 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300767ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300769 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300771 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /*
775 * The first address region returned is the data port, and
776 * the second address region returned is the status/command
777 * port.
778 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300779 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400780 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300781 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400782 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300783 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return AE_OK;
787}
788
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300789static int ec_install_handlers(struct acpi_ec *ec)
790{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300791 acpi_status status;
792 status = acpi_install_gpe_handler(NULL, ec->gpe,
793 ACPI_GPE_EDGE_TRIGGERED,
794 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300795 if (ACPI_FAILURE(status))
796 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300797
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300798 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
799 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
800
801 status = acpi_install_address_space_handler(ec->handle,
802 ACPI_ADR_SPACE_EC,
803 &acpi_ec_space_handler,
804 &acpi_ec_space_setup, ec);
805 if (ACPI_FAILURE(status)) {
806 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
807 return -ENODEV;
808 }
809
810 return 0;
811}
812
Len Brown50526df2005-08-11 17:32:05 -0400813static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300815 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400816 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 ec = acpi_driver_data(device);
822
823 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400826 /* Boot EC is already working */
827 if (ec != boot_ec)
828 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 Starikovskiyf9319f92007-08-24 08:10:11 +0400838 acpi_status status;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300839 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400842 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300845 if (!ec)
846 return -EINVAL;
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400847
848 /* Don't touch boot EC */
849 if (ec == boot_ec)
850 return 0;
851
852 status = acpi_remove_address_space_handler(ec->handle,
853 ACPI_ADR_SPACE_EC,
854 &acpi_ec_space_handler);
855 if (ACPI_FAILURE(status))
856 return -ENODEV;
857
858 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
859 if (ACPI_FAILURE(status))
860 return -ENODEV;
861
Patrick Mocheld550d982006-06-27 00:41:40 -0400862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Len Brown50526df2005-08-11 17:32:05 -0400865int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Len Brown50526df2005-08-11 17:32:05 -0400867 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300868 acpi_status status;
869 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300871 boot_ec = make_acpi_ec();
872 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300873 return -ENOMEM;
874 /*
875 * Generate a boot ec context
876 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300877 status = acpi_get_table(ACPI_SIG_ECDT, 1,
878 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400879 if (ACPI_SUCCESS(status)) {
880 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n\n");
881 boot_ec->command_addr = ecdt_ptr->control.address;
882 boot_ec->data_addr = ecdt_ptr->data.address;
883 boot_ec->gpe = ecdt_ptr->gpe;
884 boot_ec->handle = ACPI_ROOT_OBJECT;
885 } else {
886 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
887 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
888 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400889 /* Check that acpi_get_devices actually find something */
890 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400891 goto error;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300894 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300895 if (!ret) {
896 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300897 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300898 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300899 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300900 kfree(boot_ec);
901 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return -ENODEV;
904}
905
Len Brown50526df2005-08-11 17:32:05 -0400906static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Len Brown50526df2005-08-11 17:32:05 -0400908 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
914 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400915 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* Now register the driver for the EC */
918 result = acpi_bus_register_driver(&acpi_ec_driver);
919 if (result < 0) {
920 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927subsys_initcall(acpi_ec_init);
928
929/* EC driver currently not unloadable */
930#if 0
Len Brown50526df2005-08-11 17:32:05 -0400931static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 acpi_bus_unregister_driver(&acpi_ec_driver);
935
936 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
937
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
Len Brown50526df2005-08-11 17:32:05 -0400940#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Len Brown02b28a32005-12-05 16:33:04 -0500942static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400943{
Len Brown02b28a32005-12-05 16:33:04 -0500944 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400945
Len Brown02b28a32005-12-05 16:33:04 -0500946 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400947 return 0;
948
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300949 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
950
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500951 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400952
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800953 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400954}
Len Brown50526df2005-08-11 17:32:05 -0400955
Len Brown53f11d42005-12-05 16:46:36 -0500956__setup("ec_intr=", acpi_ec_set_intr_mode);