xref: /openbmc/linux/sound/core/pcm.c (revision 8e6ac8c6)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Digital Audio (PCM) abstract layer
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/slab.h>
9da155d5bSPaul Gortmaker #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/time.h>
111a60d4c5SIngo Molnar #include <linux/mutex.h>
1251990e82SPaul Gortmaker #include <linux/device.h>
1394ffb030SGustavo A. R. Silva #include <linux/nospec.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds #include <sound/minors.h>
161da177e4SLinus Torvalds #include <sound/pcm.h>
17a820ccbeSTakashi Iwai #include <sound/timer.h>
181da177e4SLinus Torvalds #include <sound/control.h>
191da177e4SLinus Torvalds #include <sound/info.h>
201da177e4SLinus Torvalds 
212c4842d3STakashi Sakamoto #include "pcm_local.h"
222c4842d3STakashi Sakamoto 
23c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
241da177e4SLinus Torvalds MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
261da177e4SLinus Torvalds 
27f87135f5SClemens Ladisch static LIST_HEAD(snd_pcm_devices);
281a60d4c5SIngo Molnar static DEFINE_MUTEX(register_mutex);
2958f30d65STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
3058f30d65STakashi Iwai static LIST_HEAD(snd_pcm_notify_list);
3158f30d65STakashi Iwai #endif
321da177e4SLinus Torvalds 
33877211f5STakashi Iwai static int snd_pcm_free(struct snd_pcm *pcm);
34877211f5STakashi Iwai static int snd_pcm_dev_free(struct snd_device *device);
35877211f5STakashi Iwai static int snd_pcm_dev_register(struct snd_device *device);
36877211f5STakashi Iwai static int snd_pcm_dev_disconnect(struct snd_device *device);
371da177e4SLinus Torvalds 
snd_pcm_get(struct snd_card * card,int device)38f90c06a2SPawel MOLL static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
39f87135f5SClemens Ladisch {
40f87135f5SClemens Ladisch 	struct snd_pcm *pcm;
41f87135f5SClemens Ladisch 
429244b2c3SJohannes Berg 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
43f87135f5SClemens Ladisch 		if (pcm->card == card && pcm->device == device)
44f87135f5SClemens Ladisch 			return pcm;
45f87135f5SClemens Ladisch 	}
46f87135f5SClemens Ladisch 	return NULL;
47f87135f5SClemens Ladisch }
48f87135f5SClemens Ladisch 
snd_pcm_next(struct snd_card * card,int device)49f90c06a2SPawel MOLL static int snd_pcm_next(struct snd_card *card, int device)
50f90c06a2SPawel MOLL {
51f90c06a2SPawel MOLL 	struct snd_pcm *pcm;
52f90c06a2SPawel MOLL 
53f90c06a2SPawel MOLL 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
54f90c06a2SPawel MOLL 		if (pcm->card == card && pcm->device > device)
55f90c06a2SPawel MOLL 			return pcm->device;
56f90c06a2SPawel MOLL 		else if (pcm->card->number > card->number)
57f90c06a2SPawel MOLL 			return -1;
58f90c06a2SPawel MOLL 	}
59f90c06a2SPawel MOLL 	return -1;
60f90c06a2SPawel MOLL }
61f90c06a2SPawel MOLL 
snd_pcm_add(struct snd_pcm * newpcm)62f90c06a2SPawel MOLL static int snd_pcm_add(struct snd_pcm *newpcm)
63f90c06a2SPawel MOLL {
64f90c06a2SPawel MOLL 	struct snd_pcm *pcm;
65f90c06a2SPawel MOLL 
66b95bd3a4STakashi Iwai 	if (newpcm->internal)
67b95bd3a4STakashi Iwai 		return 0;
68b95bd3a4STakashi Iwai 
69f90c06a2SPawel MOLL 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
70f90c06a2SPawel MOLL 		if (pcm->card == newpcm->card && pcm->device == newpcm->device)
71f90c06a2SPawel MOLL 			return -EBUSY;
72f90c06a2SPawel MOLL 		if (pcm->card->number > newpcm->card->number ||
73f90c06a2SPawel MOLL 				(pcm->card == newpcm->card &&
74f90c06a2SPawel MOLL 				pcm->device > newpcm->device)) {
75f90c06a2SPawel MOLL 			list_add(&newpcm->list, pcm->list.prev);
76f90c06a2SPawel MOLL 			return 0;
77f90c06a2SPawel MOLL 		}
78f90c06a2SPawel MOLL 	}
79f90c06a2SPawel MOLL 	list_add_tail(&newpcm->list, &snd_pcm_devices);
80f90c06a2SPawel MOLL 	return 0;
81f90c06a2SPawel MOLL }
82f90c06a2SPawel MOLL 
snd_pcm_control_ioctl(struct snd_card * card,struct snd_ctl_file * control,unsigned int cmd,unsigned long arg)83877211f5STakashi Iwai static int snd_pcm_control_ioctl(struct snd_card *card,
84877211f5STakashi Iwai 				 struct snd_ctl_file *control,
851da177e4SLinus Torvalds 				 unsigned int cmd, unsigned long arg)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	switch (cmd) {
881da177e4SLinus Torvalds 	case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
891da177e4SLinus Torvalds 		{
901da177e4SLinus Torvalds 			int device;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 			if (get_user(device, (int __user *)arg))
931da177e4SLinus Torvalds 				return -EFAULT;
941a60d4c5SIngo Molnar 			mutex_lock(&register_mutex);
95f90c06a2SPawel MOLL 			device = snd_pcm_next(card, device);
961a60d4c5SIngo Molnar 			mutex_unlock(&register_mutex);
971da177e4SLinus Torvalds 			if (put_user(device, (int __user *)arg))
981da177e4SLinus Torvalds 				return -EFAULT;
991da177e4SLinus Torvalds 			return 0;
1001da177e4SLinus Torvalds 		}
1011da177e4SLinus Torvalds 	case SNDRV_CTL_IOCTL_PCM_INFO:
1021da177e4SLinus Torvalds 		{
103877211f5STakashi Iwai 			struct snd_pcm_info __user *info;
1041da177e4SLinus Torvalds 			unsigned int device, subdevice;
105877211f5STakashi Iwai 			int stream;
106877211f5STakashi Iwai 			struct snd_pcm *pcm;
107877211f5STakashi Iwai 			struct snd_pcm_str *pstr;
108877211f5STakashi Iwai 			struct snd_pcm_substream *substream;
109f87135f5SClemens Ladisch 			int err;
110f87135f5SClemens Ladisch 
111877211f5STakashi Iwai 			info = (struct snd_pcm_info __user *)arg;
1121da177e4SLinus Torvalds 			if (get_user(device, &info->device))
1131da177e4SLinus Torvalds 				return -EFAULT;
1141da177e4SLinus Torvalds 			if (get_user(stream, &info->stream))
1151da177e4SLinus Torvalds 				return -EFAULT;
1161da177e4SLinus Torvalds 			if (stream < 0 || stream > 1)
1171da177e4SLinus Torvalds 				return -EINVAL;
11894ffb030SGustavo A. R. Silva 			stream = array_index_nospec(stream, 2);
1191da177e4SLinus Torvalds 			if (get_user(subdevice, &info->subdevice))
1201da177e4SLinus Torvalds 				return -EFAULT;
1211a60d4c5SIngo Molnar 			mutex_lock(&register_mutex);
122f90c06a2SPawel MOLL 			pcm = snd_pcm_get(card, device);
123f87135f5SClemens Ladisch 			if (pcm == NULL) {
124f87135f5SClemens Ladisch 				err = -ENXIO;
125f87135f5SClemens Ladisch 				goto _error;
126f87135f5SClemens Ladisch 			}
127f87135f5SClemens Ladisch 			pstr = &pcm->streams[stream];
128f87135f5SClemens Ladisch 			if (pstr->substream_count == 0) {
129f87135f5SClemens Ladisch 				err = -ENOENT;
130f87135f5SClemens Ladisch 				goto _error;
131f87135f5SClemens Ladisch 			}
132f87135f5SClemens Ladisch 			if (subdevice >= pstr->substream_count) {
133f87135f5SClemens Ladisch 				err = -ENXIO;
134f87135f5SClemens Ladisch 				goto _error;
135f87135f5SClemens Ladisch 			}
136f87135f5SClemens Ladisch 			for (substream = pstr->substream; substream;
137f87135f5SClemens Ladisch 			     substream = substream->next)
1381da177e4SLinus Torvalds 				if (substream->number == (int)subdevice)
1391da177e4SLinus Torvalds 					break;
140f87135f5SClemens Ladisch 			if (substream == NULL) {
141f87135f5SClemens Ladisch 				err = -ENXIO;
142f87135f5SClemens Ladisch 				goto _error;
143f87135f5SClemens Ladisch 			}
144362bca57SRobb Glasser 			mutex_lock(&pcm->open_mutex);
145f87135f5SClemens Ladisch 			err = snd_pcm_info_user(substream, info);
146362bca57SRobb Glasser 			mutex_unlock(&pcm->open_mutex);
147f87135f5SClemens Ladisch 		_error:
1481a60d4c5SIngo Molnar 			mutex_unlock(&register_mutex);
149f87135f5SClemens Ladisch 			return err;
1501da177e4SLinus Torvalds 		}
1511da177e4SLinus Torvalds 	case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
1521da177e4SLinus Torvalds 		{
1531da177e4SLinus Torvalds 			int val;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 			if (get_user(val, (int __user *)arg))
1561da177e4SLinus Torvalds 				return -EFAULT;
15723c18d4bSTakashi Iwai 			control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
1581da177e4SLinus Torvalds 			return 0;
1591da177e4SLinus Torvalds 		}
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
1621da177e4SLinus Torvalds }
16321a3479aSJaroslav Kysela 
1641da177e4SLinus Torvalds #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
1651da177e4SLinus Torvalds 
166d03af9b8STakashi Iwai static const char * const snd_pcm_format_names[] = {
1671da177e4SLinus Torvalds 	FORMAT(S8),
1681da177e4SLinus Torvalds 	FORMAT(U8),
1691da177e4SLinus Torvalds 	FORMAT(S16_LE),
1701da177e4SLinus Torvalds 	FORMAT(S16_BE),
1711da177e4SLinus Torvalds 	FORMAT(U16_LE),
1721da177e4SLinus Torvalds 	FORMAT(U16_BE),
1731da177e4SLinus Torvalds 	FORMAT(S24_LE),
1741da177e4SLinus Torvalds 	FORMAT(S24_BE),
1751da177e4SLinus Torvalds 	FORMAT(U24_LE),
1761da177e4SLinus Torvalds 	FORMAT(U24_BE),
1771da177e4SLinus Torvalds 	FORMAT(S32_LE),
1781da177e4SLinus Torvalds 	FORMAT(S32_BE),
1791da177e4SLinus Torvalds 	FORMAT(U32_LE),
1801da177e4SLinus Torvalds 	FORMAT(U32_BE),
1811da177e4SLinus Torvalds 	FORMAT(FLOAT_LE),
1821da177e4SLinus Torvalds 	FORMAT(FLOAT_BE),
1831da177e4SLinus Torvalds 	FORMAT(FLOAT64_LE),
1841da177e4SLinus Torvalds 	FORMAT(FLOAT64_BE),
1851da177e4SLinus Torvalds 	FORMAT(IEC958_SUBFRAME_LE),
1861da177e4SLinus Torvalds 	FORMAT(IEC958_SUBFRAME_BE),
1871da177e4SLinus Torvalds 	FORMAT(MU_LAW),
1881da177e4SLinus Torvalds 	FORMAT(A_LAW),
1891da177e4SLinus Torvalds 	FORMAT(IMA_ADPCM),
1901da177e4SLinus Torvalds 	FORMAT(MPEG),
1911da177e4SLinus Torvalds 	FORMAT(GSM),
1921da177e4SLinus Torvalds 	FORMAT(SPECIAL),
1931da177e4SLinus Torvalds 	FORMAT(S24_3LE),
1941da177e4SLinus Torvalds 	FORMAT(S24_3BE),
1951da177e4SLinus Torvalds 	FORMAT(U24_3LE),
1961da177e4SLinus Torvalds 	FORMAT(U24_3BE),
1971da177e4SLinus Torvalds 	FORMAT(S20_3LE),
1981da177e4SLinus Torvalds 	FORMAT(S20_3BE),
1991da177e4SLinus Torvalds 	FORMAT(U20_3LE),
2001da177e4SLinus Torvalds 	FORMAT(U20_3BE),
2011da177e4SLinus Torvalds 	FORMAT(S18_3LE),
2021da177e4SLinus Torvalds 	FORMAT(S18_3BE),
2031da177e4SLinus Torvalds 	FORMAT(U18_3LE),
2041da177e4SLinus Torvalds 	FORMAT(U18_3BE),
2057a28826aSDan Carpenter 	FORMAT(G723_24),
2067a28826aSDan Carpenter 	FORMAT(G723_24_1B),
2077a28826aSDan Carpenter 	FORMAT(G723_40),
2087a28826aSDan Carpenter 	FORMAT(G723_40_1B),
209ef7a4f97SDaniel Mack 	FORMAT(DSD_U8),
210ef7a4f97SDaniel Mack 	FORMAT(DSD_U16_LE),
211d4288d3fSJurgen Kramer 	FORMAT(DSD_U32_LE),
212d42472ecSJussi Laako 	FORMAT(DSD_U16_BE),
213d42472ecSJussi Laako 	FORMAT(DSD_U32_BE),
2141da177e4SLinus Torvalds };
2151da177e4SLinus Torvalds 
21630b771cfSTakashi Iwai /**
21730b771cfSTakashi Iwai  * snd_pcm_format_name - Return a name string for the given PCM format
21830b771cfSTakashi Iwai  * @format: PCM format
2194e2b7067STakashi Iwai  *
2204e2b7067STakashi Iwai  * Return: the format name string
22130b771cfSTakashi Iwai  */
snd_pcm_format_name(snd_pcm_format_t format)2226e5265ecSTakashi Iwai const char *snd_pcm_format_name(snd_pcm_format_t format)
223e28563ccSTakashi Iwai {
224fea952e5SClemens Ladisch 	if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
2257a28826aSDan Carpenter 		return "Unknown";
226fea952e5SClemens Ladisch 	return snd_pcm_format_names[(__force unsigned int)format];
227e28563ccSTakashi Iwai }
2286e5265ecSTakashi Iwai EXPORT_SYMBOL_GPL(snd_pcm_format_name);
2296e5265ecSTakashi Iwai 
2306e5265ecSTakashi Iwai #ifdef CONFIG_SND_VERBOSE_PROCFS
2316e5265ecSTakashi Iwai 
2326e5265ecSTakashi Iwai #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
2336e5265ecSTakashi Iwai #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
2346e5265ecSTakashi Iwai #define READY(v) [SNDRV_PCM_READY_##v] = #v
2356e5265ecSTakashi Iwai #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
2366e5265ecSTakashi Iwai #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
2376e5265ecSTakashi Iwai #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
2386e5265ecSTakashi Iwai #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
2396e5265ecSTakashi Iwai #define START(v) [SNDRV_PCM_START_##v] = #v
2406e5265ecSTakashi Iwai #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
241e28563ccSTakashi Iwai 
242d03af9b8STakashi Iwai static const char * const snd_pcm_stream_names[] = {
243e28563ccSTakashi Iwai 	STREAM(PLAYBACK),
244e28563ccSTakashi Iwai 	STREAM(CAPTURE),
245e28563ccSTakashi Iwai };
246e28563ccSTakashi Iwai 
247d03af9b8STakashi Iwai static const char * const snd_pcm_state_names[] = {
248e28563ccSTakashi Iwai 	STATE(OPEN),
249e28563ccSTakashi Iwai 	STATE(SETUP),
250e28563ccSTakashi Iwai 	STATE(PREPARED),
251e28563ccSTakashi Iwai 	STATE(RUNNING),
252e28563ccSTakashi Iwai 	STATE(XRUN),
253e28563ccSTakashi Iwai 	STATE(DRAINING),
254e28563ccSTakashi Iwai 	STATE(PAUSED),
255e28563ccSTakashi Iwai 	STATE(SUSPENDED),
256*8e6ac8c6SJason Zhang 	STATE(DISCONNECTED),
257e28563ccSTakashi Iwai };
258e28563ccSTakashi Iwai 
259d03af9b8STakashi Iwai static const char * const snd_pcm_access_names[] = {
260e28563ccSTakashi Iwai 	ACCESS(MMAP_INTERLEAVED),
261e28563ccSTakashi Iwai 	ACCESS(MMAP_NONINTERLEAVED),
262e28563ccSTakashi Iwai 	ACCESS(MMAP_COMPLEX),
263e28563ccSTakashi Iwai 	ACCESS(RW_INTERLEAVED),
264e28563ccSTakashi Iwai 	ACCESS(RW_NONINTERLEAVED),
265e28563ccSTakashi Iwai };
266e28563ccSTakashi Iwai 
267d03af9b8STakashi Iwai static const char * const snd_pcm_subformat_names[] = {
2681da177e4SLinus Torvalds 	SUBFORMAT(STD),
2691da177e4SLinus Torvalds };
2701da177e4SLinus Torvalds 
271d03af9b8STakashi Iwai static const char * const snd_pcm_tstamp_mode_names[] = {
2721da177e4SLinus Torvalds 	TSTAMP(NONE),
2738c121586SJaroslav Kysela 	TSTAMP(ENABLE),
2741da177e4SLinus Torvalds };
2751da177e4SLinus Torvalds 
snd_pcm_stream_name(int stream)276877211f5STakashi Iwai static const char *snd_pcm_stream_name(int stream)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	return snd_pcm_stream_names[stream];
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
snd_pcm_access_name(snd_pcm_access_t access)2811da177e4SLinus Torvalds static const char *snd_pcm_access_name(snd_pcm_access_t access)
2821da177e4SLinus Torvalds {
283fea952e5SClemens Ladisch 	return snd_pcm_access_names[(__force int)access];
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
snd_pcm_subformat_name(snd_pcm_subformat_t subformat)2861da177e4SLinus Torvalds static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
2871da177e4SLinus Torvalds {
288fea952e5SClemens Ladisch 	return snd_pcm_subformat_names[(__force int)subformat];
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
snd_pcm_tstamp_mode_name(int mode)291877211f5STakashi Iwai static const char *snd_pcm_tstamp_mode_name(int mode)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	return snd_pcm_tstamp_mode_names[mode];
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
snd_pcm_state_name(snd_pcm_state_t state)2961da177e4SLinus Torvalds static const char *snd_pcm_state_name(snd_pcm_state_t state)
2971da177e4SLinus Torvalds {
298fea952e5SClemens Ladisch 	return snd_pcm_state_names[(__force int)state];
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3018eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
3021da177e4SLinus Torvalds #include <linux/soundcard.h>
3031a60d4c5SIngo Molnar 
snd_pcm_oss_format_name(int format)3041da177e4SLinus Torvalds static const char *snd_pcm_oss_format_name(int format)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	switch (format) {
3071da177e4SLinus Torvalds 	case AFMT_MU_LAW:
3081da177e4SLinus Torvalds 		return "MU_LAW";
3091da177e4SLinus Torvalds 	case AFMT_A_LAW:
3101da177e4SLinus Torvalds 		return "A_LAW";
3111da177e4SLinus Torvalds 	case AFMT_IMA_ADPCM:
3121da177e4SLinus Torvalds 		return "IMA_ADPCM";
3131da177e4SLinus Torvalds 	case AFMT_U8:
3141da177e4SLinus Torvalds 		return "U8";
3151da177e4SLinus Torvalds 	case AFMT_S16_LE:
3161da177e4SLinus Torvalds 		return "S16_LE";
3171da177e4SLinus Torvalds 	case AFMT_S16_BE:
3181da177e4SLinus Torvalds 		return "S16_BE";
3191da177e4SLinus Torvalds 	case AFMT_S8:
3201da177e4SLinus Torvalds 		return "S8";
3211da177e4SLinus Torvalds 	case AFMT_U16_LE:
3221da177e4SLinus Torvalds 		return "U16_LE";
3231da177e4SLinus Torvalds 	case AFMT_U16_BE:
3241da177e4SLinus Torvalds 		return "U16_BE";
3251da177e4SLinus Torvalds 	case AFMT_MPEG:
3261da177e4SLinus Torvalds 		return "MPEG";
3271da177e4SLinus Torvalds 	default:
3281da177e4SLinus Torvalds 		return "unknown";
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds #endif
3321da177e4SLinus Torvalds 
snd_pcm_proc_info_read(struct snd_pcm_substream * substream,struct snd_info_buffer * buffer)333877211f5STakashi Iwai static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
334877211f5STakashi Iwai 				   struct snd_info_buffer *buffer)
3351da177e4SLinus Torvalds {
336877211f5STakashi Iwai 	struct snd_pcm_info *info;
3371da177e4SLinus Torvalds 	int err;
3381da177e4SLinus Torvalds 
3397c22f1aaSTakashi Iwai 	if (! substream)
3407c22f1aaSTakashi Iwai 		return;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	info = kmalloc(sizeof(*info), GFP_KERNEL);
343ec0e9937STakashi Iwai 	if (!info)
3441da177e4SLinus Torvalds 		return;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	err = snd_pcm_info(substream, info);
3471da177e4SLinus Torvalds 	if (err < 0) {
3481da177e4SLinus Torvalds 		snd_iprintf(buffer, "error %d\n", err);
3491da177e4SLinus Torvalds 		kfree(info);
3501da177e4SLinus Torvalds 		return;
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds 	snd_iprintf(buffer, "card: %d\n", info->card);
3531da177e4SLinus Torvalds 	snd_iprintf(buffer, "device: %d\n", info->device);
3541da177e4SLinus Torvalds 	snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
3551da177e4SLinus Torvalds 	snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
3561da177e4SLinus Torvalds 	snd_iprintf(buffer, "id: %s\n", info->id);
3571da177e4SLinus Torvalds 	snd_iprintf(buffer, "name: %s\n", info->name);
3581da177e4SLinus Torvalds 	snd_iprintf(buffer, "subname: %s\n", info->subname);
3591da177e4SLinus Torvalds 	snd_iprintf(buffer, "class: %d\n", info->dev_class);
3601da177e4SLinus Torvalds 	snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
3611da177e4SLinus Torvalds 	snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
3621da177e4SLinus Torvalds 	snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
3631da177e4SLinus Torvalds 	kfree(info);
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
snd_pcm_stream_proc_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)366877211f5STakashi Iwai static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
367877211f5STakashi Iwai 					  struct snd_info_buffer *buffer)
3681da177e4SLinus Torvalds {
369877211f5STakashi Iwai 	snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
370877211f5STakashi Iwai 			       buffer);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
snd_pcm_substream_proc_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)373877211f5STakashi Iwai static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
374877211f5STakashi Iwai 					     struct snd_info_buffer *buffer)
3751da177e4SLinus Torvalds {
3769fe856e4SJoe Perches 	snd_pcm_proc_info_read(entry->private_data, buffer);
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
snd_pcm_substream_proc_hw_params_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)379877211f5STakashi Iwai static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
380877211f5STakashi Iwai 						  struct snd_info_buffer *buffer)
3811da177e4SLinus Torvalds {
382877211f5STakashi Iwai 	struct snd_pcm_substream *substream = entry->private_data;
383901d46d5STakashi Iwai 	struct snd_pcm_runtime *runtime;
384901d46d5STakashi Iwai 
385901d46d5STakashi Iwai 	mutex_lock(&substream->pcm->open_mutex);
386901d46d5STakashi Iwai 	runtime = substream->runtime;
3871da177e4SLinus Torvalds 	if (!runtime) {
3881da177e4SLinus Torvalds 		snd_iprintf(buffer, "closed\n");
389901d46d5STakashi Iwai 		goto unlock;
3901da177e4SLinus Torvalds 	}
391f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_OPEN) {
3921da177e4SLinus Torvalds 		snd_iprintf(buffer, "no setup\n");
393901d46d5STakashi Iwai 		goto unlock;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 	snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
3961da177e4SLinus Torvalds 	snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
3971da177e4SLinus Torvalds 	snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
3981da177e4SLinus Torvalds 	snd_iprintf(buffer, "channels: %u\n", runtime->channels);
3991da177e4SLinus Torvalds 	snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
4001da177e4SLinus Torvalds 	snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
4011da177e4SLinus Torvalds 	snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
4028eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
4031da177e4SLinus Torvalds 	if (substream->oss.oss) {
4041da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
4051da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
4061da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
4071da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
4081da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
4091da177e4SLinus Torvalds 		snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds #endif
412901d46d5STakashi Iwai  unlock:
413901d46d5STakashi Iwai 	mutex_unlock(&substream->pcm->open_mutex);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
snd_pcm_substream_proc_sw_params_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)416877211f5STakashi Iwai static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
417877211f5STakashi Iwai 						  struct snd_info_buffer *buffer)
4181da177e4SLinus Torvalds {
419877211f5STakashi Iwai 	struct snd_pcm_substream *substream = entry->private_data;
420901d46d5STakashi Iwai 	struct snd_pcm_runtime *runtime;
421901d46d5STakashi Iwai 
422901d46d5STakashi Iwai 	mutex_lock(&substream->pcm->open_mutex);
423901d46d5STakashi Iwai 	runtime = substream->runtime;
4241da177e4SLinus Torvalds 	if (!runtime) {
4251da177e4SLinus Torvalds 		snd_iprintf(buffer, "closed\n");
426901d46d5STakashi Iwai 		goto unlock;
4271da177e4SLinus Torvalds 	}
428f0061c18STakashi Iwai 	if (runtime->state == SNDRV_PCM_STATE_OPEN) {
4291da177e4SLinus Torvalds 		snd_iprintf(buffer, "no setup\n");
430901d46d5STakashi Iwai 		goto unlock;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 	snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
4331da177e4SLinus Torvalds 	snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
4341da177e4SLinus Torvalds 	snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
4351da177e4SLinus Torvalds 	snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
4361da177e4SLinus Torvalds 	snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
4371da177e4SLinus Torvalds 	snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
4381da177e4SLinus Torvalds 	snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
4391da177e4SLinus Torvalds 	snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
440901d46d5STakashi Iwai  unlock:
441901d46d5STakashi Iwai 	mutex_unlock(&substream->pcm->open_mutex);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
snd_pcm_substream_proc_status_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)444877211f5STakashi Iwai static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
445877211f5STakashi Iwai 					       struct snd_info_buffer *buffer)
4461da177e4SLinus Torvalds {
447877211f5STakashi Iwai 	struct snd_pcm_substream *substream = entry->private_data;
448901d46d5STakashi Iwai 	struct snd_pcm_runtime *runtime;
4493ddee7f8SBaolin Wang 	struct snd_pcm_status64 status;
4501da177e4SLinus Torvalds 	int err;
451901d46d5STakashi Iwai 
452901d46d5STakashi Iwai 	mutex_lock(&substream->pcm->open_mutex);
453901d46d5STakashi Iwai 	runtime = substream->runtime;
4541da177e4SLinus Torvalds 	if (!runtime) {
4551da177e4SLinus Torvalds 		snd_iprintf(buffer, "closed\n");
456901d46d5STakashi Iwai 		goto unlock;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 	memset(&status, 0, sizeof(status));
4593ddee7f8SBaolin Wang 	err = snd_pcm_status64(substream, &status);
4601da177e4SLinus Torvalds 	if (err < 0) {
4611da177e4SLinus Torvalds 		snd_iprintf(buffer, "error %d\n", err);
462901d46d5STakashi Iwai 		goto unlock;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
465e7373b70SClemens Ladisch 	snd_iprintf(buffer, "owner_pid   : %d\n", pid_vnr(substream->pid));
4663ddee7f8SBaolin Wang 	snd_iprintf(buffer, "trigger_time: %lld.%09lld\n",
4673ddee7f8SBaolin Wang 		status.trigger_tstamp_sec, status.trigger_tstamp_nsec);
4683ddee7f8SBaolin Wang 	snd_iprintf(buffer, "tstamp      : %lld.%09lld\n",
4693ddee7f8SBaolin Wang 		status.tstamp_sec, status.tstamp_nsec);
4701da177e4SLinus Torvalds 	snd_iprintf(buffer, "delay       : %ld\n", status.delay);
4711da177e4SLinus Torvalds 	snd_iprintf(buffer, "avail       : %ld\n", status.avail);
4721da177e4SLinus Torvalds 	snd_iprintf(buffer, "avail_max   : %ld\n", status.avail_max);
4731da177e4SLinus Torvalds 	snd_iprintf(buffer, "-----\n");
4741da177e4SLinus Torvalds 	snd_iprintf(buffer, "hw_ptr      : %ld\n", runtime->status->hw_ptr);
4751da177e4SLinus Torvalds 	snd_iprintf(buffer, "appl_ptr    : %ld\n", runtime->control->appl_ptr);
476901d46d5STakashi Iwai  unlock:
477901d46d5STakashi Iwai 	mutex_unlock(&substream->pcm->open_mutex);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
48061fb63c0SJaroslav Kysela #ifdef CONFIG_SND_PCM_XRUN_DEBUG
snd_pcm_xrun_injection_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)4812b30d411STakashi Iwai static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
4822b30d411STakashi Iwai 					 struct snd_info_buffer *buffer)
4832b30d411STakashi Iwai {
4842b30d411STakashi Iwai 	struct snd_pcm_substream *substream = entry->private_data;
4852b30d411STakashi Iwai 
486e647f5a5STakashi Iwai 	snd_pcm_stop_xrun(substream);
4872b30d411STakashi Iwai }
4882b30d411STakashi Iwai 
snd_pcm_xrun_debug_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)489877211f5STakashi Iwai static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
490877211f5STakashi Iwai 				    struct snd_info_buffer *buffer)
4911da177e4SLinus Torvalds {
492877211f5STakashi Iwai 	struct snd_pcm_str *pstr = entry->private_data;
4931da177e4SLinus Torvalds 	snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
snd_pcm_xrun_debug_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)496877211f5STakashi Iwai static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
497877211f5STakashi Iwai 				     struct snd_info_buffer *buffer)
4981da177e4SLinus Torvalds {
499877211f5STakashi Iwai 	struct snd_pcm_str *pstr = entry->private_data;
5001da177e4SLinus Torvalds 	char line[64];
5011da177e4SLinus Torvalds 	if (!snd_info_get_line(buffer, line, sizeof(line)))
5021da177e4SLinus Torvalds 		pstr->xrun_debug = simple_strtoul(line, NULL, 10);
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds #endif
5051da177e4SLinus Torvalds 
snd_pcm_stream_proc_init(struct snd_pcm_str * pstr)506877211f5STakashi Iwai static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
5071da177e4SLinus Torvalds {
508877211f5STakashi Iwai 	struct snd_pcm *pcm = pstr->pcm;
509877211f5STakashi Iwai 	struct snd_info_entry *entry;
5101da177e4SLinus Torvalds 	char name[16];
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	sprintf(name, "pcm%i%c", pcm->device,
5131da177e4SLinus Torvalds 		pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
514c8da9be4SMarkus Elfring 	entry = snd_info_create_card_entry(pcm->card, name,
515c8da9be4SMarkus Elfring 					   pcm->card->proc_root);
516c8da9be4SMarkus Elfring 	if (!entry)
5171da177e4SLinus Torvalds 		return -ENOMEM;
5186a73cf46SJoe Perches 	entry->mode = S_IFDIR | 0555;
5191da177e4SLinus Torvalds 	pstr->proc_root = entry;
520c8da9be4SMarkus Elfring 	entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root);
521a8d14981STakashi Iwai 	if (entry)
522bf850204STakashi Iwai 		snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
52361fb63c0SJaroslav Kysela #ifdef CONFIG_SND_PCM_XRUN_DEBUG
524c8da9be4SMarkus Elfring 	entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
525c8da9be4SMarkus Elfring 					   pstr->proc_root);
526c8da9be4SMarkus Elfring 	if (entry) {
527a8d14981STakashi Iwai 		snd_info_set_text_ops(entry, pstr, snd_pcm_xrun_debug_read);
5281da177e4SLinus Torvalds 		entry->c.text.write = snd_pcm_xrun_debug_write;
5296a73cf46SJoe Perches 		entry->mode |= 0200;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds #endif
5321da177e4SLinus Torvalds 	return 0;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
snd_pcm_stream_proc_done(struct snd_pcm_str * pstr)535877211f5STakashi Iwai static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
5361da177e4SLinus Torvalds {
537746d4a02STakashi Iwai 	snd_info_free_entry(pstr->proc_root);
5381da177e4SLinus Torvalds 	pstr->proc_root = NULL;
5391da177e4SLinus Torvalds 	return 0;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
542a8d14981STakashi Iwai static struct snd_info_entry *
create_substream_info_entry(struct snd_pcm_substream * substream,const char * name,void (* read)(struct snd_info_entry *,struct snd_info_buffer *))543a8d14981STakashi Iwai create_substream_info_entry(struct snd_pcm_substream *substream,
544a8d14981STakashi Iwai 			    const char *name,
545a8d14981STakashi Iwai 			    void (*read)(struct snd_info_entry *,
546a8d14981STakashi Iwai 					 struct snd_info_buffer *))
547a8d14981STakashi Iwai {
548a8d14981STakashi Iwai 	struct snd_info_entry *entry;
549a8d14981STakashi Iwai 
550a8d14981STakashi Iwai 	entry = snd_info_create_card_entry(substream->pcm->card, name,
551a8d14981STakashi Iwai 					   substream->proc_root);
552a8d14981STakashi Iwai 	if (entry)
553a8d14981STakashi Iwai 		snd_info_set_text_ops(entry, substream, read);
554a8d14981STakashi Iwai 	return entry;
555a8d14981STakashi Iwai }
556a8d14981STakashi Iwai 
snd_pcm_substream_proc_init(struct snd_pcm_substream * substream)557877211f5STakashi Iwai static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
5581da177e4SLinus Torvalds {
559877211f5STakashi Iwai 	struct snd_info_entry *entry;
560877211f5STakashi Iwai 	struct snd_card *card;
5611da177e4SLinus Torvalds 	char name[16];
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	card = substream->pcm->card;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	sprintf(name, "sub%i", substream->number);
566c8da9be4SMarkus Elfring 	entry = snd_info_create_card_entry(card, name,
567c8da9be4SMarkus Elfring 					   substream->pstr->proc_root);
568c8da9be4SMarkus Elfring 	if (!entry)
5691da177e4SLinus Torvalds 		return -ENOMEM;
5706a73cf46SJoe Perches 	entry->mode = S_IFDIR | 0555;
5711da177e4SLinus Torvalds 	substream->proc_root = entry;
572a8d14981STakashi Iwai 
573a8d14981STakashi Iwai 	create_substream_info_entry(substream, "info",
574bf850204STakashi Iwai 				    snd_pcm_substream_proc_info_read);
575a8d14981STakashi Iwai 	create_substream_info_entry(substream, "hw_params",
576bf850204STakashi Iwai 				    snd_pcm_substream_proc_hw_params_read);
577a8d14981STakashi Iwai 	create_substream_info_entry(substream, "sw_params",
578bf850204STakashi Iwai 				    snd_pcm_substream_proc_sw_params_read);
579a8d14981STakashi Iwai 	create_substream_info_entry(substream, "status",
580bf850204STakashi Iwai 				    snd_pcm_substream_proc_status_read);
5811da177e4SLinus Torvalds 
5822b30d411STakashi Iwai #ifdef CONFIG_SND_PCM_XRUN_DEBUG
583a8d14981STakashi Iwai 	entry = create_substream_info_entry(substream, "xrun_injection", NULL);
5842b30d411STakashi Iwai 	if (entry) {
5852b30d411STakashi Iwai 		entry->c.text.write = snd_pcm_xrun_injection_write;
5866a73cf46SJoe Perches 		entry->mode = S_IFREG | 0200;
5872b30d411STakashi Iwai 	}
5882b30d411STakashi Iwai #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
5892b30d411STakashi Iwai 
5901da177e4SLinus Torvalds 	return 0;
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
593b7d90a35STakashi Iwai #else /* !CONFIG_SND_VERBOSE_PROCFS */
snd_pcm_stream_proc_init(struct snd_pcm_str * pstr)594e28563ccSTakashi Iwai static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
snd_pcm_stream_proc_done(struct snd_pcm_str * pstr)595e28563ccSTakashi Iwai static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
snd_pcm_substream_proc_init(struct snd_pcm_substream * substream)596e28563ccSTakashi Iwai static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
597b7d90a35STakashi Iwai #endif /* CONFIG_SND_VERBOSE_PROCFS */
5981da177e4SLinus Torvalds 
599ef46c7afSTakashi Iwai static const struct attribute_group *pcm_dev_attr_groups[];
600ef46c7afSTakashi Iwai 
6013d21ef0bSTakashi Iwai /*
6023d21ef0bSTakashi Iwai  * PM callbacks: we need to deal only with suspend here, as the resume is
6033d21ef0bSTakashi Iwai  * triggered either from user-space or the driver's resume callback
6043d21ef0bSTakashi Iwai  */
6053d21ef0bSTakashi Iwai #ifdef CONFIG_PM_SLEEP
do_pcm_suspend(struct device * dev)6063d21ef0bSTakashi Iwai static int do_pcm_suspend(struct device *dev)
6073d21ef0bSTakashi Iwai {
608bc41a722STakashi Iwai 	struct snd_pcm_str *pstr = dev_get_drvdata(dev);
6093d21ef0bSTakashi Iwai 
6103d21ef0bSTakashi Iwai 	if (!pstr->pcm->no_device_suspend)
6113d21ef0bSTakashi Iwai 		snd_pcm_suspend_all(pstr->pcm);
6123d21ef0bSTakashi Iwai 	return 0;
6133d21ef0bSTakashi Iwai }
6143d21ef0bSTakashi Iwai #endif
6153d21ef0bSTakashi Iwai 
6163d21ef0bSTakashi Iwai static const struct dev_pm_ops pcm_dev_pm_ops = {
6173d21ef0bSTakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(do_pcm_suspend, NULL)
6183d21ef0bSTakashi Iwai };
6193d21ef0bSTakashi Iwai 
6203d21ef0bSTakashi Iwai /* device type for PCM -- basically only for passing PM callbacks */
6213d21ef0bSTakashi Iwai static const struct device_type pcm_dev_type = {
6223d21ef0bSTakashi Iwai 	.name = "pcm",
6233d21ef0bSTakashi Iwai 	.pm = &pcm_dev_pm_ops,
6243d21ef0bSTakashi Iwai };
6253d21ef0bSTakashi Iwai 
6261da177e4SLinus Torvalds /**
6271da177e4SLinus Torvalds  * snd_pcm_new_stream - create a new PCM stream
6281da177e4SLinus Torvalds  * @pcm: the pcm instance
6291da177e4SLinus Torvalds  * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
6301da177e4SLinus Torvalds  * @substream_count: the number of substreams
6311da177e4SLinus Torvalds  *
6321da177e4SLinus Torvalds  * Creates a new stream for the pcm.
6331da177e4SLinus Torvalds  * The corresponding stream on the pcm must have been empty before
6341da177e4SLinus Torvalds  * calling this, i.e. zero must be given to the argument of
6351da177e4SLinus Torvalds  * snd_pcm_new().
6361da177e4SLinus Torvalds  *
637eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
6381da177e4SLinus Torvalds  */
snd_pcm_new_stream(struct snd_pcm * pcm,int stream,int substream_count)639877211f5STakashi Iwai int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	int idx, err;
642877211f5STakashi Iwai 	struct snd_pcm_str *pstr = &pcm->streams[stream];
643877211f5STakashi Iwai 	struct snd_pcm_substream *substream, *prev;
6441da177e4SLinus Torvalds 
6458eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
6461a60d4c5SIngo Molnar 	mutex_init(&pstr->oss.setup_mutex);
6471da177e4SLinus Torvalds #endif
6481da177e4SLinus Torvalds 	pstr->stream = stream;
6491da177e4SLinus Torvalds 	pstr->pcm = pcm;
6501da177e4SLinus Torvalds 	pstr->substream_count = substream_count;
651ef46c7afSTakashi Iwai 	if (!substream_count)
652ef46c7afSTakashi Iwai 		return 0;
653ef46c7afSTakashi Iwai 
654bc41a722STakashi Iwai 	err = snd_device_alloc(&pstr->dev, pcm->card);
655bc41a722STakashi Iwai 	if (err < 0)
656bc41a722STakashi Iwai 		return err;
657bc41a722STakashi Iwai 	dev_set_name(pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
658ef46c7afSTakashi Iwai 		     stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
659bc41a722STakashi Iwai 	pstr->dev->groups = pcm_dev_attr_groups;
660bc41a722STakashi Iwai 	pstr->dev->type = &pcm_dev_type;
661bc41a722STakashi Iwai 	dev_set_drvdata(pstr->dev, pstr);
662ef46c7afSTakashi Iwai 
663ef46c7afSTakashi Iwai 	if (!pcm->internal) {
6641da177e4SLinus Torvalds 		err = snd_pcm_stream_proc_init(pstr);
66573e77ba0STakashi Iwai 		if (err < 0) {
66609e56df8STakashi Iwai 			pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
6671da177e4SLinus Torvalds 			return err;
6681da177e4SLinus Torvalds 		}
66973e77ba0STakashi Iwai 	}
6701da177e4SLinus Torvalds 	prev = NULL;
6711da177e4SLinus Torvalds 	for (idx = 0, prev = NULL; idx < substream_count; idx++) {
672ca2c0966STakashi Iwai 		substream = kzalloc(sizeof(*substream), GFP_KERNEL);
673ec0e9937STakashi Iwai 		if (!substream)
6741da177e4SLinus Torvalds 			return -ENOMEM;
6751da177e4SLinus Torvalds 		substream->pcm = pcm;
6761da177e4SLinus Torvalds 		substream->pstr = pstr;
6771da177e4SLinus Torvalds 		substream->number = idx;
6781da177e4SLinus Torvalds 		substream->stream = stream;
6791da177e4SLinus Torvalds 		sprintf(substream->name, "subdevice #%i", idx);
6801da177e4SLinus Torvalds 		substream->buffer_bytes_max = UINT_MAX;
6811da177e4SLinus Torvalds 		if (prev == NULL)
6821da177e4SLinus Torvalds 			pstr->substream = substream;
6831da177e4SLinus Torvalds 		else
6841da177e4SLinus Torvalds 			prev->next = substream;
685945e5038SLiam Girdwood 
686945e5038SLiam Girdwood 		if (!pcm->internal) {
6871da177e4SLinus Torvalds 			err = snd_pcm_substream_proc_init(substream);
6881da177e4SLinus Torvalds 			if (err < 0) {
68909e56df8STakashi Iwai 				pcm_err(pcm,
69009e56df8STakashi Iwai 					"Error in snd_pcm_stream_proc_init\n");
6914d361285SAkinobu Mita 				if (prev == NULL)
6924d361285SAkinobu Mita 					pstr->substream = NULL;
6934d361285SAkinobu Mita 				else
6944d361285SAkinobu Mita 					prev->next = NULL;
6951da177e4SLinus Torvalds 				kfree(substream);
6961da177e4SLinus Torvalds 				return err;
6971da177e4SLinus Torvalds 			}
698945e5038SLiam Girdwood 		}
6991da177e4SLinus Torvalds 		substream->group = &substream->self_group;
70073365cb1STakashi Iwai 		snd_pcm_group_init(&substream->self_group);
7011da177e4SLinus Torvalds 		list_add_tail(&substream->link_list, &substream->self_group.substreams);
7029c323fcbSTakashi Iwai 		atomic_set(&substream->mmap_count, 0);
7031da177e4SLinus Torvalds 		prev = substream;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 	return 0;
7061da177e4SLinus Torvalds }
707e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_new_stream);
708e88e8ae6STakashi Iwai 
_snd_pcm_new(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,bool internal,struct snd_pcm ** rpcm)709945e5038SLiam Girdwood static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
710945e5038SLiam Girdwood 		int playback_count, int capture_count, bool internal,
711877211f5STakashi Iwai 		struct snd_pcm **rpcm)
7121da177e4SLinus Torvalds {
713877211f5STakashi Iwai 	struct snd_pcm *pcm;
7141da177e4SLinus Torvalds 	int err;
715f15ee210STakashi Iwai 	static const struct snd_device_ops ops = {
7161da177e4SLinus Torvalds 		.dev_free = snd_pcm_dev_free,
7171da177e4SLinus Torvalds 		.dev_register =	snd_pcm_dev_register,
7181da177e4SLinus Torvalds 		.dev_disconnect = snd_pcm_dev_disconnect,
7191da177e4SLinus Torvalds 	};
720f15ee210STakashi Iwai 	static const struct snd_device_ops internal_ops = {
7218b645e4aSTakashi Iwai 		.dev_free = snd_pcm_dev_free,
7228b645e4aSTakashi Iwai 	};
7231da177e4SLinus Torvalds 
7247eaa943cSTakashi Iwai 	if (snd_BUG_ON(!card))
7257eaa943cSTakashi Iwai 		return -ENXIO;
7267eaa943cSTakashi Iwai 	if (rpcm)
7271da177e4SLinus Torvalds 		*rpcm = NULL;
728ca2c0966STakashi Iwai 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
729ec0e9937STakashi Iwai 	if (!pcm)
7301da177e4SLinus Torvalds 		return -ENOMEM;
7311da177e4SLinus Torvalds 	pcm->card = card;
7321da177e4SLinus Torvalds 	pcm->device = device;
733945e5038SLiam Girdwood 	pcm->internal = internal;
734b95bd3a4STakashi Iwai 	mutex_init(&pcm->open_mutex);
735b95bd3a4STakashi Iwai 	init_waitqueue_head(&pcm->open_wait);
736b95bd3a4STakashi Iwai 	INIT_LIST_HEAD(&pcm->list);
73773e77ba0STakashi Iwai 	if (id)
73875b1a8f9SJoe Perches 		strscpy(pcm->id, id, sizeof(pcm->id));
73997d15a14SMarkus Elfring 
74097d15a14SMarkus Elfring 	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
74197d15a14SMarkus Elfring 				 playback_count);
74297d15a14SMarkus Elfring 	if (err < 0)
74397d15a14SMarkus Elfring 		goto free_pcm;
74497d15a14SMarkus Elfring 
74597d15a14SMarkus Elfring 	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
74697d15a14SMarkus Elfring 	if (err < 0)
74797d15a14SMarkus Elfring 		goto free_pcm;
74897d15a14SMarkus Elfring 
7498b645e4aSTakashi Iwai 	err = snd_device_new(card, SNDRV_DEV_PCM, pcm,
7508b645e4aSTakashi Iwai 			     internal ? &internal_ops : &ops);
75197d15a14SMarkus Elfring 	if (err < 0)
75297d15a14SMarkus Elfring 		goto free_pcm;
75397d15a14SMarkus Elfring 
7547eaa943cSTakashi Iwai 	if (rpcm)
7551da177e4SLinus Torvalds 		*rpcm = pcm;
7561da177e4SLinus Torvalds 	return 0;
75797d15a14SMarkus Elfring 
75897d15a14SMarkus Elfring free_pcm:
75997d15a14SMarkus Elfring 	snd_pcm_free(pcm);
76097d15a14SMarkus Elfring 	return err;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763945e5038SLiam Girdwood /**
764945e5038SLiam Girdwood  * snd_pcm_new - create a new PCM instance
765945e5038SLiam Girdwood  * @card: the card instance
766945e5038SLiam Girdwood  * @id: the id string
767945e5038SLiam Girdwood  * @device: the device index (zero based)
768945e5038SLiam Girdwood  * @playback_count: the number of substreams for playback
769945e5038SLiam Girdwood  * @capture_count: the number of substreams for capture
770945e5038SLiam Girdwood  * @rpcm: the pointer to store the new pcm instance
771945e5038SLiam Girdwood  *
772945e5038SLiam Girdwood  * Creates a new PCM instance.
773945e5038SLiam Girdwood  *
774945e5038SLiam Girdwood  * The pcm operators have to be set afterwards to the new instance
775945e5038SLiam Girdwood  * via snd_pcm_set_ops().
776945e5038SLiam Girdwood  *
777eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
778945e5038SLiam Girdwood  */
snd_pcm_new(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,struct snd_pcm ** rpcm)779945e5038SLiam Girdwood int snd_pcm_new(struct snd_card *card, const char *id, int device,
780945e5038SLiam Girdwood 		int playback_count, int capture_count, struct snd_pcm **rpcm)
781945e5038SLiam Girdwood {
782945e5038SLiam Girdwood 	return _snd_pcm_new(card, id, device, playback_count, capture_count,
783945e5038SLiam Girdwood 			false, rpcm);
784945e5038SLiam Girdwood }
785e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_new);
786e88e8ae6STakashi Iwai 
787945e5038SLiam Girdwood /**
788945e5038SLiam Girdwood  * snd_pcm_new_internal - create a new internal PCM instance
789945e5038SLiam Girdwood  * @card: the card instance
790945e5038SLiam Girdwood  * @id: the id string
791945e5038SLiam Girdwood  * @device: the device index (zero based - shared with normal PCMs)
792945e5038SLiam Girdwood  * @playback_count: the number of substreams for playback
793945e5038SLiam Girdwood  * @capture_count: the number of substreams for capture
794945e5038SLiam Girdwood  * @rpcm: the pointer to store the new pcm instance
795945e5038SLiam Girdwood  *
796945e5038SLiam Girdwood  * Creates a new internal PCM instance with no userspace device or procfs
797945e5038SLiam Girdwood  * entries. This is used by ASoC Back End PCMs in order to create a PCM that
798945e5038SLiam Girdwood  * will only be used internally by kernel drivers. i.e. it cannot be opened
799945e5038SLiam Girdwood  * by userspace. It provides existing ASoC components drivers with a substream
800945e5038SLiam Girdwood  * and access to any private data.
801945e5038SLiam Girdwood  *
802945e5038SLiam Girdwood  * The pcm operators have to be set afterwards to the new instance
803945e5038SLiam Girdwood  * via snd_pcm_set_ops().
804945e5038SLiam Girdwood  *
805eb7c06e8SYacine Belkadi  * Return: Zero if successful, or a negative error code on failure.
806945e5038SLiam Girdwood  */
snd_pcm_new_internal(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,struct snd_pcm ** rpcm)807945e5038SLiam Girdwood int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
808945e5038SLiam Girdwood 	int playback_count, int capture_count,
809945e5038SLiam Girdwood 	struct snd_pcm **rpcm)
810945e5038SLiam Girdwood {
811945e5038SLiam Girdwood 	return _snd_pcm_new(card, id, device, playback_count, capture_count,
812945e5038SLiam Girdwood 			true, rpcm);
813945e5038SLiam Girdwood }
814945e5038SLiam Girdwood EXPORT_SYMBOL(snd_pcm_new_internal);
815945e5038SLiam Girdwood 
free_chmap(struct snd_pcm_str * pstr)816a8ff48cbSTakashi Iwai static void free_chmap(struct snd_pcm_str *pstr)
817a8ff48cbSTakashi Iwai {
818a8ff48cbSTakashi Iwai 	if (pstr->chmap_kctl) {
8195471e976STakashi Iwai 		struct snd_card *card = pstr->pcm->card;
8205471e976STakashi Iwai 
8215471e976STakashi Iwai 		snd_ctl_remove(card, pstr->chmap_kctl);
822a8ff48cbSTakashi Iwai 		pstr->chmap_kctl = NULL;
823a8ff48cbSTakashi Iwai 	}
824a8ff48cbSTakashi Iwai }
825a8ff48cbSTakashi Iwai 
snd_pcm_free_stream(struct snd_pcm_str * pstr)826877211f5STakashi Iwai static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
8271da177e4SLinus Torvalds {
828877211f5STakashi Iwai 	struct snd_pcm_substream *substream, *substream_next;
8298eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
830877211f5STakashi Iwai 	struct snd_pcm_oss_setup *setup, *setupn;
8311da177e4SLinus Torvalds #endif
832480e32ebSTakashi Iwai 
833480e32ebSTakashi Iwai 	/* free all proc files under the stream */
834480e32ebSTakashi Iwai 	snd_pcm_stream_proc_done(pstr);
835480e32ebSTakashi Iwai 
8361da177e4SLinus Torvalds 	substream = pstr->substream;
8371da177e4SLinus Torvalds 	while (substream) {
8381da177e4SLinus Torvalds 		substream_next = substream->next;
839c461482cSTakashi Iwai 		snd_pcm_timer_done(substream);
8401da177e4SLinus Torvalds 		kfree(substream);
8411da177e4SLinus Torvalds 		substream = substream_next;
8421da177e4SLinus Torvalds 	}
8438eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
8441da177e4SLinus Torvalds 	for (setup = pstr->oss.setup_list; setup; setup = setupn) {
8451da177e4SLinus Torvalds 		setupn = setup->next;
8461da177e4SLinus Torvalds 		kfree(setup->task_name);
8471da177e4SLinus Torvalds 		kfree(setup);
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds #endif
850a8ff48cbSTakashi Iwai 	free_chmap(pstr);
851ef46c7afSTakashi Iwai 	if (pstr->substream_count)
852bc41a722STakashi Iwai 		put_device(pstr->dev);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
85558f30d65STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
85658f30d65STakashi Iwai #define pcm_call_notify(pcm, call)					\
85758f30d65STakashi Iwai 	do {								\
85858f30d65STakashi Iwai 		struct snd_pcm_notify *_notify;				\
85958f30d65STakashi Iwai 		list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
86058f30d65STakashi Iwai 			_notify->call(pcm);				\
86158f30d65STakashi Iwai 	} while (0)
86258f30d65STakashi Iwai #else
8639aee03f3SArnd Bergmann #define pcm_call_notify(pcm, call) do {} while (0)
86458f30d65STakashi Iwai #endif
86558f30d65STakashi Iwai 
snd_pcm_free(struct snd_pcm * pcm)866877211f5STakashi Iwai static int snd_pcm_free(struct snd_pcm *pcm)
8671da177e4SLinus Torvalds {
8687eaa943cSTakashi Iwai 	if (!pcm)
8697eaa943cSTakashi Iwai 		return 0;
87058f30d65STakashi Iwai 	if (!pcm->internal)
87158f30d65STakashi Iwai 		pcm_call_notify(pcm, n_unregister);
8721da177e4SLinus Torvalds 	if (pcm->private_free)
8731da177e4SLinus Torvalds 		pcm->private_free(pcm);
8741da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_free_for_all(pcm);
8751da177e4SLinus Torvalds 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
8761da177e4SLinus Torvalds 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
8771da177e4SLinus Torvalds 	kfree(pcm);
8781da177e4SLinus Torvalds 	return 0;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
snd_pcm_dev_free(struct snd_device * device)881877211f5STakashi Iwai static int snd_pcm_dev_free(struct snd_device *device)
8821da177e4SLinus Torvalds {
883877211f5STakashi Iwai 	struct snd_pcm *pcm = device->device_data;
8841da177e4SLinus Torvalds 	return snd_pcm_free(pcm);
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
snd_pcm_attach_substream(struct snd_pcm * pcm,int stream,struct file * file,struct snd_pcm_substream ** rsubstream)8873bf75f9bSTakashi Iwai int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
8883bf75f9bSTakashi Iwai 			     struct file *file,
889877211f5STakashi Iwai 			     struct snd_pcm_substream **rsubstream)
8901da177e4SLinus Torvalds {
891877211f5STakashi Iwai 	struct snd_pcm_str * pstr;
892877211f5STakashi Iwai 	struct snd_pcm_substream *substream;
893877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime;
894877211f5STakashi Iwai 	struct snd_card *card;
89523c18d4bSTakashi Iwai 	int prefer_subdevice;
8961da177e4SLinus Torvalds 	size_t size;
8971da177e4SLinus Torvalds 
8987eaa943cSTakashi Iwai 	if (snd_BUG_ON(!pcm || !rsubstream))
8997eaa943cSTakashi Iwai 		return -ENXIO;
900ad876c86STakashi Iwai 	if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
901ad876c86STakashi Iwai 		       stream != SNDRV_PCM_STREAM_CAPTURE))
902ad876c86STakashi Iwai 		return -EINVAL;
9031da177e4SLinus Torvalds 	*rsubstream = NULL;
9041da177e4SLinus Torvalds 	pstr = &pcm->streams[stream];
9053bf75f9bSTakashi Iwai 	if (pstr->substream == NULL || pstr->substream_count == 0)
9061da177e4SLinus Torvalds 		return -ENODEV;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	card = pcm->card;
90923c18d4bSTakashi Iwai 	prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
912ad876c86STakashi Iwai 		int opposite = !stream;
913ad876c86STakashi Iwai 
914ad876c86STakashi Iwai 		for (substream = pcm->streams[opposite].substream; substream;
915ad876c86STakashi Iwai 		     substream = substream->next) {
9161da177e4SLinus Torvalds 			if (SUBSTREAM_BUSY(substream))
9171da177e4SLinus Torvalds 				return -EAGAIN;
9181da177e4SLinus Torvalds 		}
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9210df63e44STakashi Iwai 	if (file->f_flags & O_APPEND) {
9220df63e44STakashi Iwai 		if (prefer_subdevice < 0) {
9230df63e44STakashi Iwai 			if (pstr->substream_count > 1)
9240df63e44STakashi Iwai 				return -EINVAL; /* must be unique */
9250df63e44STakashi Iwai 			substream = pstr->substream;
9260df63e44STakashi Iwai 		} else {
9270df63e44STakashi Iwai 			for (substream = pstr->substream; substream;
9280df63e44STakashi Iwai 			     substream = substream->next)
9290df63e44STakashi Iwai 				if (substream->number == prefer_subdevice)
9300df63e44STakashi Iwai 					break;
9310df63e44STakashi Iwai 		}
9320df63e44STakashi Iwai 		if (! substream)
9330df63e44STakashi Iwai 			return -ENODEV;
9340df63e44STakashi Iwai 		if (! SUBSTREAM_BUSY(substream))
9350df63e44STakashi Iwai 			return -EBADFD;
9360df63e44STakashi Iwai 		substream->ref_count++;
9370df63e44STakashi Iwai 		*rsubstream = substream;
9380df63e44STakashi Iwai 		return 0;
9390df63e44STakashi Iwai 	}
9400df63e44STakashi Iwai 
941ad876c86STakashi Iwai 	for (substream = pstr->substream; substream; substream = substream->next) {
942ad876c86STakashi Iwai 		if (!SUBSTREAM_BUSY(substream) &&
943ad876c86STakashi Iwai 		    (prefer_subdevice == -1 ||
944ad876c86STakashi Iwai 		     substream->number == prefer_subdevice))
9451da177e4SLinus Torvalds 			break;
946ad876c86STakashi Iwai 	}
9471da177e4SLinus Torvalds 	if (substream == NULL)
9481da177e4SLinus Torvalds 		return -EAGAIN;
9491da177e4SLinus Torvalds 
950ca2c0966STakashi Iwai 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
9511da177e4SLinus Torvalds 	if (runtime == NULL)
9521da177e4SLinus Torvalds 		return -ENOMEM;
9531da177e4SLinus Torvalds 
954877211f5STakashi Iwai 	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
955734b5a0bSTakashi Iwai 	runtime->status = alloc_pages_exact(size, GFP_KERNEL);
9561da177e4SLinus Torvalds 	if (runtime->status == NULL) {
9571da177e4SLinus Torvalds 		kfree(runtime);
9581da177e4SLinus Torvalds 		return -ENOMEM;
9591da177e4SLinus Torvalds 	}
960734b5a0bSTakashi Iwai 	memset(runtime->status, 0, size);
9611da177e4SLinus Torvalds 
962877211f5STakashi Iwai 	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
963734b5a0bSTakashi Iwai 	runtime->control = alloc_pages_exact(size, GFP_KERNEL);
9641da177e4SLinus Torvalds 	if (runtime->control == NULL) {
965734b5a0bSTakashi Iwai 		free_pages_exact(runtime->status,
966877211f5STakashi Iwai 			       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
9671da177e4SLinus Torvalds 		kfree(runtime);
9681da177e4SLinus Torvalds 		return -ENOMEM;
9691da177e4SLinus Torvalds 	}
970734b5a0bSTakashi Iwai 	memset(runtime->control, 0, size);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	init_waitqueue_head(&runtime->sleep);
973c91a988dSJaroslav Kysela 	init_waitqueue_head(&runtime->tsleep);
9741da177e4SLinus Torvalds 
975f0061c18STakashi Iwai 	__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_OPEN);
97692ee3c60STakashi Iwai 	mutex_init(&runtime->buffer_mutex);
977bc55cfd5STakashi Iwai 	atomic_set(&runtime->buffer_accessing, 0);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	substream->runtime = runtime;
9801da177e4SLinus Torvalds 	substream->private_data = pcm->private_data;
9810df63e44STakashi Iwai 	substream->ref_count = 1;
9820df63e44STakashi Iwai 	substream->f_flags = file->f_flags;
983e7373b70SClemens Ladisch 	substream->pid = get_pid(task_pid(current));
9841da177e4SLinus Torvalds 	pstr->substream_opened++;
9851da177e4SLinus Torvalds 	*rsubstream = substream;
9861da177e4SLinus Torvalds 	return 0;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
snd_pcm_detach_substream(struct snd_pcm_substream * substream)9893bf75f9bSTakashi Iwai void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
9901da177e4SLinus Torvalds {
991877211f5STakashi Iwai 	struct snd_pcm_runtime *runtime;
9920df63e44STakashi Iwai 
9937eaa943cSTakashi Iwai 	if (PCM_RUNTIME_CHECK(substream))
9947eaa943cSTakashi Iwai 		return;
9951da177e4SLinus Torvalds 	runtime = substream->runtime;
9961da177e4SLinus Torvalds 	if (runtime->private_free != NULL)
9971da177e4SLinus Torvalds 		runtime->private_free(runtime);
998734b5a0bSTakashi Iwai 	free_pages_exact(runtime->status,
999877211f5STakashi Iwai 		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
1000734b5a0bSTakashi Iwai 	free_pages_exact(runtime->control,
1001877211f5STakashi Iwai 		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
10021da177e4SLinus Torvalds 	kfree(runtime->hw_constraints.rules);
1003a820ccbeSTakashi Iwai 	/* Avoid concurrent access to runtime via PCM timer interface */
1004931522b9SPierre-Louis Bossart 	if (substream->timer) {
1005a820ccbeSTakashi Iwai 		spin_lock_irq(&substream->timer->lock);
10061da177e4SLinus Torvalds 		substream->runtime = NULL;
1007a820ccbeSTakashi Iwai 		spin_unlock_irq(&substream->timer->lock);
1008931522b9SPierre-Louis Bossart 	} else {
1009931522b9SPierre-Louis Bossart 		substream->runtime = NULL;
1010931522b9SPierre-Louis Bossart 	}
101192ee3c60STakashi Iwai 	mutex_destroy(&runtime->buffer_mutex);
101296b09709STakashi Iwai 	snd_fasync_free(runtime->fasync);
1013a820ccbeSTakashi Iwai 	kfree(runtime);
1014e7373b70SClemens Ladisch 	put_pid(substream->pid);
1015e7373b70SClemens Ladisch 	substream->pid = NULL;
10161da177e4SLinus Torvalds 	substream->pstr->substream_opened--;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
pcm_class_show(struct device * dev,struct device_attribute * attr,char * buf)1019e1dc219aSYueHaibing static ssize_t pcm_class_show(struct device *dev,
1020d80f19faSGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)
10219d19f48cSTakashi Iwai {
1022bc41a722STakashi Iwai 	struct snd_pcm_str *pstr = dev_get_drvdata(dev);
102360b93030STakashi Iwai 	struct snd_pcm *pcm = pstr->pcm;
10249d19f48cSTakashi Iwai 	const char *str;
10259d19f48cSTakashi Iwai 	static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
10269d19f48cSTakashi Iwai 		[SNDRV_PCM_CLASS_GENERIC] = "generic",
10279d19f48cSTakashi Iwai 		[SNDRV_PCM_CLASS_MULTI] = "multi",
10289d19f48cSTakashi Iwai 		[SNDRV_PCM_CLASS_MODEM] = "modem",
10299d19f48cSTakashi Iwai 		[SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
10309d19f48cSTakashi Iwai 	};
10319d19f48cSTakashi Iwai 
103260b93030STakashi Iwai 	if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
10339d19f48cSTakashi Iwai 		str = "none";
10349d19f48cSTakashi Iwai 	else
10359d19f48cSTakashi Iwai 		str = strs[pcm->dev_class];
103601043e3eSTakashi Iwai 	return sysfs_emit(buf, "%s\n", str);
10379d19f48cSTakashi Iwai }
10389d19f48cSTakashi Iwai 
1039e1dc219aSYueHaibing static DEVICE_ATTR_RO(pcm_class);
1040caa751baSTakashi Iwai static struct attribute *pcm_dev_attrs[] = {
1041caa751baSTakashi Iwai 	&dev_attr_pcm_class.attr,
1042caa751baSTakashi Iwai 	NULL
1043caa751baSTakashi Iwai };
1044caa751baSTakashi Iwai 
1045343fe850SArvind Yadav static const struct attribute_group pcm_dev_attr_group = {
1046caa751baSTakashi Iwai 	.attrs	= pcm_dev_attrs,
1047caa751baSTakashi Iwai };
1048caa751baSTakashi Iwai 
1049caa751baSTakashi Iwai static const struct attribute_group *pcm_dev_attr_groups[] = {
1050caa751baSTakashi Iwai 	&pcm_dev_attr_group,
1051caa751baSTakashi Iwai 	NULL
1052caa751baSTakashi Iwai };
10539d19f48cSTakashi Iwai 
snd_pcm_dev_register(struct snd_device * device)1054877211f5STakashi Iwai static int snd_pcm_dev_register(struct snd_device *device)
10551da177e4SLinus Torvalds {
1056f87135f5SClemens Ladisch 	int cidx, err;
1057877211f5STakashi Iwai 	struct snd_pcm_substream *substream;
10584b3be6afSJulia Lawall 	struct snd_pcm *pcm;
10591da177e4SLinus Torvalds 
10604b3be6afSJulia Lawall 	if (snd_BUG_ON(!device || !device->device_data))
10617eaa943cSTakashi Iwai 		return -ENXIO;
10624b3be6afSJulia Lawall 	pcm = device->device_data;
1063b95bd3a4STakashi Iwai 
10641a60d4c5SIngo Molnar 	mutex_lock(&register_mutex);
1065f90c06a2SPawel MOLL 	err = snd_pcm_add(pcm);
1066b95bd3a4STakashi Iwai 	if (err)
1067b95bd3a4STakashi Iwai 		goto unlock;
10681da177e4SLinus Torvalds 	for (cidx = 0; cidx < 2; cidx++) {
10691da177e4SLinus Torvalds 		int devtype = -1;
1070b95bd3a4STakashi Iwai 		if (pcm->streams[cidx].substream == NULL)
10711da177e4SLinus Torvalds 			continue;
10721da177e4SLinus Torvalds 		switch (cidx) {
10731da177e4SLinus Torvalds 		case SNDRV_PCM_STREAM_PLAYBACK:
10741da177e4SLinus Torvalds 			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
10751da177e4SLinus Torvalds 			break;
10761da177e4SLinus Torvalds 		case SNDRV_PCM_STREAM_CAPTURE:
10771da177e4SLinus Torvalds 			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
10781da177e4SLinus Torvalds 			break;
10791da177e4SLinus Torvalds 		}
1080c78085fcSJohannes Berg 		/* register pcm */
108140a4b263STakashi Iwai 		err = snd_register_device(devtype, pcm->card, pcm->device,
108240a4b263STakashi Iwai 					  &snd_pcm_f_ops[cidx], pcm,
1083bc41a722STakashi Iwai 					  pcm->streams[cidx].dev);
1084c78085fcSJohannes Berg 		if (err < 0) {
1085b95bd3a4STakashi Iwai 			list_del_init(&pcm->list);
1086b95bd3a4STakashi Iwai 			goto unlock;
10871da177e4SLinus Torvalds 		}
1088caa751baSTakashi Iwai 
10891da177e4SLinus Torvalds 		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
10901da177e4SLinus Torvalds 			snd_pcm_timer_init(substream);
10911da177e4SLinus Torvalds 	}
10929244b2c3SJohannes Berg 
109358f30d65STakashi Iwai 	pcm_call_notify(pcm, n_register);
10949244b2c3SJohannes Berg 
1095b95bd3a4STakashi Iwai  unlock:
10961a60d4c5SIngo Molnar 	mutex_unlock(&register_mutex);
1097b95bd3a4STakashi Iwai 	return err;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
snd_pcm_dev_disconnect(struct snd_device * device)1100877211f5STakashi Iwai static int snd_pcm_dev_disconnect(struct snd_device *device)
11011da177e4SLinus Torvalds {
1102877211f5STakashi Iwai 	struct snd_pcm *pcm = device->device_data;
1103877211f5STakashi Iwai 	struct snd_pcm_substream *substream;
110440a4b263STakashi Iwai 	int cidx;
11051da177e4SLinus Torvalds 
11061a60d4c5SIngo Molnar 	mutex_lock(&register_mutex);
11079b0573c0STakashi Iwai 	mutex_lock(&pcm->open_mutex);
11080914f796STakashi Iwai 	wake_up(&pcm->open_wait);
1109f87135f5SClemens Ladisch 	list_del_init(&pcm->list);
11108d19b4e0STakashi Iwai 
11118d19b4e0STakashi Iwai 	for_each_pcm_substream(pcm, cidx, substream) {
11129b0573c0STakashi Iwai 		snd_pcm_stream_lock_irq(substream);
11130914f796STakashi Iwai 		if (substream->runtime) {
11146ca73de7STakashi Iwai 			if (snd_pcm_running(substream))
11158d19b4e0STakashi Iwai 				snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
11166ca73de7STakashi Iwai 			/* to be sure, set the state unconditionally */
1117f0061c18STakashi Iwai 			__snd_pcm_set_state(substream->runtime,
1118f0061c18STakashi Iwai 					    SNDRV_PCM_STATE_DISCONNECTED);
11190914f796STakashi Iwai 			wake_up(&substream->runtime->sleep);
11200914f796STakashi Iwai 			wake_up(&substream->runtime->tsleep);
11210914f796STakashi Iwai 		}
11229b0573c0STakashi Iwai 		snd_pcm_stream_unlock_irq(substream);
11239b0573c0STakashi Iwai 	}
11248b645e4aSTakashi Iwai 
11258d19b4e0STakashi Iwai 	for_each_pcm_substream(pcm, cidx, substream)
112629bb274eSTakashi Iwai 		snd_pcm_sync_stop(substream, false);
112729bb274eSTakashi Iwai 
112858f30d65STakashi Iwai 	pcm_call_notify(pcm, n_disconnect);
11291da177e4SLinus Torvalds 	for (cidx = 0; cidx < 2; cidx++) {
1130bc41a722STakashi Iwai 		if (pcm->streams[cidx].dev)
1131bc41a722STakashi Iwai 			snd_unregister_device(pcm->streams[cidx].dev);
1132a8ff48cbSTakashi Iwai 		free_chmap(&pcm->streams[cidx]);
11331da177e4SLinus Torvalds 	}
11349b0573c0STakashi Iwai 	mutex_unlock(&pcm->open_mutex);
11351a60d4c5SIngo Molnar 	mutex_unlock(&register_mutex);
1136c461482cSTakashi Iwai 	return 0;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
113958f30d65STakashi Iwai #if IS_ENABLED(CONFIG_SND_PCM_OSS)
114030b771cfSTakashi Iwai /**
114130b771cfSTakashi Iwai  * snd_pcm_notify - Add/remove the notify list
114230b771cfSTakashi Iwai  * @notify: PCM notify list
114330b771cfSTakashi Iwai  * @nfree: 0 = register, 1 = unregister
114430b771cfSTakashi Iwai  *
114530b771cfSTakashi Iwai  * This adds the given notifier to the global list so that the callback is
114630b771cfSTakashi Iwai  * called for each registered PCM devices.  This exists only for PCM OSS
114730b771cfSTakashi Iwai  * emulation, so far.
11484e2b7067STakashi Iwai  *
11494e2b7067STakashi Iwai  * Return: zero if successful, or a negative error code
115030b771cfSTakashi Iwai  */
snd_pcm_notify(struct snd_pcm_notify * notify,int nfree)1151877211f5STakashi Iwai int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
11521da177e4SLinus Torvalds {
11539244b2c3SJohannes Berg 	struct snd_pcm *pcm;
11541da177e4SLinus Torvalds 
11557eaa943cSTakashi Iwai 	if (snd_BUG_ON(!notify ||
11567eaa943cSTakashi Iwai 		       !notify->n_register ||
11577eaa943cSTakashi Iwai 		       !notify->n_unregister ||
11587eaa943cSTakashi Iwai 		       !notify->n_disconnect))
11597eaa943cSTakashi Iwai 		return -EINVAL;
11601a60d4c5SIngo Molnar 	mutex_lock(&register_mutex);
11611da177e4SLinus Torvalds 	if (nfree) {
11621da177e4SLinus Torvalds 		list_del(&notify->list);
11639244b2c3SJohannes Berg 		list_for_each_entry(pcm, &snd_pcm_devices, list)
11649244b2c3SJohannes Berg 			notify->n_unregister(pcm);
11651da177e4SLinus Torvalds 	} else {
11661da177e4SLinus Torvalds 		list_add_tail(&notify->list, &snd_pcm_notify_list);
11679244b2c3SJohannes Berg 		list_for_each_entry(pcm, &snd_pcm_devices, list)
11689244b2c3SJohannes Berg 			notify->n_register(pcm);
11691da177e4SLinus Torvalds 	}
11701a60d4c5SIngo Molnar 	mutex_unlock(&register_mutex);
11711da177e4SLinus Torvalds 	return 0;
11721da177e4SLinus Torvalds }
1173e88e8ae6STakashi Iwai EXPORT_SYMBOL(snd_pcm_notify);
117458f30d65STakashi Iwai #endif /* CONFIG_SND_PCM_OSS */
1175e88e8ae6STakashi Iwai 
1176cd6a6503SJie Yang #ifdef CONFIG_SND_PROC_FS
11771da177e4SLinus Torvalds /*
11781da177e4SLinus Torvalds  *  Info interface
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds 
snd_pcm_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1181877211f5STakashi Iwai static void snd_pcm_proc_read(struct snd_info_entry *entry,
1182877211f5STakashi Iwai 			      struct snd_info_buffer *buffer)
11831da177e4SLinus Torvalds {
1184877211f5STakashi Iwai 	struct snd_pcm *pcm;
11851da177e4SLinus Torvalds 
11861a60d4c5SIngo Molnar 	mutex_lock(&register_mutex);
11879244b2c3SJohannes Berg 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
1188f87135f5SClemens Ladisch 		snd_iprintf(buffer, "%02i-%02i: %s : %s",
1189f87135f5SClemens Ladisch 			    pcm->card->number, pcm->device, pcm->id, pcm->name);
11901da177e4SLinus Torvalds 		if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1191877211f5STakashi Iwai 			snd_iprintf(buffer, " : playback %i",
1192877211f5STakashi Iwai 				    pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
11931da177e4SLinus Torvalds 		if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1194877211f5STakashi Iwai 			snd_iprintf(buffer, " : capture %i",
1195877211f5STakashi Iwai 				    pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
11961da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n");
11971da177e4SLinus Torvalds 	}
11981a60d4c5SIngo Molnar 	mutex_unlock(&register_mutex);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12016581f4e7STakashi Iwai static struct snd_info_entry *snd_pcm_proc_entry;
12021da177e4SLinus Torvalds 
snd_pcm_proc_init(void)1203e28563ccSTakashi Iwai static void snd_pcm_proc_init(void)
12041da177e4SLinus Torvalds {
1205877211f5STakashi Iwai 	struct snd_info_entry *entry;
12061da177e4SLinus Torvalds 
1207c8da9be4SMarkus Elfring 	entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL);
1208c8da9be4SMarkus Elfring 	if (entry) {
1209bf850204STakashi Iwai 		snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
12101da177e4SLinus Torvalds 		if (snd_info_register(entry) < 0) {
12111da177e4SLinus Torvalds 			snd_info_free_entry(entry);
12121da177e4SLinus Torvalds 			entry = NULL;
12131da177e4SLinus Torvalds 		}
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 	snd_pcm_proc_entry = entry;
1216e28563ccSTakashi Iwai }
1217e28563ccSTakashi Iwai 
snd_pcm_proc_done(void)1218e28563ccSTakashi Iwai static void snd_pcm_proc_done(void)
1219e28563ccSTakashi Iwai {
1220746d4a02STakashi Iwai 	snd_info_free_entry(snd_pcm_proc_entry);
1221e28563ccSTakashi Iwai }
1222e28563ccSTakashi Iwai 
1223cd6a6503SJie Yang #else /* !CONFIG_SND_PROC_FS */
1224e28563ccSTakashi Iwai #define snd_pcm_proc_init()
1225e28563ccSTakashi Iwai #define snd_pcm_proc_done()
1226cd6a6503SJie Yang #endif /* CONFIG_SND_PROC_FS */
1227e28563ccSTakashi Iwai 
1228e28563ccSTakashi Iwai 
1229e28563ccSTakashi Iwai /*
1230e28563ccSTakashi Iwai  *  ENTRY functions
1231e28563ccSTakashi Iwai  */
1232e28563ccSTakashi Iwai 
alsa_pcm_init(void)1233e28563ccSTakashi Iwai static int __init alsa_pcm_init(void)
1234e28563ccSTakashi Iwai {
1235e28563ccSTakashi Iwai 	snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1236e28563ccSTakashi Iwai 	snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1237e28563ccSTakashi Iwai 	snd_pcm_proc_init();
12381da177e4SLinus Torvalds 	return 0;
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
alsa_pcm_exit(void)12411da177e4SLinus Torvalds static void __exit alsa_pcm_exit(void)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
12441da177e4SLinus Torvalds 	snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1245e28563ccSTakashi Iwai 	snd_pcm_proc_done();
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds module_init(alsa_pcm_init)
12491da177e4SLinus Torvalds module_exit(alsa_pcm_exit)
1250