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 ensures private data behaves like if it were kmalloced
35 * separately. Also ensures the minimum alignment for safe DMA
36 * operations (which may or may not mean cache alignment).
38 unsigned long privdata[] __aligned(ARCH_DMA_MINALIGN);
41 static void counter_device_release(struct device *dev)
43 struct counter_device *const counter =
44 container_of(dev, struct counter_device, dev);
46 counter_chrdev_remove(counter);
47 ida_free(&counter_ida, dev->id);
49 kfree(container_of(counter, struct counter_device_allochelper, counter));
52 static struct device_type counter_device_type = {
53 .name = "counter_device",
54 .release = counter_device_release,
57 static struct bus_type counter_bus_type = {
59 .dev_name = "counter",
62 static dev_t counter_devt;
65 * counter_priv - access counter device private data
66 * @counter: counter device
68 * Get the counter device private data
70 void *counter_priv(const struct counter_device *const counter)
72 struct counter_device_allochelper *ch =
73 container_of(counter, struct counter_device_allochelper, counter);
77 EXPORT_SYMBOL_NS_GPL(counter_priv, COUNTER);
80 * counter_alloc - allocate a counter_device
81 * @sizeof_priv: size of the driver private data
83 * This is part one of counter registration. The structure is allocated
84 * dynamically to ensure the right lifetime for the embedded struct device.
86 * If this succeeds, call counter_put() to get rid of the counter_device again.
88 struct counter_device *counter_alloc(size_t sizeof_priv)
90 struct counter_device_allochelper *ch;
91 struct counter_device *counter;
95 ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
99 counter = &ch->counter;
102 /* Acquire unique ID */
103 err = ida_alloc(&counter_ida, GFP_KERNEL);
108 mutex_init(&counter->ops_exist_lock);
109 dev->type = &counter_device_type;
110 dev->bus = &counter_bus_type;
111 dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
113 err = counter_chrdev_add(counter);
117 device_initialize(dev);
119 err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
121 goto err_dev_set_name;
127 counter_chrdev_remove(counter);
130 ida_free(&counter_ida, dev->id);
137 EXPORT_SYMBOL_NS_GPL(counter_alloc, COUNTER);
139 void counter_put(struct counter_device *counter)
141 put_device(&counter->dev);
143 EXPORT_SYMBOL_NS_GPL(counter_put, COUNTER);
146 * counter_add - complete registration of a counter
147 * @counter: the counter to add
149 * This is part two of counter registration.
151 * If this succeeds, call counter_unregister() to get rid of the counter_device again.
153 int counter_add(struct counter_device *counter)
156 struct device *dev = &counter->dev;
158 if (counter->parent) {
159 dev->parent = counter->parent;
160 dev->of_node = counter->parent->of_node;
163 err = counter_sysfs_add(counter);
167 /* implies device_add(dev) */
168 return cdev_device_add(&counter->chrdev, dev);
170 EXPORT_SYMBOL_NS_GPL(counter_add, COUNTER);
173 * counter_unregister - unregister Counter from the system
174 * @counter: pointer to Counter to unregister
176 * The Counter is unregistered from the system.
178 void counter_unregister(struct counter_device *const counter)
183 cdev_device_del(&counter->chrdev, &counter->dev);
185 mutex_lock(&counter->ops_exist_lock);
188 wake_up(&counter->events_wait);
190 mutex_unlock(&counter->ops_exist_lock);
192 EXPORT_SYMBOL_NS_GPL(counter_unregister, COUNTER);
194 static void devm_counter_release(void *counter)
196 counter_unregister(counter);
199 static void devm_counter_put(void *counter)
201 counter_put(counter);
205 * devm_counter_alloc - allocate a counter_device
206 * @dev: the device to register the release callback for
207 * @sizeof_priv: size of the driver private data
209 * This is the device managed version of counter_add(). It registers a cleanup
210 * callback to care for calling counter_put().
212 struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
214 struct counter_device *counter;
217 counter = counter_alloc(sizeof_priv);
221 err = devm_add_action_or_reset(dev, devm_counter_put, counter);
227 EXPORT_SYMBOL_NS_GPL(devm_counter_alloc, COUNTER);
230 * devm_counter_add - complete registration of a counter
231 * @dev: the device to register the release callback for
232 * @counter: the counter to add
234 * This is the device managed version of counter_add(). It registers a cleanup
235 * callback to care for calling counter_unregister().
237 int devm_counter_add(struct device *dev,
238 struct counter_device *const counter)
242 err = counter_add(counter);
246 return devm_add_action_or_reset(dev, devm_counter_release, counter);
248 EXPORT_SYMBOL_NS_GPL(devm_counter_add, COUNTER);
250 #define COUNTER_DEV_MAX 256
252 static int __init counter_init(void)
256 err = bus_register(&counter_bus_type);
260 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX,
263 goto err_unregister_bus;
268 bus_unregister(&counter_bus_type);
272 static void __exit counter_exit(void)
274 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
275 bus_unregister(&counter_bus_type);
278 subsys_initcall(counter_init);
279 module_exit(counter_exit);
281 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
282 MODULE_DESCRIPTION("Generic Counter interface");
283 MODULE_LICENSE("GPL v2");