William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generic Counter interface |
| 4 | * Copyright (C) 2020 William Breathitt Gray |
| 5 | */ |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 6 | #include <linux/cdev.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 7 | #include <linux/counter.h> |
| 8 | #include <linux/device.h> |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 9 | #include <linux/device/bus.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 10 | #include <linux/export.h> |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 11 | #include <linux/fs.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
| 13 | #include <linux/idr.h> |
| 14 | #include <linux/init.h> |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 15 | #include <linux/kdev_t.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 16 | #include <linux/module.h> |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 18 | #include <linux/slab.h> |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/wait.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 21 | |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 22 | #include "counter-chrdev.h" |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 23 | #include "counter-sysfs.h" |
| 24 | |
| 25 | /* Provides a unique ID for each counter device */ |
| 26 | static DEFINE_IDA(counter_ida); |
| 27 | |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 28 | struct counter_device_allochelper { |
| 29 | struct counter_device counter; |
| 30 | |
| 31 | /* |
| 32 | * This is cache line aligned to ensure private data behaves like if it |
| 33 | * were kmalloced separately. |
| 34 | */ |
| 35 | unsigned long privdata[] ____cacheline_aligned; |
| 36 | }; |
| 37 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 38 | static void counter_device_release(struct device *dev) |
| 39 | { |
Uwe Kleine-König | b56346d | 2021-12-30 16:02:38 +0100 | [diff] [blame] | 40 | struct counter_device *const counter = |
| 41 | container_of(dev, struct counter_device, dev); |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 42 | |
| 43 | counter_chrdev_remove(counter); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 44 | ida_free(&counter_ida, dev->id); |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 45 | |
Uwe Kleine-König | f2ee475 | 2021-12-30 16:03:00 +0100 | [diff] [blame] | 46 | kfree(container_of(counter, struct counter_device_allochelper, counter)); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static struct device_type counter_device_type = { |
| 50 | .name = "counter_device", |
| 51 | .release = counter_device_release, |
| 52 | }; |
| 53 | |
| 54 | static struct bus_type counter_bus_type = { |
| 55 | .name = "counter", |
| 56 | .dev_name = "counter", |
| 57 | }; |
| 58 | |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 59 | static dev_t counter_devt; |
| 60 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 61 | /** |
Uwe Kleine-König | 5207fb2 | 2021-12-30 16:02:41 +0100 | [diff] [blame] | 62 | * counter_priv - access counter device private data |
| 63 | * @counter: counter device |
| 64 | * |
| 65 | * Get the counter device private data |
| 66 | */ |
| 67 | void *counter_priv(const struct counter_device *const counter) |
| 68 | { |
Uwe Kleine-König | f2ee475 | 2021-12-30 16:03:00 +0100 | [diff] [blame] | 69 | struct counter_device_allochelper *ch = |
| 70 | container_of(counter, struct counter_device_allochelper, counter); |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 71 | |
Uwe Kleine-König | f2ee475 | 2021-12-30 16:03:00 +0100 | [diff] [blame] | 72 | return &ch->privdata; |
Uwe Kleine-König | 5207fb2 | 2021-12-30 16:02:41 +0100 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL_GPL(counter_priv); |
| 75 | |
| 76 | /** |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 77 | * counter_alloc - allocate a counter_device |
| 78 | * @sizeof_priv: size of the driver private data |
| 79 | * |
| 80 | * This is part one of counter registration. The structure is allocated |
| 81 | * dynamically to ensure the right lifetime for the embedded struct device. |
| 82 | * |
| 83 | * If this succeeds, call counter_put() to get rid of the counter_device again. |
| 84 | */ |
| 85 | struct counter_device *counter_alloc(size_t sizeof_priv) |
| 86 | { |
| 87 | struct counter_device_allochelper *ch; |
| 88 | struct counter_device *counter; |
| 89 | struct device *dev; |
| 90 | int err; |
| 91 | |
| 92 | ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL); |
Dan Carpenter | fc55e63 | 2022-01-11 20:32:43 +0300 | [diff] [blame] | 93 | if (!ch) |
| 94 | return NULL; |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 95 | |
| 96 | counter = &ch->counter; |
| 97 | dev = &counter->dev; |
| 98 | |
| 99 | /* Acquire unique ID */ |
| 100 | err = ida_alloc(&counter_ida, GFP_KERNEL); |
| 101 | if (err < 0) |
| 102 | goto err_ida_alloc; |
| 103 | dev->id = err; |
| 104 | |
| 105 | mutex_init(&counter->ops_exist_lock); |
| 106 | dev->type = &counter_device_type; |
| 107 | dev->bus = &counter_bus_type; |
| 108 | dev->devt = MKDEV(MAJOR(counter_devt), dev->id); |
| 109 | |
| 110 | err = counter_chrdev_add(counter); |
| 111 | if (err < 0) |
| 112 | goto err_chrdev_add; |
| 113 | |
| 114 | device_initialize(dev); |
| 115 | |
| 116 | return counter; |
| 117 | |
| 118 | err_chrdev_add: |
| 119 | |
| 120 | ida_free(&counter_ida, dev->id); |
| 121 | err_ida_alloc: |
| 122 | |
| 123 | kfree(ch); |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 124 | |
Dan Carpenter | fc55e63 | 2022-01-11 20:32:43 +0300 | [diff] [blame] | 125 | return NULL; |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL_GPL(counter_alloc); |
| 128 | |
| 129 | void counter_put(struct counter_device *counter) |
| 130 | { |
| 131 | put_device(&counter->dev); |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(counter_put); |
| 134 | |
| 135 | /** |
| 136 | * counter_add - complete registration of a counter |
| 137 | * @counter: the counter to add |
| 138 | * |
| 139 | * This is part two of counter registration. |
| 140 | * |
| 141 | * If this succeeds, call counter_unregister() to get rid of the counter_device again. |
| 142 | */ |
| 143 | int counter_add(struct counter_device *counter) |
| 144 | { |
| 145 | int err; |
| 146 | struct device *dev = &counter->dev; |
| 147 | |
| 148 | if (counter->parent) { |
| 149 | dev->parent = counter->parent; |
| 150 | dev->of_node = counter->parent->of_node; |
| 151 | } |
| 152 | |
| 153 | err = counter_sysfs_add(counter); |
| 154 | if (err < 0) |
| 155 | return err; |
| 156 | |
| 157 | /* implies device_add(dev) */ |
| 158 | return cdev_device_add(&counter->chrdev, dev); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(counter_add); |
| 161 | |
| 162 | /** |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 163 | * counter_unregister - unregister Counter from the system |
| 164 | * @counter: pointer to Counter to unregister |
| 165 | * |
| 166 | * The Counter is unregistered from the system. |
| 167 | */ |
| 168 | void counter_unregister(struct counter_device *const counter) |
| 169 | { |
| 170 | if (!counter) |
| 171 | return; |
| 172 | |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 173 | cdev_device_del(&counter->chrdev, &counter->dev); |
| 174 | |
| 175 | mutex_lock(&counter->ops_exist_lock); |
| 176 | |
| 177 | counter->ops = NULL; |
| 178 | wake_up(&counter->events_wait); |
| 179 | |
| 180 | mutex_unlock(&counter->ops_exist_lock); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL_GPL(counter_unregister); |
| 183 | |
| 184 | static void devm_counter_release(void *counter) |
| 185 | { |
| 186 | counter_unregister(counter); |
| 187 | } |
| 188 | |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 189 | static void devm_counter_put(void *counter) |
| 190 | { |
| 191 | counter_put(counter); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * devm_counter_alloc - allocate a counter_device |
| 196 | * @dev: the device to register the release callback for |
| 197 | * @sizeof_priv: size of the driver private data |
| 198 | * |
| 199 | * This is the device managed version of counter_add(). It registers a cleanup |
| 200 | * callback to care for calling counter_put(). |
| 201 | */ |
| 202 | struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv) |
| 203 | { |
| 204 | struct counter_device *counter; |
| 205 | int err; |
| 206 | |
| 207 | counter = counter_alloc(sizeof_priv); |
Dan Carpenter | fc55e63 | 2022-01-11 20:32:43 +0300 | [diff] [blame] | 208 | if (!counter) |
| 209 | return NULL; |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 210 | |
| 211 | err = devm_add_action_or_reset(dev, devm_counter_put, counter); |
| 212 | if (err < 0) |
Dan Carpenter | fc55e63 | 2022-01-11 20:32:43 +0300 | [diff] [blame] | 213 | return NULL; |
Uwe Kleine-König | c18e276 | 2021-12-30 16:02:50 +0100 | [diff] [blame] | 214 | |
| 215 | return counter; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(devm_counter_alloc); |
| 218 | |
| 219 | /** |
| 220 | * devm_counter_add - complete registration of a counter |
| 221 | * @dev: the device to register the release callback for |
| 222 | * @counter: the counter to add |
| 223 | * |
| 224 | * This is the device managed version of counter_add(). It registers a cleanup |
| 225 | * callback to care for calling counter_unregister(). |
| 226 | */ |
| 227 | int devm_counter_add(struct device *dev, |
| 228 | struct counter_device *const counter) |
| 229 | { |
| 230 | int err; |
| 231 | |
| 232 | err = counter_add(counter); |
| 233 | if (err < 0) |
| 234 | return err; |
| 235 | |
| 236 | return devm_add_action_or_reset(dev, devm_counter_release, counter); |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(devm_counter_add); |
| 239 | |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 240 | #define COUNTER_DEV_MAX 256 |
| 241 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 242 | static int __init counter_init(void) |
| 243 | { |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 244 | int err; |
| 245 | |
| 246 | err = bus_register(&counter_bus_type); |
| 247 | if (err < 0) |
| 248 | return err; |
| 249 | |
| 250 | err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX, "counter"); |
| 251 | if (err < 0) |
| 252 | goto err_unregister_bus; |
| 253 | |
| 254 | return 0; |
| 255 | |
| 256 | err_unregister_bus: |
| 257 | bus_unregister(&counter_bus_type); |
| 258 | return err; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void __exit counter_exit(void) |
| 262 | { |
William Breathitt Gray | b6c50af | 2021-09-29 12:15:59 +0900 | [diff] [blame] | 263 | unregister_chrdev_region(counter_devt, COUNTER_DEV_MAX); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 264 | bus_unregister(&counter_bus_type); |
| 265 | } |
| 266 | |
| 267 | subsys_initcall(counter_init); |
| 268 | module_exit(counter_exit); |
| 269 | |
| 270 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 271 | MODULE_DESCRIPTION("Generic Counter interface"); |
| 272 | MODULE_LICENSE("GPL v2"); |