Commit 4ae0ff16efeffe7d06726fd3022cdb2f3e9e6892
Exists in
master
and in
39 other branches
Merge branch 'timer-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kerne…
…l/git/tip/linux-2.6-tip * 'timer-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: RTC: rtc-omap: Fix a leak of the IRQ during init failure posix clocks: Replace mutex with reader/writer semaphore
Showing 3 changed files Inline Diff
drivers/rtc/rtc-omap.c
1 | /* | 1 | /* |
2 | * TI OMAP1 Real Time Clock interface for Linux | 2 | * TI OMAP1 Real Time Clock interface for Linux |
3 | * | 3 | * |
4 | * Copyright (C) 2003 MontaVista Software, Inc. | 4 | * Copyright (C) 2003 MontaVista Software, Inc. |
5 | * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> | 5 | * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> |
6 | * | 6 | * |
7 | * Copyright (C) 2006 David Brownell (new RTC framework) | 7 | * Copyright (C) 2006 David Brownell (new RTC framework) |
8 | * | 8 | * |
9 | * This program is free software; you can redistribute it and/or | 9 | * This program is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU General Public License | 10 | * modify it under the terms of the GNU General Public License |
11 | * as published by the Free Software Foundation; either version | 11 | * as published by the Free Software Foundation; either version |
12 | * 2 of the License, or (at your option) any later version. | 12 | * 2 of the License, or (at your option) any later version. |
13 | */ | 13 | */ |
14 | 14 | ||
15 | #include <linux/kernel.h> | 15 | #include <linux/kernel.h> |
16 | #include <linux/init.h> | 16 | #include <linux/init.h> |
17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
18 | #include <linux/ioport.h> | 18 | #include <linux/ioport.h> |
19 | #include <linux/delay.h> | 19 | #include <linux/delay.h> |
20 | #include <linux/rtc.h> | 20 | #include <linux/rtc.h> |
21 | #include <linux/bcd.h> | 21 | #include <linux/bcd.h> |
22 | #include <linux/platform_device.h> | 22 | #include <linux/platform_device.h> |
23 | 23 | ||
24 | #include <asm/io.h> | 24 | #include <asm/io.h> |
25 | 25 | ||
26 | 26 | ||
27 | /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock | 27 | /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock |
28 | * with century-range alarm matching, driven by the 32kHz clock. | 28 | * with century-range alarm matching, driven by the 32kHz clock. |
29 | * | 29 | * |
30 | * The main user-visible ways it differs from PC RTCs are by omitting | 30 | * The main user-visible ways it differs from PC RTCs are by omitting |
31 | * "don't care" alarm fields and sub-second periodic IRQs, and having | 31 | * "don't care" alarm fields and sub-second periodic IRQs, and having |
32 | * an autoadjust mechanism to calibrate to the true oscillator rate. | 32 | * an autoadjust mechanism to calibrate to the true oscillator rate. |
33 | * | 33 | * |
34 | * Board-specific wiring options include using split power mode with | 34 | * Board-specific wiring options include using split power mode with |
35 | * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset), | 35 | * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset), |
36 | * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from | 36 | * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from |
37 | * low power modes) for OMAP1 boards (OMAP-L138 has this built into | 37 | * low power modes) for OMAP1 boards (OMAP-L138 has this built into |
38 | * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment. | 38 | * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment. |
39 | */ | 39 | */ |
40 | 40 | ||
41 | #define OMAP_RTC_BASE 0xfffb4800 | 41 | #define OMAP_RTC_BASE 0xfffb4800 |
42 | 42 | ||
43 | /* RTC registers */ | 43 | /* RTC registers */ |
44 | #define OMAP_RTC_SECONDS_REG 0x00 | 44 | #define OMAP_RTC_SECONDS_REG 0x00 |
45 | #define OMAP_RTC_MINUTES_REG 0x04 | 45 | #define OMAP_RTC_MINUTES_REG 0x04 |
46 | #define OMAP_RTC_HOURS_REG 0x08 | 46 | #define OMAP_RTC_HOURS_REG 0x08 |
47 | #define OMAP_RTC_DAYS_REG 0x0C | 47 | #define OMAP_RTC_DAYS_REG 0x0C |
48 | #define OMAP_RTC_MONTHS_REG 0x10 | 48 | #define OMAP_RTC_MONTHS_REG 0x10 |
49 | #define OMAP_RTC_YEARS_REG 0x14 | 49 | #define OMAP_RTC_YEARS_REG 0x14 |
50 | #define OMAP_RTC_WEEKS_REG 0x18 | 50 | #define OMAP_RTC_WEEKS_REG 0x18 |
51 | 51 | ||
52 | #define OMAP_RTC_ALARM_SECONDS_REG 0x20 | 52 | #define OMAP_RTC_ALARM_SECONDS_REG 0x20 |
53 | #define OMAP_RTC_ALARM_MINUTES_REG 0x24 | 53 | #define OMAP_RTC_ALARM_MINUTES_REG 0x24 |
54 | #define OMAP_RTC_ALARM_HOURS_REG 0x28 | 54 | #define OMAP_RTC_ALARM_HOURS_REG 0x28 |
55 | #define OMAP_RTC_ALARM_DAYS_REG 0x2c | 55 | #define OMAP_RTC_ALARM_DAYS_REG 0x2c |
56 | #define OMAP_RTC_ALARM_MONTHS_REG 0x30 | 56 | #define OMAP_RTC_ALARM_MONTHS_REG 0x30 |
57 | #define OMAP_RTC_ALARM_YEARS_REG 0x34 | 57 | #define OMAP_RTC_ALARM_YEARS_REG 0x34 |
58 | 58 | ||
59 | #define OMAP_RTC_CTRL_REG 0x40 | 59 | #define OMAP_RTC_CTRL_REG 0x40 |
60 | #define OMAP_RTC_STATUS_REG 0x44 | 60 | #define OMAP_RTC_STATUS_REG 0x44 |
61 | #define OMAP_RTC_INTERRUPTS_REG 0x48 | 61 | #define OMAP_RTC_INTERRUPTS_REG 0x48 |
62 | 62 | ||
63 | #define OMAP_RTC_COMP_LSB_REG 0x4c | 63 | #define OMAP_RTC_COMP_LSB_REG 0x4c |
64 | #define OMAP_RTC_COMP_MSB_REG 0x50 | 64 | #define OMAP_RTC_COMP_MSB_REG 0x50 |
65 | #define OMAP_RTC_OSC_REG 0x54 | 65 | #define OMAP_RTC_OSC_REG 0x54 |
66 | 66 | ||
67 | /* OMAP_RTC_CTRL_REG bit fields: */ | 67 | /* OMAP_RTC_CTRL_REG bit fields: */ |
68 | #define OMAP_RTC_CTRL_SPLIT (1<<7) | 68 | #define OMAP_RTC_CTRL_SPLIT (1<<7) |
69 | #define OMAP_RTC_CTRL_DISABLE (1<<6) | 69 | #define OMAP_RTC_CTRL_DISABLE (1<<6) |
70 | #define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5) | 70 | #define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5) |
71 | #define OMAP_RTC_CTRL_TEST (1<<4) | 71 | #define OMAP_RTC_CTRL_TEST (1<<4) |
72 | #define OMAP_RTC_CTRL_MODE_12_24 (1<<3) | 72 | #define OMAP_RTC_CTRL_MODE_12_24 (1<<3) |
73 | #define OMAP_RTC_CTRL_AUTO_COMP (1<<2) | 73 | #define OMAP_RTC_CTRL_AUTO_COMP (1<<2) |
74 | #define OMAP_RTC_CTRL_ROUND_30S (1<<1) | 74 | #define OMAP_RTC_CTRL_ROUND_30S (1<<1) |
75 | #define OMAP_RTC_CTRL_STOP (1<<0) | 75 | #define OMAP_RTC_CTRL_STOP (1<<0) |
76 | 76 | ||
77 | /* OMAP_RTC_STATUS_REG bit fields: */ | 77 | /* OMAP_RTC_STATUS_REG bit fields: */ |
78 | #define OMAP_RTC_STATUS_POWER_UP (1<<7) | 78 | #define OMAP_RTC_STATUS_POWER_UP (1<<7) |
79 | #define OMAP_RTC_STATUS_ALARM (1<<6) | 79 | #define OMAP_RTC_STATUS_ALARM (1<<6) |
80 | #define OMAP_RTC_STATUS_1D_EVENT (1<<5) | 80 | #define OMAP_RTC_STATUS_1D_EVENT (1<<5) |
81 | #define OMAP_RTC_STATUS_1H_EVENT (1<<4) | 81 | #define OMAP_RTC_STATUS_1H_EVENT (1<<4) |
82 | #define OMAP_RTC_STATUS_1M_EVENT (1<<3) | 82 | #define OMAP_RTC_STATUS_1M_EVENT (1<<3) |
83 | #define OMAP_RTC_STATUS_1S_EVENT (1<<2) | 83 | #define OMAP_RTC_STATUS_1S_EVENT (1<<2) |
84 | #define OMAP_RTC_STATUS_RUN (1<<1) | 84 | #define OMAP_RTC_STATUS_RUN (1<<1) |
85 | #define OMAP_RTC_STATUS_BUSY (1<<0) | 85 | #define OMAP_RTC_STATUS_BUSY (1<<0) |
86 | 86 | ||
87 | /* OMAP_RTC_INTERRUPTS_REG bit fields: */ | 87 | /* OMAP_RTC_INTERRUPTS_REG bit fields: */ |
88 | #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3) | 88 | #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3) |
89 | #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2) | 89 | #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2) |
90 | 90 | ||
91 | static void __iomem *rtc_base; | 91 | static void __iomem *rtc_base; |
92 | 92 | ||
93 | #define rtc_read(addr) __raw_readb(rtc_base + (addr)) | 93 | #define rtc_read(addr) __raw_readb(rtc_base + (addr)) |
94 | #define rtc_write(val, addr) __raw_writeb(val, rtc_base + (addr)) | 94 | #define rtc_write(val, addr) __raw_writeb(val, rtc_base + (addr)) |
95 | 95 | ||
96 | 96 | ||
97 | /* we rely on the rtc framework to handle locking (rtc->ops_lock), | 97 | /* we rely on the rtc framework to handle locking (rtc->ops_lock), |
98 | * so the only other requirement is that register accesses which | 98 | * so the only other requirement is that register accesses which |
99 | * require BUSY to be clear are made with IRQs locally disabled | 99 | * require BUSY to be clear are made with IRQs locally disabled |
100 | */ | 100 | */ |
101 | static void rtc_wait_not_busy(void) | 101 | static void rtc_wait_not_busy(void) |
102 | { | 102 | { |
103 | int count = 0; | 103 | int count = 0; |
104 | u8 status; | 104 | u8 status; |
105 | 105 | ||
106 | /* BUSY may stay active for 1/32768 second (~30 usec) */ | 106 | /* BUSY may stay active for 1/32768 second (~30 usec) */ |
107 | for (count = 0; count < 50; count++) { | 107 | for (count = 0; count < 50; count++) { |
108 | status = rtc_read(OMAP_RTC_STATUS_REG); | 108 | status = rtc_read(OMAP_RTC_STATUS_REG); |
109 | if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0) | 109 | if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0) |
110 | break; | 110 | break; |
111 | udelay(1); | 111 | udelay(1); |
112 | } | 112 | } |
113 | /* now we have ~15 usec to read/write various registers */ | 113 | /* now we have ~15 usec to read/write various registers */ |
114 | } | 114 | } |
115 | 115 | ||
116 | static irqreturn_t rtc_irq(int irq, void *rtc) | 116 | static irqreturn_t rtc_irq(int irq, void *rtc) |
117 | { | 117 | { |
118 | unsigned long events = 0; | 118 | unsigned long events = 0; |
119 | u8 irq_data; | 119 | u8 irq_data; |
120 | 120 | ||
121 | irq_data = rtc_read(OMAP_RTC_STATUS_REG); | 121 | irq_data = rtc_read(OMAP_RTC_STATUS_REG); |
122 | 122 | ||
123 | /* alarm irq? */ | 123 | /* alarm irq? */ |
124 | if (irq_data & OMAP_RTC_STATUS_ALARM) { | 124 | if (irq_data & OMAP_RTC_STATUS_ALARM) { |
125 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); | 125 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); |
126 | events |= RTC_IRQF | RTC_AF; | 126 | events |= RTC_IRQF | RTC_AF; |
127 | } | 127 | } |
128 | 128 | ||
129 | /* 1/sec periodic/update irq? */ | 129 | /* 1/sec periodic/update irq? */ |
130 | if (irq_data & OMAP_RTC_STATUS_1S_EVENT) | 130 | if (irq_data & OMAP_RTC_STATUS_1S_EVENT) |
131 | events |= RTC_IRQF | RTC_UF; | 131 | events |= RTC_IRQF | RTC_UF; |
132 | 132 | ||
133 | rtc_update_irq(rtc, 1, events); | 133 | rtc_update_irq(rtc, 1, events); |
134 | 134 | ||
135 | return IRQ_HANDLED; | 135 | return IRQ_HANDLED; |
136 | } | 136 | } |
137 | 137 | ||
138 | static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) | 138 | static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
139 | { | 139 | { |
140 | u8 reg; | 140 | u8 reg; |
141 | 141 | ||
142 | local_irq_disable(); | 142 | local_irq_disable(); |
143 | rtc_wait_not_busy(); | 143 | rtc_wait_not_busy(); |
144 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); | 144 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
145 | if (enabled) | 145 | if (enabled) |
146 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; | 146 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; |
147 | else | 147 | else |
148 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; | 148 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; |
149 | rtc_wait_not_busy(); | 149 | rtc_wait_not_busy(); |
150 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); | 150 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); |
151 | local_irq_enable(); | 151 | local_irq_enable(); |
152 | 152 | ||
153 | return 0; | 153 | return 0; |
154 | } | 154 | } |
155 | 155 | ||
156 | /* this hardware doesn't support "don't care" alarm fields */ | 156 | /* this hardware doesn't support "don't care" alarm fields */ |
157 | static int tm2bcd(struct rtc_time *tm) | 157 | static int tm2bcd(struct rtc_time *tm) |
158 | { | 158 | { |
159 | if (rtc_valid_tm(tm) != 0) | 159 | if (rtc_valid_tm(tm) != 0) |
160 | return -EINVAL; | 160 | return -EINVAL; |
161 | 161 | ||
162 | tm->tm_sec = bin2bcd(tm->tm_sec); | 162 | tm->tm_sec = bin2bcd(tm->tm_sec); |
163 | tm->tm_min = bin2bcd(tm->tm_min); | 163 | tm->tm_min = bin2bcd(tm->tm_min); |
164 | tm->tm_hour = bin2bcd(tm->tm_hour); | 164 | tm->tm_hour = bin2bcd(tm->tm_hour); |
165 | tm->tm_mday = bin2bcd(tm->tm_mday); | 165 | tm->tm_mday = bin2bcd(tm->tm_mday); |
166 | 166 | ||
167 | tm->tm_mon = bin2bcd(tm->tm_mon + 1); | 167 | tm->tm_mon = bin2bcd(tm->tm_mon + 1); |
168 | 168 | ||
169 | /* epoch == 1900 */ | 169 | /* epoch == 1900 */ |
170 | if (tm->tm_year < 100 || tm->tm_year > 199) | 170 | if (tm->tm_year < 100 || tm->tm_year > 199) |
171 | return -EINVAL; | 171 | return -EINVAL; |
172 | tm->tm_year = bin2bcd(tm->tm_year - 100); | 172 | tm->tm_year = bin2bcd(tm->tm_year - 100); |
173 | 173 | ||
174 | return 0; | 174 | return 0; |
175 | } | 175 | } |
176 | 176 | ||
177 | static void bcd2tm(struct rtc_time *tm) | 177 | static void bcd2tm(struct rtc_time *tm) |
178 | { | 178 | { |
179 | tm->tm_sec = bcd2bin(tm->tm_sec); | 179 | tm->tm_sec = bcd2bin(tm->tm_sec); |
180 | tm->tm_min = bcd2bin(tm->tm_min); | 180 | tm->tm_min = bcd2bin(tm->tm_min); |
181 | tm->tm_hour = bcd2bin(tm->tm_hour); | 181 | tm->tm_hour = bcd2bin(tm->tm_hour); |
182 | tm->tm_mday = bcd2bin(tm->tm_mday); | 182 | tm->tm_mday = bcd2bin(tm->tm_mday); |
183 | tm->tm_mon = bcd2bin(tm->tm_mon) - 1; | 183 | tm->tm_mon = bcd2bin(tm->tm_mon) - 1; |
184 | /* epoch == 1900 */ | 184 | /* epoch == 1900 */ |
185 | tm->tm_year = bcd2bin(tm->tm_year) + 100; | 185 | tm->tm_year = bcd2bin(tm->tm_year) + 100; |
186 | } | 186 | } |
187 | 187 | ||
188 | 188 | ||
189 | static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm) | 189 | static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm) |
190 | { | 190 | { |
191 | /* we don't report wday/yday/isdst ... */ | 191 | /* we don't report wday/yday/isdst ... */ |
192 | local_irq_disable(); | 192 | local_irq_disable(); |
193 | rtc_wait_not_busy(); | 193 | rtc_wait_not_busy(); |
194 | 194 | ||
195 | tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG); | 195 | tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG); |
196 | tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG); | 196 | tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG); |
197 | tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG); | 197 | tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG); |
198 | tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG); | 198 | tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG); |
199 | tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG); | 199 | tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG); |
200 | tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG); | 200 | tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG); |
201 | 201 | ||
202 | local_irq_enable(); | 202 | local_irq_enable(); |
203 | 203 | ||
204 | bcd2tm(tm); | 204 | bcd2tm(tm); |
205 | return 0; | 205 | return 0; |
206 | } | 206 | } |
207 | 207 | ||
208 | static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm) | 208 | static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm) |
209 | { | 209 | { |
210 | if (tm2bcd(tm) < 0) | 210 | if (tm2bcd(tm) < 0) |
211 | return -EINVAL; | 211 | return -EINVAL; |
212 | local_irq_disable(); | 212 | local_irq_disable(); |
213 | rtc_wait_not_busy(); | 213 | rtc_wait_not_busy(); |
214 | 214 | ||
215 | rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG); | 215 | rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG); |
216 | rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG); | 216 | rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG); |
217 | rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG); | 217 | rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG); |
218 | rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG); | 218 | rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG); |
219 | rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG); | 219 | rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG); |
220 | rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG); | 220 | rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG); |
221 | 221 | ||
222 | local_irq_enable(); | 222 | local_irq_enable(); |
223 | 223 | ||
224 | return 0; | 224 | return 0; |
225 | } | 225 | } |
226 | 226 | ||
227 | static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) | 227 | static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
228 | { | 228 | { |
229 | local_irq_disable(); | 229 | local_irq_disable(); |
230 | rtc_wait_not_busy(); | 230 | rtc_wait_not_busy(); |
231 | 231 | ||
232 | alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG); | 232 | alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG); |
233 | alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG); | 233 | alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG); |
234 | alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG); | 234 | alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG); |
235 | alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG); | 235 | alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG); |
236 | alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG); | 236 | alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG); |
237 | alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG); | 237 | alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG); |
238 | 238 | ||
239 | local_irq_enable(); | 239 | local_irq_enable(); |
240 | 240 | ||
241 | bcd2tm(&alm->time); | 241 | bcd2tm(&alm->time); |
242 | alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) | 242 | alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) |
243 | & OMAP_RTC_INTERRUPTS_IT_ALARM); | 243 | & OMAP_RTC_INTERRUPTS_IT_ALARM); |
244 | 244 | ||
245 | return 0; | 245 | return 0; |
246 | } | 246 | } |
247 | 247 | ||
248 | static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) | 248 | static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
249 | { | 249 | { |
250 | u8 reg; | 250 | u8 reg; |
251 | 251 | ||
252 | if (tm2bcd(&alm->time) < 0) | 252 | if (tm2bcd(&alm->time) < 0) |
253 | return -EINVAL; | 253 | return -EINVAL; |
254 | 254 | ||
255 | local_irq_disable(); | 255 | local_irq_disable(); |
256 | rtc_wait_not_busy(); | 256 | rtc_wait_not_busy(); |
257 | 257 | ||
258 | rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG); | 258 | rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG); |
259 | rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG); | 259 | rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG); |
260 | rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG); | 260 | rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG); |
261 | rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG); | 261 | rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG); |
262 | rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG); | 262 | rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG); |
263 | rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG); | 263 | rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG); |
264 | 264 | ||
265 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); | 265 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
266 | if (alm->enabled) | 266 | if (alm->enabled) |
267 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; | 267 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; |
268 | else | 268 | else |
269 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; | 269 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; |
270 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); | 270 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); |
271 | 271 | ||
272 | local_irq_enable(); | 272 | local_irq_enable(); |
273 | 273 | ||
274 | return 0; | 274 | return 0; |
275 | } | 275 | } |
276 | 276 | ||
277 | static struct rtc_class_ops omap_rtc_ops = { | 277 | static struct rtc_class_ops omap_rtc_ops = { |
278 | .read_time = omap_rtc_read_time, | 278 | .read_time = omap_rtc_read_time, |
279 | .set_time = omap_rtc_set_time, | 279 | .set_time = omap_rtc_set_time, |
280 | .read_alarm = omap_rtc_read_alarm, | 280 | .read_alarm = omap_rtc_read_alarm, |
281 | .set_alarm = omap_rtc_set_alarm, | 281 | .set_alarm = omap_rtc_set_alarm, |
282 | .alarm_irq_enable = omap_rtc_alarm_irq_enable, | 282 | .alarm_irq_enable = omap_rtc_alarm_irq_enable, |
283 | }; | 283 | }; |
284 | 284 | ||
285 | static int omap_rtc_alarm; | 285 | static int omap_rtc_alarm; |
286 | static int omap_rtc_timer; | 286 | static int omap_rtc_timer; |
287 | 287 | ||
288 | static int __init omap_rtc_probe(struct platform_device *pdev) | 288 | static int __init omap_rtc_probe(struct platform_device *pdev) |
289 | { | 289 | { |
290 | struct resource *res, *mem; | 290 | struct resource *res, *mem; |
291 | struct rtc_device *rtc; | 291 | struct rtc_device *rtc; |
292 | u8 reg, new_ctrl; | 292 | u8 reg, new_ctrl; |
293 | 293 | ||
294 | omap_rtc_timer = platform_get_irq(pdev, 0); | 294 | omap_rtc_timer = platform_get_irq(pdev, 0); |
295 | if (omap_rtc_timer <= 0) { | 295 | if (omap_rtc_timer <= 0) { |
296 | pr_debug("%s: no update irq?\n", pdev->name); | 296 | pr_debug("%s: no update irq?\n", pdev->name); |
297 | return -ENOENT; | 297 | return -ENOENT; |
298 | } | 298 | } |
299 | 299 | ||
300 | omap_rtc_alarm = platform_get_irq(pdev, 1); | 300 | omap_rtc_alarm = platform_get_irq(pdev, 1); |
301 | if (omap_rtc_alarm <= 0) { | 301 | if (omap_rtc_alarm <= 0) { |
302 | pr_debug("%s: no alarm irq?\n", pdev->name); | 302 | pr_debug("%s: no alarm irq?\n", pdev->name); |
303 | return -ENOENT; | 303 | return -ENOENT; |
304 | } | 304 | } |
305 | 305 | ||
306 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 306 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
307 | if (!res) { | 307 | if (!res) { |
308 | pr_debug("%s: RTC resource data missing\n", pdev->name); | 308 | pr_debug("%s: RTC resource data missing\n", pdev->name); |
309 | return -ENOENT; | 309 | return -ENOENT; |
310 | } | 310 | } |
311 | 311 | ||
312 | mem = request_mem_region(res->start, resource_size(res), pdev->name); | 312 | mem = request_mem_region(res->start, resource_size(res), pdev->name); |
313 | if (!mem) { | 313 | if (!mem) { |
314 | pr_debug("%s: RTC registers at %08x are not free\n", | 314 | pr_debug("%s: RTC registers at %08x are not free\n", |
315 | pdev->name, res->start); | 315 | pdev->name, res->start); |
316 | return -EBUSY; | 316 | return -EBUSY; |
317 | } | 317 | } |
318 | 318 | ||
319 | rtc_base = ioremap(res->start, resource_size(res)); | 319 | rtc_base = ioremap(res->start, resource_size(res)); |
320 | if (!rtc_base) { | 320 | if (!rtc_base) { |
321 | pr_debug("%s: RTC registers can't be mapped\n", pdev->name); | 321 | pr_debug("%s: RTC registers can't be mapped\n", pdev->name); |
322 | goto fail; | 322 | goto fail; |
323 | } | 323 | } |
324 | 324 | ||
325 | rtc = rtc_device_register(pdev->name, &pdev->dev, | 325 | rtc = rtc_device_register(pdev->name, &pdev->dev, |
326 | &omap_rtc_ops, THIS_MODULE); | 326 | &omap_rtc_ops, THIS_MODULE); |
327 | if (IS_ERR(rtc)) { | 327 | if (IS_ERR(rtc)) { |
328 | pr_debug("%s: can't register RTC device, err %ld\n", | 328 | pr_debug("%s: can't register RTC device, err %ld\n", |
329 | pdev->name, PTR_ERR(rtc)); | 329 | pdev->name, PTR_ERR(rtc)); |
330 | goto fail0; | 330 | goto fail0; |
331 | } | 331 | } |
332 | platform_set_drvdata(pdev, rtc); | 332 | platform_set_drvdata(pdev, rtc); |
333 | dev_set_drvdata(&rtc->dev, mem); | 333 | dev_set_drvdata(&rtc->dev, mem); |
334 | 334 | ||
335 | /* clear pending irqs, and set 1/second periodic, | 335 | /* clear pending irqs, and set 1/second periodic, |
336 | * which we'll use instead of update irqs | 336 | * which we'll use instead of update irqs |
337 | */ | 337 | */ |
338 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); | 338 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
339 | 339 | ||
340 | /* clear old status */ | 340 | /* clear old status */ |
341 | reg = rtc_read(OMAP_RTC_STATUS_REG); | 341 | reg = rtc_read(OMAP_RTC_STATUS_REG); |
342 | if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) { | 342 | if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) { |
343 | pr_info("%s: RTC power up reset detected\n", | 343 | pr_info("%s: RTC power up reset detected\n", |
344 | pdev->name); | 344 | pdev->name); |
345 | rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG); | 345 | rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG); |
346 | } | 346 | } |
347 | if (reg & (u8) OMAP_RTC_STATUS_ALARM) | 347 | if (reg & (u8) OMAP_RTC_STATUS_ALARM) |
348 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); | 348 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); |
349 | 349 | ||
350 | /* handle periodic and alarm irqs */ | 350 | /* handle periodic and alarm irqs */ |
351 | if (request_irq(omap_rtc_timer, rtc_irq, IRQF_DISABLED, | 351 | if (request_irq(omap_rtc_timer, rtc_irq, IRQF_DISABLED, |
352 | dev_name(&rtc->dev), rtc)) { | 352 | dev_name(&rtc->dev), rtc)) { |
353 | pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", | 353 | pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", |
354 | pdev->name, omap_rtc_timer); | 354 | pdev->name, omap_rtc_timer); |
355 | goto fail1; | 355 | goto fail1; |
356 | } | 356 | } |
357 | if ((omap_rtc_timer != omap_rtc_alarm) && | 357 | if ((omap_rtc_timer != omap_rtc_alarm) && |
358 | (request_irq(omap_rtc_alarm, rtc_irq, IRQF_DISABLED, | 358 | (request_irq(omap_rtc_alarm, rtc_irq, IRQF_DISABLED, |
359 | dev_name(&rtc->dev), rtc))) { | 359 | dev_name(&rtc->dev), rtc))) { |
360 | pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n", | 360 | pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n", |
361 | pdev->name, omap_rtc_alarm); | 361 | pdev->name, omap_rtc_alarm); |
362 | goto fail2; | 362 | goto fail2; |
363 | } | 363 | } |
364 | 364 | ||
365 | /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ | 365 | /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ |
366 | reg = rtc_read(OMAP_RTC_CTRL_REG); | 366 | reg = rtc_read(OMAP_RTC_CTRL_REG); |
367 | if (reg & (u8) OMAP_RTC_CTRL_STOP) | 367 | if (reg & (u8) OMAP_RTC_CTRL_STOP) |
368 | pr_info("%s: already running\n", pdev->name); | 368 | pr_info("%s: already running\n", pdev->name); |
369 | 369 | ||
370 | /* force to 24 hour mode */ | 370 | /* force to 24 hour mode */ |
371 | new_ctrl = reg & ~(OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP); | 371 | new_ctrl = reg & ~(OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP); |
372 | new_ctrl |= OMAP_RTC_CTRL_STOP; | 372 | new_ctrl |= OMAP_RTC_CTRL_STOP; |
373 | 373 | ||
374 | /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: | 374 | /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: |
375 | * | 375 | * |
376 | * - Device wake-up capability setting should come through chip | 376 | * - Device wake-up capability setting should come through chip |
377 | * init logic. OMAP1 boards should initialize the "wakeup capable" | 377 | * init logic. OMAP1 boards should initialize the "wakeup capable" |
378 | * flag in the platform device if the board is wired right for | 378 | * flag in the platform device if the board is wired right for |
379 | * being woken up by RTC alarm. For OMAP-L138, this capability | 379 | * being woken up by RTC alarm. For OMAP-L138, this capability |
380 | * is built into the SoC by the "Deep Sleep" capability. | 380 | * is built into the SoC by the "Deep Sleep" capability. |
381 | * | 381 | * |
382 | * - Boards wired so RTC_ON_nOFF is used as the reset signal, | 382 | * - Boards wired so RTC_ON_nOFF is used as the reset signal, |
383 | * rather than nPWRON_RESET, should forcibly enable split | 383 | * rather than nPWRON_RESET, should forcibly enable split |
384 | * power mode. (Some chip errata report that RTC_CTRL_SPLIT | 384 | * power mode. (Some chip errata report that RTC_CTRL_SPLIT |
385 | * is write-only, and always reads as zero...) | 385 | * is write-only, and always reads as zero...) |
386 | */ | 386 | */ |
387 | 387 | ||
388 | if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) | 388 | if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) |
389 | pr_info("%s: split power mode\n", pdev->name); | 389 | pr_info("%s: split power mode\n", pdev->name); |
390 | 390 | ||
391 | if (reg != new_ctrl) | 391 | if (reg != new_ctrl) |
392 | rtc_write(new_ctrl, OMAP_RTC_CTRL_REG); | 392 | rtc_write(new_ctrl, OMAP_RTC_CTRL_REG); |
393 | 393 | ||
394 | return 0; | 394 | return 0; |
395 | 395 | ||
396 | fail2: | 396 | fail2: |
397 | free_irq(omap_rtc_timer, NULL); | 397 | free_irq(omap_rtc_timer, rtc); |
398 | fail1: | 398 | fail1: |
399 | rtc_device_unregister(rtc); | 399 | rtc_device_unregister(rtc); |
400 | fail0: | 400 | fail0: |
401 | iounmap(rtc_base); | 401 | iounmap(rtc_base); |
402 | fail: | 402 | fail: |
403 | release_mem_region(mem->start, resource_size(mem)); | 403 | release_mem_region(mem->start, resource_size(mem)); |
404 | return -EIO; | 404 | return -EIO; |
405 | } | 405 | } |
406 | 406 | ||
407 | static int __exit omap_rtc_remove(struct platform_device *pdev) | 407 | static int __exit omap_rtc_remove(struct platform_device *pdev) |
408 | { | 408 | { |
409 | struct rtc_device *rtc = platform_get_drvdata(pdev); | 409 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
410 | struct resource *mem = dev_get_drvdata(&rtc->dev); | 410 | struct resource *mem = dev_get_drvdata(&rtc->dev); |
411 | 411 | ||
412 | device_init_wakeup(&pdev->dev, 0); | 412 | device_init_wakeup(&pdev->dev, 0); |
413 | 413 | ||
414 | /* leave rtc running, but disable irqs */ | 414 | /* leave rtc running, but disable irqs */ |
415 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); | 415 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
416 | 416 | ||
417 | free_irq(omap_rtc_timer, rtc); | 417 | free_irq(omap_rtc_timer, rtc); |
418 | 418 | ||
419 | if (omap_rtc_timer != omap_rtc_alarm) | 419 | if (omap_rtc_timer != omap_rtc_alarm) |
420 | free_irq(omap_rtc_alarm, rtc); | 420 | free_irq(omap_rtc_alarm, rtc); |
421 | 421 | ||
422 | rtc_device_unregister(rtc); | 422 | rtc_device_unregister(rtc); |
423 | iounmap(rtc_base); | 423 | iounmap(rtc_base); |
424 | release_mem_region(mem->start, resource_size(mem)); | 424 | release_mem_region(mem->start, resource_size(mem)); |
425 | return 0; | 425 | return 0; |
426 | } | 426 | } |
427 | 427 | ||
428 | #ifdef CONFIG_PM | 428 | #ifdef CONFIG_PM |
429 | 429 | ||
430 | static u8 irqstat; | 430 | static u8 irqstat; |
431 | 431 | ||
432 | static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state) | 432 | static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state) |
433 | { | 433 | { |
434 | irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG); | 434 | irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
435 | 435 | ||
436 | /* FIXME the RTC alarm is not currently acting as a wakeup event | 436 | /* FIXME the RTC alarm is not currently acting as a wakeup event |
437 | * source, and in fact this enable() call is just saving a flag | 437 | * source, and in fact this enable() call is just saving a flag |
438 | * that's never used... | 438 | * that's never used... |
439 | */ | 439 | */ |
440 | if (device_may_wakeup(&pdev->dev)) | 440 | if (device_may_wakeup(&pdev->dev)) |
441 | enable_irq_wake(omap_rtc_alarm); | 441 | enable_irq_wake(omap_rtc_alarm); |
442 | else | 442 | else |
443 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); | 443 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
444 | 444 | ||
445 | return 0; | 445 | return 0; |
446 | } | 446 | } |
447 | 447 | ||
448 | static int omap_rtc_resume(struct platform_device *pdev) | 448 | static int omap_rtc_resume(struct platform_device *pdev) |
449 | { | 449 | { |
450 | if (device_may_wakeup(&pdev->dev)) | 450 | if (device_may_wakeup(&pdev->dev)) |
451 | disable_irq_wake(omap_rtc_alarm); | 451 | disable_irq_wake(omap_rtc_alarm); |
452 | else | 452 | else |
453 | rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG); | 453 | rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG); |
454 | return 0; | 454 | return 0; |
455 | } | 455 | } |
456 | 456 | ||
457 | #else | 457 | #else |
458 | #define omap_rtc_suspend NULL | 458 | #define omap_rtc_suspend NULL |
459 | #define omap_rtc_resume NULL | 459 | #define omap_rtc_resume NULL |
460 | #endif | 460 | #endif |
461 | 461 | ||
462 | static void omap_rtc_shutdown(struct platform_device *pdev) | 462 | static void omap_rtc_shutdown(struct platform_device *pdev) |
463 | { | 463 | { |
464 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); | 464 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
465 | } | 465 | } |
466 | 466 | ||
467 | MODULE_ALIAS("platform:omap_rtc"); | 467 | MODULE_ALIAS("platform:omap_rtc"); |
468 | static struct platform_driver omap_rtc_driver = { | 468 | static struct platform_driver omap_rtc_driver = { |
469 | .remove = __exit_p(omap_rtc_remove), | 469 | .remove = __exit_p(omap_rtc_remove), |
470 | .suspend = omap_rtc_suspend, | 470 | .suspend = omap_rtc_suspend, |
471 | .resume = omap_rtc_resume, | 471 | .resume = omap_rtc_resume, |
472 | .shutdown = omap_rtc_shutdown, | 472 | .shutdown = omap_rtc_shutdown, |
473 | .driver = { | 473 | .driver = { |
474 | .name = "omap_rtc", | 474 | .name = "omap_rtc", |
475 | .owner = THIS_MODULE, | 475 | .owner = THIS_MODULE, |
476 | }, | 476 | }, |
477 | }; | 477 | }; |
478 | 478 | ||
479 | static int __init rtc_init(void) | 479 | static int __init rtc_init(void) |
480 | { | 480 | { |
481 | return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe); | 481 | return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe); |
482 | } | 482 | } |
483 | module_init(rtc_init); | 483 | module_init(rtc_init); |
484 | 484 | ||
485 | static void __exit rtc_exit(void) | 485 | static void __exit rtc_exit(void) |
486 | { | 486 | { |
487 | platform_driver_unregister(&omap_rtc_driver); | 487 | platform_driver_unregister(&omap_rtc_driver); |
488 | } | 488 | } |
489 | module_exit(rtc_exit); | 489 | module_exit(rtc_exit); |
490 | 490 | ||
491 | MODULE_AUTHOR("George G. Davis (and others)"); | 491 | MODULE_AUTHOR("George G. Davis (and others)"); |
492 | MODULE_LICENSE("GPL"); | 492 | MODULE_LICENSE("GPL"); |
493 | 493 |
include/linux/posix-clock.h
1 | /* | 1 | /* |
2 | * posix-clock.h - support for dynamic clock devices | 2 | * posix-clock.h - support for dynamic clock devices |
3 | * | 3 | * |
4 | * Copyright (C) 2010 OMICRON electronics GmbH | 4 | * Copyright (C) 2010 OMICRON electronics GmbH |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | 7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or | 8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. | 9 | * (at your option) any later version. |
10 | * | 10 | * |
11 | * This program is distributed in the hope that it will be useful, | 11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. | 14 | * GNU General Public License for more details. |
15 | * | 15 | * |
16 | * You should have received a copy of the GNU General Public License | 16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | */ | 19 | */ |
20 | #ifndef _LINUX_POSIX_CLOCK_H_ | 20 | #ifndef _LINUX_POSIX_CLOCK_H_ |
21 | #define _LINUX_POSIX_CLOCK_H_ | 21 | #define _LINUX_POSIX_CLOCK_H_ |
22 | 22 | ||
23 | #include <linux/cdev.h> | 23 | #include <linux/cdev.h> |
24 | #include <linux/fs.h> | 24 | #include <linux/fs.h> |
25 | #include <linux/poll.h> | 25 | #include <linux/poll.h> |
26 | #include <linux/posix-timers.h> | 26 | #include <linux/posix-timers.h> |
27 | #include <linux/rwsem.h> | ||
27 | 28 | ||
28 | struct posix_clock; | 29 | struct posix_clock; |
29 | 30 | ||
30 | /** | 31 | /** |
31 | * struct posix_clock_operations - functional interface to the clock | 32 | * struct posix_clock_operations - functional interface to the clock |
32 | * | 33 | * |
33 | * Every posix clock is represented by a character device. Drivers may | 34 | * Every posix clock is represented by a character device. Drivers may |
34 | * optionally offer extended capabilities by implementing the | 35 | * optionally offer extended capabilities by implementing the |
35 | * character device methods. The character device file operations are | 36 | * character device methods. The character device file operations are |
36 | * first handled by the clock device layer, then passed on to the | 37 | * first handled by the clock device layer, then passed on to the |
37 | * driver by calling these functions. | 38 | * driver by calling these functions. |
38 | * | 39 | * |
39 | * @owner: The clock driver should set to THIS_MODULE | 40 | * @owner: The clock driver should set to THIS_MODULE |
40 | * @clock_adjtime: Adjust the clock | 41 | * @clock_adjtime: Adjust the clock |
41 | * @clock_gettime: Read the current time | 42 | * @clock_gettime: Read the current time |
42 | * @clock_getres: Get the clock resolution | 43 | * @clock_getres: Get the clock resolution |
43 | * @clock_settime: Set the current time value | 44 | * @clock_settime: Set the current time value |
44 | * @timer_create: Create a new timer | 45 | * @timer_create: Create a new timer |
45 | * @timer_delete: Remove a previously created timer | 46 | * @timer_delete: Remove a previously created timer |
46 | * @timer_gettime: Get remaining time and interval of a timer | 47 | * @timer_gettime: Get remaining time and interval of a timer |
47 | * @timer_setttime: Set a timer's initial expiration and interval | 48 | * @timer_setttime: Set a timer's initial expiration and interval |
48 | * @fasync: Optional character device fasync method | 49 | * @fasync: Optional character device fasync method |
49 | * @mmap: Optional character device mmap method | 50 | * @mmap: Optional character device mmap method |
50 | * @open: Optional character device open method | 51 | * @open: Optional character device open method |
51 | * @release: Optional character device release method | 52 | * @release: Optional character device release method |
52 | * @ioctl: Optional character device ioctl method | 53 | * @ioctl: Optional character device ioctl method |
53 | * @read: Optional character device read method | 54 | * @read: Optional character device read method |
54 | * @poll: Optional character device poll method | 55 | * @poll: Optional character device poll method |
55 | */ | 56 | */ |
56 | struct posix_clock_operations { | 57 | struct posix_clock_operations { |
57 | struct module *owner; | 58 | struct module *owner; |
58 | 59 | ||
59 | int (*clock_adjtime)(struct posix_clock *pc, struct timex *tx); | 60 | int (*clock_adjtime)(struct posix_clock *pc, struct timex *tx); |
60 | 61 | ||
61 | int (*clock_gettime)(struct posix_clock *pc, struct timespec *ts); | 62 | int (*clock_gettime)(struct posix_clock *pc, struct timespec *ts); |
62 | 63 | ||
63 | int (*clock_getres) (struct posix_clock *pc, struct timespec *ts); | 64 | int (*clock_getres) (struct posix_clock *pc, struct timespec *ts); |
64 | 65 | ||
65 | int (*clock_settime)(struct posix_clock *pc, | 66 | int (*clock_settime)(struct posix_clock *pc, |
66 | const struct timespec *ts); | 67 | const struct timespec *ts); |
67 | 68 | ||
68 | int (*timer_create) (struct posix_clock *pc, struct k_itimer *kit); | 69 | int (*timer_create) (struct posix_clock *pc, struct k_itimer *kit); |
69 | 70 | ||
70 | int (*timer_delete) (struct posix_clock *pc, struct k_itimer *kit); | 71 | int (*timer_delete) (struct posix_clock *pc, struct k_itimer *kit); |
71 | 72 | ||
72 | void (*timer_gettime)(struct posix_clock *pc, | 73 | void (*timer_gettime)(struct posix_clock *pc, |
73 | struct k_itimer *kit, struct itimerspec *tsp); | 74 | struct k_itimer *kit, struct itimerspec *tsp); |
74 | 75 | ||
75 | int (*timer_settime)(struct posix_clock *pc, | 76 | int (*timer_settime)(struct posix_clock *pc, |
76 | struct k_itimer *kit, int flags, | 77 | struct k_itimer *kit, int flags, |
77 | struct itimerspec *tsp, struct itimerspec *old); | 78 | struct itimerspec *tsp, struct itimerspec *old); |
78 | /* | 79 | /* |
79 | * Optional character device methods: | 80 | * Optional character device methods: |
80 | */ | 81 | */ |
81 | int (*fasync) (struct posix_clock *pc, | 82 | int (*fasync) (struct posix_clock *pc, |
82 | int fd, struct file *file, int on); | 83 | int fd, struct file *file, int on); |
83 | 84 | ||
84 | long (*ioctl) (struct posix_clock *pc, | 85 | long (*ioctl) (struct posix_clock *pc, |
85 | unsigned int cmd, unsigned long arg); | 86 | unsigned int cmd, unsigned long arg); |
86 | 87 | ||
87 | int (*mmap) (struct posix_clock *pc, | 88 | int (*mmap) (struct posix_clock *pc, |
88 | struct vm_area_struct *vma); | 89 | struct vm_area_struct *vma); |
89 | 90 | ||
90 | int (*open) (struct posix_clock *pc, fmode_t f_mode); | 91 | int (*open) (struct posix_clock *pc, fmode_t f_mode); |
91 | 92 | ||
92 | uint (*poll) (struct posix_clock *pc, | 93 | uint (*poll) (struct posix_clock *pc, |
93 | struct file *file, poll_table *wait); | 94 | struct file *file, poll_table *wait); |
94 | 95 | ||
95 | int (*release) (struct posix_clock *pc); | 96 | int (*release) (struct posix_clock *pc); |
96 | 97 | ||
97 | ssize_t (*read) (struct posix_clock *pc, | 98 | ssize_t (*read) (struct posix_clock *pc, |
98 | uint flags, char __user *buf, size_t cnt); | 99 | uint flags, char __user *buf, size_t cnt); |
99 | }; | 100 | }; |
100 | 101 | ||
101 | /** | 102 | /** |
102 | * struct posix_clock - represents a dynamic posix clock | 103 | * struct posix_clock - represents a dynamic posix clock |
103 | * | 104 | * |
104 | * @ops: Functional interface to the clock | 105 | * @ops: Functional interface to the clock |
105 | * @cdev: Character device instance for this clock | 106 | * @cdev: Character device instance for this clock |
106 | * @kref: Reference count. | 107 | * @kref: Reference count. |
107 | * @mutex: Protects the 'zombie' field from concurrent access. | 108 | * @rwsem: Protects the 'zombie' field from concurrent access. |
108 | * @zombie: If 'zombie' is true, then the hardware has disappeared. | 109 | * @zombie: If 'zombie' is true, then the hardware has disappeared. |
109 | * @release: A function to free the structure when the reference count reaches | 110 | * @release: A function to free the structure when the reference count reaches |
110 | * zero. May be NULL if structure is statically allocated. | 111 | * zero. May be NULL if structure is statically allocated. |
111 | * | 112 | * |
112 | * Drivers should embed their struct posix_clock within a private | 113 | * Drivers should embed their struct posix_clock within a private |
113 | * structure, obtaining a reference to it during callbacks using | 114 | * structure, obtaining a reference to it during callbacks using |
114 | * container_of(). | 115 | * container_of(). |
115 | */ | 116 | */ |
116 | struct posix_clock { | 117 | struct posix_clock { |
117 | struct posix_clock_operations ops; | 118 | struct posix_clock_operations ops; |
118 | struct cdev cdev; | 119 | struct cdev cdev; |
119 | struct kref kref; | 120 | struct kref kref; |
120 | struct mutex mutex; | 121 | struct rw_semaphore rwsem; |
121 | bool zombie; | 122 | bool zombie; |
122 | void (*release)(struct posix_clock *clk); | 123 | void (*release)(struct posix_clock *clk); |
123 | }; | 124 | }; |
124 | 125 | ||
125 | /** | 126 | /** |
126 | * posix_clock_register() - register a new clock | 127 | * posix_clock_register() - register a new clock |
127 | * @clk: Pointer to the clock. Caller must provide 'ops' and 'release' | 128 | * @clk: Pointer to the clock. Caller must provide 'ops' and 'release' |
128 | * @devid: Allocated device id | 129 | * @devid: Allocated device id |
129 | * | 130 | * |
130 | * A clock driver calls this function to register itself with the | 131 | * A clock driver calls this function to register itself with the |
131 | * clock device subsystem. If 'clk' points to dynamically allocated | 132 | * clock device subsystem. If 'clk' points to dynamically allocated |
132 | * memory, then the caller must provide a 'release' function to free | 133 | * memory, then the caller must provide a 'release' function to free |
133 | * that memory. | 134 | * that memory. |
134 | * | 135 | * |
135 | * Returns zero on success, non-zero otherwise. | 136 | * Returns zero on success, non-zero otherwise. |
136 | */ | 137 | */ |
137 | int posix_clock_register(struct posix_clock *clk, dev_t devid); | 138 | int posix_clock_register(struct posix_clock *clk, dev_t devid); |
138 | 139 | ||
139 | /** | 140 | /** |
140 | * posix_clock_unregister() - unregister a clock | 141 | * posix_clock_unregister() - unregister a clock |
141 | * @clk: Clock instance previously registered via posix_clock_register() | 142 | * @clk: Clock instance previously registered via posix_clock_register() |
142 | * | 143 | * |
143 | * A clock driver calls this function to remove itself from the clock | 144 | * A clock driver calls this function to remove itself from the clock |
144 | * device subsystem. The posix_clock itself will remain (in an | 145 | * device subsystem. The posix_clock itself will remain (in an |
145 | * inactive state) until its reference count drops to zero, at which | 146 | * inactive state) until its reference count drops to zero, at which |
146 | * point it will be deallocated with its 'release' method. | 147 | * point it will be deallocated with its 'release' method. |
147 | */ | 148 | */ |
148 | void posix_clock_unregister(struct posix_clock *clk); | 149 | void posix_clock_unregister(struct posix_clock *clk); |
149 | 150 | ||
150 | #endif | 151 | #endif |
151 | 152 |
kernel/time/posix-clock.c
1 | /* | 1 | /* |
2 | * posix-clock.c - support for dynamic clock devices | 2 | * posix-clock.c - support for dynamic clock devices |
3 | * | 3 | * |
4 | * Copyright (C) 2010 OMICRON electronics GmbH | 4 | * Copyright (C) 2010 OMICRON electronics GmbH |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | 7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or | 8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. | 9 | * (at your option) any later version. |
10 | * | 10 | * |
11 | * This program is distributed in the hope that it will be useful, | 11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. | 14 | * GNU General Public License for more details. |
15 | * | 15 | * |
16 | * You should have received a copy of the GNU General Public License | 16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | */ | 19 | */ |
20 | #include <linux/device.h> | 20 | #include <linux/device.h> |
21 | #include <linux/file.h> | 21 | #include <linux/file.h> |
22 | #include <linux/mutex.h> | ||
23 | #include <linux/posix-clock.h> | 22 | #include <linux/posix-clock.h> |
24 | #include <linux/slab.h> | 23 | #include <linux/slab.h> |
25 | #include <linux/syscalls.h> | 24 | #include <linux/syscalls.h> |
26 | #include <linux/uaccess.h> | 25 | #include <linux/uaccess.h> |
27 | 26 | ||
28 | static void delete_clock(struct kref *kref); | 27 | static void delete_clock(struct kref *kref); |
29 | 28 | ||
30 | /* | 29 | /* |
31 | * Returns NULL if the posix_clock instance attached to 'fp' is old and stale. | 30 | * Returns NULL if the posix_clock instance attached to 'fp' is old and stale. |
32 | */ | 31 | */ |
33 | static struct posix_clock *get_posix_clock(struct file *fp) | 32 | static struct posix_clock *get_posix_clock(struct file *fp) |
34 | { | 33 | { |
35 | struct posix_clock *clk = fp->private_data; | 34 | struct posix_clock *clk = fp->private_data; |
36 | 35 | ||
37 | mutex_lock(&clk->mutex); | 36 | down_read(&clk->rwsem); |
38 | 37 | ||
39 | if (!clk->zombie) | 38 | if (!clk->zombie) |
40 | return clk; | 39 | return clk; |
41 | 40 | ||
42 | mutex_unlock(&clk->mutex); | 41 | up_read(&clk->rwsem); |
43 | 42 | ||
44 | return NULL; | 43 | return NULL; |
45 | } | 44 | } |
46 | 45 | ||
47 | static void put_posix_clock(struct posix_clock *clk) | 46 | static void put_posix_clock(struct posix_clock *clk) |
48 | { | 47 | { |
49 | mutex_unlock(&clk->mutex); | 48 | up_read(&clk->rwsem); |
50 | } | 49 | } |
51 | 50 | ||
52 | static ssize_t posix_clock_read(struct file *fp, char __user *buf, | 51 | static ssize_t posix_clock_read(struct file *fp, char __user *buf, |
53 | size_t count, loff_t *ppos) | 52 | size_t count, loff_t *ppos) |
54 | { | 53 | { |
55 | struct posix_clock *clk = get_posix_clock(fp); | 54 | struct posix_clock *clk = get_posix_clock(fp); |
56 | int err = -EINVAL; | 55 | int err = -EINVAL; |
57 | 56 | ||
58 | if (!clk) | 57 | if (!clk) |
59 | return -ENODEV; | 58 | return -ENODEV; |
60 | 59 | ||
61 | if (clk->ops.read) | 60 | if (clk->ops.read) |
62 | err = clk->ops.read(clk, fp->f_flags, buf, count); | 61 | err = clk->ops.read(clk, fp->f_flags, buf, count); |
63 | 62 | ||
64 | put_posix_clock(clk); | 63 | put_posix_clock(clk); |
65 | 64 | ||
66 | return err; | 65 | return err; |
67 | } | 66 | } |
68 | 67 | ||
69 | static unsigned int posix_clock_poll(struct file *fp, poll_table *wait) | 68 | static unsigned int posix_clock_poll(struct file *fp, poll_table *wait) |
70 | { | 69 | { |
71 | struct posix_clock *clk = get_posix_clock(fp); | 70 | struct posix_clock *clk = get_posix_clock(fp); |
72 | int result = 0; | 71 | int result = 0; |
73 | 72 | ||
74 | if (!clk) | 73 | if (!clk) |
75 | return -ENODEV; | 74 | return -ENODEV; |
76 | 75 | ||
77 | if (clk->ops.poll) | 76 | if (clk->ops.poll) |
78 | result = clk->ops.poll(clk, fp, wait); | 77 | result = clk->ops.poll(clk, fp, wait); |
79 | 78 | ||
80 | put_posix_clock(clk); | 79 | put_posix_clock(clk); |
81 | 80 | ||
82 | return result; | 81 | return result; |
83 | } | 82 | } |
84 | 83 | ||
85 | static int posix_clock_fasync(int fd, struct file *fp, int on) | 84 | static int posix_clock_fasync(int fd, struct file *fp, int on) |
86 | { | 85 | { |
87 | struct posix_clock *clk = get_posix_clock(fp); | 86 | struct posix_clock *clk = get_posix_clock(fp); |
88 | int err = 0; | 87 | int err = 0; |
89 | 88 | ||
90 | if (!clk) | 89 | if (!clk) |
91 | return -ENODEV; | 90 | return -ENODEV; |
92 | 91 | ||
93 | if (clk->ops.fasync) | 92 | if (clk->ops.fasync) |
94 | err = clk->ops.fasync(clk, fd, fp, on); | 93 | err = clk->ops.fasync(clk, fd, fp, on); |
95 | 94 | ||
96 | put_posix_clock(clk); | 95 | put_posix_clock(clk); |
97 | 96 | ||
98 | return err; | 97 | return err; |
99 | } | 98 | } |
100 | 99 | ||
101 | static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma) | 100 | static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma) |
102 | { | 101 | { |
103 | struct posix_clock *clk = get_posix_clock(fp); | 102 | struct posix_clock *clk = get_posix_clock(fp); |
104 | int err = -ENODEV; | 103 | int err = -ENODEV; |
105 | 104 | ||
106 | if (!clk) | 105 | if (!clk) |
107 | return -ENODEV; | 106 | return -ENODEV; |
108 | 107 | ||
109 | if (clk->ops.mmap) | 108 | if (clk->ops.mmap) |
110 | err = clk->ops.mmap(clk, vma); | 109 | err = clk->ops.mmap(clk, vma); |
111 | 110 | ||
112 | put_posix_clock(clk); | 111 | put_posix_clock(clk); |
113 | 112 | ||
114 | return err; | 113 | return err; |
115 | } | 114 | } |
116 | 115 | ||
117 | static long posix_clock_ioctl(struct file *fp, | 116 | static long posix_clock_ioctl(struct file *fp, |
118 | unsigned int cmd, unsigned long arg) | 117 | unsigned int cmd, unsigned long arg) |
119 | { | 118 | { |
120 | struct posix_clock *clk = get_posix_clock(fp); | 119 | struct posix_clock *clk = get_posix_clock(fp); |
121 | int err = -ENOTTY; | 120 | int err = -ENOTTY; |
122 | 121 | ||
123 | if (!clk) | 122 | if (!clk) |
124 | return -ENODEV; | 123 | return -ENODEV; |
125 | 124 | ||
126 | if (clk->ops.ioctl) | 125 | if (clk->ops.ioctl) |
127 | err = clk->ops.ioctl(clk, cmd, arg); | 126 | err = clk->ops.ioctl(clk, cmd, arg); |
128 | 127 | ||
129 | put_posix_clock(clk); | 128 | put_posix_clock(clk); |
130 | 129 | ||
131 | return err; | 130 | return err; |
132 | } | 131 | } |
133 | 132 | ||
134 | #ifdef CONFIG_COMPAT | 133 | #ifdef CONFIG_COMPAT |
135 | static long posix_clock_compat_ioctl(struct file *fp, | 134 | static long posix_clock_compat_ioctl(struct file *fp, |
136 | unsigned int cmd, unsigned long arg) | 135 | unsigned int cmd, unsigned long arg) |
137 | { | 136 | { |
138 | struct posix_clock *clk = get_posix_clock(fp); | 137 | struct posix_clock *clk = get_posix_clock(fp); |
139 | int err = -ENOTTY; | 138 | int err = -ENOTTY; |
140 | 139 | ||
141 | if (!clk) | 140 | if (!clk) |
142 | return -ENODEV; | 141 | return -ENODEV; |
143 | 142 | ||
144 | if (clk->ops.ioctl) | 143 | if (clk->ops.ioctl) |
145 | err = clk->ops.ioctl(clk, cmd, arg); | 144 | err = clk->ops.ioctl(clk, cmd, arg); |
146 | 145 | ||
147 | put_posix_clock(clk); | 146 | put_posix_clock(clk); |
148 | 147 | ||
149 | return err; | 148 | return err; |
150 | } | 149 | } |
151 | #endif | 150 | #endif |
152 | 151 | ||
153 | static int posix_clock_open(struct inode *inode, struct file *fp) | 152 | static int posix_clock_open(struct inode *inode, struct file *fp) |
154 | { | 153 | { |
155 | int err; | 154 | int err; |
156 | struct posix_clock *clk = | 155 | struct posix_clock *clk = |
157 | container_of(inode->i_cdev, struct posix_clock, cdev); | 156 | container_of(inode->i_cdev, struct posix_clock, cdev); |
158 | 157 | ||
159 | mutex_lock(&clk->mutex); | 158 | down_read(&clk->rwsem); |
160 | 159 | ||
161 | if (clk->zombie) { | 160 | if (clk->zombie) { |
162 | err = -ENODEV; | 161 | err = -ENODEV; |
163 | goto out; | 162 | goto out; |
164 | } | 163 | } |
165 | if (clk->ops.open) | 164 | if (clk->ops.open) |
166 | err = clk->ops.open(clk, fp->f_mode); | 165 | err = clk->ops.open(clk, fp->f_mode); |
167 | else | 166 | else |
168 | err = 0; | 167 | err = 0; |
169 | 168 | ||
170 | if (!err) { | 169 | if (!err) { |
171 | kref_get(&clk->kref); | 170 | kref_get(&clk->kref); |
172 | fp->private_data = clk; | 171 | fp->private_data = clk; |
173 | } | 172 | } |
174 | out: | 173 | out: |
175 | mutex_unlock(&clk->mutex); | 174 | up_read(&clk->rwsem); |
176 | return err; | 175 | return err; |
177 | } | 176 | } |
178 | 177 | ||
179 | static int posix_clock_release(struct inode *inode, struct file *fp) | 178 | static int posix_clock_release(struct inode *inode, struct file *fp) |
180 | { | 179 | { |
181 | struct posix_clock *clk = fp->private_data; | 180 | struct posix_clock *clk = fp->private_data; |
182 | int err = 0; | 181 | int err = 0; |
183 | 182 | ||
184 | if (clk->ops.release) | 183 | if (clk->ops.release) |
185 | err = clk->ops.release(clk); | 184 | err = clk->ops.release(clk); |
186 | 185 | ||
187 | kref_put(&clk->kref, delete_clock); | 186 | kref_put(&clk->kref, delete_clock); |
188 | 187 | ||
189 | fp->private_data = NULL; | 188 | fp->private_data = NULL; |
190 | 189 | ||
191 | return err; | 190 | return err; |
192 | } | 191 | } |
193 | 192 | ||
194 | static const struct file_operations posix_clock_file_operations = { | 193 | static const struct file_operations posix_clock_file_operations = { |
195 | .owner = THIS_MODULE, | 194 | .owner = THIS_MODULE, |
196 | .llseek = no_llseek, | 195 | .llseek = no_llseek, |
197 | .read = posix_clock_read, | 196 | .read = posix_clock_read, |
198 | .poll = posix_clock_poll, | 197 | .poll = posix_clock_poll, |
199 | .unlocked_ioctl = posix_clock_ioctl, | 198 | .unlocked_ioctl = posix_clock_ioctl, |
200 | .open = posix_clock_open, | 199 | .open = posix_clock_open, |
201 | .release = posix_clock_release, | 200 | .release = posix_clock_release, |
202 | .fasync = posix_clock_fasync, | 201 | .fasync = posix_clock_fasync, |
203 | .mmap = posix_clock_mmap, | 202 | .mmap = posix_clock_mmap, |
204 | #ifdef CONFIG_COMPAT | 203 | #ifdef CONFIG_COMPAT |
205 | .compat_ioctl = posix_clock_compat_ioctl, | 204 | .compat_ioctl = posix_clock_compat_ioctl, |
206 | #endif | 205 | #endif |
207 | }; | 206 | }; |
208 | 207 | ||
209 | int posix_clock_register(struct posix_clock *clk, dev_t devid) | 208 | int posix_clock_register(struct posix_clock *clk, dev_t devid) |
210 | { | 209 | { |
211 | int err; | 210 | int err; |
212 | 211 | ||
213 | kref_init(&clk->kref); | 212 | kref_init(&clk->kref); |
214 | mutex_init(&clk->mutex); | 213 | init_rwsem(&clk->rwsem); |
215 | 214 | ||
216 | cdev_init(&clk->cdev, &posix_clock_file_operations); | 215 | cdev_init(&clk->cdev, &posix_clock_file_operations); |
217 | clk->cdev.owner = clk->ops.owner; | 216 | clk->cdev.owner = clk->ops.owner; |
218 | err = cdev_add(&clk->cdev, devid, 1); | 217 | err = cdev_add(&clk->cdev, devid, 1); |
219 | if (err) | ||
220 | goto no_cdev; | ||
221 | 218 | ||
222 | return err; | 219 | return err; |
223 | no_cdev: | ||
224 | mutex_destroy(&clk->mutex); | ||
225 | return err; | ||
226 | } | 220 | } |
227 | EXPORT_SYMBOL_GPL(posix_clock_register); | 221 | EXPORT_SYMBOL_GPL(posix_clock_register); |
228 | 222 | ||
229 | static void delete_clock(struct kref *kref) | 223 | static void delete_clock(struct kref *kref) |
230 | { | 224 | { |
231 | struct posix_clock *clk = container_of(kref, struct posix_clock, kref); | 225 | struct posix_clock *clk = container_of(kref, struct posix_clock, kref); |
232 | mutex_destroy(&clk->mutex); | 226 | |
233 | if (clk->release) | 227 | if (clk->release) |
234 | clk->release(clk); | 228 | clk->release(clk); |
235 | } | 229 | } |
236 | 230 | ||
237 | void posix_clock_unregister(struct posix_clock *clk) | 231 | void posix_clock_unregister(struct posix_clock *clk) |
238 | { | 232 | { |
239 | cdev_del(&clk->cdev); | 233 | cdev_del(&clk->cdev); |
240 | 234 | ||
241 | mutex_lock(&clk->mutex); | 235 | down_write(&clk->rwsem); |
242 | clk->zombie = true; | 236 | clk->zombie = true; |
243 | mutex_unlock(&clk->mutex); | 237 | up_write(&clk->rwsem); |
244 | 238 | ||
245 | kref_put(&clk->kref, delete_clock); | 239 | kref_put(&clk->kref, delete_clock); |
246 | } | 240 | } |
247 | EXPORT_SYMBOL_GPL(posix_clock_unregister); | 241 | EXPORT_SYMBOL_GPL(posix_clock_unregister); |
248 | 242 | ||
249 | struct posix_clock_desc { | 243 | struct posix_clock_desc { |
250 | struct file *fp; | 244 | struct file *fp; |
251 | struct posix_clock *clk; | 245 | struct posix_clock *clk; |
252 | }; | 246 | }; |
253 | 247 | ||
254 | static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd) | 248 | static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd) |
255 | { | 249 | { |
256 | struct file *fp = fget(CLOCKID_TO_FD(id)); | 250 | struct file *fp = fget(CLOCKID_TO_FD(id)); |
257 | int err = -EINVAL; | 251 | int err = -EINVAL; |
258 | 252 | ||
259 | if (!fp) | 253 | if (!fp) |
260 | return err; | 254 | return err; |
261 | 255 | ||
262 | if (fp->f_op->open != posix_clock_open || !fp->private_data) | 256 | if (fp->f_op->open != posix_clock_open || !fp->private_data) |
263 | goto out; | 257 | goto out; |
264 | 258 | ||
265 | cd->fp = fp; | 259 | cd->fp = fp; |
266 | cd->clk = get_posix_clock(fp); | 260 | cd->clk = get_posix_clock(fp); |
267 | 261 | ||
268 | err = cd->clk ? 0 : -ENODEV; | 262 | err = cd->clk ? 0 : -ENODEV; |
269 | out: | 263 | out: |
270 | if (err) | 264 | if (err) |
271 | fput(fp); | 265 | fput(fp); |
272 | return err; | 266 | return err; |
273 | } | 267 | } |
274 | 268 | ||
275 | static void put_clock_desc(struct posix_clock_desc *cd) | 269 | static void put_clock_desc(struct posix_clock_desc *cd) |
276 | { | 270 | { |
277 | put_posix_clock(cd->clk); | 271 | put_posix_clock(cd->clk); |
278 | fput(cd->fp); | 272 | fput(cd->fp); |
279 | } | 273 | } |
280 | 274 | ||
281 | static int pc_clock_adjtime(clockid_t id, struct timex *tx) | 275 | static int pc_clock_adjtime(clockid_t id, struct timex *tx) |
282 | { | 276 | { |
283 | struct posix_clock_desc cd; | 277 | struct posix_clock_desc cd; |
284 | int err; | 278 | int err; |
285 | 279 | ||
286 | err = get_clock_desc(id, &cd); | 280 | err = get_clock_desc(id, &cd); |
287 | if (err) | 281 | if (err) |
288 | return err; | 282 | return err; |
289 | 283 | ||
290 | if ((cd.fp->f_mode & FMODE_WRITE) == 0) { | 284 | if ((cd.fp->f_mode & FMODE_WRITE) == 0) { |
291 | err = -EACCES; | 285 | err = -EACCES; |
292 | goto out; | 286 | goto out; |
293 | } | 287 | } |
294 | 288 | ||
295 | if (cd.clk->ops.clock_adjtime) | 289 | if (cd.clk->ops.clock_adjtime) |
296 | err = cd.clk->ops.clock_adjtime(cd.clk, tx); | 290 | err = cd.clk->ops.clock_adjtime(cd.clk, tx); |
297 | else | 291 | else |
298 | err = -EOPNOTSUPP; | 292 | err = -EOPNOTSUPP; |
299 | out: | 293 | out: |
300 | put_clock_desc(&cd); | 294 | put_clock_desc(&cd); |
301 | 295 | ||
302 | return err; | 296 | return err; |
303 | } | 297 | } |
304 | 298 | ||
305 | static int pc_clock_gettime(clockid_t id, struct timespec *ts) | 299 | static int pc_clock_gettime(clockid_t id, struct timespec *ts) |
306 | { | 300 | { |
307 | struct posix_clock_desc cd; | 301 | struct posix_clock_desc cd; |
308 | int err; | 302 | int err; |
309 | 303 | ||
310 | err = get_clock_desc(id, &cd); | 304 | err = get_clock_desc(id, &cd); |
311 | if (err) | 305 | if (err) |
312 | return err; | 306 | return err; |
313 | 307 | ||
314 | if (cd.clk->ops.clock_gettime) | 308 | if (cd.clk->ops.clock_gettime) |
315 | err = cd.clk->ops.clock_gettime(cd.clk, ts); | 309 | err = cd.clk->ops.clock_gettime(cd.clk, ts); |
316 | else | 310 | else |
317 | err = -EOPNOTSUPP; | 311 | err = -EOPNOTSUPP; |
318 | 312 | ||
319 | put_clock_desc(&cd); | 313 | put_clock_desc(&cd); |
320 | 314 | ||
321 | return err; | 315 | return err; |
322 | } | 316 | } |
323 | 317 | ||
324 | static int pc_clock_getres(clockid_t id, struct timespec *ts) | 318 | static int pc_clock_getres(clockid_t id, struct timespec *ts) |
325 | { | 319 | { |
326 | struct posix_clock_desc cd; | 320 | struct posix_clock_desc cd; |
327 | int err; | 321 | int err; |
328 | 322 | ||
329 | err = get_clock_desc(id, &cd); | 323 | err = get_clock_desc(id, &cd); |
330 | if (err) | 324 | if (err) |
331 | return err; | 325 | return err; |
332 | 326 | ||
333 | if (cd.clk->ops.clock_getres) | 327 | if (cd.clk->ops.clock_getres) |
334 | err = cd.clk->ops.clock_getres(cd.clk, ts); | 328 | err = cd.clk->ops.clock_getres(cd.clk, ts); |
335 | else | 329 | else |
336 | err = -EOPNOTSUPP; | 330 | err = -EOPNOTSUPP; |
337 | 331 | ||
338 | put_clock_desc(&cd); | 332 | put_clock_desc(&cd); |
339 | 333 | ||
340 | return err; | 334 | return err; |
341 | } | 335 | } |
342 | 336 | ||
343 | static int pc_clock_settime(clockid_t id, const struct timespec *ts) | 337 | static int pc_clock_settime(clockid_t id, const struct timespec *ts) |
344 | { | 338 | { |
345 | struct posix_clock_desc cd; | 339 | struct posix_clock_desc cd; |
346 | int err; | 340 | int err; |
347 | 341 | ||
348 | err = get_clock_desc(id, &cd); | 342 | err = get_clock_desc(id, &cd); |
349 | if (err) | 343 | if (err) |
350 | return err; | 344 | return err; |
351 | 345 | ||
352 | if ((cd.fp->f_mode & FMODE_WRITE) == 0) { | 346 | if ((cd.fp->f_mode & FMODE_WRITE) == 0) { |
353 | err = -EACCES; | 347 | err = -EACCES; |
354 | goto out; | 348 | goto out; |
355 | } | 349 | } |
356 | 350 | ||
357 | if (cd.clk->ops.clock_settime) | 351 | if (cd.clk->ops.clock_settime) |
358 | err = cd.clk->ops.clock_settime(cd.clk, ts); | 352 | err = cd.clk->ops.clock_settime(cd.clk, ts); |
359 | else | 353 | else |
360 | err = -EOPNOTSUPP; | 354 | err = -EOPNOTSUPP; |
361 | out: | 355 | out: |
362 | put_clock_desc(&cd); | 356 | put_clock_desc(&cd); |
363 | 357 | ||
364 | return err; | 358 | return err; |
365 | } | 359 | } |
366 | 360 | ||
367 | static int pc_timer_create(struct k_itimer *kit) | 361 | static int pc_timer_create(struct k_itimer *kit) |
368 | { | 362 | { |
369 | clockid_t id = kit->it_clock; | 363 | clockid_t id = kit->it_clock; |
370 | struct posix_clock_desc cd; | 364 | struct posix_clock_desc cd; |
371 | int err; | 365 | int err; |
372 | 366 | ||
373 | err = get_clock_desc(id, &cd); | 367 | err = get_clock_desc(id, &cd); |
374 | if (err) | 368 | if (err) |
375 | return err; | 369 | return err; |
376 | 370 | ||
377 | if (cd.clk->ops.timer_create) | 371 | if (cd.clk->ops.timer_create) |
378 | err = cd.clk->ops.timer_create(cd.clk, kit); | 372 | err = cd.clk->ops.timer_create(cd.clk, kit); |
379 | else | 373 | else |
380 | err = -EOPNOTSUPP; | 374 | err = -EOPNOTSUPP; |
381 | 375 | ||
382 | put_clock_desc(&cd); | 376 | put_clock_desc(&cd); |
383 | 377 | ||
384 | return err; | 378 | return err; |
385 | } | 379 | } |
386 | 380 | ||
387 | static int pc_timer_delete(struct k_itimer *kit) | 381 | static int pc_timer_delete(struct k_itimer *kit) |
388 | { | 382 | { |
389 | clockid_t id = kit->it_clock; | 383 | clockid_t id = kit->it_clock; |
390 | struct posix_clock_desc cd; | 384 | struct posix_clock_desc cd; |
391 | int err; | 385 | int err; |
392 | 386 | ||
393 | err = get_clock_desc(id, &cd); | 387 | err = get_clock_desc(id, &cd); |
394 | if (err) | 388 | if (err) |
395 | return err; | 389 | return err; |
396 | 390 | ||
397 | if (cd.clk->ops.timer_delete) | 391 | if (cd.clk->ops.timer_delete) |
398 | err = cd.clk->ops.timer_delete(cd.clk, kit); | 392 | err = cd.clk->ops.timer_delete(cd.clk, kit); |
399 | else | 393 | else |
400 | err = -EOPNOTSUPP; | 394 | err = -EOPNOTSUPP; |
401 | 395 | ||
402 | put_clock_desc(&cd); | 396 | put_clock_desc(&cd); |
403 | 397 | ||
404 | return err; | 398 | return err; |
405 | } | 399 | } |
406 | 400 | ||
407 | static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts) | 401 | static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts) |
408 | { | 402 | { |
409 | clockid_t id = kit->it_clock; | 403 | clockid_t id = kit->it_clock; |
410 | struct posix_clock_desc cd; | 404 | struct posix_clock_desc cd; |
411 | 405 | ||
412 | if (get_clock_desc(id, &cd)) | 406 | if (get_clock_desc(id, &cd)) |
413 | return; | 407 | return; |
414 | 408 | ||
415 | if (cd.clk->ops.timer_gettime) | 409 | if (cd.clk->ops.timer_gettime) |
416 | cd.clk->ops.timer_gettime(cd.clk, kit, ts); | 410 | cd.clk->ops.timer_gettime(cd.clk, kit, ts); |
417 | 411 | ||
418 | put_clock_desc(&cd); | 412 | put_clock_desc(&cd); |
419 | } | 413 | } |
420 | 414 | ||
421 | static int pc_timer_settime(struct k_itimer *kit, int flags, | 415 | static int pc_timer_settime(struct k_itimer *kit, int flags, |
422 | struct itimerspec *ts, struct itimerspec *old) | 416 | struct itimerspec *ts, struct itimerspec *old) |
423 | { | 417 | { |
424 | clockid_t id = kit->it_clock; | 418 | clockid_t id = kit->it_clock; |
425 | struct posix_clock_desc cd; | 419 | struct posix_clock_desc cd; |
426 | int err; | 420 | int err; |
427 | 421 | ||
428 | err = get_clock_desc(id, &cd); | 422 | err = get_clock_desc(id, &cd); |
429 | if (err) | 423 | if (err) |
430 | return err; | 424 | return err; |
431 | 425 | ||
432 | if (cd.clk->ops.timer_settime) | 426 | if (cd.clk->ops.timer_settime) |
433 | err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old); | 427 | err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old); |
434 | else | 428 | else |
435 | err = -EOPNOTSUPP; | 429 | err = -EOPNOTSUPP; |
436 | 430 | ||
437 | put_clock_desc(&cd); | 431 | put_clock_desc(&cd); |
438 | 432 | ||
439 | return err; | 433 | return err; |
440 | } | 434 | } |
441 | 435 | ||
442 | struct k_clock clock_posix_dynamic = { | 436 | struct k_clock clock_posix_dynamic = { |
443 | .clock_getres = pc_clock_getres, | 437 | .clock_getres = pc_clock_getres, |
444 | .clock_set = pc_clock_settime, | 438 | .clock_set = pc_clock_settime, |
445 | .clock_get = pc_clock_gettime, | 439 | .clock_get = pc_clock_gettime, |
446 | .clock_adj = pc_clock_adjtime, | 440 | .clock_adj = pc_clock_adjtime, |
447 | .timer_create = pc_timer_create, | 441 | .timer_create = pc_timer_create, |
448 | .timer_set = pc_timer_settime, | 442 | .timer_set = pc_timer_settime, |
449 | .timer_del = pc_timer_delete, | 443 | .timer_del = pc_timer_delete, |
450 | .timer_get = pc_timer_gettime, | 444 | .timer_get = pc_timer_gettime, |
451 | }; | 445 | }; |
452 | 446 |