blob: 37c3cc1b3dd5e592486fc53504fb017d74b70df1 [file] [log] [blame]
Alessandro Zummoe8242902006-03-27 01:16:41 -08001/*
2 * RTC subsystem, dev interface
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
14#include <linux/module.h>
15#include <linux/rtc.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040016#include <linux/sched.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{
25 int err;
26 struct rtc_device *rtc = container_of(inode->i_cdev,
27 struct rtc_device, char_dev);
David Brownellff8371a2006-09-30 23:28:17 -070028 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -080029
David Brownellb1c3c892008-08-12 15:08:41 -070030 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
31 return -EBUSY;
Alessandro Zummoe8242902006-03-27 01:16:41 -080032
David Brownellab6a2d72007-05-08 00:33:30 -070033 file->private_data = rtc;
Alessandro Zummoe8242902006-03-27 01:16:41 -080034
David Brownellcd966202007-05-08 00:33:40 -070035 err = ops->open ? ops->open(rtc->dev.parent) : 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -080036 if (err == 0) {
37 spin_lock_irq(&rtc->irq_lock);
38 rtc->irq_data = 0;
39 spin_unlock_irq(&rtc->irq_lock);
40
David Brownellb1c3c892008-08-12 15:08:41 -070041 return 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -080042 }
43
Jiri Kosina8853c202007-11-28 16:22:03 -080044 /* something has gone wrong */
Jiri Kosina372a3022007-12-04 23:45:05 -080045 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummoe8242902006-03-27 01:16:41 -080046 return err;
47}
48
49
50static ssize_t
51rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
52{
Mark Zhan88efe132007-10-16 01:28:17 -070053 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -080054
55 DECLARE_WAITQUEUE(wait, current);
56 unsigned long data;
57 ssize_t ret;
58
Atsushi Nemoto3418ff72006-05-01 12:16:16 -070059 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -080060 return -EINVAL;
61
62 add_wait_queue(&rtc->irq_queue, &wait);
63 do {
64 __set_current_state(TASK_INTERRUPTIBLE);
65
66 spin_lock_irq(&rtc->irq_lock);
67 data = rtc->irq_data;
68 rtc->irq_data = 0;
69 spin_unlock_irq(&rtc->irq_lock);
70
71 if (data != 0) {
72 ret = 0;
73 break;
74 }
75 if (file->f_flags & O_NONBLOCK) {
76 ret = -EAGAIN;
77 break;
78 }
79 if (signal_pending(current)) {
80 ret = -ERESTARTSYS;
81 break;
82 }
83 schedule();
84 } while (1);
85 set_current_state(TASK_RUNNING);
86 remove_wait_queue(&rtc->irq_queue, &wait);
87
88 if (ret == 0) {
89 /* Check for any data updates */
90 if (rtc->ops->read_callback)
David Brownellcd966202007-05-08 00:33:40 -070091 data = rtc->ops->read_callback(rtc->dev.parent,
Atsushi Nemoto3418ff72006-05-01 12:16:16 -070092 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -080093
Atsushi Nemoto3418ff72006-05-01 12:16:16 -070094 if (sizeof(int) != sizeof(long) &&
95 count == sizeof(unsigned int))
96 ret = put_user(data, (unsigned int __user *)buf) ?:
97 sizeof(unsigned int);
98 else
99 ret = put_user(data, (unsigned long __user *)buf) ?:
100 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800101 }
102 return ret;
103}
104
105static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
106{
Mark Zhan88efe132007-10-16 01:28:17 -0700107 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800108 unsigned long data;
109
110 poll_wait(file, &rtc->irq_queue, wait);
111
112 data = rtc->irq_data;
113
114 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
115}
116
David Brownell5ad31a572008-07-23 21:30:33 -0700117static long rtc_dev_ioctl(struct file *file,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800118 unsigned int cmd, unsigned long arg)
119{
120 int err = 0;
David Brownellab6a2d72007-05-08 00:33:30 -0700121 struct rtc_device *rtc = file->private_data;
David Brownellff8371a2006-09-30 23:28:17 -0700122 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800123 struct rtc_time tm;
124 struct rtc_wkalrm alarm;
125 void __user *uarg = (void __user *) arg;
126
David Brownell5ad31a572008-07-23 21:30:33 -0700127 err = mutex_lock_interruptible(&rtc->ops_lock);
128 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700129 return err;
David Brownell5ad31a572008-07-23 21:30:33 -0700130
David Brownell2601a462006-11-25 11:09:27 -0800131 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700132 * for certain ioctls. doing this check here is useful
133 * to avoid duplicate code in each driver.
134 */
135 switch (cmd) {
136 case RTC_EPOCH_SET:
137 case RTC_SET_TIME:
138 if (!capable(CAP_SYS_TIME))
David Brownell5ad31a572008-07-23 21:30:33 -0700139 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700140 break;
141
142 case RTC_IRQP_SET:
143 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700144 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700145 break;
146
147 case RTC_PIE_ON:
Bryan Kadzban9d013d32007-10-16 01:28:23 -0700148 if (rtc->irq_freq > rtc->max_user_freq &&
149 !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700150 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700151 break;
152 }
153
David Brownell5ad31a572008-07-23 21:30:33 -0700154 if (err)
155 goto done;
156
John Stultzac54cd22011-02-02 16:55:19 -0800157 /*
David Brownell8a0bdfd2008-02-06 01:38:45 -0800158 * Drivers *SHOULD NOT* provide ioctl implementations
159 * for these requests. Instead, provide methods to
160 * support the following code, so that the RTC's main
161 * features are accessible without using ioctls.
162 *
163 * RTC and alarm times will be in UTC, by preference,
164 * but dual-booting with MS-Windows implies RTCs must
165 * use the local wall clock time.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800166 */
167
168 switch (cmd) {
169 case RTC_ALM_READ:
David Brownell5ad31a572008-07-23 21:30:33 -0700170 mutex_unlock(&rtc->ops_lock);
171
David Brownellab6a2d72007-05-08 00:33:30 -0700172 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800173 if (err < 0)
174 return err;
175
176 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700177 err = -EFAULT;
178 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800179
180 case RTC_ALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700181 mutex_unlock(&rtc->ops_lock);
182
Alessandro Zummoe8242902006-03-27 01:16:41 -0800183 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
184 return -EFAULT;
185
186 alarm.enabled = 0;
187 alarm.pending = 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800188 alarm.time.tm_wday = -1;
189 alarm.time.tm_yday = -1;
190 alarm.time.tm_isdst = -1;
David Brownellf8245c22007-05-08 00:34:07 -0700191
192 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
193 * Rather than expecting every RTC to implement "don't care"
194 * for day/month/year fields, just force the alarm to have
195 * the right values for those fields.
196 *
197 * RTC_WKALM_SET should be used instead. Not only does it
198 * eliminate the need for a separate RTC_AIE_ON call, it
199 * doesn't have the "alarm 23:59:59 in the future" race.
200 *
201 * NOTE: some legacy code may have used invalid fields as
202 * wildcards, exposing hardware "periodic alarm" capabilities.
203 * Not supported here.
204 */
205 {
206 unsigned long now, then;
207
208 err = rtc_read_time(rtc, &tm);
209 if (err < 0)
210 return err;
211 rtc_tm_to_time(&tm, &now);
212
213 alarm.time.tm_mday = tm.tm_mday;
214 alarm.time.tm_mon = tm.tm_mon;
215 alarm.time.tm_year = tm.tm_year;
216 err = rtc_valid_tm(&alarm.time);
217 if (err < 0)
218 return err;
219 rtc_tm_to_time(&alarm.time, &then);
220
221 /* alarm may need to wrap into tomorrow */
222 if (then < now) {
223 rtc_time_to_tm(now + 24 * 60 * 60, &tm);
224 alarm.time.tm_mday = tm.tm_mday;
225 alarm.time.tm_mon = tm.tm_mon;
226 alarm.time.tm_year = tm.tm_year;
227 }
228 }
229
David Brownell5ad31a572008-07-23 21:30:33 -0700230 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800231
232 case RTC_RD_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700233 mutex_unlock(&rtc->ops_lock);
234
David Brownellab6a2d72007-05-08 00:33:30 -0700235 err = rtc_read_time(rtc, &tm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800236 if (err < 0)
237 return err;
238
239 if (copy_to_user(uarg, &tm, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700240 err = -EFAULT;
241 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800242
243 case RTC_SET_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700244 mutex_unlock(&rtc->ops_lock);
245
Alessandro Zummoe8242902006-03-27 01:16:41 -0800246 if (copy_from_user(&tm, uarg, sizeof(tm)))
247 return -EFAULT;
248
David Brownell5ad31a572008-07-23 21:30:33 -0700249 return rtc_set_time(rtc, &tm);
David Brownell2601a462006-11-25 11:09:27 -0800250
Alessandro Zummod691eb92007-10-16 01:28:15 -0700251 case RTC_PIE_ON:
252 err = rtc_irq_set_state(rtc, NULL, 1);
253 break;
254
255 case RTC_PIE_OFF:
256 err = rtc_irq_set_state(rtc, NULL, 0);
David Brownell2601a462006-11-25 11:09:27 -0800257 break;
258
Alessandro Zummo099e6572009-01-04 12:00:54 -0800259 case RTC_AIE_ON:
260 mutex_unlock(&rtc->ops_lock);
261 return rtc_alarm_irq_enable(rtc, 1);
262
263 case RTC_AIE_OFF:
264 mutex_unlock(&rtc->ops_lock);
265 return rtc_alarm_irq_enable(rtc, 0);
266
267 case RTC_UIE_ON:
268 mutex_unlock(&rtc->ops_lock);
269 return rtc_update_irq_enable(rtc, 1);
270
271 case RTC_UIE_OFF:
272 mutex_unlock(&rtc->ops_lock);
273 return rtc_update_irq_enable(rtc, 0);
274
David Brownell2601a462006-11-25 11:09:27 -0800275 case RTC_IRQP_SET:
Alessandro Zummod691eb92007-10-16 01:28:15 -0700276 err = rtc_irq_set_freq(rtc, NULL, arg);
277 break;
278
279 case RTC_IRQP_READ:
280 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
David Brownell2601a462006-11-25 11:09:27 -0800281 break;
282
Alessandro Zummoe8242902006-03-27 01:16:41 -0800283#if 0
284 case RTC_EPOCH_SET:
285#ifndef rtc_epoch
286 /*
287 * There were no RTC clocks before 1900.
288 */
289 if (arg < 1900) {
290 err = -EINVAL;
291 break;
292 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800293 rtc_epoch = arg;
294 err = 0;
295#endif
296 break;
297
298 case RTC_EPOCH_READ:
299 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
300 break;
301#endif
302 case RTC_WKALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700303 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800304 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
305 return -EFAULT;
306
David Brownell5ad31a572008-07-23 21:30:33 -0700307 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800308
309 case RTC_WKALM_RD:
David Brownell5ad31a572008-07-23 21:30:33 -0700310 mutex_unlock(&rtc->ops_lock);
David Brownellab6a2d72007-05-08 00:33:30 -0700311 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800312 if (err < 0)
313 return err;
314
315 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700316 err = -EFAULT;
317 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800318
319 default:
John Stultzac54cd22011-02-02 16:55:19 -0800320 /* Finally try the driver's ioctl interface */
321 if (ops->ioctl) {
322 err = ops->ioctl(rtc->dev.parent, cmd, arg);
323 if (err == -ENOIOCTLCMD)
324 err = -ENOTTY;
325 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800326 break;
327 }
328
David Brownell5ad31a572008-07-23 21:30:33 -0700329done:
330 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800331 return err;
332}
333
Marcin Slusarz2e4a75c2008-10-03 15:23:36 -0700334static int rtc_dev_fasync(int fd, struct file *file, int on)
335{
336 struct rtc_device *rtc = file->private_data;
337 return fasync_helper(fd, file, on, &rtc->async_queue);
338}
339
Alessandro Zummoe8242902006-03-27 01:16:41 -0800340static int rtc_dev_release(struct inode *inode, struct file *file)
341{
Mark Zhan88efe132007-10-16 01:28:17 -0700342 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800343
David Brownell743e6a52008-10-15 22:03:04 -0700344 /* We shut down the repeating IRQs that userspace enabled,
345 * since nothing is listening to them.
346 * - Update (UIE) ... currently only managed through ioctls
347 * - Periodic (PIE) ... also used through rtc_*() interface calls
348 *
349 * Leave the alarm alone; it may be set to trigger a system wakeup
350 * later, or be used by kernel code, and is a one-shot event anyway.
351 */
Alessandro Zummo099e6572009-01-04 12:00:54 -0800352
353 /* Keep ioctl until all drivers are converted */
David Brownell743e6a52008-10-15 22:03:04 -0700354 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800355 rtc_update_irq_enable(rtc, 0);
Tomas Janousek5cdc98b2008-07-29 22:33:51 -0700356 rtc_irq_set_state(rtc, NULL, 0);
357
Alessandro Zummoe8242902006-03-27 01:16:41 -0800358 if (rtc->ops->release)
David Brownellcd966202007-05-08 00:33:40 -0700359 rtc->ops->release(rtc->dev.parent);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800360
Jiri Kosina372a3022007-12-04 23:45:05 -0800361 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800362 return 0;
363}
364
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800365static const struct file_operations rtc_dev_fops = {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800366 .owner = THIS_MODULE,
367 .llseek = no_llseek,
368 .read = rtc_dev_read,
369 .poll = rtc_dev_poll,
David Brownell5ad31a572008-07-23 21:30:33 -0700370 .unlocked_ioctl = rtc_dev_ioctl,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800371 .open = rtc_dev_open,
372 .release = rtc_dev_release,
373 .fasync = rtc_dev_fasync,
374};
375
376/* insertion/removal hooks */
377
David Brownellcb3a58d2007-05-08 00:33:46 -0700378void rtc_dev_prepare(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800379{
David Brownell5726fb22007-05-08 00:33:27 -0700380 if (!rtc_devt)
381 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800382
383 if (rtc->id >= RTC_DEV_MAX) {
David Brownell5726fb22007-05-08 00:33:27 -0700384 pr_debug("%s: too many RTC devices\n", rtc->name);
385 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800386 }
387
David Brownellcd966202007-05-08 00:33:40 -0700388 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
David Brownell5726fb22007-05-08 00:33:27 -0700389
Alessandro Zummoe8242902006-03-27 01:16:41 -0800390 cdev_init(&rtc->char_dev, &rtc_dev_fops);
391 rtc->char_dev.owner = rtc->owner;
David Brownellcb3a58d2007-05-08 00:33:46 -0700392}
Alessandro Zummoe8242902006-03-27 01:16:41 -0800393
David Brownellcb3a58d2007-05-08 00:33:46 -0700394void rtc_dev_add_device(struct rtc_device *rtc)
395{
David Brownellcd966202007-05-08 00:33:40 -0700396 if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
David Brownell5726fb22007-05-08 00:33:27 -0700397 printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
398 rtc->name, MAJOR(rtc_devt), rtc->id);
399 else
400 pr_debug("%s: dev (%d:%d)\n", rtc->name,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800401 MAJOR(rtc_devt), rtc->id);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800402}
403
David Brownell5726fb22007-05-08 00:33:27 -0700404void rtc_dev_del_device(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800405{
David Brownellcd966202007-05-08 00:33:40 -0700406 if (rtc->dev.devt)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800407 cdev_del(&rtc->char_dev);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800408}
409
David Brownell5726fb22007-05-08 00:33:27 -0700410void __init rtc_dev_init(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800411{
412 int err;
413
Alessandro Zummoe8242902006-03-27 01:16:41 -0800414 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
David Brownell5726fb22007-05-08 00:33:27 -0700415 if (err < 0)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800416 printk(KERN_ERR "%s: failed to allocate char dev region\n",
417 __FILE__);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800418}
419
David Brownell5726fb22007-05-08 00:33:27 -0700420void __exit rtc_dev_exit(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800421{
David Brownell5726fb22007-05-08 00:33:27 -0700422 if (rtc_devt)
423 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800424}