blob: a979bc3f49a99d297135d4e9143b53063a488d7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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-Hartman23681e42006-06-14 12:14:34 -070018#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/semaphore.h>
21
22#include "base.h"
23#include "power/power.h"
24
25int (*platform_notify)(struct device * dev) = NULL;
26int (*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 Torvalds1da177e2005-04-16 15:20:36 -070035static ssize_t
36dev_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 Torokhov4a0c20b2005-04-29 01:23:47 -050040 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040043 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return ret;
45}
46
47static ssize_t
48dev_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 Torokhov4a0c20b2005-04-29 01:23:47 -050053 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040056 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ret;
58}
59
60static 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 */
74static 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
88static struct kobj_type ktype_device = {
89 .release = device_release,
90 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93
Kay Sievers312c0042005-11-16 09:00:00 +010094static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
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-Hartman23681e42006-06-14 12:14:34 -0700102 if (dev->class)
103 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 return 0;
106}
107
Kay Sievers312c0042005-11-16 09:00:00 +0100108static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct device *dev = to_dev(kobj);
111
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700112 if (dev->bus)
113 return dev->bus->name;
114 if (dev->class)
115 return dev->class->name;
116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Kay Sievers312c0042005-11-16 09:00:00 +0100119static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 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-Hartman23681e42006-06-14 12:14:34 -0700127 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700137 /* add bus name of physical device */
138 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100139 add_uevent_var(envp, num_envp, &i,
140 buffer, buffer_size, &length,
141 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* add driver name of physical device */
144 if (dev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100145 add_uevent_var(envp, num_envp, &i,
146 buffer, buffer_size, &length,
147 "PHYSDEVDRIVER=%s", dev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
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 Sievers312c0042005-11-16 09:00:00 +0100156 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100158 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100160 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 __FUNCTION__, retval);
162 }
163 }
164
165 return retval;
166}
167
Kay Sievers312c0042005-11-16 09:00:00 +0100168static struct kset_uevent_ops device_uevent_ops = {
169 .filter = dev_uevent_filter,
170 .name = dev_uevent_name,
171 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
Kay Sieversa7fd6702005-10-01 14:49:43 +0200174static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t count)
176{
Kay Sievers312c0042005-11-16 09:00:00 +0100177 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200178 return count;
179}
180
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700181static 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 Waitz0863afb2006-01-09 20:53:55 -0800187/*
188 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190
Kay Sievers312c0042005-11-16 09:00:00 +0100191decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193
194/**
195 * device_create_file - create sysfs attribute file for device.
196 * @dev: device.
197 * @attr: device attribute descriptor.
198 */
199
200int 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
216void 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 Bottomley34bb61f2005-09-06 16:56:51 -0700224static 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
231static 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 Torvalds1da177e2005-04-16 15:20:36 -0700238
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
250void device_initialize(struct device *dev)
251{
252 kobj_set_kset_s(dev, devices_subsys);
253 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700254 klist_init(&dev->klist_children, klist_children_get,
255 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700257 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800258 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700259 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
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 */
273int device_add(struct device *dev)
274{
275 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700276 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int error = -EINVAL;
278
279 dev = get_device(dev);
280 if (!dev || !strlen(dev->bus_id))
281 goto Error;
282
283 parent = get_device(dev->parent);
284
285 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
286
287 /* first, register with generic layer. */
288 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
289 if (parent)
290 dev->kobj.parent = &parent->kobj;
291
292 if ((error = kobject_add(&dev->kobj)))
293 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200294
295 dev->uevent_attr.attr.name = "uevent";
296 dev->uevent_attr.attr.mode = S_IWUSR;
297 if (dev->driver)
298 dev->uevent_attr.attr.owner = dev->driver->owner;
299 dev->uevent_attr.store = store_uevent;
300 device_create_file(dev, &dev->uevent_attr);
301
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700302 if (MAJOR(dev->devt)) {
303 struct device_attribute *attr;
304 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
305 if (!attr) {
306 error = -ENOMEM;
307 goto PMError;
308 }
309 attr->attr.name = "dev";
310 attr->attr.mode = S_IRUGO;
311 if (dev->driver)
312 attr->attr.owner = dev->driver->owner;
313 attr->show = show_dev;
314 error = device_create_file(dev, attr);
315 if (error) {
316 kfree(attr);
317 goto attrError;
318 }
319
320 dev->devt_attr = attr;
321 }
322
Kay Sieversb9d9c822006-06-15 15:31:56 +0200323 if (dev->class) {
324 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
325 "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700326 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
327 dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700328
329 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
330 class_name = make_class_name(dev->class->name, &dev->kobj);
331 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200332 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if ((error = device_pm_add(dev)))
335 goto PMError;
336 if ((error = bus_add_device(dev)))
337 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200338 kobject_uevent(&dev->kobj, KOBJ_ADD);
339 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (parent)
James Bottomleyd856f1e32005-08-19 09:14:01 -0400341 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* notify platform of device entry */
344 if (platform_notify)
345 platform_notify(dev);
346 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700347 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 put_device(dev);
349 return error;
350 BusError:
351 device_pm_remove(dev);
352 PMError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700353 if (dev->devt_attr) {
354 device_remove_file(dev, dev->devt_attr);
355 kfree(dev->devt_attr);
356 }
357 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100358 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 kobject_del(&dev->kobj);
360 Error:
361 if (parent)
362 put_device(parent);
363 goto Done;
364}
365
366
367/**
368 * device_register - register a device with the system.
369 * @dev: pointer to the device structure
370 *
371 * This happens in two clean steps - initialize the device
372 * and add it to the system. The two steps can be called
373 * separately, but this is the easiest and most common.
374 * I.e. you should only call the two helpers separately if
375 * have a clearly defined need to use and refcount the device
376 * before it is added to the hierarchy.
377 */
378
379int device_register(struct device *dev)
380{
381 device_initialize(dev);
382 return device_add(dev);
383}
384
385
386/**
387 * get_device - increment reference count for device.
388 * @dev: device.
389 *
390 * This simply forwards the call to kobject_get(), though
391 * we do take care to provide for the case that we get a NULL
392 * pointer passed in.
393 */
394
395struct device * get_device(struct device * dev)
396{
397 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
398}
399
400
401/**
402 * put_device - decrement reference count.
403 * @dev: device in question.
404 */
405void put_device(struct device * dev)
406{
407 if (dev)
408 kobject_put(&dev->kobj);
409}
410
411
412/**
413 * device_del - delete device from system.
414 * @dev: device.
415 *
416 * This is the first part of the device unregistration
417 * sequence. This removes the device from the lists we control
418 * from here, has it removed from the other driver model
419 * subsystems it was added to in device_add(), and removes it
420 * from the kobject hierarchy.
421 *
422 * NOTE: this should be called manually _iff_ device_add() was
423 * also called manually.
424 */
425
426void device_del(struct device * dev)
427{
428 struct device * parent = dev->parent;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700429 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700432 klist_del(&dev->knode_parent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700433 if (dev->devt_attr)
434 device_remove_file(dev, dev->devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200435 if (dev->class) {
436 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700437 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700438 class_name = make_class_name(dev->class->name, &dev->kobj);
439 sysfs_remove_link(&dev->kobj, "device");
440 sysfs_remove_link(&dev->parent->kobj, class_name);
441 kfree(class_name);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200442 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200443 device_remove_file(dev, &dev->uevent_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* Notify the platform of the removal, in case they
446 * need to do anything...
447 */
448 if (platform_notify_remove)
449 platform_notify_remove(dev);
450 bus_remove_device(dev);
451 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100452 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kobject_del(&dev->kobj);
454 if (parent)
455 put_device(parent);
456}
457
458/**
459 * device_unregister - unregister device from system.
460 * @dev: device going away.
461 *
462 * We do this in two parts, like we do device_register(). First,
463 * we remove it from all the subsystems with device_del(), then
464 * we decrement the reference count via put_device(). If that
465 * is the final reference count, the device will be cleaned up
466 * via device_release() above. Otherwise, the structure will
467 * stick around until the final reference to the device is dropped.
468 */
469void device_unregister(struct device * dev)
470{
471 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
472 device_del(dev);
473 put_device(dev);
474}
475
476
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800477static struct device * next_device(struct klist_iter * i)
478{
479 struct klist_node * n = klist_next(i);
480 return n ? container_of(n, struct device, knode_parent) : NULL;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/**
484 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700485 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * @data: data for the callback.
487 * @fn: function to be called for each device.
488 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700489 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * passing it @data.
491 *
492 * We check the return of @fn each time. If it returns anything
493 * other than 0, we break out and return that value.
494 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800495int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int (*fn)(struct device *, void *))
497{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800498 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct device * child;
500 int error = 0;
501
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800502 klist_iter_init(&parent->klist_children, &i);
503 while ((child = next_device(&i)) && !error)
504 error = fn(child, data);
505 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return error;
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509int __init devices_init(void)
510{
511 return subsystem_register(&devices_subsys);
512}
513
514EXPORT_SYMBOL_GPL(device_for_each_child);
515
516EXPORT_SYMBOL_GPL(device_initialize);
517EXPORT_SYMBOL_GPL(device_add);
518EXPORT_SYMBOL_GPL(device_register);
519
520EXPORT_SYMBOL_GPL(device_del);
521EXPORT_SYMBOL_GPL(device_unregister);
522EXPORT_SYMBOL_GPL(get_device);
523EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525EXPORT_SYMBOL_GPL(device_create_file);
526EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700527
528
529static void device_create_release(struct device *dev)
530{
531 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
532 kfree(dev);
533}
534
535/**
536 * device_create - creates a device and registers it with sysfs
537 * @cs: pointer to the struct class that this device should be registered to.
538 * @parent: pointer to the parent struct device of this new device, if any.
539 * @dev: the dev_t for the char device to be added.
540 * @fmt: string for the class device's name
541 *
542 * This function can be used by char device classes. A struct
543 * device will be created in sysfs, registered to the specified
544 * class.
545 * A "dev" file will be created, showing the dev_t for the device, if
546 * the dev_t is not 0,0.
547 * If a pointer to a parent struct device is passed in, the newly
548 * created struct device will be a child of that device in sysfs. The
549 * pointer to the struct device will be returned from the call. Any
550 * further sysfs files that might be required can be created using this
551 * pointer.
552 *
553 * Note: the struct class passed to this function must have previously
554 * been created with a call to class_create().
555 */
556struct device *device_create(struct class *class, struct device *parent,
557 dev_t devt, char *fmt, ...)
558{
559 va_list args;
560 struct device *dev = NULL;
561 int retval = -ENODEV;
562
563 if (class == NULL || IS_ERR(class))
564 goto error;
565 if (parent == NULL) {
566 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
567 goto error;
568 }
569
570 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
571 if (!dev) {
572 retval = -ENOMEM;
573 goto error;
574 }
575
576 dev->devt = devt;
577 dev->class = class;
578 dev->parent = parent;
579 dev->release = device_create_release;
580
581 va_start(args, fmt);
582 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
583 va_end(args);
584 retval = device_register(dev);
585 if (retval)
586 goto error;
587
588 /* tie the class to the device */
589 down(&class->sem);
590 list_add_tail(&dev->node, &class->devices);
591 up(&class->sem);
592
593 return dev;
594
595error:
596 kfree(dev);
597 return ERR_PTR(retval);
598}
599EXPORT_SYMBOL_GPL(device_create);
600
601/**
602 * device_destroy - removes a device that was created with device_create()
603 * @class: the pointer to the struct class that this device was registered * with.
604 * @dev: the dev_t of the device that was previously registered.
605 *
606 * This call unregisters and cleans up a class device that was created with a
607 * call to class_device_create()
608 */
609void device_destroy(struct class *class, dev_t devt)
610{
611 struct device *dev = NULL;
612 struct device *dev_tmp;
613
614 down(&class->sem);
615 list_for_each_entry(dev_tmp, &class->devices, node) {
616 if (dev_tmp->devt == devt) {
617 dev = dev_tmp;
618 break;
619 }
620 }
621 up(&class->sem);
622
623 if (dev) {
624 list_del_init(&dev->node);
625 device_unregister(dev);
626 }
627}
628EXPORT_SYMBOL_GPL(device_destroy);