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