ec.c 24.3 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

/* EC status register */
L
Linus Torvalds 已提交
50 51
#define ACPI_EC_FLAG_OBF	0x01	/* Output buffer full */
#define ACPI_EC_FLAG_IBF	0x02	/* Input buffer full */
D
Dmitry Torokhov 已提交
52
#define ACPI_EC_FLAG_BURST	0x10	/* burst mode */
L
Linus Torvalds 已提交
53
#define ACPI_EC_FLAG_SCI	0x20	/* EC-SCI occurred */
54 55

/* EC commands */
L
Linus Torvalds 已提交
56 57
#define ACPI_EC_COMMAND_READ	0x80
#define ACPI_EC_COMMAND_WRITE	0x81
D
Dmitry Torokhov 已提交
58 59
#define ACPI_EC_BURST_ENABLE	0x82
#define ACPI_EC_BURST_DISABLE	0x83
L
Linus Torvalds 已提交
60
#define ACPI_EC_COMMAND_QUERY	0x84
61 62 63 64 65 66 67 68 69 70 71 72 73 74

/* EC events */
enum {
	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
	ACPI_EC_EVENT_IBF_0,		/* Input buffer empty */
};

#define ACPI_EC_DELAY		50	/* Wait 50ms max. during EC ops */
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */
#define ACPI_EC_UDELAY         100	/* Poll @ 100us increments */
#define ACPI_EC_UDELAY_COUNT   1000	/* Wait 10ms max. during EC ops */

enum {
	EC_INTR = 1,	/* Output buffer full */
75
	EC_POLL,	/* Input buffer empty */
76 77
};

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

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

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

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

L
Linus Torvalds 已提交
113 114 115 116
/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

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

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

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

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

137 138
static int acpi_ec_check_status(u8 status, u8 event)
{
L
Luming Yu 已提交
139
	switch (event) {
140 141 142
	case ACPI_EC_EVENT_OBF_1:
		if (status & ACPI_EC_FLAG_OBF)
			return 1;
L
Luming Yu 已提交
143
		break;
144 145 146
	case ACPI_EC_EVENT_IBF_0:
		if (!(status & ACPI_EC_FLAG_IBF))
			return 1;
L
Luming Yu 已提交
147 148
		break;
	default:
149
		break;
L
Luming Yu 已提交
150 151
	}

152
	return 0;
L
Luming Yu 已提交
153
}
D
Dmitry Torokhov 已提交
154

155
static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
156
{
157 158
	int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
	long time_left;
D
Dmitry Torokhov 已提交
159

160
	ec->expect_event = event;
161
	if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
162 163
		ec->expect_event = 0;
		return 0;
164 165
	}

166 167 168 169 170 171 172 173 174
	do {
		if (acpi_ec_mode == EC_POLL) {
			udelay(ACPI_EC_UDELAY);
		} else {
			time_left = wait_event_timeout(ec->wait,
				    !ec->expect_event,
				    msecs_to_jiffies(ACPI_EC_DELAY));
			if (time_left > 0) {
				ec->expect_event = 0;
175
				return 0;
176
			}
177
		}
178 179 180 181 182 183 184
		if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
			ec->expect_event = 0;
			return 0;
		}
	} while (--i > 0);

	ec->expect_event = 0;
L
Linus Torvalds 已提交
185

186
	return -ETIME;
L
Linus Torvalds 已提交
187 188
}

189
#ifdef ACPI_FUTURE_USAGE
190 191 192 193
/*
 * Note: samsung nv5000 doesn't work with ec burst mode.
 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
 */
194
int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
195
{
196 197
	u8 tmp = 0;
	u8 status = 0;
D
Dmitry Torokhov 已提交
198 199 200


	status = acpi_ec_read_status(ec);
L
Len Brown 已提交
201
	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
202
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
L
Len Brown 已提交
203
		if (status)
204
			goto end;
205 206 207
		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 已提交
208
		if (tmp != 0x90) {	/* Burst ACK byte */
209
			return -EINVAL;
D
Dmitry Torokhov 已提交
210
		}
211 212
	}

213
	atomic_set(&ec->leaving_burst, 0);
214
	return 0;
215 216
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
217
	return -1;
D
Dmitry Torokhov 已提交
218 219
}

