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 |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/string.h> |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 18 | #include <linux/kdev_t.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/semaphore.h> |
| 21 | |
| 22 | #include "base.h" |
| 23 | #include "power/power.h" |
| 24 | |
| 25 | int (*platform_notify)(struct device * dev) = NULL; |
| 26 | int (*platform_notify_remove)(struct device * dev) = NULL; |
| 27 | |
| 28 | /* |
| 29 | * sysfs bindings for devices. |
| 30 | */ |
| 31 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 32 | /** |
| 33 | * dev_driver_string - Return a device's driver name, if at all possible |
| 34 | * @dev: struct device to get the name of |
| 35 | * |
| 36 | * Will return the device's driver's name if it is bound to a device. If |
| 37 | * the device is not bound to a device, it will return the name of the bus |
| 38 | * it is attached to. If it is not attached to a bus either, an empty |
| 39 | * string will be returned. |
| 40 | */ |
| 41 | const char *dev_driver_string(struct device *dev) |
| 42 | { |
| 43 | return dev->driver ? dev->driver->name : |
| 44 | (dev->bus ? dev->bus->name : ""); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(dev_driver_string); |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 49 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static ssize_t |
| 52 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 53 | { |
| 54 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 55 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 56 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 59 | ret = dev_attr->show(dev, dev_attr, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static ssize_t |
| 64 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
| 65 | const char * buf, size_t count) |
| 66 | { |
| 67 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 68 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 69 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 72 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static struct sysfs_ops dev_sysfs_ops = { |
| 77 | .show = dev_attr_show, |
| 78 | .store = dev_attr_store, |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * device_release - free device structure. |
| 84 | * @kobj: device's kobject. |
| 85 | * |
| 86 | * This is called once the reference count for the object |
| 87 | * reaches 0. We forward the call to the device's release |
| 88 | * method, which should handle actually freeing the structure. |
| 89 | */ |
| 90 | static void device_release(struct kobject * kobj) |
| 91 | { |
| 92 | struct device * dev = to_dev(kobj); |
| 93 | |
| 94 | if (dev->release) |
| 95 | dev->release(dev); |
| 96 | else { |
| 97 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
| 98 | "it is broken and must be fixed.\n", |
| 99 | dev->bus_id); |
| 100 | WARN_ON(1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static struct kobj_type ktype_device = { |
| 105 | .release = device_release, |
| 106 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 110 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | struct kobj_type *ktype = get_ktype(kobj); |
| 113 | |
| 114 | if (ktype == &ktype_device) { |
| 115 | struct device *dev = to_dev(kobj); |
| 116 | if (dev->bus) |
| 117 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 118 | if (dev->class) |
| 119 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 124 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct device *dev = to_dev(kobj); |
| 127 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 128 | if (dev->bus) |
| 129 | return dev->bus->name; |
| 130 | if (dev->class) |
| 131 | return dev->class->name; |
| 132 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 135 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int num_envp, char *buffer, int buffer_size) |
| 137 | { |
| 138 | struct device *dev = to_dev(kobj); |
| 139 | int i = 0; |
| 140 | int length = 0; |
| 141 | int retval = 0; |
| 142 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 143 | /* add the major/minor if present */ |
| 144 | if (MAJOR(dev->devt)) { |
| 145 | add_uevent_var(envp, num_envp, &i, |
| 146 | buffer, buffer_size, &length, |
| 147 | "MAJOR=%u", MAJOR(dev->devt)); |
| 148 | add_uevent_var(envp, num_envp, &i, |
| 149 | buffer, buffer_size, &length, |
| 150 | "MINOR=%u", MINOR(dev->devt)); |
| 151 | } |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* add bus name of physical device */ |
| 154 | if (dev->bus) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 155 | add_uevent_var(envp, num_envp, &i, |
| 156 | buffer, buffer_size, &length, |
| 157 | "PHYSDEVBUS=%s", dev->bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* add driver name of physical device */ |
| 160 | if (dev->driver) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 161 | add_uevent_var(envp, num_envp, &i, |
| 162 | buffer, buffer_size, &length, |
| 163 | "PHYSDEVDRIVER=%s", dev->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* terminate, set to next free slot, shrink available space */ |
| 166 | envp[i] = NULL; |
| 167 | envp = &envp[i]; |
| 168 | num_envp -= i; |
| 169 | buffer = &buffer[length]; |
| 170 | buffer_size -= length; |
| 171 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 172 | if (dev->bus && dev->bus->uevent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 174 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (retval) { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 176 | pr_debug ("%s - uevent() returned %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | __FUNCTION__, retval); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return retval; |
| 182 | } |
| 183 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 184 | static struct kset_uevent_ops device_uevent_ops = { |
| 185 | .filter = dev_uevent_filter, |
| 186 | .name = dev_uevent_name, |
| 187 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 190 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 191 | const char *buf, size_t count) |
| 192 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 193 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 194 | return count; |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 197 | static ssize_t show_dev(struct device *dev, struct device_attribute *attr, |
| 198 | char *buf) |
| 199 | { |
| 200 | return print_dev_t(buf, dev->devt); |
| 201 | } |
| 202 | |
Martin Waitz | 0863afb | 2006-01-09 20:53:55 -0800 | [diff] [blame] | 203 | /* |
| 204 | * devices_subsys - structure to be registered with kobject core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | */ |
| 206 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 207 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | /** |
| 211 | * device_create_file - create sysfs attribute file for device. |
| 212 | * @dev: device. |
| 213 | * @attr: device attribute descriptor. |
| 214 | */ |
| 215 | |
| 216 | int device_create_file(struct device * dev, struct device_attribute * attr) |
| 217 | { |
| 218 | int error = 0; |
| 219 | if (get_device(dev)) { |
| 220 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
| 221 | put_device(dev); |
| 222 | } |
| 223 | return error; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * device_remove_file - remove sysfs attribute file. |
| 228 | * @dev: device. |
| 229 | * @attr: device attribute descriptor. |
| 230 | */ |
| 231 | |
| 232 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
| 233 | { |
| 234 | if (get_device(dev)) { |
| 235 | sysfs_remove_file(&dev->kobj, &attr->attr); |
| 236 | put_device(dev); |
| 237 | } |
| 238 | } |
| 239 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 240 | static void klist_children_get(struct klist_node *n) |
| 241 | { |
| 242 | struct device *dev = container_of(n, struct device, knode_parent); |
| 243 | |
| 244 | get_device(dev); |
| 245 | } |
| 246 | |
| 247 | static void klist_children_put(struct klist_node *n) |
| 248 | { |
| 249 | struct device *dev = container_of(n, struct device, knode_parent); |
| 250 | |
| 251 | put_device(dev); |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * device_initialize - init device structure. |
| 257 | * @dev: device. |
| 258 | * |
| 259 | * This prepares the device for use by other layers, |
| 260 | * including adding it to the device hierarchy. |
| 261 | * It is the first half of device_register(), if called by |
| 262 | * that, though it can also be called separately, so one |
| 263 | * may use @dev's fields (e.g. the refcount). |
| 264 | */ |
| 265 | |
| 266 | void device_initialize(struct device *dev) |
| 267 | { |
| 268 | kobj_set_kset_s(dev, devices_subsys); |
| 269 | kobject_init(&dev->kobj); |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 270 | klist_init(&dev->klist_children, klist_children_get, |
| 271 | klist_children_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | INIT_LIST_HEAD(&dev->dma_pools); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 273 | INIT_LIST_HEAD(&dev->node); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 274 | init_MUTEX(&dev->sem); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 275 | device_init_wakeup(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * device_add - add device to device hierarchy. |
| 280 | * @dev: device. |
| 281 | * |
| 282 | * This is part 2 of device_register(), though may be called |
| 283 | * separately _iff_ device_initialize() has been called separately. |
| 284 | * |
| 285 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
| 286 | * to the global and sibling lists for the device, then |
| 287 | * adds it to the other relevant subsystems of the driver model. |
| 288 | */ |
| 289 | int device_add(struct device *dev) |
| 290 | { |
| 291 | struct device *parent = NULL; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 292 | char *class_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | int error = -EINVAL; |
| 294 | |
| 295 | dev = get_device(dev); |
| 296 | if (!dev || !strlen(dev->bus_id)) |
| 297 | goto Error; |
| 298 | |
| 299 | parent = get_device(dev->parent); |
| 300 | |
| 301 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
| 302 | |
| 303 | /* first, register with generic layer. */ |
| 304 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); |
| 305 | if (parent) |
| 306 | dev->kobj.parent = &parent->kobj; |
| 307 | |
| 308 | if ((error = kobject_add(&dev->kobj))) |
| 309 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 310 | |
| 311 | dev->uevent_attr.attr.name = "uevent"; |
| 312 | dev->uevent_attr.attr.mode = S_IWUSR; |
| 313 | if (dev->driver) |
| 314 | dev->uevent_attr.attr.owner = dev->driver->owner; |
| 315 | dev->uevent_attr.store = store_uevent; |
| 316 | device_create_file(dev, &dev->uevent_attr); |
| 317 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 318 | if (MAJOR(dev->devt)) { |
| 319 | struct device_attribute *attr; |
| 320 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 321 | if (!attr) { |
| 322 | error = -ENOMEM; |
| 323 | goto PMError; |
| 324 | } |
| 325 | attr->attr.name = "dev"; |
| 326 | attr->attr.mode = S_IRUGO; |
| 327 | if (dev->driver) |
| 328 | attr->attr.owner = dev->driver->owner; |
| 329 | attr->show = show_dev; |
| 330 | error = device_create_file(dev, attr); |
| 331 | if (error) { |
| 332 | kfree(attr); |
| 333 | goto attrError; |
| 334 | } |
| 335 | |
| 336 | dev->devt_attr = attr; |
| 337 | } |
| 338 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 339 | if (dev->class) { |
| 340 | sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, |
| 341 | "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 342 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, |
| 343 | dev->bus_id); |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 344 | |
| 345 | sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); |
| 346 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 347 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 348 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if ((error = device_pm_add(dev))) |
| 351 | goto PMError; |
| 352 | if ((error = bus_add_device(dev))) |
| 353 | goto BusError; |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 354 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 355 | bus_attach_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (parent) |
James Bottomley | d856f1e3 | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 357 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame^] | 359 | if (dev->class) { |
| 360 | /* tie the class to the device */ |
| 361 | down(&dev->class->sem); |
| 362 | list_add_tail(&dev->node, &dev->class->devices); |
| 363 | up(&dev->class->sem); |
| 364 | } |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | /* notify platform of device entry */ |
| 367 | if (platform_notify) |
| 368 | platform_notify(dev); |
| 369 | Done: |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 370 | kfree(class_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | put_device(dev); |
| 372 | return error; |
| 373 | BusError: |
| 374 | device_pm_remove(dev); |
| 375 | PMError: |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 376 | if (dev->devt_attr) { |
| 377 | device_remove_file(dev, dev->devt_attr); |
| 378 | kfree(dev->devt_attr); |
| 379 | } |
| 380 | attrError: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 381 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | kobject_del(&dev->kobj); |
| 383 | Error: |
| 384 | if (parent) |
| 385 | put_device(parent); |
| 386 | goto Done; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | /** |
| 391 | * device_register - register a device with the system. |
| 392 | * @dev: pointer to the device structure |
| 393 | * |
| 394 | * This happens in two clean steps - initialize the device |
| 395 | * and add it to the system. The two steps can be called |
| 396 | * separately, but this is the easiest and most common. |
| 397 | * I.e. you should only call the two helpers separately if |
| 398 | * have a clearly defined need to use and refcount the device |
| 399 | * before it is added to the hierarchy. |
| 400 | */ |
| 401 | |
| 402 | int device_register(struct device *dev) |
| 403 | { |
| 404 | device_initialize(dev); |
| 405 | return device_add(dev); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | /** |
| 410 | * get_device - increment reference count for device. |
| 411 | * @dev: device. |
| 412 | * |
| 413 | * This simply forwards the call to kobject_get(), though |
| 414 | * we do take care to provide for the case that we get a NULL |
| 415 | * pointer passed in. |
| 416 | */ |
| 417 | |
| 418 | struct device * get_device(struct device * dev) |
| 419 | { |
| 420 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /** |
| 425 | * put_device - decrement reference count. |
| 426 | * @dev: device in question. |
| 427 | */ |
| 428 | void put_device(struct device * dev) |
| 429 | { |
| 430 | if (dev) |
| 431 | kobject_put(&dev->kobj); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /** |
| 436 | * device_del - delete device from system. |
| 437 | * @dev: device. |
| 438 | * |
| 439 | * This is the first part of the device unregistration |
| 440 | * sequence. This removes the device from the lists we control |
| 441 | * from here, has it removed from the other driver model |
| 442 | * subsystems it was added to in device_add(), and removes it |
| 443 | * from the kobject hierarchy. |
| 444 | * |
| 445 | * NOTE: this should be called manually _iff_ device_add() was |
| 446 | * also called manually. |
| 447 | */ |
| 448 | |
| 449 | void device_del(struct device * dev) |
| 450 | { |
| 451 | struct device * parent = dev->parent; |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 452 | char *class_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (parent) |
Patrick Mochel | d62c0f9 | 2005-06-24 08:39:33 -0700 | [diff] [blame] | 455 | klist_del(&dev->knode_parent); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 456 | if (dev->devt_attr) |
| 457 | device_remove_file(dev, dev->devt_attr); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 458 | if (dev->class) { |
| 459 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 460 | sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); |
Greg Kroah-Hartman | e9a7d30 | 2006-06-20 13:59:20 -0700 | [diff] [blame] | 461 | class_name = make_class_name(dev->class->name, &dev->kobj); |
| 462 | sysfs_remove_link(&dev->kobj, "device"); |
| 463 | sysfs_remove_link(&dev->parent->kobj, class_name); |
| 464 | kfree(class_name); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame^] | 465 | down(&dev->class->sem); |
| 466 | list_del_init(&dev->node); |
| 467 | up(&dev->class->sem); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 468 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 469 | device_remove_file(dev, &dev->uevent_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* Notify the platform of the removal, in case they |
| 472 | * need to do anything... |
| 473 | */ |
| 474 | if (platform_notify_remove) |
| 475 | platform_notify_remove(dev); |
| 476 | bus_remove_device(dev); |
| 477 | device_pm_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 478 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | kobject_del(&dev->kobj); |
| 480 | if (parent) |
| 481 | put_device(parent); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * device_unregister - unregister device from system. |
| 486 | * @dev: device going away. |
| 487 | * |
| 488 | * We do this in two parts, like we do device_register(). First, |
| 489 | * we remove it from all the subsystems with device_del(), then |
| 490 | * we decrement the reference count via put_device(). If that |
| 491 | * is the final reference count, the device will be cleaned up |
| 492 | * via device_release() above. Otherwise, the structure will |
| 493 | * stick around until the final reference to the device is dropped. |
| 494 | */ |
| 495 | void device_unregister(struct device * dev) |
| 496 | { |
| 497 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
| 498 | device_del(dev); |
| 499 | put_device(dev); |
| 500 | } |
| 501 | |
| 502 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 503 | static struct device * next_device(struct klist_iter * i) |
| 504 | { |
| 505 | struct klist_node * n = klist_next(i); |
| 506 | return n ? container_of(n, struct device, knode_parent) : NULL; |
| 507 | } |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /** |
| 510 | * device_for_each_child - device child iterator. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 511 | * @parent: parent struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | * @data: data for the callback. |
| 513 | * @fn: function to be called for each device. |
| 514 | * |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 515 | * Iterate over @parent's child devices, and call @fn for each, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | * passing it @data. |
| 517 | * |
| 518 | * We check the return of @fn each time. If it returns anything |
| 519 | * other than 0, we break out and return that value. |
| 520 | */ |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 521 | int device_for_each_child(struct device * parent, void * data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | int (*fn)(struct device *, void *)) |
| 523 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 524 | struct klist_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct device * child; |
| 526 | int error = 0; |
| 527 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 528 | klist_iter_init(&parent->klist_children, &i); |
| 529 | while ((child = next_device(&i)) && !error) |
| 530 | error = fn(child, data); |
| 531 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return error; |
| 533 | } |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int __init devices_init(void) |
| 536 | { |
| 537 | return subsystem_register(&devices_subsys); |
| 538 | } |
| 539 | |
| 540 | EXPORT_SYMBOL_GPL(device_for_each_child); |
| 541 | |
| 542 | EXPORT_SYMBOL_GPL(device_initialize); |
| 543 | EXPORT_SYMBOL_GPL(device_add); |
| 544 | EXPORT_SYMBOL_GPL(device_register); |
| 545 | |
| 546 | EXPORT_SYMBOL_GPL(device_del); |
| 547 | EXPORT_SYMBOL_GPL(device_unregister); |
| 548 | EXPORT_SYMBOL_GPL(get_device); |
| 549 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | EXPORT_SYMBOL_GPL(device_create_file); |
| 552 | EXPORT_SYMBOL_GPL(device_remove_file); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 553 | |
| 554 | |
| 555 | static void device_create_release(struct device *dev) |
| 556 | { |
| 557 | pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); |
| 558 | kfree(dev); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * device_create - creates a device and registers it with sysfs |
| 563 | * @cs: pointer to the struct class that this device should be registered to. |
| 564 | * @parent: pointer to the parent struct device of this new device, if any. |
| 565 | * @dev: the dev_t for the char device to be added. |
| 566 | * @fmt: string for the class device's name |
| 567 | * |
| 568 | * This function can be used by char device classes. A struct |
| 569 | * device will be created in sysfs, registered to the specified |
| 570 | * class. |
| 571 | * A "dev" file will be created, showing the dev_t for the device, if |
| 572 | * the dev_t is not 0,0. |
| 573 | * If a pointer to a parent struct device is passed in, the newly |
| 574 | * created struct device will be a child of that device in sysfs. The |
| 575 | * pointer to the struct device will be returned from the call. Any |
| 576 | * further sysfs files that might be required can be created using this |
| 577 | * pointer. |
| 578 | * |
| 579 | * Note: the struct class passed to this function must have previously |
| 580 | * been created with a call to class_create(). |
| 581 | */ |
| 582 | struct device *device_create(struct class *class, struct device *parent, |
| 583 | dev_t devt, char *fmt, ...) |
| 584 | { |
| 585 | va_list args; |
| 586 | struct device *dev = NULL; |
| 587 | int retval = -ENODEV; |
| 588 | |
| 589 | if (class == NULL || IS_ERR(class)) |
| 590 | goto error; |
| 591 | if (parent == NULL) { |
| 592 | printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__); |
| 593 | goto error; |
| 594 | } |
| 595 | |
| 596 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 597 | if (!dev) { |
| 598 | retval = -ENOMEM; |
| 599 | goto error; |
| 600 | } |
| 601 | |
| 602 | dev->devt = devt; |
| 603 | dev->class = class; |
| 604 | dev->parent = parent; |
| 605 | dev->release = device_create_release; |
| 606 | |
| 607 | va_start(args, fmt); |
| 608 | vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); |
| 609 | va_end(args); |
| 610 | retval = device_register(dev); |
| 611 | if (retval) |
| 612 | goto error; |
| 613 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 614 | return dev; |
| 615 | |
| 616 | error: |
| 617 | kfree(dev); |
| 618 | return ERR_PTR(retval); |
| 619 | } |
| 620 | EXPORT_SYMBOL_GPL(device_create); |
| 621 | |
| 622 | /** |
| 623 | * device_destroy - removes a device that was created with device_create() |
| 624 | * @class: the pointer to the struct class that this device was registered * with. |
| 625 | * @dev: the dev_t of the device that was previously registered. |
| 626 | * |
| 627 | * This call unregisters and cleans up a class device that was created with a |
| 628 | * call to class_device_create() |
| 629 | */ |
| 630 | void device_destroy(struct class *class, dev_t devt) |
| 631 | { |
| 632 | struct device *dev = NULL; |
| 633 | struct device *dev_tmp; |
| 634 | |
| 635 | down(&class->sem); |
| 636 | list_for_each_entry(dev_tmp, &class->devices, node) { |
| 637 | if (dev_tmp->devt == devt) { |
| 638 | dev = dev_tmp; |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | up(&class->sem); |
| 643 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame^] | 644 | if (dev) |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 645 | device_unregister(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 646 | } |
| 647 | EXPORT_SYMBOL_GPL(device_destroy); |