efirtc.c 9.3 KB

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