ac.c 10.2 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
/*
 *  acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
 *
 *  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>
28
#include <linux/slab.h>
L
Linus Torvalds 已提交
29 30
#include <linux/init.h>
#include <linux/types.h>
31 32
#include <linux/dmi.h>
#include <linux/delay.h>
33
#ifdef CONFIG_ACPI_PROCFS_POWER
L
Linus Torvalds 已提交
34 35
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
36
#endif
37
#include <linux/platform_device.h>
38
#include <linux/power_supply.h>
L
Linus Torvalds 已提交
39 40 41
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

42 43
#define PREFIX "ACPI: "

L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52
#define ACPI_AC_CLASS			"ac_adapter"
#define ACPI_AC_DEVICE_NAME		"AC Adapter"
#define ACPI_AC_FILE_STATE		"state"
#define ACPI_AC_NOTIFY_STATUS		0x80
#define ACPI_AC_STATUS_OFFLINE		0x00
#define ACPI_AC_STATUS_ONLINE		0x01
#define ACPI_AC_STATUS_UNKNOWN		0xFF

#define _COMPONENT		ACPI_AC_COMPONENT
53
ACPI_MODULE_NAME("ac");
L
Linus Torvalds 已提交
54

55
MODULE_AUTHOR("Paul Diefenbaugh");
56
MODULE_DESCRIPTION("ACPI AC Adapter Driver");
L
Linus Torvalds 已提交
57 58
MODULE_LICENSE("GPL");

59
#ifdef CONFIG_ACPI_PROCFS_POWER
60 61
extern struct proc_dir_entry *acpi_lock_ac_dir(void);
extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
62 63
static int acpi_ac_open_fs(struct inode *inode, struct file *file);
#endif
64

65 66
static int ac_sleep_before_get_state_ms;

L
Linus Torvalds 已提交
67
struct acpi_ac {
68
	struct power_supply charger;
69 70
	struct acpi_device *adev;
	struct platform_device *pdev;
71
	unsigned long long state;
L
Linus Torvalds 已提交
72 73
};

74
#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
75

76
#ifdef CONFIG_ACPI_PROCFS_POWER
77
static const struct file_operations acpi_ac_fops = {
78
	.owner = THIS_MODULE,
L
Len Brown 已提交
79 80 81 82
	.open = acpi_ac_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
83
};
84
#endif
85

L
Linus Torvalds 已提交
86 87 88 89
/* --------------------------------------------------------------------------
                               AC Adapter Management
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
90
static int acpi_ac_get_state(struct acpi_ac *ac)
L
Linus Torvalds 已提交
91
{
92
	acpi_status status;
L
Linus Torvalds 已提交
93

94 95
	status = acpi_evaluate_integer(ac->adev->handle, "_PSR", NULL,
				       &ac->state);
L
Linus Torvalds 已提交
96
	if (ACPI_FAILURE(status)) {
97 98
		ACPI_EXCEPTION((AE_INFO, status,
				"Error reading AC Adapter state"));
L
Linus Torvalds 已提交
99
		ac->state = ACPI_AC_STATUS_UNKNOWN;
100
		return -ENODEV;
L
Linus Torvalds 已提交
101
	}
L
Len Brown 已提交
102

103
	return 0;
L
Linus Torvalds 已提交
104 105
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/* --------------------------------------------------------------------------
                            sysfs I/F
   -------------------------------------------------------------------------- */
static int get_ac_property(struct power_supply *psy,
			   enum power_supply_property psp,
			   union power_supply_propval *val)
{
	struct acpi_ac *ac = to_acpi_ac(psy);

	if (!ac)
		return -ENODEV;

	if (acpi_ac_get_state(ac))
		return -ENODEV;

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = ac->state;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static enum power_supply_property ac_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
};

135
#ifdef CONFIG_ACPI_PROCFS_POWER
L
Linus Torvalds 已提交
136 137 138 139
/* --------------------------------------------------------------------------
                              FS Interface (/proc)
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
140
static struct proc_dir_entry *acpi_ac_dir;
L
Linus Torvalds 已提交
141 142 143

static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
{
144
	struct acpi_ac *ac = seq->private;
L
Linus Torvalds 已提交
145 146 147


	if (!ac)
148
		return 0;
L
Linus Torvalds 已提交
149 150 151

	if (acpi_ac_get_state(ac)) {
		seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
152
		return 0;
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

	seq_puts(seq, "state:                   ");
	switch (ac->state) {
	case ACPI_AC_STATUS_OFFLINE:
		seq_puts(seq, "off-line\n");
		break;
	case ACPI_AC_STATUS_ONLINE:
		seq_puts(seq, "on-line\n");
		break;
	default:
		seq_puts(seq, "unknown\n");
		break;
	}

168
	return 0;
L
Linus Torvalds 已提交
169
}
L
Len Brown 已提交
170

L
Linus Torvalds 已提交
171 172
static int acpi_ac_open_fs(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
173
	return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
L
Linus Torvalds 已提交
174 175
}

176
static int acpi_ac_add_fs(struct acpi_ac *ac)
L
Linus Torvalds 已提交
177
{
L
Len Brown 已提交
178
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
179

180 181
	printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
			" please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
182 183 184 185
	if (!acpi_device_dir(ac->adev)) {
		acpi_device_dir(ac->adev) =
			proc_mkdir(acpi_device_bid(ac->adev), acpi_ac_dir);
		if (!acpi_device_dir(ac->adev))
186
			return -ENODEV;
L
Linus Torvalds 已提交
187 188 189
	}

	/* 'state' [R] */
