ec.c 23.8 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
#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"
48 49
#undef PREFIX
#define PREFIX				"ACPI: EC: "
50
/* EC status register */
L
Linus Torvalds 已提交
51 52
#define ACPI_EC_FLAG_OBF	0x01	/* Output buffer full */
#define ACPI_EC_FLAG_IBF	0x02	/* Input buffer full */
D
Dmitry Torokhov 已提交
53
#define ACPI_EC_FLAG_BURST	0x10	/* burst mode */
L
Linus Torvalds 已提交
54
#define ACPI_EC_FLAG_SCI	0x20	/* EC-SCI occurred */
55
/* EC commands */
56
enum ec_command {
A
Alexey Starikovskiy 已提交
57 58 59 60 61
	ACPI_EC_COMMAND_READ = 0x80,
	ACPI_EC_COMMAND_WRITE = 0x81,
	ACPI_EC_BURST_ENABLE = 0x82,
	ACPI_EC_BURST_DISABLE = 0x83,
	ACPI_EC_COMMAND_QUERY = 0x84,
62
};
63
/* EC events */
64
enum ec_event {
65
	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
A
Alexey Starikovskiy 已提交
66
	ACPI_EC_EVENT_IBF_0,	/* Input buffer empty */
67 68
};

69
#define ACPI_EC_DELAY		500	/* Wait 500ms max. during EC ops */
70 71
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */

72
static enum ec_mode {
A
Alexey Starikovskiy 已提交
73 74
	EC_INTR = 1,		/* Output buffer full */
	EC_POLL,		/* Input buffer empty */
75
} acpi_ec_mode = EC_INTR;
76

L
Len Brown 已提交
77 78 79
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);
80
static int acpi_ec_add(struct acpi_device *device);
L
Linus Torvalds 已提交
81 82

static struct acpi_driver acpi_ec_driver = {
L
Len Brown 已提交
83 84 85 86
	.name = ACPI_EC_DRIVER_NAME,
	.class = ACPI_EC_CLASS,
	.ids = ACPI_EC_HID,
	.ops = {
87
		.add = acpi_ec_add,
L
Len Brown 已提交
88 89 90 91
		.remove = acpi_ec_remove,
		.start = acpi_ec_start,
		.stop = acpi_ec_stop,
		},
L
Linus Torvalds 已提交
92
};
93 94

/* If we find an EC via the ECDT, we need to keep a ptr to its context */
95
static struct acpi_ec {
96 97
	acpi_handle handle;
	unsigned long uid;
98
	unsigned long gpe;
99 100
	unsigned long command_addr;
	unsigned long data_addr;
101
	unsigned long global_lock;
102
	struct mutex lock;
103
	atomic_t query_pending;
104 105
	atomic_t leaving_burst;	/* 0 : No, 1 : Yes, 2: abort */
	wait_queue_head_t wait;
106
} *ec_ecdt;
107 108 109 110

/* External interfaces use first EC only, so remember */
static struct acpi_device *first_ec;

L
Linus Torvalds 已提交
111 112 113 114
/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

115
static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
L
Linus Torvalds 已提交
116
{
117
	return inb(ec->command_addr);
D
Dmitry Torokhov 已提交
118 119
}

120
static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
121
{
122
	return inb(ec->data_addr);
123 124
}

125
static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
L
Luming Yu 已提交
126
{
127
	outb(command, ec->command_addr);
L
Luming Yu 已提交
128 129
}

130
static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
L
Luming Yu 已提交
131
{
132
	outb(data, ec->data_addr);
133
}
L
Luming Yu 已提交
134

