blob: 10e851021ecabcc01e2c86354401eeda2b3cbd47 [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"
44#define ACPI_EC_HID "PNP0C09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_EC_DEVICE_NAME "Embedded Controller"
46#define ACPI_EC_FILE_INFO "info"
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040047
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030048#undef PREFIX
49#define PREFIX "ACPI: EC: "
Alexey Starikovskiy43509332007-05-29 16:42:57 +040050
Denis M. Sadykov703959d2006-09-26 19:50:33 +040051/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
53#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050054#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Alexey Starikovskiy43509332007-05-29 16:42:57 +040056
Denis M. Sadykov703959d2006-09-26 19:50:33 +040057/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030058enum ec_command {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030059 ACPI_EC_COMMAND_READ = 0x80,
60 ACPI_EC_COMMAND_WRITE = 0x81,
61 ACPI_EC_BURST_ENABLE = 0x82,
62 ACPI_EC_BURST_DISABLE = 0x83,
63 ACPI_EC_COMMAND_QUERY = 0x84,
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030064};
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040065
Denis M. Sadykov703959d2006-09-26 19:50:33 +040066/* EC events */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030067enum ec_event {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040068 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030069 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040070};
71
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030072#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040073#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040074
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030075static enum ec_mode {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +030076 EC_INTR = 1, /* Output buffer full */
77 EC_POLL, /* Input buffer empty */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030078} acpi_ec_mode = EC_INTR;
Denis M. Sadykov703959d2006-09-26 19:50:33 +040079
Len Brown50526df2005-08-11 17:32:05 -040080static int acpi_ec_remove(struct acpi_device *device, int type);
81static int acpi_ec_start(struct acpi_device *device);
82static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040083static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static struct acpi_driver acpi_ec_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050086 .name = "ec",
Len Brown50526df2005-08-11 17:32:05 -040087 .class = ACPI_EC_CLASS,
88 .ids = ACPI_EC_HID,
89 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040090 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040091 .remove = acpi_ec_remove,
92 .start = acpi_ec_start,
93 .stop = acpi_ec_stop,
94 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040096
97/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Alexey Starikovskiyd0338792007-03-07 22:28:00 +030098/* External interfaces use first EC only, so remember */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +040099typedef int (*acpi_ec_query_func) (void *data);
100
101struct acpi_ec_query_handler {
102 struct list_head node;
103 acpi_ec_query_func func;
104 acpi_handle handle;
105 void *data;
106 u8 query_bit;
107};
108
Adrian Bunka854e082006-12-19 12:56:12 -0800109static struct acpi_ec {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400110 acpi_handle handle;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300111 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400112 unsigned long command_addr;
113 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400114 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300115 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300116 atomic_t query_pending;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500117 atomic_t event_count;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118 wait_queue_head_t wait;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400119 struct list_head list;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300120} *boot_ec, *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* --------------------------------------------------------------------------
123 Transaction Management
124 -------------------------------------------------------------------------- */
125
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400126static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400128 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400131static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400132{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400133 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400134}
135
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400136static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400137{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400138 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400139}
140
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400141static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400142{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400143 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400144}
145
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500146static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
147 unsigned old_count)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400148{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300149 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500150 if (old_count == atomic_read(&ec->event_count))
151 return 0;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300152 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400153 if (status & ACPI_EC_FLAG_OBF)
154 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300155 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400156 if (!(status & ACPI_EC_FLAG_IBF))
157 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400158 }
159
160 return 0;
161}
162
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200163static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
164 unsigned count, int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400165{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200166 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300167 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
168 while (time_before(jiffies, delay)) {
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500169 if (acpi_ec_check_status(ec, event, 0))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400170 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400171 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300172 } else {
173 if (wait_event_timeout(ec->wait,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500174 acpi_ec_check_status(ec, event, count),
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300175 msecs_to_jiffies(ACPI_EC_DELAY)) ||
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500176 acpi_ec_check_status(ec, event, 0)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400177 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300178 } else {
179 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
180 " status = %d, expect_event = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300181 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400182 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300183 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400184
Patrick Mocheld550d982006-06-27 00:41:40 -0400185 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500186}
187
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400188static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300189 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200190 u8 * rdata, unsigned rdata_len,
191 int force_poll)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400192{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300193 int result = 0;
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500194 unsigned count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400195 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400196
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300197 for (; wdata_len > 0; --wdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200198 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300199 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300200 printk(KERN_ERR PREFIX
201 "write_cmd timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300202 goto end;
203 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500204 count = atomic_read(&ec->event_count);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400205 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400206 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400207
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300208 if (!rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200209 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300210 if (result) {
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300211 printk(KERN_ERR PREFIX
212 "finish-write timeout, command = %d\n", command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300213 goto end;
214 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300215 } else if (command == ACPI_EC_COMMAND_QUERY) {
216 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400217 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400218
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300219 for (; rdata_len > 0; --rdata_len) {
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200220 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300221 if (result) {
222 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300223 command);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300224 goto end;
225 }
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500226 count = atomic_read(&ec->event_count);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400227 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400228 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300229 end:
230 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400231}
232
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400233static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300234 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200235 u8 * rdata, unsigned rdata_len,
236 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400237{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400238 int status;
Len Brown50526df2005-08-11 17:32:05 -0400239 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400241 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300244 if (rdata)
245 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300247 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300250 if (ACPI_FAILURE(status)) {
251 mutex_unlock(&ec->lock);
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return -ENODEV;
Alexey Starikovskiyc24e9122007-02-15 23:16:18 +0300253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500255
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300256 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300257 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300258
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200259 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
Luming Yu716e0842005-08-10 01:40:00 -0400260 if (status) {
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400261 printk(KERN_ERR PREFIX
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300262 "input buffer is not empty, aborting transaction\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500263 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300266 status = acpi_ec_transaction_unlocked(ec, command,
267 wdata, wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200268 rdata, rdata_len,
269 force_poll);
Len Brown50526df2005-08-11 17:32:05 -0400270
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300271 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400273 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300275 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300280/*
281 * Note: samsung nv5000 doesn't work with ec burst mode.
282 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
283 */
284int acpi_ec_burst_enable(struct acpi_ec *ec)
285{
286 u8 d;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200287 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300288}
289
290int acpi_ec_burst_disable(struct acpi_ec *ec)
291{
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200292 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300293}
294
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300295static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400296{
297 int result;
298 u8 d;
299
300 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200301 &address, 1, &d, 1, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400302 *data = d;
303 return result;
304}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400305
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400306static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
307{
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300308 u8 wdata[2] = { address, data };
309 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200310 wdata, 2, NULL, 0, 0);
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * Externally callable EC access functions. For now, assume 1 EC only
315 */
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300316int ec_burst_enable(void)
317{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300318 if (!first_ec)
319 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300320 return acpi_ec_burst_enable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300321}
322
323EXPORT_SYMBOL(ec_burst_enable);
324
325int ec_burst_disable(void)
326{
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300327 if (!first_ec)
328 return -ENODEV;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300329 return acpi_ec_burst_disable(first_ec);
Alexey Starikovskiyc45aac42007-03-07 22:28:00 +0300330}
331
332EXPORT_SYMBOL(ec_burst_disable);
333
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300334int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400337 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!first_ec)
340 return -ENODEV;
341
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300342 err = acpi_ec_read(first_ec, addr, &temp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!err) {
345 *val = temp_data;
346 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400347 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return err;
349}
Len Brown50526df2005-08-11 17:32:05 -0400350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351EXPORT_SYMBOL(ec_read);
352
Len Brown50526df2005-08-11 17:32:05 -0400353int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int err;
356
357 if (!first_ec)
358 return -ENODEV;
359
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300360 err = acpi_ec_write(first_ec, addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 return err;
363}
Len Brown50526df2005-08-11 17:32:05 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365EXPORT_SYMBOL(ec_write);
366
Randy Dunlap616362d2006-10-27 01:47:34 -0400367int ec_transaction(u8 command,
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500368 const u8 * wdata, unsigned wdata_len,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200369 u8 * rdata, unsigned rdata_len,
370 int force_poll)
Luming Yu45bea152005-07-23 04:08:00 -0400371{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400372 if (!first_ec)
373 return -ENODEV;
374
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300375 return acpi_ec_transaction(first_ec, command, wdata,
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200376 wdata_len, rdata, rdata_len,
377 force_poll);
Luming Yu45bea152005-07-23 04:08:00 -0400378}
Luming Yu45bea152005-07-23 04:08:00 -0400379
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400380EXPORT_SYMBOL(ec_transaction);
381
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300382static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400383{
384 int result;
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300385 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400386
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300387 if (!ec || !data)
388 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400389
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300390 /*
391 * Query the EC to find out which _Qxx method we need to evaluate.
392 * Note that successful completion of the query causes the ACPI_EC_SCI
393 * bit to be cleared (and thus clearing the interrupt source).
394 */
Luming Yu45bea152005-07-23 04:08:00 -0400395
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200396 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300397 if (result)
398 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400399
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300400 if (!d)
401 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400402
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300403 *data = d;
404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/* --------------------------------------------------------------------------
408 Event Management
409 -------------------------------------------------------------------------- */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400410int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
411 acpi_handle handle, acpi_ec_query_func func,
412 void *data)
413{
414 struct acpi_ec_query_handler *handler =
415 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
416 if (!handler)
417 return -ENOMEM;
418
419 handler->query_bit = query_bit;
420 handler->handle = handle;
421 handler->func = func;
422 handler->data = data;
423 mutex_lock(&ec->lock);
424 list_add_tail(&handler->node, &ec->list);
425 mutex_unlock(&ec->lock);
426 return 0;
427}
428
429EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
430
431void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
432{
433 struct acpi_ec_query_handler *handler;
434 mutex_lock(&ec->lock);
435 list_for_each_entry(handler, &ec->list, node) {
436 if (query_bit == handler->query_bit) {
437 list_del(&handler->node);
438 kfree(handler);
439 break;
440 }
441 }
442 mutex_unlock(&ec->lock);
443}
444
445EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Len Brown50526df2005-08-11 17:32:05 -0400447static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300449 struct acpi_ec *ec = ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400450 u8 value = 0;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400451 struct acpi_ec_query_handler *handler, copy;
Luming Yu45bea152005-07-23 04:08:00 -0400452
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300453 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300454 return;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400455 mutex_lock(&ec->lock);
456 list_for_each_entry(handler, &ec->list, node) {
457 if (value == handler->query_bit) {
458 /* have custom handler for this bit */
459 memcpy(&copy, handler, sizeof(copy));
460 mutex_unlock(&ec->lock);
461 if (copy.func) {
462 copy.func(copy.data);
463 } else if (copy.handle) {
464 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
465 }
466 return;
467 }
468 }
469 mutex_unlock(&ec->lock);
470 printk(KERN_ERR PREFIX "Handler for query 0x%x is not found!\n", value);
Luming Yu45bea152005-07-23 04:08:00 -0400471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Len Brown50526df2005-08-11 17:32:05 -0400473static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Len Brown50526df2005-08-11 17:32:05 -0400475 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400476 u8 value;
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300477 struct acpi_ec *ec = data;
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200478
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500479 atomic_inc(&ec->event_count);
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300480
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400481 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300482 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500483 }
484
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300485 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300486 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
487 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300488 status =
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400489 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400490 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300491
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500492 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400493 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/* --------------------------------------------------------------------------
497 Address Space Management
498 -------------------------------------------------------------------------- */
499
500static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400501acpi_ec_space_setup(acpi_handle region_handle,
502 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 /*
505 * The EC object is in the handler context and is needed
506 * when calling the acpi_ec_space_handler.
507 */
Len Brown50526df2005-08-11 17:32:05 -0400508 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
509 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 return AE_OK;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static acpi_status
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400515acpi_ec_space_handler(u32 function, acpi_physical_address address,
516 u32 bits, acpi_integer *value,
Len Brown50526df2005-08-11 17:32:05 -0400517 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300519 struct acpi_ec *ec = handler_context;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400520 int result = 0, i = 0;
521 u8 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400524 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400526 if (function != ACPI_READ && function != ACPI_WRITE)
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return AE_BAD_PARAMETER;
Alexey Starikovskiy5b7734b2007-05-29 16:42:52 +0400528
529 if (bits != 8 && acpi_strict)
530 return AE_BAD_PARAMETER;
531
532 while (bits - i > 0) {
533 if (function == ACPI_READ) {
534 result = acpi_ec_read(ec, address, &temp);
535 (*value) |= ((acpi_integer)temp) << i;
536 } else {
537 temp = 0xff & ((*value) >> i);
538 result = acpi_ec_write(ec, address, temp);
539 }
540 i += 8;
541 ++address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (result) {
545 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400546 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/* --------------------------------------------------------------------------
560 FS Interface (/proc)
561 -------------------------------------------------------------------------- */
562
Len Brown50526df2005-08-11 17:32:05 -0400563static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Len Brown50526df2005-08-11 17:32:05 -0400565static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300567 struct acpi_ec *ec = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!ec)
570 goto end;
571
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300572 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
573 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
574 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
575 seq_printf(seq, "use global lock:\t%s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400576 ec->global_lock ? "yes" : "no");
Len Brown50526df2005-08-11 17:32:05 -0400577 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
582{
583 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
584}
585
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400586static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400587 .open = acpi_ec_info_open_fs,
588 .read = seq_read,
589 .llseek = seq_lseek,
590 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .owner = THIS_MODULE,
592};
593
Len Brown50526df2005-08-11 17:32:05 -0400594static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Len Brown50526df2005-08-11 17:32:05 -0400596 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!acpi_device_dir(device)) {
599 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400600 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400606 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else {
610 entry->proc_fops = &acpi_ec_info_ops;
611 entry->data = acpi_driver_data(device);
612 entry->owner = THIS_MODULE;
613 }
614
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Len Brown50526df2005-08-11 17:32:05 -0400618static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (acpi_device_dir(device)) {
622 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
623 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
624 acpi_device_dir(device) = NULL;
625 }
626
Patrick Mocheld550d982006-06-27 00:41:40 -0400627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* --------------------------------------------------------------------------
631 Driver Interface
632 -------------------------------------------------------------------------- */
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300633static acpi_status
634ec_parse_io_ports(struct acpi_resource *resource, void *context);
635
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300636static struct acpi_ec *make_acpi_ec(void)
637{
638 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
639 if (!ec)
640 return NULL;
641
Alexey Starikovskiy9fd9f8e2007-03-07 22:28:00 +0300642 atomic_set(&ec->query_pending, 1);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300643 atomic_set(&ec->event_count, 1);
644 mutex_init(&ec->lock);
645 init_waitqueue_head(&ec->wait);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400646 INIT_LIST_HEAD(&ec->list);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300647
648 return ec;
649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400651static acpi_status
652acpi_ec_register_query_methods(acpi_handle handle, u32 level,
653 void *context, void **return_value)
654{
655 struct acpi_namespace_node *node = handle;
656 struct acpi_ec *ec = context;
657 int value = 0;
658 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
659 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
660 }
661 return AE_OK;
662}
663
664static int ec_parse_device(struct acpi_ec *ec, acpi_handle handle)
665{
666 if (ACPI_FAILURE(acpi_walk_resources(handle, METHOD_NAME__CRS,
667 ec_parse_io_ports, ec)))
668 return -EINVAL;
669
670 /* Get GPE bit assignment (EC events). */
671 /* TODO: Add support for _GPE returning a package */
672 if (ACPI_FAILURE(acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe)))
673 return -EINVAL;
674
675 /* Use the global lock for all EC transactions? */
676 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
677
678 /* Find and register all query methods */
679 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
680 acpi_ec_register_query_methods, ec, NULL);
681
682 ec->handle = handle;
683
684 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
685 ec->gpe, ec->command_addr, ec->data_addr);
686
687 return 0;
688}
689
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400690static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400692 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400695 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300697 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
698 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
699
700 ec = make_acpi_ec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400704 if (ec_parse_device(ec, device->handle)) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300705 kfree(ec);
706 return -EINVAL;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400707 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300708
709 /* Check if we found the boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300710 if (boot_ec) {
711 if (boot_ec->gpe == ec->gpe) {
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300712 /* We might have incorrect info for GL at boot time */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300713 mutex_lock(&boot_ec->lock);
714 boot_ec->global_lock = ec->global_lock;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400715 /* Copy handlers from new ec into boot ec */
716 list_splice(&ec->list, &boot_ec->list);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300717 mutex_unlock(&boot_ec->lock);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300718 kfree(ec);
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300719 ec = boot_ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300720 }
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300721 } else
722 first_ec = ec;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300723 ec->handle = device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 acpi_driver_data(device) = ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300726 acpi_ec_add_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Len Brown50526df2005-08-11 17:32:05 -0400730static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300732 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400733 struct acpi_ec_query_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 ec = acpi_driver_data(device);
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400739 mutex_lock(&ec->lock);
740 list_for_each_entry(handler, &ec->list, node) {
741 list_del(&handler->node);
742 kfree(handler);
743 }
744 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 acpi_ec_remove_fs(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300746 acpi_driver_data(device) = NULL;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300747 if (ec == first_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300748 first_ec = NULL;
749
750 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300751 if (boot_ec != ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300752 kfree(ec);
Patrick Mocheld550d982006-06-27 00:41:40 -0400753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static acpi_status
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300757ec_parse_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Alexey Starikovskiy3d02b902007-03-07 22:28:00 +0300759 struct acpi_ec *ec = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300761 if (resource->type != ACPI_RESOURCE_TYPE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
765 * The first address region returned is the data port, and
766 * the second address region returned is the status/command
767 * port.
768 */
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300769 if (ec->data_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400770 ec->data_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300771 else if (ec->command_addr == 0)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400772 ec->command_addr = resource->data.io.minimum;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300773 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return AE_CTRL_TERMINATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return AE_OK;
777}
778
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300779static int ec_install_handlers(struct acpi_ec *ec)
780{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300781 acpi_status status;
782 status = acpi_install_gpe_handler(NULL, ec->gpe,
783 ACPI_GPE_EDGE_TRIGGERED,
784 &acpi_ec_gpe_handler, ec);
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300785 if (ACPI_FAILURE(status))
786 return -ENODEV;
Alexey Starikovskiy01f22462007-03-07 22:28:00 +0300787
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300788 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
789 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
790
791 status = acpi_install_address_space_handler(ec->handle,
792 ACPI_ADR_SPACE_EC,
793 &acpi_ec_space_handler,
794 &acpi_ec_space_setup, ec);
795 if (ACPI_FAILURE(status)) {
796 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
797 return -ENODEV;
798 }
799
800 return 0;
801}
802
Len Brown50526df2005-08-11 17:32:05 -0400803static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300805 struct acpi_ec *ec;
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400806 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 ec = acpi_driver_data(device);
812
813 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400814 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300816 /* Boot EC is already working */
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400817 if (ec != boot_ec)
818 ret = ec_install_handlers(ec);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300819
Alexey Starikovskiy837012e2007-05-29 16:43:02 +0400820 /* EC is fully operational, allow queries */
821 atomic_set(&ec->query_pending, 0);
822
823 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Len Brown50526df2005-08-11 17:32:05 -0400826static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300828 acpi_status status;
829 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 ec = acpi_driver_data(device);
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300835 if (!ec)
836 return -EINVAL;
837
838 /* Don't touch boot EC */
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300839 if (ec == boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400842 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400843 ACPI_ADR_SPACE_EC,
844 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400846 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Alexey Starikovskiy6ccedb12006-12-07 18:42:17 +0300848 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400850 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Len Brown50526df2005-08-11 17:32:05 -0400855int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Len Brown50526df2005-08-11 17:32:05 -0400857 int ret;
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300858 acpi_status status;
859 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300861 boot_ec = make_acpi_ec();
862 if (!boot_ec)
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300863 return -ENOMEM;
864 /*
865 * Generate a boot ec context
866 */
867
868 status = acpi_get_table(ACPI_SIG_ECDT, 1,
869 (struct acpi_table_header **)&ecdt_ptr);
870 if (ACPI_FAILURE(status))
871 goto error;
872
Alexey Starikovskiy43509332007-05-29 16:42:57 +0400873 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300874
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300875 boot_ec->command_addr = ecdt_ptr->control.address;
876 boot_ec->data_addr = ecdt_ptr->data.address;
877 boot_ec->gpe = ecdt_ptr->gpe;
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300878 boot_ec->handle = ACPI_ROOT_OBJECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300880 ret = ec_install_handlers(boot_ec);
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300881 if (!ret) {
882 first_ec = boot_ec;
Alexey Starikovskiye8284322007-03-07 22:28:00 +0300883 return 0;
Alexey Starikovskiyd0338792007-03-07 22:28:00 +0300884 }
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300885 error:
Alexey Starikovskiyd66d9692007-03-07 22:28:00 +0300886 kfree(boot_ec);
887 boot_ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return -ENODEV;
890}
891
Len Brown50526df2005-08-11 17:32:05 -0400892static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Len Brown50526df2005-08-11 17:32:05 -0400894 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
900 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400901 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* Now register the driver for the EC */
904 result = acpi_bus_register_driver(&acpi_ec_driver);
905 if (result < 0) {
906 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Patrick Mocheld550d982006-06-27 00:41:40 -0400910 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913subsys_initcall(acpi_ec_init);
914
915/* EC driver currently not unloadable */
916#if 0
Len Brown50526df2005-08-11 17:32:05 -0400917static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 acpi_bus_unregister_driver(&acpi_ec_driver);
921
922 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
923
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
Len Brown50526df2005-08-11 17:32:05 -0400926#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Len Brown02b28a32005-12-05 16:33:04 -0500928static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -0400929{
Len Brown02b28a32005-12-05 16:33:04 -0500930 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -0400931
Len Brown02b28a32005-12-05 16:33:04 -0500932 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -0400933 return 0;
934
Alexey Starikovskiyc0900c32007-03-07 22:28:00 +0300935 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
936
Alexey Starikovskiy9e197212007-03-07 18:29:35 -0500937 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400938
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800939 return 1;
Luming Yu45bea152005-07-23 04:08:00 -0400940}
Len Brown50526df2005-08-11 17:32:05 -0400941
Len Brown53f11d42005-12-05 16:46:36 -0500942__setup("ec_intr=", acpi_ec_set_intr_mode);