at v2.6.17-rc5 8.7 kB view raw
1/* 2 * RTC subsystem, dev interface 3 * 4 * Copyright (C) 2005 Tower Technologies 5 * Author: Alessandro Zummo <a.zummo@towertech.it> 6 * 7 * based on arch/arm/common/rtctime.c 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12*/ 13 14#include <linux/module.h> 15#include <linux/rtc.h> 16 17static struct class *rtc_dev_class; 18static dev_t rtc_devt; 19 20#define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */ 21 22static int rtc_dev_open(struct inode *inode, struct file *file) 23{ 24 int err; 25 struct rtc_device *rtc = container_of(inode->i_cdev, 26 struct rtc_device, char_dev); 27 struct rtc_class_ops *ops = rtc->ops; 28 29 /* We keep the lock as long as the device is in use 30 * and return immediately if busy 31 */ 32 if (!(mutex_trylock(&rtc->char_lock))) 33 return -EBUSY; 34 35 file->private_data = &rtc->class_dev; 36 37 err = ops->open ? ops->open(rtc->class_dev.dev) : 0; 38 if (err == 0) { 39 spin_lock_irq(&rtc->irq_lock); 40 rtc->irq_data = 0; 41 spin_unlock_irq(&rtc->irq_lock); 42 43 return 0; 44 } 45 46 /* something has gone wrong, release the lock */ 47 mutex_unlock(&rtc->char_lock); 48 return err; 49} 50 51 52static ssize_t 53rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 54{ 55 struct rtc_device *rtc = to_rtc_device(file->private_data); 56 57 DECLARE_WAITQUEUE(wait, current); 58 unsigned long data; 59 ssize_t ret; 60 61 if (count != sizeof(unsigned int) && count < sizeof(unsigned long)) 62 return -EINVAL; 63 64 add_wait_queue(&rtc->irq_queue, &wait); 65 do { 66 __set_current_state(TASK_INTERRUPTIBLE); 67 68 spin_lock_irq(&rtc->irq_lock); 69 data = rtc->irq_data; 70 rtc->irq_data = 0; 71 spin_unlock_irq(&rtc->irq_lock); 72 73 if (data != 0) { 74 ret = 0; 75 break; 76 } 77 if (file->f_flags & O_NONBLOCK) { 78 ret = -EAGAIN; 79 break; 80 } 81 if (signal_pending(current)) { 82 ret = -ERESTARTSYS; 83 break; 84 } 85 schedule(); 86 } while (1); 87 set_current_state(TASK_RUNNING); 88 remove_wait_queue(&rtc->irq_queue, &wait); 89 90 if (ret == 0) { 91 /* Check for any data updates */ 92 if (rtc->ops->read_callback) 93 data = rtc->ops->read_callback(rtc->class_dev.dev, 94 data); 95 96 if (sizeof(int) != sizeof(long) && 97 count == sizeof(unsigned int)) 98 ret = put_user(data, (unsigned int __user *)buf) ?: 99 sizeof(unsigned int); 100 else 101 ret = put_user(data, (unsigned long __user *)buf) ?: 102 sizeof(unsigned long); 103 } 104 return ret; 105} 106 107static unsigned int rtc_dev_poll(struct file *file, poll_table *wait) 108{ 109 struct rtc_device *rtc = to_rtc_device(file->private_data); 110 unsigned long data; 111 112 poll_wait(file, &rtc->irq_queue, wait); 113 114 data = rtc->irq_data; 115 116 return (data != 0) ? (POLLIN | POLLRDNORM) : 0; 117} 118 119static int rtc_dev_ioctl(struct inode *inode, struct file *file, 120 unsigned int cmd, unsigned long arg) 121{ 122 int err = 0; 123 struct class_device *class_dev = file->private_data; 124 struct rtc_device *rtc = to_rtc_device(class_dev); 125 struct rtc_class_ops *ops = rtc->ops; 126 struct rtc_time tm; 127 struct rtc_wkalrm alarm; 128 void __user *uarg = (void __user *) arg; 129 130 /* avoid conflicting IRQ users */ 131 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) { 132 spin_lock(&rtc->irq_task_lock); 133 if (rtc->irq_task) 134 err = -EBUSY; 135 spin_unlock(&rtc->irq_task_lock); 136 137 if (err < 0) 138 return err; 139 } 140 141 /* try the driver's ioctl interface */ 142 if (ops->ioctl) { 143 err = ops->ioctl(class_dev->dev, cmd, arg); 144 if (err != -ENOIOCTLCMD) 145 return err; 146 } 147 148 /* if the driver does not provide the ioctl interface 149 * or if that particular ioctl was not implemented 150 * (-ENOIOCTLCMD), we will try to emulate here. 151 */ 152 153 switch (cmd) { 154 case RTC_ALM_READ: 155 err = rtc_read_alarm(class_dev, &alarm); 156 if (err < 0) 157 return err; 158 159 if (copy_to_user(uarg, &alarm.time, sizeof(tm))) 160 return -EFAULT; 161 break; 162 163 case RTC_ALM_SET: 164 if (copy_from_user(&alarm.time, uarg, sizeof(tm))) 165 return -EFAULT; 166 167 alarm.enabled = 0; 168 alarm.pending = 0; 169 alarm.time.tm_mday = -1; 170 alarm.time.tm_mon = -1; 171 alarm.time.tm_year = -1; 172 alarm.time.tm_wday = -1; 173 alarm.time.tm_yday = -1; 174 alarm.time.tm_isdst = -1; 175 err = rtc_set_alarm(class_dev, &alarm); 176 break; 177 178 case RTC_RD_TIME: 179 err = rtc_read_time(class_dev, &tm); 180 if (err < 0) 181 return err; 182 183 if (copy_to_user(uarg, &tm, sizeof(tm))) 184 return -EFAULT; 185 break; 186 187 case RTC_SET_TIME: 188 if (!capable(CAP_SYS_TIME)) 189 return -EACCES; 190 191 if (copy_from_user(&tm, uarg, sizeof(tm))) 192 return -EFAULT; 193 194 err = rtc_set_time(class_dev, &tm); 195 break; 196#if 0 197 case RTC_EPOCH_SET: 198#ifndef rtc_epoch 199 /* 200 * There were no RTC clocks before 1900. 201 */ 202 if (arg < 1900) { 203 err = -EINVAL; 204 break; 205 } 206 if (!capable(CAP_SYS_TIME)) { 207 err = -EACCES; 208 break; 209 } 210 rtc_epoch = arg; 211 err = 0; 212#endif 213 break; 214 215 case RTC_EPOCH_READ: 216 err = put_user(rtc_epoch, (unsigned long __user *)uarg); 217 break; 218#endif 219 case RTC_WKALM_SET: 220 if (copy_from_user(&alarm, uarg, sizeof(alarm))) 221 return -EFAULT; 222 223 err = rtc_set_alarm(class_dev, &alarm); 224 break; 225 226 case RTC_WKALM_RD: 227 err = rtc_read_alarm(class_dev, &alarm); 228 if (err < 0) 229 return err; 230 231 if (copy_to_user(uarg, &alarm, sizeof(alarm))) 232 return -EFAULT; 233 break; 234 235 default: 236 err = -ENOTTY; 237 break; 238 } 239 240 return err; 241} 242 243static int rtc_dev_release(struct inode *inode, struct file *file) 244{ 245 struct rtc_device *rtc = to_rtc_device(file->private_data); 246 247 if (rtc->ops->release) 248 rtc->ops->release(rtc->class_dev.dev); 249 250 mutex_unlock(&rtc->char_lock); 251 return 0; 252} 253 254static int rtc_dev_fasync(int fd, struct file *file, int on) 255{ 256 struct rtc_device *rtc = to_rtc_device(file->private_data); 257 return fasync_helper(fd, file, on, &rtc->async_queue); 258} 259 260static struct file_operations rtc_dev_fops = { 261 .owner = THIS_MODULE, 262 .llseek = no_llseek, 263 .read = rtc_dev_read, 264 .poll = rtc_dev_poll, 265 .ioctl = rtc_dev_ioctl, 266 .open = rtc_dev_open, 267 .release = rtc_dev_release, 268 .fasync = rtc_dev_fasync, 269}; 270 271/* insertion/removal hooks */ 272 273static int rtc_dev_add_device(struct class_device *class_dev, 274 struct class_interface *class_intf) 275{ 276 int err = 0; 277 struct rtc_device *rtc = to_rtc_device(class_dev); 278 279 if (rtc->id >= RTC_DEV_MAX) { 280 dev_err(class_dev->dev, "too many RTCs\n"); 281 return -EINVAL; 282 } 283 284 mutex_init(&rtc->char_lock); 285 spin_lock_init(&rtc->irq_lock); 286 init_waitqueue_head(&rtc->irq_queue); 287 288 cdev_init(&rtc->char_dev, &rtc_dev_fops); 289 rtc->char_dev.owner = rtc->owner; 290 291 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) { 292 cdev_del(&rtc->char_dev); 293 dev_err(class_dev->dev, 294 "failed to add char device %d:%d\n", 295 MAJOR(rtc_devt), rtc->id); 296 return -ENODEV; 297 } 298 299 rtc->rtc_dev = class_device_create(rtc_dev_class, NULL, 300 MKDEV(MAJOR(rtc_devt), rtc->id), 301 class_dev->dev, "rtc%d", rtc->id); 302 if (IS_ERR(rtc->rtc_dev)) { 303 dev_err(class_dev->dev, "cannot create rtc_dev device\n"); 304 err = PTR_ERR(rtc->rtc_dev); 305 goto err_cdev_del; 306 } 307 308 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n", 309 MAJOR(rtc->rtc_dev->devt), 310 MINOR(rtc->rtc_dev->devt)); 311 312 return 0; 313 314err_cdev_del: 315 316 cdev_del(&rtc->char_dev); 317 return err; 318} 319 320static void rtc_dev_remove_device(struct class_device *class_dev, 321 struct class_interface *class_intf) 322{ 323 struct rtc_device *rtc = to_rtc_device(class_dev); 324 325 if (rtc->rtc_dev) { 326 dev_dbg(class_dev->dev, "removing char %d:%d\n", 327 MAJOR(rtc->rtc_dev->devt), 328 MINOR(rtc->rtc_dev->devt)); 329 330 class_device_unregister(rtc->rtc_dev); 331 cdev_del(&rtc->char_dev); 332 } 333} 334 335/* interface registration */ 336 337static struct class_interface rtc_dev_interface = { 338 .add = &rtc_dev_add_device, 339 .remove = &rtc_dev_remove_device, 340}; 341 342static int __init rtc_dev_init(void) 343{ 344 int err; 345 346 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev"); 347 if (IS_ERR(rtc_dev_class)) 348 return PTR_ERR(rtc_dev_class); 349 350 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); 351 if (err < 0) { 352 printk(KERN_ERR "%s: failed to allocate char dev region\n", 353 __FILE__); 354 goto err_destroy_class; 355 } 356 357 err = rtc_interface_register(&rtc_dev_interface); 358 if (err < 0) { 359 printk(KERN_ERR "%s: failed to register the interface\n", 360 __FILE__); 361 goto err_unregister_chrdev; 362 } 363 364 return 0; 365 366err_unregister_chrdev: 367 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); 368 369err_destroy_class: 370 class_destroy(rtc_dev_class); 371 372 return err; 373} 374 375static void __exit rtc_dev_exit(void) 376{ 377 class_interface_unregister(&rtc_dev_interface); 378 class_destroy(rtc_dev_class); 379 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); 380} 381 382module_init(rtc_dev_init); 383module_exit(rtc_dev_exit); 384 385MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); 386MODULE_DESCRIPTION("RTC class dev interface"); 387MODULE_LICENSE("GPL");