efirtc.c 9.5 KB

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