0001
0002
0003
0004
0005
0006
0007 #include <linux/init.h>
0008 #include <linux/slab.h>
0009 #include <linux/module.h>
0010 #include <linux/time.h>
0011 #include <linux/mutex.h>
0012 #include <linux/device.h>
0013 #include <linux/nospec.h>
0014 #include <sound/core.h>
0015 #include <sound/minors.h>
0016 #include <sound/pcm.h>
0017 #include <sound/timer.h>
0018 #include <sound/control.h>
0019 #include <sound/info.h>
0020
0021 #include "pcm_local.h"
0022
0023 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
0024 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
0025 MODULE_LICENSE("GPL");
0026
0027 static LIST_HEAD(snd_pcm_devices);
0028 static DEFINE_MUTEX(register_mutex);
0029 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0030 static LIST_HEAD(snd_pcm_notify_list);
0031 #endif
0032
0033 static int snd_pcm_free(struct snd_pcm *pcm);
0034 static int snd_pcm_dev_free(struct snd_device *device);
0035 static int snd_pcm_dev_register(struct snd_device *device);
0036 static int snd_pcm_dev_disconnect(struct snd_device *device);
0037
0038 static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
0039 {
0040 struct snd_pcm *pcm;
0041
0042 list_for_each_entry(pcm, &snd_pcm_devices, list) {
0043 if (pcm->card == card && pcm->device == device)
0044 return pcm;
0045 }
0046 return NULL;
0047 }
0048
0049 static int snd_pcm_next(struct snd_card *card, int device)
0050 {
0051 struct snd_pcm *pcm;
0052
0053 list_for_each_entry(pcm, &snd_pcm_devices, list) {
0054 if (pcm->card == card && pcm->device > device)
0055 return pcm->device;
0056 else if (pcm->card->number > card->number)
0057 return -1;
0058 }
0059 return -1;
0060 }
0061
0062 static int snd_pcm_add(struct snd_pcm *newpcm)
0063 {
0064 struct snd_pcm *pcm;
0065
0066 if (newpcm->internal)
0067 return 0;
0068
0069 list_for_each_entry(pcm, &snd_pcm_devices, list) {
0070 if (pcm->card == newpcm->card && pcm->device == newpcm->device)
0071 return -EBUSY;
0072 if (pcm->card->number > newpcm->card->number ||
0073 (pcm->card == newpcm->card &&
0074 pcm->device > newpcm->device)) {
0075 list_add(&newpcm->list, pcm->list.prev);
0076 return 0;
0077 }
0078 }
0079 list_add_tail(&newpcm->list, &snd_pcm_devices);
0080 return 0;
0081 }
0082
0083 static int snd_pcm_control_ioctl(struct snd_card *card,
0084 struct snd_ctl_file *control,
0085 unsigned int cmd, unsigned long arg)
0086 {
0087 switch (cmd) {
0088 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
0089 {
0090 int device;
0091
0092 if (get_user(device, (int __user *)arg))
0093 return -EFAULT;
0094 mutex_lock(®ister_mutex);
0095 device = snd_pcm_next(card, device);
0096 mutex_unlock(®ister_mutex);
0097 if (put_user(device, (int __user *)arg))
0098 return -EFAULT;
0099 return 0;
0100 }
0101 case SNDRV_CTL_IOCTL_PCM_INFO:
0102 {
0103 struct snd_pcm_info __user *info;
0104 unsigned int device, subdevice;
0105 int stream;
0106 struct snd_pcm *pcm;
0107 struct snd_pcm_str *pstr;
0108 struct snd_pcm_substream *substream;
0109 int err;
0110
0111 info = (struct snd_pcm_info __user *)arg;
0112 if (get_user(device, &info->device))
0113 return -EFAULT;
0114 if (get_user(stream, &info->stream))
0115 return -EFAULT;
0116 if (stream < 0 || stream > 1)
0117 return -EINVAL;
0118 stream = array_index_nospec(stream, 2);
0119 if (get_user(subdevice, &info->subdevice))
0120 return -EFAULT;
0121 mutex_lock(®ister_mutex);
0122 pcm = snd_pcm_get(card, device);
0123 if (pcm == NULL) {
0124 err = -ENXIO;
0125 goto _error;
0126 }
0127 pstr = &pcm->streams[stream];
0128 if (pstr->substream_count == 0) {
0129 err = -ENOENT;
0130 goto _error;
0131 }
0132 if (subdevice >= pstr->substream_count) {
0133 err = -ENXIO;
0134 goto _error;
0135 }
0136 for (substream = pstr->substream; substream;
0137 substream = substream->next)
0138 if (substream->number == (int)subdevice)
0139 break;
0140 if (substream == NULL) {
0141 err = -ENXIO;
0142 goto _error;
0143 }
0144 mutex_lock(&pcm->open_mutex);
0145 err = snd_pcm_info_user(substream, info);
0146 mutex_unlock(&pcm->open_mutex);
0147 _error:
0148 mutex_unlock(®ister_mutex);
0149 return err;
0150 }
0151 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
0152 {
0153 int val;
0154
0155 if (get_user(val, (int __user *)arg))
0156 return -EFAULT;
0157 control->preferred_subdevice[SND_CTL_SUBDEV_PCM] = val;
0158 return 0;
0159 }
0160 }
0161 return -ENOIOCTLCMD;
0162 }
0163
0164 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
0165
0166 static const char * const snd_pcm_format_names[] = {
0167 FORMAT(S8),
0168 FORMAT(U8),
0169 FORMAT(S16_LE),
0170 FORMAT(S16_BE),
0171 FORMAT(U16_LE),
0172 FORMAT(U16_BE),
0173 FORMAT(S24_LE),
0174 FORMAT(S24_BE),
0175 FORMAT(U24_LE),
0176 FORMAT(U24_BE),
0177 FORMAT(S32_LE),
0178 FORMAT(S32_BE),
0179 FORMAT(U32_LE),
0180 FORMAT(U32_BE),
0181 FORMAT(FLOAT_LE),
0182 FORMAT(FLOAT_BE),
0183 FORMAT(FLOAT64_LE),
0184 FORMAT(FLOAT64_BE),
0185 FORMAT(IEC958_SUBFRAME_LE),
0186 FORMAT(IEC958_SUBFRAME_BE),
0187 FORMAT(MU_LAW),
0188 FORMAT(A_LAW),
0189 FORMAT(IMA_ADPCM),
0190 FORMAT(MPEG),
0191 FORMAT(GSM),
0192 FORMAT(SPECIAL),
0193 FORMAT(S24_3LE),
0194 FORMAT(S24_3BE),
0195 FORMAT(U24_3LE),
0196 FORMAT(U24_3BE),
0197 FORMAT(S20_3LE),
0198 FORMAT(S20_3BE),
0199 FORMAT(U20_3LE),
0200 FORMAT(U20_3BE),
0201 FORMAT(S18_3LE),
0202 FORMAT(S18_3BE),
0203 FORMAT(U18_3LE),
0204 FORMAT(U18_3BE),
0205 FORMAT(G723_24),
0206 FORMAT(G723_24_1B),
0207 FORMAT(G723_40),
0208 FORMAT(G723_40_1B),
0209 FORMAT(DSD_U8),
0210 FORMAT(DSD_U16_LE),
0211 FORMAT(DSD_U32_LE),
0212 FORMAT(DSD_U16_BE),
0213 FORMAT(DSD_U32_BE),
0214 };
0215
0216
0217
0218
0219
0220
0221
0222 const char *snd_pcm_format_name(snd_pcm_format_t format)
0223 {
0224 if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
0225 return "Unknown";
0226 return snd_pcm_format_names[(__force unsigned int)format];
0227 }
0228 EXPORT_SYMBOL_GPL(snd_pcm_format_name);
0229
0230 #ifdef CONFIG_SND_VERBOSE_PROCFS
0231
0232 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
0233 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
0234 #define READY(v) [SNDRV_PCM_READY_##v] = #v
0235 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
0236 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
0237 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
0238 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
0239 #define START(v) [SNDRV_PCM_START_##v] = #v
0240 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
0241
0242 static const char * const snd_pcm_stream_names[] = {
0243 STREAM(PLAYBACK),
0244 STREAM(CAPTURE),
0245 };
0246
0247 static const char * const snd_pcm_state_names[] = {
0248 STATE(OPEN),
0249 STATE(SETUP),
0250 STATE(PREPARED),
0251 STATE(RUNNING),
0252 STATE(XRUN),
0253 STATE(DRAINING),
0254 STATE(PAUSED),
0255 STATE(SUSPENDED),
0256 };
0257
0258 static const char * const snd_pcm_access_names[] = {
0259 ACCESS(MMAP_INTERLEAVED),
0260 ACCESS(MMAP_NONINTERLEAVED),
0261 ACCESS(MMAP_COMPLEX),
0262 ACCESS(RW_INTERLEAVED),
0263 ACCESS(RW_NONINTERLEAVED),
0264 };
0265
0266 static const char * const snd_pcm_subformat_names[] = {
0267 SUBFORMAT(STD),
0268 };
0269
0270 static const char * const snd_pcm_tstamp_mode_names[] = {
0271 TSTAMP(NONE),
0272 TSTAMP(ENABLE),
0273 };
0274
0275 static const char *snd_pcm_stream_name(int stream)
0276 {
0277 return snd_pcm_stream_names[stream];
0278 }
0279
0280 static const char *snd_pcm_access_name(snd_pcm_access_t access)
0281 {
0282 return snd_pcm_access_names[(__force int)access];
0283 }
0284
0285 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
0286 {
0287 return snd_pcm_subformat_names[(__force int)subformat];
0288 }
0289
0290 static const char *snd_pcm_tstamp_mode_name(int mode)
0291 {
0292 return snd_pcm_tstamp_mode_names[mode];
0293 }
0294
0295 static const char *snd_pcm_state_name(snd_pcm_state_t state)
0296 {
0297 return snd_pcm_state_names[(__force int)state];
0298 }
0299
0300 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0301 #include <linux/soundcard.h>
0302
0303 static const char *snd_pcm_oss_format_name(int format)
0304 {
0305 switch (format) {
0306 case AFMT_MU_LAW:
0307 return "MU_LAW";
0308 case AFMT_A_LAW:
0309 return "A_LAW";
0310 case AFMT_IMA_ADPCM:
0311 return "IMA_ADPCM";
0312 case AFMT_U8:
0313 return "U8";
0314 case AFMT_S16_LE:
0315 return "S16_LE";
0316 case AFMT_S16_BE:
0317 return "S16_BE";
0318 case AFMT_S8:
0319 return "S8";
0320 case AFMT_U16_LE:
0321 return "U16_LE";
0322 case AFMT_U16_BE:
0323 return "U16_BE";
0324 case AFMT_MPEG:
0325 return "MPEG";
0326 default:
0327 return "unknown";
0328 }
0329 }
0330 #endif
0331
0332 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
0333 struct snd_info_buffer *buffer)
0334 {
0335 struct snd_pcm_info *info;
0336 int err;
0337
0338 if (! substream)
0339 return;
0340
0341 info = kmalloc(sizeof(*info), GFP_KERNEL);
0342 if (!info)
0343 return;
0344
0345 err = snd_pcm_info(substream, info);
0346 if (err < 0) {
0347 snd_iprintf(buffer, "error %d\n", err);
0348 kfree(info);
0349 return;
0350 }
0351 snd_iprintf(buffer, "card: %d\n", info->card);
0352 snd_iprintf(buffer, "device: %d\n", info->device);
0353 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
0354 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
0355 snd_iprintf(buffer, "id: %s\n", info->id);
0356 snd_iprintf(buffer, "name: %s\n", info->name);
0357 snd_iprintf(buffer, "subname: %s\n", info->subname);
0358 snd_iprintf(buffer, "class: %d\n", info->dev_class);
0359 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
0360 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
0361 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
0362 kfree(info);
0363 }
0364
0365 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
0366 struct snd_info_buffer *buffer)
0367 {
0368 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
0369 buffer);
0370 }
0371
0372 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
0373 struct snd_info_buffer *buffer)
0374 {
0375 snd_pcm_proc_info_read(entry->private_data, buffer);
0376 }
0377
0378 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
0379 struct snd_info_buffer *buffer)
0380 {
0381 struct snd_pcm_substream *substream = entry->private_data;
0382 struct snd_pcm_runtime *runtime;
0383
0384 mutex_lock(&substream->pcm->open_mutex);
0385 runtime = substream->runtime;
0386 if (!runtime) {
0387 snd_iprintf(buffer, "closed\n");
0388 goto unlock;
0389 }
0390 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
0391 snd_iprintf(buffer, "no setup\n");
0392 goto unlock;
0393 }
0394 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
0395 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
0396 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
0397 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
0398 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
0399 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
0400 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
0401 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0402 if (substream->oss.oss) {
0403 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
0404 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
0405 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
0406 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
0407 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
0408 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
0409 }
0410 #endif
0411 unlock:
0412 mutex_unlock(&substream->pcm->open_mutex);
0413 }
0414
0415 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
0416 struct snd_info_buffer *buffer)
0417 {
0418 struct snd_pcm_substream *substream = entry->private_data;
0419 struct snd_pcm_runtime *runtime;
0420
0421 mutex_lock(&substream->pcm->open_mutex);
0422 runtime = substream->runtime;
0423 if (!runtime) {
0424 snd_iprintf(buffer, "closed\n");
0425 goto unlock;
0426 }
0427 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
0428 snd_iprintf(buffer, "no setup\n");
0429 goto unlock;
0430 }
0431 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
0432 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
0433 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
0434 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
0435 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
0436 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
0437 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
0438 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
0439 unlock:
0440 mutex_unlock(&substream->pcm->open_mutex);
0441 }
0442
0443 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
0444 struct snd_info_buffer *buffer)
0445 {
0446 struct snd_pcm_substream *substream = entry->private_data;
0447 struct snd_pcm_runtime *runtime;
0448 struct snd_pcm_status64 status;
0449 int err;
0450
0451 mutex_lock(&substream->pcm->open_mutex);
0452 runtime = substream->runtime;
0453 if (!runtime) {
0454 snd_iprintf(buffer, "closed\n");
0455 goto unlock;
0456 }
0457 memset(&status, 0, sizeof(status));
0458 err = snd_pcm_status64(substream, &status);
0459 if (err < 0) {
0460 snd_iprintf(buffer, "error %d\n", err);
0461 goto unlock;
0462 }
0463 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
0464 snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
0465 snd_iprintf(buffer, "trigger_time: %lld.%09lld\n",
0466 status.trigger_tstamp_sec, status.trigger_tstamp_nsec);
0467 snd_iprintf(buffer, "tstamp : %lld.%09lld\n",
0468 status.tstamp_sec, status.tstamp_nsec);
0469 snd_iprintf(buffer, "delay : %ld\n", status.delay);
0470 snd_iprintf(buffer, "avail : %ld\n", status.avail);
0471 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
0472 snd_iprintf(buffer, "-----\n");
0473 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
0474 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
0475 unlock:
0476 mutex_unlock(&substream->pcm->open_mutex);
0477 }
0478
0479 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
0480 static void snd_pcm_xrun_injection_write(struct snd_info_entry *entry,
0481 struct snd_info_buffer *buffer)
0482 {
0483 struct snd_pcm_substream *substream = entry->private_data;
0484
0485 snd_pcm_stop_xrun(substream);
0486 }
0487
0488 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
0489 struct snd_info_buffer *buffer)
0490 {
0491 struct snd_pcm_str *pstr = entry->private_data;
0492 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
0493 }
0494
0495 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
0496 struct snd_info_buffer *buffer)
0497 {
0498 struct snd_pcm_str *pstr = entry->private_data;
0499 char line[64];
0500 if (!snd_info_get_line(buffer, line, sizeof(line)))
0501 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
0502 }
0503 #endif
0504
0505 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
0506 {
0507 struct snd_pcm *pcm = pstr->pcm;
0508 struct snd_info_entry *entry;
0509 char name[16];
0510
0511 sprintf(name, "pcm%i%c", pcm->device,
0512 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
0513 entry = snd_info_create_card_entry(pcm->card, name,
0514 pcm->card->proc_root);
0515 if (!entry)
0516 return -ENOMEM;
0517 entry->mode = S_IFDIR | 0555;
0518 pstr->proc_root = entry;
0519 entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root);
0520 if (entry)
0521 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
0522 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
0523 entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
0524 pstr->proc_root);
0525 if (entry) {
0526 snd_info_set_text_ops(entry, pstr, snd_pcm_xrun_debug_read);
0527 entry->c.text.write = snd_pcm_xrun_debug_write;
0528 entry->mode |= 0200;
0529 }
0530 #endif
0531 return 0;
0532 }
0533
0534 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
0535 {
0536 snd_info_free_entry(pstr->proc_root);
0537 pstr->proc_root = NULL;
0538 return 0;
0539 }
0540
0541 static struct snd_info_entry *
0542 create_substream_info_entry(struct snd_pcm_substream *substream,
0543 const char *name,
0544 void (*read)(struct snd_info_entry *,
0545 struct snd_info_buffer *))
0546 {
0547 struct snd_info_entry *entry;
0548
0549 entry = snd_info_create_card_entry(substream->pcm->card, name,
0550 substream->proc_root);
0551 if (entry)
0552 snd_info_set_text_ops(entry, substream, read);
0553 return entry;
0554 }
0555
0556 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
0557 {
0558 struct snd_info_entry *entry;
0559 struct snd_card *card;
0560 char name[16];
0561
0562 card = substream->pcm->card;
0563
0564 sprintf(name, "sub%i", substream->number);
0565 entry = snd_info_create_card_entry(card, name,
0566 substream->pstr->proc_root);
0567 if (!entry)
0568 return -ENOMEM;
0569 entry->mode = S_IFDIR | 0555;
0570 substream->proc_root = entry;
0571
0572 create_substream_info_entry(substream, "info",
0573 snd_pcm_substream_proc_info_read);
0574 create_substream_info_entry(substream, "hw_params",
0575 snd_pcm_substream_proc_hw_params_read);
0576 create_substream_info_entry(substream, "sw_params",
0577 snd_pcm_substream_proc_sw_params_read);
0578 create_substream_info_entry(substream, "status",
0579 snd_pcm_substream_proc_status_read);
0580
0581 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
0582 entry = create_substream_info_entry(substream, "xrun_injection", NULL);
0583 if (entry) {
0584 entry->c.text.write = snd_pcm_xrun_injection_write;
0585 entry->mode = S_IFREG | 0200;
0586 }
0587 #endif
0588
0589 return 0;
0590 }
0591
0592 #else
0593 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
0594 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
0595 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
0596 #endif
0597
0598 static const struct attribute_group *pcm_dev_attr_groups[];
0599
0600
0601
0602
0603
0604 #ifdef CONFIG_PM_SLEEP
0605 static int do_pcm_suspend(struct device *dev)
0606 {
0607 struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
0608
0609 if (!pstr->pcm->no_device_suspend)
0610 snd_pcm_suspend_all(pstr->pcm);
0611 return 0;
0612 }
0613 #endif
0614
0615 static const struct dev_pm_ops pcm_dev_pm_ops = {
0616 SET_SYSTEM_SLEEP_PM_OPS(do_pcm_suspend, NULL)
0617 };
0618
0619
0620 static const struct device_type pcm_dev_type = {
0621 .name = "pcm",
0622 .pm = &pcm_dev_pm_ops,
0623 };
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
0639 {
0640 int idx, err;
0641 struct snd_pcm_str *pstr = &pcm->streams[stream];
0642 struct snd_pcm_substream *substream, *prev;
0643
0644 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0645 mutex_init(&pstr->oss.setup_mutex);
0646 #endif
0647 pstr->stream = stream;
0648 pstr->pcm = pcm;
0649 pstr->substream_count = substream_count;
0650 if (!substream_count)
0651 return 0;
0652
0653 snd_device_initialize(&pstr->dev, pcm->card);
0654 pstr->dev.groups = pcm_dev_attr_groups;
0655 pstr->dev.type = &pcm_dev_type;
0656 dev_set_name(&pstr->dev, "pcmC%iD%i%c", pcm->card->number, pcm->device,
0657 stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
0658
0659 if (!pcm->internal) {
0660 err = snd_pcm_stream_proc_init(pstr);
0661 if (err < 0) {
0662 pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");
0663 return err;
0664 }
0665 }
0666 prev = NULL;
0667 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
0668 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
0669 if (!substream)
0670 return -ENOMEM;
0671 substream->pcm = pcm;
0672 substream->pstr = pstr;
0673 substream->number = idx;
0674 substream->stream = stream;
0675 sprintf(substream->name, "subdevice #%i", idx);
0676 substream->buffer_bytes_max = UINT_MAX;
0677 if (prev == NULL)
0678 pstr->substream = substream;
0679 else
0680 prev->next = substream;
0681
0682 if (!pcm->internal) {
0683 err = snd_pcm_substream_proc_init(substream);
0684 if (err < 0) {
0685 pcm_err(pcm,
0686 "Error in snd_pcm_stream_proc_init\n");
0687 if (prev == NULL)
0688 pstr->substream = NULL;
0689 else
0690 prev->next = NULL;
0691 kfree(substream);
0692 return err;
0693 }
0694 }
0695 substream->group = &substream->self_group;
0696 snd_pcm_group_init(&substream->self_group);
0697 list_add_tail(&substream->link_list, &substream->self_group.substreams);
0698 atomic_set(&substream->mmap_count, 0);
0699 prev = substream;
0700 }
0701 return 0;
0702 }
0703 EXPORT_SYMBOL(snd_pcm_new_stream);
0704
0705 static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
0706 int playback_count, int capture_count, bool internal,
0707 struct snd_pcm **rpcm)
0708 {
0709 struct snd_pcm *pcm;
0710 int err;
0711 static const struct snd_device_ops ops = {
0712 .dev_free = snd_pcm_dev_free,
0713 .dev_register = snd_pcm_dev_register,
0714 .dev_disconnect = snd_pcm_dev_disconnect,
0715 };
0716 static const struct snd_device_ops internal_ops = {
0717 .dev_free = snd_pcm_dev_free,
0718 };
0719
0720 if (snd_BUG_ON(!card))
0721 return -ENXIO;
0722 if (rpcm)
0723 *rpcm = NULL;
0724 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
0725 if (!pcm)
0726 return -ENOMEM;
0727 pcm->card = card;
0728 pcm->device = device;
0729 pcm->internal = internal;
0730 mutex_init(&pcm->open_mutex);
0731 init_waitqueue_head(&pcm->open_wait);
0732 INIT_LIST_HEAD(&pcm->list);
0733 if (id)
0734 strscpy(pcm->id, id, sizeof(pcm->id));
0735
0736 err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
0737 playback_count);
0738 if (err < 0)
0739 goto free_pcm;
0740
0741 err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
0742 if (err < 0)
0743 goto free_pcm;
0744
0745 err = snd_device_new(card, SNDRV_DEV_PCM, pcm,
0746 internal ? &internal_ops : &ops);
0747 if (err < 0)
0748 goto free_pcm;
0749
0750 if (rpcm)
0751 *rpcm = pcm;
0752 return 0;
0753
0754 free_pcm:
0755 snd_pcm_free(pcm);
0756 return err;
0757 }
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775 int snd_pcm_new(struct snd_card *card, const char *id, int device,
0776 int playback_count, int capture_count, struct snd_pcm **rpcm)
0777 {
0778 return _snd_pcm_new(card, id, device, playback_count, capture_count,
0779 false, rpcm);
0780 }
0781 EXPORT_SYMBOL(snd_pcm_new);
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803 int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
0804 int playback_count, int capture_count,
0805 struct snd_pcm **rpcm)
0806 {
0807 return _snd_pcm_new(card, id, device, playback_count, capture_count,
0808 true, rpcm);
0809 }
0810 EXPORT_SYMBOL(snd_pcm_new_internal);
0811
0812 static void free_chmap(struct snd_pcm_str *pstr)
0813 {
0814 if (pstr->chmap_kctl) {
0815 struct snd_card *card = pstr->pcm->card;
0816
0817 down_write(&card->controls_rwsem);
0818 snd_ctl_remove(card, pstr->chmap_kctl);
0819 up_write(&card->controls_rwsem);
0820 pstr->chmap_kctl = NULL;
0821 }
0822 }
0823
0824 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
0825 {
0826 struct snd_pcm_substream *substream, *substream_next;
0827 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0828 struct snd_pcm_oss_setup *setup, *setupn;
0829 #endif
0830
0831
0832 snd_pcm_stream_proc_done(pstr);
0833
0834 substream = pstr->substream;
0835 while (substream) {
0836 substream_next = substream->next;
0837 snd_pcm_timer_done(substream);
0838 kfree(substream);
0839 substream = substream_next;
0840 }
0841 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0842 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
0843 setupn = setup->next;
0844 kfree(setup->task_name);
0845 kfree(setup);
0846 }
0847 #endif
0848 free_chmap(pstr);
0849 if (pstr->substream_count)
0850 put_device(&pstr->dev);
0851 }
0852
0853 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
0854 #define pcm_call_notify(pcm, call) \
0855 do { \
0856 struct snd_pcm_notify *_notify; \
0857 list_for_each_entry(_notify, &snd_pcm_notify_list, list) \
0858 _notify->call(pcm); \
0859 } while (0)
0860 #else
0861 #define pcm_call_notify(pcm, call) do {} while (0)
0862 #endif
0863
0864 static int snd_pcm_free(struct snd_pcm *pcm)
0865 {
0866 if (!pcm)
0867 return 0;
0868 if (!pcm->internal)
0869 pcm_call_notify(pcm, n_unregister);
0870 if (pcm->private_free)
0871 pcm->private_free(pcm);
0872 snd_pcm_lib_preallocate_free_for_all(pcm);
0873 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
0874 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
0875 kfree(pcm);
0876 return 0;
0877 }
0878
0879 static int snd_pcm_dev_free(struct snd_device *device)
0880 {
0881 struct snd_pcm *pcm = device->device_data;
0882 return snd_pcm_free(pcm);
0883 }
0884
0885 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
0886 struct file *file,
0887 struct snd_pcm_substream **rsubstream)
0888 {
0889 struct snd_pcm_str * pstr;
0890 struct snd_pcm_substream *substream;
0891 struct snd_pcm_runtime *runtime;
0892 struct snd_card *card;
0893 int prefer_subdevice;
0894 size_t size;
0895
0896 if (snd_BUG_ON(!pcm || !rsubstream))
0897 return -ENXIO;
0898 if (snd_BUG_ON(stream != SNDRV_PCM_STREAM_PLAYBACK &&
0899 stream != SNDRV_PCM_STREAM_CAPTURE))
0900 return -EINVAL;
0901 *rsubstream = NULL;
0902 pstr = &pcm->streams[stream];
0903 if (pstr->substream == NULL || pstr->substream_count == 0)
0904 return -ENODEV;
0905
0906 card = pcm->card;
0907 prefer_subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_PCM);
0908
0909 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
0910 int opposite = !stream;
0911
0912 for (substream = pcm->streams[opposite].substream; substream;
0913 substream = substream->next) {
0914 if (SUBSTREAM_BUSY(substream))
0915 return -EAGAIN;
0916 }
0917 }
0918
0919 if (file->f_flags & O_APPEND) {
0920 if (prefer_subdevice < 0) {
0921 if (pstr->substream_count > 1)
0922 return -EINVAL;
0923 substream = pstr->substream;
0924 } else {
0925 for (substream = pstr->substream; substream;
0926 substream = substream->next)
0927 if (substream->number == prefer_subdevice)
0928 break;
0929 }
0930 if (! substream)
0931 return -ENODEV;
0932 if (! SUBSTREAM_BUSY(substream))
0933 return -EBADFD;
0934 substream->ref_count++;
0935 *rsubstream = substream;
0936 return 0;
0937 }
0938
0939 for (substream = pstr->substream; substream; substream = substream->next) {
0940 if (!SUBSTREAM_BUSY(substream) &&
0941 (prefer_subdevice == -1 ||
0942 substream->number == prefer_subdevice))
0943 break;
0944 }
0945 if (substream == NULL)
0946 return -EAGAIN;
0947
0948 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
0949 if (runtime == NULL)
0950 return -ENOMEM;
0951
0952 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
0953 runtime->status = alloc_pages_exact(size, GFP_KERNEL);
0954 if (runtime->status == NULL) {
0955 kfree(runtime);
0956 return -ENOMEM;
0957 }
0958 memset(runtime->status, 0, size);
0959
0960 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
0961 runtime->control = alloc_pages_exact(size, GFP_KERNEL);
0962 if (runtime->control == NULL) {
0963 free_pages_exact(runtime->status,
0964 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
0965 kfree(runtime);
0966 return -ENOMEM;
0967 }
0968 memset(runtime->control, 0, size);
0969
0970 init_waitqueue_head(&runtime->sleep);
0971 init_waitqueue_head(&runtime->tsleep);
0972
0973 runtime->status->state = SNDRV_PCM_STATE_OPEN;
0974 mutex_init(&runtime->buffer_mutex);
0975 atomic_set(&runtime->buffer_accessing, 0);
0976
0977 substream->runtime = runtime;
0978 substream->private_data = pcm->private_data;
0979 substream->ref_count = 1;
0980 substream->f_flags = file->f_flags;
0981 substream->pid = get_pid(task_pid(current));
0982 pstr->substream_opened++;
0983 *rsubstream = substream;
0984 return 0;
0985 }
0986
0987 void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
0988 {
0989 struct snd_pcm_runtime *runtime;
0990
0991 if (PCM_RUNTIME_CHECK(substream))
0992 return;
0993 runtime = substream->runtime;
0994 if (runtime->private_free != NULL)
0995 runtime->private_free(runtime);
0996 free_pages_exact(runtime->status,
0997 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
0998 free_pages_exact(runtime->control,
0999 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
1000 kfree(runtime->hw_constraints.rules);
1001
1002 if (substream->timer) {
1003 spin_lock_irq(&substream->timer->lock);
1004 substream->runtime = NULL;
1005 spin_unlock_irq(&substream->timer->lock);
1006 } else {
1007 substream->runtime = NULL;
1008 }
1009 mutex_destroy(&runtime->buffer_mutex);
1010 snd_fasync_free(runtime->fasync);
1011 kfree(runtime);
1012 put_pid(substream->pid);
1013 substream->pid = NULL;
1014 substream->pstr->substream_opened--;
1015 }
1016
1017 static ssize_t pcm_class_show(struct device *dev,
1018 struct device_attribute *attr, char *buf)
1019 {
1020 struct snd_pcm_str *pstr = container_of(dev, struct snd_pcm_str, dev);
1021 struct snd_pcm *pcm = pstr->pcm;
1022 const char *str;
1023 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1024 [SNDRV_PCM_CLASS_GENERIC] = "generic",
1025 [SNDRV_PCM_CLASS_MULTI] = "multi",
1026 [SNDRV_PCM_CLASS_MODEM] = "modem",
1027 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1028 };
1029
1030 if (pcm->dev_class > SNDRV_PCM_CLASS_LAST)
1031 str = "none";
1032 else
1033 str = strs[pcm->dev_class];
1034 return sysfs_emit(buf, "%s\n", str);
1035 }
1036
1037 static DEVICE_ATTR_RO(pcm_class);
1038 static struct attribute *pcm_dev_attrs[] = {
1039 &dev_attr_pcm_class.attr,
1040 NULL
1041 };
1042
1043 static const struct attribute_group pcm_dev_attr_group = {
1044 .attrs = pcm_dev_attrs,
1045 };
1046
1047 static const struct attribute_group *pcm_dev_attr_groups[] = {
1048 &pcm_dev_attr_group,
1049 NULL
1050 };
1051
1052 static int snd_pcm_dev_register(struct snd_device *device)
1053 {
1054 int cidx, err;
1055 struct snd_pcm_substream *substream;
1056 struct snd_pcm *pcm;
1057
1058 if (snd_BUG_ON(!device || !device->device_data))
1059 return -ENXIO;
1060 pcm = device->device_data;
1061
1062 mutex_lock(®ister_mutex);
1063 err = snd_pcm_add(pcm);
1064 if (err)
1065 goto unlock;
1066 for (cidx = 0; cidx < 2; cidx++) {
1067 int devtype = -1;
1068 if (pcm->streams[cidx].substream == NULL)
1069 continue;
1070 switch (cidx) {
1071 case SNDRV_PCM_STREAM_PLAYBACK:
1072 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1073 break;
1074 case SNDRV_PCM_STREAM_CAPTURE:
1075 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1076 break;
1077 }
1078
1079 err = snd_register_device(devtype, pcm->card, pcm->device,
1080 &snd_pcm_f_ops[cidx], pcm,
1081 &pcm->streams[cidx].dev);
1082 if (err < 0) {
1083 list_del_init(&pcm->list);
1084 goto unlock;
1085 }
1086
1087 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1088 snd_pcm_timer_init(substream);
1089 }
1090
1091 pcm_call_notify(pcm, n_register);
1092
1093 unlock:
1094 mutex_unlock(®ister_mutex);
1095 return err;
1096 }
1097
1098 static int snd_pcm_dev_disconnect(struct snd_device *device)
1099 {
1100 struct snd_pcm *pcm = device->device_data;
1101 struct snd_pcm_substream *substream;
1102 int cidx;
1103
1104 mutex_lock(®ister_mutex);
1105 mutex_lock(&pcm->open_mutex);
1106 wake_up(&pcm->open_wait);
1107 list_del_init(&pcm->list);
1108
1109 for_each_pcm_substream(pcm, cidx, substream) {
1110 snd_pcm_stream_lock_irq(substream);
1111 if (substream->runtime) {
1112 if (snd_pcm_running(substream))
1113 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
1114
1115 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
1116 wake_up(&substream->runtime->sleep);
1117 wake_up(&substream->runtime->tsleep);
1118 }
1119 snd_pcm_stream_unlock_irq(substream);
1120 }
1121
1122 for_each_pcm_substream(pcm, cidx, substream)
1123 snd_pcm_sync_stop(substream, false);
1124
1125 pcm_call_notify(pcm, n_disconnect);
1126 for (cidx = 0; cidx < 2; cidx++) {
1127 snd_unregister_device(&pcm->streams[cidx].dev);
1128 free_chmap(&pcm->streams[cidx]);
1129 }
1130 mutex_unlock(&pcm->open_mutex);
1131 mutex_unlock(®ister_mutex);
1132 return 0;
1133 }
1134
1135 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1148 {
1149 struct snd_pcm *pcm;
1150
1151 if (snd_BUG_ON(!notify ||
1152 !notify->n_register ||
1153 !notify->n_unregister ||
1154 !notify->n_disconnect))
1155 return -EINVAL;
1156 mutex_lock(®ister_mutex);
1157 if (nfree) {
1158 list_del(¬ify->list);
1159 list_for_each_entry(pcm, &snd_pcm_devices, list)
1160 notify->n_unregister(pcm);
1161 } else {
1162 list_add_tail(¬ify->list, &snd_pcm_notify_list);
1163 list_for_each_entry(pcm, &snd_pcm_devices, list)
1164 notify->n_register(pcm);
1165 }
1166 mutex_unlock(®ister_mutex);
1167 return 0;
1168 }
1169 EXPORT_SYMBOL(snd_pcm_notify);
1170 #endif
1171
1172 #ifdef CONFIG_SND_PROC_FS
1173
1174
1175
1176
1177 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1178 struct snd_info_buffer *buffer)
1179 {
1180 struct snd_pcm *pcm;
1181
1182 mutex_lock(®ister_mutex);
1183 list_for_each_entry(pcm, &snd_pcm_devices, list) {
1184 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1185 pcm->card->number, pcm->device, pcm->id, pcm->name);
1186 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1187 snd_iprintf(buffer, " : playback %i",
1188 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1189 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1190 snd_iprintf(buffer, " : capture %i",
1191 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1192 snd_iprintf(buffer, "\n");
1193 }
1194 mutex_unlock(®ister_mutex);
1195 }
1196
1197 static struct snd_info_entry *snd_pcm_proc_entry;
1198
1199 static void snd_pcm_proc_init(void)
1200 {
1201 struct snd_info_entry *entry;
1202
1203 entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL);
1204 if (entry) {
1205 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1206 if (snd_info_register(entry) < 0) {
1207 snd_info_free_entry(entry);
1208 entry = NULL;
1209 }
1210 }
1211 snd_pcm_proc_entry = entry;
1212 }
1213
1214 static void snd_pcm_proc_done(void)
1215 {
1216 snd_info_free_entry(snd_pcm_proc_entry);
1217 }
1218
1219 #else
1220 #define snd_pcm_proc_init()
1221 #define snd_pcm_proc_done()
1222 #endif
1223
1224
1225
1226
1227
1228
1229 static int __init alsa_pcm_init(void)
1230 {
1231 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1232 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1233 snd_pcm_proc_init();
1234 return 0;
1235 }
1236
1237 static void __exit alsa_pcm_exit(void)
1238 {
1239 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1240 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1241 snd_pcm_proc_done();
1242 }
1243
1244 module_init(alsa_pcm_init)
1245 module_exit(alsa_pcm_exit)