1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#define RTC_VERSION "1.07"
44
45#include <linux/module.h>
46#include <linux/config.h>
47#include <linux/errno.h>
48#include <linux/miscdevice.h>
49#include <linux/fcntl.h>
50
51#include <linux/rtc.h>
52#include <linux/init.h>
53#include <linux/poll.h>
54#include <linux/proc_fs.h>
55
56#include <asm/uaccess.h>
57#include <asm/system.h>
58#include <asm/rtc.h>
59
60
61
62
63
64
65
66
67static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
68
69
70
71
72
73#define RTC_IS_OPEN 0x01
74
75static unsigned char gen_rtc_status;
76static unsigned long gen_rtc_irq_data;
77
78
79static unsigned char days_in_mo[] =
80{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
81
82static int irq_active;
83
84#ifdef CONFIG_GEN_RTC_X
85struct tq_struct genrtc_task;
86static struct timer_list timer_task;
87
88static unsigned int oldsecs;
89static int lostint;
90static int tt_exp;
91
92static void gen_rtc_timer(unsigned long data);
93
94static volatile int stask_active;
95static volatile int ttask_active;
96static int stop_rtc_timers;
97static spinlock_t gen_rtc_lock = SPIN_LOCK_UNLOCKED;
98
99static void gen_rtc_interrupt(unsigned long arg);
100
101
102
103
104
105static void genrtc_troutine(void *data)
106{
107 unsigned int tmp = get_rtc_ss();
108
109 if (stop_rtc_timers) {
110 stask_active = 0;
111 return;
112 }
113
114 if (oldsecs != tmp){
115 oldsecs = tmp;
116
117 timer_task.function = gen_rtc_timer;
118 timer_task.expires = jiffies + HZ - (HZ/10);
119 tt_exp=timer_task.expires;
120 ttask_active=1;
121 stask_active=0;
122 add_timer(&timer_task);
123
124 gen_rtc_interrupt(0);
125 } else if (schedule_task(&genrtc_task) == 0)
126 stask_active = 0;
127}
128
129static void gen_rtc_timer(unsigned long data)
130{
131 lostint = get_rtc_ss() - oldsecs ;
132 if (lostint<0)
133 lostint = 60 - lostint;
134 if (time_after(jiffies, tt_exp))
135 printk(KERN_INFO "genrtc: timer task delayed by %ld jiffies\n",
136 jiffies-tt_exp);
137 ttask_active=0;
138 stask_active=1;
139 if ((schedule_task(&genrtc_task) == 0))
140 stask_active = 0;
141}
142
143
144
145
146
147
148
149
150static void gen_rtc_interrupt(unsigned long arg)
151{
152
153
154
155
156 gen_rtc_irq_data += 0x100;
157 gen_rtc_irq_data &= ~0xff;
158 gen_rtc_irq_data |= RTC_UIE;
159
160 if (lostint){
161 printk("genrtc: system delaying clock ticks?\n");
162
163 gen_rtc_irq_data += ((lostint-1)<<8);
164 lostint = 0;
165 }
166
167 wake_up_interruptible(&gen_rtc_wait);
168}
169
170
171
172
173static ssize_t gen_rtc_read(struct file *file, char *buf,
174 size_t count, loff_t *ppos)
175{
176 DECLARE_WAITQUEUE(wait, current);
177 unsigned long data;
178 ssize_t retval;
179
180 if (count != sizeof (unsigned int) && count != sizeof (unsigned long))
181 return -EINVAL;
182
183 if (file->f_flags & O_NONBLOCK && !gen_rtc_irq_data)
184 return -EAGAIN;
185
186 add_wait_queue(&gen_rtc_wait, &wait);
187 retval = -ERESTARTSYS;
188
189 while (1) {
190 set_current_state(TASK_INTERRUPTIBLE);
191 data = xchg(&gen_rtc_irq_data, 0);
192 if (data)
193 break;
194 if (signal_pending(current))
195 goto out;
196 schedule();
197 }
198
199
200 if (sizeof (int) != sizeof (long) && count == sizeof (unsigned int)) {
201 unsigned int uidata = data;
202 retval = put_user(uidata, (unsigned long *)buf);
203 }
204 else {
205 retval = put_user(data, (unsigned long *)buf);
206 }
207 if (!retval)
208 retval = sizeof(unsigned long);
209 out:
210 current->state = TASK_RUNNING;
211 remove_wait_queue(&gen_rtc_wait, &wait);
212
213 return retval;
214}
215
216static unsigned int gen_rtc_poll(struct file *file,
217 struct poll_table_struct *wait)
218{
219 poll_wait(file, &gen_rtc_wait, wait);
220 if (gen_rtc_irq_data != 0)
221 return POLLIN | POLLRDNORM;
222 return 0;
223}
224
225#endif
226
227
228
229
230
231
232
233static inline void gen_clear_rtc_irq_bit(unsigned char bit)
234{
235#ifdef CONFIG_GEN_RTC_X
236 stop_rtc_timers = 1;
237 if (ttask_active){
238 del_timer_sync(&timer_task);
239 ttask_active = 0;
240 }
241 while (stask_active)
242 schedule();
243
244 spin_lock(&gen_rtc_lock);
245 irq_active = 0;
246 spin_unlock(&gen_rtc_lock);
247#endif
248}
249
250static inline int gen_set_rtc_irq_bit(unsigned char bit)
251{
252#ifdef CONFIG_GEN_RTC_X
253 spin_lock(&gen_rtc_lock);
254 if ( !irq_active ) {
255 irq_active = 1;
256 stop_rtc_timers = 0;
257 lostint = 0;
258 genrtc_task.routine = genrtc_troutine;
259 oldsecs = get_rtc_ss();
260 init_timer(&timer_task);
261
262 stask_active = 1;
263 if (schedule_task(&genrtc_task) == 0){
264 stask_active = 0;
265 }
266 }
267 spin_unlock(&gen_rtc_lock);
268 gen_rtc_irq_data = 0;
269 return 0;
270#else
271 return -EINVAL;
272#endif
273}
274
275static int gen_rtc_ioctl(struct inode *inode, struct file *file,
276 unsigned int cmd, unsigned long arg)
277{
278 struct rtc_time wtime;
279 struct rtc_pll_info pll;
280
281 switch (cmd) {
282
283 case RTC_PLL_GET:
284 if (get_rtc_pll(&pll))
285 return -EINVAL;
286 else
287 return copy_to_user((void *)arg, &pll, sizeof pll) ? -EFAULT : 0;
288
289 case RTC_PLL_SET:
290 if (!capable(CAP_SYS_TIME))
291 return -EACCES;
292 if (copy_from_user(&pll, (struct rtc_pll_info*)arg,
293 sizeof(pll)))
294 return -EFAULT;
295 return set_rtc_pll(&pll);
296
297 case RTC_UIE_OFF:
298 gen_clear_rtc_irq_bit(RTC_UIE);
299 return 0;
300
301 case RTC_UIE_ON:
302 return gen_set_rtc_irq_bit(RTC_UIE);
303
304 case RTC_RD_TIME:
305
306 memset(&wtime, 0, sizeof(wtime));
307 get_rtc_time(&wtime);
308
309 return copy_to_user((void *)arg, &wtime, sizeof(wtime)) ? -EFAULT : 0;
310
311 case RTC_SET_TIME:
312 {
313 int year;
314 unsigned char leap_yr;
315
316 if (!capable(CAP_SYS_TIME))
317 return -EACCES;
318
319 if (copy_from_user(&wtime, (struct rtc_time *)arg,
320 sizeof(wtime)))
321 return -EFAULT;
322
323 year = wtime.tm_year + 1900;
324 leap_yr = ((!(year % 4) && (year % 100)) ||
325 !(year % 400));
326
327 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
328 return -EINVAL;
329
330 if (wtime.tm_mday < 0 || wtime.tm_mday >
331 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
332 return -EINVAL;
333
334 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
335 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
336 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
337 return -EINVAL;
338
339 return set_rtc_time(&wtime);
340 }
341 }
342
343 return -EINVAL;
344}
345
346
347
348
349
350
351
352static int gen_rtc_open(struct inode *inode, struct file *file)
353{
354 if (gen_rtc_status & RTC_IS_OPEN)
355 return -EBUSY;
356
357 MOD_INC_USE_COUNT;
358
359 gen_rtc_status |= RTC_IS_OPEN;
360 gen_rtc_irq_data = 0;
361 irq_active = 0;
362
363 return 0;
364}
365
366static int gen_rtc_release(struct inode *inode, struct file *file)
367{
368
369
370
371
372
373 gen_clear_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
374
375 gen_rtc_status &= ~RTC_IS_OPEN;
376 MOD_DEC_USE_COUNT;
377
378 return 0;
379}
380
381
382#ifdef CONFIG_PROC_FS
383
384
385
386
387
388static int gen_rtc_proc_output(char *buf)
389{
390 char *p;
391 struct rtc_time tm;
392 unsigned int flags;
393 struct rtc_pll_info pll;
394
395 p = buf;
396
397 flags = get_rtc_time(&tm);
398
399 p += sprintf(p,
400 "rtc_time\t: %02d:%02d:%02d\n"
401 "rtc_date\t: %04d-%02d-%02d\n"
402 "rtc_epoch\t: %04u\n",
403 tm.tm_hour, tm.tm_min, tm.tm_sec,
404 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 1900);
405
406 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
407
408 p += sprintf(p, "alarm\t\t: ");
409 if (tm.tm_hour <= 24)
410 p += sprintf(p, "%02d:", tm.tm_hour);
411 else
412 p += sprintf(p, "**:");
413
414 if (tm.tm_min <= 59)
415 p += sprintf(p, "%02d:", tm.tm_min);
416 else
417 p += sprintf(p, "**:");
418
419 if (tm.tm_sec <= 59)
420 p += sprintf(p, "%02d\n", tm.tm_sec);
421 else
422 p += sprintf(p, "**\n");
423
424 p += sprintf(p,
425 "DST_enable\t: %s\n"
426 "BCD\t\t: %s\n"
427 "24hr\t\t: %s\n"
428 "square_wave\t: %s\n"
429 "alarm_IRQ\t: %s\n"
430 "update_IRQ\t: %s\n"
431 "periodic_IRQ\t: %s\n"
432 "periodic_freq\t: %ld\n"
433 "batt_status\t: %s\n",
434 (flags & RTC_DST_EN) ? "yes" : "no",
435 (flags & RTC_DM_BINARY) ? "no" : "yes",
436 (flags & RTC_24H) ? "yes" : "no",
437 (flags & RTC_SQWE) ? "yes" : "no",
438 (flags & RTC_AIE) ? "yes" : "no",
439 irq_active ? "yes" : "no",
440 (flags & RTC_PIE) ? "yes" : "no",
441 0L ,
442 (flags & RTC_BATT_BAD) ? "bad" : "okay");
443 if (!get_rtc_pll(&pll))
444 p += sprintf(p,
445 "PLL adjustment\t: %d\n"
446 "PLL max +ve adjustment\t: %d\n"
447 "PLL max -ve adjustment\t: %d\n"
448 "PLL +ve adjustment factor\t: %d\n"
449 "PLL -ve adjustment factor\t: %d\n"
450 "PLL frequency\t: %ld\n",
451 pll.pll_value,
452 pll.pll_max,
453 pll.pll_min,
454 pll.pll_posmult,
455 pll.pll_negmult,
456 pll.pll_clock);
457 return p - buf;
458}
459
460static int gen_rtc_read_proc(char *page, char **start, off_t off,
461 int count, int *eof, void *data)
462{
463 int len = gen_rtc_proc_output (page);
464 if (len <= off+count) *eof = 1;
465 *start = page + off;
466 len -= off;
467 if (len>count) len = count;
468 if (len<0) len = 0;
469 return len;
470}
471
472#endif
473
474
475
476
477
478
479static struct file_operations gen_rtc_fops = {
480 .owner = THIS_MODULE,
481#ifdef CONFIG_GEN_RTC_X
482 .read = gen_rtc_read,
483 .poll = gen_rtc_poll,
484#endif
485 .ioctl = gen_rtc_ioctl,
486 .open = gen_rtc_open,
487 .release = gen_rtc_release,
488};
489
490static struct miscdevice rtc_gen_dev =
491{
492 .minor = RTC_MINOR,
493 .name = "rtc",
494 .fops = &gen_rtc_fops,
495};
496
497static int __init rtc_generic_init(void)
498{
499 int retval;
500
501 printk(KERN_INFO "Generic RTC Driver v%s\n", RTC_VERSION);
502
503 retval = misc_register(&rtc_gen_dev);
504 if(retval < 0)
505 return retval;
506
507#ifdef CONFIG_PROC_FS
508 if((create_proc_read_entry ("driver/rtc", 0, 0, gen_rtc_read_proc, NULL)) == NULL){
509 misc_deregister(&rtc_gen_dev);
510 return -ENOMEM;
511 }
512#endif
513
514 return 0;
515}
516
517static void __exit rtc_generic_exit(void)
518{
519 remove_proc_entry ("driver/rtc", NULL);
520 misc_deregister(&rtc_gen_dev);
521}
522
523
524module_init(rtc_generic_init);
525module_exit(rtc_generic_exit);
526EXPORT_NO_SYMBOLS;
527
528MODULE_AUTHOR("Richard Zidlicky");
529MODULE_LICENSE("GPL");
530
531