Linux 6.13-rc4
[linux.git] / drivers / dca / dca-sysfs.c
blobb915c2b4601e60e905d5f8c56d0e2393d5d542e9
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4 */
6 #include <linux/kernel.h>
7 #include <linux/spinlock.h>
8 #include <linux/device.h>
9 #include <linux/idr.h>
10 #include <linux/kdev_t.h>
11 #include <linux/err.h>
12 #include <linux/dca.h>
13 #include <linux/gfp.h>
14 #include <linux/export.h>
16 static const struct class dca_class = {
17 .name = "dca",
19 static struct idr dca_idr;
20 static spinlock_t dca_idr_lock;
22 int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
24 struct device *cd;
25 static int req_count;
27 cd = device_create(&dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
28 "requester%d", req_count++);
29 return PTR_ERR_OR_ZERO(cd);
32 void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
34 device_destroy(&dca_class, MKDEV(0, slot + 1));
37 int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
39 struct device *cd;
40 int ret;
42 idr_preload(GFP_KERNEL);
43 spin_lock(&dca_idr_lock);
45 ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
46 if (ret >= 0)
47 dca->id = ret;
49 spin_unlock(&dca_idr_lock);
50 idr_preload_end();
51 if (ret < 0)
52 return ret;
54 cd = device_create(&dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
55 if (IS_ERR(cd)) {
56 spin_lock(&dca_idr_lock);
57 idr_remove(&dca_idr, dca->id);
58 spin_unlock(&dca_idr_lock);
59 return PTR_ERR(cd);
61 dca->cd = cd;
62 return 0;
65 void dca_sysfs_remove_provider(struct dca_provider *dca)
67 device_unregister(dca->cd);
68 dca->cd = NULL;
69 spin_lock(&dca_idr_lock);
70 idr_remove(&dca_idr, dca->id);
71 spin_unlock(&dca_idr_lock);
74 int __init dca_sysfs_init(void)
76 int err;
78 idr_init(&dca_idr);
79 spin_lock_init(&dca_idr_lock);
81 err = class_register(&dca_class);
82 if (err) {
83 idr_destroy(&dca_idr);
84 return err;
86 return 0;
89 void __exit dca_sysfs_exit(void)
91 class_unregister(&dca_class);
92 idr_destroy(&dca_idr);