Commit d32aa745 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Linus Walleij
Browse files

pinctrl: tb10x: Use flexible-array member and struct_size() helper



Update the code to use a flexible array member instead of a pointer in
structure tb10x_pinctrl and use the struct_size() helper:

struct tb10x_pinctrl {
        ...
	struct tb10x_of_pinfunc pinfuncs[];
};

Also, make use of the struct_size() helper instead of an open-coded
version in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(struct tb10x_pinctrl) + of_get_child_count(of_node) * sizeof(struct tb10x_of_pinfunc)

with:

struct_size(state, pinfuncs, of_get_child_count(of_node))

This code was detected with the help of Coccinelle.

Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarGustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 76c4c597
Loading
Loading
Loading
Loading
+5 −7
Original line number Original line Diff line number Diff line
@@ -483,22 +483,22 @@ struct tb10x_port {
 * @base: register set base address.
 * @base: register set base address.
 * @pingroups: pointer to an array of the pin groups this driver manages.
 * @pingroups: pointer to an array of the pin groups this driver manages.
 * @pinfuncgrpcnt: number of pingroups in @pingroups.
 * @pinfuncgrpcnt: number of pingroups in @pingroups.
 * @pinfuncs: pointer to an array of pin functions this driver manages.
 * @pinfuncnt: number of pin functions in @pinfuncs.
 * @pinfuncnt: number of pin functions in @pinfuncs.
 * @mutex: mutex for exclusive access to a pin controller's state.
 * @mutex: mutex for exclusive access to a pin controller's state.
 * @ports: current state of each port.
 * @ports: current state of each port.
 * @gpios: Indicates if a given pin is currently used as GPIO (1) or not (0).
 * @gpios: Indicates if a given pin is currently used as GPIO (1) or not (0).
 * @pinfuncs: flexible array of pin functions this driver manages.
 */
 */
struct tb10x_pinctrl {
struct tb10x_pinctrl {
	struct pinctrl_dev *pctl;
	struct pinctrl_dev *pctl;
	void *base;
	void *base;
	const struct tb10x_pinfuncgrp *pingroups;
	const struct tb10x_pinfuncgrp *pingroups;
	unsigned int pinfuncgrpcnt;
	unsigned int pinfuncgrpcnt;
	struct tb10x_of_pinfunc *pinfuncs;
	unsigned int pinfuncnt;
	unsigned int pinfuncnt;
	struct mutex mutex;
	struct mutex mutex;
	struct tb10x_port ports[TB10X_PORTS];
	struct tb10x_port ports[TB10X_PORTS];
	DECLARE_BITMAP(gpios, MAX_PIN + 1);
	DECLARE_BITMAP(gpios, MAX_PIN + 1);
	struct tb10x_of_pinfunc pinfuncs[];
};
};


static inline void tb10x_pinctrl_set_config(struct tb10x_pinctrl *state,
static inline void tb10x_pinctrl_set_config(struct tb10x_pinctrl *state,
@@ -771,15 +771,13 @@ static int tb10x_pinctrl_probe(struct platform_device *pdev)
		return -EINVAL;
		return -EINVAL;
	}
	}


	state = devm_kzalloc(dev, sizeof(struct tb10x_pinctrl) +
	state = devm_kzalloc(dev, struct_size(state, pinfuncs,
					of_get_child_count(of_node)
					      of_get_child_count(of_node)),
					* sizeof(struct tb10x_of_pinfunc),
			     GFP_KERNEL);
			     GFP_KERNEL);
	if (!state)
	if (!state)
		return -ENOMEM;
		return -ENOMEM;


	platform_set_drvdata(pdev, state);
	platform_set_drvdata(pdev, state);
	state->pinfuncs = (struct tb10x_of_pinfunc *)(state + 1);
	mutex_init(&state->mutex);
	mutex_init(&state->mutex);


	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);