xref: /linux/drivers/base/core.c (revision 7f3edee81fbd49114c28057512906f169caa0bed)
1 /*
2  * drivers/base/core.c - core driver model code (device registration, etc)
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2006 Novell, Inc.
8  *
9  * This file is released under the GPLv2
10  *
11  */
12 
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/genhd.h>
22 #include <asm/semaphore.h>
23 
24 #include "base.h"
25 #include "power/power.h"
26 
27 int (*platform_notify)(struct device *dev) = NULL;
28 int (*platform_notify_remove)(struct device *dev) = NULL;
29 
30 /*
31  * sysfs bindings for devices.
32  */
33 
34 /**
35  * dev_driver_string - Return a device's driver name, if at all possible
36  * @dev: struct device to get the name of
37  *
38  * Will return the device's driver's name if it is bound to a device.  If
39  * the device is not bound to a device, it will return the name of the bus
40  * it is attached to.  If it is not attached to a bus either, an empty
41  * string will be returned.
42  */
43 const char *dev_driver_string(struct device *dev)
44 {
45 	return dev->driver ? dev->driver->name :
46 			(dev->bus ? dev->bus->name :
47 			(dev->class ? dev->class->name : ""));
48 }
49 EXPORT_SYMBOL(dev_driver_string);
50 
51 #define to_dev(obj) container_of(obj, struct device, kobj)
52 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
53 
54 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
55 			     char *buf)
56 {
57 	struct device_attribute *dev_attr = to_dev_attr(attr);
58 	struct device *dev = to_dev(kobj);
59 	ssize_t ret = -EIO;
60 
61 	if (dev_attr->show)
62 		ret = dev_attr->show(dev, dev_attr, buf);
63 	return ret;
64 }
65 
66 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
67 			      const char *buf, size_t count)
68 {
69 	struct device_attribute *dev_attr = to_dev_attr(attr);
70 	struct device *dev = to_dev(kobj);
71 	ssize_t ret = -EIO;
72 
73 	if (dev_attr->store)
74 		ret = dev_attr->store(dev, dev_attr, buf, count);
75 	return ret;
76 }
77 
78 static struct sysfs_ops dev_sysfs_ops = {
79 	.show	= dev_attr_show,
80 	.store	= dev_attr_store,
81 };
82 
83 
84 /**
85  *	device_release - free device structure.
86  *	@kobj:	device's kobject.
87  *
88  *	This is called once the reference count for the object
89  *	reaches 0. We forward the call to the device's release
90  *	method, which should handle actually freeing the structure.
91  */
92 static void device_release(struct kobject *kobj)
93 {
94 	struct device *dev = to_dev(kobj);
95 
96 	if (dev->release)
97 		dev->release(dev);
98 	else if (dev->type && dev->type->release)
99 		dev->type->release(dev);
100 	else if (dev->class && dev->class->dev_release)
101 		dev->class->dev_release(dev);
102 	else {
103 		printk(KERN_ERR "Device '%s' does not have a release() "
104 			"function, it is broken and must be fixed.\n",
105 			dev->bus_id);
106 		WARN_ON(1);
107 	}
108 }
109 
110 static struct kobj_type device_ktype = {
111 	.release	= device_release,
112 	.sysfs_ops	= &dev_sysfs_ops,
113 };
114 
115 
116 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
117 {
118 	struct kobj_type *ktype = get_ktype(kobj);
119 
120 	if (ktype == &device_ktype) {
121 		struct device *dev = to_dev(kobj);
122 		if (dev->uevent_suppress)
123 			return 0;
124 		if (dev->bus)
125 			return 1;
126 		if (dev->class)
127 			return 1;
128 	}
129 	return 0;
130 }
131 
132 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
133 {
134 	struct device *dev = to_dev(kobj);
135 
136 	if (dev->bus)
137 		return dev->bus->name;
138 	if (dev->class)
139 		return dev->class->name;
140 	return NULL;
141 }
142 
143 static int dev_uevent(struct kset *kset, struct kobject *kobj,
144 		      struct kobj_uevent_env *env)
145 {
146 	struct device *dev = to_dev(kobj);
147 	int retval = 0;
148 
149 	/* add the major/minor if present */
150 	if (MAJOR(dev->devt)) {
151 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
152 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
153 	}
154 
155 	if (dev->type && dev->type->name)
156 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
157 
158 	if (dev->driver)
159 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
160 
161 #ifdef CONFIG_SYSFS_DEPRECATED
162 	if (dev->class) {
163 		struct device *parent = dev->parent;
164 
165 		/* find first bus device in parent chain */
166 		while (parent && !parent->bus)
167 			parent = parent->parent;
168 		if (parent && parent->bus) {
169 			const char *path;
170 
171 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
172 			if (path) {
173 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
174 				kfree(path);
175 			}
176 
177 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
178 
179 			if (parent->driver)
180 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
181 					       parent->driver->name);
182 		}
183 	} else if (dev->bus) {
184 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
185 
186 		if (dev->driver)
187 			add_uevent_var(env, "PHYSDEVDRIVER=%s",
188 				       dev->driver->name);
189 	}
190 #endif
191 
192 	/* have the bus specific function add its stuff */
193 	if (dev->bus && dev->bus->uevent) {
194 		retval = dev->bus->uevent(dev, env);
195 		if (retval)
196 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
197 				 dev->bus_id, __FUNCTION__, retval);
198 	}
199 
200 	/* have the class specific function add its stuff */
201 	if (dev->class && dev->class->dev_uevent) {
202 		retval = dev->class->dev_uevent(dev, env);
203 		if (retval)
204 			pr_debug("device: '%s': %s: class uevent() "
205 				 "returned %d\n", dev->bus_id,
206 				 __FUNCTION__, retval);
207 	}
208 
209 	/* have the device type specific fuction add its stuff */
210 	if (dev->type && dev->type->uevent) {
211 		retval = dev->type->uevent(dev, env);
212 		if (retval)
213 			pr_debug("device: '%s': %s: dev_type uevent() "
214 				 "returned %d\n", dev->bus_id,
215 				 __FUNCTION__, retval);
216 	}
217 
218 	return retval;
219 }
220 
221 static struct kset_uevent_ops device_uevent_ops = {
222 	.filter =	dev_uevent_filter,
223 	.name =		dev_uevent_name,
224 	.uevent =	dev_uevent,
225 };
226 
227 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
228 			   char *buf)
229 {
230 	struct kobject *top_kobj;
231 	struct kset *kset;
232 	struct kobj_uevent_env *env = NULL;
233 	int i;
234 	size_t count = 0;
235 	int retval;
236 
237 	/* search the kset, the device belongs to */
238 	top_kobj = &dev->kobj;
239 	while (!top_kobj->kset && top_kobj->parent)
240 		top_kobj = top_kobj->parent;
241 	if (!top_kobj->kset)
242 		goto out;
243 
244 	kset = top_kobj->kset;
245 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
246 		goto out;
247 
248 	/* respect filter */
249 	if (kset->uevent_ops && kset->uevent_ops->filter)
250 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
251 			goto out;
252 
253 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
254 	if (!env)
255 		return -ENOMEM;
256 
257 	/* let the kset specific function add its keys */
258 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
259 	if (retval)
260 		goto out;
261 
262 	/* copy keys to file */
263 	for (i = 0; i < env->envp_idx; i++)
264 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
265 out:
266 	kfree(env);
267 	return count;
268 }
269 
270 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
271 			    const char *buf, size_t count)
272 {
273 	enum kobject_action action;
274 
275 	if (kobject_action_type(buf, count, &action) == 0) {
276 		kobject_uevent(&dev->kobj, action);
277 		goto out;
278 	}
279 
280 	dev_err(dev, "uevent: unsupported action-string; this will "
281 		     "be ignored in a future kernel version\n");
282 	kobject_uevent(&dev->kobj, KOBJ_ADD);
283 out:
284 	return count;
285 }
286 
287 static struct device_attribute uevent_attr =
288 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
289 
290 static int device_add_attributes(struct device *dev,
291 				 struct device_attribute *attrs)
292 {
293 	int error = 0;
294 	int i;
295 
296 	if (attrs) {
297 		for (i = 0; attr_name(attrs[i]); i++) {
298 			error = device_create_file(dev, &attrs[i]);
299 			if (error)
300 				break;
301 		}
302 		if (error)
303 			while (--i >= 0)
304 				device_remove_file(dev, &attrs[i]);
305 	}
306 	return error;
307 }
308 
309 static void device_remove_attributes(struct device *dev,
310 				     struct device_attribute *attrs)
311 {
312 	int i;
313 
314 	if (attrs)
315 		for (i = 0; attr_name(attrs[i]); i++)
316 			device_remove_file(dev, &attrs[i]);
317 }
318 
319 static int device_add_groups(struct device *dev,
320 			     struct attribute_group **groups)
321 {
322 	int error = 0;
323 	int i;
324 
325 	if (groups) {
326 		for (i = 0; groups[i]; i++) {
327 			error = sysfs_create_group(&dev->kobj, groups[i]);
328 			if (error) {
329 				while (--i >= 0)
330 					sysfs_remove_group(&dev->kobj,
331 							   groups[i]);
332 				break;
333 			}
334 		}
335 	}
336 	return error;
337 }
338 
339 static void device_remove_groups(struct device *dev,
340 				 struct attribute_group **groups)
341 {
342 	int i;
343 
344 	if (groups)
345 		for (i = 0; groups[i]; i++)
346 			sysfs_remove_group(&dev->kobj, groups[i]);
347 }
348 
349 static int device_add_attrs(struct device *dev)
350 {
351 	struct class *class = dev->class;
352 	struct device_type *type = dev->type;
353 	int error;
354 
355 	if (class) {
356 		error = device_add_attributes(dev, class->dev_attrs);
357 		if (error)
358 			return error;
359 	}
360 
361 	if (type) {
362 		error = device_add_groups(dev, type->groups);
363 		if (error)
364 			goto err_remove_class_attrs;
365 	}
366 
367 	error = device_add_groups(dev, dev->groups);
368 	if (error)
369 		goto err_remove_type_groups;
370 
371 	return 0;
372 
373  err_remove_type_groups:
374 	if (type)
375 		device_remove_groups(dev, type->groups);
376  err_remove_class_attrs:
377 	if (class)
378 		device_remove_attributes(dev, class->dev_attrs);
379 
380 	return error;
381 }
382 
383 static void device_remove_attrs(struct device *dev)
384 {
385 	struct class *class = dev->class;
386 	struct device_type *type = dev->type;
387 
388 	device_remove_groups(dev, dev->groups);
389 
390 	if (type)
391 		device_remove_groups(dev, type->groups);
392 
393 	if (class)
394 		device_remove_attributes(dev, class->dev_attrs);
395 }
396 
397 
398 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
399 			char *buf)
400 {
401 	return print_dev_t(buf, dev->devt);
402 }
403 
404 static struct device_attribute devt_attr =
405 	__ATTR(dev, S_IRUGO, show_dev, NULL);
406 
407 /* kset to create /sys/devices/  */
408 struct kset *devices_kset;
409 
410 /**
411  * device_create_file - create sysfs attribute file for device.
412  * @dev: device.
413  * @attr: device attribute descriptor.
414  */
415 int device_create_file(struct device *dev, struct device_attribute *attr)
416 {
417 	int error = 0;
418 	if (get_device(dev)) {
419 		error = sysfs_create_file(&dev->kobj, &attr->attr);
420 		put_device(dev);
421 	}
422 	return error;
423 }
424 
425 /**
426  * device_remove_file - remove sysfs attribute file.
427  * @dev: device.
428  * @attr: device attribute descriptor.
429  */
430 void device_remove_file(struct device *dev, struct device_attribute *attr)
431 {
432 	if (get_device(dev)) {
433 		sysfs_remove_file(&dev->kobj, &attr->attr);
434 		put_device(dev);
435 	}
436 }
437 
438 /**
439  * device_create_bin_file - create sysfs binary attribute file for device.
440  * @dev: device.
441  * @attr: device binary attribute descriptor.
442  */
443 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
444 {
445 	int error = -EINVAL;
446 	if (dev)
447 		error = sysfs_create_bin_file(&dev->kobj, attr);
448 	return error;
449 }
450 EXPORT_SYMBOL_GPL(device_create_bin_file);
451 
452 /**
453  * device_remove_bin_file - remove sysfs binary attribute file
454  * @dev: device.
455  * @attr: device binary attribute descriptor.
456  */
457 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
458 {
459 	if (dev)
460 		sysfs_remove_bin_file(&dev->kobj, attr);
461 }
462 EXPORT_SYMBOL_GPL(device_remove_bin_file);
463 
464 /**
465  * device_schedule_callback_owner - helper to schedule a callback for a device
466  * @dev: device.
467  * @func: callback function to invoke later.
468  * @owner: module owning the callback routine
469  *
470  * Attribute methods must not unregister themselves or their parent device
471  * (which would amount to the same thing).  Attempts to do so will deadlock,
472  * since unregistration is mutually exclusive with driver callbacks.
473  *
474  * Instead methods can call this routine, which will attempt to allocate
475  * and schedule a workqueue request to call back @func with @dev as its
476  * argument in the workqueue's process context.  @dev will be pinned until
477  * @func returns.
478  *
479  * This routine is usually called via the inline device_schedule_callback(),
480  * which automatically sets @owner to THIS_MODULE.
481  *
482  * Returns 0 if the request was submitted, -ENOMEM if storage could not
483  * be allocated, -ENODEV if a reference to @owner isn't available.
484  *
485  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
486  * underlying sysfs routine (since it is intended for use by attribute
487  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
488  */
489 int device_schedule_callback_owner(struct device *dev,
490 		void (*func)(struct device *), struct module *owner)
491 {
492 	return sysfs_schedule_callback(&dev->kobj,
493 			(void (*)(void *)) func, dev, owner);
494 }
495 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
496 
497 static void klist_children_get(struct klist_node *n)
498 {
499 	struct device *dev = container_of(n, struct device, knode_parent);
500 
501 	get_device(dev);
502 }
503 
504 static void klist_children_put(struct klist_node *n)
505 {
506 	struct device *dev = container_of(n, struct device, knode_parent);
507 
508 	put_device(dev);
509 }
510 
511 /**
512  * device_initialize - init device structure.
513  * @dev: device.
514  *
515  * This prepares the device for use by other layers,
516  * including adding it to the device hierarchy.
517  * It is the first half of device_register(), if called by
518  * that, though it can also be called separately, so one
519  * may use @dev's fields (e.g. the refcount).
520  */
521 void device_initialize(struct device *dev)
522 {
523 	dev->kobj.kset = devices_kset;
524 	kobject_init(&dev->kobj, &device_ktype);
525 	klist_init(&dev->klist_children, klist_children_get,
526 		   klist_children_put);
527 	INIT_LIST_HEAD(&dev->dma_pools);
528 	INIT_LIST_HEAD(&dev->node);
529 	init_MUTEX(&dev->sem);
530 	spin_lock_init(&dev->devres_lock);
531 	INIT_LIST_HEAD(&dev->devres_head);
532 	device_init_wakeup(dev, 0);
533 	set_dev_node(dev, -1);
534 }
535 
536 #ifdef CONFIG_SYSFS_DEPRECATED
537 static struct kobject *get_device_parent(struct device *dev,
538 					 struct device *parent)
539 {
540 	/* class devices without a parent live in /sys/class/<classname>/ */
541 	if (dev->class && (!parent || parent->class != dev->class))
542 		return &dev->class->subsys.kobj;
543 	/* all other devices keep their parent */
544 	else if (parent)
545 		return &parent->kobj;
546 
547 	return NULL;
548 }
549 
550 static inline void cleanup_device_parent(struct device *dev) {}
551 static inline void cleanup_glue_dir(struct device *dev,
552 				    struct kobject *glue_dir) {}
553 #else
554 static struct kobject *virtual_device_parent(struct device *dev)
555 {
556 	static struct kobject *virtual_dir = NULL;
557 
558 	if (!virtual_dir)
559 		virtual_dir = kobject_create_and_add("virtual",
560 						     &devices_kset->kobj);
561 
562 	return virtual_dir;
563 }
564 
565 static struct kobject *get_device_parent(struct device *dev,
566 					 struct device *parent)
567 {
568 	int retval;
569 
570 	if (dev->class) {
571 		struct kobject *kobj = NULL;
572 		struct kobject *parent_kobj;
573 		struct kobject *k;
574 
575 		/*
576 		 * If we have no parent, we live in "virtual".
577 		 * Class-devices with a non class-device as parent, live
578 		 * in a "glue" directory to prevent namespace collisions.
579 		 */
580 		if (parent == NULL)
581 			parent_kobj = virtual_device_parent(dev);
582 		else if (parent->class)
583 			return &parent->kobj;
584 		else
585 			parent_kobj = &parent->kobj;
586 
587 		/* find our class-directory at the parent and reference it */
588 		spin_lock(&dev->class->class_dirs.list_lock);
589 		list_for_each_entry(k, &dev->class->class_dirs.list, entry)
590 			if (k->parent == parent_kobj) {
591 				kobj = kobject_get(k);
592 				break;
593 			}
594 		spin_unlock(&dev->class->class_dirs.list_lock);
595 		if (kobj)
596 			return kobj;
597 
598 		/* or create a new class-directory at the parent device */
599 		k = kobject_create();
600 		if (!k)
601 			return NULL;
602 		k->kset = &dev->class->class_dirs;
603 		retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
604 		if (retval < 0) {
605 			kobject_put(k);
606 			return NULL;
607 		}
608 		/* do not emit an uevent for this simple "glue" directory */
609 		return k;
610 	}
611 
612 	if (parent)
613 		return &parent->kobj;
614 	return NULL;
615 }
616 
617 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
618 {
619 	/* see if we live in a "glue" directory */
620 	if (!dev->class || glue_dir->kset != &dev->class->class_dirs)
621 		return;
622 
623 	kobject_put(glue_dir);
624 }
625 
626 static void cleanup_device_parent(struct device *dev)
627 {
628 	cleanup_glue_dir(dev, dev->kobj.parent);
629 }
630 #endif
631 
632 static void setup_parent(struct device *dev, struct device *parent)
633 {
634 	struct kobject *kobj;
635 	kobj = get_device_parent(dev, parent);
636 	if (kobj)
637 		dev->kobj.parent = kobj;
638 }
639 
640 static int device_add_class_symlinks(struct device *dev)
641 {
642 	int error;
643 
644 	if (!dev->class)
645 		return 0;
646 
647 	error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
648 				  "subsystem");
649 	if (error)
650 		goto out;
651 
652 #ifdef CONFIG_SYSFS_DEPRECATED
653 	/* stacked class devices need a symlink in the class directory */
654 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
655 	    dev->type != &part_type) {
656 		error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
657 					  dev->bus_id);
658 		if (error)
659 			goto out_subsys;
660 	}
661 
662 	if (dev->parent && dev->type != &part_type) {
663 		struct device *parent = dev->parent;
664 		char *class_name;
665 
666 		/*
667 		 * stacked class devices have the 'device' link
668 		 * pointing to the bus device instead of the parent
669 		 */
670 		while (parent->class && !parent->bus && parent->parent)
671 			parent = parent->parent;
672 
673 		error = sysfs_create_link(&dev->kobj,
674 					  &parent->kobj,
675 					  "device");
676 		if (error)
677 			goto out_busid;
678 
679 		class_name = make_class_name(dev->class->name,
680 						&dev->kobj);
681 		if (class_name)
682 			error = sysfs_create_link(&dev->parent->kobj,
683 						&dev->kobj, class_name);
684 		kfree(class_name);
685 		if (error)
686 			goto out_device;
687 	}
688 	return 0;
689 
690 out_device:
691 	if (dev->parent && dev->type != &part_type)
692 		sysfs_remove_link(&dev->kobj, "device");
693 out_busid:
694 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
695 	    dev->type != &part_type)
696 		sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
697 #else
698 	/* link in the class directory pointing to the device */
699 	error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
700 				  dev->bus_id);
701 	if (error)
702 		goto out_subsys;
703 
704 	if (dev->parent && dev->type != &part_type) {
705 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
706 					  "device");
707 		if (error)
708 			goto out_busid;
709 	}
710 	return 0;
711 
712 out_busid:
713 	sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
714 #endif
715 
716 out_subsys:
717 	sysfs_remove_link(&dev->kobj, "subsystem");
718 out:
719 	return error;
720 }
721 
722 static void device_remove_class_symlinks(struct device *dev)
723 {
724 	if (!dev->class)
725 		return;
726 
727 #ifdef CONFIG_SYSFS_DEPRECATED
728 	if (dev->parent && dev->type != &part_type) {
729 		char *class_name;
730 
731 		class_name = make_class_name(dev->class->name, &dev->kobj);
732 		if (class_name) {
733 			sysfs_remove_link(&dev->parent->kobj, class_name);
734 			kfree(class_name);
735 		}
736 		sysfs_remove_link(&dev->kobj, "device");
737 	}
738 
739 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
740 	    dev->type != &part_type)
741 		sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
742 #else
743 	if (dev->parent && dev->type != &part_type)
744 		sysfs_remove_link(&dev->kobj, "device");
745 
746 	sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
747 #endif
748 
749 	sysfs_remove_link(&dev->kobj, "subsystem");
750 }
751 
752 /**
753  * device_add - add device to device hierarchy.
754  * @dev: device.
755  *
756  * This is part 2 of device_register(), though may be called
757  * separately _iff_ device_initialize() has been called separately.
758  *
759  * This adds it to the kobject hierarchy via kobject_add(), adds it
760  * to the global and sibling lists for the device, then
761  * adds it to the other relevant subsystems of the driver model.
762  */
763 int device_add(struct device *dev)
764 {
765 	struct device *parent = NULL;
766 	struct class_interface *class_intf;
767 	int error;
768 
769 	error = pm_sleep_lock();
770 	if (error) {
771 		dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
772 		dump_stack();
773 		return error;
774 	}
775 
776 	dev = get_device(dev);
777 	if (!dev || !strlen(dev->bus_id)) {
778 		error = -EINVAL;
779 		goto Error;
780 	}
781 
782 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
783 
784 	parent = get_device(dev->parent);
785 	setup_parent(dev, parent);
786 
787 	/* first, register with generic layer. */
788 	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
789 	if (error)
790 		goto Error;
791 
792 	/* notify platform of device entry */
793 	if (platform_notify)
794 		platform_notify(dev);
795 
796 	/* notify clients of device entry (new way) */
797 	if (dev->bus)
798 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
799 					     BUS_NOTIFY_ADD_DEVICE, dev);
800 
801 	error = device_create_file(dev, &uevent_attr);
802 	if (error)
803 		goto attrError;
804 
805 	if (MAJOR(dev->devt)) {
806 		error = device_create_file(dev, &devt_attr);
807 		if (error)
808 			goto ueventattrError;
809 	}
810 
811 	error = device_add_class_symlinks(dev);
812 	if (error)
813 		goto SymlinkError;
814 	error = device_add_attrs(dev);
815 	if (error)
816 		goto AttrsError;
817 	error = dpm_sysfs_add(dev);
818 	if (error)
819 		goto PMError;
820 	device_pm_add(dev);
821 	error = bus_add_device(dev);
822 	if (error)
823 		goto BusError;
824 	kobject_uevent(&dev->kobj, KOBJ_ADD);
825 	bus_attach_device(dev);
826 	if (parent)
827 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
828 
829 	if (dev->class) {
830 		down(&dev->class->sem);
831 		/* tie the class to the device */
832 		list_add_tail(&dev->node, &dev->class->devices);
833 
834 		/* notify any interfaces that the device is here */
835 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
836 			if (class_intf->add_dev)
837 				class_intf->add_dev(dev, class_intf);
838 		up(&dev->class->sem);
839 	}
840  Done:
841 	put_device(dev);
842 	pm_sleep_unlock();
843 	return error;
844  BusError:
845 	device_pm_remove(dev);
846 	dpm_sysfs_remove(dev);
847  PMError:
848 	if (dev->bus)
849 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
850 					     BUS_NOTIFY_DEL_DEVICE, dev);
851 	device_remove_attrs(dev);
852  AttrsError:
853 	device_remove_class_symlinks(dev);
854  SymlinkError:
855 	if (MAJOR(dev->devt))
856 		device_remove_file(dev, &devt_attr);
857  ueventattrError:
858 	device_remove_file(dev, &uevent_attr);
859  attrError:
860 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
861 	kobject_del(&dev->kobj);
862  Error:
863 	cleanup_device_parent(dev);
864 	if (parent)
865 		put_device(parent);
866 	goto Done;
867 }
868 
869 /**
870  * device_register - register a device with the system.
871  * @dev: pointer to the device structure
872  *
873  * This happens in two clean steps - initialize the device
874  * and add it to the system. The two steps can be called
875  * separately, but this is the easiest and most common.
876  * I.e. you should only call the two helpers separately if
877  * have a clearly defined need to use and refcount the device
878  * before it is added to the hierarchy.
879  */
880 int device_register(struct device *dev)
881 {
882 	device_initialize(dev);
883 	return device_add(dev);
884 }
885 
886 /**
887  * get_device - increment reference count for device.
888  * @dev: device.
889  *
890  * This simply forwards the call to kobject_get(), though
891  * we do take care to provide for the case that we get a NULL
892  * pointer passed in.
893  */
894 struct device *get_device(struct device *dev)
895 {
896 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
897 }
898 
899 /**
900  * put_device - decrement reference count.
901  * @dev: device in question.
902  */
903 void put_device(struct device *dev)
904 {
905 	/* might_sleep(); */
906 	if (dev)
907 		kobject_put(&dev->kobj);
908 }
909 
910 /**
911  * device_del - delete device from system.
912  * @dev: device.
913  *
914  * This is the first part of the device unregistration
915  * sequence. This removes the device from the lists we control
916  * from here, has it removed from the other driver model
917  * subsystems it was added to in device_add(), and removes it
918  * from the kobject hierarchy.
919  *
920  * NOTE: this should be called manually _iff_ device_add() was
921  * also called manually.
922  */
923 void device_del(struct device *dev)
924 {
925 	struct device *parent = dev->parent;
926 	struct class_interface *class_intf;
927 
928 	device_pm_remove(dev);
929 	if (parent)
930 		klist_del(&dev->knode_parent);
931 	if (MAJOR(dev->devt))
932 		device_remove_file(dev, &devt_attr);
933 	if (dev->class) {
934 		device_remove_class_symlinks(dev);
935 
936 		down(&dev->class->sem);
937 		/* notify any interfaces that the device is now gone */
938 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
939 			if (class_intf->remove_dev)
940 				class_intf->remove_dev(dev, class_intf);
941 		/* remove the device from the class list */
942 		list_del_init(&dev->node);
943 		up(&dev->class->sem);
944 	}
945 	device_remove_file(dev, &uevent_attr);
946 	device_remove_attrs(dev);
947 	bus_remove_device(dev);
948 
949 	/*
950 	 * Some platform devices are driven without driver attached
951 	 * and managed resources may have been acquired.  Make sure
952 	 * all resources are released.
953 	 */
954 	devres_release_all(dev);
955 
956 	/* Notify the platform of the removal, in case they
957 	 * need to do anything...
958 	 */
959 	if (platform_notify_remove)
960 		platform_notify_remove(dev);
961 	if (dev->bus)
962 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
963 					     BUS_NOTIFY_DEL_DEVICE, dev);
964 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
965 	cleanup_device_parent(dev);
966 	kobject_del(&dev->kobj);
967 	put_device(parent);
968 }
969 
970 /**
971  * device_unregister - unregister device from system.
972  * @dev: device going away.
973  *
974  * We do this in two parts, like we do device_register(). First,
975  * we remove it from all the subsystems with device_del(), then
976  * we decrement the reference count via put_device(). If that
977  * is the final reference count, the device will be cleaned up
978  * via device_release() above. Otherwise, the structure will
979  * stick around until the final reference to the device is dropped.
980  */
981 void device_unregister(struct device *dev)
982 {
983 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
984 	device_del(dev);
985 	put_device(dev);
986 }
987 
988 static struct device *next_device(struct klist_iter *i)
989 {
990 	struct klist_node *n = klist_next(i);
991 	return n ? container_of(n, struct device, knode_parent) : NULL;
992 }
993 
994 /**
995  * device_for_each_child - device child iterator.
996  * @parent: parent struct device.
997  * @data: data for the callback.
998  * @fn: function to be called for each device.
999  *
1000  * Iterate over @parent's child devices, and call @fn for each,
1001  * passing it @data.
1002  *
1003  * We check the return of @fn each time. If it returns anything
1004  * other than 0, we break out and return that value.
1005  */
1006 int device_for_each_child(struct device *parent, void *data,
1007 			  int (*fn)(struct device *dev, void *data))
1008 {
1009 	struct klist_iter i;
1010 	struct device *child;
1011 	int error = 0;
1012 
1013 	klist_iter_init(&parent->klist_children, &i);
1014 	while ((child = next_device(&i)) && !error)
1015 		error = fn(child, data);
1016 	klist_iter_exit(&i);
1017 	return error;
1018 }
1019 
1020 /**
1021  * device_find_child - device iterator for locating a particular device.
1022  * @parent: parent struct device
1023  * @data: Data to pass to match function
1024  * @match: Callback function to check device
1025  *
1026  * This is similar to the device_for_each_child() function above, but it
1027  * returns a reference to a device that is 'found' for later use, as
1028  * determined by the @match callback.
1029  *
1030  * The callback should return 0 if the device doesn't match and non-zero
1031  * if it does.  If the callback returns non-zero and a reference to the
1032  * current device can be obtained, this function will return to the caller
1033  * and not iterate over any more devices.
1034  */
1035 struct device *device_find_child(struct device *parent, void *data,
1036 				 int (*match)(struct device *dev, void *data))
1037 {
1038 	struct klist_iter i;
1039 	struct device *child;
1040 
1041 	if (!parent)
1042 		return NULL;
1043 
1044 	klist_iter_init(&parent->klist_children, &i);
1045 	while ((child = next_device(&i)))
1046 		if (match(child, data) && get_device(child))
1047 			break;
1048 	klist_iter_exit(&i);
1049 	return child;
1050 }
1051 
1052 int __init devices_init(void)
1053 {
1054 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1055 	if (!devices_kset)
1056 		return -ENOMEM;
1057 	return 0;
1058 }
1059 
1060 EXPORT_SYMBOL_GPL(device_for_each_child);
1061 EXPORT_SYMBOL_GPL(device_find_child);
1062 
1063 EXPORT_SYMBOL_GPL(device_initialize);
1064 EXPORT_SYMBOL_GPL(device_add);
1065 EXPORT_SYMBOL_GPL(device_register);
1066 
1067 EXPORT_SYMBOL_GPL(device_del);
1068 EXPORT_SYMBOL_GPL(device_unregister);
1069 EXPORT_SYMBOL_GPL(get_device);
1070 EXPORT_SYMBOL_GPL(put_device);
1071 
1072 EXPORT_SYMBOL_GPL(device_create_file);
1073 EXPORT_SYMBOL_GPL(device_remove_file);
1074 
1075 
1076 static void device_create_release(struct device *dev)
1077 {
1078 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
1079 	kfree(dev);
1080 }
1081 
1082 /**
1083  * device_create - creates a device and registers it with sysfs
1084  * @class: pointer to the struct class that this device should be registered to
1085  * @parent: pointer to the parent struct device of this new device, if any
1086  * @devt: the dev_t for the char device to be added
1087  * @fmt: string for the device's name
1088  *
1089  * This function can be used by char device classes.  A struct device
1090  * will be created in sysfs, registered to the specified class.
1091  *
1092  * A "dev" file will be created, showing the dev_t for the device, if
1093  * the dev_t is not 0,0.
1094  * If a pointer to a parent struct device is passed in, the newly created
1095  * struct device will be a child of that device in sysfs.
1096  * The pointer to the struct device will be returned from the call.
1097  * Any further sysfs files that might be required can be created using this
1098  * pointer.
1099  *
1100  * Note: the struct class passed to this function must have previously
1101  * been created with a call to class_create().
1102  */
1103 struct device *device_create(struct class *class, struct device *parent,
1104 			     dev_t devt, const char *fmt, ...)
1105 {
1106 	va_list args;
1107 	struct device *dev = NULL;
1108 	int retval = -ENODEV;
1109 
1110 	if (class == NULL || IS_ERR(class))
1111 		goto error;
1112 
1113 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1114 	if (!dev) {
1115 		retval = -ENOMEM;
1116 		goto error;
1117 	}
1118 
1119 	dev->devt = devt;
1120 	dev->class = class;
1121 	dev->parent = parent;
1122 	dev->release = device_create_release;
1123 
1124 	va_start(args, fmt);
1125 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1126 	va_end(args);
1127 	retval = device_register(dev);
1128 	if (retval)
1129 		goto error;
1130 
1131 	return dev;
1132 
1133 error:
1134 	kfree(dev);
1135 	return ERR_PTR(retval);
1136 }
1137 EXPORT_SYMBOL_GPL(device_create);
1138 
1139 /**
1140  * find_device - finds a device that was created with device_create()
1141  * @class: pointer to the struct class that this device was registered with
1142  * @devt: the dev_t of the device that was previously registered
1143  */
1144 static struct device *find_device(struct class *class, dev_t devt)
1145 {
1146 	struct device *dev = NULL;
1147 	struct device *dev_tmp;
1148 
1149 	down(&class->sem);
1150 	list_for_each_entry(dev_tmp, &class->devices, node) {
1151 		if (dev_tmp->devt == devt) {
1152 			dev = dev_tmp;
1153 			break;
1154 		}
1155 	}
1156 	up(&class->sem);
1157 	return dev;
1158 }
1159 
1160 /**
1161  * device_destroy - removes a device that was created with device_create()
1162  * @class: pointer to the struct class that this device was registered with
1163  * @devt: the dev_t of the device that was previously registered
1164  *
1165  * This call unregisters and cleans up a device that was created with a
1166  * call to device_create().
1167  */
1168 void device_destroy(struct class *class, dev_t devt)
1169 {
1170 	struct device *dev;
1171 
1172 	dev = find_device(class, devt);
1173 	if (dev)
1174 		device_unregister(dev);
1175 }
1176 EXPORT_SYMBOL_GPL(device_destroy);
1177 
1178 #ifdef CONFIG_PM_SLEEP
1179 /**
1180  * destroy_suspended_device - asks the PM core to remove a suspended device
1181  * @class: pointer to the struct class that this device was registered with
1182  * @devt: the dev_t of the device that was previously registered
1183  *
1184  * This call notifies the PM core of the necessity to unregister a suspended
1185  * device created with a call to device_create() (devices cannot be
1186  * unregistered directly while suspended, since the PM core holds their
1187  * semaphores at that time).
1188  *
1189  * It can only be called within the scope of a system sleep transition.  In
1190  * practice this means it has to be directly or indirectly invoked either by
1191  * a suspend or resume method, or by the PM core (e.g. via
1192  * disable_nonboot_cpus() or enable_nonboot_cpus()).
1193  */
1194 void destroy_suspended_device(struct class *class, dev_t devt)
1195 {
1196 	struct device *dev;
1197 
1198 	dev = find_device(class, devt);
1199 	if (dev)
1200 		device_pm_schedule_removal(dev);
1201 }
1202 EXPORT_SYMBOL_GPL(destroy_suspended_device);
1203 #endif /* CONFIG_PM_SLEEP */
1204 
1205 /**
1206  * device_rename - renames a device
1207  * @dev: the pointer to the struct device to be renamed
1208  * @new_name: the new name of the device
1209  */
1210 int device_rename(struct device *dev, char *new_name)
1211 {
1212 	char *old_class_name = NULL;
1213 	char *new_class_name = NULL;
1214 	char *old_device_name = NULL;
1215 	int error;
1216 
1217 	dev = get_device(dev);
1218 	if (!dev)
1219 		return -EINVAL;
1220 
1221 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
1222 		 __FUNCTION__, new_name);
1223 
1224 #ifdef CONFIG_SYSFS_DEPRECATED
1225 	if ((dev->class) && (dev->parent))
1226 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
1227 #endif
1228 
1229 	old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1230 	if (!old_device_name) {
1231 		error = -ENOMEM;
1232 		goto out;
1233 	}
1234 	strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
1235 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1236 
1237 	error = kobject_rename(&dev->kobj, new_name);
1238 	if (error) {
1239 		strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
1240 		goto out;
1241 	}
1242 
1243 #ifdef CONFIG_SYSFS_DEPRECATED
1244 	if (old_class_name) {
1245 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1246 		if (new_class_name) {
1247 			error = sysfs_create_link(&dev->parent->kobj,
1248 						  &dev->kobj, new_class_name);
1249 			if (error)
1250 				goto out;
1251 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1252 		}
1253 	}
1254 #else
1255 	if (dev->class) {
1256 		sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1257 		error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1258 					  dev->bus_id);
1259 		if (error) {
1260 			dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
1261 				__FUNCTION__, error);
1262 		}
1263 	}
1264 #endif
1265 
1266 out:
1267 	put_device(dev);
1268 
1269 	kfree(new_class_name);
1270 	kfree(old_class_name);
1271 	kfree(old_device_name);
1272 
1273 	return error;
1274 }
1275 EXPORT_SYMBOL_GPL(device_rename);
1276 
1277 static int device_move_class_links(struct device *dev,
1278 				   struct device *old_parent,
1279 				   struct device *new_parent)
1280 {
1281 	int error = 0;
1282 #ifdef CONFIG_SYSFS_DEPRECATED
1283 	char *class_name;
1284 
1285 	class_name = make_class_name(dev->class->name, &dev->kobj);
1286 	if (!class_name) {
1287 		error = -ENOMEM;
1288 		goto out;
1289 	}
1290 	if (old_parent) {
1291 		sysfs_remove_link(&dev->kobj, "device");
1292 		sysfs_remove_link(&old_parent->kobj, class_name);
1293 	}
1294 	if (new_parent) {
1295 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1296 					  "device");
1297 		if (error)
1298 			goto out;
1299 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1300 					  class_name);
1301 		if (error)
1302 			sysfs_remove_link(&dev->kobj, "device");
1303 	} else
1304 		error = 0;
1305 out:
1306 	kfree(class_name);
1307 	return error;
1308 #else
1309 	if (old_parent)
1310 		sysfs_remove_link(&dev->kobj, "device");
1311 	if (new_parent)
1312 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1313 					  "device");
1314 	return error;
1315 #endif
1316 }
1317 
1318 /**
1319  * device_move - moves a device to a new parent
1320  * @dev: the pointer to the struct device to be moved
1321  * @new_parent: the new parent of the device (can by NULL)
1322  */
1323 int device_move(struct device *dev, struct device *new_parent)
1324 {
1325 	int error;
1326 	struct device *old_parent;
1327 	struct kobject *new_parent_kobj;
1328 
1329 	dev = get_device(dev);
1330 	if (!dev)
1331 		return -EINVAL;
1332 
1333 	new_parent = get_device(new_parent);
1334 	new_parent_kobj = get_device_parent(dev, new_parent);
1335 
1336 	pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
1337 		 __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
1338 	error = kobject_move(&dev->kobj, new_parent_kobj);
1339 	if (error) {
1340 		cleanup_glue_dir(dev, new_parent_kobj);
1341 		put_device(new_parent);
1342 		goto out;
1343 	}
1344 	old_parent = dev->parent;
1345 	dev->parent = new_parent;
1346 	if (old_parent)
1347 		klist_remove(&dev->knode_parent);
1348 	if (new_parent)
1349 		klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1350 	if (!dev->class)
1351 		goto out_put;
1352 	error = device_move_class_links(dev, old_parent, new_parent);
1353 	if (error) {
1354 		/* We ignore errors on cleanup since we're hosed anyway... */
1355 		device_move_class_links(dev, new_parent, old_parent);
1356 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1357 			if (new_parent)
1358 				klist_remove(&dev->knode_parent);
1359 			if (old_parent)
1360 				klist_add_tail(&dev->knode_parent,
1361 					       &old_parent->klist_children);
1362 		}
1363 		cleanup_glue_dir(dev, new_parent_kobj);
1364 		put_device(new_parent);
1365 		goto out;
1366 	}
1367 out_put:
1368 	put_device(old_parent);
1369 out:
1370 	put_device(dev);
1371 	return error;
1372 }
1373 EXPORT_SYMBOL_GPL(device_move);
1374 
1375 /**
1376  * device_shutdown - call ->shutdown() on each device to shutdown.
1377  */
1378 void device_shutdown(void)
1379 {
1380 	struct device *dev, *devn;
1381 
1382 	list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1383 				kobj.entry) {
1384 		if (dev->bus && dev->bus->shutdown) {
1385 			dev_dbg(dev, "shutdown\n");
1386 			dev->bus->shutdown(dev);
1387 		} else if (dev->driver && dev->driver->shutdown) {
1388 			dev_dbg(dev, "shutdown\n");
1389 			dev->driver->shutdown(dev);
1390 		}
1391 	}
1392 }
1393