0001
0002
0003
0004
0005
0006 #include <linux/kernel.h>
0007 #include <linux/spinlock.h>
0008 #include <linux/device.h>
0009 #include <linux/idr.h>
0010 #include <linux/kdev_t.h>
0011 #include <linux/err.h>
0012 #include <linux/dca.h>
0013 #include <linux/gfp.h>
0014 #include <linux/export.h>
0015
0016 static struct class *dca_class;
0017 static struct idr dca_idr;
0018 static spinlock_t dca_idr_lock;
0019
0020 int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
0021 {
0022 struct device *cd;
0023 static int req_count;
0024
0025 cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
0026 "requester%d", req_count++);
0027 return PTR_ERR_OR_ZERO(cd);
0028 }
0029
0030 void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
0031 {
0032 device_destroy(dca_class, MKDEV(0, slot + 1));
0033 }
0034
0035 int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
0036 {
0037 struct device *cd;
0038 int ret;
0039
0040 idr_preload(GFP_KERNEL);
0041 spin_lock(&dca_idr_lock);
0042
0043 ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
0044 if (ret >= 0)
0045 dca->id = ret;
0046
0047 spin_unlock(&dca_idr_lock);
0048 idr_preload_end();
0049 if (ret < 0)
0050 return ret;
0051
0052 cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
0053 if (IS_ERR(cd)) {
0054 spin_lock(&dca_idr_lock);
0055 idr_remove(&dca_idr, dca->id);
0056 spin_unlock(&dca_idr_lock);
0057 return PTR_ERR(cd);
0058 }
0059 dca->cd = cd;
0060 return 0;
0061 }
0062
0063 void dca_sysfs_remove_provider(struct dca_provider *dca)
0064 {
0065 device_unregister(dca->cd);
0066 dca->cd = NULL;
0067 spin_lock(&dca_idr_lock);
0068 idr_remove(&dca_idr, dca->id);
0069 spin_unlock(&dca_idr_lock);
0070 }
0071
0072 int __init dca_sysfs_init(void)
0073 {
0074 idr_init(&dca_idr);
0075 spin_lock_init(&dca_idr_lock);
0076
0077 dca_class = class_create(THIS_MODULE, "dca");
0078 if (IS_ERR(dca_class)) {
0079 idr_destroy(&dca_idr);
0080 return PTR_ERR(dca_class);
0081 }
0082 return 0;
0083 }
0084
0085 void __exit dca_sysfs_exit(void)
0086 {
0087 class_destroy(dca_class);
0088 idr_destroy(&dca_idr);
0089 }
0090