]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - sound/core/pcm.c
x86/speculation: Rework speculative_store_bypass_update()
[mirror_ubuntu-artful-kernel.git] / sound / core / pcm.c
CommitLineData
1da177e4
LT
1/*
2 * Digital Audio (PCM) abstract layer
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1da177e4
LT
22#include <linux/init.h>
23#include <linux/slab.h>
da155d5b 24#include <linux/module.h>
1da177e4 25#include <linux/time.h>
1a60d4c5 26#include <linux/mutex.h>
51990e82 27#include <linux/device.h>
1da177e4
LT
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>
33
2c4842d3
TS
34#include "pcm_local.h"
35
c1017a4c 36MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
1da177e4
LT
37MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
38MODULE_LICENSE("GPL");
39
f87135f5 40static LIST_HEAD(snd_pcm_devices);
1a60d4c5 41static DEFINE_MUTEX(register_mutex);
58f30d65
TI
42#if IS_ENABLED(CONFIG_SND_PCM_OSS)
43static LIST_HEAD(snd_pcm_notify_list);
44#endif
1da177e4 45
877211f5
TI
46static int snd_pcm_free(struct snd_pcm *pcm);
47static int snd_pcm_dev_free(struct snd_device *device);
48static int snd_pcm_dev_register(struct snd_device *device);
49static int snd_pcm_dev_disconnect(struct snd_device *device);
1da177e4 50
f90c06a2 51static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
f87135f5 52{
f87135f5
CL
53 struct snd_pcm *pcm;
54
9244b2c3 55 list_for_each_entry(pcm, &snd_pcm_devices, list) {
f87135f5
CL
56 if (pcm->card == card && pcm->device == device)
57 return pcm;
58 }
59 return NULL;
60}
61
f90c06a2
PM
62static int snd_pcm_next(struct snd_card *card, int device)
63{
64 struct snd_pcm *pcm;
65
66 list_for_each_entry(pcm, &snd_pcm_devices, list) {
67 if (pcm->card == card && pcm->device > device)
68 return pcm->device;
69 else if (pcm->card->number > card->number)
70 return -1;
71 }
72 return -1;
73}
74
75static int snd_pcm_add(struct snd_pcm *newpcm)
76{
77 struct snd_pcm *pcm;
78
b95bd3a4
TI
79 if (newpcm->internal)
80 return 0;
81
f90c06a2
PM
82 list_for_each_entry(pcm, &snd_pcm_devices, list) {
83 if (pcm->card == newpcm->card && pcm->device == newpcm->device)
84 return -EBUSY;
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);
89 return 0;
90 }
91 }
92 list_add_tail(&newpcm->list, &snd_pcm_devices);
93 return 0;
94}
95
877211f5
TI
96static int snd_pcm_control_ioctl(struct snd_card *card,
97 struct snd_ctl_file *control,
1da177e4
LT
98 unsigned int cmd, unsigned long arg)
99{
1da177e4
LT
100 switch (cmd) {
101 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
102 {
103 int device;
104
105 if (get_user(device, (int __user *)arg))
106 return -EFAULT;
1a60d4c5 107 mutex_lock(&register_mutex);
f90c06a2 108 device = snd_pcm_next(card, device);
1a60d4c5 109 mutex_unlock(&register_mutex);
1da177e4
LT
110 if (put_user(device, (int __user *)arg))
111 return -EFAULT;
112 return 0;
113 }
114 case SNDRV_CTL_IOCTL_PCM_INFO:
115 {
877211f5 116 struct snd_pcm_info __user *info;
1da177e4 117 unsigned int device, subdevice;
877211f5
TI
118 int stream;
119 struct snd_pcm *pcm;
120 struct snd_pcm_str *pstr;
121 struct snd_pcm_substream *substream;
f87135f5
CL
122 int err;
123
877211f5 124 info = (struct snd_pcm_info __user *)arg;
1da177e4
LT
125 if (get_user(device, &info->device))
126 return -EFAULT;
1da177e4
LT
127 if (get_user(stream, &info->stream))
128 return -EFAULT;
129 if (stream < 0 || stream > 1)
130 return -EINVAL;
1da177e4
LT
131 if (get_user(subdevice, &info->subdevice))
132 return -EFAULT;
1a60d4c5 133 mutex_lock(&register_mutex);
f90c06a2 134 pcm = snd_pcm_get(card, device);
f87135f5
CL
135 if (pcm == NULL) {
136 err = -ENXIO;
137 goto _error;
138 }
139 pstr = &pcm->streams[stream];
140 if (pstr->substream_count == 0) {
141 err = -ENOENT;
142 goto _error;
143 }
144 if (subdevice >= pstr->substream_count) {
145 err = -ENXIO;
146 goto _error;
147 }
148 for (substream = pstr->substream; substream;
149 substream = substream->next)
1da177e4
LT
150 if (substream->number == (int)subdevice)
151 break;
f87135f5
CL
152 if (substream == NULL) {
153 err = -ENXIO;
154 goto _error;
155 }
d5b39a01 156 mutex_lock(&pcm->open_mutex);
f87135f5 157 err = snd_pcm_info_user(substream, info);
d5b39a01 158 mutex_unlock(&pcm->open_mutex);
f87135f5 159 _error:
1a60d4c5 160 mutex_unlock(&register_mutex);
f87135f5 161 return err;
1da177e4
LT
162 }
163 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
164 {
165 int val;
166
167 if (get_user(val, (int __user *)arg))
168 return -EFAULT;
23c18d4b 169 control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
1da177e4
LT
170 return 0;
171 }
172 }
173 return -ENOIOCTLCMD;
174}
21a3479a 175
1da177e4 176#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
1da177e4 177
1da177e4
LT
178static char *snd_pcm_format_names[] = {
179 FORMAT(S8),
180 FORMAT(U8),
181 FORMAT(S16_LE),
182 FORMAT(S16_BE),
183 FORMAT(U16_LE),
184 FORMAT(U16_BE),
185 FORMAT(S24_LE),
186 FORMAT(S24_BE),
187 FORMAT(U24_LE),
188 FORMAT(U24_BE),
189 FORMAT(S32_LE),
190 FORMAT(S32_BE),
191 FORMAT(U32_LE),
192 FORMAT(U32_BE),
193 FORMAT(FLOAT_LE),
194 FORMAT(FLOAT_BE),
195 FORMAT(FLOAT64_LE),
196 FORMAT(FLOAT64_BE),
197 FORMAT(IEC958_SUBFRAME_LE),
198 FORMAT(IEC958_SUBFRAME_BE),
199 FORMAT(MU_LAW),
200 FORMAT(A_LAW),
201 FORMAT(IMA_ADPCM),
202 FORMAT(MPEG),
203 FORMAT(GSM),
204 FORMAT(SPECIAL),
205 FORMAT(S24_3LE),
206 FORMAT(S24_3BE),
207 FORMAT(U24_3LE),
208 FORMAT(U24_3BE),
209 FORMAT(S20_3LE),
210 FORMAT(S20_3BE),
211 FORMAT(U20_3LE),
212 FORMAT(U20_3BE),
213 FORMAT(S18_3LE),
214 FORMAT(S18_3BE),
215 FORMAT(U18_3LE),
216 FORMAT(U18_3BE),
7a28826a
DC
217 FORMAT(G723_24),
218 FORMAT(G723_24_1B),
219 FORMAT(G723_40),
220 FORMAT(G723_40_1B),
ef7a4f97
DM
221 FORMAT(DSD_U8),
222 FORMAT(DSD_U16_LE),
d4288d3f 223 FORMAT(DSD_U32_LE),
d42472ec
JL
224 FORMAT(DSD_U16_BE),
225 FORMAT(DSD_U32_BE),
1da177e4
LT
226};
227
30b771cf
TI
228/**
229 * snd_pcm_format_name - Return a name string for the given PCM format
230 * @format: PCM format
231 */
6e5265ec 232const char *snd_pcm_format_name(snd_pcm_format_t format)
e28563cc 233{
fea952e5 234 if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
7a28826a 235 return "Unknown";
fea952e5 236 return snd_pcm_format_names[(__force unsigned int)format];
e28563cc 237}
6e5265ec
TI
238EXPORT_SYMBOL_GPL(snd_pcm_format_name);
239
240#ifdef CONFIG_SND_VERBOSE_PROCFS
241
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
e28563cc 251
e28563cc
TI
252static char *snd_pcm_stream_names[] = {
253 STREAM(PLAYBACK),
254 STREAM(CAPTURE),
255};
256
257static char *snd_pcm_state_names[] = {
258 STATE(OPEN),
259 STATE(SETUP),
260 STATE(PREPARED),
261 STATE(RUNNING),
262 STATE(XRUN),
263 STATE(DRAINING),
264 STATE(PAUSED),
265 STATE(SUSPENDED),
266};
267
268static 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),
274};
275
1da177e4
LT
276static char *snd_pcm_subformat_names[] = {
277 SUBFORMAT(STD),
278};
279
280static char *snd_pcm_tstamp_mode_names[] = {
281 TSTAMP(NONE),
8c121586 282 TSTAMP(ENABLE),
1da177e4
LT
283};
284
877211f5 285static const char *snd_pcm_stream_name(int stream)
1da177e4 286{
1da177e4
LT
287 return snd_pcm_stream_names[stream];
288}
289
290static const char *snd_pcm_access_name(snd_pcm_access_t access)
291{
fea952e5 292 return snd_pcm_access_names[(__force int)access];
1da177e4
LT
293}
294
1da177e4
LT
295static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
296{
fea952e5 297 return snd_pcm_subformat_names[(__force int)subformat];
1da177e4
LT
298}
299
877211f5 300static const char *snd_pcm_tstamp_mode_name(int mode)
1da177e4 301{
1da177e4
LT
302 return snd_pcm_tstamp_mode_names[mode];
303}
304
305static const char *snd_pcm_state_name(snd_pcm_state_t state)
306{
fea952e5 307 return snd_pcm_state_names[(__force int)state];
1da177e4
LT
308}
309
8eeaa2f9 310#if IS_ENABLED(CONFIG_SND_PCM_OSS)
1da177e4 311#include <linux/soundcard.h>
1a60d4c5 312
1da177e4
LT
313static const char *snd_pcm_oss_format_name(int format)
314{
315 switch (format) {
316 case AFMT_MU_LAW:
317 return "MU_LAW";
318 case AFMT_A_LAW:
319 return "A_LAW";
320 case AFMT_IMA_ADPCM:
321 return "IMA_ADPCM";
322 case AFMT_U8:
323 return "U8";
324 case AFMT_S16_LE:
325 return "S16_LE";
326 case AFMT_S16_BE:
327 return "S16_BE";
328 case AFMT_S8:
329 return "S8";
330 case AFMT_U16_LE:
331 return "U16_LE";
332 case AFMT_U16_BE:
333 return "U16_BE";
334 case AFMT_MPEG:
335 return "MPEG";
336 default:
337 return "unknown";
338 }
339}
340#endif
341
877211f5
TI
342static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
343 struct snd_info_buffer *buffer)
1da177e4 344{
877211f5 345 struct snd_pcm_info *info;
1da177e4
LT
346 int err;
347
7c22f1aa
TI
348 if (! substream)
349 return;
1da177e4
LT
350
351 info = kmalloc(sizeof(*info), GFP_KERNEL);
ec0e9937 352 if (!info)
1da177e4 353 return;
1da177e4
LT
354
355 err = snd_pcm_info(substream, info);
356 if (err < 0) {
357 snd_iprintf(buffer, "error %d\n", err);
358 kfree(info);
359 return;
360 }
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);
372 kfree(info);
373}
374
877211f5
TI
375static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
376 struct snd_info_buffer *buffer)
1da177e4 377{
877211f5
TI
378 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
379 buffer);
1da177e4
LT
380}
381
877211f5
TI
382static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
383 struct snd_info_buffer *buffer)
1da177e4 384{
9fe856e4 385 snd_pcm_proc_info_read(entry->private_data, buffer);
1da177e4
LT
386}
387
877211f5
TI
388static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
389 struct snd_info_buffer *buffer)
1da177e4 390{
877211f5 391 struct snd_pcm_substream *substream = entry->private_data;
901d46d5
TI
392 struct snd_pcm_runtime *runtime;
393
394 mutex_lock(&substream->pcm->open_mutex);
395 runtime = substream->runtime;
1da177e4
LT
396 if (!runtime) {
397 snd_iprintf(buffer, "closed\n");
901d46d5 398 goto unlock;
1da177e4 399 }
1da177e4
LT
400 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
401 snd_iprintf(buffer, "no setup\n");
901d46d5 402 goto unlock;
1da177e4
LT
403 }
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);
8eeaa2f9 411#if IS_ENABLED(CONFIG_SND_PCM_OSS)
1da177e4
LT
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);
419 }
420#endif
901d46d5
TI
421 unlock:
422 mutex_unlock(&substream->pcm->open_mutex);
1da177e4
LT
423}
424
877211f5
TI
425static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
426 struct snd_info_buffer *buffer)
1da177e4 427{
877211f5 428 struct snd_pcm_substream *substream = entry->private_data;
901d46d5
TI
429 struct snd_pcm_runtime *runtime;
430
431 mutex_lock(&substream->pcm->open_mutex);
432 runtime = substream->runtime;
1da177e4
LT
433 if (!runtime) {
434 snd_iprintf(buffer, "closed\n");
901d46d5 435 goto unlock;
1da177e4 436 }
1da177e4
LT
437 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
438 snd_iprintf(buffer, "no setup\n");
901d46d5 439 goto unlock;
1da177e4
LT
440 }
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);
1da177e4 443 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
1da177e4
LT
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);
901d46d5
TI
449 unlock:
450 mutex_unlock(&substream->pcm->open_mutex);
1da177e4
LT
451}
452
877211f5
TI
453static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
454 struct snd_info_buffer *buffer)
1da177e4 455{
877211f5 456 struct snd_pcm_substream *substream = entry->private_data;
901d46d5 457 struct snd_pcm_runtime *runtime;
877211f5 458 struct snd_pcm_status status;
1da177e4 459 int err;
901d46d5
TI
460
461 mutex_lock(&substream->pcm->open_mutex);
462 runtime = substream->runtime;
1da177e4
LT
463 if (!runtime) {
464 snd_iprintf(buffer, "closed\n");
901d46d5 465 goto unlock;
1da177e4
LT
466 }
467 memset(&status, 0, sizeof(status));
468 err = snd_pcm_status(substream, &status);
469 if (err < 0) {
470 snd_iprintf(buffer, "error %d\n", err);
901d46d5 471 goto unlock;
1da177e4
LT
472 }
473 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
e7373b70 474 snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
1da177e4
LT
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);
901d46d5
TI
485 unlock:
486 mutex_unlock(&substream->pcm->open_mutex);
1da177e4 487}
1da177e4 488
61fb63c0 489#ifdef CONFIG_SND_PCM_XRUN_DEBUG
2b30d411
TI
490static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
491 struct snd_info_buffer *buffer)
492{
493 struct snd_pcm_substream *substream = entry->private_data;
494 struct snd_pcm_runtime *runtime;
495
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);
501}
502
877211f5
TI
503static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
504 struct snd_info_buffer *buffer)
1da177e4 505{
877211f5 506 struct snd_pcm_str *pstr = entry->private_data;
1da177e4
LT
507 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
508}
509
877211f5
TI
510static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
511 struct snd_info_buffer *buffer)
1da177e4 512{
877211f5 513 struct snd_pcm_str *pstr = entry->private_data;
1da177e4
LT
514 char line[64];
515 if (!snd_info_get_line(buffer, line, sizeof(line)))
516 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
517}
518#endif
519
877211f5 520static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
1da177e4 521{
877211f5
TI
522 struct snd_pcm *pcm = pstr->pcm;
523 struct snd_info_entry *entry;
1da177e4
LT
524 char name[16];
525
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)
529 return -ENOMEM;
530 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
533 return -ENOMEM;
534 }
535 pstr->proc_root = entry;
536
537 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
bf850204 538 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
1da177e4
LT
539 if (snd_info_register(entry) < 0) {
540 snd_info_free_entry(entry);
541 entry = NULL;
542 }
543 }
544 pstr->proc_info_entry = entry;
545
61fb63c0 546#ifdef CONFIG_SND_PCM_XRUN_DEBUG
877211f5
TI
547 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
548 pstr->proc_root)) != NULL) {
1da177e4 549 entry->c.text.read = snd_pcm_xrun_debug_read;
1da177e4 550 entry->c.text.write = snd_pcm_xrun_debug_write;
bd7bf042 551 entry->mode |= S_IWUSR;
1da177e4
LT
552 entry->private_data = pstr;
553 if (snd_info_register(entry) < 0) {
554 snd_info_free_entry(entry);
555 entry = NULL;
556 }
557 }
558 pstr->proc_xrun_debug_entry = entry;
559#endif
560 return 0;
561}
562
877211f5 563static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
1da177e4 564{
61fb63c0 565#ifdef CONFIG_SND_PCM_XRUN_DEBUG
746d4a02
TI
566 snd_info_free_entry(pstr->proc_xrun_debug_entry);
567 pstr->proc_xrun_debug_entry = NULL;
1da177e4 568#endif
746d4a02
TI
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;
1da177e4
LT
573 return 0;
574}
575
877211f5 576static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
1da177e4 577{
877211f5
TI
578 struct snd_info_entry *entry;
579 struct snd_card *card;
1da177e4
LT
580 char name[16];
581
582 card = substream->pcm->card;
583
584 sprintf(name, "sub%i", substream->number);
585 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
586 return -ENOMEM;
587 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
588 if (snd_info_register(entry) < 0) {
589 snd_info_free_entry(entry);
590 return -ENOMEM;
591 }
592 substream->proc_root = entry;
593
594 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
bf850204
TI
595 snd_info_set_text_ops(entry, substream,
596 snd_pcm_substream_proc_info_read);
1da177e4
LT
597 if (snd_info_register(entry) < 0) {
598 snd_info_free_entry(entry);
599 entry = NULL;
600 }
601 }
602 substream->proc_info_entry = entry;
603
604 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
bf850204
TI
605 snd_info_set_text_ops(entry, substream,
606 snd_pcm_substream_proc_hw_params_read);
1da177e4
LT
607 if (snd_info_register(entry) < 0) {
608 snd_info_free_entry(entry);
609 entry = NULL;
610 }
611 }
612 substream->proc_hw_params_entry = entry;
613
614 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
bf850204
TI
615 snd_info_set_text_ops(entry, substream,
616 snd_pcm_substream_proc_sw_params_read);
1da177e4
LT
617 if (snd_info_register(entry) < 0) {
618 snd_info_free_entry(entry);
619 entry = NULL;
620 }
621 }
622 substream->proc_sw_params_entry = entry;
623
624 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
bf850204
TI
625 snd_info_set_text_ops(entry, substream,
626 snd_pcm_substream_proc_status_read);
1da177e4
LT
627 if (snd_info_register(entry) < 0) {
628 snd_info_free_entry(entry);
629 entry = NULL;
630 }
631 }
632 substream->proc_status_entry = entry;
633
2b30d411
TI
634#ifdef CONFIG_SND_PCM_XRUN_DEBUG
635 entry = snd_info_create_card_entry(card, "xrun_injection",
636 substream->proc_root);
637 if (entry) {
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);
644 entry = NULL;
645 }
646 }
647 substream->proc_xrun_injection_entry = entry;
648#endif /* CONFIG_SND_PCM_XRUN_DEBUG */
649
1da177e4
LT
650 return 0;
651}
746d4a02 652
877211f5 653static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
1da177e4 654{
746d4a02
TI
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;
2b30d411
TI
663#ifdef CONFIG_SND_PCM_XRUN_DEBUG
664 snd_info_free_entry(substream->proc_xrun_injection_entry);
665 substream->proc_xrun_injection_entry = NULL;
666#endif
746d4a02
TI
667 snd_info_free_entry(substream->proc_root);
668 substream->proc_root = NULL;
1da177e4
LT
669 return 0;
670}
b7d90a35 671#else /* !CONFIG_SND_VERBOSE_PROCFS */
e28563cc
TI
672static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
673static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
674static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
675static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
b7d90a35 676#endif /* CONFIG_SND_VERBOSE_PROCFS */
1da177e4 677
ef46c7af
TI
678static const struct attribute_group *pcm_dev_attr_groups[];
679
1da177e4
LT
680/**
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
685 *
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
689 * snd_pcm_new().
690 *
eb7c06e8 691 * Return: Zero if successful, or a negative error code on failure.
1da177e4 692 */
877211f5 693int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
1da177e4
LT
694{
695 int idx, err;
877211f5
TI
696 struct snd_pcm_str *pstr = &pcm->streams[stream];
697 struct snd_pcm_substream *substream, *prev;
1da177e4 698
8eeaa2f9 699#if IS_ENABLED(CONFIG_SND_PCM_OSS)
1a60d4c5 700 mutex_init(&pstr->oss.setup_mutex);
1da177e4
LT
701#endif
702 pstr->stream = stream;
703 pstr->pcm = pcm;
704 pstr->substream_count = substream_count;
ef46c7af
TI
705 if (!substream_count)
706 return 0;
707
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');
712
713 if (!pcm->internal) {
1da177e4 714 err = snd_pcm_stream_proc_init(pstr);
73e77ba0 715 if (err < 0) {
09e56df8 716 pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
1da177e4 717 return err;
73e77ba0 718 }
1da177e4
LT
719 }
720 prev = NULL;
721 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
ca2c0966 722 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
ec0e9937 723 if (!substream)
1da177e4
LT
724 return -ENOMEM;
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;
731 if (prev == NULL)
732 pstr->substream = substream;
733 else
734 prev->next = substream;
945e5038
LG
735
736 if (!pcm->internal) {
737 err = snd_pcm_substream_proc_init(substream);
738 if (err < 0) {
09e56df8
TI
739 pcm_err(pcm,
740 "Error in snd_pcm_stream_proc_init\n");
945e5038
LG
741 if (prev == NULL)
742 pstr->substream = NULL;
743 else
744 prev->next = NULL;
745 kfree(substream);
746 return err;
747 }
1da177e4
LT
748 }
749 substream->group = &substream->self_group;
750 spin_lock_init(&substream->self_group.lock);
257f8cce 751 mutex_init(&substream->self_group.mutex);
1da177e4
LT
752 INIT_LIST_HEAD(&substream->self_group.substreams);
753 list_add_tail(&substream->link_list, &substream->self_group.substreams);
9c323fcb 754 atomic_set(&substream->mmap_count, 0);
1da177e4
LT
755 prev = substream;
756 }
757 return 0;
758}
e88e8ae6
TI
759EXPORT_SYMBOL(snd_pcm_new_stream);
760
945e5038
LG
761static 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)
1da177e4 764{
877211f5 765 struct snd_pcm *pcm;
1da177e4 766 int err;
877211f5 767 static struct snd_device_ops ops = {
1da177e4
LT
768 .dev_free = snd_pcm_dev_free,
769 .dev_register = snd_pcm_dev_register,
770 .dev_disconnect = snd_pcm_dev_disconnect,
1da177e4
LT
771 };
772
7eaa943c
TI
773 if (snd_BUG_ON(!card))
774 return -ENXIO;
775 if (rpcm)
776 *rpcm = NULL;
ca2c0966 777 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
ec0e9937 778 if (!pcm)
1da177e4
LT
779 return -ENOMEM;
780 pcm->card = card;
781 pcm->device = device;
945e5038 782 pcm->internal = internal;
b95bd3a4
TI
783 mutex_init(&pcm->open_mutex);
784 init_waitqueue_head(&pcm->open_wait);
785 INIT_LIST_HEAD(&pcm->list);
73e77ba0 786 if (id)
1da177e4 787 strlcpy(pcm->id, id, sizeof(pcm->id));
1da177e4
LT
788 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
789 snd_pcm_free(pcm);
790 return err;
791 }
792 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
793 snd_pcm_free(pcm);
794 return err;
795 }
1da177e4
LT
796 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
797 snd_pcm_free(pcm);
798 return err;
799 }
7eaa943c
TI
800 if (rpcm)
801 *rpcm = pcm;
1da177e4
LT
802 return 0;
803}
804
945e5038
LG
805/**
806 * snd_pcm_new - create a new PCM instance
807 * @card: the card instance
808 * @id: the id string
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
813 *
814 * Creates a new PCM instance.
815 *
816 * The pcm operators have to be set afterwards to the new instance
817 * via snd_pcm_set_ops().
818 *
eb7c06e8 819 * Return: Zero if successful, or a negative error code on failure.
945e5038
LG
820 */
821int snd_pcm_new(struct snd_card *card, const char *id, int device,
822 int playback_count, int capture_count, struct snd_pcm **rpcm)
823{
824 return _snd_pcm_new(card, id, device, playback_count, capture_count,
825 false, rpcm);
826}
e88e8ae6
TI
827EXPORT_SYMBOL(snd_pcm_new);
828
945e5038
LG
829/**
830 * snd_pcm_new_internal - create a new internal PCM instance
831 * @card: the card instance
832 * @id: the id string
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
837 *
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.
843 *
844 * The pcm operators have to be set afterwards to the new instance
845 * via snd_pcm_set_ops().
846 *
eb7c06e8 847 * Return: Zero if successful, or a negative error code on failure.
945e5038
LG
848 */
849int 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)
852{
853 return _snd_pcm_new(card, id, device, playback_count, capture_count,
854 true, rpcm);
855}
856EXPORT_SYMBOL(snd_pcm_new_internal);
857
a8ff48cb
TI
858static void free_chmap(struct snd_pcm_str *pstr)
859{
860 if (pstr->chmap_kctl) {
861 snd_ctl_remove(pstr->pcm->card, pstr->chmap_kctl);
862 pstr->chmap_kctl = NULL;
863 }
864}
865
877211f5 866static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
1da177e4 867{
877211f5 868 struct snd_pcm_substream *substream, *substream_next;
8eeaa2f9 869#if IS_ENABLED(CONFIG_SND_PCM_OSS)
877211f5 870 struct snd_pcm_oss_setup *setup, *setupn;
1da177e4
LT
871#endif
872 substream = pstr->substream;
873 while (substream) {
874 substream_next = substream->next;
c461482c 875 snd_pcm_timer_done(substream);
1da177e4
LT
876 snd_pcm_substream_proc_done(substream);
877 kfree(substream);
878 substream = substream_next;
879 }
880 snd_pcm_stream_proc_done(pstr);
8eeaa2f9 881#if IS_ENABLED(CONFIG_SND_PCM_OSS)
1da177e4
LT
882 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
883 setupn = setup->next;
884 kfree(setup->task_name);
885 kfree(setup);
886 }
887#endif
a8ff48cb 888 free_chmap(pstr);
ef46c7af
TI
889 if (pstr->substream_count)
890 put_device(&pstr->dev);
1da177e4
LT
891}
892
58f30d65
TI
893#if IS_ENABLED(CONFIG_SND_PCM_OSS)
894#define pcm_call_notify(pcm, call) \
895 do { \
896 struct snd_pcm_notify *_notify; \
897 list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
898 _notify->call(pcm); \
899 } while (0)
900#else
9aee03f3 901#define pcm_call_notify(pcm, call) do {} while (0)
58f30d65
TI
902#endif
903
877211f5 904static int snd_pcm_free(struct snd_pcm *pcm)
1da177e4 905{
7eaa943c
TI
906 if (!pcm)
907 return 0;
58f30d65
TI
908 if (!pcm->internal)
909 pcm_call_notify(pcm, n_unregister);
1da177e4
LT
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]);
915 kfree(pcm);
916 return 0;
917}
918
877211f5 919static int snd_pcm_dev_free(struct snd_device *device)
1da177e4 920{
877211f5 921 struct snd_pcm *pcm = device->device_data;
1da177e4
LT
922 return snd_pcm_free(pcm);
923}
924
3bf75f9b
TI
925int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
926 struct file *file,
927 struct snd_pcm_substream **rsubstream)
1da177e4 928{
877211f5
TI
929 struct snd_pcm_str * pstr;
930 struct snd_pcm_substream *substream;
931 struct snd_pcm_runtime *runtime;
877211f5 932 struct snd_card *card;
23c18d4b 933 int prefer_subdevice;
1da177e4
LT
934 size_t size;
935
7eaa943c
TI
936 if (snd_BUG_ON(!pcm || !rsubstream))
937 return -ENXIO;
ad876c86
TI
938 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
939 stream != SNDRV_PCM_STREAM_CAPTURE))
940 return -EINVAL;
1da177e4 941 *rsubstream = NULL;
1da177e4 942 pstr = &pcm->streams[stream];
3bf75f9b 943 if (pstr->substream == NULL || pstr->substream_count == 0)
1da177e4
LT
944 return -ENODEV;
945
946 card = pcm->card;
23c18d4b 947 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
1da177e4 948
ad876c86
TI
949 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
950 int opposite = !stream;
951
952 for (substream = pcm->streams[opposite].substream; substream;
953 substream = substream->next) {
954 if (SUBSTREAM_BUSY(substream))
955 return -EAGAIN;
1da177e4 956 }
1da177e4
LT
957 }
958
0df63e44
TI
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;
964 } else {
965 for (substream = pstr->substream; substream;
966 substream = substream->next)
967 if (substream->number == prefer_subdevice)
968 break;
969 }
970 if (! substream)
971 return -ENODEV;
972 if (! SUBSTREAM_BUSY(substream))
973 return -EBADFD;
974 substream->ref_count++;
975 *rsubstream = substream;
976 return 0;
977 }
978
ad876c86
TI
979 for (substream = pstr->substream; substream; substream = substream->next) {
980 if (!SUBSTREAM_BUSY(substream) &&
981 (prefer_subdevice == -1 ||
982 substream->number == prefer_subdevice))
1da177e4 983 break;
ad876c86 984 }
1da177e4
LT
985 if (substream == NULL)
986 return -EAGAIN;
987
ca2c0966 988 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
1da177e4
LT
989 if (runtime == NULL)
990 return -ENOMEM;
991
877211f5 992 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
1da177e4
LT
993 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
994 if (runtime->status == NULL) {
995 kfree(runtime);
996 return -ENOMEM;
997 }
998 memset((void*)runtime->status, 0, size);
999
877211f5 1000 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
1da177e4
LT
1001 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
1002 if (runtime->control == NULL) {
877211f5
TI
1003 snd_free_pages((void*)runtime->status,
1004 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
1da177e4
LT
1005 kfree(runtime);
1006 return -ENOMEM;
1007 }
1008 memset((void*)runtime->control, 0, size);
1009
1010 init_waitqueue_head(&runtime->sleep);
c91a988d 1011 init_waitqueue_head(&runtime->tsleep);
1da177e4
LT
1012
1013 runtime->status->state = SNDRV_PCM_STATE_OPEN;
1014
1015 substream->runtime = runtime;
1016 substream->private_data = pcm->private_data;
0df63e44
TI
1017 substream->ref_count = 1;
1018 substream->f_flags = file->f_flags;
e7373b70 1019 substream->pid = get_pid(task_pid(current));
1da177e4
LT
1020 pstr->substream_opened++;
1021 *rsubstream = substream;
1022 return 0;
1023}
1024
3bf75f9b 1025void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
1da177e4 1026{
877211f5 1027 struct snd_pcm_runtime *runtime;
0df63e44 1028
7eaa943c
TI
1029 if (PCM_RUNTIME_CHECK(substream))
1030 return;
1da177e4 1031 runtime = substream->runtime;
1da177e4
LT
1032 if (runtime->private_free != NULL)
1033 runtime->private_free(runtime);
877211f5
TI
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)));
1da177e4
LT
1038 kfree(runtime->hw_constraints.rules);
1039 kfree(runtime);
1040 substream->runtime = NULL;
e7373b70
CL
1041 put_pid(substream->pid);
1042 substream->pid = NULL;
1da177e4
LT
1043 substream->pstr->substream_opened--;
1044}
1045
d80f19fa
GKH
1046static ssize_t show_pcm_class(struct device *dev,
1047 struct device_attribute *attr, char *buf)
9d19f48c 1048{
60b93030
TI
1049 struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
1050 struct snd_pcm *pcm = pstr->pcm;
9d19f48c
TI
1051 const char *str;
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",
1057 };
1058
60b93030 1059 if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
9d19f48c
TI
1060 str = "none";
1061 else
1062 str = strs[pcm->dev_class];
1063 return snprintf(buf, PAGE_SIZE, "%s\n", str);
1064}
1065
caa751ba
TI
1066static DEVICE_ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1067static struct attribute *pcm_dev_attrs[] = {
1068 &dev_attr_pcm_class.attr,
1069 NULL
1070};
1071
343fe850 1072static const struct attribute_group pcm_dev_attr_group = {
caa751ba
TI
1073 .attrs = pcm_dev_attrs,
1074};
1075
1076static const struct attribute_group *pcm_dev_attr_groups[] = {
1077 &pcm_dev_attr_group,
1078 NULL
1079};
9d19f48c 1080
877211f5 1081static int snd_pcm_dev_register(struct snd_device *device)
1da177e4 1082{
f87135f5 1083 int cidx, err;
877211f5 1084 struct snd_pcm_substream *substream;
4b3be6af 1085 struct snd_pcm *pcm;
1da177e4 1086
4b3be6af 1087 if (snd_BUG_ON(!device || !device->device_data))
7eaa943c 1088 return -ENXIO;
4b3be6af 1089 pcm = device->device_data;
b95bd3a4
TI
1090 if (pcm->internal)
1091 return 0;
1092
1a60d4c5 1093 mutex_lock(&register_mutex);
f90c06a2 1094 err = snd_pcm_add(pcm);
b95bd3a4
TI
1095 if (err)
1096 goto unlock;
1da177e4
LT
1097 for (cidx = 0; cidx < 2; cidx++) {
1098 int devtype = -1;
b95bd3a4 1099 if (pcm->streams[cidx].substream == NULL)
1da177e4
LT
1100 continue;
1101 switch (cidx) {
1102 case SNDRV_PCM_STREAM_PLAYBACK:
1da177e4
LT
1103 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1104 break;
1105 case SNDRV_PCM_STREAM_CAPTURE:
1da177e4
LT
1106 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1107 break;
1108 }
c78085fc 1109 /* register pcm */
40a4b263
TI
1110 err = snd_register_device(devtype, pcm->card, pcm->device,
1111 &snd_pcm_f_ops[cidx], pcm,
1112 &pcm->streams[cidx].dev);
c78085fc 1113 if (err < 0) {
b95bd3a4
TI
1114 list_del_init(&pcm->list);
1115 goto unlock;
1da177e4 1116 }
caa751ba 1117
1da177e4
LT
1118 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1119 snd_pcm_timer_init(substream);
1120 }
9244b2c3 1121
58f30d65 1122 pcm_call_notify(pcm, n_register);
9244b2c3 1123
b95bd3a4 1124 unlock:
1a60d4c5 1125 mutex_unlock(&register_mutex);
b95bd3a4 1126 return err;
1da177e4
LT
1127}
1128
877211f5 1129static int snd_pcm_dev_disconnect(struct snd_device *device)
1da177e4 1130{
877211f5 1131 struct snd_pcm *pcm = device->device_data;
877211f5 1132 struct snd_pcm_substream *substream;
40a4b263 1133 int cidx;
1da177e4 1134
1a60d4c5 1135 mutex_lock(&register_mutex);
9b0573c0 1136 mutex_lock(&pcm->open_mutex);
0914f796 1137 wake_up(&pcm->open_wait);
f87135f5 1138 list_del_init(&pcm->list);
646e1dd8 1139 for (cidx = 0; cidx < 2; cidx++) {
9b0573c0
TI
1140 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1141 snd_pcm_stream_lock_irq(substream);
0914f796 1142 if (substream->runtime) {
1da177e4 1143 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
0914f796
TI
1144 wake_up(&substream->runtime->sleep);
1145 wake_up(&substream->runtime->tsleep);
1146 }
9b0573c0
TI
1147 snd_pcm_stream_unlock_irq(substream);
1148 }
646e1dd8
TI
1149 }
1150 if (!pcm->internal) {
58f30d65 1151 pcm_call_notify(pcm, n_disconnect);
1da177e4 1152 }
1da177e4 1153 for (cidx = 0; cidx < 2; cidx++) {
b2022138
TI
1154 if (!pcm->internal)
1155 snd_unregister_device(&pcm->streams[cidx].dev);
a8ff48cb 1156 free_chmap(&pcm->streams[cidx]);
1da177e4 1157 }
9b0573c0 1158 mutex_unlock(&pcm->open_mutex);
1a60d4c5 1159 mutex_unlock(&register_mutex);
c461482c 1160 return 0;
1da177e4
LT
1161}
1162
58f30d65 1163#if IS_ENABLED(CONFIG_SND_PCM_OSS)
30b771cf
TI
1164/**
1165 * snd_pcm_notify - Add/remove the notify list
1166 * @notify: PCM notify list
1167 * @nfree: 0 = register, 1 = unregister
1168 *
1169 * This adds the given notifier to the global list so that the callback is
1170 * called for each registered PCM devices. This exists only for PCM OSS
1171 * emulation, so far.
1172 */
877211f5 1173int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1da177e4 1174{
9244b2c3 1175 struct snd_pcm *pcm;
1da177e4 1176
7eaa943c
TI
1177 if (snd_BUG_ON(!notify ||
1178 !notify->n_register ||
1179 !notify->n_unregister ||
1180 !notify->n_disconnect))
1181 return -EINVAL;
1a60d4c5 1182 mutex_lock(&register_mutex);
1da177e4
LT
1183 if (nfree) {
1184 list_del(&notify->list);
9244b2c3
JB
1185 list_for_each_entry(pcm, &snd_pcm_devices, list)
1186 notify->n_unregister(pcm);
1da177e4
LT
1187 } else {
1188 list_add_tail(&notify->list, &snd_pcm_notify_list);
9244b2c3
JB
1189 list_for_each_entry(pcm, &snd_pcm_devices, list)
1190 notify->n_register(pcm);
1da177e4 1191 }
1a60d4c5 1192 mutex_unlock(&register_mutex);
1da177e4
LT
1193 return 0;
1194}
e88e8ae6 1195EXPORT_SYMBOL(snd_pcm_notify);
58f30d65 1196#endif /* CONFIG_SND_PCM_OSS */
e88e8ae6 1197
cd6a6503 1198#ifdef CONFIG_SND_PROC_FS
1da177e4
LT
1199/*
1200 * Info interface
1201 */
1202
877211f5
TI
1203static void snd_pcm_proc_read(struct snd_info_entry *entry,
1204 struct snd_info_buffer *buffer)
1da177e4 1205{
877211f5 1206 struct snd_pcm *pcm;
1da177e4 1207
1a60d4c5 1208 mutex_lock(&register_mutex);
9244b2c3 1209 list_for_each_entry(pcm, &snd_pcm_devices, list) {
f87135f5
CL
1210 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1211 pcm->card->number, pcm->device, pcm->id, pcm->name);
1da177e4 1212 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
877211f5
TI
1213 snd_iprintf(buffer, " : playback %i",
1214 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1da177e4 1215 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
877211f5
TI
1216 snd_iprintf(buffer, " : capture %i",
1217 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1da177e4
LT
1218 snd_iprintf(buffer, "\n");
1219 }
1a60d4c5 1220 mutex_unlock(&register_mutex);
1da177e4
LT
1221}
1222
6581f4e7 1223static struct snd_info_entry *snd_pcm_proc_entry;
1da177e4 1224
e28563cc 1225static void snd_pcm_proc_init(void)
1da177e4 1226{
877211f5 1227 struct snd_info_entry *entry;
1da177e4 1228
1da177e4 1229 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
bf850204 1230 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1da177e4
LT
1231 if (snd_info_register(entry) < 0) {
1232 snd_info_free_entry(entry);
1233 entry = NULL;
1234 }
1235 }
1236 snd_pcm_proc_entry = entry;
e28563cc
TI
1237}
1238
1239static void snd_pcm_proc_done(void)
1240{
746d4a02 1241 snd_info_free_entry(snd_pcm_proc_entry);
e28563cc
TI
1242}
1243
cd6a6503 1244#else /* !CONFIG_SND_PROC_FS */
e28563cc
TI
1245#define snd_pcm_proc_init()
1246#define snd_pcm_proc_done()
cd6a6503 1247#endif /* CONFIG_SND_PROC_FS */
e28563cc
TI
1248
1249
1250/*
1251 * ENTRY functions
1252 */
1253
1254static int __init alsa_pcm_init(void)
1255{
1256 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1257 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1258 snd_pcm_proc_init();
1da177e4
LT
1259 return 0;
1260}
1261
1262static void __exit alsa_pcm_exit(void)
1263{
1264 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1265 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
e28563cc 1266 snd_pcm_proc_done();
1da177e4
LT
1267}
1268
1269module_init(alsa_pcm_init)
1270module_exit(alsa_pcm_exit)