blob: b6f935d0c3adf6ea4ce7a434872b31f56bff33bc [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
49#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050050#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
53#define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050054#define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
Len Brown50526df2005-08-11 17:32:05 -040056#define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
57#define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define ACPI_EC_COMMAND_READ 0x80
59#define ACPI_EC_COMMAND_WRITE 0x81
Dmitry Torokhov451566f2005-03-19 01:10:05 -050060#define ACPI_EC_BURST_ENABLE 0x82
61#define ACPI_EC_BURST_DISABLE 0x83
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define ACPI_EC_COMMAND_QUERY 0x84
Len Brown02b28a332005-12-05 16:33:04 -050063#define EC_POLL 0xFF
64#define EC_INTR 0x00
Len Brown50526df2005-08-11 17:32:05 -040065static int acpi_ec_remove(struct acpi_device *device, int type);
66static int acpi_ec_start(struct acpi_device *device);
67static int acpi_ec_stop(struct acpi_device *device, int type);
Len Brown02b28a332005-12-05 16:33:04 -050068static int acpi_ec_intr_add(struct acpi_device *device);
69static int acpi_ec_poll_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static struct acpi_driver acpi_ec_driver = {
Len Brown50526df2005-08-11 17:32:05 -040072 .name = ACPI_EC_DRIVER_NAME,
73 .class = ACPI_EC_CLASS,
74 .ids = ACPI_EC_HID,
75 .ops = {
Len Brown53f11d42005-12-05 16:46:36 -050076 .add = acpi_ec_intr_add,
Len Brown50526df2005-08-11 17:32:05 -040077 .remove = acpi_ec_remove,
78 .start = acpi_ec_start,
79 .stop = acpi_ec_stop,
80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
Luming Yu45bea152005-07-23 04:08:00 -040082union acpi_ec {
83 struct {
Len Brown50526df2005-08-11 17:32:05 -040084 u32 mode;
85 acpi_handle handle;
86 unsigned long uid;
87 unsigned long gpe_bit;
88 struct acpi_generic_address status_addr;
89 struct acpi_generic_address command_addr;
90 struct acpi_generic_address data_addr;
91 unsigned long global_lock;
Luming Yu45bea152005-07-23 04:08:00 -040092 } common;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Luming Yu45bea152005-07-23 04:08:00 -040094 struct {
Len Brown50526df2005-08-11 17:32:05 -040095 u32 mode;
96 acpi_handle handle;
97 unsigned long uid;
98 unsigned long gpe_bit;
99 struct acpi_generic_address status_addr;
100 struct acpi_generic_address command_addr;
101 struct acpi_generic_address data_addr;
102 unsigned long global_lock;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400103 u8 expect_event;
Len Brown50526df2005-08-11 17:32:05 -0400104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 atomic_t pending_gpe;
106 struct semaphore sem;
107 wait_queue_head_t wait;
Len Brown02b28a332005-12-05 16:33:04 -0500108 } intr;
Luming Yu45bea152005-07-23 04:08:00 -0400109
110 struct {
Len Brown50526df2005-08-11 17:32:05 -0400111 u32 mode;
112 acpi_handle handle;
113 unsigned long uid;
114 unsigned long gpe_bit;
115 struct acpi_generic_address status_addr;
116 struct acpi_generic_address command_addr;
117 struct acpi_generic_address data_addr;
118 unsigned long global_lock;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500119 struct semaphore sem;
Len Brown02b28a332005-12-05 16:33:04 -0500120 } poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Len Brown02b28a332005-12-05 16:33:04 -0500123static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400124static int acpi_ec_intr_wait(union acpi_ec *ec, u8 event);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400125static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
126 const u8 *wdata, unsigned wdata_len,
127 u8 *rdata, unsigned rdata_len);
128static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
129 const u8 *wdata, unsigned wdata_len,
130 u8 *rdata, unsigned rdata_len);
Len Brown02b28a332005-12-05 16:33:04 -0500131static void acpi_ec_gpe_poll_query(void *ec_cxt);
132static void acpi_ec_gpe_intr_query(void *ec_cxt);
133static u32 acpi_ec_gpe_poll_handler(void *data);
134static u32 acpi_ec_gpe_intr_handler(void *data);
Luming Yu45bea152005-07-23 04:08:00 -0400135static acpi_status __init
Len Brown02b28a332005-12-05 16:33:04 -0500136acpi_fake_ecdt_poll_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -0400137 u32 Level, void *context, void **retval);
Luming Yu45bea152005-07-23 04:08:00 -0400138
139static acpi_status __init
Len Brown02b28a332005-12-05 16:33:04 -0500140acpi_fake_ecdt_intr_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -0400141 u32 Level, void *context, void **retval);
Luming Yu45bea152005-07-23 04:08:00 -0400142
Len Brown02b28a332005-12-05 16:33:04 -0500143static int __init acpi_ec_poll_get_real_ecdt(void);
144static int __init acpi_ec_intr_get_real_ecdt(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Len Brown50526df2005-08-11 17:32:05 -0400146static union acpi_ec *ec_ecdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* External interfaces use first EC only, so remember */
149static struct acpi_device *first_ec;
Len Brown53f11d42005-12-05 16:46:36 -0500150static int acpi_ec_poll_mode = EC_INTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/* --------------------------------------------------------------------------
153 Transaction Management
154 -------------------------------------------------------------------------- */
155
Arjan van de Ven858119e2006-01-14 13:20:43 -0800156static u32 acpi_ec_read_status(union acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Len Brown50526df2005-08-11 17:32:05 -0400158 u32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Luming Yu45bea152005-07-23 04:08:00 -0400160 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500161 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400164static int acpi_ec_check_status(u32 status, u8 event) {
165
166 switch (event) {
167 case ACPI_EC_EVENT_OBF:
168 if (status & ACPI_EC_FLAG_OBF)
169 return 1;
170 case ACPI_EC_EVENT_IBE:
171 if (!(status & ACPI_EC_FLAG_IBF))
172 return 1;
173 default:
174 break;
175 }
176
177 return 0;
178}
179
Len Brown50526df2005-08-11 17:32:05 -0400180static int acpi_ec_wait(union acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400181{
Len Brown02b28a332005-12-05 16:33:04 -0500182 if (acpi_ec_poll_mode)
183 return acpi_ec_poll_wait(ec, event);
Luming Yu45bea152005-07-23 04:08:00 -0400184 else
Len Brown02b28a332005-12-05 16:33:04 -0500185 return acpi_ec_intr_wait(ec, event);
Luming Yu45bea152005-07-23 04:08:00 -0400186}
187
Len Brown02b28a332005-12-05 16:33:04 -0500188static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400189{
Len Brown50526df2005-08-11 17:32:05 -0400190 u32 acpi_ec_status = 0;
191 u32 i = ACPI_EC_UDELAY_COUNT;
Luming Yu45bea152005-07-23 04:08:00 -0400192
193 if (!ec)
194 return -EINVAL;
195
196 /* Poll the EC status register waiting for the event to occur. */
197 switch (event) {
198 case ACPI_EC_EVENT_OBF:
199 do {
Len Brown50526df2005-08-11 17:32:05 -0400200 acpi_hw_low_level_read(8, &acpi_ec_status,
201 &ec->common.status_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400202 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
203 return 0;
204 udelay(ACPI_EC_UDELAY);
Len Brown50526df2005-08-11 17:32:05 -0400205 } while (--i > 0);
Luming Yu45bea152005-07-23 04:08:00 -0400206 break;
207 case ACPI_EC_EVENT_IBE:
208 do {
Len Brown50526df2005-08-11 17:32:05 -0400209 acpi_hw_low_level_read(8, &acpi_ec_status,
210 &ec->common.status_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400211 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
212 return 0;
213 udelay(ACPI_EC_UDELAY);
Len Brown50526df2005-08-11 17:32:05 -0400214 } while (--i > 0);
Luming Yu45bea152005-07-23 04:08:00 -0400215 break;
216 default:
217 return -EINVAL;
218 }
219
220 return -ETIME;
221}
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500222
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400223static int acpi_ec_intr_wait(union acpi_ec *ec, u8 event)
224{
225 long time_left;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500226
Len Brown02b28a332005-12-05 16:33:04 -0500227 ec->intr.expect_event = event;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500228
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400229 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
Len Brown02b28a332005-12-05 16:33:04 -0500230 ec->intr.expect_event = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400231 return 0;
Luming Yu716e0842005-08-10 01:40:00 -0400232 }
233
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400234 time_left = wait_event_timeout(ec->intr.wait, !ec->intr.expect_event,
235 msecs_to_jiffies(ACPI_EC_DELAY));
Len Brown50526df2005-08-11 17:32:05 -0400236
Len Brown02b28a332005-12-05 16:33:04 -0500237 ec->intr.expect_event = 0;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400238 if (time_left <= 0) {
239 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
240 return 0;
241 }
242 } else {
243 return 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500244 }
245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500247}
248
Len Brown02b28a332005-12-05 16:33:04 -0500249#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400250/*
251 * Note: samsung nv5000 doesn't work with ec burst mode.
252 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
253 */
254int acpi_ec_enter_burst_mode(union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500255{
Len Brown50526df2005-08-11 17:32:05 -0400256 u32 tmp = 0;
257 int status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500258
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500259
260 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400261 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Luming Yu716e0842005-08-10 01:40:00 -0400262 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Len Brown50526df2005-08-11 17:32:05 -0400263 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400264 goto end;
Len Brown50526df2005-08-11 17:32:05 -0400265 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
266 &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
Luming Yu45bea152005-07-23 04:08:00 -0400268 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
Len Brown50526df2005-08-11 17:32:05 -0400269 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400270 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500271 }
Luming Yu668d74c2005-07-23 00:26:33 -0400272 }
273
Len Brown02b28a332005-12-05 16:33:04 -0500274 atomic_set(&ec->intr.leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400276 end:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400277 ACPI_EXCEPTION ((AE_INFO, status, "EC wait, burst mode");
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500279}
280
Luming Yu06a2a382005-09-27 00:43:00 -0400281int acpi_ec_leave_burst_mode(union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500282{
Luming Yu06a2a382005-09-27 00:43:00 -0400283 int status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500284
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500285
Luming Yu06a2a382005-09-27 00:43:00 -0400286 status = acpi_ec_read_status(ec);
287 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
288 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
289 if(status)
290 goto end;
291 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
292 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400293 }
Len Brown02b28a332005-12-05 16:33:04 -0500294 atomic_set(&ec->intr.leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400295 return 0;
Luming Yu06a2a382005-09-27 00:43:00 -0400296end:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400297 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode");
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500299}
Len Brown02b28a332005-12-05 16:33:04 -0500300#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400302static int acpi_ec_transaction(union acpi_ec *ec, u8 command,
303 const u8 *wdata, unsigned wdata_len,
304 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400305{
Len Brown02b28a332005-12-05 16:33:04 -0500306 if (acpi_ec_poll_mode)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400307 return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400308 else
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400309 return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
310}
311static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
312{
313 int result;
314 u8 d;
315 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
316 *data = d;
317 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400318}
Len Brown50526df2005-08-11 17:32:05 -0400319static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
Luming Yu45bea152005-07-23 04:08:00 -0400320{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400321 u8 wdata[2] = { address, data };
322 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
Luming Yu45bea152005-07-23 04:08:00 -0400323}
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400324
325static int acpi_ec_transaction_unlocked(union acpi_ec *ec, u8 command,
326 const u8 *wdata, unsigned wdata_len,
327 u8 *rdata, unsigned rdata_len)
328{
329 int result;
330
331 acpi_hw_low_level_write(8, command, &ec->common.command_addr);
332
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400333 for (; wdata_len > 0; wdata_len --) {
334 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
335 if (result)
336 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400337
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400338 acpi_hw_low_level_write(8, *(wdata++), &ec->common.data_addr);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400339 }
340
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400341 if (command == ACPI_EC_COMMAND_WRITE) {
342 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
343 if (result)
344 return result;
345 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400346
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400347 for (; rdata_len > 0; rdata_len --) {
348 u32 d;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400349
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400350 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
351 if (result)
352 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400353
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400354 acpi_hw_low_level_read(8, &d, &ec->common.data_addr);
355 *(rdata++) = (u8) d;
356 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400357
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400358 return 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400359}
360
361static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
362 const u8 *wdata, unsigned wdata_len,
363 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400364{
Len Brown50526df2005-08-11 17:32:05 -0400365 acpi_status status = AE_OK;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400366 int result;
Len Brown50526df2005-08-11 17:32:05 -0400367 u32 glk = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400368
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400369 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400371
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400372 if (rdata)
373 memset(rdata, 0, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400374
375 if (ec->common.global_lock) {
376 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
377 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return -ENODEV;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400379 }
Luming Yu45bea152005-07-23 04:08:00 -0400380
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500381 if (down_interruptible(&ec->poll.sem)) {
382 result = -ERESTARTSYS;
383 goto end_nosem;
384 }
Luming Yu45bea152005-07-23 04:08:00 -0400385
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400386 result = acpi_ec_transaction_unlocked(ec, command,
387 wdata, wdata_len,
388 rdata, rdata_len);
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500389 up(&ec->poll.sem);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400390
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500391end_nosem:
Luming Yu45bea152005-07-23 04:08:00 -0400392 if (ec->common.global_lock)
393 acpi_release_global_lock(glk);
394
Patrick Mocheld550d982006-06-27 00:41:40 -0400395 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400396}
397
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400398static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
399 const u8 *wdata, unsigned wdata_len,
400 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400401{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400402 int status;
Len Brown50526df2005-08-11 17:32:05 -0400403 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400405 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400408 if (rdata)
409 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Luming Yu45bea152005-07-23 04:08:00 -0400411 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
413 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400414 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500416
417 WARN_ON(in_interrupt());
Len Brown02b28a332005-12-05 16:33:04 -0500418 down(&ec->intr.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500419
Luming Yu716e0842005-08-10 01:40:00 -0400420 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
421 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500422 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500423 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400426 status = acpi_ec_transaction_unlocked(ec, command,
427 wdata, wdata_len,
428 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400429
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400430end:
Len Brown02b28a332005-12-05 16:33:04 -0500431 up(&ec->intr.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Luming Yu45bea152005-07-23 04:08:00 -0400433 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 acpi_release_global_lock(glk);
435
Patrick Mocheld550d982006-06-27 00:41:40 -0400436 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * Externally callable EC access functions. For now, assume 1 EC only
441 */
Len Brown50526df2005-08-11 17:32:05 -0400442int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Luming Yu45bea152005-07-23 04:08:00 -0400444 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int err;
446 u32 temp_data;
447
448 if (!first_ec)
449 return -ENODEV;
450
451 ec = acpi_driver_data(first_ec);
452
453 err = acpi_ec_read(ec, addr, &temp_data);
454
455 if (!err) {
456 *val = temp_data;
457 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400458 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return err;
460}
Len Brown50526df2005-08-11 17:32:05 -0400461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462EXPORT_SYMBOL(ec_read);
463
Len Brown50526df2005-08-11 17:32:05 -0400464int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Luming Yu45bea152005-07-23 04:08:00 -0400466 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int err;
468
469 if (!first_ec)
470 return -ENODEV;
471
472 ec = acpi_driver_data(first_ec);
473
474 err = acpi_ec_write(ec, addr, val);
475
476 return err;
477}
Len Brown50526df2005-08-11 17:32:05 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479EXPORT_SYMBOL(ec_write);
480
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400481extern int ec_transaction(u8 command,
482 const u8 *wdata, unsigned wdata_len,
483 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400484{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400485 union acpi_ec *ec;
486
487 if (!first_ec)
488 return -ENODEV;
489
490 ec = acpi_driver_data(first_ec);
491
492 return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400493}
Luming Yu45bea152005-07-23 04:08:00 -0400494
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400495EXPORT_SYMBOL(ec_transaction);
Luming Yu45bea152005-07-23 04:08:00 -0400496
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400497static int acpi_ec_query(union acpi_ec *ec, u32 * data) {
498 int result;
499 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400500
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400501 if (!ec || !data)
502 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400503
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400504 /*
505 * Query the EC to find out which _Qxx method we need to evaluate.
506 * Note that successful completion of the query causes the ACPI_EC_SCI
507 * bit to be cleared (and thus clearing the interrupt source).
508 */
Luming Yu45bea152005-07-23 04:08:00 -0400509
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400510 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
511 if (result)
512 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400513
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400514 if (!d)
515 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400516
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400517 *data = d;
518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/* --------------------------------------------------------------------------
522 Event Management
523 -------------------------------------------------------------------------- */
524
Luming Yu45bea152005-07-23 04:08:00 -0400525union acpi_ec_query_data {
Len Brown50526df2005-08-11 17:32:05 -0400526 acpi_handle handle;
527 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528};
529
Len Brown50526df2005-08-11 17:32:05 -0400530static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Len Brown02b28a332005-12-05 16:33:04 -0500532 if (acpi_ec_poll_mode)
533 acpi_ec_gpe_poll_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400534 else
Len Brown02b28a332005-12-05 16:33:04 -0500535 acpi_ec_gpe_intr_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400536}
537
Len Brown02b28a332005-12-05 16:33:04 -0500538static void acpi_ec_gpe_poll_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400539{
Len Brown50526df2005-08-11 17:32:05 -0400540 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
541 u32 value = 0;
Len Brown50526df2005-08-11 17:32:05 -0400542 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 };
Luming Yu45bea152005-07-23 04:08:00 -0400546
Luming Yu45bea152005-07-23 04:08:00 -0400547
548 if (!ec_cxt)
549 goto end;
550
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500551 if (down_interruptible (&ec->poll.sem)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500553 }
Luming Yu45bea152005-07-23 04:08:00 -0400554 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500555 up(&ec->poll.sem);
Luming Yu45bea152005-07-23 04:08:00 -0400556
557 /* TBD: Implement asynch events!
558 * NOTE: All we care about are EC-SCI's. Other EC events are
559 * handled via polling (yuck!). This is because some systems
560 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
561 * a purely interrupt-driven approach (grumble, grumble).
562 */
563 if (!(value & ACPI_EC_FLAG_SCI))
564 goto end;
565
566 if (acpi_ec_query(ec, &value))
567 goto end;
568
569 object_name[2] = hex[((value >> 4) & 0x0F)];
570 object_name[3] = hex[(value & 0x0F)];
571
572 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
573
574 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
575
Len Brown50526df2005-08-11 17:32:05 -0400576 end:
Luming Yu45bea152005-07-23 04:08:00 -0400577 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
578}
Len Brown02b28a332005-12-05 16:33:04 -0500579static void acpi_ec_gpe_intr_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400580{
Len Brown50526df2005-08-11 17:32:05 -0400581 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
582 u32 value;
583 int result = -ENODATA;
584 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
585 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
586 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
587 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500590 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
591 result = acpi_ec_query(ec, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500593 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto end;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 object_name[2] = hex[((value >> 4) & 0x0F)];
597 object_name[3] = hex[(value & 0x0F)];
598
599 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
600
Luming Yu45bea152005-07-23 04:08:00 -0400601 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
Len Brown50526df2005-08-11 17:32:05 -0400602 end:
Len Brown02b28a332005-12-05 16:33:04 -0500603 atomic_dec(&ec->intr.pending_gpe);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Len Brown50526df2005-08-11 17:32:05 -0400607static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Len Brown02b28a332005-12-05 16:33:04 -0500609 if (acpi_ec_poll_mode)
610 return acpi_ec_gpe_poll_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400611 else
Len Brown02b28a332005-12-05 16:33:04 -0500612 return acpi_ec_gpe_intr_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400613}
Len Brown02b28a332005-12-05 16:33:04 -0500614static u32 acpi_ec_gpe_poll_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400615{
Len Brown50526df2005-08-11 17:32:05 -0400616 acpi_status status = AE_OK;
617 union acpi_ec *ec = (union acpi_ec *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (!ec)
620 return ACPI_INTERRUPT_NOT_HANDLED;
621
Luming Yu45bea152005-07-23 04:08:00 -0400622 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
623
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400624 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400625
626 if (status == AE_OK)
627 return ACPI_INTERRUPT_HANDLED;
628 else
629 return ACPI_INTERRUPT_NOT_HANDLED;
630}
Len Brown02b28a332005-12-05 16:33:04 -0500631static u32 acpi_ec_gpe_intr_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400632{
Len Brown50526df2005-08-11 17:32:05 -0400633 acpi_status status = AE_OK;
634 u32 value;
635 union acpi_ec *ec = (union acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400636
637 if (!ec)
638 return ACPI_INTERRUPT_NOT_HANDLED;
639
Luming Yu716e0842005-08-10 01:40:00 -0400640 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500641 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Len Brown02b28a332005-12-05 16:33:04 -0500643 switch (ec->intr.expect_event) {
Luming Yu716e0842005-08-10 01:40:00 -0400644 case ACPI_EC_EVENT_OBF:
645 if (!(value & ACPI_EC_FLAG_OBF))
646 break;
Vladimir Lebedev49fee9812006-06-20 16:46:00 -0400647 ec->intr.expect_event = 0;
648 wake_up(&ec->intr.wait);
649 break;
Luming Yu716e0842005-08-10 01:40:00 -0400650 case ACPI_EC_EVENT_IBE:
651 if ((value & ACPI_EC_FLAG_IBF))
652 break;
Len Brown02b28a332005-12-05 16:33:04 -0500653 ec->intr.expect_event = 0;
654 wake_up(&ec->intr.wait);
Vladimir Lebedev49fee9812006-06-20 16:46:00 -0400655 break;
Luming Yu716e0842005-08-10 01:40:00 -0400656 default:
657 break;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500658 }
659
Len Brown50526df2005-08-11 17:32:05 -0400660 if (value & ACPI_EC_FLAG_SCI) {
Len Brown02b28a332005-12-05 16:33:04 -0500661 atomic_add(1, &ec->intr.pending_gpe);
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400662 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
Len Brown50526df2005-08-11 17:32:05 -0400663 acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400664 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400665 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
666 }
Luming Yu45bea152005-07-23 04:08:00 -0400667 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500668 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400669 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672/* --------------------------------------------------------------------------
673 Address Space Management
674 -------------------------------------------------------------------------- */
675
676static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400677acpi_ec_space_setup(acpi_handle region_handle,
678 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 /*
681 * The EC object is in the handler context and is needed
682 * when calling the acpi_ec_space_handler.
683 */
Len Brown50526df2005-08-11 17:32:05 -0400684 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
685 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return AE_OK;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400691acpi_ec_space_handler(u32 function,
692 acpi_physical_address address,
693 u32 bit_width,
694 acpi_integer * value,
695 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Len Brown50526df2005-08-11 17:32:05 -0400697 int result = 0;
698 union acpi_ec *ec = NULL;
699 u64 temp = *value;
700 acpi_integer f_v = 0;
701 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400705 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Luming Yufa9cd542005-03-19 01:54:47 -0500707 if (bit_width != 8 && acpi_strict) {
Len Brown50526df2005-08-11 17:32:05 -0400708 printk(KERN_WARNING PREFIX
709 "acpi_ec_space_handler: bit_width should be 8\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400710 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Len Brown50526df2005-08-11 17:32:05 -0400713 ec = (union acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Len Brown50526df2005-08-11 17:32:05 -0400715 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 switch (function) {
717 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500718 temp = 0;
Len Brown50526df2005-08-11 17:32:05 -0400719 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500722 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724 default:
725 result = -EINVAL;
726 goto out;
727 break;
728 }
729
730 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500731 if (bit_width) {
732 if (function == ACPI_READ)
733 f_v |= temp << 8 * i;
734 if (function == ACPI_WRITE)
735 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500737 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto next_byte;
739 }
740
Luming Yufa9cd542005-03-19 01:54:47 -0500741 if (function == ACPI_READ) {
742 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *value = f_v;
744 }
745
Len Brown50526df2005-08-11 17:32:05 -0400746 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (result) {
748 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400749 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400758 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* --------------------------------------------------------------------------
763 FS Interface (/proc)
764 -------------------------------------------------------------------------- */
765
Len Brown50526df2005-08-11 17:32:05 -0400766static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Len Brown50526df2005-08-11 17:32:05 -0400768static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Len Brown50526df2005-08-11 17:32:05 -0400770 union acpi_ec *ec = (union acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (!ec)
774 goto end;
775
776 seq_printf(seq, "gpe bit: 0x%02x\n",
Len Brown50526df2005-08-11 17:32:05 -0400777 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Len Brown50526df2005-08-11 17:32:05 -0400779 (u32) ec->common.status_addr.address,
780 (u32) ec->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 seq_printf(seq, "use global lock: %s\n",
Len Brown50526df2005-08-11 17:32:05 -0400782 ec->common.global_lock ? "yes" : "no");
Luming Yu45bea152005-07-23 04:08:00 -0400783 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Len Brown50526df2005-08-11 17:32:05 -0400785 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
790{
791 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
792}
793
Arjan van de Vend7508032006-07-04 13:06:00 -0400794static const struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400795 .open = acpi_ec_info_open_fs,
796 .read = seq_read,
797 .llseek = seq_lseek,
798 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .owner = THIS_MODULE,
800};
801
Len Brown50526df2005-08-11 17:32:05 -0400802static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Len Brown50526df2005-08-11 17:32:05 -0400804 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (!acpi_device_dir(device)) {
808 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400809 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400815 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400817 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 else {
819 entry->proc_fops = &acpi_ec_info_ops;
820 entry->data = acpi_driver_data(device);
821 entry->owner = THIS_MODULE;
822 }
823
Patrick Mocheld550d982006-06-27 00:41:40 -0400824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Len Brown50526df2005-08-11 17:32:05 -0400827static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (acpi_device_dir(device)) {
831 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
832 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
833 acpi_device_dir(device) = NULL;
834 }
835
Patrick Mocheld550d982006-06-27 00:41:40 -0400836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/* --------------------------------------------------------------------------
840 Driver Interface
841 -------------------------------------------------------------------------- */
842
Len Brown02b28a332005-12-05 16:33:04 -0500843static int acpi_ec_poll_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Len Brown50526df2005-08-11 17:32:05 -0400845 int result = 0;
846 acpi_status status = AE_OK;
847 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Luming Yu45bea152005-07-23 04:08:00 -0400853 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400855 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -0400856 memset(ec, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Luming Yu45bea152005-07-23 04:08:00 -0400858 ec->common.handle = device->handle;
859 ec->common.uid = -1;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -0500860 init_MUTEX(&ec->poll.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
862 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
863 acpi_driver_data(device) = ec;
864
865 /* Use the global lock for all EC transactions? */
Len Brown50526df2005-08-11 17:32:05 -0400866 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
867 &ec->common.global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500869 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
870 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
871 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400873 ACPI_ADR_SPACE_EC,
874 &acpi_ec_space_handler);
875
876 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
877 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 kfree(ec_ecdt);
880 }
881
882 /* Get GPE bit assignment (EC events). */
883 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400884 status =
885 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
886 &ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400888 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 result = -ENODEV;
890 goto end;
891 }
892
893 result = acpi_ec_add_fs(device);
894 if (result)
895 goto end;
896
Len Brown02b28a332005-12-05 16:33:04 -0500897 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
Len Brown50526df2005-08-11 17:32:05 -0400898 acpi_device_name(device), acpi_device_bid(device),
899 (u32) ec->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -0400900
901 if (!first_ec)
902 first_ec = device;
903
Len Brown50526df2005-08-11 17:32:05 -0400904 end:
Luming Yu45bea152005-07-23 04:08:00 -0400905 if (result)
906 kfree(ec);
907
Patrick Mocheld550d982006-06-27 00:41:40 -0400908 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400909}
Len Brown02b28a332005-12-05 16:33:04 -0500910static int acpi_ec_intr_add(struct acpi_device *device)
Luming Yu45bea152005-07-23 04:08:00 -0400911{
Len Brown50526df2005-08-11 17:32:05 -0400912 int result = 0;
913 acpi_status status = AE_OK;
914 union acpi_ec *ec = NULL;
Luming Yu45bea152005-07-23 04:08:00 -0400915
Luming Yu45bea152005-07-23 04:08:00 -0400916
917 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400918 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400919
920 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
921 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400922 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -0400923 memset(ec, 0, sizeof(union acpi_ec));
924
925 ec->common.handle = device->handle;
926 ec->common.uid = -1;
Len Brown02b28a332005-12-05 16:33:04 -0500927 atomic_set(&ec->intr.pending_gpe, 0);
928 atomic_set(&ec->intr.leaving_burst, 1);
929 init_MUTEX(&ec->intr.sem);
930 init_waitqueue_head(&ec->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -0400931 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
932 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
933 acpi_driver_data(device) = ec;
934
935 /* Use the global lock for all EC transactions? */
Len Brown50526df2005-08-11 17:32:05 -0400936 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
937 &ec->common.global_lock);
Luming Yu45bea152005-07-23 04:08:00 -0400938
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500939 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
940 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
941 if (ec_ecdt) {
Luming Yu45bea152005-07-23 04:08:00 -0400942 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400943 ACPI_ADR_SPACE_EC,
944 &acpi_ec_space_handler);
Luming Yu45bea152005-07-23 04:08:00 -0400945
Len Brown50526df2005-08-11 17:32:05 -0400946 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
947 &acpi_ec_gpe_handler);
Luming Yu45bea152005-07-23 04:08:00 -0400948
949 kfree(ec_ecdt);
950 }
951
952 /* Get GPE bit assignment (EC events). */
953 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400954 status =
955 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
956 &ec->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -0400957 if (ACPI_FAILURE(status)) {
Len Brown64684632006-06-26 23:41:38 -0400958 printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
Luming Yu45bea152005-07-23 04:08:00 -0400959 result = -ENODEV;
960 goto end;
961 }
962
963 result = acpi_ec_add_fs(device);
964 if (result)
965 goto end;
966
Len Brown02b28a332005-12-05 16:33:04 -0500967 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
Len Brown50526df2005-08-11 17:32:05 -0400968 acpi_device_name(device), acpi_device_bid(device),
969 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (!first_ec)
972 first_ec = device;
973
Len Brown50526df2005-08-11 17:32:05 -0400974 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (result)
976 kfree(ec);
977
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Len Brown50526df2005-08-11 17:32:05 -0400981static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Len Brown50526df2005-08-11 17:32:05 -0400983 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 ec = acpi_driver_data(device);
990
991 acpi_ec_remove_fs(device);
992
993 kfree(ec);
994
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400999acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Len Brown50526df2005-08-11 17:32:05 -04001001 union acpi_ec *ec = (union acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct acpi_generic_address *addr;
1003
Bob Moore50eca3e2005-09-30 19:03:00 -04001004 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return AE_OK;
1006 }
1007
1008 /*
1009 * The first address region returned is the data port, and
1010 * the second address region returned is the status/command
1011 * port.
1012 */
Luming Yu45bea152005-07-23 04:08:00 -04001013 if (ec->common.data_addr.register_bit_width == 0) {
1014 addr = &ec->common.data_addr;
1015 } else if (ec->common.command_addr.register_bit_width == 0) {
1016 addr = &ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
1018 return AE_CTRL_TERMINATE;
1019 }
1020
1021 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1022 addr->register_bit_width = 8;
1023 addr->register_bit_offset = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -04001024 addr->address = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 return AE_OK;
1027}
1028
Len Brown50526df2005-08-11 17:32:05 -04001029static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Len Brown50526df2005-08-11 17:32:05 -04001031 acpi_status status = AE_OK;
1032 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 ec = acpi_driver_data(device);
1039
1040 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -04001041 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Get I/O port addresses. Convert to GAS format.
1045 */
Luming Yu45bea152005-07-23 04:08:00 -04001046 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001047 acpi_ec_io_ports, ec);
1048 if (ACPI_FAILURE(status)
1049 || ec->common.command_addr.register_bit_width == 0) {
Len Brown64684632006-06-26 23:41:38 -04001050 printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001051 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Luming Yu45bea152005-07-23 04:08:00 -04001054 ec->common.status_addr = ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
Len Brown50526df2005-08-11 17:32:05 -04001057 (u32) ec->common.gpe_bit,
1058 (u32) ec->common.command_addr.address,
1059 (u32) ec->common.data_addr.address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /*
1062 * Install GPE handler
1063 */
Luming Yu45bea152005-07-23 04:08:00 -04001064 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001065 ACPI_GPE_EDGE_TRIGGERED,
1066 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -04001068 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Len Brown50526df2005-08-11 17:32:05 -04001070 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1071 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Len Brown50526df2005-08-11 17:32:05 -04001073 status = acpi_install_address_space_handler(ec->common.handle,
1074 ACPI_ADR_SPACE_EC,
1075 &acpi_ec_space_handler,
1076 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (ACPI_FAILURE(status)) {
Len Brown50526df2005-08-11 17:32:05 -04001078 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1079 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -04001080 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
Patrick Mocheld550d982006-06-27 00:41:40 -04001083 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Len Brown50526df2005-08-11 17:32:05 -04001086static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Len Brown50526df2005-08-11 17:32:05 -04001088 acpi_status status = AE_OK;
1089 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001093 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 ec = acpi_driver_data(device);
1096
Luming Yu45bea152005-07-23 04:08:00 -04001097 status = acpi_remove_address_space_handler(ec->common.handle,
Len Brown50526df2005-08-11 17:32:05 -04001098 ACPI_ADR_SPACE_EC,
1099 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001101 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Len Brown50526df2005-08-11 17:32:05 -04001103 status =
1104 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1105 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001107 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -04001113acpi_fake_ecdt_callback(acpi_handle handle,
1114 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Luming Yu45bea152005-07-23 04:08:00 -04001116
Len Brown02b28a332005-12-05 16:33:04 -05001117 if (acpi_ec_poll_mode)
1118 return acpi_fake_ecdt_poll_callback(handle,
Len Brown50526df2005-08-11 17:32:05 -04001119 Level, context, retval);
Luming Yu45bea152005-07-23 04:08:00 -04001120 else
Len Brown02b28a332005-12-05 16:33:04 -05001121 return acpi_fake_ecdt_intr_callback(handle,
Len Brown50526df2005-08-11 17:32:05 -04001122 Level, context, retval);
Luming Yu45bea152005-07-23 04:08:00 -04001123}
1124
1125static acpi_status __init
Len Brown02b28a332005-12-05 16:33:04 -05001126acpi_fake_ecdt_poll_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -04001127 u32 Level, void *context, void **retval)
Luming Yu45bea152005-07-23 04:08:00 -04001128{
Len Brown50526df2005-08-11 17:32:05 -04001129 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001132 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (ACPI_FAILURE(status))
1134 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001135 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Luming Yu45bea152005-07-23 04:08:00 -04001137 ec_ecdt->common.uid = -1;
1138 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Len Brown50526df2005-08-11 17:32:05 -04001140 status =
1141 acpi_evaluate_integer(handle, "_GPE", NULL,
1142 &ec_ecdt->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (ACPI_FAILURE(status))
1144 return status;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -05001145 init_MUTEX(&ec_ecdt->poll.sem);
Luming Yu45bea152005-07-23 04:08:00 -04001146 ec_ecdt->common.global_lock = TRUE;
1147 ec_ecdt->common.handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Len Brown50526df2005-08-11 17:32:05 -04001149 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1150 (u32) ec_ecdt->common.gpe_bit,
1151 (u32) ec_ecdt->common.command_addr.address,
1152 (u32) ec_ecdt->common.data_addr.address);
Luming Yu45bea152005-07-23 04:08:00 -04001153
1154 return AE_CTRL_TERMINATE;
1155}
1156
1157static acpi_status __init
Len Brown02b28a332005-12-05 16:33:04 -05001158acpi_fake_ecdt_intr_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -04001159 u32 Level, void *context, void **retval)
Luming Yu45bea152005-07-23 04:08:00 -04001160{
Len Brown50526df2005-08-11 17:32:05 -04001161 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -04001162
Len Brown02b28a332005-12-05 16:33:04 -05001163 init_MUTEX(&ec_ecdt->intr.sem);
1164 init_waitqueue_head(&ec_ecdt->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -04001165 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001166 acpi_ec_io_ports, ec_ecdt);
Luming Yu45bea152005-07-23 04:08:00 -04001167 if (ACPI_FAILURE(status))
1168 return status;
1169 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1170
1171 ec_ecdt->common.uid = -1;
1172 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1173
Len Brown50526df2005-08-11 17:32:05 -04001174 status =
1175 acpi_evaluate_integer(handle, "_GPE", NULL,
1176 &ec_ecdt->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -04001177 if (ACPI_FAILURE(status))
1178 return status;
1179 ec_ecdt->common.global_lock = TRUE;
1180 ec_ecdt->common.handle = handle;
1181
Len Brown50526df2005-08-11 17:32:05 -04001182 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1183 (u32) ec_ecdt->common.gpe_bit,
1184 (u32) ec_ecdt->common.command_addr.address,
1185 (u32) ec_ecdt->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 return AE_CTRL_TERMINATE;
1188}
1189
1190/*
1191 * Some BIOS (such as some from Gateway laptops) access EC region very early
1192 * such as in BAT0._INI or EC._INI before an EC device is found and
1193 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1194 * required, but if EC regison is accessed early, it is required.
1195 * The routine tries to workaround the BIOS bug by pre-scan EC device
1196 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1197 * op region (since _REG isn't invoked yet). The assumption is true for
1198 * all systems found.
1199 */
Len Brown50526df2005-08-11 17:32:05 -04001200static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Len Brown50526df2005-08-11 17:32:05 -04001202 acpi_status status;
1203 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1206
Luming Yu45bea152005-07-23 04:08:00 -04001207 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!ec_ecdt) {
1209 ret = -ENOMEM;
1210 goto error;
1211 }
Luming Yu45bea152005-07-23 04:08:00 -04001212 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Len Brown50526df2005-08-11 17:32:05 -04001214 status = acpi_get_devices(ACPI_EC_HID,
1215 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (ACPI_FAILURE(status)) {
1217 kfree(ec_ecdt);
1218 ec_ecdt = NULL;
1219 ret = -ENODEV;
1220 goto error;
1221 }
1222 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001223 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1225 return ret;
1226}
1227
Len Brown50526df2005-08-11 17:32:05 -04001228static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Len Brown02b28a332005-12-05 16:33:04 -05001230 if (acpi_ec_poll_mode)
1231 return acpi_ec_poll_get_real_ecdt();
Luming Yu45bea152005-07-23 04:08:00 -04001232 else
Len Brown02b28a332005-12-05 16:33:04 -05001233 return acpi_ec_intr_get_real_ecdt();
Luming Yu45bea152005-07-23 04:08:00 -04001234}
1235
Len Brown02b28a332005-12-05 16:33:04 -05001236static int __init acpi_ec_poll_get_real_ecdt(void)
Luming Yu45bea152005-07-23 04:08:00 -04001237{
Len Brown50526df2005-08-11 17:32:05 -04001238 acpi_status status;
1239 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -04001240
Len Brown50526df2005-08-11 17:32:05 -04001241 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1242 (struct acpi_table_header **)
1243 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -04001244 if (ACPI_FAILURE(status))
1245 return -ENODEV;
1246
1247 printk(KERN_INFO PREFIX "Found ECDT\n");
1248
1249 /*
1250 * Generate a temporary ec context to use until the namespace is scanned
1251 */
1252 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1253 if (!ec_ecdt)
1254 return -ENOMEM;
1255 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1256
1257 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1258 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1259 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1260 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Rich Townsendf9a6ee1a2005-12-19 23:07:00 -05001261 init_MUTEX(&ec_ecdt->poll.sem);
Luming Yu45bea152005-07-23 04:08:00 -04001262 /* use the GL just to be safe */
1263 ec_ecdt->common.global_lock = TRUE;
1264 ec_ecdt->common.uid = ecdt_ptr->uid;
1265
Len Brown50526df2005-08-11 17:32:05 -04001266 status =
1267 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Luming Yu45bea152005-07-23 04:08:00 -04001268 if (ACPI_FAILURE(status)) {
1269 goto error;
1270 }
1271
1272 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001273 error:
Luming Yu45bea152005-07-23 04:08:00 -04001274 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1275 kfree(ec_ecdt);
1276 ec_ecdt = NULL;
1277
1278 return -ENODEV;
1279}
1280
Len Brown02b28a332005-12-05 16:33:04 -05001281static int __init acpi_ec_intr_get_real_ecdt(void)
Luming Yu45bea152005-07-23 04:08:00 -04001282{
Len Brown50526df2005-08-11 17:32:05 -04001283 acpi_status status;
1284 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Dmitry Torokhov451566f2005-03-19 01:10:05 -05001286 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
Len Brown50526df2005-08-11 17:32:05 -04001287 (struct acpi_table_header **)
1288 &ecdt_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ACPI_FAILURE(status))
1290 return -ENODEV;
1291
1292 printk(KERN_INFO PREFIX "Found ECDT\n");
1293
1294 /*
1295 * Generate a temporary ec context to use until the namespace is scanned
1296 */
Luming Yu45bea152005-07-23 04:08:00 -04001297 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!ec_ecdt)
1299 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -04001300 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Len Brown02b28a332005-12-05 16:33:04 -05001302 init_MUTEX(&ec_ecdt->intr.sem);
1303 init_waitqueue_head(&ec_ecdt->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -04001304 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1305 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1306 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1307 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* use the GL just to be safe */
Luming Yu45bea152005-07-23 04:08:00 -04001309 ec_ecdt->common.global_lock = TRUE;
1310 ec_ecdt->common.uid = ecdt_ptr->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Len Brown50526df2005-08-11 17:32:05 -04001312 status =
1313 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (ACPI_FAILURE(status)) {
1315 goto error;
1316 }
1317
1318 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001319 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1321 kfree(ec_ecdt);
1322 ec_ecdt = NULL;
1323
1324 return -ENODEV;
1325}
1326
1327static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -04001328int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Len Brown50526df2005-08-11 17:32:05 -04001330 acpi_status status;
1331 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 ret = acpi_ec_get_real_ecdt();
1334 /* Try to make a fake ECDT */
1335 if (ret && acpi_fake_ecdt_enabled) {
1336 ret = acpi_ec_fake_ecdt();
1337 }
1338
1339 if (ret)
1340 return 0;
1341
1342 /*
1343 * Install GPE handler
1344 */
Luming Yu45bea152005-07-23 04:08:00 -04001345 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001346 ACPI_GPE_EDGE_TRIGGERED,
1347 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (ACPI_FAILURE(status)) {
1349 goto error;
1350 }
Len Brown50526df2005-08-11 17:32:05 -04001351 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1352 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Len Brown50526df2005-08-11 17:32:05 -04001354 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1355 ACPI_ADR_SPACE_EC,
1356 &acpi_ec_space_handler,
1357 &acpi_ec_space_setup,
1358 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001360 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001361 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto error;
1363 }
1364
1365 return 0;
1366
Len Brown50526df2005-08-11 17:32:05 -04001367 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1369 kfree(ec_ecdt);
1370 ec_ecdt = NULL;
1371
1372 return -ENODEV;
1373}
1374
Len Brown50526df2005-08-11 17:32:05 -04001375static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Len Brown50526df2005-08-11 17:32:05 -04001377 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1384 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001385 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /* Now register the driver for the EC */
1388 result = acpi_bus_register_driver(&acpi_ec_driver);
1389 if (result < 0) {
1390 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001391 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Patrick Mocheld550d982006-06-27 00:41:40 -04001394 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397subsys_initcall(acpi_ec_init);
1398
1399/* EC driver currently not unloadable */
1400#if 0
Len Brown50526df2005-08-11 17:32:05 -04001401static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 acpi_bus_unregister_driver(&acpi_ec_driver);
1405
1406 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1407
Patrick Mocheld550d982006-06-27 00:41:40 -04001408 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
Len Brown50526df2005-08-11 17:32:05 -04001410#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412static int __init acpi_fake_ecdt_setup(char *str)
1413{
1414 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001415 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a332005-12-05 16:33:04 -05001419static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001420{
Len Brown02b28a332005-12-05 16:33:04 -05001421 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001422
Len Brown02b28a332005-12-05 16:33:04 -05001423 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001424 return 0;
1425
Len Brown02b28a332005-12-05 16:33:04 -05001426 if (intr) {
1427 acpi_ec_poll_mode = EC_INTR;
1428 acpi_ec_driver.ops.add = acpi_ec_intr_add;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001429 } else {
Len Brown02b28a332005-12-05 16:33:04 -05001430 acpi_ec_poll_mode = EC_POLL;
1431 acpi_ec_driver.ops.add = acpi_ec_poll_add;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001432 }
Len Brown02b28a332005-12-05 16:33:04 -05001433 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001434 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001435}
Len Brown50526df2005-08-11 17:32:05 -04001436
Len Brown53f11d42005-12-05 16:46:36 -05001437__setup("ec_intr=", acpi_ec_set_intr_mode);