blob: cdff5f97648086fdb86773257ba431d9d9cd6276 [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);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200750 mutex_init(&substream->self_group.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 INIT_LIST_HEAD(&substream->self_group.substreams);
752 list_add_tail(&substream->link_list, &substream->self_group.substreams);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200753 atomic_set(&substream->mmap_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 prev = substream;
755 }
756 return 0;
757}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200758EXPORT_SYMBOL(snd_pcm_new_stream);
759
Liam Girdwood945e5032012-02-08 20:33:31 +0000760static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
761 int playback_count, int capture_count, bool internal,
762 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwai877211f2005-11-17 13:59:38 +0100764 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100766 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 .dev_free = snd_pcm_dev_free,
768 .dev_register = snd_pcm_dev_register,
769 .dev_disconnect = snd_pcm_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 };
771
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200772 if (snd_BUG_ON(!card))
773 return -ENXIO;
774 if (rpcm)
775 *rpcm = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200776 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100777 if (!pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -ENOMEM;
779 pcm->card = card;
780 pcm->device = device;
Liam Girdwood945e5032012-02-08 20:33:31 +0000781 pcm->internal = internal;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +0100782 mutex_init(&pcm->open_mutex);
783 init_waitqueue_head(&pcm->open_wait);
784 INIT_LIST_HEAD(&pcm->list);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100785 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
788 snd_pcm_free(pcm);
789 return err;
790 }
791 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
792 snd_pcm_free(pcm);
793 return err;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
796 snd_pcm_free(pcm);
797 return err;
798 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200799 if (rpcm)
800 *rpcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
Liam Girdwood945e5032012-02-08 20:33:31 +0000804/**
805 * snd_pcm_new - create a new PCM instance
806 * @card: the card instance
807 * @id: the id string
808 * @device: the device index (zero based)
809 * @playback_count: the number of substreams for playback
810 * @capture_count: the number of substreams for capture
811 * @rpcm: the pointer to store the new pcm instance
812 *
813 * Creates a new PCM instance.
814 *
815 * The pcm operators have to be set afterwards to the new instance
816 * via snd_pcm_set_ops().
817 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100818 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000819 */
820int snd_pcm_new(struct snd_card *card, const char *id, int device,
821 int playback_count, int capture_count, struct snd_pcm **rpcm)
822{
823 return _snd_pcm_new(card, id, device, playback_count, capture_count,
824 false, rpcm);
825}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200826EXPORT_SYMBOL(snd_pcm_new);
827
Liam Girdwood945e5032012-02-08 20:33:31 +0000828/**
829 * snd_pcm_new_internal - create a new internal PCM instance
830 * @card: the card instance
831 * @id: the id string
832 * @device: the device index (zero based - shared with normal PCMs)
833 * @playback_count: the number of substreams for playback
834 * @capture_count: the number of substreams for capture
835 * @rpcm: the pointer to store the new pcm instance
836 *
837 * Creates a new internal PCM instance with no userspace device or procfs
838 * entries. This is used by ASoC Back End PCMs in order to create a PCM that
839 * will only be used internally by kernel drivers. i.e. it cannot be opened
840 * by userspace. It provides existing ASoC components drivers with a substream
841 * and access to any private data.
842 *
843 * The pcm operators have to be set afterwards to the new instance
844 * via snd_pcm_set_ops().
845 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100846 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000847 */
848int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
849 int playback_count, int capture_count,
850 struct snd_pcm **rpcm)
851{
852 return _snd_pcm_new(card, id, device, playback_count, capture_count,
853 true, rpcm);
854}
855EXPORT_SYMBOL(snd_pcm_new_internal);
856
Takashi Iwaia8ff48c2016-07-08 08:23:43 +0200857static void free_chmap(struct snd_pcm_str *pstr)
858{
859 if (pstr->chmap_kctl) {
860 snd_ctl_remove(pstr->pcm->card, pstr->chmap_kctl);
861 pstr->chmap_kctl = NULL;
862 }
863}
864
Takashi Iwai877211f2005-11-17 13:59:38 +0100865static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 struct snd_pcm_substream *substream, *substream_next;
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100868#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Takashi Iwai877211f2005-11-17 13:59:38 +0100869 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#endif
871 substream = pstr->substream;
872 while (substream) {
873 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200874 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 snd_pcm_substream_proc_done(substream);
876 kfree(substream);
877 substream = substream_next;
878 }
879 snd_pcm_stream_proc_done(pstr);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100880#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
882 setupn = setup->next;
883 kfree(setup->task_name);
884 kfree(setup);
885 }
886#endif
Takashi Iwaia8ff48c2016-07-08 08:23:43 +0200887 free_chmap(pstr);
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100888 if (pstr->substream_count)
889 put_device(&pstr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Takashi Iwai877211f2005-11-17 13:59:38 +0100892static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwaic4614822006-06-23 14:38:23 +0200894 struct snd_pcm_notify *notify;
895
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200896 if (!pcm)
897 return 0;
Takashi Iwai646e1dd2015-02-20 17:04:08 +0100898 if (!pcm->internal) {
899 list_for_each_entry(notify, &snd_pcm_notify_list, list)
900 notify->n_unregister(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +0200901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (pcm->private_free)
903 pcm->private_free(pcm);
904 snd_pcm_lib_preallocate_free_for_all(pcm);
905 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
906 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
907 kfree(pcm);
908 return 0;
909}
910
Takashi Iwai877211f2005-11-17 13:59:38 +0100911static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai877211f2005-11-17 13:59:38 +0100913 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return snd_pcm_free(pcm);
915}
916
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200917int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
918 struct file *file,
919 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm_str * pstr;
922 struct snd_pcm_substream *substream;
923 struct snd_pcm_runtime *runtime;
Takashi Iwai877211f2005-11-17 13:59:38 +0100924 struct snd_card *card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100925 int prefer_subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 size_t size;
927
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200928 if (snd_BUG_ON(!pcm || !rsubstream))
929 return -ENXIO;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100930 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
931 stream != SNDRV_PCM_STREAM_CAPTURE))
932 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *rsubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200935 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -ENODEV;
937
938 card = pcm->card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100939 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Takashi Iwaiad876c82015-02-20 16:26:00 +0100941 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
942 int opposite = !stream;
943
944 for (substream = pcm->streams[opposite].substream; substream;
945 substream = substream->next) {
946 if (SUBSTREAM_BUSY(substream))
947 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Takashi Iwai0df63e42006-04-28 15:13:41 +0200951 if (file->f_flags & O_APPEND) {
952 if (prefer_subdevice < 0) {
953 if (pstr->substream_count > 1)
954 return -EINVAL; /* must be unique */
955 substream = pstr->substream;
956 } else {
957 for (substream = pstr->substream; substream;
958 substream = substream->next)
959 if (substream->number == prefer_subdevice)
960 break;
961 }
962 if (! substream)
963 return -ENODEV;
964 if (! SUBSTREAM_BUSY(substream))
965 return -EBADFD;
966 substream->ref_count++;
967 *rsubstream = substream;
968 return 0;
969 }
970
Takashi Iwaiad876c82015-02-20 16:26:00 +0100971 for (substream = pstr->substream; substream; substream = substream->next) {
972 if (!SUBSTREAM_BUSY(substream) &&
973 (prefer_subdevice == -1 ||
974 substream->number == prefer_subdevice))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (substream == NULL)
978 return -EAGAIN;
979
Takashi Iwaica2c0962005-09-09 14:20:23 +0200980 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (runtime == NULL)
982 return -ENOMEM;
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
986 if (runtime->status == NULL) {
987 kfree(runtime);
988 return -ENOMEM;
989 }
990 memset((void*)runtime->status, 0, size);
991
Takashi Iwai877211f2005-11-17 13:59:38 +0100992 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
994 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100995 snd_free_pages((void*)runtime->status,
996 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 kfree(runtime);
998 return -ENOMEM;
999 }
1000 memset((void*)runtime->control, 0, size);
1001
1002 init_waitqueue_head(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001003 init_waitqueue_head(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 runtime->status->state = SNDRV_PCM_STATE_OPEN;
1006
1007 substream->runtime = runtime;
1008 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001009 substream->ref_count = 1;
1010 substream->f_flags = file->f_flags;
Clemens Ladische7373b72009-11-10 10:13:30 +01001011 substream->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 pstr->substream_opened++;
1013 *rsubstream = substream;
1014 return 0;
1015}
1016
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001017void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Takashi Iwai877211f2005-11-17 13:59:38 +01001019 struct snd_pcm_runtime *runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001020
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001021 if (PCM_RUNTIME_CHECK(substream))
1022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (runtime->private_free != NULL)
1025 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +01001026 snd_free_pages((void*)runtime->status,
1027 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
1028 snd_free_pages((void*)runtime->control,
1029 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree(runtime->hw_constraints.rules);
Takashi Iwaidd34b662018-04-02 22:41:43 +02001031 /* Avoid concurrent access to runtime via PCM timer interface */
1032 if (substream->timer)
1033 spin_lock_irq(&substream->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 substream->runtime = NULL;
Takashi Iwaidd34b662018-04-02 22:41:43 +02001035 if (substream->timer)
1036 spin_unlock_irq(&substream->timer->lock);
1037 kfree(runtime);
Clemens Ladische7373b72009-11-10 10:13:30 +01001038 put_pid(substream->pid);
1039 substream->pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 substream->pstr->substream_opened--;
1041}
1042
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -07001043static ssize_t show_pcm_class(struct device *dev,
1044 struct device_attribute *attr, char *buf)
Takashi Iwai9d19f482006-09-06 14:27:46 +02001045{
Takashi Iwai60b93032015-06-23 11:56:22 +02001046 struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
1047 struct snd_pcm *pcm = pstr->pcm;
Takashi Iwai9d19f482006-09-06 14:27:46 +02001048 const char *str;
1049 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1050 [SNDRV_PCM_CLASS_GENERIC] = "generic",
1051 [SNDRV_PCM_CLASS_MULTI] = "multi",
1052 [SNDRV_PCM_CLASS_MODEM] = "modem",
1053 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1054 };
1055
Takashi Iwai60b93032015-06-23 11:56:22 +02001056 if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
Takashi Iwai9d19f482006-09-06 14:27:46 +02001057 str = "none";
1058 else
1059 str = strs[pcm->dev_class];
1060 return snprintf(buf, PAGE_SIZE, "%s\n", str);
1061}
1062
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001063static DEVICE_ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1064static struct attribute *pcm_dev_attrs[] = {
1065 &dev_attr_pcm_class.attr,
1066 NULL
1067};
1068
1069static struct attribute_group pcm_dev_attr_group = {
1070 .attrs = pcm_dev_attrs,
1071};
1072
1073static const struct attribute_group *pcm_dev_attr_groups[] = {
1074 &pcm_dev_attr_group,
1075 NULL
1076};
Takashi Iwai9d19f482006-09-06 14:27:46 +02001077
Takashi Iwai877211f2005-11-17 13:59:38 +01001078static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001080 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +01001081 struct snd_pcm_substream *substream;
Johannes Berg9244b2c2006-10-05 16:02:22 +02001082 struct snd_pcm_notify *notify;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001083 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001085 if (snd_BUG_ON(!device || !device->device_data))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001086 return -ENXIO;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001087 pcm = device->device_data;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001088 if (pcm->internal)
1089 return 0;
1090
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001091 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +01001092 err = snd_pcm_add(pcm);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001093 if (err)
1094 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 for (cidx = 0; cidx < 2; cidx++) {
1096 int devtype = -1;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001097 if (pcm->streams[cidx].substream == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 continue;
1099 switch (cidx) {
1100 case SNDRV_PCM_STREAM_PLAYBACK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1102 break;
1103 case SNDRV_PCM_STREAM_CAPTURE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1105 break;
1106 }
Johannes Bergc78085f2006-10-05 15:06:34 +02001107 /* register pcm */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001108 err = snd_register_device(devtype, pcm->card, pcm->device,
1109 &snd_pcm_f_ops[cidx], pcm,
1110 &pcm->streams[cidx].dev);
Johannes Bergc78085f2006-10-05 15:06:34 +02001111 if (err < 0) {
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001112 list_del_init(&pcm->list);
1113 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1117 snd_pcm_timer_init(substream);
1118 }
Johannes Berg9244b2c2006-10-05 16:02:22 +02001119
1120 list_for_each_entry(notify, &snd_pcm_notify_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 notify->n_register(pcm);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001122
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001123 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001124 mutex_unlock(&register_mutex);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Takashi Iwai877211f2005-11-17 13:59:38 +01001128static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwai877211f2005-11-17 13:59:38 +01001130 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +02001131 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +01001132 struct snd_pcm_substream *substream;
Takashi Iwai40a4b262015-01-30 08:34:58 +01001133 int cidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001135 mutex_lock(&register_mutex);
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001136 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001137 wake_up(&pcm->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001138 list_del_init(&pcm->list);
Takashi Iwai646e1dd2015-02-20 17:04:08 +01001139 for (cidx = 0; cidx < 2; cidx++) {
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001140 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1141 snd_pcm_stream_lock_irq(substream);
Takashi Iwai0914f792012-10-16 16:43:39 +02001142 if (substream->runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwai0914f792012-10-16 16:43:39 +02001144 wake_up(&substream->runtime->sleep);
1145 wake_up(&substream->runtime->tsleep);
1146 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001147 snd_pcm_stream_unlock_irq(substream);
1148 }
Takashi Iwai646e1dd2015-02-20 17:04:08 +01001149 }
1150 if (!pcm->internal) {
1151 list_for_each_entry(notify, &snd_pcm_notify_list, list)
1152 notify->n_disconnect(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 for (cidx = 0; cidx < 2; cidx++) {
Takashi Iwaib2022132015-02-20 17:05:27 +01001155 if (!pcm->internal)
1156 snd_unregister_device(&pcm->streams[cidx].dev);
Takashi Iwaia8ff48c2016-07-08 08:23:43 +02001157 free_chmap(&pcm->streams[cidx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001159 mutex_unlock(&pcm->open_mutex);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001160 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Takashi Iwai30b771c2014-10-30 15:02:50 +01001164/**
1165 * snd_pcm_notify - Add/remove the notify list
1166 * @notify: PCM notify list
1167 * @nfree: 0 = register, 1 = unregister
1168 *
1169 * This adds the given notifier to the global list so that the callback is
1170 * called for each registered PCM devices. This exists only for PCM OSS
1171 * emulation, so far.
1172 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001173int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Johannes Berg9244b2c2006-10-05 16:02:22 +02001175 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001177 if (snd_BUG_ON(!notify ||
1178 !notify->n_register ||
1179 !notify->n_unregister ||
1180 !notify->n_disconnect))
1181 return -EINVAL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001182 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (nfree) {
1184 list_del(&notify->list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001185 list_for_each_entry(pcm, &snd_pcm_devices, list)
1186 notify->n_unregister(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 } else {
1188 list_add_tail(&notify->list, &snd_pcm_notify_list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001189 list_for_each_entry(pcm, &snd_pcm_devices, list)
1190 notify->n_register(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001192 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return 0;
1194}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001195EXPORT_SYMBOL(snd_pcm_notify);
1196
Jie Yangcd6a6502015-05-27 19:45:45 +08001197#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/*
1199 * Info interface
1200 */
1201
Takashi Iwai877211f2005-11-17 13:59:38 +01001202static void snd_pcm_proc_read(struct snd_info_entry *entry,
1203 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Takashi Iwai877211f2005-11-17 13:59:38 +01001205 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001207 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001208 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01001209 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1210 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001212 snd_iprintf(buffer, " : playback %i",
1213 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 snd_iprintf(buffer, " : capture %i",
1216 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 snd_iprintf(buffer, "\n");
1218 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001219 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001222static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Takashi Iwaie28563c2005-12-01 10:42:42 +01001224static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Takashi Iwai877211f2005-11-17 13:59:38 +01001226 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001229 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (snd_info_register(entry) < 0) {
1231 snd_info_free_entry(entry);
1232 entry = NULL;
1233 }
1234 }
1235 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001236}
1237
1238static void snd_pcm_proc_done(void)
1239{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001240 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001241}
1242
Jie Yangcd6a6502015-05-27 19:45:45 +08001243#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001244#define snd_pcm_proc_init()
1245#define snd_pcm_proc_done()
Jie Yangcd6a6502015-05-27 19:45:45 +08001246#endif /* CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001247
1248
1249/*
1250 * ENTRY functions
1251 */
1252
1253static int __init alsa_pcm_init(void)
1254{
1255 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1256 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1257 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
1259}
1260
1261static void __exit alsa_pcm_exit(void)
1262{
1263 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1264 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001265 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268module_init(alsa_pcm_init)
1269module_exit(alsa_pcm_exit)