Skip to content

Commit aa4858e

Browse files
geertubrgl
authored andcommitted
gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
The tmp[] member of the gpiochip_fwd structure is used to store both the temporary values bitmap and the desc pointers for operations on multiple GPIOs. As both are arrays with sizes unknown at compile-time, accessing them requires offset calculations, which are currently duplicated in gpio_fwd_get_multiple() and gpio_fwd_set_multiple(). Introduce (a) accessors for both arrays and (b) a macro to calculate the needed storage size. This confines the layout of the tmp[] member into a single spot, to ease maintenance. Signed-off-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 7687a5b commit aa4858e

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

drivers/gpio/gpio-aggregator.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ struct gpiochip_fwd {
247247
unsigned long tmp[]; /* values and descs for multiple ops */
248248
};
249249

250+
#define fwd_tmp_values(fwd) &(fwd)->tmp[0]
251+
#define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
252+
253+
#define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
254+
250255
static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
251256
{
252257
struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
@@ -279,15 +284,11 @@ static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
279284
static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
280285
unsigned long *bits)
281286
{
282-
struct gpio_desc **descs;
283-
unsigned long *values;
287+
struct gpio_desc **descs = fwd_tmp_descs(fwd);
288+
unsigned long *values = fwd_tmp_values(fwd);
284289
unsigned int i, j = 0;
285290
int error;
286291

287-
/* Both values bitmap and desc pointers are stored in tmp[] */
288-
values = &fwd->tmp[0];
289-
descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
290-
291292
bitmap_clear(values, 0, fwd->chip.ngpio);
292293
for_each_set_bit(i, mask, fwd->chip.ngpio)
293294
descs[j++] = fwd->descs[i];
@@ -333,14 +334,10 @@ static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
333334
static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
334335
unsigned long *bits)
335336
{
336-
struct gpio_desc **descs;
337-
unsigned long *values;
337+
struct gpio_desc **descs = fwd_tmp_descs(fwd);
338+
unsigned long *values = fwd_tmp_values(fwd);
338339
unsigned int i, j = 0;
339340

340-
/* Both values bitmap and desc pointers are stored in tmp[] */
341-
values = &fwd->tmp[0];
342-
descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
343-
344341
for_each_set_bit(i, mask, fwd->chip.ngpio) {
345342
__assign_bit(j, values, test_bit(i, bits));
346343
descs[j++] = fwd->descs[i];
@@ -398,8 +395,8 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
398395
unsigned int i;
399396
int error;
400397

401-
fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
402-
BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
398+
fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
399+
GFP_KERNEL);
403400
if (!fwd)
404401
return ERR_PTR(-ENOMEM);
405402

0 commit comments

Comments
 (0)