Blame view

drivers/rtc/rtc-dev.c 12.2 KB
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * RTC subsystem, dev interface
   *
   * Copyright (C) 2005 Tower Technologies
   * Author: Alessandro Zummo <a.zummo@towertech.it>
   *
   * based on arch/arm/common/rtctime.c
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
  */
  
  #include <linux/module.h>
  #include <linux/rtc.h>
5726fb201   David Brownell   rtc: remove /sys/...
16
  #include "rtc-core.h"
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
17

e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
18
19
20
21
22
23
24
25
26
  static dev_t rtc_devt;
  
  #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
  
  static int rtc_dev_open(struct inode *inode, struct file *file)
  {
  	int err;
  	struct rtc_device *rtc = container_of(inode->i_cdev,
  					struct rtc_device, char_dev);
ff8371ac9   David Brownell   [PATCH] constify ...
27
  	const struct rtc_class_ops *ops = rtc->ops;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
28

b1c3c8982   David Brownell   revert "rtc: cdev...
29
30
  	if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  		return -EBUSY;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
31

ab6a2d70d   David Brownell   rtc: rtc interfac...
32
  	file->private_data = rtc;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
33

cd9662094   David Brownell   rtc: remove rest ...
34
  	err = ops->open ? ops->open(rtc->dev.parent) : 0;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
35
36
37
38
  	if (err == 0) {
  		spin_lock_irq(&rtc->irq_lock);
  		rtc->irq_data = 0;
  		spin_unlock_irq(&rtc->irq_lock);
b1c3c8982   David Brownell   revert "rtc: cdev...
39
  		return 0;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
40
  	}
8853c202b   Jiri Kosina   RTC: convert mute...
41
  	/* something has gone wrong */
372a302e9   Jiri Kosina   RTC: assure prope...
42
  	clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
43
44
  	return err;
  }
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
45
46
47
48
49
  #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  /*
   * Routine to poll RTC seconds field for change as often as possible,
   * after first RTC_UIE use timer to reduce polling
   */
c4028958b   David Howells   WorkStruct: make ...
50
  static void rtc_uie_task(struct work_struct *work)
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
51
  {
c4028958b   David Howells   WorkStruct: make ...
52
53
  	struct rtc_device *rtc =
  		container_of(work, struct rtc_device, uie_task);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
54
55
56
  	struct rtc_time tm;
  	int num = 0;
  	int err;
ab6a2d70d   David Brownell   rtc: rtc interfac...
57
  	err = rtc_read_time(rtc, &tm);
d728b1e69   David Brownell   [PATCH] rtc class...
58
59
60
  
  	local_irq_disable();
  	spin_lock(&rtc->irq_lock);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
61
62
63
64
65
66
67
68
69
70
71
72
  	if (rtc->stop_uie_polling || err) {
  		rtc->uie_task_active = 0;
  	} else if (rtc->oldsecs != tm.tm_sec) {
  		num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
  		rtc->oldsecs = tm.tm_sec;
  		rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
  		rtc->uie_timer_active = 1;
  		rtc->uie_task_active = 0;
  		add_timer(&rtc->uie_timer);
  	} else if (schedule_work(&rtc->uie_task) == 0) {
  		rtc->uie_task_active = 0;
  	}
d728b1e69   David Brownell   [PATCH] rtc class...
73
  	spin_unlock(&rtc->irq_lock);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
74
  	if (num)
ab6a2d70d   David Brownell   rtc: rtc interfac...
75
  		rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
d728b1e69   David Brownell   [PATCH] rtc class...
76
  	local_irq_enable();
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
77
  }
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
78
79
80
81
82
83
84
85
86
87
88
89
  static void rtc_uie_timer(unsigned long data)
  {
  	struct rtc_device *rtc = (struct rtc_device *)data;
  	unsigned long flags;
  
  	spin_lock_irqsave(&rtc->irq_lock, flags);
  	rtc->uie_timer_active = 0;
  	rtc->uie_task_active = 1;
  	if ((schedule_work(&rtc->uie_task) == 0))
  		rtc->uie_task_active = 0;
  	spin_unlock_irqrestore(&rtc->irq_lock, flags);
  }
