ec.c 39.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
 *
 *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
D
Dmitry Torokhov 已提交
34
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
35 36 37 38 39 40
#include <asm/io.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
#include <acpi/actypes.h>

#define _COMPONENT		ACPI_EC_COMPONENT
L
Len Brown 已提交
41
ACPI_MODULE_NAME("acpi_ec")
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49
#define ACPI_EC_COMPONENT		0x00100000
#define ACPI_EC_CLASS			"embedded_controller"
#define ACPI_EC_HID			"PNP0C09"
#define ACPI_EC_DRIVER_NAME		"ACPI Embedded Controller Driver"
#define ACPI_EC_DEVICE_NAME		"Embedded Controller"
#define ACPI_EC_FILE_INFO		"info"
#define ACPI_EC_FLAG_OBF	0x01	/* Output buffer full */
#define ACPI_EC_FLAG_IBF	0x02	/* Input buffer full */
D
Dmitry Torokhov 已提交
50
#define ACPI_EC_FLAG_BURST	0x10	/* burst mode */
L
Linus Torvalds 已提交
51 52 53
#define ACPI_EC_FLAG_SCI	0x20	/* EC-SCI occurred */
#define ACPI_EC_EVENT_OBF	0x01	/* Output buffer full */
#define ACPI_EC_EVENT_IBE	0x02	/* Input buffer empty */
D
Dmitry Torokhov 已提交
54
#define ACPI_EC_DELAY		50	/* Wait 50ms max. during EC ops */
L
Linus Torvalds 已提交
55
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */
L
Len Brown 已提交
56 57
#define ACPI_EC_UDELAY         100	/* Poll @ 100us increments */
#define ACPI_EC_UDELAY_COUNT   1000	/* Wait 10ms max. during EC ops */
L
Linus Torvalds 已提交
58 59
#define ACPI_EC_COMMAND_READ	0x80
#define ACPI_EC_COMMAND_WRITE	0x81
D
Dmitry Torokhov 已提交
60 61
#define ACPI_EC_BURST_ENABLE	0x82
#define ACPI_EC_BURST_DISABLE	0x83
L
Linus Torvalds 已提交
62
#define ACPI_EC_COMMAND_QUERY	0x84
L
Luming Yu 已提交
63 64
#define EC_POLLING		0xFF
#define EC_BURST		0x00
L
Len Brown 已提交
65 66 67 68 69
static int acpi_ec_remove(struct acpi_device *device, int type);
static int acpi_ec_start(struct acpi_device *device);
static int acpi_ec_stop(struct acpi_device *device, int type);
static int acpi_ec_burst_add(struct acpi_device *device);
static int acpi_ec_polling_add(struct acpi_device *device);
L
Linus Torvalds 已提交
70 71

static struct acpi_driver acpi_ec_driver = {
L
Len Brown 已提交
72 73 74 75 76 77 78 79 80
	.name = ACPI_EC_DRIVER_NAME,
	.class = ACPI_EC_CLASS,
	.ids = ACPI_EC_HID,
	.ops = {
		.add = acpi_ec_polling_add,
		.remove = acpi_ec_remove,
		.start = acpi_ec_start,
		.stop = acpi_ec_stop,
		},
L
Linus Torvalds 已提交
81
};
L
Luming Yu 已提交
82 83
union acpi_ec {
	struct {
L
Len Brown 已提交
84 85 86 87 88 89 90 91
		u32 mode;
		acpi_handle handle;
		unsigned long uid;
		unsigned long gpe_bit;
		struct acpi_generic_address status_addr;
		struct acpi_generic_address command_addr;
		struct acpi_generic_address data_addr;
		unsigned long global_lock;
L
Luming Yu 已提交
92 93 94
	} common;

	struct {
L
Len Brown 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
		u32 mode;
		acpi_handle handle;
		unsigned long uid;
		unsigned long gpe_bit;
		struct acpi_generic_address status_addr;
		struct acpi_generic_address command_addr;
		struct acpi_generic_address data_addr;
		unsigned long global_lock;
		unsigned int expect_event;
		atomic_t leaving_burst;	/* 0 : No, 1 : Yes, 2: abort */
		atomic_t pending_gpe;
		struct semaphore sem;
		wait_queue_head_t wait;
	} burst;
L
Luming Yu 已提交
109 110

	struct {
L
Len Brown 已提交
111 112 113 114 115 116 117 118 119 120
		u32 mode;
		acpi_handle handle;
		unsigned long uid;
		unsigned long gpe_bit;
		struct acpi_generic_address status_addr;
		struct acpi_generic_address command_addr;
		struct acpi_generic_address data_addr;
		unsigned long global_lock;
		spinlock_t lock;
	} polling;
L
Linus Torvalds 已提交
121 122
};

L
Len Brown 已提交
123
static int acpi_ec_polling_wait(union acpi_ec *ec, u8 event);
L
Luming Yu 已提交
124
static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
L
Len Brown 已提交
125 126 127 128 129 130 131 132 133 134
static int acpi_ec_polling_read(union acpi_ec *ec, u8 address, u32 * data);
static int acpi_ec_burst_read(union acpi_ec *ec, u8 address, u32 * data);
static int acpi_ec_polling_write(union acpi_ec *ec, u8 address, u8 data);
static int acpi_ec_burst_write(union acpi_ec *ec, u8 address, u8 data);
static int acpi_ec_polling_query(union acpi_ec *ec, u32 * data);
static int acpi_ec_burst_query(union acpi_ec *ec, u32 * data);
static void acpi_ec_gpe_polling_query(void *ec_cxt);
static void acpi_ec_gpe_burst_query(void *ec_cxt);
static u32 acpi_ec_gpe_polling_handler(void *data);
static u32 acpi_ec_gpe_burst_handler(void *data);
L
Luming Yu 已提交
135
static acpi_status __init
L
Len Brown 已提交
136 137
acpi_fake_ecdt_polling_callback(acpi_handle handle,
				u32 Level, void *context, void **retval);
L
Luming Yu 已提交
138 139

static acpi_status __init
L
Len Brown 已提交
140 141 142 143 144
acpi_fake_ecdt_burst_callback(acpi_handle handle,
			      u32 Level, void *context, void **retval);

static int __init acpi_ec_polling_get_real_ecdt(void);
static int __init acpi_ec_burst_get_real_ecdt(void);
L
Linus Torvalds 已提交
145
/* If we find an EC via the ECDT, we need to keep a ptr to its context */
L
Len Brown 已提交
146
static union acpi_ec *ec_ecdt;
L
Linus Torvalds 已提交
147 148 149

/* External interfaces use first EC only, so remember */
static struct acpi_device *first_ec;
150
static int acpi_ec_polling_mode = EC_POLLING;
L
Linus Torvalds 已提交
151 152 153 154 155

/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

L
Luming Yu 已提交
156
static inline u32 acpi_ec_read_status(union acpi_ec *ec)
L
Linus Torvalds 已提交
157
{
L
Len Brown 已提交
158
	u32 status = 0;
L
Linus Torvalds 已提交
159

L
Luming Yu 已提交
160
	acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
D
Dmitry Torokhov 已提交
161 162 163
	return status;
}

