rtc: ds1685: correct day of month checking

The day of month is checked in ds1685_rtc_read_alarm
and ds1685_rtc_set_alarm.

Multiple errors exist in the day of month check.

Operator ! has a higher priority than &&.
(!(mday >= 1) && (mday <= 31)) is false for mday == 32.

When verifying the day of month the binary and the BCD mode
have to be considered.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
This commit is contained in:
Heinrich Schuchardt 2016-05-22 00:18:55 +02:00 committed by Alexandre Belloni
parent 2b2f5ff00f
commit c5776dec8f

View File

@ -102,6 +102,26 @@ ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask)
return (val & bin_mask); return (val & bin_mask);
} }
/**
* s1685_rtc_check_mday - check validity of the day of month.
* @rtc: pointer to the ds1685 rtc structure.
* @mday: day of month.
*
* Returns -EDOM if the day of month is not within 1..31 range.
*/
static inline int
ds1685_rtc_check_mday(struct ds1685_priv *rtc, u8 mday)
{
if (rtc->bcd_mode) {
if (mday < 0x01 || mday > 0x31 || (mday & 0x0f) > 0x09)
return -EDOM;
} else {
if (mday < 1 || mday > 31)
return -EDOM;
}
return 0;
}
/** /**
* ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0. * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
* @rtc: pointer to the ds1685 rtc structure. * @rtc: pointer to the ds1685 rtc structure.
@ -377,6 +397,7 @@ ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
struct platform_device *pdev = to_platform_device(dev); struct platform_device *pdev = to_platform_device(dev);
struct ds1685_priv *rtc = platform_get_drvdata(pdev); struct ds1685_priv *rtc = platform_get_drvdata(pdev);
u8 seconds, minutes, hours, mday, ctrlb, ctrlc; u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
int ret;
/* Fetch the alarm info from the RTC alarm registers. */ /* Fetch the alarm info from the RTC alarm registers. */
ds1685_rtc_begin_data_access(rtc); ds1685_rtc_begin_data_access(rtc);
@ -388,9 +409,10 @@ ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
ctrlc = rtc->read(rtc, RTC_CTRL_C); ctrlc = rtc->read(rtc, RTC_CTRL_C);
ds1685_rtc_end_data_access(rtc); ds1685_rtc_end_data_access(rtc);
/* Check month date. */ /* Check the month date for validity. */
if (!(mday >= 1) && (mday <= 31)) ret = ds1685_rtc_check_mday(rtc, mday);
return -EDOM; if (ret)
return ret;
/* /*
* Check the three alarm bytes. * Check the three alarm bytes.
@ -445,6 +467,7 @@ ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
struct platform_device *pdev = to_platform_device(dev); struct platform_device *pdev = to_platform_device(dev);
struct ds1685_priv *rtc = platform_get_drvdata(pdev); struct ds1685_priv *rtc = platform_get_drvdata(pdev);
u8 ctrlb, seconds, minutes, hours, mday; u8 ctrlb, seconds, minutes, hours, mday;
int ret;
/* Fetch the alarm info and convert to BCD. */ /* Fetch the alarm info and convert to BCD. */
seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec, seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
@ -461,8 +484,9 @@ ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
RTC_MDAY_BCD_MASK); RTC_MDAY_BCD_MASK);
/* Check the month date for validity. */ /* Check the month date for validity. */
if (!(mday >= 1) && (mday <= 31)) ret = ds1685_rtc_check_mday(rtc, mday);
return -EDOM; if (ret)
return ret;
/* /*
* Check the three alarm bytes. * Check the three alarm bytes.