blob: 591b6597c00a68eee807559bb1237727a2f2e14a [file] [log] [blame]
Maciej Sosnowskie2fc4d12009-03-21 13:31:23 -07001/*
2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
20 */
21
Shannon Nelson75896702007-10-16 01:27:41 -070022#include <linux/kernel.h>
23#include <linux/spinlock.h>
24#include <linux/device.h>
25#include <linux/idr.h>
26#include <linux/kdev_t.h>
27#include <linux/err.h>
28#include <linux/dca.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Paul Gortmaker33824162011-07-10 12:15:51 -040030#include <linux/export.h>
Shannon Nelson75896702007-10-16 01:27:41 -070031
32static struct class *dca_class;
33static struct idr dca_idr;
34static spinlock_t dca_idr_lock;
35
36int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
37{
Kay Sievers765cdb62008-02-08 15:00:49 -080038 struct device *cd;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070039 static int req_count;
Shannon Nelson75896702007-10-16 01:27:41 -070040
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070041 cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
42 "requester%d", req_count++);
Shannon Nelson75896702007-10-16 01:27:41 -070043 if (IS_ERR(cd))
44 return PTR_ERR(cd);
45 return 0;
46}
47
48void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
49{
Kay Sievers765cdb62008-02-08 15:00:49 -080050 device_destroy(dca_class, MKDEV(0, slot + 1));
Shannon Nelson75896702007-10-16 01:27:41 -070051}
52
53int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
54{
Kay Sievers765cdb62008-02-08 15:00:49 -080055 struct device *cd;
Shannon Nelson75896702007-10-16 01:27:41 -070056 int err = 0;
57
58idr_try_again:
59 if (!idr_pre_get(&dca_idr, GFP_KERNEL))
60 return -ENOMEM;
61 spin_lock(&dca_idr_lock);
62 err = idr_get_new(&dca_idr, dca, &dca->id);
63 spin_unlock(&dca_idr_lock);
64 switch (err) {
65 case 0:
66 break;
67 case -EAGAIN:
68 goto idr_try_again;
69 default:
70 return err;
71 }
72
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070073 cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
Shannon Nelson75896702007-10-16 01:27:41 -070074 if (IS_ERR(cd)) {
75 spin_lock(&dca_idr_lock);
76 idr_remove(&dca_idr, dca->id);
77 spin_unlock(&dca_idr_lock);
78 return PTR_ERR(cd);
79 }
80 dca->cd = cd;
81 return 0;
82}
83
84void dca_sysfs_remove_provider(struct dca_provider *dca)
85{
Kay Sievers765cdb62008-02-08 15:00:49 -080086 device_unregister(dca->cd);
Shannon Nelson75896702007-10-16 01:27:41 -070087 dca->cd = NULL;
88 spin_lock(&dca_idr_lock);
89 idr_remove(&dca_idr, dca->id);
90 spin_unlock(&dca_idr_lock);
91}
92
93int __init dca_sysfs_init(void)
94{
95 idr_init(&dca_idr);
96 spin_lock_init(&dca_idr_lock);
97
98 dca_class = class_create(THIS_MODULE, "dca");
99 if (IS_ERR(dca_class)) {
100 idr_destroy(&dca_idr);
101 return PTR_ERR(dca_class);
102 }
103 return 0;
104}
105
106void __exit dca_sysfs_exit(void)
107{
108 class_destroy(dca_class);
109 idr_destroy(&dca_idr);
110}
111