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 /* Provides a unique ID for each counter device */
26 static DEFINE_IDA(counter_ida);
28 struct counter_device_allochelper {
29 struct counter_device counter;
32 * This is cache line aligned to ensure private data behaves like if it
33 * were kmalloced separately.
35 unsigned long privdata[] ____cacheline_aligned;
38 static void counter_device_release(struct device *dev)
40 struct counter_device *const counter =
41 container_of(dev, struct counter_device, dev);
43 counter_chrdev_remove(counter);
44 ida_free(&counter_ida, dev->id);
46 kfree(container_of(counter, struct counter_device_allochelper, counter));
49 static struct device_type counter_device_type = {
50 .name = "counter_device",
51 .release = counter_device_release,
54 static struct bus_type counter_bus_type = {
56 .dev_name = "counter",
59 static dev_t counter_devt;
62 * counter_priv - access counter device private data
63 * @counter: counter device
65 * Get the counter device private data
67 void *counter_priv(const struct counter_device *const counter)
69 struct counter_device_allochelper *ch =
70 container_of(counter, struct counter_device_allochelper, counter);
74 EXPORT_SYMBOL_GPL(counter_priv);
77 * counter_alloc - allocate a counter_device
78 * @sizeof_priv: size of the driver private data
80 * This is part one of counter registration. The structure is allocated
81 * dynamically to ensure the right lifetime for the embedded struct device.
83 * If this succeeds, call counter_put() to get rid of the counter_device again.
85 struct counter_device *counter_alloc(size_t sizeof_priv)
87 struct counter_device_allochelper *ch;
88 struct counter_device *counter;
92 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);
122 ida_free(&counter_ida, dev->id);
130 EXPORT_SYMBOL_GPL(counter_alloc);
132 void counter_put(struct counter_device *counter)
134 put_device(&counter->dev);
136 EXPORT_SYMBOL_GPL(counter_put);
139 * counter_add - complete registration of a counter
140 * @counter: the counter to add
142 * This is part two of counter registration.
144 * If this succeeds, call counter_unregister() to get rid of the counter_device again.
146 int counter_add(struct counter_device *counter)
149 struct device *dev = &counter->dev;
151 if (counter->parent) {
152 dev->parent = counter->parent;
153 dev->of_node = counter->parent->of_node;
156 err = counter_sysfs_add(counter);
160 /* implies device_add(dev) */
161 return cdev_device_add(&counter->chrdev, dev);
163 EXPORT_SYMBOL_GPL(counter_add);
166 * counter_unregister - unregister Counter from the system
167 * @counter: pointer to Counter to unregister
169 * The Counter is unregistered from the system.
171 void counter_unregister(struct counter_device *const counter)
176 cdev_device_del(&counter->chrdev, &counter->dev);
178 mutex_lock(&counter->ops_exist_lock);
181 wake_up(&counter->events_wait);
183 mutex_unlock(&counter->ops_exist_lock);
185 EXPORT_SYMBOL_GPL(counter_unregister);
187 static void devm_counter_release(void *counter)
189 counter_unregister(counter);
192 static void devm_counter_put(void *counter)
194 counter_put(counter);
198 * devm_counter_alloc - allocate a counter_device
199 * @dev: the device to register the release callback for
200 * @sizeof_priv: size of the driver private data
202 * This is the device managed version of counter_add(). It registers a cleanup
203 * callback to care for calling counter_put().
205 struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
207 struct counter_device *counter;
210 counter = counter_alloc(sizeof_priv);
214 err = devm_add_action_or_reset(dev, devm_counter_put, counter);
220 EXPORT_SYMBOL_GPL(devm_counter_alloc);
223 * devm_counter_add - complete registration of a counter
224 * @dev: the device to register the release callback for
225 * @counter: the counter to add
227 * This is the device managed version of counter_add(). It registers a cleanup
228 * callback to care for calling counter_unregister().
230 int devm_counter_add(struct device *dev,
231 struct counter_device *const counter)
235 err = counter_add(counter);
239 return devm_add_action_or_reset(dev, devm_counter_release, counter);
241 EXPORT_SYMBOL_GPL(devm_counter_add);
243 #define COUNTER_DEV_MAX 256
245 static int __init counter_init(void)
249 err = bus_register(&counter_bus_type);
253 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX, "counter");
255 goto err_unregister_bus;
260 bus_unregister(&counter_bus_type);
264 static void __exit counter_exit(void)
266 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
267 bus_unregister(&counter_bus_type);
270 subsys_initcall(counter_init);
271 module_exit(counter_exit);
273 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
274 MODULE_DESCRIPTION("Generic Counter interface");
275 MODULE_LICENSE("GPL v2");