220
int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
221
{
222
	u8 status = 0;
D
Dmitry Torokhov 已提交
223 224


225 226
	status = acpi_ec_read_status(ec);
	if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
227
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
228 229
		if(status)
			goto end;
230 231
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
		acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
232
	}
233
	atomic_set(&ec->leaving_burst, 1);
234
	return 0;
235 236
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
237
	return -1;
D
Dmitry Torokhov 已提交
238
}
239
#endif /* ACPI_FUTURE_USAGE */
D
Dmitry Torokhov 已提交
240

241
static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
242 243
					const u8 *wdata, unsigned wdata_len,
					u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
244
{
245
	int result;
L
Luming Yu 已提交
246

247
	acpi_ec_write_cmd(ec, command);
L
Luming Yu 已提交
248

249
	for (; wdata_len > 0; wdata_len --) {
250
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
251 252
		if (result)
			return result;
253
		acpi_ec_write_data(ec, *(wdata++));
254
	}
L
Luming Yu 已提交
255

256
	if (command == ACPI_EC_COMMAND_WRITE) {
257
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
258 259 260
		if (result)
			return result;
	}
L
Luming Yu 已提交
261

262
	for (; rdata_len > 0; rdata_len --) {
263
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
264 265
		if (result)
			return result;
L
Len Brown 已提交
266

267
		*(rdata++) = acpi_ec_read_data(ec);
268
	}
L
Luming Yu 已提交
269

270
	return 0;
L
Luming Yu 已提交
271 272
}

273 274 275
static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
				const u8 *wdata, unsigned wdata_len,
				u8 *rdata, unsigned rdata_len)
L
Linus Torvalds 已提交
276
{
277
	int status;
L
Len Brown 已提交
278
	u32 glk;
L
Linus Torvalds 已提交
279

280
	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
281
		return -EINVAL;
L
Linus Torvalds 已提交
282

283 284
        if (rdata)
                memset(rdata, 0, rdata_len);
L
Linus Torvalds 已提交
285

286
	if (ec->global_lock) {
L
Linus Torvalds 已提交
287 288
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
289
			return -ENODEV;
L
Linus Torvalds 已提交
290
	}
291
	down(&ec->sem);
D
Dmitry Torokhov 已提交
292

293
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
294
	if (status) {
295
		printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
D
Dmitry Torokhov 已提交
296
		goto end;
297
	}
L
Linus Torvalds 已提交
298

299 300 301
        status = acpi_ec_transaction_unlocked(ec, command,
                                              wdata, wdata_len,
                                              rdata, rdata_len);
L
Linus Torvalds 已提交
302

303
end:
304
	up(&ec->sem);
L
Linus Torvalds 已提交
305

306
	if (ec->global_lock)
L
Linus Torvalds 已提交
307 308
		acpi_release_global_lock(glk);

309
	return status;
L
Linus Torvalds 已提交
310 311
}

312
static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
313 314 315 316 317 318 319 320 321
{
	int result;
	u8 d;

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

323 324 325 326 327 328 329
static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
{
        u8 wdata[2] = { address, data };
        return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
				   wdata, 2, NULL, 0);
}

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

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

L
Linus Torvalds 已提交
353 354
EXPORT_SYMBOL(ec_read);

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

L
Linus Torvalds 已提交
370 371
EXPORT_SYMBOL(ec_write);

