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/types.h>
19 #include <linux/wait.h>
21 #include "counter-chrdev.h"
22 #include "counter-sysfs.h"
24 /* Provides a unique ID for each counter device */
25 static DEFINE_IDA(counter_ida);
27 static void counter_device_release(struct device *dev)
29 struct counter_device *const counter = dev_get_drvdata(dev);
31 counter_chrdev_remove(counter);
32 ida_free(&counter_ida, dev->id);
35 static struct device_type counter_device_type = {
36 .name = "counter_device",
37 .release = counter_device_release,
40 static struct bus_type counter_bus_type = {
42 .dev_name = "counter",
45 static dev_t counter_devt;
48 * counter_register - register Counter to the system
49 * @counter: pointer to Counter to register
51 * This function registers a Counter to the system. A sysfs "counter" directory
52 * will be created and populated with sysfs attributes correlating with the
53 * Counter Signals, Synapses, and Counts respectively.
56 * 0 on success, negative error number on failure.
58 int counter_register(struct counter_device *const counter)
60 struct device *const dev = &counter->dev;
64 /* Acquire unique ID */
65 id = ida_alloc(&counter_ida, GFP_KERNEL);
69 mutex_init(&counter->ops_exist_lock);
71 /* Configure device structure for Counter */
73 dev->type = &counter_device_type;
74 dev->bus = &counter_bus_type;
75 dev->devt = MKDEV(MAJOR(counter_devt), id);
76 if (counter->parent) {
77 dev->parent = counter->parent;
78 dev->of_node = counter->parent->of_node;
80 device_initialize(dev);
81 dev_set_drvdata(dev, counter);
83 err = counter_sysfs_add(counter);
87 err = counter_chrdev_add(counter);
91 err = cdev_device_add(&counter->chrdev, dev);
93 goto err_remove_chrdev;
98 counter_chrdev_remove(counter);
103 EXPORT_SYMBOL_GPL(counter_register);
106 * counter_unregister - unregister Counter from the system
107 * @counter: pointer to Counter to unregister
109 * The Counter is unregistered from the system.
111 void counter_unregister(struct counter_device *const counter)
116 cdev_device_del(&counter->chrdev, &counter->dev);
118 mutex_lock(&counter->ops_exist_lock);
121 wake_up(&counter->events_wait);
123 mutex_unlock(&counter->ops_exist_lock);
125 put_device(&counter->dev);
127 EXPORT_SYMBOL_GPL(counter_unregister);
129 static void devm_counter_release(void *counter)
131 counter_unregister(counter);
135 * devm_counter_register - Resource-managed counter_register
136 * @dev: device to allocate counter_device for
137 * @counter: pointer to Counter to register
139 * Managed counter_register. The Counter registered with this function is
140 * automatically unregistered on driver detach. This function calls
141 * counter_register internally. Refer to that function for more information.
144 * 0 on success, negative error number on failure.
146 int devm_counter_register(struct device *dev,
147 struct counter_device *const counter)
151 err = counter_register(counter);
155 return devm_add_action_or_reset(dev, devm_counter_release, counter);
157 EXPORT_SYMBOL_GPL(devm_counter_register);
159 #define COUNTER_DEV_MAX 256
161 static int __init counter_init(void)
165 err = bus_register(&counter_bus_type);
169 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX, "counter");
171 goto err_unregister_bus;
176 bus_unregister(&counter_bus_type);
180 static void __exit counter_exit(void)
182 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
183 bus_unregister(&counter_bus_type);
186 subsys_initcall(counter_init);
187 module_exit(counter_exit);
189 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
190 MODULE_DESCRIPTION("Generic Counter interface");
191 MODULE_LICENSE("GPL v2");