blob: 4d17777110edbf7f16a0871d096ec2e0895409f5 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define ACPI_EC_COMMAND_READ 0x80
60#define ACPI_EC_COMMAND_WRITE 0x81
Dmitry Torokhov451566f2005-03-19 01:10:05 -050061#define ACPI_EC_BURST_ENABLE 0x82
62#define ACPI_EC_BURST_DISABLE 0x83
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define ACPI_EC_COMMAND_QUERY 0x84
Denis M. Sadykov703959d2006-09-26 19:50:33 +040064
65/* EC events */
66enum {
67 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
68 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
69};
70
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030071#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040073
74enum {
75 EC_INTR = 1, /* Output buffer full */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040076 EC_POLL, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040077};
78
Len Brown50526df2005-08-11 17:32:05 -040079static int acpi_ec_remove(struct acpi_device *device, int type);
80static int acpi_ec_start(struct acpi_device *device);
81static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040082static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static struct acpi_driver acpi_ec_driver = {
Len Brown50526df2005-08-11 17:32:05 -040085 .name = ACPI_EC_DRIVER_NAME,
86 .class = ACPI_EC_CLASS,
87 .ids = ACPI_EC_HID,
88 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040089 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040090 .remove = acpi_ec_remove,
91 .start = acpi_ec_start,
92 .stop = acpi_ec_stop,
93 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040095
96/* 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 +040097struct acpi_ec {
98 acpi_handle handle;
99 unsigned long uid;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300100 unsigned long gpe;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400101 unsigned long command_addr;
102 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400103 unsigned long global_lock;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300104 struct mutex lock;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300105 atomic_t query_pending;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400106 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
107 wait_queue_head_t wait;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400108} *ec_ecdt;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400109
110/* External interfaces use first EC only, so remember */
111static struct acpi_device *first_ec;
112static int acpi_ec_mode = EC_INTR;
113
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 Starikovskiybec5a1e2006-12-07 18:42:16 +0300138static int acpi_ec_check_status(struct acpi_ec *ec, u8 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);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400141 switch (event) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400142 case ACPI_EC_EVENT_OBF_1:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400143 if (status & ACPI_EC_FLAG_OBF)
144 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400145 break;
146 case ACPI_EC_EVENT_IBF_0:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400147 if (!(status & ACPI_EC_FLAG_IBF))
148 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400149 break;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400150 default:
151 break;
152 }
153
154 return 0;
155}
156
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400158{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300159 if (acpi_ec_mode == EC_POLL) {
Alexey Starikovskiy50c1e112006-12-07 18:42:17 +0300160 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
161 while (time_before(jiffies, delay)) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300162 if (acpi_ec_check_status(ec, event))
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400163 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400164 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300165 } else {
166 if (wait_event_timeout(ec->wait,
167 acpi_ec_check_status(ec, event),
168 msecs_to_jiffies(ACPI_EC_DELAY)) ||
169 acpi_ec_check_status(ec, event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400170 return 0;
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300171 } else {
172 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
173 " status = %d, expect_event = %d\n",
174 acpi_ec_read_status(ec), event);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400175 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300176 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400177
Patrick Mocheld550d982006-06-27 00:41:40 -0400178 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500179}
180
Len Brown02b28a32005-12-05 16:33:04 -0500181#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400182/*
183 * Note: samsung nv5000 doesn't work with ec burst mode.
184 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
185 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400186int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500187{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400188 u8 tmp = 0;
189 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500190
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500191
192 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400193 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400194 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Len Brown50526df2005-08-11 17:32:05 -0400195 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400196 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400197 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
198 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
199 tmp = acpi_ec_read_data(ec);
Len Brown50526df2005-08-11 17:32:05 -0400200 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500202 }
Luming Yu668d74c2005-07-23 00:26:33 -0400203 }
204
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400205 atomic_set(&ec->leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400207 end:
208 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500210}
211
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400212int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500213{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400214 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500215
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500216
Luming Yu06a2a382005-09-27 00:43:00 -0400217 status = acpi_ec_read_status(ec);
218 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400219 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu06a2a382005-09-27 00:43:00 -0400220 if(status)
221 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400222 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
223 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400224 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400225 atomic_set(&ec->leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400227 end:
228 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400229 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500230}
Len Brown02b28a32005-12-05 16:33:04 -0500231#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400233static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400234 const u8 *wdata, unsigned wdata_len,
235 u8 *rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400236{
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300237 int result = 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400238
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400239 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400240
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400241 for (; wdata_len > 0; wdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400242 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300243 if (result) {
244 printk(KERN_ERR PREFIX "write_cmd timeout, command = %d\n",
245 command);
246 goto end;
247 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400249 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400250
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300251 if (!rdata_len) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400252 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300253 if (result) {
254 printk(KERN_ERR PREFIX "finish-write timeout, command = %d\n",
255 command);
256 goto end;
257 }
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300258 } else if (command == ACPI_EC_COMMAND_QUERY) {
259 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400260 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400261
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400262 for (; rdata_len > 0; rdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400263 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300264 if (result) {
265 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
266 command);
267 goto end;
268 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400269
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400270 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400271 }
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300272 end:
273 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400274}
275
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400276static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
277 const u8 *wdata, unsigned wdata_len,
278 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400279{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400280 int status;
Len Brown50526df2005-08-11 17:32:05 -0400281 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400283 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400286 if (rdata)
287 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300289 mutex_lock(&ec->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400290 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
292 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500295
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300296 /* Make sure GPE is enabled before doing transaction */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300297 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300298
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400299 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu716e0842005-08-10 01:40:00 -0400300 if (status) {
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400301 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500302 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400305 status = acpi_ec_transaction_unlocked(ec, command,
306 wdata, wdata_len,
307 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400308
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400309end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400311 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 acpi_release_global_lock(glk);
Alexey Starikovskiy523953b2006-12-07 18:42:17 +0300313 mutex_unlock(&ec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400318static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400319{
320 int result;
321 u8 d;
322
323 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
324 &address, 1, &d, 1);
325 *data = d;
326 return result;
327}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400328
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400329static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
330{
331 u8 wdata[2] = { address, data };
332 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
333 wdata, 2, NULL, 0);
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
337 * Externally callable EC access functions. For now, assume 1 EC only
338 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400339int ec_read(u8 addr, u8 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400341 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400343 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (!first_ec)
346 return -ENODEV;
347
348 ec = acpi_driver_data(first_ec);
349
350 err = acpi_ec_read(ec, addr, &temp_data);
351
352 if (!err) {
353 *val = temp_data;
354 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400355 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return err;
357}
Len Brown50526df2005-08-11 17:32:05 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359EXPORT_SYMBOL(ec_read);
360
Len Brown50526df2005-08-11 17:32:05 -0400361int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400363 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int err;
365
366 if (!first_ec)
367 return -ENODEV;
368
369 ec = acpi_driver_data(first_ec);
370
371 err = acpi_ec_write(ec, addr, val);
372
373 return err;
374}
Len Brown50526df2005-08-11 17:32:05 -0400375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376EXPORT_SYMBOL(ec_write);
377
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400378extern int ec_transaction(u8 command,
379 const u8 *wdata, unsigned wdata_len,
380 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400381{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400382 struct acpi_ec *ec;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400383
384 if (!first_ec)
385 return -ENODEV;
386
387 ec = acpi_driver_data(first_ec);
388
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400389 return acpi_ec_transaction(ec, command, wdata,
390 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400391}
Luming Yu45bea152005-07-23 04:08:00 -0400392
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400393EXPORT_SYMBOL(ec_transaction);
394
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400395static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400396{
397 int result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400398 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400399
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400400 if (!ec || !data)
401 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400402
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400403 /*
404 * Query the EC to find out which _Qxx method we need to evaluate.
405 * Note that successful completion of the query causes the ACPI_EC_SCI
406 * bit to be cleared (and thus clearing the interrupt source).
407 */
Luming Yu45bea152005-07-23 04:08:00 -0400408
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400409 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
410 if (result)
411 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400412
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400413 if (!d)
414 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400415
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400416 *data = d;
417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* --------------------------------------------------------------------------
421 Event Management
422 -------------------------------------------------------------------------- */
423
Len Brown50526df2005-08-11 17:32:05 -0400424static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400426 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400427 u8 value = 0;
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300428 char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400429
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300430 if (!ec || acpi_ec_query(ec, &value))
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300431 return;
Luming Yu45bea152005-07-23 04:08:00 -0400432
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400433 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400434
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300435 printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
Luming Yu45bea152005-07-23 04:08:00 -0400436
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400437 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Len Brown50526df2005-08-11 17:32:05 -0400440static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Len Brown50526df2005-08-11 17:32:05 -0400442 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400443 u8 value;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400444 struct acpi_ec *ec = (struct acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400447 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiyaf3fd142006-12-07 18:42:16 +0300448 wake_up(&ec->wait);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500449 }
450
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300451 value = acpi_ec_read_status(ec);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300452 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
453 atomic_set(&ec->query_pending, 1);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400454 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Len Brown50526df2005-08-11 17:32:05 -0400455 }
Alexey Starikovskiye41334c2006-12-07 18:42:16 +0300456
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500457 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400458 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461/* --------------------------------------------------------------------------
462 Address Space Management
463 -------------------------------------------------------------------------- */
464
465static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400466acpi_ec_space_setup(acpi_handle region_handle,
467 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 /*
470 * The EC object is in the handler context and is needed
471 * when calling the acpi_ec_space_handler.
472 */
Len Brown50526df2005-08-11 17:32:05 -0400473 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
474 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return AE_OK;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400480acpi_ec_space_handler(u32 function,
481 acpi_physical_address address,
482 u32 bit_width,
483 acpi_integer * value,
484 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Len Brown50526df2005-08-11 17:32:05 -0400486 int result = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400487 struct acpi_ec *ec = NULL;
Len Brown50526df2005-08-11 17:32:05 -0400488 u64 temp = *value;
489 acpi_integer f_v = 0;
490 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Luming Yufa9cd542005-03-19 01:54:47 -0500496 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400497 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400500 ec = (struct acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Len Brown50526df2005-08-11 17:32:05 -0400502 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 switch (function) {
504 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500505 temp = 0;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400506 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500509 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511 default:
512 result = -EINVAL;
513 goto out;
514 break;
515 }
516
517 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500518 if (bit_width) {
519 if (function == ACPI_READ)
520 f_v |= temp << 8 * i;
521 if (function == ACPI_WRITE)
522 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500524 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto next_byte;
526 }
527
Luming Yufa9cd542005-03-19 01:54:47 -0500528 if (function == ACPI_READ) {
529 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *value = f_v;
531 }
532
Len Brown50526df2005-08-11 17:32:05 -0400533 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 switch (result) {
535 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400536 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400539 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400545 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/* --------------------------------------------------------------------------
550 FS Interface (/proc)
551 -------------------------------------------------------------------------- */
552
Len Brown50526df2005-08-11 17:32:05 -0400553static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Len Brown50526df2005-08-11 17:32:05 -0400555static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400557 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!ec)
561 goto end;
562
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300563 seq_printf(seq, "gpe: 0x%02x\n",
564 (u32) ec->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400566 (u32) ec->command_addr,
567 (u32) ec->data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400569 ec->global_lock ? "yes" : "no");
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300570 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Len Brown50526df2005-08-11 17:32:05 -0400572 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
577{
578 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
579}
580
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400581static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400582 .open = acpi_ec_info_open_fs,
583 .read = seq_read,
584 .llseek = seq_lseek,
585 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .owner = THIS_MODULE,
587};
588
Len Brown50526df2005-08-11 17:32:05 -0400589static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Len Brown50526df2005-08-11 17:32:05 -0400591 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (!acpi_device_dir(device)) {
595 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400596 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400598 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400602 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else {
606 entry->proc_fops = &acpi_ec_info_ops;
607 entry->data = acpi_driver_data(device);
608 entry->owner = THIS_MODULE;
609 }
610
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Len Brown50526df2005-08-11 17:32:05 -0400614static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (acpi_device_dir(device)) {
618 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
619 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
620 acpi_device_dir(device) = NULL;
621 }
622
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* --------------------------------------------------------------------------
627 Driver Interface
628 -------------------------------------------------------------------------- */
629
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400630static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Len Brown50526df2005-08-11 17:32:05 -0400632 int result = 0;
633 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400634 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400640 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400642 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400643 memset(ec, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400645 ec->handle = device->handle;
646 ec->uid = -1;
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300647 mutex_init(&ec->lock);
Alexey Starikovskiy5d0c2882006-12-07 18:42:16 +0300648 atomic_set(&ec->query_pending, 0);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400649 if (acpi_ec_mode == EC_INTR) {
650 atomic_set(&ec->leaving_burst, 1);
651 init_waitqueue_head(&ec->wait);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
654 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
655 acpi_driver_data(device) = ec;
656
657 /* Use the global lock for all EC transactions? */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400658 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
659 &ec->global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500661 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
662 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
663 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400665 ACPI_ADR_SPACE_EC,
666 &acpi_ec_space_handler);
667
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300668 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400669 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 kfree(ec_ecdt);
672 }
673
674 /* Get GPE bit assignment (EC events). */
675 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400676 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400677 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300678 &ec->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400680 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 result = -ENODEV;
682 goto end;
683 }
684
685 result = acpi_ec_add_fs(device);
686 if (result)
687 goto end;
688
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400689 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Len Brown50526df2005-08-11 17:32:05 -0400690 acpi_device_name(device), acpi_device_bid(device),
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300691 (u32) ec->gpe));
Luming Yu45bea152005-07-23 04:08:00 -0400692
693 if (!first_ec)
694 first_ec = device;
695
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400696 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (result)
698 kfree(ec);
699
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Len Brown50526df2005-08-11 17:32:05 -0400703static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400705 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 ec = acpi_driver_data(device);
712
713 acpi_ec_remove_fs(device);
714
715 kfree(ec);
716
Patrick Mocheld550d982006-06-27 00:41:40 -0400717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400721acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400723 struct acpi_ec *ec = (struct acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Bob Moore50eca3e2005-09-30 19:03:00 -0400725 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return AE_OK;
727 }
728
729 /*
730 * The first address region returned is the data port, and
731 * the second address region returned is the status/command
732 * port.
733 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400734 if (ec->data_addr == 0) {
735 ec->data_addr = resource->data.io.minimum;
736 } else if (ec->command_addr == 0) {
737 ec->command_addr = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 } else {
739 return AE_CTRL_TERMINATE;
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return AE_OK;
743}
744
Len Brown50526df2005-08-11 17:32:05 -0400745static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Len Brown50526df2005-08-11 17:32:05 -0400747 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400748 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 ec = acpi_driver_data(device);
755
756 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Get I/O port addresses. Convert to GAS format.
761 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400762 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400763 acpi_ec_io_ports, ec);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400764 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400765 ACPI_EXCEPTION((AE_INFO, status,
766 "Error getting I/O port addresses"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400770 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300771 ec->gpe, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
774 * Install GPE handler
775 */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300776 status = acpi_install_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400777 ACPI_GPE_EDGE_TRIGGERED,
778 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400780 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300782 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
783 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400785 status = acpi_install_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400786 ACPI_ADR_SPACE_EC,
787 &acpi_ec_space_handler,
788 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (ACPI_FAILURE(status)) {
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300790 acpi_remove_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400791 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -0400792 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Len Brown50526df2005-08-11 17:32:05 -0400798static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Len Brown50526df2005-08-11 17:32:05 -0400800 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400801 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 ec = acpi_driver_data(device);
808
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400809 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400810 ACPI_ADR_SPACE_EC,
811 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Len Brown50526df2005-08-11 17:32:05 -0400815 status =
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300816 acpi_remove_gpe_handler(NULL, ec->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400817 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Patrick Mocheld550d982006-06-27 00:41:40 -0400821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -0400825acpi_fake_ecdt_callback(acpi_handle handle,
826 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Len Brown50526df2005-08-11 17:32:05 -0400828 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300830 mutex_init(&ec_ecdt->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400831 if (acpi_ec_mode == EC_INTR) {
832 init_waitqueue_head(&ec_ecdt->wait);
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400835 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (ACPI_FAILURE(status))
837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400839 ec_ecdt->uid = -1;
840 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Len Brown50526df2005-08-11 17:32:05 -0400842 status =
843 acpi_evaluate_integer(handle, "_GPE", NULL,
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300844 &ec_ecdt->gpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ACPI_FAILURE(status))
846 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400847 ec_ecdt->global_lock = TRUE;
848 ec_ecdt->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400850 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300851 ec_ecdt->gpe, ec_ecdt->command_addr, ec_ecdt->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 return AE_CTRL_TERMINATE;
854}
855
856/*
857 * Some BIOS (such as some from Gateway laptops) access EC region very early
858 * such as in BAT0._INI or EC._INI before an EC device is found and
859 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
860 * required, but if EC regison is accessed early, it is required.
861 * The routine tries to workaround the BIOS bug by pre-scan EC device
862 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
863 * op region (since _REG isn't invoked yet). The assumption is true for
864 * all systems found.
865 */
Len Brown50526df2005-08-11 17:32:05 -0400866static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Len Brown50526df2005-08-11 17:32:05 -0400868 acpi_status status;
869 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400871 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400873 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (!ec_ecdt) {
875 ret = -ENOMEM;
876 goto error;
877 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400878 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Len Brown50526df2005-08-11 17:32:05 -0400880 status = acpi_get_devices(ACPI_EC_HID,
881 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (ACPI_FAILURE(status)) {
883 kfree(ec_ecdt);
884 ec_ecdt = NULL;
885 ret = -ENODEV;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400886 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto error;
888 }
889 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400890 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return ret;
892}
893
Len Brown50526df2005-08-11 17:32:05 -0400894static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Len Brown50526df2005-08-11 17:32:05 -0400896 acpi_status status;
897 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -0400898
Len Brown50526df2005-08-11 17:32:05 -0400899 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
900 (struct acpi_table_header **)
901 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -0400902 if (ACPI_FAILURE(status))
903 return -ENODEV;
904
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400905 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
Luming Yu45bea152005-07-23 04:08:00 -0400906
907 /*
908 * Generate a temporary ec context to use until the namespace is scanned
909 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400910 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Luming Yu45bea152005-07-23 04:08:00 -0400911 if (!ec_ecdt)
912 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400913 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Luming Yu45bea152005-07-23 04:08:00 -0400914
Alexey Starikovskiyc787a852006-12-07 18:42:16 +0300915 mutex_init(&ec_ecdt->lock);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400916 if (acpi_ec_mode == EC_INTR) {
917 init_waitqueue_head(&ec_ecdt->wait);
918 }
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400919 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
920 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300921 ec_ecdt->gpe = ecdt_ptr->gpe_bit;
Luming Yu45bea152005-07-23 04:08:00 -0400922 /* use the GL just to be safe */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400923 ec_ecdt->global_lock = TRUE;
924 ec_ecdt->uid = ecdt_ptr->uid;
Luming Yu45bea152005-07-23 04:08:00 -0400925
Len Brown50526df2005-08-11 17:32:05 -0400926 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400927 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
Luming Yu45bea152005-07-23 04:08:00 -0400928 if (ACPI_FAILURE(status)) {
929 goto error;
930 }
931
932 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400933 error:
934 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 kfree(ec_ecdt);
936 ec_ecdt = NULL;
937
938 return -ENODEV;
939}
940
941static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -0400942int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Len Brown50526df2005-08-11 17:32:05 -0400944 acpi_status status;
945 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 ret = acpi_ec_get_real_ecdt();
948 /* Try to make a fake ECDT */
949 if (ret && acpi_fake_ecdt_enabled) {
950 ret = acpi_ec_fake_ecdt();
951 }
952
953 if (ret)
954 return 0;
955
956 /*
957 * Install GPE handler
958 */
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300959 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400960 ACPI_GPE_EDGE_TRIGGERED,
961 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (ACPI_FAILURE(status)) {
963 goto error;
964 }
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300965 acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
966 acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Len Brown50526df2005-08-11 17:32:05 -0400968 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
969 ACPI_ADR_SPACE_EC,
970 &acpi_ec_space_handler,
971 &acpi_ec_space_setup,
972 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ACPI_FAILURE(status)) {
Alexey Starikovskiya86e2772006-12-07 18:42:16 +0300974 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
Len Brown50526df2005-08-11 17:32:05 -0400975 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto error;
977 }
978
979 return 0;
980
Len Brown50526df2005-08-11 17:32:05 -0400981 error:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400982 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 kfree(ec_ecdt);
984 ec_ecdt = NULL;
985
986 return -ENODEV;
987}
988
Len Brown50526df2005-08-11 17:32:05 -0400989static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Len Brown50526df2005-08-11 17:32:05 -0400991 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
998 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* Now register the driver for the EC */
1002 result = acpi_bus_register_driver(&acpi_ec_driver);
1003 if (result < 0) {
1004 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001005 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Patrick Mocheld550d982006-06-27 00:41:40 -04001008 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011subsys_initcall(acpi_ec_init);
1012
1013/* EC driver currently not unloadable */
1014#if 0
Len Brown50526df2005-08-11 17:32:05 -04001015static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 acpi_bus_unregister_driver(&acpi_ec_driver);
1019
1020 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1021
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
Len Brown50526df2005-08-11 17:32:05 -04001024#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026static int __init acpi_fake_ecdt_setup(char *str)
1027{
1028 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001033static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001034{
Len Brown02b28a32005-12-05 16:33:04 -05001035 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001036
Len Brown02b28a32005-12-05 16:33:04 -05001037 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001038 return 0;
1039
Len Brown02b28a32005-12-05 16:33:04 -05001040 if (intr) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001041 acpi_ec_mode = EC_INTR;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001042 } else {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001043 acpi_ec_mode = EC_POLL;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001044 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001045 acpi_ec_driver.ops.add = acpi_ec_add;
1046 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1047
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001048 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001049}
Len Brown50526df2005-08-11 17:32:05 -04001050
Len Brown53f11d42005-12-05 16:46:36 -05001051__setup("ec_intr=", acpi_ec_set_intr_mode);