Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Device management routines
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
6
7#include <linux/slab.h>
8#include <linux/time.h>
9#include <linux/export.h>
10#include <linux/errno.h>
11#include <sound/core.h>
12
13/**
14 * snd_device_new - create an ALSA device component
15 * @card: the card instance
16 * @type: the device type, SNDRV_DEV_XXX
17 * @device_data: the data pointer of this device
18 * @ops: the operator table
19 *
20 * Creates a new device component for the given data pointer.
21 * The device will be assigned to the card and managed together
22 * by the card.
23 *
24 * The data pointer plays a role as the identifier, too, so the
25 * pointer address must be unique and unchanged.
26 *
27 * Return: Zero if successful, or a negative error code on failure.
28 */
29int snd_device_new(struct snd_card *card, enum snd_device_type type,
30 void *device_data, struct snd_device_ops *ops)
31{
32 struct snd_device *dev;
33 struct list_head *p;
34
35 if (snd_BUG_ON(!card || !device_data || !ops))
36 return -ENXIO;
37 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
38 if (!dev)
39 return -ENOMEM;
40 INIT_LIST_HEAD(&dev->list);
41 dev->card = card;
42 dev->type = type;
43 dev->state = SNDRV_DEV_BUILD;
44 dev->device_data = device_data;
45 dev->ops = ops;
46
47 /* insert the entry in an incrementally sorted list */
48 list_for_each_prev(p, &card->devices) {
49 struct snd_device *pdev = list_entry(p, struct snd_device, list);
50 if ((unsigned int)pdev->type <= (unsigned int)type)
51 break;
52 }
53
54 list_add(&dev->list, p);
55 return 0;
56}
57EXPORT_SYMBOL(snd_device_new);
58
59static void __snd_device_disconnect(struct snd_device *dev)
60{
61 if (dev->state == SNDRV_DEV_REGISTERED) {
62 if (dev->ops->dev_disconnect &&
63 dev->ops->dev_disconnect(dev))
64 dev_err(dev->card->dev, "device disconnect failure\n");
65 dev->state = SNDRV_DEV_DISCONNECTED;
66 }
67}
68
69static void __snd_device_free(struct snd_device *dev)
70{
71 /* unlink */
72 list_del(&dev->list);
73
74 __snd_device_disconnect(dev);
75 if (dev->ops->dev_free) {
76 if (dev->ops->dev_free(dev))
77 dev_err(dev->card->dev, "device free failure\n");
78 }
79 kfree(dev);
80}
81
82static struct snd_device *look_for_dev(struct snd_card *card, void *device_data)
83{
84 struct snd_device *dev;
85
86 list_for_each_entry(dev, &card->devices, list)
87 if (dev->device_data == device_data)
88 return dev;
89
90 return NULL;
91}
92
93/**
94 * snd_device_disconnect - disconnect the device
95 * @card: the card instance
96 * @device_data: the data pointer to disconnect
97 *
98 * Turns the device into the disconnection state, invoking
99 * dev_disconnect callback, if the device was already registered.
100 *
101 * Usually called from snd_card_disconnect().
102 *
103 * Return: Zero if successful, or a negative error code on failure or if the
104 * device not found.
105 */
106void snd_device_disconnect(struct snd_card *card, void *device_data)
107{
108 struct snd_device *dev;
109
110 if (snd_BUG_ON(!card || !device_data))
111 return;
112 dev = look_for_dev(card, device_data);
113 if (dev)
114 __snd_device_disconnect(dev);
115 else
116 dev_dbg(card->dev, "device disconnect %p (from %pS), not found\n",
117 device_data, __builtin_return_address(0));
118}
119EXPORT_SYMBOL_GPL(snd_device_disconnect);
120
121/**
122 * snd_device_free - release the device from the card
123 * @card: the card instance
124 * @device_data: the data pointer to release
125 *
126 * Removes the device from the list on the card and invokes the
127 * callbacks, dev_disconnect and dev_free, corresponding to the state.
128 * Then release the device.
129 */
130void snd_device_free(struct snd_card *card, void *device_data)
131{
132 struct snd_device *dev;
133
134 if (snd_BUG_ON(!card || !device_data))
135 return;
136 dev = look_for_dev(card, device_data);
137 if (dev)
138 __snd_device_free(dev);
139 else
140 dev_dbg(card->dev, "device free %p (from %pS), not found\n",
141 device_data, __builtin_return_address(0));
142}
143EXPORT_SYMBOL(snd_device_free);
144
145static int __snd_device_register(struct snd_device *dev)
146{
147 if (dev->state == SNDRV_DEV_BUILD) {
148 if (dev->ops->dev_register) {
149 int err = dev->ops->dev_register(dev);
150 if (err < 0)
151 return err;
152 }
153 dev->state = SNDRV_DEV_REGISTERED;
154 }
155 return 0;
156}
157
158/**
159 * snd_device_register - register the device
160 * @card: the card instance
161 * @device_data: the data pointer to register
162 *
163 * Registers the device which was already created via
164 * snd_device_new(). Usually this is called from snd_card_register(),
165 * but it can be called later if any new devices are created after
166 * invocation of snd_card_register().
167 *
168 * Return: Zero if successful, or a negative error code on failure or if the
169 * device not found.
170 */
171int snd_device_register(struct snd_card *card, void *device_data)
172{
173 struct snd_device *dev;
174
175 if (snd_BUG_ON(!card || !device_data))
176 return -ENXIO;
177 dev = look_for_dev(card, device_data);
178 if (dev)
179 return __snd_device_register(dev);
180 snd_BUG();
181 return -ENXIO;
182}
183EXPORT_SYMBOL(snd_device_register);
184
185/*
186 * register all the devices on the card.
187 * called from init.c
188 */
189int snd_device_register_all(struct snd_card *card)
190{
191 struct snd_device *dev;
192 int err;
193
194 if (snd_BUG_ON(!card))
195 return -ENXIO;
196 list_for_each_entry(dev, &card->devices, list) {
197 err = __snd_device_register(dev);
198 if (err < 0)
199 return err;
200 }
201 return 0;
202}
203
204/*
205 * disconnect all the devices on the card.
206 * called from init.c
207 */
208void snd_device_disconnect_all(struct snd_card *card)
209{
210 struct snd_device *dev;
211
212 if (snd_BUG_ON(!card))
213 return;
214 list_for_each_entry_reverse(dev, &card->devices, list)
215 __snd_device_disconnect(dev);
216}
217
218/*
219 * release all the devices on the card.
220 * called from init.c
221 */
222void snd_device_free_all(struct snd_card *card)
223{
224 struct snd_device *dev, *next;
225
226 if (snd_BUG_ON(!card))
227 return;
228 list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
229 /* exception: free ctl and lowlevel stuff later */
230 if (dev->type == SNDRV_DEV_CONTROL ||
231 dev->type == SNDRV_DEV_LOWLEVEL)
232 continue;
233 __snd_device_free(dev);
234 }
235
236 /* free all */
237 list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
238 __snd_device_free(dev);
239}
1/*
2 * Device management routines
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/slab.h>
23#include <linux/time.h>
24#include <linux/export.h>
25#include <linux/errno.h>
26#include <sound/core.h>
27
28/**
29 * snd_device_new - create an ALSA device component
30 * @card: the card instance
31 * @type: the device type, SNDRV_DEV_XXX
32 * @device_data: the data pointer of this device
33 * @ops: the operator table
34 *
35 * Creates a new device component for the given data pointer.
36 * The device will be assigned to the card and managed together
37 * by the card.
38 *
39 * The data pointer plays a role as the identifier, too, so the
40 * pointer address must be unique and unchanged.
41 *
42 * Returns zero if successful, or a negative error code on failure.
43 */
44int snd_device_new(struct snd_card *card, snd_device_type_t type,
45 void *device_data, struct snd_device_ops *ops)
46{
47 struct snd_device *dev;
48
49 if (snd_BUG_ON(!card || !device_data || !ops))
50 return -ENXIO;
51 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
52 if (dev == NULL) {
53 snd_printk(KERN_ERR "Cannot allocate device\n");
54 return -ENOMEM;
55 }
56 dev->card = card;
57 dev->type = type;
58 dev->state = SNDRV_DEV_BUILD;
59 dev->device_data = device_data;
60 dev->ops = ops;
61 list_add(&dev->list, &card->devices); /* add to the head of list */
62 return 0;
63}
64
65EXPORT_SYMBOL(snd_device_new);
66
67/**
68 * snd_device_free - release the device from the card
69 * @card: the card instance
70 * @device_data: the data pointer to release
71 *
72 * Removes the device from the list on the card and invokes the
73 * callbacks, dev_disconnect and dev_free, corresponding to the state.
74 * Then release the device.
75 *
76 * Returns zero if successful, or a negative error code on failure or if the
77 * device not found.
78 */
79int snd_device_free(struct snd_card *card, void *device_data)
80{
81 struct snd_device *dev;
82
83 if (snd_BUG_ON(!card || !device_data))
84 return -ENXIO;
85 list_for_each_entry(dev, &card->devices, list) {
86 if (dev->device_data != device_data)
87 continue;
88 /* unlink */
89 list_del(&dev->list);
90 if (dev->state == SNDRV_DEV_REGISTERED &&
91 dev->ops->dev_disconnect)
92 if (dev->ops->dev_disconnect(dev))
93 snd_printk(KERN_ERR
94 "device disconnect failure\n");
95 if (dev->ops->dev_free) {
96 if (dev->ops->dev_free(dev))
97 snd_printk(KERN_ERR "device free failure\n");
98 }
99 kfree(dev);
100 return 0;
101 }
102 snd_printd("device free %p (from %pF), not found\n", device_data,
103 __builtin_return_address(0));
104 return -ENXIO;
105}
106
107EXPORT_SYMBOL(snd_device_free);
108
109/**
110 * snd_device_disconnect - disconnect the device
111 * @card: the card instance
112 * @device_data: the data pointer to disconnect
113 *
114 * Turns the device into the disconnection state, invoking
115 * dev_disconnect callback, if the device was already registered.
116 *
117 * Usually called from snd_card_disconnect().
118 *
119 * Returns zero if successful, or a negative error code on failure or if the
120 * device not found.
121 */
122int snd_device_disconnect(struct snd_card *card, void *device_data)
123{
124 struct snd_device *dev;
125
126 if (snd_BUG_ON(!card || !device_data))
127 return -ENXIO;
128 list_for_each_entry(dev, &card->devices, list) {
129 if (dev->device_data != device_data)
130 continue;
131 if (dev->state == SNDRV_DEV_REGISTERED &&
132 dev->ops->dev_disconnect) {
133 if (dev->ops->dev_disconnect(dev))
134 snd_printk(KERN_ERR "device disconnect failure\n");
135 dev->state = SNDRV_DEV_DISCONNECTED;
136 }
137 return 0;
138 }
139 snd_printd("device disconnect %p (from %pF), not found\n", device_data,
140 __builtin_return_address(0));
141 return -ENXIO;
142}
143
144/**
145 * snd_device_register - register the device
146 * @card: the card instance
147 * @device_data: the data pointer to register
148 *
149 * Registers the device which was already created via
150 * snd_device_new(). Usually this is called from snd_card_register(),
151 * but it can be called later if any new devices are created after
152 * invocation of snd_card_register().
153 *
154 * Returns zero if successful, or a negative error code on failure or if the
155 * device not found.
156 */
157int snd_device_register(struct snd_card *card, void *device_data)
158{
159 struct snd_device *dev;
160 int err;
161
162 if (snd_BUG_ON(!card || !device_data))
163 return -ENXIO;
164 list_for_each_entry(dev, &card->devices, list) {
165 if (dev->device_data != device_data)
166 continue;
167 if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
168 if ((err = dev->ops->dev_register(dev)) < 0)
169 return err;
170 dev->state = SNDRV_DEV_REGISTERED;
171 return 0;
172 }
173 snd_printd("snd_device_register busy\n");
174 return -EBUSY;
175 }
176 snd_BUG();
177 return -ENXIO;
178}
179
180EXPORT_SYMBOL(snd_device_register);
181
182/*
183 * register all the devices on the card.
184 * called from init.c
185 */
186int snd_device_register_all(struct snd_card *card)
187{
188 struct snd_device *dev;
189 int err;
190
191 if (snd_BUG_ON(!card))
192 return -ENXIO;
193 list_for_each_entry(dev, &card->devices, list) {
194 if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
195 if ((err = dev->ops->dev_register(dev)) < 0)
196 return err;
197 dev->state = SNDRV_DEV_REGISTERED;
198 }
199 }
200 return 0;
201}
202
203/*
204 * disconnect all the devices on the card.
205 * called from init.c
206 */
207int snd_device_disconnect_all(struct snd_card *card)
208{
209 struct snd_device *dev;
210 int err = 0;
211
212 if (snd_BUG_ON(!card))
213 return -ENXIO;
214 list_for_each_entry(dev, &card->devices, list) {
215 if (snd_device_disconnect(card, dev->device_data) < 0)
216 err = -ENXIO;
217 }
218 return err;
219}
220
221/*
222 * release all the devices on the card.
223 * called from init.c
224 */
225int snd_device_free_all(struct snd_card *card, snd_device_cmd_t cmd)
226{
227 struct snd_device *dev;
228 int err;
229 unsigned int range_low, range_high, type;
230
231 if (snd_BUG_ON(!card))
232 return -ENXIO;
233 range_low = (__force unsigned int)cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
234 range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
235 __again:
236 list_for_each_entry(dev, &card->devices, list) {
237 type = (__force unsigned int)dev->type;
238 if (type >= range_low && type <= range_high) {
239 if ((err = snd_device_free(card, dev->device_data)) < 0)
240 return err;
241 goto __again;
242 }
243 }
244 return 0;
245}