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