blob: eb25627b059dc3dbcbdf48ddedbdc50614bf1228 [file] [log] [blame]
Thomas Gleixnere62d9492019-05-20 19:07:58 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Maciej Sosnowskie2fc4d12009-03-21 13:31:23 -07002/*
3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Maciej Sosnowskie2fc4d12009-03-21 13:31:23 -07004 */
5
Shannon Nelson75896702007-10-16 01:27:41 -07006#include <linux/kernel.h>
7#include <linux/spinlock.h>
8#include <linux/device.h>
9#include <linux/idr.h>
10#include <linux/kdev_t.h>
11#include <linux/err.h>
12#include <linux/dca.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Paul Gortmaker33824162011-07-10 12:15:51 -040014#include <linux/export.h>
Shannon Nelson75896702007-10-16 01:27:41 -070015
16static struct class *dca_class;
17static struct idr dca_idr;
18static spinlock_t dca_idr_lock;
19
20int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
21{
Kay Sievers765cdb62008-02-08 15:00:49 -080022 struct device *cd;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070023 static int req_count;
Shannon Nelson75896702007-10-16 01:27:41 -070024
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070025 cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
26 "requester%d", req_count++);
Shannon Nelson75896702007-10-16 01:27:41 -070027 if (IS_ERR(cd))
28 return PTR_ERR(cd);
29 return 0;
30}
31
32void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
33{
Kay Sievers765cdb62008-02-08 15:00:49 -080034 device_destroy(dca_class, MKDEV(0, slot + 1));
Shannon Nelson75896702007-10-16 01:27:41 -070035}
36
37int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
38{
Kay Sievers765cdb62008-02-08 15:00:49 -080039 struct device *cd;
Tejun Heo615f2e52013-02-27 17:04:02 -080040 int ret;
Shannon Nelson75896702007-10-16 01:27:41 -070041
Tejun Heo615f2e52013-02-27 17:04:02 -080042 idr_preload(GFP_KERNEL);
Shannon Nelson75896702007-10-16 01:27:41 -070043 spin_lock(&dca_idr_lock);
Tejun Heo615f2e52013-02-27 17:04:02 -080044
45 ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
46 if (ret >= 0)
47 dca->id = ret;
48
Shannon Nelson75896702007-10-16 01:27:41 -070049 spin_unlock(&dca_idr_lock);
Tejun Heo615f2e52013-02-27 17:04:02 -080050 idr_preload_end();
51 if (ret < 0)
52 return ret;
Shannon Nelson75896702007-10-16 01:27:41 -070053
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070054 cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
Shannon Nelson75896702007-10-16 01:27:41 -070055 if (IS_ERR(cd)) {
56 spin_lock(&dca_idr_lock);
57 idr_remove(&dca_idr, dca->id);
58 spin_unlock(&dca_idr_lock);
59 return PTR_ERR(cd);
60 }
61 dca->cd = cd;
62 return 0;
63}
64
65void dca_sysfs_remove_provider(struct dca_provider *dca)
66{
Kay Sievers765cdb62008-02-08 15:00:49 -080067 device_unregister(dca->cd);
Shannon Nelson75896702007-10-16 01:27:41 -070068 dca->cd = NULL;
69 spin_lock(&dca_idr_lock);
70 idr_remove(&dca_idr, dca->id);
71 spin_unlock(&dca_idr_lock);
72}
73
74int __init dca_sysfs_init(void)
75{
76 idr_init(&dca_idr);
77 spin_lock_init(&dca_idr_lock);
78
79 dca_class = class_create(THIS_MODULE, "dca");
80 if (IS_ERR(dca_class)) {
81 idr_destroy(&dca_idr);
82 return PTR_ERR(dca_class);
83 }
84 return 0;
85}
86
87void __exit dca_sysfs_exit(void)
88{
89 class_destroy(dca_class);
90 idr_destroy(&dca_idr);
91}
92