blob: c036e2a69f33299fb403e1c2ad0749f1711cd46b [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 Brown02b28a32005-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 Brown02b28a32005-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;
103 unsigned int expect_event;
104 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 Brown02b28a32005-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;
119 spinlock_t lock;
Len Brown02b28a32005-12-05 16:33:04 -0500120 } poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Len Brown02b28a32005-12-05 16:33:04 -0500123static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131static 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 Brown02b28a32005-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 Brown02b28a32005-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 Brown02b28a32005-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
Len Brown50526df2005-08-11 17:32:05 -0400164static int acpi_ec_wait(union acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400165{
Len Brown02b28a32005-12-05 16:33:04 -0500166 if (acpi_ec_poll_mode)
167 return acpi_ec_poll_wait(ec, event);
Luming Yu45bea152005-07-23 04:08:00 -0400168 else
Len Brown02b28a32005-12-05 16:33:04 -0500169 return acpi_ec_intr_wait(ec, event);
Luming Yu45bea152005-07-23 04:08:00 -0400170}
171
Len Brown02b28a32005-12-05 16:33:04 -0500172static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400173{
Len Brown50526df2005-08-11 17:32:05 -0400174 u32 acpi_ec_status = 0;
175 u32 i = ACPI_EC_UDELAY_COUNT;
Luming Yu45bea152005-07-23 04:08:00 -0400176
177 if (!ec)
178 return -EINVAL;
179
180 /* Poll the EC status register waiting for the event to occur. */
181 switch (event) {
182 case ACPI_EC_EVENT_OBF:
183 do {
Len Brown50526df2005-08-11 17:32:05 -0400184 acpi_hw_low_level_read(8, &acpi_ec_status,
185 &ec->common.status_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400186 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
187 return 0;
188 udelay(ACPI_EC_UDELAY);
Len Brown50526df2005-08-11 17:32:05 -0400189 } while (--i > 0);
Luming Yu45bea152005-07-23 04:08:00 -0400190 break;
191 case ACPI_EC_EVENT_IBE:
192 do {
Len Brown50526df2005-08-11 17:32:05 -0400193 acpi_hw_low_level_read(8, &acpi_ec_status,
194 &ec->common.status_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400195 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
196 return 0;
197 udelay(ACPI_EC_UDELAY);
Len Brown50526df2005-08-11 17:32:05 -0400198 } while (--i > 0);
Luming Yu45bea152005-07-23 04:08:00 -0400199 break;
200 default:
201 return -EINVAL;
202 }
203
204 return -ETIME;
205}
Len Brown02b28a32005-12-05 16:33:04 -0500206static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500207{
Len Brown50526df2005-08-11 17:32:05 -0400208 int result = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500209
210 ACPI_FUNCTION_TRACE("acpi_ec_wait");
211
Len Brown02b28a32005-12-05 16:33:04 -0500212 ec->intr.expect_event = event;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500213 smp_mb();
214
Luming Yu716e0842005-08-10 01:40:00 -0400215 switch (event) {
Luming Yu716e0842005-08-10 01:40:00 -0400216 case ACPI_EC_EVENT_IBE:
217 if (~acpi_ec_read_status(ec) & event) {
Len Brown02b28a32005-12-05 16:33:04 -0500218 ec->intr.expect_event = 0;
Luming Yu716e0842005-08-10 01:40:00 -0400219 return_VALUE(0);
220 }
221 break;
Luming Yu06a2a382005-09-27 00:43:00 -0400222 default:
Luming Yu716e0842005-08-10 01:40:00 -0400223 break;
224 }
225
Len Brown02b28a32005-12-05 16:33:04 -0500226 result = wait_event_timeout(ec->intr.wait,
227 !ec->intr.expect_event,
Len Brown50526df2005-08-11 17:32:05 -0400228 msecs_to_jiffies(ACPI_EC_DELAY));
229
Len Brown02b28a32005-12-05 16:33:04 -0500230 ec->intr.expect_event = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500231 smp_mb();
232
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500233 /*
234 * Verify that the event in question has actually happened by
235 * querying EC status. Do the check even if operation timed-out
236 * to make sure that we did not miss interrupt.
237 */
238 switch (event) {
239 case ACPI_EC_EVENT_OBF:
240 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
241 return_VALUE(0);
242 break;
243
244 case ACPI_EC_EVENT_IBE:
245 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
246 return_VALUE(0);
247 break;
248 }
249
250 return_VALUE(-ETIME);
251}
252
Len Brown02b28a32005-12-05 16:33:04 -0500253#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400254/*
255 * Note: samsung nv5000 doesn't work with ec burst mode.
256 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
257 */
258int acpi_ec_enter_burst_mode(union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500259{
Len Brown50526df2005-08-11 17:32:05 -0400260 u32 tmp = 0;
261 int status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500262
263 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
264
265 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400266 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Luming Yu716e0842005-08-10 01:40:00 -0400267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Len Brown50526df2005-08-11 17:32:05 -0400268 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400269 goto end;
Len Brown50526df2005-08-11 17:32:05 -0400270 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
271 &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500272 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
Luming Yu45bea152005-07-23 04:08:00 -0400273 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
Len Brown50526df2005-08-11 17:32:05 -0400274 if (tmp != 0x90) { /* Burst ACK byte */
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500275 return_VALUE(-EINVAL);
276 }
Luming Yu668d74c2005-07-23 00:26:33 -0400277 }
278
Len Brown02b28a32005-12-05 16:33:04 -0500279 atomic_set(&ec->intr.leaving_burst, 0);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500280 return_VALUE(0);
Len Brown50526df2005-08-11 17:32:05 -0400281 end:
Len Brown1e8df532005-12-05 16:47:46 -0500282 printk(KERN_WARNING PREFIX "Error in acpi_ec_wait\n");
Luming Yu716e0842005-08-10 01:40:00 -0400283 return_VALUE(-1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500284}
285
Luming Yu06a2a382005-09-27 00:43:00 -0400286int acpi_ec_leave_burst_mode(union acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500287{
Luming Yu06a2a382005-09-27 00:43:00 -0400288 int status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500289
290 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
291
Luming Yu06a2a382005-09-27 00:43:00 -0400292 status = acpi_ec_read_status(ec);
293 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
294 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
295 if(status)
296 goto end;
297 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
298 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
299 }
Len Brown02b28a32005-12-05 16:33:04 -0500300 atomic_set(&ec->intr.leaving_burst, 1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500301 return_VALUE(0);
Luming Yu06a2a382005-09-27 00:43:00 -0400302end:
Len Brown1e8df532005-12-05 16:47:46 -0500303 printk(KERN_WARNING PREFIX "leave burst_mode:error\n");
Luming Yu06a2a382005-09-27 00:43:00 -0400304 return_VALUE(-1);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500305}
Len Brown02b28a32005-12-05 16:33:04 -0500306#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Len Brown50526df2005-08-11 17:32:05 -0400308static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
Luming Yu45bea152005-07-23 04:08:00 -0400309{
Len Brown02b28a32005-12-05 16:33:04 -0500310 if (acpi_ec_poll_mode)
311 return acpi_ec_poll_read(ec, address, data);
Luming Yu45bea152005-07-23 04:08:00 -0400312 else
Len Brown02b28a32005-12-05 16:33:04 -0500313 return acpi_ec_intr_read(ec, address, data);
Luming Yu45bea152005-07-23 04:08:00 -0400314}
Len Brown50526df2005-08-11 17:32:05 -0400315static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
Luming Yu45bea152005-07-23 04:08:00 -0400316{
Len Brown02b28a32005-12-05 16:33:04 -0500317 if (acpi_ec_poll_mode)
318 return acpi_ec_poll_write(ec, address, data);
Luming Yu45bea152005-07-23 04:08:00 -0400319 else
Len Brown02b28a32005-12-05 16:33:04 -0500320 return acpi_ec_intr_write(ec, address, data);
Luming Yu45bea152005-07-23 04:08:00 -0400321}
Len Brown02b28a32005-12-05 16:33:04 -0500322static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
Luming Yu45bea152005-07-23 04:08:00 -0400323{
Len Brown50526df2005-08-11 17:32:05 -0400324 acpi_status status = AE_OK;
325 int result = 0;
326 unsigned long flags = 0;
327 u32 glk = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400328
329 ACPI_FUNCTION_TRACE("acpi_ec_read");
330
331 if (!ec || !data)
332 return_VALUE(-EINVAL);
333
334 *data = 0;
335
336 if (ec->common.global_lock) {
337 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
338 if (ACPI_FAILURE(status))
339 return_VALUE(-ENODEV);
340 }
341
Len Brown02b28a32005-12-05 16:33:04 -0500342 spin_lock_irqsave(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400343
Len Brown50526df2005-08-11 17:32:05 -0400344 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
345 &ec->common.command_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400346 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
347 if (result)
348 goto end;
349
350 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
351 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
352 if (result)
353 goto end;
354
355 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
356
357 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
Len Brown50526df2005-08-11 17:32:05 -0400358 *data, address));
359
360 end:
Len Brown02b28a32005-12-05 16:33:04 -0500361 spin_unlock_irqrestore(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400362
363 if (ec->common.global_lock)
364 acpi_release_global_lock(glk);
365
366 return_VALUE(result);
367}
368
Len Brown02b28a32005-12-05 16:33:04 -0500369static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
Luming Yu45bea152005-07-23 04:08:00 -0400370{
Len Brown50526df2005-08-11 17:32:05 -0400371 int result = 0;
372 acpi_status status = AE_OK;
373 unsigned long flags = 0;
374 u32 glk = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400375
376 ACPI_FUNCTION_TRACE("acpi_ec_write");
377
378 if (!ec)
379 return_VALUE(-EINVAL);
380
381 if (ec->common.global_lock) {
382 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
383 if (ACPI_FAILURE(status))
384 return_VALUE(-ENODEV);
385 }
386
Len Brown02b28a32005-12-05 16:33:04 -0500387 spin_lock_irqsave(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400388
Len Brown50526df2005-08-11 17:32:05 -0400389 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
390 &ec->common.command_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400391 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
392 if (result)
393 goto end;
394
395 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
396 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
397 if (result)
398 goto end;
399
400 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
401 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
402 if (result)
403 goto end;
404
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
Len Brown50526df2005-08-11 17:32:05 -0400406 data, address));
Luming Yu45bea152005-07-23 04:08:00 -0400407
Len Brown50526df2005-08-11 17:32:05 -0400408 end:
Len Brown02b28a32005-12-05 16:33:04 -0500409 spin_unlock_irqrestore(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400410
411 if (ec->common.global_lock)
412 acpi_release_global_lock(glk);
413
414 return_VALUE(result);
415}
416
Len Brown02b28a32005-12-05 16:33:04 -0500417static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Len Brown50526df2005-08-11 17:32:05 -0400419 int status = 0;
420 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 ACPI_FUNCTION_TRACE("acpi_ec_read");
423
424 if (!ec || !data)
425 return_VALUE(-EINVAL);
426
427 *data = 0;
428
Luming Yu45bea152005-07-23 04:08:00 -0400429 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
431 if (ACPI_FAILURE(status))
432 return_VALUE(-ENODEV);
433 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500434
435 WARN_ON(in_interrupt());
Len Brown02b28a32005-12-05 16:33:04 -0500436 down(&ec->intr.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500437
Luming Yu716e0842005-08-10 01:40:00 -0400438 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
439 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500440 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500441 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400442 }
Len Brown50526df2005-08-11 17:32:05 -0400443 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
444 &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500445 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500446 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500447 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Luming Yu45bea152005-07-23 04:08:00 -0400450 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Len Brown50526df2005-08-11 17:32:05 -0400451 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
452 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500453 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500455 }
Luming Yu45bea152005-07-23 04:08:00 -0400456 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
Len Brown50526df2005-08-11 17:32:05 -0400458 *data, address));
459
460 end:
Len Brown02b28a32005-12-05 16:33:04 -0500461 up(&ec->intr.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Luming Yu45bea152005-07-23 04:08:00 -0400463 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 acpi_release_global_lock(glk);
465
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500466 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Len Brown02b28a32005-12-05 16:33:04 -0500469static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Len Brown50526df2005-08-11 17:32:05 -0400471 int status = 0;
472 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 ACPI_FUNCTION_TRACE("acpi_ec_write");
475
476 if (!ec)
477 return_VALUE(-EINVAL);
Luming Yu716e0842005-08-10 01:40:00 -0400478
Luming Yu45bea152005-07-23 04:08:00 -0400479 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
481 if (ACPI_FAILURE(status))
482 return_VALUE(-ENODEV);
483 }
484
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500485 WARN_ON(in_interrupt());
Len Brown02b28a32005-12-05 16:33:04 -0500486 down(&ec->intr.sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500487
Luming Yu716e0842005-08-10 01:40:00 -0400488 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Len Brown50526df2005-08-11 17:32:05 -0400489 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500490 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500491 }
Len Brown50526df2005-08-11 17:32:05 -0400492 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
493 &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500494 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Luming Yu716e0842005-08-10 01:40:00 -0400495 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500496 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Luming Yu45bea152005-07-23 04:08:00 -0400499 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500500 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
Len Brown50526df2005-08-11 17:32:05 -0400501 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500502 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Luming Yu45bea152005-07-23 04:08:00 -0400505 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
Len Brown50526df2005-08-11 17:32:05 -0400508 data, address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Len Brown02b28a32005-12-05 16:33:04 -0500510 up(&ec->intr.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Luming Yu45bea152005-07-23 04:08:00 -0400512 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 acpi_release_global_lock(glk);
514
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500515 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * Externally callable EC access functions. For now, assume 1 EC only
520 */
Len Brown50526df2005-08-11 17:32:05 -0400521int ec_read(u8 addr, u8 * val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Luming Yu45bea152005-07-23 04:08:00 -0400523 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int err;
525 u32 temp_data;
526
527 if (!first_ec)
528 return -ENODEV;
529
530 ec = acpi_driver_data(first_ec);
531
532 err = acpi_ec_read(ec, addr, &temp_data);
533
534 if (!err) {
535 *val = temp_data;
536 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400537 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return err;
539}
Len Brown50526df2005-08-11 17:32:05 -0400540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541EXPORT_SYMBOL(ec_read);
542
Len Brown50526df2005-08-11 17:32:05 -0400543int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Luming Yu45bea152005-07-23 04:08:00 -0400545 union acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int err;
547
548 if (!first_ec)
549 return -ENODEV;
550
551 ec = acpi_driver_data(first_ec);
552
553 err = acpi_ec_write(ec, addr, val);
554
555 return err;
556}
Len Brown50526df2005-08-11 17:32:05 -0400557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558EXPORT_SYMBOL(ec_write);
559
Len Brown50526df2005-08-11 17:32:05 -0400560static int acpi_ec_query(union acpi_ec *ec, u32 * data)
Luming Yu45bea152005-07-23 04:08:00 -0400561{
Len Brown02b28a32005-12-05 16:33:04 -0500562 if (acpi_ec_poll_mode)
563 return acpi_ec_poll_query(ec, data);
Luming Yu45bea152005-07-23 04:08:00 -0400564 else
Len Brown02b28a32005-12-05 16:33:04 -0500565 return acpi_ec_intr_query(ec, data);
Luming Yu45bea152005-07-23 04:08:00 -0400566}
Len Brown02b28a32005-12-05 16:33:04 -0500567static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
Luming Yu45bea152005-07-23 04:08:00 -0400568{
Len Brown50526df2005-08-11 17:32:05 -0400569 int result = 0;
570 acpi_status status = AE_OK;
571 unsigned long flags = 0;
572 u32 glk = 0;
Luming Yu45bea152005-07-23 04:08:00 -0400573
574 ACPI_FUNCTION_TRACE("acpi_ec_query");
575
576 if (!ec || !data)
577 return_VALUE(-EINVAL);
578
579 *data = 0;
580
581 if (ec->common.global_lock) {
582 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
583 if (ACPI_FAILURE(status))
584 return_VALUE(-ENODEV);
585 }
586
587 /*
588 * Query the EC to find out which _Qxx method we need to evaluate.
589 * Note that successful completion of the query causes the ACPI_EC_SCI
590 * bit to be cleared (and thus clearing the interrupt source).
591 */
Len Brown02b28a32005-12-05 16:33:04 -0500592 spin_lock_irqsave(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400593
Len Brown50526df2005-08-11 17:32:05 -0400594 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
595 &ec->common.command_addr);
Luming Yu45bea152005-07-23 04:08:00 -0400596 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
597 if (result)
598 goto end;
599
600 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
601 if (!*data)
602 result = -ENODATA;
603
Len Brown50526df2005-08-11 17:32:05 -0400604 end:
Len Brown02b28a32005-12-05 16:33:04 -0500605 spin_unlock_irqrestore(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400606
607 if (ec->common.global_lock)
608 acpi_release_global_lock(glk);
609
610 return_VALUE(result);
611}
Len Brown02b28a32005-12-05 16:33:04 -0500612static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Len Brown50526df2005-08-11 17:32:05 -0400614 int status = 0;
615 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 ACPI_FUNCTION_TRACE("acpi_ec_query");
618
619 if (!ec || !data)
620 return_VALUE(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *data = 0;
622
Luming Yu45bea152005-07-23 04:08:00 -0400623 if (ec->common.global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
625 if (ACPI_FAILURE(status))
626 return_VALUE(-ENODEV);
627 }
628
Len Brown02b28a32005-12-05 16:33:04 -0500629 down(&ec->intr.sem);
Luming Yu716e0842005-08-10 01:40:00 -0400630
631 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
632 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500633 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500634 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
637 * Query the EC to find out which _Qxx method we need to evaluate.
638 * Note that successful completion of the query causes the ACPI_EC_SCI
639 * bit to be cleared (and thus clearing the interrupt source).
640 */
Len Brown50526df2005-08-11 17:32:05 -0400641 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
642 &ec->common.command_addr);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500643 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
Len Brown50526df2005-08-11 17:32:05 -0400644 if (status) {
Len Brown1e8df532005-12-05 16:47:46 -0500645 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto end;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500647 }
648
Luming Yu45bea152005-07-23 04:08:00 -0400649 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!*data)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500651 status = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Len Brown50526df2005-08-11 17:32:05 -0400653 end:
Len Brown02b28a32005-12-05 16:33:04 -0500654 up(&ec->intr.sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Luming Yu45bea152005-07-23 04:08:00 -0400656 if (ec->common.global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 acpi_release_global_lock(glk);
658
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500659 return_VALUE(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* --------------------------------------------------------------------------
663 Event Management
664 -------------------------------------------------------------------------- */
665
Luming Yu45bea152005-07-23 04:08:00 -0400666union acpi_ec_query_data {
Len Brown50526df2005-08-11 17:32:05 -0400667 acpi_handle handle;
668 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
Len Brown50526df2005-08-11 17:32:05 -0400671static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Len Brown02b28a32005-12-05 16:33:04 -0500673 if (acpi_ec_poll_mode)
674 acpi_ec_gpe_poll_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400675 else
Len Brown02b28a32005-12-05 16:33:04 -0500676 acpi_ec_gpe_intr_query(ec_cxt);
Luming Yu45bea152005-07-23 04:08:00 -0400677}
678
Len Brown02b28a32005-12-05 16:33:04 -0500679static void acpi_ec_gpe_poll_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400680{
Len Brown50526df2005-08-11 17:32:05 -0400681 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
682 u32 value = 0;
683 unsigned long flags = 0;
684 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
685 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
686 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
687 };
Luming Yu45bea152005-07-23 04:08:00 -0400688
689 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
690
691 if (!ec_cxt)
692 goto end;
693
Len Brown02b28a32005-12-05 16:33:04 -0500694 spin_lock_irqsave(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400695 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
Len Brown02b28a32005-12-05 16:33:04 -0500696 spin_unlock_irqrestore(&ec->poll.lock, flags);
Luming Yu45bea152005-07-23 04:08:00 -0400697
698 /* TBD: Implement asynch events!
699 * NOTE: All we care about are EC-SCI's. Other EC events are
700 * handled via polling (yuck!). This is because some systems
701 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
702 * a purely interrupt-driven approach (grumble, grumble).
703 */
704 if (!(value & ACPI_EC_FLAG_SCI))
705 goto end;
706
707 if (acpi_ec_query(ec, &value))
708 goto end;
709
710 object_name[2] = hex[((value >> 4) & 0x0F)];
711 object_name[3] = hex[(value & 0x0F)];
712
713 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
714
715 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
716
Len Brown50526df2005-08-11 17:32:05 -0400717 end:
Luming Yu45bea152005-07-23 04:08:00 -0400718 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
719}
Len Brown02b28a32005-12-05 16:33:04 -0500720static void acpi_ec_gpe_intr_query(void *ec_cxt)
Luming Yu45bea152005-07-23 04:08:00 -0400721{
Len Brown50526df2005-08-11 17:32:05 -0400722 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
723 u32 value;
724 int result = -ENODATA;
725 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
726 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
727 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
731
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500732 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
733 result = acpi_ec_query(ec, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500735 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto end;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 object_name[2] = hex[((value >> 4) & 0x0F)];
739 object_name[3] = hex[(value & 0x0F)];
740
741 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
742
Luming Yu45bea152005-07-23 04:08:00 -0400743 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
Len Brown50526df2005-08-11 17:32:05 -0400744 end:
Len Brown02b28a32005-12-05 16:33:04 -0500745 atomic_dec(&ec->intr.pending_gpe);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500746 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Len Brown50526df2005-08-11 17:32:05 -0400749static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Len Brown02b28a32005-12-05 16:33:04 -0500751 if (acpi_ec_poll_mode)
752 return acpi_ec_gpe_poll_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400753 else
Len Brown02b28a32005-12-05 16:33:04 -0500754 return acpi_ec_gpe_intr_handler(data);
Luming Yu45bea152005-07-23 04:08:00 -0400755}
Len Brown02b28a32005-12-05 16:33:04 -0500756static u32 acpi_ec_gpe_poll_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400757{
Len Brown50526df2005-08-11 17:32:05 -0400758 acpi_status status = AE_OK;
759 union acpi_ec *ec = (union acpi_ec *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!ec)
762 return ACPI_INTERRUPT_NOT_HANDLED;
763
Luming Yu45bea152005-07-23 04:08:00 -0400764 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
765
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400766 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
Luming Yu45bea152005-07-23 04:08:00 -0400767
768 if (status == AE_OK)
769 return ACPI_INTERRUPT_HANDLED;
770 else
771 return ACPI_INTERRUPT_NOT_HANDLED;
772}
Len Brown02b28a32005-12-05 16:33:04 -0500773static u32 acpi_ec_gpe_intr_handler(void *data)
Luming Yu45bea152005-07-23 04:08:00 -0400774{
Len Brown50526df2005-08-11 17:32:05 -0400775 acpi_status status = AE_OK;
776 u32 value;
777 union acpi_ec *ec = (union acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400778
779 if (!ec)
780 return ACPI_INTERRUPT_NOT_HANDLED;
781
Luming Yu716e0842005-08-10 01:40:00 -0400782 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500783 value = acpi_ec_read_status(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Len Brown02b28a32005-12-05 16:33:04 -0500785 switch (ec->intr.expect_event) {
Luming Yu716e0842005-08-10 01:40:00 -0400786 case ACPI_EC_EVENT_OBF:
787 if (!(value & ACPI_EC_FLAG_OBF))
788 break;
789 case ACPI_EC_EVENT_IBE:
790 if ((value & ACPI_EC_FLAG_IBF))
791 break;
Len Brown02b28a32005-12-05 16:33:04 -0500792 ec->intr.expect_event = 0;
793 wake_up(&ec->intr.wait);
Luming Yu716e0842005-08-10 01:40:00 -0400794 return ACPI_INTERRUPT_HANDLED;
795 default:
796 break;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500797 }
798
Len Brown50526df2005-08-11 17:32:05 -0400799 if (value & ACPI_EC_FLAG_SCI) {
Len Brown02b28a32005-12-05 16:33:04 -0500800 atomic_add(1, &ec->intr.pending_gpe);
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400801 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
Len Brown50526df2005-08-11 17:32:05 -0400802 acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400803 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400804 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
805 }
Luming Yu45bea152005-07-23 04:08:00 -0400806 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500807 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400808 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/* --------------------------------------------------------------------------
812 Address Space Management
813 -------------------------------------------------------------------------- */
814
815static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400816acpi_ec_space_setup(acpi_handle region_handle,
817 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 /*
820 * The EC object is in the handler context and is needed
821 * when calling the acpi_ec_space_handler.
822 */
Len Brown50526df2005-08-11 17:32:05 -0400823 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
824 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 return AE_OK;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400830acpi_ec_space_handler(u32 function,
831 acpi_physical_address address,
832 u32 bit_width,
833 acpi_integer * value,
834 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Len Brown50526df2005-08-11 17:32:05 -0400836 int result = 0;
837 union acpi_ec *ec = NULL;
838 u64 temp = *value;
839 acpi_integer f_v = 0;
840 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
843
844 if ((address > 0xFF) || !value || !handler_context)
845 return_VALUE(AE_BAD_PARAMETER);
846
Luming Yufa9cd542005-03-19 01:54:47 -0500847 if (bit_width != 8 && acpi_strict) {
Len Brown50526df2005-08-11 17:32:05 -0400848 printk(KERN_WARNING PREFIX
849 "acpi_ec_space_handler: bit_width should be 8\n");
Luming Yufa9cd542005-03-19 01:54:47 -0500850 return_VALUE(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Len Brown50526df2005-08-11 17:32:05 -0400853 ec = (union acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Len Brown50526df2005-08-11 17:32:05 -0400855 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 switch (function) {
857 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500858 temp = 0;
Len Brown50526df2005-08-11 17:32:05 -0400859 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500862 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864 default:
865 result = -EINVAL;
866 goto out;
867 break;
868 }
869
870 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500871 if (bit_width) {
872 if (function == ACPI_READ)
873 f_v |= temp << 8 * i;
874 if (function == ACPI_WRITE)
875 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500877 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto next_byte;
879 }
880
Luming Yufa9cd542005-03-19 01:54:47 -0500881 if (function == ACPI_READ) {
882 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *value = f_v;
884 }
885
Len Brown50526df2005-08-11 17:32:05 -0400886 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 switch (result) {
888 case -EINVAL:
889 return_VALUE(AE_BAD_PARAMETER);
890 break;
891 case -ENODEV:
892 return_VALUE(AE_NOT_FOUND);
893 break;
894 case -ETIME:
895 return_VALUE(AE_TIME);
896 break;
897 default:
898 return_VALUE(AE_OK);
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/* --------------------------------------------------------------------------
903 FS Interface (/proc)
904 -------------------------------------------------------------------------- */
905
Len Brown50526df2005-08-11 17:32:05 -0400906static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Len Brown50526df2005-08-11 17:32:05 -0400908static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Len Brown50526df2005-08-11 17:32:05 -0400910 union acpi_ec *ec = (union acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
913
914 if (!ec)
915 goto end;
916
917 seq_printf(seq, "gpe bit: 0x%02x\n",
Len Brown50526df2005-08-11 17:32:05 -0400918 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Len Brown50526df2005-08-11 17:32:05 -0400920 (u32) ec->common.status_addr.address,
921 (u32) ec->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 seq_printf(seq, "use global lock: %s\n",
Len Brown50526df2005-08-11 17:32:05 -0400923 ec->common.global_lock ? "yes" : "no");
Luming Yu45bea152005-07-23 04:08:00 -0400924 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Len Brown50526df2005-08-11 17:32:05 -0400926 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return_VALUE(0);
928}
929
930static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
931{
932 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
933}
934
935static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400936 .open = acpi_ec_info_open_fs,
937 .read = seq_read,
938 .llseek = seq_lseek,
939 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .owner = THIS_MODULE,
941};
942
Len Brown50526df2005-08-11 17:32:05 -0400943static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Len Brown50526df2005-08-11 17:32:05 -0400945 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
948
949 if (!acpi_device_dir(device)) {
950 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400951 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!acpi_device_dir(device))
953 return_VALUE(-ENODEV);
954 }
955
956 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400957 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!entry)
959 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
Len Brown50526df2005-08-11 17:32:05 -0400960 "Unable to create '%s' fs entry\n",
961 ACPI_EC_FILE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 else {
963 entry->proc_fops = &acpi_ec_info_ops;
964 entry->data = acpi_driver_data(device);
965 entry->owner = THIS_MODULE;
966 }
967
968 return_VALUE(0);
969}
970
Len Brown50526df2005-08-11 17:32:05 -0400971static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
974
975 if (acpi_device_dir(device)) {
976 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
977 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
978 acpi_device_dir(device) = NULL;
979 }
980
981 return_VALUE(0);
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/* --------------------------------------------------------------------------
985 Driver Interface
986 -------------------------------------------------------------------------- */
987
Len Brown02b28a32005-12-05 16:33:04 -0500988static int acpi_ec_poll_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Len Brown50526df2005-08-11 17:32:05 -0400990 int result = 0;
991 acpi_status status = AE_OK;
992 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 ACPI_FUNCTION_TRACE("acpi_ec_add");
995
996 if (!device)
997 return_VALUE(-EINVAL);
998
Luming Yu45bea152005-07-23 04:08:00 -0400999 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!ec)
1001 return_VALUE(-ENOMEM);
Luming Yu45bea152005-07-23 04:08:00 -04001002 memset(ec, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Luming Yu45bea152005-07-23 04:08:00 -04001004 ec->common.handle = device->handle;
1005 ec->common.uid = -1;
Len Brown02b28a32005-12-05 16:33:04 -05001006 spin_lock_init(&ec->poll.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1008 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1009 acpi_driver_data(device) = ec;
1010
1011 /* Use the global lock for all EC transactions? */
Len Brown50526df2005-08-11 17:32:05 -04001012 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1013 &ec->common.global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Jiri Slabyff2fc3e2006-03-28 17:04:00 -05001015 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1016 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1017 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -04001019 ACPI_ADR_SPACE_EC,
1020 &acpi_ec_space_handler);
1021
1022 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1023 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 kfree(ec_ecdt);
1026 }
1027
1028 /* Get GPE bit assignment (EC events). */
1029 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -04001030 status =
1031 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1032 &ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ACPI_FAILURE(status)) {
1034 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown50526df2005-08-11 17:32:05 -04001035 "Error obtaining GPE bit assignment\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 result = -ENODEV;
1037 goto end;
1038 }
1039
1040 result = acpi_ec_add_fs(device);
1041 if (result)
1042 goto end;
1043
Len Brown02b28a32005-12-05 16:33:04 -05001044 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
Len Brown50526df2005-08-11 17:32:05 -04001045 acpi_device_name(device), acpi_device_bid(device),
1046 (u32) ec->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -04001047
1048 if (!first_ec)
1049 first_ec = device;
1050
Len Brown50526df2005-08-11 17:32:05 -04001051 end:
Luming Yu45bea152005-07-23 04:08:00 -04001052 if (result)
1053 kfree(ec);
1054
1055 return_VALUE(result);
1056}
Len Brown02b28a32005-12-05 16:33:04 -05001057static int acpi_ec_intr_add(struct acpi_device *device)
Luming Yu45bea152005-07-23 04:08:00 -04001058{
Len Brown50526df2005-08-11 17:32:05 -04001059 int result = 0;
1060 acpi_status status = AE_OK;
1061 union acpi_ec *ec = NULL;
Luming Yu45bea152005-07-23 04:08:00 -04001062
1063 ACPI_FUNCTION_TRACE("acpi_ec_add");
1064
1065 if (!device)
1066 return_VALUE(-EINVAL);
1067
1068 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1069 if (!ec)
1070 return_VALUE(-ENOMEM);
1071 memset(ec, 0, sizeof(union acpi_ec));
1072
1073 ec->common.handle = device->handle;
1074 ec->common.uid = -1;
Len Brown02b28a32005-12-05 16:33:04 -05001075 atomic_set(&ec->intr.pending_gpe, 0);
1076 atomic_set(&ec->intr.leaving_burst, 1);
1077 init_MUTEX(&ec->intr.sem);
1078 init_waitqueue_head(&ec->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -04001079 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1080 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1081 acpi_driver_data(device) = ec;
1082
1083 /* Use the global lock for all EC transactions? */
Len Brown50526df2005-08-11 17:32:05 -04001084 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1085 &ec->common.global_lock);
Luming Yu45bea152005-07-23 04:08:00 -04001086
Jiri Slabyff2fc3e2006-03-28 17:04:00 -05001087 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1088 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1089 if (ec_ecdt) {
Luming Yu45bea152005-07-23 04:08:00 -04001090 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -04001091 ACPI_ADR_SPACE_EC,
1092 &acpi_ec_space_handler);
Luming Yu45bea152005-07-23 04:08:00 -04001093
Len Brown50526df2005-08-11 17:32:05 -04001094 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1095 &acpi_ec_gpe_handler);
Luming Yu45bea152005-07-23 04:08:00 -04001096
1097 kfree(ec_ecdt);
1098 }
1099
1100 /* Get GPE bit assignment (EC events). */
1101 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -04001102 status =
1103 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1104 &ec->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -04001105 if (ACPI_FAILURE(status)) {
1106 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown50526df2005-08-11 17:32:05 -04001107 "Error obtaining GPE bit assignment\n"));
Luming Yu45bea152005-07-23 04:08:00 -04001108 result = -ENODEV;
1109 goto end;
1110 }
1111
1112 result = acpi_ec_add_fs(device);
1113 if (result)
1114 goto end;
1115
Len Brown02b28a32005-12-05 16:33:04 -05001116 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
Len Brown50526df2005-08-11 17:32:05 -04001117 acpi_device_name(device), acpi_device_bid(device),
1118 (u32) ec->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (!first_ec)
1121 first_ec = device;
1122
Len Brown50526df2005-08-11 17:32:05 -04001123 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (result)
1125 kfree(ec);
1126
1127 return_VALUE(result);
1128}
1129
Len Brown50526df2005-08-11 17:32:05 -04001130static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Len Brown50526df2005-08-11 17:32:05 -04001132 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1135
1136 if (!device)
1137 return_VALUE(-EINVAL);
1138
1139 ec = acpi_driver_data(device);
1140
1141 acpi_ec_remove_fs(device);
1142
1143 kfree(ec);
1144
1145 return_VALUE(0);
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static acpi_status
Len Brown50526df2005-08-11 17:32:05 -04001149acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Len Brown50526df2005-08-11 17:32:05 -04001151 union acpi_ec *ec = (union acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct acpi_generic_address *addr;
1153
Bob Moore50eca3e2005-09-30 19:03:00 -04001154 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return AE_OK;
1156 }
1157
1158 /*
1159 * The first address region returned is the data port, and
1160 * the second address region returned is the status/command
1161 * port.
1162 */
Luming Yu45bea152005-07-23 04:08:00 -04001163 if (ec->common.data_addr.register_bit_width == 0) {
1164 addr = &ec->common.data_addr;
1165 } else if (ec->common.command_addr.register_bit_width == 0) {
1166 addr = &ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
1168 return AE_CTRL_TERMINATE;
1169 }
1170
1171 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1172 addr->register_bit_width = 8;
1173 addr->register_bit_offset = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -04001174 addr->address = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 return AE_OK;
1177}
1178
Len Brown50526df2005-08-11 17:32:05 -04001179static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Len Brown50526df2005-08-11 17:32:05 -04001181 acpi_status status = AE_OK;
1182 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 ACPI_FUNCTION_TRACE("acpi_ec_start");
1185
1186 if (!device)
1187 return_VALUE(-EINVAL);
1188
1189 ec = acpi_driver_data(device);
1190
1191 if (!ec)
1192 return_VALUE(-EINVAL);
1193
1194 /*
1195 * Get I/O port addresses. Convert to GAS format.
1196 */
Luming Yu45bea152005-07-23 04:08:00 -04001197 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001198 acpi_ec_io_ports, ec);
1199 if (ACPI_FAILURE(status)
1200 || ec->common.command_addr.register_bit_width == 0) {
1201 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1202 "Error getting I/O port addresses"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return_VALUE(-ENODEV);
1204 }
1205
Luming Yu45bea152005-07-23 04:08:00 -04001206 ec->common.status_addr = ec->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
Len Brown50526df2005-08-11 17:32:05 -04001209 (u32) ec->common.gpe_bit,
1210 (u32) ec->common.command_addr.address,
1211 (u32) ec->common.data_addr.address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /*
1214 * Install GPE handler
1215 */
Luming Yu45bea152005-07-23 04:08:00 -04001216 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001217 ACPI_GPE_EDGE_TRIGGERED,
1218 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (ACPI_FAILURE(status)) {
1220 return_VALUE(-ENODEV);
1221 }
Len Brown50526df2005-08-11 17:32:05 -04001222 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1223 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Len Brown50526df2005-08-11 17:32:05 -04001225 status = acpi_install_address_space_handler(ec->common.handle,
1226 ACPI_ADR_SPACE_EC,
1227 &acpi_ec_space_handler,
1228 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ACPI_FAILURE(status)) {
Len Brown50526df2005-08-11 17:32:05 -04001230 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1231 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return_VALUE(-ENODEV);
1233 }
1234
1235 return_VALUE(AE_OK);
1236}
1237
Len Brown50526df2005-08-11 17:32:05 -04001238static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Len Brown50526df2005-08-11 17:32:05 -04001240 acpi_status status = AE_OK;
1241 union acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1244
1245 if (!device)
1246 return_VALUE(-EINVAL);
1247
1248 ec = acpi_driver_data(device);
1249
Luming Yu45bea152005-07-23 04:08:00 -04001250 status = acpi_remove_address_space_handler(ec->common.handle,
Len Brown50526df2005-08-11 17:32:05 -04001251 ACPI_ADR_SPACE_EC,
1252 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (ACPI_FAILURE(status))
1254 return_VALUE(-ENODEV);
1255
Len Brown50526df2005-08-11 17:32:05 -04001256 status =
1257 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1258 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (ACPI_FAILURE(status))
1260 return_VALUE(-ENODEV);
1261
1262 return_VALUE(0);
1263}
1264
1265static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -04001266acpi_fake_ecdt_callback(acpi_handle handle,
1267 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Luming Yu45bea152005-07-23 04:08:00 -04001269
Len Brown02b28a32005-12-05 16:33:04 -05001270 if (acpi_ec_poll_mode)
1271 return acpi_fake_ecdt_poll_callback(handle,
Len Brown50526df2005-08-11 17:32:05 -04001272 Level, context, retval);
Luming Yu45bea152005-07-23 04:08:00 -04001273 else
Len Brown02b28a32005-12-05 16:33:04 -05001274 return acpi_fake_ecdt_intr_callback(handle,
Len Brown50526df2005-08-11 17:32:05 -04001275 Level, context, retval);
Luming Yu45bea152005-07-23 04:08:00 -04001276}
1277
1278static acpi_status __init
Len Brown02b28a32005-12-05 16:33:04 -05001279acpi_fake_ecdt_poll_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -04001280 u32 Level, void *context, void **retval)
Luming Yu45bea152005-07-23 04:08:00 -04001281{
Len Brown50526df2005-08-11 17:32:05 -04001282 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001285 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (ACPI_FAILURE(status))
1287 return status;
Luming Yu45bea152005-07-23 04:08:00 -04001288 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Luming Yu45bea152005-07-23 04:08:00 -04001290 ec_ecdt->common.uid = -1;
1291 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Len Brown50526df2005-08-11 17:32:05 -04001293 status =
1294 acpi_evaluate_integer(handle, "_GPE", NULL,
1295 &ec_ecdt->common.gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (ACPI_FAILURE(status))
1297 return status;
Len Brown02b28a32005-12-05 16:33:04 -05001298 spin_lock_init(&ec_ecdt->poll.lock);
Luming Yu45bea152005-07-23 04:08:00 -04001299 ec_ecdt->common.global_lock = TRUE;
1300 ec_ecdt->common.handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Len Brown50526df2005-08-11 17:32:05 -04001302 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1303 (u32) ec_ecdt->common.gpe_bit,
1304 (u32) ec_ecdt->common.command_addr.address,
1305 (u32) ec_ecdt->common.data_addr.address);
Luming Yu45bea152005-07-23 04:08:00 -04001306
1307 return AE_CTRL_TERMINATE;
1308}
1309
1310static acpi_status __init
Len Brown02b28a32005-12-05 16:33:04 -05001311acpi_fake_ecdt_intr_callback(acpi_handle handle,
Len Brown50526df2005-08-11 17:32:05 -04001312 u32 Level, void *context, void **retval)
Luming Yu45bea152005-07-23 04:08:00 -04001313{
Len Brown50526df2005-08-11 17:32:05 -04001314 acpi_status status;
Luming Yu45bea152005-07-23 04:08:00 -04001315
Len Brown02b28a32005-12-05 16:33:04 -05001316 init_MUTEX(&ec_ecdt->intr.sem);
1317 init_waitqueue_head(&ec_ecdt->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -04001318 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -04001319 acpi_ec_io_ports, ec_ecdt);
Luming Yu45bea152005-07-23 04:08:00 -04001320 if (ACPI_FAILURE(status))
1321 return status;
1322 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1323
1324 ec_ecdt->common.uid = -1;
1325 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1326
Len Brown50526df2005-08-11 17:32:05 -04001327 status =
1328 acpi_evaluate_integer(handle, "_GPE", NULL,
1329 &ec_ecdt->common.gpe_bit);
Luming Yu45bea152005-07-23 04:08:00 -04001330 if (ACPI_FAILURE(status))
1331 return status;
1332 ec_ecdt->common.global_lock = TRUE;
1333 ec_ecdt->common.handle = handle;
1334
Len Brown50526df2005-08-11 17:32:05 -04001335 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1336 (u32) ec_ecdt->common.gpe_bit,
1337 (u32) ec_ecdt->common.command_addr.address,
1338 (u32) ec_ecdt->common.data_addr.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 return AE_CTRL_TERMINATE;
1341}
1342
1343/*
1344 * Some BIOS (such as some from Gateway laptops) access EC region very early
1345 * such as in BAT0._INI or EC._INI before an EC device is found and
1346 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1347 * required, but if EC regison is accessed early, it is required.
1348 * The routine tries to workaround the BIOS bug by pre-scan EC device
1349 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1350 * op region (since _REG isn't invoked yet). The assumption is true for
1351 * all systems found.
1352 */
Len Brown50526df2005-08-11 17:32:05 -04001353static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Len Brown50526df2005-08-11 17:32:05 -04001355 acpi_status status;
1356 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1359
Luming Yu45bea152005-07-23 04:08:00 -04001360 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!ec_ecdt) {
1362 ret = -ENOMEM;
1363 goto error;
1364 }
Luming Yu45bea152005-07-23 04:08:00 -04001365 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Len Brown50526df2005-08-11 17:32:05 -04001367 status = acpi_get_devices(ACPI_EC_HID,
1368 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (ACPI_FAILURE(status)) {
1370 kfree(ec_ecdt);
1371 ec_ecdt = NULL;
1372 ret = -ENODEV;
1373 goto error;
1374 }
1375 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001376 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1378 return ret;
1379}
1380
Len Brown50526df2005-08-11 17:32:05 -04001381static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Len Brown02b28a32005-12-05 16:33:04 -05001383 if (acpi_ec_poll_mode)
1384 return acpi_ec_poll_get_real_ecdt();
Luming Yu45bea152005-07-23 04:08:00 -04001385 else
Len Brown02b28a32005-12-05 16:33:04 -05001386 return acpi_ec_intr_get_real_ecdt();
Luming Yu45bea152005-07-23 04:08:00 -04001387}
1388
Len Brown02b28a32005-12-05 16:33:04 -05001389static int __init acpi_ec_poll_get_real_ecdt(void)
Luming Yu45bea152005-07-23 04:08:00 -04001390{
Len Brown50526df2005-08-11 17:32:05 -04001391 acpi_status status;
1392 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -04001393
Len Brown50526df2005-08-11 17:32:05 -04001394 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1395 (struct acpi_table_header **)
1396 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -04001397 if (ACPI_FAILURE(status))
1398 return -ENODEV;
1399
1400 printk(KERN_INFO PREFIX "Found ECDT\n");
1401
1402 /*
1403 * Generate a temporary ec context to use until the namespace is scanned
1404 */
1405 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1406 if (!ec_ecdt)
1407 return -ENOMEM;
1408 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1409
1410 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1411 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1412 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1413 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Len Brown02b28a32005-12-05 16:33:04 -05001414 spin_lock_init(&ec_ecdt->poll.lock);
Luming Yu45bea152005-07-23 04:08:00 -04001415 /* use the GL just to be safe */
1416 ec_ecdt->common.global_lock = TRUE;
1417 ec_ecdt->common.uid = ecdt_ptr->uid;
1418
Len Brown50526df2005-08-11 17:32:05 -04001419 status =
1420 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Luming Yu45bea152005-07-23 04:08:00 -04001421 if (ACPI_FAILURE(status)) {
1422 goto error;
1423 }
1424
1425 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001426 error:
Luming Yu45bea152005-07-23 04:08:00 -04001427 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1428 kfree(ec_ecdt);
1429 ec_ecdt = NULL;
1430
1431 return -ENODEV;
1432}
1433
Len Brown02b28a32005-12-05 16:33:04 -05001434static int __init acpi_ec_intr_get_real_ecdt(void)
Luming Yu45bea152005-07-23 04:08:00 -04001435{
Len Brown50526df2005-08-11 17:32:05 -04001436 acpi_status status;
1437 struct acpi_table_ecdt *ecdt_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Dmitry Torokhov451566f2005-03-19 01:10:05 -05001439 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
Len Brown50526df2005-08-11 17:32:05 -04001440 (struct acpi_table_header **)
1441 &ecdt_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (ACPI_FAILURE(status))
1443 return -ENODEV;
1444
1445 printk(KERN_INFO PREFIX "Found ECDT\n");
1446
1447 /*
1448 * Generate a temporary ec context to use until the namespace is scanned
1449 */
Luming Yu45bea152005-07-23 04:08:00 -04001450 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!ec_ecdt)
1452 return -ENOMEM;
Luming Yu45bea152005-07-23 04:08:00 -04001453 memset(ec_ecdt, 0, sizeof(union acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Len Brown02b28a32005-12-05 16:33:04 -05001455 init_MUTEX(&ec_ecdt->intr.sem);
1456 init_waitqueue_head(&ec_ecdt->intr.wait);
Luming Yu45bea152005-07-23 04:08:00 -04001457 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1458 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1459 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1460 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* use the GL just to be safe */
Luming Yu45bea152005-07-23 04:08:00 -04001462 ec_ecdt->common.global_lock = TRUE;
1463 ec_ecdt->common.uid = ecdt_ptr->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Len Brown50526df2005-08-11 17:32:05 -04001465 status =
1466 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (ACPI_FAILURE(status)) {
1468 goto error;
1469 }
1470
1471 return 0;
Len Brown50526df2005-08-11 17:32:05 -04001472 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1474 kfree(ec_ecdt);
1475 ec_ecdt = NULL;
1476
1477 return -ENODEV;
1478}
1479
1480static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -04001481int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Len Brown50526df2005-08-11 17:32:05 -04001483 acpi_status status;
1484 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 ret = acpi_ec_get_real_ecdt();
1487 /* Try to make a fake ECDT */
1488 if (ret && acpi_fake_ecdt_enabled) {
1489 ret = acpi_ec_fake_ecdt();
1490 }
1491
1492 if (ret)
1493 return 0;
1494
1495 /*
1496 * Install GPE handler
1497 */
Luming Yu45bea152005-07-23 04:08:00 -04001498 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001499 ACPI_GPE_EDGE_TRIGGERED,
1500 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (ACPI_FAILURE(status)) {
1502 goto error;
1503 }
Len Brown50526df2005-08-11 17:32:05 -04001504 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1505 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Len Brown50526df2005-08-11 17:32:05 -04001507 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1508 ACPI_ADR_SPACE_EC,
1509 &acpi_ec_space_handler,
1510 &acpi_ec_space_setup,
1511 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (ACPI_FAILURE(status)) {
Luming Yu45bea152005-07-23 04:08:00 -04001513 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -04001514 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 goto error;
1516 }
1517
1518 return 0;
1519
Len Brown50526df2005-08-11 17:32:05 -04001520 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1522 kfree(ec_ecdt);
1523 ec_ecdt = NULL;
1524
1525 return -ENODEV;
1526}
1527
Len Brown50526df2005-08-11 17:32:05 -04001528static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Len Brown50526df2005-08-11 17:32:05 -04001530 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 ACPI_FUNCTION_TRACE("acpi_ec_init");
1533
1534 if (acpi_disabled)
1535 return_VALUE(0);
1536
1537 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1538 if (!acpi_ec_dir)
1539 return_VALUE(-ENODEV);
1540
1541 /* Now register the driver for the EC */
1542 result = acpi_bus_register_driver(&acpi_ec_driver);
1543 if (result < 0) {
1544 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1545 return_VALUE(-ENODEV);
1546 }
1547
1548 return_VALUE(result);
1549}
1550
1551subsys_initcall(acpi_ec_init);
1552
1553/* EC driver currently not unloadable */
1554#if 0
Len Brown50526df2005-08-11 17:32:05 -04001555static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1558
1559 acpi_bus_unregister_driver(&acpi_ec_driver);
1560
1561 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1562
1563 return_VOID;
1564}
Len Brown50526df2005-08-11 17:32:05 -04001565#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567static int __init acpi_fake_ecdt_setup(char *str)
1568{
1569 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001570 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001574static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001575{
Len Brown02b28a32005-12-05 16:33:04 -05001576 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001577
Len Brown02b28a32005-12-05 16:33:04 -05001578 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001579 return 0;
1580
Len Brown02b28a32005-12-05 16:33:04 -05001581 if (intr) {
1582 acpi_ec_poll_mode = EC_INTR;
1583 acpi_ec_driver.ops.add = acpi_ec_intr_add;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001584 } else {
Len Brown02b28a32005-12-05 16:33:04 -05001585 acpi_ec_poll_mode = EC_POLL;
1586 acpi_ec_driver.ops.add = acpi_ec_poll_add;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001587 }
Len Brown02b28a32005-12-05 16:33:04 -05001588 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001589 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001590}
Len Brown50526df2005-08-11 17:32:05 -04001591
Len Brown53f11d42005-12-05 16:46:36 -05001592__setup("ec_intr=", acpi_ec_set_intr_mode);