L
Len Brown 已提交
164
static int acpi_ec_wait(union acpi_ec *ec, u8 event)
L
Luming Yu 已提交
165
{
L
Len Brown 已提交
166 167
	if (acpi_ec_polling_mode)
		return acpi_ec_polling_wait(ec, event);
L
Luming Yu 已提交
168
	else
L
Len Brown 已提交
169
		return acpi_ec_burst_wait(ec, event);
L
Luming Yu 已提交
170 171
}

L
Len Brown 已提交
172
static int acpi_ec_polling_wait(union acpi_ec *ec, u8 event)
L
Luming Yu 已提交
173
{
L
Len Brown 已提交
174 175
	u32 acpi_ec_status = 0;
	u32 i = ACPI_EC_UDELAY_COUNT;
L
Luming Yu 已提交
176 177 178 179 180 181 182 183

	if (!ec)
		return -EINVAL;

	/* Poll the EC status register waiting for the event to occur. */
	switch (event) {
	case ACPI_EC_EVENT_OBF:
		do {
L
Len Brown 已提交
184 185
			acpi_hw_low_level_read(8, &acpi_ec_status,
					       &ec->common.status_addr);
L
Luming Yu 已提交
186 187 188
			if (acpi_ec_status & ACPI_EC_FLAG_OBF)
				return 0;
			udelay(ACPI_EC_UDELAY);
L
Len Brown 已提交
189
		} while (--i > 0);
L
Luming Yu 已提交
190 191 192
		break;
	case ACPI_EC_EVENT_IBE:
		do {
L
Len Brown 已提交
193 194
			acpi_hw_low_level_read(8, &acpi_ec_status,
					       &ec->common.status_addr);
L
Luming Yu 已提交
195 196 197
			if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
				return 0;
			udelay(ACPI_EC_UDELAY);
L
Len Brown 已提交
198
		} while (--i > 0);
L
Luming Yu 已提交
199 200 201 202 203 204 205 206
		break;
	default:
		return -EINVAL;
	}

	return -ETIME;
}
static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
D
Dmitry Torokhov 已提交
207
{
L
Len Brown 已提交
208
	int result = 0;
D
Dmitry Torokhov 已提交
209 210

	ACPI_FUNCTION_TRACE("acpi_ec_wait");
L
Linus Torvalds 已提交
211

L
Luming Yu 已提交
212
	ec->burst.expect_event = event;
D
Dmitry Torokhov 已提交
213 214
	smp_mb();

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	switch (event) {
	case ACPI_EC_EVENT_OBF:
		if (acpi_ec_read_status(ec) & event) {
			ec->burst.expect_event = 0;
			return_VALUE(0);
		}
		break;

	case ACPI_EC_EVENT_IBE:
		if (~acpi_ec_read_status(ec) & event) {
			ec->burst.expect_event = 0;
			return_VALUE(0);
		}
		break;
	}

	result = wait_event_timeout(ec->burst.wait,
L
Len Brown 已提交
232 233 234
				    !ec->burst.expect_event,
				    msecs_to_jiffies(ACPI_EC_DELAY));

L
Luming Yu 已提交
235
	ec->burst.expect_event = 0;
D
Dmitry Torokhov 已提交
236 237 238 239 240 241 242
	smp_mb();

	/*
	 * Verify that the event in question has actually happened by
	 * querying EC status. Do the check even if operation timed-out
	 * to make sure that we did not miss interrupt.
	 */
L
Linus Torvalds 已提交
243 244
	switch (event) {
	case ACPI_EC_EVENT_OBF:
D
Dmitry Torokhov 已提交
245 246
		if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
			return_VALUE(0);
L
Linus Torvalds 已提交
247
		break;
D
Dmitry Torokhov 已提交
248

L
Linus Torvalds 已提交
249
	case ACPI_EC_EVENT_IBE:
D
Dmitry Torokhov 已提交
250 251
		if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
			return_VALUE(0);
L
Linus Torvalds 已提交
252 253 254
		break;
	}

D
Dmitry Torokhov 已提交
255
	return_VALUE(-ETIME);
L
Linus Torvalds 已提交
256 257
}

L
Len Brown 已提交
258
static int acpi_ec_enter_burst_mode(union acpi_ec *ec)
D
Dmitry Torokhov 已提交
259
{
L
Len Brown 已提交
260 261
	u32 tmp = 0;
	int status = 0;
D
Dmitry Torokhov 已提交
262 263 264 265

	ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");

	status = acpi_ec_read_status(ec);
L
Len Brown 已提交
266
	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
267
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
L
Len Brown 已提交
268
		if (status)
269
			goto end;
L
Len Brown 已提交
270 271
		acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
					&ec->common.command_addr);
D
Dmitry Torokhov 已提交
272
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
273
		if (status)
D
Dmitry Torokhov 已提交
274
			return_VALUE(-EINVAL);
L
Luming Yu 已提交
275
		acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
L
Len Brown 已提交
276
		if (tmp != 0x90) {	/* Burst ACK byte */
D
Dmitry Torokhov 已提交
277 278
			return_VALUE(-EINVAL);
		}
279 280
	}

L
Len Brown 已提交
281
	atomic_set(&ec->burst.leaving_burst, 0);
D
Dmitry Torokhov 已提交
282
	return_VALUE(0);
L
Len Brown 已提交
283
      end:
284 285
	printk("Error in acpi_ec_wait\n");
	return_VALUE(-1);
D
Dmitry Torokhov 已提交
286 287
}

L
Len Brown 已提交
288
static int acpi_ec_leave_burst_mode(union acpi_ec *ec)
D
Dmitry Torokhov 已提交
289 290 291 292
{

	ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");

293
	atomic_set(&ec->burst.leaving_burst, 1);
D
Dmitry Torokhov 已提交
294 295 296
	return_VALUE(0);
}

L
Len Brown 已提交
297
static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
L
Luming Yu 已提交
298
{
L
Len Brown 已提交
299
	if (acpi_ec_polling_mode)
L
Luming Yu 已提交
300 301 302 303
		return acpi_ec_polling_read(ec, address, data);
	else
		return acpi_ec_burst_read(ec, address, data);
}
L
Len Brown 已提交
304
static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
L
Luming Yu 已提交
305
{
L
Len Brown 已提交
306
	if (acpi_ec_polling_mode)
L
Luming Yu 已提交
307 308 309 310
		return acpi_ec_polling_write(ec, address, data);
	else
		return acpi_ec_burst_write(ec, address, data);
}
L
Len Brown 已提交
311
static int acpi_ec_polling_read(union acpi_ec *ec, u8 address, u32 * data)
L
Luming Yu 已提交
312
{
L
Len Brown 已提交
313 314 315 316
	acpi_status status = AE_OK;
	int result = 0;
	unsigned long flags = 0;
	u32 glk = 0;
L
Luming Yu 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

	ACPI_FUNCTION_TRACE("acpi_ec_read");

	if (!ec || !data)
		return_VALUE(-EINVAL);

	*data = 0;

	if (ec->common.global_lock) {
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}

	spin_lock_irqsave(&ec->polling.lock, flags);

L
Len Brown 已提交
333 334
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
				&ec->common.command_addr);
L
Luming Yu 已提交
335 336 337 338 339 340 341 342 343 344 345 346
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (result)
		goto end;

	acpi_hw_low_level_write(8, address, &ec->common.data_addr);
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
	if (result)
		goto end;

	acpi_hw_low_level_read(8, data, &ec->common.data_addr);

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
L
Len Brown 已提交
347 348 349
			  *data, address));

      end:
