Re: [PATCH 1/2] PWM: add pwm framework support

From: Matthias Kaehlcke
Date: Tue Jun 28 2011 - 15:44:48 EST


El Tue, Jun 28, 2011 at 12:02:47PM +0200 Sascha Hauer ha dit:

> This patch adds framework support for PWM (pulse width modulation) devices.
[...]
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> new file mode 100644
> index 0000000..79dc051
> --- /dev/null
> +++ b/drivers/pwm/core.c
[...]
> +/**
> + * pwmchip_add() - register a new pwm
> + * @chip: the pwm
> + *
> + * register a new pwm. pwm->pwm_id must be initialized. if pwm_id < 0 then
> + * a dynamically assigned id will be used, otherwise the id specified,
> + */
> +int pwmchip_add(struct pwm_chip *chip)
> +{
> + struct pwm_device *pwm;
> + int ret = 0;
> +
> + pwm = kzalloc(sizeof(*pwm), GFP_KERNEL);
> + if (!pwm)
> + return -ENOMEM;
> +
> + pwm->chip = chip;
> +
> + mutex_lock(&pwm_lock);
> +
> + if (chip->pwm_id >= 0 && find_pwm(chip->pwm_id)) {
> + ret = -EBUSY;
> + goto out;
> + }
> +
> + if (chip->pwm_id < 0)
> + chip->pwm_id = next_pwm_id++;
> +
> + list_add_tail(&pwm->node, &pwm_list);
> +out:
> + mutex_unlock(&pwm_lock);
> +
> + kfree(pwm);

as already pointed out by kurt, pwm should only be freed in case of an
error

> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(pwmchip_add);
> +
> +/**
> + * pwmchip_remove() - remove a pwm
> + * @chip: the pwm
> + *
> + * remove a pwm. This function may return busy if the pwm is still requested.
> + */
> +int pwmchip_remove(struct pwm_chip *chip)
> +{
> + struct pwm_device *pwm;
> + int ret = 0;
> +
> + mutex_lock(&pwm_lock);
> +
> + pwm = find_pwm(chip->pwm_id);
> + if (!pwm) {
> + ret = -ENOENT;
> + goto out;
> + }
> +
> + if (test_bit(FLAG_REQUESTED, &pwm->flags)) {
> + ret = -EBUSY;
> + goto out;
> + }
> +
> + list_del(&pwm->node);
+ kfree(pwm);
> +out:
> + mutex_unlock(&pwm_lock);
> +
> + return ret;
> +}

[...]

best regards

--
Matthias Kaehlcke
Embedded Linux Developer
Amsterdam

It always seems impossible until it's done
(Nelson Mandela)
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/