Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * rtc-efi: RTC Class Driver for EFI-based systems
  4 *
  5 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  6 *
  7 * Author: dann frazier <dannf@dannf.org>
  8 * Based on efirtc.c by Stephane Eranian
 
 
 
 
 
 
  9 */
 10
 11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/stringify.h>
 16#include <linux/time.h>
 17#include <linux/platform_device.h>
 18#include <linux/rtc.h>
 19#include <linux/efi.h>
 20
 21#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
 22
 23/*
 24 * returns day of the year [0-365]
 25 */
 26static inline int
 27compute_yday(efi_time_t *eft)
 28{
 29	/* efi_time_t.month is in the [1-12] so, we need -1 */
 30	return rtc_year_days(eft->day, eft->month - 1, eft->year);
 31}
 32
 33/*
 34 * returns day of the week [0-6] 0=Sunday
 35 */
 36static int
 37compute_wday(efi_time_t *eft, int yday)
 38{
 39	int ndays = eft->year * (365 % 7)
 40		    + (eft->year - 1) / 4
 41		    - (eft->year - 1) / 100
 42		    + (eft->year - 1) / 400
 43		    + yday;
 44
 45	/*
 46	 * 1/1/0000 may or may not have been a Sunday (if it ever existed at
 47	 * all) but assuming it was makes this calculation work correctly.
 48	 */
 49	return ndays % 7;
 50}
 51
 52static void
 53convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
 54{
 55	eft->year	= wtime->tm_year + 1900;
 56	eft->month	= wtime->tm_mon + 1;
 57	eft->day	= wtime->tm_mday;
 58	eft->hour	= wtime->tm_hour;
 59	eft->minute	= wtime->tm_min;
 60	eft->second	= wtime->tm_sec;
 61	eft->nanosecond = 0;
 62	eft->daylight	= wtime->tm_isdst ? EFI_ISDST : 0;
 63	eft->timezone	= EFI_UNSPECIFIED_TIMEZONE;
 64}
 65
 66static bool
 67convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
 68{
 69	memset(wtime, 0, sizeof(*wtime));
 70
 71	if (eft->second >= 60)
 72		return false;
 73	wtime->tm_sec  = eft->second;
 74
 75	if (eft->minute >= 60)
 76		return false;
 77	wtime->tm_min  = eft->minute;
 78
 79	if (eft->hour >= 24)
 80		return false;
 81	wtime->tm_hour = eft->hour;
 82
 83	if (!eft->day || eft->day > 31)
 84		return false;
 85	wtime->tm_mday = eft->day;
 86
 87	if (!eft->month || eft->month > 12)
 88		return false;
 89	wtime->tm_mon  = eft->month - 1;
 90
 91	if (eft->year < 1900 || eft->year > 9999)
 92		return false;
 93	wtime->tm_year = eft->year - 1900;
 94
 95	/* day in the year [1-365]*/
 96	wtime->tm_yday = compute_yday(eft);
 97
 98	/* day of the week [0-6], Sunday=0 */
 99	wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
100
101	switch (eft->daylight & EFI_ISDST) {
102	case EFI_ISDST:
103		wtime->tm_isdst = 1;
104		break;
105	case EFI_TIME_ADJUST_DAYLIGHT:
106		wtime->tm_isdst = 0;
107		break;
108	default:
109		wtime->tm_isdst = -1;
110	}
111
112	return true;
113}
114
115static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
116{
117	efi_time_t eft;
118	efi_status_t status;
119
120	/*
121	 * As of EFI v1.10, this call always returns an unsupported status
122	 */
123	status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
124				     (efi_bool_t *)&wkalrm->pending, &eft);
125
126	if (status != EFI_SUCCESS)
127		return -EINVAL;
128
129	if (!convert_from_efi_time(&eft, &wkalrm->time))
130		return -EIO;
131
132	return rtc_valid_tm(&wkalrm->time);
133}
134
135static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
136{
137	efi_time_t eft;
138	efi_status_t status;
139
140	convert_to_efi_time(&wkalrm->time, &eft);
141
142	/*
143	 * XXX Fixme:
144	 * As of EFI 0.92 with the firmware I have on my
145	 * machine this call does not seem to work quite
146	 * right
147	 *
148	 * As of v1.10, this call always returns an unsupported status
149	 */
150	status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
151
152	dev_warn(dev, "write status is %d\n", (int)status);
153
154	return status == EFI_SUCCESS ? 0 : -EINVAL;
155}
156
157static int efi_read_time(struct device *dev, struct rtc_time *tm)
158{
159	efi_status_t status;
160	efi_time_t eft;
161	efi_time_cap_t cap;
162
163	status = efi.get_time(&eft, &cap);
164
165	if (status != EFI_SUCCESS) {
166		/* should never happen */
167		dev_err(dev, "can't read time\n");
168		return -EINVAL;
169	}
170
171	if (!convert_from_efi_time(&eft, tm))
172		return -EIO;
173
174	return 0;
175}
176
177static int efi_set_time(struct device *dev, struct rtc_time *tm)
178{
179	efi_status_t status;
180	efi_time_t eft;
181
182	convert_to_efi_time(tm, &eft);
183
184	status = efi.set_time(&eft);
185
186	return status == EFI_SUCCESS ? 0 : -EINVAL;
187}
188
189static int efi_procfs(struct device *dev, struct seq_file *seq)
190{
191	efi_time_t        eft, alm;
192	efi_time_cap_t    cap;
193	efi_bool_t        enabled, pending;
194	struct rtc_device *rtc = dev_get_drvdata(dev);
195
196	memset(&eft, 0, sizeof(eft));
197	memset(&alm, 0, sizeof(alm));
198	memset(&cap, 0, sizeof(cap));
199
200	efi.get_time(&eft, &cap);
201	efi.get_wakeup_time(&enabled, &pending, &alm);
202
203	seq_printf(seq,
204		   "Time\t\t: %u:%u:%u.%09u\n"
205		   "Date\t\t: %u-%u-%u\n"
206		   "Daylight\t: %u\n",
207		   eft.hour, eft.minute, eft.second, eft.nanosecond,
208		   eft.year, eft.month, eft.day,
209		   eft.daylight);
210
211	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
212		seq_puts(seq, "Timezone\t: unspecified\n");
213	else
214		/* XXX fixme: convert to string? */
215		seq_printf(seq, "Timezone\t: %u\n", eft.timezone);
216
217	if (test_bit(RTC_FEATURE_ALARM, rtc->features)) {
218		seq_printf(seq,
219			   "Alarm Time\t: %u:%u:%u.%09u\n"
220			   "Alarm Date\t: %u-%u-%u\n"
221			   "Alarm Daylight\t: %u\n"
222			   "Enabled\t\t: %s\n"
223			   "Pending\t\t: %s\n",
224			   alm.hour, alm.minute, alm.second, alm.nanosecond,
225			   alm.year, alm.month, alm.day,
226			   alm.daylight,
227			   enabled == 1 ? "yes" : "no",
228			   pending == 1 ? "yes" : "no");
229
230		if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
231			seq_puts(seq, "Timezone\t: unspecified\n");
232		else
233			/* XXX fixme: convert to string? */
234			seq_printf(seq, "Timezone\t: %u\n", alm.timezone);
235	}
236
237	/*
238	 * now prints the capabilities
239	 */
240	seq_printf(seq,
241		   "Resolution\t: %u\n"
242		   "Accuracy\t: %u\n"
243		   "SetstoZero\t: %u\n",
244		   cap.resolution, cap.accuracy, cap.sets_to_zero);
245
246	return 0;
247}
248
249static const struct rtc_class_ops efi_rtc_ops = {
250	.read_time	= efi_read_time,
251	.set_time	= efi_set_time,
252	.read_alarm	= efi_read_alarm,
253	.set_alarm	= efi_set_alarm,
254	.proc		= efi_procfs,
255};
256
257static int __init efi_rtc_probe(struct platform_device *dev)
258{
259	struct rtc_device *rtc;
260	efi_time_t eft;
261	efi_time_cap_t cap;
262
263	/* First check if the RTC is usable */
264	if (efi.get_time(&eft, &cap) != EFI_SUCCESS)
265		return -ENODEV;
266
267	rtc = devm_rtc_allocate_device(&dev->dev);
 
268	if (IS_ERR(rtc))
269		return PTR_ERR(rtc);
270
 
271	platform_set_drvdata(dev, rtc);
272
273	rtc->ops = &efi_rtc_ops;
274	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
275	if (efi_rt_services_supported(EFI_RT_SUPPORTED_WAKEUP_SERVICES))
276		set_bit(RTC_FEATURE_ALARM_WAKEUP_ONLY, rtc->features);
277	else
278		clear_bit(RTC_FEATURE_ALARM, rtc->features);
279
280	device_init_wakeup(&dev->dev, true);
281
282	return devm_rtc_register_device(rtc);
283}
284
285static struct platform_driver efi_rtc_driver = {
286	.driver = {
287		.name = "rtc-efi",
288	},
289};
290
291module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
292
 
293MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
294MODULE_LICENSE("GPL");
295MODULE_DESCRIPTION("EFI RTC driver");
296MODULE_ALIAS("platform:rtc-efi");
v4.6
 
  1/*
  2 * rtc-efi: RTC Class Driver for EFI-based systems
  3 *
  4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5 *
  6 * Author: dann frazier <dannf@dannf.org>
  7 * Based on efirtc.c by Stephane Eranian
  8 *
  9 *  This program is free software; you can redistribute  it and/or modify it
 10 *  under  the terms of  the GNU General  Public License as published by the
 11 *  Free Software Foundation;  either version 2 of the  License, or (at your
 12 *  option) any later version.
 13 *
 14 */
 15
 16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 17
 18#include <linux/kernel.h>
 19#include <linux/module.h>
 20#include <linux/stringify.h>
 21#include <linux/time.h>
 22#include <linux/platform_device.h>
 23#include <linux/rtc.h>
 24#include <linux/efi.h>
 25
 26#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
 27
 28/*
 29 * returns day of the year [0-365]
 30 */
 31static inline int
 32compute_yday(efi_time_t *eft)
 33{
 34	/* efi_time_t.month is in the [1-12] so, we need -1 */
 35	return rtc_year_days(eft->day, eft->month - 1, eft->year);
 36}
 37
 38/*
 39 * returns day of the week [0-6] 0=Sunday
 40 */
 41static int
 42compute_wday(efi_time_t *eft, int yday)
 43{
 44	int ndays = eft->year * (365 % 7)
 45		    + (eft->year - 1) / 4
 46		    - (eft->year - 1) / 100
 47		    + (eft->year - 1) / 400
 48		    + yday;
 49
 50	/*
 51	 * 1/1/0000 may or may not have been a Sunday (if it ever existed at
 52	 * all) but assuming it was makes this calculation work correctly.
 53	 */
 54	return ndays % 7;
 55}
 56
 57static void
 58convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
 59{
 60	eft->year	= wtime->tm_year + 1900;
 61	eft->month	= wtime->tm_mon + 1;
 62	eft->day	= wtime->tm_mday;
 63	eft->hour	= wtime->tm_hour;
 64	eft->minute	= wtime->tm_min;
 65	eft->second	= wtime->tm_sec;
 66	eft->nanosecond = 0;
 67	eft->daylight	= wtime->tm_isdst ? EFI_ISDST : 0;
 68	eft->timezone	= EFI_UNSPECIFIED_TIMEZONE;
 69}
 70
 71static bool
 72convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
 73{
 74	memset(wtime, 0, sizeof(*wtime));
 75
 76	if (eft->second >= 60)
 77		return false;
 78	wtime->tm_sec  = eft->second;
 79
 80	if (eft->minute >= 60)
 81		return false;
 82	wtime->tm_min  = eft->minute;
 83
 84	if (eft->hour >= 24)
 85		return false;
 86	wtime->tm_hour = eft->hour;
 87
 88	if (!eft->day || eft->day > 31)
 89		return false;
 90	wtime->tm_mday = eft->day;
 91
 92	if (!eft->month || eft->month > 12)
 93		return false;
 94	wtime->tm_mon  = eft->month - 1;
 95
 96	if (eft->year < 1900 || eft->year > 9999)
 97		return false;
 98	wtime->tm_year = eft->year - 1900;
 99
100	/* day in the year [1-365]*/
101	wtime->tm_yday = compute_yday(eft);
102
103	/* day of the week [0-6], Sunday=0 */
104	wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
105
106	switch (eft->daylight & EFI_ISDST) {
107	case EFI_ISDST:
108		wtime->tm_isdst = 1;
109		break;
110	case EFI_TIME_ADJUST_DAYLIGHT:
111		wtime->tm_isdst = 0;
112		break;
113	default:
114		wtime->tm_isdst = -1;
115	}
116
117	return true;
118}
119
120static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
121{
122	efi_time_t eft;
123	efi_status_t status;
124
125	/*
126	 * As of EFI v1.10, this call always returns an unsupported status
127	 */
128	status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
129				     (efi_bool_t *)&wkalrm->pending, &eft);
130
131	if (status != EFI_SUCCESS)
132		return -EINVAL;
133
134	if (!convert_from_efi_time(&eft, &wkalrm->time))
135		return -EIO;
136
137	return rtc_valid_tm(&wkalrm->time);
138}
139
140static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
141{
142	efi_time_t eft;
143	efi_status_t status;
144
145	convert_to_efi_time(&wkalrm->time, &eft);
146
147	/*
148	 * XXX Fixme:
149	 * As of EFI 0.92 with the firmware I have on my
150	 * machine this call does not seem to work quite
151	 * right
152	 *
153	 * As of v1.10, this call always returns an unsupported status
154	 */
155	status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
156
157	dev_warn(dev, "write status is %d\n", (int)status);
158
159	return status == EFI_SUCCESS ? 0 : -EINVAL;
160}
161
162static int efi_read_time(struct device *dev, struct rtc_time *tm)
163{
164	efi_status_t status;
165	efi_time_t eft;
166	efi_time_cap_t cap;
167
168	status = efi.get_time(&eft, &cap);
169
170	if (status != EFI_SUCCESS) {
171		/* should never happen */
172		dev_err(dev, "can't read time\n");
173		return -EINVAL;
174	}
175
176	if (!convert_from_efi_time(&eft, tm))
177		return -EIO;
178
179	return rtc_valid_tm(tm);
180}
181
182static int efi_set_time(struct device *dev, struct rtc_time *tm)
183{
184	efi_status_t status;
185	efi_time_t eft;
186
187	convert_to_efi_time(tm, &eft);
188
189	status = efi.set_time(&eft);
190
191	return status == EFI_SUCCESS ? 0 : -EINVAL;
192}
193
194static int efi_procfs(struct device *dev, struct seq_file *seq)
195{
196	efi_time_t      eft, alm;
197	efi_time_cap_t  cap;
198	efi_bool_t      enabled, pending;
 
199
200	memset(&eft, 0, sizeof(eft));
201	memset(&alm, 0, sizeof(alm));
202	memset(&cap, 0, sizeof(cap));
203
204	efi.get_time(&eft, &cap);
205	efi.get_wakeup_time(&enabled, &pending, &alm);
206
207	seq_printf(seq,
208		   "Time\t\t: %u:%u:%u.%09u\n"
209		   "Date\t\t: %u-%u-%u\n"
210		   "Daylight\t: %u\n",
211		   eft.hour, eft.minute, eft.second, eft.nanosecond,
212		   eft.year, eft.month, eft.day,
213		   eft.daylight);
214
215	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
216		seq_puts(seq, "Timezone\t: unspecified\n");
217	else
218		/* XXX fixme: convert to string? */
219		seq_printf(seq, "Timezone\t: %u\n", eft.timezone);
220
221	seq_printf(seq,
222		   "Alarm Time\t: %u:%u:%u.%09u\n"
223		   "Alarm Date\t: %u-%u-%u\n"
224		   "Alarm Daylight\t: %u\n"
225		   "Enabled\t\t: %s\n"
226		   "Pending\t\t: %s\n",
227		   alm.hour, alm.minute, alm.second, alm.nanosecond,
228		   alm.year, alm.month, alm.day,
229		   alm.daylight,
230		   enabled == 1 ? "yes" : "no",
231		   pending == 1 ? "yes" : "no");
232
233	if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
234		seq_puts(seq, "Timezone\t: unspecified\n");
235	else
236		/* XXX fixme: convert to string? */
237		seq_printf(seq, "Timezone\t: %u\n", alm.timezone);
 
 
238
239	/*
240	 * now prints the capabilities
241	 */
242	seq_printf(seq,
243		   "Resolution\t: %u\n"
244		   "Accuracy\t: %u\n"
245		   "SetstoZero\t: %u\n",
246		   cap.resolution, cap.accuracy, cap.sets_to_zero);
247
248	return 0;
249}
250
251static const struct rtc_class_ops efi_rtc_ops = {
252	.read_time	= efi_read_time,
253	.set_time	= efi_set_time,
254	.read_alarm	= efi_read_alarm,
255	.set_alarm	= efi_set_alarm,
256	.proc		= efi_procfs,
257};
258
259static int __init efi_rtc_probe(struct platform_device *dev)
260{
261	struct rtc_device *rtc;
 
 
 
 
 
 
262
263	rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
264					THIS_MODULE);
265	if (IS_ERR(rtc))
266		return PTR_ERR(rtc);
267
268	rtc->uie_unsupported = 1;
269	platform_set_drvdata(dev, rtc);
270
271	return 0;
 
 
 
 
 
 
 
 
 
272}
273
274static struct platform_driver efi_rtc_driver = {
275	.driver = {
276		.name = "rtc-efi",
277	},
278};
279
280module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
281
282MODULE_ALIAS("platform:rtc-efi");
283MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
284MODULE_LICENSE("GPL");
285MODULE_DESCRIPTION("EFI RTC driver");
286MODULE_ALIAS("platform:rtc-efi");