L
Luming Yu 已提交
350 351 352 353 354 355 356 357
	spin_unlock_irqrestore(&ec->polling.lock, flags);

	if (ec->common.global_lock)
		acpi_release_global_lock(glk);

	return_VALUE(result);
}

L
Len Brown 已提交
358
static int acpi_ec_polling_write(union acpi_ec *ec, u8 address, u8 data)
L
Luming Yu 已提交
359
{
L
Len Brown 已提交
360 361 362 363
	int result = 0;
	acpi_status status = AE_OK;
	unsigned long flags = 0;
	u32 glk = 0;
L
Luming Yu 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377

	ACPI_FUNCTION_TRACE("acpi_ec_write");

	if (!ec)
		return_VALUE(-EINVAL);

	if (ec->common.global_lock) {
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}

	spin_lock_irqsave(&ec->polling.lock, flags);

L
Len Brown 已提交
378 379
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
				&ec->common.command_addr);
L
Luming Yu 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (result)
		goto end;

	acpi_hw_low_level_write(8, address, &ec->common.data_addr);
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (result)
		goto end;

	acpi_hw_low_level_write(8, data, &ec->common.data_addr);
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (result)
		goto end;

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
L
Len Brown 已提交
395
			  data, address));
L
Luming Yu 已提交
396

L
Len Brown 已提交
397
      end:
L
Luming Yu 已提交
398 399 400 401 402 403 404 405
	spin_unlock_irqrestore(&ec->polling.lock, flags);

	if (ec->common.global_lock)
		acpi_release_global_lock(glk);

	return_VALUE(result);
}

L
Len Brown 已提交
406
static int acpi_ec_burst_read(union acpi_ec *ec, u8 address, u32 * data)
L
Linus Torvalds 已提交
407
{
L
Len Brown 已提交
408 409
	int status = 0;
	u32 glk;
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417

	ACPI_FUNCTION_TRACE("acpi_ec_read");

	if (!ec || !data)
		return_VALUE(-EINVAL);

	*data = 0;

L
Luming Yu 已提交
418
	if (ec->common.global_lock) {
L
Linus Torvalds 已提交
419 420 421 422
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}
D
Dmitry Torokhov 已提交
423 424

	WARN_ON(in_interrupt());
L
Luming Yu 已提交
425
	down(&ec->burst.sem);
D
Dmitry Torokhov 已提交
426

427 428 429 430
	acpi_ec_enter_burst_mode(ec);
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (status) {
		printk("read EC, IB not empty\n");
D
Dmitry Torokhov 已提交
431
		goto end;
432
	}
L
Len Brown 已提交
433 434
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
				&ec->common.command_addr);
D
Dmitry Torokhov 已提交
435 436
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (status) {
437
		printk("read EC, IB not empty\n");
D
Dmitry Torokhov 已提交
438
	}
L
Linus Torvalds 已提交
439

L
Luming Yu 已提交
440
	acpi_hw_low_level_write(8, address, &ec->common.data_addr);
L
Len Brown 已提交
441 442
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
	if (status) {
443
		printk("read EC, OB not full\n");
L
Linus Torvalds 已提交
444
		goto end;
D
Dmitry Torokhov 已提交
445
	}
L
Luming Yu 已提交
446
	acpi_hw_low_level_read(8, data, &ec->common.data_addr);
L
Linus Torvalds 已提交
447
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
L
Len Brown 已提交
448 449 450
			  *data, address));

      end:
D
Dmitry Torokhov 已提交
451
	acpi_ec_leave_burst_mode(ec);
L
Luming Yu 已提交
452
	up(&ec->burst.sem);
L
Linus Torvalds 已提交
453

L
Luming Yu 已提交
454
	if (ec->common.global_lock)
L
Linus Torvalds 已提交
455 456
		acpi_release_global_lock(glk);

D
Dmitry Torokhov 已提交
457
	return_VALUE(status);
L
Linus Torvalds 已提交
458 459
}

L
Len Brown 已提交
460
static int acpi_ec_burst_write(union acpi_ec *ec, u8 address, u8 data)
L
Linus Torvalds 已提交
461
{
L
Len Brown 已提交
462 463
	int status = 0;
	u32 glk;
L
Linus Torvalds 已提交
464 465 466 467 468

	ACPI_FUNCTION_TRACE("acpi_ec_write");

	if (!ec)
		return_VALUE(-EINVAL);
469

L
Luming Yu 已提交
470
	if (ec->common.global_lock) {
L
Linus Torvalds 已提交
471 472 473 474 475
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}

D
Dmitry Torokhov 已提交
476
	WARN_ON(in_interrupt());
L
Luming Yu 已提交
477
	down(&ec->burst.sem);
D
Dmitry Torokhov 已提交
478

479
	acpi_ec_enter_burst_mode(ec);
D
Dmitry Torokhov 已提交
480

481
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
L
Len Brown 已提交
482
	if (status) {
483
		printk("write EC, IB not empty\n");
D
Dmitry Torokhov 已提交
484
	}
L
Len Brown 已提交
485 486
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
				&ec->common.command_addr);
D
Dmitry Torokhov 已提交
487
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
488
	if (status) {
L
Len Brown 已提交
489
		printk("write EC, IB not empty\n");
D
Dmitry Torokhov 已提交
490
	}
L
Linus Torvalds 已提交
491

L
Luming Yu 已提交
492
	acpi_hw_low_level_write(8, address, &ec->common.data_addr);
D
Dmitry Torokhov 已提交
493
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
L
Len Brown 已提交
494
	if (status) {
495
		printk("write EC, IB not empty\n");
D
Dmitry Torokhov 已提交
496
	}
L
Linus Torvalds 已提交
497

L
Luming Yu 已提交
498
	acpi_hw_low_level_write(8, data, &ec->common.data_addr);
L
Linus Torvalds 已提交
499 500

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
L
Len Brown 已提交
501
			  data, address));
L
Linus Torvalds 已提交
502

D
Dmitry Torokhov 已提交
503
	acpi_ec_leave_burst_mode(ec);
L
Luming Yu 已提交
504
	up(&ec->burst.sem);
L
Linus Torvalds 已提交
505

L
Luming Yu 已提交
506
	if (ec->common.global_lock)
L
Linus Torvalds 已提交
507 508
		acpi_release_global_lock(glk);

D
Dmitry Torokhov 已提交
509
	return_VALUE(status);
L
Linus Torvalds 已提交
510 511 512 513 514
}

/*
 * Externally callable EC access functions. For now, assume 1 EC only
 */
L
Len Brown 已提交
515
int ec_read(u8 addr, u8 * val)
L
Linus Torvalds 已提交
516
{
L
Luming Yu 已提交
517
	union acpi_ec *ec;
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530
	int err;
	u32 temp_data;

	if (!first_ec)
		return -ENODEV;

	ec = acpi_driver_data(first_ec);

	err = acpi_ec_read(ec, addr, &temp_data);

	if (!err) {
		*val = temp_data;
		return 0;
L
Len Brown 已提交
531
	} else
L
Linus Torvalds 已提交
532 533
		return err;
}
L
Len Brown 已提交
534

L
Linus Torvalds 已提交
535 536
EXPORT_SYMBOL(ec_read);

L
Len Brown 已提交
537
int ec_write(u8 addr, u8 val)
L
Linus Torvalds 已提交
538
{
L
Luming Yu 已提交
539
	union acpi_ec *ec;
L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547 548 549 550
	int err;

	if (!first_ec)
		return -ENODEV;

	ec = acpi_driver_data(first_ec);

	err = acpi_ec_write(ec, addr, val);

	return err;
}
L
Len Brown 已提交
551

