1*7589670fSShannon Nelson #include <linux/kernel.h> 2*7589670fSShannon Nelson #include <linux/spinlock.h> 3*7589670fSShannon Nelson #include <linux/device.h> 4*7589670fSShannon Nelson #include <linux/idr.h> 5*7589670fSShannon Nelson #include <linux/kdev_t.h> 6*7589670fSShannon Nelson #include <linux/err.h> 7*7589670fSShannon Nelson #include <linux/dca.h> 8*7589670fSShannon Nelson 9*7589670fSShannon Nelson static struct class *dca_class; 10*7589670fSShannon Nelson static struct idr dca_idr; 11*7589670fSShannon Nelson static spinlock_t dca_idr_lock; 12*7589670fSShannon Nelson 13*7589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot) 14*7589670fSShannon Nelson { 15*7589670fSShannon Nelson struct class_device *cd; 16*7589670fSShannon Nelson 17*7589670fSShannon Nelson cd = class_device_create(dca_class, dca->cd, MKDEV(0, slot + 1), 18*7589670fSShannon Nelson dev, "requester%d", slot); 19*7589670fSShannon Nelson if (IS_ERR(cd)) 20*7589670fSShannon Nelson return PTR_ERR(cd); 21*7589670fSShannon Nelson return 0; 22*7589670fSShannon Nelson } 23*7589670fSShannon Nelson 24*7589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 25*7589670fSShannon Nelson { 26*7589670fSShannon Nelson class_device_destroy(dca_class, MKDEV(0, slot + 1)); 27*7589670fSShannon Nelson } 28*7589670fSShannon Nelson 29*7589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) 30*7589670fSShannon Nelson { 31*7589670fSShannon Nelson struct class_device *cd; 32*7589670fSShannon Nelson int err = 0; 33*7589670fSShannon Nelson 34*7589670fSShannon Nelson idr_try_again: 35*7589670fSShannon Nelson if (!idr_pre_get(&dca_idr, GFP_KERNEL)) 36*7589670fSShannon Nelson return -ENOMEM; 37*7589670fSShannon Nelson spin_lock(&dca_idr_lock); 38*7589670fSShannon Nelson err = idr_get_new(&dca_idr, dca, &dca->id); 39*7589670fSShannon Nelson spin_unlock(&dca_idr_lock); 40*7589670fSShannon Nelson switch (err) { 41*7589670fSShannon Nelson case 0: 42*7589670fSShannon Nelson break; 43*7589670fSShannon Nelson case -EAGAIN: 44*7589670fSShannon Nelson goto idr_try_again; 45*7589670fSShannon Nelson default: 46*7589670fSShannon Nelson return err; 47*7589670fSShannon Nelson } 48*7589670fSShannon Nelson 49*7589670fSShannon Nelson cd = class_device_create(dca_class, NULL, MKDEV(0, 0), 50*7589670fSShannon Nelson dev, "dca%d", dca->id); 51*7589670fSShannon Nelson if (IS_ERR(cd)) { 52*7589670fSShannon Nelson spin_lock(&dca_idr_lock); 53*7589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 54*7589670fSShannon Nelson spin_unlock(&dca_idr_lock); 55*7589670fSShannon Nelson return PTR_ERR(cd); 56*7589670fSShannon Nelson } 57*7589670fSShannon Nelson dca->cd = cd; 58*7589670fSShannon Nelson return 0; 59*7589670fSShannon Nelson } 60*7589670fSShannon Nelson 61*7589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 62*7589670fSShannon Nelson { 63*7589670fSShannon Nelson class_device_unregister(dca->cd); 64*7589670fSShannon Nelson dca->cd = NULL; 65*7589670fSShannon Nelson spin_lock(&dca_idr_lock); 66*7589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 67*7589670fSShannon Nelson spin_unlock(&dca_idr_lock); 68*7589670fSShannon Nelson } 69*7589670fSShannon Nelson 70*7589670fSShannon Nelson int __init dca_sysfs_init(void) 71*7589670fSShannon Nelson { 72*7589670fSShannon Nelson idr_init(&dca_idr); 73*7589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 74*7589670fSShannon Nelson 75*7589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 76*7589670fSShannon Nelson if (IS_ERR(dca_class)) { 77*7589670fSShannon Nelson idr_destroy(&dca_idr); 78*7589670fSShannon Nelson return PTR_ERR(dca_class); 79*7589670fSShannon Nelson } 80*7589670fSShannon Nelson return 0; 81*7589670fSShannon Nelson } 82*7589670fSShannon Nelson 83*7589670fSShannon Nelson void __exit dca_sysfs_exit(void) 84*7589670fSShannon Nelson { 85*7589670fSShannon Nelson class_destroy(dca_class); 86*7589670fSShannon Nelson idr_destroy(&dca_idr); 87*7589670fSShannon Nelson } 88*7589670fSShannon Nelson 89