17589670fSShannon Nelson #include <linux/kernel.h> 27589670fSShannon Nelson #include <linux/spinlock.h> 37589670fSShannon Nelson #include <linux/device.h> 47589670fSShannon Nelson #include <linux/idr.h> 57589670fSShannon Nelson #include <linux/kdev_t.h> 67589670fSShannon Nelson #include <linux/err.h> 77589670fSShannon Nelson #include <linux/dca.h> 87589670fSShannon Nelson 97589670fSShannon Nelson static struct class *dca_class; 107589670fSShannon Nelson static struct idr dca_idr; 117589670fSShannon Nelson static spinlock_t dca_idr_lock; 127589670fSShannon Nelson 137589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot) 147589670fSShannon Nelson { 15*765cdb6cSKay Sievers struct device *cd; 167589670fSShannon Nelson 17*765cdb6cSKay Sievers cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), 18*765cdb6cSKay Sievers "requester%d", slot); 197589670fSShannon Nelson if (IS_ERR(cd)) 207589670fSShannon Nelson return PTR_ERR(cd); 217589670fSShannon Nelson return 0; 227589670fSShannon Nelson } 237589670fSShannon Nelson 247589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 257589670fSShannon Nelson { 26*765cdb6cSKay Sievers device_destroy(dca_class, MKDEV(0, slot + 1)); 277589670fSShannon Nelson } 287589670fSShannon Nelson 297589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) 307589670fSShannon Nelson { 31*765cdb6cSKay Sievers struct device *cd; 327589670fSShannon Nelson int err = 0; 337589670fSShannon Nelson 347589670fSShannon Nelson idr_try_again: 357589670fSShannon Nelson if (!idr_pre_get(&dca_idr, GFP_KERNEL)) 367589670fSShannon Nelson return -ENOMEM; 377589670fSShannon Nelson spin_lock(&dca_idr_lock); 387589670fSShannon Nelson err = idr_get_new(&dca_idr, dca, &dca->id); 397589670fSShannon Nelson spin_unlock(&dca_idr_lock); 407589670fSShannon Nelson switch (err) { 417589670fSShannon Nelson case 0: 427589670fSShannon Nelson break; 437589670fSShannon Nelson case -EAGAIN: 447589670fSShannon Nelson goto idr_try_again; 457589670fSShannon Nelson default: 467589670fSShannon Nelson return err; 477589670fSShannon Nelson } 487589670fSShannon Nelson 49*765cdb6cSKay Sievers cd = device_create(dca_class, dev, MKDEV(0, 0), "dca%d", dca->id); 507589670fSShannon Nelson if (IS_ERR(cd)) { 517589670fSShannon Nelson spin_lock(&dca_idr_lock); 527589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 537589670fSShannon Nelson spin_unlock(&dca_idr_lock); 547589670fSShannon Nelson return PTR_ERR(cd); 557589670fSShannon Nelson } 567589670fSShannon Nelson dca->cd = cd; 577589670fSShannon Nelson return 0; 587589670fSShannon Nelson } 597589670fSShannon Nelson 607589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 617589670fSShannon Nelson { 62*765cdb6cSKay Sievers device_unregister(dca->cd); 637589670fSShannon Nelson dca->cd = NULL; 647589670fSShannon Nelson spin_lock(&dca_idr_lock); 657589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 667589670fSShannon Nelson spin_unlock(&dca_idr_lock); 677589670fSShannon Nelson } 687589670fSShannon Nelson 697589670fSShannon Nelson int __init dca_sysfs_init(void) 707589670fSShannon Nelson { 717589670fSShannon Nelson idr_init(&dca_idr); 727589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 737589670fSShannon Nelson 747589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 757589670fSShannon Nelson if (IS_ERR(dca_class)) { 767589670fSShannon Nelson idr_destroy(&dca_idr); 777589670fSShannon Nelson return PTR_ERR(dca_class); 787589670fSShannon Nelson } 797589670fSShannon Nelson return 0; 807589670fSShannon Nelson } 817589670fSShannon Nelson 827589670fSShannon Nelson void __exit dca_sysfs_exit(void) 837589670fSShannon Nelson { 847589670fSShannon Nelson class_destroy(dca_class); 857589670fSShannon Nelson idr_destroy(&dca_idr); 867589670fSShannon Nelson } 877589670fSShannon Nelson 88