L
Linus Torvalds 已提交
552 553
EXPORT_SYMBOL(ec_write);

L
Len Brown 已提交
554
static int acpi_ec_query(union acpi_ec *ec, u32 * data)
L
Luming Yu 已提交
555
{
L
Len Brown 已提交
556
	if (acpi_ec_polling_mode)
L
Luming Yu 已提交
557 558 559 560
		return acpi_ec_polling_query(ec, data);
	else
		return acpi_ec_burst_query(ec, data);
}
L
Len Brown 已提交
561
static int acpi_ec_polling_query(union acpi_ec *ec, u32 * data)
L
Luming Yu 已提交
562
{
L
Len Brown 已提交
563 564 565 566
	int result = 0;
	acpi_status status = AE_OK;
	unsigned long flags = 0;
	u32 glk = 0;
L
Luming Yu 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

	ACPI_FUNCTION_TRACE("acpi_ec_query");

	if (!ec || !data)
		return_VALUE(-EINVAL);

	*data = 0;

	if (ec->common.global_lock) {
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}

	/*
	 * Query the EC to find out which _Qxx method we need to evaluate.
	 * Note that successful completion of the query causes the ACPI_EC_SCI
	 * bit to be cleared (and thus clearing the interrupt source).
	 */
	spin_lock_irqsave(&ec->polling.lock, flags);

L
Len Brown 已提交
588 589
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
				&ec->common.command_addr);
L
Luming Yu 已提交
590 591 592 593 594 595 596 597
	result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
	if (result)
		goto end;

	acpi_hw_low_level_read(8, data, &ec->common.data_addr);
	if (!*data)
		result = -ENODATA;

L
Len Brown 已提交
598
      end:
L
Luming Yu 已提交
599 600 601 602 603 604 605
	spin_unlock_irqrestore(&ec->polling.lock, flags);

	if (ec->common.global_lock)
		acpi_release_global_lock(glk);

	return_VALUE(result);
}
L
Len Brown 已提交
606
static int acpi_ec_burst_query(union acpi_ec *ec, u32 * data)
L
Linus Torvalds 已提交
607
{
L
Len Brown 已提交
608 609
	int status = 0;
	u32 glk;
L
Linus Torvalds 已提交
610 611 612 613 614 615 616

	ACPI_FUNCTION_TRACE("acpi_ec_query");

	if (!ec || !data)
		return_VALUE(-EINVAL);
	*data = 0;

L
Luming Yu 已提交
617
	if (ec->common.global_lock) {
L
Linus Torvalds 已提交
618 619 620 621 622
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
			return_VALUE(-ENODEV);
	}

L
Luming Yu 已提交
623
	down(&ec->burst.sem);
624 625 626 627

	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
	if (status) {
		printk("query EC, IB not empty\n");
D
Dmitry Torokhov 已提交
628
		goto end;
629
	}
L
Linus Torvalds 已提交
630 631 632 633 634
	/*
	 * Query the EC to find out which _Qxx method we need to evaluate.
	 * Note that successful completion of the query causes the ACPI_EC_SCI
	 * bit to be cleared (and thus clearing the interrupt source).
	 */
L
Len Brown 已提交
635 636
	acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
				&ec->common.command_addr);
D
Dmitry Torokhov 已提交
637
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
L
Len Brown 已提交
638
	if (status) {
639
		printk("query EC, OB not full\n");
L
Linus Torvalds 已提交
640
		goto end;
D
Dmitry Torokhov 已提交
641 642
	}

L
Luming Yu 已提交
643
	acpi_hw_low_level_read(8, data, &ec->common.data_addr);
L
Linus Torvalds 已提交
644
	if (!*data)
D
Dmitry Torokhov 已提交
645
		status = -ENODATA;
L
Linus Torvalds 已提交
646

L
Len Brown 已提交
647
      end:
L
Luming Yu 已提交
648
	up(&ec->burst.sem);
L
Linus Torvalds 已提交
649

L
Luming Yu 已提交
650
	if (ec->common.global_lock)
L
Linus Torvalds 已提交
651 652
		acpi_release_global_lock(glk);

D
Dmitry Torokhov 已提交
653
	return_VALUE(status);
L
Linus Torvalds 已提交
654 655 656 657 658 659
}

/* --------------------------------------------------------------------------
                                Event Management
   -------------------------------------------------------------------------- */

L
Luming Yu 已提交
660
union acpi_ec_query_data {
L
Len Brown 已提交
661 662
	acpi_handle handle;
	u8 data;
L
Linus Torvalds 已提交
663 664
};

L
Len Brown 已提交
665
static void acpi_ec_gpe_query(void *ec_cxt)
L
Linus Torvalds 已提交
666
{
L
Len Brown 已提交
667
	if (acpi_ec_polling_mode)
L
Luming Yu 已提交
668 669 670 671 672
		acpi_ec_gpe_polling_query(ec_cxt);
	else
		acpi_ec_gpe_burst_query(ec_cxt);
}

L
Len Brown 已提交
673
static void acpi_ec_gpe_polling_query(void *ec_cxt)
L
Luming Yu 已提交
674
{
L
Len Brown 已提交
675 676 677 678 679 680 681
	union acpi_ec *ec = (union acpi_ec *)ec_cxt;
	u32 value = 0;
	unsigned long flags = 0;
	static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
	const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
L
Luming Yu 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

	ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");

	if (!ec_cxt)
		goto end;

	spin_lock_irqsave(&ec->polling.lock, flags);
	acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
	spin_unlock_irqrestore(&ec->polling.lock, flags);

	/* TBD: Implement asynch events!
	 * NOTE: All we care about are EC-SCI's.  Other EC events are
	 * handled via polling (yuck!).  This is because some systems
	 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
	 *  a purely interrupt-driven approach (grumble, grumble).
	 */
	if (!(value & ACPI_EC_FLAG_SCI))
		goto end;

	if (acpi_ec_query(ec, &value))
		goto end;

	object_name[2] = hex[((value >> 4) & 0x0F)];
	object_name[3] = hex[(value & 0x0F)];

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));

	acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);

L
Len Brown 已提交
711
      end:
L
Luming Yu 已提交
712 713
	acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
}
L
Len Brown 已提交
714
static void acpi_ec_gpe_burst_query(void *ec_cxt)
L
Luming Yu 已提交
715
{
L
Len Brown 已提交
716 717 718 719 720 721 722
	union acpi_ec *ec = (union acpi_ec *)ec_cxt;
	u32 value;
	int result = -ENODATA;
	static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
	const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
L
Linus Torvalds 已提交
723 724 725

	ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");

D
Dmitry Torokhov 已提交
726 727
	if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
		result = acpi_ec_query(ec, &value);
L
Linus Torvalds 已提交
728

D
Dmitry Torokhov 已提交
729
	if (result)
L
Linus Torvalds 已提交
730 731 732 733 734 735 736
		goto end;

	object_name[2] = hex[((value >> 4) & 0x0F)];
	object_name[3] = hex[(value & 0x0F)];

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));

L
Luming Yu 已提交
737
	acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
L
Len Brown 已提交
738
      end:
L
Luming Yu 已提交
739
	atomic_dec(&ec->burst.pending_gpe);
