Linux Audio

Check our new training course

Loading...
v6.13.7
 1// SPDX-License-Identifier: GPL-2.0-or-later
 2/*
 3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 4 */
 5
 6#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>
13#include <linux/gfp.h>
14#include <linux/export.h>
15
16static const struct class dca_class = {
17	.name = "dca",
18};
19static struct idr dca_idr;
20static spinlock_t dca_idr_lock;
21
22int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
23{
24	struct device *cd;
25	static int req_count;
26
27	cd = device_create(&dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
28			   "requester%d", req_count++);
29	return PTR_ERR_OR_ZERO(cd);
 
 
30}
31
32void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
33{
34	device_destroy(&dca_class, MKDEV(0, slot + 1));
35}
36
37int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
38{
39	struct device *cd;
40	int ret;
41
42	idr_preload(GFP_KERNEL);
 
 
43	spin_lock(&dca_idr_lock);
44
45	ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
46	if (ret >= 0)
47		dca->id = ret;
48
49	spin_unlock(&dca_idr_lock);
50	idr_preload_end();
51	if (ret < 0)
52		return ret;
 
 
 
 
 
53
54	cd = device_create(&dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
55	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{
67	device_unregister(dca->cd);
68	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	int err;
77
78	idr_init(&dca_idr);
79	spin_lock_init(&dca_idr_lock);
80
81	err = class_register(&dca_class);
82	if (err) {
83		idr_destroy(&dca_idr);
84		return err;
85	}
86	return 0;
87}
88
89void __exit dca_sysfs_exit(void)
90{
91	class_unregister(&dca_class);
92	idr_destroy(&dca_idr);
93}
94
v3.1
 
  1/*
  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
 22#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>
 29#include <linux/gfp.h>
 
 30
 31static struct class *dca_class;
 
 
 32static struct idr dca_idr;
 33static spinlock_t dca_idr_lock;
 34
 35int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
 36{
 37	struct device *cd;
 38	static int req_count;
 39
 40	cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
 41			   "requester%d", req_count++);
 42	if (IS_ERR(cd))
 43		return PTR_ERR(cd);
 44	return 0;
 45}
 46
 47void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
 48{
 49	device_destroy(dca_class, MKDEV(0, slot + 1));
 50}
 51
 52int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
 53{
 54	struct device *cd;
 55	int err = 0;
 56
 57idr_try_again:
 58	if (!idr_pre_get(&dca_idr, GFP_KERNEL))
 59		return -ENOMEM;
 60	spin_lock(&dca_idr_lock);
 61	err = idr_get_new(&dca_idr, dca, &dca->id);
 
 
 
 
 62	spin_unlock(&dca_idr_lock);
 63	switch (err) {
 64	case 0:
 65		break;
 66	case -EAGAIN:
 67		goto idr_try_again;
 68	default:
 69		return err;
 70	}
 71
 72	cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
 73	if (IS_ERR(cd)) {
 74		spin_lock(&dca_idr_lock);
 75		idr_remove(&dca_idr, dca->id);
 76		spin_unlock(&dca_idr_lock);
 77		return PTR_ERR(cd);
 78	}
 79	dca->cd = cd;
 80	return 0;
 81}
 82
 83void dca_sysfs_remove_provider(struct dca_provider *dca)
 84{
 85	device_unregister(dca->cd);
 86	dca->cd = NULL;
 87	spin_lock(&dca_idr_lock);
 88	idr_remove(&dca_idr, dca->id);
 89	spin_unlock(&dca_idr_lock);
 90}
 91
 92int __init dca_sysfs_init(void)
 93{
 
 
 94	idr_init(&dca_idr);
 95	spin_lock_init(&dca_idr_lock);
 96
 97	dca_class = class_create(THIS_MODULE, "dca");
 98	if (IS_ERR(dca_class)) {
 99		idr_destroy(&dca_idr);
100		return PTR_ERR(dca_class);
101	}
102	return 0;
103}
104
105void __exit dca_sysfs_exit(void)
106{
107	class_destroy(dca_class);
108	idr_destroy(&dca_idr);
109}
110