1*e62d9491SThomas 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++); 277589670fSShannon Nelson if (IS_ERR(cd)) 287589670fSShannon Nelson return PTR_ERR(cd); 297589670fSShannon Nelson return 0; 307589670fSShannon Nelson } 317589670fSShannon Nelson 327589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 337589670fSShannon Nelson { 34765cdb6cSKay Sievers device_destroy(dca_class, MKDEV(0, slot + 1)); 357589670fSShannon Nelson } 367589670fSShannon Nelson 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 54a9b12619SGreg Kroah-Hartman 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 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 747589670fSShannon Nelson int __init dca_sysfs_init(void) 757589670fSShannon Nelson { 767589670fSShannon Nelson idr_init(&dca_idr); 777589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 787589670fSShannon Nelson 797589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 807589670fSShannon Nelson if (IS_ERR(dca_class)) { 817589670fSShannon Nelson idr_destroy(&dca_idr); 827589670fSShannon Nelson return PTR_ERR(dca_class); 837589670fSShannon Nelson } 847589670fSShannon Nelson return 0; 857589670fSShannon Nelson } 867589670fSShannon Nelson 877589670fSShannon Nelson void __exit dca_sysfs_exit(void) 887589670fSShannon Nelson { 897589670fSShannon Nelson class_destroy(dca_class); 907589670fSShannon Nelson idr_destroy(&dca_idr); 917589670fSShannon Nelson } 927589670fSShannon Nelson 93