D
Dmitry Torokhov 已提交
740
	return;
L
Linus Torvalds 已提交
741 742
}

L
Len Brown 已提交
743
static u32 acpi_ec_gpe_handler(void *data)
L
Luming Yu 已提交
744
{
L
Len Brown 已提交
745
	if (acpi_ec_polling_mode)
L
Luming Yu 已提交
746 747
		return acpi_ec_gpe_polling_handler(data);
	else
L
Len Brown 已提交
748
		return acpi_ec_gpe_burst_handler(data);
L
Luming Yu 已提交
749
}
L
Len Brown 已提交
750
static u32 acpi_ec_gpe_polling_handler(void *data)
L
Luming Yu 已提交
751
{
L
Len Brown 已提交
752 753
	acpi_status status = AE_OK;
	union acpi_ec *ec = (union acpi_ec *)data;
L
Luming Yu 已提交
754 755 756 757 758 759 760

	if (!ec)
		return ACPI_INTERRUPT_NOT_HANDLED;

	acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);

	status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
L
Len Brown 已提交
761
					     acpi_ec_gpe_query, ec);
L
Luming Yu 已提交
762 763 764 765 766 767

	if (status == AE_OK)
		return ACPI_INTERRUPT_HANDLED;
	else
		return ACPI_INTERRUPT_NOT_HANDLED;
}
L
Len Brown 已提交
768
static u32 acpi_ec_gpe_burst_handler(void *data)
L
Linus Torvalds 已提交
769
{
L
Len Brown 已提交
770 771 772
	acpi_status status = AE_OK;
	u32 value;
	union acpi_ec *ec = (union acpi_ec *)data;
L
Linus Torvalds 已提交
773 774 775 776

	if (!ec)
		return ACPI_INTERRUPT_NOT_HANDLED;

777
	acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
778
	value = acpi_ec_read_status(ec);
L
Linus Torvalds 已提交
779

L
Len Brown 已提交
780
	switch (ec->burst.expect_event) {
781 782 783 784 785 786 787
	case ACPI_EC_EVENT_OBF:
		if (!(value & ACPI_EC_FLAG_OBF))
			break;
	case ACPI_EC_EVENT_IBE:
		if ((value & ACPI_EC_FLAG_IBF))
			break;
		ec->burst.expect_event = 0;
L
Luming Yu 已提交
788
		wake_up(&ec->burst.wait);
789 790 791
		return ACPI_INTERRUPT_HANDLED;
	default:
		break;
D
Dmitry Torokhov 已提交
792 793
	}

L
Len Brown 已提交
794 795
	if (value & ACPI_EC_FLAG_SCI) {
		atomic_add(1, &ec->burst.pending_gpe);
D
Dmitry Torokhov 已提交
796
		status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
L
Len Brown 已提交
797
						     acpi_ec_gpe_query, ec);
L
Luming Yu 已提交
798
		return status == AE_OK ?
L
Len Brown 已提交
799 800
		    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
	}
L
Luming Yu 已提交
801
	acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
802
	return status == AE_OK ?
L
Len Brown 已提交
803
	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
L
Linus Torvalds 已提交
804 805 806 807 808 809 810
}

/* --------------------------------------------------------------------------
                             Address Space Management
   -------------------------------------------------------------------------- */

static acpi_status
L
Len Brown 已提交
811 812
acpi_ec_space_setup(acpi_handle region_handle,
		    u32 function, void *handler_context, void **return_context)
L
Linus Torvalds 已提交
813 814 815 816 817
{
	/*
	 * The EC object is in the handler context and is needed
	 * when calling the acpi_ec_space_handler.
	 */
L
Len Brown 已提交
818 819
	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
	    handler_context : NULL;
L
Linus Torvalds 已提交
820 821 822 823 824

	return AE_OK;
}

static acpi_status
L
Len Brown 已提交
825 826 827 828 829
acpi_ec_space_handler(u32 function,
		      acpi_physical_address address,
		      u32 bit_width,
		      acpi_integer * value,
		      void *handler_context, void *region_context)
L
Linus Torvalds 已提交
830
{
L
Len Brown 已提交
831 832 833 834 835
	int result = 0;
	union acpi_ec *ec = NULL;
	u64 temp = *value;
	acpi_integer f_v = 0;
	int i = 0;
L
Linus Torvalds 已提交
836 837 838 839 840 841

	ACPI_FUNCTION_TRACE("acpi_ec_space_handler");

	if ((address > 0xFF) || !value || !handler_context)
		return_VALUE(AE_BAD_PARAMETER);

L
Luming Yu 已提交
842
	if (bit_width != 8 && acpi_strict) {
L
Len Brown 已提交
843 844
		printk(KERN_WARNING PREFIX
		       "acpi_ec_space_handler: bit_width should be 8\n");
L
Luming Yu 已提交
845
		return_VALUE(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
846 847
	}

L
Len Brown 已提交
848
	ec = (union acpi_ec *)handler_context;
L
Linus Torvalds 已提交
849

L
Len Brown 已提交
850
      next_byte:
L
Linus Torvalds 已提交
851 852
	switch (function) {
	case ACPI_READ:
L
Luming Yu 已提交
853
		temp = 0;
L
Len Brown 已提交
854
		result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
L
Linus Torvalds 已提交
855 856
		break;
	case ACPI_WRITE:
L
Luming Yu 已提交
857
		result = acpi_ec_write(ec, (u8) address, (u8) temp);
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865
		break;
	default:
		result = -EINVAL;
		goto out;
		break;
	}

	bit_width -= 8;
L
Luming Yu 已提交
866 867 868 869 870
	if (bit_width) {
		if (function == ACPI_READ)
			f_v |= temp << 8 * i;
		if (function == ACPI_WRITE)
			temp >>= 8;
L
Linus Torvalds 已提交
871
		i++;
A
Andrew Morton 已提交
872
		address++;
L
Linus Torvalds 已提交
873 874 875
		goto next_byte;
	}

L
Luming Yu 已提交
876 877
	if (function == ACPI_READ) {
		f_v |= temp << 8 * i;
L
Linus Torvalds 已提交
878 879 880
		*value = f_v;
	}

L
Len Brown 已提交
881
      out:
L
Linus Torvalds 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	switch (result) {
	case -EINVAL:
		return_VALUE(AE_BAD_PARAMETER);
		break;
	case -ENODEV:
		return_VALUE(AE_NOT_FOUND);
		break;
	case -ETIME:
		return_VALUE(AE_TIME);
		break;
	default:
		return_VALUE(AE_OK);
	}
}

/* --------------------------------------------------------------------------
                              FS Interface (/proc)
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
901
static struct proc_dir_entry *acpi_ec_dir;
L
Linus Torvalds 已提交
902

L
Len Brown 已提交
903
static int acpi_ec_read_info(struct seq_file *seq, void *offset)
L
Linus Torvalds 已提交
904
{
L
Len Brown 已提交
905
	union acpi_ec *ec = (union acpi_ec *)seq->private;
L
Linus Torvalds 已提交
906 907 908 909 910 911 912

	ACPI_FUNCTION_TRACE("acpi_ec_read_info");

	if (!ec)
		goto end;

	seq_printf(seq, "gpe bit:                 0x%02x\n",
L
Len Brown 已提交
913
		   (u32) ec->common.gpe_bit);
L
Linus Torvalds 已提交
914
	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
L
Len Brown 已提交
915 916
		   (u32) ec->common.status_addr.address,
		   (u32) ec->common.data_addr.address);
L
Linus Torvalds 已提交
917
	seq_printf(seq, "use global lock:         %s\n",
L
Len Brown 已提交
918
		   ec->common.global_lock ? "yes" : "no");
L
Luming Yu 已提交
919
	acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
920

L
Len Brown 已提交
921
      end:
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930
	return_VALUE(0);
}

static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_ec_read_info, PDE(inode)->data);
}

static struct file_operations acpi_ec_info_ops = {
L
Len Brown 已提交
931 932 933 934
	.open = acpi_ec_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
935 936 937
	.owner = THIS_MODULE,
};

L
Len Brown 已提交
938
static int acpi_ec_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
939
{
L
Len Brown 已提交
940
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
941 942 943 944 945

	ACPI_FUNCTION_TRACE("acpi_ec_add_fs");

	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
946
						     acpi_ec_dir);
L
Linus Torvalds 已提交
947 948 949 950 951
		if (!acpi_device_dir(device))
			return_VALUE(-ENODEV);
	}

	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
L
Len Brown 已提交
952
				  acpi_device_dir(device));
L
Linus Torvalds 已提交
953 954
	if (!entry)
		ACPI_DEBUG_PRINT((ACPI_DB_WARN,
L
Len Brown 已提交
955 956
				  "Unable to create '%s' fs entry\n",
				  ACPI_EC_FILE_INFO));
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965
	else {
		entry->proc_fops = &acpi_ec_info_ops;
		entry->data = acpi_driver_data(device);
		entry->owner = THIS_MODULE;
	}

	return_VALUE(0);
}

L
Len Brown 已提交
966
static int acpi_ec_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
{
	ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");

	if (acpi_device_dir(device)) {
		remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
		remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
		acpi_device_dir(device) = NULL;
	}

	return_VALUE(0);
}

/* --------------------------------------------------------------------------
                               Driver Interface
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
983
static int acpi_ec_polling_add(struct acpi_device *device)
L
Linus Torvalds 已提交
984
{
L
Len Brown 已提交
985 986 987 988
	int result = 0;
	acpi_status status = AE_OK;
	union acpi_ec *ec = NULL;
	unsigned long uid;
L
Luming Yu 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

	ACPI_FUNCTION_TRACE("acpi_ec_add");

	if (!device)
		return_VALUE(-EINVAL);

	ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
	if (!ec)
		return_VALUE(-ENOMEM);
	memset(ec, 0, sizeof(union acpi_ec));

	ec->common.handle = device->handle;
	ec->common.uid = -1;
	spin_lock_init(&ec->polling.lock);
	strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_EC_CLASS);
	acpi_driver_data(device) = ec;

	/* Use the global lock for all EC transactions? */
L
Len Brown 已提交
1008 1009
	acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
			      &ec->common.global_lock);