135
static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
136
{
137
	u8 status = acpi_ec_read_status(ec);
A
Alexey Starikovskiy 已提交
138 139

	if (event == ACPI_EC_EVENT_OBF_1) {
140 141
		if (status & ACPI_EC_FLAG_OBF)
			return 1;
A
Alexey Starikovskiy 已提交
142
	} else if (event == ACPI_EC_EVENT_IBF_0) {
143 144
		if (!(status & ACPI_EC_FLAG_IBF))
			return 1;
L
Luming Yu 已提交
145 146
	}

147
	return 0;
L
Luming Yu 已提交
148
}
D
Dmitry Torokhov 已提交
149

150
static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event)
151
{
152
	if (acpi_ec_mode == EC_POLL) {
153 154
		unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
		while (time_before(jiffies, delay)) {
155
			if (acpi_ec_check_status(ec, event))
156 157
				return 0;
		}
158 159 160 161 162
	} else {
		if (wait_event_timeout(ec->wait,
				       acpi_ec_check_status(ec, event),
				       msecs_to_jiffies(ACPI_EC_DELAY)) ||
		    acpi_ec_check_status(ec, event)) {
163
			return 0;
164 165 166
		} else {
			printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
			       " status = %d, expect_event = %d\n",
A
Alexey Starikovskiy 已提交
167
			       acpi_ec_read_status(ec), event);
168
		}
169
	}
L
Linus Torvalds 已提交
170

171
	return -ETIME;
L
Linus Torvalds 已提交
172 173
}

174
#ifdef ACPI_FUTURE_USAGE
175 176 177 178
/*
 * Note: samsung nv5000 doesn't work with ec burst mode.
 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
 */
179
int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
180
{
181 182
	u8 tmp = 0;
	u8 status = 0;
D
Dmitry Torokhov 已提交
183 184

	status = acpi_ec_read_status(ec);
L
Len Brown 已提交
185
	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
186
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
L
Len Brown 已提交
187
		if (status)
188
			goto end;
189 190 191
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
		tmp = acpi_ec_read_data(ec);
L
Len Brown 已提交
192
		if (tmp != 0x90) {	/* Burst ACK byte */
193
			return -EINVAL;
D
Dmitry Torokhov 已提交
194
		}
195 196
	}

197
	atomic_set(&ec->leaving_burst, 0);
198
	return 0;
A
Alexey Starikovskiy 已提交
199
      end:
200
	ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
201
	return -1;
D
Dmitry Torokhov 已提交
202 203
}

204
int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
205
{
206
	u8 status = 0;
D
Dmitry Torokhov 已提交
207

208
	status = acpi_ec_read_status(ec);
A
Alexey Starikovskiy 已提交
209
	if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)) {
210
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
A
Alexey Starikovskiy 已提交
211
		if (status)
212
			goto end;
213 214
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
		acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
215
	}
216
	atomic_set(&ec->leaving_burst, 1);
217
	return 0;
A
Alexey Starikovskiy 已提交
218
      end:
219
	ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
220
	return -1;
D
Dmitry Torokhov 已提交
221
}
A
Alexey Starikovskiy 已提交
222
#endif				/* ACPI_FUTURE_USAGE */
D
Dmitry Torokhov 已提交
223

224
static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
A
Alexey Starikovskiy 已提交
225 226
					const u8 * wdata, unsigned wdata_len,
					u8 * rdata, unsigned rdata_len)
L
Luming Yu 已提交
227
{
228
	int result = 0;
L
Luming Yu 已提交
229

230
	acpi_ec_write_cmd(ec, command);
L
Luming Yu 已提交
231

A
Alexey Starikovskiy 已提交
232
	for (; wdata_len > 0; --wdata_len) {
233
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
234
		if (result) {
A
Alexey Starikovskiy 已提交
235 236
			printk(KERN_ERR PREFIX
			       "write_cmd timeout, command = %d\n", command);
237 238
			goto end;
		}
239
		acpi_ec_write_data(ec, *(wdata++));
240
	}
L
Luming Yu 已提交
241

242
	if (!rdata_len) {
243
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
244
		if (result) {
A
Alexey Starikovskiy 已提交
245 246
			printk(KERN_ERR PREFIX
			       "finish-write timeout, command = %d\n", command);
247 248
			goto end;
		}
249 250
	} else if (command == ACPI_EC_COMMAND_QUERY) {
		atomic_set(&ec->query_pending, 0);
251
	}
L
Luming Yu 已提交
252

A
Alexey Starikovskiy 已提交
253
	for (; rdata_len > 0; --rdata_len) {
254
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
255 256
		if (result) {
			printk(KERN_ERR PREFIX "read timeout, command = %d\n",
A
Alexey Starikovskiy 已提交
257
			       command);
258 259
			goto end;
		}
L
Len Brown 已提交
260

261
		*(rdata++) = acpi_ec_read_data(ec);
262
	}
263 264
      end:
	return result;
L
Luming Yu 已提交
265 266
}

