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.c109
1 files changed, 98 insertions, 11 deletions
diff --git a/drivers/base/core.c b/drivers/base/core.c
index be288b5e4180..ee0a51a3a41d 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -760,6 +760,22 @@ static void device_remove_class_symlinks(struct device *dev)
760} 760}
761 761
762/** 762/**
763 * dev_set_name - set a device name
764 * @dev: device
765 * @fmt: format string for the device's name
766 */
767int dev_set_name(struct device *dev, const char *fmt, ...)
768{
769 va_list vargs;
770
771 va_start(vargs, fmt);
772 vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
773 va_end(vargs);
774 return 0;
775}
776EXPORT_SYMBOL_GPL(dev_set_name);
777
778/**
763 * device_add - add device to device hierarchy. 779 * device_add - add device to device hierarchy.
764 * @dev: device. 780 * @dev: device.
765 * 781 *
@@ -1084,11 +1100,13 @@ static void device_create_release(struct device *dev)
1084} 1100}
1085 1101
1086/** 1102/**
1087 * device_create - creates a device and registers it with sysfs 1103 * device_create_vargs - creates a device and registers it with sysfs
1088 * @class: pointer to the struct class that this device should be registered to 1104 * @class: pointer to the struct class that this device should be registered to
1089 * @parent: pointer to the parent struct device of this new device, if any 1105 * @parent: pointer to the parent struct device of this new device, if any
1090 * @devt: the dev_t for the char device to be added 1106 * @devt: the dev_t for the char device to be added
1107 * @drvdata: the data to be added to the device for callbacks
1091 * @fmt: string for the device's name 1108 * @fmt: string for the device's name
1109 * @args: va_list for the device's name
1092 * 1110 *
1093 * This function can be used by char device classes. A struct device 1111 * This function can be used by char device classes. A struct device
1094 * will be created in sysfs, registered to the specified class. 1112 * will be created in sysfs, registered to the specified class.
@@ -1104,10 +1122,10 @@ static void device_create_release(struct device *dev)
1104 * Note: the struct class passed to this function must have previously 1122 * Note: the struct class passed to this function must have previously
1105 * been created with a call to class_create(). 1123 * been created with a call to class_create().
1106 */ 1124 */
1107struct device *device_create(struct class *class, struct device *parent, 1125struct device *device_create_vargs(struct class *class, struct device *parent,
1108 dev_t devt, const char *fmt, ...) 1126 dev_t devt, void *drvdata, const char *fmt,
1127 va_list args)
1109{ 1128{
1110 va_list args;
1111 struct device *dev = NULL; 1129 struct device *dev = NULL;
1112 int retval = -ENODEV; 1130 int retval = -ENODEV;
1113 1131
@@ -1124,10 +1142,9 @@ struct device *device_create(struct class *class, struct device *parent,
1124 dev->class = class; 1142 dev->class = class;
1125 dev->parent = parent; 1143 dev->parent = parent;
1126 dev->release = device_create_release; 1144 dev->release = device_create_release;
1145 dev_set_drvdata(dev, drvdata);
1127 1146
1128 va_start(args, fmt);
1129 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 1147 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1130 va_end(args);
1131 retval = device_register(dev); 1148 retval = device_register(dev);
1132 if (retval) 1149 if (retval)
1133 goto error; 1150 goto error;
@@ -1138,6 +1155,78 @@ error:
1138 kfree(dev); 1155 kfree(dev);
1139 return ERR_PTR(retval); 1156 return ERR_PTR(retval);
1140} 1157}
1158EXPORT_SYMBOL_GPL(device_create_vargs);
1159
1160/**
1161 * device_create_drvdata - creates a device and registers it with sysfs
1162 * @class: pointer to the struct class that this device should be registered to
1163 * @parent: pointer to the parent struct device of this new device, if any
1164 * @devt: the dev_t for the char device to be added
1165 * @drvdata: the data to be added to the device for callbacks
1166 * @fmt: string for the device's name
1167 *
1168 * This function can be used by char device classes. A struct device
1169 * will be created in sysfs, registered to the specified class.
1170 *
1171 * A "dev" file will be created, showing the dev_t for the device, if
1172 * the dev_t is not 0,0.
1173 * If a pointer to a parent struct device is passed in, the newly created
1174 * struct device will be a child of that device in sysfs.
1175 * The pointer to the struct device will be returned from the call.
1176 * Any further sysfs files that might be required can be created using this
1177 * pointer.
1178 *
1179 * Note: the struct class passed to this function must have previously
1180 * been created with a call to class_create().
1181 */
1182struct device *device_create_drvdata(struct class *class,
1183 struct device *parent,
1184 dev_t devt,
1185 void *drvdata,
1186 const char *fmt, ...)
1187{
1188 va_list vargs;
1189 struct device *dev;
1190
1191 va_start(vargs, fmt);
1192 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1193 va_end(vargs);
1194 return dev;
1195}
1196EXPORT_SYMBOL_GPL(device_create_drvdata);
1197
1198/**
1199 * device_create - creates a device and registers it with sysfs
1200 * @class: pointer to the struct class that this device should be registered to
1201 * @parent: pointer to the parent struct device of this new device, if any
1202 * @devt: the dev_t for the char device to be added
1203 * @fmt: string for the device's name
1204 *
1205 * This function can be used by char device classes. A struct device
1206 * will be created in sysfs, registered to the specified class.
1207 *
1208 * A "dev" file will be created, showing the dev_t for the device, if
1209 * the dev_t is not 0,0.
1210 * If a pointer to a parent struct device is passed in, the newly created
1211 * struct device will be a child of that device in sysfs.
1212 * The pointer to the struct device will be returned from the call.
1213 * Any further sysfs files that might be required can be created using this
1214 * pointer.
1215 *
1216 * Note: the struct class passed to this function must have previously
1217 * been created with a call to class_create().
1218 */
1219struct device *device_create(struct class *class, struct device *parent,
1220 dev_t devt, const char *fmt, ...)
1221{
1222 va_list vargs;
1223 struct device *dev;
1224
1225 va_start(vargs, fmt);
1226 dev = device_create_vargs(class, parent, devt, NULL, fmt, vargs);
1227 va_end(vargs);
1228 return dev;
1229}
1141EXPORT_SYMBOL_GPL(device_create); 1230EXPORT_SYMBOL_GPL(device_create);
1142 1231
1143static int __match_devt(struct device *dev, void *data) 1232static int __match_devt(struct device *dev, void *data)
@@ -1218,13 +1307,11 @@ int device_rename(struct device *dev, char *new_name)
1218 } 1307 }
1219#else 1308#else
1220 if (dev->class) { 1309 if (dev->class) {
1221 sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1222 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, 1310 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1223 dev->bus_id); 1311 dev->bus_id);
1224 if (error) { 1312 if (error)
1225 dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n", 1313 goto out;
1226 __func__, error); 1314 sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1227 }
1228 } 1315 }
1229#endif 1316#endif
1230 1317