L
Luming Yu 已提交
1010 1011

	/* If our UID matches the UID for the ECDT-enumerated EC,
L
Len Brown 已提交
1012
	   we now have the *real* EC info, so kill the makeshift one. */
L
Luming Yu 已提交
1013 1014 1015
	acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
	if (ec_ecdt && ec_ecdt->common.uid == uid) {
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
1016 1017 1018 1019 1020
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);

		acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
					&acpi_ec_gpe_handler);
L
Luming Yu 已提交
1021 1022 1023 1024 1025 1026

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
L
Len Brown 已提交
1027 1028 1029
	status =
	    acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
				  &ec->common.gpe_bit);
L
Luming Yu 已提交
1030 1031
	if (ACPI_FAILURE(status)) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
L
Len Brown 已提交
1032
				  "Error obtaining GPE bit assignment\n"));
L
Luming Yu 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041
		result = -ENODEV;
		goto end;
	}

	result = acpi_ec_add_fs(device);
	if (result)
		goto end;

	printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
L
Len Brown 已提交
1042 1043
	       acpi_device_name(device), acpi_device_bid(device),
	       (u32) ec->common.gpe_bit);
L
Luming Yu 已提交
1044 1045 1046 1047

	if (!first_ec)
		first_ec = device;

L
Len Brown 已提交
1048
      end:
L
Luming Yu 已提交
1049 1050 1051 1052 1053
	if (result)
		kfree(ec);

	return_VALUE(result);
}
L
Len Brown 已提交
1054
static int acpi_ec_burst_add(struct acpi_device *device)
L
Luming Yu 已提交
1055
{
L
Len Brown 已提交
1056 1057 1058 1059
	int result = 0;
	acpi_status status = AE_OK;
	union acpi_ec *ec = NULL;
	unsigned long uid;
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065

	ACPI_FUNCTION_TRACE("acpi_ec_add");

	if (!device)
		return_VALUE(-EINVAL);

L
Luming Yu 已提交
1066
	ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
1067 1068
	if (!ec)
		return_VALUE(-ENOMEM);
L
Luming Yu 已提交
1069 1070 1071 1072
	memset(ec, 0, sizeof(union acpi_ec));

	ec->common.handle = device->handle;
	ec->common.uid = -1;
L
Len Brown 已提交
1073 1074 1075 1076
	atomic_set(&ec->burst.pending_gpe, 0);
	atomic_set(&ec->burst.leaving_burst, 1);
	init_MUTEX(&ec->burst.sem);
	init_waitqueue_head(&ec->burst.wait);
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081
	strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_EC_CLASS);
	acpi_driver_data(device) = ec;

	/* Use the global lock for all EC transactions? */
L
Len Brown 已提交
1082 1083
	acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
			      &ec->common.global_lock);
L
Linus Torvalds 已提交
1084 1085

	/* If our UID matches the UID for the ECDT-enumerated EC,
L
Len Brown 已提交
1086
	   we now have the *real* EC info, so kill the makeshift one. */
L
Luming Yu 已提交
1087 1088
	acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
	if (ec_ecdt && ec_ecdt->common.uid == uid) {
L
Linus Torvalds 已提交
1089
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
1090 1091
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
1092

L
Len Brown 已提交
1093 1094
		acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099 1100

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
L
Len Brown 已提交
1101 1102 1103
	status =
	    acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
				  &ec->common.gpe_bit);
L
Linus Torvalds 已提交
1104 1105
	if (ACPI_FAILURE(status)) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
L
Len Brown 已提交
1106
				  "Error obtaining GPE bit assignment\n"));
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113 1114
		result = -ENODEV;
		goto end;
	}

	result = acpi_ec_add_fs(device);
	if (result)
		goto end;

1115
	printk("burst-mode-ec-10-Aug\n");
L
Linus Torvalds 已提交
1116
	printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
L
Len Brown 已提交
1117 1118
	       acpi_device_name(device), acpi_device_bid(device),
	       (u32) ec->common.gpe_bit);
L
Linus Torvalds 已提交
1119 1120 1121 1122

	if (!first_ec)
		first_ec = device;

L
Len Brown 已提交
1123
      end:
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129
	if (result)
		kfree(ec);

	return_VALUE(result);
}

L
Len Brown 已提交
1130
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
1131
{
L
Len Brown 已提交
1132
	union acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

	ACPI_FUNCTION_TRACE("acpi_ec_remove");

	if (!device)
		return_VALUE(-EINVAL);

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

	return_VALUE(0);
}