099e65762   Alessandro Zummo   rtc: add alarm/up...
90
  static int clear_uie(struct rtc_device *rtc)
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
91
92
  {
  	spin_lock_irq(&rtc->irq_lock);
099e65762   Alessandro Zummo   rtc: add alarm/up...
93
  	if (rtc->uie_irq_active) {
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
94
95
96
97
98
99
100
101
102
103
104
105
  		rtc->stop_uie_polling = 1;
  		if (rtc->uie_timer_active) {
  			spin_unlock_irq(&rtc->irq_lock);
  			del_timer_sync(&rtc->uie_timer);
  			spin_lock_irq(&rtc->irq_lock);
  			rtc->uie_timer_active = 0;
  		}
  		if (rtc->uie_task_active) {
  			spin_unlock_irq(&rtc->irq_lock);
  			flush_scheduled_work();
  			spin_lock_irq(&rtc->irq_lock);
  		}
099e65762   Alessandro Zummo   rtc: add alarm/up...
106
  		rtc->uie_irq_active = 0;
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
107
108
  	}
  	spin_unlock_irq(&rtc->irq_lock);
099e65762   Alessandro Zummo   rtc: add alarm/up...
109
  	return 0;
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
110
111
112
113
114
115
  }
  
  static int set_uie(struct rtc_device *rtc)
  {
  	struct rtc_time tm;
  	int err;
ab6a2d70d   David Brownell   rtc: rtc interfac...
116
  	err = rtc_read_time(rtc, &tm);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
117
118
119
  	if (err)
  		return err;
  	spin_lock_irq(&rtc->irq_lock);
099e65762   Alessandro Zummo   rtc: add alarm/up...
120
121
  	if (!rtc->uie_irq_active) {
  		rtc->uie_irq_active = 1;
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
122
123
124
125
126
127
128
129
130
131
  		rtc->stop_uie_polling = 0;
  		rtc->oldsecs = tm.tm_sec;
  		rtc->uie_task_active = 1;
  		if (schedule_work(&rtc->uie_task) == 0)
  			rtc->uie_task_active = 0;
  	}
  	rtc->irq_data = 0;
  	spin_unlock_irq(&rtc->irq_lock);
  	return 0;
  }
099e65762   Alessandro Zummo   rtc: add alarm/up...
132
133
134
135
136
137
138
139
140
  
  int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
  {
  	if (enabled)
  		return set_uie(rtc);
  	else
  		return clear_uie(rtc);
  }
  EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
141
  #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
142
143
144
145
  
  static ssize_t
  rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  {
88efe1373   Mark Zhan   rtc-dev: no need ...
146
  	struct rtc_device *rtc = file->private_data;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
147
148
149
150
  
  	DECLARE_WAITQUEUE(wait, current);
  	unsigned long data;
  	ssize_t ret;
3418ff761   Atsushi Nemoto   [PATCH] RTC: rtc-...
151
  	if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  		return -EINVAL;
  
  	add_wait_queue(&rtc->irq_queue, &wait);
  	do {
  		__set_current_state(TASK_INTERRUPTIBLE);
  
  		spin_lock_irq(&rtc->irq_lock);
  		data = rtc->irq_data;
  		rtc->irq_data = 0;
  		spin_unlock_irq(&rtc->irq_lock);
  
  		if (data != 0) {
  			ret = 0;
  			break;
  		}
  		if (file->f_flags & O_NONBLOCK) {
  			ret = -EAGAIN;
  			break;
  		}
  		if (signal_pending(current)) {
  			ret = -ERESTARTSYS;
  			break;
  		}
  		schedule();
  	} while (1);
  	set_current_state(TASK_RUNNING);
  	remove_wait_queue(&rtc->irq_queue, &wait);
  
  	if (ret == 0) {
  		/* Check for any data updates */
  		if (rtc->ops->read_callback)
cd9662094   David Brownell   rtc: remove rest ...
183
  			data = rtc->ops->read_callback(rtc->dev.parent,
3418ff761   Atsushi Nemoto   [PATCH] RTC: rtc-...
184
185
186
187
188
189
190
191
192
  						       data);
  
  		if (sizeof(int) != sizeof(long) &&
  		    count == sizeof(unsigned int))
  			ret = put_user(data, (unsigned int __user *)buf) ?:
  				sizeof(unsigned int);
  		else
  			ret = put_user(data, (unsigned long __user *)buf) ?:
  				sizeof(unsigned long);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
193
194
195
196
197
198
  	}
  	return ret;
  }
  
  static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
  {
88efe1373   Mark Zhan   rtc-dev: no need ...
199
  	struct rtc_device *rtc = file->private_data;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
200
201
202
203
204
205
206
207
  	unsigned long data;
  
  	poll_wait(file, &rtc->irq_queue, wait);
  
  	data = rtc->irq_data;
  
  	return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
  }
