blob: 8239ebc9349cf16ca47cce20da215c540e8ffa86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010026#include <linux/mutex.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050027#include <linux/device.h>
Gustavo A. R. Silva7455aca2018-12-12 15:36:28 -060028#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/minors.h>
31#include <sound/pcm.h>
Takashi Iwaidd34b662018-04-02 22:41:43 +020032#include <sound/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/control.h>
34#include <sound/info.h>
35
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020036MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
38MODULE_LICENSE("GPL");
39
Clemens Ladischf87135f2005-11-20 14:06:59 +010040static LIST_HEAD(snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static LIST_HEAD(snd_pcm_notify_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010042static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Takashi Iwai877211f2005-11-17 13:59:38 +010044static int snd_pcm_free(struct snd_pcm *pcm);
45static int snd_pcm_dev_free(struct snd_device *device);
46static int snd_pcm_dev_register(struct snd_device *device);
47static int snd_pcm_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Pawel MOLLf90c06a2008-07-30 12:46:40 +010049static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
Clemens Ladischf87135f2005-11-20 14:06:59 +010050{
Clemens Ladischf87135f2005-11-20 14:06:59 +010051 struct snd_pcm *pcm;
52
Johannes Berg9244b2c2006-10-05 16:02:22 +020053 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010054 if (pcm->card == card && pcm->device == device)
55 return pcm;
56 }
57 return NULL;
58}
59
Pawel MOLLf90c06a2008-07-30 12:46:40 +010060static int snd_pcm_next(struct snd_card *card, int device)
61{
62 struct snd_pcm *pcm;
63
64 list_for_each_entry(pcm, &snd_pcm_devices, list) {
65 if (pcm->card == card && pcm->device > device)
66 return pcm->device;
67 else if (pcm->card->number > card->number)
68 return -1;
69 }
70 return -1;
71}
72
73static int snd_pcm_add(struct snd_pcm *newpcm)
74{
75 struct snd_pcm *pcm;
76
Takashi Iwaib95bd3a2015-02-20 16:49:04 +010077 if (newpcm->internal)
78 return 0;
79
Pawel MOLLf90c06a2008-07-30 12:46:40 +010080 list_for_each_entry(pcm, &snd_pcm_devices, list) {
81 if (pcm->card == newpcm->card && pcm->device == newpcm->device)
82 return -EBUSY;
83 if (pcm->card->number > newpcm->card->number ||
84 (pcm->card == newpcm->card &&
85 pcm->device > newpcm->device)) {
86 list_add(&newpcm->list, pcm->list.prev);
87 return 0;
88 }
89 }
90 list_add_tail(&newpcm->list, &snd_pcm_devices);
91 return 0;
92}
93
Takashi Iwai877211f2005-11-17 13:59:38 +010094static int snd_pcm_control_ioctl(struct snd_card *card,
95 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int cmd, unsigned long arg)
97{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 switch (cmd) {
99 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
100 {
101 int device;
102
103 if (get_user(device, (int __user *)arg))
104 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100105 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +0100106 device = snd_pcm_next(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100107 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (put_user(device, (int __user *)arg))
109 return -EFAULT;
110 return 0;
111 }
112 case SNDRV_CTL_IOCTL_PCM_INFO:
113 {
Takashi Iwai877211f2005-11-17 13:59:38 +0100114 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +0100116 int stream;
117 struct snd_pcm *pcm;
118 struct snd_pcm_str *pstr;
119 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100120 int err;
121
Takashi Iwai877211f2005-11-17 13:59:38 +0100122 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (get_user(device, &info->device))
124 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (get_user(stream, &info->stream))
126 return -EFAULT;
127 if (stream < 0 || stream > 1)
128 return -EINVAL;
Gustavo A. R. Silva7455aca2018-12-12 15:36:28 -0600129 stream = array_index_nospec(stream, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (get_user(subdevice, &info->subdevice))
131 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100132 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +0100133 pcm = snd_pcm_get(card, device);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100134 if (pcm == NULL) {
135 err = -ENXIO;
136 goto _error;
137 }
138 pstr = &pcm->streams[stream];
139 if (pstr->substream_count == 0) {
140 err = -ENOENT;
141 goto _error;
142 }
143 if (subdevice >= pstr->substream_count) {
144 err = -ENXIO;
145 goto _error;
146 }
147 for (substream = pstr->substream; substream;
148 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (substream->number == (int)subdevice)
150 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100151 if (substream == NULL) {
152 err = -ENXIO;
153 goto _error;
154 }
Robb Glasser45ddff32017-12-05 09:16:55 -0800155 mutex_lock(&pcm->open_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100156 err = snd_pcm_info_user(substream, info);
Robb Glasser45ddff32017-12-05 09:16:55 -0800157 mutex_unlock(&pcm->open_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100158 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100159 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
163 {
164 int val;
165
166 if (get_user(val, (int __user *)arg))
167 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100168 control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170 }
171 }
172 return -ENOIOCTLCMD;
173}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static char *snd_pcm_format_names[] = {
178 FORMAT(S8),
179 FORMAT(U8),
180 FORMAT(S16_LE),
181 FORMAT(S16_BE),
182 FORMAT(U16_LE),
183 FORMAT(U16_BE),
184 FORMAT(S24_LE),
185 FORMAT(S24_BE),
186 FORMAT(U24_LE),
187 FORMAT(U24_BE),
188 FORMAT(S32_LE),
189 FORMAT(S32_BE),
190 FORMAT(U32_LE),
191 FORMAT(U32_BE),
192 FORMAT(FLOAT_LE),
193 FORMAT(FLOAT_BE),
194 FORMAT(FLOAT64_LE),
195 FORMAT(FLOAT64_BE),
196 FORMAT(IEC958_SUBFRAME_LE),
197 FORMAT(IEC958_SUBFRAME_BE),
198 FORMAT(MU_LAW),
199 FORMAT(A_LAW),
200 FORMAT(IMA_ADPCM),
201 FORMAT(MPEG),
202 FORMAT(GSM),
203 FORMAT(SPECIAL),
204 FORMAT(S24_3LE),
205 FORMAT(S24_3BE),
206 FORMAT(U24_3LE),
207 FORMAT(U24_3BE),
208 FORMAT(S20_3LE),
209 FORMAT(S20_3BE),
210 FORMAT(U20_3LE),
211 FORMAT(U20_3BE),
212 FORMAT(S18_3LE),
213 FORMAT(S18_3BE),
214 FORMAT(U18_3LE),
215 FORMAT(U18_3BE),
Dan Carpenter7a288262010-08-27 22:02:15 +0200216 FORMAT(G723_24),
217 FORMAT(G723_24_1B),
218 FORMAT(G723_40),
219 FORMAT(G723_40_1B),
Daniel Mackef7a4f92013-04-17 00:01:36 +0800220 FORMAT(DSD_U8),
221 FORMAT(DSD_U16_LE),
Jurgen Kramerd4288d32014-09-05 10:47:56 +0200222 FORMAT(DSD_U32_LE),
Jussi Laakod42472e2014-11-21 16:04:46 +0200223 FORMAT(DSD_U16_BE),
224 FORMAT(DSD_U32_BE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Takashi Iwai30b771c2014-10-30 15:02:50 +0100227/**
228 * snd_pcm_format_name - Return a name string for the given PCM format
229 * @format: PCM format
230 */
Takashi Iwai6e5265e2009-09-08 14:26:51 +0200231const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100232{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100233 if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
Dan Carpenter7a288262010-08-27 22:02:15 +0200234 return "Unknown";
Clemens Ladischfea952e2011-02-14 11:00:47 +0100235 return snd_pcm_format_names[(__force unsigned int)format];
Takashi Iwaie28563c2005-12-01 10:42:42 +0100236}
Takashi Iwai6e5265e2009-09-08 14:26:51 +0200237EXPORT_SYMBOL_GPL(snd_pcm_format_name);
238
239#ifdef CONFIG_SND_VERBOSE_PROCFS
240
241#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
242#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
243#define READY(v) [SNDRV_PCM_READY_##v] = #v
244#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
245#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
246#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
247#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
248#define START(v) [SNDRV_PCM_START_##v] = #v
249#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
Takashi Iwaie28563c2005-12-01 10:42:42 +0100250
Takashi Iwaie28563c2005-12-01 10:42:42 +0100251static char *snd_pcm_stream_names[] = {
252 STREAM(PLAYBACK),
253 STREAM(CAPTURE),
254};
255
256static char *snd_pcm_state_names[] = {
257 STATE(OPEN),
258 STATE(SETUP),
259 STATE(PREPARED),
260 STATE(RUNNING),
261 STATE(XRUN),
262 STATE(DRAINING),
263 STATE(PAUSED),
264 STATE(SUSPENDED),
265};
266
267static char *snd_pcm_access_names[] = {
268 ACCESS(MMAP_INTERLEAVED),
269 ACCESS(MMAP_NONINTERLEAVED),
270 ACCESS(MMAP_COMPLEX),
271 ACCESS(RW_INTERLEAVED),
272 ACCESS(RW_NONINTERLEAVED),
273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static char *snd_pcm_subformat_names[] = {
276 SUBFORMAT(STD),
277};
278
279static char *snd_pcm_tstamp_mode_names[] = {
280 TSTAMP(NONE),
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100281 TSTAMP(ENABLE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Takashi Iwai877211f2005-11-17 13:59:38 +0100284static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return snd_pcm_stream_names[stream];
287}
288
289static const char *snd_pcm_access_name(snd_pcm_access_t access)
290{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100291 return snd_pcm_access_names[(__force int)access];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
295{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100296 return snd_pcm_subformat_names[(__force int)subformat];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Takashi Iwai877211f2005-11-17 13:59:38 +0100299static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return snd_pcm_tstamp_mode_names[mode];
302}
303
304static const char *snd_pcm_state_name(snd_pcm_state_t state)
305{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100306 return snd_pcm_state_names[(__force int)state];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100309#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static const char *snd_pcm_oss_format_name(int format)
313{
314 switch (format) {
315 case AFMT_MU_LAW:
316 return "MU_LAW";
317 case AFMT_A_LAW:
318 return "A_LAW";
319 case AFMT_IMA_ADPCM:
320 return "IMA_ADPCM";
321 case AFMT_U8:
322 return "U8";
323 case AFMT_S16_LE:
324 return "S16_LE";
325 case AFMT_S16_BE:
326 return "S16_BE";
327 case AFMT_S8:
328 return "S8";
329 case AFMT_U16_LE:
330 return "U16_LE";
331 case AFMT_U16_BE:
332 return "U16_BE";
333 case AFMT_MPEG:
334 return "MPEG";
335 default:
336 return "unknown";
337 }
338}
339#endif
340
Takashi Iwai877211f2005-11-17 13:59:38 +0100341static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
342 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai877211f2005-11-17 13:59:38 +0100344 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int err;
346
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200347 if (! substream)
348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 info = kmalloc(sizeof(*info), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100351 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 err = snd_pcm_info(substream, info);
355 if (err < 0) {
356 snd_iprintf(buffer, "error %d\n", err);
357 kfree(info);
358 return;
359 }
360 snd_iprintf(buffer, "card: %d\n", info->card);
361 snd_iprintf(buffer, "device: %d\n", info->device);
362 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
363 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
364 snd_iprintf(buffer, "id: %s\n", info->id);
365 snd_iprintf(buffer, "name: %s\n", info->name);
366 snd_iprintf(buffer, "subname: %s\n", info->subname);
367 snd_iprintf(buffer, "class: %d\n", info->dev_class);
368 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
369 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
370 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
371 kfree(info);
372}
373
Takashi Iwai877211f2005-11-17 13:59:38 +0100374static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
375 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Takashi Iwai877211f2005-11-17 13:59:38 +0100377 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
378 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Takashi Iwai877211f2005-11-17 13:59:38 +0100381static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
382 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Joe Perches9fe856e2010-09-04 18:52:54 -0700384 snd_pcm_proc_info_read(entry->private_data, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Takashi Iwai877211f2005-11-17 13:59:38 +0100387static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
388 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Takashi Iwai877211f2005-11-17 13:59:38 +0100390 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200391 struct snd_pcm_runtime *runtime;
392
393 mutex_lock(&substream->pcm->open_mutex);
394 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!runtime) {
396 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200397 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
400 snd_iprintf(buffer, "no setup\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200401 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
404 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
405 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
406 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
407 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
408 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
409 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100410#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (substream->oss.oss) {
412 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
413 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
414 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
415 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
416 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
417 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
418 }
419#endif
Takashi Iwai901d46d2010-09-16 23:06:50 +0200420 unlock:
421 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Takashi Iwai877211f2005-11-17 13:59:38 +0100424static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
425 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Takashi Iwai877211f2005-11-17 13:59:38 +0100427 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200428 struct snd_pcm_runtime *runtime;
429
430 mutex_lock(&substream->pcm->open_mutex);
431 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!runtime) {
433 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200434 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
437 snd_iprintf(buffer, "no setup\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200438 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
441 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
444 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
445 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
446 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
447 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200448 unlock:
449 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Takashi Iwai877211f2005-11-17 13:59:38 +0100452static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
453 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Takashi Iwai877211f2005-11-17 13:59:38 +0100455 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200456 struct snd_pcm_runtime *runtime;
Takashi Iwai877211f2005-11-17 13:59:38 +0100457 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int err;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200459
460 mutex_lock(&substream->pcm->open_mutex);
461 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!runtime) {
463 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200464 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 memset(&status, 0, sizeof(status));
467 err = snd_pcm_status(substream, &status);
468 if (err < 0) {
469 snd_iprintf(buffer, "error %d\n", err);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200470 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
Clemens Ladische7373b72009-11-10 10:13:30 +0100473 snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
475 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
476 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
477 status.tstamp.tv_sec, status.tstamp.tv_nsec);
478 snd_iprintf(buffer, "delay : %ld\n", status.delay);
479 snd_iprintf(buffer, "avail : %ld\n", status.avail);
480 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
481 snd_iprintf(buffer, "-----\n");
482 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
483 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200484 unlock:
485 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200488#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai2b30d412014-11-04 14:02:40 +0100489static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
490 struct snd_info_buffer *buffer)
491{
492 struct snd_pcm_substream *substream = entry->private_data;
493 struct snd_pcm_runtime *runtime;
494
495 snd_pcm_stream_lock_irq(substream);
496 runtime = substream->runtime;
497 if (runtime && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
498 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
499 snd_pcm_stream_unlock_irq(substream);
500}
501
Takashi Iwai877211f2005-11-17 13:59:38 +0100502static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
503 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Takashi Iwai877211f2005-11-17 13:59:38 +0100505 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
507}
508
Takashi Iwai877211f2005-11-17 13:59:38 +0100509static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
510 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Takashi Iwai877211f2005-11-17 13:59:38 +0100512 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 char line[64];
514 if (!snd_info_get_line(buffer, line, sizeof(line)))
515 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
516}
517#endif
518
Takashi Iwai877211f2005-11-17 13:59:38 +0100519static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Takashi Iwai877211f2005-11-17 13:59:38 +0100521 struct snd_pcm *pcm = pstr->pcm;
522 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char name[16];
524
525 sprintf(name, "pcm%i%c", pcm->device,
526 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
527 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
528 return -ENOMEM;
529 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
530 if (snd_info_register(entry) < 0) {
531 snd_info_free_entry(entry);
532 return -ENOMEM;
533 }
534 pstr->proc_root = entry;
535
536 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200537 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (snd_info_register(entry) < 0) {
539 snd_info_free_entry(entry);
540 entry = NULL;
541 }
542 }
543 pstr->proc_info_entry = entry;
544
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200545#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100546 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
547 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200550 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 entry->private_data = pstr;
552 if (snd_info_register(entry) < 0) {
553 snd_info_free_entry(entry);
554 entry = NULL;
555 }
556 }
557 pstr->proc_xrun_debug_entry = entry;
558#endif
559 return 0;
560}
561
Takashi Iwai877211f2005-11-17 13:59:38 +0100562static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200564#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai746d4a02006-06-23 14:37:59 +0200565 snd_info_free_entry(pstr->proc_xrun_debug_entry);
566 pstr->proc_xrun_debug_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200568 snd_info_free_entry(pstr->proc_info_entry);
569 pstr->proc_info_entry = NULL;
570 snd_info_free_entry(pstr->proc_root);
571 pstr->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
Takashi Iwai877211f2005-11-17 13:59:38 +0100575static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Takashi Iwai877211f2005-11-17 13:59:38 +0100577 struct snd_info_entry *entry;
578 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 char name[16];
580
581 card = substream->pcm->card;
582
583 sprintf(name, "sub%i", substream->number);
584 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
585 return -ENOMEM;
586 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
587 if (snd_info_register(entry) < 0) {
588 snd_info_free_entry(entry);
589 return -ENOMEM;
590 }
591 substream->proc_root = entry;
592
593 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200594 snd_info_set_text_ops(entry, substream,
595 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (snd_info_register(entry) < 0) {
597 snd_info_free_entry(entry);
598 entry = NULL;
599 }
600 }
601 substream->proc_info_entry = entry;
602
603 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200604 snd_info_set_text_ops(entry, substream,
605 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (snd_info_register(entry) < 0) {
607 snd_info_free_entry(entry);
608 entry = NULL;
609 }
610 }
611 substream->proc_hw_params_entry = entry;
612
613 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200614 snd_info_set_text_ops(entry, substream,
615 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (snd_info_register(entry) < 0) {
617 snd_info_free_entry(entry);
618 entry = NULL;
619 }
620 }
621 substream->proc_sw_params_entry = entry;
622
623 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200624 snd_info_set_text_ops(entry, substream,
625 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (snd_info_register(entry) < 0) {
627 snd_info_free_entry(entry);
628 entry = NULL;
629 }
630 }
631 substream->proc_status_entry = entry;
632
Takashi Iwai2b30d412014-11-04 14:02:40 +0100633#ifdef CONFIG_SND_PCM_XRUN_DEBUG
634 entry = snd_info_create_card_entry(card, "xrun_injection",
635 substream->proc_root);
636 if (entry) {
637 entry->private_data = substream;
638 entry->c.text.read = NULL;
639 entry->c.text.write = snd_pcm_xrun_injection_write;
640 entry->mode = S_IFREG | S_IWUSR;
641 if (snd_info_register(entry) < 0) {
642 snd_info_free_entry(entry);
643 entry = NULL;
644 }
645 }
646 substream->proc_xrun_injection_entry = entry;
647#endif /* CONFIG_SND_PCM_XRUN_DEBUG */
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650}
Takashi Iwai746d4a02006-06-23 14:37:59 +0200651
Takashi Iwai877211f2005-11-17 13:59:38 +0100652static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200654 snd_info_free_entry(substream->proc_info_entry);
655 substream->proc_info_entry = NULL;
656 snd_info_free_entry(substream->proc_hw_params_entry);
657 substream->proc_hw_params_entry = NULL;
658 snd_info_free_entry(substream->proc_sw_params_entry);
659 substream->proc_sw_params_entry = NULL;
660 snd_info_free_entry(substream->proc_status_entry);
661 substream->proc_status_entry = NULL;
Takashi Iwai2b30d412014-11-04 14:02:40 +0100662#ifdef CONFIG_SND_PCM_XRUN_DEBUG
663 snd_info_free_entry(substream->proc_xrun_injection_entry);
664 substream->proc_xrun_injection_entry = NULL;
665#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200666 snd_info_free_entry(substream->proc_root);
667 substream->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200670#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100671static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
672static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
673static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
674static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200675#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100677static const struct attribute_group *pcm_dev_attr_groups[];
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/**
680 * snd_pcm_new_stream - create a new PCM stream
681 * @pcm: the pcm instance
682 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
683 * @substream_count: the number of substreams
684 *
685 * Creates a new stream for the pcm.
686 * The corresponding stream on the pcm must have been empty before
687 * calling this, i.e. zero must be given to the argument of
688 * snd_pcm_new().
689 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100690 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100692int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 struct snd_pcm_str *pstr = &pcm->streams[stream];
696 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100698#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100699 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700#endif
701 pstr->stream = stream;
702 pstr->pcm = pcm;
703 pstr->substream_count = substream_count;
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100704 if (!substream_count)
705 return 0;
706
707 snd_device_initialize(&pstr->dev, pcm->card);
708 pstr->dev.groups = pcm_dev_attr_groups;
709 dev_set_name(&pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
710 stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
711
712 if (!pcm->internal) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100714 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100715 pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 prev = NULL;
720 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200721 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100722 if (!substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -ENOMEM;
724 substream->pcm = pcm;
725 substream->pstr = pstr;
726 substream->number = idx;
727 substream->stream = stream;
728 sprintf(substream->name, "subdevice #%i", idx);
729 substream->buffer_bytes_max = UINT_MAX;
730 if (prev == NULL)
731 pstr->substream = substream;
732 else
733 prev->next = substream;
Liam Girdwood945e5032012-02-08 20:33:31 +0000734
735 if (!pcm->internal) {
736 err = snd_pcm_substream_proc_init(substream);
737 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100738 pcm_err(pcm,
739 "Error in snd_pcm_stream_proc_init\n");
Liam Girdwood945e5032012-02-08 20:33:31 +0000740 if (prev == NULL)
741 pstr->substream = NULL;
742 else
743 prev->next = NULL;
744 kfree(substream);
745 return err;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 substream->group = &substream->self_group;
749 spin_lock_init(&substream->self_group.lock);
Karthikeyan Mani9a67a572017-09-28 11:06:55 -0700750 spin_lock_init(&substream->runtime_lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200751 mutex_init(&substream->self_group.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 INIT_LIST_HEAD(&substream->self_group.substreams);
753 list_add_tail(&substream->link_list, &substream->self_group.substreams);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200754 atomic_set(&substream->mmap_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 prev = substream;
756 }
757 return 0;
758}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200759EXPORT_SYMBOL(snd_pcm_new_stream);
760
Liam Girdwood945e5032012-02-08 20:33:31 +0000761static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
762 int playback_count, int capture_count, bool internal,
763 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Takashi Iwai877211f2005-11-17 13:59:38 +0100765 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100767 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 .dev_free = snd_pcm_dev_free,
769 .dev_register = snd_pcm_dev_register,
770 .dev_disconnect = snd_pcm_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 };
772
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200773 if (snd_BUG_ON(!card))
774 return -ENXIO;
775 if (rpcm)
776 *rpcm = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200777 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100778 if (!pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -ENOMEM;
780 pcm->card = card;
781 pcm->device = device;
Liam Girdwood945e5032012-02-08 20:33:31 +0000782 pcm->internal = internal;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +0100783 mutex_init(&pcm->open_mutex);
784 init_waitqueue_head(&pcm->open_wait);
785 INIT_LIST_HEAD(&pcm->list);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100786 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
789 snd_pcm_free(pcm);
790 return err;
791 }
792 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
793 snd_pcm_free(pcm);
794 return err;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
797 snd_pcm_free(pcm);
798 return err;
799 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200800 if (rpcm)
801 *rpcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
Liam Girdwood945e5032012-02-08 20:33:31 +0000805/**
806 * snd_pcm_new - create a new PCM instance
807 * @card: the card instance
808 * @id: the id string
809 * @device: the device index (zero based)
810 * @playback_count: the number of substreams for playback
811 * @capture_count: the number of substreams for capture
812 * @rpcm: the pointer to store the new pcm instance
813 *
814 * Creates a new PCM instance.
815 *
816 * The pcm operators have to be set afterwards to the new instance
817 * via snd_pcm_set_ops().
818 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100819 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000820 */
821int snd_pcm_new(struct snd_card *card, const char *id, int device,
822 int playback_count, int capture_count, struct snd_pcm **rpcm)
823{
824 return _snd_pcm_new(card, id, device, playback_count, capture_count,
825 false, rpcm);
826}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200827EXPORT_SYMBOL(snd_pcm_new);
828
Liam Girdwood945e5032012-02-08 20:33:31 +0000829/**
830 * snd_pcm_new_internal - create a new internal PCM instance
831 * @card: the card instance
832 * @id: the id string
833 * @device: the device index (zero based - shared with normal PCMs)
834 * @playback_count: the number of substreams for playback
835 * @capture_count: the number of substreams for capture
836 * @rpcm: the pointer to store the new pcm instance
837 *
838 * Creates a new internal PCM instance with no userspace device or procfs
839 * entries. This is used by ASoC Back End PCMs in order to create a PCM that
840 * will only be used internally by kernel drivers. i.e. it cannot be opened
841 * by userspace. It provides existing ASoC components drivers with a substream
842 * and access to any private data.
843 *
844 * The pcm operators have to be set afterwards to the new instance
845 * via snd_pcm_set_ops().
846 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100847 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000848 */
849int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
850 int playback_count, int capture_count,
851 struct snd_pcm **rpcm)
852{
853 return _snd_pcm_new(card, id, device, playback_count, capture_count,
854 true, rpcm);
855}
856EXPORT_SYMBOL(snd_pcm_new_internal);
857
Takashi Iwaia8ff48c2016-07-08 08:23:43 +0200858static void free_chmap(struct snd_pcm_str *pstr)
859{
860 if (pstr->chmap_kctl) {
861 snd_ctl_remove(pstr->pcm->card, pstr->chmap_kctl);
862 pstr->chmap_kctl = NULL;
863 }
Banajit Goswami55b24c52016-11-21 19:08:27 -0800864 if (pstr->vol_kctl) {
865 snd_ctl_remove(pstr->pcm->card, pstr->vol_kctl);
866 pstr->vol_kctl = NULL;
867 }
Banajit Goswamicec03b62016-11-21 19:22:56 -0800868 if (pstr->usr_kctl) {
869 snd_ctl_remove(pstr->pcm->card, pstr->usr_kctl);
870 pstr->usr_kctl = NULL;
871 }
Takashi Iwaia8ff48c2016-07-08 08:23:43 +0200872}
873
Takashi Iwai877211f2005-11-17 13:59:38 +0100874static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai877211f2005-11-17 13:59:38 +0100876 struct snd_pcm_substream *substream, *substream_next;
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100877#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Takashi Iwai877211f2005-11-17 13:59:38 +0100878 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#endif
880 substream = pstr->substream;
881 while (substream) {
882 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200883 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 snd_pcm_substream_proc_done(substream);
885 kfree(substream);
886 substream = substream_next;
887 }
888 snd_pcm_stream_proc_done(pstr);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100889#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
891 setupn = setup->next;
892 kfree(setup->task_name);
893 kfree(setup);
894 }
895#endif
Takashi Iwaia8ff48c2016-07-08 08:23:43 +0200896 free_chmap(pstr);
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100897 if (pstr->substream_count)
898 put_device(&pstr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Takashi Iwai877211f2005-11-17 13:59:38 +0100901static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwaic4614822006-06-23 14:38:23 +0200903 struct snd_pcm_notify *notify;
904
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200905 if (!pcm)
906 return 0;
Takashi Iwai646e1dd2015-02-20 17:04:08 +0100907 if (!pcm->internal) {
908 list_for_each_entry(notify, &snd_pcm_notify_list, list)
909 notify->n_unregister(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +0200910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (pcm->private_free)
912 pcm->private_free(pcm);
913 snd_pcm_lib_preallocate_free_for_all(pcm);
914 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
915 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
916 kfree(pcm);
917 return 0;
918}
919
Takashi Iwai877211f2005-11-17 13:59:38 +0100920static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai877211f2005-11-17 13:59:38 +0100922 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return snd_pcm_free(pcm);
924}
925
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200926int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
927 struct file *file,
928 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Takashi Iwai877211f2005-11-17 13:59:38 +0100930 struct snd_pcm_str * pstr;
931 struct snd_pcm_substream *substream;
932 struct snd_pcm_runtime *runtime;
Takashi Iwai877211f2005-11-17 13:59:38 +0100933 struct snd_card *card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100934 int prefer_subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 size_t size;
936
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200937 if (snd_BUG_ON(!pcm || !rsubstream))
938 return -ENXIO;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100939 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
940 stream != SNDRV_PCM_STREAM_CAPTURE))
941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *rsubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200944 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -ENODEV;
946
947 card = pcm->card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100948 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Takashi Iwaiad876c82015-02-20 16:26:00 +0100950 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
951 int opposite = !stream;
952
953 for (substream = pcm->streams[opposite].substream; substream;
954 substream = substream->next) {
955 if (SUBSTREAM_BUSY(substream))
956 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Takashi Iwai0df63e42006-04-28 15:13:41 +0200960 if (file->f_flags & O_APPEND) {
961 if (prefer_subdevice < 0) {
962 if (pstr->substream_count > 1)
963 return -EINVAL; /* must be unique */
964 substream = pstr->substream;
965 } else {
966 for (substream = pstr->substream; substream;
967 substream = substream->next)
968 if (substream->number == prefer_subdevice)
969 break;
970 }
971 if (! substream)
972 return -ENODEV;
973 if (! SUBSTREAM_BUSY(substream))
974 return -EBADFD;
975 substream->ref_count++;
976 *rsubstream = substream;
977 return 0;
978 }
979
Takashi Iwaiad876c82015-02-20 16:26:00 +0100980 for (substream = pstr->substream; substream; substream = substream->next) {
981 if (!SUBSTREAM_BUSY(substream) &&
982 (prefer_subdevice == -1 ||
983 substream->number == prefer_subdevice))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (substream == NULL)
987 return -EAGAIN;
988
Takashi Iwaica2c0962005-09-09 14:20:23 +0200989 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (runtime == NULL)
991 return -ENOMEM;
992
Takashi Iwai877211f2005-11-17 13:59:38 +0100993 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
995 if (runtime->status == NULL) {
996 kfree(runtime);
997 return -ENOMEM;
998 }
999 memset((void*)runtime->status, 0, size);
1000
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
1003 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001004 snd_free_pages((void*)runtime->status,
1005 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 kfree(runtime);
1007 return -ENOMEM;
1008 }
1009 memset((void*)runtime->control, 0, size);
1010
1011 init_waitqueue_head(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001012 init_waitqueue_head(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 runtime->status->state = SNDRV_PCM_STATE_OPEN;
1015
1016 substream->runtime = runtime;
1017 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001018 substream->ref_count = 1;
1019 substream->f_flags = file->f_flags;
Clemens Ladische7373b72009-11-10 10:13:30 +01001020 substream->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 pstr->substream_opened++;
1022 *rsubstream = substream;
1023 return 0;
1024}
1025
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001026void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Takashi Iwai877211f2005-11-17 13:59:38 +01001028 struct snd_pcm_runtime *runtime;
Karthikeyan Mani9a67a572017-09-28 11:06:55 -07001029 unsigned long flags = 0;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001030
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001031 if (PCM_RUNTIME_CHECK(substream))
1032 return;
Karthikeyan Mani9a67a572017-09-28 11:06:55 -07001033 spin_lock_irqsave(&substream->runtime_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->private_free != NULL)
1036 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +01001037 snd_free_pages((void*)runtime->status,
1038 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
1039 snd_free_pages((void*)runtime->control,
1040 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(runtime->hw_constraints.rules);
Takashi Iwaidd34b662018-04-02 22:41:43 +02001042 /* Avoid concurrent access to runtime via PCM timer interface */
1043 if (substream->timer)
1044 spin_lock_irq(&substream->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 substream->runtime = NULL;
Takashi Iwaidd34b662018-04-02 22:41:43 +02001046 if (substream->timer)
1047 spin_unlock_irq(&substream->timer->lock);
1048 kfree(runtime);
Clemens Ladische7373b72009-11-10 10:13:30 +01001049 put_pid(substream->pid);
1050 substream->pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 substream->pstr->substream_opened--;
Karthikeyan Mani9a67a572017-09-28 11:06:55 -07001052 spin_unlock_irqrestore(&substream->runtime_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -07001055static ssize_t show_pcm_class(struct device *dev,
1056 struct device_attribute *attr, char *buf)
Takashi Iwai9d19f482006-09-06 14:27:46 +02001057{
Takashi Iwai60b93032015-06-23 11:56:22 +02001058 struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
1059 struct snd_pcm *pcm = pstr->pcm;
Takashi Iwai9d19f482006-09-06 14:27:46 +02001060 const char *str;
1061 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1062 [SNDRV_PCM_CLASS_GENERIC] = "generic",
1063 [SNDRV_PCM_CLASS_MULTI] = "multi",
1064 [SNDRV_PCM_CLASS_MODEM] = "modem",
1065 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1066 };
1067
Takashi Iwai60b93032015-06-23 11:56:22 +02001068 if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
Takashi Iwai9d19f482006-09-06 14:27:46 +02001069 str = "none";
1070 else
1071 str = strs[pcm->dev_class];
1072 return snprintf(buf, PAGE_SIZE, "%s\n", str);
1073}
1074
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001075static DEVICE_ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1076static struct attribute *pcm_dev_attrs[] = {
1077 &dev_attr_pcm_class.attr,
1078 NULL
1079};
1080
1081static struct attribute_group pcm_dev_attr_group = {
1082 .attrs = pcm_dev_attrs,
1083};
1084
1085static const struct attribute_group *pcm_dev_attr_groups[] = {
1086 &pcm_dev_attr_group,
1087 NULL
1088};
Takashi Iwai9d19f482006-09-06 14:27:46 +02001089
Takashi Iwai877211f2005-11-17 13:59:38 +01001090static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001092 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +01001093 struct snd_pcm_substream *substream;
Johannes Berg9244b2c2006-10-05 16:02:22 +02001094 struct snd_pcm_notify *notify;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001095 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001097 if (snd_BUG_ON(!device || !device->device_data))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001098 return -ENXIO;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001099 pcm = device->device_data;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001100 if (pcm->internal)
1101 return 0;
1102
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001103 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +01001104 err = snd_pcm_add(pcm);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001105 if (err)
1106 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 for (cidx = 0; cidx < 2; cidx++) {
1108 int devtype = -1;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001109 if (pcm->streams[cidx].substream == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 continue;
1111 switch (cidx) {
1112 case SNDRV_PCM_STREAM_PLAYBACK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1114 break;
1115 case SNDRV_PCM_STREAM_CAPTURE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1117 break;
1118 }
Johannes Bergc78085f2006-10-05 15:06:34 +02001119 /* register pcm */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001120 err = snd_register_device(devtype, pcm->card, pcm->device,
1121 &snd_pcm_f_ops[cidx], pcm,
1122 &pcm->streams[cidx].dev);
Johannes Bergc78085f2006-10-05 15:06:34 +02001123 if (err < 0) {
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001124 list_del_init(&pcm->list);
1125 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1129 snd_pcm_timer_init(substream);
1130 }
Johannes Berg9244b2c2006-10-05 16:02:22 +02001131
1132 list_for_each_entry(notify, &snd_pcm_notify_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 notify->n_register(pcm);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001134
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001135 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001136 mutex_unlock(&register_mutex);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Takashi Iwai877211f2005-11-17 13:59:38 +01001140static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai877211f2005-11-17 13:59:38 +01001142 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +02001143 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +01001144 struct snd_pcm_substream *substream;
Takashi Iwai40a4b262015-01-30 08:34:58 +01001145 int cidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001147 mutex_lock(&register_mutex);
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001148 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001149 wake_up(&pcm->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001150 list_del_init(&pcm->list);
Takashi Iwai646e1dd2015-02-20 17:04:08 +01001151 for (cidx = 0; cidx < 2; cidx++) {
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001152 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1153 snd_pcm_stream_lock_irq(substream);
Takashi Iwai0914f792012-10-16 16:43:39 +02001154 if (substream->runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwai0914f792012-10-16 16:43:39 +02001156 wake_up(&substream->runtime->sleep);
1157 wake_up(&substream->runtime->tsleep);
1158 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001159 snd_pcm_stream_unlock_irq(substream);
1160 }
Takashi Iwai646e1dd2015-02-20 17:04:08 +01001161 }
1162 if (!pcm->internal) {
1163 list_for_each_entry(notify, &snd_pcm_notify_list, list)
1164 notify->n_disconnect(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 for (cidx = 0; cidx < 2; cidx++) {
Takashi Iwaib2022132015-02-20 17:05:27 +01001167 if (!pcm->internal)
1168 snd_unregister_device(&pcm->streams[cidx].dev);
Takashi Iwaia8ff48c2016-07-08 08:23:43 +02001169 free_chmap(&pcm->streams[cidx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001171 mutex_unlock(&pcm->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001172 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Takashi Iwai30b771c2014-10-30 15:02:50 +01001176/**
1177 * snd_pcm_notify - Add/remove the notify list
1178 * @notify: PCM notify list
1179 * @nfree: 0 = register, 1 = unregister
1180 *
1181 * This adds the given notifier to the global list so that the callback is
1182 * called for each registered PCM devices. This exists only for PCM OSS
1183 * emulation, so far.
1184 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001185int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Johannes Berg9244b2c2006-10-05 16:02:22 +02001187 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001189 if (snd_BUG_ON(!notify ||
1190 !notify->n_register ||
1191 !notify->n_unregister ||
1192 !notify->n_disconnect))
1193 return -EINVAL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001194 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (nfree) {
1196 list_del(&notify->list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001197 list_for_each_entry(pcm, &snd_pcm_devices, list)
1198 notify->n_unregister(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
1200 list_add_tail(&notify->list, &snd_pcm_notify_list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001201 list_for_each_entry(pcm, &snd_pcm_devices, list)
1202 notify->n_register(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001204 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001207EXPORT_SYMBOL(snd_pcm_notify);
1208
Jie Yangcd6a6502015-05-27 19:45:45 +08001209#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * Info interface
1212 */
1213
Takashi Iwai877211f2005-11-17 13:59:38 +01001214static void snd_pcm_proc_read(struct snd_info_entry *entry,
1215 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwai877211f2005-11-17 13:59:38 +01001217 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001219 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001220 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01001221 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1222 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001224 snd_iprintf(buffer, " : playback %i",
1225 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001227 snd_iprintf(buffer, " : capture %i",
1228 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 snd_iprintf(buffer, "\n");
1230 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001231 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001234static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Takashi Iwaie28563c2005-12-01 10:42:42 +01001236static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Takashi Iwai877211f2005-11-17 13:59:38 +01001238 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001241 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (snd_info_register(entry) < 0) {
1243 snd_info_free_entry(entry);
1244 entry = NULL;
1245 }
1246 }
1247 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001248}
1249
1250static void snd_pcm_proc_done(void)
1251{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001252 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001253}
1254
Jie Yangcd6a6502015-05-27 19:45:45 +08001255#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001256#define snd_pcm_proc_init()
1257#define snd_pcm_proc_done()
Jie Yangcd6a6502015-05-27 19:45:45 +08001258#endif /* CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001259
1260
1261/*
1262 * ENTRY functions
1263 */
1264
1265static int __init alsa_pcm_init(void)
1266{
1267 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1268 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1269 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271}
1272
1273static void __exit alsa_pcm_exit(void)
1274{
1275 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1276 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001277 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280module_init(alsa_pcm_init)
1281module_exit(alsa_pcm_exit)