]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/core.c
Remove devfs from MAINTAINERS
[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>
1da177e4
LT
21
22#include <asm/semaphore.h>
23
24#include "base.h"
25#include "power/power.h"
26
27int (*platform_notify)(struct device * dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL;
29
b446b60e
AM
30/*
31 * Detect the LANANA-assigned LOCAL/EXPERIMENTAL majors
32 */
33bool is_lanana_major(unsigned int major)
34{
35 if (major >= 60 && major <= 63)
36 return 1;
37 if (major >= 120 && major <= 127)
38 return 1;
39 if (major >= 240 && major <= 254)
40 return 1;
41 return 0;
42}
43
1da177e4
LT
44/*
45 * sysfs bindings for devices.
46 */
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 */
57const char *dev_driver_string(struct device *dev)
58{
59 return dev->driver ? dev->driver->name :
60 (dev->bus ? dev->bus->name : "");
61}
310a922d 62EXPORT_SYMBOL(dev_driver_string);
3e95637a 63
1da177e4
LT
64#define to_dev(obj) container_of(obj, struct device, kobj)
65#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
66
1da177e4
LT
67static ssize_t
68dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
69{
70 struct device_attribute * dev_attr = to_dev_attr(attr);
71 struct device * dev = to_dev(kobj);
4a0c20bf 72 ssize_t ret = -EIO;
1da177e4
LT
73
74 if (dev_attr->show)
54b6f35c 75 ret = dev_attr->show(dev, dev_attr, buf);
1da177e4
LT
76 return ret;
77}
78
79static ssize_t
80dev_attr_store(struct kobject * kobj, struct attribute * attr,
81 const char * buf, size_t count)
82{
83 struct device_attribute * dev_attr = to_dev_attr(attr);
84 struct device * dev = to_dev(kobj);
4a0c20bf 85 ssize_t ret = -EIO;
1da177e4
LT
86
87 if (dev_attr->store)
54b6f35c 88 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
89 return ret;
90}
91
92static struct sysfs_ops dev_sysfs_ops = {
93 .show = dev_attr_show,
94 .store = dev_attr_store,
95};
96
97
98/**
99 * device_release - free device structure.
100 * @kobj: device's kobject.
101 *
102 * This is called once the reference count for the object
103 * reaches 0. We forward the call to the device's release
104 * method, which should handle actually freeing the structure.
105 */
106static void device_release(struct kobject * kobj)
107{
108 struct device * dev = to_dev(kobj);
109
110 if (dev->release)
111 dev->release(dev);
f9f852df
KS
112 else if (dev->type && dev->type->release)
113 dev->type->release(dev);
2620efef
GKH
114 else if (dev->class && dev->class->dev_release)
115 dev->class->dev_release(dev);
1da177e4
LT
116 else {
117 printk(KERN_ERR "Device '%s' does not have a release() function, "
118 "it is broken and must be fixed.\n",
119 dev->bus_id);
120 WARN_ON(1);
121 }
122}
123
124static struct kobj_type ktype_device = {
125 .release = device_release,
126 .sysfs_ops = &dev_sysfs_ops,
1da177e4
LT
127};
128
129
312c004d 130static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
131{
132 struct kobj_type *ktype = get_ktype(kobj);
133
134 if (ktype == &ktype_device) {
135 struct device *dev = to_dev(kobj);
136 if (dev->bus)
137 return 1;
23681e47
GKH
138 if (dev->class)
139 return 1;
1da177e4
LT
140 }
141 return 0;
142}
143
312c004d 144static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4
LT
145{
146 struct device *dev = to_dev(kobj);
147
23681e47
GKH
148 if (dev->bus)
149 return dev->bus->name;
150 if (dev->class)
151 return dev->class->name;
152 return NULL;
1da177e4
LT
153}
154
312c004d 155static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1da177e4
LT
156 int num_envp, char *buffer, int buffer_size)
157{
158 struct device *dev = to_dev(kobj);
159 int i = 0;
160 int length = 0;
161 int retval = 0;
162
23681e47
GKH
163 /* add the major/minor if present */
164 if (MAJOR(dev->devt)) {
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "MAJOR=%u", MAJOR(dev->devt));
168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "MINOR=%u", MINOR(dev->devt));
171 }
172
239378f1 173 if (dev->driver)
d81d9d6b
KS
174 add_uevent_var(envp, num_envp, &i,
175 buffer, buffer_size, &length,
176 "DRIVER=%s", dev->driver->name);
239378f1 177
a87cb2ac 178#ifdef CONFIG_SYSFS_DEPRECATED
239378f1
KS
179 if (dev->class) {
180 struct device *parent = dev->parent;
181
182 /* find first bus device in parent chain */
183 while (parent && !parent->bus)
184 parent = parent->parent;
185 if (parent && parent->bus) {
186 const char *path;
187
188 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
189 add_uevent_var(envp, num_envp, &i,
190 buffer, buffer_size, &length,
191 "PHYSDEVPATH=%s", path);
192 kfree(path);
193
194 add_uevent_var(envp, num_envp, &i,
195 buffer, buffer_size, &length,
196 "PHYSDEVBUS=%s", parent->bus->name);
197
198 if (parent->driver)
199 add_uevent_var(envp, num_envp, &i,
200 buffer, buffer_size, &length,
201 "PHYSDEVDRIVER=%s", parent->driver->name);
202 }
203 } else if (dev->bus) {
312c004d
KS
204 add_uevent_var(envp, num_envp, &i,
205 buffer, buffer_size, &length,
239378f1
KS
206 "PHYSDEVBUS=%s", dev->bus->name);
207
208 if (dev->driver)
209 add_uevent_var(envp, num_envp, &i,
210 buffer, buffer_size, &length,
211 "PHYSDEVDRIVER=%s", dev->driver->name);
d81d9d6b 212 }
239378f1 213#endif
1da177e4
LT
214
215 /* terminate, set to next free slot, shrink available space */
216 envp[i] = NULL;
217 envp = &envp[i];
218 num_envp -= i;
219 buffer = &buffer[length];
220 buffer_size -= length;
221
312c004d 222 if (dev->bus && dev->bus->uevent) {
1da177e4 223 /* have the bus specific function add its stuff */
312c004d 224 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
f9f852df
KS
225 if (retval)
226 pr_debug ("%s: bus uevent() returned %d\n",
1da177e4 227 __FUNCTION__, retval);
1da177e4
LT
228 }
229
2620efef
GKH
230 if (dev->class && dev->class->dev_uevent) {
231 /* have the class specific function add its stuff */
232 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
f9f852df
KS
233 if (retval)
234 pr_debug("%s: class uevent() returned %d\n",
235 __FUNCTION__, retval);
236 }
237
238 if (dev->type && dev->type->uevent) {
239 /* have the device type specific fuction add its stuff */
240 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
241 if (retval)
242 pr_debug("%s: dev_type uevent() returned %d\n",
243 __FUNCTION__, retval);
2620efef
GKH
244 }
245
1da177e4
LT
246 return retval;
247}
248
312c004d
KS
249static struct kset_uevent_ops device_uevent_ops = {
250 .filter = dev_uevent_filter,
251 .name = dev_uevent_name,
252 .uevent = dev_uevent,
1da177e4
LT
253};
254
a7fd6706
KS
255static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
256 const char *buf, size_t count)
257{
312c004d 258 kobject_uevent(&dev->kobj, KOBJ_ADD);
a7fd6706
KS
259 return count;
260}
261
de0ff00d
GKH
262static int device_add_groups(struct device *dev)
263{
264 int i;
265 int error = 0;
266
267 if (dev->groups) {
268 for (i = 0; dev->groups[i]; i++) {
269 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
270 if (error) {
271 while (--i >= 0)
272 sysfs_remove_group(&dev->kobj, dev->groups[i]);
273 goto out;
274 }
275 }
276 }
277out:
278 return error;
279}
280
281static void device_remove_groups(struct device *dev)
282{
283 int i;
284 if (dev->groups) {
285 for (i = 0; dev->groups[i]; i++) {
286 sysfs_remove_group(&dev->kobj, dev->groups[i]);
287 }
288 }
289}
290
2620efef
GKH
291static int device_add_attrs(struct device *dev)
292{
293 struct class *class = dev->class;
f9f852df 294 struct device_type *type = dev->type;
2620efef
GKH
295 int error = 0;
296 int i;
297
f9f852df 298 if (class && class->dev_attrs) {
2620efef
GKH
299 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
300 error = device_create_file(dev, &class->dev_attrs[i]);
301 if (error)
302 break;
303 }
f9f852df
KS
304 if (error)
305 while (--i >= 0)
306 device_remove_file(dev, &class->dev_attrs[i]);
2620efef 307 }
f9f852df
KS
308
309 if (type && type->attrs) {
310 for (i = 0; attr_name(type->attrs[i]); i++) {
311 error = device_create_file(dev, &type->attrs[i]);
312 if (error)
313 break;
314 }
315 if (error)
316 while (--i >= 0)
317 device_remove_file(dev, &type->attrs[i]);
318 }
319
2620efef
GKH
320 return error;
321}
322
323static void device_remove_attrs(struct device *dev)
324{
325 struct class *class = dev->class;
f9f852df 326 struct device_type *type = dev->type;
2620efef
GKH
327 int i;
328
f9f852df 329 if (class && class->dev_attrs) {
2620efef
GKH
330 for (i = 0; attr_name(class->dev_attrs[i]); i++)
331 device_remove_file(dev, &class->dev_attrs[i]);
332 }
f9f852df
KS
333
334 if (type && type->attrs) {
335 for (i = 0; attr_name(type->attrs[i]); i++)
336 device_remove_file(dev, &type->attrs[i]);
337 }
2620efef
GKH
338}
339
340
23681e47
GKH
341static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
342 char *buf)
343{
344 return print_dev_t(buf, dev->devt);
345}
346
0863afb3
MW
347/*
348 * devices_subsys - structure to be registered with kobject core.
1da177e4
LT
349 */
350
312c004d 351decl_subsys(devices, &ktype_device, &device_uevent_ops);
1da177e4
LT
352
353
354/**
355 * device_create_file - create sysfs attribute file for device.
356 * @dev: device.
357 * @attr: device attribute descriptor.
358 */
359
360int device_create_file(struct device * dev, struct device_attribute * attr)
361{
362 int error = 0;
363 if (get_device(dev)) {
364 error = sysfs_create_file(&dev->kobj, &attr->attr);
365 put_device(dev);
366 }
367 return error;
368}
369
370/**
371 * device_remove_file - remove sysfs attribute file.
372 * @dev: device.
373 * @attr: device attribute descriptor.
374 */
375
376void device_remove_file(struct device * dev, struct device_attribute * attr)
377{
378 if (get_device(dev)) {
379 sysfs_remove_file(&dev->kobj, &attr->attr);
380 put_device(dev);
381 }
382}
383
2589f188
GKH
384/**
385 * device_create_bin_file - create sysfs binary attribute file for device.
386 * @dev: device.
387 * @attr: device binary attribute descriptor.
388 */
389int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
390{
391 int error = -EINVAL;
392 if (dev)
393 error = sysfs_create_bin_file(&dev->kobj, attr);
394 return error;
395}
396EXPORT_SYMBOL_GPL(device_create_bin_file);
397
398/**
399 * device_remove_bin_file - remove sysfs binary attribute file
400 * @dev: device.
401 * @attr: device binary attribute descriptor.
402 */
403void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
404{
405 if (dev)
406 sysfs_remove_bin_file(&dev->kobj, attr);
407}
408EXPORT_SYMBOL_GPL(device_remove_bin_file);
409
34bb61f9
JB
410static void klist_children_get(struct klist_node *n)
411{
412 struct device *dev = container_of(n, struct device, knode_parent);
413
414 get_device(dev);
415}
416
417static void klist_children_put(struct klist_node *n)
418{
419 struct device *dev = container_of(n, struct device, knode_parent);
420
421 put_device(dev);
422}
423
1da177e4
LT
424
425/**
426 * device_initialize - init device structure.
427 * @dev: device.
428 *
429 * This prepares the device for use by other layers,
430 * including adding it to the device hierarchy.
431 * It is the first half of device_register(), if called by
432 * that, though it can also be called separately, so one
433 * may use @dev's fields (e.g. the refcount).
434 */
435
436void device_initialize(struct device *dev)
437{
438 kobj_set_kset_s(dev, devices_subsys);
439 kobject_init(&dev->kobj);
34bb61f9
JB
440 klist_init(&dev->klist_children, klist_children_get,
441 klist_children_put);
1da177e4 442 INIT_LIST_HEAD(&dev->dma_pools);
23681e47 443 INIT_LIST_HEAD(&dev->node);
af70316a 444 init_MUTEX(&dev->sem);
9ac7849e
TH
445 spin_lock_init(&dev->devres_lock);
446 INIT_LIST_HEAD(&dev->devres_head);
0ac85241 447 device_init_wakeup(dev, 0);
87348136 448 set_dev_node(dev, -1);
1da177e4
LT
449}
450
40fa5422 451#ifdef CONFIG_SYSFS_DEPRECATED
c744aeae
CH
452static struct kobject * get_device_parent(struct device *dev,
453 struct device *parent)
40fa5422
GKH
454{
455 /* Set the parent to the class, not the parent device */
456 /* this keeps sysfs from having a symlink to make old udevs happy */
457 if (dev->class)
c744aeae 458 return &dev->class->subsys.kset.kobj;
40fa5422 459 else if (parent)
c744aeae 460 return &parent->kobj;
40fa5422 461
c744aeae 462 return NULL;
40fa5422
GKH
463}
464#else
c744aeae 465static struct kobject * virtual_device_parent(struct device *dev)
f0ee61a6
GKH
466{
467 if (!dev->class)
c744aeae 468 return ERR_PTR(-ENODEV);
f0ee61a6
GKH
469
470 if (!dev->class->virtual_dir) {
471 static struct kobject *virtual_dir = NULL;
472
473 if (!virtual_dir)
474 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
475 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
476 }
477
c744aeae 478 return dev->class->virtual_dir;
f0ee61a6
GKH
479}
480
c744aeae
CH
481static struct kobject * get_device_parent(struct device *dev,
482 struct device *parent)
40fa5422 483{
40fa5422
GKH
484 /* if this is a class device, and has no parent, create one */
485 if ((dev->class) && (parent == NULL)) {
c744aeae 486 return virtual_device_parent(dev);
40fa5422 487 } else if (parent)
c744aeae
CH
488 return &parent->kobj;
489 return NULL;
490}
40fa5422 491
c744aeae
CH
492#endif
493static int setup_parent(struct device *dev, struct device *parent)
494{
495 struct kobject *kobj;
496 kobj = get_device_parent(dev, parent);
497 if (IS_ERR(kobj))
498 return PTR_ERR(kobj);
499 if (kobj)
500 dev->kobj.parent = kobj;
40fa5422
GKH
501 return 0;
502}
40fa5422 503
1da177e4
LT
504/**
505 * device_add - add device to device hierarchy.
506 * @dev: device.
507 *
508 * This is part 2 of device_register(), though may be called
509 * separately _iff_ device_initialize() has been called separately.
510 *
511 * This adds it to the kobject hierarchy via kobject_add(), adds it
512 * to the global and sibling lists for the device, then
513 * adds it to the other relevant subsystems of the driver model.
514 */
515int device_add(struct device *dev)
516{
517 struct device *parent = NULL;
e9a7d305 518 char *class_name = NULL;
c47ed219 519 struct class_interface *class_intf;
1da177e4
LT
520 int error = -EINVAL;
521
522 dev = get_device(dev);
523 if (!dev || !strlen(dev->bus_id))
524 goto Error;
525
40fa5422 526 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
c205ef48 527
1da177e4
LT
528 parent = get_device(dev->parent);
529
40fa5422
GKH
530 error = setup_parent(dev, parent);
531 if (error)
532 goto Error;
1da177e4
LT
533
534 /* first, register with generic layer. */
535 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
40fa5422
GKH
536 error = kobject_add(&dev->kobj);
537 if (error)
1da177e4 538 goto Error;
a7fd6706 539
37022644
BW
540 /* notify platform of device entry */
541 if (platform_notify)
542 platform_notify(dev);
543
116af378
BH
544 /* notify clients of device entry (new way) */
545 if (dev->bus)
546 blocking_notifier_call_chain(&dev->bus->bus_notifier,
547 BUS_NOTIFY_ADD_DEVICE, dev);
548
a7fd6706
KS
549 dev->uevent_attr.attr.name = "uevent";
550 dev->uevent_attr.attr.mode = S_IWUSR;
551 if (dev->driver)
552 dev->uevent_attr.attr.owner = dev->driver->owner;
553 dev->uevent_attr.store = store_uevent;
a306eea4
CH
554 error = device_create_file(dev, &dev->uevent_attr);
555 if (error)
556 goto attrError;
a7fd6706 557
23681e47
GKH
558 if (MAJOR(dev->devt)) {
559 struct device_attribute *attr;
560 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
561 if (!attr) {
562 error = -ENOMEM;
a306eea4 563 goto ueventattrError;
23681e47
GKH
564 }
565 attr->attr.name = "dev";
566 attr->attr.mode = S_IRUGO;
567 if (dev->driver)
568 attr->attr.owner = dev->driver->owner;
569 attr->show = show_dev;
570 error = device_create_file(dev, attr);
571 if (error) {
572 kfree(attr);
a306eea4 573 goto ueventattrError;
23681e47
GKH
574 }
575
576 dev->devt_attr = attr;
577 }
578
b9d9c82b
KS
579 if (dev->class) {
580 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
581 "subsystem");
40fa5422
GKH
582 /* If this is not a "fake" compatible device, then create the
583 * symlink from the class to the device. */
584 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
585 sysfs_create_link(&dev->class->subsys.kset.kobj,
586 &dev->kobj, dev->bus_id);
64bb5d2c 587 if (parent) {
cb360bbf
CH
588 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
589 "device");
f7f3461d 590#ifdef CONFIG_SYSFS_DEPRECATED
cb360bbf
CH
591 class_name = make_class_name(dev->class->name,
592 &dev->kobj);
593 if (class_name)
594 sysfs_create_link(&dev->parent->kobj,
595 &dev->kobj, class_name);
99ef3ef8 596#endif
f7f3461d 597 }
b9d9c82b 598 }
23681e47 599
2620efef
GKH
600 if ((error = device_add_attrs(dev)))
601 goto AttrsError;
de0ff00d
GKH
602 if ((error = device_add_groups(dev)))
603 goto GroupError;
1da177e4
LT
604 if ((error = device_pm_add(dev)))
605 goto PMError;
606 if ((error = bus_add_device(dev)))
607 goto BusError;
b7a3e813
KS
608 if (!dev->uevent_suppress)
609 kobject_uevent(&dev->kobj, KOBJ_ADD);
f70fa629
AS
610 if ((error = bus_attach_device(dev)))
611 goto AttachError;
1da177e4 612 if (parent)
d856f1e3 613 klist_add_tail(&dev->knode_parent, &parent->klist_children);
1da177e4 614
5d9fd169 615 if (dev->class) {
5d9fd169 616 down(&dev->class->sem);
c47ed219 617 /* tie the class to the device */
5d9fd169 618 list_add_tail(&dev->node, &dev->class->devices);
c47ed219
GKH
619
620 /* notify any interfaces that the device is here */
621 list_for_each_entry(class_intf, &dev->class->interfaces, node)
622 if (class_intf->add_dev)
623 class_intf->add_dev(dev, class_intf);
5d9fd169
GKH
624 up(&dev->class->sem);
625 }
1da177e4 626 Done:
e9a7d305 627 kfree(class_name);
1da177e4
LT
628 put_device(dev);
629 return error;
f70fa629
AS
630 AttachError:
631 bus_remove_device(dev);
1da177e4
LT
632 BusError:
633 device_pm_remove(dev);
634 PMError:
116af378
BH
635 if (dev->bus)
636 blocking_notifier_call_chain(&dev->bus->bus_notifier,
637 BUS_NOTIFY_DEL_DEVICE, dev);
de0ff00d
GKH
638 device_remove_groups(dev);
639 GroupError:
82f0cf9b 640 device_remove_attrs(dev);
2620efef 641 AttrsError:
23681e47
GKH
642 if (dev->devt_attr) {
643 device_remove_file(dev, dev->devt_attr);
644 kfree(dev->devt_attr);
645 }
82f0cf9b
JS
646
647 if (dev->class) {
648 sysfs_remove_link(&dev->kobj, "subsystem");
649 /* If this is not a "fake" compatible device, remove the
650 * symlink from the class to the device. */
651 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
652 sysfs_remove_link(&dev->class->subsys.kset.kobj,
653 dev->bus_id);
82f0cf9b 654 if (parent) {
f7f3461d 655#ifdef CONFIG_SYSFS_DEPRECATED
82f0cf9b
JS
656 char *class_name = make_class_name(dev->class->name,
657 &dev->kobj);
658 if (class_name)
659 sysfs_remove_link(&dev->parent->kobj,
660 class_name);
661 kfree(class_name);
f7f3461d 662#endif
82f0cf9b
JS
663 sysfs_remove_link(&dev->kobj, "device");
664 }
82f0cf9b
JS
665
666 down(&dev->class->sem);
667 /* notify any interfaces that the device is now gone */
668 list_for_each_entry(class_intf, &dev->class->interfaces, node)
669 if (class_intf->remove_dev)
670 class_intf->remove_dev(dev, class_intf);
671 /* remove the device from the class list */
672 list_del_init(&dev->node);
673 up(&dev->class->sem);
674 }
a306eea4
CH
675 ueventattrError:
676 device_remove_file(dev, &dev->uevent_attr);
23681e47 677 attrError:
312c004d 678 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
679 kobject_del(&dev->kobj);
680 Error:
681 if (parent)
682 put_device(parent);
683 goto Done;
684}
685
686
687/**
688 * device_register - register a device with the system.
689 * @dev: pointer to the device structure
690 *
691 * This happens in two clean steps - initialize the device
692 * and add it to the system. The two steps can be called
693 * separately, but this is the easiest and most common.
694 * I.e. you should only call the two helpers separately if
695 * have a clearly defined need to use and refcount the device
696 * before it is added to the hierarchy.
697 */
698
699int device_register(struct device *dev)
700{
701 device_initialize(dev);
702 return device_add(dev);
703}
704
705
706/**
707 * get_device - increment reference count for device.
708 * @dev: device.
709 *
710 * This simply forwards the call to kobject_get(), though
711 * we do take care to provide for the case that we get a NULL
712 * pointer passed in.
713 */
714
715struct device * get_device(struct device * dev)
716{
717 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
718}
719
720
721/**
722 * put_device - decrement reference count.
723 * @dev: device in question.
724 */
725void put_device(struct device * dev)
726{
727 if (dev)
728 kobject_put(&dev->kobj);
729}
730
731
732/**
733 * device_del - delete device from system.
734 * @dev: device.
735 *
736 * This is the first part of the device unregistration
737 * sequence. This removes the device from the lists we control
738 * from here, has it removed from the other driver model
739 * subsystems it was added to in device_add(), and removes it
740 * from the kobject hierarchy.
741 *
742 * NOTE: this should be called manually _iff_ device_add() was
743 * also called manually.
744 */
745
746void device_del(struct device * dev)
747{
748 struct device * parent = dev->parent;
c47ed219 749 struct class_interface *class_intf;
1da177e4 750
1da177e4 751 if (parent)
d62c0f9f 752 klist_del(&dev->knode_parent);
82189b98 753 if (dev->devt_attr) {
23681e47 754 device_remove_file(dev, dev->devt_attr);
82189b98
CM
755 kfree(dev->devt_attr);
756 }
b9d9c82b
KS
757 if (dev->class) {
758 sysfs_remove_link(&dev->kobj, "subsystem");
40fa5422
GKH
759 /* If this is not a "fake" compatible device, remove the
760 * symlink from the class to the device. */
761 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
762 sysfs_remove_link(&dev->class->subsys.kset.kobj,
763 dev->bus_id);
64bb5d2c 764 if (parent) {
f7f3461d 765#ifdef CONFIG_SYSFS_DEPRECATED
99ef3ef8
KS
766 char *class_name = make_class_name(dev->class->name,
767 &dev->kobj);
cb360bbf
CH
768 if (class_name)
769 sysfs_remove_link(&dev->parent->kobj,
770 class_name);
99ef3ef8 771 kfree(class_name);
f7f3461d 772#endif
99ef3ef8 773 sysfs_remove_link(&dev->kobj, "device");
64bb5d2c 774 }
99ef3ef8 775
5d9fd169 776 down(&dev->class->sem);
c47ed219
GKH
777 /* notify any interfaces that the device is now gone */
778 list_for_each_entry(class_intf, &dev->class->interfaces, node)
779 if (class_intf->remove_dev)
780 class_intf->remove_dev(dev, class_intf);
781 /* remove the device from the class list */
5d9fd169
GKH
782 list_del_init(&dev->node);
783 up(&dev->class->sem);
b9d9c82b 784 }
a7fd6706 785 device_remove_file(dev, &dev->uevent_attr);
de0ff00d 786 device_remove_groups(dev);
2620efef 787 device_remove_attrs(dev);
28953533 788 bus_remove_device(dev);
1da177e4 789
2f8d16a9
TH
790 /*
791 * Some platform devices are driven without driver attached
792 * and managed resources may have been acquired. Make sure
793 * all resources are released.
794 */
795 devres_release_all(dev);
796
1da177e4
LT
797 /* Notify the platform of the removal, in case they
798 * need to do anything...
799 */
800 if (platform_notify_remove)
801 platform_notify_remove(dev);
116af378
BH
802 if (dev->bus)
803 blocking_notifier_call_chain(&dev->bus->bus_notifier,
804 BUS_NOTIFY_DEL_DEVICE, dev);
1da177e4 805 device_pm_remove(dev);
312c004d 806 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
807 kobject_del(&dev->kobj);
808 if (parent)
809 put_device(parent);
810}
811
812/**
813 * device_unregister - unregister device from system.
814 * @dev: device going away.
815 *
816 * We do this in two parts, like we do device_register(). First,
817 * we remove it from all the subsystems with device_del(), then
818 * we decrement the reference count via put_device(). If that
819 * is the final reference count, the device will be cleaned up
820 * via device_release() above. Otherwise, the structure will
821 * stick around until the final reference to the device is dropped.
822 */
823void device_unregister(struct device * dev)
824{
825 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
826 device_del(dev);
827 put_device(dev);
828}
829
830
36239577
PM
831static struct device * next_device(struct klist_iter * i)
832{
833 struct klist_node * n = klist_next(i);
834 return n ? container_of(n, struct device, knode_parent) : NULL;
835}
836
1da177e4
LT
837/**
838 * device_for_each_child - device child iterator.
c41455fb 839 * @parent: parent struct device.
1da177e4
LT
840 * @data: data for the callback.
841 * @fn: function to be called for each device.
842 *
c41455fb 843 * Iterate over @parent's child devices, and call @fn for each,
1da177e4
LT
844 * passing it @data.
845 *
846 * We check the return of @fn each time. If it returns anything
847 * other than 0, we break out and return that value.
848 */
36239577 849int device_for_each_child(struct device * parent, void * data,
1da177e4
LT
850 int (*fn)(struct device *, void *))
851{
36239577 852 struct klist_iter i;
1da177e4
LT
853 struct device * child;
854 int error = 0;
855
36239577
PM
856 klist_iter_init(&parent->klist_children, &i);
857 while ((child = next_device(&i)) && !error)
858 error = fn(child, data);
859 klist_iter_exit(&i);
1da177e4
LT
860 return error;
861}
862
5ab69981
CH
863/**
864 * device_find_child - device iterator for locating a particular device.
865 * @parent: parent struct device
866 * @data: Data to pass to match function
867 * @match: Callback function to check device
868 *
869 * This is similar to the device_for_each_child() function above, but it
870 * returns a reference to a device that is 'found' for later use, as
871 * determined by the @match callback.
872 *
873 * The callback should return 0 if the device doesn't match and non-zero
874 * if it does. If the callback returns non-zero and a reference to the
875 * current device can be obtained, this function will return to the caller
876 * and not iterate over any more devices.
877 */
878struct device * device_find_child(struct device *parent, void *data,
879 int (*match)(struct device *, void *))
880{
881 struct klist_iter i;
882 struct device *child;
883
884 if (!parent)
885 return NULL;
886
887 klist_iter_init(&parent->klist_children, &i);
888 while ((child = next_device(&i)))
889 if (match(child, data) && get_device(child))
890 break;
891 klist_iter_exit(&i);
892 return child;
893}
894
1da177e4
LT
895int __init devices_init(void)
896{
897 return subsystem_register(&devices_subsys);
898}
899
900EXPORT_SYMBOL_GPL(device_for_each_child);
5ab69981 901EXPORT_SYMBOL_GPL(device_find_child);
1da177e4
LT
902
903EXPORT_SYMBOL_GPL(device_initialize);
904EXPORT_SYMBOL_GPL(device_add);
905EXPORT_SYMBOL_GPL(device_register);
906
907EXPORT_SYMBOL_GPL(device_del);
908EXPORT_SYMBOL_GPL(device_unregister);
909EXPORT_SYMBOL_GPL(get_device);
910EXPORT_SYMBOL_GPL(put_device);
1da177e4
LT
911
912EXPORT_SYMBOL_GPL(device_create_file);
913EXPORT_SYMBOL_GPL(device_remove_file);
23681e47
GKH
914
915
916static void device_create_release(struct device *dev)
917{
918 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
919 kfree(dev);
920}
921
922/**
923 * device_create - creates a device and registers it with sysfs
42734daf
HK
924 * @class: pointer to the struct class that this device should be registered to
925 * @parent: pointer to the parent struct device of this new device, if any
926 * @devt: the dev_t for the char device to be added
927 * @fmt: string for the device's name
928 *
929 * This function can be used by char device classes. A struct device
930 * will be created in sysfs, registered to the specified class.
23681e47 931 *
23681e47
GKH
932 * A "dev" file will be created, showing the dev_t for the device, if
933 * the dev_t is not 0,0.
42734daf
HK
934 * If a pointer to a parent struct device is passed in, the newly created
935 * struct device will be a child of that device in sysfs.
936 * The pointer to the struct device will be returned from the call.
937 * Any further sysfs files that might be required can be created using this
23681e47
GKH
938 * pointer.
939 *
940 * Note: the struct class passed to this function must have previously
941 * been created with a call to class_create().
942 */
943struct device *device_create(struct class *class, struct device *parent,
5cbe5f8a 944 dev_t devt, const char *fmt, ...)
23681e47
GKH
945{
946 va_list args;
947 struct device *dev = NULL;
948 int retval = -ENODEV;
949
950 if (class == NULL || IS_ERR(class))
951 goto error;
23681e47
GKH
952
953 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
954 if (!dev) {
955 retval = -ENOMEM;
956 goto error;
957 }
958
959 dev->devt = devt;
960 dev->class = class;
961 dev->parent = parent;
962 dev->release = device_create_release;
963
964 va_start(args, fmt);
965 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
966 va_end(args);
967 retval = device_register(dev);
968 if (retval)
969 goto error;
970
23681e47
GKH
971 return dev;
972
973error:
974 kfree(dev);
975 return ERR_PTR(retval);
976}
977EXPORT_SYMBOL_GPL(device_create);
978
979/**
980 * device_destroy - removes a device that was created with device_create()
42734daf
HK
981 * @class: pointer to the struct class that this device was registered with
982 * @devt: the dev_t of the device that was previously registered
23681e47 983 *
42734daf
HK
984 * This call unregisters and cleans up a device that was created with a
985 * call to device_create().
23681e47
GKH
986 */
987void device_destroy(struct class *class, dev_t devt)
988{
989 struct device *dev = NULL;
990 struct device *dev_tmp;
991
992 down(&class->sem);
993 list_for_each_entry(dev_tmp, &class->devices, node) {
994 if (dev_tmp->devt == devt) {
995 dev = dev_tmp;
996 break;
997 }
998 }
999 up(&class->sem);
1000
5d9fd169 1001 if (dev)
23681e47 1002 device_unregister(dev);
23681e47
GKH
1003}
1004EXPORT_SYMBOL_GPL(device_destroy);
a2de48ca
GKH
1005
1006/**
1007 * device_rename - renames a device
1008 * @dev: the pointer to the struct device to be renamed
1009 * @new_name: the new name of the device
1010 */
1011int device_rename(struct device *dev, char *new_name)
1012{
1013 char *old_class_name = NULL;
1014 char *new_class_name = NULL;
1015 char *old_symlink_name = NULL;
1016 int error;
1017
1018 dev = get_device(dev);
1019 if (!dev)
1020 return -EINVAL;
1021
1022 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1023
99ef3ef8 1024#ifdef CONFIG_SYSFS_DEPRECATED
a2de48ca
GKH
1025 if ((dev->class) && (dev->parent))
1026 old_class_name = make_class_name(dev->class->name, &dev->kobj);
99ef3ef8 1027#endif
a2de48ca
GKH
1028
1029 if (dev->class) {
1030 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
952ab431
JJ
1031 if (!old_symlink_name) {
1032 error = -ENOMEM;
1033 goto out_free_old_class;
1034 }
a2de48ca
GKH
1035 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1036 }
1037
1038 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1039
1040 error = kobject_rename(&dev->kobj, new_name);
1041
99ef3ef8 1042#ifdef CONFIG_SYSFS_DEPRECATED
a2de48ca
GKH
1043 if (old_class_name) {
1044 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1045 if (new_class_name) {
1046 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1047 new_class_name);
1048 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1049 }
1050 }
99ef3ef8
KS
1051#endif
1052
a2de48ca
GKH
1053 if (dev->class) {
1054 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1055 old_symlink_name);
1056 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1057 dev->bus_id);
1058 }
1059 put_device(dev);
1060
a2de48ca
GKH
1061 kfree(new_class_name);
1062 kfree(old_symlink_name);
952ab431
JJ
1063 out_free_old_class:
1064 kfree(old_class_name);
a2de48ca
GKH
1065
1066 return error;
1067}
8a82472f
CH
1068
1069
1070static int device_move_class_links(struct device *dev,
1071 struct device *old_parent,
1072 struct device *new_parent)
1073{
f7f3461d 1074 int error = 0;
8a82472f 1075#ifdef CONFIG_SYSFS_DEPRECATED
8a82472f
CH
1076 char *class_name;
1077
1078 class_name = make_class_name(dev->class->name, &dev->kobj);
1079 if (!class_name) {
cb360bbf 1080 error = -ENOMEM;
8a82472f
CH
1081 goto out;
1082 }
1083 if (old_parent) {
1084 sysfs_remove_link(&dev->kobj, "device");
1085 sysfs_remove_link(&old_parent->kobj, class_name);
1086 }
c744aeae
CH
1087 if (new_parent) {
1088 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1089 "device");
1090 if (error)
1091 goto out;
1092 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1093 class_name);
1094 if (error)
1095 sysfs_remove_link(&dev->kobj, "device");
1096 }
1097 else
1098 error = 0;
8a82472f
CH
1099out:
1100 kfree(class_name);
1101 return error;
1102#else
f7f3461d
GKH
1103 if (old_parent)
1104 sysfs_remove_link(&dev->kobj, "device");
1105 if (new_parent)
1106 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1107 "device");
1108 return error;
8a82472f
CH
1109#endif
1110}
1111
1112/**
1113 * device_move - moves a device to a new parent
1114 * @dev: the pointer to the struct device to be moved
c744aeae 1115 * @new_parent: the new parent of the device (can by NULL)
8a82472f
CH
1116 */
1117int device_move(struct device *dev, struct device *new_parent)
1118{
1119 int error;
1120 struct device *old_parent;
c744aeae 1121 struct kobject *new_parent_kobj;
8a82472f
CH
1122
1123 dev = get_device(dev);
1124 if (!dev)
1125 return -EINVAL;
1126
8a82472f 1127 new_parent = get_device(new_parent);
c744aeae
CH
1128 new_parent_kobj = get_device_parent (dev, new_parent);
1129 if (IS_ERR(new_parent_kobj)) {
1130 error = PTR_ERR(new_parent_kobj);
1131 put_device(new_parent);
8a82472f
CH
1132 goto out;
1133 }
1134 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
c744aeae
CH
1135 new_parent ? new_parent->bus_id : "<NULL>");
1136 error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f
CH
1137 if (error) {
1138 put_device(new_parent);
1139 goto out;
1140 }
1141 old_parent = dev->parent;
1142 dev->parent = new_parent;
1143 if (old_parent)
acf02d23 1144 klist_remove(&dev->knode_parent);
c744aeae
CH
1145 if (new_parent)
1146 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
8a82472f
CH
1147 if (!dev->class)
1148 goto out_put;
1149 error = device_move_class_links(dev, old_parent, new_parent);
1150 if (error) {
1151 /* We ignore errors on cleanup since we're hosed anyway... */
1152 device_move_class_links(dev, new_parent, old_parent);
1153 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
c744aeae
CH
1154 if (new_parent)
1155 klist_remove(&dev->knode_parent);
8a82472f
CH
1156 if (old_parent)
1157 klist_add_tail(&dev->knode_parent,
1158 &old_parent->klist_children);
1159 }
1160 put_device(new_parent);
1161 goto out;
1162 }
1163out_put:
1164 put_device(old_parent);
1165out:
1166 put_device(dev);
1167 return error;
1168}
1169
1170EXPORT_SYMBOL_GPL(device_move);