[PATCH] Add a klist to struct bus_type for its drivers.
[deliverable/linux.git] / drivers / base / core.c
CommitLineData
1da177e4
LT
1/*
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>
18
19#include <asm/semaphore.h>
20
21#include "base.h"
22#include "power/power.h"
23
24int (*platform_notify)(struct device * dev) = NULL;
25int (*platform_notify_remove)(struct device * dev) = NULL;
26
27/*
28 * sysfs bindings for devices.
29 */
30
31#define to_dev(obj) container_of(obj, struct device, kobj)
32#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
33
1da177e4
LT
34static ssize_t
35dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
36{
37 struct device_attribute * dev_attr = to_dev_attr(attr);
38 struct device * dev = to_dev(kobj);
4a0c20bf 39 ssize_t ret = -EIO;
1da177e4
LT
40
41 if (dev_attr->show)
42 ret = dev_attr->show(dev, buf);
43 return ret;
44}
45
46static ssize_t
47dev_attr_store(struct kobject * kobj, struct attribute * attr,
48 const char * buf, size_t count)
49{
50 struct device_attribute * dev_attr = to_dev_attr(attr);
51 struct device * dev = to_dev(kobj);
4a0c20bf 52 ssize_t ret = -EIO;
1da177e4
LT
53
54 if (dev_attr->store)
55 ret = dev_attr->store(dev, buf, count);
56 return ret;
57}
58
59static struct sysfs_ops dev_sysfs_ops = {
60 .show = dev_attr_show,
61 .store = dev_attr_store,
62};
63
64
65/**
66 * device_release - free device structure.
67 * @kobj: device's kobject.
68 *
69 * This is called once the reference count for the object
70 * reaches 0. We forward the call to the device's release
71 * method, which should handle actually freeing the structure.
72 */
73static void device_release(struct kobject * kobj)
74{
75 struct device * dev = to_dev(kobj);
76
77 if (dev->release)
78 dev->release(dev);
79 else {
80 printk(KERN_ERR "Device '%s' does not have a release() function, "
81 "it is broken and must be fixed.\n",
82 dev->bus_id);
83 WARN_ON(1);
84 }
85}
86
87static struct kobj_type ktype_device = {
88 .release = device_release,
89 .sysfs_ops = &dev_sysfs_ops,
1da177e4
LT
90};
91
92
93static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
94{
95 struct kobj_type *ktype = get_ktype(kobj);
96
97 if (ktype == &ktype_device) {
98 struct device *dev = to_dev(kobj);
99 if (dev->bus)
100 return 1;
101 }
102 return 0;
103}
104
419cab3f 105static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
1da177e4
LT
106{
107 struct device *dev = to_dev(kobj);
108
109 return dev->bus->name;
110}
111
112static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
113 int num_envp, char *buffer, int buffer_size)
114{
115 struct device *dev = to_dev(kobj);
116 int i = 0;
117 int length = 0;
118 int retval = 0;
119
120 /* add bus name of physical device */
121 if (dev->bus)
122 add_hotplug_env_var(envp, num_envp, &i,
123 buffer, buffer_size, &length,
124 "PHYSDEVBUS=%s", dev->bus->name);
125
126 /* add driver name of physical device */
127 if (dev->driver)
128 add_hotplug_env_var(envp, num_envp, &i,
129 buffer, buffer_size, &length,
130 "PHYSDEVDRIVER=%s", dev->driver->name);
131
132 /* terminate, set to next free slot, shrink available space */
133 envp[i] = NULL;
134 envp = &envp[i];
135 num_envp -= i;
136 buffer = &buffer[length];
137 buffer_size -= length;
138
177a4324 139 if (dev->bus && dev->bus->hotplug) {
1da177e4
LT
140 /* have the bus specific function add its stuff */
141 retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size);
142 if (retval) {
143 pr_debug ("%s - hotplug() returned %d\n",
144 __FUNCTION__, retval);
145 }
146 }
147
148 return retval;
149}
150
151static struct kset_hotplug_ops device_hotplug_ops = {
152 .filter = dev_hotplug_filter,
153 .name = dev_hotplug_name,
154 .hotplug = dev_hotplug,
155};
156
157/**
158 * device_subsys - structure to be registered with kobject core.
159 */
160
161decl_subsys(devices, &ktype_device, &device_hotplug_ops);
162
163
164/**
165 * device_create_file - create sysfs attribute file for device.
166 * @dev: device.
167 * @attr: device attribute descriptor.
168 */
169
170int device_create_file(struct device * dev, struct device_attribute * attr)
171{
172 int error = 0;
173 if (get_device(dev)) {
174 error = sysfs_create_file(&dev->kobj, &attr->attr);
175 put_device(dev);
176 }
177 return error;
178}
179
180/**
181 * device_remove_file - remove sysfs attribute file.
182 * @dev: device.
183 * @attr: device attribute descriptor.
184 */
185
186void device_remove_file(struct device * dev, struct device_attribute * attr)
187{
188 if (get_device(dev)) {
189 sysfs_remove_file(&dev->kobj, &attr->attr);
190 put_device(dev);
191 }
192}
193
194
195/**
196 * device_initialize - init device structure.
197 * @dev: device.
198 *
199 * This prepares the device for use by other layers,
200 * including adding it to the device hierarchy.
201 * It is the first half of device_register(), if called by
202 * that, though it can also be called separately, so one
203 * may use @dev's fields (e.g. the refcount).
204 */
205
206void device_initialize(struct device *dev)
207{
208 kobj_set_kset_s(dev, devices_subsys);
209 kobject_init(&dev->kobj);
210 INIT_LIST_HEAD(&dev->node);
211 INIT_LIST_HEAD(&dev->children);
212 INIT_LIST_HEAD(&dev->driver_list);
213 INIT_LIST_HEAD(&dev->bus_list);
214 INIT_LIST_HEAD(&dev->dma_pools);
af70316a 215 init_MUTEX(&dev->sem);
1da177e4
LT
216}
217
218/**
219 * device_add - add device to device hierarchy.
220 * @dev: device.
221 *
222 * This is part 2 of device_register(), though may be called
223 * separately _iff_ device_initialize() has been called separately.
224 *
225 * This adds it to the kobject hierarchy via kobject_add(), adds it
226 * to the global and sibling lists for the device, then
227 * adds it to the other relevant subsystems of the driver model.
228 */
229int device_add(struct device *dev)
230{
231 struct device *parent = NULL;
232 int error = -EINVAL;
233
234 dev = get_device(dev);
235 if (!dev || !strlen(dev->bus_id))
236 goto Error;
237
238 parent = get_device(dev->parent);
239
240 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
241
242 /* first, register with generic layer. */
243 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
244 if (parent)
245 dev->kobj.parent = &parent->kobj;
246
247 if ((error = kobject_add(&dev->kobj)))
248 goto Error;
187a1a94 249 kobject_hotplug(&dev->kobj, KOBJ_ADD);
1da177e4
LT
250 if ((error = device_pm_add(dev)))
251 goto PMError;
252 if ((error = bus_add_device(dev)))
253 goto BusError;
254 down_write(&devices_subsys.rwsem);
255 if (parent)
256 list_add_tail(&dev->node, &parent->children);
257 up_write(&devices_subsys.rwsem);
258
259 /* notify platform of device entry */
260 if (platform_notify)
261 platform_notify(dev);
262 Done:
263 put_device(dev);
264 return error;
265 BusError:
266 device_pm_remove(dev);
267 PMError:
187a1a94 268 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
269 kobject_del(&dev->kobj);
270 Error:
271 if (parent)
272 put_device(parent);
273 goto Done;
274}
275
276
277/**
278 * device_register - register a device with the system.
279 * @dev: pointer to the device structure
280 *
281 * This happens in two clean steps - initialize the device
282 * and add it to the system. The two steps can be called
283 * separately, but this is the easiest and most common.
284 * I.e. you should only call the two helpers separately if
285 * have a clearly defined need to use and refcount the device
286 * before it is added to the hierarchy.
287 */
288
289int device_register(struct device *dev)
290{
291 device_initialize(dev);
292 return device_add(dev);
293}
294
295
296/**
297 * get_device - increment reference count for device.
298 * @dev: device.
299 *
300 * This simply forwards the call to kobject_get(), though
301 * we do take care to provide for the case that we get a NULL
302 * pointer passed in.
303 */
304
305struct device * get_device(struct device * dev)
306{
307 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
308}
309
310
311/**
312 * put_device - decrement reference count.
313 * @dev: device in question.
314 */
315void put_device(struct device * dev)
316{
317 if (dev)
318 kobject_put(&dev->kobj);
319}
320
321
322/**
323 * device_del - delete device from system.
324 * @dev: device.
325 *
326 * This is the first part of the device unregistration
327 * sequence. This removes the device from the lists we control
328 * from here, has it removed from the other driver model
329 * subsystems it was added to in device_add(), and removes it
330 * from the kobject hierarchy.
331 *
332 * NOTE: this should be called manually _iff_ device_add() was
333 * also called manually.
334 */
335
336void device_del(struct device * dev)
337{
338 struct device * parent = dev->parent;
339
340 down_write(&devices_subsys.rwsem);
341 if (parent)
342 list_del_init(&dev->node);
343 up_write(&devices_subsys.rwsem);
344
345 /* Notify the platform of the removal, in case they
346 * need to do anything...
347 */
348 if (platform_notify_remove)
349 platform_notify_remove(dev);
350 bus_remove_device(dev);
351 device_pm_remove(dev);
e57cd73e 352 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
353 kobject_del(&dev->kobj);
354 if (parent)
355 put_device(parent);
356}
357
358/**
359 * device_unregister - unregister device from system.
360 * @dev: device going away.
361 *
362 * We do this in two parts, like we do device_register(). First,
363 * we remove it from all the subsystems with device_del(), then
364 * we decrement the reference count via put_device(). If that
365 * is the final reference count, the device will be cleaned up
366 * via device_release() above. Otherwise, the structure will
367 * stick around until the final reference to the device is dropped.
368 */
369void device_unregister(struct device * dev)
370{
371 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
372 device_del(dev);
373 put_device(dev);
374}
375
376
377/**
378 * device_for_each_child - device child iterator.
379 * @dev: parent struct device.
380 * @data: data for the callback.
381 * @fn: function to be called for each device.
382 *
383 * Iterate over @dev's child devices, and call @fn for each,
384 * passing it @data.
385 *
386 * We check the return of @fn each time. If it returns anything
387 * other than 0, we break out and return that value.
388 */
389int device_for_each_child(struct device * dev, void * data,
390 int (*fn)(struct device *, void *))
391{
392 struct device * child;
393 int error = 0;
394
395 down_read(&devices_subsys.rwsem);
396 list_for_each_entry(child, &dev->children, node) {
397 if((error = fn(child, data)))
398 break;
399 }
400 up_read(&devices_subsys.rwsem);
401 return error;
402}
403
404/**
405 * device_find - locate device on a bus by name.
406 * @name: name of the device.
407 * @bus: bus to scan for the device.
408 *
409 * Call kset_find_obj() to iterate over list of devices on
410 * a bus to find device by name. Return device if found.
411 *
412 * Note that kset_find_obj increments device's reference count.
413 */
414struct device *device_find(const char *name, struct bus_type *bus)
415{
416 struct kobject *k = kset_find_obj(&bus->devices, name);
417 if (k)
418 return to_dev(k);
419 return NULL;
420}
421
422int __init devices_init(void)
423{
424 return subsystem_register(&devices_subsys);
425}
426
427EXPORT_SYMBOL_GPL(device_for_each_child);
428
429EXPORT_SYMBOL_GPL(device_initialize);
430EXPORT_SYMBOL_GPL(device_add);
431EXPORT_SYMBOL_GPL(device_register);
432
433EXPORT_SYMBOL_GPL(device_del);
434EXPORT_SYMBOL_GPL(device_unregister);
435EXPORT_SYMBOL_GPL(get_device);
436EXPORT_SYMBOL_GPL(put_device);
437EXPORT_SYMBOL_GPL(device_find);
438
439EXPORT_SYMBOL_GPL(device_create_file);
440EXPORT_SYMBOL_GPL(device_remove_file);
This page took 0.058507 seconds and 5 git commands to generate.