ia64/linux-2.6.18-xen.hg

view drivers/base/core.c @ 881:e9f508296fc7

pcie io space multiplex: backport of bus event notification patch

back port of 116af378201ef793424cd10508ccf18b06d8a021 and
ec0676ee28528dc8dda13a93ee4b1f215a0c2f9d.

commit 116af378201ef793424cd10508ccf18b06d8a021
Author: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Date: Wed Oct 25 13:44:59 2006 +1000

Driver core: add notification of bus events

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>


commit ec0676ee28528dc8dda13a93ee4b1f215a0c2f9d
Author: Alan Stern <stern@rowland.harvard.edu>
Date: Fri Dec 5 14:10:31 2008 -0500

Driver core: move the bus notifier call points

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 09:56:11 2009 +0100 (2009-05-28)
parents 831230e53067
children
line source
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 */
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/kdev_t.h>
18 #include <linux/notifier.h>
20 #include <asm/semaphore.h>
22 #include "base.h"
23 #include "power/power.h"
25 int (*platform_notify)(struct device * dev) = NULL;
26 int (*platform_notify_remove)(struct device * dev) = NULL;
28 /*
29 * sysfs bindings for devices.
30 */
32 /**
33 * dev_driver_string - Return a device's driver name, if at all possible
34 * @dev: struct device to get the name of
35 *
36 * Will return the device's driver's name if it is bound to a device. If
37 * the device is not bound to a device, it will return the name of the bus
38 * it is attached to. If it is not attached to a bus either, an empty
39 * string will be returned.
40 */
41 const char *dev_driver_string(struct device *dev)
42 {
43 return dev->driver ? dev->driver->name :
44 (dev->bus ? dev->bus->name : "");
45 }
46 EXPORT_SYMBOL_GPL(dev_driver_string);
48 #define to_dev(obj) container_of(obj, struct device, kobj)
49 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
51 static ssize_t
52 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
53 {
54 struct device_attribute * dev_attr = to_dev_attr(attr);
55 struct device * dev = to_dev(kobj);
56 ssize_t ret = -EIO;
58 if (dev_attr->show)
59 ret = dev_attr->show(dev, dev_attr, buf);
60 return ret;
61 }
63 static ssize_t
64 dev_attr_store(struct kobject * kobj, struct attribute * attr,
65 const char * buf, size_t count)
66 {
67 struct device_attribute * dev_attr = to_dev_attr(attr);
68 struct device * dev = to_dev(kobj);
69 ssize_t ret = -EIO;
71 if (dev_attr->store)
72 ret = dev_attr->store(dev, dev_attr, buf, count);
73 return ret;
74 }
76 static struct sysfs_ops dev_sysfs_ops = {
77 .show = dev_attr_show,
78 .store = dev_attr_store,
79 };
82 /**
83 * device_release - free device structure.
84 * @kobj: device's kobject.
85 *
86 * This is called once the reference count for the object
87 * reaches 0. We forward the call to the device's release
88 * method, which should handle actually freeing the structure.
89 */
90 static void device_release(struct kobject * kobj)
91 {
92 struct device * dev = to_dev(kobj);
94 if (dev->release)
95 dev->release(dev);
96 else {
97 printk(KERN_ERR "Device '%s' does not have a release() function, "
98 "it is broken and must be fixed.\n",
99 dev->bus_id);
100 WARN_ON(1);
101 }
102 }
104 static struct kobj_type ktype_device = {
105 .release = device_release,
106 .sysfs_ops = &dev_sysfs_ops,
107 };
110 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
111 {
112 struct kobj_type *ktype = get_ktype(kobj);
114 if (ktype == &ktype_device) {
115 struct device *dev = to_dev(kobj);
116 if (dev->bus)
117 return 1;
118 if (dev->class)
119 return 1;
120 }
121 return 0;
122 }
124 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
125 {
126 struct device *dev = to_dev(kobj);
128 if (dev->bus)
129 return dev->bus->name;
130 if (dev->class)
131 return dev->class->name;
132 return NULL;
133 }
135 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
136 int num_envp, char *buffer, int buffer_size)
137 {
138 struct device *dev = to_dev(kobj);
139 int i = 0;
140 int length = 0;
141 int retval = 0;
143 /* add the major/minor if present */
144 if (MAJOR(dev->devt)) {
145 add_uevent_var(envp, num_envp, &i,
146 buffer, buffer_size, &length,
147 "MAJOR=%u", MAJOR(dev->devt));
148 add_uevent_var(envp, num_envp, &i,
149 buffer, buffer_size, &length,
150 "MINOR=%u", MINOR(dev->devt));
151 }
153 /* add bus name of physical device */
154 if (dev->bus)
155 add_uevent_var(envp, num_envp, &i,
156 buffer, buffer_size, &length,
157 "PHYSDEVBUS=%s", dev->bus->name);
159 /* add driver name of physical device */
160 if (dev->driver)
161 add_uevent_var(envp, num_envp, &i,
162 buffer, buffer_size, &length,
163 "PHYSDEVDRIVER=%s", dev->driver->name);
165 /* terminate, set to next free slot, shrink available space */
166 envp[i] = NULL;
167 envp = &envp[i];
168 num_envp -= i;
169 buffer = &buffer[length];
170 buffer_size -= length;
172 if (dev->bus && dev->bus->uevent) {
173 /* have the bus specific function add its stuff */
174 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
175 if (retval) {
176 pr_debug ("%s - uevent() returned %d\n",
177 __FUNCTION__, retval);
178 }
179 }
181 return retval;
182 }
184 static struct kset_uevent_ops device_uevent_ops = {
185 .filter = dev_uevent_filter,
186 .name = dev_uevent_name,
187 .uevent = dev_uevent,
188 };
190 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
192 {
193 kobject_uevent(&dev->kobj, KOBJ_ADD);
194 return count;
195 }
197 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
198 char *buf)
199 {
200 return print_dev_t(buf, dev->devt);
201 }
203 /*
204 * devices_subsys - structure to be registered with kobject core.
205 */
207 decl_subsys(devices, &ktype_device, &device_uevent_ops);
210 /**
211 * device_create_file - create sysfs attribute file for device.
212 * @dev: device.
213 * @attr: device attribute descriptor.
214 */
216 int device_create_file(struct device * dev, struct device_attribute * attr)
217 {
218 int error = 0;
219 if (get_device(dev)) {
220 error = sysfs_create_file(&dev->kobj, &attr->attr);
221 put_device(dev);
222 }
223 return error;
224 }
226 /**
227 * device_remove_file - remove sysfs attribute file.
228 * @dev: device.
229 * @attr: device attribute descriptor.
230 */
232 void device_remove_file(struct device * dev, struct device_attribute * attr)
233 {
234 if (get_device(dev)) {
235 sysfs_remove_file(&dev->kobj, &attr->attr);
236 put_device(dev);
237 }
238 }
240 static void klist_children_get(struct klist_node *n)
241 {
242 struct device *dev = container_of(n, struct device, knode_parent);
244 get_device(dev);
245 }
247 static void klist_children_put(struct klist_node *n)
248 {
249 struct device *dev = container_of(n, struct device, knode_parent);
251 put_device(dev);
252 }
255 /**
256 * device_initialize - init device structure.
257 * @dev: device.
258 *
259 * This prepares the device for use by other layers,
260 * including adding it to the device hierarchy.
261 * It is the first half of device_register(), if called by
262 * that, though it can also be called separately, so one
263 * may use @dev's fields (e.g. the refcount).
264 */
266 void device_initialize(struct device *dev)
267 {
268 kobj_set_kset_s(dev, devices_subsys);
269 kobject_init(&dev->kobj);
270 klist_init(&dev->klist_children, klist_children_get,
271 klist_children_put);
272 INIT_LIST_HEAD(&dev->dma_pools);
273 INIT_LIST_HEAD(&dev->node);
274 init_MUTEX(&dev->sem);
275 device_init_wakeup(dev, 0);
276 }
278 /**
279 * device_add - add device to device hierarchy.
280 * @dev: device.
281 *
282 * This is part 2 of device_register(), though may be called
283 * separately _iff_ device_initialize() has been called separately.
284 *
285 * This adds it to the kobject hierarchy via kobject_add(), adds it
286 * to the global and sibling lists for the device, then
287 * adds it to the other relevant subsystems of the driver model.
288 */
289 int device_add(struct device *dev)
290 {
291 struct device *parent = NULL;
292 char *class_name = NULL;
293 int error = -EINVAL;
295 dev = get_device(dev);
296 if (!dev || !strlen(dev->bus_id))
297 goto Error;
299 parent = get_device(dev->parent);
301 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
303 /* first, register with generic layer. */
304 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
305 if (parent)
306 dev->kobj.parent = &parent->kobj;
308 if ((error = kobject_add(&dev->kobj)))
309 goto Error;
311 dev->uevent_attr.attr.name = "uevent";
312 dev->uevent_attr.attr.mode = S_IWUSR;
313 if (dev->driver)
314 dev->uevent_attr.attr.owner = dev->driver->owner;
315 dev->uevent_attr.store = store_uevent;
316 device_create_file(dev, &dev->uevent_attr);
318 if (MAJOR(dev->devt)) {
319 struct device_attribute *attr;
320 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
321 if (!attr) {
322 error = -ENOMEM;
323 goto PMError;
324 }
325 attr->attr.name = "dev";
326 attr->attr.mode = S_IRUGO;
327 if (dev->driver)
328 attr->attr.owner = dev->driver->owner;
329 attr->show = show_dev;
330 error = device_create_file(dev, attr);
331 if (error) {
332 kfree(attr);
333 goto attrError;
334 }
336 dev->devt_attr = attr;
337 }
339 if (dev->class) {
340 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
341 "subsystem");
342 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
343 dev->bus_id);
345 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
346 class_name = make_class_name(dev->class->name, &dev->kobj);
347 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
348 }
350 if ((error = device_pm_add(dev)))
351 goto PMError;
352 if ((error = bus_add_device(dev)))
353 goto BusError;
355 /* Notify clients of device addition. This call must come
356 * after dpm_sysf_add() and before kobject_uevent().
357 */
358 if (dev->bus)
359 blocking_notifier_call_chain(&dev->bus->bus_notifier,
360 BUS_NOTIFY_ADD_DEVICE, dev);
362 kobject_uevent(&dev->kobj, KOBJ_ADD);
363 bus_attach_device(dev);
364 if (parent)
365 klist_add_tail(&dev->knode_parent, &parent->klist_children);
367 if (dev->class) {
368 /* tie the class to the device */
369 down(&dev->class->sem);
370 list_add_tail(&dev->node, &dev->class->devices);
371 up(&dev->class->sem);
372 }
374 /* notify platform of device entry */
375 if (platform_notify)
376 platform_notify(dev);
377 Done:
378 kfree(class_name);
379 put_device(dev);
380 return error;
381 BusError:
382 device_pm_remove(dev);
383 PMError:
384 if (dev->devt_attr) {
385 device_remove_file(dev, dev->devt_attr);
386 kfree(dev->devt_attr);
387 }
388 attrError:
389 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
390 kobject_del(&dev->kobj);
391 Error:
392 if (parent)
393 put_device(parent);
394 goto Done;
395 }
398 /**
399 * device_register - register a device with the system.
400 * @dev: pointer to the device structure
401 *
402 * This happens in two clean steps - initialize the device
403 * and add it to the system. The two steps can be called
404 * separately, but this is the easiest and most common.
405 * I.e. you should only call the two helpers separately if
406 * have a clearly defined need to use and refcount the device
407 * before it is added to the hierarchy.
408 */
410 int device_register(struct device *dev)
411 {
412 device_initialize(dev);
413 return device_add(dev);
414 }
417 /**
418 * get_device - increment reference count for device.
419 * @dev: device.
420 *
421 * This simply forwards the call to kobject_get(), though
422 * we do take care to provide for the case that we get a NULL
423 * pointer passed in.
424 */
426 struct device * get_device(struct device * dev)
427 {
428 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
429 }
432 /**
433 * put_device - decrement reference count.
434 * @dev: device in question.
435 */
436 void put_device(struct device * dev)
437 {
438 if (dev)
439 kobject_put(&dev->kobj);
440 }
443 /**
444 * device_del - delete device from system.
445 * @dev: device.
446 *
447 * This is the first part of the device unregistration
448 * sequence. This removes the device from the lists we control
449 * from here, has it removed from the other driver model
450 * subsystems it was added to in device_add(), and removes it
451 * from the kobject hierarchy.
452 *
453 * NOTE: this should be called manually _iff_ device_add() was
454 * also called manually.
455 */
457 void device_del(struct device * dev)
458 {
459 struct device * parent = dev->parent;
460 char *class_name = NULL;
462 /* Notify clients of device removal. This call must come
463 * before dpm_sysfs_remove().
464 */
465 if (dev->bus)
466 blocking_notifier_call_chain(&dev->bus->bus_notifier,
467 BUS_NOTIFY_DEL_DEVICE, dev);
468 if (parent)
469 klist_del(&dev->knode_parent);
470 if (dev->devt_attr)
471 device_remove_file(dev, dev->devt_attr);
472 if (dev->class) {
473 sysfs_remove_link(&dev->kobj, "subsystem");
474 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
475 class_name = make_class_name(dev->class->name, &dev->kobj);
476 sysfs_remove_link(&dev->kobj, "device");
477 sysfs_remove_link(&dev->parent->kobj, class_name);
478 kfree(class_name);
479 down(&dev->class->sem);
480 list_del_init(&dev->node);
481 up(&dev->class->sem);
482 }
483 device_remove_file(dev, &dev->uevent_attr);
485 /* Notify the platform of the removal, in case they
486 * need to do anything...
487 */
488 if (platform_notify_remove)
489 platform_notify_remove(dev);
490 bus_remove_device(dev);
491 device_pm_remove(dev);
492 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
493 kobject_del(&dev->kobj);
494 if (parent)
495 put_device(parent);
496 }
498 /**
499 * device_unregister - unregister device from system.
500 * @dev: device going away.
501 *
502 * We do this in two parts, like we do device_register(). First,
503 * we remove it from all the subsystems with device_del(), then
504 * we decrement the reference count via put_device(). If that
505 * is the final reference count, the device will be cleaned up
506 * via device_release() above. Otherwise, the structure will
507 * stick around until the final reference to the device is dropped.
508 */
509 void device_unregister(struct device * dev)
510 {
511 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
512 device_del(dev);
513 put_device(dev);
514 }
517 static struct device * next_device(struct klist_iter * i)
518 {
519 struct klist_node * n = klist_next(i);
520 return n ? container_of(n, struct device, knode_parent) : NULL;
521 }
523 /**
524 * device_for_each_child - device child iterator.
525 * @parent: parent struct device.
526 * @data: data for the callback.
527 * @fn: function to be called for each device.
528 *
529 * Iterate over @parent's child devices, and call @fn for each,
530 * passing it @data.
531 *
532 * We check the return of @fn each time. If it returns anything
533 * other than 0, we break out and return that value.
534 */
535 int device_for_each_child(struct device * parent, void * data,
536 int (*fn)(struct device *, void *))
537 {
538 struct klist_iter i;
539 struct device * child;
540 int error = 0;
542 klist_iter_init(&parent->klist_children, &i);
543 while ((child = next_device(&i)) && !error)
544 error = fn(child, data);
545 klist_iter_exit(&i);
546 return error;
547 }
549 int __init devices_init(void)
550 {
551 return subsystem_register(&devices_subsys);
552 }
554 EXPORT_SYMBOL_GPL(device_for_each_child);
556 EXPORT_SYMBOL_GPL(device_initialize);
557 EXPORT_SYMBOL_GPL(device_add);
558 EXPORT_SYMBOL_GPL(device_register);
560 EXPORT_SYMBOL_GPL(device_del);
561 EXPORT_SYMBOL_GPL(device_unregister);
562 EXPORT_SYMBOL_GPL(get_device);
563 EXPORT_SYMBOL_GPL(put_device);
565 EXPORT_SYMBOL_GPL(device_create_file);
566 EXPORT_SYMBOL_GPL(device_remove_file);
569 static void device_create_release(struct device *dev)
570 {
571 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
572 kfree(dev);
573 }
575 /**
576 * device_create - creates a device and registers it with sysfs
577 * @class: pointer to the struct class that this device should be registered to
578 * @parent: pointer to the parent struct device of this new device, if any
579 * @devt: the dev_t for the char device to be added
580 * @fmt: string for the device's name
581 *
582 * This function can be used by char device classes. A struct device
583 * will be created in sysfs, registered to the specified class.
584 *
585 * A "dev" file will be created, showing the dev_t for the device, if
586 * the dev_t is not 0,0.
587 * If a pointer to a parent struct device is passed in, the newly created
588 * struct device will be a child of that device in sysfs.
589 * The pointer to the struct device will be returned from the call.
590 * Any further sysfs files that might be required can be created using this
591 * pointer.
592 *
593 * Note: the struct class passed to this function must have previously
594 * been created with a call to class_create().
595 */
596 struct device *device_create(struct class *class, struct device *parent,
597 dev_t devt, char *fmt, ...)
598 {
599 va_list args;
600 struct device *dev = NULL;
601 int retval = -ENODEV;
603 if (class == NULL || IS_ERR(class))
604 goto error;
605 if (parent == NULL) {
606 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
607 goto error;
608 }
610 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
611 if (!dev) {
612 retval = -ENOMEM;
613 goto error;
614 }
616 dev->devt = devt;
617 dev->class = class;
618 dev->parent = parent;
619 dev->release = device_create_release;
621 va_start(args, fmt);
622 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
623 va_end(args);
624 retval = device_register(dev);
625 if (retval)
626 goto error;
628 return dev;
630 error:
631 kfree(dev);
632 return ERR_PTR(retval);
633 }
634 EXPORT_SYMBOL_GPL(device_create);
636 /**
637 * device_destroy - removes a device that was created with device_create()
638 * @class: pointer to the struct class that this device was registered with
639 * @devt: the dev_t of the device that was previously registered
640 *
641 * This call unregisters and cleans up a device that was created with a
642 * call to device_create().
643 */
644 void device_destroy(struct class *class, dev_t devt)
645 {
646 struct device *dev = NULL;
647 struct device *dev_tmp;
649 down(&class->sem);
650 list_for_each_entry(dev_tmp, &class->devices, node) {
651 if (dev_tmp->devt == devt) {
652 dev = dev_tmp;
653 break;
654 }
655 }
656 up(&class->sem);
658 if (dev)
659 device_unregister(dev);
660 }
661 EXPORT_SYMBOL_GPL(device_destroy);