blob: 5acc54539623879b7d80900c54a75173a4627f69 [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>
18#include <linux/types.h>
19#include <linux/wait.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090020
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090021#include "counter-chrdev.h"
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090022#include "counter-sysfs.h"
23
24/* Provides a unique ID for each counter device */
25static DEFINE_IDA(counter_ida);
26
27static void counter_device_release(struct device *dev)
28{
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090029 struct counter_device *const counter = dev_get_drvdata(dev);
30
31 counter_chrdev_remove(counter);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090032 ida_free(&counter_ida, dev->id);
33}
34
35static struct device_type counter_device_type = {
36 .name = "counter_device",
37 .release = counter_device_release,
38};
39
40static struct bus_type counter_bus_type = {
41 .name = "counter",
42 .dev_name = "counter",
43};
44
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090045static dev_t counter_devt;
46
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090047/**
48 * counter_register - register Counter to the system
49 * @counter: pointer to Counter to register
50 *
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.
William Breathitt Gray712392f2021-08-27 12:47:48 +090054 *
55 * RETURNS:
56 * 0 on success, negative error number on failure.
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090057 */
58int counter_register(struct counter_device *const counter)
59{
60 struct device *const dev = &counter->dev;
61 int id;
62 int err;
63
64 /* Acquire unique ID */
65 id = ida_alloc(&counter_ida, GFP_KERNEL);
66 if (id < 0)
67 return id;
68
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090069 mutex_init(&counter->ops_exist_lock);
70
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090071 /* Configure device structure for Counter */
72 dev->id = id;
73 dev->type = &counter_device_type;
74 dev->bus = &counter_bus_type;
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090075 dev->devt = MKDEV(MAJOR(counter_devt), id);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090076 if (counter->parent) {
77 dev->parent = counter->parent;
78 dev->of_node = counter->parent->of_node;
79 }
80 device_initialize(dev);
81 dev_set_drvdata(dev, counter);
82
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090083 err = counter_sysfs_add(counter);
84 if (err < 0)
85 goto err_free_id;
86
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090087 err = counter_chrdev_add(counter);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090088 if (err < 0)
89 goto err_free_id;
90
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090091 err = cdev_device_add(&counter->chrdev, dev);
92 if (err < 0)
93 goto err_remove_chrdev;
94
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090095 return 0;
96
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090097err_remove_chrdev:
98 counter_chrdev_remove(counter);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090099err_free_id:
100 put_device(dev);
101 return err;
102}
103EXPORT_SYMBOL_GPL(counter_register);
104
105/**
106 * counter_unregister - unregister Counter from the system
107 * @counter: pointer to Counter to unregister
108 *
109 * The Counter is unregistered from the system.
110 */
111void counter_unregister(struct counter_device *const counter)
112{
113 if (!counter)
114 return;
115
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900116 cdev_device_del(&counter->chrdev, &counter->dev);
117
118 mutex_lock(&counter->ops_exist_lock);
119
120 counter->ops = NULL;
121 wake_up(&counter->events_wait);
122
123 mutex_unlock(&counter->ops_exist_lock);
124
125 put_device(&counter->dev);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900126}
127EXPORT_SYMBOL_GPL(counter_unregister);
128
129static void devm_counter_release(void *counter)
130{
131 counter_unregister(counter);
132}
133
134/**
135 * devm_counter_register - Resource-managed counter_register
136 * @dev: device to allocate counter_device for
137 * @counter: pointer to Counter to register
138 *
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.
142 *
143 * RETURNS:
144 * 0 on success, negative error number on failure.
145 */
146int devm_counter_register(struct device *dev,
147 struct counter_device *const counter)
148{
149 int err;
150
151 err = counter_register(counter);
152 if (err < 0)
153 return err;
154
155 return devm_add_action_or_reset(dev, devm_counter_release, counter);
156}
157EXPORT_SYMBOL_GPL(devm_counter_register);
158
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900159#define COUNTER_DEV_MAX 256
160
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900161static int __init counter_init(void)
162{
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900163 int err;
164
165 err = bus_register(&counter_bus_type);
166 if (err < 0)
167 return err;
168
169 err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX, "counter");
170 if (err < 0)
171 goto err_unregister_bus;
172
173 return 0;
174
175err_unregister_bus:
176 bus_unregister(&counter_bus_type);
177 return err;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900178}
179
180static void __exit counter_exit(void)
181{
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900182 unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900183 bus_unregister(&counter_bus_type);
184}
185
186subsys_initcall(counter_init);
187module_exit(counter_exit);
188
189MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
190MODULE_DESCRIPTION("Generic Counter interface");
191MODULE_LICENSE("GPL v2");