190
	entry = proc_create_data(ACPI_AC_FILE_STATE,
191 192
				 S_IRUGO, acpi_device_dir(ac->adev),
				 &acpi_ac_fops, ac);
L
Linus Torvalds 已提交
193
	if (!entry)
194 195
		return -ENODEV;
	return 0;
L
Linus Torvalds 已提交
196 197
}

198
static int acpi_ac_remove_fs(struct acpi_ac *ac)
L
Linus Torvalds 已提交
199 200
{

201 202 203 204 205
	if (acpi_device_dir(ac->adev)) {
		remove_proc_entry(ACPI_AC_FILE_STATE,
				  acpi_device_dir(ac->adev));
		remove_proc_entry(acpi_device_bid(ac->adev), acpi_ac_dir);
		acpi_device_dir(ac->adev) = NULL;
L
Linus Torvalds 已提交
206 207
	}

208
	return 0;
L
Linus Torvalds 已提交
209
}
210
#endif
L
Linus Torvalds 已提交
211 212 213 214 215

/* --------------------------------------------------------------------------
                                   Driver Model
   -------------------------------------------------------------------------- */

216
static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data)
L
Linus Torvalds 已提交
217
{
218
	struct acpi_ac *ac = data;
L
Linus Torvalds 已提交
219 220

	if (!ac)
221
		return;
L
Linus Torvalds 已提交
222 223

	switch (event) {
L
Len Brown 已提交
224 225 226
	default:
		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
				  "Unsupported event [0x%x]\n", event));
L
Linus Torvalds 已提交
227
	case ACPI_AC_NOTIFY_STATUS:
228 229
	case ACPI_NOTIFY_BUS_CHECK:
	case ACPI_NOTIFY_DEVICE_CHECK:
230 231 232 233 234 235 236 237 238 239
		/*
		 * A buggy BIOS may notify AC first and then sleep for
		 * a specific time before doing actual operations in the
		 * EC event handler (_Qxx). This will cause the AC state
		 * reported by the ACPI event to be incorrect, so wait for a
		 * specific time for the EC event handler to make progress.
		 */
		if (ac_sleep_before_get_state_ms > 0)
			msleep(ac_sleep_before_get_state_ms);

L
Linus Torvalds 已提交
240
		acpi_ac_get_state(ac);
241 242 243 244
		acpi_bus_generate_netlink_event(ac->adev->pnp.device_class,
						dev_name(&ac->pdev->dev),
						event, (u32) ac->state);
		acpi_notifier_call_chain(ac->adev, event, (u32) ac->state);
245
		kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
L
Linus Torvalds 已提交
246 247
	}

248
	return;
L
Linus Torvalds 已提交
249 250
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int thinkpad_e530_quirk(const struct dmi_system_id *d)
{
	ac_sleep_before_get_state_ms = 1000;
	return 0;
}

static struct dmi_system_id ac_dmi_table[] = {
	{
	.callback = thinkpad_e530_quirk,
	.ident = "thinkpad e530",
	.matches = {
		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
		DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
		},
	},
	{},
};

269
static int acpi_ac_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
270
{
L
Len Brown 已提交
271 272
	int result = 0;
	struct acpi_ac *ac = NULL;
273
	struct acpi_device *adev;
L
Linus Torvalds 已提交
274

275
	if (!pdev)
276
		return -EINVAL;
L
Linus Torvalds 已提交
277

278 279 280 281
	result = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
	if (result)
		return -ENODEV;

282
	ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
L
Linus Torvalds 已提交
283
	if (!ac)
284
		return -ENOMEM;
L
Linus Torvalds 已提交
285

286 287 288 289 290
	strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME);
	strcpy(acpi_device_class(adev), ACPI_AC_CLASS);
	ac->adev = adev;
	ac->pdev = pdev;
	platform_set_drvdata(pdev, ac);
L
Linus Torvalds 已提交
291 292 293 294 295

	result = acpi_ac_get_state(ac);
	if (result)
		goto end;

296
#ifdef CONFIG_ACPI_PROCFS_POWER
297
	result = acpi_ac_add_fs(ac);