372 373 374
extern int ec_transaction(u8 command,
                          const u8 *wdata, unsigned wdata_len,
                          u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
375
{
376
	struct acpi_ec *ec;
L
Luming Yu 已提交
377

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

381
	ec = acpi_driver_data(first_ec);
L
Luming Yu 已提交
382

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

387 388
EXPORT_SYMBOL(ec_transaction);

389
static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
390 391
{
	int result;
392
        u8 d;
L
Linus Torvalds 已提交
393

394 395
        if (!ec || !data)
                return -EINVAL;
L
Linus Torvalds 已提交
396

397 398 399 400 401
        /*
         * 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).
         */
402

403 404 405
        result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
        if (result)
                return result;
L
Linus Torvalds 已提交
406

407 408
        if (!d)
                return -ENODATA;
L
Linus Torvalds 已提交
409

410 411
        *data = d;
        return 0;
L
Linus Torvalds 已提交
412 413 414 415 416 417
}

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

418
struct acpi_ec_query_data {
L
Len Brown 已提交
419 420
	acpi_handle handle;
	u8 data;
L
Linus Torvalds 已提交
421 422
};

L
Len Brown 已提交
423
static void acpi_ec_gpe_query(void *ec_cxt)
L
Luming Yu 已提交
424
{
425
	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
426 427
	u8 value = 0;
	static char object_name[8];
L
Luming Yu 已提交
428

429
	if (!ec)
L
Luming Yu 已提交
430 431
		goto end;

432
	value = acpi_ec_read_status(ec);
L
Luming Yu 已提交
433 434 435 436 437 438 439

	if (!(value & ACPI_EC_FLAG_SCI))
		goto end;

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

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

442
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
L
Luming Yu 已提交
443

444
	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
L
Luming Yu 已提交
445

L
Len Brown 已提交
446
      end:
447
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Luming Yu 已提交
448
}
L
Linus Torvalds 已提交
449

L
Len Brown 已提交
450
static u32 acpi_ec_gpe_handler(void *data)
L
Linus Torvalds 已提交
451
{
L
Len Brown 已提交
452
	acpi_status status = AE_OK;
453
	u8 value;
454
	struct acpi_ec *ec = (struct acpi_ec *)data;
L
Linus Torvalds 已提交
455

456
	acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
457
	value = acpi_ec_read_status(ec);
L
Linus Torvalds 已提交
458

459 460 461 462 463
	if (acpi_ec_mode == EC_INTR) {
		if (acpi_ec_check_status(value, ec->expect_event)) {
			ec->expect_event = 0;
			wake_up(&ec->wait);
		}
D
Dmitry Torokhov 已提交
464 465
	}

L
Len Brown 已提交
466
	if (value & ACPI_EC_FLAG_SCI) {
467
		status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
L
Luming Yu 已提交
468
		return status == AE_OK ?
L
Len Brown 已提交
469 470
		    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
	}
471
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
472
	return status == AE_OK ?
L
Len Brown 已提交
473
	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480
}

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

static acpi_status
L
Len Brown 已提交
481 482
acpi_ec_space_setup(acpi_handle region_handle,
		    u32 function, void *handler_context, void **return_context)
L
Linus Torvalds 已提交
483 484 485 486 487
{
	/*
	 * The EC object is in the handler context and is needed
	 * when calling the acpi_ec_space_handler.
	 */
L
Len Brown 已提交
488 489
	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
	    handler_context : NULL;
L
Linus Torvalds 已提交
490 491 492 493 494

	return AE_OK;
}

static acpi_status
L
Len Brown 已提交
495 496 497 498 499
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 已提交
500
{
L
Len Brown 已提交
501
	int result = 0;
502
	struct acpi_ec *ec = NULL;
L
Len Brown 已提交
503 504 505
	u64 temp = *value;
	acpi_integer f_v = 0;
	int i = 0;
L
Linus Torvalds 已提交
506 507 508


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

L
Luming Yu 已提交
511
	if (bit_width != 8 && acpi_strict) {
512
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
513 514
	}

515
	ec = (struct acpi_ec *)handler_context;
L
Linus Torvalds 已提交
516

L
Len Brown 已提交
517
      next_byte:
L
Linus Torvalds 已提交
518 519
	switch (function) {
	case ACPI_READ:
L
Luming Yu 已提交
520
		temp = 0;
521
		result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
L
Linus Torvalds 已提交
522 523
		break;
	case ACPI_WRITE:
L
Luming Yu 已提交
524
		result = acpi_ec_write(ec, (u8) address, (u8) temp);
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532
		break;
	default:
		result = -EINVAL;
		goto out;
		break;
	}

	bit_width -= 8;
L
Luming Yu 已提交
533 534 535 536 537
	if (bit_width) {
		if (function == ACPI_READ)
			f_v |= temp << 8 * i;
		if (function == ACPI_WRITE)
			temp >>= 8;
L
Linus Torvalds 已提交
538
		i++;
A
Andrew Morton 已提交
539
		address++;
L
Linus Torvalds 已提交
540 541 542
		goto next_byte;
	}

L
Luming Yu 已提交
543 544
	if (function == ACPI_READ) {
		f_v |= temp << 8 * i;
L
Linus Torvalds 已提交
545 546 547
		*value = f_v;
	}

L
Len Brown 已提交
548
      out:
L
Linus Torvalds 已提交
549 550
	switch (result) {
	case -EINVAL:
551
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
552 553
		break;
	case -ENODEV:
554
		return AE_NOT_FOUND;
L
Linus Torvalds 已提交
555 556
		break;
	case -ETIME:
557
		return AE_TIME;
L
Linus Torvalds 已提交
558 559
		break;
	default:
560
		return AE_OK;
L
Linus Torvalds 已提交
561 562 563 564 565 566 567
	}
}

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

L
Len Brown 已提交
568
static struct proc_dir_entry *acpi_ec_dir;
L
Linus Torvalds 已提交
569

L
Len Brown 已提交
570
static int acpi_ec_read_info(struct seq_file *seq, void *offset)
L
Linus Torvalds 已提交
571
{
572
	struct acpi_ec *ec = (struct acpi_ec *)seq->private;
L
Linus Torvalds 已提交
573 574 575 576 577 578


	if (!ec)
		goto end;

	seq_printf(seq, "gpe bit:                 0x%02x\n",
579
		   (u32) ec->gpe_bit);
L
Linus Torvalds 已提交
580
	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
581 582
		   (u32) ec->command_addr,
		   (u32) ec->data_addr);
L
Linus Torvalds 已提交
583
	seq_printf(seq, "use global lock:         %s\n",
584 585
		   ec->global_lock ? "yes" : "no");
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
586

L
Len Brown 已提交
587
      end:
588
	return 0;
L
Linus Torvalds 已提交
589 590 591 592 593 594 595
}

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

596
static struct file_operations acpi_ec_info_ops = {
L
Len Brown 已提交
597 598 599 600
	.open = acpi_ec_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
601 602 603
	.owner = THIS_MODULE,
};

L
Len Brown 已提交
604
static int acpi_ec_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
605
{
L
Len Brown 已提交
606
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
607 608 609 610


	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
611
						     acpi_ec_dir);
L
Linus Torvalds 已提交
612
		if (!acpi_device_dir(device))
613
			return -ENODEV;
L
Linus Torvalds 已提交
614 615 616
	}

	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