static acpi_status
L
Len Brown 已提交
1149
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
1150
{
L
Len Brown 已提交
1151
	union acpi_ec *ec = (union acpi_ec *)context;
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	struct acpi_generic_address *addr;

	if (resource->id != ACPI_RSTYPE_IO) {
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
L
Luming Yu 已提交
1163 1164 1165 1166
	if (ec->common.data_addr.register_bit_width == 0) {
		addr = &ec->common.data_addr;
	} else if (ec->common.command_addr.register_bit_width == 0) {
		addr = &ec->common.command_addr;
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	} else {
		return AE_CTRL_TERMINATE;
	}

	addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
	addr->register_bit_width = 8;
	addr->register_bit_offset = 0;
	addr->address = resource->data.io.min_base_address;

	return AE_OK;
}

L
Len Brown 已提交
1179
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
1180
{
L
Len Brown 已提交
1181 1182
	acpi_status status = AE_OK;
	union acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

	ACPI_FUNCTION_TRACE("acpi_ec_start");

	if (!device)
		return_VALUE(-EINVAL);

	ec = acpi_driver_data(device);

	if (!ec)
		return_VALUE(-EINVAL);

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
L
Luming Yu 已提交
1197
	status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
L
Len Brown 已提交
1198 1199 1200 1201 1202
				     acpi_ec_io_ports, ec);
	if (ACPI_FAILURE(status)
	    || ec->common.command_addr.register_bit_width == 0) {
		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
				  "Error getting I/O port addresses"));
L
Linus Torvalds 已提交
1203 1204 1205
		return_VALUE(-ENODEV);
	}

L
Luming Yu 已提交
1206
	ec->common.status_addr = ec->common.command_addr;
L
Linus Torvalds 已提交
1207 1208

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
L
Len Brown 已提交
1209 1210 1211
			  (u32) ec->common.gpe_bit,
			  (u32) ec->common.command_addr.address,
			  (u32) ec->common.data_addr.address));
L
Linus Torvalds 已提交
1212 1213 1214 1215

	/*
	 * Install GPE handler
	 */
L
Luming Yu 已提交
1216
	status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
L
Len Brown 已提交
1217 1218
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
1219 1220 1221
	if (ACPI_FAILURE(status)) {
		return_VALUE(-ENODEV);
	}
L
Len Brown 已提交
1222 1223
	acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
1224

L
Len Brown 已提交
1225 1226 1227 1228
	status = acpi_install_address_space_handler(ec->common.handle,
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
1229
	if (ACPI_FAILURE(status)) {
L
Len Brown 已提交
1230 1231
		acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
1232 1233 1234 1235 1236 1237
		return_VALUE(-ENODEV);
	}

	return_VALUE(AE_OK);
}

L
Len Brown 已提交
1238
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
1239
{
L
Len Brown 已提交
1240 1241
	acpi_status status = AE_OK;
	union acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247 1248 1249

	ACPI_FUNCTION_TRACE("acpi_ec_stop");

	if (!device)
		return_VALUE(-EINVAL);

	ec = acpi_driver_data(device);

L
Luming Yu 已提交
1250
	status = acpi_remove_address_space_handler(ec->common.handle,
L
Len Brown 已提交
1251 1252
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
1253 1254 1255
	if (ACPI_FAILURE(status))
		return_VALUE(-ENODEV);

L
Len Brown 已提交
1256 1257 1258
	status =
	    acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
				    &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
1259 1260 1261 1262 1263 1264 1265
	if (ACPI_FAILURE(status))
		return_VALUE(-ENODEV);

	return_VALUE(0);
}

static acpi_status __init
L
Len Brown 已提交
1266 1267
acpi_fake_ecdt_callback(acpi_handle handle,
			u32 Level, void *context, void **retval)
L
Linus Torvalds 已提交
1268
{
L
Luming Yu 已提交
1269 1270 1271

	if (acpi_ec_polling_mode)
		return acpi_fake_ecdt_polling_callback(handle,
L
Len Brown 已提交
1272
						       Level, context, retval);
L
Luming Yu 已提交
1273 1274
	else
		return acpi_fake_ecdt_burst_callback(handle,
L
Len Brown 已提交
1275
						     Level, context, retval);
L
Luming Yu 已提交
1276 1277 1278
}

static acpi_status __init
L
Len Brown 已提交
1279 1280
acpi_fake_ecdt_polling_callback(acpi_handle handle,
				u32 Level, void *context, void **retval)
L
Luming Yu 已提交
1281
{
L
Len Brown 已提交
1282
	acpi_status status;
L
Luming Yu 已提交
1283 1284

	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
1285
				     acpi_ec_io_ports, ec_ecdt);
L
Luming Yu 已提交
1286 1287 1288 1289 1290 1291 1292
	if (ACPI_FAILURE(status))
		return status;
	ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;

	ec_ecdt->common.uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);

L
Len Brown 已提交
1293 1294 1295
	status =
	    acpi_evaluate_integer(handle, "_GPE", NULL,
				  &ec_ecdt->common.gpe_bit);
L
Luming Yu 已提交
1296 1297 1298 1299 1300 1301
	if (ACPI_FAILURE(status))
		return status;
	spin_lock_init(&ec_ecdt->polling.lock);
	ec_ecdt->common.global_lock = TRUE;
	ec_ecdt->common.handle = handle;

L
Len Brown 已提交
1302 1303 1304 1305
	printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
	       (u32) ec_ecdt->common.gpe_bit,
	       (u32) ec_ecdt->common.command_addr.address,
	       (u32) ec_ecdt->common.data_addr.address);
L
Luming Yu 已提交
1306 1307 1308 1309 1310

	return AE_CTRL_TERMINATE;
}

static acpi_status __init
L
Len Brown 已提交
1311 1312
acpi_fake_ecdt_burst_callback(acpi_handle handle,
			      u32 Level, void *context, void **retval)
L
Luming Yu 已提交
1313
{
L
Len Brown 已提交
1314
	acpi_status status;
L
Linus Torvalds 已提交
1315

L
Luming Yu 已提交
1316 1317
	init_MUTEX(&ec_ecdt->burst.sem);
	init_waitqueue_head(&ec_ecdt->burst.wait);
L
Linus Torvalds 已提交
1318
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
1319
				     acpi_ec_io_ports, ec_ecdt);
L
Linus Torvalds 已提交
1320 1321
	if (ACPI_FAILURE(status))
		return status;
L
Luming Yu 已提交
1322
	ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
L
Linus Torvalds 已提交
1323

L
Luming Yu 已提交
1324 1325
	ec_ecdt->common.uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
L
Linus Torvalds 已提交
1326

L
Len Brown 已提交
1327 1328 1329
	status =
	    acpi_evaluate_integer(handle, "_GPE", NULL,
				  &ec_ecdt->common.gpe_bit);
L
Linus Torvalds 已提交
1330 1331
	if (ACPI_FAILURE(status))
		return status;
L
Luming Yu 已提交
1332 1333
	ec_ecdt->common.global_lock = TRUE;
	ec_ecdt->common.handle = handle;
L
Linus Torvalds 已提交
1334

L
Len Brown 已提交
1335 1336 1337 1338
	printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
	       (u32) ec_ecdt->common.gpe_bit,
	       (u32) ec_ecdt->common.command_addr.address,
	       (u32) ec_ecdt->common.data_addr.address);
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

	return AE_CTRL_TERMINATE;
}

