]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | int (*platform_notify)(struct device * dev) = NULL; | |
25 | int (*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 |
34 | static ssize_t |
35 | dev_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) | |
54b6f35c | 42 | ret = dev_attr->show(dev, dev_attr, buf); |
1da177e4 LT |
43 | return ret; |
44 | } | |
45 | ||
46 | static ssize_t | |
47 | dev_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) | |
54b6f35c | 55 | ret = dev_attr->store(dev, dev_attr, buf, count); |
1da177e4 LT |
56 | return ret; |
57 | } | |
58 | ||
59 | static 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 | */ | |
73 | static 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 | ||
87 | static struct kobj_type ktype_device = { | |
88 | .release = device_release, | |
89 | .sysfs_ops = &dev_sysfs_ops, | |
1da177e4 LT |
90 | }; |
91 | ||
92 | ||
312c004d | 93 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
1da177e4 LT |
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 | ||
312c004d | 105 | static const char *dev_uevent_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 | ||
312c004d | 112 | static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
1da177e4 LT |
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) | |
312c004d KS |
122 | add_uevent_var(envp, num_envp, &i, |
123 | buffer, buffer_size, &length, | |
124 | "PHYSDEVBUS=%s", dev->bus->name); | |
1da177e4 LT |
125 | |
126 | /* add driver name of physical device */ | |
127 | if (dev->driver) | |
312c004d KS |
128 | add_uevent_var(envp, num_envp, &i, |
129 | buffer, buffer_size, &length, | |
130 | "PHYSDEVDRIVER=%s", dev->driver->name); | |
1da177e4 LT |
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 | ||
312c004d | 139 | if (dev->bus && dev->bus->uevent) { |
1da177e4 | 140 | /* have the bus specific function add its stuff */ |
312c004d | 141 | retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); |
1da177e4 | 142 | if (retval) { |
312c004d | 143 | pr_debug ("%s - uevent() returned %d\n", |
1da177e4 LT |
144 | __FUNCTION__, retval); |
145 | } | |
146 | } | |
147 | ||
148 | return retval; | |
149 | } | |
150 | ||
312c004d KS |
151 | static struct kset_uevent_ops device_uevent_ops = { |
152 | .filter = dev_uevent_filter, | |
153 | .name = dev_uevent_name, | |
154 | .uevent = dev_uevent, | |
1da177e4 LT |
155 | }; |
156 | ||
a7fd6706 KS |
157 | static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, |
158 | const char *buf, size_t count) | |
159 | { | |
312c004d | 160 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
a7fd6706 KS |
161 | return count; |
162 | } | |
163 | ||
0863afb3 MW |
164 | /* |
165 | * devices_subsys - structure to be registered with kobject core. | |
1da177e4 LT |
166 | */ |
167 | ||
312c004d | 168 | decl_subsys(devices, &ktype_device, &device_uevent_ops); |
1da177e4 LT |
169 | |
170 | ||
171 | /** | |
172 | * device_create_file - create sysfs attribute file for device. | |
173 | * @dev: device. | |
174 | * @attr: device attribute descriptor. | |
175 | */ | |
176 | ||
177 | int device_create_file(struct device * dev, struct device_attribute * attr) | |
178 | { | |
179 | int error = 0; | |
180 | if (get_device(dev)) { | |
181 | error = sysfs_create_file(&dev->kobj, &attr->attr); | |
182 | put_device(dev); | |
183 | } | |
184 | return error; | |
185 | } | |
186 | ||
187 | /** | |
188 | * device_remove_file - remove sysfs attribute file. | |
189 | * @dev: device. | |
190 | * @attr: device attribute descriptor. | |
191 | */ | |
192 | ||
193 | void device_remove_file(struct device * dev, struct device_attribute * attr) | |
194 | { | |
195 | if (get_device(dev)) { | |
196 | sysfs_remove_file(&dev->kobj, &attr->attr); | |
197 | put_device(dev); | |
198 | } | |
199 | } | |
200 | ||
34bb61f9 JB |
201 | static void klist_children_get(struct klist_node *n) |
202 | { | |
203 | struct device *dev = container_of(n, struct device, knode_parent); | |
204 | ||
205 | get_device(dev); | |
206 | } | |
207 | ||
208 | static void klist_children_put(struct klist_node *n) | |
209 | { | |
210 | struct device *dev = container_of(n, struct device, knode_parent); | |
211 | ||
212 | put_device(dev); | |
213 | } | |
214 | ||
1da177e4 LT |
215 | |
216 | /** | |
217 | * device_initialize - init device structure. | |
218 | * @dev: device. | |
219 | * | |
220 | * This prepares the device for use by other layers, | |
221 | * including adding it to the device hierarchy. | |
222 | * It is the first half of device_register(), if called by | |
223 | * that, though it can also be called separately, so one | |
224 | * may use @dev's fields (e.g. the refcount). | |
225 | */ | |
226 | ||
227 | void device_initialize(struct device *dev) | |
228 | { | |
229 | kobj_set_kset_s(dev, devices_subsys); | |
230 | kobject_init(&dev->kobj); | |
34bb61f9 JB |
231 | klist_init(&dev->klist_children, klist_children_get, |
232 | klist_children_put); | |
1da177e4 | 233 | INIT_LIST_HEAD(&dev->dma_pools); |
af70316a | 234 | init_MUTEX(&dev->sem); |
0ac85241 | 235 | device_init_wakeup(dev, 0); |
1da177e4 LT |
236 | } |
237 | ||
238 | /** | |
239 | * device_add - add device to device hierarchy. | |
240 | * @dev: device. | |
241 | * | |
242 | * This is part 2 of device_register(), though may be called | |
243 | * separately _iff_ device_initialize() has been called separately. | |
244 | * | |
245 | * This adds it to the kobject hierarchy via kobject_add(), adds it | |
246 | * to the global and sibling lists for the device, then | |
247 | * adds it to the other relevant subsystems of the driver model. | |
248 | */ | |
249 | int device_add(struct device *dev) | |
250 | { | |
251 | struct device *parent = NULL; | |
252 | int error = -EINVAL; | |
253 | ||
254 | dev = get_device(dev); | |
255 | if (!dev || !strlen(dev->bus_id)) | |
256 | goto Error; | |
257 | ||
258 | parent = get_device(dev->parent); | |
259 | ||
260 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); | |
261 | ||
262 | /* first, register with generic layer. */ | |
263 | kobject_set_name(&dev->kobj, "%s", dev->bus_id); | |
264 | if (parent) | |
265 | dev->kobj.parent = &parent->kobj; | |
266 | ||
267 | if ((error = kobject_add(&dev->kobj))) | |
268 | goto Error; | |
a7fd6706 KS |
269 | |
270 | dev->uevent_attr.attr.name = "uevent"; | |
271 | dev->uevent_attr.attr.mode = S_IWUSR; | |
272 | if (dev->driver) | |
273 | dev->uevent_attr.attr.owner = dev->driver->owner; | |
274 | dev->uevent_attr.store = store_uevent; | |
275 | device_create_file(dev, &dev->uevent_attr); | |
276 | ||
1da177e4 LT |
277 | if ((error = device_pm_add(dev))) |
278 | goto PMError; | |
279 | if ((error = bus_add_device(dev))) | |
280 | goto BusError; | |
53877d06 KS |
281 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
282 | bus_attach_device(dev); | |
1da177e4 | 283 | if (parent) |
d856f1e3 | 284 | klist_add_tail(&dev->knode_parent, &parent->klist_children); |
1da177e4 LT |
285 | |
286 | /* notify platform of device entry */ | |
287 | if (platform_notify) | |
288 | platform_notify(dev); | |
289 | Done: | |
290 | put_device(dev); | |
291 | return error; | |
292 | BusError: | |
293 | device_pm_remove(dev); | |
294 | PMError: | |
312c004d | 295 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
296 | kobject_del(&dev->kobj); |
297 | Error: | |
298 | if (parent) | |
299 | put_device(parent); | |
300 | goto Done; | |
301 | } | |
302 | ||
303 | ||
304 | /** | |
305 | * device_register - register a device with the system. | |
306 | * @dev: pointer to the device structure | |
307 | * | |
308 | * This happens in two clean steps - initialize the device | |
309 | * and add it to the system. The two steps can be called | |
310 | * separately, but this is the easiest and most common. | |
311 | * I.e. you should only call the two helpers separately if | |
312 | * have a clearly defined need to use and refcount the device | |
313 | * before it is added to the hierarchy. | |
314 | */ | |
315 | ||
316 | int device_register(struct device *dev) | |
317 | { | |
318 | device_initialize(dev); | |
319 | return device_add(dev); | |
320 | } | |
321 | ||
322 | ||
323 | /** | |
324 | * get_device - increment reference count for device. | |
325 | * @dev: device. | |
326 | * | |
327 | * This simply forwards the call to kobject_get(), though | |
328 | * we do take care to provide for the case that we get a NULL | |
329 | * pointer passed in. | |
330 | */ | |
331 | ||
332 | struct device * get_device(struct device * dev) | |
333 | { | |
334 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; | |
335 | } | |
336 | ||
337 | ||
338 | /** | |
339 | * put_device - decrement reference count. | |
340 | * @dev: device in question. | |
341 | */ | |
342 | void put_device(struct device * dev) | |
343 | { | |
344 | if (dev) | |
345 | kobject_put(&dev->kobj); | |
346 | } | |
347 | ||
348 | ||
349 | /** | |
350 | * device_del - delete device from system. | |
351 | * @dev: device. | |
352 | * | |
353 | * This is the first part of the device unregistration | |
354 | * sequence. This removes the device from the lists we control | |
355 | * from here, has it removed from the other driver model | |
356 | * subsystems it was added to in device_add(), and removes it | |
357 | * from the kobject hierarchy. | |
358 | * | |
359 | * NOTE: this should be called manually _iff_ device_add() was | |
360 | * also called manually. | |
361 | */ | |
362 | ||
363 | void device_del(struct device * dev) | |
364 | { | |
365 | struct device * parent = dev->parent; | |
366 | ||
1da177e4 | 367 | if (parent) |
d62c0f9f | 368 | klist_del(&dev->knode_parent); |
a7fd6706 | 369 | device_remove_file(dev, &dev->uevent_attr); |
1da177e4 LT |
370 | |
371 | /* Notify the platform of the removal, in case they | |
372 | * need to do anything... | |
373 | */ | |
374 | if (platform_notify_remove) | |
375 | platform_notify_remove(dev); | |
376 | bus_remove_device(dev); | |
377 | device_pm_remove(dev); | |
312c004d | 378 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
1da177e4 LT |
379 | kobject_del(&dev->kobj); |
380 | if (parent) | |
381 | put_device(parent); | |
382 | } | |
383 | ||
384 | /** | |
385 | * device_unregister - unregister device from system. | |
386 | * @dev: device going away. | |
387 | * | |
388 | * We do this in two parts, like we do device_register(). First, | |
389 | * we remove it from all the subsystems with device_del(), then | |
390 | * we decrement the reference count via put_device(). If that | |
391 | * is the final reference count, the device will be cleaned up | |
392 | * via device_release() above. Otherwise, the structure will | |
393 | * stick around until the final reference to the device is dropped. | |
394 | */ | |
395 | void device_unregister(struct device * dev) | |
396 | { | |
397 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); | |
398 | device_del(dev); | |
399 | put_device(dev); | |
400 | } | |
401 | ||
402 | ||
36239577 PM |
403 | static struct device * next_device(struct klist_iter * i) |
404 | { | |
405 | struct klist_node * n = klist_next(i); | |
406 | return n ? container_of(n, struct device, knode_parent) : NULL; | |
407 | } | |
408 | ||
1da177e4 LT |
409 | /** |
410 | * device_for_each_child - device child iterator. | |
c41455fb | 411 | * @parent: parent struct device. |
1da177e4 LT |
412 | * @data: data for the callback. |
413 | * @fn: function to be called for each device. | |
414 | * | |
c41455fb | 415 | * Iterate over @parent's child devices, and call @fn for each, |
1da177e4 LT |
416 | * passing it @data. |
417 | * | |
418 | * We check the return of @fn each time. If it returns anything | |
419 | * other than 0, we break out and return that value. | |
420 | */ | |
36239577 | 421 | int device_for_each_child(struct device * parent, void * data, |
1da177e4 LT |
422 | int (*fn)(struct device *, void *)) |
423 | { | |
36239577 | 424 | struct klist_iter i; |
1da177e4 LT |
425 | struct device * child; |
426 | int error = 0; | |
427 | ||
36239577 PM |
428 | klist_iter_init(&parent->klist_children, &i); |
429 | while ((child = next_device(&i)) && !error) | |
430 | error = fn(child, data); | |
431 | klist_iter_exit(&i); | |
1da177e4 LT |
432 | return error; |
433 | } | |
434 | ||
1da177e4 LT |
435 | int __init devices_init(void) |
436 | { | |
437 | return subsystem_register(&devices_subsys); | |
438 | } | |
439 | ||
440 | EXPORT_SYMBOL_GPL(device_for_each_child); | |
441 | ||
442 | EXPORT_SYMBOL_GPL(device_initialize); | |
443 | EXPORT_SYMBOL_GPL(device_add); | |
444 | EXPORT_SYMBOL_GPL(device_register); | |
445 | ||
446 | EXPORT_SYMBOL_GPL(device_del); | |
447 | EXPORT_SYMBOL_GPL(device_unregister); | |
448 | EXPORT_SYMBOL_GPL(get_device); | |
449 | EXPORT_SYMBOL_GPL(put_device); | |
1da177e4 LT |
450 | |
451 | EXPORT_SYMBOL_GPL(device_create_file); | |
452 | EXPORT_SYMBOL_GPL(device_remove_file); |