sys.c 14.0 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
/*
 * sys.c - pseudo-bus for system 'devices' (cpus, PICs, timers, etc)
 *
 * Copyright (c) 2002-3 Patrick Mochel
 *               2002-3 Open Source Development Lab
 *
 * This file is released under the GPLv2
 *
 * This exports a 'system' bus type.
 * By default, a 'sys' bus gets added to the root of the system. There will
 * always be core system devices. Devices can use sysdev_register() to
 * add themselves as children of the system bus.
 */

#include <linux/sysdev.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
22
#include <linux/pm.h>
23
#include <linux/device.h>
24
#include <linux/mutex.h>
25
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
26

27 28
#include "base.h"

L
Linus Torvalds 已提交
29 30 31 32 33
#define to_sysdev(k) container_of(k, struct sys_device, kobj)
#define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)


static ssize_t
34
sysdev_show(struct kobject *kobj, struct attribute *attr, char *buffer)
L
Linus Torvalds 已提交
35
{
36 37
	struct sys_device *sysdev = to_sysdev(kobj);
	struct sysdev_attribute *sysdev_attr = to_sysdev_attr(attr);
L
Linus Torvalds 已提交
38 39

	if (sysdev_attr->show)
40
		return sysdev_attr->show(sysdev, sysdev_attr, buffer);
41
	return -EIO;
L
Linus Torvalds 已提交
42 43 44 45
}


static ssize_t
46 47
sysdev_store(struct kobject *kobj, struct attribute *attr,
	     const char *buffer, size_t count)
L
Linus Torvalds 已提交
48
{
49 50
	struct sys_device *sysdev = to_sysdev(kobj);
	struct sysdev_attribute *sysdev_attr = to_sysdev_attr(attr);
L
Linus Torvalds 已提交
51 52

	if (sysdev_attr->store)
53
		return sysdev_attr->store(sysdev, sysdev_attr, buffer, count);
54
	return -EIO;
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66
}

static struct sysfs_ops sysfs_ops = {
	.show	= sysdev_show,
	.store	= sysdev_store,
};

static struct kobj_type ktype_sysdev = {
	.sysfs_ops	= &sysfs_ops,
};


67
int sysdev_create_file(struct sys_device *s, struct sysdev_attribute *a)
L
Linus Torvalds 已提交
68 69 70 71 72
{
	return sysfs_create_file(&s->kobj, &a->attr);
}


73
void sysdev_remove_file(struct sys_device *s, struct sysdev_attribute *a)
L
Linus Torvalds 已提交
74 75 76 77 78 79 80
{
	sysfs_remove_file(&s->kobj, &a->attr);
}

EXPORT_SYMBOL_GPL(sysdev_create_file);
EXPORT_SYMBOL_GPL(sysdev_remove_file);

81 82 83 84 85 86 87
#define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
#define to_sysdev_class_attr(a) container_of(a, \
	struct sysdev_class_attribute, attr)

static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
				 char *buffer)
{
88
	struct sysdev_class *class = to_sysdev_class(kobj);
89 90 91 92 93 94 95 96 97 98
	struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);

	if (class_attr->show)
		return class_attr->show(class, buffer);
	return -EIO;
}

static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
				  const char *buffer, size_t count)
{
99 100
	struct sysdev_class *class = to_sysdev_class(kobj);
	struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
101 102 103 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

	if (class_attr->store)
		return class_attr->store(class, buffer, count);
	return -EIO;
}

static struct sysfs_ops sysfs_class_ops = {
	.show	= sysdev_class_show,
	.store	= sysdev_class_store,
};

static struct kobj_type ktype_sysdev_class = {
	.sysfs_ops	= &sysfs_class_ops,
};

int sysdev_class_create_file(struct sysdev_class *c,
			     struct sysdev_class_attribute *a)
{
	return sysfs_create_file(&c->kset.kobj, &a->attr);
}
EXPORT_SYMBOL_GPL(sysdev_class_create_file);

