blob: 31109193e2c4f9c8d46780811d38db82ef244131 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static 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);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050039 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040042 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 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);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050052 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040055 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
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
Dmitry Torokhov419cab32005-04-26 02:32:54 -0500105static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
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
Alexander Nyberg177a4322005-02-26 13:38:51 +0100139 if (dev->bus && dev->bus->hotplug) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* 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
James Bottomley34bb61f2005-09-06 16:56:51 -0700194static void klist_children_get(struct klist_node *n)
195{
196 struct device *dev = container_of(n, struct device, knode_parent);
197
198 get_device(dev);
199}
200
201static void klist_children_put(struct klist_node *n)
202{
203 struct device *dev = container_of(n, struct device, knode_parent);
204
205 put_device(dev);
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/**
210 * device_initialize - init device structure.
211 * @dev: device.
212 *
213 * This prepares the device for use by other layers,
214 * including adding it to the device hierarchy.
215 * It is the first half of device_register(), if called by
216 * that, though it can also be called separately, so one
217 * may use @dev's fields (e.g. the refcount).
218 */
219
220void device_initialize(struct device *dev)
221{
222 kobj_set_kset_s(dev, devices_subsys);
223 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700224 klist_init(&dev->klist_children, klist_children_get,
225 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 INIT_LIST_HEAD(&dev->dma_pools);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800227 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700228 device_init_wakeup(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/**
232 * device_add - add device to device hierarchy.
233 * @dev: device.
234 *
235 * This is part 2 of device_register(), though may be called
236 * separately _iff_ device_initialize() has been called separately.
237 *
238 * This adds it to the kobject hierarchy via kobject_add(), adds it
239 * to the global and sibling lists for the device, then
240 * adds it to the other relevant subsystems of the driver model.
241 */
242int device_add(struct device *dev)
243{
244 struct device *parent = NULL;
245 int error = -EINVAL;
246
247 dev = get_device(dev);
248 if (!dev || !strlen(dev->bus_id))
249 goto Error;
250
251 parent = get_device(dev->parent);
252
253 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
254
255 /* first, register with generic layer. */
256 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
257 if (parent)
258 dev->kobj.parent = &parent->kobj;
259
260 if ((error = kobject_add(&dev->kobj)))
261 goto Error;
Kay Sievers187a1a92005-05-23 15:50:26 -0700262 kobject_hotplug(&dev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if ((error = device_pm_add(dev)))
264 goto PMError;
265 if ((error = bus_add_device(dev)))
266 goto BusError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400268 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* notify platform of device entry */
271 if (platform_notify)
272 platform_notify(dev);
273 Done:
274 put_device(dev);
275 return error;
276 BusError:
277 device_pm_remove(dev);
278 PMError:
Kay Sievers187a1a92005-05-23 15:50:26 -0700279 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 kobject_del(&dev->kobj);
281 Error:
282 if (parent)
283 put_device(parent);
284 goto Done;
285}
286
287
288/**
289 * device_register - register a device with the system.
290 * @dev: pointer to the device structure
291 *
292 * This happens in two clean steps - initialize the device
293 * and add it to the system. The two steps can be called
294 * separately, but this is the easiest and most common.
295 * I.e. you should only call the two helpers separately if
296 * have a clearly defined need to use and refcount the device
297 * before it is added to the hierarchy.
298 */
299
300int device_register(struct device *dev)
301{
302 device_initialize(dev);
303 return device_add(dev);
304}
305
306
307/**
308 * get_device - increment reference count for device.
309 * @dev: device.
310 *
311 * This simply forwards the call to kobject_get(), though
312 * we do take care to provide for the case that we get a NULL
313 * pointer passed in.
314 */
315
316struct device * get_device(struct device * dev)
317{
318 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
319}
320
321
322/**
323 * put_device - decrement reference count.
324 * @dev: device in question.
325 */
326void put_device(struct device * dev)
327{
328 if (dev)
329 kobject_put(&dev->kobj);
330}
331
332
333/**
334 * device_del - delete device from system.
335 * @dev: device.
336 *
337 * This is the first part of the device unregistration
338 * sequence. This removes the device from the lists we control
339 * from here, has it removed from the other driver model
340 * subsystems it was added to in device_add(), and removes it
341 * from the kobject hierarchy.
342 *
343 * NOTE: this should be called manually _iff_ device_add() was
344 * also called manually.
345 */
346
347void device_del(struct device * dev)
348{
349 struct device * parent = dev->parent;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700352 klist_del(&dev->knode_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Notify the platform of the removal, in case they
355 * need to do anything...
356 */
357 if (platform_notify_remove)
358 platform_notify_remove(dev);
359 bus_remove_device(dev);
360 device_pm_remove(dev);
kay.sievers@vrfy.orge57cd732005-04-18 21:57:36 -0700361 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 kobject_del(&dev->kobj);
363 if (parent)
364 put_device(parent);
365}
366
367/**
368 * device_unregister - unregister device from system.
369 * @dev: device going away.
370 *
371 * We do this in two parts, like we do device_register(). First,
372 * we remove it from all the subsystems with device_del(), then
373 * we decrement the reference count via put_device(). If that
374 * is the final reference count, the device will be cleaned up
375 * via device_release() above. Otherwise, the structure will
376 * stick around until the final reference to the device is dropped.
377 */
378void device_unregister(struct device * dev)
379{
380 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
381 device_del(dev);
382 put_device(dev);
383}
384
385
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800386static struct device * next_device(struct klist_iter * i)
387{
388 struct klist_node * n = klist_next(i);
389 return n ? container_of(n, struct device, knode_parent) : NULL;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/**
393 * device_for_each_child - device child iterator.
394 * @dev: parent struct device.
395 * @data: data for the callback.
396 * @fn: function to be called for each device.
397 *
398 * Iterate over @dev's child devices, and call @fn for each,
399 * passing it @data.
400 *
401 * We check the return of @fn each time. If it returns anything
402 * other than 0, we break out and return that value.
403 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800404int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int (*fn)(struct device *, void *))
406{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800407 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct device * child;
409 int error = 0;
410
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800411 klist_iter_init(&parent->klist_children, &i);
412 while ((child = next_device(&i)) && !error)
413 error = fn(child, data);
414 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return error;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418int __init devices_init(void)
419{
420 return subsystem_register(&devices_subsys);
421}
422
423EXPORT_SYMBOL_GPL(device_for_each_child);
424
425EXPORT_SYMBOL_GPL(device_initialize);
426EXPORT_SYMBOL_GPL(device_add);
427EXPORT_SYMBOL_GPL(device_register);
428
429EXPORT_SYMBOL_GPL(device_del);
430EXPORT_SYMBOL_GPL(device_unregister);
431EXPORT_SYMBOL_GPL(get_device);
432EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434EXPORT_SYMBOL_GPL(device_create_file);
435EXPORT_SYMBOL_GPL(device_remove_file);