L
Len Brown 已提交
617
				  acpi_device_dir(device));
L
Linus Torvalds 已提交
618
	if (!entry)
619
		return -ENODEV;
L
Linus Torvalds 已提交
620 621 622 623 624 625
	else {
		entry->proc_fops = &acpi_ec_info_ops;
		entry->data = acpi_driver_data(device);
		entry->owner = THIS_MODULE;
	}

626
	return 0;
L
Linus Torvalds 已提交
627 628
}

L
Len Brown 已提交
629
static int acpi_ec_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637
{

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

638
	return 0;
L
Linus Torvalds 已提交
639 640 641 642 643 644
}

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

645
static int acpi_ec_add(struct acpi_device *device)
L
Linus Torvalds 已提交
646
{
L
Len Brown 已提交
647 648
	int result = 0;
	acpi_status status = AE_OK;
649
	struct acpi_ec *ec = NULL;
L
Luming Yu 已提交
650 651 652


	if (!device)
653
		return -EINVAL;
L
Luming Yu 已提交
654

655
	ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
656
	if (!ec)
657
		return -ENOMEM;
658 659 660 661 662 663 664 665
	memset(ec, 0, sizeof(struct acpi_ec));

	ec->handle = device->handle;
	ec->uid = -1;
	init_MUTEX(&ec->sem);
	if (acpi_ec_mode == EC_INTR) {
		atomic_set(&ec->leaving_burst, 1);
		init_waitqueue_head(&ec->wait);
L
Luming Yu 已提交
666
	}
L
Linus Torvalds 已提交
667 668 669 670 671
	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? */
672 673
	acpi_evaluate_integer(ec->handle, "_GLK", NULL,
			      &ec->global_lock);
L
Linus Torvalds 已提交
674

J
Jiri Slaby 已提交
675 676 677
	/* 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 已提交
678
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
679 680
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
681

682
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
683
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
684 685 686 687 688 689

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
L
Len Brown 已提交
690
	status =
691 692
	    acpi_evaluate_integer(ec->handle, "_GPE", NULL,
				  &ec->gpe_bit);
L
Linus Torvalds 已提交
693
	if (ACPI_FAILURE(status)) {
694
		ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702
		result = -ENODEV;
		goto end;
	}

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

703
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
L
Len Brown 已提交
704
	       acpi_device_name(device), acpi_device_bid(device),
705
	       (u32) ec->gpe_bit));
L
Linus Torvalds 已提交
706 707 708 709

	if (!first_ec)
		first_ec = device;

710
  end:
L
Linus Torvalds 已提交
711 712 713
	if (result)
		kfree(ec);

714
	return result;
L
Linus Torvalds 已提交
715 716
}

L
Len Brown 已提交
717
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
718
{
719
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
720 721 722


	if (!device)
723
		return -EINVAL;
L
Linus Torvalds 已提交
724 725 726 727 728 729 730

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

731
	return 0;
L
Linus Torvalds 已提交
732 733 734
}

static acpi_status
L
Len Brown 已提交
735
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
736
{
737
	struct acpi_ec *ec = (struct acpi_ec *)context;
L
Linus Torvalds 已提交
738

B
Bob Moore 已提交
739
	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
748 749 750 751
	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 已提交
752 753 754 755 756 757 758
	} else {
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

L
Len Brown 已提交
759
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
760
{
L
Len Brown 已提交
761
	acpi_status status = AE_OK;
762
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
763 764 765


	if (!device)
766
		return -EINVAL;
L
Linus Torvalds 已提交
767 768 769 770

	ec = acpi_driver_data(device);

	if (!ec)
771
		return -EINVAL;
L
Linus Torvalds 已提交
772 773 774 775

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
776
	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
777
				     acpi_ec_io_ports, ec);
778
	if (ACPI_FAILURE(status) || ec->command_addr == 0) {
779 780
		ACPI_EXCEPTION((AE_INFO, status,
				"Error getting I/O port addresses"));
781
		return -ENODEV;
L
Linus Torvalds 已提交
782 783
	}

784 785
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
			  ec->gpe_bit, ec->command_addr, ec->data_addr));
L
Linus Torvalds 已提交
786 787 788 789

	/*
	 * Install GPE handler
	 */
790
	status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
791 792
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
793
	if (ACPI_FAILURE(status)) {
794
		return -ENODEV;
L
Linus Torvalds 已提交
795
	}
796 797
	acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
798

799
	status = acpi_install_address_space_handler(ec->handle,
L
Len Brown 已提交
800 801 802
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
803
	if (ACPI_FAILURE(status)) {
804
		acpi_remove_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
805
					&acpi_ec_gpe_handler);
806
		return -ENODEV;
L
Linus Torvalds 已提交
807 808
	}

809
	return AE_OK;
L
Linus Torvalds 已提交
810 811
}

