blob: 856cc1af40df85efe53805822c62886af0754143 [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>
Jonathan Corbet51a776f2008-05-15 09:38:18 -060016#include <linux/smp_lock.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
Jonathan Corbet51a776f2008-05-15 09:38:18 -060030 lock_kernel();
31 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) {
32 err = -EBUSY;
33 goto out;
34 }
Alessandro Zummoe8242902006-03-27 01:16:41 -080035
David Brownellab6a2d72007-05-08 00:33:30 -070036 file->private_data = rtc;
Alessandro Zummoe8242902006-03-27 01:16:41 -080037
David Brownellcd966202007-05-08 00:33:40 -070038 err = ops->open ? ops->open(rtc->dev.parent) : 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -080039 if (err == 0) {
40 spin_lock_irq(&rtc->irq_lock);
41 rtc->irq_data = 0;
42 spin_unlock_irq(&rtc->irq_lock);
43
Jonathan Corbet51a776f2008-05-15 09:38:18 -060044 goto out;
Alessandro Zummoe8242902006-03-27 01:16:41 -080045 }
46
Jiri Kosina8853c202007-11-28 16:22:03 -080047 /* something has gone wrong */
Jiri Kosina372a3022007-12-04 23:45:05 -080048 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Jonathan Corbet51a776f2008-05-15 09:38:18 -060049out:
50 unlock_kernel();
Alessandro Zummoe8242902006-03-27 01:16:41 -080051 return err;
52}
53
Atsushi Nemoto655066c2006-06-25 05:48:17 -070054#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
55/*
56 * Routine to poll RTC seconds field for change as often as possible,
57 * after first RTC_UIE use timer to reduce polling
58 */
David Howellsc4028952006-11-22 14:57:56 +000059static void rtc_uie_task(struct work_struct *work)
Atsushi Nemoto655066c2006-06-25 05:48:17 -070060{
David Howellsc4028952006-11-22 14:57:56 +000061 struct rtc_device *rtc =
62 container_of(work, struct rtc_device, uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070063 struct rtc_time tm;
64 int num = 0;
65 int err;
66
David Brownellab6a2d72007-05-08 00:33:30 -070067 err = rtc_read_time(rtc, &tm);
David Brownelld728b1e2006-11-25 11:09:28 -080068
69 local_irq_disable();
70 spin_lock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070071 if (rtc->stop_uie_polling || err) {
72 rtc->uie_task_active = 0;
73 } else if (rtc->oldsecs != tm.tm_sec) {
74 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
75 rtc->oldsecs = tm.tm_sec;
76 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
77 rtc->uie_timer_active = 1;
78 rtc->uie_task_active = 0;
79 add_timer(&rtc->uie_timer);
80 } else if (schedule_work(&rtc->uie_task) == 0) {
81 rtc->uie_task_active = 0;
82 }
David Brownelld728b1e2006-11-25 11:09:28 -080083 spin_unlock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070084 if (num)
David Brownellab6a2d72007-05-08 00:33:30 -070085 rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
David Brownelld728b1e2006-11-25 11:09:28 -080086 local_irq_enable();
Atsushi Nemoto655066c2006-06-25 05:48:17 -070087}
Atsushi Nemoto655066c2006-06-25 05:48:17 -070088static void rtc_uie_timer(unsigned long data)
89{
90 struct rtc_device *rtc = (struct rtc_device *)data;
91 unsigned long flags;
92
93 spin_lock_irqsave(&rtc->irq_lock, flags);
94 rtc->uie_timer_active = 0;
95 rtc->uie_task_active = 1;
96 if ((schedule_work(&rtc->uie_task) == 0))
97 rtc->uie_task_active = 0;
98 spin_unlock_irqrestore(&rtc->irq_lock, flags);
99}
100
101static void clear_uie(struct rtc_device *rtc)
102{
103 spin_lock_irq(&rtc->irq_lock);
104 if (rtc->irq_active) {
105 rtc->stop_uie_polling = 1;
106 if (rtc->uie_timer_active) {
107 spin_unlock_irq(&rtc->irq_lock);
108 del_timer_sync(&rtc->uie_timer);
109 spin_lock_irq(&rtc->irq_lock);
110 rtc->uie_timer_active = 0;
111 }
112 if (rtc->uie_task_active) {
113 spin_unlock_irq(&rtc->irq_lock);
114 flush_scheduled_work();
115 spin_lock_irq(&rtc->irq_lock);
116 }
117 rtc->irq_active = 0;
118 }
119 spin_unlock_irq(&rtc->irq_lock);
120}
121
122static int set_uie(struct rtc_device *rtc)
123{
124 struct rtc_time tm;
125 int err;
126
David Brownellab6a2d72007-05-08 00:33:30 -0700127 err = rtc_read_time(rtc, &tm);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700128 if (err)
129 return err;
130 spin_lock_irq(&rtc->irq_lock);
131 if (!rtc->irq_active) {
132 rtc->irq_active = 1;
133 rtc->stop_uie_polling = 0;
134 rtc->oldsecs = tm.tm_sec;
135 rtc->uie_task_active = 1;
136 if (schedule_work(&rtc->uie_task) == 0)
137 rtc->uie_task_active = 0;
138 }
139 rtc->irq_data = 0;
140 spin_unlock_irq(&rtc->irq_lock);
141 return 0;
142}
143#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800144
145static ssize_t
146rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
147{
Mark Zhan88efe132007-10-16 01:28:17 -0700148 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800149
150 DECLARE_WAITQUEUE(wait, current);
151 unsigned long data;
152 ssize_t ret;
153
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700154 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800155 return -EINVAL;
156
157 add_wait_queue(&rtc->irq_queue, &wait);
158 do {
159 __set_current_state(TASK_INTERRUPTIBLE);
160
161 spin_lock_irq(&rtc->irq_lock);
162 data = rtc->irq_data;
163 rtc->irq_data = 0;
164 spin_unlock_irq(&rtc->irq_lock);
165
166 if (data != 0) {
167 ret = 0;
168 break;
169 }
170 if (file->f_flags & O_NONBLOCK) {
171 ret = -EAGAIN;
172 break;
173 }
174 if (signal_pending(current)) {
175 ret = -ERESTARTSYS;
176 break;
177 }
178 schedule();
179 } while (1);
180 set_current_state(TASK_RUNNING);
181 remove_wait_queue(&rtc->irq_queue, &wait);
182
183 if (ret == 0) {
184 /* Check for any data updates */
185 if (rtc->ops->read_callback)
David Brownellcd966202007-05-08 00:33:40 -0700186 data = rtc->ops->read_callback(rtc->dev.parent,
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700187 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800188
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700189 if (sizeof(int) != sizeof(long) &&
190 count == sizeof(unsigned int))
191 ret = put_user(data, (unsigned int __user *)buf) ?:
192 sizeof(unsigned int);
193 else
194 ret = put_user(data, (unsigned long __user *)buf) ?:
195 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800196 }
197 return ret;
198}
199
200static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
201{
Mark Zhan88efe132007-10-16 01:28:17 -0700202 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800203 unsigned long data;
204
205 poll_wait(file, &rtc->irq_queue, wait);
206
207 data = rtc->irq_data;
208
209 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
210}
211
David Brownell5ad31a572008-07-23 21:30:33 -0700212static long rtc_dev_ioctl(struct file *file,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800213 unsigned int cmd, unsigned long arg)
214{
215 int err = 0;
David Brownellab6a2d72007-05-08 00:33:30 -0700216 struct rtc_device *rtc = file->private_data;
David Brownellff8371a2006-09-30 23:28:17 -0700217 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800218 struct rtc_time tm;
219 struct rtc_wkalrm alarm;
220 void __user *uarg = (void __user *) arg;
221
David Brownell5ad31a572008-07-23 21:30:33 -0700222 err = mutex_lock_interruptible(&rtc->ops_lock);
223 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700224 return err;
David Brownell5ad31a572008-07-23 21:30:33 -0700225
David Brownell2601a462006-11-25 11:09:27 -0800226 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700227 * for certain ioctls. doing this check here is useful
228 * to avoid duplicate code in each driver.
229 */
230 switch (cmd) {
231 case RTC_EPOCH_SET:
232 case RTC_SET_TIME:
233 if (!capable(CAP_SYS_TIME))
David Brownell5ad31a572008-07-23 21:30:33 -0700234 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700235 break;
236
237 case RTC_IRQP_SET:
238 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700239 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700240 break;
241
242 case RTC_PIE_ON:
Bryan Kadzban9d013d32007-10-16 01:28:23 -0700243 if (rtc->irq_freq > rtc->max_user_freq &&
244 !capable(CAP_SYS_RESOURCE))
David Brownell5ad31a572008-07-23 21:30:33 -0700245 err = -EACCES;
Alessandro Zummo110d6932006-06-25 05:48:20 -0700246 break;
247 }
248
David Brownell5ad31a572008-07-23 21:30:33 -0700249 if (err)
250 goto done;
251
Alessandro Zummoe8242902006-03-27 01:16:41 -0800252 /* try the driver's ioctl interface */
253 if (ops->ioctl) {
David Brownellcd966202007-05-08 00:33:40 -0700254 err = ops->ioctl(rtc->dev.parent, cmd, arg);
David Brownell5ad31a572008-07-23 21:30:33 -0700255 if (err != -ENOIOCTLCMD) {
256 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800257 return err;
David Brownell5ad31a572008-07-23 21:30:33 -0700258 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800259 }
260
261 /* if the driver does not provide the ioctl interface
262 * or if that particular ioctl was not implemented
Alessandro Zummob3969e52006-05-20 15:00:29 -0700263 * (-ENOIOCTLCMD), we will try to emulate here.
David Brownell8a0bdfd2008-02-06 01:38:45 -0800264 *
265 * Drivers *SHOULD NOT* provide ioctl implementations
266 * for these requests. Instead, provide methods to
267 * support the following code, so that the RTC's main
268 * features are accessible without using ioctls.
269 *
270 * RTC and alarm times will be in UTC, by preference,
271 * but dual-booting with MS-Windows implies RTCs must
272 * use the local wall clock time.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800273 */
274
275 switch (cmd) {
276 case RTC_ALM_READ:
David Brownell5ad31a572008-07-23 21:30:33 -0700277 mutex_unlock(&rtc->ops_lock);
278
David Brownellab6a2d72007-05-08 00:33:30 -0700279 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800280 if (err < 0)
281 return err;
282
283 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700284 err = -EFAULT;
285 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800286
287 case RTC_ALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700288 mutex_unlock(&rtc->ops_lock);
289
Alessandro Zummoe8242902006-03-27 01:16:41 -0800290 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
291 return -EFAULT;
292
293 alarm.enabled = 0;
294 alarm.pending = 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800295 alarm.time.tm_wday = -1;
296 alarm.time.tm_yday = -1;
297 alarm.time.tm_isdst = -1;
David Brownellf8245c22007-05-08 00:34:07 -0700298
299 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
300 * Rather than expecting every RTC to implement "don't care"
301 * for day/month/year fields, just force the alarm to have
302 * the right values for those fields.
303 *
304 * RTC_WKALM_SET should be used instead. Not only does it
305 * eliminate the need for a separate RTC_AIE_ON call, it
306 * doesn't have the "alarm 23:59:59 in the future" race.
307 *
308 * NOTE: some legacy code may have used invalid fields as
309 * wildcards, exposing hardware "periodic alarm" capabilities.
310 * Not supported here.
311 */
312 {
313 unsigned long now, then;
314
315 err = rtc_read_time(rtc, &tm);
316 if (err < 0)
317 return err;
318 rtc_tm_to_time(&tm, &now);
319
320 alarm.time.tm_mday = tm.tm_mday;
321 alarm.time.tm_mon = tm.tm_mon;
322 alarm.time.tm_year = tm.tm_year;
323 err = rtc_valid_tm(&alarm.time);
324 if (err < 0)
325 return err;
326 rtc_tm_to_time(&alarm.time, &then);
327
328 /* alarm may need to wrap into tomorrow */
329 if (then < now) {
330 rtc_time_to_tm(now + 24 * 60 * 60, &tm);
331 alarm.time.tm_mday = tm.tm_mday;
332 alarm.time.tm_mon = tm.tm_mon;
333 alarm.time.tm_year = tm.tm_year;
334 }
335 }
336
David Brownell5ad31a572008-07-23 21:30:33 -0700337 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800338
339 case RTC_RD_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700340 mutex_unlock(&rtc->ops_lock);
341
David Brownellab6a2d72007-05-08 00:33:30 -0700342 err = rtc_read_time(rtc, &tm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800343 if (err < 0)
344 return err;
345
346 if (copy_to_user(uarg, &tm, sizeof(tm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700347 err = -EFAULT;
348 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800349
350 case RTC_SET_TIME:
David Brownell5ad31a572008-07-23 21:30:33 -0700351 mutex_unlock(&rtc->ops_lock);
352
Alessandro Zummoe8242902006-03-27 01:16:41 -0800353 if (copy_from_user(&tm, uarg, sizeof(tm)))
354 return -EFAULT;
355
David Brownell5ad31a572008-07-23 21:30:33 -0700356 return rtc_set_time(rtc, &tm);
David Brownell2601a462006-11-25 11:09:27 -0800357
Alessandro Zummod691eb92007-10-16 01:28:15 -0700358 case RTC_PIE_ON:
359 err = rtc_irq_set_state(rtc, NULL, 1);
360 break;
361
362 case RTC_PIE_OFF:
363 err = rtc_irq_set_state(rtc, NULL, 0);
David Brownell2601a462006-11-25 11:09:27 -0800364 break;
365
366 case RTC_IRQP_SET:
Alessandro Zummod691eb92007-10-16 01:28:15 -0700367 err = rtc_irq_set_freq(rtc, NULL, arg);
368 break;
369
370 case RTC_IRQP_READ:
371 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
David Brownell2601a462006-11-25 11:09:27 -0800372 break;
373
Alessandro Zummoe8242902006-03-27 01:16:41 -0800374#if 0
375 case RTC_EPOCH_SET:
376#ifndef rtc_epoch
377 /*
378 * There were no RTC clocks before 1900.
379 */
380 if (arg < 1900) {
381 err = -EINVAL;
382 break;
383 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800384 rtc_epoch = arg;
385 err = 0;
386#endif
387 break;
388
389 case RTC_EPOCH_READ:
390 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
391 break;
392#endif
393 case RTC_WKALM_SET:
David Brownell5ad31a572008-07-23 21:30:33 -0700394 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800395 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
396 return -EFAULT;
397
David Brownell5ad31a572008-07-23 21:30:33 -0700398 return rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800399
400 case RTC_WKALM_RD:
David Brownell5ad31a572008-07-23 21:30:33 -0700401 mutex_unlock(&rtc->ops_lock);
David Brownellab6a2d72007-05-08 00:33:30 -0700402 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800403 if (err < 0)
404 return err;
405
406 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
David Brownell5ad31a572008-07-23 21:30:33 -0700407 err = -EFAULT;
408 return err;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800409
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700410#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
411 case RTC_UIE_OFF:
412 clear_uie(rtc);
David Brownell5ad31a572008-07-23 21:30:33 -0700413 break;
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700414
415 case RTC_UIE_ON:
David Brownell5ad31a572008-07-23 21:30:33 -0700416 err = set_uie(rtc);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700417#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800418 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700419 err = -ENOTTY;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800420 break;
421 }
422
David Brownell5ad31a572008-07-23 21:30:33 -0700423done:
424 mutex_unlock(&rtc->ops_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800425 return err;
426}
427
428static int rtc_dev_release(struct inode *inode, struct file *file)
429{
Mark Zhan88efe132007-10-16 01:28:17 -0700430 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800431
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700432#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
433 clear_uie(rtc);
434#endif
Tomas Janousek5cdc98b2008-07-29 22:33:51 -0700435 rtc_irq_set_state(rtc, NULL, 0);
436
Alessandro Zummoe8242902006-03-27 01:16:41 -0800437 if (rtc->ops->release)
David Brownellcd966202007-05-08 00:33:40 -0700438 rtc->ops->release(rtc->dev.parent);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800439
Jiri Kosina372a3022007-12-04 23:45:05 -0800440 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800441 return 0;
442}
443
444static int rtc_dev_fasync(int fd, struct file *file, int on)
445{
Mark Zhan88efe132007-10-16 01:28:17 -0700446 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800447 return fasync_helper(fd, file, on, &rtc->async_queue);
448}
449
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800450static const struct file_operations rtc_dev_fops = {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800451 .owner = THIS_MODULE,
452 .llseek = no_llseek,
453 .read = rtc_dev_read,
454 .poll = rtc_dev_poll,
David Brownell5ad31a572008-07-23 21:30:33 -0700455 .unlocked_ioctl = rtc_dev_ioctl,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800456 .open = rtc_dev_open,
457 .release = rtc_dev_release,
458 .fasync = rtc_dev_fasync,
459};
460
461/* insertion/removal hooks */
462
David Brownellcb3a58d2007-05-08 00:33:46 -0700463void rtc_dev_prepare(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800464{
David Brownell5726fb22007-05-08 00:33:27 -0700465 if (!rtc_devt)
466 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800467
468 if (rtc->id >= RTC_DEV_MAX) {
David Brownell5726fb22007-05-08 00:33:27 -0700469 pr_debug("%s: too many RTC devices\n", rtc->name);
470 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800471 }
472
David Brownellcd966202007-05-08 00:33:40 -0700473 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
David Brownell5726fb22007-05-08 00:33:27 -0700474
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700475#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
David Howellsc4028952006-11-22 14:57:56 +0000476 INIT_WORK(&rtc->uie_task, rtc_uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700477 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
478#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800479
480 cdev_init(&rtc->char_dev, &rtc_dev_fops);
481 rtc->char_dev.owner = rtc->owner;
David Brownellcb3a58d2007-05-08 00:33:46 -0700482}
Alessandro Zummoe8242902006-03-27 01:16:41 -0800483
David Brownellcb3a58d2007-05-08 00:33:46 -0700484void rtc_dev_add_device(struct rtc_device *rtc)
485{
David Brownellcd966202007-05-08 00:33:40 -0700486 if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
David Brownell5726fb22007-05-08 00:33:27 -0700487 printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
488 rtc->name, MAJOR(rtc_devt), rtc->id);
489 else
490 pr_debug("%s: dev (%d:%d)\n", rtc->name,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800491 MAJOR(rtc_devt), rtc->id);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800492}
493
David Brownell5726fb22007-05-08 00:33:27 -0700494void rtc_dev_del_device(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800495{
David Brownellcd966202007-05-08 00:33:40 -0700496 if (rtc->dev.devt)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800497 cdev_del(&rtc->char_dev);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800498}
499
David Brownell5726fb22007-05-08 00:33:27 -0700500void __init rtc_dev_init(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800501{
502 int err;
503
Alessandro Zummoe8242902006-03-27 01:16:41 -0800504 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
David Brownell5726fb22007-05-08 00:33:27 -0700505 if (err < 0)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800506 printk(KERN_ERR "%s: failed to allocate char dev region\n",
507 __FILE__);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800508}
509
David Brownell5726fb22007-05-08 00:33:27 -0700510void __exit rtc_dev_exit(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800511{
David Brownell5726fb22007-05-08 00:33:27 -0700512 if (rtc_devt)
513 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800514}