267
static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
A
Alexey Starikovskiy 已提交
268 269
			       const u8 * wdata, unsigned wdata_len,
			       u8 * rdata, unsigned rdata_len)
L
Linus Torvalds 已提交
270
{
271
	int status;
L
Len Brown 已提交
272
	u32 glk;
L
Linus Torvalds 已提交
273

274
	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
275
		return -EINVAL;
L
Linus Torvalds 已提交
276

A
Alexey Starikovskiy 已提交
277 278
	if (rdata)
		memset(rdata, 0, rdata_len);
L
Linus Torvalds 已提交
279

280
	mutex_lock(&ec->lock);
281
	if (ec->global_lock) {
L
Linus Torvalds 已提交
282 283
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
284
			return -ENODEV;
L
Linus Torvalds 已提交
285
	}
D
Dmitry Torokhov 已提交
286

287
	/* Make sure GPE is enabled before doing transaction */
288
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
289

290
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
291
	if (status) {
A
Alexey Starikovskiy 已提交
292 293
		printk(KERN_DEBUG PREFIX
		       "input buffer is not empty, aborting transaction\n");
D
Dmitry Torokhov 已提交
294
		goto end;
295
	}
L
Linus Torvalds 已提交
296

A
Alexey Starikovskiy 已提交
297 298 299
	status = acpi_ec_transaction_unlocked(ec, command,
					      wdata, wdata_len,
					      rdata, rdata_len);
L
Linus Torvalds 已提交
300

A
Alexey Starikovskiy 已提交
301
      end:
L
Linus Torvalds 已提交
302

303
	if (ec->global_lock)
L
Linus Torvalds 已提交
304
		acpi_release_global_lock(glk);
305
	mutex_unlock(&ec->lock);
L
Linus Torvalds 已提交
306

307
	return status;
L
Linus Torvalds 已提交
308 309
}

A
Alexey Starikovskiy 已提交
310
static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
311 312 313 314 315 316 317 318 319
{
	int result;
	u8 d;

	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
				     &address, 1, &d, 1);
	*data = d;
	return result;
}
320

321 322
static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
{
A
Alexey Starikovskiy 已提交
323 324
	u8 wdata[2] = { address, data };
	return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
325 326 327
				   wdata, 2, NULL, 0);
}

L
Linus Torvalds 已提交
328 329 330
/*
 * Externally callable EC access functions. For now, assume 1 EC only
 */
A
Alexey Starikovskiy 已提交
331
int ec_read(u8 addr, u8 * val)
L
Linus Torvalds 已提交
332
{
333
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
334
	int err;
335
	u8 temp_data;
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346

	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 已提交
347
	} else
L
Linus Torvalds 已提交
348 349
		return err;
}
L
Len Brown 已提交
350

L
Linus Torvalds 已提交
351 352
EXPORT_SYMBOL(ec_read);

