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
);
123 /* add bus name of physical device */
125 add_hotplug_env_var(envp
, num_envp
, &i
,
126 buffer
, buffer_size
, &length
,
127 "PHYSDEVBUS=%s", dev
->bus
->name
);
129 /* add driver name of physical device */
131 add_hotplug_env_var(envp
, num_envp
, &i
,
132 buffer
, buffer_size
, &length
,
133 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
135 /* terminate, set to next free slot, shrink available space */
139 buffer
= &buffer
[length
];
140 buffer_size
-= length
;
142 if (dev
->bus
&& dev
->bus
->hotplug
) {
143 /* have the bus specific function add its stuff */
144 retval
= dev
->bus
->hotplug (dev
, envp
, num_envp
, buffer
, buffer_size
);
146 pr_debug ("%s - hotplug() returned %d\n",
147 __FUNCTION__
, retval
);
154 static struct kset_hotplug_ops device_hotplug_ops
= {
155 .filter
= dev_hotplug_filter
,
156 .name
= dev_hotplug_name
,
157 .hotplug
= dev_hotplug
,
161 * device_subsys - structure to be registered with kobject core.
164 decl_subsys(devices
, &ktype_device
, &device_hotplug_ops
);
168 * device_create_file - create sysfs attribute file for device.
170 * @attr: device attribute descriptor.
173 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
176 if (get_device(dev
)) {
177 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
184 * device_remove_file - remove sysfs attribute file.
186 * @attr: device attribute descriptor.
189 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
191 if (get_device(dev
)) {
192 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
199 * device_initialize - init device structure.
202 * This prepares the device for use by other layers,
203 * including adding it to the device hierarchy.
204 * It is the first half of device_register(), if called by
205 * that, though it can also be called separately, so one
206 * may use @dev's fields (e.g. the refcount).
209 void device_initialize(struct device
*dev
)
211 kobj_set_kset_s(dev
, devices_subsys
);
212 kobject_init(&dev
->kobj
);
213 INIT_LIST_HEAD(&dev
->node
);
214 INIT_LIST_HEAD(&dev
->children
);
215 INIT_LIST_HEAD(&dev
->driver_list
);
216 INIT_LIST_HEAD(&dev
->bus_list
);
217 INIT_LIST_HEAD(&dev
->dma_pools
);
221 * device_add - add device to device hierarchy.
224 * This is part 2 of device_register(), though may be called
225 * separately _iff_ device_initialize() has been called separately.
227 * This adds it to the kobject hierarchy via kobject_add(), adds it
228 * to the global and sibling lists for the device, then
229 * adds it to the other relevant subsystems of the driver model.
231 int device_add(struct device
*dev
)
233 struct device
*parent
= NULL
;
236 dev
= get_device(dev
);
237 if (!dev
|| !strlen(dev
->bus_id
))
240 parent
= get_device(dev
->parent
);
242 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
244 /* first, register with generic layer. */
245 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
247 dev
->kobj
.parent
= &parent
->kobj
;
249 if ((error
= kobject_add(&dev
->kobj
)))
251 if ((error
= device_pm_add(dev
)))
253 if ((error
= bus_add_device(dev
)))
255 down_write(&devices_subsys
.rwsem
);
257 list_add_tail(&dev
->node
, &parent
->children
);
258 up_write(&devices_subsys
.rwsem
);
260 /* notify platform of device entry */
262 platform_notify(dev
);
264 kobject_hotplug(&dev
->kobj
, KOBJ_ADD
);
269 device_pm_remove(dev
);
271 kobject_del(&dev
->kobj
);
280 * device_register - register a device with the system.
281 * @dev: pointer to the device structure
283 * This happens in two clean steps - initialize the device
284 * and add it to the system. The two steps can be called
285 * separately, but this is the easiest and most common.
286 * I.e. you should only call the two helpers separately if
287 * have a clearly defined need to use and refcount the device
288 * before it is added to the hierarchy.
291 int device_register(struct device
*dev
)
293 device_initialize(dev
);
294 return device_add(dev
);
299 * get_device - increment reference count for device.
302 * This simply forwards the call to kobject_get(), though
303 * we do take care to provide for the case that we get a NULL
307 struct device
* get_device(struct device
* dev
)
309 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
314 * put_device - decrement reference count.
315 * @dev: device in question.
317 void put_device(struct device
* dev
)
320 kobject_put(&dev
->kobj
);
325 * device_del - delete device from system.
328 * This is the first part of the device unregistration
329 * sequence. This removes the device from the lists we control
330 * from here, has it removed from the other driver model
331 * subsystems it was added to in device_add(), and removes it
332 * from the kobject hierarchy.
334 * NOTE: this should be called manually _iff_ device_add() was
335 * also called manually.
338 void device_del(struct device
* dev
)
340 struct device
* parent
= dev
->parent
;
342 down_write(&devices_subsys
.rwsem
);
344 list_del_init(&dev
->node
);
345 up_write(&devices_subsys
.rwsem
);
347 /* Notify the platform of the removal, in case they
348 * need to do anything...
350 if (platform_notify_remove
)
351 platform_notify_remove(dev
);
352 bus_remove_device(dev
);
353 device_pm_remove(dev
);
354 kobject_hotplug(&dev
->kobj
, KOBJ_REMOVE
);
355 kobject_del(&dev
->kobj
);
361 * device_unregister - unregister device from system.
362 * @dev: device going away.
364 * We do this in two parts, like we do device_register(). First,
365 * we remove it from all the subsystems with device_del(), then
366 * we decrement the reference count via put_device(). If that
367 * is the final reference count, the device will be cleaned up
368 * via device_release() above. Otherwise, the structure will
369 * stick around until the final reference to the device is dropped.
371 void device_unregister(struct device
* dev
)
373 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
380 * device_for_each_child - device child iterator.
381 * @dev: parent struct device.
382 * @data: data for the callback.
383 * @fn: function to be called for each device.
385 * Iterate over @dev's child devices, and call @fn for each,
388 * We check the return of @fn each time. If it returns anything
389 * other than 0, we break out and return that value.
391 int device_for_each_child(struct device
* dev
, void * data
,
392 int (*fn
)(struct device
*, void *))
394 struct device
* child
;
397 down_read(&devices_subsys
.rwsem
);
398 list_for_each_entry(child
, &dev
->children
, node
) {
399 if((error
= fn(child
, data
)))
402 up_read(&devices_subsys
.rwsem
);
407 * device_find - locate device on a bus by name.
408 * @name: name of the device.
409 * @bus: bus to scan for the device.
411 * Call kset_find_obj() to iterate over list of devices on
412 * a bus to find device by name. Return device if found.
414 * Note that kset_find_obj increments device's reference count.
416 struct device
*device_find(const char *name
, struct bus_type
*bus
)
418 struct kobject
*k
= kset_find_obj(&bus
->devices
, name
);
424 int __init
devices_init(void)
426 return subsystem_register(&devices_subsys
);
429 EXPORT_SYMBOL_GPL(device_for_each_child
);
431 EXPORT_SYMBOL_GPL(device_initialize
);
432 EXPORT_SYMBOL_GPL(device_add
);
433 EXPORT_SYMBOL_GPL(device_register
);
435 EXPORT_SYMBOL_GPL(device_del
);
436 EXPORT_SYMBOL_GPL(device_unregister
);
437 EXPORT_SYMBOL_GPL(get_device
);
438 EXPORT_SYMBOL_GPL(put_device
);
439 EXPORT_SYMBOL_GPL(device_find
);
441 EXPORT_SYMBOL_GPL(device_create_file
);
442 EXPORT_SYMBOL_GPL(device_remove_file
);