Re: [rtc-linux] [PATCH] rtc: add support of nvram for maxim dallas rtc ds1343

From: Andrew Morton
Date: Fri Jun 20 2014 - 18:54:35 EST


On Sat, 24 May 2014 21:34:33 +0530 Raghavendra Ganiga <ravi23ganiga@xxxxxxxxx> wrote:

> This is a patch to add support of nvram for maxim dallas
> rtc ds1343
>
> ...
>
> --- a/drivers/rtc/rtc-ds1343.c
> +++ b/drivers/rtc/rtc-ds1343.c
> @@ -4,6 +4,7 @@
> * Real Time Clock
> *
> * Author : Raghavendra Chandra Ganiga <ravi23ganiga@xxxxxxxxx>
> + * Ankur Srivastava <sankurece@xxxxxxxxx> : DS1343 Nvram Support
> *
> * This program is free software; you can redistribute it and/or modify
> * it under the terms of the GNU General Public License version 2 as
> @@ -45,6 +46,9 @@
> #define DS1343_CONTROL_REG 0x0F
> #define DS1343_STATUS_REG 0x10
> #define DS1343_TRICKLE_REG 0x11
> +#define DS1343_NVRAM 0x20
> +
> +#define DS1343_NVRAM_LEN 96
>
> /* DS1343 Control Registers bits */
> #define DS1343_EOSC 0x80
> @@ -149,6 +153,64 @@ static ssize_t ds1343_store_glitchfilter(struct device *dev,
> static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
> ds1343_store_glitchfilter);
>
> +static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
> + struct bin_attribute *attr,
> + char *buf, loff_t off, size_t count)
> +{
> + int ret;
> + unsigned char address;
> + struct device *dev = kobj_to_dev(kobj);
> + struct ds1343_priv *priv = dev_get_drvdata(dev);
> +
> + if (unlikely(!count))
> + return count;
> +
> + if ((count + off) > DS1343_NVRAM_LEN)

I worry about what happens if (count + off) wraps through zero.

> + count = DS1343_NVRAM_LEN - off;

We might end up with an enormous value in `count'?

> + address = DS1343_NVRAM + off;
> +
> + ret = regmap_bulk_write(priv->map, address, buf, count);
> + if (ret < 0)
> + dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
> +
> + return (ret < 0) ? ret : count;
> +}
> +
> +
> +static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
> + struct bin_attribute *attr,
> + char *buf, loff_t off, size_t count)
> +{
> + int ret;
> + unsigned char address;
> + struct device *dev = kobj_to_dev(kobj);
> + struct ds1343_priv *priv = dev_get_drvdata(dev);
> +
> + if (unlikely(!count))
> + return count;
> +
> + if ((count + off) > DS1343_NVRAM_LEN)
> + count = DS1343_NVRAM_LEN - off;

Here too.

> + address = DS1343_NVRAM + off;
> +
> + ret = regmap_bulk_read(priv->map, address, buf, count);
> + if (ret < 0)
> + dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
> +
> + return (ret < 0) ? ret : count;
> +}
> +
> +

--
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/