L
Len Brown 已提交
353
int ec_write(u8 addr, u8 val)
L
Linus Torvalds 已提交
354
{
355
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366
	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 已提交
367

L
Linus Torvalds 已提交
368 369
EXPORT_SYMBOL(ec_write);

370
int ec_transaction(u8 command,
A
Alexey Starikovskiy 已提交
371 372
			  const u8 * wdata, unsigned wdata_len,
			  u8 * rdata, unsigned rdata_len)
L
Luming Yu 已提交
373
{
374
	struct acpi_ec *ec;
L
Luming Yu 已提交
375

376 377
	if (!first_ec)
		return -ENODEV;
L
Luming Yu 已提交
378

379
	ec = acpi_driver_data(first_ec);
L
Luming Yu 已提交
380

381 382
	return acpi_ec_transaction(ec, command, wdata,
				   wdata_len, rdata, rdata_len);
L
Luming Yu 已提交
383
}
L
Linus Torvalds 已提交
384

385 386
EXPORT_SYMBOL(ec_transaction);

A
Alexey Starikovskiy 已提交
387
static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
388 389
{
	int result;
A
Alexey Starikovskiy 已提交
390
	u8 d;
L
Linus Torvalds 已提交
391

A
Alexey Starikovskiy 已提交
392 393
	if (!ec || !data)
		return -EINVAL;
L
Linus Torvalds 已提交
394

A
Alexey Starikovskiy 已提交
395 396 397 398 399
	/*
	 * 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).
	 */
400

A
Alexey Starikovskiy 已提交
401 402 403
	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
	if (result)
		return result;
L
Linus Torvalds 已提交
404

A
Alexey Starikovskiy 已提交
405 406
	if (!d)
		return -ENODATA;
L
Linus Torvalds 已提交
407

A
Alexey Starikovskiy 已提交
408 409
	*data = d;
	return 0;
L
Linus Torvalds 已提交
410 411 412 413 414 415
}

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

L
Len Brown 已提交
416
static void acpi_ec_gpe_query(void *ec_cxt)
L
Luming Yu 已提交
417
{
418
	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
419
	u8 value = 0;
420
	char object_name[8];
L
Luming Yu 已提交
421

422
	if (!ec || acpi_ec_query(ec, &value))
423
		return;
L
Luming Yu 已提交
424

425
	snprintf(object_name, 8, "_Q%2.2X", value);
L
Luming Yu 已提交
426

427
	printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
L
Luming Yu 已提交
428

429
	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
L
Luming Yu 已提交
430
}
L
Linus Torvalds 已提交
431

L
Len Brown 已提交
432
static u32 acpi_ec_gpe_handler(void *data)
L
Linus Torvalds 已提交
433
{
L
Len Brown 已提交
434
	acpi_status status = AE_OK;
435
	u8 value;
436
	struct acpi_ec *ec = (struct acpi_ec *)data;
L
Linus Torvalds 已提交
437

438
	if (acpi_ec_mode == EC_INTR) {
439
		wake_up(&ec->wait);
D
Dmitry Torokhov 已提交
440 441
	}

442
	value = acpi_ec_read_status(ec);
443 444
	if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
		atomic_set(&ec->query_pending, 1);
A
Alexey Starikovskiy 已提交
445 446 447
		status =
		    acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
				    ec);
L
Len Brown 已提交
448
	}
449

D
Dmitry Torokhov 已提交
450
	return status == AE_OK ?
L
Len Brown 已提交
451
	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
L
Linus Torvalds 已提交
452 453 454 455 456 457 458
}

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

static acpi_status
L
Len Brown 已提交
459 460
acpi_ec_space_setup(acpi_handle region_handle,
		    u32 function, void *handler_context, void **return_context)
L
Linus Torvalds 已提交
461 462 463 464 465
{
	/*
	 * The EC object is in the handler context and is needed
	 * when calling the acpi_ec_space_handler.
	 */
L
Len Brown 已提交
466 467
	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
	    handler_context : NULL;
L
Linus Torvalds 已提交
468 469 470 471 472

	return AE_OK;
}