5ad31a575   David Brownell   rtc: remove BKL f...
208
  static long rtc_dev_ioctl(struct file *file,
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
209
210
211
  		unsigned int cmd, unsigned long arg)
  {
  	int err = 0;
ab6a2d70d   David Brownell   rtc: rtc interfac...
212
  	struct rtc_device *rtc = file->private_data;
ff8371ac9   David Brownell   [PATCH] constify ...
213
  	const struct rtc_class_ops *ops = rtc->ops;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
214
215
216
  	struct rtc_time tm;
  	struct rtc_wkalrm alarm;
  	void __user *uarg = (void __user *) arg;
5ad31a575   David Brownell   rtc: remove BKL f...
217
218
  	err = mutex_lock_interruptible(&rtc->ops_lock);
  	if (err)
b68bb2632   David Brownell   rtc: don't return...
219
  		return err;
5ad31a575   David Brownell   rtc: remove BKL f...
220

2601a4647   David Brownell   [PATCH] rtc frame...
221
  	/* check that the calling task has appropriate permissions
110d693d5   Alessandro Zummo   [PATCH] rtc subsy...
222
223
224
225
226
227
228
  	 * for certain ioctls. doing this check here is useful
  	 * to avoid duplicate code in each driver.
  	 */
  	switch (cmd) {
  	case RTC_EPOCH_SET:
  	case RTC_SET_TIME:
  		if (!capable(CAP_SYS_TIME))
5ad31a575   David Brownell   rtc: remove BKL f...
229
  			err = -EACCES;
110d693d5   Alessandro Zummo   [PATCH] rtc subsy...
230
231
232
233
  		break;
  
  	case RTC_IRQP_SET:
  		if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
5ad31a575   David Brownell   rtc: remove BKL f...
234
  			err = -EACCES;
110d693d5   Alessandro Zummo   [PATCH] rtc subsy...
235
236
237
  		break;
  
  	case RTC_PIE_ON:
9d013d3b1   Bryan Kadzban   rtc: allow valida...
238
239
  		if (rtc->irq_freq > rtc->max_user_freq &&
  				!capable(CAP_SYS_RESOURCE))
5ad31a575   David Brownell   rtc: remove BKL f...
240
  			err = -EACCES;
110d693d5   Alessandro Zummo   [PATCH] rtc subsy...
241
242
  		break;
  	}
5ad31a575   David Brownell   rtc: remove BKL f...
243
244
  	if (err)
  		goto done;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
245
246
  	/* try the driver's ioctl interface */
  	if (ops->ioctl) {
cd9662094   David Brownell   rtc: remove rest ...
247
  		err = ops->ioctl(rtc->dev.parent, cmd, arg);
5ad31a575   David Brownell   rtc: remove BKL f...
248
249
  		if (err != -ENOIOCTLCMD) {
  			mutex_unlock(&rtc->ops_lock);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
250
  			return err;
5ad31a575   David Brownell   rtc: remove BKL f...
251
  		}
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
252
253
254
255
  	}
  
  	/* if the driver does not provide the ioctl interface
  	 * or if that particular ioctl was not implemented
b3969e583   Alessandro Zummo   [PATCH] rtc subsy...
256
  	 * (-ENOIOCTLCMD), we will try to emulate here.
8a0bdfd7a   David Brownell   rtc-cmos alarm ac...
257
258
259
260
261
262
263
264
265
  	 *
  	 * Drivers *SHOULD NOT* provide ioctl implementations
  	 * for these requests.  Instead, provide methods to
  	 * support the following code, so that the RTC's main
  	 * features are accessible without using ioctls.
  	 *
  	 * RTC and alarm times will be in UTC, by preference,
  	 * but dual-booting with MS-Windows implies RTCs must
  	 * use the local wall clock time.
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
266
267
268
269
  	 */
  
  	switch (cmd) {
  	case RTC_ALM_READ:
5ad31a575   David Brownell   rtc: remove BKL f...
270
  		mutex_unlock(&rtc->ops_lock);
ab6a2d70d   David Brownell   rtc: rtc interfac...
271
  		err = rtc_read_alarm(rtc, &alarm);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
272
273
274
275
  		if (err < 0)
  			return err;
  
  		if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
5ad31a575   David Brownell   rtc: remove BKL f...
276
277
  			err = -EFAULT;
  		return err;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
278
279
  
  	case RTC_ALM_SET:
5ad31a575   David Brownell   rtc: remove BKL f...
280
  		mutex_unlock(&rtc->ops_lock);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
281
282
283
284
285
  		if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  			return -EFAULT;
  
  		alarm.enabled = 0;
  		alarm.pending = 0;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
286
287
288
  		alarm.time.tm_wday = -1;
  		alarm.time.tm_yday = -1;
  		alarm.time.tm_isdst = -1;
f8245c268   David Brownell   rtc: remove "RTC_...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  
  		/* RTC_ALM_SET alarms may be up to 24 hours in the future.
  		 * Rather than expecting every RTC to implement "don't care"
  		 * for day/month/year fields, just force the alarm to have
  		 * the right values for those fields.
  		 *
  		 * RTC_WKALM_SET should be used instead.  Not only does it
  		 * eliminate the need for a separate RTC_AIE_ON call, it
  		 * doesn't have the "alarm 23:59:59 in the future" race.
  		 *
  		 * NOTE:  some legacy code may have used invalid fields as
  		 * wildcards, exposing hardware "periodic alarm" capabilities.
  		 * Not supported here.
  		 */
  		{
  			unsigned long now, then;
  
  			err = rtc_read_time(rtc, &tm);
  			if (err < 0)
  				return err;
  			rtc_tm_to_time(&tm, &now);
  
  			alarm.time.tm_mday = tm.tm_mday;
  			alarm.time.tm_mon = tm.tm_mon;
  			alarm.time.tm_year = tm.tm_year;
  			err  = rtc_valid_tm(&alarm.time);
  			if (err < 0)
  				return err;
  			rtc_tm_to_time(&alarm.time, &then);
  
  			/* alarm may need to wrap into tomorrow */
  			if (then < now) {
  				rtc_time_to_tm(now + 24 * 60 * 60, &tm);
  				alarm.time.tm_mday = tm.tm_mday;
  				alarm.time.tm_mon = tm.tm_mon;
  				alarm.time.tm_year = tm.tm_year;
  			}
  		}
5ad31a575   David Brownell   rtc: remove BKL f...
327
  		return rtc_set_alarm(rtc, &alarm);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
328
329
  
  	case RTC_RD_TIME:
5ad31a575   David Brownell   rtc: remove BKL f...
330
  		mutex_unlock(&rtc->ops_lock);
ab6a2d70d   David Brownell   rtc: rtc interfac...
331
  		err = rtc_read_time(rtc, &tm);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
332
333
334
335
  		if (err < 0)
  			return err;
  
  		if (copy_to_user(uarg, &tm, sizeof(tm)))
5ad31a575   David Brownell   rtc: remove BKL f...
336
337
  			err = -EFAULT;
  		return err;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
338
339
  
  	case RTC_SET_TIME:
5ad31a575   David Brownell   rtc: remove BKL f...
340
  		mutex_unlock(&rtc->ops_lock);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
341
342
  		if (copy_from_user(&tm, uarg, sizeof(tm)))
  			return -EFAULT;
5ad31a575   David Brownell   rtc: remove BKL f...
343
  		return rtc_set_time(rtc, &tm);
2601a4647   David Brownell   [PATCH] rtc frame...
344

d691eb901   Alessandro Zummo   RTC: periodic irq...
345
346
347
348
349
350
  	case RTC_PIE_ON:
  		err = rtc_irq_set_state(rtc, NULL, 1);
  		break;
  
  	case RTC_PIE_OFF:
  		err = rtc_irq_set_state(rtc, NULL, 0);
2601a4647   David Brownell   [PATCH] rtc frame...
351
  		break;
099e65762   Alessandro Zummo   rtc: add alarm/up...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  	case RTC_AIE_ON:
  		mutex_unlock(&rtc->ops_lock);
  		return rtc_alarm_irq_enable(rtc, 1);
  
  	case RTC_AIE_OFF:
  		mutex_unlock(&rtc->ops_lock);
  		return rtc_alarm_irq_enable(rtc, 0);
  
  	case RTC_UIE_ON:
  		mutex_unlock(&rtc->ops_lock);
  		return rtc_update_irq_enable(rtc, 1);
  
  	case RTC_UIE_OFF:
  		mutex_unlock(&rtc->ops_lock);
  		return rtc_update_irq_enable(rtc, 0);
2601a4647   David Brownell   [PATCH] rtc frame...
367
  	case RTC_IRQP_SET:
d691eb901   Alessandro Zummo   RTC: periodic irq...
368
369
370
371
372
  		err = rtc_irq_set_freq(rtc, NULL, arg);
  		break;
  
  	case RTC_IRQP_READ:
  		err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
2601a4647   David Brownell   [PATCH] rtc frame...
373
  		break;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
374
375
376
377
378
379
380
381
382
383
  #if 0
  	case RTC_EPOCH_SET:
  #ifndef rtc_epoch
  		/*
  		 * There were no RTC clocks before 1900.
  		 */
  		if (arg < 1900) {
  			err = -EINVAL;
  			break;
  		}
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
384
385
386
387
388
389
390
391
392
393
  		rtc_epoch = arg;
  		err = 0;
  #endif
  		break;
  
  	case RTC_EPOCH_READ:
  		err = put_user(rtc_epoch, (unsigned long __user *)uarg);
  		break;
  #endif
  	case RTC_WKALM_SET:
5ad31a575   David Brownell   rtc: remove BKL f...
394
  		mutex_unlock(&rtc->ops_lock);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
395
396
  		if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  			return -EFAULT;
5ad31a575   David Brownell   rtc: remove BKL f...
397
  		return rtc_set_alarm(rtc, &alarm);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
398
399
  
  	case RTC_WKALM_RD:
5ad31a575   David Brownell   rtc: remove BKL f...
400
  		mutex_unlock(&rtc->ops_lock);
ab6a2d70d   David Brownell   rtc: rtc interfac...
401
  		err = rtc_read_alarm(rtc, &alarm);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
402
403
404
405
  		if (err < 0)
  			return err;
  
  		if (copy_to_user(uarg, &alarm, sizeof(alarm)))
5ad31a575   David Brownell   rtc: remove BKL f...
406
407
  			err = -EFAULT;
  		return err;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
408
409
  
  	default:
b3969e583   Alessandro Zummo   [PATCH] rtc subsy...
410
  		err = -ENOTTY;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
411
412
  		break;
  	}
5ad31a575   David Brownell   rtc: remove BKL f...
413
414
  done:
  	mutex_unlock(&rtc->ops_lock);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
415
416
  	return err;
  }