void sysdev_class_remove_file(struct sysdev_class *c,
			      struct sysdev_class_attribute *a)
{
	sysfs_remove_file(&c->kset.kobj, &a->attr);
}
EXPORT_SYMBOL_GPL(sysdev_class_remove_file);

130
static struct kset *system_kset;
L
Linus Torvalds 已提交
131

132
int sysdev_class_register(struct sysdev_class *cls)
L
Linus Torvalds 已提交
133
{
134 135
	int retval;

136 137
	pr_debug("Registering sysdev class '%s'\n", cls->name);

L
Linus Torvalds 已提交
138
	INIT_LIST_HEAD(&cls->drivers);
139
	memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
140
	cls->kset.kobj.parent = &system_kset->kobj;
141
	cls->kset.kobj.ktype = &ktype_sysdev_class;
142
	cls->kset.kobj.kset = system_kset;
143

144
	retval = kobject_set_name(&cls->kset.kobj, "%s", cls->name);
145 146 147
	if (retval)
		return retval;

L
Linus Torvalds 已提交
148 149 150
	return kset_register(&cls->kset);
}

151
void sysdev_class_unregister(struct sysdev_class *cls)
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160
{
	pr_debug("Unregistering sysdev class '%s'\n",
		 kobject_name(&cls->kset.kobj));
	kset_unregister(&cls->kset);
}

EXPORT_SYMBOL_GPL(sysdev_class_register);
EXPORT_SYMBOL_GPL(sysdev_class_unregister);

161
static DEFINE_MUTEX(sysdev_drivers_lock);
L
Linus Torvalds 已提交
162 163 164

/**
 *	sysdev_driver_register - Register auxillary driver
165
 *	@cls:	Device class driver belongs to.
L
Linus Torvalds 已提交
166 167
 *	@drv:	Driver.
 *
168
 *	@drv is inserted into @cls->drivers to be
L
Linus Torvalds 已提交
169 170 171 172
 *	called on each operation on devices of that class. The refcount
 *	of @cls is incremented.
 */

173
int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
L
Linus Torvalds 已提交
174
{
175 176
	int err = 0;

177
	if (!cls) {
A
Arjan van de Ven 已提交
178
		WARN(1, KERN_WARNING "sysdev: invalid class passed to "
179 180 181 182 183
			"sysdev_driver_register!\n");
		return -EINVAL;
	}

	/* Check whether this driver has already been added to a class. */
A
Arjan van de Ven 已提交
184 185
	if (drv->entry.next && !list_empty(&drv->entry))
		WARN(1, KERN_WARNING "sysdev: class %s: driver (%p) has already"
186 187 188
			" been registered to a class, something is wrong, but "
			"will forge on!\n", cls->name, drv);

189
	mutex_lock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198
	if (cls && kset_get(&cls->kset)) {
		list_add_tail(&drv->entry, &cls->drivers);

		/* If devices of this class already exist, tell the driver */
		if (drv->add) {
			struct sys_device *dev;
			list_for_each_entry(dev, &cls->kset.list, kobj.entry)
				drv->add(dev);
		}
199 200
	} else {
		err = -EINVAL;
A
Arjan van de Ven 已提交
201
		WARN(1, KERN_ERR "%s: invalid device class\n", __func__);
202
	}
203
	mutex_unlock(&sysdev_drivers_lock);
204
	return err;
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212
}


/**
 *	sysdev_driver_unregister - Remove an auxillary driver.
 *	@cls:	Class driver belongs to.
 *	@drv:	Driver.
 */
213 214
void sysdev_driver_unregister(struct sysdev_class *cls,
			      struct sysdev_driver *drv)
L
Linus Torvalds 已提交
215
{
216
	mutex_lock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225
	list_del_init(&drv->entry);
	if (cls) {
		if (drv->remove) {
			struct sys_device *dev;
			list_for_each_entry(dev, &cls->kset.list, kobj.entry)
				drv->remove(dev);
		}
		kset_put(&cls->kset);
	}
226
	mutex_unlock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237 238
}

