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