2e4a75cdc   Marcin Slusarz   rtc: fix kernel p...
417
418
419
420
421
  static int rtc_dev_fasync(int fd, struct file *file, int on)
  {
  	struct rtc_device *rtc = file->private_data;
  	return fasync_helper(fd, file, on, &rtc->async_queue);
  }
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
422
423
  static int rtc_dev_release(struct inode *inode, struct file *file)
  {
88efe1373   Mark Zhan   rtc-dev: no need ...
424
  	struct rtc_device *rtc = file->private_data;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
425

743e6a504   David Brownell   rtc: file close()...
426
427
428
429
430
431
432
433
  	/* We shut down the repeating IRQs that userspace enabled,
  	 * since nothing is listening to them.
  	 *  - Update (UIE) ... currently only managed through ioctls
  	 *  - Periodic (PIE) ... also used through rtc_*() interface calls
  	 *
  	 * Leave the alarm alone; it may be set to trigger a system wakeup
  	 * later, or be used by kernel code, and is a one-shot event anyway.
  	 */
099e65762   Alessandro Zummo   rtc: add alarm/up...
434
435
  
  	/* Keep ioctl until all drivers are converted */
743e6a504   David Brownell   rtc: file close()...
436
  	rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
099e65762   Alessandro Zummo   rtc: add alarm/up...
437
  	rtc_update_irq_enable(rtc, 0);
5cdc98b8f   Tomas Janousek   rtc-dev: stop per...
438
  	rtc_irq_set_state(rtc, NULL, 0);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
439
  	if (rtc->ops->release)
cd9662094   David Brownell   rtc: remove rest ...
440
  		rtc->ops->release(rtc->dev.parent);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
441

372a302e9   Jiri Kosina   RTC: assure prope...
442
  	clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
443
444
  	return 0;
  }
