xref: /linux/drivers/dca/dca-sysfs.c (revision 0548745fa335a4665b039b9fd8e121339932c24b)
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 
167589670fSShannon Nelson static struct class *dca_class;
177589670fSShannon Nelson static struct idr dca_idr;
187589670fSShannon Nelson static spinlock_t dca_idr_lock;
197589670fSShannon Nelson 
207589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
217589670fSShannon Nelson {
22765cdb6cSKay Sievers 	struct device *cd;
237f1b358aSMaciej Sosnowski 	static int req_count;
247589670fSShannon Nelson 
25a9b12619SGreg Kroah-Hartman 	cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
267f1b358aSMaciej Sosnowski 			   "requester%d", req_count++);
27*0548745fSTang Bin 	return PTR_ERR_OR_ZERO(cd);
287589670fSShannon Nelson }
297589670fSShannon Nelson 
307589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
317589670fSShannon Nelson {
32765cdb6cSKay Sievers 	device_destroy(dca_class, MKDEV(0, slot + 1));
337589670fSShannon Nelson }
347589670fSShannon Nelson 
357589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
367589670fSShannon Nelson {
37765cdb6cSKay Sievers 	struct device *cd;
38615f2e5cSTejun Heo 	int ret;
397589670fSShannon Nelson 
40615f2e5cSTejun Heo 	idr_preload(GFP_KERNEL);
417589670fSShannon Nelson 	spin_lock(&dca_idr_lock);
42615f2e5cSTejun Heo 
43615f2e5cSTejun Heo 	ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
44615f2e5cSTejun Heo 	if (ret >= 0)
45615f2e5cSTejun Heo 		dca->id = ret;
46615f2e5cSTejun Heo 
477589670fSShannon Nelson 	spin_unlock(&dca_idr_lock);
48615f2e5cSTejun Heo 	idr_preload_end();
49615f2e5cSTejun Heo 	if (ret < 0)
50615f2e5cSTejun Heo 		return ret;
517589670fSShannon Nelson 
52a9b12619SGreg Kroah-Hartman 	cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
537589670fSShannon Nelson 	if (IS_ERR(cd)) {
547589670fSShannon Nelson 		spin_lock(&dca_idr_lock);
557589670fSShannon Nelson 		idr_remove(&dca_idr, dca->id);
567589670fSShannon Nelson 		spin_unlock(&dca_idr_lock);
577589670fSShannon Nelson 		return PTR_ERR(cd);
587589670fSShannon Nelson 	}
597589670fSShannon Nelson 	dca->cd = cd;
607589670fSShannon Nelson 	return 0;
617589670fSShannon Nelson }
627589670fSShannon Nelson 
637589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca)
647589670fSShannon Nelson {
65765cdb6cSKay Sievers 	device_unregister(dca->cd);
667589670fSShannon Nelson 	dca->cd = NULL;
677589670fSShannon Nelson 	spin_lock(&dca_idr_lock);
687589670fSShannon Nelson 	idr_remove(&dca_idr, dca->id);
697589670fSShannon Nelson 	spin_unlock(&dca_idr_lock);
707589670fSShannon Nelson }
717589670fSShannon Nelson 
727589670fSShannon Nelson int __init dca_sysfs_init(void)
737589670fSShannon Nelson {
747589670fSShannon Nelson 	idr_init(&dca_idr);
757589670fSShannon Nelson 	spin_lock_init(&dca_idr_lock);
767589670fSShannon Nelson 
777589670fSShannon Nelson 	dca_class = class_create(THIS_MODULE, "dca");
787589670fSShannon Nelson 	if (IS_ERR(dca_class)) {
797589670fSShannon Nelson 		idr_destroy(&dca_idr);
807589670fSShannon Nelson 		return PTR_ERR(dca_class);
817589670fSShannon Nelson 	}
827589670fSShannon Nelson 	return 0;
837589670fSShannon Nelson }
847589670fSShannon Nelson 
857589670fSShannon Nelson void __exit dca_sysfs_exit(void)
867589670fSShannon Nelson {
877589670fSShannon Nelson 	class_destroy(dca_class);
887589670fSShannon Nelson 	idr_destroy(&dca_idr);
897589670fSShannon Nelson }
907589670fSShannon Nelson 
91