sctp: translate host order to network order when setting a hmacid
[linux/fpc-iii.git] / drivers / base / devres.c
blob4e2fb405da871e88cde1bb5bf65208442ea5dde1
1 /*
2 * drivers/base/devres.c - device resource management
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
8 */
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
14 #include "base.h"
16 struct devres_node {
17 struct list_head entry;
18 dr_release_t release;
19 #ifdef CONFIG_DEBUG_DEVRES
20 const char *name;
21 size_t size;
22 #endif
25 struct devres {
26 struct devres_node node;
27 /* -- 3 pointers */
28 unsigned long long data[]; /* guarantee ull alignment */
31 struct devres_group {
32 struct devres_node node[2];
33 void *id;
34 int color;
35 /* -- 8 pointers */
38 #ifdef CONFIG_DEBUG_DEVRES
39 static int log_devres = 0;
40 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
42 static void set_node_dbginfo(struct devres_node *node, const char *name,
43 size_t size)
45 node->name = name;
46 node->size = size;
49 static void devres_log(struct device *dev, struct devres_node *node,
50 const char *op)
52 if (unlikely(log_devres))
53 dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
54 op, node, node->name, (unsigned long)node->size);
56 #else /* CONFIG_DEBUG_DEVRES */
57 #define set_node_dbginfo(node, n, s) do {} while (0)
58 #define devres_log(dev, node, op) do {} while (0)
59 #endif /* CONFIG_DEBUG_DEVRES */
62 * Release functions for devres group. These callbacks are used only
63 * for identification.
65 static void group_open_release(struct device *dev, void *res)
67 /* noop */
70 static void group_close_release(struct device *dev, void *res)
72 /* noop */
75 static struct devres_group * node_to_group(struct devres_node *node)
77 if (node->release == &group_open_release)
78 return container_of(node, struct devres_group, node[0]);
79 if (node->release == &group_close_release)
80 return container_of(node, struct devres_group, node[1]);
81 return NULL;
84 static __always_inline struct devres * alloc_dr(dr_release_t release,
85 size_t size, gfp_t gfp)
87 size_t tot_size = sizeof(struct devres) + size;
88 struct devres *dr;
90 dr = kmalloc_track_caller(tot_size, gfp);
91 if (unlikely(!dr))
92 return NULL;
94 memset(dr, 0, tot_size);
95 INIT_LIST_HEAD(&dr->node.entry);
96 dr->node.release = release;
97 return dr;
100 static void add_dr(struct device *dev, struct devres_node *node)
102 devres_log(dev, node, "ADD");
103 BUG_ON(!list_empty(&node->entry));
104 list_add_tail(&node->entry, &dev->devres_head);
107 #ifdef CONFIG_DEBUG_DEVRES
108 void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
109 const char *name)
111 struct devres *dr;
113 dr = alloc_dr(release, size, gfp);
114 if (unlikely(!dr))
115 return NULL;
116 set_node_dbginfo(&dr->node, name, size);
117 return dr->data;
119 EXPORT_SYMBOL_GPL(__devres_alloc);
120 #else
122 * devres_alloc - Allocate device resource data
123 * @release: Release function devres will be associated with
124 * @size: Allocation size
125 * @gfp: Allocation flags
127 * Allocate devres of @size bytes. The allocated area is zeroed, then
128 * associated with @release. The returned pointer can be passed to
129 * other devres_*() functions.
131 * RETURNS:
132 * Pointer to allocated devres on success, NULL on failure.
134 void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
136 struct devres *dr;
138 dr = alloc_dr(release, size, gfp);
139 if (unlikely(!dr))
140 return NULL;
141 return dr->data;
143 EXPORT_SYMBOL_GPL(devres_alloc);
144 #endif
147 * devres_for_each_res - Resource iterator
148 * @dev: Device to iterate resource from
149 * @release: Look for resources associated with this release function
150 * @match: Match function (optional)
151 * @match_data: Data for the match function
152 * @fn: Function to be called for each matched resource.
153 * @data: Data for @fn, the 3rd parameter of @fn
155 * Call @fn for each devres of @dev which is associated with @release
156 * and for which @match returns 1.
158 * RETURNS:
159 * void
161 void devres_for_each_res(struct device *dev, dr_release_t release,
162 dr_match_t match, void *match_data,
163 void (*fn)(struct device *, void *, void *),
164 void *data)
166 struct devres_node *node;
167 struct devres_node *tmp;
168 unsigned long flags;
170 if (!fn)
171 return;
173 spin_lock_irqsave(&dev->devres_lock, flags);
174 list_for_each_entry_safe_reverse(node, tmp,
175 &dev->devres_head, entry) {
176 struct devres *dr = container_of(node, struct devres, node);
178 if (node->release != release)
179 continue;
180 if (match && !match(dev, dr->data, match_data))
181 continue;
182 fn(dev, dr->data, data);
184 spin_unlock_irqrestore(&dev->devres_lock, flags);
186 EXPORT_SYMBOL_GPL(devres_for_each_res);
189 * devres_free - Free device resource data
190 * @res: Pointer to devres data to free
192 * Free devres created with devres_alloc().
194 void devres_free(void *res)
196 if (res) {
197 struct devres *dr = container_of(res, struct devres, data);
199 BUG_ON(!list_empty(&dr->node.entry));
200 kfree(dr);
203 EXPORT_SYMBOL_GPL(devres_free);
206 * devres_add - Register device resource
207 * @dev: Device to add resource to
208 * @res: Resource to register
210 * Register devres @res to @dev. @res should have been allocated
211 * using devres_alloc(). On driver detach, the associated release
212 * function will be invoked and devres will be freed automatically.
214 void devres_add(struct device *dev, void *res)
216 struct devres *dr = container_of(res, struct devres, data);
217 unsigned long flags;
219 spin_lock_irqsave(&dev->devres_lock, flags);
220 add_dr(dev, &dr->node);
221 spin_unlock_irqrestore(&dev->devres_lock, flags);
223 EXPORT_SYMBOL_GPL(devres_add);
225 static struct devres *find_dr(struct device *dev, dr_release_t release,
226 dr_match_t match, void *match_data)
228 struct devres_node *node;
230 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
231 struct devres *dr = container_of(node, struct devres, node);
233 if (node->release != release)
234 continue;
235 if (match && !match(dev, dr->data, match_data))
236 continue;
237 return dr;
240 return NULL;
244 * devres_find - Find device resource
245 * @dev: Device to lookup resource from
246 * @release: Look for resources associated with this release function
247 * @match: Match function (optional)
248 * @match_data: Data for the match function
250 * Find the latest devres of @dev which is associated with @release
251 * and for which @match returns 1. If @match is NULL, it's considered
252 * to match all.
254 * RETURNS:
255 * Pointer to found devres, NULL if not found.
257 void * devres_find(struct device *dev, dr_release_t release,
258 dr_match_t match, void *match_data)
260 struct devres *dr;
261 unsigned long flags;
263 spin_lock_irqsave(&dev->devres_lock, flags);
264 dr = find_dr(dev, release, match, match_data);
265 spin_unlock_irqrestore(&dev->devres_lock, flags);
267 if (dr)
268 return dr->data;
269 return NULL;
271 EXPORT_SYMBOL_GPL(devres_find);
274 * devres_get - Find devres, if non-existent, add one atomically
275 * @dev: Device to lookup or add devres for
276 * @new_res: Pointer to new initialized devres to add if not found
277 * @match: Match function (optional)
278 * @match_data: Data for the match function
280 * Find the latest devres of @dev which has the same release function
281 * as @new_res and for which @match return 1. If found, @new_res is
282 * freed; otherwise, @new_res is added atomically.
284 * RETURNS:
285 * Pointer to found or added devres.
287 void * devres_get(struct device *dev, void *new_res,
288 dr_match_t match, void *match_data)
290 struct devres *new_dr = container_of(new_res, struct devres, data);
291 struct devres *dr;
292 unsigned long flags;
294 spin_lock_irqsave(&dev->devres_lock, flags);
295 dr = find_dr(dev, new_dr->node.release, match, match_data);
296 if (!dr) {
297 add_dr(dev, &new_dr->node);
298 dr = new_dr;
299 new_res = NULL;
301 spin_unlock_irqrestore(&dev->devres_lock, flags);
302 devres_free(new_res);
304 return dr->data;
306 EXPORT_SYMBOL_GPL(devres_get);
309 * devres_remove - Find a device resource and remove it
310 * @dev: Device to find resource from
311 * @release: Look for resources associated with this release function
312 * @match: Match function (optional)
313 * @match_data: Data for the match function
315 * Find the latest devres of @dev associated with @release and for
316 * which @match returns 1. If @match is NULL, it's considered to
317 * match all. If found, the resource is removed atomically and
318 * returned.
320 * RETURNS:
321 * Pointer to removed devres on success, NULL if not found.
323 void * devres_remove(struct device *dev, dr_release_t release,
324 dr_match_t match, void *match_data)
326 struct devres *dr;
327 unsigned long flags;
329 spin_lock_irqsave(&dev->devres_lock, flags);
330 dr = find_dr(dev, release, match, match_data);
331 if (dr) {
332 list_del_init(&dr->node.entry);
333 devres_log(dev, &dr->node, "REM");
335 spin_unlock_irqrestore(&dev->devres_lock, flags);
337 if (dr)
338 return dr->data;
339 return NULL;
341 EXPORT_SYMBOL_GPL(devres_remove);
344 * devres_destroy - Find a device resource and destroy it
345 * @dev: Device to find resource from
346 * @release: Look for resources associated with this release function
347 * @match: Match function (optional)
348 * @match_data: Data for the match function
350 * Find the latest devres of @dev associated with @release and for
351 * which @match returns 1. If @match is NULL, it's considered to
352 * match all. If found, the resource is removed atomically and freed.
354 * Note that the release function for the resource will not be called,
355 * only the devres-allocated data will be freed. The caller becomes
356 * responsible for freeing any other data.
358 * RETURNS:
359 * 0 if devres is found and freed, -ENOENT if not found.
361 int devres_destroy(struct device *dev, dr_release_t release,
362 dr_match_t match, void *match_data)
364 void *res;
366 res = devres_remove(dev, release, match, match_data);
367 if (unlikely(!res))
368 return -ENOENT;
370 devres_free(res);
371 return 0;
373 EXPORT_SYMBOL_GPL(devres_destroy);
377 * devres_release - Find a device resource and destroy it, calling release
378 * @dev: Device to find resource from
379 * @release: Look for resources associated with this release function
380 * @match: Match function (optional)
381 * @match_data: Data for the match function
383 * Find the latest devres of @dev associated with @release and for
384 * which @match returns 1. If @match is NULL, it's considered to
385 * match all. If found, the resource is removed atomically, the
386 * release function called and the resource freed.
388 * RETURNS:
389 * 0 if devres is found and freed, -ENOENT if not found.
391 int devres_release(struct device *dev, dr_release_t release,
392 dr_match_t match, void *match_data)
394 void *res;
396 res = devres_remove(dev, release, match, match_data);
397 if (unlikely(!res))
398 return -ENOENT;
400 (*release)(dev, res);
401 devres_free(res);
402 return 0;
404 EXPORT_SYMBOL_GPL(devres_release);
406 static int remove_nodes(struct device *dev,
407 struct list_head *first, struct list_head *end,
408 struct list_head *todo)
410 int cnt = 0, nr_groups = 0;
411 struct list_head *cur;
413 /* First pass - move normal devres entries to @todo and clear
414 * devres_group colors.
416 cur = first;
417 while (cur != end) {
418 struct devres_node *node;
419 struct devres_group *grp;
421 node = list_entry(cur, struct devres_node, entry);
422 cur = cur->next;
424 grp = node_to_group(node);
425 if (grp) {
426 /* clear color of group markers in the first pass */
427 grp->color = 0;
428 nr_groups++;
429 } else {
430 /* regular devres entry */
431 if (&node->entry == first)
432 first = first->next;
433 list_move_tail(&node->entry, todo);
434 cnt++;
438 if (!nr_groups)
439 return cnt;
441 /* Second pass - Scan groups and color them. A group gets
442 * color value of two iff the group is wholly contained in
443 * [cur, end). That is, for a closed group, both opening and
444 * closing markers should be in the range, while just the
445 * opening marker is enough for an open group.
447 cur = first;
448 while (cur != end) {
449 struct devres_node *node;
450 struct devres_group *grp;
452 node = list_entry(cur, struct devres_node, entry);
453 cur = cur->next;
455 grp = node_to_group(node);
456 BUG_ON(!grp || list_empty(&grp->node[0].entry));
458 grp->color++;
459 if (list_empty(&grp->node[1].entry))
460 grp->color++;
462 BUG_ON(grp->color <= 0 || grp->color > 2);
463 if (grp->color == 2) {
464 /* No need to update cur or end. The removed
465 * nodes are always before both.
467 list_move_tail(&grp->node[0].entry, todo);
468 list_del_init(&grp->node[1].entry);
472 return cnt;
475 static int release_nodes(struct device *dev, struct list_head *first,
476 struct list_head *end, unsigned long flags)
477 __releases(&dev->devres_lock)
479 LIST_HEAD(todo);
480 int cnt;
481 struct devres *dr, *tmp;
483 cnt = remove_nodes(dev, first, end, &todo);
485 spin_unlock_irqrestore(&dev->devres_lock, flags);
487 /* Release. Note that both devres and devres_group are
488 * handled as devres in the following loop. This is safe.
490 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
491 devres_log(dev, &dr->node, "REL");
492 dr->node.release(dev, dr->data);
493 kfree(dr);
496 return cnt;
500 * devres_release_all - Release all managed resources
501 * @dev: Device to release resources for
503 * Release all resources associated with @dev. This function is
504 * called on driver detach.
506 int devres_release_all(struct device *dev)
508 unsigned long flags;
510 /* Looks like an uninitialized device structure */
511 if (WARN_ON(dev->devres_head.next == NULL))
512 return -ENODEV;
513 spin_lock_irqsave(&dev->devres_lock, flags);
514 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
515 flags);
519 * devres_open_group - Open a new devres group
520 * @dev: Device to open devres group for
521 * @id: Separator ID
522 * @gfp: Allocation flags
524 * Open a new devres group for @dev with @id. For @id, using a
525 * pointer to an object which won't be used for another group is
526 * recommended. If @id is NULL, address-wise unique ID is created.
528 * RETURNS:
529 * ID of the new group, NULL on failure.
531 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
533 struct devres_group *grp;
534 unsigned long flags;
536 grp = kmalloc(sizeof(*grp), gfp);
537 if (unlikely(!grp))
538 return NULL;
540 grp->node[0].release = &group_open_release;
541 grp->node[1].release = &group_close_release;
542 INIT_LIST_HEAD(&grp->node[0].entry);
543 INIT_LIST_HEAD(&grp->node[1].entry);
544 set_node_dbginfo(&grp->node[0], "grp<", 0);
545 set_node_dbginfo(&grp->node[1], "grp>", 0);
546 grp->id = grp;
547 if (id)
548 grp->id = id;
550 spin_lock_irqsave(&dev->devres_lock, flags);
551 add_dr(dev, &grp->node[0]);
552 spin_unlock_irqrestore(&dev->devres_lock, flags);
553 return grp->id;
555 EXPORT_SYMBOL_GPL(devres_open_group);
557 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
558 static struct devres_group * find_group(struct device *dev, void *id)
560 struct devres_node *node;
562 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
563 struct devres_group *grp;
565 if (node->release != &group_open_release)
566 continue;
568 grp = container_of(node, struct devres_group, node[0]);
570 if (id) {
571 if (grp->id == id)
572 return grp;
573 } else if (list_empty(&grp->node[1].entry))
574 return grp;
577 return NULL;
581 * devres_close_group - Close a devres group
582 * @dev: Device to close devres group for
583 * @id: ID of target group, can be NULL
585 * Close the group identified by @id. If @id is NULL, the latest open
586 * group is selected.
588 void devres_close_group(struct device *dev, void *id)
590 struct devres_group *grp;
591 unsigned long flags;
593 spin_lock_irqsave(&dev->devres_lock, flags);
595 grp = find_group(dev, id);
596 if (grp)
597 add_dr(dev, &grp->node[1]);
598 else
599 WARN_ON(1);
601 spin_unlock_irqrestore(&dev->devres_lock, flags);
603 EXPORT_SYMBOL_GPL(devres_close_group);
606 * devres_remove_group - Remove a devres group
607 * @dev: Device to remove group for
608 * @id: ID of target group, can be NULL
610 * Remove the group identified by @id. If @id is NULL, the latest
611 * open group is selected. Note that removing a group doesn't affect
612 * any other resources.
614 void devres_remove_group(struct device *dev, void *id)
616 struct devres_group *grp;
617 unsigned long flags;
619 spin_lock_irqsave(&dev->devres_lock, flags);
621 grp = find_group(dev, id);
622 if (grp) {
623 list_del_init(&grp->node[0].entry);
624 list_del_init(&grp->node[1].entry);
625 devres_log(dev, &grp->node[0], "REM");
626 } else
627 WARN_ON(1);
629 spin_unlock_irqrestore(&dev->devres_lock, flags);
631 kfree(grp);
633 EXPORT_SYMBOL_GPL(devres_remove_group);
636 * devres_release_group - Release resources in a devres group
637 * @dev: Device to release group for
638 * @id: ID of target group, can be NULL
640 * Release all resources in the group identified by @id. If @id is
641 * NULL, the latest open group is selected. The selected group and
642 * groups properly nested inside the selected group are removed.
644 * RETURNS:
645 * The number of released non-group resources.
647 int devres_release_group(struct device *dev, void *id)
649 struct devres_group *grp;
650 unsigned long flags;
651 int cnt = 0;
653 spin_lock_irqsave(&dev->devres_lock, flags);
655 grp = find_group(dev, id);
656 if (grp) {
657 struct list_head *first = &grp->node[0].entry;
658 struct list_head *end = &dev->devres_head;
660 if (!list_empty(&grp->node[1].entry))
661 end = grp->node[1].entry.next;
663 cnt = release_nodes(dev, first, end, flags);
664 } else {
665 WARN_ON(1);
666 spin_unlock_irqrestore(&dev->devres_lock, flags);
669 return cnt;
671 EXPORT_SYMBOL_GPL(devres_release_group);
674 * Custom devres actions allow inserting a simple function call
675 * into the teadown sequence.
678 struct action_devres {
679 void *data;
680 void (*action)(void *);
683 static int devm_action_match(struct device *dev, void *res, void *p)
685 struct action_devres *devres = res;
686 struct action_devres *target = p;
688 return devres->action == target->action &&
689 devres->data == target->data;
692 static void devm_action_release(struct device *dev, void *res)
694 struct action_devres *devres = res;
696 devres->action(devres->data);
700 * devm_add_action() - add a custom action to list of managed resources
701 * @dev: Device that owns the action
702 * @action: Function that should be called
703 * @data: Pointer to data passed to @action implementation
705 * This adds a custom action to the list of managed resources so that
706 * it gets executed as part of standard resource unwinding.
708 int devm_add_action(struct device *dev, void (*action)(void *), void *data)
710 struct action_devres *devres;
712 devres = devres_alloc(devm_action_release,
713 sizeof(struct action_devres), GFP_KERNEL);
714 if (!devres)
715 return -ENOMEM;
717 devres->data = data;
718 devres->action = action;
720 devres_add(dev, devres);
721 return 0;
723 EXPORT_SYMBOL_GPL(devm_add_action);
726 * devm_remove_action() - removes previously added custom action
727 * @dev: Device that owns the action
728 * @action: Function implementing the action
729 * @data: Pointer to data passed to @action implementation
731 * Removes instance of @action previously added by devm_add_action().
732 * Both action and data should match one of the existing entries.
734 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
736 struct action_devres devres = {
737 .data = data,
738 .action = action,
741 WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
742 &devres));
745 EXPORT_SYMBOL_GPL(devm_remove_action);
748 * Managed kzalloc/kfree
750 static void devm_kzalloc_release(struct device *dev, void *res)
752 /* noop */
755 static int devm_kzalloc_match(struct device *dev, void *res, void *data)
757 return res == data;
761 * devm_kzalloc - Resource-managed kzalloc
762 * @dev: Device to allocate memory for
763 * @size: Allocation size
764 * @gfp: Allocation gfp flags
766 * Managed kzalloc. Memory allocated with this function is
767 * automatically freed on driver detach. Like all other devres
768 * resources, guaranteed alignment is unsigned long long.
770 * RETURNS:
771 * Pointer to allocated memory on success, NULL on failure.
773 void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
775 struct devres *dr;
777 /* use raw alloc_dr for kmalloc caller tracing */
778 dr = alloc_dr(devm_kzalloc_release, size, gfp);
779 if (unlikely(!dr))
780 return NULL;
782 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
783 devres_add(dev, dr->data);
784 return dr->data;
786 EXPORT_SYMBOL_GPL(devm_kzalloc);
789 * devm_kfree - Resource-managed kfree
790 * @dev: Device this memory belongs to
791 * @p: Memory to free
793 * Free memory allocated with devm_kzalloc().
795 void devm_kfree(struct device *dev, void *p)
797 int rc;
799 rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
800 WARN_ON(rc);
802 EXPORT_SYMBOL_GPL(devm_kfree);