d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
445
  static const struct file_operations rtc_dev_fops = {
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
446
447
448
449
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.read		= rtc_dev_read,
  	.poll		= rtc_dev_poll,
5ad31a575   David Brownell   rtc: remove BKL f...
450
  	.unlocked_ioctl	= rtc_dev_ioctl,
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
451
452
453
454
455
456
  	.open		= rtc_dev_open,
  	.release	= rtc_dev_release,
  	.fasync		= rtc_dev_fasync,
  };
  
  /* insertion/removal hooks */
cb3a58d2a   David Brownell   rtc: update to cl...
457
  void rtc_dev_prepare(struct rtc_device *rtc)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
458
  {
5726fb201   David Brownell   rtc: remove /sys/...
459
460
  	if (!rtc_devt)
  		return;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
461
462
  
  	if (rtc->id >= RTC_DEV_MAX) {
5726fb201   David Brownell   rtc: remove /sys/...
463
464
465
  		pr_debug("%s: too many RTC devices
  ", rtc->name);
  		return;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
466
  	}
cd9662094   David Brownell   rtc: remove rest ...
467
  	rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
5726fb201   David Brownell   rtc: remove /sys/...
468

655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
469
  #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
c4028958b   David Howells   WorkStruct: make ...
470
  	INIT_WORK(&rtc->uie_task, rtc_uie_task);
655066c38   Atsushi Nemoto   [PATCH] RTC: rtc-...
471
472
  	setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
  #endif
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
473
474
475
  
  	cdev_init(&rtc->char_dev, &rtc_dev_fops);
  	rtc->char_dev.owner = rtc->owner;
cb3a58d2a   David Brownell   rtc: update to cl...
476
  }
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
477

cb3a58d2a   David Brownell   rtc: update to cl...
478
479
  void rtc_dev_add_device(struct rtc_device *rtc)
  {
cd9662094   David Brownell   rtc: remove rest ...
480
  	if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
5726fb201   David Brownell   rtc: remove /sys/...
481
482
483
484
485
486
  		printk(KERN_WARNING "%s: failed to add char device %d:%d
  ",
  			rtc->name, MAJOR(rtc_devt), rtc->id);
  	else
  		pr_debug("%s: dev (%d:%d)
  ", rtc->name,
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
487
  			MAJOR(rtc_devt), rtc->id);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
488
  }
5726fb201   David Brownell   rtc: remove /sys/...
489
  void rtc_dev_del_device(struct rtc_device *rtc)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
490
  {
cd9662094   David Brownell   rtc: remove rest ...
491
  	if (rtc->dev.devt)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
492
  		cdev_del(&rtc->char_dev);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
493
  }
5726fb201   David Brownell   rtc: remove /sys/...
494
  void __init rtc_dev_init(void)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
495
496
  {
  	int err;
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
497
  	err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
5726fb201   David Brownell   rtc: remove /sys/...
498
  	if (err < 0)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
499
500
501
  		printk(KERN_ERR "%s: failed to allocate char dev region
  ",
  			__FILE__);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
502
  }
5726fb201   David Brownell   rtc: remove /sys/...
503
  void __exit rtc_dev_exit(void)
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
504
  {
5726fb201   David Brownell   rtc: remove /sys/...
505
506
  	if (rtc_devt)
  		unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
e824290e5   Alessandro Zummo   [PATCH] RTC subsy...
507
  }