1 /* include/linux/android_alarm.h
2  *
3  * Copyright (C) 2006-2007 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #ifndef _LINUX_ANDROID_ALARM_H
17 #define _LINUX_ANDROID_ALARM_H
18 
19 #include <linux/ioctl.h>
20 #include <linux/time.h>
21 
22 enum android_alarm_type {
23 	/* return code bit numbers or set alarm arg */
24 	ANDROID_ALARM_RTC_WAKEUP,
25 	ANDROID_ALARM_RTC,
26 	ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
27 	ANDROID_ALARM_ELAPSED_REALTIME,
28 	ANDROID_ALARM_SYSTEMTIME,
29 
30 	ANDROID_ALARM_TYPE_COUNT,
31 
32 	/* return code bit numbers */
33 	/* ANDROID_ALARM_TIME_CHANGE = 16 */
34 };
35 
36 #ifdef __KERNEL__
37 
38 #include <linux/ktime.h>
39 #include <linux/rbtree.h>
40 
41 /*
42  * The alarm interface is similar to the hrtimer interface but adds support
43  * for wakeup from suspend. It also adds an elapsed realtime clock that can
44  * be used for periodic timers that need to keep runing while the system is
45  * suspended and not be disrupted when the wall time is set.
46  */
47 
48 /**
49  * struct alarm - the basic alarm structure
50  * @node:	red black tree node for time ordered insertion
51  * @type:	alarm type. rtc/elapsed-realtime/systemtime, wakeup/non-wakeup.
52  * @softexpires: the absolute earliest expiry time of the alarm.
53  * @expires:	the absolute expiry time.
54  * @function:	alarm expiry callback function
55  *
56  * The alarm structure must be initialized by alarm_init()
57  *
58  */
59 
60 struct android_alarm {
61 	struct rb_node		node;
62 	enum android_alarm_type type;
63 	ktime_t			softexpires;
64 	ktime_t			expires;
65 	void			(*function)(struct android_alarm *);
66 };
67 
68 void android_alarm_init(struct android_alarm *alarm,
69 	enum android_alarm_type type, void (*function)(struct android_alarm *));
70 void android_alarm_start_range(struct android_alarm *alarm, ktime_t start,
71 								ktime_t end);
72 int android_alarm_try_to_cancel(struct android_alarm *alarm);
73 int android_alarm_cancel(struct android_alarm *alarm);
74 ktime_t alarm_get_elapsed_realtime(void);
75 
76 /* set rtc while preserving elapsed realtime */
77 int android_alarm_set_rtc(const struct timespec ts);
78 
79 #ifdef CONFIG_ANDROID_ALARM_OLDDRV_COMPAT
80 /*
81  * Some older drivers depend on the old API,
82  * so provide compatability macros for now.
83  */
84 #define alarm android_alarm
85 #define alarm_init(x, y, z) android_alarm_init(x, y, z)
86 #define alarm_start_range(x, y, z) android_alarm_start_range(x, y, z)
87 #define alarm_try_to_cancel(x) android_alarm_try_to_cancel(x)
88 #define alarm_cancel(x) android_alarm_cancel(x)
89 #define alarm_set_rtc(x) android_alarm_set_rtc(x)
90 #endif
91 
92 
93 #endif
94 
95 enum android_alarm_return_flags {
96 	ANDROID_ALARM_RTC_WAKEUP_MASK = 1U << ANDROID_ALARM_RTC_WAKEUP,
97 	ANDROID_ALARM_RTC_MASK = 1U << ANDROID_ALARM_RTC,
98 	ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK =
99 				1U << ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
100 	ANDROID_ALARM_ELAPSED_REALTIME_MASK =
101 				1U << ANDROID_ALARM_ELAPSED_REALTIME,
102 	ANDROID_ALARM_SYSTEMTIME_MASK = 1U << ANDROID_ALARM_SYSTEMTIME,
103 	ANDROID_ALARM_TIME_CHANGE_MASK = 1U << 16
104 };
105 
106 /* Disable alarm */
107 #define ANDROID_ALARM_CLEAR(type)           _IO('a', 0 | ((type) << 4))
108 
109 /* Ack last alarm and wait for next */
110 #define ANDROID_ALARM_WAIT                  _IO('a', 1)
111 
112 #define ALARM_IOW(c, type, size)            _IOW('a', (c) | ((type) << 4), size)
113 /* Set alarm */
114 #define ANDROID_ALARM_SET(type)             ALARM_IOW(2, type, struct timespec)
115 #define ANDROID_ALARM_SET_AND_WAIT(type)    ALARM_IOW(3, type, struct timespec)
116 #define ANDROID_ALARM_GET_TIME(type)        ALARM_IOW(4, type, struct timespec)
117 #define ANDROID_ALARM_SET_RTC               _IOW('a', 5, struct timespec)
118 #define ANDROID_ALARM_BASE_CMD(cmd)         (cmd & ~(_IOC(0, 0, 0xf0, 0)))
119 #define ANDROID_ALARM_IOCTL_TO_TYPE(cmd)    (_IOC_NR(cmd) >> 4)
120 
121 #endif
122