Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.2 366 lines 8.7 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * EFI Time Services Driver for Linux 4 * 5 * Copyright (C) 1999 Hewlett-Packard Co 6 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com> 7 * 8 * Based on skeleton from the drivers/char/rtc.c driver by P. Gortmaker 9 * 10 * This code provides an architected & portable interface to the real time 11 * clock by using EFI instead of direct bit fiddling. The functionalities are 12 * quite different from the rtc.c driver. The only way to talk to the device 13 * is by using ioctl(). There is a /proc interface which provides the raw 14 * information. 15 * 16 * Please note that we have kept the API as close as possible to the 17 * legacy RTC. The standard /sbin/hwclock program should work normally 18 * when used to get/set the time. 19 * 20 * NOTES: 21 * - Locking is required for safe execution of EFI calls with regards 22 * to interrupts and SMP. 23 * 24 * TODO (December 1999): 25 * - provide the API to set/get the WakeUp Alarm (different from the 26 * rtc.c alarm). 27 * - SMP testing 28 * - Add module support 29 */ 30 31#include <linux/types.h> 32#include <linux/errno.h> 33#include <linux/miscdevice.h> 34#include <linux/init.h> 35#include <linux/rtc.h> 36#include <linux/proc_fs.h> 37#include <linux/seq_file.h> 38#include <linux/efi.h> 39#include <linux/uaccess.h> 40 41 42#define EFI_RTC_VERSION "0.4" 43 44#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT) 45/* 46 * EFI Epoch is 1/1/1998 47 */ 48#define EFI_RTC_EPOCH 1998 49 50static DEFINE_SPINLOCK(efi_rtc_lock); 51 52static long efi_rtc_ioctl(struct file *file, unsigned int cmd, 53 unsigned long arg); 54 55#define is_leap(year) \ 56 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) 57 58static const unsigned short int __mon_yday[2][13] = 59{ 60 /* Normal years. */ 61 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 62 /* Leap years. */ 63 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } 64}; 65 66/* 67 * returns day of the year [0-365] 68 */ 69static inline int 70compute_yday(efi_time_t *eft) 71{ 72 /* efi_time_t.month is in the [1-12] so, we need -1 */ 73 return __mon_yday[is_leap(eft->year)][eft->month-1]+ eft->day -1; 74} 75/* 76 * returns day of the week [0-6] 0=Sunday 77 * 78 * Don't try to provide a year that's before 1998, please ! 79 */ 80static int 81compute_wday(efi_time_t *eft) 82{ 83 int y; 84 int ndays = 0; 85 86 if ( eft->year < 1998 ) { 87 printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n"); 88 return -1; 89 } 90 91 for(y=EFI_RTC_EPOCH; y < eft->year; y++ ) { 92 ndays += 365 + (is_leap(y) ? 1 : 0); 93 } 94 ndays += compute_yday(eft); 95 96 /* 97 * 4=1/1/1998 was a Thursday 98 */ 99 return (ndays + 4) % 7; 100} 101 102static void 103convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft) 104{ 105 106 eft->year = wtime->tm_year + 1900; 107 eft->month = wtime->tm_mon + 1; 108 eft->day = wtime->tm_mday; 109 eft->hour = wtime->tm_hour; 110 eft->minute = wtime->tm_min; 111 eft->second = wtime->tm_sec; 112 eft->nanosecond = 0; 113 eft->daylight = wtime->tm_isdst ? EFI_ISDST: 0; 114 eft->timezone = EFI_UNSPECIFIED_TIMEZONE; 115} 116 117static void 118convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) 119{ 120 memset(wtime, 0, sizeof(*wtime)); 121 wtime->tm_sec = eft->second; 122 wtime->tm_min = eft->minute; 123 wtime->tm_hour = eft->hour; 124 wtime->tm_mday = eft->day; 125 wtime->tm_mon = eft->month - 1; 126 wtime->tm_year = eft->year - 1900; 127 128 /* day of the week [0-6], Sunday=0 */ 129 wtime->tm_wday = compute_wday(eft); 130 131 /* day in the year [1-365]*/ 132 wtime->tm_yday = compute_yday(eft); 133 134 135 switch (eft->daylight & EFI_ISDST) { 136 case EFI_ISDST: 137 wtime->tm_isdst = 1; 138 break; 139 case EFI_TIME_ADJUST_DAYLIGHT: 140 wtime->tm_isdst = 0; 141 break; 142 default: 143 wtime->tm_isdst = -1; 144 } 145} 146 147static long efi_rtc_ioctl(struct file *file, unsigned int cmd, 148 unsigned long arg) 149{ 150 151 efi_status_t status; 152 unsigned long flags; 153 efi_time_t eft; 154 efi_time_cap_t cap; 155 struct rtc_time wtime; 156 struct rtc_wkalrm __user *ewp; 157 unsigned char enabled, pending; 158 159 switch (cmd) { 160 case RTC_UIE_ON: 161 case RTC_UIE_OFF: 162 case RTC_PIE_ON: 163 case RTC_PIE_OFF: 164 case RTC_AIE_ON: 165 case RTC_AIE_OFF: 166 case RTC_ALM_SET: 167 case RTC_ALM_READ: 168 case RTC_IRQP_READ: 169 case RTC_IRQP_SET: 170 case RTC_EPOCH_READ: 171 case RTC_EPOCH_SET: 172 return -EINVAL; 173 174 case RTC_RD_TIME: 175 spin_lock_irqsave(&efi_rtc_lock, flags); 176 177 status = efi.get_time(&eft, &cap); 178 179 spin_unlock_irqrestore(&efi_rtc_lock,flags); 180 181 if (status != EFI_SUCCESS) { 182 /* should never happen */ 183 printk(KERN_ERR "efitime: can't read time\n"); 184 return -EINVAL; 185 } 186 187 convert_from_efi_time(&eft, &wtime); 188 189 return copy_to_user((void __user *)arg, &wtime, 190 sizeof (struct rtc_time)) ? - EFAULT : 0; 191 192 case RTC_SET_TIME: 193 194 if (!capable(CAP_SYS_TIME)) return -EACCES; 195 196 if (copy_from_user(&wtime, (struct rtc_time __user *)arg, 197 sizeof(struct rtc_time)) ) 198 return -EFAULT; 199 200 convert_to_efi_time(&wtime, &eft); 201 202 spin_lock_irqsave(&efi_rtc_lock, flags); 203 204 status = efi.set_time(&eft); 205 206 spin_unlock_irqrestore(&efi_rtc_lock,flags); 207 208 return status == EFI_SUCCESS ? 0 : -EINVAL; 209 210 case RTC_WKALM_SET: 211 212 if (!capable(CAP_SYS_TIME)) return -EACCES; 213 214 ewp = (struct rtc_wkalrm __user *)arg; 215 216 if ( get_user(enabled, &ewp->enabled) 217 || copy_from_user(&wtime, &ewp->time, sizeof(struct rtc_time)) ) 218 return -EFAULT; 219 220 convert_to_efi_time(&wtime, &eft); 221 222 spin_lock_irqsave(&efi_rtc_lock, flags); 223 /* 224 * XXX Fixme: 225 * As of EFI 0.92 with the firmware I have on my 226 * machine this call does not seem to work quite 227 * right 228 */ 229 status = efi.set_wakeup_time((efi_bool_t)enabled, &eft); 230 231 spin_unlock_irqrestore(&efi_rtc_lock,flags); 232 233 return status == EFI_SUCCESS ? 0 : -EINVAL; 234 235 case RTC_WKALM_RD: 236 237 spin_lock_irqsave(&efi_rtc_lock, flags); 238 239 status = efi.get_wakeup_time((efi_bool_t *)&enabled, (efi_bool_t *)&pending, &eft); 240 241 spin_unlock_irqrestore(&efi_rtc_lock,flags); 242 243 if (status != EFI_SUCCESS) return -EINVAL; 244 245 ewp = (struct rtc_wkalrm __user *)arg; 246 247 if ( put_user(enabled, &ewp->enabled) 248 || put_user(pending, &ewp->pending)) return -EFAULT; 249 250 convert_from_efi_time(&eft, &wtime); 251 252 return copy_to_user(&ewp->time, &wtime, 253 sizeof(struct rtc_time)) ? -EFAULT : 0; 254 } 255 return -ENOTTY; 256} 257 258/* 259 * The various file operations we support. 260 */ 261 262static const struct file_operations efi_rtc_fops = { 263 .owner = THIS_MODULE, 264 .unlocked_ioctl = efi_rtc_ioctl, 265 .llseek = no_llseek, 266}; 267 268static struct miscdevice efi_rtc_dev= { 269 EFI_RTC_MINOR, 270 "efirtc", 271 &efi_rtc_fops 272}; 273 274/* 275 * We export RAW EFI information to /proc/driver/efirtc 276 */ 277static int efi_rtc_proc_show(struct seq_file *m, void *v) 278{ 279 efi_time_t eft, alm; 280 efi_time_cap_t cap; 281 efi_bool_t enabled, pending; 282 unsigned long flags; 283 284 memset(&eft, 0, sizeof(eft)); 285 memset(&alm, 0, sizeof(alm)); 286 memset(&cap, 0, sizeof(cap)); 287 288 spin_lock_irqsave(&efi_rtc_lock, flags); 289 290 efi.get_time(&eft, &cap); 291 efi.get_wakeup_time(&enabled, &pending, &alm); 292 293 spin_unlock_irqrestore(&efi_rtc_lock,flags); 294 295 seq_printf(m, 296 "Time : %u:%u:%u.%09u\n" 297 "Date : %u-%u-%u\n" 298 "Daylight : %u\n", 299 eft.hour, eft.minute, eft.second, eft.nanosecond, 300 eft.year, eft.month, eft.day, 301 eft.daylight); 302 303 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE) 304 seq_puts(m, "Timezone : unspecified\n"); 305 else 306 /* XXX fixme: convert to string? */ 307 seq_printf(m, "Timezone : %u\n", eft.timezone); 308 309 310 seq_printf(m, 311 "Alarm Time : %u:%u:%u.%09u\n" 312 "Alarm Date : %u-%u-%u\n" 313 "Alarm Daylight : %u\n" 314 "Enabled : %s\n" 315 "Pending : %s\n", 316 alm.hour, alm.minute, alm.second, alm.nanosecond, 317 alm.year, alm.month, alm.day, 318 alm.daylight, 319 enabled == 1 ? "yes" : "no", 320 pending == 1 ? "yes" : "no"); 321 322 if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE) 323 seq_puts(m, "Timezone : unspecified\n"); 324 else 325 /* XXX fixme: convert to string? */ 326 seq_printf(m, "Timezone : %u\n", alm.timezone); 327 328 /* 329 * now prints the capabilities 330 */ 331 seq_printf(m, 332 "Resolution : %u\n" 333 "Accuracy : %u\n" 334 "SetstoZero : %u\n", 335 cap.resolution, cap.accuracy, cap.sets_to_zero); 336 337 return 0; 338} 339static int __init 340efi_rtc_init(void) 341{ 342 int ret; 343 struct proc_dir_entry *dir; 344 345 printk(KERN_INFO "EFI Time Services Driver v%s\n", EFI_RTC_VERSION); 346 347 ret = misc_register(&efi_rtc_dev); 348 if (ret) { 349 printk(KERN_ERR "efirtc: can't misc_register on minor=%d\n", 350 EFI_RTC_MINOR); 351 return ret; 352 } 353 354 dir = proc_create_single("driver/efirtc", 0, NULL, efi_rtc_proc_show); 355 if (dir == NULL) { 356 printk(KERN_ERR "efirtc: can't create /proc/driver/efirtc.\n"); 357 misc_deregister(&efi_rtc_dev); 358 return -1; 359 } 360 return 0; 361} 362device_initcall(efi_rtc_init); 363 364/* 365MODULE_LICENSE("GPL"); 366*/