1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * RTC subsystem, dev interface
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2005 Tower Technologies
6*4882a593Smuzhiyun * Author: Alessandro Zummo <a.zummo@towertech.it>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * based on arch/arm/common/rtctime.c
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/compat.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/rtc.h>
16*4882a593Smuzhiyun #include <linux/sched/signal.h>
17*4882a593Smuzhiyun #include "rtc-core.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun static dev_t rtc_devt;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
22*4882a593Smuzhiyun
rtc_dev_open(struct inode * inode,struct file * file)23*4882a593Smuzhiyun static int rtc_dev_open(struct inode *inode, struct file *file)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun struct rtc_device *rtc = container_of(inode->i_cdev,
26*4882a593Smuzhiyun struct rtc_device, char_dev);
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
29*4882a593Smuzhiyun return -EBUSY;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun file->private_data = rtc;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
34*4882a593Smuzhiyun rtc->irq_data = 0;
35*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun return 0;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * Routine to poll RTC seconds field for change as often as possible,
43*4882a593Smuzhiyun * after first RTC_UIE use timer to reduce polling
44*4882a593Smuzhiyun */
rtc_uie_task(struct work_struct * work)45*4882a593Smuzhiyun static void rtc_uie_task(struct work_struct *work)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct rtc_device *rtc =
48*4882a593Smuzhiyun container_of(work, struct rtc_device, uie_task);
49*4882a593Smuzhiyun struct rtc_time tm;
50*4882a593Smuzhiyun int num = 0;
51*4882a593Smuzhiyun int err;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun err = rtc_read_time(rtc, &tm);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
56*4882a593Smuzhiyun if (rtc->stop_uie_polling || err) {
57*4882a593Smuzhiyun rtc->uie_task_active = 0;
58*4882a593Smuzhiyun } else if (rtc->oldsecs != tm.tm_sec) {
59*4882a593Smuzhiyun num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
60*4882a593Smuzhiyun rtc->oldsecs = tm.tm_sec;
61*4882a593Smuzhiyun rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
62*4882a593Smuzhiyun rtc->uie_timer_active = 1;
63*4882a593Smuzhiyun rtc->uie_task_active = 0;
64*4882a593Smuzhiyun add_timer(&rtc->uie_timer);
65*4882a593Smuzhiyun } else if (schedule_work(&rtc->uie_task) == 0) {
66*4882a593Smuzhiyun rtc->uie_task_active = 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
69*4882a593Smuzhiyun if (num)
70*4882a593Smuzhiyun rtc_handle_legacy_irq(rtc, num, RTC_UF);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
rtc_uie_timer(struct timer_list * t)73*4882a593Smuzhiyun static void rtc_uie_timer(struct timer_list *t)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
76*4882a593Smuzhiyun unsigned long flags;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun spin_lock_irqsave(&rtc->irq_lock, flags);
79*4882a593Smuzhiyun rtc->uie_timer_active = 0;
80*4882a593Smuzhiyun rtc->uie_task_active = 1;
81*4882a593Smuzhiyun if ((schedule_work(&rtc->uie_task) == 0))
82*4882a593Smuzhiyun rtc->uie_task_active = 0;
83*4882a593Smuzhiyun spin_unlock_irqrestore(&rtc->irq_lock, flags);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
clear_uie(struct rtc_device * rtc)86*4882a593Smuzhiyun static int clear_uie(struct rtc_device *rtc)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
89*4882a593Smuzhiyun if (rtc->uie_irq_active) {
90*4882a593Smuzhiyun rtc->stop_uie_polling = 1;
91*4882a593Smuzhiyun if (rtc->uie_timer_active) {
92*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
93*4882a593Smuzhiyun del_timer_sync(&rtc->uie_timer);
94*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
95*4882a593Smuzhiyun rtc->uie_timer_active = 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun if (rtc->uie_task_active) {
98*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
99*4882a593Smuzhiyun flush_scheduled_work();
100*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun rtc->uie_irq_active = 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
105*4882a593Smuzhiyun return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
set_uie(struct rtc_device * rtc)108*4882a593Smuzhiyun static int set_uie(struct rtc_device *rtc)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun struct rtc_time tm;
111*4882a593Smuzhiyun int err;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun err = rtc_read_time(rtc, &tm);
114*4882a593Smuzhiyun if (err)
115*4882a593Smuzhiyun return err;
116*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
117*4882a593Smuzhiyun if (!rtc->uie_irq_active) {
118*4882a593Smuzhiyun rtc->uie_irq_active = 1;
119*4882a593Smuzhiyun rtc->stop_uie_polling = 0;
120*4882a593Smuzhiyun rtc->oldsecs = tm.tm_sec;
121*4882a593Smuzhiyun rtc->uie_task_active = 1;
122*4882a593Smuzhiyun if (schedule_work(&rtc->uie_task) == 0)
123*4882a593Smuzhiyun rtc->uie_task_active = 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun rtc->irq_data = 0;
126*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
rtc_dev_update_irq_enable_emul(struct rtc_device * rtc,unsigned int enabled)130*4882a593Smuzhiyun int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun if (enabled)
133*4882a593Smuzhiyun return set_uie(rtc);
134*4882a593Smuzhiyun else
135*4882a593Smuzhiyun return clear_uie(rtc);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun static ssize_t
rtc_dev_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)142*4882a593Smuzhiyun rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun DECLARE_WAITQUEUE(wait, current);
147*4882a593Smuzhiyun unsigned long data;
148*4882a593Smuzhiyun ssize_t ret;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
151*4882a593Smuzhiyun return -EINVAL;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun add_wait_queue(&rtc->irq_queue, &wait);
154*4882a593Smuzhiyun do {
155*4882a593Smuzhiyun __set_current_state(TASK_INTERRUPTIBLE);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun spin_lock_irq(&rtc->irq_lock);
158*4882a593Smuzhiyun data = rtc->irq_data;
159*4882a593Smuzhiyun rtc->irq_data = 0;
160*4882a593Smuzhiyun spin_unlock_irq(&rtc->irq_lock);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (data != 0) {
163*4882a593Smuzhiyun ret = 0;
164*4882a593Smuzhiyun break;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun if (file->f_flags & O_NONBLOCK) {
167*4882a593Smuzhiyun ret = -EAGAIN;
168*4882a593Smuzhiyun break;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun if (signal_pending(current)) {
171*4882a593Smuzhiyun ret = -ERESTARTSYS;
172*4882a593Smuzhiyun break;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun schedule();
175*4882a593Smuzhiyun } while (1);
176*4882a593Smuzhiyun set_current_state(TASK_RUNNING);
177*4882a593Smuzhiyun remove_wait_queue(&rtc->irq_queue, &wait);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (ret == 0) {
180*4882a593Smuzhiyun if (sizeof(int) != sizeof(long) &&
181*4882a593Smuzhiyun count == sizeof(unsigned int))
182*4882a593Smuzhiyun ret = put_user(data, (unsigned int __user *)buf) ?:
183*4882a593Smuzhiyun sizeof(unsigned int);
184*4882a593Smuzhiyun else
185*4882a593Smuzhiyun ret = put_user(data, (unsigned long __user *)buf) ?:
186*4882a593Smuzhiyun sizeof(unsigned long);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun return ret;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
rtc_dev_poll(struct file * file,poll_table * wait)191*4882a593Smuzhiyun static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
194*4882a593Smuzhiyun unsigned long data;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun poll_wait(file, &rtc->irq_queue, wait);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun data = rtc->irq_data;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
rtc_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)203*4882a593Smuzhiyun static long rtc_dev_ioctl(struct file *file,
204*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun int err = 0;
207*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
208*4882a593Smuzhiyun const struct rtc_class_ops *ops = rtc->ops;
209*4882a593Smuzhiyun struct rtc_time tm;
210*4882a593Smuzhiyun struct rtc_wkalrm alarm;
211*4882a593Smuzhiyun void __user *uarg = (void __user *)arg;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
214*4882a593Smuzhiyun if (err)
215*4882a593Smuzhiyun return err;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* check that the calling task has appropriate permissions
218*4882a593Smuzhiyun * for certain ioctls. doing this check here is useful
219*4882a593Smuzhiyun * to avoid duplicate code in each driver.
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun switch (cmd) {
222*4882a593Smuzhiyun case RTC_EPOCH_SET:
223*4882a593Smuzhiyun case RTC_SET_TIME:
224*4882a593Smuzhiyun if (!capable(CAP_SYS_TIME))
225*4882a593Smuzhiyun err = -EACCES;
226*4882a593Smuzhiyun break;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun case RTC_IRQP_SET:
229*4882a593Smuzhiyun if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
230*4882a593Smuzhiyun err = -EACCES;
231*4882a593Smuzhiyun break;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun case RTC_PIE_ON:
234*4882a593Smuzhiyun if (rtc->irq_freq > rtc->max_user_freq &&
235*4882a593Smuzhiyun !capable(CAP_SYS_RESOURCE))
236*4882a593Smuzhiyun err = -EACCES;
237*4882a593Smuzhiyun break;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (err)
241*4882a593Smuzhiyun goto done;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Drivers *SHOULD NOT* provide ioctl implementations
245*4882a593Smuzhiyun * for these requests. Instead, provide methods to
246*4882a593Smuzhiyun * support the following code, so that the RTC's main
247*4882a593Smuzhiyun * features are accessible without using ioctls.
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * RTC and alarm times will be in UTC, by preference,
250*4882a593Smuzhiyun * but dual-booting with MS-Windows implies RTCs must
251*4882a593Smuzhiyun * use the local wall clock time.
252*4882a593Smuzhiyun */
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun switch (cmd) {
255*4882a593Smuzhiyun case RTC_ALM_READ:
256*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun err = rtc_read_alarm(rtc, &alarm);
259*4882a593Smuzhiyun if (err < 0)
260*4882a593Smuzhiyun return err;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
263*4882a593Smuzhiyun err = -EFAULT;
264*4882a593Smuzhiyun return err;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun case RTC_ALM_SET:
267*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
270*4882a593Smuzhiyun return -EFAULT;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun alarm.enabled = 0;
273*4882a593Smuzhiyun alarm.pending = 0;
274*4882a593Smuzhiyun alarm.time.tm_wday = -1;
275*4882a593Smuzhiyun alarm.time.tm_yday = -1;
276*4882a593Smuzhiyun alarm.time.tm_isdst = -1;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /* RTC_ALM_SET alarms may be up to 24 hours in the future.
279*4882a593Smuzhiyun * Rather than expecting every RTC to implement "don't care"
280*4882a593Smuzhiyun * for day/month/year fields, just force the alarm to have
281*4882a593Smuzhiyun * the right values for those fields.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * RTC_WKALM_SET should be used instead. Not only does it
284*4882a593Smuzhiyun * eliminate the need for a separate RTC_AIE_ON call, it
285*4882a593Smuzhiyun * doesn't have the "alarm 23:59:59 in the future" race.
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * NOTE: some legacy code may have used invalid fields as
288*4882a593Smuzhiyun * wildcards, exposing hardware "periodic alarm" capabilities.
289*4882a593Smuzhiyun * Not supported here.
290*4882a593Smuzhiyun */
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun time64_t now, then;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun err = rtc_read_time(rtc, &tm);
295*4882a593Smuzhiyun if (err < 0)
296*4882a593Smuzhiyun return err;
297*4882a593Smuzhiyun now = rtc_tm_to_time64(&tm);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun alarm.time.tm_mday = tm.tm_mday;
300*4882a593Smuzhiyun alarm.time.tm_mon = tm.tm_mon;
301*4882a593Smuzhiyun alarm.time.tm_year = tm.tm_year;
302*4882a593Smuzhiyun err = rtc_valid_tm(&alarm.time);
303*4882a593Smuzhiyun if (err < 0)
304*4882a593Smuzhiyun return err;
305*4882a593Smuzhiyun then = rtc_tm_to_time64(&alarm.time);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /* alarm may need to wrap into tomorrow */
308*4882a593Smuzhiyun if (then < now) {
309*4882a593Smuzhiyun rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
310*4882a593Smuzhiyun alarm.time.tm_mday = tm.tm_mday;
311*4882a593Smuzhiyun alarm.time.tm_mon = tm.tm_mon;
312*4882a593Smuzhiyun alarm.time.tm_year = tm.tm_year;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun return rtc_set_alarm(rtc, &alarm);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun case RTC_RD_TIME:
319*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun err = rtc_read_time(rtc, &tm);
322*4882a593Smuzhiyun if (err < 0)
323*4882a593Smuzhiyun return err;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (copy_to_user(uarg, &tm, sizeof(tm)))
326*4882a593Smuzhiyun err = -EFAULT;
327*4882a593Smuzhiyun return err;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun case RTC_SET_TIME:
330*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (copy_from_user(&tm, uarg, sizeof(tm)))
333*4882a593Smuzhiyun return -EFAULT;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun return rtc_set_time(rtc, &tm);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun case RTC_PIE_ON:
338*4882a593Smuzhiyun err = rtc_irq_set_state(rtc, 1);
339*4882a593Smuzhiyun break;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun case RTC_PIE_OFF:
342*4882a593Smuzhiyun err = rtc_irq_set_state(rtc, 0);
343*4882a593Smuzhiyun break;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun case RTC_AIE_ON:
346*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
347*4882a593Smuzhiyun return rtc_alarm_irq_enable(rtc, 1);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun case RTC_AIE_OFF:
350*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
351*4882a593Smuzhiyun return rtc_alarm_irq_enable(rtc, 0);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun case RTC_UIE_ON:
354*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
355*4882a593Smuzhiyun return rtc_update_irq_enable(rtc, 1);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun case RTC_UIE_OFF:
358*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
359*4882a593Smuzhiyun return rtc_update_irq_enable(rtc, 0);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun case RTC_IRQP_SET:
362*4882a593Smuzhiyun err = rtc_irq_set_freq(rtc, arg);
363*4882a593Smuzhiyun break;
364*4882a593Smuzhiyun case RTC_IRQP_READ:
365*4882a593Smuzhiyun err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
366*4882a593Smuzhiyun break;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun case RTC_WKALM_SET:
369*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
370*4882a593Smuzhiyun if (copy_from_user(&alarm, uarg, sizeof(alarm)))
371*4882a593Smuzhiyun return -EFAULT;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun return rtc_set_alarm(rtc, &alarm);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun case RTC_WKALM_RD:
376*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
377*4882a593Smuzhiyun err = rtc_read_alarm(rtc, &alarm);
378*4882a593Smuzhiyun if (err < 0)
379*4882a593Smuzhiyun return err;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun if (copy_to_user(uarg, &alarm, sizeof(alarm)))
382*4882a593Smuzhiyun err = -EFAULT;
383*4882a593Smuzhiyun return err;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun default:
386*4882a593Smuzhiyun /* Finally try the driver's ioctl interface */
387*4882a593Smuzhiyun if (ops->ioctl) {
388*4882a593Smuzhiyun err = ops->ioctl(rtc->dev.parent, cmd, arg);
389*4882a593Smuzhiyun if (err == -ENOIOCTLCMD)
390*4882a593Smuzhiyun err = -ENOTTY;
391*4882a593Smuzhiyun } else {
392*4882a593Smuzhiyun err = -ENOTTY;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun break;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun done:
398*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
399*4882a593Smuzhiyun return err;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
403*4882a593Smuzhiyun #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
404*4882a593Smuzhiyun #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
405*4882a593Smuzhiyun #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
406*4882a593Smuzhiyun
rtc_dev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)407*4882a593Smuzhiyun static long rtc_dev_compat_ioctl(struct file *file,
408*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
411*4882a593Smuzhiyun void __user *uarg = compat_ptr(arg);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun switch (cmd) {
414*4882a593Smuzhiyun case RTC_IRQP_READ32:
415*4882a593Smuzhiyun return put_user(rtc->irq_freq, (__u32 __user *)uarg);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun case RTC_IRQP_SET32:
418*4882a593Smuzhiyun /* arg is a plain integer, not pointer */
419*4882a593Smuzhiyun return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun case RTC_EPOCH_SET32:
422*4882a593Smuzhiyun /* arg is a plain integer, not pointer */
423*4882a593Smuzhiyun return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun #endif
429*4882a593Smuzhiyun
rtc_dev_fasync(int fd,struct file * file,int on)430*4882a593Smuzhiyun static int rtc_dev_fasync(int fd, struct file *file, int on)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun return fasync_helper(fd, file, on, &rtc->async_queue);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
rtc_dev_release(struct inode * inode,struct file * file)437*4882a593Smuzhiyun static int rtc_dev_release(struct inode *inode, struct file *file)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun struct rtc_device *rtc = file->private_data;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* We shut down the repeating IRQs that userspace enabled,
442*4882a593Smuzhiyun * since nothing is listening to them.
443*4882a593Smuzhiyun * - Update (UIE) ... currently only managed through ioctls
444*4882a593Smuzhiyun * - Periodic (PIE) ... also used through rtc_*() interface calls
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * Leave the alarm alone; it may be set to trigger a system wakeup
447*4882a593Smuzhiyun * later, or be used by kernel code, and is a one-shot event anyway.
448*4882a593Smuzhiyun */
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /* Keep ioctl until all drivers are converted */
451*4882a593Smuzhiyun rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
452*4882a593Smuzhiyun rtc_update_irq_enable(rtc, 0);
453*4882a593Smuzhiyun rtc_irq_set_state(rtc, 0);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
456*4882a593Smuzhiyun return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun static const struct file_operations rtc_dev_fops = {
460*4882a593Smuzhiyun .owner = THIS_MODULE,
461*4882a593Smuzhiyun .llseek = no_llseek,
462*4882a593Smuzhiyun .read = rtc_dev_read,
463*4882a593Smuzhiyun .poll = rtc_dev_poll,
464*4882a593Smuzhiyun .unlocked_ioctl = rtc_dev_ioctl,
465*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
466*4882a593Smuzhiyun .compat_ioctl = rtc_dev_compat_ioctl,
467*4882a593Smuzhiyun #endif
468*4882a593Smuzhiyun .open = rtc_dev_open,
469*4882a593Smuzhiyun .release = rtc_dev_release,
470*4882a593Smuzhiyun .fasync = rtc_dev_fasync,
471*4882a593Smuzhiyun };
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /* insertion/removal hooks */
474*4882a593Smuzhiyun
rtc_dev_prepare(struct rtc_device * rtc)475*4882a593Smuzhiyun void rtc_dev_prepare(struct rtc_device *rtc)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun if (!rtc_devt)
478*4882a593Smuzhiyun return;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun if (rtc->id >= RTC_DEV_MAX) {
481*4882a593Smuzhiyun dev_dbg(&rtc->dev, "too many RTC devices\n");
482*4882a593Smuzhiyun return;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
488*4882a593Smuzhiyun INIT_WORK(&rtc->uie_task, rtc_uie_task);
489*4882a593Smuzhiyun timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
490*4882a593Smuzhiyun #endif
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun cdev_init(&rtc->char_dev, &rtc_dev_fops);
493*4882a593Smuzhiyun rtc->char_dev.owner = rtc->owner;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
rtc_dev_init(void)496*4882a593Smuzhiyun void __init rtc_dev_init(void)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun int err;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
501*4882a593Smuzhiyun if (err < 0)
502*4882a593Smuzhiyun pr_err("failed to allocate char dev region\n");
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
rtc_dev_exit(void)505*4882a593Smuzhiyun void __exit rtc_dev_exit(void)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun if (rtc_devt)
508*4882a593Smuzhiyun unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
509*4882a593Smuzhiyun }
510