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