2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/device.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/pcm.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
34 #include "pcm_local.h"
36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
37 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
38 MODULE_LICENSE("GPL");
40 static LIST_HEAD(snd_pcm_devices
);
41 static DEFINE_MUTEX(register_mutex
);
42 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
43 static LIST_HEAD(snd_pcm_notify_list
);
46 static int snd_pcm_free(struct snd_pcm
*pcm
);
47 static int snd_pcm_dev_free(struct snd_device
*device
);
48 static int snd_pcm_dev_register(struct snd_device
*device
);
49 static int snd_pcm_dev_disconnect(struct snd_device
*device
);
51 static struct snd_pcm
*snd_pcm_get(struct snd_card
*card
, int device
)
55 list_for_each_entry(pcm
, &snd_pcm_devices
, list
) {
56 if (pcm
->card
== card
&& pcm
->device
== device
)
62 static int snd_pcm_next(struct snd_card
*card
, int device
)
66 list_for_each_entry(pcm
, &snd_pcm_devices
, list
) {
67 if (pcm
->card
== card
&& pcm
->device
> device
)
69 else if (pcm
->card
->number
> card
->number
)
75 static int snd_pcm_add(struct snd_pcm
*newpcm
)
82 list_for_each_entry(pcm
, &snd_pcm_devices
, list
) {
83 if (pcm
->card
== newpcm
->card
&& pcm
->device
== newpcm
->device
)
85 if (pcm
->card
->number
> newpcm
->card
->number
||
86 (pcm
->card
== newpcm
->card
&&
87 pcm
->device
> newpcm
->device
)) {
88 list_add(&newpcm
->list
, pcm
->list
.prev
);
92 list_add_tail(&newpcm
->list
, &snd_pcm_devices
);
96 static int snd_pcm_control_ioctl(struct snd_card
*card
,
97 struct snd_ctl_file
*control
,
98 unsigned int cmd
, unsigned long arg
)
101 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE
:
105 if (get_user(device
, (int __user
*)arg
))
107 mutex_lock(®ister_mutex
);
108 device
= snd_pcm_next(card
, device
);
109 mutex_unlock(®ister_mutex
);
110 if (put_user(device
, (int __user
*)arg
))
114 case SNDRV_CTL_IOCTL_PCM_INFO
:
116 struct snd_pcm_info __user
*info
;
117 unsigned int device
, subdevice
;
120 struct snd_pcm_str
*pstr
;
121 struct snd_pcm_substream
*substream
;
124 info
= (struct snd_pcm_info __user
*)arg
;
125 if (get_user(device
, &info
->device
))
127 if (get_user(stream
, &info
->stream
))
129 if (stream
< 0 || stream
> 1)
131 if (get_user(subdevice
, &info
->subdevice
))
133 mutex_lock(®ister_mutex
);
134 pcm
= snd_pcm_get(card
, device
);
139 pstr
= &pcm
->streams
[stream
];
140 if (pstr
->substream_count
== 0) {
144 if (subdevice
>= pstr
->substream_count
) {
148 for (substream
= pstr
->substream
; substream
;
149 substream
= substream
->next
)
150 if (substream
->number
== (int)subdevice
)
152 if (substream
== NULL
) {
156 mutex_lock(&pcm
->open_mutex
);
157 err
= snd_pcm_info_user(substream
, info
);
158 mutex_unlock(&pcm
->open_mutex
);
160 mutex_unlock(®ister_mutex
);
163 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE
:
167 if (get_user(val
, (int __user
*)arg
))
169 control
->preferred_subdevice
[SND_CTL_SUBDEV_PCM
] = val
;
176 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
178 static char *snd_pcm_format_names
[] = {
197 FORMAT(IEC958_SUBFRAME_LE
),
198 FORMAT(IEC958_SUBFRAME_BE
),
229 * snd_pcm_format_name - Return a name string for the given PCM format
230 * @format: PCM format
232 const char *snd_pcm_format_name(snd_pcm_format_t format
)
234 if ((__force
unsigned int)format
>= ARRAY_SIZE(snd_pcm_format_names
))
236 return snd_pcm_format_names
[(__force
unsigned int)format
];
238 EXPORT_SYMBOL_GPL(snd_pcm_format_name
);
240 #ifdef CONFIG_SND_VERBOSE_PROCFS
242 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
243 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
244 #define READY(v) [SNDRV_PCM_READY_##v] = #v
245 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
246 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
247 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
248 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
249 #define START(v) [SNDRV_PCM_START_##v] = #v
250 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
252 static char *snd_pcm_stream_names
[] = {
257 static char *snd_pcm_state_names
[] = {
268 static char *snd_pcm_access_names
[] = {
269 ACCESS(MMAP_INTERLEAVED
),
270 ACCESS(MMAP_NONINTERLEAVED
),
271 ACCESS(MMAP_COMPLEX
),
272 ACCESS(RW_INTERLEAVED
),
273 ACCESS(RW_NONINTERLEAVED
),
276 static char *snd_pcm_subformat_names
[] = {
280 static char *snd_pcm_tstamp_mode_names
[] = {
285 static const char *snd_pcm_stream_name(int stream
)
287 return snd_pcm_stream_names
[stream
];
290 static const char *snd_pcm_access_name(snd_pcm_access_t access
)
292 return snd_pcm_access_names
[(__force
int)access
];
295 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat
)
297 return snd_pcm_subformat_names
[(__force
int)subformat
];
300 static const char *snd_pcm_tstamp_mode_name(int mode
)
302 return snd_pcm_tstamp_mode_names
[mode
];
305 static const char *snd_pcm_state_name(snd_pcm_state_t state
)
307 return snd_pcm_state_names
[(__force
int)state
];
310 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
311 #include <linux/soundcard.h>
313 static const char *snd_pcm_oss_format_name(int format
)
342 static void snd_pcm_proc_info_read(struct snd_pcm_substream
*substream
,
343 struct snd_info_buffer
*buffer
)
345 struct snd_pcm_info
*info
;
351 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
355 err
= snd_pcm_info(substream
, info
);
357 snd_iprintf(buffer
, "error %d\n", err
);
361 snd_iprintf(buffer
, "card: %d\n", info
->card
);
362 snd_iprintf(buffer
, "device: %d\n", info
->device
);
363 snd_iprintf(buffer
, "subdevice: %d\n", info
->subdevice
);
364 snd_iprintf(buffer
, "stream: %s\n", snd_pcm_stream_name(info
->stream
));
365 snd_iprintf(buffer
, "id: %s\n", info
->id
);
366 snd_iprintf(buffer
, "name: %s\n", info
->name
);
367 snd_iprintf(buffer
, "subname: %s\n", info
->subname
);
368 snd_iprintf(buffer
, "class: %d\n", info
->dev_class
);
369 snd_iprintf(buffer
, "subclass: %d\n", info
->dev_subclass
);
370 snd_iprintf(buffer
, "subdevices_count: %d\n", info
->subdevices_count
);
371 snd_iprintf(buffer
, "subdevices_avail: %d\n", info
->subdevices_avail
);
375 static void snd_pcm_stream_proc_info_read(struct snd_info_entry
*entry
,
376 struct snd_info_buffer
*buffer
)
378 snd_pcm_proc_info_read(((struct snd_pcm_str
*)entry
->private_data
)->substream
,
382 static void snd_pcm_substream_proc_info_read(struct snd_info_entry
*entry
,
383 struct snd_info_buffer
*buffer
)
385 snd_pcm_proc_info_read(entry
->private_data
, buffer
);
388 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry
*entry
,
389 struct snd_info_buffer
*buffer
)
391 struct snd_pcm_substream
*substream
= entry
->private_data
;
392 struct snd_pcm_runtime
*runtime
;
394 mutex_lock(&substream
->pcm
->open_mutex
);
395 runtime
= substream
->runtime
;
397 snd_iprintf(buffer
, "closed\n");
400 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
401 snd_iprintf(buffer
, "no setup\n");
404 snd_iprintf(buffer
, "access: %s\n", snd_pcm_access_name(runtime
->access
));
405 snd_iprintf(buffer
, "format: %s\n", snd_pcm_format_name(runtime
->format
));
406 snd_iprintf(buffer
, "subformat: %s\n", snd_pcm_subformat_name(runtime
->subformat
));
407 snd_iprintf(buffer
, "channels: %u\n", runtime
->channels
);
408 snd_iprintf(buffer
, "rate: %u (%u/%u)\n", runtime
->rate
, runtime
->rate_num
, runtime
->rate_den
);
409 snd_iprintf(buffer
, "period_size: %lu\n", runtime
->period_size
);
410 snd_iprintf(buffer
, "buffer_size: %lu\n", runtime
->buffer_size
);
411 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
412 if (substream
->oss
.oss
) {
413 snd_iprintf(buffer
, "OSS format: %s\n", snd_pcm_oss_format_name(runtime
->oss
.format
));
414 snd_iprintf(buffer
, "OSS channels: %u\n", runtime
->oss
.channels
);
415 snd_iprintf(buffer
, "OSS rate: %u\n", runtime
->oss
.rate
);
416 snd_iprintf(buffer
, "OSS period bytes: %lu\n", (unsigned long)runtime
->oss
.period_bytes
);
417 snd_iprintf(buffer
, "OSS periods: %u\n", runtime
->oss
.periods
);
418 snd_iprintf(buffer
, "OSS period frames: %lu\n", (unsigned long)runtime
->oss
.period_frames
);
422 mutex_unlock(&substream
->pcm
->open_mutex
);
425 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry
*entry
,
426 struct snd_info_buffer
*buffer
)
428 struct snd_pcm_substream
*substream
= entry
->private_data
;
429 struct snd_pcm_runtime
*runtime
;
431 mutex_lock(&substream
->pcm
->open_mutex
);
432 runtime
= substream
->runtime
;
434 snd_iprintf(buffer
, "closed\n");
437 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
438 snd_iprintf(buffer
, "no setup\n");
441 snd_iprintf(buffer
, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime
->tstamp_mode
));
442 snd_iprintf(buffer
, "period_step: %u\n", runtime
->period_step
);
443 snd_iprintf(buffer
, "avail_min: %lu\n", runtime
->control
->avail_min
);
444 snd_iprintf(buffer
, "start_threshold: %lu\n", runtime
->start_threshold
);
445 snd_iprintf(buffer
, "stop_threshold: %lu\n", runtime
->stop_threshold
);
446 snd_iprintf(buffer
, "silence_threshold: %lu\n", runtime
->silence_threshold
);
447 snd_iprintf(buffer
, "silence_size: %lu\n", runtime
->silence_size
);
448 snd_iprintf(buffer
, "boundary: %lu\n", runtime
->boundary
);
450 mutex_unlock(&substream
->pcm
->open_mutex
);
453 static void snd_pcm_substream_proc_status_read(struct snd_info_entry
*entry
,
454 struct snd_info_buffer
*buffer
)
456 struct snd_pcm_substream
*substream
= entry
->private_data
;
457 struct snd_pcm_runtime
*runtime
;
458 struct snd_pcm_status status
;
461 mutex_lock(&substream
->pcm
->open_mutex
);
462 runtime
= substream
->runtime
;
464 snd_iprintf(buffer
, "closed\n");
467 memset(&status
, 0, sizeof(status
));
468 err
= snd_pcm_status(substream
, &status
);
470 snd_iprintf(buffer
, "error %d\n", err
);
473 snd_iprintf(buffer
, "state: %s\n", snd_pcm_state_name(status
.state
));
474 snd_iprintf(buffer
, "owner_pid : %d\n", pid_vnr(substream
->pid
));
475 snd_iprintf(buffer
, "trigger_time: %ld.%09ld\n",
476 status
.trigger_tstamp
.tv_sec
, status
.trigger_tstamp
.tv_nsec
);
477 snd_iprintf(buffer
, "tstamp : %ld.%09ld\n",
478 status
.tstamp
.tv_sec
, status
.tstamp
.tv_nsec
);
479 snd_iprintf(buffer
, "delay : %ld\n", status
.delay
);
480 snd_iprintf(buffer
, "avail : %ld\n", status
.avail
);
481 snd_iprintf(buffer
, "avail_max : %ld\n", status
.avail_max
);
482 snd_iprintf(buffer
, "-----\n");
483 snd_iprintf(buffer
, "hw_ptr : %ld\n", runtime
->status
->hw_ptr
);
484 snd_iprintf(buffer
, "appl_ptr : %ld\n", runtime
->control
->appl_ptr
);
486 mutex_unlock(&substream
->pcm
->open_mutex
);
489 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
490 static void snd_pcm_xrun_injection_write(struct snd_info_entry
*entry
,
491 struct snd_info_buffer
*buffer
)
493 struct snd_pcm_substream
*substream
= entry
->private_data
;
494 struct snd_pcm_runtime
*runtime
;
496 snd_pcm_stream_lock_irq(substream
);
497 runtime
= substream
->runtime
;
498 if (runtime
&& runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
499 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
500 snd_pcm_stream_unlock_irq(substream
);
503 static void snd_pcm_xrun_debug_read(struct snd_info_entry
*entry
,
504 struct snd_info_buffer
*buffer
)
506 struct snd_pcm_str
*pstr
= entry
->private_data
;
507 snd_iprintf(buffer
, "%d\n", pstr
->xrun_debug
);
510 static void snd_pcm_xrun_debug_write(struct snd_info_entry
*entry
,
511 struct snd_info_buffer
*buffer
)
513 struct snd_pcm_str
*pstr
= entry
->private_data
;
515 if (!snd_info_get_line(buffer
, line
, sizeof(line
)))
516 pstr
->xrun_debug
= simple_strtoul(line
, NULL
, 10);
520 static int snd_pcm_stream_proc_init(struct snd_pcm_str
*pstr
)
522 struct snd_pcm
*pcm
= pstr
->pcm
;
523 struct snd_info_entry
*entry
;
526 sprintf(name
, "pcm%i%c", pcm
->device
,
527 pstr
->stream
== SNDRV_PCM_STREAM_PLAYBACK
? 'p' : 'c');
528 if ((entry
= snd_info_create_card_entry(pcm
->card
, name
, pcm
->card
->proc_root
)) == NULL
)
530 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
531 if (snd_info_register(entry
) < 0) {
532 snd_info_free_entry(entry
);
535 pstr
->proc_root
= entry
;
537 if ((entry
= snd_info_create_card_entry(pcm
->card
, "info", pstr
->proc_root
)) != NULL
) {
538 snd_info_set_text_ops(entry
, pstr
, snd_pcm_stream_proc_info_read
);
539 if (snd_info_register(entry
) < 0) {
540 snd_info_free_entry(entry
);
544 pstr
->proc_info_entry
= entry
;
546 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
547 if ((entry
= snd_info_create_card_entry(pcm
->card
, "xrun_debug",
548 pstr
->proc_root
)) != NULL
) {
549 entry
->c
.text
.read
= snd_pcm_xrun_debug_read
;
550 entry
->c
.text
.write
= snd_pcm_xrun_debug_write
;
551 entry
->mode
|= S_IWUSR
;
552 entry
->private_data
= pstr
;
553 if (snd_info_register(entry
) < 0) {
554 snd_info_free_entry(entry
);
558 pstr
->proc_xrun_debug_entry
= entry
;
563 static int snd_pcm_stream_proc_done(struct snd_pcm_str
*pstr
)
565 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
566 snd_info_free_entry(pstr
->proc_xrun_debug_entry
);
567 pstr
->proc_xrun_debug_entry
= NULL
;
569 snd_info_free_entry(pstr
->proc_info_entry
);
570 pstr
->proc_info_entry
= NULL
;
571 snd_info_free_entry(pstr
->proc_root
);
572 pstr
->proc_root
= NULL
;
576 static int snd_pcm_substream_proc_init(struct snd_pcm_substream
*substream
)
578 struct snd_info_entry
*entry
;
579 struct snd_card
*card
;
582 card
= substream
->pcm
->card
;
584 sprintf(name
, "sub%i", substream
->number
);
585 if ((entry
= snd_info_create_card_entry(card
, name
, substream
->pstr
->proc_root
)) == NULL
)
587 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
588 if (snd_info_register(entry
) < 0) {
589 snd_info_free_entry(entry
);
592 substream
->proc_root
= entry
;
594 if ((entry
= snd_info_create_card_entry(card
, "info", substream
->proc_root
)) != NULL
) {
595 snd_info_set_text_ops(entry
, substream
,
596 snd_pcm_substream_proc_info_read
);
597 if (snd_info_register(entry
) < 0) {
598 snd_info_free_entry(entry
);
602 substream
->proc_info_entry
= entry
;
604 if ((entry
= snd_info_create_card_entry(card
, "hw_params", substream
->proc_root
)) != NULL
) {
605 snd_info_set_text_ops(entry
, substream
,
606 snd_pcm_substream_proc_hw_params_read
);
607 if (snd_info_register(entry
) < 0) {
608 snd_info_free_entry(entry
);
612 substream
->proc_hw_params_entry
= entry
;
614 if ((entry
= snd_info_create_card_entry(card
, "sw_params", substream
->proc_root
)) != NULL
) {
615 snd_info_set_text_ops(entry
, substream
,
616 snd_pcm_substream_proc_sw_params_read
);
617 if (snd_info_register(entry
) < 0) {
618 snd_info_free_entry(entry
);
622 substream
->proc_sw_params_entry
= entry
;
624 if ((entry
= snd_info_create_card_entry(card
, "status", substream
->proc_root
)) != NULL
) {
625 snd_info_set_text_ops(entry
, substream
,
626 snd_pcm_substream_proc_status_read
);
627 if (snd_info_register(entry
) < 0) {
628 snd_info_free_entry(entry
);
632 substream
->proc_status_entry
= entry
;
634 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
635 entry
= snd_info_create_card_entry(card
, "xrun_injection",
636 substream
->proc_root
);
638 entry
->private_data
= substream
;
639 entry
->c
.text
.read
= NULL
;
640 entry
->c
.text
.write
= snd_pcm_xrun_injection_write
;
641 entry
->mode
= S_IFREG
| S_IWUSR
;
642 if (snd_info_register(entry
) < 0) {
643 snd_info_free_entry(entry
);
647 substream
->proc_xrun_injection_entry
= entry
;
648 #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
653 static int snd_pcm_substream_proc_done(struct snd_pcm_substream
*substream
)
655 snd_info_free_entry(substream
->proc_info_entry
);
656 substream
->proc_info_entry
= NULL
;
657 snd_info_free_entry(substream
->proc_hw_params_entry
);
658 substream
->proc_hw_params_entry
= NULL
;
659 snd_info_free_entry(substream
->proc_sw_params_entry
);
660 substream
->proc_sw_params_entry
= NULL
;
661 snd_info_free_entry(substream
->proc_status_entry
);
662 substream
->proc_status_entry
= NULL
;
663 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
664 snd_info_free_entry(substream
->proc_xrun_injection_entry
);
665 substream
->proc_xrun_injection_entry
= NULL
;
667 snd_info_free_entry(substream
->proc_root
);
668 substream
->proc_root
= NULL
;
671 #else /* !CONFIG_SND_VERBOSE_PROCFS */
672 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str
*pstr
) { return 0; }
673 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str
*pstr
) { return 0; }
674 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream
*substream
) { return 0; }
675 static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream
*substream
) { return 0; }
676 #endif /* CONFIG_SND_VERBOSE_PROCFS */
678 static const struct attribute_group
*pcm_dev_attr_groups
[];
681 * snd_pcm_new_stream - create a new PCM stream
682 * @pcm: the pcm instance
683 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
684 * @substream_count: the number of substreams
686 * Creates a new stream for the pcm.
687 * The corresponding stream on the pcm must have been empty before
688 * calling this, i.e. zero must be given to the argument of
691 * Return: Zero if successful, or a negative error code on failure.
693 int snd_pcm_new_stream(struct snd_pcm
*pcm
, int stream
, int substream_count
)
696 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
697 struct snd_pcm_substream
*substream
, *prev
;
699 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
700 mutex_init(&pstr
->oss
.setup_mutex
);
702 pstr
->stream
= stream
;
704 pstr
->substream_count
= substream_count
;
705 if (!substream_count
)
708 snd_device_initialize(&pstr
->dev
, pcm
->card
);
709 pstr
->dev
.groups
= pcm_dev_attr_groups
;
710 dev_set_name(&pstr
->dev
, "pcmC%iD%i%c", pcm
->card
->number
, pcm
->device
,
711 stream
== SNDRV_PCM_STREAM_PLAYBACK
? 'p' : 'c');
713 if (!pcm
->internal
) {
714 err
= snd_pcm_stream_proc_init(pstr
);
716 pcm_err(pcm
, "Error in snd_pcm_stream_proc_init\n");
721 for (idx
= 0, prev
= NULL
; idx
< substream_count
; idx
++) {
722 substream
= kzalloc(sizeof(*substream
), GFP_KERNEL
);
725 substream
->pcm
= pcm
;
726 substream
->pstr
= pstr
;
727 substream
->number
= idx
;
728 substream
->stream
= stream
;
729 sprintf(substream
->name
, "subdevice #%i", idx
);
730 substream
->buffer_bytes_max
= UINT_MAX
;
732 pstr
->substream
= substream
;
734 prev
->next
= substream
;
736 if (!pcm
->internal
) {
737 err
= snd_pcm_substream_proc_init(substream
);
740 "Error in snd_pcm_stream_proc_init\n");
742 pstr
->substream
= NULL
;
749 substream
->group
= &substream
->self_group
;
750 spin_lock_init(&substream
->self_group
.lock
);
751 mutex_init(&substream
->self_group
.mutex
);
752 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
753 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
754 atomic_set(&substream
->mmap_count
, 0);
759 EXPORT_SYMBOL(snd_pcm_new_stream
);
761 static int _snd_pcm_new(struct snd_card
*card
, const char *id
, int device
,
762 int playback_count
, int capture_count
, bool internal
,
763 struct snd_pcm
**rpcm
)
767 static struct snd_device_ops ops
= {
768 .dev_free
= snd_pcm_dev_free
,
769 .dev_register
= snd_pcm_dev_register
,
770 .dev_disconnect
= snd_pcm_dev_disconnect
,
773 if (snd_BUG_ON(!card
))
777 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
781 pcm
->device
= device
;
782 pcm
->internal
= internal
;
783 mutex_init(&pcm
->open_mutex
);
784 init_waitqueue_head(&pcm
->open_wait
);
785 INIT_LIST_HEAD(&pcm
->list
);
787 strlcpy(pcm
->id
, id
, sizeof(pcm
->id
));
788 if ((err
= snd_pcm_new_stream(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, playback_count
)) < 0) {
792 if ((err
= snd_pcm_new_stream(pcm
, SNDRV_PCM_STREAM_CAPTURE
, capture_count
)) < 0) {
796 if ((err
= snd_device_new(card
, SNDRV_DEV_PCM
, pcm
, &ops
)) < 0) {
806 * snd_pcm_new - create a new PCM instance
807 * @card: the card instance
809 * @device: the device index (zero based)
810 * @playback_count: the number of substreams for playback
811 * @capture_count: the number of substreams for capture
812 * @rpcm: the pointer to store the new pcm instance
814 * Creates a new PCM instance.
816 * The pcm operators have to be set afterwards to the new instance
817 * via snd_pcm_set_ops().
819 * Return: Zero if successful, or a negative error code on failure.
821 int snd_pcm_new(struct snd_card
*card
, const char *id
, int device
,
822 int playback_count
, int capture_count
, struct snd_pcm
**rpcm
)
824 return _snd_pcm_new(card
, id
, device
, playback_count
, capture_count
,
827 EXPORT_SYMBOL(snd_pcm_new
);
830 * snd_pcm_new_internal - create a new internal PCM instance
831 * @card: the card instance
833 * @device: the device index (zero based - shared with normal PCMs)
834 * @playback_count: the number of substreams for playback
835 * @capture_count: the number of substreams for capture
836 * @rpcm: the pointer to store the new pcm instance
838 * Creates a new internal PCM instance with no userspace device or procfs
839 * entries. This is used by ASoC Back End PCMs in order to create a PCM that
840 * will only be used internally by kernel drivers. i.e. it cannot be opened
841 * by userspace. It provides existing ASoC components drivers with a substream
842 * and access to any private data.
844 * The pcm operators have to be set afterwards to the new instance
845 * via snd_pcm_set_ops().
847 * Return: Zero if successful, or a negative error code on failure.
849 int snd_pcm_new_internal(struct snd_card
*card
, const char *id
, int device
,
850 int playback_count
, int capture_count
,
851 struct snd_pcm
**rpcm
)
853 return _snd_pcm_new(card
, id
, device
, playback_count
, capture_count
,
856 EXPORT_SYMBOL(snd_pcm_new_internal
);
858 static void free_chmap(struct snd_pcm_str
*pstr
)
860 if (pstr
->chmap_kctl
) {
861 snd_ctl_remove(pstr
->pcm
->card
, pstr
->chmap_kctl
);
862 pstr
->chmap_kctl
= NULL
;
866 static void snd_pcm_free_stream(struct snd_pcm_str
* pstr
)
868 struct snd_pcm_substream
*substream
, *substream_next
;
869 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
870 struct snd_pcm_oss_setup
*setup
, *setupn
;
872 substream
= pstr
->substream
;
874 substream_next
= substream
->next
;
875 snd_pcm_timer_done(substream
);
876 snd_pcm_substream_proc_done(substream
);
878 substream
= substream_next
;
880 snd_pcm_stream_proc_done(pstr
);
881 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
882 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setupn
) {
883 setupn
= setup
->next
;
884 kfree(setup
->task_name
);
889 if (pstr
->substream_count
)
890 put_device(&pstr
->dev
);
893 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
894 #define pcm_call_notify(pcm, call) \
896 struct snd_pcm_notify *_notify; \
897 list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
898 _notify->call(pcm); \
901 #define pcm_call_notify(pcm, call) do {} while (0)
904 static int snd_pcm_free(struct snd_pcm
*pcm
)
909 pcm_call_notify(pcm
, n_unregister
);
910 if (pcm
->private_free
)
911 pcm
->private_free(pcm
);
912 snd_pcm_lib_preallocate_free_for_all(pcm
);
913 snd_pcm_free_stream(&pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
]);
914 snd_pcm_free_stream(&pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
]);
919 static int snd_pcm_dev_free(struct snd_device
*device
)
921 struct snd_pcm
*pcm
= device
->device_data
;
922 return snd_pcm_free(pcm
);
925 int snd_pcm_attach_substream(struct snd_pcm
*pcm
, int stream
,
927 struct snd_pcm_substream
**rsubstream
)
929 struct snd_pcm_str
* pstr
;
930 struct snd_pcm_substream
*substream
;
931 struct snd_pcm_runtime
*runtime
;
932 struct snd_card
*card
;
933 int prefer_subdevice
;
936 if (snd_BUG_ON(!pcm
|| !rsubstream
))
938 if (snd_BUG_ON(stream
!= SNDRV_PCM_STREAM_PLAYBACK
&&
939 stream
!= SNDRV_PCM_STREAM_CAPTURE
))
942 pstr
= &pcm
->streams
[stream
];
943 if (pstr
->substream
== NULL
|| pstr
->substream_count
== 0)
947 prefer_subdevice
= snd_ctl_get_preferred_subdevice(card
, SND_CTL_SUBDEV_PCM
);
949 if (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
) {
950 int opposite
= !stream
;
952 for (substream
= pcm
->streams
[opposite
].substream
; substream
;
953 substream
= substream
->next
) {
954 if (SUBSTREAM_BUSY(substream
))
959 if (file
->f_flags
& O_APPEND
) {
960 if (prefer_subdevice
< 0) {
961 if (pstr
->substream_count
> 1)
962 return -EINVAL
; /* must be unique */
963 substream
= pstr
->substream
;
965 for (substream
= pstr
->substream
; substream
;
966 substream
= substream
->next
)
967 if (substream
->number
== prefer_subdevice
)
972 if (! SUBSTREAM_BUSY(substream
))
974 substream
->ref_count
++;
975 *rsubstream
= substream
;
979 for (substream
= pstr
->substream
; substream
; substream
= substream
->next
) {
980 if (!SUBSTREAM_BUSY(substream
) &&
981 (prefer_subdevice
== -1 ||
982 substream
->number
== prefer_subdevice
))
985 if (substream
== NULL
)
988 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
992 size
= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
));
993 runtime
->status
= snd_malloc_pages(size
, GFP_KERNEL
);
994 if (runtime
->status
== NULL
) {
998 memset((void*)runtime
->status
, 0, size
);
1000 size
= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
));
1001 runtime
->control
= snd_malloc_pages(size
, GFP_KERNEL
);
1002 if (runtime
->control
== NULL
) {
1003 snd_free_pages((void*)runtime
->status
,
1004 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)));
1008 memset((void*)runtime
->control
, 0, size
);
1010 init_waitqueue_head(&runtime
->sleep
);
1011 init_waitqueue_head(&runtime
->tsleep
);
1013 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
1015 substream
->runtime
= runtime
;
1016 substream
->private_data
= pcm
->private_data
;
1017 substream
->ref_count
= 1;
1018 substream
->f_flags
= file
->f_flags
;
1019 substream
->pid
= get_pid(task_pid(current
));
1020 pstr
->substream_opened
++;
1021 *rsubstream
= substream
;
1025 void snd_pcm_detach_substream(struct snd_pcm_substream
*substream
)
1027 struct snd_pcm_runtime
*runtime
;
1029 if (PCM_RUNTIME_CHECK(substream
))
1031 runtime
= substream
->runtime
;
1032 if (runtime
->private_free
!= NULL
)
1033 runtime
->private_free(runtime
);
1034 snd_free_pages((void*)runtime
->status
,
1035 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)));
1036 snd_free_pages((void*)runtime
->control
,
1037 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)));
1038 kfree(runtime
->hw_constraints
.rules
);
1040 substream
->runtime
= NULL
;
1041 put_pid(substream
->pid
);
1042 substream
->pid
= NULL
;
1043 substream
->pstr
->substream_opened
--;
1046 static ssize_t
show_pcm_class(struct device
*dev
,
1047 struct device_attribute
*attr
, char *buf
)
1049 struct snd_pcm_str
*pstr
= container_of(dev
, struct snd_pcm_str
, dev
);
1050 struct snd_pcm
*pcm
= pstr
->pcm
;
1052 static const char *strs
[SNDRV_PCM_CLASS_LAST
+ 1] = {
1053 [SNDRV_PCM_CLASS_GENERIC
] = "generic",
1054 [SNDRV_PCM_CLASS_MULTI
] = "multi",
1055 [SNDRV_PCM_CLASS_MODEM
] = "modem",
1056 [SNDRV_PCM_CLASS_DIGITIZER
] = "digitizer",
1059 if (pcm
->dev_class
> SNDRV_PCM_CLASS_LAST
)
1062 str
= strs
[pcm
->dev_class
];
1063 return snprintf(buf
, PAGE_SIZE
, "%s\n", str
);
1066 static DEVICE_ATTR(pcm_class
, S_IRUGO
, show_pcm_class
, NULL
);
1067 static struct attribute
*pcm_dev_attrs
[] = {
1068 &dev_attr_pcm_class
.attr
,
1072 static const struct attribute_group pcm_dev_attr_group
= {
1073 .attrs
= pcm_dev_attrs
,
1076 static const struct attribute_group
*pcm_dev_attr_groups
[] = {
1077 &pcm_dev_attr_group
,
1081 static int snd_pcm_dev_register(struct snd_device
*device
)
1084 struct snd_pcm_substream
*substream
;
1085 struct snd_pcm
*pcm
;
1087 if (snd_BUG_ON(!device
|| !device
->device_data
))
1089 pcm
= device
->device_data
;
1093 mutex_lock(®ister_mutex
);
1094 err
= snd_pcm_add(pcm
);
1097 for (cidx
= 0; cidx
< 2; cidx
++) {
1099 if (pcm
->streams
[cidx
].substream
== NULL
)
1102 case SNDRV_PCM_STREAM_PLAYBACK
:
1103 devtype
= SNDRV_DEVICE_TYPE_PCM_PLAYBACK
;
1105 case SNDRV_PCM_STREAM_CAPTURE
:
1106 devtype
= SNDRV_DEVICE_TYPE_PCM_CAPTURE
;
1110 err
= snd_register_device(devtype
, pcm
->card
, pcm
->device
,
1111 &snd_pcm_f_ops
[cidx
], pcm
,
1112 &pcm
->streams
[cidx
].dev
);
1114 list_del_init(&pcm
->list
);
1118 for (substream
= pcm
->streams
[cidx
].substream
; substream
; substream
= substream
->next
)
1119 snd_pcm_timer_init(substream
);
1122 pcm_call_notify(pcm
, n_register
);
1125 mutex_unlock(®ister_mutex
);
1129 static int snd_pcm_dev_disconnect(struct snd_device
*device
)
1131 struct snd_pcm
*pcm
= device
->device_data
;
1132 struct snd_pcm_substream
*substream
;
1135 mutex_lock(®ister_mutex
);
1136 mutex_lock(&pcm
->open_mutex
);
1137 wake_up(&pcm
->open_wait
);
1138 list_del_init(&pcm
->list
);
1139 for (cidx
= 0; cidx
< 2; cidx
++) {
1140 for (substream
= pcm
->streams
[cidx
].substream
; substream
; substream
= substream
->next
) {
1141 snd_pcm_stream_lock_irq(substream
);
1142 if (substream
->runtime
) {
1143 substream
->runtime
->status
->state
= SNDRV_PCM_STATE_DISCONNECTED
;
1144 wake_up(&substream
->runtime
->sleep
);
1145 wake_up(&substream
->runtime
->tsleep
);
1147 snd_pcm_stream_unlock_irq(substream
);
1150 if (!pcm
->internal
) {
1151 pcm_call_notify(pcm
, n_disconnect
);
1153 for (cidx
= 0; cidx
< 2; cidx
++) {
1155 snd_unregister_device(&pcm
->streams
[cidx
].dev
);
1156 free_chmap(&pcm
->streams
[cidx
]);
1158 mutex_unlock(&pcm
->open_mutex
);
1159 mutex_unlock(®ister_mutex
);
1163 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
1165 * snd_pcm_notify - Add/remove the notify list
1166 * @notify: PCM notify list
1167 * @nfree: 0 = register, 1 = unregister
1169 * This adds the given notifier to the global list so that the callback is
1170 * called for each registered PCM devices. This exists only for PCM OSS
1171 * emulation, so far.
1173 int snd_pcm_notify(struct snd_pcm_notify
*notify
, int nfree
)
1175 struct snd_pcm
*pcm
;
1177 if (snd_BUG_ON(!notify
||
1178 !notify
->n_register
||
1179 !notify
->n_unregister
||
1180 !notify
->n_disconnect
))
1182 mutex_lock(®ister_mutex
);
1184 list_del(¬ify
->list
);
1185 list_for_each_entry(pcm
, &snd_pcm_devices
, list
)
1186 notify
->n_unregister(pcm
);
1188 list_add_tail(¬ify
->list
, &snd_pcm_notify_list
);
1189 list_for_each_entry(pcm
, &snd_pcm_devices
, list
)
1190 notify
->n_register(pcm
);
1192 mutex_unlock(®ister_mutex
);
1195 EXPORT_SYMBOL(snd_pcm_notify
);
1196 #endif /* CONFIG_SND_PCM_OSS */
1198 #ifdef CONFIG_SND_PROC_FS
1203 static void snd_pcm_proc_read(struct snd_info_entry
*entry
,
1204 struct snd_info_buffer
*buffer
)
1206 struct snd_pcm
*pcm
;
1208 mutex_lock(®ister_mutex
);
1209 list_for_each_entry(pcm
, &snd_pcm_devices
, list
) {
1210 snd_iprintf(buffer
, "%02i-%02i: %s : %s",
1211 pcm
->card
->number
, pcm
->device
, pcm
->id
, pcm
->name
);
1212 if (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
)
1213 snd_iprintf(buffer
, " : playback %i",
1214 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
);
1215 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
)
1216 snd_iprintf(buffer
, " : capture %i",
1217 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
);
1218 snd_iprintf(buffer
, "\n");
1220 mutex_unlock(®ister_mutex
);
1223 static struct snd_info_entry
*snd_pcm_proc_entry
;
1225 static void snd_pcm_proc_init(void)
1227 struct snd_info_entry
*entry
;
1229 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "pcm", NULL
)) != NULL
) {
1230 snd_info_set_text_ops(entry
, NULL
, snd_pcm_proc_read
);
1231 if (snd_info_register(entry
) < 0) {
1232 snd_info_free_entry(entry
);
1236 snd_pcm_proc_entry
= entry
;
1239 static void snd_pcm_proc_done(void)
1241 snd_info_free_entry(snd_pcm_proc_entry
);
1244 #else /* !CONFIG_SND_PROC_FS */
1245 #define snd_pcm_proc_init()
1246 #define snd_pcm_proc_done()
1247 #endif /* CONFIG_SND_PROC_FS */
1254 static int __init
alsa_pcm_init(void)
1256 snd_ctl_register_ioctl(snd_pcm_control_ioctl
);
1257 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl
);
1258 snd_pcm_proc_init();
1262 static void __exit
alsa_pcm_exit(void)
1264 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl
);
1265 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl
);
1266 snd_pcm_proc_done();
1269 module_init(alsa_pcm_init
)
1270 module_exit(alsa_pcm_exit
)