blob: 3f24481fc04a1258624020a9a919f2d10640057a [file] [log] [blame]
William Breathitt Grayaaec1a02021-08-27 12:47:47 +09001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generic Counter interface
4 * Copyright (C) 2020 William Breathitt Gray
5 */
William Breathitt Grayb6c50af2021-09-29 12:15:59 +09006#include <linux/cdev.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +09007#include <linux/counter.h>
8#include <linux/device.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +09009#include <linux/device/bus.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090010#include <linux/export.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090011#include <linux/fs.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090012#include <linux/gfp.h>
13#include <linux/idr.h>
14#include <linux/init.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090015#include <linux/kdev_t.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090016#include <linux/module.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090017#include <linux/mutex.h>
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010018#include <linux/slab.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090019#include <linux/types.h>
20#include <linux/wait.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090021
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090022#include "counter-chrdev.h"
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090023#include "counter-sysfs.h"
24
William Breathitt Gray4da08472022-03-15 15:38:53 -040025#define COUNTER_NAME "counter"
26
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090027/* Provides a unique ID for each counter device */
28static DEFINE_IDA(counter_ida);
29
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010030struct counter_device_allochelper {
31 struct counter_device counter;
32
33 /*
Nuno Sac83ccdc2024-02-09 16:50:34 +010034 * 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).
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010037 */
Nuno Sac83ccdc2024-02-09 16:50:34 +010038 unsigned long privdata[] __aligned(ARCH_DMA_MINALIGN);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010039};
40
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090041static void counter_device_release(struct device *dev)
42{
Uwe Kleine-Königb56346d2021-12-30 16:02:38 +010043 struct counter_device *const counter =
44 container_of(dev, struct counter_device, dev);
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090045
46 counter_chrdev_remove(counter);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090047 ida_free(&counter_ida, dev->id);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010048
Uwe Kleine-Königf2ee4752021-12-30 16:03:00 +010049 kfree(container_of(counter, struct counter_device_allochelper, counter));
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090050}
51
52static struct device_type counter_device_type = {
53 .name = "counter_device",
54 .release = counter_device_release,
55};
56
57static struct bus_type counter_bus_type = {
58 .name = "counter",
59 .dev_name = "counter",
60};
61
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090062static dev_t counter_devt;
63
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090064/**
Uwe Kleine-König5207fb22021-12-30 16:02:41 +010065 * counter_priv - access counter device private data
66 * @counter: counter device
67 *
68 * Get the counter device private data
69 */
70void *counter_priv(const struct counter_device *const counter)
71{
Uwe Kleine-Königf2ee4752021-12-30 16:03:00 +010072 struct counter_device_allochelper *ch =
73 container_of(counter, struct counter_device_allochelper, counter);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010074
Uwe Kleine-Königf2ee4752021-12-30 16:03:00 +010075 return &ch->privdata;
Uwe Kleine-König5207fb22021-12-30 16:02:41 +010076}
William Breathitt Gray3216e552022-09-27 18:53:36 -040077EXPORT_SYMBOL_NS_GPL(counter_priv, COUNTER);
Uwe Kleine-König5207fb22021-12-30 16:02:41 +010078
79/**
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010080 * counter_alloc - allocate a counter_device
81 * @sizeof_priv: size of the driver private data
82 *
83 * This is part one of counter registration. The structure is allocated
84 * dynamically to ensure the right lifetime for the embedded struct device.
85 *
86 * If this succeeds, call counter_put() to get rid of the counter_device again.
87 */
88struct counter_device *counter_alloc(size_t sizeof_priv)
89{
90 struct counter_device_allochelper *ch;
91 struct counter_device *counter;
92 struct device *dev;
93 int err;
94
95 ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
Dan Carpenterfc55e632022-01-11 20:32:43 +030096 if (!ch)
97 return NULL;
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +010098
99 counter = &ch->counter;
100 dev = &counter->dev;
101
102 /* Acquire unique ID */
103 err = ida_alloc(&counter_ida, GFP_KERNEL);
104 if (err < 0)
105 goto err_ida_alloc;
106 dev->id = err;
107
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);
112
113 err = counter_chrdev_add(counter);
114 if (err < 0)
115 goto err_chrdev_add;
116
117 device_initialize(dev);
118
William Breathitt Gray4da08472022-03-15 15:38:53 -0400119 err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
120 if (err)
121 goto err_dev_set_name;
122
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100123 return counter;
124
William Breathitt Gray4da08472022-03-15 15:38:53 -0400125err_dev_set_name:
126
127 counter_chrdev_remove(counter);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100128err_chrdev_add:
129
130 ida_free(&counter_ida, dev->id);
131err_ida_alloc:
132
133 kfree(ch);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100134
Dan Carpenterfc55e632022-01-11 20:32:43 +0300135 return NULL;
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100136}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400137EXPORT_SYMBOL_NS_GPL(counter_alloc, COUNTER);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100138
139void counter_put(struct counter_device *counter)
140{
141 put_device(&counter->dev);
142}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400143EXPORT_SYMBOL_NS_GPL(counter_put, COUNTER);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100144
145/**
146 * counter_add - complete registration of a counter
147 * @counter: the counter to add
148 *
149 * This is part two of counter registration.
150 *
151 * If this succeeds, call counter_unregister() to get rid of the counter_device again.
152 */
153int counter_add(struct counter_device *counter)
154{
155 int err;
156 struct device *dev = &counter->dev;
157
158 if (counter->parent) {
159 dev->parent = counter->parent;
160 dev->of_node = counter->parent->of_node;
161 }
162
163 err = counter_sysfs_add(counter);
164 if (err < 0)
165 return err;
166
167 /* implies device_add(dev) */
168 return cdev_device_add(&counter->chrdev, dev);
169}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400170EXPORT_SYMBOL_NS_GPL(counter_add, COUNTER);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100171
172/**
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900173 * counter_unregister - unregister Counter from the system
174 * @counter: pointer to Counter to unregister
175 *
176 * The Counter is unregistered from the system.
177 */
178void counter_unregister(struct counter_device *const counter)
179{
180 if (!counter)
181 return;
182
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900183 cdev_device_del(&counter->chrdev, &counter->dev);
184
185 mutex_lock(&counter->ops_exist_lock);
186
187 counter->ops = NULL;
188 wake_up(&counter->events_wait);
189
190 mutex_unlock(&counter->ops_exist_lock);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900191}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400192EXPORT_SYMBOL_NS_GPL(counter_unregister, COUNTER);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900193
194static void devm_counter_release(void *counter)
195{
196 counter_unregister(counter);
197}
198
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100199static void devm_counter_put(void *counter)
200{
201 counter_put(counter);
202}
203
204/**
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
208 *
209 * This is the device managed version of counter_add(). It registers a cleanup
210 * callback to care for calling counter_put().
211 */
212struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
213{
214 struct counter_device *counter;
215 int err;
216
217 counter = counter_alloc(sizeof_priv);
Dan Carpenterfc55e632022-01-11 20:32:43 +0300218 if (!counter)
219 return NULL;
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100220
221 err = devm_add_action_or_reset(dev, devm_counter_put, counter);
222 if (err < 0)
Dan Carpenterfc55e632022-01-11 20:32:43 +0300223 return NULL;
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100224
225 return counter;
226}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400227EXPORT_SYMBOL_NS_GPL(devm_counter_alloc, COUNTER);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100228
229/**
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
233 *
234 * This is the device managed version of counter_add(). It registers a cleanup
235 * callback to care for calling counter_unregister().
236 */
237int devm_counter_add(struct device *dev,
238 struct counter_device *const counter)
239{
240 int err;
241
242 err = counter_add(counter);
243 if (err < 0)
244 return err;
245
246 return devm_add_action_or_reset(dev, devm_counter_release, counter);
247}
William Breathitt Gray3216e552022-09-27 18:53:36 -0400248EXPORT_SYMBOL_NS_GPL(devm_counter_add, COUNTER);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100249
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900250#define COUNTER_DEV_MAX 256
251
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900252static int __init counter_init(void)
253{
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900254 int err;
255
256 err = bus_register(&counter_bus_type);
257 if (err < 0)
258 return err;
259
William Breathitt Gray4da08472022-03-15 15:38:53 -0400260 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX,
261 COUNTER_NAME);
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900262 if (err < 0)
263 goto err_unregister_bus;
264
265 return 0;
266
267err_unregister_bus:
268 bus_unregister(&counter_bus_type);
269 return err;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900270}
271
272static void __exit counter_exit(void)
273{
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900274 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900275 bus_unregister(&counter_bus_type);
276}
277
278subsys_initcall(counter_init);
279module_exit(counter_exit);
280
281MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
282MODULE_DESCRIPTION("Generic Counter interface");
283MODULE_LICENSE("GPL v2");