static acpi_status
L
Len Brown 已提交
473 474 475 476 477
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 已提交
478
{
L
Len Brown 已提交
479
	int result = 0;
480
	struct acpi_ec *ec = NULL;
L
Len Brown 已提交
481 482 483
	u64 temp = *value;
	acpi_integer f_v = 0;
	int i = 0;
L
Linus Torvalds 已提交
484 485

	if ((address > 0xFF) || !value || !handler_context)
486
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
487

L
Luming Yu 已提交
488
	if (bit_width != 8 && acpi_strict) {
489
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
490 491
	}

492
	ec = (struct acpi_ec *)handler_context;
L
Linus Torvalds 已提交
493

L
Len Brown 已提交
494
      next_byte:
L
Linus Torvalds 已提交
495 496
	switch (function) {
	case ACPI_READ:
L
Luming Yu 已提交
497
		temp = 0;
A
Alexey Starikovskiy 已提交
498
		result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
L
Linus Torvalds 已提交
499 500
		break;
	case ACPI_WRITE:
L
Luming Yu 已提交
501
		result = acpi_ec_write(ec, (u8) address, (u8) temp);
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509
		break;
	default:
		result = -EINVAL;
		goto out;
		break;
	}

	bit_width -= 8;
L
Luming Yu 已提交
510 511 512 513 514
	if (bit_width) {
		if (function == ACPI_READ)
			f_v |= temp << 8 * i;
		if (function == ACPI_WRITE)
			temp >>= 8;
L
Linus Torvalds 已提交
515
		i++;
A
Andrew Morton 已提交
516
		address++;
L
Linus Torvalds 已提交
517 518 519
		goto next_byte;
	}

L
Luming Yu 已提交
520 521
	if (function == ACPI_READ) {
		f_v |= temp << 8 * i;
L
Linus Torvalds 已提交
522 523 524
		*value = f_v;
	}

L
Len Brown 已提交
525
      out:
L
Linus Torvalds 已提交
526 527
	switch (result) {
	case -EINVAL:
528
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
529 530
		break;
	case -ENODEV:
531
		return AE_NOT_FOUND;
L
Linus Torvalds 已提交
532 533
		break;
	case -ETIME:
534
		return AE_TIME;
L
Linus Torvalds 已提交
535 536
		break;
	default:
537
		return AE_OK;
L
Linus Torvalds 已提交
538 539 540 541 542 543 544
	}
}

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

L
Len Brown 已提交
545
static struct proc_dir_entry *acpi_ec_dir;
L
Linus Torvalds 已提交
546

L
Len Brown 已提交
547
static int acpi_ec_read_info(struct seq_file *seq, void *offset)
L
Linus Torvalds 已提交
548
{
549
	struct acpi_ec *ec = (struct acpi_ec *)seq->private;
L
Linus Torvalds 已提交
550 551 552 553

	if (!ec)
		goto end;

A
Alexey Starikovskiy 已提交
554
	seq_printf(seq, "gpe:                 0x%02x\n", (u32) ec->gpe);
L
Linus Torvalds 已提交
555
	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
A
Alexey Starikovskiy 已提交
556
		   (u32) ec->command_addr, (u32) ec->data_addr);
L
Linus Torvalds 已提交
557
	seq_printf(seq, "use global lock:         %s\n",
558
		   ec->global_lock ? "yes" : "no");
559
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
560

L
Len Brown 已提交
561
      end:
562
	return 0;
L
Linus Torvalds 已提交
563 564 565 566 567 568 569
}

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

570
static struct file_operations acpi_ec_info_ops = {
L
Len Brown 已提交
571 572 573 574
	.open = acpi_ec_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
575 576 577
	.owner = THIS_MODULE,
};

L
Len Brown 已提交
578
static int acpi_ec_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
579
{
L
Len Brown 已提交
580
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
581 582 583

	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
584
						     acpi_ec_dir);
L
Linus Torvalds 已提交
585
		if (!acpi_device_dir(device))
