aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/core.c')
-rw-r--r--drivers/base/core.c197
1 files changed, 155 insertions, 42 deletions
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 8c2cc2648f5a..61df508fa62b 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -109,6 +109,7 @@ static struct sysfs_ops dev_sysfs_ops = {
109static void device_release(struct kobject *kobj) 109static void device_release(struct kobject *kobj)
110{ 110{
111 struct device *dev = to_dev(kobj); 111 struct device *dev = to_dev(kobj);
112 struct device_private *p = dev->p;
112 113
113 if (dev->release) 114 if (dev->release)
114 dev->release(dev); 115 dev->release(dev);
@@ -119,7 +120,8 @@ static void device_release(struct kobject *kobj)
119 else 120 else
120 WARN(1, KERN_ERR "Device '%s' does not have a release() " 121 WARN(1, KERN_ERR "Device '%s' does not have a release() "
121 "function, it is broken and must be fixed.\n", 122 "function, it is broken and must be fixed.\n",
122 dev->bus_id); 123 dev_name(dev));
124 kfree(p);
123} 125}
124 126
125static struct kobj_type device_ktype = { 127static struct kobj_type device_ktype = {
@@ -209,7 +211,7 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
209 retval = dev->bus->uevent(dev, env); 211 retval = dev->bus->uevent(dev, env);
210 if (retval) 212 if (retval)
211 pr_debug("device: '%s': %s: bus uevent() returned %d\n", 213 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
212 dev->bus_id, __func__, retval); 214 dev_name(dev), __func__, retval);
213 } 215 }
214 216
215 /* have the class specific function add its stuff */ 217 /* have the class specific function add its stuff */
@@ -217,7 +219,7 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
217 retval = dev->class->dev_uevent(dev, env); 219 retval = dev->class->dev_uevent(dev, env);
218 if (retval) 220 if (retval)
219 pr_debug("device: '%s': %s: class uevent() " 221 pr_debug("device: '%s': %s: class uevent() "
220 "returned %d\n", dev->bus_id, 222 "returned %d\n", dev_name(dev),
221 __func__, retval); 223 __func__, retval);
222 } 224 }
223 225
@@ -226,7 +228,7 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
226 retval = dev->type->uevent(dev, env); 228 retval = dev->type->uevent(dev, env);
227 if (retval) 229 if (retval)
228 pr_debug("device: '%s': %s: dev_type uevent() " 230 pr_debug("device: '%s': %s: dev_type uevent() "
229 "returned %d\n", dev->bus_id, 231 "returned %d\n", dev_name(dev),
230 __func__, retval); 232 __func__, retval);
231 } 233 }
232 234
@@ -507,14 +509,16 @@ EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
507 509
508static void klist_children_get(struct klist_node *n) 510static void klist_children_get(struct klist_node *n)
509{ 511{
510 struct device *dev = container_of(n, struct device, knode_parent); 512 struct device_private *p = to_device_private_parent(n);
513 struct device *dev = p->device;
511 514
512 get_device(dev); 515 get_device(dev);
513} 516}
514 517
515static void klist_children_put(struct klist_node *n) 518static void klist_children_put(struct klist_node *n)
516{ 519{
517 struct device *dev = container_of(n, struct device, knode_parent); 520 struct device_private *p = to_device_private_parent(n);
521 struct device *dev = p->device;
518 522
519 put_device(dev); 523 put_device(dev);
520} 524}
@@ -536,9 +540,15 @@ static void klist_children_put(struct klist_node *n)
536 */ 540 */
537void device_initialize(struct device *dev) 541void device_initialize(struct device *dev)
538{ 542{
543 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
544 if (!dev->p) {
545 WARN_ON(1);
546 return;
547 }
548 dev->p->device = dev;
539 dev->kobj.kset = devices_kset; 549 dev->kobj.kset = devices_kset;
540 kobject_init(&dev->kobj, &device_ktype); 550 kobject_init(&dev->kobj, &device_ktype);
541 klist_init(&dev->klist_children, klist_children_get, 551 klist_init(&dev->p->klist_children, klist_children_get,
542 klist_children_put); 552 klist_children_put);
543 INIT_LIST_HEAD(&dev->dma_pools); 553 INIT_LIST_HEAD(&dev->dma_pools);
544 init_MUTEX(&dev->sem); 554 init_MUTEX(&dev->sem);
@@ -672,7 +682,7 @@ static int device_add_class_symlinks(struct device *dev)
672 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 682 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
673 device_is_not_partition(dev)) { 683 device_is_not_partition(dev)) {
674 error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 684 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
675 &dev->kobj, dev->bus_id); 685 &dev->kobj, dev_name(dev));
676 if (error) 686 if (error)
677 goto out_subsys; 687 goto out_subsys;
678 } 688 }
@@ -712,11 +722,11 @@ out_busid:
712 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 722 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
713 device_is_not_partition(dev)) 723 device_is_not_partition(dev))
714 sysfs_remove_link(&dev->class->p->class_subsys.kobj, 724 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
715 dev->bus_id); 725 dev_name(dev));
716#else 726#else
717 /* link in the class directory pointing to the device */ 727 /* link in the class directory pointing to the device */
718 error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 728 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
719 &dev->kobj, dev->bus_id); 729 &dev->kobj, dev_name(dev));
720 if (error) 730 if (error)
721 goto out_subsys; 731 goto out_subsys;
722 732
@@ -729,7 +739,7 @@ out_busid:
729 return 0; 739 return 0;
730 740
731out_busid: 741out_busid:
732 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev->bus_id); 742 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
733#endif 743#endif
734 744
735out_subsys: 745out_subsys:
@@ -758,12 +768,12 @@ static void device_remove_class_symlinks(struct device *dev)
758 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 768 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
759 device_is_not_partition(dev)) 769 device_is_not_partition(dev))
760 sysfs_remove_link(&dev->class->p->class_subsys.kobj, 770 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
761 dev->bus_id); 771 dev_name(dev));
762#else 772#else
763 if (dev->parent && device_is_not_partition(dev)) 773 if (dev->parent && device_is_not_partition(dev))
764 sysfs_remove_link(&dev->kobj, "device"); 774 sysfs_remove_link(&dev->kobj, "device");
765 775
766 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev->bus_id); 776 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
767#endif 777#endif
768 778
769 sysfs_remove_link(&dev->kobj, "subsystem"); 779 sysfs_remove_link(&dev->kobj, "subsystem");
@@ -866,7 +876,7 @@ int device_add(struct device *dev)
866 if (!strlen(dev->bus_id)) 876 if (!strlen(dev->bus_id))
867 goto done; 877 goto done;
868 878
869 pr_debug("device: '%s': %s\n", dev->bus_id, __func__); 879 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
870 880
871 parent = get_device(dev->parent); 881 parent = get_device(dev->parent);
872 setup_parent(dev, parent); 882 setup_parent(dev, parent);
@@ -876,7 +886,7 @@ int device_add(struct device *dev)
876 set_dev_node(dev, dev_to_node(parent)); 886 set_dev_node(dev, dev_to_node(parent));
877 887
878 /* first, register with generic layer. */ 888 /* first, register with generic layer. */
879 error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id); 889 error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
880 if (error) 890 if (error)
881 goto Error; 891 goto Error;
882 892
@@ -884,11 +894,6 @@ int device_add(struct device *dev)
884 if (platform_notify) 894 if (platform_notify)
885 platform_notify(dev); 895 platform_notify(dev);
886 896
887 /* notify clients of device entry (new way) */
888 if (dev->bus)
889 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
890 BUS_NOTIFY_ADD_DEVICE, dev);
891
892 error = device_create_file(dev, &uevent_attr); 897 error = device_create_file(dev, &uevent_attr);
893 if (error) 898 if (error)
894 goto attrError; 899 goto attrError;
@@ -916,10 +921,19 @@ int device_add(struct device *dev)
916 if (error) 921 if (error)
917 goto DPMError; 922 goto DPMError;
918 device_pm_add(dev); 923 device_pm_add(dev);
924
925 /* Notify clients of device addition. This call must come
926 * after dpm_sysf_add() and before kobject_uevent().
927 */
928 if (dev->bus)
929 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
930 BUS_NOTIFY_ADD_DEVICE, dev);
931
919 kobject_uevent(&dev->kobj, KOBJ_ADD); 932 kobject_uevent(&dev->kobj, KOBJ_ADD);
920 bus_attach_device(dev); 933 bus_attach_device(dev);
921 if (parent) 934 if (parent)
922 klist_add_tail(&dev->knode_parent, &parent->klist_children); 935 klist_add_tail(&dev->p->knode_parent,
936 &parent->p->klist_children);
923 937
924 if (dev->class) { 938 if (dev->class) {
925 mutex_lock(&dev->class->p->class_mutex); 939 mutex_lock(&dev->class->p->class_mutex);
@@ -940,9 +954,6 @@ done:
940 DPMError: 954 DPMError:
941 bus_remove_device(dev); 955 bus_remove_device(dev);
942 BusError: 956 BusError:
943 if (dev->bus)
944 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
945 BUS_NOTIFY_DEL_DEVICE, dev);
946 device_remove_attrs(dev); 957 device_remove_attrs(dev);
947 AttrsError: 958 AttrsError:
948 device_remove_class_symlinks(dev); 959 device_remove_class_symlinks(dev);
@@ -1027,10 +1038,16 @@ void device_del(struct device *dev)
1027 struct device *parent = dev->parent; 1038 struct device *parent = dev->parent;
1028 struct class_interface *class_intf; 1039 struct class_interface *class_intf;
1029 1040
1041 /* Notify clients of device removal. This call must come
1042 * before dpm_sysfs_remove().
1043 */
1044 if (dev->bus)
1045 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1046 BUS_NOTIFY_DEL_DEVICE, dev);
1030 device_pm_remove(dev); 1047 device_pm_remove(dev);
1031 dpm_sysfs_remove(dev); 1048 dpm_sysfs_remove(dev);
1032 if (parent) 1049 if (parent)
1033 klist_del(&dev->knode_parent); 1050 klist_del(&dev->p->knode_parent);
1034 if (MAJOR(dev->devt)) { 1051 if (MAJOR(dev->devt)) {
1035 device_remove_sys_dev_entry(dev); 1052 device_remove_sys_dev_entry(dev);
1036 device_remove_file(dev, &devt_attr); 1053 device_remove_file(dev, &devt_attr);
@@ -1064,9 +1081,6 @@ void device_del(struct device *dev)
1064 */ 1081 */
1065 if (platform_notify_remove) 1082 if (platform_notify_remove)
1066 platform_notify_remove(dev); 1083 platform_notify_remove(dev);
1067 if (dev->bus)
1068 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1069 BUS_NOTIFY_DEL_DEVICE, dev);
1070 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1084 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1071 cleanup_device_parent(dev); 1085 cleanup_device_parent(dev);
1072 kobject_del(&dev->kobj); 1086 kobject_del(&dev->kobj);
@@ -1086,7 +1100,7 @@ void device_del(struct device *dev)
1086 */ 1100 */
1087void device_unregister(struct device *dev) 1101void device_unregister(struct device *dev)
1088{ 1102{
1089 pr_debug("device: '%s': %s\n", dev->bus_id, __func__); 1103 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1090 device_del(dev); 1104 device_del(dev);
1091 put_device(dev); 1105 put_device(dev);
1092} 1106}
@@ -1094,7 +1108,14 @@ void device_unregister(struct device *dev)
1094static struct device *next_device(struct klist_iter *i) 1108static struct device *next_device(struct klist_iter *i)
1095{ 1109{
1096 struct klist_node *n = klist_next(i); 1110 struct klist_node *n = klist_next(i);
1097 return n ? container_of(n, struct device, knode_parent) : NULL; 1111 struct device *dev = NULL;
1112 struct device_private *p;
1113
1114 if (n) {
1115 p = to_device_private_parent(n);
1116 dev = p->device;
1117 }
1118 return dev;
1098} 1119}
1099 1120
1100/** 1121/**
@@ -1116,7 +1137,7 @@ int device_for_each_child(struct device *parent, void *data,
1116 struct device *child; 1137 struct device *child;
1117 int error = 0; 1138 int error = 0;
1118 1139
1119 klist_iter_init(&parent->klist_children, &i); 1140 klist_iter_init(&parent->p->klist_children, &i);
1120 while ((child = next_device(&i)) && !error) 1141 while ((child = next_device(&i)) && !error)
1121 error = fn(child, data); 1142 error = fn(child, data);
1122 klist_iter_exit(&i); 1143 klist_iter_exit(&i);
@@ -1147,7 +1168,7 @@ struct device *device_find_child(struct device *parent, void *data,
1147 if (!parent) 1168 if (!parent)
1148 return NULL; 1169 return NULL;
1149 1170
1150 klist_iter_init(&parent->klist_children, &i); 1171 klist_iter_init(&parent->p->klist_children, &i);
1151 while ((child = next_device(&i))) 1172 while ((child = next_device(&i)))
1152 if (match(child, data) && get_device(child)) 1173 if (match(child, data) && get_device(child))
1153 break; 1174 break;
@@ -1196,10 +1217,101 @@ EXPORT_SYMBOL_GPL(put_device);
1196EXPORT_SYMBOL_GPL(device_create_file); 1217EXPORT_SYMBOL_GPL(device_create_file);
1197EXPORT_SYMBOL_GPL(device_remove_file); 1218EXPORT_SYMBOL_GPL(device_remove_file);
1198 1219
1220struct root_device
1221{
1222 struct device dev;
1223 struct module *owner;
1224};
1225
1226#define to_root_device(dev) container_of(dev, struct root_device, dev)
1227
1228static void root_device_release(struct device *dev)
1229{
1230 kfree(to_root_device(dev));
1231}
1232
1233/**
1234 * __root_device_register - allocate and register a root device
1235 * @name: root device name
1236 * @owner: owner module of the root device, usually THIS_MODULE
1237 *
1238 * This function allocates a root device and registers it
1239 * using device_register(). In order to free the returned
1240 * device, use root_device_unregister().
1241 *
1242 * Root devices are dummy devices which allow other devices
1243 * to be grouped under /sys/devices. Use this function to
1244 * allocate a root device and then use it as the parent of
1245 * any device which should appear under /sys/devices/{name}
1246 *
1247 * The /sys/devices/{name} directory will also contain a
1248 * 'module' symlink which points to the @owner directory
1249 * in sysfs.
1250 *
1251 * Note: You probably want to use root_device_register().
1252 */
1253struct device *__root_device_register(const char *name, struct module *owner)
1254{
1255 struct root_device *root;
1256 int err = -ENOMEM;
1257
1258 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1259 if (!root)
1260 return ERR_PTR(err);
1261
1262 err = dev_set_name(&root->dev, name);
1263 if (err) {
1264 kfree(root);
1265 return ERR_PTR(err);
1266 }
1267
1268 root->dev.release = root_device_release;
1269
1270 err = device_register(&root->dev);
1271 if (err) {
1272 put_device(&root->dev);
1273 return ERR_PTR(err);
1274 }
1275
1276#ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1277 if (owner) {
1278 struct module_kobject *mk = &owner->mkobj;
1279
1280 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1281 if (err) {
1282 device_unregister(&root->dev);
1283 return ERR_PTR(err);
1284 }
1285 root->owner = owner;
1286 }
1287#endif
1288
1289 return &root->dev;
1290}
1291EXPORT_SYMBOL_GPL(__root_device_register);
1292
1293/**
1294 * root_device_unregister - unregister and free a root device
1295 * @root: device going away.
1296 *
1297 * This function unregisters and cleans up a device that was created by
1298 * root_device_register().
1299 */
1300void root_device_unregister(struct device *dev)
1301{
1302 struct root_device *root = to_root_device(dev);
1303
1304 if (root->owner)
1305 sysfs_remove_link(&root->dev.kobj, "module");
1306
1307 device_unregister(dev);
1308}
1309EXPORT_SYMBOL_GPL(root_device_unregister);
1310
1199 1311
1200static void device_create_release(struct device *dev) 1312static void device_create_release(struct device *dev)
1201{ 1313{
1202 pr_debug("device: '%s': %s\n", dev->bus_id, __func__); 1314 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1203 kfree(dev); 1315 kfree(dev);
1204} 1316}
1205 1317
@@ -1344,7 +1456,7 @@ int device_rename(struct device *dev, char *new_name)
1344 if (!dev) 1456 if (!dev)
1345 return -EINVAL; 1457 return -EINVAL;
1346 1458
1347 pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id, 1459 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1348 __func__, new_name); 1460 __func__, new_name);
1349 1461
1350#ifdef CONFIG_SYSFS_DEPRECATED 1462#ifdef CONFIG_SYSFS_DEPRECATED
@@ -1381,7 +1493,7 @@ int device_rename(struct device *dev, char *new_name)
1381#else 1493#else
1382 if (dev->class) { 1494 if (dev->class) {
1383 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj, 1495 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1384 &dev->kobj, dev->bus_id); 1496 &dev->kobj, dev_name(dev));
1385 if (error) 1497 if (error)
1386 goto out; 1498 goto out;
1387 sysfs_remove_link(&dev->class->p->class_subsys.kobj, 1499 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
@@ -1459,8 +1571,8 @@ int device_move(struct device *dev, struct device *new_parent)
1459 new_parent = get_device(new_parent); 1571 new_parent = get_device(new_parent);
1460 new_parent_kobj = get_device_parent(dev, new_parent); 1572 new_parent_kobj = get_device_parent(dev, new_parent);
1461 1573
1462 pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id, 1574 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1463 __func__, new_parent ? new_parent->bus_id : "<NULL>"); 1575 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1464 error = kobject_move(&dev->kobj, new_parent_kobj); 1576 error = kobject_move(&dev->kobj, new_parent_kobj);
1465 if (error) { 1577 if (error) {
1466 cleanup_glue_dir(dev, new_parent_kobj); 1578 cleanup_glue_dir(dev, new_parent_kobj);
@@ -1470,9 +1582,10 @@ int device_move(struct device *dev, struct device *new_parent)
1470 old_parent = dev->parent; 1582 old_parent = dev->parent;
1471 dev->parent = new_parent; 1583 dev->parent = new_parent;
1472 if (old_parent) 1584 if (old_parent)
1473 klist_remove(&dev->knode_parent); 1585 klist_remove(&dev->p->knode_parent);
1474 if (new_parent) { 1586 if (new_parent) {
1475 klist_add_tail(&dev->knode_parent, &new_parent->klist_children); 1587 klist_add_tail(&dev->p->knode_parent,
1588 &new_parent->p->klist_children);
1476 set_dev_node(dev, dev_to_node(new_parent)); 1589 set_dev_node(dev, dev_to_node(new_parent));
1477 } 1590 }
1478 1591
@@ -1484,11 +1597,11 @@ int device_move(struct device *dev, struct device *new_parent)
1484 device_move_class_links(dev, new_parent, old_parent); 1597 device_move_class_links(dev, new_parent, old_parent);
1485 if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1598 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1486 if (new_parent) 1599 if (new_parent)
1487 klist_remove(&dev->knode_parent); 1600 klist_remove(&dev->p->knode_parent);
1488 dev->parent = old_parent; 1601 dev->parent = old_parent;
1489 if (old_parent) { 1602 if (old_parent) {
1490 klist_add_tail(&dev->knode_parent, 1603 klist_add_tail(&dev->p->knode_parent,
1491 &old_parent->klist_children); 1604 &old_parent->p->klist_children);
1492 set_dev_node(dev, dev_to_node(old_parent)); 1605 set_dev_node(dev, dev_to_node(old_parent));
1493 } 1606 }
1494 } 1607 }