blob: 27c2176895dee0ccb5efd195d008f8a0333a0904 [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
Alan Stern3e956372006-06-16 17:10:48 -040032/**
33 * dev_driver_string - Return a device's driver name, if at all possible
34 * @dev: struct device to get the name of
35 *
36 * Will return the device's driver's name if it is bound to a device. If
37 * the device is not bound to a device, it will return the name of the bus
38 * it is attached to. If it is not attached to a bus either, an empty
39 * string will be returned.
40 */
41const char *dev_driver_string(struct device *dev)
42{
43 return dev->driver ? dev->driver->name :
44 (dev->bus ? dev->bus->name : "");
45}
46EXPORT_SYMBOL_GPL(dev_driver_string);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define to_dev(obj) container_of(obj, struct device, kobj)
49#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static ssize_t
52dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
53{
54 struct device_attribute * dev_attr = to_dev_attr(attr);
55 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050056 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040059 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return ret;
61}
62
63static ssize_t
64dev_attr_store(struct kobject * kobj, struct attribute * attr,
65 const char * buf, size_t count)
66{
67 struct device_attribute * dev_attr = to_dev_attr(attr);
68 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050069 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040072 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return ret;
74}
75
76static struct sysfs_ops dev_sysfs_ops = {
77 .show = dev_attr_show,
78 .store = dev_attr_store,
79};
80
81
82/**
83 * device_release - free device structure.
84 * @kobj: device's kobject.
85 *
86 * This is called once the reference count for the object
87 * reaches 0. We forward the call to the device's release
88 * method, which should handle actually freeing the structure.
89 */
90static void device_release(struct kobject * kobj)
91{
92 struct device * dev = to_dev(kobj);
93
94 if (dev->release)
95 dev->release(dev);
96 else {
97 printk(KERN_ERR "Device '%s' does not have a release() function, "
98 "it is broken and must be fixed.\n",
99 dev->bus_id);
100 WARN_ON(1);
101 }
102}
103
104static struct kobj_type ktype_device = {
105 .release = device_release,
106 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109
Kay Sievers312c0042005-11-16 09:00:00 +0100110static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct kobj_type *ktype = get_ktype(kobj);
113
114 if (ktype == &ktype_device) {
115 struct device *dev = to_dev(kobj);
116 if (dev->bus)
117 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700118 if (dev->class)
119 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 return 0;
122}
123
Kay Sievers312c0042005-11-16 09:00:00 +0100124static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct device *dev = to_dev(kobj);
127
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700128 if (dev->bus)
129 return dev->bus->name;
130 if (dev->class)
131 return dev->class->name;
132 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Kay Sievers312c0042005-11-16 09:00:00 +0100135static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int num_envp, char *buffer, int buffer_size)
137{
138 struct device *dev = to_dev(kobj);
139 int i = 0;
140 int length = 0;
141 int retval = 0;
142
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700143 /* add the major/minor if present */
144 if (MAJOR(dev->devt)) {
145 add_uevent_var(envp, num_envp, &i,
146 buffer, buffer_size, &length,
147 "MAJOR=%u", MAJOR(dev->devt));
148 add_uevent_var(envp, num_envp, &i,
149 buffer, buffer_size, &length,
150 "MINOR=%u", MINOR(dev->devt));
151 }
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* add bus name of physical device */
154 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100155 add_uevent_var(envp, num_envp, &i,
156 buffer, buffer_size, &length,
157 "PHYSDEVBUS=%s", dev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* add driver name of physical device */
160 if (dev->driver)
Kay Sievers312c0042005-11-16 09:00:00 +0100161 add_uevent_var(envp, num_envp, &i,
162 buffer, buffer_size, &length,
163 "PHYSDEVDRIVER=%s", dev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* terminate, set to next free slot, shrink available space */
166 envp[i] = NULL;
167 envp = &envp[i];
168 num_envp -= i;
169 buffer = &buffer[length];
170 buffer_size -= length;
171
Kay Sievers312c0042005-11-16 09:00:00 +0100172 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100174 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100176 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 __FUNCTION__, retval);
178 }
179 }
180
181 return retval;
182}
183
Kay Sievers312c0042005-11-16 09:00:00 +0100184static struct kset_uevent_ops device_uevent_ops = {
185 .filter = dev_uevent_filter,
186 .name = dev_uevent_name,
187 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Kay Sieversa7fd6702005-10-01 14:49:43 +0200190static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
192{
Kay Sievers312c0042005-11-16 09:00:00 +0100193 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200194 return count;
195}
196
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700197static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
198 char *buf)
199{
200 return print_dev_t(buf, dev->devt);
201}
202
Martin Waitz0863afb2006-01-09 20:53:55 -0800203/*
204 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206
Kay Sievers312c0042005-11-16 09:00:00 +0100207decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209
210/**
211 * device_create_file - create sysfs attribute file for device.
212 * @dev: device.
213 * @attr: device attribute descriptor.
214 */
215
216int device_create_file(struct device * dev, struct device_attribute * attr)
217{
218 int error = 0;
219 if (get_device(dev)) {
220 error = sysfs_create_file(&dev->kobj, &attr->attr);
221 put_device(dev);
222 }
223 return error;
224}
225
226/**
227 * device_remove_file - remove sysfs attribute file.
228 * @dev: device.
229 * @attr: device attribute descriptor.
230 */
231
232void device_remove_file(struct device * dev, struct device_attribute * attr)
233{
234 if (get_device(dev)) {
235 sysfs_remove_file(&dev->kobj, &attr->attr);
236 put_device(dev);
237 }
238}
239
James Bottomley34bb61f2005-09-06 16:56:51 -0700240static void klist_children_get(struct klist_node *n)
241{
242 struct device *dev = container_of(n, struct device, knode_parent);
243
244 get_device(dev);
245}
246
247static void klist_children_put(struct klist_node *n)
248{
249 struct device *dev = container_of(n, struct device, knode_parent);
250
251 put_device(dev);
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/**
256 * device_initialize - init device structure.
257 * @dev: device.
258 *
259 * This prepares the device for use by other layers,
260 * including adding it to the device hierarchy.
261 * It is the first half of device_register(), if called by
262 * that, though it can also be called separately, so one
263 * may use @dev's fields (e.g. the refcount).
264 */
265
266void device_initialize(struct device *dev)
267{
268 kobj_set_kset_s(dev, devices_subsys);
269 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700270 klist_init(&dev->klist_children, klist_children_get,
271 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700273 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800274 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700275 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
279 * device_add - add device to device hierarchy.
280 * @dev: device.
281 *
282 * This is part 2 of device_register(), though may be called
283 * separately _iff_ device_initialize() has been called separately.
284 *
285 * This adds it to the kobject hierarchy via kobject_add(), adds it
286 * to the global and sibling lists for the device, then
287 * adds it to the other relevant subsystems of the driver model.
288 */
289int device_add(struct device *dev)
290{
291 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700292 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int error = -EINVAL;
294
295 dev = get_device(dev);
296 if (!dev || !strlen(dev->bus_id))
297 goto Error;
298
299 parent = get_device(dev->parent);
300
301 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
302
303 /* first, register with generic layer. */
304 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
305 if (parent)
306 dev->kobj.parent = &parent->kobj;
307
308 if ((error = kobject_add(&dev->kobj)))
309 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200310
311 dev->uevent_attr.attr.name = "uevent";
312 dev->uevent_attr.attr.mode = S_IWUSR;
313 if (dev->driver)
314 dev->uevent_attr.attr.owner = dev->driver->owner;
315 dev->uevent_attr.store = store_uevent;
316 device_create_file(dev, &dev->uevent_attr);
317
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700318 if (MAJOR(dev->devt)) {
319 struct device_attribute *attr;
320 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
321 if (!attr) {
322 error = -ENOMEM;
323 goto PMError;
324 }
325 attr->attr.name = "dev";
326 attr->attr.mode = S_IRUGO;
327 if (dev->driver)
328 attr->attr.owner = dev->driver->owner;
329 attr->show = show_dev;
330 error = device_create_file(dev, attr);
331 if (error) {
332 kfree(attr);
333 goto attrError;
334 }
335
336 dev->devt_attr = attr;
337 }
338
Kay Sieversb9d9c822006-06-15 15:31:56 +0200339 if (dev->class) {
340 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
341 "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700342 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
343 dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700344
345 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
346 class_name = make_class_name(dev->class->name, &dev->kobj);
347 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200348 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if ((error = device_pm_add(dev)))
351 goto PMError;
352 if ((error = bus_add_device(dev)))
353 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200354 kobject_uevent(&dev->kobj, KOBJ_ADD);
355 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400357 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700359 if (dev->class) {
360 /* tie the class to the device */
361 down(&dev->class->sem);
362 list_add_tail(&dev->node, &dev->class->devices);
363 up(&dev->class->sem);
364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* notify platform of device entry */
367 if (platform_notify)
368 platform_notify(dev);
369 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700370 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 put_device(dev);
372 return error;
373 BusError:
374 device_pm_remove(dev);
375 PMError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700376 if (dev->devt_attr) {
377 device_remove_file(dev, dev->devt_attr);
378 kfree(dev->devt_attr);
379 }
380 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100381 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kobject_del(&dev->kobj);
383 Error:
384 if (parent)
385 put_device(parent);
386 goto Done;
387}
388
389
390/**
391 * device_register - register a device with the system.
392 * @dev: pointer to the device structure
393 *
394 * This happens in two clean steps - initialize the device
395 * and add it to the system. The two steps can be called
396 * separately, but this is the easiest and most common.
397 * I.e. you should only call the two helpers separately if
398 * have a clearly defined need to use and refcount the device
399 * before it is added to the hierarchy.
400 */
401
402int device_register(struct device *dev)
403{
404 device_initialize(dev);
405 return device_add(dev);
406}
407
408
409/**
410 * get_device - increment reference count for device.
411 * @dev: device.
412 *
413 * This simply forwards the call to kobject_get(), though
414 * we do take care to provide for the case that we get a NULL
415 * pointer passed in.
416 */
417
418struct device * get_device(struct device * dev)
419{
420 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
421}
422
423
424/**
425 * put_device - decrement reference count.
426 * @dev: device in question.
427 */
428void put_device(struct device * dev)
429{
430 if (dev)
431 kobject_put(&dev->kobj);
432}
433
434
435/**
436 * device_del - delete device from system.
437 * @dev: device.
438 *
439 * This is the first part of the device unregistration
440 * sequence. This removes the device from the lists we control
441 * from here, has it removed from the other driver model
442 * subsystems it was added to in device_add(), and removes it
443 * from the kobject hierarchy.
444 *
445 * NOTE: this should be called manually _iff_ device_add() was
446 * also called manually.
447 */
448
449void device_del(struct device * dev)
450{
451 struct device * parent = dev->parent;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700452 char *class_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700455 klist_del(&dev->knode_parent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700456 if (dev->devt_attr)
457 device_remove_file(dev, dev->devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200458 if (dev->class) {
459 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700460 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700461 class_name = make_class_name(dev->class->name, &dev->kobj);
462 sysfs_remove_link(&dev->kobj, "device");
463 sysfs_remove_link(&dev->parent->kobj, class_name);
464 kfree(class_name);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700465 down(&dev->class->sem);
466 list_del_init(&dev->node);
467 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200468 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200469 device_remove_file(dev, &dev->uevent_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Notify the platform of the removal, in case they
472 * need to do anything...
473 */
474 if (platform_notify_remove)
475 platform_notify_remove(dev);
476 bus_remove_device(dev);
477 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100478 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 kobject_del(&dev->kobj);
480 if (parent)
481 put_device(parent);
482}
483
484/**
485 * device_unregister - unregister device from system.
486 * @dev: device going away.
487 *
488 * We do this in two parts, like we do device_register(). First,
489 * we remove it from all the subsystems with device_del(), then
490 * we decrement the reference count via put_device(). If that
491 * is the final reference count, the device will be cleaned up
492 * via device_release() above. Otherwise, the structure will
493 * stick around until the final reference to the device is dropped.
494 */
495void device_unregister(struct device * dev)
496{
497 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
498 device_del(dev);
499 put_device(dev);
500}
501
502
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800503static struct device * next_device(struct klist_iter * i)
504{
505 struct klist_node * n = klist_next(i);
506 return n ? container_of(n, struct device, knode_parent) : NULL;
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/**
510 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700511 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * @data: data for the callback.
513 * @fn: function to be called for each device.
514 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700515 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * passing it @data.
517 *
518 * We check the return of @fn each time. If it returns anything
519 * other than 0, we break out and return that value.
520 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800521int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int (*fn)(struct device *, void *))
523{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800524 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct device * child;
526 int error = 0;
527
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800528 klist_iter_init(&parent->klist_children, &i);
529 while ((child = next_device(&i)) && !error)
530 error = fn(child, data);
531 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return error;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535int __init devices_init(void)
536{
537 return subsystem_register(&devices_subsys);
538}
539
540EXPORT_SYMBOL_GPL(device_for_each_child);
541
542EXPORT_SYMBOL_GPL(device_initialize);
543EXPORT_SYMBOL_GPL(device_add);
544EXPORT_SYMBOL_GPL(device_register);
545
546EXPORT_SYMBOL_GPL(device_del);
547EXPORT_SYMBOL_GPL(device_unregister);
548EXPORT_SYMBOL_GPL(get_device);
549EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551EXPORT_SYMBOL_GPL(device_create_file);
552EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700553
554
555static void device_create_release(struct device *dev)
556{
557 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
558 kfree(dev);
559}
560
561/**
562 * device_create - creates a device and registers it with sysfs
563 * @cs: pointer to the struct class that this device should be registered to.
564 * @parent: pointer to the parent struct device of this new device, if any.
565 * @dev: the dev_t for the char device to be added.
566 * @fmt: string for the class device's name
567 *
568 * This function can be used by char device classes. A struct
569 * device will be created in sysfs, registered to the specified
570 * class.
571 * A "dev" file will be created, showing the dev_t for the device, if
572 * the dev_t is not 0,0.
573 * If a pointer to a parent struct device is passed in, the newly
574 * created struct device will be a child of that device in sysfs. The
575 * pointer to the struct device will be returned from the call. Any
576 * further sysfs files that might be required can be created using this
577 * pointer.
578 *
579 * Note: the struct class passed to this function must have previously
580 * been created with a call to class_create().
581 */
582struct device *device_create(struct class *class, struct device *parent,
583 dev_t devt, char *fmt, ...)
584{
585 va_list args;
586 struct device *dev = NULL;
587 int retval = -ENODEV;
588
589 if (class == NULL || IS_ERR(class))
590 goto error;
591 if (parent == NULL) {
592 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
593 goto error;
594 }
595
596 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
597 if (!dev) {
598 retval = -ENOMEM;
599 goto error;
600 }
601
602 dev->devt = devt;
603 dev->class = class;
604 dev->parent = parent;
605 dev->release = device_create_release;
606
607 va_start(args, fmt);
608 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
609 va_end(args);
610 retval = device_register(dev);
611 if (retval)
612 goto error;
613
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700614 return dev;
615
616error:
617 kfree(dev);
618 return ERR_PTR(retval);
619}
620EXPORT_SYMBOL_GPL(device_create);
621
622/**
623 * device_destroy - removes a device that was created with device_create()
624 * @class: the pointer to the struct class that this device was registered * with.
625 * @dev: the dev_t of the device that was previously registered.
626 *
627 * This call unregisters and cleans up a class device that was created with a
628 * call to class_device_create()
629 */
630void device_destroy(struct class *class, dev_t devt)
631{
632 struct device *dev = NULL;
633 struct device *dev_tmp;
634
635 down(&class->sem);
636 list_for_each_entry(dev_tmp, &class->devices, node) {
637 if (dev_tmp->devt == devt) {
638 dev = dev_tmp;
639 break;
640 }
641 }
642 up(&class->sem);
643
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700644 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700645 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700646}
647EXPORT_SYMBOL_GPL(device_destroy);