xref: /linux/drivers/dca/dca-sysfs.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
1e2fc4d19SMaciej Sosnowski /*
2e2fc4d19SMaciej Sosnowski  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
3e2fc4d19SMaciej Sosnowski  *
4e2fc4d19SMaciej Sosnowski  * This program is free software; you can redistribute it and/or modify it
5e2fc4d19SMaciej Sosnowski  * under the terms of the GNU General Public License as published by the Free
6e2fc4d19SMaciej Sosnowski  * Software Foundation; either version 2 of the License, or (at your option)
7e2fc4d19SMaciej Sosnowski  * any later version.
8e2fc4d19SMaciej Sosnowski  *
9e2fc4d19SMaciej Sosnowski  * This program is distributed in the hope that it will be useful, but WITHOUT
10e2fc4d19SMaciej Sosnowski  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11e2fc4d19SMaciej Sosnowski  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12e2fc4d19SMaciej Sosnowski  * more details.
13e2fc4d19SMaciej Sosnowski  *
14e2fc4d19SMaciej Sosnowski  * You should have received a copy of the GNU General Public License along with
15e2fc4d19SMaciej Sosnowski  * this program; if not, write to the Free Software Foundation, Inc., 59
16e2fc4d19SMaciej Sosnowski  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17e2fc4d19SMaciej Sosnowski  *
18e2fc4d19SMaciej Sosnowski  * The full GNU General Public License is included in this distribution in the
19e2fc4d19SMaciej Sosnowski  * file called COPYING.
20e2fc4d19SMaciej Sosnowski  */
21e2fc4d19SMaciej Sosnowski 
227589670fSShannon Nelson #include <linux/kernel.h>
237589670fSShannon Nelson #include <linux/spinlock.h>
247589670fSShannon Nelson #include <linux/device.h>
257589670fSShannon Nelson #include <linux/idr.h>
267589670fSShannon Nelson #include <linux/kdev_t.h>
277589670fSShannon Nelson #include <linux/err.h>
287589670fSShannon Nelson #include <linux/dca.h>
29*5a0e3ad6STejun Heo #include <linux/gfp.h>
307589670fSShannon Nelson 
317589670fSShannon Nelson static struct class *dca_class;
327589670fSShannon Nelson static struct idr dca_idr;
337589670fSShannon Nelson static spinlock_t dca_idr_lock;
347589670fSShannon Nelson 
357589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
367589670fSShannon Nelson {
37765cdb6cSKay Sievers 	struct device *cd;
387f1b358aSMaciej Sosnowski 	static int req_count;
397589670fSShannon Nelson 
40a9b12619SGreg Kroah-Hartman 	cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
417f1b358aSMaciej Sosnowski 			   "requester%d", req_count++);
427589670fSShannon Nelson 	if (IS_ERR(cd))
437589670fSShannon Nelson 		return PTR_ERR(cd);
447589670fSShannon Nelson 	return 0;
457589670fSShannon Nelson }
467589670fSShannon Nelson 
477589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
487589670fSShannon Nelson {
49765cdb6cSKay Sievers 	device_destroy(dca_class, MKDEV(0, slot + 1));
507589670fSShannon Nelson }
517589670fSShannon Nelson 
527589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
537589670fSShannon Nelson {
54765cdb6cSKay Sievers 	struct device *cd;
557589670fSShannon Nelson 	int err = 0;
567589670fSShannon Nelson 
577589670fSShannon Nelson idr_try_again:
587589670fSShannon Nelson 	if (!idr_pre_get(&dca_idr, GFP_KERNEL))
597589670fSShannon Nelson 		return -ENOMEM;
607589670fSShannon Nelson 	spin_lock(&dca_idr_lock);
617589670fSShannon Nelson 	err = idr_get_new(&dca_idr, dca, &dca->id);
627589670fSShannon Nelson 	spin_unlock(&dca_idr_lock);
637589670fSShannon Nelson 	switch (err) {
647589670fSShannon Nelson 	case 0:
657589670fSShannon Nelson 		break;
667589670fSShannon Nelson 	case -EAGAIN:
677589670fSShannon Nelson 		goto idr_try_again;
687589670fSShannon Nelson 	default:
697589670fSShannon Nelson 		return err;
707589670fSShannon Nelson 	}
717589670fSShannon Nelson 
72a9b12619SGreg Kroah-Hartman 	cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
737589670fSShannon Nelson 	if (IS_ERR(cd)) {
747589670fSShannon Nelson 		spin_lock(&dca_idr_lock);
757589670fSShannon Nelson 		idr_remove(&dca_idr, dca->id);
767589670fSShannon Nelson 		spin_unlock(&dca_idr_lock);
777589670fSShannon Nelson 		return PTR_ERR(cd);
787589670fSShannon Nelson 	}
797589670fSShannon Nelson 	dca->cd = cd;
807589670fSShannon Nelson 	return 0;
817589670fSShannon Nelson }
827589670fSShannon Nelson 
837589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca)
847589670fSShannon Nelson {
85765cdb6cSKay Sievers 	device_unregister(dca->cd);
867589670fSShannon Nelson 	dca->cd = NULL;
877589670fSShannon Nelson 	spin_lock(&dca_idr_lock);
887589670fSShannon Nelson 	idr_remove(&dca_idr, dca->id);
897589670fSShannon Nelson 	spin_unlock(&dca_idr_lock);
907589670fSShannon Nelson }
917589670fSShannon Nelson 
927589670fSShannon Nelson int __init dca_sysfs_init(void)
937589670fSShannon Nelson {
947589670fSShannon Nelson 	idr_init(&dca_idr);
957589670fSShannon Nelson 	spin_lock_init(&dca_idr_lock);
967589670fSShannon Nelson 
977589670fSShannon Nelson 	dca_class = class_create(THIS_MODULE, "dca");
987589670fSShannon Nelson 	if (IS_ERR(dca_class)) {
997589670fSShannon Nelson 		idr_destroy(&dca_idr);
1007589670fSShannon Nelson 		return PTR_ERR(dca_class);
1017589670fSShannon Nelson 	}
1027589670fSShannon Nelson 	return 0;
1037589670fSShannon Nelson }
1047589670fSShannon Nelson 
1057589670fSShannon Nelson void __exit dca_sysfs_exit(void)
1067589670fSShannon Nelson {
1077589670fSShannon Nelson 	class_destroy(dca_class);
1087589670fSShannon Nelson 	idr_destroy(&dca_idr);
1097589670fSShannon Nelson }
1107589670fSShannon Nelson 
111