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