blob: bb538b9690e08186ae6c7d73240901b95b6b9ee3 [file] [log] [blame]
Shannon Nelson75896702007-10-16 01:27:41 -07001#include <linux/kernel.h>
2#include <linux/spinlock.h>
3#include <linux/device.h>
4#include <linux/idr.h>
5#include <linux/kdev_t.h>
6#include <linux/err.h>
7#include <linux/dca.h>
8
9static struct class *dca_class;
10static struct idr dca_idr;
11static spinlock_t dca_idr_lock;
12
13int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
14{
Kay Sievers765cdb62008-02-08 15:00:49 -080015 struct device *cd;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070016 static int req_count;
Shannon Nelson75896702007-10-16 01:27:41 -070017
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070018 cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
19 "requester%d", req_count++);
Shannon Nelson75896702007-10-16 01:27:41 -070020 if (IS_ERR(cd))
21 return PTR_ERR(cd);
22 return 0;
23}
24
25void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
26{
Kay Sievers765cdb62008-02-08 15:00:49 -080027 device_destroy(dca_class, MKDEV(0, slot + 1));
Shannon Nelson75896702007-10-16 01:27:41 -070028}
29
30int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
31{
Kay Sievers765cdb62008-02-08 15:00:49 -080032 struct device *cd;
Shannon Nelson75896702007-10-16 01:27:41 -070033 int err = 0;
34
35idr_try_again:
36 if (!idr_pre_get(&dca_idr, GFP_KERNEL))
37 return -ENOMEM;
38 spin_lock(&dca_idr_lock);
39 err = idr_get_new(&dca_idr, dca, &dca->id);
40 spin_unlock(&dca_idr_lock);
41 switch (err) {
42 case 0:
43 break;
44 case -EAGAIN:
45 goto idr_try_again;
46 default:
47 return err;
48 }
49
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070050 cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
Shannon Nelson75896702007-10-16 01:27:41 -070051 if (IS_ERR(cd)) {
52 spin_lock(&dca_idr_lock);
53 idr_remove(&dca_idr, dca->id);
54 spin_unlock(&dca_idr_lock);
55 return PTR_ERR(cd);
56 }
57 dca->cd = cd;
58 return 0;
59}
60
61void dca_sysfs_remove_provider(struct dca_provider *dca)
62{
Kay Sievers765cdb62008-02-08 15:00:49 -080063 device_unregister(dca->cd);
Shannon Nelson75896702007-10-16 01:27:41 -070064 dca->cd = NULL;
65 spin_lock(&dca_idr_lock);
66 idr_remove(&dca_idr, dca->id);
67 spin_unlock(&dca_idr_lock);
68}
69
70int __init dca_sysfs_init(void)
71{
72 idr_init(&dca_idr);
73 spin_lock_init(&dca_idr_lock);
74
75 dca_class = class_create(THIS_MODULE, "dca");
76 if (IS_ERR(dca_class)) {
77 idr_destroy(&dca_idr);
78 return PTR_ERR(dca_class);
79 }
80 return 0;
81}
82
83void __exit dca_sysfs_exit(void)
84{
85 class_destroy(dca_class);
86 idr_destroy(&dca_idr);
87}
88