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>
19 #include <asm/semaphore.h>
22 #include "power/power.h"
24 int (*platform_notify
)(struct device
* dev
) = NULL
;
25 int (*platform_notify_remove
)(struct device
* dev
) = NULL
;
28 * sysfs bindings for devices.
31 #define to_dev(obj) container_of(obj, struct device, kobj)
32 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
34 extern struct attribute
* dev_default_attrs
[];
37 dev_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
39 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
40 struct device
* dev
= to_dev(kobj
);
44 ret
= dev_attr
->show(dev
, buf
);
49 dev_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
50 const char * buf
, size_t count
)
52 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
53 struct device
* dev
= to_dev(kobj
);
57 ret
= dev_attr
->store(dev
, buf
, count
);
61 static struct sysfs_ops dev_sysfs_ops
= {
62 .show
= dev_attr_show
,
63 .store
= dev_attr_store
,
68 * device_release - free device structure.
69 * @kobj: device's kobject.
71 * This is called once the reference count for the object
72 * reaches 0. We forward the call to the device's release
73 * method, which should handle actually freeing the structure.
75 static void device_release(struct kobject
* kobj
)
77 struct device
* dev
= to_dev(kobj
);
82 printk(KERN_ERR
"Device '%s' does not have a release() function, "
83 "it is broken and must be fixed.\n",
89 static struct kobj_type ktype_device
= {
90 .release
= device_release
,
91 .sysfs_ops
= &dev_sysfs_ops
,
92 .default_attrs
= dev_default_attrs
,
96 static int dev_hotplug_filter(struct kset
*kset
, struct kobject
*kobj
)
98 struct kobj_type
*ktype
= get_ktype(kobj
);
100 if (ktype
== &ktype_device
) {
101 struct device
*dev
= to_dev(kobj
);
108 static char *dev_hotplug_name(struct kset
*kset
, struct kobject
*kobj
)
110 struct device
*dev
= to_dev(kobj
);
112 return dev
->bus
->name
;
115 static int dev_hotplug(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
116 int num_envp
, char *buffer
, int buffer_size
)
118 struct device
*dev
= to_dev(kobj
);
121 if (dev
->bus
->hotplug
) {
122 /* have the bus specific function add its stuff */
123 retval
= dev
->bus
->hotplug (dev
, envp
, num_envp
, buffer
, buffer_size
);
125 pr_debug ("%s - hotplug() returned %d\n",
126 __FUNCTION__
, retval
);
133 static struct kset_hotplug_ops device_hotplug_ops
= {
134 .filter
= dev_hotplug_filter
,
135 .name
= dev_hotplug_name
,
136 .hotplug
= dev_hotplug
,
140 * device_subsys - structure to be registered with kobject core.
143 decl_subsys(devices
, &ktype_device
, &device_hotplug_ops
);
147 * device_create_file - create sysfs attribute file for device.
149 * @attr: device attribute descriptor.
152 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
155 if (get_device(dev
)) {
156 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
163 * device_remove_file - remove sysfs attribute file.
165 * @attr: device attribute descriptor.
168 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
170 if (get_device(dev
)) {
171 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
178 * device_initialize - init device structure.
181 * This prepares the device for use by other layers,
182 * including adding it to the device hierarchy.
183 * It is the first half of device_register(), if called by
184 * that, though it can also be called separately, so one
185 * may use @dev's fields (e.g. the refcount).
188 void device_initialize(struct device
*dev
)
190 kobj_set_kset_s(dev
, devices_subsys
);
191 kobject_init(&dev
->kobj
);
192 INIT_LIST_HEAD(&dev
->node
);
193 INIT_LIST_HEAD(&dev
->children
);
194 INIT_LIST_HEAD(&dev
->driver_list
);
195 INIT_LIST_HEAD(&dev
->bus_list
);
196 INIT_LIST_HEAD(&dev
->dma_pools
);
200 * device_add - add device to device hierarchy.
203 * This is part 2 of device_register(), though may be called
204 * separately _iff_ device_initialize() has been called separately.
206 * This adds it to the kobject hierarchy via kobject_add(), adds it
207 * to the global and sibling lists for the device, then
208 * adds it to the other relevant subsystems of the driver model.
210 int device_add(struct device
*dev
)
212 struct device
* parent
;
216 dev
= get_device(dev
);
217 if (!dev
|| !strlen(dev
->bus_id
))
220 parent
= get_device(dev
->parent
);
222 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
224 /* first, register with generic layer. */
225 kobject_set_name(&dev
->kobj
, dev
->bus_id
);
227 dev
->kobj
.parent
= &parent
->kobj
;
229 if ((error
= kobject_add(&dev
->kobj
)))
231 if ((error
= device_pm_add(dev
)))
236 if ((error
= bus_add_device(dev
)))
240 down_write(&devices_subsys
.rwsem
);
242 list_add_tail(&dev
->node
, &parent
->children
);
243 up_write(&devices_subsys
.rwsem
);
245 /* notify platform of device entry */
247 platform_notify(dev
);
253 device_pm_remove(dev
);
255 kobject_del(&dev
->kobj
);
264 * device_register - register a device with the system.
265 * @dev: pointer to the device structure
267 * This happens in two clean steps - initialize the device
268 * and add it to the system. The two steps can be called
269 * separately, but this is the easiest and most common.
270 * I.e. you should only call the two helpers separately if
271 * have a clearly defined need to use and refcount the device
272 * before it is added to the hierarchy.
275 int device_register(struct device
*dev
)
277 device_initialize(dev
);
278 return device_add(dev
);
283 * get_device - increment reference count for device.
286 * This simply forwards the call to kobject_get(), though
287 * we do take care to provide for the case that we get a NULL
291 struct device
* get_device(struct device
* dev
)
293 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
298 * put_device - decrement reference count.
299 * @dev: device in question.
301 void put_device(struct device
* dev
)
303 kobject_put(&dev
->kobj
);
308 * device_del - delete device from system.
311 * This is the first part of the device unregistration
312 * sequence. This removes the device from the lists we control
313 * from here, has it removed from the other driver model
314 * subsystems it was added to in device_add(), and removes it
315 * from the kobject hierarchy.
317 * NOTE: this should be called manually _iff_ device_add() was
318 * also called manually.
321 void device_del(struct device
* dev
)
323 struct device
* parent
= dev
->parent
;
325 down_write(&devices_subsys
.rwsem
);
327 list_del_init(&dev
->node
);
328 up_write(&devices_subsys
.rwsem
);
330 /* Notify the platform of the removal, in case they
331 * need to do anything...
333 if (platform_notify_remove
)
334 platform_notify_remove(dev
);
335 bus_remove_device(dev
);
336 device_pm_remove(dev
);
337 kobject_del(&dev
->kobj
);
343 * device_unregister - unregister device from system.
344 * @dev: device going away.
346 * We do this in two parts, like we do device_register(). First,
347 * we remove it from all the subsystems with device_del(), then
348 * we decrement the reference count via put_device(). If that
349 * is the final reference count, the device will be cleaned up
350 * via device_release() above. Otherwise, the structure will
351 * stick around until the final reference to the device is dropped.
353 void device_unregister(struct device
* dev
)
355 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
362 * device_for_each_child - device child iterator.
363 * @dev: parent struct device.
364 * @data: data for the callback.
365 * @fn: function to be called for each device.
367 * Iterate over @dev's child devices, and call @fn for each,
370 * We check the return of @fn each time. If it returns anything
371 * other than 0, we break out and return that value.
373 int device_for_each_child(struct device
* dev
, void * data
,
374 int (*fn
)(struct device
*, void *))
376 struct device
* child
;
379 down_read(&devices_subsys
.rwsem
);
380 list_for_each_entry(child
, &dev
->children
, node
) {
381 if((error
= fn(child
, data
)))
384 up_read(&devices_subsys
.rwsem
);
389 * device_find - locate device on a bus by name.
390 * @name: name of the device.
391 * @bus: bus to scan for the device.
393 * Call kset_find_obj() to iterate over list of devices on
394 * a bus to find device by name. Return device if found.
396 * Note that kset_find_obj increments device's reference count.
398 struct device
*device_find(const char *name
, struct bus_type
*bus
)
400 struct kobject
*k
= kset_find_obj(&bus
->devices
, name
);
406 int __init
devices_init(void)
408 return subsystem_register(&devices_subsys
);
411 EXPORT_SYMBOL(device_for_each_child
);
413 EXPORT_SYMBOL(device_initialize
);
414 EXPORT_SYMBOL(device_add
);
415 EXPORT_SYMBOL(device_register
);
417 EXPORT_SYMBOL(device_del
);
418 EXPORT_SYMBOL(device_unregister
);
419 EXPORT_SYMBOL(get_device
);
420 EXPORT_SYMBOL(put_device
);
421 EXPORT_SYMBOL(device_find
);
423 EXPORT_SYMBOL(device_create_file
);
424 EXPORT_SYMBOL(device_remove_file
);