1 // SPDX-License-Identifier: GPL-2.0
3 * Generic Counter interface
4 * Copyright (C) 2020 William Breathitt Gray
6 #include <linux/cdev.h>
7 #include <linux/counter.h>
8 #include <linux/device.h>
9 #include <linux/device/bus.h>
10 #include <linux/export.h>
12 #include <linux/gfp.h>
13 #include <linux/idr.h>
14 #include <linux/init.h>
15 #include <linux/kdev_t.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/wait.h>
22 #include "counter-chrdev.h"
23 #include "counter-sysfs.h"
25 #define COUNTER_NAME "counter"
27 /* Provides a unique ID for each counter device */
28 static DEFINE_IDA(counter_ida);
30 struct counter_device_allochelper {
31 struct counter_device counter;
34 * This is cache line aligned to ensure private data behaves like if it
35 * were kmalloced separately.
37 unsigned long privdata[] ____cacheline_aligned;
40 static void counter_device_release(struct device *dev)
42 struct counter_device *const counter =
43 container_of(dev, struct counter_device, dev);
45 counter_chrdev_remove(counter);
46 ida_free(&counter_ida, dev->id);
48 kfree(container_of(counter, struct counter_device_allochelper, counter));
51 static struct device_type counter_device_type = {
52 .name = "counter_device",
53 .release = counter_device_release,
56 static struct bus_type counter_bus_type = {
58 .dev_name = "counter",
61 static dev_t counter_devt;
64 * counter_priv - access counter device private data
65 * @counter: counter device
67 * Get the counter device private data
69 void *counter_priv(const struct counter_device *const counter)
71 struct counter_device_allochelper *ch =
72 container_of(counter, struct counter_device_allochelper, counter);
76 EXPORT_SYMBOL_NS_GPL(counter_priv, COUNTER);
79 * counter_alloc - allocate a counter_device
80 * @sizeof_priv: size of the driver private data
82 * This is part one of counter registration. The structure is allocated
83 * dynamically to ensure the right lifetime for the embedded struct device.
85 * If this succeeds, call counter_put() to get rid of the counter_device again.
87 struct counter_device *counter_alloc(size_t sizeof_priv)
89 struct counter_device_allochelper *ch;
90 struct counter_device *counter;
94 ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
98 counter = &ch->counter;
101 /* Acquire unique ID */
102 err = ida_alloc(&counter_ida, GFP_KERNEL);
107 mutex_init(&counter->ops_exist_lock);
108 dev->type = &counter_device_type;
109 dev->bus = &counter_bus_type;
110 dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
112 err = counter_chrdev_add(counter);
116 device_initialize(dev);
118 err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
120 goto err_dev_set_name;
126 counter_chrdev_remove(counter);
129 ida_free(&counter_ida, dev->id);
136 EXPORT_SYMBOL_NS_GPL(counter_alloc, COUNTER);
138 void counter_put(struct counter_device *counter)
140 put_device(&counter->dev);
142 EXPORT_SYMBOL_NS_GPL(counter_put, COUNTER);
145 * counter_add - complete registration of a counter
146 * @counter: the counter to add
148 * This is part two of counter registration.
150 * If this succeeds, call counter_unregister() to get rid of the counter_device again.
152 int counter_add(struct counter_device *counter)
155 struct device *dev = &counter->dev;
157 if (counter->parent) {
158 dev->parent = counter->parent;
159 dev->of_node = counter->parent->of_node;
162 err = counter_sysfs_add(counter);
166 /* implies device_add(dev) */
167 return cdev_device_add(&counter->chrdev, dev);
169 EXPORT_SYMBOL_NS_GPL(counter_add, COUNTER);
172 * counter_unregister - unregister Counter from the system
173 * @counter: pointer to Counter to unregister
175 * The Counter is unregistered from the system.
177 void counter_unregister(struct counter_device *const counter)
182 cdev_device_del(&counter->chrdev, &counter->dev);
184 mutex_lock(&counter->ops_exist_lock);
187 wake_up(&counter->events_wait);
189 mutex_unlock(&counter->ops_exist_lock);
191 EXPORT_SYMBOL_NS_GPL(counter_unregister, COUNTER);
193 static void devm_counter_release(void *counter)
195 counter_unregister(counter);
198 static void devm_counter_put(void *counter)
200 counter_put(counter);
204 * devm_counter_alloc - allocate a counter_device
205 * @dev: the device to register the release callback for
206 * @sizeof_priv: size of the driver private data
208 * This is the device managed version of counter_add(). It registers a cleanup
209 * callback to care for calling counter_put().
211 struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
213 struct counter_device *counter;
216 counter = counter_alloc(sizeof_priv);
220 err = devm_add_action_or_reset(dev, devm_counter_put, counter);
226 EXPORT_SYMBOL_NS_GPL(devm_counter_alloc, COUNTER);
229 * devm_counter_add - complete registration of a counter
230 * @dev: the device to register the release callback for
231 * @counter: the counter to add
233 * This is the device managed version of counter_add(). It registers a cleanup
234 * callback to care for calling counter_unregister().
236 int devm_counter_add(struct device *dev,
237 struct counter_device *const counter)
241 err = counter_add(counter);
245 return devm_add_action_or_reset(dev, devm_counter_release, counter);
247 EXPORT_SYMBOL_NS_GPL(devm_counter_add, COUNTER);
249 #define COUNTER_DEV_MAX 256
251 static int __init counter_init(void)
255 err = bus_register(&counter_bus_type);
259 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX,
262 goto err_unregister_bus;
267 bus_unregister(&counter_bus_type);
271 static void __exit counter_exit(void)
273 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
274 bus_unregister(&counter_bus_type);
277 subsys_initcall(counter_init);
278 module_exit(counter_exit);
280 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
281 MODULE_DESCRIPTION("Generic Counter interface");
282 MODULE_LICENSE("GPL v2");