586
			return -ENODEV;
L
Linus Torvalds 已提交
587 588 589
	}

	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
L
Len Brown 已提交
590
				  acpi_device_dir(device));
L
Linus Torvalds 已提交
591
	if (!entry)
592
		return -ENODEV;
L
Linus Torvalds 已提交
593 594 595 596 597 598
	else {
		entry->proc_fops = &acpi_ec_info_ops;
		entry->data = acpi_driver_data(device);
		entry->owner = THIS_MODULE;
	}

599
	return 0;
L
Linus Torvalds 已提交
600 601
}

L
Len Brown 已提交
602
static int acpi_ec_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610
{

	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;
	}

611
	return 0;
L
Linus Torvalds 已提交
612 613 614 615 616 617
}

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

618
static int acpi_ec_add(struct acpi_device *device)
L
Linus Torvalds 已提交
619
{
L
Len Brown 已提交
620 621
	int result = 0;
	acpi_status status = AE_OK;
622
	struct acpi_ec *ec = NULL;
L
Luming Yu 已提交
623 624

	if (!device)
625
		return -EINVAL;
L
Luming Yu 已提交
626

627
	ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
628
	if (!ec)
629
		return -ENOMEM;
630 631 632

	ec->handle = device->handle;
	ec->uid = -1;
633
	mutex_init(&ec->lock);
634
	atomic_set(&ec->query_pending, 0);
635 636 637
	if (acpi_ec_mode == EC_INTR) {
		atomic_set(&ec->leaving_burst, 1);
		init_waitqueue_head(&ec->wait);
L
Luming Yu 已提交
638
	}
L
Linus Torvalds 已提交
639 640 641 642 643
	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? */
A
Alexey Starikovskiy 已提交
644
	acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
L
Linus Torvalds 已提交
645

J
Jiri Slaby 已提交
646 647 648
	/* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
	   http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
	if (ec_ecdt) {
L
Linus Torvalds 已提交
649
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
650 651
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
652

653
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
654
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
655 656 657 658 659 660

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
A
Alexey Starikovskiy 已提交
661
	status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
L
Linus Torvalds 已提交
662
	if (ACPI_FAILURE(status)) {
A
Alexey Starikovskiy 已提交
663 664
		ACPI_EXCEPTION((AE_INFO, status,
				"Obtaining GPE bit assignment"));
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672
		result = -ENODEV;
		goto end;
	}

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

673
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
A
Alexey Starikovskiy 已提交
674 675
			  acpi_device_name(device), acpi_device_bid(device),
			  (u32) ec->gpe));
L
Linus Torvalds 已提交
676 677 678 679

	if (!first_ec)
		first_ec = device;

A
Alexey Starikovskiy 已提交
680
      end:
L
Linus Torvalds 已提交
681 682 683
	if (result)
		kfree(ec);

684
	return result;
L
Linus Torvalds 已提交
685 686
}

L
Len Brown 已提交
687
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
688
{
689
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
690 691

	if (!device)
692
		return -EINVAL;
L
Linus Torvalds 已提交
693 694 695 696 697 698 699

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

700
	return 0;
L
Linus Torvalds 已提交
701 702 703
}

static acpi_status
L
Len Brown 已提交
704
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
705
{
706
	struct acpi_ec *ec = (struct acpi_ec *)context;
L
Linus Torvalds 已提交
707

B
Bob Moore 已提交
708
	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
717 718 719 720
	if (ec->data_addr == 0) {
		ec->data_addr = resource->data.io.minimum;
	} else if (ec->command_addr == 0) {
		ec->command_addr = resource->data.io.minimum;
L
Linus Torvalds 已提交
721 722 723 724 725 726 727
	} else {
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

L
Len Brown 已提交
728
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
729
{
L
Len Brown 已提交
730
	acpi_status status = AE_OK;
731
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
732 733

	if (!device)
734
		return -EINVAL;
L
Linus Torvalds 已提交
735 736 737 738

	ec = acpi_driver_data(device);

	if (!ec)
739
		return -EINVAL;
L
Linus Torvalds 已提交
740 741 742 743

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
744
	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
745
				     acpi_ec_io_ports, ec);
746
	if (ACPI_FAILURE(status) || ec->command_addr == 0) {
747 748
		ACPI_EXCEPTION((AE_INFO, status,
				"Error getting I/O port addresses"));
749
		return -ENODEV;
L
Linus Torvalds 已提交
750 751
	}

752
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
753
			  ec->gpe, ec->command_addr, ec->data_addr));
L
Linus Torvalds 已提交
754 755 756 757

	/*
	 * Install GPE handler
	 */
758
	status = acpi_install_gpe_handler(NULL, ec->gpe,
L
Len Brown 已提交
759 760
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
761
	if (ACPI_FAILURE(status)) {
762
		return -ENODEV;
L
Linus Torvalds 已提交
763
	}
764 765
	acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
766

767
	status = acpi_install_address_space_handler(ec->handle,
L
Len Brown 已提交
768 769 770
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
771
	if (ACPI_FAILURE(status)) {
A
Alexey Starikovskiy 已提交
772
		acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
773
		return -ENODEV;
L
Linus Torvalds 已提交
774 775
	}

776
	return AE_OK;
L
Linus Torvalds 已提交
777 778
}

L
Len Brown 已提交
779
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
780
{
L
Len Brown 已提交
781
	acpi_status status = AE_OK;
782
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
783 784

	if (!device)
785
		return -EINVAL;
L
Linus Torvalds 已提交
786 787 788

	ec = acpi_driver_data(device);

789
	status = acpi_remove_address_space_handler(ec->handle,
L
Len Brown 已提交
790 791
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
792
	if (ACPI_FAILURE(status))
793
		return -ENODEV;
L
Linus Torvalds 已提交
794

A
Alexey Starikovskiy 已提交
795
	status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
796
	if (ACPI_FAILURE(status))
797
		return -ENODEV;
L
Linus Torvalds 已提交
798

799
	return 0;
L
Linus Torvalds 已提交
800 801 802
}

static acpi_status __init
L
Len Brown 已提交
803 804
acpi_fake_ecdt_callback(acpi_handle handle,
			u32 Level, void *context, void **retval)
L
Linus Torvalds 已提交
805
{
L
Len Brown 已提交
806
	acpi_status status;
L
Linus Torvalds 已提交
807

808
	mutex_init(&ec_ecdt->lock);
809 810 811
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
	}
L
Linus Torvalds 已提交
812
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
813
				     acpi_ec_io_ports, ec_ecdt);
L
Linus Torvalds 已提交
814 815 816
	if (ACPI_FAILURE(status))
		return status;

817 818
	ec_ecdt->uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
L
Linus Torvalds 已提交
819

A
Alexey Starikovskiy 已提交
820
	status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe);
L
Linus Torvalds 已提交
821 822
	if (ACPI_FAILURE(status))
		return status;
823 824
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->handle = handle;
L
Linus Torvalds 已提交
825

826
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
A
Alexey Starikovskiy 已提交
827 828
			  ec_ecdt->gpe, ec_ecdt->command_addr,
			  ec_ecdt->data_addr));
