blob: e9a04052084b47916df3632441ef72ffc15ff843 [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);
Alexey Starikovskiy30c08572007-09-26 19:43:22 +0400428 list_add(&handler->node, &ec->list);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400429 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);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400443 }
444 }
445 mutex_unlock(&ec->lock);
446}
447
448EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Len Brown50526df2005-08-11 17:32:05 -0400450static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300452 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400453 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400454 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400455
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300456 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300457 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400458 mutex_lock(&ec->lock);
459 list_for_each_entry(handler, &ec->list, node) {
460 if (value == handler->query_bit) {
461 /* have custom handler for this bit */
462 memcpy(&copy, handler, sizeof(copy));
463 mutex_unlock(&ec->lock);
464 if (copy.func) {
465 copy.func(copy.data);
466 } else if (copy.handle) {
467 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
468 }
469 return;
470 }
471 }
472 mutex_unlock(&ec->lock);
Luming Yu45bea152005-07-23 04:08:00 -0400473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Len Brown50526df2005-08-11 17:32:05 -0400475static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Len Brown50526df2005-08-11 17:32:05 -0400477 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400478 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300479 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200480
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500481 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300482
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400483 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300484 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500485 }
486
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300487 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300488 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
489 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300490 status =
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400491 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400492 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300493
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500494 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400495 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/* --------------------------------------------------------------------------
499 Address Space Management
500 -------------------------------------------------------------------------- */
501
502static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400503acpi_ec_space_setup(acpi_handle region_handle,
504 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 /*
507 * The EC object is in the handler context and is needed
508 * when calling the acpi_ec_space_handler.
509 */
Len Brown50526df2005-08-11 17:32:05 -0400510 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
511 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 return AE_OK;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400517acpi_ec_space_handler(u32 function, acpi_physical_address address,
518 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400519 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300521 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400522 int result = 0, i = 0;
523 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400526 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400528 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400530
531 if (bits != 8 && acpi_strict)
532 return AE_BAD_PARAMETER;
533
534 while (bits - i > 0) {
535 if (function == ACPI_READ) {
536 result = acpi_ec_read(ec, address, &temp);
537 (*value) |= ((acpi_integer)temp) << i;
538 } else {
539 temp = 0xff & ((*value) >> i);
540 result = acpi_ec_write(ec, address, temp);
541 }
542 i += 8;
543 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 switch (result) {
547 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400548 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/* --------------------------------------------------------------------------
562 FS Interface (/proc)
563 -------------------------------------------------------------------------- */
564
Len Brown50526df2005-08-11 17:32:05 -0400565static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Len Brown50526df2005-08-11 17:32:05 -0400567static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300569 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!ec)
572 goto end;
573
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300574 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
575 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
576 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
577 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400578 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400579 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
584{
585 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
586}
587
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400588static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400589 .open = acpi_ec_info_open_fs,
590 .read = seq_read,
591 .llseek = seq_lseek,
592 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .owner = THIS_MODULE,
594};
595
Len Brown50526df2005-08-11 17:32:05 -0400596static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Len Brown50526df2005-08-11 17:32:05 -0400598 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!acpi_device_dir(device)) {
601 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400602 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400608 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400610 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 else {
612 entry->proc_fops = &acpi_ec_info_ops;
613 entry->data = acpi_driver_data(device);
614 entry->owner = THIS_MODULE;
615 }
616
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Len Brown50526df2005-08-11 17:32:05 -0400620static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (acpi_device_dir(device)) {
624 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
625 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
626 acpi_device_dir(device) = NULL;
627 }
628
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/* --------------------------------------------------------------------------
633 Driver Interface
634 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300635static acpi_status
636ec_parse_io_ports(struct acpi_resource *resource, void *context);
637
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300638static struct acpi_ec *make_acpi_ec(void)
639{
640 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
641 if (!ec)
642 return NULL;
643
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300644 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300645 atomic_set(&ec->event_count, 1);
646 mutex_init(&ec->lock);
647 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400648 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300649
650 return ec;
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400653static acpi_status
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400654acpi_ec_register_query_methods(acpi_handle handle, u32 level,
655 void *context, void **return_value)
656{
657 struct acpi_namespace_node *node = handle;
658 struct acpi_ec *ec = context;
659 int value = 0;
660 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
661 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
662 }
663 return AE_OK;
664}
665
666static acpi_status
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400667ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400668{
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400669 acpi_status status;
670
671 struct acpi_ec *ec = context;
672 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
673 ec_parse_io_ports, ec);
674 if (ACPI_FAILURE(status))
675 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400676
677 /* Get GPE bit assignment (EC events). */
678 /* TODO: Add support for _GPE returning a package */
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400679 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
680 if (ACPI_FAILURE(status))
681 return status;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400682
Alexey Starikovskiyc019b192007-08-14 01:03:42 -0400683 /* Find and register all query methods */
684 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
685 acpi_ec_register_query_methods, ec, NULL);
686
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400687 /* Use the global lock for all EC transactions? */
688 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
689
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400690 ec->handle = handle;
691
Meelis Roos0a5245092007-07-26 12:56:55 +0300692 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400693 ec->gpe, ec->command_addr, ec->data_addr);
694
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400695 return AE_CTRL_TERMINATE;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400696}
697
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400698static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400700 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400703 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300705 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
706 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
707
708 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400710 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400712 if (ec_parse_device(device->handle, 0, ec, NULL) !=
713 AE_CTRL_TERMINATE) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300714 kfree(ec);
715 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400716 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300717
718 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300719 if (boot_ec) {
720 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400721 /* We might have incorrect info for GL at boot time */
722 mutex_lock(&boot_ec->lock);
723 boot_ec->global_lock = ec->global_lock;
724 /* Copy handlers from new ec into boot ec */
725 list_splice(&ec->list, &boot_ec->list);
726 mutex_unlock(&boot_ec->lock);
727 kfree(ec);
728 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300729 }
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400730 } else
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300731 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300732 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300735 acpi_ec_add_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Len Brown50526df2005-08-11 17:32:05 -0400739static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300741 struct acpi_ec *ec;
Adrian Bunk07ddf762007-07-29 17:00:37 +0200742 struct acpi_ec_query_handler *handler, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400748 mutex_lock(&ec->lock);
Adrian Bunk07ddf762007-07-29 17:00:37 +0200749 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400750 list_del(&handler->node);
751 kfree(handler);
752 }
753 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300755 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300756 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300757 first_ec = NULL;
758
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400759 /* Don't touch boot EC */
760 if (boot_ec != ec)
761 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300766ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300768 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300770 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
774 * The first address region returned is the data port, and
775 * the second address region returned is the status/command
776 * port.
777 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300778 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400779 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300780 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400781 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300782 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return AE_OK;
786}
787
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300788static int ec_install_handlers(struct acpi_ec *ec)
789{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300790 acpi_status status;
791 status = acpi_install_gpe_handler(NULL, ec->gpe,
792 ACPI_GPE_EDGE_TRIGGERED,
793 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300794 if (ACPI_FAILURE(status))
795 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300796
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300797 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
798 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
799
800 status = acpi_install_address_space_handler(ec->handle,
801 ACPI_ADR_SPACE_EC,
802 &acpi_ec_space_handler,
803 &acpi_ec_space_setup, ec);
804 if (ACPI_FAILURE(status)) {
805 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
806 return -ENODEV;
807 }
808
809 return 0;
810}
811
Len Brown50526df2005-08-11 17:32:05 -0400812static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300814 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400815 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 ec = acpi_driver_data(device);
821
822 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400825 /* Boot EC is already working */
826 if (ec != boot_ec)
827 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300828
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400829 /* EC is fully operational, allow queries */
830 atomic_set(&ec->query_pending, 0);
831
832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Len Brown50526df2005-08-11 17:32:05 -0400835static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Alexey Starikovskiyf9319f92007-08-24 08:10:11 +0400837 acpi_status status;
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 Starikovskiyf9319f92007-08-24 08:10:11 +0400846
847 /* Don't touch boot EC */
848 if (ec == boot_ec)
849 return 0;
850
851 status = acpi_remove_address_space_handler(ec->handle,
852 ACPI_ADR_SPACE_EC,
853 &acpi_ec_space_handler);
854 if (ACPI_FAILURE(status))
855 return -ENODEV;
856
857 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
858 if (ACPI_FAILURE(status))
859 return -ENODEV;
860
Patrick Mocheld550d982006-06-27 00:41:40 -0400861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Len Brown50526df2005-08-11 17:32:05 -0400864int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Len Brown50526df2005-08-11 17:32:05 -0400866 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300867 acpi_status status;
868 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300870 boot_ec = make_acpi_ec();
871 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300872 return -ENOMEM;
873 /*
874 * Generate a boot ec context
875 */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300876 status = acpi_get_table(ACPI_SIG_ECDT, 1,
877 (struct acpi_table_header **)&ecdt_ptr);
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400878 if (ACPI_SUCCESS(status)) {
879 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n\n");
880 boot_ec->command_addr = ecdt_ptr->control.address;
881 boot_ec->data_addr = ecdt_ptr->data.address;
882 boot_ec->gpe = ecdt_ptr->gpe;
883 boot_ec->handle = ACPI_ROOT_OBJECT;
884 } else {
885 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
886 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
887 boot_ec, NULL);
Alexey Starikovskiy2d8348b2007-08-31 09:05:26 +0400888 /* Check that acpi_get_devices actually find something */
889 if (ACPI_FAILURE(status) || !boot_ec->handle)
Alexey Starikovskiycd8c93a2007-08-03 17:52:48 -0400890 goto error;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300893 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300894 if (!ret) {
895 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300896 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300897 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300898 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300899 kfree(boot_ec);
900 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 return -ENODEV;
903}
904
Len Brown50526df2005-08-11 17:32:05 -0400905static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Len Brown50526df2005-08-11 17:32:05 -0400907 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
913 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400914 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /* Now register the driver for the EC */
917 result = acpi_bus_register_driver(&acpi_ec_driver);
918 if (result < 0) {
919 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400920 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926subsys_initcall(acpi_ec_init);
927
928/* EC driver currently not unloadable */
929#if 0
Len Brown50526df2005-08-11 17:32:05 -0400930static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 acpi_bus_unregister_driver(&acpi_ec_driver);
934
935 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
936
Patrick Mocheld550d982006-06-27 00:41:40 -0400937 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
Len Brown50526df2005-08-11 17:32:05 -0400939#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Len Brown02b28a32005-12-05 16:33:04 -0500941static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400942{
Len Brown02b28a32005-12-05 16:33:04 -0500943 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400944
Len Brown02b28a32005-12-05 16:33:04 -0500945 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400946 return 0;
947
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300948 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
949
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500950 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400951
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800952 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400953}
Len Brown50526df2005-08-11 17:32:05 -0400954
Len Brown53f11d42005-12-05 16:46:36 -0500955__setup("ec_intr=", acpi_ec_set_intr_mode);