EXPORT_SYMBOL_GPL(sysdev_driver_register);
EXPORT_SYMBOL_GPL(sysdev_driver_unregister);



/**
 *	sysdev_register - add a system device to the tree
 *	@sysdev:	device in question
 *
 */
239
int sysdev_register(struct sys_device *sysdev)
L
Linus Torvalds 已提交
240 241
{
	int error;
242
	struct sysdev_class *cls = sysdev->cls;
L
Linus Torvalds 已提交
243 244 245 246

	if (!cls)
		return -EINVAL;

247 248
	pr_debug("Registering sys device of class '%s'\n",
		 kobject_name(&cls->kset.kobj));
249

250 251 252
	/* initialize the kobject to 0, in case it had previously been used */
	memset(&sysdev->kobj, 0x00, sizeof(struct kobject));

L
Linus Torvalds 已提交
253 254 255 256
	/* Make sure the kset is set */
	sysdev->kobj.kset = &cls->kset;

	/* Register the object */
257 258 259
	error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
				     "%s%d", kobject_name(&cls->kset.kobj),
				     sysdev->id);
L
Linus Torvalds 已提交
260 261

	if (!error) {
262
		struct sysdev_driver *drv;
L
Linus Torvalds 已提交
263

264 265 266
		pr_debug("Registering sys device '%s'\n",
			 kobject_name(&sysdev->kobj));

267
		mutex_lock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276
		/* Generic notification is implicit, because it's that
		 * code that should have called us.
		 */

		/* Notify class auxillary drivers */
		list_for_each_entry(drv, &cls->drivers, entry) {
			if (drv->add)
				drv->add(sysdev);
		}
277
		mutex_unlock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
278
	}
279

280
	kobject_uevent(&sysdev->kobj, KOBJ_ADD);
L
Linus Torvalds 已提交
281 282 283
	return error;
}

284
void sysdev_unregister(struct sys_device *sysdev)
L
Linus Torvalds 已提交
285
{
286
	struct sysdev_driver *drv;
L
Linus Torvalds 已提交
287

288
	mutex_lock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
289 290 291 292
	list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
		if (drv->remove)
			drv->remove(sysdev);
	}
293
	mutex_unlock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
294

295
	kobject_put(&sysdev->kobj);
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304
}



/**
 *	sysdev_shutdown - Shut down all system devices.
 *
 *	Loop over each class of system devices, and the devices in each
 *	of those classes. For each device, we call the shutdown method for
305
 *	each driver registered for the device - the auxillaries,
L
Linus Torvalds 已提交
306 307 308
 *	and the class driver.
 *
 *	Note: The list is iterated in reverse order, so that we shut down
309
 *	child devices before we shut down their parents. The list ordering
L
Linus Torvalds 已提交
310 311 312 313 314
 *	is guaranteed by virtue of the fact that child devices are registered
 *	after their parents.
 */
void sysdev_shutdown(void)
{
315
	struct sysdev_class *cls;
L
Linus Torvalds 已提交
316 317 318

	pr_debug("Shutting Down System Devices\n");

319
	mutex_lock(&sysdev_drivers_lock);
320
	list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
321
		struct sys_device *sysdev;
L
Linus Torvalds 已提交
322 323 324 325 326

		pr_debug("Shutting down type '%s':\n",
			 kobject_name(&cls->kset.kobj));

		list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
327
			struct sysdev_driver *drv;
L
Linus Torvalds 已提交
328 329
			pr_debug(" %s\n", kobject_name(&sysdev->kobj));

330
			/* Call auxillary drivers first */
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340
			list_for_each_entry(drv, &cls->drivers, entry) {
				if (drv->shutdown)
					drv->shutdown(sysdev);
			}

			/* Now call the generic one */
			if (cls->shutdown)
				cls->shutdown(sysdev);
		}
	}
341
	mutex_unlock(&sysdev_drivers_lock);
