module: Convert symbol namespace to string literal
[linux.git] / drivers / counter / counter-core.c
blob50bd30ba3d03775b7e67d96d80e8e56d979b684d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Generic Counter interface
4 * Copyright (C) 2020 William Breathitt Gray
5 */
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>
11 #include <linux/fs.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 const struct device_type counter_device_type = {
53 .name = "counter_device",
54 .release = counter_device_release,
57 static const struct bus_type counter_bus_type = {
58 .name = "counter",
59 .dev_name = "counter",
62 static dev_t counter_devt;
64 /**
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);
75 return &ch->privdata;
77 EXPORT_SYMBOL_NS_GPL(counter_priv, "COUNTER");
79 /**
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;
92 struct device *dev;
93 int err;
95 ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
96 if (!ch)
97 return NULL;
99 counter = &ch->counter;
100 dev = &counter->dev;
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;
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);
114 if (err < 0)
115 goto err_chrdev_add;
117 device_initialize(dev);
119 err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
120 if (err)
121 goto err_dev_set_name;
123 return counter;
125 err_dev_set_name:
127 counter_chrdev_remove(counter);
128 err_chrdev_add:
130 ida_free(&counter_ida, dev->id);
131 err_ida_alloc:
133 kfree(ch);
135 return NULL;
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)
155 int err;
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);
164 if (err < 0)
165 return err;
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)
180 if (!counter)
181 return;
183 cdev_device_del(&counter->chrdev, &counter->dev);
185 mutex_lock(&counter->ops_exist_lock);
187 counter->ops = NULL;
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;
215 int err;
217 counter = counter_alloc(sizeof_priv);
218 if (!counter)
219 return NULL;
221 err = devm_add_action_or_reset(dev, devm_counter_put, counter);
222 if (err < 0)
223 return NULL;
225 return 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)
240 int err;
242 err = counter_add(counter);
243 if (err < 0)
244 return err;
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)
254 int err;
256 err = bus_register(&counter_bus_type);
257 if (err < 0)
258 return err;
260 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX,
261 COUNTER_NAME);
262 if (err < 0)
263 goto err_unregister_bus;
265 return 0;
267 err_unregister_bus:
268 bus_unregister(&counter_bus_type);
269 return err;
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");