/*
 * Some BIOS (such as some from Gateway laptops) access EC region very early
 * such as in BAT0._INI or EC._INI before an EC device is found and
 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
 * required, but if EC regison is accessed early, it is required.
 * The routine tries to workaround the BIOS bug by pre-scan EC device
 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
 * op region (since _REG isn't invoked yet). The assumption is true for
 * all systems found.
 */
L
Len Brown 已提交
1353
static int __init acpi_ec_fake_ecdt(void)
L
Linus Torvalds 已提交
1354
{
L
Len Brown 已提交
1355 1356
	acpi_status status;
	int ret = 0;
L
Linus Torvalds 已提交
1357 1358 1359

	printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");

L
Luming Yu 已提交
1360
	ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
1361 1362 1363 1364
	if (!ec_ecdt) {
		ret = -ENOMEM;
		goto error;
	}
L
Luming Yu 已提交
1365
	memset(ec_ecdt, 0, sizeof(union acpi_ec));
L
Linus Torvalds 已提交
1366

L
Len Brown 已提交
1367 1368
	status = acpi_get_devices(ACPI_EC_HID,
				  acpi_fake_ecdt_callback, NULL, NULL);
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374 1375
	if (ACPI_FAILURE(status)) {
		kfree(ec_ecdt);
		ec_ecdt = NULL;
		ret = -ENODEV;
		goto error;
	}
	return 0;
L
Len Brown 已提交
1376
      error:
L
Linus Torvalds 已提交
1377 1378 1379 1380
	printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
	return ret;
}

L
Len Brown 已提交
1381
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
1382 1383 1384 1385 1386 1387 1388
{
	if (acpi_ec_polling_mode)
		return acpi_ec_polling_get_real_ecdt();
	else
		return acpi_ec_burst_get_real_ecdt();
}

L
Len Brown 已提交
1389
static int __init acpi_ec_polling_get_real_ecdt(void)
L
Luming Yu 已提交
1390
{
L
Len Brown 已提交
1391 1392
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
1393

L
Len Brown 已提交
1394 1395 1396
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Luming Yu 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	if (ACPI_FAILURE(status))
		return -ENODEV;

	printk(KERN_INFO PREFIX "Found ECDT\n");

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
	ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
	if (!ec_ecdt)
		return -ENOMEM;
	memset(ec_ecdt, 0, sizeof(union acpi_ec));

	ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
	ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
	ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
	ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
	spin_lock_init(&ec_ecdt->polling.lock);
	/* use the GL just to be safe */
	ec_ecdt->common.global_lock = TRUE;
	ec_ecdt->common.uid = ecdt_ptr->uid;

L
Len Brown 已提交
1419 1420
	status =
	    acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
L
Luming Yu 已提交
1421 1422 1423 1424 1425
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
L
Len Brown 已提交
1426
      error:
L
Luming Yu 已提交
1427 1428 1429 1430 1431 1432 1433
	printk(KERN_ERR PREFIX "Could not use ECDT\n");
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
1434
static int __init acpi_ec_burst_get_real_ecdt(void)
L
Linus Torvalds 已提交
1435
{
L
Len Brown 已提交
1436 1437
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Linus Torvalds 已提交
1438

D
Dmitry Torokhov 已提交
1439
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
L
Len Brown 已提交
1440 1441
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Linus Torvalds 已提交
1442 1443 1444 1445 1446 1447 1448 1449
	if (ACPI_FAILURE(status))
		return -ENODEV;

	printk(KERN_INFO PREFIX "Found ECDT\n");

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
L
Luming Yu 已提交
1450
	ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
1451 1452
	if (!ec_ecdt)
		return -ENOMEM;
L
Luming Yu 已提交
1453 1454
	memset(ec_ecdt, 0, sizeof(union acpi_ec));

L
Len Brown 已提交
1455 1456
	init_MUTEX(&ec_ecdt->burst.sem);
	init_waitqueue_head(&ec_ecdt->burst.wait);
L
Luming Yu 已提交
1457 1458 1459 1460
	ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
	ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
	ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
	ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
L
Linus Torvalds 已提交
1461
	/* use the GL just to be safe */
L
Luming Yu 已提交
1462 1463
	ec_ecdt->common.global_lock = TRUE;
	ec_ecdt->common.uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
1464

L
Len Brown 已提交
1465 1466
	status =
	    acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
L
Linus Torvalds 已提交
1467 1468 1469 1470 1471
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
L
Len Brown 已提交
1472
      error:
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480
	printk(KERN_ERR PREFIX "Could not use ECDT\n");
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

static int __initdata acpi_fake_ecdt_enabled;
L
Len Brown 已提交
1481
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
1482
{
L
Len Brown 已提交
1483 1484
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

	ret = acpi_ec_get_real_ecdt();
	/* Try to make a fake ECDT */
	if (ret && acpi_fake_ecdt_enabled) {
		ret = acpi_ec_fake_ecdt();
	}

	if (ret)
		return 0;

	/*
	 * Install GPE handler
	 */
L
Luming Yu 已提交
1498
	status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
L
Len Brown 已提交
1499 1500
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
1501 1502 1503
	if (ACPI_FAILURE(status)) {
		goto error;
	}
L
Len Brown 已提交
1504 1505 1506 1507 1508 1509 1510 1511
	acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);

	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup,
						    ec_ecdt);
L
Linus Torvalds 已提交
1512
	if (ACPI_FAILURE(status)) {
L
Luming Yu 已提交
1513
		acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
L
Len Brown 已提交
1514
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519
		goto error;
	}

	return 0;

L
Len Brown 已提交
1520
      error:
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526 1527
	printk(KERN_ERR PREFIX "Could not use ECDT\n");
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
1528
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
1529
{
L
Len Brown 已提交
1530
	int result = 0;
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554

	ACPI_FUNCTION_TRACE("acpi_ec_init");

	if (acpi_disabled)
		return_VALUE(0);

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
		return_VALUE(-ENODEV);

	/* Now register the driver for the EC */
	result = acpi_bus_register_driver(&acpi_ec_driver);
	if (result < 0) {
		remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
		return_VALUE(-ENODEV);
	}

	return_VALUE(result);
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
1555
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564
{
	ACPI_FUNCTION_TRACE("acpi_ec_exit");

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

	return_VOID;
}
L
Len Brown 已提交
1565
#endif				/* 0 */
L
Linus Torvalds 已提交
1566 1567 1568 1569 1570 1571

static int __init acpi_fake_ecdt_setup(char *str)
{
	acpi_fake_ecdt_enabled = 1;
	return 0;
}
1572

L
Linus Torvalds 已提交
1573
__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
L
Luming Yu 已提交
1574 1575
static int __init acpi_ec_set_polling_mode(char *str)
{
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	int burst;

	if (!get_option(&str, &burst))
		return 0;

	if (burst) {
		acpi_ec_polling_mode = EC_BURST;
		acpi_ec_driver.ops.add = acpi_ec_burst_add;
	} else {
		acpi_ec_polling_mode = EC_POLLING;
		acpi_ec_driver.ops.add = acpi_ec_polling_add;
	}
L
Len Brown 已提交
1588
	printk(KERN_INFO PREFIX "EC %s mode.\n", burst ? "burst" : "polling");
L
Luming Yu 已提交
1589 1590
	return 0;
}
L
Len Brown 已提交
1591

1592
__setup("ec_burst=", acpi_ec_set_polling_mode);