Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * RTC subsystem, base class |
| 3 | * |
| 4 | * Copyright (C) 2005 Tower Technologies |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 6 | * |
| 7 | * class skeleton from drivers/hwmon/hwmon.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 | #include <linux/kdev_t.h> |
| 17 | #include <linux/idr.h> |
| 18 | |
| 19 | static DEFINE_IDR(rtc_idr); |
| 20 | static DEFINE_MUTEX(idr_lock); |
| 21 | struct class *rtc_class; |
| 22 | |
| 23 | static void rtc_device_release(struct class_device *class_dev) |
| 24 | { |
| 25 | struct rtc_device *rtc = to_rtc_device(class_dev); |
| 26 | mutex_lock(&idr_lock); |
| 27 | idr_remove(&rtc_idr, rtc->id); |
| 28 | mutex_unlock(&idr_lock); |
| 29 | kfree(rtc); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * rtc_device_register - register w/ RTC class |
| 34 | * @dev: the device to register |
| 35 | * |
| 36 | * rtc_device_unregister() must be called when the class device is no |
| 37 | * longer needed. |
| 38 | * |
| 39 | * Returns the pointer to the new struct class device. |
| 40 | */ |
| 41 | struct rtc_device *rtc_device_register(const char *name, struct device *dev, |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 42 | const struct rtc_class_ops *ops, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 43 | struct module *owner) |
| 44 | { |
| 45 | struct rtc_device *rtc; |
| 46 | int id, err; |
| 47 | |
| 48 | if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) { |
| 49 | err = -ENOMEM; |
| 50 | goto exit; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | mutex_lock(&idr_lock); |
| 55 | err = idr_get_new(&rtc_idr, NULL, &id); |
| 56 | mutex_unlock(&idr_lock); |
| 57 | |
| 58 | if (err < 0) |
| 59 | goto exit; |
| 60 | |
| 61 | id = id & MAX_ID_MASK; |
| 62 | |
| 63 | rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL); |
| 64 | if (rtc == NULL) { |
| 65 | err = -ENOMEM; |
| 66 | goto exit_idr; |
| 67 | } |
| 68 | |
| 69 | rtc->id = id; |
| 70 | rtc->ops = ops; |
| 71 | rtc->owner = owner; |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 72 | rtc->max_user_freq = 64; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 73 | rtc->class_dev.dev = dev; |
| 74 | rtc->class_dev.class = rtc_class; |
| 75 | rtc->class_dev.release = rtc_device_release; |
| 76 | |
| 77 | mutex_init(&rtc->ops_lock); |
| 78 | spin_lock_init(&rtc->irq_lock); |
| 79 | spin_lock_init(&rtc->irq_task_lock); |
| 80 | |
| 81 | strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE); |
| 82 | snprintf(rtc->class_dev.class_id, BUS_ID_SIZE, "rtc%d", id); |
| 83 | |
| 84 | err = class_device_register(&rtc->class_dev); |
| 85 | if (err) |
| 86 | goto exit_kfree; |
| 87 | |
| 88 | dev_info(dev, "rtc core: registered %s as %s\n", |
| 89 | rtc->name, rtc->class_dev.class_id); |
| 90 | |
| 91 | return rtc; |
| 92 | |
| 93 | exit_kfree: |
| 94 | kfree(rtc); |
| 95 | |
| 96 | exit_idr: |
Sonny Rao | 6ac12df | 2006-06-27 02:54:06 -0700 | [diff] [blame] | 97 | mutex_lock(&idr_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 98 | idr_remove(&rtc_idr, id); |
Sonny Rao | 6ac12df | 2006-06-27 02:54:06 -0700 | [diff] [blame] | 99 | mutex_unlock(&idr_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 100 | |
| 101 | exit: |
Alessandro Zummo | d1d65b7 | 2006-04-10 22:54:45 -0700 | [diff] [blame] | 102 | dev_err(dev, "rtc core: unable to register %s, err = %d\n", |
| 103 | name, err); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 104 | return ERR_PTR(err); |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(rtc_device_register); |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * rtc_device_unregister - removes the previously registered RTC class device |
| 111 | * |
| 112 | * @rtc: the RTC class device to destroy |
| 113 | */ |
| 114 | void rtc_device_unregister(struct rtc_device *rtc) |
| 115 | { |
David Brownell | e109ebd | 2007-02-28 20:12:40 -0800 | [diff] [blame] | 116 | if (class_device_get(&rtc->class_dev) != NULL) { |
| 117 | mutex_lock(&rtc->ops_lock); |
| 118 | /* remove innards of this RTC, then disable it, before |
| 119 | * letting any rtc_class_open() users access it again |
| 120 | */ |
| 121 | class_device_unregister(&rtc->class_dev); |
| 122 | rtc->ops = NULL; |
| 123 | mutex_unlock(&rtc->ops_lock); |
| 124 | class_device_put(&rtc->class_dev); |
| 125 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL_GPL(rtc_device_unregister); |
| 128 | |
| 129 | int rtc_interface_register(struct class_interface *intf) |
| 130 | { |
| 131 | intf->class = rtc_class; |
| 132 | return class_interface_register(intf); |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(rtc_interface_register); |
| 135 | |
| 136 | static int __init rtc_init(void) |
| 137 | { |
| 138 | rtc_class = class_create(THIS_MODULE, "rtc"); |
| 139 | if (IS_ERR(rtc_class)) { |
| 140 | printk(KERN_ERR "%s: couldn't create class\n", __FILE__); |
| 141 | return PTR_ERR(rtc_class); |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static void __exit rtc_exit(void) |
| 147 | { |
| 148 | class_destroy(rtc_class); |
| 149 | } |
| 150 | |
David Brownell | 818a867 | 2006-09-30 23:28:15 -0700 | [diff] [blame] | 151 | subsys_initcall(rtc_init); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 152 | module_exit(rtc_exit); |
| 153 | |
David Brownell | 818a867 | 2006-09-30 23:28:15 -0700 | [diff] [blame] | 154 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 155 | MODULE_DESCRIPTION("RTC class support"); |
| 156 | MODULE_LICENSE("GPL"); |