xref: /linux/drivers/base/core.c (revision e190bfe56841551b1ad5abb42ebd0c4798cc8c01)
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 <linux/kallsyms.h>
23 #include <linux/mutex.h>
24 #include <linux/async.h>
25 
26 #include "base.h"
27 #include "power/power.h"
28 
29 int (*platform_notify)(struct device *dev) = NULL;
30 int (*platform_notify_remove)(struct device *dev) = NULL;
31 static struct kobject *dev_kobj;
32 struct kobject *sysfs_dev_char_kobj;
33 struct kobject *sysfs_dev_block_kobj;
34 
35 #ifdef CONFIG_BLOCK
36 static inline int device_is_not_partition(struct device *dev)
37 {
38 	return !(dev->type == &part_type);
39 }
40 #else
41 static inline int device_is_not_partition(struct device *dev)
42 {
43 	return 1;
44 }
45 #endif
46 
47 /**
48  * dev_driver_string - Return a device's driver name, if at all possible
49  * @dev: struct device to get the name of
50  *
51  * Will return the device's driver's name if it is bound to a device.  If
52  * the device is not bound to a device, it will return the name of the bus
53  * it is attached to.  If it is not attached to a bus either, an empty
54  * string will be returned.
55  */
56 const char *dev_driver_string(const struct device *dev)
57 {
58 	struct device_driver *drv;
59 
60 	/* dev->driver can change to NULL underneath us because of unbinding,
61 	 * so be careful about accessing it.  dev->bus and dev->class should
62 	 * never change once they are set, so they don't need special care.
63 	 */
64 	drv = ACCESS_ONCE(dev->driver);
65 	return drv ? drv->name :
66 			(dev->bus ? dev->bus->name :
67 			(dev->class ? dev->class->name : ""));
68 }
69 EXPORT_SYMBOL(dev_driver_string);
70 
71 #define to_dev(obj) container_of(obj, struct device, kobj)
72 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
73 
74 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
75 			     char *buf)
76 {
77 	struct device_attribute *dev_attr = to_dev_attr(attr);
78 	struct device *dev = to_dev(kobj);
79 	ssize_t ret = -EIO;
80 
81 	if (dev_attr->show)
82 		ret = dev_attr->show(dev, dev_attr, buf);
83 	if (ret >= (ssize_t)PAGE_SIZE) {
84 		print_symbol("dev_attr_show: %s returned bad count\n",
85 				(unsigned long)dev_attr->show);
86 	}
87 	return ret;
88 }
89 
90 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
91 			      const char *buf, size_t count)
92 {
93 	struct device_attribute *dev_attr = to_dev_attr(attr);
94 	struct device *dev = to_dev(kobj);
95 	ssize_t ret = -EIO;
96 
97 	if (dev_attr->store)
98 		ret = dev_attr->store(dev, dev_attr, buf, count);
99 	return ret;
100 }
101 
102 static const struct sysfs_ops dev_sysfs_ops = {
103 	.show	= dev_attr_show,
104 	.store	= dev_attr_store,
105 };
106 
107 
108 /**
109  *	device_release - free device structure.
110  *	@kobj:	device's kobject.
111  *
112  *	This is called once the reference count for the object
113  *	reaches 0. We forward the call to the device's release
114  *	method, which should handle actually freeing the structure.
115  */
116 static void device_release(struct kobject *kobj)
117 {
118 	struct device *dev = to_dev(kobj);
119 	struct device_private *p = dev->p;
120 
121 	if (dev->release)
122 		dev->release(dev);
123 	else if (dev->type && dev->type->release)
124 		dev->type->release(dev);
125 	else if (dev->class && dev->class->dev_release)
126 		dev->class->dev_release(dev);
127 	else
128 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
129 			"function, it is broken and must be fixed.\n",
130 			dev_name(dev));
131 	kfree(p);
132 }
133 
134 static const void *device_namespace(struct kobject *kobj)
135 {
136 	struct device *dev = to_dev(kobj);
137 	const void *ns = NULL;
138 
139 	if (dev->class && dev->class->ns_type)
140 		ns = dev->class->namespace(dev);
141 
142 	return ns;
143 }
144 
145 static struct kobj_type device_ktype = {
146 	.release	= device_release,
147 	.sysfs_ops	= &dev_sysfs_ops,
148 	.namespace	= device_namespace,
149 };
150 
151 
152 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
153 {
154 	struct kobj_type *ktype = get_ktype(kobj);
155 
156 	if (ktype == &device_ktype) {
157 		struct device *dev = to_dev(kobj);
158 		if (dev->bus)
159 			return 1;
160 		if (dev->class)
161 			return 1;
162 	}
163 	return 0;
164 }
165 
166 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
167 {
168 	struct device *dev = to_dev(kobj);
169 
170 	if (dev->bus)
171 		return dev->bus->name;
172 	if (dev->class)
173 		return dev->class->name;
174 	return NULL;
175 }
176 
177 static int dev_uevent(struct kset *kset, struct kobject *kobj,
178 		      struct kobj_uevent_env *env)
179 {
180 	struct device *dev = to_dev(kobj);
181 	int retval = 0;
182 
183 	/* add device node properties if present */
184 	if (MAJOR(dev->devt)) {
185 		const char *tmp;
186 		const char *name;
187 		mode_t mode = 0;
188 
189 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
190 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
191 		name = device_get_devnode(dev, &mode, &tmp);
192 		if (name) {
193 			add_uevent_var(env, "DEVNAME=%s", name);
194 			kfree(tmp);
195 			if (mode)
196 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
197 		}
198 	}
199 
200 	if (dev->type && dev->type->name)
201 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
202 
203 	if (dev->driver)
204 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
205 
206 #ifdef CONFIG_SYSFS_DEPRECATED
207 	if (dev->class) {
208 		struct device *parent = dev->parent;
209 
210 		/* find first bus device in parent chain */
211 		while (parent && !parent->bus)
212 			parent = parent->parent;
213 		if (parent && parent->bus) {
214 			const char *path;
215 
216 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
217 			if (path) {
218 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
219 				kfree(path);
220 			}
221 
222 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
223 
224 			if (parent->driver)
225 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
226 					       parent->driver->name);
227 		}
228 	} else if (dev->bus) {
229 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
230 
231 		if (dev->driver)
232 			add_uevent_var(env, "PHYSDEVDRIVER=%s",
233 				       dev->driver->name);
234 	}
235 #endif
236 
237 	/* have the bus specific function add its stuff */
238 	if (dev->bus && dev->bus->uevent) {
239 		retval = dev->bus->uevent(dev, env);
240 		if (retval)
241 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
242 				 dev_name(dev), __func__, retval);
243 	}
244 
245 	/* have the class specific function add its stuff */
246 	if (dev->class && dev->class->dev_uevent) {
247 		retval = dev->class->dev_uevent(dev, env);
248 		if (retval)
249 			pr_debug("device: '%s': %s: class uevent() "
250 				 "returned %d\n", dev_name(dev),
251 				 __func__, retval);
252 	}
253 
254 	/* have the device type specific fuction add its stuff */
255 	if (dev->type && dev->type->uevent) {
256 		retval = dev->type->uevent(dev, env);
257 		if (retval)
258 			pr_debug("device: '%s': %s: dev_type uevent() "
259 				 "returned %d\n", dev_name(dev),
260 				 __func__, retval);
261 	}
262 
263 	return retval;
264 }
265 
266 static const struct kset_uevent_ops device_uevent_ops = {
267 	.filter =	dev_uevent_filter,
268 	.name =		dev_uevent_name,
269 	.uevent =	dev_uevent,
270 };
271 
272 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
273 			   char *buf)
274 {
275 	struct kobject *top_kobj;
276 	struct kset *kset;
277 	struct kobj_uevent_env *env = NULL;
278 	int i;
279 	size_t count = 0;
280 	int retval;
281 
282 	/* search the kset, the device belongs to */
283 	top_kobj = &dev->kobj;
284 	while (!top_kobj->kset && top_kobj->parent)
285 		top_kobj = top_kobj->parent;
286 	if (!top_kobj->kset)
287 		goto out;
288 
289 	kset = top_kobj->kset;
290 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
291 		goto out;
292 
293 	/* respect filter */
294 	if (kset->uevent_ops && kset->uevent_ops->filter)
295 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
296 			goto out;
297 
298 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
299 	if (!env)
300 		return -ENOMEM;
301 
302 	/* let the kset specific function add its keys */
303 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
304 	if (retval)
305 		goto out;
306 
307 	/* copy keys to file */
308 	for (i = 0; i < env->envp_idx; i++)
309 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
310 out:
311 	kfree(env);
312 	return count;
313 }
314 
315 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
316 			    const char *buf, size_t count)
317 {
318 	enum kobject_action action;
319 
320 	if (kobject_action_type(buf, count, &action) == 0)
321 		kobject_uevent(&dev->kobj, action);
322 	else
323 		dev_err(dev, "uevent: unknown action-string\n");
324 	return count;
325 }
326 
327 static struct device_attribute uevent_attr =
328 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
329 
330 static int device_add_attributes(struct device *dev,
331 				 struct device_attribute *attrs)
332 {
333 	int error = 0;
334 	int i;
335 
336 	if (attrs) {
337 		for (i = 0; attr_name(attrs[i]); i++) {
338 			error = device_create_file(dev, &attrs[i]);
339 			if (error)
340 				break;
341 		}
342 		if (error)
343 			while (--i >= 0)
344 				device_remove_file(dev, &attrs[i]);
345 	}
346 	return error;
347 }
348 
349 static void device_remove_attributes(struct device *dev,
350 				     struct device_attribute *attrs)
351 {
352 	int i;
353 
354 	if (attrs)
355 		for (i = 0; attr_name(attrs[i]); i++)
356 			device_remove_file(dev, &attrs[i]);
357 }
358 
359 static int device_add_groups(struct device *dev,
360 			     const struct attribute_group **groups)
361 {
362 	int error = 0;
363 	int i;
364 
365 	if (groups) {
366 		for (i = 0; groups[i]; i++) {
367 			error = sysfs_create_group(&dev->kobj, groups[i]);
368 			if (error) {
369 				while (--i >= 0)
370 					sysfs_remove_group(&dev->kobj,
371 							   groups[i]);
372 				break;
373 			}
374 		}
375 	}
376 	return error;
377 }
378 
379 static void device_remove_groups(struct device *dev,
380 				 const struct attribute_group **groups)
381 {
382 	int i;
383 
384 	if (groups)
385 		for (i = 0; groups[i]; i++)
386 			sysfs_remove_group(&dev->kobj, groups[i]);
387 }
388 
389 static int device_add_attrs(struct device *dev)
390 {
391 	struct class *class = dev->class;
392 	struct device_type *type = dev->type;
393 	int error;
394 
395 	if (class) {
396 		error = device_add_attributes(dev, class->dev_attrs);
397 		if (error)
398 			return error;
399 	}
400 
401 	if (type) {
402 		error = device_add_groups(dev, type->groups);
403 		if (error)
404 			goto err_remove_class_attrs;
405 	}
406 
407 	error = device_add_groups(dev, dev->groups);
408 	if (error)
409 		goto err_remove_type_groups;
410 
411 	return 0;
412 
413  err_remove_type_groups:
414 	if (type)
415 		device_remove_groups(dev, type->groups);
416  err_remove_class_attrs:
417 	if (class)
418 		device_remove_attributes(dev, class->dev_attrs);
419 
420 	return error;
421 }
422 
423 static void device_remove_attrs(struct device *dev)
424 {
425 	struct class *class = dev->class;
426 	struct device_type *type = dev->type;
427 
428 	device_remove_groups(dev, dev->groups);
429 
430 	if (type)
431 		device_remove_groups(dev, type->groups);
432 
433 	if (class)
434 		device_remove_attributes(dev, class->dev_attrs);
435 }
436 
437 
438 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
439 			char *buf)
440 {
441 	return print_dev_t(buf, dev->devt);
442 }
443 
444 static struct device_attribute devt_attr =
445 	__ATTR(dev, S_IRUGO, show_dev, NULL);
446 
447 /* kset to create /sys/devices/  */
448 struct kset *devices_kset;
449 
450 /**
451  * device_create_file - create sysfs attribute file for device.
452  * @dev: device.
453  * @attr: device attribute descriptor.
454  */
455 int device_create_file(struct device *dev,
456 		       const struct device_attribute *attr)
457 {
458 	int error = 0;
459 	if (dev)
460 		error = sysfs_create_file(&dev->kobj, &attr->attr);
461 	return error;
462 }
463 
464 /**
465  * device_remove_file - remove sysfs attribute file.
466  * @dev: device.
467  * @attr: device attribute descriptor.
468  */
469 void device_remove_file(struct device *dev,
470 			const struct device_attribute *attr)
471 {
472 	if (dev)
473 		sysfs_remove_file(&dev->kobj, &attr->attr);
474 }
475 
476 /**
477  * device_create_bin_file - create sysfs binary attribute file for device.
478  * @dev: device.
479  * @attr: device binary attribute descriptor.
480  */
481 int device_create_bin_file(struct device *dev,
482 			   const struct bin_attribute *attr)
483 {
484 	int error = -EINVAL;
485 	if (dev)
486 		error = sysfs_create_bin_file(&dev->kobj, attr);
487 	return error;
488 }
489 EXPORT_SYMBOL_GPL(device_create_bin_file);
490 
491 /**
492  * device_remove_bin_file - remove sysfs binary attribute file
493  * @dev: device.
494  * @attr: device binary attribute descriptor.
495  */
496 void device_remove_bin_file(struct device *dev,
497 			    const struct bin_attribute *attr)
498 {
499 	if (dev)
500 		sysfs_remove_bin_file(&dev->kobj, attr);
501 }
502 EXPORT_SYMBOL_GPL(device_remove_bin_file);
503 
504 /**
505  * device_schedule_callback_owner - helper to schedule a callback for a device
506  * @dev: device.
507  * @func: callback function to invoke later.
508  * @owner: module owning the callback routine
509  *
510  * Attribute methods must not unregister themselves or their parent device
511  * (which would amount to the same thing).  Attempts to do so will deadlock,
512  * since unregistration is mutually exclusive with driver callbacks.
513  *
514  * Instead methods can call this routine, which will attempt to allocate
515  * and schedule a workqueue request to call back @func with @dev as its
516  * argument in the workqueue's process context.  @dev will be pinned until
517  * @func returns.
518  *
519  * This routine is usually called via the inline device_schedule_callback(),
520  * which automatically sets @owner to THIS_MODULE.
521  *
522  * Returns 0 if the request was submitted, -ENOMEM if storage could not
523  * be allocated, -ENODEV if a reference to @owner isn't available.
524  *
525  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
526  * underlying sysfs routine (since it is intended for use by attribute
527  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
528  */
529 int device_schedule_callback_owner(struct device *dev,
530 		void (*func)(struct device *), struct module *owner)
531 {
532 	return sysfs_schedule_callback(&dev->kobj,
533 			(void (*)(void *)) func, dev, owner);
534 }
535 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
536 
537 static void klist_children_get(struct klist_node *n)
538 {
539 	struct device_private *p = to_device_private_parent(n);
540 	struct device *dev = p->device;
541 
542 	get_device(dev);
543 }
544 
545 static void klist_children_put(struct klist_node *n)
546 {
547 	struct device_private *p = to_device_private_parent(n);
548 	struct device *dev = p->device;
549 
550 	put_device(dev);
551 }
552 
553 /**
554  * device_initialize - init device structure.
555  * @dev: device.
556  *
557  * This prepares the device for use by other layers by initializing
558  * its fields.
559  * It is the first half of device_register(), if called by
560  * that function, though it can also be called separately, so one
561  * may use @dev's fields. In particular, get_device()/put_device()
562  * may be used for reference counting of @dev after calling this
563  * function.
564  *
565  * NOTE: Use put_device() to give up your reference instead of freeing
566  * @dev directly once you have called this function.
567  */
568 void device_initialize(struct device *dev)
569 {
570 	dev->kobj.kset = devices_kset;
571 	kobject_init(&dev->kobj, &device_ktype);
572 	INIT_LIST_HEAD(&dev->dma_pools);
573 	mutex_init(&dev->mutex);
574 	lockdep_set_novalidate_class(&dev->mutex);
575 	spin_lock_init(&dev->devres_lock);
576 	INIT_LIST_HEAD(&dev->devres_head);
577 	device_pm_init(dev);
578 	set_dev_node(dev, -1);
579 }
580 
581 #ifdef CONFIG_SYSFS_DEPRECATED
582 static struct kobject *get_device_parent(struct device *dev,
583 					 struct device *parent)
584 {
585 	/* class devices without a parent live in /sys/class/<classname>/ */
586 	if (dev->class && (!parent || parent->class != dev->class))
587 		return &dev->class->p->class_subsys.kobj;
588 	/* all other devices keep their parent */
589 	else if (parent)
590 		return &parent->kobj;
591 
592 	return NULL;
593 }
594 
595 static inline void cleanup_device_parent(struct device *dev) {}
596 static inline void cleanup_glue_dir(struct device *dev,
597 				    struct kobject *glue_dir) {}
598 #else
599 static struct kobject *virtual_device_parent(struct device *dev)
600 {
601 	static struct kobject *virtual_dir = NULL;
602 
603 	if (!virtual_dir)
604 		virtual_dir = kobject_create_and_add("virtual",
605 						     &devices_kset->kobj);
606 
607 	return virtual_dir;
608 }
609 
610 struct class_dir {
611 	struct kobject kobj;
612 	struct class *class;
613 };
614 
615 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
616 
617 static void class_dir_release(struct kobject *kobj)
618 {
619 	struct class_dir *dir = to_class_dir(kobj);
620 	kfree(dir);
621 }
622 
623 static const
624 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
625 {
626 	struct class_dir *dir = to_class_dir(kobj);
627 	return dir->class->ns_type;
628 }
629 
630 static struct kobj_type class_dir_ktype = {
631 	.release	= class_dir_release,
632 	.sysfs_ops	= &kobj_sysfs_ops,
633 	.child_ns_type	= class_dir_child_ns_type
634 };
635 
636 static struct kobject *
637 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
638 {
639 	struct class_dir *dir;
640 	int retval;
641 
642 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
643 	if (!dir)
644 		return NULL;
645 
646 	dir->class = class;
647 	kobject_init(&dir->kobj, &class_dir_ktype);
648 
649 	dir->kobj.kset = &class->p->class_dirs;
650 
651 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
652 	if (retval < 0) {
653 		kobject_put(&dir->kobj);
654 		return NULL;
655 	}
656 	return &dir->kobj;
657 }
658 
659 
660 static struct kobject *get_device_parent(struct device *dev,
661 					 struct device *parent)
662 {
663 	if (dev->class) {
664 		static DEFINE_MUTEX(gdp_mutex);
665 		struct kobject *kobj = NULL;
666 		struct kobject *parent_kobj;
667 		struct kobject *k;
668 
669 		/*
670 		 * If we have no parent, we live in "virtual".
671 		 * Class-devices with a non class-device as parent, live
672 		 * in a "glue" directory to prevent namespace collisions.
673 		 */
674 		if (parent == NULL)
675 			parent_kobj = virtual_device_parent(dev);
676 		else if (parent->class)
677 			return &parent->kobj;
678 		else
679 			parent_kobj = &parent->kobj;
680 
681 		mutex_lock(&gdp_mutex);
682 
683 		/* find our class-directory at the parent and reference it */
684 		spin_lock(&dev->class->p->class_dirs.list_lock);
685 		list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
686 			if (k->parent == parent_kobj) {
687 				kobj = kobject_get(k);
688 				break;
689 			}
690 		spin_unlock(&dev->class->p->class_dirs.list_lock);
691 		if (kobj) {
692 			mutex_unlock(&gdp_mutex);
693 			return kobj;
694 		}
695 
696 		/* or create a new class-directory at the parent device */
697 		k = class_dir_create_and_add(dev->class, parent_kobj);
698 		/* do not emit an uevent for this simple "glue" directory */
699 		mutex_unlock(&gdp_mutex);
700 		return k;
701 	}
702 
703 	if (parent)
704 		return &parent->kobj;
705 	return NULL;
706 }
707 
708 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
709 {
710 	/* see if we live in a "glue" directory */
711 	if (!glue_dir || !dev->class ||
712 	    glue_dir->kset != &dev->class->p->class_dirs)
713 		return;
714 
715 	kobject_put(glue_dir);
716 }
717 
718 static void cleanup_device_parent(struct device *dev)
719 {
720 	cleanup_glue_dir(dev, dev->kobj.parent);
721 }
722 #endif
723 
724 static void setup_parent(struct device *dev, struct device *parent)
725 {
726 	struct kobject *kobj;
727 	kobj = get_device_parent(dev, parent);
728 	if (kobj)
729 		dev->kobj.parent = kobj;
730 }
731 
732 static int device_add_class_symlinks(struct device *dev)
733 {
734 	int error;
735 
736 	if (!dev->class)
737 		return 0;
738 
739 	error = sysfs_create_link(&dev->kobj,
740 				  &dev->class->p->class_subsys.kobj,
741 				  "subsystem");
742 	if (error)
743 		goto out;
744 
745 #ifdef CONFIG_SYSFS_DEPRECATED
746 	/* stacked class devices need a symlink in the class directory */
747 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
748 	    device_is_not_partition(dev)) {
749 		error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
750 					  &dev->kobj, dev_name(dev));
751 		if (error)
752 			goto out_subsys;
753 	}
754 
755 	if (dev->parent && device_is_not_partition(dev)) {
756 		struct device *parent = dev->parent;
757 		char *class_name;
758 
759 		/*
760 		 * stacked class devices have the 'device' link
761 		 * pointing to the bus device instead of the parent
762 		 */
763 		while (parent->class && !parent->bus && parent->parent)
764 			parent = parent->parent;
765 
766 		error = sysfs_create_link(&dev->kobj,
767 					  &parent->kobj,
768 					  "device");
769 		if (error)
770 			goto out_busid;
771 
772 		class_name = make_class_name(dev->class->name,
773 						&dev->kobj);
774 		if (class_name)
775 			error = sysfs_create_link(&dev->parent->kobj,
776 						&dev->kobj, class_name);
777 		kfree(class_name);
778 		if (error)
779 			goto out_device;
780 	}
781 	return 0;
782 
783 out_device:
784 	if (dev->parent && device_is_not_partition(dev))
785 		sysfs_remove_link(&dev->kobj, "device");
786 out_busid:
787 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
788 	    device_is_not_partition(dev))
789 		sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
790 				  dev_name(dev));
791 #else
792 	/* link in the class directory pointing to the device */
793 	error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
794 				  &dev->kobj, dev_name(dev));
795 	if (error)
796 		goto out_subsys;
797 
798 	if (dev->parent && device_is_not_partition(dev)) {
799 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
800 					  "device");
801 		if (error)
802 			goto out_busid;
803 	}
804 	return 0;
805 
806 out_busid:
807 	sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
808 #endif
809 
810 out_subsys:
811 	sysfs_remove_link(&dev->kobj, "subsystem");
812 out:
813 	return error;
814 }
815 
816 static void device_remove_class_symlinks(struct device *dev)
817 {
818 	if (!dev->class)
819 		return;
820 
821 #ifdef CONFIG_SYSFS_DEPRECATED
822 	if (dev->parent && device_is_not_partition(dev)) {
823 		char *class_name;
824 
825 		class_name = make_class_name(dev->class->name, &dev->kobj);
826 		if (class_name) {
827 			sysfs_remove_link(&dev->parent->kobj, class_name);
828 			kfree(class_name);
829 		}
830 		sysfs_remove_link(&dev->kobj, "device");
831 	}
832 
833 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
834 	    device_is_not_partition(dev))
835 		sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
836 				  dev_name(dev));
837 #else
838 	if (dev->parent && device_is_not_partition(dev))
839 		sysfs_remove_link(&dev->kobj, "device");
840 
841 	sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
842 #endif
843 
844 	sysfs_remove_link(&dev->kobj, "subsystem");
845 }
846 
847 /**
848  * dev_set_name - set a device name
849  * @dev: device
850  * @fmt: format string for the device's name
851  */
852 int dev_set_name(struct device *dev, const char *fmt, ...)
853 {
854 	va_list vargs;
855 	int err;
856 
857 	va_start(vargs, fmt);
858 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
859 	va_end(vargs);
860 	return err;
861 }
862 EXPORT_SYMBOL_GPL(dev_set_name);
863 
864 /**
865  * device_to_dev_kobj - select a /sys/dev/ directory for the device
866  * @dev: device
867  *
868  * By default we select char/ for new entries.  Setting class->dev_obj
869  * to NULL prevents an entry from being created.  class->dev_kobj must
870  * be set (or cleared) before any devices are registered to the class
871  * otherwise device_create_sys_dev_entry() and
872  * device_remove_sys_dev_entry() will disagree about the the presence
873  * of the link.
874  */
875 static struct kobject *device_to_dev_kobj(struct device *dev)
876 {
877 	struct kobject *kobj;
878 
879 	if (dev->class)
880 		kobj = dev->class->dev_kobj;
881 	else
882 		kobj = sysfs_dev_char_kobj;
883 
884 	return kobj;
885 }
886 
887 static int device_create_sys_dev_entry(struct device *dev)
888 {
889 	struct kobject *kobj = device_to_dev_kobj(dev);
890 	int error = 0;
891 	char devt_str[15];
892 
893 	if (kobj) {
894 		format_dev_t(devt_str, dev->devt);
895 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
896 	}
897 
898 	return error;
899 }
900 
901 static void device_remove_sys_dev_entry(struct device *dev)
902 {
903 	struct kobject *kobj = device_to_dev_kobj(dev);
904 	char devt_str[15];
905 
906 	if (kobj) {
907 		format_dev_t(devt_str, dev->devt);
908 		sysfs_remove_link(kobj, devt_str);
909 	}
910 }
911 
912 int device_private_init(struct device *dev)
913 {
914 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
915 	if (!dev->p)
916 		return -ENOMEM;
917 	dev->p->device = dev;
918 	klist_init(&dev->p->klist_children, klist_children_get,
919 		   klist_children_put);
920 	return 0;
921 }
922 
923 /**
924  * device_add - add device to device hierarchy.
925  * @dev: device.
926  *
927  * This is part 2 of device_register(), though may be called
928  * separately _iff_ device_initialize() has been called separately.
929  *
930  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
931  * to the global and sibling lists for the device, then
932  * adds it to the other relevant subsystems of the driver model.
933  *
934  * NOTE: _Never_ directly free @dev after calling this function, even
935  * if it returned an error! Always use put_device() to give up your
936  * reference instead.
937  */
938 int device_add(struct device *dev)
939 {
940 	struct device *parent = NULL;
941 	struct class_interface *class_intf;
942 	int error = -EINVAL;
943 
944 	dev = get_device(dev);
945 	if (!dev)
946 		goto done;
947 
948 	if (!dev->p) {
949 		error = device_private_init(dev);
950 		if (error)
951 			goto done;
952 	}
953 
954 	/*
955 	 * for statically allocated devices, which should all be converted
956 	 * some day, we need to initialize the name. We prevent reading back
957 	 * the name, and force the use of dev_name()
958 	 */
959 	if (dev->init_name) {
960 		dev_set_name(dev, "%s", dev->init_name);
961 		dev->init_name = NULL;
962 	}
963 
964 	if (!dev_name(dev)) {
965 		error = -EINVAL;
966 		goto name_error;
967 	}
968 
969 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
970 
971 	parent = get_device(dev->parent);
972 	setup_parent(dev, parent);
973 
974 	/* use parent numa_node */
975 	if (parent)
976 		set_dev_node(dev, dev_to_node(parent));
977 
978 	/* first, register with generic layer. */
979 	/* we require the name to be set before, and pass NULL */
980 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
981 	if (error)
982 		goto Error;
983 
984 	/* notify platform of device entry */
985 	if (platform_notify)
986 		platform_notify(dev);
987 
988 	error = device_create_file(dev, &uevent_attr);
989 	if (error)
990 		goto attrError;
991 
992 	if (MAJOR(dev->devt)) {
993 		error = device_create_file(dev, &devt_attr);
994 		if (error)
995 			goto ueventattrError;
996 
997 		error = device_create_sys_dev_entry(dev);
998 		if (error)
999 			goto devtattrError;
1000 
1001 		devtmpfs_create_node(dev);
1002 	}
1003 
1004 	error = device_add_class_symlinks(dev);
1005 	if (error)
1006 		goto SymlinkError;
1007 	error = device_add_attrs(dev);
1008 	if (error)
1009 		goto AttrsError;
1010 	error = bus_add_device(dev);
1011 	if (error)
1012 		goto BusError;
1013 	error = dpm_sysfs_add(dev);
1014 	if (error)
1015 		goto DPMError;
1016 	device_pm_add(dev);
1017 
1018 	/* Notify clients of device addition.  This call must come
1019 	 * after dpm_sysf_add() and before kobject_uevent().
1020 	 */
1021 	if (dev->bus)
1022 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1023 					     BUS_NOTIFY_ADD_DEVICE, dev);
1024 
1025 	kobject_uevent(&dev->kobj, KOBJ_ADD);
1026 	bus_probe_device(dev);
1027 	if (parent)
1028 		klist_add_tail(&dev->p->knode_parent,
1029 			       &parent->p->klist_children);
1030 
1031 	if (dev->class) {
1032 		mutex_lock(&dev->class->p->class_mutex);
1033 		/* tie the class to the device */
1034 		klist_add_tail(&dev->knode_class,
1035 			       &dev->class->p->class_devices);
1036 
1037 		/* notify any interfaces that the device is here */
1038 		list_for_each_entry(class_intf,
1039 				    &dev->class->p->class_interfaces, node)
1040 			if (class_intf->add_dev)
1041 				class_intf->add_dev(dev, class_intf);
1042 		mutex_unlock(&dev->class->p->class_mutex);
1043 	}
1044 done:
1045 	put_device(dev);
1046 	return error;
1047  DPMError:
1048 	bus_remove_device(dev);
1049  BusError:
1050 	device_remove_attrs(dev);
1051  AttrsError:
1052 	device_remove_class_symlinks(dev);
1053  SymlinkError:
1054 	if (MAJOR(dev->devt))
1055 		devtmpfs_delete_node(dev);
1056 	if (MAJOR(dev->devt))
1057 		device_remove_sys_dev_entry(dev);
1058  devtattrError:
1059 	if (MAJOR(dev->devt))
1060 		device_remove_file(dev, &devt_attr);
1061  ueventattrError:
1062 	device_remove_file(dev, &uevent_attr);
1063  attrError:
1064 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1065 	kobject_del(&dev->kobj);
1066  Error:
1067 	cleanup_device_parent(dev);
1068 	if (parent)
1069 		put_device(parent);
1070 name_error:
1071 	kfree(dev->p);
1072 	dev->p = NULL;
1073 	goto done;
1074 }
1075 
1076 /**
1077  * device_register - register a device with the system.
1078  * @dev: pointer to the device structure
1079  *
1080  * This happens in two clean steps - initialize the device
1081  * and add it to the system. The two steps can be called
1082  * separately, but this is the easiest and most common.
1083  * I.e. you should only call the two helpers separately if
1084  * have a clearly defined need to use and refcount the device
1085  * before it is added to the hierarchy.
1086  *
1087  * NOTE: _Never_ directly free @dev after calling this function, even
1088  * if it returned an error! Always use put_device() to give up the
1089  * reference initialized in this function instead.
1090  */
1091 int device_register(struct device *dev)
1092 {
1093 	device_initialize(dev);
1094 	return device_add(dev);
1095 }
1096 
1097 /**
1098  * get_device - increment reference count for device.
1099  * @dev: device.
1100  *
1101  * This simply forwards the call to kobject_get(), though
1102  * we do take care to provide for the case that we get a NULL
1103  * pointer passed in.
1104  */
1105 struct device *get_device(struct device *dev)
1106 {
1107 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1108 }
1109 
1110 /**
1111  * put_device - decrement reference count.
1112  * @dev: device in question.
1113  */
1114 void put_device(struct device *dev)
1115 {
1116 	/* might_sleep(); */
1117 	if (dev)
1118 		kobject_put(&dev->kobj);
1119 }
1120 
1121 /**
1122  * device_del - delete device from system.
1123  * @dev: device.
1124  *
1125  * This is the first part of the device unregistration
1126  * sequence. This removes the device from the lists we control
1127  * from here, has it removed from the other driver model
1128  * subsystems it was added to in device_add(), and removes it
1129  * from the kobject hierarchy.
1130  *
1131  * NOTE: this should be called manually _iff_ device_add() was
1132  * also called manually.
1133  */
1134 void device_del(struct device *dev)
1135 {
1136 	struct device *parent = dev->parent;
1137 	struct class_interface *class_intf;
1138 
1139 	/* Notify clients of device removal.  This call must come
1140 	 * before dpm_sysfs_remove().
1141 	 */
1142 	if (dev->bus)
1143 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1144 					     BUS_NOTIFY_DEL_DEVICE, dev);
1145 	device_pm_remove(dev);
1146 	dpm_sysfs_remove(dev);
1147 	if (parent)
1148 		klist_del(&dev->p->knode_parent);
1149 	if (MAJOR(dev->devt)) {
1150 		devtmpfs_delete_node(dev);
1151 		device_remove_sys_dev_entry(dev);
1152 		device_remove_file(dev, &devt_attr);
1153 	}
1154 	if (dev->class) {
1155 		device_remove_class_symlinks(dev);
1156 
1157 		mutex_lock(&dev->class->p->class_mutex);
1158 		/* notify any interfaces that the device is now gone */
1159 		list_for_each_entry(class_intf,
1160 				    &dev->class->p->class_interfaces, node)
1161 			if (class_intf->remove_dev)
1162 				class_intf->remove_dev(dev, class_intf);
1163 		/* remove the device from the class list */
1164 		klist_del(&dev->knode_class);
1165 		mutex_unlock(&dev->class->p->class_mutex);
1166 	}
1167 	device_remove_file(dev, &uevent_attr);
1168 	device_remove_attrs(dev);
1169 	bus_remove_device(dev);
1170 
1171 	/*
1172 	 * Some platform devices are driven without driver attached
1173 	 * and managed resources may have been acquired.  Make sure
1174 	 * all resources are released.
1175 	 */
1176 	devres_release_all(dev);
1177 
1178 	/* Notify the platform of the removal, in case they
1179 	 * need to do anything...
1180 	 */
1181 	if (platform_notify_remove)
1182 		platform_notify_remove(dev);
1183 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1184 	cleanup_device_parent(dev);
1185 	kobject_del(&dev->kobj);
1186 	put_device(parent);
1187 }
1188 
1189 /**
1190  * device_unregister - unregister device from system.
1191  * @dev: device going away.
1192  *
1193  * We do this in two parts, like we do device_register(). First,
1194  * we remove it from all the subsystems with device_del(), then
1195  * we decrement the reference count via put_device(). If that
1196  * is the final reference count, the device will be cleaned up
1197  * via device_release() above. Otherwise, the structure will
1198  * stick around until the final reference to the device is dropped.
1199  */
1200 void device_unregister(struct device *dev)
1201 {
1202 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1203 	device_del(dev);
1204 	put_device(dev);
1205 }
1206 
1207 static struct device *next_device(struct klist_iter *i)
1208 {
1209 	struct klist_node *n = klist_next(i);
1210 	struct device *dev = NULL;
1211 	struct device_private *p;
1212 
1213 	if (n) {
1214 		p = to_device_private_parent(n);
1215 		dev = p->device;
1216 	}
1217 	return dev;
1218 }
1219 
1220 /**
1221  * device_get_devnode - path of device node file
1222  * @dev: device
1223  * @mode: returned file access mode
1224  * @tmp: possibly allocated string
1225  *
1226  * Return the relative path of a possible device node.
1227  * Non-default names may need to allocate a memory to compose
1228  * a name. This memory is returned in tmp and needs to be
1229  * freed by the caller.
1230  */
1231 const char *device_get_devnode(struct device *dev,
1232 			       mode_t *mode, const char **tmp)
1233 {
1234 	char *s;
1235 
1236 	*tmp = NULL;
1237 
1238 	/* the device type may provide a specific name */
1239 	if (dev->type && dev->type->devnode)
1240 		*tmp = dev->type->devnode(dev, mode);
1241 	if (*tmp)
1242 		return *tmp;
1243 
1244 	/* the class may provide a specific name */
1245 	if (dev->class && dev->class->devnode)
1246 		*tmp = dev->class->devnode(dev, mode);
1247 	if (*tmp)
1248 		return *tmp;
1249 
1250 	/* return name without allocation, tmp == NULL */
1251 	if (strchr(dev_name(dev), '!') == NULL)
1252 		return dev_name(dev);
1253 
1254 	/* replace '!' in the name with '/' */
1255 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1256 	if (!*tmp)
1257 		return NULL;
1258 	while ((s = strchr(*tmp, '!')))
1259 		s[0] = '/';
1260 	return *tmp;
1261 }
1262 
1263 /**
1264  * device_for_each_child - device child iterator.
1265  * @parent: parent struct device.
1266  * @data: data for the callback.
1267  * @fn: function to be called for each device.
1268  *
1269  * Iterate over @parent's child devices, and call @fn for each,
1270  * passing it @data.
1271  *
1272  * We check the return of @fn each time. If it returns anything
1273  * other than 0, we break out and return that value.
1274  */
1275 int device_for_each_child(struct device *parent, void *data,
1276 			  int (*fn)(struct device *dev, void *data))
1277 {
1278 	struct klist_iter i;
1279 	struct device *child;
1280 	int error = 0;
1281 
1282 	if (!parent->p)
1283 		return 0;
1284 
1285 	klist_iter_init(&parent->p->klist_children, &i);
1286 	while ((child = next_device(&i)) && !error)
1287 		error = fn(child, data);
1288 	klist_iter_exit(&i);
1289 	return error;
1290 }
1291 
1292 /**
1293  * device_find_child - device iterator for locating a particular device.
1294  * @parent: parent struct device
1295  * @data: Data to pass to match function
1296  * @match: Callback function to check device
1297  *
1298  * This is similar to the device_for_each_child() function above, but it
1299  * returns a reference to a device that is 'found' for later use, as
1300  * determined by the @match callback.
1301  *
1302  * The callback should return 0 if the device doesn't match and non-zero
1303  * if it does.  If the callback returns non-zero and a reference to the
1304  * current device can be obtained, this function will return to the caller
1305  * and not iterate over any more devices.
1306  */
1307 struct device *device_find_child(struct device *parent, void *data,
1308 				 int (*match)(struct device *dev, void *data))
1309 {
1310 	struct klist_iter i;
1311 	struct device *child;
1312 
1313 	if (!parent)
1314 		return NULL;
1315 
1316 	klist_iter_init(&parent->p->klist_children, &i);
1317 	while ((child = next_device(&i)))
1318 		if (match(child, data) && get_device(child))
1319 			break;
1320 	klist_iter_exit(&i);
1321 	return child;
1322 }
1323 
1324 int __init devices_init(void)
1325 {
1326 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1327 	if (!devices_kset)
1328 		return -ENOMEM;
1329 	dev_kobj = kobject_create_and_add("dev", NULL);
1330 	if (!dev_kobj)
1331 		goto dev_kobj_err;
1332 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1333 	if (!sysfs_dev_block_kobj)
1334 		goto block_kobj_err;
1335 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1336 	if (!sysfs_dev_char_kobj)
1337 		goto char_kobj_err;
1338 
1339 	return 0;
1340 
1341  char_kobj_err:
1342 	kobject_put(sysfs_dev_block_kobj);
1343  block_kobj_err:
1344 	kobject_put(dev_kobj);
1345  dev_kobj_err:
1346 	kset_unregister(devices_kset);
1347 	return -ENOMEM;
1348 }
1349 
1350 EXPORT_SYMBOL_GPL(device_for_each_child);
1351 EXPORT_SYMBOL_GPL(device_find_child);
1352 
1353 EXPORT_SYMBOL_GPL(device_initialize);
1354 EXPORT_SYMBOL_GPL(device_add);
1355 EXPORT_SYMBOL_GPL(device_register);
1356 
1357 EXPORT_SYMBOL_GPL(device_del);
1358 EXPORT_SYMBOL_GPL(device_unregister);
1359 EXPORT_SYMBOL_GPL(get_device);
1360 EXPORT_SYMBOL_GPL(put_device);
1361 
1362 EXPORT_SYMBOL_GPL(device_create_file);
1363 EXPORT_SYMBOL_GPL(device_remove_file);
1364 
1365 struct root_device
1366 {
1367 	struct device dev;
1368 	struct module *owner;
1369 };
1370 
1371 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1372 
1373 static void root_device_release(struct device *dev)
1374 {
1375 	kfree(to_root_device(dev));
1376 }
1377 
1378 /**
1379  * __root_device_register - allocate and register a root device
1380  * @name: root device name
1381  * @owner: owner module of the root device, usually THIS_MODULE
1382  *
1383  * This function allocates a root device and registers it
1384  * using device_register(). In order to free the returned
1385  * device, use root_device_unregister().
1386  *
1387  * Root devices are dummy devices which allow other devices
1388  * to be grouped under /sys/devices. Use this function to
1389  * allocate a root device and then use it as the parent of
1390  * any device which should appear under /sys/devices/{name}
1391  *
1392  * The /sys/devices/{name} directory will also contain a
1393  * 'module' symlink which points to the @owner directory
1394  * in sysfs.
1395  *
1396  * Returns &struct device pointer on success, or ERR_PTR() on error.
1397  *
1398  * Note: You probably want to use root_device_register().
1399  */
1400 struct device *__root_device_register(const char *name, struct module *owner)
1401 {
1402 	struct root_device *root;
1403 	int err = -ENOMEM;
1404 
1405 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1406 	if (!root)
1407 		return ERR_PTR(err);
1408 
1409 	err = dev_set_name(&root->dev, "%s", name);
1410 	if (err) {
1411 		kfree(root);
1412 		return ERR_PTR(err);
1413 	}
1414 
1415 	root->dev.release = root_device_release;
1416 
1417 	err = device_register(&root->dev);
1418 	if (err) {
1419 		put_device(&root->dev);
1420 		return ERR_PTR(err);
1421 	}
1422 
1423 #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
1424 	if (owner) {
1425 		struct module_kobject *mk = &owner->mkobj;
1426 
1427 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1428 		if (err) {
1429 			device_unregister(&root->dev);
1430 			return ERR_PTR(err);
1431 		}
1432 		root->owner = owner;
1433 	}
1434 #endif
1435 
1436 	return &root->dev;
1437 }
1438 EXPORT_SYMBOL_GPL(__root_device_register);
1439 
1440 /**
1441  * root_device_unregister - unregister and free a root device
1442  * @dev: device going away
1443  *
1444  * This function unregisters and cleans up a device that was created by
1445  * root_device_register().
1446  */
1447 void root_device_unregister(struct device *dev)
1448 {
1449 	struct root_device *root = to_root_device(dev);
1450 
1451 	if (root->owner)
1452 		sysfs_remove_link(&root->dev.kobj, "module");
1453 
1454 	device_unregister(dev);
1455 }
1456 EXPORT_SYMBOL_GPL(root_device_unregister);
1457 
1458 
1459 static void device_create_release(struct device *dev)
1460 {
1461 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1462 	kfree(dev);
1463 }
1464 
1465 /**
1466  * device_create_vargs - creates a device and registers it with sysfs
1467  * @class: pointer to the struct class that this device should be registered to
1468  * @parent: pointer to the parent struct device of this new device, if any
1469  * @devt: the dev_t for the char device to be added
1470  * @drvdata: the data to be added to the device for callbacks
1471  * @fmt: string for the device's name
1472  * @args: va_list for the device's name
1473  *
1474  * This function can be used by char device classes.  A struct device
1475  * will be created in sysfs, registered to the specified class.
1476  *
1477  * A "dev" file will be created, showing the dev_t for the device, if
1478  * the dev_t is not 0,0.
1479  * If a pointer to a parent struct device is passed in, the newly created
1480  * struct device will be a child of that device in sysfs.
1481  * The pointer to the struct device will be returned from the call.
1482  * Any further sysfs files that might be required can be created using this
1483  * pointer.
1484  *
1485  * Returns &struct device pointer on success, or ERR_PTR() on error.
1486  *
1487  * Note: the struct class passed to this function must have previously
1488  * been created with a call to class_create().
1489  */
1490 struct device *device_create_vargs(struct class *class, struct device *parent,
1491 				   dev_t devt, void *drvdata, const char *fmt,
1492 				   va_list args)
1493 {
1494 	struct device *dev = NULL;
1495 	int retval = -ENODEV;
1496 
1497 	if (class == NULL || IS_ERR(class))
1498 		goto error;
1499 
1500 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1501 	if (!dev) {
1502 		retval = -ENOMEM;
1503 		goto error;
1504 	}
1505 
1506 	dev->devt = devt;
1507 	dev->class = class;
1508 	dev->parent = parent;
1509 	dev->release = device_create_release;
1510 	dev_set_drvdata(dev, drvdata);
1511 
1512 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1513 	if (retval)
1514 		goto error;
1515 
1516 	retval = device_register(dev);
1517 	if (retval)
1518 		goto error;
1519 
1520 	return dev;
1521 
1522 error:
1523 	put_device(dev);
1524 	return ERR_PTR(retval);
1525 }
1526 EXPORT_SYMBOL_GPL(device_create_vargs);
1527 
1528 /**
1529  * device_create - creates a device and registers it with sysfs
1530  * @class: pointer to the struct class that this device should be registered to
1531  * @parent: pointer to the parent struct device of this new device, if any
1532  * @devt: the dev_t for the char device to be added
1533  * @drvdata: the data to be added to the device for callbacks
1534  * @fmt: string for the device's name
1535  *
1536  * This function can be used by char device classes.  A struct device
1537  * will be created in sysfs, registered to the specified class.
1538  *
1539  * A "dev" file will be created, showing the dev_t for the device, if
1540  * the dev_t is not 0,0.
1541  * If a pointer to a parent struct device is passed in, the newly created
1542  * struct device will be a child of that device in sysfs.
1543  * The pointer to the struct device will be returned from the call.
1544  * Any further sysfs files that might be required can be created using this
1545  * pointer.
1546  *
1547  * Returns &struct device pointer on success, or ERR_PTR() on error.
1548  *
1549  * Note: the struct class passed to this function must have previously
1550  * been created with a call to class_create().
1551  */
1552 struct device *device_create(struct class *class, struct device *parent,
1553 			     dev_t devt, void *drvdata, const char *fmt, ...)
1554 {
1555 	va_list vargs;
1556 	struct device *dev;
1557 
1558 	va_start(vargs, fmt);
1559 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1560 	va_end(vargs);
1561 	return dev;
1562 }
1563 EXPORT_SYMBOL_GPL(device_create);
1564 
1565 static int __match_devt(struct device *dev, void *data)
1566 {
1567 	dev_t *devt = data;
1568 
1569 	return dev->devt == *devt;
1570 }
1571 
1572 /**
1573  * device_destroy - removes a device that was created with device_create()
1574  * @class: pointer to the struct class that this device was registered with
1575  * @devt: the dev_t of the device that was previously registered
1576  *
1577  * This call unregisters and cleans up a device that was created with a
1578  * call to device_create().
1579  */
1580 void device_destroy(struct class *class, dev_t devt)
1581 {
1582 	struct device *dev;
1583 
1584 	dev = class_find_device(class, NULL, &devt, __match_devt);
1585 	if (dev) {
1586 		put_device(dev);
1587 		device_unregister(dev);
1588 	}
1589 }
1590 EXPORT_SYMBOL_GPL(device_destroy);
1591 
1592 /**
1593  * device_rename - renames a device
1594  * @dev: the pointer to the struct device to be renamed
1595  * @new_name: the new name of the device
1596  *
1597  * It is the responsibility of the caller to provide mutual
1598  * exclusion between two different calls of device_rename
1599  * on the same device to ensure that new_name is valid and
1600  * won't conflict with other devices.
1601  */
1602 int device_rename(struct device *dev, char *new_name)
1603 {
1604 	char *old_class_name = NULL;
1605 	char *new_class_name = NULL;
1606 	char *old_device_name = NULL;
1607 	int error;
1608 
1609 	dev = get_device(dev);
1610 	if (!dev)
1611 		return -EINVAL;
1612 
1613 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1614 		 __func__, new_name);
1615 
1616 #ifdef CONFIG_SYSFS_DEPRECATED
1617 	if ((dev->class) && (dev->parent))
1618 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
1619 #endif
1620 
1621 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1622 	if (!old_device_name) {
1623 		error = -ENOMEM;
1624 		goto out;
1625 	}
1626 
1627 #ifndef CONFIG_SYSFS_DEPRECATED
1628 	if (dev->class) {
1629 		error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1630 			&dev->kobj, old_device_name, new_name);
1631 		if (error)
1632 			goto out;
1633 	}
1634 #endif
1635 	error = kobject_rename(&dev->kobj, new_name);
1636 	if (error)
1637 		goto out;
1638 
1639 #ifdef CONFIG_SYSFS_DEPRECATED
1640 	if (old_class_name) {
1641 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1642 		if (new_class_name) {
1643 			error = sysfs_rename_link(&dev->parent->kobj,
1644 						  &dev->kobj,
1645 						  old_class_name,
1646 						  new_class_name);
1647 		}
1648 	}
1649 #endif
1650 
1651 out:
1652 	put_device(dev);
1653 
1654 	kfree(new_class_name);
1655 	kfree(old_class_name);
1656 	kfree(old_device_name);
1657 
1658 	return error;
1659 }
1660 EXPORT_SYMBOL_GPL(device_rename);
1661 
1662 static int device_move_class_links(struct device *dev,
1663 				   struct device *old_parent,
1664 				   struct device *new_parent)
1665 {
1666 	int error = 0;
1667 #ifdef CONFIG_SYSFS_DEPRECATED
1668 	char *class_name;
1669 
1670 	class_name = make_class_name(dev->class->name, &dev->kobj);
1671 	if (!class_name) {
1672 		error = -ENOMEM;
1673 		goto out;
1674 	}
1675 	if (old_parent) {
1676 		sysfs_remove_link(&dev->kobj, "device");
1677 		sysfs_remove_link(&old_parent->kobj, class_name);
1678 	}
1679 	if (new_parent) {
1680 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1681 					  "device");
1682 		if (error)
1683 			goto out;
1684 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1685 					  class_name);
1686 		if (error)
1687 			sysfs_remove_link(&dev->kobj, "device");
1688 	} else
1689 		error = 0;
1690 out:
1691 	kfree(class_name);
1692 	return error;
1693 #else
1694 	if (old_parent)
1695 		sysfs_remove_link(&dev->kobj, "device");
1696 	if (new_parent)
1697 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1698 					  "device");
1699 	return error;
1700 #endif
1701 }
1702 
1703 /**
1704  * device_move - moves a device to a new parent
1705  * @dev: the pointer to the struct device to be moved
1706  * @new_parent: the new parent of the device (can by NULL)
1707  * @dpm_order: how to reorder the dpm_list
1708  */
1709 int device_move(struct device *dev, struct device *new_parent,
1710 		enum dpm_order dpm_order)
1711 {
1712 	int error;
1713 	struct device *old_parent;
1714 	struct kobject *new_parent_kobj;
1715 
1716 	dev = get_device(dev);
1717 	if (!dev)
1718 		return -EINVAL;
1719 
1720 	device_pm_lock();
1721 	new_parent = get_device(new_parent);
1722 	new_parent_kobj = get_device_parent(dev, new_parent);
1723 
1724 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1725 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1726 	error = kobject_move(&dev->kobj, new_parent_kobj);
1727 	if (error) {
1728 		cleanup_glue_dir(dev, new_parent_kobj);
1729 		put_device(new_parent);
1730 		goto out;
1731 	}
1732 	old_parent = dev->parent;
1733 	dev->parent = new_parent;
1734 	if (old_parent)
1735 		klist_remove(&dev->p->knode_parent);
1736 	if (new_parent) {
1737 		klist_add_tail(&dev->p->knode_parent,
1738 			       &new_parent->p->klist_children);
1739 		set_dev_node(dev, dev_to_node(new_parent));
1740 	}
1741 
1742 	if (!dev->class)
1743 		goto out_put;
1744 	error = device_move_class_links(dev, old_parent, new_parent);
1745 	if (error) {
1746 		/* We ignore errors on cleanup since we're hosed anyway... */
1747 		device_move_class_links(dev, new_parent, old_parent);
1748 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1749 			if (new_parent)
1750 				klist_remove(&dev->p->knode_parent);
1751 			dev->parent = old_parent;
1752 			if (old_parent) {
1753 				klist_add_tail(&dev->p->knode_parent,
1754 					       &old_parent->p->klist_children);
1755 				set_dev_node(dev, dev_to_node(old_parent));
1756 			}
1757 		}
1758 		cleanup_glue_dir(dev, new_parent_kobj);
1759 		put_device(new_parent);
1760 		goto out;
1761 	}
1762 	switch (dpm_order) {
1763 	case DPM_ORDER_NONE:
1764 		break;
1765 	case DPM_ORDER_DEV_AFTER_PARENT:
1766 		device_pm_move_after(dev, new_parent);
1767 		break;
1768 	case DPM_ORDER_PARENT_BEFORE_DEV:
1769 		device_pm_move_before(new_parent, dev);
1770 		break;
1771 	case DPM_ORDER_DEV_LAST:
1772 		device_pm_move_last(dev);
1773 		break;
1774 	}
1775 out_put:
1776 	put_device(old_parent);
1777 out:
1778 	device_pm_unlock();
1779 	put_device(dev);
1780 	return error;
1781 }
1782 EXPORT_SYMBOL_GPL(device_move);
1783 
1784 /**
1785  * device_shutdown - call ->shutdown() on each device to shutdown.
1786  */
1787 void device_shutdown(void)
1788 {
1789 	struct device *dev;
1790 
1791 	spin_lock(&devices_kset->list_lock);
1792 	/*
1793 	 * Walk the devices list backward, shutting down each in turn.
1794 	 * Beware that device unplug events may also start pulling
1795 	 * devices offline, even as the system is shutting down.
1796 	 */
1797 	while (!list_empty(&devices_kset->list)) {
1798 		dev = list_entry(devices_kset->list.prev, struct device,
1799 				kobj.entry);
1800 		get_device(dev);
1801 		/*
1802 		 * Make sure the device is off the kset list, in the
1803 		 * event that dev->*->shutdown() doesn't remove it.
1804 		 */
1805 		list_del_init(&dev->kobj.entry);
1806 		spin_unlock(&devices_kset->list_lock);
1807 
1808 		if (dev->bus && dev->bus->shutdown) {
1809 			dev_dbg(dev, "shutdown\n");
1810 			dev->bus->shutdown(dev);
1811 		} else if (dev->driver && dev->driver->shutdown) {
1812 			dev_dbg(dev, "shutdown\n");
1813 			dev->driver->shutdown(dev);
1814 		}
1815 		put_device(dev);
1816 
1817 		spin_lock(&devices_kset->list_lock);
1818 	}
1819 	spin_unlock(&devices_kset->list_lock);
1820 	async_synchronize_full();
1821 }
1822