efirtc.c 9.7 KB

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