OSDN Git Service

cifs: fix incorrect handling of smb2_set_sparse() return in smb3_simple_falloc
[tomoyo/tomoyo-test1.git] / drivers / rtc / hctosys.c
1 /*
2  * RTC subsystem, initialize system time on startup
3  *
4  * Copyright (C) 2005 Tower Technologies
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/rtc.h>
15
16 /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
17  * whether it stores the most close value or the value with partial
18  * seconds truncated. However, it is important that we use it to store
19  * the truncated value. This is because otherwise it is necessary,
20  * in an rtc sync function, to read both xtime.tv_sec and
21  * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
22  * of >32bits is not possible. So storing the most close value would
23  * slow down the sync API. So here we have the truncated value and
24  * the best guess is to add 0.5s.
25  */
26
27 static int __init rtc_hctosys(void)
28 {
29         int err = -ENODEV;
30         struct rtc_time tm;
31         struct timespec64 tv64 = {
32                 .tv_nsec = NSEC_PER_SEC >> 1,
33         };
34         struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
35
36         if (rtc == NULL) {
37                 pr_info("unable to open rtc device (%s)\n",
38                         CONFIG_RTC_HCTOSYS_DEVICE);
39                 goto err_open;
40         }
41
42         err = rtc_read_time(rtc, &tm);
43         if (err) {
44                 dev_err(rtc->dev.parent,
45                         "hctosys: unable to read the hardware clock\n");
46                 goto err_read;
47
48         }
49
50         tv64.tv_sec = rtc_tm_to_time64(&tm);
51
52 #if BITS_PER_LONG == 32
53         if (tv64.tv_sec > INT_MAX) {
54                 err = -ERANGE;
55                 goto err_read;
56         }
57 #endif
58
59         err = do_settimeofday64(&tv64);
60
61         dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
62                  &tm, (long long)tv64.tv_sec);
63
64 err_read:
65         rtc_class_close(rtc);
66
67 err_open:
68         rtc_hctosys_ret = err;
69
70         return err;
71 }
72
73 late_initcall(rtc_hctosys);