L
Len Brown 已提交
812
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
813
{
L
Len Brown 已提交
814
	acpi_status status = AE_OK;
815
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
816 817 818


	if (!device)
819
		return -EINVAL;
L
Linus Torvalds 已提交
820 821 822

	ec = acpi_driver_data(device);

823
	status = acpi_remove_address_space_handler(ec->handle,
L
Len Brown 已提交
824 825
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
826
	if (ACPI_FAILURE(status))
827
		return -ENODEV;
L
Linus Torvalds 已提交
828

L
Len Brown 已提交
829
	status =
830
	    acpi_remove_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
831
				    &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
832
	if (ACPI_FAILURE(status))
833
		return -ENODEV;
L
Linus Torvalds 已提交
834

835
	return 0;
L
Linus Torvalds 已提交
836 837 838
}

static acpi_status __init
L
Len Brown 已提交
839 840
acpi_fake_ecdt_callback(acpi_handle handle,
			u32 Level, void *context, void **retval)
L
Linus Torvalds 已提交
841
{
L
Len Brown 已提交
842
	acpi_status status;
L
Linus Torvalds 已提交
843

844 845 846 847
	init_MUTEX(&ec_ecdt->sem);
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
	}
L
Linus Torvalds 已提交
848
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
849
				     acpi_ec_io_ports, ec_ecdt);
L
Linus Torvalds 已提交
850 851 852
	if (ACPI_FAILURE(status))
		return status;

853 854
	ec_ecdt->uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
L
Linus Torvalds 已提交
855

L
Len Brown 已提交
856 857
	status =
	    acpi_evaluate_integer(handle, "_GPE", NULL,
858
				  &ec_ecdt->gpe_bit);
