blob: e05bb148754ca52e65d6e88d411b291710ed05b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38#include <acpi/actypes.h>
39
40#define _COMPONENT ACPI_EC_COMPONENT
Len Brown50526df2005-08-11 17:32:05 -040041ACPI_MODULE_NAME("acpi_ec")
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_EC_COMPONENT 0x00100000
43#define ACPI_EC_CLASS "embedded_controller"
44#define ACPI_EC_HID "PNP0C09"
45#define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
46#define ACPI_EC_DEVICE_NAME "Embedded Controller"
47#define ACPI_EC_FILE_INFO "info"
Denis M. Sadykov703959d2006-09-26 19:50:33 +040048
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +030049#undef PREFIX
50#define PREFIX "ACPI: EC: "
51
Denis M. Sadykov703959d2006-09-26 19:50:33 +040052/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
54#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050055#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040057
58/* EC commands */
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +030059enum ec_command {
60 ACPI_EC_COMMAND_READ = 0x80,
61 ACPI_EC_COMMAND_WRITE = 0x81,
62 ACPI_EC_BURST_ENABLE = 0x82,
63 ACPI_EC_BURST_DISABLE = 0x83,
64 ACPI_EC_COMMAND_QUERY = 0x84,
65};
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 */
69 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
70};
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 {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040076 EC_INTR = 1, /* Output buffer full */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040077 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 Brown50526df2005-08-11 17:32:05 -040086 .name = ACPI_EC_DRIVER_NAME,
87 .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 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040098struct acpi_ec {
99 acpi_handle handle;
100 unsigned long uid;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300101 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400102 unsigned long command_addr;
103 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400104 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300105 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300106 atomic_t query_pending;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400107 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
108 wait_queue_head_t wait;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400109} *ec_ecdt;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400110
111/* External interfaces use first EC only, so remember */
112static struct acpi_device *first_ec;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* --------------------------------------------------------------------------
115 Transaction Management
116 -------------------------------------------------------------------------- */
117
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400118static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400120 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400123static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400124{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400125 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400126}
127
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400128static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400129{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400130 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400131}
132
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400133static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400134{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400135 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400136}
137
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +0300138static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400139{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300140 u8 status = acpi_ec_read_status(ec);
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300141
142 if (event == ACPI_EC_EVENT_OBF_1) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400143 if (status & ACPI_EC_FLAG_OBF)
144 return 1;
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300145 } else if (event == ACPI_EC_EVENT_IBF_0) {
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400146 if (!(status & ACPI_EC_FLAG_IBF))
147 return 1;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400148 }
149
150 return 0;
151}
152
Alexey Starikovskiy3261ff42006-12-07 18:42:17 +0300153static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event)
Luming Yu45bea152005-07-23 04:08:00 -0400154{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300155 if (acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300156 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
157 while (time_before(jiffies, delay)) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300158 if (acpi_ec_check_status(ec, event))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400159 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400160 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300161 } else {
162 if (wait_event_timeout(ec->wait,
163 acpi_ec_check_status(ec, event),
164 msecs_to_jiffies(ACPI_EC_DELAY)) ||
165 acpi_ec_check_status(ec, event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400166 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300167 } else {
168 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
169 " status = %d, expect_event = %d\n",
170 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400171 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300172 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400173
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500175}
176
Len Brown02b28a32005-12-05 16:33:04 -0500177#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400178/*
179 * Note: samsung nv5000 doesn't work with ec burst mode.
180 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
181 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400182int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500183{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400184 u8 tmp = 0;
185 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500186
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500187
188 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400189 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400190 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Len Brown50526df2005-08-11 17:32:05 -0400191 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400192 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400193 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
194 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
195 tmp = acpi_ec_read_data(ec);
Len Brown50526df2005-08-11 17:32:05 -0400196 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400197 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500198 }
Luming Yu668d74c2005-07-23 00:26:33 -0400199 }
200
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400201 atomic_set(&ec->leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400203 end:
204 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400205 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500206}
207
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400208int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500209{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400210 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500211
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500212
Luming Yu06a2a382005-09-27 00:43:00 -0400213 status = acpi_ec_read_status(ec);
214 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400215 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu06a2a382005-09-27 00:43:00 -0400216 if(status)
217 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400218 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
219 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400220 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400221 atomic_set(&ec->leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400222 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400223 end:
224 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500226}
Len Brown02b28a32005-12-05 16:33:04 -0500227#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400229static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400230 const u8 *wdata, unsigned wdata_len,
231 u8 *rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400232{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300233 int result = 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400234
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400235 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400236
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300237 for (; wdata_len > 0; --wdata_len) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400238 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300239 if (result) {
240 printk(KERN_ERR PREFIX "write_cmd timeout, command = %d\n",
241 command);
242 goto end;
243 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400244 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400245 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400246
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300247 if (!rdata_len) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300249 if (result) {
250 printk(KERN_ERR PREFIX "finish-write timeout, command = %d\n",
251 command);
252 goto end;
253 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300254 } else if (command == ACPI_EC_COMMAND_QUERY) {
255 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400256 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400257
Alexey Starikovskiy78d0af32006-12-07 18:42:17 +0300258 for (; rdata_len > 0; --rdata_len) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400259 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300260 if (result) {
261 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
262 command);
263 goto end;
264 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400265
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400266 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400267 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300268 end:
269 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400270}
271
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400272static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
273 const u8 *wdata, unsigned wdata_len,
274 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400275{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400276 int status;
Len Brown50526df2005-08-11 17:32:05 -0400277 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400279 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400282 if (rdata)
283 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300285 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400286 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
288 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500291
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300292 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300293 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300294
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400295 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu716e0842005-08-10 01:40:00 -0400296 if (status) {
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400297 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500298 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400301 status = acpi_ec_transaction_unlocked(ec, command,
302 wdata, wdata_len,
303 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400304
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400305end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400307 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300309 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400314static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400315{
316 int result;
317 u8 d;
318
319 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
320 &address, 1, &d, 1);
321 *data = d;
322 return result;
323}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400324
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400325static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
326{
327 u8 wdata[2] = { address, data };
328 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
329 wdata, 2, NULL, 0);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * Externally callable EC access functions. For now, assume 1 EC only
334 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400335int ec_read(u8 addr, u8 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400337 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400339 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (!first_ec)
342 return -ENODEV;
343
344 ec = acpi_driver_data(first_ec);
345
346 err = acpi_ec_read(ec, addr, &temp_data);
347
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{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400359 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int err;
361
362 if (!first_ec)
363 return -ENODEV;
364
365 ec = acpi_driver_data(first_ec);
366
367 err = acpi_ec_write(ec, addr, val);
368
369 return err;
370}
Len Brown50526df2005-08-11 17:32:05 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372EXPORT_SYMBOL(ec_write);
373
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400374extern int ec_transaction(u8 command,
375 const u8 *wdata, unsigned wdata_len,
376 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400377{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400378 struct acpi_ec *ec;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400379
380 if (!first_ec)
381 return -ENODEV;
382
383 ec = acpi_driver_data(first_ec);
384
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400385 return acpi_ec_transaction(ec, command, wdata,
386 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400387}
Luming Yu45bea152005-07-23 04:08:00 -0400388
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400389EXPORT_SYMBOL(ec_transaction);
390
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400391static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400392{
393 int result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400394 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400395
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400396 if (!ec || !data)
397 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400398
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400399 /*
400 * Query the EC to find out which _Qxx method we need to evaluate.
401 * Note that successful completion of the query causes the ACPI_EC_SCI
402 * bit to be cleared (and thus clearing the interrupt source).
403 */
Luming Yu45bea152005-07-23 04:08:00 -0400404
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400405 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
406 if (result)
407 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400408
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400409 if (!d)
410 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400411
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400412 *data = d;
413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/* --------------------------------------------------------------------------
417 Event Management
418 -------------------------------------------------------------------------- */
419
Len Brown50526df2005-08-11 17:32:05 -0400420static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400422 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400423 u8 value = 0;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300424 char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400425
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300426 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300427 return;
Luming Yu45bea152005-07-23 04:08:00 -0400428
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400429 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400430
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300431 printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
Luming Yu45bea152005-07-23 04:08:00 -0400432
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400433 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Len Brown50526df2005-08-11 17:32:05 -0400436static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown50526df2005-08-11 17:32:05 -0400438 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400439 u8 value;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400440 struct acpi_ec *ec = (struct acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400443 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300444 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500445 }
446
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300447 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300448 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
449 atomic_set(&ec->query_pending, 1);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400450 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400451 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300452
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500453 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400454 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/* --------------------------------------------------------------------------
458 Address Space Management
459 -------------------------------------------------------------------------- */
460
461static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400462acpi_ec_space_setup(acpi_handle region_handle,
463 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 /*
466 * The EC object is in the handler context and is needed
467 * when calling the acpi_ec_space_handler.
468 */
Len Brown50526df2005-08-11 17:32:05 -0400469 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
470 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return AE_OK;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400476acpi_ec_space_handler(u32 function,
477 acpi_physical_address address,
478 u32 bit_width,
479 acpi_integer * value,
480 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Len Brown50526df2005-08-11 17:32:05 -0400482 int result = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400483 struct acpi_ec *ec = NULL;
Len Brown50526df2005-08-11 17:32:05 -0400484 u64 temp = *value;
485 acpi_integer f_v = 0;
486 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400490 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Luming Yufa9cd542005-03-19 01:54:47 -0500492 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400496 ec = (struct acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Len Brown50526df2005-08-11 17:32:05 -0400498 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 switch (function) {
500 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500501 temp = 0;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400502 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500505 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 default:
508 result = -EINVAL;
509 goto out;
510 break;
511 }
512
513 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500514 if (bit_width) {
515 if (function == ACPI_READ)
516 f_v |= temp << 8 * i;
517 if (function == ACPI_WRITE)
518 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500520 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto next_byte;
522 }
523
Luming Yufa9cd542005-03-19 01:54:47 -0500524 if (function == ACPI_READ) {
525 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *value = f_v;
527 }
528
Len Brown50526df2005-08-11 17:32:05 -0400529 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 switch (result) {
531 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400535 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400538 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400541 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* --------------------------------------------------------------------------
546 FS Interface (/proc)
547 -------------------------------------------------------------------------- */
548
Len Brown50526df2005-08-11 17:32:05 -0400549static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Len Brown50526df2005-08-11 17:32:05 -0400551static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400553 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (!ec)
557 goto end;
558
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300559 seq_printf(seq, "gpe: 0x%02x\n",
560 (u32) ec->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400562 (u32) ec->command_addr,
563 (u32) ec->data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400565 ec->global_lock ? "yes" : "no");
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300566 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Len Brown50526df2005-08-11 17:32:05 -0400568 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
573{
574 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
575}
576
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400577static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400578 .open = acpi_ec_info_open_fs,
579 .read = seq_read,
580 .llseek = seq_lseek,
581 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .owner = THIS_MODULE,
583};
584
Len Brown50526df2005-08-11 17:32:05 -0400585static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Len Brown50526df2005-08-11 17:32:05 -0400587 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (!acpi_device_dir(device)) {
591 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400592 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400594 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400598 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400600 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 else {
602 entry->proc_fops = &acpi_ec_info_ops;
603 entry->data = acpi_driver_data(device);
604 entry->owner = THIS_MODULE;
605 }
606
Patrick Mocheld550d982006-06-27 00:41:40 -0400607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Len Brown50526df2005-08-11 17:32:05 -0400610static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (acpi_device_dir(device)) {
614 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
615 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
616 acpi_device_dir(device) = NULL;
617 }
618
Patrick Mocheld550d982006-06-27 00:41:40 -0400619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/* --------------------------------------------------------------------------
623 Driver Interface
624 -------------------------------------------------------------------------- */
625
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400626static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Len Brown50526df2005-08-11 17:32:05 -0400628 int result = 0;
629 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400630 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400636 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400639 memset(ec, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400641 ec->handle = device->handle;
642 ec->uid = -1;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300643 mutex_init(&ec->lock);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300644 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400645 if (acpi_ec_mode == EC_INTR) {
646 atomic_set(&ec->leaving_burst, 1);
647 init_waitqueue_head(&ec->wait);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
650 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
651 acpi_driver_data(device) = ec;
652
653 /* Use the global lock for all EC transactions? */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400654 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
655 &ec->global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500657 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
658 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
659 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400661 ACPI_ADR_SPACE_EC,
662 &acpi_ec_space_handler);
663
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300664 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400665 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 kfree(ec_ecdt);
668 }
669
670 /* Get GPE bit assignment (EC events). */
671 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400672 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400673 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300674 &ec->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400676 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 result = -ENODEV;
678 goto end;
679 }
680
681 result = acpi_ec_add_fs(device);
682 if (result)
683 goto end;
684
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400685 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Len Brown50526df2005-08-11 17:32:05 -0400686 acpi_device_name(device), acpi_device_bid(device),
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300687 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400688
689 if (!first_ec)
690 first_ec = device;
691
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400692 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (result)
694 kfree(ec);
695
Patrick Mocheld550d982006-06-27 00:41:40 -0400696 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Len Brown50526df2005-08-11 17:32:05 -0400699static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400701 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400705 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 ec = acpi_driver_data(device);
708
709 acpi_ec_remove_fs(device);
710
711 kfree(ec);
712
Patrick Mocheld550d982006-06-27 00:41:40 -0400713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400717acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400719 struct acpi_ec *ec = (struct acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Bob Moore50eca3e2005-09-30 19:03:00 -0400721 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return AE_OK;
723 }
724
725 /*
726 * The first address region returned is the data port, and
727 * the second address region returned is the status/command
728 * port.
729 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400730 if (ec->data_addr == 0) {
731 ec->data_addr = resource->data.io.minimum;
732 } else if (ec->command_addr == 0) {
733 ec->command_addr = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 } else {
735 return AE_CTRL_TERMINATE;
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return AE_OK;
739}
740
Len Brown50526df2005-08-11 17:32:05 -0400741static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Len Brown50526df2005-08-11 17:32:05 -0400743 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400744 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 ec = acpi_driver_data(device);
751
752 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * Get I/O port addresses. Convert to GAS format.
757 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400758 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400759 acpi_ec_io_ports, ec);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400760 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400761 ACPI_EXCEPTION((AE_INFO, status,
762 "Error getting I/O port addresses"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400766 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300767 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /*
770 * Install GPE handler
771 */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300772 status = acpi_install_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400773 ACPI_GPE_EDGE_TRIGGERED,
774 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400776 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300778 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
779 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400781 status = acpi_install_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400782 ACPI_ADR_SPACE_EC,
783 &acpi_ec_space_handler,
784 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ACPI_FAILURE(status)) {
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300786 acpi_remove_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400787 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -0400788 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Patrick Mocheld550d982006-06-27 00:41:40 -0400791 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Len Brown50526df2005-08-11 17:32:05 -0400794static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Len Brown50526df2005-08-11 17:32:05 -0400796 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400797 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400801 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 ec = acpi_driver_data(device);
804
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400805 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400806 ACPI_ADR_SPACE_EC,
807 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400809 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Len Brown50526df2005-08-11 17:32:05 -0400811 status =
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300812 acpi_remove_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400813 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Patrick Mocheld550d982006-06-27 00:41:40 -0400817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -0400821acpi_fake_ecdt_callback(acpi_handle handle,
822 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Len Brown50526df2005-08-11 17:32:05 -0400824 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300826 mutex_init(&ec_ecdt->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400827 if (acpi_ec_mode == EC_INTR) {
828 init_waitqueue_head(&ec_ecdt->wait);
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400831 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (ACPI_FAILURE(status))
833 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400835 ec_ecdt->uid = -1;
836 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Len Brown50526df2005-08-11 17:32:05 -0400838 status =
839 acpi_evaluate_integer(handle, "_GPE", NULL,
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300840 &ec_ecdt->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (ACPI_FAILURE(status))
842 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400843 ec_ecdt->global_lock = TRUE;
844 ec_ecdt->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400846 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300847 ec_ecdt->gpe, ec_ecdt->command_addr, ec_ecdt->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 return AE_CTRL_TERMINATE;
850}
851
852/*
853 * Some BIOS (such as some from Gateway laptops) access EC region very early
854 * such as in BAT0._INI or EC._INI before an EC device is found and
855 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
856 * required, but if EC regison is accessed early, it is required.
857 * The routine tries to workaround the BIOS bug by pre-scan EC device
858 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
859 * op region (since _REG isn't invoked yet). The assumption is true for
860 * all systems found.
861 */
Len Brown50526df2005-08-11 17:32:05 -0400862static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Len Brown50526df2005-08-11 17:32:05 -0400864 acpi_status status;
865 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400867 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400869 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!ec_ecdt) {
871 ret = -ENOMEM;
872 goto error;
873 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400874 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Len Brown50526df2005-08-11 17:32:05 -0400876 status = acpi_get_devices(ACPI_EC_HID,
877 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (ACPI_FAILURE(status)) {
879 kfree(ec_ecdt);
880 ec_ecdt = NULL;
881 ret = -ENODEV;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400882 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto error;
884 }
885 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400886 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return ret;
888}
889
Len Brown50526df2005-08-11 17:32:05 -0400890static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Len Brown50526df2005-08-11 17:32:05 -0400892 acpi_status status;
893 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -0400894
Len Brown50526df2005-08-11 17:32:05 -0400895 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
896 (struct acpi_table_header **)
897 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -0400898 if (ACPI_FAILURE(status))
899 return -ENODEV;
900
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400901 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
Luming Yu45bea152005-07-23 04:08:00 -0400902
903 /*
904 * Generate a temporary ec context to use until the namespace is scanned
905 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400906 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Luming Yu45bea152005-07-23 04:08:00 -0400907 if (!ec_ecdt)
908 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400909 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Luming Yu45bea152005-07-23 04:08:00 -0400910
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300911 mutex_init(&ec_ecdt->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400912 if (acpi_ec_mode == EC_INTR) {
913 init_waitqueue_head(&ec_ecdt->wait);
914 }
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400915 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
916 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300917 ec_ecdt->gpe = ecdt_ptr->gpe_bit;
Luming Yu45bea152005-07-23 04:08:00 -0400918 /* use the GL just to be safe */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400919 ec_ecdt->global_lock = TRUE;
920 ec_ecdt->uid = ecdt_ptr->uid;
Luming Yu45bea152005-07-23 04:08:00 -0400921
Len Brown50526df2005-08-11 17:32:05 -0400922 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400923 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
Luming Yu45bea152005-07-23 04:08:00 -0400924 if (ACPI_FAILURE(status)) {
925 goto error;
926 }
927
928 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400929 error:
930 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 kfree(ec_ecdt);
932 ec_ecdt = NULL;
933
934 return -ENODEV;
935}
936
937static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -0400938int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Len Brown50526df2005-08-11 17:32:05 -0400940 acpi_status status;
941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 ret = acpi_ec_get_real_ecdt();
944 /* Try to make a fake ECDT */
945 if (ret && acpi_fake_ecdt_enabled) {
946 ret = acpi_ec_fake_ecdt();
947 }
948
949 if (ret)
950 return 0;
951
952 /*
953 * Install GPE handler
954 */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300955 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400956 ACPI_GPE_EDGE_TRIGGERED,
957 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (ACPI_FAILURE(status)) {
959 goto error;
960 }
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300961 acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
962 acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Len Brown50526df2005-08-11 17:32:05 -0400964 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
965 ACPI_ADR_SPACE_EC,
966 &acpi_ec_space_handler,
967 &acpi_ec_space_setup,
968 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (ACPI_FAILURE(status)) {
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300970 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400971 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto error;
973 }
974
975 return 0;
976
Len Brown50526df2005-08-11 17:32:05 -0400977 error:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400978 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 kfree(ec_ecdt);
980 ec_ecdt = NULL;
981
982 return -ENODEV;
983}
984
Len Brown50526df2005-08-11 17:32:05 -0400985static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Len Brown50526df2005-08-11 17:32:05 -0400987 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
994 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* Now register the driver for the EC */
998 result = acpi_bus_register_driver(&acpi_ec_driver);
999 if (result < 0) {
1000 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007subsys_initcall(acpi_ec_init);
1008
1009/* EC driver currently not unloadable */
1010#if 0
Len Brown50526df2005-08-11 17:32:05 -04001011static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 acpi_bus_unregister_driver(&acpi_ec_driver);
1015
1016 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1017
Patrick Mocheld550d982006-06-27 00:41:40 -04001018 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
Len Brown50526df2005-08-11 17:32:05 -04001020#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022static int __init acpi_fake_ecdt_setup(char *str)
1023{
1024 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001025 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001029static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001030{
Len Brown02b28a32005-12-05 16:33:04 -05001031 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001032
Len Brown02b28a32005-12-05 16:33:04 -05001033 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001034 return 0;
1035
Len Brown02b28a32005-12-05 16:33:04 -05001036 if (intr) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001037 acpi_ec_mode = EC_INTR;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001038 } else {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001039 acpi_ec_mode = EC_POLL;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001040 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001041 acpi_ec_driver.ops.add = acpi_ec_add;
1042 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1043
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001044 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001045}
Len Brown50526df2005-08-11 17:32:05 -04001046
Len Brown53f11d42005-12-05 16:46:36 -05001047__setup("ec_intr=", acpi_ec_set_intr_mode);