[PATCH] jbd: journal_dirty_data re-check for unmapped buffers
[linux-2.6/mini2440.git] / sound / core / pcm.c
blobfbbbcd20c4cc7d6001163631dbfa15b55900e56d
1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/pcm.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
34 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
35 MODULE_LICENSE("GPL");
37 static LIST_HEAD(snd_pcm_devices);
38 static LIST_HEAD(snd_pcm_notify_list);
39 static DEFINE_MUTEX(register_mutex);
41 static int snd_pcm_free(struct snd_pcm *pcm);
42 static int snd_pcm_dev_free(struct snd_device *device);
43 static int snd_pcm_dev_register(struct snd_device *device);
44 static int snd_pcm_dev_disconnect(struct snd_device *device);
46 static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
48 struct list_head *p;
49 struct snd_pcm *pcm;
51 list_for_each(p, &snd_pcm_devices) {
52 pcm = list_entry(p, struct snd_pcm, list);
53 if (pcm->card == card && pcm->device == device)
54 return pcm;
56 return NULL;
59 static int snd_pcm_control_ioctl(struct snd_card *card,
60 struct snd_ctl_file *control,
61 unsigned int cmd, unsigned long arg)
63 switch (cmd) {
64 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
66 int device;
68 if (get_user(device, (int __user *)arg))
69 return -EFAULT;
70 mutex_lock(&register_mutex);
71 device = device < 0 ? 0 : device + 1;
72 while (device < SNDRV_PCM_DEVICES) {
73 if (snd_pcm_search(card, device))
74 break;
75 device++;
77 if (device == SNDRV_PCM_DEVICES)
78 device = -1;
79 mutex_unlock(&register_mutex);
80 if (put_user(device, (int __user *)arg))
81 return -EFAULT;
82 return 0;
84 case SNDRV_CTL_IOCTL_PCM_INFO:
86 struct snd_pcm_info __user *info;
87 unsigned int device, subdevice;
88 int stream;
89 struct snd_pcm *pcm;
90 struct snd_pcm_str *pstr;
91 struct snd_pcm_substream *substream;
92 int err;
94 info = (struct snd_pcm_info __user *)arg;
95 if (get_user(device, &info->device))
96 return -EFAULT;
97 if (get_user(stream, &info->stream))
98 return -EFAULT;
99 if (stream < 0 || stream > 1)
100 return -EINVAL;
101 if (get_user(subdevice, &info->subdevice))
102 return -EFAULT;
103 mutex_lock(&register_mutex);
104 pcm = snd_pcm_search(card, device);
105 if (pcm == NULL) {
106 err = -ENXIO;
107 goto _error;
109 pstr = &pcm->streams[stream];
110 if (pstr->substream_count == 0) {
111 err = -ENOENT;
112 goto _error;
114 if (subdevice >= pstr->substream_count) {
115 err = -ENXIO;
116 goto _error;
118 for (substream = pstr->substream; substream;
119 substream = substream->next)
120 if (substream->number == (int)subdevice)
121 break;
122 if (substream == NULL) {
123 err = -ENXIO;
124 goto _error;
126 err = snd_pcm_info_user(substream, info);
127 _error:
128 mutex_unlock(&register_mutex);
129 return err;
131 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
133 int val;
135 if (get_user(val, (int __user *)arg))
136 return -EFAULT;
137 control->prefer_pcm_subdevice = val;
138 return 0;
141 return -ENOIOCTLCMD;
144 #ifdef CONFIG_SND_VERBOSE_PROCFS
146 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
147 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
148 #define READY(v) [SNDRV_PCM_READY_##v] = #v
149 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
150 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
151 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
152 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
153 #define START(v) [SNDRV_PCM_START_##v] = #v
154 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
155 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
157 static char *snd_pcm_format_names[] = {
158 FORMAT(S8),
159 FORMAT(U8),
160 FORMAT(S16_LE),
161 FORMAT(S16_BE),
162 FORMAT(U16_LE),
163 FORMAT(U16_BE),
164 FORMAT(S24_LE),
165 FORMAT(S24_BE),
166 FORMAT(U24_LE),
167 FORMAT(U24_BE),
168 FORMAT(S32_LE),
169 FORMAT(S32_BE),
170 FORMAT(U32_LE),
171 FORMAT(U32_BE),
172 FORMAT(FLOAT_LE),
173 FORMAT(FLOAT_BE),
174 FORMAT(FLOAT64_LE),
175 FORMAT(FLOAT64_BE),
176 FORMAT(IEC958_SUBFRAME_LE),
177 FORMAT(IEC958_SUBFRAME_BE),
178 FORMAT(MU_LAW),
179 FORMAT(A_LAW),
180 FORMAT(IMA_ADPCM),
181 FORMAT(MPEG),
182 FORMAT(GSM),
183 FORMAT(SPECIAL),
184 FORMAT(S24_3LE),
185 FORMAT(S24_3BE),
186 FORMAT(U24_3LE),
187 FORMAT(U24_3BE),
188 FORMAT(S20_3LE),
189 FORMAT(S20_3BE),
190 FORMAT(U20_3LE),
191 FORMAT(U20_3BE),
192 FORMAT(S18_3LE),
193 FORMAT(S18_3BE),
194 FORMAT(U18_3LE),
195 FORMAT(U18_3BE),
198 static const char *snd_pcm_format_name(snd_pcm_format_t format)
200 return snd_pcm_format_names[format];
203 static char *snd_pcm_stream_names[] = {
204 STREAM(PLAYBACK),
205 STREAM(CAPTURE),
208 static char *snd_pcm_state_names[] = {
209 STATE(OPEN),
210 STATE(SETUP),
211 STATE(PREPARED),
212 STATE(RUNNING),
213 STATE(XRUN),
214 STATE(DRAINING),
215 STATE(PAUSED),
216 STATE(SUSPENDED),
219 static char *snd_pcm_access_names[] = {
220 ACCESS(MMAP_INTERLEAVED),
221 ACCESS(MMAP_NONINTERLEAVED),
222 ACCESS(MMAP_COMPLEX),
223 ACCESS(RW_INTERLEAVED),
224 ACCESS(RW_NONINTERLEAVED),
227 static char *snd_pcm_subformat_names[] = {
228 SUBFORMAT(STD),
231 static char *snd_pcm_tstamp_mode_names[] = {
232 TSTAMP(NONE),
233 TSTAMP(MMAP),
236 static const char *snd_pcm_stream_name(int stream)
238 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
239 return snd_pcm_stream_names[stream];
242 static const char *snd_pcm_access_name(snd_pcm_access_t access)
244 return snd_pcm_access_names[access];
247 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
249 return snd_pcm_subformat_names[subformat];
252 static const char *snd_pcm_tstamp_mode_name(int mode)
254 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
255 return snd_pcm_tstamp_mode_names[mode];
258 static const char *snd_pcm_state_name(snd_pcm_state_t state)
260 return snd_pcm_state_names[state];
263 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
264 #include <linux/soundcard.h>
266 static const char *snd_pcm_oss_format_name(int format)
268 switch (format) {
269 case AFMT_MU_LAW:
270 return "MU_LAW";
271 case AFMT_A_LAW:
272 return "A_LAW";
273 case AFMT_IMA_ADPCM:
274 return "IMA_ADPCM";
275 case AFMT_U8:
276 return "U8";
277 case AFMT_S16_LE:
278 return "S16_LE";
279 case AFMT_S16_BE:
280 return "S16_BE";
281 case AFMT_S8:
282 return "S8";
283 case AFMT_U16_LE:
284 return "U16_LE";
285 case AFMT_U16_BE:
286 return "U16_BE";
287 case AFMT_MPEG:
288 return "MPEG";
289 default:
290 return "unknown";
293 #endif
295 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
296 struct snd_info_buffer *buffer)
298 struct snd_pcm_info *info;
299 int err;
301 if (! substream)
302 return;
304 info = kmalloc(sizeof(*info), GFP_KERNEL);
305 if (! info) {
306 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
307 return;
310 err = snd_pcm_info(substream, info);
311 if (err < 0) {
312 snd_iprintf(buffer, "error %d\n", err);
313 kfree(info);
314 return;
316 snd_iprintf(buffer, "card: %d\n", info->card);
317 snd_iprintf(buffer, "device: %d\n", info->device);
318 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
319 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
320 snd_iprintf(buffer, "id: %s\n", info->id);
321 snd_iprintf(buffer, "name: %s\n", info->name);
322 snd_iprintf(buffer, "subname: %s\n", info->subname);
323 snd_iprintf(buffer, "class: %d\n", info->dev_class);
324 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
325 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
326 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
327 kfree(info);
330 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
331 struct snd_info_buffer *buffer)
333 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
334 buffer);
337 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
338 struct snd_info_buffer *buffer)
340 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
341 buffer);
344 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
345 struct snd_info_buffer *buffer)
347 struct snd_pcm_substream *substream = entry->private_data;
348 struct snd_pcm_runtime *runtime = substream->runtime;
349 if (!runtime) {
350 snd_iprintf(buffer, "closed\n");
351 return;
353 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
354 snd_iprintf(buffer, "no setup\n");
355 return;
357 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
358 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
359 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
360 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
361 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
362 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
363 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
364 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
365 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
366 if (substream->oss.oss) {
367 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
368 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
369 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
370 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
371 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
372 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
374 #endif
377 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
378 struct snd_info_buffer *buffer)
380 struct snd_pcm_substream *substream = entry->private_data;
381 struct snd_pcm_runtime *runtime = substream->runtime;
382 if (!runtime) {
383 snd_iprintf(buffer, "closed\n");
384 return;
386 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
387 snd_iprintf(buffer, "no setup\n");
388 return;
390 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
391 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
392 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
393 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
394 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
395 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
396 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
397 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
398 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
399 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
402 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
403 struct snd_info_buffer *buffer)
405 struct snd_pcm_substream *substream = entry->private_data;
406 struct snd_pcm_runtime *runtime = substream->runtime;
407 struct snd_pcm_status status;
408 int err;
409 if (!runtime) {
410 snd_iprintf(buffer, "closed\n");
411 return;
413 memset(&status, 0, sizeof(status));
414 err = snd_pcm_status(substream, &status);
415 if (err < 0) {
416 snd_iprintf(buffer, "error %d\n", err);
417 return;
419 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
420 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
421 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
422 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
423 status.tstamp.tv_sec, status.tstamp.tv_nsec);
424 snd_iprintf(buffer, "delay : %ld\n", status.delay);
425 snd_iprintf(buffer, "avail : %ld\n", status.avail);
426 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
427 snd_iprintf(buffer, "-----\n");
428 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
429 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
432 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
433 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
434 struct snd_info_buffer *buffer)
436 struct snd_pcm_str *pstr = entry->private_data;
437 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
440 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
441 struct snd_info_buffer *buffer)
443 struct snd_pcm_str *pstr = entry->private_data;
444 char line[64];
445 if (!snd_info_get_line(buffer, line, sizeof(line)))
446 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
448 #endif
450 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
452 struct snd_pcm *pcm = pstr->pcm;
453 struct snd_info_entry *entry;
454 char name[16];
456 sprintf(name, "pcm%i%c", pcm->device,
457 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
458 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
459 return -ENOMEM;
460 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
461 if (snd_info_register(entry) < 0) {
462 snd_info_free_entry(entry);
463 return -ENOMEM;
465 pstr->proc_root = entry;
467 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
468 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
469 if (snd_info_register(entry) < 0) {
470 snd_info_free_entry(entry);
471 entry = NULL;
474 pstr->proc_info_entry = entry;
476 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
477 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
478 pstr->proc_root)) != NULL) {
479 entry->c.text.read = snd_pcm_xrun_debug_read;
480 entry->c.text.write = snd_pcm_xrun_debug_write;
481 entry->mode |= S_IWUSR;
482 entry->private_data = pstr;
483 if (snd_info_register(entry) < 0) {
484 snd_info_free_entry(entry);
485 entry = NULL;
488 pstr->proc_xrun_debug_entry = entry;
489 #endif
490 return 0;
493 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
495 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
496 snd_info_free_entry(pstr->proc_xrun_debug_entry);
497 pstr->proc_xrun_debug_entry = NULL;
498 #endif
499 snd_info_free_entry(pstr->proc_info_entry);
500 pstr->proc_info_entry = NULL;
501 snd_info_free_entry(pstr->proc_root);
502 pstr->proc_root = NULL;
503 return 0;
506 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
508 struct snd_info_entry *entry;
509 struct snd_card *card;
510 char name[16];
512 card = substream->pcm->card;
514 sprintf(name, "sub%i", substream->number);
515 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
516 return -ENOMEM;
517 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
518 if (snd_info_register(entry) < 0) {
519 snd_info_free_entry(entry);
520 return -ENOMEM;
522 substream->proc_root = entry;
524 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
525 snd_info_set_text_ops(entry, substream,
526 snd_pcm_substream_proc_info_read);
527 if (snd_info_register(entry) < 0) {
528 snd_info_free_entry(entry);
529 entry = NULL;
532 substream->proc_info_entry = entry;
534 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
535 snd_info_set_text_ops(entry, substream,
536 snd_pcm_substream_proc_hw_params_read);
537 if (snd_info_register(entry) < 0) {
538 snd_info_free_entry(entry);
539 entry = NULL;
542 substream->proc_hw_params_entry = entry;
544 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
545 snd_info_set_text_ops(entry, substream,
546 snd_pcm_substream_proc_sw_params_read);
547 if (snd_info_register(entry) < 0) {
548 snd_info_free_entry(entry);
549 entry = NULL;
552 substream->proc_sw_params_entry = entry;
554 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
555 snd_info_set_text_ops(entry, substream,
556 snd_pcm_substream_proc_status_read);
557 if (snd_info_register(entry) < 0) {
558 snd_info_free_entry(entry);
559 entry = NULL;
562 substream->proc_status_entry = entry;
564 return 0;
567 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
569 snd_info_free_entry(substream->proc_info_entry);
570 substream->proc_info_entry = NULL;
571 snd_info_free_entry(substream->proc_hw_params_entry);
572 substream->proc_hw_params_entry = NULL;
573 snd_info_free_entry(substream->proc_sw_params_entry);
574 substream->proc_sw_params_entry = NULL;
575 snd_info_free_entry(substream->proc_status_entry);
576 substream->proc_status_entry = NULL;
577 snd_info_free_entry(substream->proc_root);
578 substream->proc_root = NULL;
579 return 0;
581 #else /* !CONFIG_SND_VERBOSE_PROCFS */
582 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
583 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
584 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
585 static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
586 #endif /* CONFIG_SND_VERBOSE_PROCFS */
589 * snd_pcm_new_stream - create a new PCM stream
590 * @pcm: the pcm instance
591 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
592 * @substream_count: the number of substreams
594 * Creates a new stream for the pcm.
595 * The corresponding stream on the pcm must have been empty before
596 * calling this, i.e. zero must be given to the argument of
597 * snd_pcm_new().
599 * Returns zero if successful, or a negative error code on failure.
601 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
603 int idx, err;
604 struct snd_pcm_str *pstr = &pcm->streams[stream];
605 struct snd_pcm_substream *substream, *prev;
607 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
608 mutex_init(&pstr->oss.setup_mutex);
609 #endif
610 pstr->stream = stream;
611 pstr->pcm = pcm;
612 pstr->substream_count = substream_count;
613 if (substream_count > 0) {
614 err = snd_pcm_stream_proc_init(pstr);
615 if (err < 0) {
616 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
617 return err;
620 prev = NULL;
621 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
622 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
623 if (substream == NULL) {
624 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
625 return -ENOMEM;
627 substream->pcm = pcm;
628 substream->pstr = pstr;
629 substream->number = idx;
630 substream->stream = stream;
631 sprintf(substream->name, "subdevice #%i", idx);
632 snprintf(substream->latency_id, sizeof(substream->latency_id),
633 "ALSA-PCM%d-%d%c%d", pcm->card->number, pcm->device,
634 (stream ? 'c' : 'p'), idx);
635 substream->buffer_bytes_max = UINT_MAX;
636 if (prev == NULL)
637 pstr->substream = substream;
638 else
639 prev->next = substream;
640 err = snd_pcm_substream_proc_init(substream);
641 if (err < 0) {
642 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
643 kfree(substream);
644 return err;
646 substream->group = &substream->self_group;
647 spin_lock_init(&substream->self_group.lock);
648 INIT_LIST_HEAD(&substream->self_group.substreams);
649 list_add_tail(&substream->link_list, &substream->self_group.substreams);
650 spin_lock_init(&substream->timer_lock);
651 atomic_set(&substream->mmap_count, 0);
652 prev = substream;
654 return 0;
657 EXPORT_SYMBOL(snd_pcm_new_stream);
660 * snd_pcm_new - create a new PCM instance
661 * @card: the card instance
662 * @id: the id string
663 * @device: the device index (zero based)
664 * @playback_count: the number of substreams for playback
665 * @capture_count: the number of substreams for capture
666 * @rpcm: the pointer to store the new pcm instance
668 * Creates a new PCM instance.
670 * The pcm operators have to be set afterwards to the new instance
671 * via snd_pcm_set_ops().
673 * Returns zero if successful, or a negative error code on failure.
675 int snd_pcm_new(struct snd_card *card, char *id, int device,
676 int playback_count, int capture_count,
677 struct snd_pcm ** rpcm)
679 struct snd_pcm *pcm;
680 int err;
681 static struct snd_device_ops ops = {
682 .dev_free = snd_pcm_dev_free,
683 .dev_register = snd_pcm_dev_register,
684 .dev_disconnect = snd_pcm_dev_disconnect,
687 snd_assert(rpcm != NULL, return -EINVAL);
688 *rpcm = NULL;
689 snd_assert(card != NULL, return -ENXIO);
690 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
691 if (pcm == NULL) {
692 snd_printk(KERN_ERR "Cannot allocate PCM\n");
693 return -ENOMEM;
695 pcm->card = card;
696 pcm->device = device;
697 if (id)
698 strlcpy(pcm->id, id, sizeof(pcm->id));
699 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
700 snd_pcm_free(pcm);
701 return err;
703 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
704 snd_pcm_free(pcm);
705 return err;
707 mutex_init(&pcm->open_mutex);
708 init_waitqueue_head(&pcm->open_wait);
709 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
710 snd_pcm_free(pcm);
711 return err;
713 *rpcm = pcm;
714 return 0;
717 EXPORT_SYMBOL(snd_pcm_new);
719 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
721 struct snd_pcm_substream *substream, *substream_next;
722 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
723 struct snd_pcm_oss_setup *setup, *setupn;
724 #endif
725 substream = pstr->substream;
726 while (substream) {
727 substream_next = substream->next;
728 snd_pcm_timer_done(substream);
729 snd_pcm_substream_proc_done(substream);
730 kfree(substream);
731 substream = substream_next;
733 snd_pcm_stream_proc_done(pstr);
734 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
735 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
736 setupn = setup->next;
737 kfree(setup->task_name);
738 kfree(setup);
740 #endif
743 static int snd_pcm_free(struct snd_pcm *pcm)
745 struct snd_pcm_notify *notify;
747 snd_assert(pcm != NULL, return -ENXIO);
748 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
749 notify->n_unregister(pcm);
751 if (pcm->private_free)
752 pcm->private_free(pcm);
753 snd_pcm_lib_preallocate_free_for_all(pcm);
754 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
755 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
756 kfree(pcm);
757 return 0;
760 static int snd_pcm_dev_free(struct snd_device *device)
762 struct snd_pcm *pcm = device->device_data;
763 return snd_pcm_free(pcm);
766 static void snd_pcm_tick_timer_func(unsigned long data)
768 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
769 snd_pcm_tick_elapsed(substream);
772 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
773 struct file *file,
774 struct snd_pcm_substream **rsubstream)
776 struct snd_pcm_str * pstr;
777 struct snd_pcm_substream *substream;
778 struct snd_pcm_runtime *runtime;
779 struct snd_ctl_file *kctl;
780 struct snd_card *card;
781 struct list_head *list;
782 int prefer_subdevice = -1;
783 size_t size;
785 snd_assert(rsubstream != NULL, return -EINVAL);
786 *rsubstream = NULL;
787 snd_assert(pcm != NULL, return -ENXIO);
788 pstr = &pcm->streams[stream];
789 if (pstr->substream == NULL || pstr->substream_count == 0)
790 return -ENODEV;
792 card = pcm->card;
793 down_read(&card->controls_rwsem);
794 list_for_each(list, &card->ctl_files) {
795 kctl = snd_ctl_file(list);
796 if (kctl->pid == current->pid) {
797 prefer_subdevice = kctl->prefer_pcm_subdevice;
798 if (prefer_subdevice != -1)
799 break;
802 up_read(&card->controls_rwsem);
804 switch (stream) {
805 case SNDRV_PCM_STREAM_PLAYBACK:
806 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
807 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
808 if (SUBSTREAM_BUSY(substream))
809 return -EAGAIN;
812 break;
813 case SNDRV_PCM_STREAM_CAPTURE:
814 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
815 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
816 if (SUBSTREAM_BUSY(substream))
817 return -EAGAIN;
820 break;
821 default:
822 return -EINVAL;
825 if (file->f_flags & O_APPEND) {
826 if (prefer_subdevice < 0) {
827 if (pstr->substream_count > 1)
828 return -EINVAL; /* must be unique */
829 substream = pstr->substream;
830 } else {
831 for (substream = pstr->substream; substream;
832 substream = substream->next)
833 if (substream->number == prefer_subdevice)
834 break;
836 if (! substream)
837 return -ENODEV;
838 if (! SUBSTREAM_BUSY(substream))
839 return -EBADFD;
840 substream->ref_count++;
841 *rsubstream = substream;
842 return 0;
845 if (prefer_subdevice >= 0) {
846 for (substream = pstr->substream; substream; substream = substream->next)
847 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
848 goto __ok;
850 for (substream = pstr->substream; substream; substream = substream->next)
851 if (!SUBSTREAM_BUSY(substream))
852 break;
853 __ok:
854 if (substream == NULL)
855 return -EAGAIN;
857 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
858 if (runtime == NULL)
859 return -ENOMEM;
861 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
862 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
863 if (runtime->status == NULL) {
864 kfree(runtime);
865 return -ENOMEM;
867 memset((void*)runtime->status, 0, size);
869 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
870 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
871 if (runtime->control == NULL) {
872 snd_free_pages((void*)runtime->status,
873 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
874 kfree(runtime);
875 return -ENOMEM;
877 memset((void*)runtime->control, 0, size);
879 init_waitqueue_head(&runtime->sleep);
880 init_timer(&runtime->tick_timer);
881 runtime->tick_timer.function = snd_pcm_tick_timer_func;
882 runtime->tick_timer.data = (unsigned long) substream;
884 runtime->status->state = SNDRV_PCM_STATE_OPEN;
886 substream->runtime = runtime;
887 substream->private_data = pcm->private_data;
888 substream->ref_count = 1;
889 substream->f_flags = file->f_flags;
890 pstr->substream_opened++;
891 *rsubstream = substream;
892 return 0;
895 void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
897 struct snd_pcm_runtime *runtime;
899 runtime = substream->runtime;
900 snd_assert(runtime != NULL, return);
901 if (runtime->private_free != NULL)
902 runtime->private_free(runtime);
903 snd_free_pages((void*)runtime->status,
904 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
905 snd_free_pages((void*)runtime->control,
906 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
907 kfree(runtime->hw_constraints.rules);
908 kfree(runtime);
909 substream->runtime = NULL;
910 substream->pstr->substream_opened--;
913 static ssize_t show_pcm_class(struct class_device *class_device, char *buf)
915 struct snd_pcm *pcm;
916 const char *str;
917 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
918 [SNDRV_PCM_CLASS_GENERIC] = "generic",
919 [SNDRV_PCM_CLASS_MULTI] = "multi",
920 [SNDRV_PCM_CLASS_MODEM] = "modem",
921 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
924 if (! (pcm = class_get_devdata(class_device)) ||
925 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
926 str = "none";
927 else
928 str = strs[pcm->dev_class];
929 return snprintf(buf, PAGE_SIZE, "%s\n", str);
932 static struct class_device_attribute pcm_attrs =
933 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
935 static int snd_pcm_dev_register(struct snd_device *device)
937 int cidx, err;
938 struct snd_pcm_substream *substream;
939 struct list_head *list;
940 char str[16];
941 struct snd_pcm *pcm = device->device_data;
943 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
944 mutex_lock(&register_mutex);
945 if (snd_pcm_search(pcm->card, pcm->device)) {
946 mutex_unlock(&register_mutex);
947 return -EBUSY;
949 list_add_tail(&pcm->list, &snd_pcm_devices);
950 for (cidx = 0; cidx < 2; cidx++) {
951 int devtype = -1;
952 if (pcm->streams[cidx].substream == NULL)
953 continue;
954 switch (cidx) {
955 case SNDRV_PCM_STREAM_PLAYBACK:
956 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
957 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
958 break;
959 case SNDRV_PCM_STREAM_CAPTURE:
960 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
961 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
962 break;
964 if ((err = snd_register_device(devtype, pcm->card,
965 pcm->device,
966 &snd_pcm_f_ops[cidx],
967 pcm, str)) < 0)
969 list_del(&pcm->list);
970 mutex_unlock(&register_mutex);
971 return err;
973 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
974 &pcm_attrs);
975 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
976 snd_pcm_timer_init(substream);
978 list_for_each(list, &snd_pcm_notify_list) {
979 struct snd_pcm_notify *notify;
980 notify = list_entry(list, struct snd_pcm_notify, list);
981 notify->n_register(pcm);
983 mutex_unlock(&register_mutex);
984 return 0;
987 static int snd_pcm_dev_disconnect(struct snd_device *device)
989 struct snd_pcm *pcm = device->device_data;
990 struct snd_pcm_notify *notify;
991 struct snd_pcm_substream *substream;
992 int cidx, devtype;
994 mutex_lock(&register_mutex);
995 if (list_empty(&pcm->list))
996 goto unlock;
998 list_del_init(&pcm->list);
999 for (cidx = 0; cidx < 2; cidx++)
1000 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1001 if (substream->runtime)
1002 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
1003 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
1004 notify->n_disconnect(pcm);
1006 for (cidx = 0; cidx < 2; cidx++) {
1007 devtype = -1;
1008 switch (cidx) {
1009 case SNDRV_PCM_STREAM_PLAYBACK:
1010 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1011 break;
1012 case SNDRV_PCM_STREAM_CAPTURE:
1013 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1014 break;
1016 snd_unregister_device(devtype, pcm->card, pcm->device);
1018 unlock:
1019 mutex_unlock(&register_mutex);
1020 return 0;
1023 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1025 struct list_head *p;
1027 snd_assert(notify != NULL &&
1028 notify->n_register != NULL &&
1029 notify->n_unregister != NULL &&
1030 notify->n_disconnect, return -EINVAL);
1031 mutex_lock(&register_mutex);
1032 if (nfree) {
1033 list_del(&notify->list);
1034 list_for_each(p, &snd_pcm_devices)
1035 notify->n_unregister(list_entry(p,
1036 struct snd_pcm, list));
1037 } else {
1038 list_add_tail(&notify->list, &snd_pcm_notify_list);
1039 list_for_each(p, &snd_pcm_devices)
1040 notify->n_register(list_entry(p, struct snd_pcm, list));
1042 mutex_unlock(&register_mutex);
1043 return 0;
1046 EXPORT_SYMBOL(snd_pcm_notify);
1048 #ifdef CONFIG_PROC_FS
1050 * Info interface
1053 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1054 struct snd_info_buffer *buffer)
1056 struct list_head *p;
1057 struct snd_pcm *pcm;
1059 mutex_lock(&register_mutex);
1060 list_for_each(p, &snd_pcm_devices) {
1061 pcm = list_entry(p, struct snd_pcm, list);
1062 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1063 pcm->card->number, pcm->device, pcm->id, pcm->name);
1064 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1065 snd_iprintf(buffer, " : playback %i",
1066 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1067 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1068 snd_iprintf(buffer, " : capture %i",
1069 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1070 snd_iprintf(buffer, "\n");
1072 mutex_unlock(&register_mutex);
1075 static struct snd_info_entry *snd_pcm_proc_entry;
1077 static void snd_pcm_proc_init(void)
1079 struct snd_info_entry *entry;
1081 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1082 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1083 if (snd_info_register(entry) < 0) {
1084 snd_info_free_entry(entry);
1085 entry = NULL;
1088 snd_pcm_proc_entry = entry;
1091 static void snd_pcm_proc_done(void)
1093 snd_info_free_entry(snd_pcm_proc_entry);
1096 #else /* !CONFIG_PROC_FS */
1097 #define snd_pcm_proc_init()
1098 #define snd_pcm_proc_done()
1099 #endif /* CONFIG_PROC_FS */
1103 * ENTRY functions
1106 static int __init alsa_pcm_init(void)
1108 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1109 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1110 snd_pcm_proc_init();
1111 return 0;
1114 static void __exit alsa_pcm_exit(void)
1116 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1117 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1118 snd_pcm_proc_done();
1121 module_init(alsa_pcm_init)
1122 module_exit(alsa_pcm_exit)