blob: c816b4eab50d63ecc5765792834b9b359955c822 [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 */
75 EC_POLL, /* Input buffer empty */
76};
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. Sadykov703959d2006-09-26 19:50:33 +040094struct acpi_ec {
95 acpi_handle handle;
96 unsigned long uid;
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
102 struct semaphore sem;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 wait_queue_head_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400108/* If we find an EC via the ECDT, we need to keep a ptr to its context */
109static struct acpi_ec *ec_ecdt;
110
111/* External interfaces use first EC only, so remember */
112static struct acpi_device *first_ec;
113static int acpi_ec_mode = EC_INTR;
114
115static int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400116 const u8 *wdata, unsigned wdata_len,
117 u8 *rdata, unsigned rdata_len);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400118static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400119 const u8 *wdata, unsigned wdata_len,
120 u8 *rdata, unsigned rdata_len);
Len Brown02b28a32005-12-05 16:33:04 -0500121static void acpi_ec_gpe_poll_query(void *ec_cxt);
122static void acpi_ec_gpe_intr_query(void *ec_cxt);
123static u32 acpi_ec_gpe_poll_handler(void *data);
124static u32 acpi_ec_gpe_intr_handler(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
129
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130static u32 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Len Brown50526df2005-08-11 17:32:05 -0400132 u32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400134 acpi_hw_low_level_read(8, &status, &ec->status_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500135 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400138static u32 acpi_ec_read_data(struct acpi_ec *ec)
139{
140 u32 data = 0;
141
142 acpi_hw_low_level_read(8, &data, &ec->data_addr);
143 return data;
144}
145
146static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
147{
148 acpi_hw_low_level_write(8, command, &ec->command_addr);
149}
150
151static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
152{
153 acpi_hw_low_level_write(8, data, &ec->data_addr);
154}
155
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400156static int acpi_ec_check_status(u32 status, u8 event) {
157
158 switch (event) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400159 case ACPI_EC_EVENT_OBF_1:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400160 if (status & ACPI_EC_FLAG_OBF)
161 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400162 break;
163 case ACPI_EC_EVENT_IBF_0:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400164 if (!(status & ACPI_EC_FLAG_IBF))
165 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400166 break;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400167 default:
168 break;
169 }
170
171 return 0;
172}
173
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400174static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400175{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400176 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
177 long time_left;
Luming Yu45bea152005-07-23 04:08:00 -0400178
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400179 ec->expect_event = event;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400180 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400181 ec->expect_event = 0;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400182 return 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500183 }
184
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400185 do {
186 if (acpi_ec_mode == EC_POLL) {
187 udelay(ACPI_EC_UDELAY);
188 } else {
189 time_left = wait_event_timeout(ec->wait,
190 !ec->expect_event,
191 msecs_to_jiffies(ACPI_EC_DELAY));
192 if (time_left > 0) {
193 ec->expect_event = 0;
194 return 0;
195 }
196 }
197 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
198 ec->expect_event = 0;
199 return 0;
200 }
201 } while (--i > 0);
202
203 ec->expect_event = 0;
204
Patrick Mocheld550d982006-06-27 00:41:40 -0400205 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500206}
207
Len Brown02b28a32005-12-05 16:33:04 -0500208#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400209/*
210 * Note: samsung nv5000 doesn't work with ec burst mode.
211 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
212 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400213int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500214{
Len Brown50526df2005-08-11 17:32:05 -0400215 u32 tmp = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400216 u32 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500217
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500218
219 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400220 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400221 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Len Brown50526df2005-08-11 17:32:05 -0400222 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400223 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400224 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
225 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
226 tmp = acpi_ec_read_data(ec);
Len Brown50526df2005-08-11 17:32:05 -0400227 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400228 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500229 }
Luming Yu668d74c2005-07-23 00:26:33 -0400230 }
231
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400232 atomic_set(&ec->leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400233 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400234 end:
235 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500237}
238
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400239int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500240{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400241 u32 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500242
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500243
Luming Yu06a2a382005-09-27 00:43:00 -0400244 status = acpi_ec_read_status(ec);
245 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400246 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu06a2a382005-09-27 00:43:00 -0400247 if(status)
248 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400249 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
250 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400251 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400252 atomic_set(&ec->leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400254 end:
255 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500257}
Len Brown02b28a32005-12-05 16:33:04 -0500258#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400260static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400261 const u8 *wdata, unsigned wdata_len,
262 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400263{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400264 if (acpi_ec_mode == EC_POLL)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400265 return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400266 else
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400267 return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
268}
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400269static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400270{
271 int result;
272 u8 d;
273 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
274 *data = d;
275 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400276}
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400277static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
Luming Yu45bea152005-07-23 04:08:00 -0400278{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400279 u8 wdata[2] = { address, data };
280 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
Luming Yu45bea152005-07-23 04:08:00 -0400281}
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400282
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400283static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
284 const u8 *wdata, unsigned wdata_len,
285 u8 *rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400286{
287 int result;
288
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400289 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400290
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400291 for (; wdata_len > 0; wdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400292 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400293 if (result)
294 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400295
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400296 acpi_ec_write_data(ec, *(wdata++));
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400297 }
298
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400299 if (command == ACPI_EC_COMMAND_WRITE) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400300 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400301 if (result)
302 return result;
303 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400304
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400305 for (; rdata_len > 0; rdata_len --) {
306 u32 d;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400307
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400308 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400309 if (result)
310 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400311
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400312 d = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400313 *(rdata++) = (u8) d;
314 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400315
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400316 return 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400317}
318
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400319static int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400320 const u8 *wdata, unsigned wdata_len,
321 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400322{
Len Brown50526df2005-08-11 17:32:05 -0400323 acpi_status status = AE_OK;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400324 int result;
Len Brown50526df2005-08-11 17:32:05 -0400325 u32 glk = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400326
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400327 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400328 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400329
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400330 if (rdata)
331 memset(rdata, 0, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400332
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400333 if (ec->global_lock) {
Luming Yu45bea152005-07-23 04:08:00 -0400334 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
335 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400336 return -ENODEV;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400337 }
Luming Yu45bea152005-07-23 04:08:00 -0400338
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400339 if (down_interruptible(&ec->sem)) {
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500340 result = -ERESTARTSYS;
341 goto end_nosem;
342 }
Luming Yu45bea152005-07-23 04:08:00 -0400343
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400344 result = acpi_ec_transaction_unlocked(ec, command,
345 wdata, wdata_len,
346 rdata, rdata_len);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400347 up(&ec->sem);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400348
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500349end_nosem:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400350 if (ec->global_lock)
Luming Yu45bea152005-07-23 04:08:00 -0400351 acpi_release_global_lock(glk);
352
Patrick Mocheld550d982006-06-27 00:41:40 -0400353 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400354}
355
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400356static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400357 const u8 *wdata, unsigned wdata_len,
358 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400359{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400360 int status;
Len Brown50526df2005-08-11 17:32:05 -0400361 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400363 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400366 if (rdata)
367 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400369 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
371 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500374
375 WARN_ON(in_interrupt());
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400376 down(&ec->sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500377
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400378 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu716e0842005-08-10 01:40:00 -0400379 if (status) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400380 ACPI_EXCEPTION((AE_INFO, status, "read EC, IB not empty"));
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500381 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400384 status = acpi_ec_transaction_unlocked(ec, command,
385 wdata, wdata_len,
386 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400387
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400388end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400389 up(&ec->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400391 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 acpi_release_global_lock(glk);
393
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * Externally callable EC access functions. For now, assume 1 EC only
399 */
Len Brown50526df2005-08-11 17:32:05 -0400400int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400402 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int err;
404 u32 temp_data;
405
406 if (!first_ec)
407 return -ENODEV;
408
409 ec = acpi_driver_data(first_ec);
410
411 err = acpi_ec_read(ec, addr, &temp_data);
412
413 if (!err) {
414 *val = temp_data;
415 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400416 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return err;
418}
Len Brown50526df2005-08-11 17:32:05 -0400419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420EXPORT_SYMBOL(ec_read);
421
Len Brown50526df2005-08-11 17:32:05 -0400422int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400424 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int err;
426
427 if (!first_ec)
428 return -ENODEV;
429
430 ec = acpi_driver_data(first_ec);
431
432 err = acpi_ec_write(ec, addr, val);
433
434 return err;
435}
Len Brown50526df2005-08-11 17:32:05 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437EXPORT_SYMBOL(ec_write);
438
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400439extern int ec_transaction(u8 command,
440 const u8 *wdata, unsigned wdata_len,
441 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400442{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400443 struct acpi_ec *ec;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400444
445 if (!first_ec)
446 return -ENODEV;
447
448 ec = acpi_driver_data(first_ec);
449
450 return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400451}
Luming Yu45bea152005-07-23 04:08:00 -0400452
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400453EXPORT_SYMBOL(ec_transaction);
Luming Yu45bea152005-07-23 04:08:00 -0400454
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400455static int acpi_ec_query(struct acpi_ec *ec, u32 * data) {
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400456 int result;
457 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400458
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400459 if (!ec || !data)
460 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400461
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400462 /*
463 * Query the EC to find out which _Qxx method we need to evaluate.
464 * Note that successful completion of the query causes the ACPI_EC_SCI
465 * bit to be cleared (and thus clearing the interrupt source).
466 */
Luming Yu45bea152005-07-23 04:08:00 -0400467
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400468 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
469 if (result)
470 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400471
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400472 if (!d)
473 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400474
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400475 *data = d;
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/* --------------------------------------------------------------------------
480 Event Management
481 -------------------------------------------------------------------------- */
482
Luming Yu45bea152005-07-23 04:08:00 -0400483union acpi_ec_query_data {
Len Brown50526df2005-08-11 17:32:05 -0400484 acpi_handle handle;
485 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486};
487
Len Brown50526df2005-08-11 17:32:05 -0400488static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400490 if (acpi_ec_mode == EC_POLL)
Len Brown02b28a32005-12-05 16:33:04 -0500491 acpi_ec_gpe_poll_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400492 else
Len Brown02b28a32005-12-05 16:33:04 -0500493 acpi_ec_gpe_intr_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400494}
495
Len Brown02b28a32005-12-05 16:33:04 -0500496static void acpi_ec_gpe_poll_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400497{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400498 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Len Brown50526df2005-08-11 17:32:05 -0400499 u32 value = 0;
Len Brown50526df2005-08-11 17:32:05 -0400500 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
501 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
502 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
503 };
Luming Yu45bea152005-07-23 04:08:00 -0400504
Luming Yu45bea152005-07-23 04:08:00 -0400505
506 if (!ec_cxt)
507 goto end;
508
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400509 if (down_interruptible (&ec->sem)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500511 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400512 value = acpi_ec_read_status(ec);
513 up(&ec->sem);
Luming Yu45bea152005-07-23 04:08:00 -0400514
515 /* TBD: Implement asynch events!
516 * NOTE: All we care about are EC-SCI's. Other EC events are
517 * handled via polling (yuck!). This is because some systems
518 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
519 * a purely interrupt-driven approach (grumble, grumble).
520 */
521 if (!(value & ACPI_EC_FLAG_SCI))
522 goto end;
523
524 if (acpi_ec_query(ec, &value))
525 goto end;
526
527 object_name[2] = hex[((value >> 4) & 0x0F)];
528 object_name[3] = hex[(value & 0x0F)];
529
530 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
531
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400532 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400533
Len Brown50526df2005-08-11 17:32:05 -0400534 end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400535 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Luming Yu45bea152005-07-23 04:08:00 -0400536}
Len Brown02b28a32005-12-05 16:33:04 -0500537static void acpi_ec_gpe_intr_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400538{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400539 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Len Brown50526df2005-08-11 17:32:05 -0400540 u32 value;
541 int result = -ENODATA;
542 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
543 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
544 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
545 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500548 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
549 result = acpi_ec_query(ec, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500551 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto end;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 object_name[2] = hex[((value >> 4) & 0x0F)];
555 object_name[3] = hex[(value & 0x0F)];
556
557 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
558
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400559 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Len Brown50526df2005-08-11 17:32:05 -0400560 end:
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Len Brown50526df2005-08-11 17:32:05 -0400564static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400566 if (acpi_ec_mode == EC_POLL)
Len Brown02b28a32005-12-05 16:33:04 -0500567 return acpi_ec_gpe_poll_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400568 else
Len Brown02b28a32005-12-05 16:33:04 -0500569 return acpi_ec_gpe_intr_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400570}
Len Brown02b28a32005-12-05 16:33:04 -0500571static u32 acpi_ec_gpe_poll_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400572{
Len Brown50526df2005-08-11 17:32:05 -0400573 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400574 struct acpi_ec *ec = (struct acpi_ec *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (!ec)
577 return ACPI_INTERRUPT_NOT_HANDLED;
578
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400579 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Luming Yu45bea152005-07-23 04:08:00 -0400580
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400581 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400582
583 if (status == AE_OK)
584 return ACPI_INTERRUPT_HANDLED;
585 else
586 return ACPI_INTERRUPT_NOT_HANDLED;
587}
Len Brown02b28a32005-12-05 16:33:04 -0500588static u32 acpi_ec_gpe_intr_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400589{
Len Brown50526df2005-08-11 17:32:05 -0400590 acpi_status status = AE_OK;
591 u32 value;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400592 struct acpi_ec *ec = (struct acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400593
594 if (!ec)
595 return ACPI_INTERRUPT_NOT_HANDLED;
596
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400597 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500598 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400600 switch (ec->expect_event) {
601 case ACPI_EC_EVENT_OBF_1:
Luming Yu716e0842005-08-10 01:40:00 -0400602 if (!(value & ACPI_EC_FLAG_OBF))
603 break;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400604 ec->expect_event = 0;
605 wake_up(&ec->wait);
Vladimir Lebedev49fee9812006-06-20 16:46:00 -0400606 break;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400607 case ACPI_EC_EVENT_IBF_0:
Luming Yu716e0842005-08-10 01:40:00 -0400608 if ((value & ACPI_EC_FLAG_IBF))
609 break;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400610 ec->expect_event = 0;
611 wake_up(&ec->wait);
Vladimir Lebedev49fee9812006-06-20 16:46:00 -0400612 break;
Luming Yu716e0842005-08-10 01:40:00 -0400613 default:
614 break;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500615 }
616
Len Brown50526df2005-08-11 17:32:05 -0400617 if (value & ACPI_EC_FLAG_SCI) {
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400618 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
Len Brown50526df2005-08-11 17:32:05 -0400619 acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400620 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400621 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
622 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400623 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500624 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400625 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/* --------------------------------------------------------------------------
629 Address Space Management
630 -------------------------------------------------------------------------- */
631
632static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400633acpi_ec_space_setup(acpi_handle region_handle,
634 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 /*
637 * The EC object is in the handler context and is needed
638 * when calling the acpi_ec_space_handler.
639 */
Len Brown50526df2005-08-11 17:32:05 -0400640 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
641 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 return AE_OK;
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400647acpi_ec_space_handler(u32 function,
648 acpi_physical_address address,
649 u32 bit_width,
650 acpi_integer * value,
651 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Len Brown50526df2005-08-11 17:32:05 -0400653 int result = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400654 struct acpi_ec *ec = NULL;
Len Brown50526df2005-08-11 17:32:05 -0400655 u64 temp = *value;
656 acpi_integer f_v = 0;
657 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Luming Yufa9cd542005-03-19 01:54:47 -0500663 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400667 ec = (struct acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Len Brown50526df2005-08-11 17:32:05 -0400669 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 switch (function) {
671 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500672 temp = 0;
Len Brown50526df2005-08-11 17:32:05 -0400673 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
675 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500676 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 default:
679 result = -EINVAL;
680 goto out;
681 break;
682 }
683
684 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500685 if (bit_width) {
686 if (function == ACPI_READ)
687 f_v |= temp << 8 * i;
688 if (function == ACPI_WRITE)
689 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500691 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto next_byte;
693 }
694
Luming Yufa9cd542005-03-19 01:54:47 -0500695 if (function == ACPI_READ) {
696 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *value = f_v;
698 }
699
Len Brown50526df2005-08-11 17:32:05 -0400700 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 switch (result) {
702 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400703 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400712 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/* --------------------------------------------------------------------------
717 FS Interface (/proc)
718 -------------------------------------------------------------------------- */
719
Len Brown50526df2005-08-11 17:32:05 -0400720static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Len Brown50526df2005-08-11 17:32:05 -0400722static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400724 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (!ec)
728 goto end;
729
730 seq_printf(seq, "gpe bit: 0x%02x\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400731 (u32) ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400733 (u32) ec->status_addr.address,
734 (u32) ec->data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400736 ec->global_lock ? "yes" : "no");
737 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Len Brown50526df2005-08-11 17:32:05 -0400739 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
744{
745 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
746}
747
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400748static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400749 .open = acpi_ec_info_open_fs,
750 .read = seq_read,
751 .llseek = seq_lseek,
752 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .owner = THIS_MODULE,
754};
755
Len Brown50526df2005-08-11 17:32:05 -0400756static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Len Brown50526df2005-08-11 17:32:05 -0400758 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!acpi_device_dir(device)) {
762 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400763 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400765 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400769 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400771 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 else {
773 entry->proc_fops = &acpi_ec_info_ops;
774 entry->data = acpi_driver_data(device);
775 entry->owner = THIS_MODULE;
776 }
777
Patrick Mocheld550d982006-06-27 00:41:40 -0400778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Len Brown50526df2005-08-11 17:32:05 -0400781static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (acpi_device_dir(device)) {
785 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
786 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
787 acpi_device_dir(device) = NULL;
788 }
789
Patrick Mocheld550d982006-06-27 00:41:40 -0400790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/* --------------------------------------------------------------------------
794 Driver Interface
795 -------------------------------------------------------------------------- */
796
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400797static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Len Brown50526df2005-08-11 17:32:05 -0400799 int result = 0;
800 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
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400807 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400809 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400810 memset(ec, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400812 ec->handle = device->handle;
813 ec->uid = -1;
814 init_MUTEX(&ec->sem);
815 if (acpi_ec_mode == EC_INTR) {
816 atomic_set(&ec->leaving_burst, 1);
817 init_waitqueue_head(&ec->wait);
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
820 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
821 acpi_driver_data(device) = ec;
822
823 /* Use the global lock for all EC transactions? */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400824 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
825 &ec->global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500827 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
828 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
829 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400831 ACPI_ADR_SPACE_EC,
832 &acpi_ec_space_handler);
833
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400834 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400835 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 kfree(ec_ecdt);
838 }
839
840 /* Get GPE bit assignment (EC events). */
841 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400842 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400843 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
844 &ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400846 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 result = -ENODEV;
848 goto end;
849 }
850
851 result = acpi_ec_add_fs(device);
852 if (result)
853 goto end;
854
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400855 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Len Brown50526df2005-08-11 17:32:05 -0400856 acpi_device_name(device), acpi_device_bid(device),
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400857 (u32) ec->gpe_bit));
Luming Yu45bea152005-07-23 04:08:00 -0400858
859 if (!first_ec)
860 first_ec = device;
861
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400862 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (result)
864 kfree(ec);
865
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Len Brown50526df2005-08-11 17:32:05 -0400869static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400871 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 ec = acpi_driver_data(device);
878
879 acpi_ec_remove_fs(device);
880
881 kfree(ec);
882
Patrick Mocheld550d982006-06-27 00:41:40 -0400883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400887acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400889 struct acpi_ec *ec = (struct acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct acpi_generic_address *addr;
891
Bob Moore50eca3e2005-09-30 19:03:00 -0400892 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return AE_OK;
894 }
895
896 /*
897 * The first address region returned is the data port, and
898 * the second address region returned is the status/command
899 * port.
900 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400901 if (ec->data_addr.register_bit_width == 0) {
902 addr = &ec->data_addr;
903 } else if (ec->command_addr.register_bit_width == 0) {
904 addr = &ec->command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else {
906 return AE_CTRL_TERMINATE;
907 }
908
909 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
910 addr->register_bit_width = 8;
911 addr->register_bit_offset = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400912 addr->address = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 return AE_OK;
915}
916
Len Brown50526df2005-08-11 17:32:05 -0400917static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Len Brown50526df2005-08-11 17:32:05 -0400919 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400920 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 ec = acpi_driver_data(device);
927
928 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /*
932 * Get I/O port addresses. Convert to GAS format.
933 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400934 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400935 acpi_ec_io_ports, ec);
936 if (ACPI_FAILURE(status)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400937 || ec->command_addr.register_bit_width == 0) {
938 ACPI_EXCEPTION((AE_INFO, status,
939 "Error getting I/O port addresses"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400940 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400943 ec->status_addr = ec->command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400945 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
946 (u32) ec->gpe_bit,
947 (u32) ec->command_addr.address,
948 (u32) ec->data_addr.address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * Install GPE handler
952 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400953 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400954 ACPI_GPE_EDGE_TRIGGERED,
955 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400957 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400959 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
960 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400962 status = acpi_install_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400963 ACPI_ADR_SPACE_EC,
964 &acpi_ec_space_handler,
965 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400967 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400968 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Patrick Mocheld550d982006-06-27 00:41:40 -0400972 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Len Brown50526df2005-08-11 17:32:05 -0400975static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Len Brown50526df2005-08-11 17:32:05 -0400977 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400978 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 ec = acpi_driver_data(device);
985
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400986 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400987 ACPI_ADR_SPACE_EC,
988 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400990 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Len Brown50526df2005-08-11 17:32:05 -0400992 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400993 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400994 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400996 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -04001002acpi_fake_ecdt_callback(acpi_handle handle,
1003 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Len Brown50526df2005-08-11 17:32:05 -04001005 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001007 init_MUTEX(&ec_ecdt->sem);
1008 if (acpi_ec_mode == EC_INTR) {
1009 init_waitqueue_head(&ec_ecdt->wait);
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001012 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (ACPI_FAILURE(status))
1014 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001015 ec_ecdt->status_addr = ec_ecdt->command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001017 ec_ecdt->uid = -1;
1018 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Len Brown50526df2005-08-11 17:32:05 -04001020 status =
1021 acpi_evaluate_integer(handle, "_GPE", NULL,
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001022 &ec_ecdt->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ACPI_FAILURE(status))
1024 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001025 ec_ecdt->global_lock = TRUE;
1026 ec_ecdt->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001028 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
1029 (u32) ec_ecdt->gpe_bit,
1030 (u32) ec_ecdt->command_addr.address,
1031 (u32) ec_ecdt->data_addr.address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 return AE_CTRL_TERMINATE;
1034}
1035
1036/*
1037 * Some BIOS (such as some from Gateway laptops) access EC region very early
1038 * such as in BAT0._INI or EC._INI before an EC device is found and
1039 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1040 * required, but if EC regison is accessed early, it is required.
1041 * The routine tries to workaround the BIOS bug by pre-scan EC device
1042 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1043 * op region (since _REG isn't invoked yet). The assumption is true for
1044 * all systems found.
1045 */
Len Brown50526df2005-08-11 17:32:05 -04001046static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Len Brown50526df2005-08-11 17:32:05 -04001048 acpi_status status;
1049 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001051 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001053 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!ec_ecdt) {
1055 ret = -ENOMEM;
1056 goto error;
1057 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001058 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Len Brown50526df2005-08-11 17:32:05 -04001060 status = acpi_get_devices(ACPI_EC_HID,
1061 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ACPI_FAILURE(status)) {
1063 kfree(ec_ecdt);
1064 ec_ecdt = NULL;
1065 ret = -ENODEV;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001066 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto error;
1068 }
1069 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001070 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return ret;
1072}
1073
Len Brown50526df2005-08-11 17:32:05 -04001074static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Len Brown50526df2005-08-11 17:32:05 -04001076 acpi_status status;
1077 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -04001078
Len Brown50526df2005-08-11 17:32:05 -04001079 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1080 (struct acpi_table_header **)
1081 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -04001082 if (ACPI_FAILURE(status))
1083 return -ENODEV;
1084
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001085 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
Luming Yu45bea152005-07-23 04:08:00 -04001086
1087 /*
1088 * Generate a temporary ec context to use until the namespace is scanned
1089 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001090 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Luming Yu45bea152005-07-23 04:08:00 -04001091 if (!ec_ecdt)
1092 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001093 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Luming Yu45bea152005-07-23 04:08:00 -04001094
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001095 init_MUTEX(&ec_ecdt->sem);
1096 if (acpi_ec_mode == EC_INTR) {
1097 init_waitqueue_head(&ec_ecdt->wait);
1098 }
1099 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1100 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1101 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1102 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
Luming Yu45bea152005-07-23 04:08:00 -04001103 /* use the GL just to be safe */
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001104 ec_ecdt->global_lock = TRUE;
1105 ec_ecdt->uid = ecdt_ptr->uid;
Luming Yu45bea152005-07-23 04:08:00 -04001106
Len Brown50526df2005-08-11 17:32:05 -04001107 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001108 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
Luming Yu45bea152005-07-23 04:08:00 -04001109 if (ACPI_FAILURE(status)) {
1110 goto error;
1111 }
1112
1113 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001114 error:
1115 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 kfree(ec_ecdt);
1117 ec_ecdt = NULL;
1118
1119 return -ENODEV;
1120}
1121
1122static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -04001123int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Len Brown50526df2005-08-11 17:32:05 -04001125 acpi_status status;
1126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 ret = acpi_ec_get_real_ecdt();
1129 /* Try to make a fake ECDT */
1130 if (ret && acpi_fake_ecdt_enabled) {
1131 ret = acpi_ec_fake_ecdt();
1132 }
1133
1134 if (ret)
1135 return 0;
1136
1137 /*
1138 * Install GPE handler
1139 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001140 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001141 ACPI_GPE_EDGE_TRIGGERED,
1142 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (ACPI_FAILURE(status)) {
1144 goto error;
1145 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001146 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1147 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Len Brown50526df2005-08-11 17:32:05 -04001149 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1150 ACPI_ADR_SPACE_EC,
1151 &acpi_ec_space_handler,
1152 &acpi_ec_space_setup,
1153 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001155 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001156 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 goto error;
1158 }
1159
1160 return 0;
1161
Len Brown50526df2005-08-11 17:32:05 -04001162 error:
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001163 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 kfree(ec_ecdt);
1165 ec_ecdt = NULL;
1166
1167 return -ENODEV;
1168}
1169
Len Brown50526df2005-08-11 17:32:05 -04001170static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Len Brown50526df2005-08-11 17:32:05 -04001172 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1179 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /* Now register the driver for the EC */
1183 result = acpi_bus_register_driver(&acpi_ec_driver);
1184 if (result < 0) {
1185 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001186 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Patrick Mocheld550d982006-06-27 00:41:40 -04001189 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192subsys_initcall(acpi_ec_init);
1193
1194/* EC driver currently not unloadable */
1195#if 0
Len Brown50526df2005-08-11 17:32:05 -04001196static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 acpi_bus_unregister_driver(&acpi_ec_driver);
1200
1201 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1202
Patrick Mocheld550d982006-06-27 00:41:40 -04001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Len Brown50526df2005-08-11 17:32:05 -04001205#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207static int __init acpi_fake_ecdt_setup(char *str)
1208{
1209 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001210 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001214static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001215{
Len Brown02b28a32005-12-05 16:33:04 -05001216 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001217
Len Brown02b28a32005-12-05 16:33:04 -05001218 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001219 return 0;
1220
Len Brown02b28a32005-12-05 16:33:04 -05001221 if (intr) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001222 acpi_ec_mode = EC_INTR;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001223 } else {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001224 acpi_ec_mode = EC_POLL;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001225 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001226 acpi_ec_driver.ops.add = acpi_ec_add;
1227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1228
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001229 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001230}
Len Brown50526df2005-08-11 17:32:05 -04001231
Len Brown53f11d42005-12-05 16:46:36 -05001232__setup("ec_intr=", acpi_ec_set_intr_mode);