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