2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
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 #include <linux/kdev_t.h>
20 #include <asm/semaphore.h>
23 #include "power/power.h"
25 int (*platform_notify
)(struct device
* dev
) = NULL
;
26 int (*platform_notify_remove
)(struct device
* dev
) = NULL
;
29 * sysfs bindings for devices.
32 #define to_dev(obj) container_of(obj, struct device, kobj)
33 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
36 dev_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
38 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
39 struct device
* dev
= to_dev(kobj
);
43 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
48 dev_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
49 const char * buf
, size_t count
)
51 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
52 struct device
* dev
= to_dev(kobj
);
56 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
60 static struct sysfs_ops dev_sysfs_ops
= {
61 .show
= dev_attr_show
,
62 .store
= dev_attr_store
,
67 * device_release - free device structure.
68 * @kobj: device's kobject.
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.
74 static void device_release(struct kobject
* kobj
)
76 struct device
* dev
= to_dev(kobj
);
81 printk(KERN_ERR
"Device '%s' does not have a release() function, "
82 "it is broken and must be fixed.\n",
88 static struct kobj_type ktype_device
= {
89 .release
= device_release
,
90 .sysfs_ops
= &dev_sysfs_ops
,
94 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
96 struct kobj_type
*ktype
= get_ktype(kobj
);
98 if (ktype
== &ktype_device
) {
99 struct device
*dev
= to_dev(kobj
);
108 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
110 struct device
*dev
= to_dev(kobj
);
113 return dev
->bus
->name
;
115 return dev
->class->name
;
119 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
120 int num_envp
, char *buffer
, int buffer_size
)
122 struct device
*dev
= to_dev(kobj
);
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
));
137 /* add bus name of physical device */
139 add_uevent_var(envp
, num_envp
, &i
,
140 buffer
, buffer_size
, &length
,
141 "PHYSDEVBUS=%s", dev
->bus
->name
);
143 /* add driver name of physical device */
145 add_uevent_var(envp
, num_envp
, &i
,
146 buffer
, buffer_size
, &length
,
147 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
149 /* terminate, set to next free slot, shrink available space */
153 buffer
= &buffer
[length
];
154 buffer_size
-= length
;
156 if (dev
->bus
&& dev
->bus
->uevent
) {
157 /* have the bus specific function add its stuff */
158 retval
= dev
->bus
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
160 pr_debug ("%s - uevent() returned %d\n",
161 __FUNCTION__
, retval
);
168 static struct kset_uevent_ops device_uevent_ops
= {
169 .filter
= dev_uevent_filter
,
170 .name
= dev_uevent_name
,
171 .uevent
= dev_uevent
,
174 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
175 const char *buf
, size_t count
)
177 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
181 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
184 return print_dev_t(buf
, dev
->devt
);
188 * devices_subsys - structure to be registered with kobject core.
191 decl_subsys(devices
, &ktype_device
, &device_uevent_ops
);
195 * device_create_file - create sysfs attribute file for device.
197 * @attr: device attribute descriptor.
200 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
203 if (get_device(dev
)) {
204 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
211 * device_remove_file - remove sysfs attribute file.
213 * @attr: device attribute descriptor.
216 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
218 if (get_device(dev
)) {
219 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
224 static void klist_children_get(struct klist_node
*n
)
226 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
231 static void klist_children_put(struct klist_node
*n
)
233 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
240 * device_initialize - init device structure.
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).
250 void device_initialize(struct device
*dev
)
252 kobj_set_kset_s(dev
, devices_subsys
);
253 kobject_init(&dev
->kobj
);
254 klist_init(&dev
->klist_children
, klist_children_get
,
256 INIT_LIST_HEAD(&dev
->dma_pools
);
257 INIT_LIST_HEAD(&dev
->node
);
258 init_MUTEX(&dev
->sem
);
259 device_init_wakeup(dev
, 0);
263 * device_add - add device to device hierarchy.
266 * This is part 2 of device_register(), though may be called
267 * separately _iff_ device_initialize() has been called separately.
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.
273 int device_add(struct device
*dev
)
275 struct device
*parent
= NULL
;
278 dev
= get_device(dev
);
279 if (!dev
|| !strlen(dev
->bus_id
))
282 parent
= get_device(dev
->parent
);
284 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
286 /* first, register with generic layer. */
287 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
289 dev
->kobj
.parent
= &parent
->kobj
;
291 if ((error
= kobject_add(&dev
->kobj
)))
294 dev
->uevent_attr
.attr
.name
= "uevent";
295 dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
297 dev
->uevent_attr
.attr
.owner
= dev
->driver
->owner
;
298 dev
->uevent_attr
.store
= store_uevent
;
299 device_create_file(dev
, &dev
->uevent_attr
);
301 if (MAJOR(dev
->devt
)) {
302 struct device_attribute
*attr
;
303 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
308 attr
->attr
.name
= "dev";
309 attr
->attr
.mode
= S_IRUGO
;
311 attr
->attr
.owner
= dev
->driver
->owner
;
312 attr
->show
= show_dev
;
313 error
= device_create_file(dev
, attr
);
319 dev
->devt_attr
= attr
;
323 sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kset
.kobj
,
325 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
, &dev
->kobj
,
329 if ((error
= device_pm_add(dev
)))
331 if ((error
= bus_add_device(dev
)))
333 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
334 bus_attach_device(dev
);
336 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
338 /* notify platform of device entry */
340 platform_notify(dev
);
345 device_pm_remove(dev
);
347 if (dev
->devt_attr
) {
348 device_remove_file(dev
, dev
->devt_attr
);
349 kfree(dev
->devt_attr
);
352 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
353 kobject_del(&dev
->kobj
);
362 * device_register - register a device with the system.
363 * @dev: pointer to the device structure
365 * This happens in two clean steps - initialize the device
366 * and add it to the system. The two steps can be called
367 * separately, but this is the easiest and most common.
368 * I.e. you should only call the two helpers separately if
369 * have a clearly defined need to use and refcount the device
370 * before it is added to the hierarchy.
373 int device_register(struct device
*dev
)
375 device_initialize(dev
);
376 return device_add(dev
);
381 * get_device - increment reference count for device.
384 * This simply forwards the call to kobject_get(), though
385 * we do take care to provide for the case that we get a NULL
389 struct device
* get_device(struct device
* dev
)
391 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
396 * put_device - decrement reference count.
397 * @dev: device in question.
399 void put_device(struct device
* dev
)
402 kobject_put(&dev
->kobj
);
407 * device_del - delete device from system.
410 * This is the first part of the device unregistration
411 * sequence. This removes the device from the lists we control
412 * from here, has it removed from the other driver model
413 * subsystems it was added to in device_add(), and removes it
414 * from the kobject hierarchy.
416 * NOTE: this should be called manually _iff_ device_add() was
417 * also called manually.
420 void device_del(struct device
* dev
)
422 struct device
* parent
= dev
->parent
;
425 klist_del(&dev
->knode_parent
);
427 device_remove_file(dev
, dev
->devt_attr
);
429 sysfs_remove_link(&dev
->kobj
, "subsystem");
430 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
, dev
->bus_id
);
432 device_remove_file(dev
, &dev
->uevent_attr
);
434 /* Notify the platform of the removal, in case they
435 * need to do anything...
437 if (platform_notify_remove
)
438 platform_notify_remove(dev
);
439 bus_remove_device(dev
);
440 device_pm_remove(dev
);
441 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
442 kobject_del(&dev
->kobj
);
448 * device_unregister - unregister device from system.
449 * @dev: device going away.
451 * We do this in two parts, like we do device_register(). First,
452 * we remove it from all the subsystems with device_del(), then
453 * we decrement the reference count via put_device(). If that
454 * is the final reference count, the device will be cleaned up
455 * via device_release() above. Otherwise, the structure will
456 * stick around until the final reference to the device is dropped.
458 void device_unregister(struct device
* dev
)
460 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
466 static struct device
* next_device(struct klist_iter
* i
)
468 struct klist_node
* n
= klist_next(i
);
469 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
473 * device_for_each_child - device child iterator.
474 * @parent: parent struct device.
475 * @data: data for the callback.
476 * @fn: function to be called for each device.
478 * Iterate over @parent's child devices, and call @fn for each,
481 * We check the return of @fn each time. If it returns anything
482 * other than 0, we break out and return that value.
484 int device_for_each_child(struct device
* parent
, void * data
,
485 int (*fn
)(struct device
*, void *))
488 struct device
* child
;
491 klist_iter_init(&parent
->klist_children
, &i
);
492 while ((child
= next_device(&i
)) && !error
)
493 error
= fn(child
, data
);
498 int __init
devices_init(void)
500 return subsystem_register(&devices_subsys
);
503 EXPORT_SYMBOL_GPL(device_for_each_child
);
505 EXPORT_SYMBOL_GPL(device_initialize
);
506 EXPORT_SYMBOL_GPL(device_add
);
507 EXPORT_SYMBOL_GPL(device_register
);
509 EXPORT_SYMBOL_GPL(device_del
);
510 EXPORT_SYMBOL_GPL(device_unregister
);
511 EXPORT_SYMBOL_GPL(get_device
);
512 EXPORT_SYMBOL_GPL(put_device
);
514 EXPORT_SYMBOL_GPL(device_create_file
);
515 EXPORT_SYMBOL_GPL(device_remove_file
);
518 static void device_create_release(struct device
*dev
)
520 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
525 * device_create - creates a device and registers it with sysfs
526 * @cs: pointer to the struct class that this device should be registered to.
527 * @parent: pointer to the parent struct device of this new device, if any.
528 * @dev: the dev_t for the char device to be added.
529 * @fmt: string for the class device's name
531 * This function can be used by char device classes. A struct
532 * device will be created in sysfs, registered to the specified
534 * A "dev" file will be created, showing the dev_t for the device, if
535 * the dev_t is not 0,0.
536 * If a pointer to a parent struct device is passed in, the newly
537 * created struct device will be a child of that device in sysfs. The
538 * pointer to the struct device will be returned from the call. Any
539 * further sysfs files that might be required can be created using this
542 * Note: the struct class passed to this function must have previously
543 * been created with a call to class_create().
545 struct device
*device_create(struct class *class, struct device
*parent
,
546 dev_t devt
, char *fmt
, ...)
549 struct device
*dev
= NULL
;
550 int retval
= -ENODEV
;
552 if (class == NULL
|| IS_ERR(class))
554 if (parent
== NULL
) {
555 printk(KERN_WARNING
"%s does not work yet for NULL parents\n", __FUNCTION__
);
559 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
567 dev
->parent
= parent
;
568 dev
->release
= device_create_release
;
571 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
573 retval
= device_register(dev
);
577 /* tie the class to the device */
579 list_add_tail(&dev
->node
, &class->devices
);
586 return ERR_PTR(retval
);
588 EXPORT_SYMBOL_GPL(device_create
);
591 * device_destroy - removes a device that was created with device_create()
592 * @class: the pointer to the struct class that this device was registered * with.
593 * @dev: the dev_t of the device that was previously registered.
595 * This call unregisters and cleans up a class device that was created with a
596 * call to class_device_create()
598 void device_destroy(struct class *class, dev_t devt
)
600 struct device
*dev
= NULL
;
601 struct device
*dev_tmp
;
604 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
605 if (dev_tmp
->devt
== devt
) {
613 list_del_init(&dev
->node
);
614 device_unregister(dev
);
617 EXPORT_SYMBOL_GPL(device_destroy
);