L
Linus Torvalds 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842

	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 已提交
843
static int __init acpi_ec_fake_ecdt(void)
L
Linus Torvalds 已提交
844
{
L
Len Brown 已提交
845 846
	acpi_status status;
	int ret = 0;
L
Linus Torvalds 已提交
847

848
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
L
Linus Torvalds 已提交
849

850
	ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
851 852 853 854 855
	if (!ec_ecdt) {
		ret = -ENOMEM;
		goto error;
	}

L
Len Brown 已提交
856 857
	status = acpi_get_devices(ACPI_EC_HID,
				  acpi_fake_ecdt_callback, NULL, NULL);
L
Linus Torvalds 已提交
858 859 860 861
	if (ACPI_FAILURE(status)) {
		kfree(ec_ecdt);
		ec_ecdt = NULL;
		ret = -ENODEV;
862
		ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
L
Linus Torvalds 已提交
863 864 865
		goto error;
	}
	return 0;
A
Alexey Starikovskiy 已提交
866
      error:
L
Linus Torvalds 已提交
867 868 869
	return ret;
}

L
Len Brown 已提交
870
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
871
{
L
Len Brown 已提交
872 873
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
874

L
Len Brown 已提交
875 876 877
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Luming Yu 已提交
878 879 880
	if (ACPI_FAILURE(status))
		return -ENODEV;

881
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
L
Luming Yu 已提交
882 883 884 885

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
886
	ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
887 888 889
	if (!ec_ecdt)
		return -ENOMEM;

890
	mutex_init(&ec_ecdt->lock);
891 892
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
L
Luming Yu 已提交
893
	}