L
Linus Torvalds 已提交
342 343
}

344 345 346 347 348 349 350 351
static void __sysdev_resume(struct sys_device *dev)
{
	struct sysdev_class *cls = dev->cls;
	struct sysdev_driver *drv;

	/* First, call the class-specific one */
	if (cls->resume)
		cls->resume(dev);
352 353
	WARN_ONCE(!irqs_disabled(),
		"Interrupts enabled after %pF\n", cls->resume);
354 355 356 357 358

	/* Call auxillary drivers next. */
	list_for_each_entry(drv, &cls->drivers, entry) {
		if (drv->resume)
			drv->resume(dev);
359 360
		WARN_ONCE(!irqs_disabled(),
			"Interrupts enabled after %pF\n", drv->resume);
361 362
	}
}
L
Linus Torvalds 已提交
363 364 365 366 367

/**
 *	sysdev_suspend - Suspend all system devices.
 *	@state:		Power state to enter.
 *
368
 *	We perform an almost identical operation as sysdev_shutdown()
L
Linus Torvalds 已提交
369 370 371 372 373 374 375
 *	above, though calling ->suspend() instead. Interrupts are disabled
 *	when this called. Devices are responsible for both saving state and
 *	quiescing or powering down the device.
 *
 *	This is only called by the device PM core, so we let them handle
 *	all synchronization.
 */
376
int sysdev_suspend(pm_message_t state)
L
Linus Torvalds 已提交
377
{
378
	struct sysdev_class *cls;
379 380 381
	struct sys_device *sysdev, *err_dev;
	struct sysdev_driver *drv, *err_drv;
	int ret;
L
Linus Torvalds 已提交
382

383 384 385 386 387 388 389
	pr_debug("Checking wake-up interrupts\n");

	/* Return error code if there are any wake-up interrupts pending */
	ret = check_wakeup_irqs();
	if (ret)
		return ret;

390 391 392
	WARN_ONCE(!irqs_disabled(),
		"Interrupts enabled while suspending system devices\n");

L
Linus Torvalds 已提交
393 394
	pr_debug("Suspending System Devices\n");

395
	list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
L
Linus Torvalds 已提交
396 397 398 399 400 401
		pr_debug("Suspending type '%s':\n",
			 kobject_name(&cls->kset.kobj));

		list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
			pr_debug(" %s\n", kobject_name(&sysdev->kobj));

402
			/* Call auxillary drivers first */
L
Linus Torvalds 已提交
403
			list_for_each_entry(drv, &cls->drivers, entry) {
404 405 406 407 408
				if (drv->suspend) {
					ret = drv->suspend(sysdev, state);
					if (ret)
						goto aux_driver;
				}
409 410 411
				WARN_ONCE(!irqs_disabled(),
					"Interrupts enabled after %pF\n",
					drv->suspend);
L
Linus Torvalds 已提交
412 413 414
			}

			/* Now call the generic one */
415 416 417 418
			if (cls->suspend) {
				ret = cls->suspend(sysdev, state);
				if (ret)
					goto cls_driver;
419 420 421
				WARN_ONCE(!irqs_disabled(),
					"Interrupts enabled after %pF\n",
					cls->suspend);
422
			}
L
Linus Torvalds 已提交
423 424 425
		}
	}
	return 0;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	/* resume current sysdev */
cls_driver:
	drv = NULL;
	printk(KERN_ERR "Class suspend failed for %s\n",
		kobject_name(&sysdev->kobj));

aux_driver:
	if (drv)
		printk(KERN_ERR "Class driver suspend failed for %s\n",
				kobject_name(&sysdev->kobj));
	list_for_each_entry(err_drv, &cls->drivers, entry) {
		if (err_drv == drv)
			break;
		if (err_drv->resume)
			err_drv->resume(sysdev);
	}

	/* resume other sysdevs in current class */
	list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
		if (err_dev == sysdev)
			break;
		pr_debug(" %s\n", kobject_name(&err_dev->kobj));
		__sysdev_resume(err_dev);
	}

	/* resume other classes */
