Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 6 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * Copyright (c) 2006 Novell, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is released under the GPLv2 |
| 10 | * |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 19 | #include <linux/kdev_t.h> |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 20 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/semaphore.h> |
| 23 | |
| 24 | #include "base.h" |
| 25 | #include "power/power.h" |
| 26 | |
| 27 | int (*platform_notify)(struct device * dev) = NULL; |
| 28 | int (*platform_notify_remove)(struct device * dev) = NULL; |
| 29 | |
| 30 | /* |
| 31 | * sysfs bindings for devices. |
| 32 | */ |
| 33 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 34 | /** |
| 35 | * dev_driver_string - Return a device's driver name, if at all possible |
| 36 | * @dev: struct device to get the name of |
| 37 | * |
| 38 | * Will return the device's driver's name if it is bound to a device. If |
| 39 | * the device is not bound to a device, it will return the name of the bus |
| 40 | * it is attached to. If it is not attached to a bus either, an empty |
| 41 | * string will be returned. |
| 42 | */ |
| 43 | const char *dev_driver_string(struct device *dev) |
| 44 | { |
| 45 | return dev->driver ? dev->driver->name : |
| 46 | (dev->bus ? dev->bus->name : ""); |
| 47 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 48 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 51 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static ssize_t |
| 54 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 55 | { |
| 56 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 57 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 58 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 61 | ret = dev_attr->show(dev, dev_attr, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static ssize_t |
| 66 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
| 67 | const char * buf, size_t count) |
| 68 | { |
| 69 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 70 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 71 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 74 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static struct sysfs_ops dev_sysfs_ops = { |
| 79 | .show = dev_attr_show, |
| 80 | .store = dev_attr_store, |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * device_release - free device structure. |
| 86 | * @kobj: device's kobject. |
| 87 | * |
| 88 | * This is called once the reference count for the object |
| 89 | * reaches 0. We forward the call to the device's release |
| 90 | * method, which should handle actually freeing the structure. |
| 91 | */ |
| 92 | static void device_release(struct kobject * kobj) |
| 93 | { |
| 94 | struct device * dev = to_dev(kobj); |
| 95 | |
| 96 | if (dev->release) |
| 97 | dev->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 98 | else if (dev->class && dev->class->dev_release) |
| 99 | dev->class->dev_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | else { |
| 101 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
| 102 | "it is broken and must be fixed.\n", |
| 103 | dev->bus_id); |
| 104 | WARN_ON(1); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static struct kobj_type ktype_device = { |
| 109 | .release = device_release, |
| 110 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 114 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | struct kobj_type *ktype = get_ktype(kobj); |
| 117 | |
| 118 | if (ktype == &ktype_device) { |
| 119 | struct device *dev = to_dev(kobj); |
| 120 | if (dev->bus) |
| 121 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 122 | if (dev->class) |
| 123 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 128 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct device *dev = to_dev(kobj); |
| 131 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 132 | if (dev->bus) |
| 133 | return dev->bus->name; |
| 134 | if (dev->class) |
| 135 | return dev->class->name; |
| 136 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 139 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int num_envp, char *buffer, int buffer_size) |
| 141 | { |
| 142 | struct device *dev = to_dev(kobj); |
| 143 | int i = 0; |
| 144 | int length = 0; |
| 145 | int retval = 0; |
| 146 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 147 | /* add the major/minor if present */ |
| 148 | if (MAJOR(dev->devt)) { |
| 149 | add_uevent_var(envp, num_envp, &i, |
| 150 | buffer, buffer_size, &length, |
| 151 | "MAJOR=%u", MAJOR(dev->devt)); |
| 152 | add_uevent_var(envp, num_envp, &i, |
| 153 | buffer, buffer_size, &length, |
| 154 | "MINOR=%u", MINOR(dev->devt)); |
| 155 | } |
| 156 | |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 157 | /* add bus name (same as SUBSYSTEM, deprecated) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (dev->bus) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 159 | add_uevent_var(envp, num_envp, &i, |
| 160 | buffer, buffer_size, &length, |
| 161 | "PHYSDEVBUS=%s", dev->bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 163 | /* add driver name (PHYSDEV* values are deprecated)*/ |
| 164 | if (dev->driver) { |
| 165 | add_uevent_var(envp, num_envp, &i, |
| 166 | buffer, buffer_size, &length, |
| 167 | "DRIVER=%s", dev->driver->name); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 168 | add_uevent_var(envp, num_envp, &i, |
| 169 | buffer, buffer_size, &length, |
| 170 | "PHYSDEVDRIVER=%s", dev->driver->name); |
Kay Sievers | d81d9d6 | 2006-08-13 06:17:09 +0200 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* terminate, set to next free slot, shrink available space */ |
| 174 | envp[i] = NULL; |
| 175 | envp = &envp[i]; |
| 176 | num_envp -= i; |
| 177 | buffer = &buffer[length]; |
| 178 | buffer_size -= length; |
| 179 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 180 | if (dev->bus && dev->bus->uevent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 182 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (retval) { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 184 | pr_debug ("%s - uevent() returned %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | __FUNCTION__, retval); |
| 186 | } |
| 187 | } |
| 188 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 189 | if (dev->class && dev->class->dev_uevent) { |
| 190 | /* have the class specific function add its stuff */ |
| 191 | retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); |
| 192 | if (retval) { |
| 193 | pr_debug("%s - dev_uevent() returned %d\n", |
| 194 | __FUNCTION__, retval); |
| 195 | } |
| 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return retval; |
| 199 | } |
| 200 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 201 | static struct kset_uevent_ops device_uevent_ops = { |
| 202 | .filter = dev_uevent_filter, |
| 203 | .name = dev_uevent_name, |
| 204 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 207 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 208 | const char *buf, size_t count) |
| 209 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 210 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 211 | return count; |
| 212 | } |
| 213 | |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 214 | static int device_add_groups(struct device *dev) |
| 215 | { |
| 216 | int i; |
| 217 | int error = 0; |
| 218 | |
| 219 | if (dev->groups) { |
| 220 | for (i = 0; dev->groups[i]; i++) { |
| 221 | error = sysfs_create_group(&dev->kobj, dev->groups[i]); |
| 222 | if (error) { |
| 223 | while (--i >= 0) |
| 224 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 225 | goto out; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | out: |
| 230 | return error; |
| 231 | } |
| 232 | |
| 233 | static void device_remove_groups(struct device *dev) |
| 234 | { |
| 235 | int i; |
| 236 | if (dev->groups) { |
| 237 | for (i = 0; dev->groups[i]; i++) { |
| 238 | sysfs_remove_group(&dev->kobj, dev->groups[i]); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 243 | static int device_add_attrs(struct device *dev) |
| 244 | { |
| 245 | struct class *class = dev->class; |
| 246 | int error = 0; |
| 247 | int i; |
| 248 | |
| 249 | if (!class) |
| 250 | return 0; |
| 251 | |
| 252 | if (class->dev_attrs) { |
| 253 | for (i = 0; attr_name(class->dev_attrs[i]); i++) { |
| 254 | error = device_create_file(dev, &class->dev_attrs[i]); |
| 255 | if (error) |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | if (error) |
| 260 | while (--i >= 0) |
| 261 | device_remove_file(dev, &class->dev_attrs[i]); |
| 262 | return error; |
| 263 | } |
| 264 | |
| 265 | static void device_remove_attrs(struct device *dev) |
| 266 | { |
| 267 | struct class *class = dev->class; |
| 268 | int i; |
| 269 | |
| 270 | if (!class) |
| 271 | return; |
| 272 | |
| 273 | if (class->dev_attrs) { |
| 274 | for (i = 0; attr_name(class->dev_attrs[i]); i++) |
| 275 | device_remove_file(dev, &class->dev_attrs[i]); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 280 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
| 281 | char *buf) |
| 282 | { |
| 283 | return print_dev_t(buf, dev->devt); |
| 284 | } |
| 285 | |
Martin Waitz | 0863afb | 2006-01-09 20:53:55 -0800 | [diff] [blame] | 286 | /* |
| 287 | * devices_subsys - structure to be registered with kobject core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | */ |
| 289 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 290 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | |
| 293 | /** |
| 294 | * device_create_file - create sysfs attribute file for device. |
| 295 | * @dev: device. |
| 296 | * @attr: device attribute descriptor. |
| 297 | */ |
| 298 | |
| 299 | int device_create_file(struct device * dev, struct device_attribute * attr) |
| 300 | { |
| 301 | int error = 0; |
| 302 | if (get_device(dev)) { |
| 303 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
| 304 | put_device(dev); |
| 305 | } |
| 306 | return error; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * device_remove_file - remove sysfs attribute file. |
| 311 | * @dev: device. |
| 312 | * @attr: device attribute descriptor. |
| 313 | */ |
| 314 | |
| 315 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
| 316 | { |
| 317 | if (get_device(dev)) { |
| 318 | sysfs_remove_file(&dev->kobj, &attr->attr); |
| 319 | put_device(dev); |
| 320 | } |
| 321 | } |
| 322 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 323 | /** |
| 324 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 325 | * @dev: device. |
| 326 | * @attr: device binary attribute descriptor. |
| 327 | */ |
| 328 | int device_create_bin_file(struct device *dev, struct bin_attribute *attr) |
| 329 | { |
| 330 | int error = -EINVAL; |
| 331 | if (dev) |
| 332 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 333 | return error; |
| 334 | } |
| 335 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 336 | |
| 337 | /** |
| 338 | * device_remove_bin_file - remove sysfs binary attribute file |
| 339 | * @dev: device. |
| 340 | * @attr: device binary attribute descriptor. |
| 341 | */ |
| 342 | void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) |
| 343 | { |
| 344 | if (dev) |
| 345 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 346 | } |
| 347 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 348 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 349 | static void klist_children_get(struct klist_node *n) |
| 350 | { |
| 351 | struct device *dev = container_of(n, struct device, knode_parent); |
| 352 | |
| 353 | get_device(dev); |
| 354 | } |
| 355 | |
| 356 | static void klist_children_put(struct klist_node *n) |
| 357 | { |
| 358 | struct device *dev = container_of(n, struct device, knode_parent); |
| 359 | |
| 360 | put_device(dev); |
| 361 | } |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * device_initialize - init device structure. |
| 366 | * @dev: device. |
| 367 | * |
| 368 | * This prepares the device for use by other layers, |
| 369 | * including adding it to the device hierarchy. |
| 370 | * It is the first half of device_register(), if called by |
| 371 | * that, though it can also be called separately, so one |
| 372 | * may use @dev's fields (e.g. the refcount). |
| 373 | */ |
| 374 | |
| 375 | void device_initialize(struct device *dev) |
| 376 | { |
| 377 | kobj_set_kset_s(dev, devices_subsys); |
| 378 | kobject_init(&dev->kobj); |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 379 | klist_init(&dev->klist_children, klist_children_get, |
| 380 | klist_children_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | INIT_LIST_HEAD(&dev->dma_pools); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 382 | INIT_LIST_HEAD(&dev->node); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 383 | init_MUTEX(&dev->sem); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 384 | device_init_wakeup(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 387 | static int virtual_device_parent(struct device *dev) |
| 388 | { |
| 389 | if (!dev->class) |
| 390 | return -ENODEV; |
| 391 | |
| 392 | if (!dev->class->virtual_dir) { |
| 393 | static struct kobject *virtual_dir = NULL; |
| 394 | |
| 395 | if (!virtual_dir) |
| 396 | virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); |
| 397 | dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name); |
| 398 | } |
| 399 | |
| 400 | dev->kobj.parent = dev->class->virtual_dir; |
| 401 | return 0; |
| 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /** |
| 405 | * device_add - add device to device hierarchy. |
| 406 | * @dev: device. |
| 407 | * |
| 408 | * This is part 2 of device_register(), though may be called |
| 409 | * separately _iff_ device_initialize() has been called separately. |
| 410 | * |
| 411 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
| 412 | * to the global and sibling lists for the device, then |
| 413 | * adds it to the other relevant subsystems of the driver model. |
| 414 | */ |
| 415 | int device_add(struct device *dev) |
| 416 | { |
| 417 | struct device *parent = NULL; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 418 | char *class_name = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 419 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | int error = -EINVAL; |
| 421 | |
| 422 | dev = get_device(dev); |
| 423 | if (!dev || !strlen(dev->bus_id)) |
| 424 | goto Error; |
| 425 | |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 426 | /* if this is a class device, and has no parent, create one */ |
| 427 | if ((dev->class) && (dev->parent == NULL)) { |
| 428 | error = virtual_device_parent(dev); |
| 429 | if (error) |
| 430 | goto Error; |
| 431 | } |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | parent = get_device(dev->parent); |
| 434 | |
| 435 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
| 436 | |
| 437 | /* first, register with generic layer. */ |
| 438 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); |
| 439 | if (parent) |
| 440 | dev->kobj.parent = &parent->kobj; |
| 441 | |
| 442 | if ((error = kobject_add(&dev->kobj))) |
| 443 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 444 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 445 | /* notify platform of device entry */ |
| 446 | if (platform_notify) |
| 447 | platform_notify(dev); |
| 448 | |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 449 | /* notify clients of device entry (new way) */ |
| 450 | if (dev->bus) |
| 451 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 452 | BUS_NOTIFY_ADD_DEVICE, dev); |
| 453 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 454 | dev->uevent_attr.attr.name = "uevent"; |
| 455 | dev->uevent_attr.attr.mode = S_IWUSR; |
| 456 | if (dev->driver) |
| 457 | dev->uevent_attr.attr.owner = dev->driver->owner; |
| 458 | dev->uevent_attr.store = store_uevent; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 459 | error = device_create_file(dev, &dev->uevent_attr); |
| 460 | if (error) |
| 461 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 462 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 463 | if (MAJOR(dev->devt)) { |
| 464 | struct device_attribute *attr; |
| 465 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 466 | if (!attr) { |
| 467 | error = -ENOMEM; |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 468 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 469 | } |
| 470 | attr->attr.name = "dev"; |
| 471 | attr->attr.mode = S_IRUGO; |
| 472 | if (dev->driver) |
| 473 | attr->attr.owner = dev->driver->owner; |
| 474 | attr->show = show_dev; |
| 475 | error = device_create_file(dev, attr); |
| 476 | if (error) { |
| 477 | kfree(attr); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 478 | goto ueventattrError; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | dev->devt_attr = attr; |
| 482 | } |
| 483 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 484 | if (dev->class) { |
| 485 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, |
| 486 | "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 487 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 488 | dev->bus_id); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 489 | if (parent) { |
| 490 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); |
| 491 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 492 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); |
| 493 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 494 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 495 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 496 | if ((error = device_add_attrs(dev))) |
| 497 | goto AttrsError; |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 498 | if ((error = device_add_groups(dev))) |
| 499 | goto GroupError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if ((error = device_pm_add(dev))) |
| 501 | goto PMError; |
| 502 | if ((error = bus_add_device(dev))) |
| 503 | goto BusError; |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 504 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Alan Stern | f70fa62 | 2006-10-05 17:03:24 -0400 | [diff] [blame] | 505 | if ((error = bus_attach_device(dev))) |
| 506 | goto AttachError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (parent) |
James Bottomley | d856f1e | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 508 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 510 | if (dev->class) { |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 511 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 512 | /* tie the class to the device */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 513 | list_add_tail(&dev->node, &dev->class->devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 514 | |
| 515 | /* notify any interfaces that the device is here */ |
| 516 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 517 | if (class_intf->add_dev) |
| 518 | class_intf->add_dev(dev, class_intf); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 519 | up(&dev->class->sem); |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | Done: |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 522 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | put_device(dev); |
| 524 | return error; |
Alan Stern | f70fa62 | 2006-10-05 17:03:24 -0400 | [diff] [blame] | 525 | AttachError: |
| 526 | bus_remove_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | BusError: |
| 528 | device_pm_remove(dev); |
| 529 | PMError: |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 530 | if (dev->bus) |
| 531 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 532 | BUS_NOTIFY_DEL_DEVICE, dev); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 533 | device_remove_groups(dev); |
| 534 | GroupError: |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 535 | device_remove_attrs(dev); |
| 536 | AttrsError: |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 537 | if (dev->devt_attr) { |
| 538 | device_remove_file(dev, dev->devt_attr); |
| 539 | kfree(dev->devt_attr); |
| 540 | } |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 541 | ueventattrError: |
| 542 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 543 | attrError: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 544 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | kobject_del(&dev->kobj); |
| 546 | Error: |
| 547 | if (parent) |
| 548 | put_device(parent); |
| 549 | goto Done; |
| 550 | } |
| 551 | |
| 552 | |
| 553 | /** |
| 554 | * device_register - register a device with the system. |
| 555 | * @dev: pointer to the device structure |
| 556 | * |
| 557 | * This happens in two clean steps - initialize the device |
| 558 | * and add it to the system. The two steps can be called |
| 559 | * separately, but this is the easiest and most common. |
| 560 | * I.e. you should only call the two helpers separately if |
| 561 | * have a clearly defined need to use and refcount the device |
| 562 | * before it is added to the hierarchy. |
| 563 | */ |
| 564 | |
| 565 | int device_register(struct device *dev) |
| 566 | { |
| 567 | device_initialize(dev); |
| 568 | return device_add(dev); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | /** |
| 573 | * get_device - increment reference count for device. |
| 574 | * @dev: device. |
| 575 | * |
| 576 | * This simply forwards the call to kobject_get(), though |
| 577 | * we do take care to provide for the case that we get a NULL |
| 578 | * pointer passed in. |
| 579 | */ |
| 580 | |
| 581 | struct device * get_device(struct device * dev) |
| 582 | { |
| 583 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | /** |
| 588 | * put_device - decrement reference count. |
| 589 | * @dev: device in question. |
| 590 | */ |
| 591 | void put_device(struct device * dev) |
| 592 | { |
| 593 | if (dev) |
| 594 | kobject_put(&dev->kobj); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /** |
| 599 | * device_del - delete device from system. |
| 600 | * @dev: device. |
| 601 | * |
| 602 | * This is the first part of the device unregistration |
| 603 | * sequence. This removes the device from the lists we control |
| 604 | * from here, has it removed from the other driver model |
| 605 | * subsystems it was added to in device_add(), and removes it |
| 606 | * from the kobject hierarchy. |
| 607 | * |
| 608 | * NOTE: this should be called manually _iff_ device_add() was |
| 609 | * also called manually. |
| 610 | */ |
| 611 | |
| 612 | void device_del(struct device * dev) |
| 613 | { |
| 614 | struct device * parent = dev->parent; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 615 | char *class_name = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 616 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (parent) |
Patrick Mochel | d62c0f9 | 2005-06-24 08:39:33 -0700 | [diff] [blame] | 619 | klist_del(&dev->knode_parent); |
Catalin Marinas | 82189b9 | 2006-11-25 11:09:30 -0800 | [diff] [blame] | 620 | if (dev->devt_attr) { |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 621 | device_remove_file(dev, dev->devt_attr); |
Catalin Marinas | 82189b9 | 2006-11-25 11:09:30 -0800 | [diff] [blame] | 622 | kfree(dev->devt_attr); |
| 623 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 624 | if (dev->class) { |
| 625 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 626 | sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 627 | class_name = make_class_name(dev->class->name, &dev->kobj); |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 628 | if (parent) { |
| 629 | sysfs_remove_link(&dev->kobj, "device"); |
| 630 | sysfs_remove_link(&dev->parent->kobj, class_name); |
| 631 | } |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 632 | kfree(class_name); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 633 | down(&dev->class->sem); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 634 | /* notify any interfaces that the device is now gone */ |
| 635 | list_for_each_entry(class_intf, &dev->class->interfaces, node) |
| 636 | if (class_intf->remove_dev) |
| 637 | class_intf->remove_dev(dev, class_intf); |
| 638 | /* remove the device from the class list */ |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 639 | list_del_init(&dev->node); |
| 640 | up(&dev->class->sem); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 641 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 642 | device_remove_file(dev, &dev->uevent_attr); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 643 | device_remove_groups(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 644 | device_remove_attrs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | /* Notify the platform of the removal, in case they |
| 647 | * need to do anything... |
| 648 | */ |
| 649 | if (platform_notify_remove) |
| 650 | platform_notify_remove(dev); |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 651 | if (dev->bus) |
| 652 | blocking_notifier_call_chain(&dev->bus->bus_notifier, |
| 653 | BUS_NOTIFY_DEL_DEVICE, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | bus_remove_device(dev); |
| 655 | device_pm_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 656 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | kobject_del(&dev->kobj); |
| 658 | if (parent) |
| 659 | put_device(parent); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * device_unregister - unregister device from system. |
| 664 | * @dev: device going away. |
| 665 | * |
| 666 | * We do this in two parts, like we do device_register(). First, |
| 667 | * we remove it from all the subsystems with device_del(), then |
| 668 | * we decrement the reference count via put_device(). If that |
| 669 | * is the final reference count, the device will be cleaned up |
| 670 | * via device_release() above. Otherwise, the structure will |
| 671 | * stick around until the final reference to the device is dropped. |
| 672 | */ |
| 673 | void device_unregister(struct device * dev) |
| 674 | { |
| 675 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
| 676 | device_del(dev); |
| 677 | put_device(dev); |
| 678 | } |
| 679 | |
| 680 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 681 | static struct device * next_device(struct klist_iter * i) |
| 682 | { |
| 683 | struct klist_node * n = klist_next(i); |
| 684 | return n ? container_of(n, struct device, knode_parent) : NULL; |
| 685 | } |
| 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | /** |
| 688 | * device_for_each_child - device child iterator. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 689 | * @parent: parent struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | * @data: data for the callback. |
| 691 | * @fn: function to be called for each device. |
| 692 | * |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 693 | * Iterate over @parent's child devices, and call @fn for each, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | * passing it @data. |
| 695 | * |
| 696 | * We check the return of @fn each time. If it returns anything |
| 697 | * other than 0, we break out and return that value. |
| 698 | */ |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 699 | int device_for_each_child(struct device * parent, void * data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | int (*fn)(struct device *, void *)) |
| 701 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 702 | struct klist_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | struct device * child; |
| 704 | int error = 0; |
| 705 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 706 | klist_iter_init(&parent->klist_children, &i); |
| 707 | while ((child = next_device(&i)) && !error) |
| 708 | error = fn(child, data); |
| 709 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return error; |
| 711 | } |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | int __init devices_init(void) |
| 714 | { |
| 715 | return subsystem_register(&devices_subsys); |
| 716 | } |
| 717 | |
| 718 | EXPORT_SYMBOL_GPL(device_for_each_child); |
| 719 | |
| 720 | EXPORT_SYMBOL_GPL(device_initialize); |
| 721 | EXPORT_SYMBOL_GPL(device_add); |
| 722 | EXPORT_SYMBOL_GPL(device_register); |
| 723 | |
| 724 | EXPORT_SYMBOL_GPL(device_del); |
| 725 | EXPORT_SYMBOL_GPL(device_unregister); |
| 726 | EXPORT_SYMBOL_GPL(get_device); |
| 727 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | EXPORT_SYMBOL_GPL(device_create_file); |
| 730 | EXPORT_SYMBOL_GPL(device_remove_file); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 731 | |
| 732 | |
| 733 | static void device_create_release(struct device *dev) |
| 734 | { |
| 735 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); |
| 736 | kfree(dev); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * device_create - creates a device and registers it with sysfs |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 741 | * @class: pointer to the struct class that this device should be registered to |
| 742 | * @parent: pointer to the parent struct device of this new device, if any |
| 743 | * @devt: the dev_t for the char device to be added |
| 744 | * @fmt: string for the device's name |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 745 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 746 | * This function can be used by char device classes. A struct device |
| 747 | * will be created in sysfs, registered to the specified class. |
| 748 | * |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 749 | * A "dev" file will be created, showing the dev_t for the device, if |
| 750 | * the dev_t is not 0,0. |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 751 | * If a pointer to a parent struct device is passed in, the newly created |
| 752 | * struct device will be a child of that device in sysfs. |
| 753 | * The pointer to the struct device will be returned from the call. |
| 754 | * Any further sysfs files that might be required can be created using this |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 755 | * pointer. |
| 756 | * |
| 757 | * Note: the struct class passed to this function must have previously |
| 758 | * been created with a call to class_create(). |
| 759 | */ |
| 760 | struct device *device_create(struct class *class, struct device *parent, |
Greg Kroah-Hartman | 5cbe5f8 | 2006-08-10 01:19:19 -0400 | [diff] [blame] | 761 | dev_t devt, const char *fmt, ...) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 762 | { |
| 763 | va_list args; |
| 764 | struct device *dev = NULL; |
| 765 | int retval = -ENODEV; |
| 766 | |
| 767 | if (class == NULL || IS_ERR(class)) |
| 768 | goto error; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 769 | |
| 770 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 771 | if (!dev) { |
| 772 | retval = -ENOMEM; |
| 773 | goto error; |
| 774 | } |
| 775 | |
| 776 | dev->devt = devt; |
| 777 | dev->class = class; |
| 778 | dev->parent = parent; |
| 779 | dev->release = device_create_release; |
| 780 | |
| 781 | va_start(args, fmt); |
| 782 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); |
| 783 | va_end(args); |
| 784 | retval = device_register(dev); |
| 785 | if (retval) |
| 786 | goto error; |
| 787 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 788 | return dev; |
| 789 | |
| 790 | error: |
| 791 | kfree(dev); |
| 792 | return ERR_PTR(retval); |
| 793 | } |
| 794 | EXPORT_SYMBOL_GPL(device_create); |
| 795 | |
| 796 | /** |
| 797 | * device_destroy - removes a device that was created with device_create() |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 798 | * @class: pointer to the struct class that this device was registered with |
| 799 | * @devt: the dev_t of the device that was previously registered |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 800 | * |
Henrik Kretzschmar | 42734da | 2006-07-05 00:53:19 +0200 | [diff] [blame] | 801 | * This call unregisters and cleans up a device that was created with a |
| 802 | * call to device_create(). |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 803 | */ |
| 804 | void device_destroy(struct class *class, dev_t devt) |
| 805 | { |
| 806 | struct device *dev = NULL; |
| 807 | struct device *dev_tmp; |
| 808 | |
| 809 | down(&class->sem); |
| 810 | list_for_each_entry(dev_tmp, &class->devices, node) { |
| 811 | if (dev_tmp->devt == devt) { |
| 812 | dev = dev_tmp; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | up(&class->sem); |
| 817 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 818 | if (dev) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 819 | device_unregister(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 820 | } |
| 821 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 822 | |
| 823 | /** |
| 824 | * device_rename - renames a device |
| 825 | * @dev: the pointer to the struct device to be renamed |
| 826 | * @new_name: the new name of the device |
| 827 | */ |
| 828 | int device_rename(struct device *dev, char *new_name) |
| 829 | { |
| 830 | char *old_class_name = NULL; |
| 831 | char *new_class_name = NULL; |
| 832 | char *old_symlink_name = NULL; |
| 833 | int error; |
| 834 | |
| 835 | dev = get_device(dev); |
| 836 | if (!dev) |
| 837 | return -EINVAL; |
| 838 | |
| 839 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); |
| 840 | |
| 841 | if ((dev->class) && (dev->parent)) |
| 842 | old_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 843 | |
| 844 | if (dev->class) { |
| 845 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); |
Jesper Juhl | 952ab43 | 2006-09-28 23:56:01 +0200 | [diff] [blame] | 846 | if (!old_symlink_name) { |
| 847 | error = -ENOMEM; |
| 848 | goto out_free_old_class; |
| 849 | } |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 850 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); |
| 851 | } |
| 852 | |
| 853 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); |
| 854 | |
| 855 | error = kobject_rename(&dev->kobj, new_name); |
| 856 | |
| 857 | if (old_class_name) { |
| 858 | new_class_name = make_class_name(dev->class->name, &dev->kobj); |
| 859 | if (new_class_name) { |
| 860 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, |
| 861 | new_class_name); |
| 862 | sysfs_remove_link(&dev->parent->kobj, old_class_name); |
| 863 | } |
| 864 | } |
| 865 | if (dev->class) { |
| 866 | sysfs_remove_link(&dev->class->subsys.kset.kobj, |
| 867 | old_symlink_name); |
| 868 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 869 | dev->bus_id); |
| 870 | } |
| 871 | put_device(dev); |
| 872 | |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 873 | kfree(new_class_name); |
| 874 | kfree(old_symlink_name); |
Jesper Juhl | 952ab43 | 2006-09-28 23:56:01 +0200 | [diff] [blame] | 875 | out_free_old_class: |
| 876 | kfree(old_class_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 877 | |
| 878 | return error; |
| 879 | } |