L
Linus Torvalds 已提交
859 860
	if (ACPI_FAILURE(status))
		return status;
861 862
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->handle = handle;
L
Linus Torvalds 已提交
863

864 865
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
	       ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879

	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 已提交
880
static int __init acpi_ec_fake_ecdt(void)
L
Linus Torvalds 已提交
881
{
L
Len Brown 已提交
882 883
	acpi_status status;
	int ret = 0;
L
Linus Torvalds 已提交
884

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

887
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
888 889 890 891
	if (!ec_ecdt) {
		ret = -ENOMEM;
		goto error;
	}
892
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Linus Torvalds 已提交
893

L
Len Brown 已提交
894 895
	status = acpi_get_devices(ACPI_EC_HID,
				  acpi_fake_ecdt_callback, NULL, NULL);
L
Linus Torvalds 已提交
896 897 898 899
	if (ACPI_FAILURE(status)) {
		kfree(ec_ecdt);
		ec_ecdt = NULL;
		ret = -ENODEV;
900
		ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
L
Linus Torvalds 已提交
901 902 903
		goto error;
	}
	return 0;
904
  error:
L
Linus Torvalds 已提交
905 906 907
	return ret;
}

L
Len Brown 已提交
908
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
909
{
L
Len Brown 已提交
910 911
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
912

L
Len Brown 已提交
913 914 915
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Luming Yu 已提交
916 917 918
	if (ACPI_FAILURE(status))
		return -ENODEV;

919
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
L
Luming Yu 已提交
920 921 922 923

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
924
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
925 926
	if (!ec_ecdt)
		return -ENOMEM;
927
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Luming Yu 已提交
928

929 930 931
	init_MUTEX(&ec_ecdt->sem);
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
L
Luming Yu 已提交
932
	}
933 934
	ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
	ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
935
	ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
L
Linus Torvalds 已提交
936
	/* use the GL just to be safe */
937 938
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
939

L
Len Brown 已提交
940
	status =
941
	    acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
L
Linus Torvalds 已提交
942 943 944 945 946
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
947 948
  error:
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
949 950 951 952 953 954 955
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

static int __initdata acpi_fake_ecdt_enabled;
L
Len Brown 已提交
956
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
957
{
L
Len Brown 已提交
958 959
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972

	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
	 */
973
	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
974 975
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
976 977 978
	if (ACPI_FAILURE(status)) {
		goto error;
	}
979 980
	acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
L
Len Brown 已提交
981 982 983 984 985 986

	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 已提交
987
	if (ACPI_FAILURE(status)) {
988
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
989
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
990 991 992 993 994
		goto error;
	}

	return 0;

L
Len Brown 已提交
995
      error:
996
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
1003
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
1004
{
L
Len Brown 已提交
1005
	int result = 0;
L
Linus Torvalds 已提交
1006 1007 1008


	if (acpi_disabled)
1009
		return 0;
L
Linus Torvalds 已提交
1010 1011 1012

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
1013
		return -ENODEV;
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018

	/* 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);
1019
		return -ENODEV;
L
Linus Torvalds 已提交
1020 1021
	}

1022
	return result;
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
1029
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035
{

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

1036
	return;
L
Linus Torvalds 已提交
1037
}
L
Len Brown 已提交
1038
#endif				/* 0 */
L
Linus Torvalds 已提交
1039 1040 1041 1042

static int __init acpi_fake_ecdt_setup(char *str)
{
	acpi_fake_ecdt_enabled = 1;
1043
	return 1;
L
Linus Torvalds 已提交
1044
}
1045

L
Linus Torvalds 已提交
1046
__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1047
static int __init acpi_ec_set_intr_mode(char *str)
L
Luming Yu 已提交
1048
{
1049
	int intr;
1050

1051
	if (!get_option(&str, &intr))
1052 1053
		return 0;

1054
	if (intr) {
1055
		acpi_ec_mode = EC_INTR;
1056
	} else {
1057
		acpi_ec_mode = EC_POLL;
1058
	}
1059 1060 1061
	acpi_ec_driver.ops.add = acpi_ec_add;
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));

1062
	return 1;
L
Luming Yu 已提交
1063
}
L
Len Brown 已提交
1064

1065
__setup("ec_intr=", acpi_ec_set_intr_mode);