452
	list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
453 454 455 456 457 458
		list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
			pr_debug(" %s\n", kobject_name(&err_dev->kobj));
			__sysdev_resume(err_dev);
		}
	}
	return ret;
L
Linus Torvalds 已提交
459
}
460
EXPORT_SYMBOL_GPL(sysdev_suspend);
L
Linus Torvalds 已提交
461 462 463 464

/**
 *	sysdev_resume - Bring system devices back to life.
 *
465
 *	Similar to sysdev_suspend(), but we iterate the list forwards
L
Linus Torvalds 已提交
466 467 468 469 470 471
 *	to guarantee that parent devices are resumed before their children.
 *
 *	Note: Interrupts are disabled when called.
 */
int sysdev_resume(void)
{
472
	struct sysdev_class *cls;
L
Linus Torvalds 已提交
473

474 475 476
	WARN_ONCE(!irqs_disabled(),
		"Interrupts enabled while resuming system devices\n");

L
Linus Torvalds 已提交
477 478
	pr_debug("Resuming System Devices\n");

479
	list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
480
		struct sys_device *sysdev;
L
Linus Torvalds 已提交
481 482 483 484 485 486 487

		pr_debug("Resuming type '%s':\n",
			 kobject_name(&cls->kset.kobj));

		list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
			pr_debug(" %s\n", kobject_name(&sysdev->kobj));

488
			__sysdev_resume(sysdev);
L
Linus Torvalds 已提交
489 490 491 492
		}
	}
	return 0;
}
493
EXPORT_SYMBOL_GPL(sysdev_resume);
L
Linus Torvalds 已提交
494 495 496

int __init system_bus_init(void)
{
497 498 499 500
	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
	if (!system_kset)
		return -ENOMEM;
	return 0;
L
Linus Torvalds 已提交
501 502 503 504
}

EXPORT_SYMBOL_GPL(sysdev_register);
EXPORT_SYMBOL_GPL(sysdev_unregister);
505 506 507 508 509 510 511 512 513 514 515 516 517

#define to_ext_attr(x) container_of(x, struct sysdev_ext_attribute, attr)

ssize_t sysdev_store_ulong(struct sys_device *sysdev,
			   struct sysdev_attribute *attr,
			   const char *buf, size_t size)
{
	struct sysdev_ext_attribute *ea = to_ext_attr(attr);
	char *end;
	unsigned long new = simple_strtoul(buf, &end, 0);
	if (end == buf)
		return -EINVAL;
	*(unsigned long *)(ea->var) = new;
518 519
	/* Always return full write size even if we didn't consume all */
	return size;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
}
EXPORT_SYMBOL_GPL(sysdev_store_ulong);

ssize_t sysdev_show_ulong(struct sys_device *sysdev,
			  struct sysdev_attribute *attr,
			  char *buf)
{
	struct sysdev_ext_attribute *ea = to_ext_attr(attr);
	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
}
EXPORT_SYMBOL_GPL(sysdev_show_ulong);

ssize_t sysdev_store_int(struct sys_device *sysdev,
			   struct sysdev_attribute *attr,
			   const char *buf, size_t size)
{
	struct sysdev_ext_attribute *ea = to_ext_attr(attr);
	char *end;
	long new = simple_strtol(buf, &end, 0);
	if (end == buf || new > INT_MAX || new < INT_MIN)
		return -EINVAL;
	*(int *)(ea->var) = new;
542 543
	/* Always return full write size even if we didn't consume all */
	return size;
544 545 546 547 548 549 550 551 552 553 554 555
}
EXPORT_SYMBOL_GPL(sysdev_store_int);

ssize_t sysdev_show_int(struct sys_device *sysdev,
			  struct sysdev_attribute *attr,
			  char *buf)
{
	struct sysdev_ext_attribute *ea = to_ext_attr(attr);
	return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
}
EXPORT_SYMBOL_GPL(sysdev_show_int);