mirror of
https://github.com/torvalds/linux.git
synced 2024-11-10 14:11:52 +00:00
2207994d01
Use the helper that checks for overflows internally instead of manually calculating the size of the new array. Link: https://lkml.kernel.org/r/20201109110654.12547-6-brgl@bgdev.pl Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Borislav Petkov <bp@suse.de> Cc: Christian Knig <christian.koenig@amd.com> Cc: Christoph Lameter <cl@linux.com> Cc: Daniel Vetter <daniel@ffwll.ch> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: David Airlie <airlied@linux.ie> Cc: David Rientjes <rientjes@google.com> Cc: Gustavo Padovan <gustavo@padovan.org> Cc: James Morse <james.morse@arm.com> Cc: Jaroslav Kysela <perex@perex.cz> Cc: Jason Wang <jasowang@redhat.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Linus Walleij <linus.walleij@linaro.org> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: Mauro Carvalho Chehab <mchehab@kernel.org> Cc: Maxime Ripard <mripard@kernel.org> Cc: "Michael S . Tsirkin" <mst@redhat.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: Robert Richter <rric@kernel.org> Cc: Sumit Semwal <sumit.semwal@linaro.org> Cc: Takashi Iwai <tiwai@suse.com> Cc: Takashi Iwai <tiwai@suse.de> Cc: Thomas Zimmermann <tzimmermann@suse.de> Cc: Tony Luck <tony.luck@intel.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
141 lines
3.7 KiB
C
141 lines
3.7 KiB
C
/*
|
|
* Utils functions to implement the pincontrol driver.
|
|
*
|
|
* Copyright (c) 2013, NVIDIA Corporation.
|
|
*
|
|
* Author: Laxman Dewangan <ldewangan@nvidia.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License as
|
|
* published by the Free Software Foundation version 2.
|
|
*
|
|
* This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
|
|
* whether express or implied; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
* 02111-1307, USA
|
|
*/
|
|
#include <linux/device.h>
|
|
#include <linux/export.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/pinctrl/pinctrl.h>
|
|
#include <linux/of.h>
|
|
#include <linux/slab.h>
|
|
#include "core.h"
|
|
#include "pinctrl-utils.h"
|
|
|
|
int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,
|
|
struct pinctrl_map **map, unsigned *reserved_maps,
|
|
unsigned *num_maps, unsigned reserve)
|
|
{
|
|
unsigned old_num = *reserved_maps;
|
|
unsigned new_num = *num_maps + reserve;
|
|
struct pinctrl_map *new_map;
|
|
|
|
if (old_num >= new_num)
|
|
return 0;
|
|
|
|
new_map = krealloc_array(*map, new_num, sizeof(*new_map), GFP_KERNEL);
|
|
if (!new_map) {
|
|
dev_err(pctldev->dev, "krealloc(map) failed\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
|
|
|
|
*map = new_map;
|
|
*reserved_maps = new_num;
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map);
|
|
|
|
int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev,
|
|
struct pinctrl_map **map, unsigned *reserved_maps,
|
|
unsigned *num_maps, const char *group,
|
|
const char *function)
|
|
{
|
|
if (WARN_ON(*num_maps == *reserved_maps))
|
|
return -ENOSPC;
|
|
|
|
(*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
|
|
(*map)[*num_maps].data.mux.group = group;
|
|
(*map)[*num_maps].data.mux.function = function;
|
|
(*num_maps)++;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux);
|
|
|
|
int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev,
|
|
struct pinctrl_map **map, unsigned *reserved_maps,
|
|
unsigned *num_maps, const char *group,
|
|
unsigned long *configs, unsigned num_configs,
|
|
enum pinctrl_map_type type)
|
|
{
|
|
unsigned long *dup_configs;
|
|
|
|
if (WARN_ON(*num_maps == *reserved_maps))
|
|
return -ENOSPC;
|
|
|
|
dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
|
|
GFP_KERNEL);
|
|
if (!dup_configs)
|
|
return -ENOMEM;
|
|
|
|
(*map)[*num_maps].type = type;
|
|
(*map)[*num_maps].data.configs.group_or_pin = group;
|
|
(*map)[*num_maps].data.configs.configs = dup_configs;
|
|
(*map)[*num_maps].data.configs.num_configs = num_configs;
|
|
(*num_maps)++;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs);
|
|
|
|
int pinctrl_utils_add_config(struct pinctrl_dev *pctldev,
|
|
unsigned long **configs, unsigned *num_configs,
|
|
unsigned long config)
|
|
{
|
|
unsigned old_num = *num_configs;
|
|
unsigned new_num = old_num + 1;
|
|
unsigned long *new_configs;
|
|
|
|
new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
|
|
GFP_KERNEL);
|
|
if (!new_configs) {
|
|
dev_err(pctldev->dev, "krealloc(configs) failed\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
new_configs[old_num] = config;
|
|
|
|
*configs = new_configs;
|
|
*num_configs = new_num;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(pinctrl_utils_add_config);
|
|
|
|
void pinctrl_utils_free_map(struct pinctrl_dev *pctldev,
|
|
struct pinctrl_map *map, unsigned num_maps)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < num_maps; i++) {
|
|
switch (map[i].type) {
|
|
case PIN_MAP_TYPE_CONFIGS_GROUP:
|
|
case PIN_MAP_TYPE_CONFIGS_PIN:
|
|
kfree(map[i].data.configs.configs);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
kfree(map);
|
|
}
|
|
EXPORT_SYMBOL_GPL(pinctrl_utils_free_map);
|