xref: /kernel/linux/linux-5.10/sound/core/pcm.c (revision 8c2ecf20)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *  Digital Audio (PCM) abstract layer
4 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
6
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/module.h>
10#include <linux/time.h>
11#include <linux/mutex.h>
12#include <linux/device.h>
13#include <linux/nospec.h>
14#include <sound/core.h>
15#include <sound/minors.h>
16#include <sound/pcm.h>
17#include <sound/timer.h>
18#include <sound/control.h>
19#include <sound/info.h>
20
21#include "pcm_local.h"
22
23MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
24MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
25MODULE_LICENSE("GPL");
26
27static LIST_HEAD(snd_pcm_devices);
28static DEFINE_MUTEX(register_mutex);
29#if IS_ENABLED(CONFIG_SND_PCM_OSS)
30static LIST_HEAD(snd_pcm_notify_list);
31#endif
32
33static int snd_pcm_free(struct snd_pcm *pcm);
34static int snd_pcm_dev_free(struct snd_device *device);
35static int snd_pcm_dev_register(struct snd_device *device);
36static int snd_pcm_dev_disconnect(struct snd_device *device);
37
38static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
39{
40	struct snd_pcm *pcm;
41
42	list_for_each_entry(pcm, &snd_pcm_devices, list) {
43		if (pcm->card == card && pcm->device == device)
44			return pcm;
45	}
46	return NULL;
47}
48
49static int snd_pcm_next(struct snd_card *card, int device)
50{
51	struct snd_pcm *pcm;
52
53	list_for_each_entry(pcm, &snd_pcm_devices, list) {
54		if (pcm->card == card && pcm->device > device)
55			return pcm->device;
56		else if (pcm->card->number > card->number)
57			return -1;
58	}
59	return -1;
60}
61
62static int snd_pcm_add(struct snd_pcm *newpcm)
63{
64	struct snd_pcm *pcm;
65
66	if (newpcm->internal)
67		return 0;
68
69	list_for_each_entry(pcm, &snd_pcm_devices, list) {
70		if (pcm->card == newpcm->card && pcm->device == newpcm->device)
71			return -EBUSY;
72		if (pcm->card->number > newpcm->card->number ||
73				(pcm->card == newpcm->card &&
74				pcm->device > newpcm->device)) {
75			list_add(&newpcm->list, pcm->list.prev);
76			return 0;
77		}
78	}
79	list_add_tail(&newpcm->list, &snd_pcm_devices);
80	return 0;
81}
82
83static int snd_pcm_control_ioctl(struct snd_card *card,
84				 struct snd_ctl_file *control,
85				 unsigned int cmd, unsigned long arg)
86{
87	switch (cmd) {
88	case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
89		{
90			int device;
91
92			if (get_user(device, (int __user *)arg))
93				return -EFAULT;
94			mutex_lock(&register_mutex);
95			device = snd_pcm_next(card, device);
96			mutex_unlock(&register_mutex);
97			if (put_user(device, (int __user *)arg))
98				return -EFAULT;
99			return 0;
100		}
101	case SNDRV_CTL_IOCTL_PCM_INFO:
102		{
103			struct snd_pcm_info __user *info;
104			unsigned int device, subdevice;
105			int stream;
106			struct snd_pcm *pcm;
107			struct snd_pcm_str *pstr;
108			struct snd_pcm_substream *substream;
109			int err;
110
111			info = (struct snd_pcm_info __user *)arg;
112			if (get_user(device, &info->device))
113				return -EFAULT;
114			if (get_user(stream, &info->stream))
115				return -EFAULT;
116			if (stream < 0 || stream > 1)
117				return -EINVAL;
118			stream = array_index_nospec(stream, 2);
119			if (get_user(subdevice, &info->subdevice))
120				return -EFAULT;
121			mutex_lock(&register_mutex);
122			pcm = snd_pcm_get(card, device);
123			if (pcm == NULL) {
124				err = -ENXIO;
125				goto _error;
126			}
127			pstr = &pcm->streams[stream];
128			if (pstr->substream_count == 0) {
129				err = -ENOENT;
130				goto _error;
131			}
132			if (subdevice >= pstr->substream_count) {
133				err = -ENXIO;
134				goto _error;
135			}
136			for (substream = pstr->substream; substream;
137			     substream = substream->next)
138				if (substream->number == (int)subdevice)
139					break;
140			if (substream == NULL) {
141				err = -ENXIO;
142				goto _error;
143			}
144			mutex_lock(&pcm->open_mutex);
145			err = snd_pcm_info_user(substream, info);
146			mutex_unlock(&pcm->open_mutex);
147		_error:
148			mutex_unlock(&register_mutex);
149			return err;
150		}
151	case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
152		{
153			int val;
154
155			if (get_user(val, (int __user *)arg))
156				return -EFAULT;
157			control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
158			return 0;
159		}
160	}
161	return -ENOIOCTLCMD;
162}
163
164#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
165
166static const char * const snd_pcm_format_names[] = {
167	FORMAT(S8),
168	FORMAT(U8),
169	FORMAT(S16_LE),
170	FORMAT(S16_BE),
171	FORMAT(U16_LE),
172	FORMAT(U16_BE),
173	FORMAT(S24_LE),
174	FORMAT(S24_BE),
175	FORMAT(U24_LE),
176	FORMAT(U24_BE),
177	FORMAT(S32_LE),
178	FORMAT(S32_BE),
179	FORMAT(U32_LE),
180	FORMAT(U32_BE),
181	FORMAT(FLOAT_LE),
182	FORMAT(FLOAT_BE),
183	FORMAT(FLOAT64_LE),
184	FORMAT(FLOAT64_BE),
185	FORMAT(IEC958_SUBFRAME_LE),
186	FORMAT(IEC958_SUBFRAME_BE),
187	FORMAT(MU_LAW),
188	FORMAT(A_LAW),
189	FORMAT(IMA_ADPCM),
190	FORMAT(MPEG),
191	FORMAT(GSM),
192	FORMAT(SPECIAL),
193	FORMAT(S24_3LE),
194	FORMAT(S24_3BE),
195	FORMAT(U24_3LE),
196	FORMAT(U24_3BE),
197	FORMAT(S20_3LE),
198	FORMAT(S20_3BE),
199	FORMAT(U20_3LE),
200	FORMAT(U20_3BE),
201	FORMAT(S18_3LE),
202	FORMAT(S18_3BE),
203	FORMAT(U18_3LE),
204	FORMAT(U18_3BE),
205	FORMAT(G723_24),
206	FORMAT(G723_24_1B),
207	FORMAT(G723_40),
208	FORMAT(G723_40_1B),
209	FORMAT(DSD_U8),
210	FORMAT(DSD_U16_LE),
211	FORMAT(DSD_U32_LE),
212	FORMAT(DSD_U16_BE),
213	FORMAT(DSD_U32_BE),
214};
215
216/**
217 * snd_pcm_format_name - Return a name string for the given PCM format
218 * @format: PCM format
219 */
220const char *snd_pcm_format_name(snd_pcm_format_t format)
221{
222	if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
223		return "Unknown";
224	return snd_pcm_format_names[(__force unsigned int)format];
225}
226EXPORT_SYMBOL_GPL(snd_pcm_format_name);
227
228#ifdef CONFIG_SND_VERBOSE_PROCFS
229
230#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
231#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
232#define READY(v) [SNDRV_PCM_READY_##v] = #v
233#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
234#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
235#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
236#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
237#define START(v) [SNDRV_PCM_START_##v] = #v
238#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
239
240static const char * const snd_pcm_stream_names[] = {
241	STREAM(PLAYBACK),
242	STREAM(CAPTURE),
243};
244
245static const char * const snd_pcm_state_names[] = {
246	STATE(OPEN),
247	STATE(SETUP),
248	STATE(PREPARED),
249	STATE(RUNNING),
250	STATE(XRUN),
251	STATE(DRAINING),
252	STATE(PAUSED),
253	STATE(SUSPENDED),
254	STATE(DISCONNECTED),
255};
256
257static const char * const snd_pcm_access_names[] = {
258	ACCESS(MMAP_INTERLEAVED),
259	ACCESS(MMAP_NONINTERLEAVED),
260	ACCESS(MMAP_COMPLEX),
261	ACCESS(RW_INTERLEAVED),
262	ACCESS(RW_NONINTERLEAVED),
263};
264
265static const char * const snd_pcm_subformat_names[] = {
266	SUBFORMAT(STD),
267};
268
269static const char * const snd_pcm_tstamp_mode_names[] = {
270	TSTAMP(NONE),
271	TSTAMP(ENABLE),
272};
273
274static const char *snd_pcm_stream_name(int stream)
275{
276	return snd_pcm_stream_names[stream];
277}
278
279static const char *snd_pcm_access_name(snd_pcm_access_t access)
280{
281	return snd_pcm_access_names[(__force int)access];
282}
283
284static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
285{
286	return snd_pcm_subformat_names[(__force int)subformat];
287}
288
289static const char *snd_pcm_tstamp_mode_name(int mode)
290{
291	return snd_pcm_tstamp_mode_names[mode];
292}
293
294static const char *snd_pcm_state_name(snd_pcm_state_t state)
295{
296	return snd_pcm_state_names[(__force int)state];
297}
298
299#if IS_ENABLED(CONFIG_SND_PCM_OSS)
300#include <linux/soundcard.h>
301
302static const char *snd_pcm_oss_format_name(int format)
303{
304	switch (format) {
305	case AFMT_MU_LAW:
306		return "MU_LAW";
307	case AFMT_A_LAW:
308		return "A_LAW";
309	case AFMT_IMA_ADPCM:
310		return "IMA_ADPCM";
311	case AFMT_U8:
312		return "U8";
313	case AFMT_S16_LE:
314		return "S16_LE";
315	case AFMT_S16_BE:
316		return "S16_BE";
317	case AFMT_S8:
318		return "S8";
319	case AFMT_U16_LE:
320		return "U16_LE";
321	case AFMT_U16_BE:
322		return "U16_BE";
323	case AFMT_MPEG:
324		return "MPEG";
325	default:
326		return "unknown";
327	}
328}
329#endif
330
331static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
332				   struct snd_info_buffer *buffer)
333{
334	struct snd_pcm_info *info;
335	int err;
336
337	if (! substream)
338		return;
339
340	info = kmalloc(sizeof(*info), GFP_KERNEL);
341	if (!info)
342		return;
343
344	err = snd_pcm_info(substream, info);
345	if (err < 0) {
346		snd_iprintf(buffer, "error %d\n", err);
347		kfree(info);
348		return;
349	}
350	snd_iprintf(buffer, "card: %d\n", info->card);
351	snd_iprintf(buffer, "device: %d\n", info->device);
352	snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
353	snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
354	snd_iprintf(buffer, "id: %s\n", info->id);
355	snd_iprintf(buffer, "name: %s\n", info->name);
356	snd_iprintf(buffer, "subname: %s\n", info->subname);
357	snd_iprintf(buffer, "class: %d\n", info->dev_class);
358	snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
359	snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
360	snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
361	kfree(info);
362}
363
364static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
365					  struct snd_info_buffer *buffer)
366{
367	snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
368			       buffer);
369}
370
371static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
372					     struct snd_info_buffer *buffer)
373{
374	snd_pcm_proc_info_read(entry->private_data, buffer);
375}
376
377static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
378						  struct snd_info_buffer *buffer)
379{
380	struct snd_pcm_substream *substream = entry->private_data;
381	struct snd_pcm_runtime *runtime;
382
383	mutex_lock(&substream->pcm->open_mutex);
384	runtime = substream->runtime;
385	if (!runtime) {
386		snd_iprintf(buffer, "closed\n");
387		goto unlock;
388	}
389	if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
390		snd_iprintf(buffer, "no setup\n");
391		goto unlock;
392	}
393	snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
394	snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
395	snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
396	snd_iprintf(buffer, "channels: %u\n", runtime->channels);
397	snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
398	snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
399	snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
400#if IS_ENABLED(CONFIG_SND_PCM_OSS)
401	if (substream->oss.oss) {
402		snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
403		snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
404		snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
405		snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
406		snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
407		snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
408	}
409#endif
410 unlock:
411	mutex_unlock(&substream->pcm->open_mutex);
412}
413
414static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
415						  struct snd_info_buffer *buffer)
416{
417	struct snd_pcm_substream *substream = entry->private_data;
418	struct snd_pcm_runtime *runtime;
419
420	mutex_lock(&substream->pcm->open_mutex);
421	runtime = substream->runtime;
422	if (!runtime) {
423		snd_iprintf(buffer, "closed\n");
424		goto unlock;
425	}
426	if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
427		snd_iprintf(buffer, "no setup\n");
428		goto unlock;
429	}
430	snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
431	snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
432	snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
433	snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
434	snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
435	snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
436	snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
437	snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
438 unlock:
439	mutex_unlock(&substream->pcm->open_mutex);
440}
441
442static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
443					       struct snd_info_buffer *buffer)
444{
445	struct snd_pcm_substream *substream = entry->private_data;
446	struct snd_pcm_runtime *runtime;
447	struct snd_pcm_status64 status;
448	int err;
449
450	mutex_lock(&substream->pcm->open_mutex);
451	runtime = substream->runtime;
452	if (!runtime) {
453		snd_iprintf(buffer, "closed\n");
454		goto unlock;
455	}
456	memset(&status, 0, sizeof(status));
457	err = snd_pcm_status64(substream, &status);
458	if (err < 0) {
459		snd_iprintf(buffer, "error %d\n", err);
460		goto unlock;
461	}
462	snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
463	snd_iprintf(buffer, "owner_pid   : %d\n", pid_vnr(substream->pid));
464	snd_iprintf(buffer, "trigger_time: %lld.%09lld\n",
465		status.trigger_tstamp_sec, status.trigger_tstamp_nsec);
466	snd_iprintf(buffer, "tstamp      : %lld.%09lld\n",
467		status.tstamp_sec, status.tstamp_nsec);
468	snd_iprintf(buffer, "delay       : %ld\n", status.delay);
469	snd_iprintf(buffer, "avail       : %ld\n", status.avail);
470	snd_iprintf(buffer, "avail_max   : %ld\n", status.avail_max);
471	snd_iprintf(buffer, "-----\n");
472	snd_iprintf(buffer, "hw_ptr      : %ld\n", runtime->status->hw_ptr);
473	snd_iprintf(buffer, "appl_ptr    : %ld\n", runtime->control->appl_ptr);
474 unlock:
475	mutex_unlock(&substream->pcm->open_mutex);
476}
477
478#ifdef CONFIG_SND_PCM_XRUN_DEBUG
479static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
480					 struct snd_info_buffer *buffer)
481{
482	struct snd_pcm_substream *substream = entry->private_data;
483
484	snd_pcm_stop_xrun(substream);
485}
486
487static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
488				    struct snd_info_buffer *buffer)
489{
490	struct snd_pcm_str *pstr = entry->private_data;
491	snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
492}
493
494static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
495				     struct snd_info_buffer *buffer)
496{
497	struct snd_pcm_str *pstr = entry->private_data;
498	char line[64];
499	if (!snd_info_get_line(buffer, line, sizeof(line)))
500		pstr->xrun_debug = simple_strtoul(line, NULL, 10);
501}
502#endif
503
504static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
505{
506	struct snd_pcm *pcm = pstr->pcm;
507	struct snd_info_entry *entry;
508	char name[16];
509
510	sprintf(name, "pcm%i%c", pcm->device,
511		pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
512	entry = snd_info_create_card_entry(pcm->card, name,
513					   pcm->card->proc_root);
514	if (!entry)
515		return -ENOMEM;
516	entry->mode = S_IFDIR | 0555;
517	pstr->proc_root = entry;
518	entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root);
519	if (entry)
520		snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
521#ifdef CONFIG_SND_PCM_XRUN_DEBUG
522	entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
523					   pstr->proc_root);
524	if (entry) {
525		snd_info_set_text_ops(entry, pstr, snd_pcm_xrun_debug_read);
526		entry->c.text.write = snd_pcm_xrun_debug_write;
527		entry->mode |= 0200;
528	}
529#endif
530	return 0;
531}
532
533static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
534{
535	snd_info_free_entry(pstr->proc_root);
536	pstr->proc_root = NULL;
537	return 0;
538}
539
540static struct snd_info_entry *
541create_substream_info_entry(struct snd_pcm_substream *substream,
542			    const char *name,
543			    void (*read)(struct snd_info_entry *,
544					 struct snd_info_buffer *))
545{
546	struct snd_info_entry *entry;
547
548	entry = snd_info_create_card_entry(substream->pcm->card, name,
549					   substream->proc_root);
550	if (entry)
551		snd_info_set_text_ops(entry, substream, read);
552	return entry;
553}
554
555static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
556{
557	struct snd_info_entry *entry;
558	struct snd_card *card;
559	char name[16];
560
561	card = substream->pcm->card;
562
563	sprintf(name, "sub%i", substream->number);
564	entry = snd_info_create_card_entry(card, name,
565					   substream->pstr->proc_root);
566	if (!entry)
567		return -ENOMEM;
568	entry->mode = S_IFDIR | 0555;
569	substream->proc_root = entry;
570
571	create_substream_info_entry(substream, "info",
572				    snd_pcm_substream_proc_info_read);
573	create_substream_info_entry(substream, "hw_params",
574				    snd_pcm_substream_proc_hw_params_read);
575	create_substream_info_entry(substream, "sw_params",
576				    snd_pcm_substream_proc_sw_params_read);
577	create_substream_info_entry(substream, "status",
578				    snd_pcm_substream_proc_status_read);
579
580#ifdef CONFIG_SND_PCM_XRUN_DEBUG
581	entry = create_substream_info_entry(substream, "xrun_injection", NULL);
582	if (entry) {
583		entry->c.text.write = snd_pcm_xrun_injection_write;
584		entry->mode = S_IFREG | 0200;
585	}
586#endif /* CONFIG_SND_PCM_XRUN_DEBUG */
587
588	return 0;
589}
590
591#else /* !CONFIG_SND_VERBOSE_PROCFS */
592static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
593static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
594static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
595#endif /* CONFIG_SND_VERBOSE_PROCFS */
596
597static const struct attribute_group *pcm_dev_attr_groups[];
598
599/*
600 * PM callbacks: we need to deal only with suspend here, as the resume is
601 * triggered either from user-space or the driver's resume callback
602 */
603#ifdef CONFIG_PM_SLEEP
604static int do_pcm_suspend(struct device *dev)
605{
606	struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
607
608	if (!pstr->pcm->no_device_suspend)
609		snd_pcm_suspend_all(pstr->pcm);
610	return 0;
611}
612#endif
613
614static const struct dev_pm_ops pcm_dev_pm_ops = {
615	SET_SYSTEM_SLEEP_PM_OPS(do_pcm_suspend, NULL)
616};
617
618/* device type for PCM -- basically only for passing PM callbacks */
619static const struct device_type pcm_dev_type = {
620	.name = "pcm",
621	.pm = &pcm_dev_pm_ops,
622};
623
624/**
625 * snd_pcm_new_stream - create a new PCM stream
626 * @pcm: the pcm instance
627 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
628 * @substream_count: the number of substreams
629 *
630 * Creates a new stream for the pcm.
631 * The corresponding stream on the pcm must have been empty before
632 * calling this, i.e. zero must be given to the argument of
633 * snd_pcm_new().
634 *
635 * Return: Zero if successful, or a negative error code on failure.
636 */
637int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
638{
639	int idx, err;
640	struct snd_pcm_str *pstr = &pcm->streams[stream];
641	struct snd_pcm_substream *substream, *prev;
642
643#if IS_ENABLED(CONFIG_SND_PCM_OSS)
644	mutex_init(&pstr->oss.setup_mutex);
645#endif
646	pstr->stream = stream;
647	pstr->pcm = pcm;
648	pstr->substream_count = substream_count;
649	if (!substream_count)
650		return 0;
651
652	snd_device_initialize(&pstr->dev, pcm->card);
653	pstr->dev.groups = pcm_dev_attr_groups;
654	pstr->dev.type = &pcm_dev_type;
655	dev_set_name(&pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
656		     stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
657
658	if (!pcm->internal) {
659		err = snd_pcm_stream_proc_init(pstr);
660		if (err < 0) {
661			pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
662			return err;
663		}
664	}
665	prev = NULL;
666	for (idx = 0, prev = NULL; idx < substream_count; idx++) {
667		substream = kzalloc(sizeof(*substream), GFP_KERNEL);
668		if (!substream)
669			return -ENOMEM;
670		substream->pcm = pcm;
671		substream->pstr = pstr;
672		substream->number = idx;
673		substream->stream = stream;
674		sprintf(substream->name, "subdevice #%i", idx);
675		substream->buffer_bytes_max = UINT_MAX;
676		if (prev == NULL)
677			pstr->substream = substream;
678		else
679			prev->next = substream;
680
681		if (!pcm->internal) {
682			err = snd_pcm_substream_proc_init(substream);
683			if (err < 0) {
684				pcm_err(pcm,
685					"Error in snd_pcm_stream_proc_init\n");
686				if (prev == NULL)
687					pstr->substream = NULL;
688				else
689					prev->next = NULL;
690				kfree(substream);
691				return err;
692			}
693		}
694		substream->group = &substream->self_group;
695		snd_pcm_group_init(&substream->self_group);
696		list_add_tail(&substream->link_list, &substream->self_group.substreams);
697		atomic_set(&substream->mmap_count, 0);
698		prev = substream;
699	}
700	return 0;
701}
702EXPORT_SYMBOL(snd_pcm_new_stream);
703
704static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
705		int playback_count, int capture_count, bool internal,
706		struct snd_pcm **rpcm)
707{
708	struct snd_pcm *pcm;
709	int err;
710	static const struct snd_device_ops ops = {
711		.dev_free = snd_pcm_dev_free,
712		.dev_register =	snd_pcm_dev_register,
713		.dev_disconnect = snd_pcm_dev_disconnect,
714	};
715	static const struct snd_device_ops internal_ops = {
716		.dev_free = snd_pcm_dev_free,
717	};
718
719	if (snd_BUG_ON(!card))
720		return -ENXIO;
721	if (rpcm)
722		*rpcm = NULL;
723	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
724	if (!pcm)
725		return -ENOMEM;
726	pcm->card = card;
727	pcm->device = device;
728	pcm->internal = internal;
729	mutex_init(&pcm->open_mutex);
730	init_waitqueue_head(&pcm->open_wait);
731	INIT_LIST_HEAD(&pcm->list);
732	if (id)
733		strlcpy(pcm->id, id, sizeof(pcm->id));
734
735	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
736				 playback_count);
737	if (err < 0)
738		goto free_pcm;
739
740	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
741	if (err < 0)
742		goto free_pcm;
743
744	err = snd_device_new(card, SNDRV_DEV_PCM, pcm,
745			     internal ? &internal_ops : &ops);
746	if (err < 0)
747		goto free_pcm;
748
749	if (rpcm)
750		*rpcm = pcm;
751	return 0;
752
753free_pcm:
754	snd_pcm_free(pcm);
755	return err;
756}
757
758/**
759 * snd_pcm_new - create a new PCM instance
760 * @card: the card instance
761 * @id: the id string
762 * @device: the device index (zero based)
763 * @playback_count: the number of substreams for playback
764 * @capture_count: the number of substreams for capture
765 * @rpcm: the pointer to store the new pcm instance
766 *
767 * Creates a new PCM instance.
768 *
769 * The pcm operators have to be set afterwards to the new instance
770 * via snd_pcm_set_ops().
771 *
772 * Return: Zero if successful, or a negative error code on failure.
773 */
774int snd_pcm_new(struct snd_card *card, const char *id, int device,
775		int playback_count, int capture_count, struct snd_pcm **rpcm)
776{
777	return _snd_pcm_new(card, id, device, playback_count, capture_count,
778			false, rpcm);
779}
780EXPORT_SYMBOL(snd_pcm_new);
781
782/**
783 * snd_pcm_new_internal - create a new internal PCM instance
784 * @card: the card instance
785 * @id: the id string
786 * @device: the device index (zero based - shared with normal PCMs)
787 * @playback_count: the number of substreams for playback
788 * @capture_count: the number of substreams for capture
789 * @rpcm: the pointer to store the new pcm instance
790 *
791 * Creates a new internal PCM instance with no userspace device or procfs
792 * entries. This is used by ASoC Back End PCMs in order to create a PCM that
793 * will only be used internally by kernel drivers. i.e. it cannot be opened
794 * by userspace. It provides existing ASoC components drivers with a substream
795 * and access to any private data.
796 *
797 * The pcm operators have to be set afterwards to the new instance
798 * via snd_pcm_set_ops().
799 *
800 * Return: Zero if successful, or a negative error code on failure.
801 */
802int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
803	int playback_count, int capture_count,
804	struct snd_pcm **rpcm)
805{
806	return _snd_pcm_new(card, id, device, playback_count, capture_count,
807			true, rpcm);
808}
809EXPORT_SYMBOL(snd_pcm_new_internal);
810
811static void free_chmap(struct snd_pcm_str *pstr)
812{
813	if (pstr->chmap_kctl) {
814		struct snd_card *card = pstr->pcm->card;
815
816		down_write(&card->controls_rwsem);
817		snd_ctl_remove(card, pstr->chmap_kctl);
818		up_write(&card->controls_rwsem);
819		pstr->chmap_kctl = NULL;
820	}
821}
822
823static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
824{
825	struct snd_pcm_substream *substream, *substream_next;
826#if IS_ENABLED(CONFIG_SND_PCM_OSS)
827	struct snd_pcm_oss_setup *setup, *setupn;
828#endif
829
830	/* free all proc files under the stream */
831	snd_pcm_stream_proc_done(pstr);
832
833	substream = pstr->substream;
834	while (substream) {
835		substream_next = substream->next;
836		snd_pcm_timer_done(substream);
837		kfree(substream);
838		substream = substream_next;
839	}
840#if IS_ENABLED(CONFIG_SND_PCM_OSS)
841	for (setup = pstr->oss.setup_list; setup; setup = setupn) {
842		setupn = setup->next;
843		kfree(setup->task_name);
844		kfree(setup);
845	}
846#endif
847	free_chmap(pstr);
848	if (pstr->substream_count)
849		put_device(&pstr->dev);
850}
851
852#if IS_ENABLED(CONFIG_SND_PCM_OSS)
853#define pcm_call_notify(pcm, call)					\
854	do {								\
855		struct snd_pcm_notify *_notify;				\
856		list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
857			_notify->call(pcm);				\
858	} while (0)
859#else
860#define pcm_call_notify(pcm, call) do {} while (0)
861#endif
862
863static int snd_pcm_free(struct snd_pcm *pcm)
864{
865	if (!pcm)
866		return 0;
867	if (!pcm->internal)
868		pcm_call_notify(pcm, n_unregister);
869	if (pcm->private_free)
870		pcm->private_free(pcm);
871	snd_pcm_lib_preallocate_free_for_all(pcm);
872	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
873	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
874	kfree(pcm);
875	return 0;
876}
877
878static int snd_pcm_dev_free(struct snd_device *device)
879{
880	struct snd_pcm *pcm = device->device_data;
881	return snd_pcm_free(pcm);
882}
883
884int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
885			     struct file *file,
886			     struct snd_pcm_substream **rsubstream)
887{
888	struct snd_pcm_str * pstr;
889	struct snd_pcm_substream *substream;
890	struct snd_pcm_runtime *runtime;
891	struct snd_card *card;
892	int prefer_subdevice;
893	size_t size;
894
895	if (snd_BUG_ON(!pcm || !rsubstream))
896		return -ENXIO;
897	if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
898		       stream != SNDRV_PCM_STREAM_CAPTURE))
899		return -EINVAL;
900	*rsubstream = NULL;
901	pstr = &pcm->streams[stream];
902	if (pstr->substream == NULL || pstr->substream_count == 0)
903		return -ENODEV;
904
905	card = pcm->card;
906	prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
907
908	if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
909		int opposite = !stream;
910
911		for (substream = pcm->streams[opposite].substream; substream;
912		     substream = substream->next) {
913			if (SUBSTREAM_BUSY(substream))
914				return -EAGAIN;
915		}
916	}
917
918	if (file->f_flags & O_APPEND) {
919		if (prefer_subdevice < 0) {
920			if (pstr->substream_count > 1)
921				return -EINVAL; /* must be unique */
922			substream = pstr->substream;
923		} else {
924			for (substream = pstr->substream; substream;
925			     substream = substream->next)
926				if (substream->number == prefer_subdevice)
927					break;
928		}
929		if (! substream)
930			return -ENODEV;
931		if (! SUBSTREAM_BUSY(substream))
932			return -EBADFD;
933		substream->ref_count++;
934		*rsubstream = substream;
935		return 0;
936	}
937
938	for (substream = pstr->substream; substream; substream = substream->next) {
939		if (!SUBSTREAM_BUSY(substream) &&
940		    (prefer_subdevice == -1 ||
941		     substream->number == prefer_subdevice))
942			break;
943	}
944	if (substream == NULL)
945		return -EAGAIN;
946
947	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
948	if (runtime == NULL)
949		return -ENOMEM;
950
951	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
952	runtime->status = alloc_pages_exact(size, GFP_KERNEL);
953	if (runtime->status == NULL) {
954		kfree(runtime);
955		return -ENOMEM;
956	}
957	memset(runtime->status, 0, size);
958
959	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
960	runtime->control = alloc_pages_exact(size, GFP_KERNEL);
961	if (runtime->control == NULL) {
962		free_pages_exact(runtime->status,
963			       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
964		kfree(runtime);
965		return -ENOMEM;
966	}
967	memset(runtime->control, 0, size);
968
969	init_waitqueue_head(&runtime->sleep);
970	init_waitqueue_head(&runtime->tsleep);
971
972	runtime->status->state = SNDRV_PCM_STATE_OPEN;
973	mutex_init(&runtime->buffer_mutex);
974	atomic_set(&runtime->buffer_accessing, 0);
975
976	substream->runtime = runtime;
977	substream->private_data = pcm->private_data;
978	substream->ref_count = 1;
979	substream->f_flags = file->f_flags;
980	substream->pid = get_pid(task_pid(current));
981	pstr->substream_opened++;
982	*rsubstream = substream;
983	return 0;
984}
985
986void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
987{
988	struct snd_pcm_runtime *runtime;
989
990	if (PCM_RUNTIME_CHECK(substream))
991		return;
992	runtime = substream->runtime;
993	if (runtime->private_free != NULL)
994		runtime->private_free(runtime);
995	free_pages_exact(runtime->status,
996		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
997	free_pages_exact(runtime->control,
998		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
999	kfree(runtime->hw_constraints.rules);
1000	/* Avoid concurrent access to runtime via PCM timer interface */
1001	if (substream->timer) {
1002		spin_lock_irq(&substream->timer->lock);
1003		substream->runtime = NULL;
1004		spin_unlock_irq(&substream->timer->lock);
1005	} else {
1006		substream->runtime = NULL;
1007	}
1008	mutex_destroy(&runtime->buffer_mutex);
1009	kfree(runtime);
1010	put_pid(substream->pid);
1011	substream->pid = NULL;
1012	substream->pstr->substream_opened--;
1013}
1014
1015static ssize_t show_pcm_class(struct device *dev,
1016			      struct device_attribute *attr, char *buf)
1017{
1018	struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
1019	struct snd_pcm *pcm = pstr->pcm;
1020	const char *str;
1021	static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1022		[SNDRV_PCM_CLASS_GENERIC] = "generic",
1023		[SNDRV_PCM_CLASS_MULTI] = "multi",
1024		[SNDRV_PCM_CLASS_MODEM] = "modem",
1025		[SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1026	};
1027
1028	if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
1029		str = "none";
1030	else
1031		str = strs[pcm->dev_class];
1032	return sprintf(buf, "%s\n", str);
1033}
1034
1035static DEVICE_ATTR(pcm_class, 0444, show_pcm_class, NULL);
1036static struct attribute *pcm_dev_attrs[] = {
1037	&dev_attr_pcm_class.attr,
1038	NULL
1039};
1040
1041static const struct attribute_group pcm_dev_attr_group = {
1042	.attrs	= pcm_dev_attrs,
1043};
1044
1045static const struct attribute_group *pcm_dev_attr_groups[] = {
1046	&pcm_dev_attr_group,
1047	NULL
1048};
1049
1050static int snd_pcm_dev_register(struct snd_device *device)
1051{
1052	int cidx, err;
1053	struct snd_pcm_substream *substream;
1054	struct snd_pcm *pcm;
1055
1056	if (snd_BUG_ON(!device || !device->device_data))
1057		return -ENXIO;
1058	pcm = device->device_data;
1059
1060	mutex_lock(&register_mutex);
1061	err = snd_pcm_add(pcm);
1062	if (err)
1063		goto unlock;
1064	for (cidx = 0; cidx < 2; cidx++) {
1065		int devtype = -1;
1066		if (pcm->streams[cidx].substream == NULL)
1067			continue;
1068		switch (cidx) {
1069		case SNDRV_PCM_STREAM_PLAYBACK:
1070			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1071			break;
1072		case SNDRV_PCM_STREAM_CAPTURE:
1073			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1074			break;
1075		}
1076		/* register pcm */
1077		err = snd_register_device(devtype, pcm->card, pcm->device,
1078					  &snd_pcm_f_ops[cidx], pcm,
1079					  &pcm->streams[cidx].dev);
1080		if (err < 0) {
1081			list_del_init(&pcm->list);
1082			goto unlock;
1083		}
1084
1085		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1086			snd_pcm_timer_init(substream);
1087	}
1088
1089	pcm_call_notify(pcm, n_register);
1090
1091 unlock:
1092	mutex_unlock(&register_mutex);
1093	return err;
1094}
1095
1096static int snd_pcm_dev_disconnect(struct snd_device *device)
1097{
1098	struct snd_pcm *pcm = device->device_data;
1099	struct snd_pcm_substream *substream;
1100	int cidx;
1101
1102	mutex_lock(&register_mutex);
1103	mutex_lock(&pcm->open_mutex);
1104	wake_up(&pcm->open_wait);
1105	list_del_init(&pcm->list);
1106	for (cidx = 0; cidx < 2; cidx++) {
1107		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1108			snd_pcm_stream_lock_irq(substream);
1109			if (substream->runtime) {
1110				if (snd_pcm_running(substream))
1111					snd_pcm_stop(substream,
1112						     SNDRV_PCM_STATE_DISCONNECTED);
1113				/* to be sure, set the state unconditionally */
1114				substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
1115				wake_up(&substream->runtime->sleep);
1116				wake_up(&substream->runtime->tsleep);
1117			}
1118			snd_pcm_stream_unlock_irq(substream);
1119		}
1120	}
1121
1122	for (cidx = 0; cidx < 2; cidx++)
1123		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1124			snd_pcm_sync_stop(substream, false);
1125
1126	pcm_call_notify(pcm, n_disconnect);
1127	for (cidx = 0; cidx < 2; cidx++) {
1128		snd_unregister_device(&pcm->streams[cidx].dev);
1129		free_chmap(&pcm->streams[cidx]);
1130	}
1131	mutex_unlock(&pcm->open_mutex);
1132	mutex_unlock(&register_mutex);
1133	return 0;
1134}
1135
1136#if IS_ENABLED(CONFIG_SND_PCM_OSS)
1137/**
1138 * snd_pcm_notify - Add/remove the notify list
1139 * @notify: PCM notify list
1140 * @nfree: 0 = register, 1 = unregister
1141 *
1142 * This adds the given notifier to the global list so that the callback is
1143 * called for each registered PCM devices.  This exists only for PCM OSS
1144 * emulation, so far.
1145 */
1146int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1147{
1148	struct snd_pcm *pcm;
1149
1150	if (snd_BUG_ON(!notify ||
1151		       !notify->n_register ||
1152		       !notify->n_unregister ||
1153		       !notify->n_disconnect))
1154		return -EINVAL;
1155	mutex_lock(&register_mutex);
1156	if (nfree) {
1157		list_del(&notify->list);
1158		list_for_each_entry(pcm, &snd_pcm_devices, list)
1159			notify->n_unregister(pcm);
1160	} else {
1161		list_add_tail(&notify->list, &snd_pcm_notify_list);
1162		list_for_each_entry(pcm, &snd_pcm_devices, list)
1163			notify->n_register(pcm);
1164	}
1165	mutex_unlock(&register_mutex);
1166	return 0;
1167}
1168EXPORT_SYMBOL(snd_pcm_notify);
1169#endif /* CONFIG_SND_PCM_OSS */
1170
1171#ifdef CONFIG_SND_PROC_FS
1172/*
1173 *  Info interface
1174 */
1175
1176static void snd_pcm_proc_read(struct snd_info_entry *entry,
1177			      struct snd_info_buffer *buffer)
1178{
1179	struct snd_pcm *pcm;
1180
1181	mutex_lock(&register_mutex);
1182	list_for_each_entry(pcm, &snd_pcm_devices, list) {
1183		snd_iprintf(buffer, "%02i-%02i: %s : %s",
1184			    pcm->card->number, pcm->device, pcm->id, pcm->name);
1185		if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1186			snd_iprintf(buffer, " : playback %i",
1187				    pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1188		if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1189			snd_iprintf(buffer, " : capture %i",
1190				    pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1191		snd_iprintf(buffer, "\n");
1192	}
1193	mutex_unlock(&register_mutex);
1194}
1195
1196static struct snd_info_entry *snd_pcm_proc_entry;
1197
1198static void snd_pcm_proc_init(void)
1199{
1200	struct snd_info_entry *entry;
1201
1202	entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL);
1203	if (entry) {
1204		snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1205		if (snd_info_register(entry) < 0) {
1206			snd_info_free_entry(entry);
1207			entry = NULL;
1208		}
1209	}
1210	snd_pcm_proc_entry = entry;
1211}
1212
1213static void snd_pcm_proc_done(void)
1214{
1215	snd_info_free_entry(snd_pcm_proc_entry);
1216}
1217
1218#else /* !CONFIG_SND_PROC_FS */
1219#define snd_pcm_proc_init()
1220#define snd_pcm_proc_done()
1221#endif /* CONFIG_SND_PROC_FS */
1222
1223
1224/*
1225 *  ENTRY functions
1226 */
1227
1228static int __init alsa_pcm_init(void)
1229{
1230	snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1231	snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1232	snd_pcm_proc_init();
1233	return 0;
1234}
1235
1236static void __exit alsa_pcm_exit(void)
1237{
1238	snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1239	snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1240	snd_pcm_proc_done();
1241}
1242
1243module_init(alsa_pcm_init)
1244module_exit(alsa_pcm_exit)
1245