blob: d440629e08e2da242a4c8d1c52586adab8ba32c4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/pcm.h>
31#include <sound/control.h>
32#include <sound/info.h>
33
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020034MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
36MODULE_LICENSE("GPL");
37
Clemens Ladischf87135f2005-11-20 14:06:59 +010038static LIST_HEAD(snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static LIST_HEAD(snd_pcm_notify_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010040static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai877211f2005-11-17 13:59:38 +010042static int snd_pcm_free(struct snd_pcm *pcm);
43static int snd_pcm_dev_free(struct snd_device *device);
44static int snd_pcm_dev_register(struct snd_device *device);
45static int snd_pcm_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Pawel MOLLf90c06a2008-07-30 12:46:40 +010047static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
Clemens Ladischf87135f2005-11-20 14:06:59 +010048{
Clemens Ladischf87135f2005-11-20 14:06:59 +010049 struct snd_pcm *pcm;
50
Johannes Berg9244b2c2006-10-05 16:02:22 +020051 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010052 if (pcm->card == card && pcm->device == device)
53 return pcm;
54 }
55 return NULL;
56}
57
Pawel MOLLf90c06a2008-07-30 12:46:40 +010058static int snd_pcm_next(struct snd_card *card, int device)
59{
60 struct snd_pcm *pcm;
61
62 list_for_each_entry(pcm, &snd_pcm_devices, list) {
63 if (pcm->card == card && pcm->device > device)
64 return pcm->device;
65 else if (pcm->card->number > card->number)
66 return -1;
67 }
68 return -1;
69}
70
71static int snd_pcm_add(struct snd_pcm *newpcm)
72{
73 struct snd_pcm *pcm;
74
Takashi Iwaib95bd3a2015-02-20 16:49:04 +010075 if (newpcm->internal)
76 return 0;
77
Pawel MOLLf90c06a2008-07-30 12:46:40 +010078 list_for_each_entry(pcm, &snd_pcm_devices, list) {
79 if (pcm->card == newpcm->card && pcm->device == newpcm->device)
80 return -EBUSY;
81 if (pcm->card->number > newpcm->card->number ||
82 (pcm->card == newpcm->card &&
83 pcm->device > newpcm->device)) {
84 list_add(&newpcm->list, pcm->list.prev);
85 return 0;
86 }
87 }
88 list_add_tail(&newpcm->list, &snd_pcm_devices);
89 return 0;
90}
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092static int snd_pcm_control_ioctl(struct snd_card *card,
93 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned int cmd, unsigned long arg)
95{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 switch (cmd) {
97 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
98 {
99 int device;
100
101 if (get_user(device, (int __user *)arg))
102 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100103 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +0100104 device = snd_pcm_next(card, device);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100105 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (put_user(device, (int __user *)arg))
107 return -EFAULT;
108 return 0;
109 }
110 case SNDRV_CTL_IOCTL_PCM_INFO:
111 {
Takashi Iwai877211f2005-11-17 13:59:38 +0100112 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +0100114 int stream;
115 struct snd_pcm *pcm;
116 struct snd_pcm_str *pstr;
117 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100118 int err;
119
Takashi Iwai877211f2005-11-17 13:59:38 +0100120 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (get_user(device, &info->device))
122 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (get_user(stream, &info->stream))
124 return -EFAULT;
125 if (stream < 0 || stream > 1)
126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (get_user(subdevice, &info->subdevice))
128 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100129 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +0100130 pcm = snd_pcm_get(card, device);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100131 if (pcm == NULL) {
132 err = -ENXIO;
133 goto _error;
134 }
135 pstr = &pcm->streams[stream];
136 if (pstr->substream_count == 0) {
137 err = -ENOENT;
138 goto _error;
139 }
140 if (subdevice >= pstr->substream_count) {
141 err = -ENXIO;
142 goto _error;
143 }
144 for (substream = pstr->substream; substream;
145 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (substream->number == (int)subdevice)
147 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100148 if (substream == NULL) {
149 err = -ENXIO;
150 goto _error;
151 }
152 err = snd_pcm_info_user(substream, info);
153 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100154 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
158 {
159 int val;
160
161 if (get_user(val, (int __user *)arg))
162 return -EFAULT;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100163 control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165 }
166 }
167 return -ENOIOCTLCMD;
168}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static char *snd_pcm_format_names[] = {
173 FORMAT(S8),
174 FORMAT(U8),
175 FORMAT(S16_LE),
176 FORMAT(S16_BE),
177 FORMAT(U16_LE),
178 FORMAT(U16_BE),
179 FORMAT(S24_LE),
180 FORMAT(S24_BE),
181 FORMAT(U24_LE),
182 FORMAT(U24_BE),
183 FORMAT(S32_LE),
184 FORMAT(S32_BE),
185 FORMAT(U32_LE),
186 FORMAT(U32_BE),
187 FORMAT(FLOAT_LE),
188 FORMAT(FLOAT_BE),
189 FORMAT(FLOAT64_LE),
190 FORMAT(FLOAT64_BE),
191 FORMAT(IEC958_SUBFRAME_LE),
192 FORMAT(IEC958_SUBFRAME_BE),
193 FORMAT(MU_LAW),
194 FORMAT(A_LAW),
195 FORMAT(IMA_ADPCM),
196 FORMAT(MPEG),
197 FORMAT(GSM),
198 FORMAT(SPECIAL),
199 FORMAT(S24_3LE),
200 FORMAT(S24_3BE),
201 FORMAT(U24_3LE),
202 FORMAT(U24_3BE),
203 FORMAT(S20_3LE),
204 FORMAT(S20_3BE),
205 FORMAT(U20_3LE),
206 FORMAT(U20_3BE),
207 FORMAT(S18_3LE),
208 FORMAT(S18_3BE),
209 FORMAT(U18_3LE),
210 FORMAT(U18_3BE),
Dan Carpenter7a288262010-08-27 22:02:15 +0200211 FORMAT(G723_24),
212 FORMAT(G723_24_1B),
213 FORMAT(G723_40),
214 FORMAT(G723_40_1B),
Daniel Mackef7a4f92013-04-17 00:01:36 +0800215 FORMAT(DSD_U8),
216 FORMAT(DSD_U16_LE),
Jurgen Kramerd4288d32014-09-05 10:47:56 +0200217 FORMAT(DSD_U32_LE),
Jussi Laakod42472e2014-11-21 16:04:46 +0200218 FORMAT(DSD_U16_BE),
219 FORMAT(DSD_U32_BE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Takashi Iwai30b771c2014-10-30 15:02:50 +0100222/**
223 * snd_pcm_format_name - Return a name string for the given PCM format
224 * @format: PCM format
225 */
Takashi Iwai6e5265e2009-09-08 14:26:51 +0200226const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100227{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100228 if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
Dan Carpenter7a288262010-08-27 22:02:15 +0200229 return "Unknown";
Clemens Ladischfea952e2011-02-14 11:00:47 +0100230 return snd_pcm_format_names[(__force unsigned int)format];
Takashi Iwaie28563c2005-12-01 10:42:42 +0100231}
Takashi Iwai6e5265e2009-09-08 14:26:51 +0200232EXPORT_SYMBOL_GPL(snd_pcm_format_name);
233
234#ifdef CONFIG_SND_VERBOSE_PROCFS
235
236#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
237#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
238#define READY(v) [SNDRV_PCM_READY_##v] = #v
239#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
240#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
241#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
242#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
243#define START(v) [SNDRV_PCM_START_##v] = #v
244#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
Takashi Iwaie28563c2005-12-01 10:42:42 +0100245
Takashi Iwaie28563c2005-12-01 10:42:42 +0100246static char *snd_pcm_stream_names[] = {
247 STREAM(PLAYBACK),
248 STREAM(CAPTURE),
249};
250
251static char *snd_pcm_state_names[] = {
252 STATE(OPEN),
253 STATE(SETUP),
254 STATE(PREPARED),
255 STATE(RUNNING),
256 STATE(XRUN),
257 STATE(DRAINING),
258 STATE(PAUSED),
259 STATE(SUSPENDED),
260};
261
262static char *snd_pcm_access_names[] = {
263 ACCESS(MMAP_INTERLEAVED),
264 ACCESS(MMAP_NONINTERLEAVED),
265 ACCESS(MMAP_COMPLEX),
266 ACCESS(RW_INTERLEAVED),
267 ACCESS(RW_NONINTERLEAVED),
268};
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static char *snd_pcm_subformat_names[] = {
271 SUBFORMAT(STD),
272};
273
274static char *snd_pcm_tstamp_mode_names[] = {
275 TSTAMP(NONE),
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100276 TSTAMP(ENABLE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Takashi Iwai877211f2005-11-17 13:59:38 +0100279static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return snd_pcm_stream_names[stream];
282}
283
284static const char *snd_pcm_access_name(snd_pcm_access_t access)
285{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100286 return snd_pcm_access_names[(__force int)access];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
290{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100291 return snd_pcm_subformat_names[(__force int)subformat];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Takashi Iwai877211f2005-11-17 13:59:38 +0100294static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return snd_pcm_tstamp_mode_names[mode];
297}
298
299static const char *snd_pcm_state_name(snd_pcm_state_t state)
300{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100301 return snd_pcm_state_names[(__force int)state];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100304#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static const char *snd_pcm_oss_format_name(int format)
308{
309 switch (format) {
310 case AFMT_MU_LAW:
311 return "MU_LAW";
312 case AFMT_A_LAW:
313 return "A_LAW";
314 case AFMT_IMA_ADPCM:
315 return "IMA_ADPCM";
316 case AFMT_U8:
317 return "U8";
318 case AFMT_S16_LE:
319 return "S16_LE";
320 case AFMT_S16_BE:
321 return "S16_BE";
322 case AFMT_S8:
323 return "S8";
324 case AFMT_U16_LE:
325 return "U16_LE";
326 case AFMT_U16_BE:
327 return "U16_BE";
328 case AFMT_MPEG:
329 return "MPEG";
330 default:
331 return "unknown";
332 }
333}
334#endif
335
Takashi Iwai877211f2005-11-17 13:59:38 +0100336static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
337 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Takashi Iwai877211f2005-11-17 13:59:38 +0100339 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int err;
341
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200342 if (! substream)
343 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 info = kmalloc(sizeof(*info), GFP_KERNEL);
346 if (! info) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100347 pcm_dbg(substream->pcm,
348 "snd_pcm_proc_info_read: cannot malloc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return;
350 }
351
352 err = snd_pcm_info(substream, info);
353 if (err < 0) {
354 snd_iprintf(buffer, "error %d\n", err);
355 kfree(info);
356 return;
357 }
358 snd_iprintf(buffer, "card: %d\n", info->card);
359 snd_iprintf(buffer, "device: %d\n", info->device);
360 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
361 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
362 snd_iprintf(buffer, "id: %s\n", info->id);
363 snd_iprintf(buffer, "name: %s\n", info->name);
364 snd_iprintf(buffer, "subname: %s\n", info->subname);
365 snd_iprintf(buffer, "class: %d\n", info->dev_class);
366 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
367 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
368 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
369 kfree(info);
370}
371
Takashi Iwai877211f2005-11-17 13:59:38 +0100372static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
373 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai877211f2005-11-17 13:59:38 +0100375 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
376 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Takashi Iwai877211f2005-11-17 13:59:38 +0100379static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
380 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Joe Perches9fe856e2010-09-04 18:52:54 -0700382 snd_pcm_proc_info_read(entry->private_data, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Takashi Iwai877211f2005-11-17 13:59:38 +0100385static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
386 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Takashi Iwai877211f2005-11-17 13:59:38 +0100388 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200389 struct snd_pcm_runtime *runtime;
390
391 mutex_lock(&substream->pcm->open_mutex);
392 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!runtime) {
394 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200395 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
398 snd_iprintf(buffer, "no setup\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200399 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
402 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
403 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
404 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
405 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
406 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
407 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100408#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (substream->oss.oss) {
410 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
411 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
412 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
413 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
414 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
415 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
416 }
417#endif
Takashi Iwai901d46d2010-09-16 23:06:50 +0200418 unlock:
419 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Takashi Iwai877211f2005-11-17 13:59:38 +0100422static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
423 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Takashi Iwai877211f2005-11-17 13:59:38 +0100425 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200426 struct snd_pcm_runtime *runtime;
427
428 mutex_lock(&substream->pcm->open_mutex);
429 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!runtime) {
431 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200432 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
435 snd_iprintf(buffer, "no setup\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200436 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
439 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
442 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
443 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
444 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
445 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200446 unlock:
447 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Takashi Iwai877211f2005-11-17 13:59:38 +0100450static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
451 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Takashi Iwai877211f2005-11-17 13:59:38 +0100453 struct snd_pcm_substream *substream = entry->private_data;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200454 struct snd_pcm_runtime *runtime;
Takashi Iwai877211f2005-11-17 13:59:38 +0100455 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int err;
Takashi Iwai901d46d2010-09-16 23:06:50 +0200457
458 mutex_lock(&substream->pcm->open_mutex);
459 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!runtime) {
461 snd_iprintf(buffer, "closed\n");
Takashi Iwai901d46d2010-09-16 23:06:50 +0200462 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 memset(&status, 0, sizeof(status));
465 err = snd_pcm_status(substream, &status);
466 if (err < 0) {
467 snd_iprintf(buffer, "error %d\n", err);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200468 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
Clemens Ladische7373b72009-11-10 10:13:30 +0100471 snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
473 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
474 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
475 status.tstamp.tv_sec, status.tstamp.tv_nsec);
476 snd_iprintf(buffer, "delay : %ld\n", status.delay);
477 snd_iprintf(buffer, "avail : %ld\n", status.avail);
478 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
479 snd_iprintf(buffer, "-----\n");
480 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
481 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
Takashi Iwai901d46d2010-09-16 23:06:50 +0200482 unlock:
483 mutex_unlock(&substream->pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200486#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai2b30d412014-11-04 14:02:40 +0100487static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
488 struct snd_info_buffer *buffer)
489{
490 struct snd_pcm_substream *substream = entry->private_data;
491 struct snd_pcm_runtime *runtime;
492
493 snd_pcm_stream_lock_irq(substream);
494 runtime = substream->runtime;
495 if (runtime && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
496 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
497 snd_pcm_stream_unlock_irq(substream);
498}
499
Takashi Iwai877211f2005-11-17 13:59:38 +0100500static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
501 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Takashi Iwai877211f2005-11-17 13:59:38 +0100503 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
505}
506
Takashi Iwai877211f2005-11-17 13:59:38 +0100507static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
508 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Takashi Iwai877211f2005-11-17 13:59:38 +0100510 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 char line[64];
512 if (!snd_info_get_line(buffer, line, sizeof(line)))
513 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
514}
515#endif
516
Takashi Iwai877211f2005-11-17 13:59:38 +0100517static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai877211f2005-11-17 13:59:38 +0100519 struct snd_pcm *pcm = pstr->pcm;
520 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char name[16];
522
523 sprintf(name, "pcm%i%c", pcm->device,
524 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
525 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
526 return -ENOMEM;
527 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
528 if (snd_info_register(entry) < 0) {
529 snd_info_free_entry(entry);
530 return -ENOMEM;
531 }
532 pstr->proc_root = entry;
533
534 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200535 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (snd_info_register(entry) < 0) {
537 snd_info_free_entry(entry);
538 entry = NULL;
539 }
540 }
541 pstr->proc_info_entry = entry;
542
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200543#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100544 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
545 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200548 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 entry->private_data = pstr;
550 if (snd_info_register(entry) < 0) {
551 snd_info_free_entry(entry);
552 entry = NULL;
553 }
554 }
555 pstr->proc_xrun_debug_entry = entry;
556#endif
557 return 0;
558}
559
Takashi Iwai877211f2005-11-17 13:59:38 +0100560static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200562#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai746d4a02006-06-23 14:37:59 +0200563 snd_info_free_entry(pstr->proc_xrun_debug_entry);
564 pstr->proc_xrun_debug_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200566 snd_info_free_entry(pstr->proc_info_entry);
567 pstr->proc_info_entry = NULL;
568 snd_info_free_entry(pstr->proc_root);
569 pstr->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
Takashi Iwai877211f2005-11-17 13:59:38 +0100573static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai877211f2005-11-17 13:59:38 +0100575 struct snd_info_entry *entry;
576 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 char name[16];
578
579 card = substream->pcm->card;
580
581 sprintf(name, "sub%i", substream->number);
582 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
583 return -ENOMEM;
584 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
585 if (snd_info_register(entry) < 0) {
586 snd_info_free_entry(entry);
587 return -ENOMEM;
588 }
589 substream->proc_root = entry;
590
591 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200592 snd_info_set_text_ops(entry, substream,
593 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (snd_info_register(entry) < 0) {
595 snd_info_free_entry(entry);
596 entry = NULL;
597 }
598 }
599 substream->proc_info_entry = entry;
600
601 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200602 snd_info_set_text_ops(entry, substream,
603 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (snd_info_register(entry) < 0) {
605 snd_info_free_entry(entry);
606 entry = NULL;
607 }
608 }
609 substream->proc_hw_params_entry = entry;
610
611 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200612 snd_info_set_text_ops(entry, substream,
613 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (snd_info_register(entry) < 0) {
615 snd_info_free_entry(entry);
616 entry = NULL;
617 }
618 }
619 substream->proc_sw_params_entry = entry;
620
621 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200622 snd_info_set_text_ops(entry, substream,
623 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (snd_info_register(entry) < 0) {
625 snd_info_free_entry(entry);
626 entry = NULL;
627 }
628 }
629 substream->proc_status_entry = entry;
630
Takashi Iwai2b30d412014-11-04 14:02:40 +0100631#ifdef CONFIG_SND_PCM_XRUN_DEBUG
632 entry = snd_info_create_card_entry(card, "xrun_injection",
633 substream->proc_root);
634 if (entry) {
635 entry->private_data = substream;
636 entry->c.text.read = NULL;
637 entry->c.text.write = snd_pcm_xrun_injection_write;
638 entry->mode = S_IFREG | S_IWUSR;
639 if (snd_info_register(entry) < 0) {
640 snd_info_free_entry(entry);
641 entry = NULL;
642 }
643 }
644 substream->proc_xrun_injection_entry = entry;
645#endif /* CONFIG_SND_PCM_XRUN_DEBUG */
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648}
Takashi Iwai746d4a02006-06-23 14:37:59 +0200649
Takashi Iwai877211f2005-11-17 13:59:38 +0100650static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200652 snd_info_free_entry(substream->proc_info_entry);
653 substream->proc_info_entry = NULL;
654 snd_info_free_entry(substream->proc_hw_params_entry);
655 substream->proc_hw_params_entry = NULL;
656 snd_info_free_entry(substream->proc_sw_params_entry);
657 substream->proc_sw_params_entry = NULL;
658 snd_info_free_entry(substream->proc_status_entry);
659 substream->proc_status_entry = NULL;
Takashi Iwai2b30d412014-11-04 14:02:40 +0100660#ifdef CONFIG_SND_PCM_XRUN_DEBUG
661 snd_info_free_entry(substream->proc_xrun_injection_entry);
662 substream->proc_xrun_injection_entry = NULL;
663#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200664 snd_info_free_entry(substream->proc_root);
665 substream->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200668#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100669static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
670static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
671static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
672static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200673#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100675static const struct attribute_group *pcm_dev_attr_groups[];
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
678 * snd_pcm_new_stream - create a new PCM stream
679 * @pcm: the pcm instance
680 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
681 * @substream_count: the number of substreams
682 *
683 * Creates a new stream for the pcm.
684 * The corresponding stream on the pcm must have been empty before
685 * calling this, i.e. zero must be given to the argument of
686 * snd_pcm_new().
687 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100688 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100690int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100693 struct snd_pcm_str *pstr = &pcm->streams[stream];
694 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100696#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100697 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
699 pstr->stream = stream;
700 pstr->pcm = pcm;
701 pstr->substream_count = substream_count;
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100702 if (!substream_count)
703 return 0;
704
705 snd_device_initialize(&pstr->dev, pcm->card);
706 pstr->dev.groups = pcm_dev_attr_groups;
707 dev_set_name(&pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
708 stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
709
710 if (!pcm->internal) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100712 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100713 pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 prev = NULL;
718 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200719 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100720 if (substream == NULL) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100721 pcm_err(pcm, "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 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 Iwai73e77ba2005-11-17 17:44:01 +0100777 if (pcm == NULL) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100778 dev_err(card->dev, "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 pcm->card = card;
782 pcm->device = device;
Liam Girdwood945e5032012-02-08 20:33:31 +0000783 pcm->internal = internal;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +0100784 mutex_init(&pcm->open_mutex);
785 init_waitqueue_head(&pcm->open_wait);
786 INIT_LIST_HEAD(&pcm->list);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100787 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
790 snd_pcm_free(pcm);
791 return err;
792 }
793 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
794 snd_pcm_free(pcm);
795 return err;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
798 snd_pcm_free(pcm);
799 return err;
800 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200801 if (rpcm)
802 *rpcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804}
805
Liam Girdwood945e5032012-02-08 20:33:31 +0000806/**
807 * snd_pcm_new - create a new PCM instance
808 * @card: the card instance
809 * @id: the id string
810 * @device: the device index (zero based)
811 * @playback_count: the number of substreams for playback
812 * @capture_count: the number of substreams for capture
813 * @rpcm: the pointer to store the new pcm instance
814 *
815 * Creates a new PCM instance.
816 *
817 * The pcm operators have to be set afterwards to the new instance
818 * via snd_pcm_set_ops().
819 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100820 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000821 */
822int snd_pcm_new(struct snd_card *card, const char *id, int device,
823 int playback_count, int capture_count, struct snd_pcm **rpcm)
824{
825 return _snd_pcm_new(card, id, device, playback_count, capture_count,
826 false, rpcm);
827}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200828EXPORT_SYMBOL(snd_pcm_new);
829
Liam Girdwood945e5032012-02-08 20:33:31 +0000830/**
831 * snd_pcm_new_internal - create a new internal PCM instance
832 * @card: the card instance
833 * @id: the id string
834 * @device: the device index (zero based - shared with normal PCMs)
835 * @playback_count: the number of substreams for playback
836 * @capture_count: the number of substreams for capture
837 * @rpcm: the pointer to store the new pcm instance
838 *
839 * Creates a new internal PCM instance with no userspace device or procfs
840 * entries. This is used by ASoC Back End PCMs in order to create a PCM that
841 * will only be used internally by kernel drivers. i.e. it cannot be opened
842 * by userspace. It provides existing ASoC components drivers with a substream
843 * and access to any private data.
844 *
845 * The pcm operators have to be set afterwards to the new instance
846 * via snd_pcm_set_ops().
847 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100848 * Return: Zero if successful, or a negative error code on failure.
Liam Girdwood945e5032012-02-08 20:33:31 +0000849 */
850int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
851 int playback_count, int capture_count,
852 struct snd_pcm **rpcm)
853{
854 return _snd_pcm_new(card, id, device, playback_count, capture_count,
855 true, rpcm);
856}
857EXPORT_SYMBOL(snd_pcm_new_internal);
858
Takashi Iwai877211f2005-11-17 13:59:38 +0100859static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai877211f2005-11-17 13:59:38 +0100861 struct snd_pcm_substream *substream, *substream_next;
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100862#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Takashi Iwai877211f2005-11-17 13:59:38 +0100863 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#endif
865 substream = pstr->substream;
866 while (substream) {
867 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200868 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 snd_pcm_substream_proc_done(substream);
870 kfree(substream);
871 substream = substream_next;
872 }
873 snd_pcm_stream_proc_done(pstr);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100874#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
876 setupn = setup->next;
877 kfree(setup->task_name);
878 kfree(setup);
879 }
880#endif
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100881 if (pstr->substream_count)
882 put_device(&pstr->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Takashi Iwai877211f2005-11-17 13:59:38 +0100885static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwaic4614822006-06-23 14:38:23 +0200887 struct snd_pcm_notify *notify;
888
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200889 if (!pcm)
890 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200891 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
892 notify->n_unregister(pcm);
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (pcm->private_free)
895 pcm->private_free(pcm);
896 snd_pcm_lib_preallocate_free_for_all(pcm);
897 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
898 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
899 kfree(pcm);
900 return 0;
901}
902
Takashi Iwai877211f2005-11-17 13:59:38 +0100903static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai877211f2005-11-17 13:59:38 +0100905 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return snd_pcm_free(pcm);
907}
908
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200909int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
910 struct file *file,
911 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai877211f2005-11-17 13:59:38 +0100913 struct snd_pcm_str * pstr;
914 struct snd_pcm_substream *substream;
915 struct snd_pcm_runtime *runtime;
Takashi Iwai877211f2005-11-17 13:59:38 +0100916 struct snd_card *card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100917 int prefer_subdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 size_t size;
919
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200920 if (snd_BUG_ON(!pcm || !rsubstream))
921 return -ENXIO;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100922 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
923 stream != SNDRV_PCM_STREAM_CAPTURE))
924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *rsubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200927 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ENODEV;
929
930 card = pcm->card;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100931 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Takashi Iwaiad876c82015-02-20 16:26:00 +0100933 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
934 int opposite = !stream;
935
936 for (substream = pcm->streams[opposite].substream; substream;
937 substream = substream->next) {
938 if (SUBSTREAM_BUSY(substream))
939 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Takashi Iwai0df63e42006-04-28 15:13:41 +0200943 if (file->f_flags & O_APPEND) {
944 if (prefer_subdevice < 0) {
945 if (pstr->substream_count > 1)
946 return -EINVAL; /* must be unique */
947 substream = pstr->substream;
948 } else {
949 for (substream = pstr->substream; substream;
950 substream = substream->next)
951 if (substream->number == prefer_subdevice)
952 break;
953 }
954 if (! substream)
955 return -ENODEV;
956 if (! SUBSTREAM_BUSY(substream))
957 return -EBADFD;
958 substream->ref_count++;
959 *rsubstream = substream;
960 return 0;
961 }
962
Takashi Iwaiad876c82015-02-20 16:26:00 +0100963 for (substream = pstr->substream; substream; substream = substream->next) {
964 if (!SUBSTREAM_BUSY(substream) &&
965 (prefer_subdevice == -1 ||
966 substream->number == prefer_subdevice))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
Takashi Iwaiad876c82015-02-20 16:26:00 +0100968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (substream == NULL)
970 return -EAGAIN;
971
Takashi Iwaica2c0962005-09-09 14:20:23 +0200972 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (runtime == NULL)
974 return -ENOMEM;
975
Takashi Iwai877211f2005-11-17 13:59:38 +0100976 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
978 if (runtime->status == NULL) {
979 kfree(runtime);
980 return -ENOMEM;
981 }
982 memset((void*)runtime->status, 0, size);
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
986 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100987 snd_free_pages((void*)runtime->status,
988 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 kfree(runtime);
990 return -ENOMEM;
991 }
992 memset((void*)runtime->control, 0, size);
993
994 init_waitqueue_head(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100995 init_waitqueue_head(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 runtime->status->state = SNDRV_PCM_STATE_OPEN;
998
999 substream->runtime = runtime;
1000 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001001 substream->ref_count = 1;
1002 substream->f_flags = file->f_flags;
Clemens Ladische7373b72009-11-10 10:13:30 +01001003 substream->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pstr->substream_opened++;
1005 *rsubstream = substream;
1006 return 0;
1007}
1008
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001009void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 struct snd_pcm_runtime *runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001012
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001013 if (PCM_RUNTIME_CHECK(substream))
1014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (runtime->private_free != NULL)
1017 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +01001018 snd_free_pages((void*)runtime->status,
1019 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
1020 snd_free_pages((void*)runtime->control,
1021 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 kfree(runtime->hw_constraints.rules);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +01001023#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Sachin Kamat51d503d2012-11-21 14:36:54 +05301024 kfree(runtime->hwptr_log);
Jaroslav Kysela4d96eb22009-12-20 11:47:57 +01001025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 kfree(runtime);
1027 substream->runtime = NULL;
Clemens Ladische7373b72009-11-10 10:13:30 +01001028 put_pid(substream->pid);
1029 substream->pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 substream->pstr->substream_opened--;
1031}
1032
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -07001033static ssize_t show_pcm_class(struct device *dev,
1034 struct device_attribute *attr, char *buf)
Takashi Iwai9d19f482006-09-06 14:27:46 +02001035{
1036 struct snd_pcm *pcm;
1037 const char *str;
1038 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1039 [SNDRV_PCM_CLASS_GENERIC] = "generic",
1040 [SNDRV_PCM_CLASS_MULTI] = "multi",
1041 [SNDRV_PCM_CLASS_MODEM] = "modem",
1042 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1043 };
1044
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -07001045 if (! (pcm = dev_get_drvdata(dev)) ||
Takashi Iwai9d19f482006-09-06 14:27:46 +02001046 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
1047 str = "none";
1048 else
1049 str = strs[pcm->dev_class];
1050 return snprintf(buf, PAGE_SIZE, "%s\n", str);
1051}
1052
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001053static DEVICE_ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1054static struct attribute *pcm_dev_attrs[] = {
1055 &dev_attr_pcm_class.attr,
1056 NULL
1057};
1058
1059static struct attribute_group pcm_dev_attr_group = {
1060 .attrs = pcm_dev_attrs,
1061};
1062
1063static const struct attribute_group *pcm_dev_attr_groups[] = {
1064 &pcm_dev_attr_group,
1065 NULL
1066};
Takashi Iwai9d19f482006-09-06 14:27:46 +02001067
Takashi Iwai877211f2005-11-17 13:59:38 +01001068static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001070 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_substream *substream;
Johannes Berg9244b2c2006-10-05 16:02:22 +02001072 struct snd_pcm_notify *notify;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001073 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001075 if (snd_BUG_ON(!device || !device->device_data))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001076 return -ENXIO;
Julia Lawall4b3be6a2009-10-17 08:33:22 +02001077 pcm = device->device_data;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001078 if (pcm->internal)
1079 return 0;
1080
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001081 mutex_lock(&register_mutex);
Pawel MOLLf90c06a2008-07-30 12:46:40 +01001082 err = snd_pcm_add(pcm);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001083 if (err)
1084 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 for (cidx = 0; cidx < 2; cidx++) {
1086 int devtype = -1;
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001087 if (pcm->streams[cidx].substream == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 continue;
1089 switch (cidx) {
1090 case SNDRV_PCM_STREAM_PLAYBACK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1092 break;
1093 case SNDRV_PCM_STREAM_CAPTURE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1095 break;
1096 }
Johannes Bergc78085f2006-10-05 15:06:34 +02001097 /* register pcm */
Takashi Iwai40a4b262015-01-30 08:34:58 +01001098 err = snd_register_device(devtype, pcm->card, pcm->device,
1099 &snd_pcm_f_ops[cidx], pcm,
1100 &pcm->streams[cidx].dev);
Johannes Bergc78085f2006-10-05 15:06:34 +02001101 if (err < 0) {
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001102 list_del_init(&pcm->list);
1103 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Takashi Iwaicaa751b2014-02-25 08:30:50 +01001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1107 snd_pcm_timer_init(substream);
1108 }
Johannes Berg9244b2c2006-10-05 16:02:22 +02001109
1110 list_for_each_entry(notify, &snd_pcm_notify_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 notify->n_register(pcm);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001112
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001113 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001114 mutex_unlock(&register_mutex);
Takashi Iwaib95bd3a2015-02-20 16:49:04 +01001115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Takashi Iwai877211f2005-11-17 13:59:38 +01001118static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Takashi Iwai877211f2005-11-17 13:59:38 +01001120 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +02001121 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +01001122 struct snd_pcm_substream *substream;
Takashi Iwai40a4b262015-01-30 08:34:58 +01001123 int cidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001125 mutex_lock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001126 if (list_empty(&pcm->list))
1127 goto unlock;
1128
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001129 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02001130 wake_up(&pcm->open_wait);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001131 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 for (cidx = 0; cidx < 2; cidx++)
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001133 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1134 snd_pcm_stream_lock_irq(substream);
Takashi Iwai0914f792012-10-16 16:43:39 +02001135 if (substream->runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwai0914f792012-10-16 16:43:39 +02001137 wake_up(&substream->runtime->sleep);
1138 wake_up(&substream->runtime->tsleep);
1139 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001140 snd_pcm_stream_unlock_irq(substream);
1141 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001142 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 notify->n_disconnect(pcm);
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 for (cidx = 0; cidx < 2; cidx++) {
Takashi Iwai40a4b262015-01-30 08:34:58 +01001146 snd_unregister_device(&pcm->streams[cidx].dev);
Takashi Iwai2d3391e2012-07-27 18:27:00 +02001147 if (pcm->streams[cidx].chmap_kctl) {
1148 snd_ctl_remove(pcm->card, pcm->streams[cidx].chmap_kctl);
1149 pcm->streams[cidx].chmap_kctl = NULL;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001152 mutex_unlock(&pcm->open_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001153 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001154 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Takashi Iwai30b771c2014-10-30 15:02:50 +01001158/**
1159 * snd_pcm_notify - Add/remove the notify list
1160 * @notify: PCM notify list
1161 * @nfree: 0 = register, 1 = unregister
1162 *
1163 * This adds the given notifier to the global list so that the callback is
1164 * called for each registered PCM devices. This exists only for PCM OSS
1165 * emulation, so far.
1166 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001167int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Johannes Berg9244b2c2006-10-05 16:02:22 +02001169 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001171 if (snd_BUG_ON(!notify ||
1172 !notify->n_register ||
1173 !notify->n_unregister ||
1174 !notify->n_disconnect))
1175 return -EINVAL;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001176 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (nfree) {
1178 list_del(&notify->list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001179 list_for_each_entry(pcm, &snd_pcm_devices, list)
1180 notify->n_unregister(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else {
1182 list_add_tail(&notify->list, &snd_pcm_notify_list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001183 list_for_each_entry(pcm, &snd_pcm_devices, list)
1184 notify->n_register(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001186 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001189EXPORT_SYMBOL(snd_pcm_notify);
1190
Takashi Iwaie28563c2005-12-01 10:42:42 +01001191#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/*
1193 * Info interface
1194 */
1195
Takashi Iwai877211f2005-11-17 13:59:38 +01001196static void snd_pcm_proc_read(struct snd_info_entry *entry,
1197 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Takashi Iwai877211f2005-11-17 13:59:38 +01001199 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001201 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001202 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01001203 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1204 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 snd_iprintf(buffer, " : playback %i",
1207 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001209 snd_iprintf(buffer, " : capture %i",
1210 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 snd_iprintf(buffer, "\n");
1212 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001213 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001216static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Takashi Iwaie28563c2005-12-01 10:42:42 +01001218static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Takashi Iwai877211f2005-11-17 13:59:38 +01001220 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001223 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (snd_info_register(entry) < 0) {
1225 snd_info_free_entry(entry);
1226 entry = NULL;
1227 }
1228 }
1229 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001230}
1231
1232static void snd_pcm_proc_done(void)
1233{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001234 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001235}
1236
1237#else /* !CONFIG_PROC_FS */
1238#define snd_pcm_proc_init()
1239#define snd_pcm_proc_done()
1240#endif /* CONFIG_PROC_FS */
1241
1242
1243/*
1244 * ENTRY functions
1245 */
1246
1247static int __init alsa_pcm_init(void)
1248{
1249 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1250 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1251 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
1255static void __exit alsa_pcm_exit(void)
1256{
1257 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1258 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001259 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262module_init(alsa_pcm_init)
1263module_exit(alsa_pcm_exit)