1e62d9491SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e2fc4d19SMaciej Sosnowski /*
3e2fc4d19SMaciej Sosnowski * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4e2fc4d19SMaciej Sosnowski */
5e2fc4d19SMaciej Sosnowski
67589670fSShannon Nelson #include <linux/kernel.h>
77589670fSShannon Nelson #include <linux/spinlock.h>
87589670fSShannon Nelson #include <linux/device.h>
97589670fSShannon Nelson #include <linux/idr.h>
107589670fSShannon Nelson #include <linux/kdev_t.h>
117589670fSShannon Nelson #include <linux/err.h>
127589670fSShannon Nelson #include <linux/dca.h>
135a0e3ad6STejun Heo #include <linux/gfp.h>
143382416dSPaul Gortmaker #include <linux/export.h>
157589670fSShannon Nelson
16*d7d3ae44SIvan Orlov static const struct class dca_class = {
17*d7d3ae44SIvan Orlov .name = "dca",
18*d7d3ae44SIvan Orlov };
197589670fSShannon Nelson static struct idr dca_idr;
207589670fSShannon Nelson static spinlock_t dca_idr_lock;
217589670fSShannon Nelson
dca_sysfs_add_req(struct dca_provider * dca,struct device * dev,int slot)227589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
237589670fSShannon Nelson {
24765cdb6cSKay Sievers struct device *cd;
257f1b358aSMaciej Sosnowski static int req_count;
267589670fSShannon Nelson
27*d7d3ae44SIvan Orlov cd = device_create(&dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
287f1b358aSMaciej Sosnowski "requester%d", req_count++);
290548745fSTang Bin return PTR_ERR_OR_ZERO(cd);
307589670fSShannon Nelson }
317589670fSShannon Nelson
dca_sysfs_remove_req(struct dca_provider * dca,int slot)327589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
337589670fSShannon Nelson {
34*d7d3ae44SIvan Orlov device_destroy(&dca_class, MKDEV(0, slot + 1));
357589670fSShannon Nelson }
367589670fSShannon Nelson
dca_sysfs_add_provider(struct dca_provider * dca,struct device * dev)377589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
387589670fSShannon Nelson {
39765cdb6cSKay Sievers struct device *cd;
40615f2e5cSTejun Heo int ret;
417589670fSShannon Nelson
42615f2e5cSTejun Heo idr_preload(GFP_KERNEL);
437589670fSShannon Nelson spin_lock(&dca_idr_lock);
44615f2e5cSTejun Heo
45615f2e5cSTejun Heo ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
46615f2e5cSTejun Heo if (ret >= 0)
47615f2e5cSTejun Heo dca->id = ret;
48615f2e5cSTejun Heo
497589670fSShannon Nelson spin_unlock(&dca_idr_lock);
50615f2e5cSTejun Heo idr_preload_end();
51615f2e5cSTejun Heo if (ret < 0)
52615f2e5cSTejun Heo return ret;
537589670fSShannon Nelson
54*d7d3ae44SIvan Orlov cd = device_create(&dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
557589670fSShannon Nelson if (IS_ERR(cd)) {
567589670fSShannon Nelson spin_lock(&dca_idr_lock);
577589670fSShannon Nelson idr_remove(&dca_idr, dca->id);
587589670fSShannon Nelson spin_unlock(&dca_idr_lock);
597589670fSShannon Nelson return PTR_ERR(cd);
607589670fSShannon Nelson }
617589670fSShannon Nelson dca->cd = cd;
627589670fSShannon Nelson return 0;
637589670fSShannon Nelson }
647589670fSShannon Nelson
dca_sysfs_remove_provider(struct dca_provider * dca)657589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca)
667589670fSShannon Nelson {
67765cdb6cSKay Sievers device_unregister(dca->cd);
687589670fSShannon Nelson dca->cd = NULL;
697589670fSShannon Nelson spin_lock(&dca_idr_lock);
707589670fSShannon Nelson idr_remove(&dca_idr, dca->id);
717589670fSShannon Nelson spin_unlock(&dca_idr_lock);
727589670fSShannon Nelson }
737589670fSShannon Nelson
dca_sysfs_init(void)747589670fSShannon Nelson int __init dca_sysfs_init(void)
757589670fSShannon Nelson {
76*d7d3ae44SIvan Orlov int err;
77*d7d3ae44SIvan Orlov
787589670fSShannon Nelson idr_init(&dca_idr);
797589670fSShannon Nelson spin_lock_init(&dca_idr_lock);
807589670fSShannon Nelson
81*d7d3ae44SIvan Orlov err = class_register(&dca_class);
82*d7d3ae44SIvan Orlov if (err) {
837589670fSShannon Nelson idr_destroy(&dca_idr);
84*d7d3ae44SIvan Orlov return err;
857589670fSShannon Nelson }
867589670fSShannon Nelson return 0;
877589670fSShannon Nelson }
887589670fSShannon Nelson
dca_sysfs_exit(void)897589670fSShannon Nelson void __exit dca_sysfs_exit(void)
907589670fSShannon Nelson {
91*d7d3ae44SIvan Orlov class_unregister(&dca_class);
927589670fSShannon Nelson idr_destroy(&dca_idr);
937589670fSShannon Nelson }
947589670fSShannon Nelson
95