blob: e6d4b084dca2eeb40213074998ccdfde52278260 [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
49/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050052#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040054
55/* EC commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ACPI_EC_COMMAND_READ 0x80
57#define ACPI_EC_COMMAND_WRITE 0x81
Dmitry Torokhov451566f2005-03-19 01:10:05 -050058#define ACPI_EC_BURST_ENABLE 0x82
59#define ACPI_EC_BURST_DISABLE 0x83
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define ACPI_EC_COMMAND_QUERY 0x84
Denis M. Sadykov703959d2006-09-26 19:50:33 +040061
62/* EC events */
63enum {
64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
66};
67
68#define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
69#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
70#define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
71#define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
72
73enum {
74 EC_INTR = 1, /* Output buffer full */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040075 EC_POLL, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040076};
77
Len Brown50526df2005-08-11 17:32:05 -040078static int acpi_ec_remove(struct acpi_device *device, int type);
79static int acpi_ec_start(struct acpi_device *device);
80static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040081static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct acpi_driver acpi_ec_driver = {
Len Brown50526df2005-08-11 17:32:05 -040084 .name = ACPI_EC_DRIVER_NAME,
85 .class = ACPI_EC_CLASS,
86 .ids = ACPI_EC_HID,
87 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040088 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040089 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
91 .stop = acpi_ec_stop,
92 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040094
95/* 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 +040096struct acpi_ec {
97 acpi_handle handle;
98 unsigned long uid;
99 unsigned long gpe_bit;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400100 unsigned long command_addr;
101 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400102 unsigned long global_lock;
103 struct semaphore sem;
104 unsigned int expect_event;
105 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
106 wait_queue_head_t wait;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400107} *ec_ecdt;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400108
109/* External interfaces use first EC only, so remember */
110static struct acpi_device *first_ec;
111static int acpi_ec_mode = EC_INTR;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* --------------------------------------------------------------------------
114 Transaction Management
115 -------------------------------------------------------------------------- */
116
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400117static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400119 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400122static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400124 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400125}
126
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400127static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400128{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400129 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130}
131
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400132static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400133{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400134 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400135}
136
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400137static int acpi_ec_check_status(u8 status, u8 event)
138{
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400139 switch (event) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400140 case ACPI_EC_EVENT_OBF_1:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400141 if (status & ACPI_EC_FLAG_OBF)
142 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400143 break;
144 case ACPI_EC_EVENT_IBF_0:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400145 if (!(status & ACPI_EC_FLAG_IBF))
146 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400147 break;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400148 default:
149 break;
150 }
151
152 return 0;
153}
154
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400155static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400156{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400157 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
158 long time_left;
Luming Yu45bea152005-07-23 04:08:00 -0400159
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400160 ec->expect_event = event;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400161 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400162 ec->expect_event = 0;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400163 return 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500164 }
165
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400166 do {
167 if (acpi_ec_mode == EC_POLL) {
168 udelay(ACPI_EC_UDELAY);
169 } else {
170 time_left = wait_event_timeout(ec->wait,
171 !ec->expect_event,
172 msecs_to_jiffies(ACPI_EC_DELAY));
173 if (time_left > 0) {
174 ec->expect_event = 0;
175 return 0;
176 }
177 }
178 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
179 ec->expect_event = 0;
180 return 0;
181 }
182 } while (--i > 0);
183
184 ec->expect_event = 0;
185
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500187}
188
Len Brown02b28a32005-12-05 16:33:04 -0500189#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400190/*
191 * Note: samsung nv5000 doesn't work with ec burst mode.
192 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
193 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400194int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500195{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400196 u8 tmp = 0;
197 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500198
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500199
200 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400201 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400202 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Len Brown50526df2005-08-11 17:32:05 -0400203 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400204 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400205 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
206 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
207 tmp = acpi_ec_read_data(ec);
Len Brown50526df2005-08-11 17:32:05 -0400208 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500210 }
Luming Yu668d74c2005-07-23 00:26:33 -0400211 }
212
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400213 atomic_set(&ec->leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400215 end:
216 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400217 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500218}
219
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400220int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500221{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400222 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500223
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500224
Luming Yu06a2a382005-09-27 00:43:00 -0400225 status = acpi_ec_read_status(ec);
226 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400227 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu06a2a382005-09-27 00:43:00 -0400228 if(status)
229 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400230 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
231 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400232 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400233 atomic_set(&ec->leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400235 end:
236 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400237 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500238}
Len Brown02b28a32005-12-05 16:33:04 -0500239#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400241static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400242 const u8 *wdata, unsigned wdata_len,
243 u8 *rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400244{
245 int result;
246
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400247 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400248
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400249 for (; wdata_len > 0; wdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400250 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400251 if (result)
252 return result;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400253 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400254 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400255
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400256 if (command == ACPI_EC_COMMAND_WRITE) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400257 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400258 if (result)
259 return result;
260 }
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);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400264 if (result)
265 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400266
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400267 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400268 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400269
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400270 return 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400271}
272
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400273static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
274 const u8 *wdata, unsigned wdata_len,
275 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400276{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400277 int status;
Len Brown50526df2005-08-11 17:32:05 -0400278 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400280 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400283 if (rdata)
284 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
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 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400291 down(&ec->sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500292
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400293 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu716e0842005-08-10 01:40:00 -0400294 if (status) {
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400295 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500296 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400299 status = acpi_ec_transaction_unlocked(ec, command,
300 wdata, wdata_len,
301 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400302
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400303end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400304 up(&ec->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400306 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 acpi_release_global_lock(glk);
308
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400312static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400313{
314 int result;
315 u8 d;
316
317 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
318 &address, 1, &d, 1);
319 *data = d;
320 return result;
321}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400322
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400323static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
324{
325 u8 wdata[2] = { address, data };
326 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
327 wdata, 2, NULL, 0);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
331 * Externally callable EC access functions. For now, assume 1 EC only
332 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400333int ec_read(u8 addr, u8 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400335 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400337 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!first_ec)
340 return -ENODEV;
341
342 ec = acpi_driver_data(first_ec);
343
344 err = acpi_ec_read(ec, addr, &temp_data);
345
346 if (!err) {
347 *val = temp_data;
348 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400349 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return err;
351}
Len Brown50526df2005-08-11 17:32:05 -0400352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353EXPORT_SYMBOL(ec_read);
354
Len Brown50526df2005-08-11 17:32:05 -0400355int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400357 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int err;
359
360 if (!first_ec)
361 return -ENODEV;
362
363 ec = acpi_driver_data(first_ec);
364
365 err = acpi_ec_write(ec, addr, val);
366
367 return err;
368}
Len Brown50526df2005-08-11 17:32:05 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370EXPORT_SYMBOL(ec_write);
371
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400372extern int ec_transaction(u8 command,
373 const u8 *wdata, unsigned wdata_len,
374 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400375{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400376 struct acpi_ec *ec;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400377
378 if (!first_ec)
379 return -ENODEV;
380
381 ec = acpi_driver_data(first_ec);
382
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400383 return acpi_ec_transaction(ec, command, wdata,
384 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400385}
Luming Yu45bea152005-07-23 04:08:00 -0400386
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400387EXPORT_SYMBOL(ec_transaction);
388
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400389static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400390{
391 int result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400392 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400393
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400394 if (!ec || !data)
395 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400396
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400397 /*
398 * Query the EC to find out which _Qxx method we need to evaluate.
399 * Note that successful completion of the query causes the ACPI_EC_SCI
400 * bit to be cleared (and thus clearing the interrupt source).
401 */
Luming Yu45bea152005-07-23 04:08:00 -0400402
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400403 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
404 if (result)
405 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400406
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400407 if (!d)
408 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400409
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400410 *data = d;
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* --------------------------------------------------------------------------
415 Event Management
416 -------------------------------------------------------------------------- */
417
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400418struct acpi_ec_query_data {
Len Brown50526df2005-08-11 17:32:05 -0400419 acpi_handle handle;
420 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421};
422
Len Brown50526df2005-08-11 17:32:05 -0400423static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400425 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400426 u8 value = 0;
427 static char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400428
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400429 if (!ec)
Luming Yu45bea152005-07-23 04:08:00 -0400430 goto end;
431
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400432 value = acpi_ec_read_status(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400433
Luming Yu45bea152005-07-23 04:08:00 -0400434 if (!(value & ACPI_EC_FLAG_SCI))
435 goto end;
436
437 if (acpi_ec_query(ec, &value))
438 goto end;
439
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400440 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400441
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400442 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
Luming Yu45bea152005-07-23 04:08:00 -0400443
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400444 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400445
Len Brown50526df2005-08-11 17:32:05 -0400446 end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400447 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Luming Yu45bea152005-07-23 04:08:00 -0400448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Len Brown50526df2005-08-11 17:32:05 -0400450static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Len Brown50526df2005-08-11 17:32:05 -0400452 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400453 u8 value;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400454 struct acpi_ec *ec = (struct acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400455
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400456 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500457 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400459 if (acpi_ec_mode == EC_INTR) {
460 if (acpi_ec_check_status(value, ec->expect_event)) {
461 ec->expect_event = 0;
462 wake_up(&ec->wait);
463 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500464 }
465
Len Brown50526df2005-08-11 17:32:05 -0400466 if (value & ACPI_EC_FLAG_SCI) {
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400467 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400468 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400469 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
470 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400471 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500472 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400473 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/* --------------------------------------------------------------------------
477 Address Space Management
478 -------------------------------------------------------------------------- */
479
480static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400481acpi_ec_space_setup(acpi_handle region_handle,
482 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 /*
485 * The EC object is in the handler context and is needed
486 * when calling the acpi_ec_space_handler.
487 */
Len Brown50526df2005-08-11 17:32:05 -0400488 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
489 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 return AE_OK;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400495acpi_ec_space_handler(u32 function,
496 acpi_physical_address address,
497 u32 bit_width,
498 acpi_integer * value,
499 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Len Brown50526df2005-08-11 17:32:05 -0400501 int result = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400502 struct acpi_ec *ec = NULL;
Len Brown50526df2005-08-11 17:32:05 -0400503 u64 temp = *value;
504 acpi_integer f_v = 0;
505 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400509 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Luming Yufa9cd542005-03-19 01:54:47 -0500511 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400512 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400515 ec = (struct acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Len Brown50526df2005-08-11 17:32:05 -0400517 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (function) {
519 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500520 temp = 0;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400521 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500524 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526 default:
527 result = -EINVAL;
528 goto out;
529 break;
530 }
531
532 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500533 if (bit_width) {
534 if (function == ACPI_READ)
535 f_v |= temp << 8 * i;
536 if (function == ACPI_WRITE)
537 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500539 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto next_byte;
541 }
542
Luming Yufa9cd542005-03-19 01:54:47 -0500543 if (function == ACPI_READ) {
544 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *value = f_v;
546 }
547
Len Brown50526df2005-08-11 17:32:05 -0400548 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 switch (result) {
550 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* --------------------------------------------------------------------------
565 FS Interface (/proc)
566 -------------------------------------------------------------------------- */
567
Len Brown50526df2005-08-11 17:32:05 -0400568static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Len Brown50526df2005-08-11 17:32:05 -0400570static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400572 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (!ec)
576 goto end;
577
578 seq_printf(seq, "gpe bit: 0x%02x\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400579 (u32) ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400581 (u32) ec->command_addr,
582 (u32) ec->data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400584 ec->global_lock ? "yes" : "no");
585 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Len Brown50526df2005-08-11 17:32:05 -0400587 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
592{
593 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
594}
595
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400596static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400597 .open = acpi_ec_info_open_fs,
598 .read = seq_read,
599 .llseek = seq_lseek,
600 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 .owner = THIS_MODULE,
602};
603
Len Brown50526df2005-08-11 17:32:05 -0400604static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Len Brown50526df2005-08-11 17:32:05 -0400606 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (!acpi_device_dir(device)) {
610 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400611 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400613 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400617 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400619 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 else {
621 entry->proc_fops = &acpi_ec_info_ops;
622 entry->data = acpi_driver_data(device);
623 entry->owner = THIS_MODULE;
624 }
625
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Len Brown50526df2005-08-11 17:32:05 -0400629static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (acpi_device_dir(device)) {
633 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
634 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
635 acpi_device_dir(device) = NULL;
636 }
637
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* --------------------------------------------------------------------------
642 Driver Interface
643 -------------------------------------------------------------------------- */
644
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400645static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Len Brown50526df2005-08-11 17:32:05 -0400647 int result = 0;
648 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400649 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400655 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400657 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400658 memset(ec, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400660 ec->handle = device->handle;
661 ec->uid = -1;
662 init_MUTEX(&ec->sem);
663 if (acpi_ec_mode == EC_INTR) {
664 atomic_set(&ec->leaving_burst, 1);
665 init_waitqueue_head(&ec->wait);
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
668 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
669 acpi_driver_data(device) = ec;
670
671 /* Use the global lock for all EC transactions? */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400672 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
673 &ec->global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500675 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
676 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
677 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400679 ACPI_ADR_SPACE_EC,
680 &acpi_ec_space_handler);
681
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400682 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400683 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 kfree(ec_ecdt);
686 }
687
688 /* Get GPE bit assignment (EC events). */
689 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400690 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400691 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
692 &ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400694 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 result = -ENODEV;
696 goto end;
697 }
698
699 result = acpi_ec_add_fs(device);
700 if (result)
701 goto end;
702
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400703 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Len Brown50526df2005-08-11 17:32:05 -0400704 acpi_device_name(device), acpi_device_bid(device),
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400705 (u32) ec->gpe_bit));
Luming Yu45bea152005-07-23 04:08:00 -0400706
707 if (!first_ec)
708 first_ec = device;
709
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400710 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (result)
712 kfree(ec);
713
Patrick Mocheld550d982006-06-27 00:41:40 -0400714 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Len Brown50526df2005-08-11 17:32:05 -0400717static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400719 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 ec = acpi_driver_data(device);
726
727 acpi_ec_remove_fs(device);
728
729 kfree(ec);
730
Patrick Mocheld550d982006-06-27 00:41:40 -0400731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400735acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400737 struct acpi_ec *ec = (struct acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Bob Moore50eca3e2005-09-30 19:03:00 -0400739 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return AE_OK;
741 }
742
743 /*
744 * The first address region returned is the data port, and
745 * the second address region returned is the status/command
746 * port.
747 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400748 if (ec->data_addr == 0) {
749 ec->data_addr = resource->data.io.minimum;
750 } else if (ec->command_addr == 0) {
751 ec->command_addr = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 } else {
753 return AE_CTRL_TERMINATE;
754 }
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return AE_OK;
757}
758
Len Brown50526df2005-08-11 17:32:05 -0400759static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Len Brown50526df2005-08-11 17:32:05 -0400761 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400762 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 ec = acpi_driver_data(device);
769
770 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
774 * Get I/O port addresses. Convert to GAS format.
775 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400776 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400777 acpi_ec_io_ports, ec);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400778 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400779 ACPI_EXCEPTION((AE_INFO, status,
780 "Error getting I/O port addresses"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400781 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400784 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
785 ec->gpe_bit, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * Install GPE handler
789 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400790 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400791 ACPI_GPE_EDGE_TRIGGERED,
792 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400794 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400796 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
797 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400799 status = acpi_install_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400800 ACPI_ADR_SPACE_EC,
801 &acpi_ec_space_handler,
802 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400804 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400805 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -0400806 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Patrick Mocheld550d982006-06-27 00:41:40 -0400809 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Len Brown50526df2005-08-11 17:32:05 -0400812static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Len Brown50526df2005-08-11 17:32:05 -0400814 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400815 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 ec = acpi_driver_data(device);
822
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400823 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400824 ACPI_ADR_SPACE_EC,
825 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400827 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Len Brown50526df2005-08-11 17:32:05 -0400829 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400830 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400831 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400833 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Patrick Mocheld550d982006-06-27 00:41:40 -0400835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -0400839acpi_fake_ecdt_callback(acpi_handle handle,
840 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Len Brown50526df2005-08-11 17:32:05 -0400842 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400844 init_MUTEX(&ec_ecdt->sem);
845 if (acpi_ec_mode == EC_INTR) {
846 init_waitqueue_head(&ec_ecdt->wait);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400849 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (ACPI_FAILURE(status))
851 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400853 ec_ecdt->uid = -1;
854 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Len Brown50526df2005-08-11 17:32:05 -0400856 status =
857 acpi_evaluate_integer(handle, "_GPE", NULL,
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400858 &ec_ecdt->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (ACPI_FAILURE(status))
860 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400861 ec_ecdt->global_lock = TRUE;
862 ec_ecdt->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400864 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
865 ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 return AE_CTRL_TERMINATE;
868}
869
870/*
871 * Some BIOS (such as some from Gateway laptops) access EC region very early
872 * such as in BAT0._INI or EC._INI before an EC device is found and
873 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
874 * required, but if EC regison is accessed early, it is required.
875 * The routine tries to workaround the BIOS bug by pre-scan EC device
876 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
877 * op region (since _REG isn't invoked yet). The assumption is true for
878 * all systems found.
879 */
Len Brown50526df2005-08-11 17:32:05 -0400880static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Len Brown50526df2005-08-11 17:32:05 -0400882 acpi_status status;
883 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400885 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400887 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (!ec_ecdt) {
889 ret = -ENOMEM;
890 goto error;
891 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400892 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Len Brown50526df2005-08-11 17:32:05 -0400894 status = acpi_get_devices(ACPI_EC_HID,
895 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (ACPI_FAILURE(status)) {
897 kfree(ec_ecdt);
898 ec_ecdt = NULL;
899 ret = -ENODEV;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400900 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 goto error;
902 }
903 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400904 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return ret;
906}
907
Len Brown50526df2005-08-11 17:32:05 -0400908static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Len Brown50526df2005-08-11 17:32:05 -0400910 acpi_status status;
911 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -0400912
Len Brown50526df2005-08-11 17:32:05 -0400913 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
914 (struct acpi_table_header **)
915 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -0400916 if (ACPI_FAILURE(status))
917 return -ENODEV;
918
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400919 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
Luming Yu45bea152005-07-23 04:08:00 -0400920
921 /*
922 * Generate a temporary ec context to use until the namespace is scanned
923 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400924 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Luming Yu45bea152005-07-23 04:08:00 -0400925 if (!ec_ecdt)
926 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400927 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Luming Yu45bea152005-07-23 04:08:00 -0400928
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400929 init_MUTEX(&ec_ecdt->sem);
930 if (acpi_ec_mode == EC_INTR) {
931 init_waitqueue_head(&ec_ecdt->wait);
932 }
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400933 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
934 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400935 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
Luming Yu45bea152005-07-23 04:08:00 -0400936 /* use the GL just to be safe */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400937 ec_ecdt->global_lock = TRUE;
938 ec_ecdt->uid = ecdt_ptr->uid;
Luming Yu45bea152005-07-23 04:08:00 -0400939
Len Brown50526df2005-08-11 17:32:05 -0400940 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400941 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
Luming Yu45bea152005-07-23 04:08:00 -0400942 if (ACPI_FAILURE(status)) {
943 goto error;
944 }
945
946 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400947 error:
948 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 kfree(ec_ecdt);
950 ec_ecdt = NULL;
951
952 return -ENODEV;
953}
954
955static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -0400956int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Len Brown50526df2005-08-11 17:32:05 -0400958 acpi_status status;
959 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 ret = acpi_ec_get_real_ecdt();
962 /* Try to make a fake ECDT */
963 if (ret && acpi_fake_ecdt_enabled) {
964 ret = acpi_ec_fake_ecdt();
965 }
966
967 if (ret)
968 return 0;
969
970 /*
971 * Install GPE handler
972 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400973 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400974 ACPI_GPE_EDGE_TRIGGERED,
975 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (ACPI_FAILURE(status)) {
977 goto error;
978 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400979 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
980 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Len Brown50526df2005-08-11 17:32:05 -0400982 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
983 ACPI_ADR_SPACE_EC,
984 &acpi_ec_space_handler,
985 &acpi_ec_space_setup,
986 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400988 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400989 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto error;
991 }
992
993 return 0;
994
Len Brown50526df2005-08-11 17:32:05 -0400995 error:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400996 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 kfree(ec_ecdt);
998 ec_ecdt = NULL;
999
1000 return -ENODEV;
1001}
1002
Len Brown50526df2005-08-11 17:32:05 -04001003static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Len Brown50526df2005-08-11 17:32:05 -04001005 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1012 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1017 if (result < 0) {
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025subsys_initcall(acpi_ec_init);
1026
1027/* EC driver currently not unloadable */
1028#if 0
Len Brown50526df2005-08-11 17:32:05 -04001029static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1033
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1035
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
Len Brown50526df2005-08-11 17:32:05 -04001038#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040static int __init acpi_fake_ecdt_setup(char *str)
1041{
1042 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001043 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001047static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001048{
Len Brown02b28a32005-12-05 16:33:04 -05001049 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001050
Len Brown02b28a32005-12-05 16:33:04 -05001051 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001052 return 0;
1053
Len Brown02b28a32005-12-05 16:33:04 -05001054 if (intr) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001055 acpi_ec_mode = EC_INTR;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001056 } else {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001057 acpi_ec_mode = EC_POLL;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001058 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001059 acpi_ec_driver.ops.add = acpi_ec_add;
1060 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1061
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001062 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001063}
Len Brown50526df2005-08-11 17:32:05 -04001064
Len Brown53f11d42005-12-05 16:46:36 -05001065__setup("ec_intr=", acpi_ec_set_intr_mode);