forked from rrcarlosr/Jetpack
119 lines
3.0 KiB
C
119 lines
3.0 KiB
C
/*
|
|
* Copyright 2012 Hauke Mehrtens <hauke@hauke-m.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* Backport functionality introduced in Linux 3.1.
|
|
*/
|
|
|
|
#include <linux/idr.h>
|
|
#include <linux/cpufreq.h>
|
|
#include <linux/of.h>
|
|
|
|
static DEFINE_SPINLOCK(compat_simple_ida_lock);
|
|
|
|
/**
|
|
* ida_simple_get - get a new id.
|
|
* @ida: the (initialized) ida.
|
|
* @start: the minimum id (inclusive, < 0x8000000)
|
|
* @end: the maximum id (exclusive, < 0x8000000 or 0)
|
|
* @gfp_mask: memory allocation flags
|
|
*
|
|
* Allocates an id in the range start <= id < end, or returns -ENOSPC.
|
|
* On memory allocation failure, returns -ENOMEM.
|
|
*
|
|
* Use ida_simple_remove() to get rid of an id.
|
|
*/
|
|
int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
|
|
gfp_t gfp_mask)
|
|
{
|
|
int ret, id;
|
|
unsigned int max;
|
|
unsigned long flags;
|
|
|
|
BUG_ON((int)start < 0);
|
|
BUG_ON((int)end < 0);
|
|
|
|
if (end == 0)
|
|
max = 0x80000000;
|
|
else {
|
|
BUG_ON(end < start);
|
|
max = end - 1;
|
|
}
|
|
|
|
again:
|
|
if (!ida_pre_get(ida, gfp_mask))
|
|
return -ENOMEM;
|
|
|
|
spin_lock_irqsave(&compat_simple_ida_lock, flags);
|
|
ret = ida_get_new_above(ida, start, &id);
|
|
if (!ret) {
|
|
if (id > max) {
|
|
ida_remove(ida, id);
|
|
ret = -ENOSPC;
|
|
} else {
|
|
ret = id;
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&compat_simple_ida_lock, flags);
|
|
|
|
if (unlikely(ret == -EAGAIN))
|
|
goto again;
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(ida_simple_get);
|
|
|
|
/**
|
|
* ida_simple_remove - remove an allocated id.
|
|
* @ida: the (initialized) ida.
|
|
* @id: the id returned by ida_simple_get.
|
|
*/
|
|
void ida_simple_remove(struct ida *ida, unsigned int id)
|
|
{
|
|
unsigned long flags;
|
|
|
|
BUG_ON((int)id < 0);
|
|
spin_lock_irqsave(&compat_simple_ida_lock, flags);
|
|
ida_remove(ida, id);
|
|
spin_unlock_irqrestore(&compat_simple_ida_lock, flags);
|
|
}
|
|
EXPORT_SYMBOL_GPL(ida_simple_remove);
|
|
/* source lib/idr.c */
|
|
|
|
#ifdef CONFIG_OF
|
|
/**
|
|
* of_property_read_u32_array - Find and read an array of 32 bit integers
|
|
* from a property.
|
|
*
|
|
* @np: device node from which the property value is to be read.
|
|
* @propname: name of the property to be searched.
|
|
* @out_values: pointer to return value, modified only if return value is 0.
|
|
* @sz: number of array elements to read
|
|
*
|
|
* Search for a property in a device node and read 32-bit value(s) from
|
|
* it. Returns 0 on success, -EINVAL if the property does not exist,
|
|
* -ENODATA if property does not have a value, and -EOVERFLOW if the
|
|
* property data isn't large enough.
|
|
*
|
|
* The out_values is modified only if a valid u32 value can be decoded.
|
|
*/
|
|
int of_property_read_u32_array(const struct device_node *np,
|
|
const char *propname, u32 *out_values,
|
|
size_t sz)
|
|
{
|
|
const __be32 *val = of_find_property_value_of_size(np, propname,
|
|
(sz * sizeof(*out_values)));
|
|
|
|
if (IS_ERR(val))
|
|
return PTR_ERR(val);
|
|
|
|
while (sz--)
|
|
*out_values++ = be32_to_cpup(val++);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(of_property_read_u32_array);
|
|
#endif
|