L
Linus Torvalds 已提交
298 299
	if (result)
		goto end;
300 301
#endif
	ac->charger.name = acpi_device_bid(adev);
302 303 304 305
	ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
	ac->charger.properties = ac_props;
	ac->charger.num_properties = ARRAY_SIZE(ac_props);
	ac->charger.get_property = get_ac_property;
306
	result = power_supply_register(&pdev->dev, &ac->charger);
307 308
	if (result)
		goto end;
L
Linus Torvalds 已提交
309

310 311 312 313 314 315
	result = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
			ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler, ac);
	if (result) {
		power_supply_unregister(&ac->charger);
		goto end;
	}
L
Len Brown 已提交
316
	printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
317
	       acpi_device_name(adev), acpi_device_bid(adev),
L
Len Brown 已提交
318
	       ac->state ? "on-line" : "off-line");
L
Linus Torvalds 已提交
319

L
Len Brown 已提交
320
      end:
L
Linus Torvalds 已提交
321
	if (result) {
322
#ifdef CONFIG_ACPI_PROCFS_POWER
323
		acpi_ac_remove_fs(ac);
324
#endif
L
Linus Torvalds 已提交
325 326 327
		kfree(ac);
	}

328
	dmi_check_system(ac_dmi_table);
329
	return result;
L
Linus Torvalds 已提交
330 331
}

332
#ifdef CONFIG_PM_SLEEP
333
static int acpi_ac_resume(struct device *dev)
334 335 336
{
	struct acpi_ac *ac;
	unsigned old_state;
337 338 339 340

	if (!dev)
		return -EINVAL;

341
	ac = platform_get_drvdata(to_platform_device(dev));
342
	if (!ac)
343
		return -EINVAL;
344

345 346 347 348 349 350 351
	old_state = ac->state;
	if (acpi_ac_get_state(ac))
		return 0;
	if (old_state != ac->state)
		kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
	return 0;
}
352
#endif
353
static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume);
354

355
static int acpi_ac_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
356
{
357
	struct acpi_ac *ac;
L
Linus Torvalds 已提交
358

359
	if (!pdev)
360
		return -EINVAL;
L
Linus Torvalds 已提交
361

362 363
	acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
			ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler);
L
Linus Torvalds 已提交
364

365
	ac = platform_get_drvdata(pdev);
366 367
	if (ac->charger.dev)
		power_supply_unregister(&ac->charger);
368

369
#ifdef CONFIG_ACPI_PROCFS_POWER
370
	acpi_ac_remove_fs(ac);
371
#endif
L
Linus Torvalds 已提交
372 373 374

	kfree(ac);

375
	return 0;
L
Linus Torvalds 已提交
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
static const struct acpi_device_id acpi_ac_match[] = {
	{ "ACPI0003", 0 },
	{ }
};
MODULE_DEVICE_TABLE(acpi, acpi_ac_match);

static struct platform_driver acpi_ac_driver = {
	.probe          = acpi_ac_probe,
	.remove         = acpi_ac_remove,
	.driver         = {
		.name   = "acpi-ac",
		.owner  = THIS_MODULE,
		.pm     = &acpi_ac_pm_ops,
		.acpi_match_table = ACPI_PTR(acpi_ac_match),
	},
};

L
Len Brown 已提交
395
static int __init acpi_ac_init(void)
L
Linus Torvalds 已提交
396
{
397
	int result;
L
Linus Torvalds 已提交
398

P
Pavel Machek 已提交
399 400
	if (acpi_disabled)
		return -ENODEV;
L
Linus Torvalds 已提交
401

402
#ifdef CONFIG_ACPI_PROCFS_POWER
403
	acpi_ac_dir = acpi_lock_ac_dir();
L
Linus Torvalds 已提交
404
	if (!acpi_ac_dir)
405
		return -ENODEV;
406
#endif
L
Linus Torvalds 已提交
407

408
	result = platform_driver_register(&acpi_ac_driver);
L
Linus Torvalds 已提交
409
	if (result < 0) {
410
#ifdef CONFIG_ACPI_PROCFS_POWER
411
		acpi_unlock_ac_dir(acpi_ac_dir);
412
#endif
413
		return -ENODEV;
L
Linus Torvalds 已提交
414 415
	}

416
	return 0;
L
Linus Torvalds 已提交
417 418
}

L
Len Brown 已提交
419
static void __exit acpi_ac_exit(void)
L
Linus Torvalds 已提交
420
{
421
	platform_driver_unregister(&acpi_ac_driver);
422
#ifdef CONFIG_ACPI_PROCFS_POWER
423
	acpi_unlock_ac_dir(acpi_ac_dir);
424
#endif
L
Linus Torvalds 已提交
425 426 427
}
module_init(acpi_ac_init);
module_exit(acpi_ac_exit);