| 1 | /*
|
|---|
| 2 | * based on 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 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
|
|---|
| 7 | * Copyright (c) 2006 Novell, Inc.
|
|---|
| 8 | *
|
|---|
| 9 | * This file is released under the GPLv2
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #include <linux/device.h>
|
|---|
| 14 | #include <linux/err.h>
|
|---|
| 15 | #include <linux/pci.h>
|
|---|
| 16 | #include <linux/init.h>
|
|---|
| 17 | #include <linux/module.h>
|
|---|
| 18 | #include <linux/slab.h>
|
|---|
| 19 | #include <linux/string.h>
|
|---|
| 20 | #include <linux/kdev_t.h>
|
|---|
| 21 | #include <linux/notifier.h>
|
|---|
| 22 | #include <linux/mutex.h>
|
|---|
| 23 | #include <linux/pm_runtime.h>
|
|---|
| 24 | #include <linux/sysfs.h>
|
|---|
| 25 | #include <linux/kref.h>
|
|---|
| 26 | #include <linux/kobject.h>
|
|---|
| 27 | #include <linux/lockdep.h>
|
|---|
| 28 | #include <linux/printk.h>
|
|---|
| 29 |
|
|---|
| 30 | struct device_private {
|
|---|
| 31 | /* struct klist klist_children;
|
|---|
| 32 | struct klist_node knode_parent;
|
|---|
| 33 | struct klist_node knode_driver;
|
|---|
| 34 | struct klist_node knode_bus;
|
|---|
| 35 | struct list_head deferred_probe;*/
|
|---|
| 36 | struct device *device;
|
|---|
| 37 | };
|
|---|
| 38 | /**
|
|---|
| 39 | * device_release - free device structure.
|
|---|
| 40 | * @kobj: device's kobject.
|
|---|
| 41 | *
|
|---|
| 42 | * This is called once the reference count for the object
|
|---|
| 43 | * reaches 0. We forward the call to the device's release
|
|---|
| 44 | * method, which should handle actually freeing the structure.
|
|---|
| 45 | */
|
|---|
| 46 | static void device_release(struct kobject *kobj)
|
|---|
| 47 | {
|
|---|
| 48 | struct device *dev = kobj_to_dev(kobj);
|
|---|
| 49 | struct device_private *p = dev->p;
|
|---|
| 50 |
|
|---|
| 51 | #if 0
|
|---|
| 52 | /*
|
|---|
| 53 | * Some platform devices are driven without driver attached
|
|---|
| 54 | * and managed resources may have been acquired. Make sure
|
|---|
| 55 | * all resources are released.
|
|---|
| 56 | *
|
|---|
| 57 | * Drivers still can add resources into device after device
|
|---|
| 58 | * is deleted but alive, so release devres here to avoid
|
|---|
| 59 | * possible memory leak.
|
|---|
| 60 | */
|
|---|
| 61 | devres_release_all(dev);
|
|---|
| 62 |
|
|---|
| 63 | if (dev->release)
|
|---|
| 64 | dev->release(dev);
|
|---|
| 65 | else if (dev->type && dev->type->release)
|
|---|
| 66 | dev->type->release(dev);
|
|---|
| 67 | else if (dev->class && dev->class->dev_release)
|
|---|
| 68 | dev->class->dev_release(dev);
|
|---|
| 69 | else
|
|---|
| 70 | WARN(1, KERN_ERR "Device '%s' does not have a release() "
|
|---|
| 71 | "function, it is broken and must be fixed.\n",
|
|---|
| 72 | dev_name(dev));
|
|---|
| 73 | #endif
|
|---|
| 74 | kfree(p);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | static const void *device_namespace(struct kobject *kobj)
|
|---|
| 78 | {
|
|---|
| 79 | struct device *dev = kobj_to_dev(kobj);
|
|---|
| 80 | const void *ns = NULL;
|
|---|
| 81 |
|
|---|
| 82 | if (dev->class && dev->class->ns_type)
|
|---|
| 83 | ns = dev->class->namespace(dev);
|
|---|
| 84 |
|
|---|
| 85 | return ns;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static struct kobj_type device_ktype = {
|
|---|
| 89 | .release = device_release,
|
|---|
| 90 | // .sysfs_ops = &dev_sysfs_ops,
|
|---|
| 91 | .namespace = device_namespace,
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | /* /sys/devices/ */
|
|---|
| 95 | struct kset *devices_kset;
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * device_initialize - init device structure.
|
|---|
| 99 | * @dev: device.
|
|---|
| 100 | *
|
|---|
| 101 | * This prepares the device for use by other layers by initializing
|
|---|
| 102 | * its fields.
|
|---|
| 103 | * It is the first half of device_register(), if called by
|
|---|
| 104 | * that function, though it can also be called separately, so one
|
|---|
| 105 | * may use @dev's fields. In particular, get_device()/put_device()
|
|---|
| 106 | * may be used for reference counting of @dev after calling this
|
|---|
| 107 | * function.
|
|---|
| 108 | *
|
|---|
| 109 | * All fields in @dev must be initialized by the caller to 0, except
|
|---|
| 110 | * for those explicitly set to some other value. The simplest
|
|---|
| 111 | * approach is to use kzalloc() to allocate the structure containing
|
|---|
| 112 | * @dev.
|
|---|
| 113 | *
|
|---|
| 114 | * NOTE: Use put_device() to give up your reference instead of freeing
|
|---|
| 115 | * @dev directly once you have called this function.
|
|---|
| 116 | */
|
|---|
| 117 | void device_initialize(struct device *dev)
|
|---|
| 118 | {
|
|---|
| 119 | dev->kobj.kset = devices_kset;
|
|---|
| 120 | kobject_init(&dev->kobj, &device_ktype);
|
|---|
| 121 | INIT_LIST_HEAD(&dev->dma_pools);
|
|---|
| 122 | mutex_init(&dev->mutex);
|
|---|
| 123 | lockdep_set_novalidate_class(&dev->mutex);
|
|---|
| 124 | spin_lock_init(&dev->devres_lock);
|
|---|
| 125 | INIT_LIST_HEAD(&dev->devres_head);
|
|---|
| 126 | // device_pm_init(dev);
|
|---|
| 127 | // set_dev_node(dev, -1);
|
|---|
| 128 | }
|
|---|
| 129 | EXPORT_SYMBOL_GPL(device_initialize);
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * get_device - increment reference count for device.
|
|---|
| 133 | * @dev: device.
|
|---|
| 134 | *
|
|---|
| 135 | * This simply forwards the call to kobject_get(), though
|
|---|
| 136 | * we do take care to provide for the case that we get a NULL
|
|---|
| 137 | * pointer passed in.
|
|---|
| 138 | */
|
|---|
| 139 | struct device *get_device(struct device *dev)
|
|---|
| 140 | {
|
|---|
| 141 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
|
|---|
| 142 | }
|
|---|
| 143 | EXPORT_SYMBOL_GPL(get_device);
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * put_device - decrement reference count.
|
|---|
| 147 | * @dev: device in question.
|
|---|
| 148 | */
|
|---|
| 149 | void put_device(struct device *dev)
|
|---|
| 150 | {
|
|---|
| 151 | /* might_sleep(); */
|
|---|
| 152 | if (dev)
|
|---|
| 153 | kobject_put(&dev->kobj);
|
|---|
| 154 | }
|
|---|
| 155 | EXPORT_SYMBOL_GPL(put_device);
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * dev_driver_string - Return a device's driver name, if at all possible
|
|---|
| 159 | * @dev: struct device to get the name of
|
|---|
| 160 | *
|
|---|
| 161 | * Will return the device's driver's name if it is bound to a device. If
|
|---|
| 162 | * the device is not bound to a driver, it will return the name of the bus
|
|---|
| 163 | * it is attached to. If it is not attached to a bus either, an empty
|
|---|
| 164 | * string will be returned.
|
|---|
| 165 | */
|
|---|
| 166 | const char *dev_driver_string(const struct device *dev)
|
|---|
| 167 | {
|
|---|
| 168 | struct device_driver *drv;
|
|---|
| 169 |
|
|---|
| 170 | /* dev->driver can change to NULL underneath us because of unbinding,
|
|---|
| 171 | * so be careful about accessing it. dev->bus and dev->class should
|
|---|
| 172 | * never change once they are set, so they don't need special care.
|
|---|
| 173 | */
|
|---|
| 174 | drv = dev->driver;
|
|---|
| 175 | return drv ? drv->name :
|
|---|
| 176 | (dev->bus ? dev->bus->name :
|
|---|
| 177 | (dev->class ? dev->class->name : ""));
|
|---|
| 178 | }
|
|---|