894 895
	ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
	ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
896
	ec_ecdt->gpe = ecdt_ptr->gpe_bit;
L
Linus Torvalds 已提交
897
	/* use the GL just to be safe */
898 899
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
900

A
Alexey Starikovskiy 已提交
901
	status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
L
Linus Torvalds 已提交
902 903 904 905 906
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
A
Alexey Starikovskiy 已提交
907
      error:
908
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
909 910 911 912 913 914 915
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

static int __initdata acpi_fake_ecdt_enabled;
L
Len Brown 已提交
916
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
917
{
L
Len Brown 已提交
918 919
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932

	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
	 */
933
	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
934 935
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
936 937 938
	if (ACPI_FAILURE(status)) {
		goto error;
	}
939 940
	acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
L
Len Brown 已提交
941 942 943 944 945 946

	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 已提交
947
	if (ACPI_FAILURE(status)) {
948
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
949
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
950 951 952 953 954
		goto error;
	}

	return 0;

L
Len Brown 已提交
955
      error:
956
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
957 958 959 960 961 962
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
963
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
964
{
L
Len Brown 已提交
965
	int result = 0;
L
Linus Torvalds 已提交
966 967

	if (acpi_disabled)
968
		return 0;
L
Linus Torvalds 已提交
969 970 971

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
972
		return -ENODEV;
L
Linus Torvalds 已提交
973 974 975 976 977

	/* 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);
978
		return -ENODEV;
L
Linus Torvalds 已提交
979 980
	}

981
	return result;
L
Linus Torvalds 已提交
982 983 984 985 986 987
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
988
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
989 990 991 992 993 994
{

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

995
	return;
L
Linus Torvalds 已提交
996
}
L
Len Brown 已提交
997
#endif				/* 0 */
L
Linus Torvalds 已提交
998 999 1000 1001

static int __init acpi_fake_ecdt_setup(char *str)
{
	acpi_fake_ecdt_enabled = 1;
1002
	return 1;
L
Linus Torvalds 已提交
1003
}
1004

L
Linus Torvalds 已提交
1005
__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1006
static int __init acpi_ec_set_intr_mode(char *str)
L
Luming Yu 已提交
1007
{
1008
	int intr;
1009

1010
	if (!get_option(&str, &intr))
1011 1012
		return 0;

1013
	if (intr) {
1014
		acpi_ec_mode = EC_INTR;
1015
	} else {
1016
		acpi_ec_mode = EC_POLL;
1017
	}
1018
	acpi_ec_driver.ops.add = acpi_ec_add;
A
Alexey Starikovskiy 已提交
1019 1020
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n",
			  intr ? "interrupt" : "polling"));
1021

1022
	return 1;
L
Luming Yu 已提交
1023
}
L
Len Brown 已提交
1024

1025
__setup("ec_intr=", acpi_ec_set_intr_mode);