blob: 5b8ebe86124a903c3674d183b5073b9ea3250008 [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummoe8242902006-03-27 01:16:41 -08002/*
3 * RTC subsystem, dev interface
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 *
8 * based on arch/arm/common/rtctime.c
Alexandre Bellonicdf75452019-03-13 23:02:48 +01009 */
Alessandro Zummoe8242902006-03-27 01:16:41 -080010
Jingoo Hanc100a5e2013-02-21 16:45:23 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Arnd Bergmann076ff652018-08-24 00:11:19 +020013#include <linux/compat.h>
Alessandro Zummoe8242902006-03-27 01:16:41 -080014#include <linux/module.h>
15#include <linux/rtc.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
David Brownell5726fb22007-05-08 00:33:27 -070017#include "rtc-core.h"
Alessandro Zummoe8242902006-03-27 01:16:41 -080018
Alessandro Zummoe8242902006-03-27 01:16:41 -080019static dev_t rtc_devt;
20
21#define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
22
23static int rtc_dev_open(struct inode *inode, struct file *file)
24{
Alessandro Zummoe8242902006-03-27 01:16:41 -080025 struct rtc_device *rtc = container_of(inode->i_cdev,
26 struct rtc_device, char_dev);
Alessandro Zummoe8242902006-03-27 01:16:41 -080027
David Brownellb1c3c892008-08-12 15:08:41 -070028 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
29 return -EBUSY;
Alessandro Zummoe8242902006-03-27 01:16:41 -080030
David Brownellab6a2d72007-05-08 00:33:30 -070031 file->private_data = rtc;
Alessandro Zummoe8242902006-03-27 01:16:41 -080032
Alexandre Belloniea369ea2017-08-23 02:33:04 +020033 spin_lock_irq(&rtc->irq_lock);
34 rtc->irq_data = 0;
35 spin_unlock_irq(&rtc->irq_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -080036
Alexandre Belloniea369ea2017-08-23 02:33:04 +020037 return 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -080038}
39
John Stultz6e57b1d2011-02-11 17:45:40 -080040#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
41/*
42 * Routine to poll RTC seconds field for change as often as possible,
43 * after first RTC_UIE use timer to reduce polling
44 */
45static void rtc_uie_task(struct work_struct *work)
46{
47 struct rtc_device *rtc =
48 container_of(work, struct rtc_device, uie_task);
49 struct rtc_time tm;
50 int num = 0;
51 int err;
52
53 err = rtc_read_time(rtc, &tm);
54
55 spin_lock_irq(&rtc->irq_lock);
56 if (rtc->stop_uie_polling || err) {
57 rtc->uie_task_active = 0;
58 } else if (rtc->oldsecs != tm.tm_sec) {
59 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
60 rtc->oldsecs = tm.tm_sec;
Alexandre Belloni606cc432019-03-20 12:59:09 +010061 rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
John Stultz6e57b1d2011-02-11 17:45:40 -080062 rtc->uie_timer_active = 1;
63 rtc->uie_task_active = 0;
64 add_timer(&rtc->uie_timer);
65 } else if (schedule_work(&rtc->uie_task) == 0) {
66 rtc->uie_task_active = 0;
67 }
68 spin_unlock_irq(&rtc->irq_lock);
69 if (num)
John Stultz456d66e2011-02-11 18:15:23 -080070 rtc_handle_legacy_irq(rtc, num, RTC_UF);
John Stultz6e57b1d2011-02-11 17:45:40 -080071}
Alexandre Belloni606cc432019-03-20 12:59:09 +010072
Kees Cooke99e88a2017-10-16 14:43:17 -070073static void rtc_uie_timer(struct timer_list *t)
John Stultz6e57b1d2011-02-11 17:45:40 -080074{
Kees Cooke99e88a2017-10-16 14:43:17 -070075 struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
John Stultz6e57b1d2011-02-11 17:45:40 -080076 unsigned long flags;
77
78 spin_lock_irqsave(&rtc->irq_lock, flags);
79 rtc->uie_timer_active = 0;
80 rtc->uie_task_active = 1;
81 if ((schedule_work(&rtc->uie_task) == 0))
82 rtc->uie_task_active = 0;
83 spin_unlock_irqrestore(&rtc->irq_lock, flags);
84}
85
86static int clear_uie(struct rtc_device *rtc)
87{
88 spin_lock_irq(&rtc->irq_lock);
89 if (rtc->uie_irq_active) {
90 rtc->stop_uie_polling = 1;
91 if (rtc->uie_timer_active) {
92 spin_unlock_irq(&rtc->irq_lock);
93 del_timer_sync(&rtc->uie_timer);
94 spin_lock_irq(&rtc->irq_lock);
95 rtc->uie_timer_active = 0;
96 }
97 if (rtc->uie_task_active) {
98 spin_unlock_irq(&rtc->irq_lock);
99 flush_scheduled_work();
100 spin_lock_irq(&rtc->irq_lock);
101 }
102 rtc->uie_irq_active = 0;
103 }
104 spin_unlock_irq(&rtc->irq_lock);
105 return 0;
106}
107
108static int set_uie(struct rtc_device *rtc)
109{
110 struct rtc_time tm;
111 int err;
112
113 err = rtc_read_time(rtc, &tm);
114 if (err)
115 return err;
116 spin_lock_irq(&rtc->irq_lock);
117 if (!rtc->uie_irq_active) {
118 rtc->uie_irq_active = 1;
119 rtc->stop_uie_polling = 0;
120 rtc->oldsecs = tm.tm_sec;
121 rtc->uie_task_active = 1;
122 if (schedule_work(&rtc->uie_task) == 0)
123 rtc->uie_task_active = 0;
124 }
125 rtc->irq_data = 0;
126 spin_unlock_irq(&rtc->irq_lock);
127 return 0;
128}
129
130int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
131{
132 if (enabled)
133 return set_uie(rtc);
134 else
135 return clear_uie(rtc);
136}
137EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
138
139#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800140
141static ssize_t
142rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143{
Mark Zhan88efe132007-10-16 01:28:17 -0700144 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800145
146 DECLARE_WAITQUEUE(wait, current);
147 unsigned long data;
148 ssize_t ret;
149
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800151 return -EINVAL;
152
153 add_wait_queue(&rtc->irq_queue, &wait);
154 do {
155 __set_current_state(TASK_INTERRUPTIBLE);
156
157 spin_lock_irq(&rtc->irq_lock);
158 data = rtc->irq_data;
159 rtc->irq_data = 0;
160 spin_unlock_irq(&rtc->irq_lock);
161
162 if (data != 0) {
163 ret = 0;
164 break;
165 }
166 if (file->f_flags & O_NONBLOCK) {
167 ret = -EAGAIN;
168 break;
169 }
170 if (signal_pending(current)) {
171 ret = -ERESTARTSYS;
172 break;
173 }
174 schedule();
175 } while (1);
176 set_current_state(TASK_RUNNING);
177 remove_wait_queue(&rtc->irq_queue, &wait);
178
179 if (ret == 0) {
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700180 if (sizeof(int) != sizeof(long) &&
181 count == sizeof(unsigned int))
182 ret = put_user(data, (unsigned int __user *)buf) ?:
183 sizeof(unsigned int);
184 else
185 ret = put_user(data, (unsigned long __user *)buf) ?:
186 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800187 }
188 return ret;
189}
190
Al Viroafc9a422017-07-03 06:39:46 -0400191static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800192{
Mark Zhan88efe132007-10-16 01:28:17 -0700193 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800194 unsigned long data;
195
196 poll_wait(file, &rtc->irq_queue, wait);
197
198 data = rtc->irq_data;
199
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800200 return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800201}
202
David Brownell5ad31a572008-07-23 21:30:33 -0700203static long rtc_dev_ioctl(struct file *file,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100204 unsigned int cmd, unsigned long arg)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800205{
206 int err = 0;
David Brownellab6a2d72007-05-08 00:33:30 -0700207 struct rtc_device *rtc = file->private_data;
David Brownellff8371a2006-09-30 23:28:17 -0700208 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800209 struct rtc_time tm;
210 struct rtc_wkalrm alarm;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100211 void __user *uarg = (void __user *)arg;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800212
David Brownell5ad31a572008-07-23 21:30:33 -0700213 err = mutex_lock_interruptible(&rtc->ops_lock);
214 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700215 return err;
David Brownell5ad31a572008-07-23 21:30:33 -0700216
David Brownell2601a462006-11-25 11:09:27 -0800217 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700218 * for certain ioctls. doing this check here is useful
219 * to avoid duplicate code in each driver.
220 */
221 switch (cmd) {
222 case RTC_EPOCH_SET:
223 case RTC_SET_TIME:
224 if (!capable(CAP_SYS_TIME))
David Brownell5ad31a572008-07-23 21:30:33 -0700225 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700226 break;
227
228 case RTC_IRQP_SET:
229 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700230 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700231 break;
232
233 case RTC_PIE_ON:
Bryan Kadzban9d013d32007-10-16 01:28:23 -0700234 if (rtc->irq_freq > rtc->max_user_freq &&
Alexandre Belloni606cc432019-03-20 12:59:09 +0100235 !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700236 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700237 break;
238 }
239
David Brownell5ad31a572008-07-23 21:30:33 -0700240 if (err)
241 goto done;
242
John Stultzac54cd22011-02-02 16:55:19 -0800243 /*
David Brownell8a0bdfd2008-02-06 01:38:45 -0800244 * Drivers *SHOULD NOT* provide ioctl implementations
245 * for these requests. Instead, provide methods to
246 * support the following code, so that the RTC's main
247 * features are accessible without using ioctls.
248 *
249 * RTC and alarm times will be in UTC, by preference,
250 * but dual-booting with MS-Windows implies RTCs must
251 * use the local wall clock time.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800252 */
253
254 switch (cmd) {
255 case RTC_ALM_READ:
David Brownell5ad31a572008-07-23 21:30:33 -0700256 mutex_unlock(&rtc->ops_lock);
257
David Brownellab6a2d72007-05-08 00:33:30 -0700258 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800259 if (err < 0)
260 return err;
261
262 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700263 err = -EFAULT;
264 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800265
266 case RTC_ALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700267 mutex_unlock(&rtc->ops_lock);
268
Alessandro Zummoe8242902006-03-27 01:16:41 -0800269 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
270 return -EFAULT;
271
272 alarm.enabled = 0;
273 alarm.pending = 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800274 alarm.time.tm_wday = -1;
275 alarm.time.tm_yday = -1;
276 alarm.time.tm_isdst = -1;
David Brownellf8245c22007-05-08 00:34:07 -0700277
278 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
279 * Rather than expecting every RTC to implement "don't care"
280 * for day/month/year fields, just force the alarm to have
281 * the right values for those fields.
282 *
283 * RTC_WKALM_SET should be used instead. Not only does it
284 * eliminate the need for a separate RTC_AIE_ON call, it
285 * doesn't have the "alarm 23:59:59 in the future" race.
286 *
287 * NOTE: some legacy code may have used invalid fields as
288 * wildcards, exposing hardware "periodic alarm" capabilities.
289 * Not supported here.
290 */
291 {
Xunlei Pang4ec23642015-01-22 02:31:52 +0000292 time64_t now, then;
David Brownellf8245c22007-05-08 00:34:07 -0700293
294 err = rtc_read_time(rtc, &tm);
295 if (err < 0)
296 return err;
Xunlei Pang4ec23642015-01-22 02:31:52 +0000297 now = rtc_tm_to_time64(&tm);
David Brownellf8245c22007-05-08 00:34:07 -0700298
299 alarm.time.tm_mday = tm.tm_mday;
300 alarm.time.tm_mon = tm.tm_mon;
301 alarm.time.tm_year = tm.tm_year;
302 err = rtc_valid_tm(&alarm.time);
303 if (err < 0)
304 return err;
Xunlei Pang4ec23642015-01-22 02:31:52 +0000305 then = rtc_tm_to_time64(&alarm.time);
David Brownellf8245c22007-05-08 00:34:07 -0700306
307 /* alarm may need to wrap into tomorrow */
308 if (then < now) {
Xunlei Pang4ec23642015-01-22 02:31:52 +0000309 rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
David Brownellf8245c22007-05-08 00:34:07 -0700310 alarm.time.tm_mday = tm.tm_mday;
311 alarm.time.tm_mon = tm.tm_mon;
312 alarm.time.tm_year = tm.tm_year;
313 }
314 }
315
David Brownell5ad31a572008-07-23 21:30:33 -0700316 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800317
318 case RTC_RD_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700319 mutex_unlock(&rtc->ops_lock);
320
David Brownellab6a2d72007-05-08 00:33:30 -0700321 err = rtc_read_time(rtc, &tm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800322 if (err < 0)
323 return err;
324
325 if (copy_to_user(uarg, &tm, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700326 err = -EFAULT;
327 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800328
329 case RTC_SET_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700330 mutex_unlock(&rtc->ops_lock);
331
Alessandro Zummoe8242902006-03-27 01:16:41 -0800332 if (copy_from_user(&tm, uarg, sizeof(tm)))
333 return -EFAULT;
334
David Brownell5ad31a572008-07-23 21:30:33 -0700335 return rtc_set_time(rtc, &tm);
David Brownell2601a462006-11-25 11:09:27 -0800336
Alessandro Zummod691eb92007-10-16 01:28:15 -0700337 case RTC_PIE_ON:
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200338 err = rtc_irq_set_state(rtc, 1);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700339 break;
340
341 case RTC_PIE_OFF:
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200342 err = rtc_irq_set_state(rtc, 0);
David Brownell2601a462006-11-25 11:09:27 -0800343 break;
344
Alessandro Zummo099e6572009-01-04 12:00:54 -0800345 case RTC_AIE_ON:
346 mutex_unlock(&rtc->ops_lock);
347 return rtc_alarm_irq_enable(rtc, 1);
348
349 case RTC_AIE_OFF:
350 mutex_unlock(&rtc->ops_lock);
351 return rtc_alarm_irq_enable(rtc, 0);
352
353 case RTC_UIE_ON:
354 mutex_unlock(&rtc->ops_lock);
355 return rtc_update_irq_enable(rtc, 1);
356
357 case RTC_UIE_OFF:
358 mutex_unlock(&rtc->ops_lock);
359 return rtc_update_irq_enable(rtc, 0);
360
David Brownell2601a462006-11-25 11:09:27 -0800361 case RTC_IRQP_SET:
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200362 err = rtc_irq_set_freq(rtc, arg);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700363 break;
Alessandro Zummod691eb92007-10-16 01:28:15 -0700364 case RTC_IRQP_READ:
365 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
David Brownell2601a462006-11-25 11:09:27 -0800366 break;
367
Alessandro Zummoe8242902006-03-27 01:16:41 -0800368 case RTC_WKALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700369 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800370 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
371 return -EFAULT;
372
David Brownell5ad31a572008-07-23 21:30:33 -0700373 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800374
375 case RTC_WKALM_RD:
David Brownell5ad31a572008-07-23 21:30:33 -0700376 mutex_unlock(&rtc->ops_lock);
David Brownellab6a2d72007-05-08 00:33:30 -0700377 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800378 if (err < 0)
379 return err;
380
381 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700382 err = -EFAULT;
383 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800384
385 default:
John Stultzac54cd22011-02-02 16:55:19 -0800386 /* Finally try the driver's ioctl interface */
387 if (ops->ioctl) {
388 err = ops->ioctl(rtc->dev.parent, cmd, arg);
389 if (err == -ENOIOCTLCMD)
390 err = -ENOTTY;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100391 } else {
John Stultze17fd4b2011-05-31 23:26:11 -0700392 err = -ENOTTY;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100393 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800394 break;
395 }
396
David Brownell5ad31a572008-07-23 21:30:33 -0700397done:
398 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800399 return err;
400}
401
Arnd Bergmann076ff652018-08-24 00:11:19 +0200402#ifdef CONFIG_COMPAT
403#define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
404#define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
405#define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
406
407static long rtc_dev_compat_ioctl(struct file *file,
408 unsigned int cmd, unsigned long arg)
409{
410 struct rtc_device *rtc = file->private_data;
411 void __user *uarg = compat_ptr(arg);
412
413 switch (cmd) {
414 case RTC_IRQP_READ32:
415 return put_user(rtc->irq_freq, (__u32 __user *)uarg);
416
417 case RTC_IRQP_SET32:
418 /* arg is a plain integer, not pointer */
419 return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
420
421 case RTC_EPOCH_SET32:
422 /* arg is a plain integer, not pointer */
423 return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
424 }
425
426 return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
427}
428#endif
429
Marcin Slusarz2e4a75c2008-10-03 15:23:36 -0700430static int rtc_dev_fasync(int fd, struct file *file, int on)
431{
432 struct rtc_device *rtc = file->private_data;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100433
Marcin Slusarz2e4a75c2008-10-03 15:23:36 -0700434 return fasync_helper(fd, file, on, &rtc->async_queue);
435}
436
Alessandro Zummoe8242902006-03-27 01:16:41 -0800437static int rtc_dev_release(struct inode *inode, struct file *file)
438{
Mark Zhan88efe132007-10-16 01:28:17 -0700439 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800440
David Brownell743e6a52008-10-15 22:03:04 -0700441 /* We shut down the repeating IRQs that userspace enabled,
442 * since nothing is listening to them.
443 * - Update (UIE) ... currently only managed through ioctls
444 * - Periodic (PIE) ... also used through rtc_*() interface calls
445 *
446 * Leave the alarm alone; it may be set to trigger a system wakeup
447 * later, or be used by kernel code, and is a one-shot event anyway.
448 */
Alessandro Zummo099e6572009-01-04 12:00:54 -0800449
450 /* Keep ioctl until all drivers are converted */
David Brownell743e6a52008-10-15 22:03:04 -0700451 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800452 rtc_update_irq_enable(rtc, 0);
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200453 rtc_irq_set_state(rtc, 0);
Tomas Janousek5cdc98b2008-07-29 22:33:51 -0700454
Jiri Kosina372a3022007-12-04 23:45:05 -0800455 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800456 return 0;
457}
458
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800459static const struct file_operations rtc_dev_fops = {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800460 .owner = THIS_MODULE,
461 .llseek = no_llseek,
462 .read = rtc_dev_read,
463 .poll = rtc_dev_poll,
David Brownell5ad31a572008-07-23 21:30:33 -0700464 .unlocked_ioctl = rtc_dev_ioctl,
Arnd Bergmann076ff652018-08-24 00:11:19 +0200465#ifdef CONFIG_COMPAT
466 .compat_ioctl = rtc_dev_compat_ioctl,
467#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800468 .open = rtc_dev_open,
469 .release = rtc_dev_release,
470 .fasync = rtc_dev_fasync,
471};
472
473/* insertion/removal hooks */
474
David Brownellcb3a58d2007-05-08 00:33:46 -0700475void rtc_dev_prepare(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800476{
David Brownell5726fb22007-05-08 00:33:27 -0700477 if (!rtc_devt)
478 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800479
480 if (rtc->id >= RTC_DEV_MAX) {
Alexandre Belloni9f860652017-06-02 14:01:37 +0200481 dev_dbg(&rtc->dev, "too many RTC devices\n");
David Brownell5726fb22007-05-08 00:33:27 -0700482 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800483 }
484
David Brownellcd966202007-05-08 00:33:40 -0700485 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
David Brownell5726fb22007-05-08 00:33:27 -0700486
John Stultz6e57b1d2011-02-11 17:45:40 -0800487#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
488 INIT_WORK(&rtc->uie_task, rtc_uie_task);
Kees Cooke99e88a2017-10-16 14:43:17 -0700489 timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
John Stultz6e57b1d2011-02-11 17:45:40 -0800490#endif
491
Alessandro Zummoe8242902006-03-27 01:16:41 -0800492 cdev_init(&rtc->char_dev, &rtc_dev_fops);
493 rtc->char_dev.owner = rtc->owner;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800494}
495
David Brownell5726fb22007-05-08 00:33:27 -0700496void __init rtc_dev_init(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800497{
498 int err;
499
Alessandro Zummoe8242902006-03-27 01:16:41 -0800500 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
David Brownell5726fb22007-05-08 00:33:27 -0700501 if (err < 0)
Jingoo Hanc100a5e2013-02-21 16:45:23 -0800502 pr_err("failed to allocate char dev region\n");
Alessandro Zummoe8242902006-03-27 01:16:41 -0800503}
504
David Brownell5726fb22007-05-08 00:33:27 -0700505void __exit rtc_dev_exit(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800506{
David Brownell5726fb22007-05-08 00:33:27 -0700507 if (rtc_devt)
508 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800509}