Linux 5.4.79
[linux/fpc-iii.git] / sound / usb / media.c
blob812017eacbcfacb80f8b53cf3303a8cf195a81ee
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * media.c - Media Controller specific ALSA driver code
5 * Copyright (c) 2019 Shuah Khan <shuah@kernel.org>
7 */
9 /*
10 * This file adds Media Controller support to the ALSA driver
11 * to use the Media Controller API to share the tuner with DVB
12 * and V4L2 drivers that control the media device.
14 * The media device is created based on the existing quirks framework.
15 * Using this approach, the media controller API usage can be added for
16 * a specific device.
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
25 #include <sound/pcm.h>
26 #include <sound/core.h>
28 #include "usbaudio.h"
29 #include "card.h"
30 #include "mixer.h"
31 #include "media.h"
33 int snd_media_stream_init(struct snd_usb_substream *subs, struct snd_pcm *pcm,
34 int stream)
36 struct media_device *mdev;
37 struct media_ctl *mctl;
38 struct device *pcm_dev = &pcm->streams[stream].dev;
39 u32 intf_type;
40 int ret = 0;
41 u16 mixer_pad;
42 struct media_entity *entity;
44 mdev = subs->stream->chip->media_dev;
45 if (!mdev)
46 return 0;
48 if (subs->media_ctl)
49 return 0;
51 /* allocate media_ctl */
52 mctl = kzalloc(sizeof(*mctl), GFP_KERNEL);
53 if (!mctl)
54 return -ENOMEM;
56 mctl->media_dev = mdev;
57 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
58 intf_type = MEDIA_INTF_T_ALSA_PCM_PLAYBACK;
59 mctl->media_entity.function = MEDIA_ENT_F_AUDIO_PLAYBACK;
60 mctl->media_pad.flags = MEDIA_PAD_FL_SOURCE;
61 mixer_pad = 1;
62 } else {
63 intf_type = MEDIA_INTF_T_ALSA_PCM_CAPTURE;
64 mctl->media_entity.function = MEDIA_ENT_F_AUDIO_CAPTURE;
65 mctl->media_pad.flags = MEDIA_PAD_FL_SINK;
66 mixer_pad = 2;
68 mctl->media_entity.name = pcm->name;
69 media_entity_pads_init(&mctl->media_entity, 1, &mctl->media_pad);
70 ret = media_device_register_entity(mctl->media_dev,
71 &mctl->media_entity);
72 if (ret)
73 goto free_mctl;
75 mctl->intf_devnode = media_devnode_create(mdev, intf_type, 0,
76 MAJOR(pcm_dev->devt),
77 MINOR(pcm_dev->devt));
78 if (!mctl->intf_devnode) {
79 ret = -ENOMEM;
80 goto unregister_entity;
82 mctl->intf_link = media_create_intf_link(&mctl->media_entity,
83 &mctl->intf_devnode->intf,
84 MEDIA_LNK_FL_ENABLED);
85 if (!mctl->intf_link) {
86 ret = -ENOMEM;
87 goto devnode_remove;
90 /* create link between mixer and audio */
91 media_device_for_each_entity(entity, mdev) {
92 switch (entity->function) {
93 case MEDIA_ENT_F_AUDIO_MIXER:
94 ret = media_create_pad_link(entity, mixer_pad,
95 &mctl->media_entity, 0,
96 MEDIA_LNK_FL_ENABLED);
97 if (ret)
98 goto remove_intf_link;
99 break;
103 subs->media_ctl = mctl;
104 return 0;
106 remove_intf_link:
107 media_remove_intf_link(mctl->intf_link);
108 devnode_remove:
109 media_devnode_remove(mctl->intf_devnode);
110 unregister_entity:
111 media_device_unregister_entity(&mctl->media_entity);
112 free_mctl:
113 kfree(mctl);
114 return ret;
117 void snd_media_stream_delete(struct snd_usb_substream *subs)
119 struct media_ctl *mctl = subs->media_ctl;
121 if (mctl) {
122 struct media_device *mdev;
124 mdev = mctl->media_dev;
125 if (mdev && media_devnode_is_registered(mdev->devnode)) {
126 media_devnode_remove(mctl->intf_devnode);
127 media_device_unregister_entity(&mctl->media_entity);
128 media_entity_cleanup(&mctl->media_entity);
130 kfree(mctl);
131 subs->media_ctl = NULL;
135 int snd_media_start_pipeline(struct snd_usb_substream *subs)
137 struct media_ctl *mctl = subs->media_ctl;
138 int ret = 0;
140 if (!mctl)
141 return 0;
143 mutex_lock(&mctl->media_dev->graph_mutex);
144 if (mctl->media_dev->enable_source)
145 ret = mctl->media_dev->enable_source(&mctl->media_entity,
146 &mctl->media_pipe);
147 mutex_unlock(&mctl->media_dev->graph_mutex);
148 return ret;
151 void snd_media_stop_pipeline(struct snd_usb_substream *subs)
153 struct media_ctl *mctl = subs->media_ctl;
155 if (!mctl)
156 return;
158 mutex_lock(&mctl->media_dev->graph_mutex);
159 if (mctl->media_dev->disable_source)
160 mctl->media_dev->disable_source(&mctl->media_entity);
161 mutex_unlock(&mctl->media_dev->graph_mutex);
164 static int snd_media_mixer_init(struct snd_usb_audio *chip)
166 struct device *ctl_dev = &chip->card->ctl_dev;
167 struct media_intf_devnode *ctl_intf;
168 struct usb_mixer_interface *mixer;
169 struct media_device *mdev = chip->media_dev;
170 struct media_mixer_ctl *mctl;
171 u32 intf_type = MEDIA_INTF_T_ALSA_CONTROL;
172 int ret;
174 if (!mdev)
175 return -ENODEV;
177 ctl_intf = chip->ctl_intf_media_devnode;
178 if (!ctl_intf) {
179 ctl_intf = media_devnode_create(mdev, intf_type, 0,
180 MAJOR(ctl_dev->devt),
181 MINOR(ctl_dev->devt));
182 if (!ctl_intf)
183 return -ENOMEM;
184 chip->ctl_intf_media_devnode = ctl_intf;
187 list_for_each_entry(mixer, &chip->mixer_list, list) {
189 if (mixer->media_mixer_ctl)
190 continue;
192 /* allocate media_mixer_ctl */
193 mctl = kzalloc(sizeof(*mctl), GFP_KERNEL);
194 if (!mctl)
195 return -ENOMEM;
197 mctl->media_dev = mdev;
198 mctl->media_entity.function = MEDIA_ENT_F_AUDIO_MIXER;
199 mctl->media_entity.name = chip->card->mixername;
200 mctl->media_pad[0].flags = MEDIA_PAD_FL_SINK;
201 mctl->media_pad[1].flags = MEDIA_PAD_FL_SOURCE;
202 mctl->media_pad[2].flags = MEDIA_PAD_FL_SOURCE;
203 media_entity_pads_init(&mctl->media_entity, MEDIA_MIXER_PAD_MAX,
204 mctl->media_pad);
205 ret = media_device_register_entity(mctl->media_dev,
206 &mctl->media_entity);
207 if (ret) {
208 kfree(mctl);
209 return ret;
212 mctl->intf_link = media_create_intf_link(&mctl->media_entity,
213 &ctl_intf->intf,
214 MEDIA_LNK_FL_ENABLED);
215 if (!mctl->intf_link) {
216 media_device_unregister_entity(&mctl->media_entity);
217 media_entity_cleanup(&mctl->media_entity);
218 kfree(mctl);
219 return -ENOMEM;
221 mctl->intf_devnode = ctl_intf;
222 mixer->media_mixer_ctl = mctl;
224 return 0;
227 static void snd_media_mixer_delete(struct snd_usb_audio *chip)
229 struct usb_mixer_interface *mixer;
230 struct media_device *mdev = chip->media_dev;
232 if (!mdev)
233 return;
235 list_for_each_entry(mixer, &chip->mixer_list, list) {
236 struct media_mixer_ctl *mctl;
238 mctl = mixer->media_mixer_ctl;
239 if (!mixer->media_mixer_ctl)
240 continue;
242 if (media_devnode_is_registered(mdev->devnode)) {
243 media_device_unregister_entity(&mctl->media_entity);
244 media_entity_cleanup(&mctl->media_entity);
246 kfree(mctl);
247 mixer->media_mixer_ctl = NULL;
249 if (media_devnode_is_registered(mdev->devnode))
250 media_devnode_remove(chip->ctl_intf_media_devnode);
251 chip->ctl_intf_media_devnode = NULL;
254 int snd_media_device_create(struct snd_usb_audio *chip,
255 struct usb_interface *iface)
257 struct media_device *mdev;
258 struct usb_device *usbdev = interface_to_usbdev(iface);
259 int ret = 0;
261 /* usb-audio driver is probed for each usb interface, and
262 * there are multiple interfaces per device. Avoid calling
263 * media_device_usb_allocate() each time usb_audio_probe()
264 * is called. Do it only once.
266 if (chip->media_dev) {
267 mdev = chip->media_dev;
268 goto snd_mixer_init;
271 mdev = media_device_usb_allocate(usbdev, KBUILD_MODNAME, THIS_MODULE);
272 if (IS_ERR(mdev))
273 return -ENOMEM;
275 /* save media device - avoid lookups */
276 chip->media_dev = mdev;
278 snd_mixer_init:
279 /* Create media entities for mixer and control dev */
280 ret = snd_media_mixer_init(chip);
281 /* media_device might be registered, print error and continue */
282 if (ret)
283 dev_err(&usbdev->dev,
284 "Couldn't create media mixer entities. Error: %d\n",
285 ret);
287 if (!media_devnode_is_registered(mdev->devnode)) {
288 /* dont'register if snd_media_mixer_init() failed */
289 if (ret)
290 goto create_fail;
292 /* register media_device */
293 ret = media_device_register(mdev);
294 create_fail:
295 if (ret) {
296 snd_media_mixer_delete(chip);
297 media_device_delete(mdev, KBUILD_MODNAME, THIS_MODULE);
298 /* clear saved media_dev */
299 chip->media_dev = NULL;
300 dev_err(&usbdev->dev,
301 "Couldn't register media device. Error: %d\n",
302 ret);
303 return ret;
307 return ret;
310 void snd_media_device_delete(struct snd_usb_audio *chip)
312 struct media_device *mdev = chip->media_dev;
313 struct snd_usb_stream *stream;
315 /* release resources */
316 list_for_each_entry(stream, &chip->pcm_list, list) {
317 snd_media_stream_delete(&stream->substream[0]);
318 snd_media_stream_delete(&stream->substream[1]);
321 snd_media_mixer_delete(chip);
323 if (mdev) {
324 media_device_delete(mdev, KBUILD_MODNAME, THIS_MODULE);
325 chip->media_dev = NULL;