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 { 15765cdb6cSKay Sievers struct device *cd; 16*7f1b358aSMaciej Sosnowski static int req_count; 177589670fSShannon Nelson 18765cdb6cSKay Sievers cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), 19*7f1b358aSMaciej Sosnowski "requester%d", req_count++); 207589670fSShannon Nelson if (IS_ERR(cd)) 217589670fSShannon Nelson return PTR_ERR(cd); 227589670fSShannon Nelson return 0; 237589670fSShannon Nelson } 247589670fSShannon Nelson 257589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 267589670fSShannon Nelson { 27765cdb6cSKay Sievers device_destroy(dca_class, MKDEV(0, slot + 1)); 287589670fSShannon Nelson } 297589670fSShannon Nelson 307589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) 317589670fSShannon Nelson { 32765cdb6cSKay Sievers struct device *cd; 337589670fSShannon Nelson int err = 0; 347589670fSShannon Nelson 357589670fSShannon Nelson idr_try_again: 367589670fSShannon Nelson if (!idr_pre_get(&dca_idr, GFP_KERNEL)) 377589670fSShannon Nelson return -ENOMEM; 387589670fSShannon Nelson spin_lock(&dca_idr_lock); 397589670fSShannon Nelson err = idr_get_new(&dca_idr, dca, &dca->id); 407589670fSShannon Nelson spin_unlock(&dca_idr_lock); 417589670fSShannon Nelson switch (err) { 427589670fSShannon Nelson case 0: 437589670fSShannon Nelson break; 447589670fSShannon Nelson case -EAGAIN: 457589670fSShannon Nelson goto idr_try_again; 467589670fSShannon Nelson default: 477589670fSShannon Nelson return err; 487589670fSShannon Nelson } 497589670fSShannon Nelson 50765cdb6cSKay Sievers cd = device_create(dca_class, dev, MKDEV(0, 0), "dca%d", dca->id); 517589670fSShannon Nelson if (IS_ERR(cd)) { 527589670fSShannon Nelson spin_lock(&dca_idr_lock); 537589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 547589670fSShannon Nelson spin_unlock(&dca_idr_lock); 557589670fSShannon Nelson return PTR_ERR(cd); 567589670fSShannon Nelson } 577589670fSShannon Nelson dca->cd = cd; 587589670fSShannon Nelson return 0; 597589670fSShannon Nelson } 607589670fSShannon Nelson 617589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 627589670fSShannon Nelson { 63765cdb6cSKay Sievers device_unregister(dca->cd); 647589670fSShannon Nelson dca->cd = NULL; 657589670fSShannon Nelson spin_lock(&dca_idr_lock); 667589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 677589670fSShannon Nelson spin_unlock(&dca_idr_lock); 687589670fSShannon Nelson } 697589670fSShannon Nelson 707589670fSShannon Nelson int __init dca_sysfs_init(void) 717589670fSShannon Nelson { 727589670fSShannon Nelson idr_init(&dca_idr); 737589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 747589670fSShannon Nelson 757589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 767589670fSShannon Nelson if (IS_ERR(dca_class)) { 777589670fSShannon Nelson idr_destroy(&dca_idr); 787589670fSShannon Nelson return PTR_ERR(dca_class); 797589670fSShannon Nelson } 807589670fSShannon Nelson return 0; 817589670fSShannon Nelson } 827589670fSShannon Nelson 837589670fSShannon Nelson void __exit dca_sysfs_exit(void) 847589670fSShannon Nelson { 857589670fSShannon Nelson class_destroy(dca_class); 867589670fSShannon Nelson idr_destroy(&dca_idr); 877589670fSShannon Nelson } 887589670fSShannon Nelson 89