blob: 2bc5f69ec2a850c879d795830e96305516c2d9d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/time.h>
26#include <sound/core.h>
27#include <sound/minors.h>
28#include <sound/pcm.h>
29#include <sound/control.h>
30#include <sound/info.h>
31
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
33MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34MODULE_LICENSE("GPL");
35
Takashi Iwai877211f2005-11-17 13:59:38 +010036struct snd_pcm *snd_pcm_devices[SNDRV_CARDS * SNDRV_PCM_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static LIST_HEAD(snd_pcm_notify_list);
38static DECLARE_MUTEX(register_mutex);
39
Takashi Iwai877211f2005-11-17 13:59:38 +010040static int snd_pcm_free(struct snd_pcm *pcm);
41static int snd_pcm_dev_free(struct snd_device *device);
42static int snd_pcm_dev_register(struct snd_device *device);
43static int snd_pcm_dev_disconnect(struct snd_device *device);
44static int snd_pcm_dev_unregister(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Takashi Iwai877211f2005-11-17 13:59:38 +010046static int snd_pcm_control_ioctl(struct snd_card *card,
47 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 unsigned int cmd, unsigned long arg)
49{
50 unsigned int tmp;
51
52 tmp = card->number * SNDRV_PCM_DEVICES;
53 switch (cmd) {
54 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
55 {
56 int device;
57
58 if (get_user(device, (int __user *)arg))
59 return -EFAULT;
60 device = device < 0 ? 0 : device + 1;
61 while (device < SNDRV_PCM_DEVICES) {
62 if (snd_pcm_devices[tmp + device])
63 break;
64 device++;
65 }
66 if (device == SNDRV_PCM_DEVICES)
67 device = -1;
68 if (put_user(device, (int __user *)arg))
69 return -EFAULT;
70 return 0;
71 }
72 case SNDRV_CTL_IOCTL_PCM_INFO:
73 {
Takashi Iwai877211f2005-11-17 13:59:38 +010074 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010076 int stream;
77 struct snd_pcm *pcm;
78 struct snd_pcm_str *pstr;
79 struct snd_pcm_substream *substream;
80 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (get_user(device, &info->device))
82 return -EFAULT;
83 if (device >= SNDRV_PCM_DEVICES)
84 return -ENXIO;
85 pcm = snd_pcm_devices[tmp + device];
86 if (pcm == NULL)
87 return -ENXIO;
88 if (get_user(stream, &info->stream))
89 return -EFAULT;
90 if (stream < 0 || stream > 1)
91 return -EINVAL;
92 pstr = &pcm->streams[stream];
93 if (pstr->substream_count == 0)
94 return -ENOENT;
95 if (get_user(subdevice, &info->subdevice))
96 return -EFAULT;
97 if (subdevice >= pstr->substream_count)
98 return -ENXIO;
99 for (substream = pstr->substream; substream; substream = substream->next)
100 if (substream->number == (int)subdevice)
101 break;
102 if (substream == NULL)
103 return -ENXIO;
104 return snd_pcm_info_user(substream, info);
105 }
106 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
107 {
108 int val;
109
110 if (get_user(val, (int __user *)arg))
111 return -EFAULT;
112 control->prefer_pcm_subdevice = val;
113 return 0;
114 }
115 }
116 return -ENOIOCTLCMD;
117}
118#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
119#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
120#define READY(v) [SNDRV_PCM_READY_##v] = #v
121#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
122#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
123#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
124#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
125#define START(v) [SNDRV_PCM_START_##v] = #v
126#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
127#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
128
129static char *snd_pcm_stream_names[] = {
130 STREAM(PLAYBACK),
131 STREAM(CAPTURE),
132};
133
134static char *snd_pcm_state_names[] = {
135 STATE(OPEN),
136 STATE(SETUP),
137 STATE(PREPARED),
138 STATE(RUNNING),
139 STATE(XRUN),
140 STATE(DRAINING),
141 STATE(PAUSED),
142 STATE(SUSPENDED),
143};
144
145static char *snd_pcm_access_names[] = {
146 ACCESS(MMAP_INTERLEAVED),
147 ACCESS(MMAP_NONINTERLEAVED),
148 ACCESS(MMAP_COMPLEX),
149 ACCESS(RW_INTERLEAVED),
150 ACCESS(RW_NONINTERLEAVED),
151};
152
153static char *snd_pcm_format_names[] = {
154 FORMAT(S8),
155 FORMAT(U8),
156 FORMAT(S16_LE),
157 FORMAT(S16_BE),
158 FORMAT(U16_LE),
159 FORMAT(U16_BE),
160 FORMAT(S24_LE),
161 FORMAT(S24_BE),
162 FORMAT(U24_LE),
163 FORMAT(U24_BE),
164 FORMAT(S32_LE),
165 FORMAT(S32_BE),
166 FORMAT(U32_LE),
167 FORMAT(U32_BE),
168 FORMAT(FLOAT_LE),
169 FORMAT(FLOAT_BE),
170 FORMAT(FLOAT64_LE),
171 FORMAT(FLOAT64_BE),
172 FORMAT(IEC958_SUBFRAME_LE),
173 FORMAT(IEC958_SUBFRAME_BE),
174 FORMAT(MU_LAW),
175 FORMAT(A_LAW),
176 FORMAT(IMA_ADPCM),
177 FORMAT(MPEG),
178 FORMAT(GSM),
179 FORMAT(SPECIAL),
180 FORMAT(S24_3LE),
181 FORMAT(S24_3BE),
182 FORMAT(U24_3LE),
183 FORMAT(U24_3BE),
184 FORMAT(S20_3LE),
185 FORMAT(S20_3BE),
186 FORMAT(U20_3LE),
187 FORMAT(U20_3BE),
188 FORMAT(S18_3LE),
189 FORMAT(S18_3BE),
190 FORMAT(U18_3LE),
191 FORMAT(U18_3BE),
192};
193
194static char *snd_pcm_subformat_names[] = {
195 SUBFORMAT(STD),
196};
197
198static char *snd_pcm_tstamp_mode_names[] = {
199 TSTAMP(NONE),
200 TSTAMP(MMAP),
201};
202
Takashi Iwai877211f2005-11-17 13:59:38 +0100203static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
206 return snd_pcm_stream_names[stream];
207}
208
209static const char *snd_pcm_access_name(snd_pcm_access_t access)
210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return snd_pcm_access_names[access];
212}
213
214const char *snd_pcm_format_name(snd_pcm_format_t format)
215{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return snd_pcm_format_names[format];
217}
218
219static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return snd_pcm_subformat_names[subformat];
222}
223
Takashi Iwai877211f2005-11-17 13:59:38 +0100224static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
227 return snd_pcm_tstamp_mode_names[mode];
228}
229
230static const char *snd_pcm_state_name(snd_pcm_state_t state)
231{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return snd_pcm_state_names[state];
233}
234
235#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
236#include <linux/soundcard.h>
237static const char *snd_pcm_oss_format_name(int format)
238{
239 switch (format) {
240 case AFMT_MU_LAW:
241 return "MU_LAW";
242 case AFMT_A_LAW:
243 return "A_LAW";
244 case AFMT_IMA_ADPCM:
245 return "IMA_ADPCM";
246 case AFMT_U8:
247 return "U8";
248 case AFMT_S16_LE:
249 return "S16_LE";
250 case AFMT_S16_BE:
251 return "S16_BE";
252 case AFMT_S8:
253 return "S8";
254 case AFMT_U16_LE:
255 return "U16_LE";
256 case AFMT_U16_BE:
257 return "U16_BE";
258 case AFMT_MPEG:
259 return "MPEG";
260 default:
261 return "unknown";
262 }
263}
264#endif
265
266#ifdef CONFIG_PROC_FS
Takashi Iwai877211f2005-11-17 13:59:38 +0100267static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
268 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Takashi Iwai877211f2005-11-17 13:59:38 +0100270 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int err;
272
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200273 if (! substream)
274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 info = kmalloc(sizeof(*info), GFP_KERNEL);
277 if (! info) {
278 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
279 return;
280 }
281
282 err = snd_pcm_info(substream, info);
283 if (err < 0) {
284 snd_iprintf(buffer, "error %d\n", err);
285 kfree(info);
286 return;
287 }
288 snd_iprintf(buffer, "card: %d\n", info->card);
289 snd_iprintf(buffer, "device: %d\n", info->device);
290 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
291 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
292 snd_iprintf(buffer, "id: %s\n", info->id);
293 snd_iprintf(buffer, "name: %s\n", info->name);
294 snd_iprintf(buffer, "subname: %s\n", info->subname);
295 snd_iprintf(buffer, "class: %d\n", info->dev_class);
296 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
297 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
298 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
299 kfree(info);
300}
301
Takashi Iwai877211f2005-11-17 13:59:38 +0100302static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
303 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Takashi Iwai877211f2005-11-17 13:59:38 +0100305 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
306 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Takashi Iwai877211f2005-11-17 13:59:38 +0100309static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
310 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Takashi Iwai877211f2005-11-17 13:59:38 +0100312 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
313 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Takashi Iwai877211f2005-11-17 13:59:38 +0100316static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
317 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwai877211f2005-11-17 13:59:38 +0100319 struct snd_pcm_substream *substream = entry->private_data;
320 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!runtime) {
322 snd_iprintf(buffer, "closed\n");
323 return;
324 }
325 snd_pcm_stream_lock_irq(substream);
326 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
327 snd_iprintf(buffer, "no setup\n");
328 snd_pcm_stream_unlock_irq(substream);
329 return;
330 }
331 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
332 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
333 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
334 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
335 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
336 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
337 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
338 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
339#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
340 if (substream->oss.oss) {
341 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
342 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
343 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
344 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
345 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
346 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
347 }
348#endif
349 snd_pcm_stream_unlock_irq(substream);
350}
351
Takashi Iwai877211f2005-11-17 13:59:38 +0100352static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
353 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Takashi Iwai877211f2005-11-17 13:59:38 +0100355 struct snd_pcm_substream *substream = entry->private_data;
356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!runtime) {
358 snd_iprintf(buffer, "closed\n");
359 return;
360 }
361 snd_pcm_stream_lock_irq(substream);
362 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
363 snd_iprintf(buffer, "no setup\n");
364 snd_pcm_stream_unlock_irq(substream);
365 return;
366 }
367 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
368 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
369 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
370 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
371 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
372 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
373 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
374 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
375 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
376 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
377 snd_pcm_stream_unlock_irq(substream);
378}
379
Takashi Iwai877211f2005-11-17 13:59:38 +0100380static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
381 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Takashi Iwai877211f2005-11-17 13:59:38 +0100383 struct snd_pcm_substream *substream = entry->private_data;
384 struct snd_pcm_runtime *runtime = substream->runtime;
385 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int err;
387 if (!runtime) {
388 snd_iprintf(buffer, "closed\n");
389 return;
390 }
391 memset(&status, 0, sizeof(status));
392 err = snd_pcm_status(substream, &status);
393 if (err < 0) {
394 snd_iprintf(buffer, "error %d\n", err);
395 return;
396 }
397 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
398 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
399 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
400 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
401 status.tstamp.tv_sec, status.tstamp.tv_nsec);
402 snd_iprintf(buffer, "delay : %ld\n", status.delay);
403 snd_iprintf(buffer, "avail : %ld\n", status.avail);
404 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
405 snd_iprintf(buffer, "-----\n");
406 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
407 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
408}
409#endif
410
411#ifdef CONFIG_SND_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100412static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
413 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Takashi Iwai877211f2005-11-17 13:59:38 +0100415 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
417}
418
Takashi Iwai877211f2005-11-17 13:59:38 +0100419static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
420 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Takashi Iwai877211f2005-11-17 13:59:38 +0100422 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 char line[64];
424 if (!snd_info_get_line(buffer, line, sizeof(line)))
425 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
426}
427#endif
428
Takashi Iwai877211f2005-11-17 13:59:38 +0100429static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwai877211f2005-11-17 13:59:38 +0100431 struct snd_pcm *pcm = pstr->pcm;
432 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 char name[16];
434
435 sprintf(name, "pcm%i%c", pcm->device,
436 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
437 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
438 return -ENOMEM;
439 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
440 if (snd_info_register(entry) < 0) {
441 snd_info_free_entry(entry);
442 return -ENOMEM;
443 }
444 pstr->proc_root = entry;
445
446 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
447 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
448 if (snd_info_register(entry) < 0) {
449 snd_info_free_entry(entry);
450 entry = NULL;
451 }
452 }
453 pstr->proc_info_entry = entry;
454
455#ifdef CONFIG_SND_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100456 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
457 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 entry->c.text.read_size = 64;
459 entry->c.text.read = snd_pcm_xrun_debug_read;
460 entry->c.text.write_size = 64;
461 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200462 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 entry->private_data = pstr;
464 if (snd_info_register(entry) < 0) {
465 snd_info_free_entry(entry);
466 entry = NULL;
467 }
468 }
469 pstr->proc_xrun_debug_entry = entry;
470#endif
471 return 0;
472}
473
Takashi Iwai877211f2005-11-17 13:59:38 +0100474static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476#ifdef CONFIG_SND_DEBUG
477 if (pstr->proc_xrun_debug_entry) {
478 snd_info_unregister(pstr->proc_xrun_debug_entry);
479 pstr->proc_xrun_debug_entry = NULL;
480 }
481#endif
482 if (pstr->proc_info_entry) {
483 snd_info_unregister(pstr->proc_info_entry);
484 pstr->proc_info_entry = NULL;
485 }
486 if (pstr->proc_root) {
487 snd_info_unregister(pstr->proc_root);
488 pstr->proc_root = NULL;
489 }
490 return 0;
491}
492
Takashi Iwai877211f2005-11-17 13:59:38 +0100493static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwai877211f2005-11-17 13:59:38 +0100495 struct snd_info_entry *entry;
496 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 char name[16];
498
499 card = substream->pcm->card;
500
501 sprintf(name, "sub%i", substream->number);
502 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
503 return -ENOMEM;
504 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
505 if (snd_info_register(entry) < 0) {
506 snd_info_free_entry(entry);
507 return -ENOMEM;
508 }
509 substream->proc_root = entry;
510
511 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
512 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
513 if (snd_info_register(entry) < 0) {
514 snd_info_free_entry(entry);
515 entry = NULL;
516 }
517 }
518 substream->proc_info_entry = entry;
519
520 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
521 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
522 if (snd_info_register(entry) < 0) {
523 snd_info_free_entry(entry);
524 entry = NULL;
525 }
526 }
527 substream->proc_hw_params_entry = entry;
528
529 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
530 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
533 entry = NULL;
534 }
535 }
536 substream->proc_sw_params_entry = entry;
537
538 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
539 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
540 if (snd_info_register(entry) < 0) {
541 snd_info_free_entry(entry);
542 entry = NULL;
543 }
544 }
545 substream->proc_status_entry = entry;
546
547 return 0;
548}
549
Takashi Iwai877211f2005-11-17 13:59:38 +0100550static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 if (substream->proc_info_entry) {
553 snd_info_unregister(substream->proc_info_entry);
554 substream->proc_info_entry = NULL;
555 }
556 if (substream->proc_hw_params_entry) {
557 snd_info_unregister(substream->proc_hw_params_entry);
558 substream->proc_hw_params_entry = NULL;
559 }
560 if (substream->proc_sw_params_entry) {
561 snd_info_unregister(substream->proc_sw_params_entry);
562 substream->proc_sw_params_entry = NULL;
563 }
564 if (substream->proc_status_entry) {
565 snd_info_unregister(substream->proc_status_entry);
566 substream->proc_status_entry = NULL;
567 }
568 if (substream->proc_root) {
569 snd_info_unregister(substream->proc_root);
570 substream->proc_root = NULL;
571 }
572 return 0;
573}
574
575/**
576 * snd_pcm_new_stream - create a new PCM stream
577 * @pcm: the pcm instance
578 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
579 * @substream_count: the number of substreams
580 *
581 * Creates a new stream for the pcm.
582 * The corresponding stream on the pcm must have been empty before
583 * calling this, i.e. zero must be given to the argument of
584 * snd_pcm_new().
585 *
586 * Returns zero if successful, or a negative error code on failure.
587 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100588int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100591 struct snd_pcm_str *pstr = &pcm->streams[stream];
592 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
595 init_MUTEX(&pstr->oss.setup_mutex);
596#endif
597 pstr->stream = stream;
598 pstr->pcm = pcm;
599 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (substream_count > 0) {
601 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100602 if (err < 0) {
603 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 prev = NULL;
608 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200609 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100610 if (substream == NULL) {
611 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 substream->pcm = pcm;
615 substream->pstr = pstr;
616 substream->number = idx;
617 substream->stream = stream;
618 sprintf(substream->name, "subdevice #%i", idx);
619 substream->buffer_bytes_max = UINT_MAX;
620 if (prev == NULL)
621 pstr->substream = substream;
622 else
623 prev->next = substream;
624 err = snd_pcm_substream_proc_init(substream);
625 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100626 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kfree(substream);
628 return err;
629 }
630 substream->group = &substream->self_group;
631 spin_lock_init(&substream->self_group.lock);
632 INIT_LIST_HEAD(&substream->self_group.substreams);
633 list_add_tail(&substream->link_list, &substream->self_group.substreams);
634 spin_lock_init(&substream->timer_lock);
635 prev = substream;
636 }
637 return 0;
638}
639
640/**
641 * snd_pcm_new - create a new PCM instance
642 * @card: the card instance
643 * @id: the id string
644 * @device: the device index (zero based)
645 * @playback_count: the number of substreams for playback
646 * @capture_count: the number of substreams for capture
647 * @rpcm: the pointer to store the new pcm instance
648 *
649 * Creates a new PCM instance.
650 *
651 * The pcm operators have to be set afterwards to the new instance
652 * via snd_pcm_set_ops().
653 *
654 * Returns zero if successful, or a negative error code on failure.
655 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100656int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100658 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Takashi Iwai877211f2005-11-17 13:59:38 +0100660 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100662 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .dev_free = snd_pcm_dev_free,
664 .dev_register = snd_pcm_dev_register,
665 .dev_disconnect = snd_pcm_dev_disconnect,
666 .dev_unregister = snd_pcm_dev_unregister
667 };
668
669 snd_assert(rpcm != NULL, return -EINVAL);
670 *rpcm = NULL;
671 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200672 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100673 if (pcm == NULL) {
674 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 pcm->card = card;
678 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100679 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
682 snd_pcm_free(pcm);
683 return err;
684 }
685 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
686 snd_pcm_free(pcm);
687 return err;
688 }
689 init_MUTEX(&pcm->open_mutex);
690 init_waitqueue_head(&pcm->open_wait);
691 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
692 snd_pcm_free(pcm);
693 return err;
694 }
695 *rpcm = pcm;
696 return 0;
697}
698
Takashi Iwai877211f2005-11-17 13:59:38 +0100699static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Takashi Iwai877211f2005-11-17 13:59:38 +0100701 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100703 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704#endif
705 substream = pstr->substream;
706 while (substream) {
707 substream_next = substream->next;
708 snd_pcm_substream_proc_done(substream);
709 kfree(substream);
710 substream = substream_next;
711 }
712 snd_pcm_stream_proc_done(pstr);
713#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
714 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
715 setupn = setup->next;
716 kfree(setup->task_name);
717 kfree(setup);
718 }
719#endif
720}
721
Takashi Iwai877211f2005-11-17 13:59:38 +0100722static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 snd_assert(pcm != NULL, return -ENXIO);
725 if (pcm->private_free)
726 pcm->private_free(pcm);
727 snd_pcm_lib_preallocate_free_for_all(pcm);
728 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
729 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
730 kfree(pcm);
731 return 0;
732}
733
Takashi Iwai877211f2005-11-17 13:59:38 +0100734static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Takashi Iwai877211f2005-11-17 13:59:38 +0100736 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return snd_pcm_free(pcm);
738}
739
740static void snd_pcm_tick_timer_func(unsigned long data)
741{
Takashi Iwai877211f2005-11-17 13:59:38 +0100742 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 snd_pcm_tick_elapsed(substream);
744}
745
Takashi Iwai877211f2005-11-17 13:59:38 +0100746int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
747 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Takashi Iwai877211f2005-11-17 13:59:38 +0100749 struct snd_pcm_str * pstr;
750 struct snd_pcm_substream *substream;
751 struct snd_pcm_runtime *runtime;
752 struct snd_ctl_file *kctl;
753 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct list_head *list;
755 int prefer_subdevice = -1;
756 size_t size;
757
758 snd_assert(rsubstream != NULL, return -EINVAL);
759 *rsubstream = NULL;
760 snd_assert(pcm != NULL, return -ENXIO);
761 pstr = &pcm->streams[stream];
762 if (pstr->substream == NULL)
763 return -ENODEV;
764
765 card = pcm->card;
766 down_read(&card->controls_rwsem);
767 list_for_each(list, &card->ctl_files) {
768 kctl = snd_ctl_file(list);
769 if (kctl->pid == current->pid) {
770 prefer_subdevice = kctl->prefer_pcm_subdevice;
771 break;
772 }
773 }
774 up_read(&card->controls_rwsem);
775
776 if (pstr->substream_count == 0)
777 return -ENODEV;
778 switch (stream) {
779 case SNDRV_PCM_STREAM_PLAYBACK:
780 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
781 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
782 if (SUBSTREAM_BUSY(substream))
783 return -EAGAIN;
784 }
785 }
786 break;
787 case SNDRV_PCM_STREAM_CAPTURE:
788 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
789 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
790 if (SUBSTREAM_BUSY(substream))
791 return -EAGAIN;
792 }
793 }
794 break;
795 default:
796 return -EINVAL;
797 }
798
799 if (prefer_subdevice >= 0) {
800 for (substream = pstr->substream; substream; substream = substream->next)
801 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
802 goto __ok;
803 }
804 for (substream = pstr->substream; substream; substream = substream->next)
805 if (!SUBSTREAM_BUSY(substream))
806 break;
807 __ok:
808 if (substream == NULL)
809 return -EAGAIN;
810
Takashi Iwaica2c0962005-09-09 14:20:23 +0200811 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (runtime == NULL)
813 return -ENOMEM;
814
Takashi Iwai877211f2005-11-17 13:59:38 +0100815 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
817 if (runtime->status == NULL) {
818 kfree(runtime);
819 return -ENOMEM;
820 }
821 memset((void*)runtime->status, 0, size);
822
Takashi Iwai877211f2005-11-17 13:59:38 +0100823 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
825 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100826 snd_free_pages((void*)runtime->status,
827 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 kfree(runtime);
829 return -ENOMEM;
830 }
831 memset((void*)runtime->control, 0, size);
832
833 init_waitqueue_head(&runtime->sleep);
834 atomic_set(&runtime->mmap_count, 0);
835 init_timer(&runtime->tick_timer);
836 runtime->tick_timer.function = snd_pcm_tick_timer_func;
837 runtime->tick_timer.data = (unsigned long) substream;
838
839 runtime->status->state = SNDRV_PCM_STATE_OPEN;
840
841 substream->runtime = runtime;
842 substream->private_data = pcm->private_data;
843 pstr->substream_opened++;
844 *rsubstream = substream;
845 return 0;
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848void snd_pcm_release_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai877211f2005-11-17 13:59:38 +0100850 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 substream->file = NULL;
852 runtime = substream->runtime;
853 snd_assert(runtime != NULL, return);
854 if (runtime->private_free != NULL)
855 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100856 snd_free_pages((void*)runtime->status,
857 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
858 snd_free_pages((void*)runtime->control,
859 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 kfree(runtime->hw_constraints.rules);
861 kfree(runtime);
862 substream->runtime = NULL;
863 substream->pstr->substream_opened--;
864}
865
Takashi Iwai877211f2005-11-17 13:59:38 +0100866static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 int idx, cidx, err;
869 unsigned short minor;
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct list_head *list;
872 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100873 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
876 down(&register_mutex);
877 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
878 if (snd_pcm_devices[idx]) {
879 up(&register_mutex);
880 return -EBUSY;
881 }
882 snd_pcm_devices[idx] = pcm;
883 for (cidx = 0; cidx < 2; cidx++) {
884 int devtype = -1;
885 if (pcm->streams[cidx].substream == NULL)
886 continue;
887 switch (cidx) {
888 case SNDRV_PCM_STREAM_PLAYBACK:
889 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
890 minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
891 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
892 break;
893 case SNDRV_PCM_STREAM_CAPTURE:
894 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
895 minor = SNDRV_MINOR_PCM_CAPTURE + idx;
896 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
897 break;
898 }
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100899 if ((err = snd_register_device(devtype, pcm->card,
900 pcm->device,
901 &snd_pcm_f_ops[cidx], str)) < 0)
902 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 snd_pcm_devices[idx] = NULL;
904 up(&register_mutex);
905 return err;
906 }
907 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
908 snd_pcm_timer_init(substream);
909 }
910 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100911 struct snd_pcm_notify *notify;
912 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 notify->n_register(pcm);
914 }
915 up(&register_mutex);
916 return 0;
917}
918
Takashi Iwai877211f2005-11-17 13:59:38 +0100919static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100923 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int idx, cidx;
925
926 down(&register_mutex);
927 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
928 snd_pcm_devices[idx] = NULL;
929 for (cidx = 0; cidx < 2; cidx++)
930 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
931 if (substream->runtime)
932 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
933 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100934 struct snd_pcm_notify *notify;
935 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 notify->n_disconnect(pcm);
937 }
938 up(&register_mutex);
939 return 0;
940}
941
Takashi Iwai877211f2005-11-17 13:59:38 +0100942static int snd_pcm_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 int idx, cidx, devtype;
Takashi Iwai877211f2005-11-17 13:59:38 +0100945 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100947 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 snd_assert(pcm != NULL, return -ENXIO);
950 down(&register_mutex);
951 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
952 snd_pcm_devices[idx] = NULL;
953 for (cidx = 0; cidx < 2; cidx++) {
954 devtype = -1;
955 switch (cidx) {
956 case SNDRV_PCM_STREAM_PLAYBACK:
957 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
958 break;
959 case SNDRV_PCM_STREAM_CAPTURE:
960 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
961 break;
962 }
963 snd_unregister_device(devtype, pcm->card, pcm->device);
964 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
965 snd_pcm_timer_done(substream);
966 }
967 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100968 struct snd_pcm_notify *notify;
969 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 notify->n_unregister(pcm);
971 }
972 up(&register_mutex);
973 return snd_pcm_free(pcm);
974}
975
Takashi Iwai877211f2005-11-17 13:59:38 +0100976int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 int idx;
979
980 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
981 down(&register_mutex);
982 if (nfree) {
983 list_del(&notify->list);
984 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
985 if (snd_pcm_devices[idx] == NULL)
986 continue;
987 notify->n_unregister(snd_pcm_devices[idx]);
988 }
989 } else {
990 list_add_tail(&notify->list, &snd_pcm_notify_list);
991 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
992 if (snd_pcm_devices[idx] == NULL)
993 continue;
994 notify->n_register(snd_pcm_devices[idx]);
995 }
996 }
997 up(&register_mutex);
998 return 0;
999}
1000
1001/*
1002 * Info interface
1003 */
1004
Takashi Iwai877211f2005-11-17 13:59:38 +01001005static void snd_pcm_proc_read(struct snd_info_entry *entry,
1006 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 int idx;
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 down(&register_mutex);
1012 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
1013 pcm = snd_pcm_devices[idx];
1014 if (pcm == NULL)
1015 continue;
1016 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
1017 idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
1018 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001019 snd_iprintf(buffer, " : playback %i",
1020 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001022 snd_iprintf(buffer, " : capture %i",
1023 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 snd_iprintf(buffer, "\n");
1025 }
1026 up(&register_mutex);
1027}
1028
1029/*
1030 * ENTRY functions
1031 */
1032
Takashi Iwai877211f2005-11-17 13:59:38 +01001033static struct snd_info_entry *snd_pcm_proc_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035static int __init alsa_pcm_init(void)
1036{
Takashi Iwai877211f2005-11-17 13:59:38 +01001037 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1040 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1041 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001042 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128,
1043 snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (snd_info_register(entry) < 0) {
1045 snd_info_free_entry(entry);
1046 entry = NULL;
1047 }
1048 }
1049 snd_pcm_proc_entry = entry;
1050 return 0;
1051}
1052
1053static void __exit alsa_pcm_exit(void)
1054{
1055 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1056 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1057 if (snd_pcm_proc_entry) {
1058 snd_info_unregister(snd_pcm_proc_entry);
1059 snd_pcm_proc_entry = NULL;
1060 }
1061}
1062
1063module_init(alsa_pcm_init)
1064module_exit(alsa_pcm_exit)
1065
1066EXPORT_SYMBOL(snd_pcm_devices);
1067EXPORT_SYMBOL(snd_pcm_new);
1068EXPORT_SYMBOL(snd_pcm_new_stream);
1069EXPORT_SYMBOL(snd_pcm_notify);
1070EXPORT_SYMBOL(snd_pcm_open_substream);
1071EXPORT_SYMBOL(snd_pcm_release_substream);
1072EXPORT_SYMBOL(snd_pcm_format_name);
1073 /* pcm_native.c */
1074EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075#ifdef CONFIG_PM
1076EXPORT_SYMBOL(snd_pcm_suspend);
1077EXPORT_SYMBOL(snd_pcm_suspend_all);
1078#endif
1079EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1080EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1081EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1082EXPORT_SYMBOL(snd_pcm_mmap_data);
1083#if SNDRV_PCM_INFO_MMAP_IOMEM
1084EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1085#endif
1086 /* pcm_misc.c */
1087EXPORT_SYMBOL(snd_pcm_format_signed);
1088EXPORT_SYMBOL(snd_pcm_format_unsigned);
1089EXPORT_SYMBOL(snd_pcm_format_linear);
1090EXPORT_SYMBOL(snd_pcm_format_little_endian);
1091EXPORT_SYMBOL(snd_pcm_format_big_endian);
1092EXPORT_SYMBOL(snd_pcm_format_width);
1093EXPORT_SYMBOL(snd_pcm_format_physical_width);
Takashi Iwai9502dca2005-05-18 16:25:46 +02001094EXPORT_SYMBOL(snd_pcm_format_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095EXPORT_SYMBOL(snd_pcm_format_silence_64);
1096EXPORT_SYMBOL(snd_pcm_format_set_silence);
1097EXPORT_SYMBOL(snd_pcm_build_linear_format);
1098EXPORT_SYMBOL(snd_pcm_limit_hw_rates);