Skip to content

Commit 5d2eaf8

Browse files
Stephen WarrenLinus Walleij
authored andcommitted
pinctrl: Don't copy function name when requesting a pin
Instead, store a pointer to the currently assigned function. This allows us to delete the mux_requested variable from pin_desc; a pin is requested if its currently assigned function is non-NULL. When a pin is requested as a GPIO rather than a regular function, the assigned function name is dynamically constructed. In this case, we have to kstrdup() the dynamically constructed name, so that mux_function doesn't pointed at stack data. This requires pin_free to be told whether to free the mux_function pointer or not. This removes the hard-coded maximum function name length. Signed-off-by: Stephen Warren <swarren@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 9af1e44 commit 5d2eaf8

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎drivers/pinctrl/core.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ struct pin_desc {
5959
spinlock_t lock;
6060
/* These fields only added when supporting pinmux drivers */
6161
#ifdef CONFIG_PINMUX
62-
bool mux_requested;
63-
char mux_function[16];
62+
const char *mux_function;
6463
#endif
6564
};
6665

‎drivers/pinctrl/pinmux.c‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ static int pin_request(struct pinctrl_dev *pctldev,
130130
}
131131

132132
spin_lock(&desc->lock);
133-
if (desc->mux_requested) {
133+
if (desc->mux_function) {
134134
spin_unlock(&desc->lock);
135135
dev_err(&pctldev->dev,
136136
"pin already requested\n");
137137
goto out;
138138
}
139-
desc->mux_requested = true;
140-
strncpy(desc->mux_function, function, sizeof(desc->mux_function));
139+
desc->mux_function = function;
141140
spin_unlock(&desc->lock);
142141

143142
/* Let each pin increase references to this module */
@@ -168,8 +167,7 @@ static int pin_request(struct pinctrl_dev *pctldev,
168167
out_free_pin:
169168
if (status) {
170169
spin_lock(&desc->lock);
171-
desc->mux_requested = false;
172-
desc->mux_function[0] = '\0';
170+
desc->mux_function = NULL;
173171
spin_unlock(&desc->lock);
174172
}
175173
out:
@@ -184,8 +182,9 @@ static int pin_request(struct pinctrl_dev *pctldev,
184182
* pin_free() - release a single muxed in pin so something else can be muxed
185183
* @pctldev: pin controller device handling this pin
186184
* @pin: the pin to free
185+
* @free_func: whether to free the pin's assigned function name string
187186
*/
188-
static void pin_free(struct pinctrl_dev *pctldev, int pin)
187+
static void pin_free(struct pinctrl_dev *pctldev, int pin, int free_func)
189188
{
190189
const struct pinmux_ops *ops = pctldev->desc->pmxops;
191190
struct pin_desc *desc;
@@ -201,8 +200,9 @@ static void pin_free(struct pinctrl_dev *pctldev, int pin)
201200
ops->free(pctldev, pin);
202201

203202
spin_lock(&desc->lock);
204-
desc->mux_requested = false;
205-
desc->mux_function[0] = '\0';
203+
if (free_func)
204+
kfree(desc->mux_function);
205+
desc->mux_function = NULL;
206206
spin_unlock(&desc->lock);
207207
module_put(pctldev->owner);
208208
}
@@ -214,6 +214,7 @@ static void pin_free(struct pinctrl_dev *pctldev, int pin)
214214
int pinmux_request_gpio(unsigned gpio)
215215
{
216216
char gpiostr[16];
217+
const char *function;
217218
struct pinctrl_dev *pctldev;
218219
struct pinctrl_gpio_range *range;
219220
int ret;
@@ -229,7 +230,15 @@ int pinmux_request_gpio(unsigned gpio)
229230
/* Conjure some name stating what chip and pin this is taken by */
230231
snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
231232

232-
return pin_request(pctldev, pin, gpiostr, true, range);
233+
function = kstrdup(gpiostr, GFP_KERNEL);
234+
if (!function)
235+
return -EINVAL;
236+
237+
ret = pin_request(pctldev, pin, function, true, range);
238+
if (ret < 0)
239+
kfree(function);
240+
241+
return ret;
233242
}
234243
EXPORT_SYMBOL_GPL(pinmux_request_gpio);
235244

@@ -251,7 +260,7 @@ void pinmux_free_gpio(unsigned gpio)
251260
/* Convert to the pin controllers number space */
252261
pin = gpio - range->base;
253262

254-
pin_free(pctldev, pin);
263+
pin_free(pctldev, pin, true);
255264
}
256265
EXPORT_SYMBOL_GPL(pinmux_free_gpio);
257266

@@ -351,7 +360,7 @@ static int acquire_pins(struct pinctrl_dev *pctldev,
351360
/* On error release all taken pins */
352361
i--; /* this pin just failed */
353362
for (; i >= 0; i--)
354-
pin_free(pctldev, pins[i]);
363+
pin_free(pctldev, pins[i], false);
355364
return -ENODEV;
356365
}
357366
}
@@ -381,7 +390,7 @@ static void release_pins(struct pinctrl_dev *pctldev,
381390
return;
382391
}
383392
for (i = 0; i < num_pins; i++)
384-
pin_free(pctldev, pins[i]);
393+
pin_free(pctldev, pins[i], false);
385394
}
386395

387396
/**
@@ -1013,7 +1022,8 @@ static int pinmux_pins_show(struct seq_file *s, void *what)
10131022

10141023
seq_printf(s, "pin %d (%s): %s\n", pin,
10151024
desc->name ? desc->name : "unnamed",
1016-
desc->mux_requested ? desc->mux_function : "UNCLAIMED");
1025+
desc->mux_function ? desc->mux_function
1026+
: "UNCLAIMED");
10171027
}
10181028

10191029
return 0;

0 commit comments

Comments
 (0)