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> |
| 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 | |
| 31 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 32 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | static ssize_t |
| 35 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 36 | { |
| 37 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 38 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 39 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 42 | ret = dev_attr->show(dev, dev_attr, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static ssize_t |
| 47 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
| 48 | const char * buf, size_t count) |
| 49 | { |
| 50 | struct device_attribute * dev_attr = to_dev_attr(attr); |
| 51 | struct device * dev = to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 52 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 55 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | static struct sysfs_ops dev_sysfs_ops = { |
| 60 | .show = dev_attr_show, |
| 61 | .store = dev_attr_store, |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * device_release - free device structure. |
| 67 | * @kobj: device's kobject. |
| 68 | * |
| 69 | * This is called once the reference count for the object |
| 70 | * reaches 0. We forward the call to the device's release |
| 71 | * method, which should handle actually freeing the structure. |
| 72 | */ |
| 73 | static void device_release(struct kobject * kobj) |
| 74 | { |
| 75 | struct device * dev = to_dev(kobj); |
| 76 | |
| 77 | if (dev->release) |
| 78 | dev->release(dev); |
| 79 | else { |
| 80 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
| 81 | "it is broken and must be fixed.\n", |
| 82 | dev->bus_id); |
| 83 | WARN_ON(1); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static struct kobj_type ktype_device = { |
| 88 | .release = device_release, |
| 89 | .sysfs_ops = &dev_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | |
| 93 | static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj) |
| 94 | { |
| 95 | struct kobj_type *ktype = get_ktype(kobj); |
| 96 | |
| 97 | if (ktype == &ktype_device) { |
| 98 | struct device *dev = to_dev(kobj); |
| 99 | if (dev->bus) |
| 100 | return 1; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Dmitry Torokhov | 419cab3 | 2005-04-26 02:32:54 -0500 | [diff] [blame] | 105 | static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | struct device *dev = to_dev(kobj); |
| 108 | |
| 109 | return dev->bus->name; |
| 110 | } |
| 111 | |
| 112 | static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp, |
| 113 | int num_envp, char *buffer, int buffer_size) |
| 114 | { |
| 115 | struct device *dev = to_dev(kobj); |
| 116 | int i = 0; |
| 117 | int length = 0; |
| 118 | int retval = 0; |
| 119 | |
| 120 | /* add bus name of physical device */ |
| 121 | if (dev->bus) |
| 122 | add_hotplug_env_var(envp, num_envp, &i, |
| 123 | buffer, buffer_size, &length, |
| 124 | "PHYSDEVBUS=%s", dev->bus->name); |
| 125 | |
| 126 | /* add driver name of physical device */ |
| 127 | if (dev->driver) |
| 128 | add_hotplug_env_var(envp, num_envp, &i, |
| 129 | buffer, buffer_size, &length, |
| 130 | "PHYSDEVDRIVER=%s", dev->driver->name); |
| 131 | |
| 132 | /* terminate, set to next free slot, shrink available space */ |
| 133 | envp[i] = NULL; |
| 134 | envp = &envp[i]; |
| 135 | num_envp -= i; |
| 136 | buffer = &buffer[length]; |
| 137 | buffer_size -= length; |
| 138 | |
Alexander Nyberg | 177a432 | 2005-02-26 13:38:51 +0100 | [diff] [blame] | 139 | if (dev->bus && dev->bus->hotplug) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* have the bus specific function add its stuff */ |
| 141 | retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size); |
| 142 | if (retval) { |
| 143 | pr_debug ("%s - hotplug() returned %d\n", |
| 144 | __FUNCTION__, retval); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return retval; |
| 149 | } |
| 150 | |
| 151 | static struct kset_hotplug_ops device_hotplug_ops = { |
| 152 | .filter = dev_hotplug_filter, |
| 153 | .name = dev_hotplug_name, |
| 154 | .hotplug = dev_hotplug, |
| 155 | }; |
| 156 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 157 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
| 158 | const char *buf, size_t count) |
| 159 | { |
| 160 | kobject_hotplug(&dev->kobj, KOBJ_ADD); |
| 161 | return count; |
| 162 | } |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /** |
| 165 | * device_subsys - structure to be registered with kobject core. |
| 166 | */ |
| 167 | |
| 168 | decl_subsys(devices, &ktype_device, &device_hotplug_ops); |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * device_create_file - create sysfs attribute file for device. |
| 173 | * @dev: device. |
| 174 | * @attr: device attribute descriptor. |
| 175 | */ |
| 176 | |
| 177 | int device_create_file(struct device * dev, struct device_attribute * attr) |
| 178 | { |
| 179 | int error = 0; |
| 180 | if (get_device(dev)) { |
| 181 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
| 182 | put_device(dev); |
| 183 | } |
| 184 | return error; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * device_remove_file - remove sysfs attribute file. |
| 189 | * @dev: device. |
| 190 | * @attr: device attribute descriptor. |
| 191 | */ |
| 192 | |
| 193 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
| 194 | { |
| 195 | if (get_device(dev)) { |
| 196 | sysfs_remove_file(&dev->kobj, &attr->attr); |
| 197 | put_device(dev); |
| 198 | } |
| 199 | } |
| 200 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 201 | static void klist_children_get(struct klist_node *n) |
| 202 | { |
| 203 | struct device *dev = container_of(n, struct device, knode_parent); |
| 204 | |
| 205 | get_device(dev); |
| 206 | } |
| 207 | |
| 208 | static void klist_children_put(struct klist_node *n) |
| 209 | { |
| 210 | struct device *dev = container_of(n, struct device, knode_parent); |
| 211 | |
| 212 | put_device(dev); |
| 213 | } |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * device_initialize - init device structure. |
| 218 | * @dev: device. |
| 219 | * |
| 220 | * This prepares the device for use by other layers, |
| 221 | * including adding it to the device hierarchy. |
| 222 | * It is the first half of device_register(), if called by |
| 223 | * that, though it can also be called separately, so one |
| 224 | * may use @dev's fields (e.g. the refcount). |
| 225 | */ |
| 226 | |
| 227 | void device_initialize(struct device *dev) |
| 228 | { |
| 229 | kobj_set_kset_s(dev, devices_subsys); |
| 230 | kobject_init(&dev->kobj); |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 231 | klist_init(&dev->klist_children, klist_children_get, |
| 232 | klist_children_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | INIT_LIST_HEAD(&dev->dma_pools); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 234 | init_MUTEX(&dev->sem); |
David Brownell | 0ac8524 | 2005-09-12 19:39:34 -0700 | [diff] [blame] | 235 | device_init_wakeup(dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * device_add - add device to device hierarchy. |
| 240 | * @dev: device. |
| 241 | * |
| 242 | * This is part 2 of device_register(), though may be called |
| 243 | * separately _iff_ device_initialize() has been called separately. |
| 244 | * |
| 245 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
| 246 | * to the global and sibling lists for the device, then |
| 247 | * adds it to the other relevant subsystems of the driver model. |
| 248 | */ |
| 249 | int device_add(struct device *dev) |
| 250 | { |
| 251 | struct device *parent = NULL; |
| 252 | int error = -EINVAL; |
| 253 | |
| 254 | dev = get_device(dev); |
| 255 | if (!dev || !strlen(dev->bus_id)) |
| 256 | goto Error; |
| 257 | |
| 258 | parent = get_device(dev->parent); |
| 259 | |
| 260 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
| 261 | |
| 262 | /* first, register with generic layer. */ |
| 263 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); |
| 264 | if (parent) |
| 265 | dev->kobj.parent = &parent->kobj; |
| 266 | |
| 267 | if ((error = kobject_add(&dev->kobj))) |
| 268 | goto Error; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 269 | |
| 270 | dev->uevent_attr.attr.name = "uevent"; |
| 271 | dev->uevent_attr.attr.mode = S_IWUSR; |
| 272 | if (dev->driver) |
| 273 | dev->uevent_attr.attr.owner = dev->driver->owner; |
| 274 | dev->uevent_attr.store = store_uevent; |
| 275 | device_create_file(dev, &dev->uevent_attr); |
| 276 | |
Kay Sievers | 187a1a9 | 2005-05-23 15:50:26 -0700 | [diff] [blame] | 277 | kobject_hotplug(&dev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if ((error = device_pm_add(dev))) |
| 279 | goto PMError; |
| 280 | if ((error = bus_add_device(dev))) |
| 281 | goto BusError; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (parent) |
James Bottomley | d856f1e3 | 2005-08-19 09:14:01 -0400 | [diff] [blame] | 283 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* notify platform of device entry */ |
| 286 | if (platform_notify) |
| 287 | platform_notify(dev); |
| 288 | Done: |
| 289 | put_device(dev); |
| 290 | return error; |
| 291 | BusError: |
| 292 | device_pm_remove(dev); |
| 293 | PMError: |
Kay Sievers | 187a1a9 | 2005-05-23 15:50:26 -0700 | [diff] [blame] | 294 | kobject_hotplug(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | kobject_del(&dev->kobj); |
| 296 | Error: |
| 297 | if (parent) |
| 298 | put_device(parent); |
| 299 | goto Done; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /** |
| 304 | * device_register - register a device with the system. |
| 305 | * @dev: pointer to the device structure |
| 306 | * |
| 307 | * This happens in two clean steps - initialize the device |
| 308 | * and add it to the system. The two steps can be called |
| 309 | * separately, but this is the easiest and most common. |
| 310 | * I.e. you should only call the two helpers separately if |
| 311 | * have a clearly defined need to use and refcount the device |
| 312 | * before it is added to the hierarchy. |
| 313 | */ |
| 314 | |
| 315 | int device_register(struct device *dev) |
| 316 | { |
| 317 | device_initialize(dev); |
| 318 | return device_add(dev); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * get_device - increment reference count for device. |
| 324 | * @dev: device. |
| 325 | * |
| 326 | * This simply forwards the call to kobject_get(), though |
| 327 | * we do take care to provide for the case that we get a NULL |
| 328 | * pointer passed in. |
| 329 | */ |
| 330 | |
| 331 | struct device * get_device(struct device * dev) |
| 332 | { |
| 333 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * put_device - decrement reference count. |
| 339 | * @dev: device in question. |
| 340 | */ |
| 341 | void put_device(struct device * dev) |
| 342 | { |
| 343 | if (dev) |
| 344 | kobject_put(&dev->kobj); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * device_del - delete device from system. |
| 350 | * @dev: device. |
| 351 | * |
| 352 | * This is the first part of the device unregistration |
| 353 | * sequence. This removes the device from the lists we control |
| 354 | * from here, has it removed from the other driver model |
| 355 | * subsystems it was added to in device_add(), and removes it |
| 356 | * from the kobject hierarchy. |
| 357 | * |
| 358 | * NOTE: this should be called manually _iff_ device_add() was |
| 359 | * also called manually. |
| 360 | */ |
| 361 | |
| 362 | void device_del(struct device * dev) |
| 363 | { |
| 364 | struct device * parent = dev->parent; |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (parent) |
Patrick Mochel | d62c0f9 | 2005-06-24 08:39:33 -0700 | [diff] [blame] | 367 | klist_del(&dev->knode_parent); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 368 | device_remove_file(dev, &dev->uevent_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | /* Notify the platform of the removal, in case they |
| 371 | * need to do anything... |
| 372 | */ |
| 373 | if (platform_notify_remove) |
| 374 | platform_notify_remove(dev); |
| 375 | bus_remove_device(dev); |
| 376 | device_pm_remove(dev); |
kay.sievers@vrfy.org | e57cd73 | 2005-04-18 21:57:36 -0700 | [diff] [blame] | 377 | kobject_hotplug(&dev->kobj, KOBJ_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | kobject_del(&dev->kobj); |
| 379 | if (parent) |
| 380 | put_device(parent); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * device_unregister - unregister device from system. |
| 385 | * @dev: device going away. |
| 386 | * |
| 387 | * We do this in two parts, like we do device_register(). First, |
| 388 | * we remove it from all the subsystems with device_del(), then |
| 389 | * we decrement the reference count via put_device(). If that |
| 390 | * is the final reference count, the device will be cleaned up |
| 391 | * via device_release() above. Otherwise, the structure will |
| 392 | * stick around until the final reference to the device is dropped. |
| 393 | */ |
| 394 | void device_unregister(struct device * dev) |
| 395 | { |
| 396 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
| 397 | device_del(dev); |
| 398 | put_device(dev); |
| 399 | } |
| 400 | |
| 401 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 402 | static struct device * next_device(struct klist_iter * i) |
| 403 | { |
| 404 | struct klist_node * n = klist_next(i); |
| 405 | return n ? container_of(n, struct device, knode_parent) : NULL; |
| 406 | } |
| 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | /** |
| 409 | * device_for_each_child - device child iterator. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame^] | 410 | * @parent: parent struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * @data: data for the callback. |
| 412 | * @fn: function to be called for each device. |
| 413 | * |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame^] | 414 | * Iterate over @parent's child devices, and call @fn for each, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | * passing it @data. |
| 416 | * |
| 417 | * We check the return of @fn each time. If it returns anything |
| 418 | * other than 0, we break out and return that value. |
| 419 | */ |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 420 | int device_for_each_child(struct device * parent, void * data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | int (*fn)(struct device *, void *)) |
| 422 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 423 | struct klist_iter i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | struct device * child; |
| 425 | int error = 0; |
| 426 | |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 427 | klist_iter_init(&parent->klist_children, &i); |
| 428 | while ((child = next_device(&i)) && !error) |
| 429 | error = fn(child, data); |
| 430 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return error; |
| 432 | } |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | int __init devices_init(void) |
| 435 | { |
| 436 | return subsystem_register(&devices_subsys); |
| 437 | } |
| 438 | |
| 439 | EXPORT_SYMBOL_GPL(device_for_each_child); |
| 440 | |
| 441 | EXPORT_SYMBOL_GPL(device_initialize); |
| 442 | EXPORT_SYMBOL_GPL(device_add); |
| 443 | EXPORT_SYMBOL_GPL(device_register); |
| 444 | |
| 445 | EXPORT_SYMBOL_GPL(device_del); |
| 446 | EXPORT_SYMBOL_GPL(device_unregister); |
| 447 | EXPORT_SYMBOL_GPL(get_device); |
| 448 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | EXPORT_SYMBOL_GPL(device_create_file); |
| 451 | EXPORT_SYMBOL_GPL(device_remove_file); |