MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / base / core.c
blob7079733d3e427c704aac52730c133593bf9515b7
1 /*
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
9 */
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>
21 #include "base.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[];
36 static ssize_t
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);
41 ssize_t ret = 0;
43 if (dev_attr->show)
44 ret = dev_attr->show(dev, buf);
45 return ret;
48 static ssize_t
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);
54 ssize_t ret = 0;
56 if (dev_attr->store)
57 ret = dev_attr->store(dev, buf, count);
58 return ret;
61 static struct sysfs_ops dev_sysfs_ops = {
62 .show = dev_attr_show,
63 .store = dev_attr_store,
67 /**
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);
79 if (dev->release)
80 dev->release(dev);
81 else {
82 printk(KERN_ERR "Device '%s' does not have a release() function, "
83 "it is broken and must be fixed.\n",
84 dev->bus_id);
85 WARN_ON(1);
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);
102 if (dev->bus)
103 return 1;
105 return 0;
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);
119 int retval = 0;
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);
124 if (retval) {
125 pr_debug ("%s - hotplug() returned %d\n",
126 __FUNCTION__, retval);
130 return 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.
148 * @dev: device.
149 * @attr: device attribute descriptor.
152 int device_create_file(struct device * dev, struct device_attribute * attr)
154 int error = 0;
155 if (get_device(dev)) {
156 error = sysfs_create_file(&dev->kobj, &attr->attr);
157 put_device(dev);
159 return error;
163 * device_remove_file - remove sysfs attribute file.
164 * @dev: device.
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);
172 put_device(dev);
178 * device_initialize - init device structure.
179 * @dev: device.
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.
201 * @dev: device.
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;
213 int error;
216 dev = get_device(dev);
217 if (!dev || !strlen(dev->bus_id))
218 return -EINVAL;
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);
226 if (parent)
227 dev->kobj.parent = &parent->kobj;
229 if ((error = kobject_add(&dev->kobj)))
230 goto Error;
231 if ((error = device_pm_add(dev)))
232 goto PMError;
236 if ((error = bus_add_device(dev)))
237 goto BusError;
240 down_write(&devices_subsys.rwsem);
241 if (parent)
242 list_add_tail(&dev->node, &parent->children);
243 up_write(&devices_subsys.rwsem);
245 /* notify platform of device entry */
246 if (platform_notify)
247 platform_notify(dev);
248 Done:
249 put_device(dev);
251 return error;
252 BusError:
253 device_pm_remove(dev);
254 PMError:
255 kobject_del(&dev->kobj);
256 Error:
257 if (parent)
258 put_device(parent);
259 goto Done;
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.
284 * @dev: 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
288 * pointer passed in.
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.
309 * @dev: device.
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);
326 if (parent)
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);
338 if (parent)
339 put_device(parent);
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);
356 device_del(dev);
357 put_device(dev);
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,
368 * passing it @data.
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;
377 int error = 0;
379 down_read(&devices_subsys.rwsem);
380 list_for_each_entry(child, &dev->children, node) {
381 if((error = fn(child, data)))
382 break;
384 up_read(&devices_subsys.rwsem);
385 return error;
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);
401 if (k)
402 return to_dev(k);
403 return NULL;
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);