blob: bf8f412988b8ead58fe147d102c006fda12b78fb [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>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/minors.h>
29#include <sound/pcm.h>
30#include <sound/control.h>
31#include <sound/info.h>
32
33MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
34MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
35MODULE_LICENSE("GPL");
36
Clemens Ladischf87135f2005-11-20 14:06:59 +010037static LIST_HEAD(snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static LIST_HEAD(snd_pcm_notify_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010039static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai877211f2005-11-17 13:59:38 +010041static int snd_pcm_free(struct snd_pcm *pcm);
42static int snd_pcm_dev_free(struct snd_device *device);
43static int snd_pcm_dev_register(struct snd_device *device);
44static int snd_pcm_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Clemens Ladischf87135f2005-11-20 14:06:59 +010046static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
47{
48 struct list_head *p;
49 struct snd_pcm *pcm;
50
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;
55 }
56 return NULL;
57}
58
Takashi Iwai877211f2005-11-17 13:59:38 +010059static int snd_pcm_control_ioctl(struct snd_card *card,
60 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned int cmd, unsigned long arg)
62{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 switch (cmd) {
64 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
65 {
66 int device;
67
68 if (get_user(device, (int __user *)arg))
69 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010070 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 device = device < 0 ? 0 : device + 1;
72 while (device < SNDRV_PCM_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010073 if (snd_pcm_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 device++;
76 }
77 if (device == SNDRV_PCM_DEVICES)
78 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010079 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (put_user(device, (int __user *)arg))
81 return -EFAULT;
82 return 0;
83 }
84 case SNDRV_CTL_IOCTL_PCM_INFO:
85 {
Takashi Iwai877211f2005-11-17 13:59:38 +010086 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010088 int stream;
89 struct snd_pcm *pcm;
90 struct snd_pcm_str *pstr;
91 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +010092 int err;
93
Takashi Iwai877211f2005-11-17 13:59:38 +010094 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (get_user(device, &info->device))
96 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (get_user(stream, &info->stream))
98 return -EFAULT;
99 if (stream < 0 || stream > 1)
100 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (get_user(subdevice, &info->subdevice))
102 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100103 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100104 pcm = snd_pcm_search(card, device);
105 if (pcm == NULL) {
106 err = -ENXIO;
107 goto _error;
108 }
109 pstr = &pcm->streams[stream];
110 if (pstr->substream_count == 0) {
111 err = -ENOENT;
112 goto _error;
113 }
114 if (subdevice >= pstr->substream_count) {
115 err = -ENXIO;
116 goto _error;
117 }
118 for (substream = pstr->substream; substream;
119 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (substream->number == (int)subdevice)
121 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100122 if (substream == NULL) {
123 err = -ENXIO;
124 goto _error;
125 }
126 err = snd_pcm_info_user(substream, info);
127 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100128 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
132 {
133 int val;
134
135 if (get_user(val, (int __user *)arg))
136 return -EFAULT;
137 control->prefer_pcm_subdevice = val;
138 return 0;
139 }
140 }
141 return -ENOIOCTLCMD;
142}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100143
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200144#ifdef CONFIG_SND_VERBOSE_PROCFS
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#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
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static 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),
196};
197
Adrian Bunk12831c12006-04-11 11:12:46 +0200198static const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100199{
200 return snd_pcm_format_names[format];
201}
202
Takashi Iwaie28563c2005-12-01 10:42:42 +0100203static char *snd_pcm_stream_names[] = {
204 STREAM(PLAYBACK),
205 STREAM(CAPTURE),
206};
207
208static 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),
217};
218
219static 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),
225};
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static char *snd_pcm_subformat_names[] = {
228 SUBFORMAT(STD),
229};
230
231static char *snd_pcm_tstamp_mode_names[] = {
232 TSTAMP(NONE),
233 TSTAMP(MMAP),
234};
235
Takashi Iwai877211f2005-11-17 13:59:38 +0100236static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
239 return snd_pcm_stream_names[stream];
240}
241
242static const char *snd_pcm_access_name(snd_pcm_access_t access)
243{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return snd_pcm_access_names[access];
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return snd_pcm_subformat_names[subformat];
250}
251
Takashi Iwai877211f2005-11-17 13:59:38 +0100252static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
255 return snd_pcm_tstamp_mode_names[mode];
256}
257
258static const char *snd_pcm_state_name(snd_pcm_state_t state)
259{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return snd_pcm_state_names[state];
261}
262
263#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
264#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static const char *snd_pcm_oss_format_name(int format)
267{
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";
291 }
292}
293#endif
294
Takashi Iwai877211f2005-11-17 13:59:38 +0100295static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
296 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwai877211f2005-11-17 13:59:38 +0100298 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int err;
300
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200301 if (! substream)
302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 info = kmalloc(sizeof(*info), GFP_KERNEL);
305 if (! info) {
306 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
307 return;
308 }
309
310 err = snd_pcm_info(substream, info);
311 if (err < 0) {
312 snd_iprintf(buffer, "error %d\n", err);
313 kfree(info);
314 return;
315 }
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);
328}
329
Takashi Iwai877211f2005-11-17 13:59:38 +0100330static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
331 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Takashi Iwai877211f2005-11-17 13:59:38 +0100333 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
334 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Takashi Iwai877211f2005-11-17 13:59:38 +0100337static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
338 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Takashi Iwai877211f2005-11-17 13:59:38 +0100340 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
341 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Takashi Iwai877211f2005-11-17 13:59:38 +0100344static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
345 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Takashi Iwai877211f2005-11-17 13:59:38 +0100347 struct snd_pcm_substream *substream = entry->private_data;
348 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!runtime) {
350 snd_iprintf(buffer, "closed\n");
351 return;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
354 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return;
356 }
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);
373 }
374#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Takashi Iwai877211f2005-11-17 13:59:38 +0100377static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
378 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Takashi Iwai877211f2005-11-17 13:59:38 +0100380 struct snd_pcm_substream *substream = entry->private_data;
381 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!runtime) {
383 snd_iprintf(buffer, "closed\n");
384 return;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
387 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return;
389 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Takashi Iwai877211f2005-11-17 13:59:38 +0100402static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
403 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Takashi Iwai877211f2005-11-17 13:59:38 +0100405 struct snd_pcm_substream *substream = entry->private_data;
406 struct snd_pcm_runtime *runtime = substream->runtime;
407 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int err;
409 if (!runtime) {
410 snd_iprintf(buffer, "closed\n");
411 return;
412 }
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;
418 }
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);
430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200432#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100433static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
434 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Takashi Iwai877211f2005-11-17 13:59:38 +0100436 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
438}
439
Takashi Iwai877211f2005-11-17 13:59:38 +0100440static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
441 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Takashi Iwai877211f2005-11-17 13:59:38 +0100443 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 char line[64];
445 if (!snd_info_get_line(buffer, line, sizeof(line)))
446 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
447}
448#endif
449
Takashi Iwai877211f2005-11-17 13:59:38 +0100450static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Takashi Iwai877211f2005-11-17 13:59:38 +0100452 struct snd_pcm *pcm = pstr->pcm;
453 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 char name[16];
455
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;
464 }
465 pstr->proc_root = entry;
466
467 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200468 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (snd_info_register(entry) < 0) {
470 snd_info_free_entry(entry);
471 entry = NULL;
472 }
473 }
474 pstr->proc_info_entry = entry;
475
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200476#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100477 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
478 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200481 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 entry->private_data = pstr;
483 if (snd_info_register(entry) < 0) {
484 snd_info_free_entry(entry);
485 entry = NULL;
486 }
487 }
488 pstr->proc_xrun_debug_entry = entry;
489#endif
490 return 0;
491}
492
Takashi Iwai877211f2005-11-17 13:59:38 +0100493static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200495#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai746d4a02006-06-23 14:37:59 +0200496 snd_info_free_entry(pstr->proc_xrun_debug_entry);
497 pstr->proc_xrun_debug_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200499 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504}
505
Takashi Iwai877211f2005-11-17 13:59:38 +0100506static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwai877211f2005-11-17 13:59:38 +0100508 struct snd_info_entry *entry;
509 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 char name[16];
511
512 card = substream->pcm->card;
513
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;
521 }
522 substream->proc_root = entry;
523
524 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200525 snd_info_set_text_ops(entry, substream,
526 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (snd_info_register(entry) < 0) {
528 snd_info_free_entry(entry);
529 entry = NULL;
530 }
531 }
532 substream->proc_info_entry = entry;
533
534 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200535 snd_info_set_text_ops(entry, substream,
536 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (snd_info_register(entry) < 0) {
538 snd_info_free_entry(entry);
539 entry = NULL;
540 }
541 }
542 substream->proc_hw_params_entry = entry;
543
544 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200545 snd_info_set_text_ops(entry, substream,
546 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (snd_info_register(entry) < 0) {
548 snd_info_free_entry(entry);
549 entry = NULL;
550 }
551 }
552 substream->proc_sw_params_entry = entry;
553
554 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200555 snd_info_set_text_ops(entry, substream,
556 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (snd_info_register(entry) < 0) {
558 snd_info_free_entry(entry);
559 entry = NULL;
560 }
561 }
562 substream->proc_status_entry = entry;
563
564 return 0;
565}
Takashi Iwai746d4a02006-06-23 14:37:59 +0200566
Takashi Iwai877211f2005-11-17 13:59:38 +0100567static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200569 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200581#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100582static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
583static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
584static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
585static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200586#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588/**
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
593 *
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().
598 *
599 * Returns zero if successful, or a negative error code on failure.
600 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100601int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100604 struct snd_pcm_str *pstr = &pcm->streams[stream];
605 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100608 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#endif
610 pstr->stream = stream;
611 pstr->pcm = pcm;
612 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (substream_count > 0) {
614 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100615 if (err < 0) {
616 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 prev = NULL;
621 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200622 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100623 if (substream == NULL) {
624 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 substream->pcm = pcm;
628 substream->pstr = pstr;
629 substream->number = idx;
630 substream->stream = stream;
631 sprintf(substream->name, "subdevice #%i", idx);
632 substream->buffer_bytes_max = UINT_MAX;
633 if (prev == NULL)
634 pstr->substream = substream;
635 else
636 prev->next = substream;
637 err = snd_pcm_substream_proc_init(substream);
638 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100639 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kfree(substream);
641 return err;
642 }
643 substream->group = &substream->self_group;
644 spin_lock_init(&substream->self_group.lock);
645 INIT_LIST_HEAD(&substream->self_group.substreams);
646 list_add_tail(&substream->link_list, &substream->self_group.substreams);
647 spin_lock_init(&substream->timer_lock);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200648 atomic_set(&substream->mmap_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 prev = substream;
650 }
651 return 0;
652}
653
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200654EXPORT_SYMBOL(snd_pcm_new_stream);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/**
657 * snd_pcm_new - create a new PCM instance
658 * @card: the card instance
659 * @id: the id string
660 * @device: the device index (zero based)
661 * @playback_count: the number of substreams for playback
662 * @capture_count: the number of substreams for capture
663 * @rpcm: the pointer to store the new pcm instance
664 *
665 * Creates a new PCM instance.
666 *
667 * The pcm operators have to be set afterwards to the new instance
668 * via snd_pcm_set_ops().
669 *
670 * Returns zero if successful, or a negative error code on failure.
671 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100672int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100674 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwai877211f2005-11-17 13:59:38 +0100676 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100678 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .dev_free = snd_pcm_dev_free,
680 .dev_register = snd_pcm_dev_register,
681 .dev_disconnect = snd_pcm_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 };
683
684 snd_assert(rpcm != NULL, return -EINVAL);
685 *rpcm = NULL;
686 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200687 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100688 if (pcm == NULL) {
689 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 pcm->card = card;
693 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100694 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
697 snd_pcm_free(pcm);
698 return err;
699 }
700 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
701 snd_pcm_free(pcm);
702 return err;
703 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100704 mutex_init(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 init_waitqueue_head(&pcm->open_wait);
706 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
707 snd_pcm_free(pcm);
708 return err;
709 }
710 *rpcm = pcm;
711 return 0;
712}
713
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200714EXPORT_SYMBOL(snd_pcm_new);
715
Takashi Iwai877211f2005-11-17 13:59:38 +0100716static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai877211f2005-11-17 13:59:38 +0100718 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100720 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#endif
722 substream = pstr->substream;
723 while (substream) {
724 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200725 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 snd_pcm_substream_proc_done(substream);
727 kfree(substream);
728 substream = substream_next;
729 }
730 snd_pcm_stream_proc_done(pstr);
731#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
732 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
733 setupn = setup->next;
734 kfree(setup->task_name);
735 kfree(setup);
736 }
737#endif
738}
739
Takashi Iwai877211f2005-11-17 13:59:38 +0100740static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Takashi Iwaic4614822006-06-23 14:38:23 +0200742 struct snd_pcm_notify *notify;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 snd_assert(pcm != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200745 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
746 notify->n_unregister(pcm);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (pcm->private_free)
749 pcm->private_free(pcm);
750 snd_pcm_lib_preallocate_free_for_all(pcm);
751 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
752 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
753 kfree(pcm);
754 return 0;
755}
756
Takashi Iwai877211f2005-11-17 13:59:38 +0100757static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Takashi Iwai877211f2005-11-17 13:59:38 +0100759 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return snd_pcm_free(pcm);
761}
762
763static void snd_pcm_tick_timer_func(unsigned long data)
764{
Takashi Iwai877211f2005-11-17 13:59:38 +0100765 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 snd_pcm_tick_elapsed(substream);
767}
768
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200769int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
770 struct file *file,
771 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Takashi Iwai877211f2005-11-17 13:59:38 +0100773 struct snd_pcm_str * pstr;
774 struct snd_pcm_substream *substream;
775 struct snd_pcm_runtime *runtime;
776 struct snd_ctl_file *kctl;
777 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct list_head *list;
779 int prefer_subdevice = -1;
780 size_t size;
781
782 snd_assert(rsubstream != NULL, return -EINVAL);
783 *rsubstream = NULL;
784 snd_assert(pcm != NULL, return -ENXIO);
785 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200786 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -ENODEV;
788
789 card = pcm->card;
790 down_read(&card->controls_rwsem);
791 list_for_each(list, &card->ctl_files) {
792 kctl = snd_ctl_file(list);
793 if (kctl->pid == current->pid) {
794 prefer_subdevice = kctl->prefer_pcm_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200795 if (prefer_subdevice != -1)
796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
799 up_read(&card->controls_rwsem);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 switch (stream) {
802 case SNDRV_PCM_STREAM_PLAYBACK:
803 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
804 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
805 if (SUBSTREAM_BUSY(substream))
806 return -EAGAIN;
807 }
808 }
809 break;
810 case SNDRV_PCM_STREAM_CAPTURE:
811 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
812 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
813 if (SUBSTREAM_BUSY(substream))
814 return -EAGAIN;
815 }
816 }
817 break;
818 default:
819 return -EINVAL;
820 }
821
Takashi Iwai0df63e42006-04-28 15:13:41 +0200822 if (file->f_flags & O_APPEND) {
823 if (prefer_subdevice < 0) {
824 if (pstr->substream_count > 1)
825 return -EINVAL; /* must be unique */
826 substream = pstr->substream;
827 } else {
828 for (substream = pstr->substream; substream;
829 substream = substream->next)
830 if (substream->number == prefer_subdevice)
831 break;
832 }
833 if (! substream)
834 return -ENODEV;
835 if (! SUBSTREAM_BUSY(substream))
836 return -EBADFD;
837 substream->ref_count++;
838 *rsubstream = substream;
839 return 0;
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (prefer_subdevice >= 0) {
843 for (substream = pstr->substream; substream; substream = substream->next)
844 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
845 goto __ok;
846 }
847 for (substream = pstr->substream; substream; substream = substream->next)
848 if (!SUBSTREAM_BUSY(substream))
849 break;
850 __ok:
851 if (substream == NULL)
852 return -EAGAIN;
853
Takashi Iwaica2c0962005-09-09 14:20:23 +0200854 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (runtime == NULL)
856 return -ENOMEM;
857
Takashi Iwai877211f2005-11-17 13:59:38 +0100858 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
860 if (runtime->status == NULL) {
861 kfree(runtime);
862 return -ENOMEM;
863 }
864 memset((void*)runtime->status, 0, size);
865
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
868 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100869 snd_free_pages((void*)runtime->status,
870 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 kfree(runtime);
872 return -ENOMEM;
873 }
874 memset((void*)runtime->control, 0, size);
875
876 init_waitqueue_head(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 init_timer(&runtime->tick_timer);
878 runtime->tick_timer.function = snd_pcm_tick_timer_func;
879 runtime->tick_timer.data = (unsigned long) substream;
880
881 runtime->status->state = SNDRV_PCM_STATE_OPEN;
882
883 substream->runtime = runtime;
884 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200885 substream->ref_count = 1;
886 substream->f_flags = file->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 pstr->substream_opened++;
888 *rsubstream = substream;
889 return 0;
890}
891
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200892void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai877211f2005-11-17 13:59:38 +0100894 struct snd_pcm_runtime *runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 runtime = substream->runtime;
897 snd_assert(runtime != NULL, return);
898 if (runtime->private_free != NULL)
899 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100900 snd_free_pages((void*)runtime->status,
901 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
902 snd_free_pages((void*)runtime->control,
903 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree(runtime->hw_constraints.rules);
905 kfree(runtime);
906 substream->runtime = NULL;
907 substream->pstr->substream_opened--;
908}
909
Takashi Iwai9d19f482006-09-06 14:27:46 +0200910static ssize_t show_pcm_class(struct class_device *class_device, char *buf)
911{
912 struct snd_pcm *pcm;
913 const char *str;
914 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
915 [SNDRV_PCM_CLASS_GENERIC] = "generic",
916 [SNDRV_PCM_CLASS_MULTI] = "multi",
917 [SNDRV_PCM_CLASS_MODEM] = "modem",
918 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
919 };
920
921 if (! (pcm = class_get_devdata(class_device)) ||
922 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
923 str = "none";
924 else
925 str = strs[pcm->dev_class];
926 return snprintf(buf, PAGE_SIZE, "%s\n", str);
927}
928
929static struct class_device_attribute pcm_attrs =
930 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
931
Takashi Iwai877211f2005-11-17 13:59:38 +0100932static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100934 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100935 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct list_head *list;
937 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100938 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100941 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100942 if (snd_pcm_search(pcm->card, pcm->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100943 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -EBUSY;
945 }
Clemens Ladischf87135f2005-11-20 14:06:59 +0100946 list_add_tail(&pcm->list, &snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 for (cidx = 0; cidx < 2; cidx++) {
948 int devtype = -1;
949 if (pcm->streams[cidx].substream == NULL)
950 continue;
951 switch (cidx) {
952 case SNDRV_PCM_STREAM_PLAYBACK:
953 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
955 break;
956 case SNDRV_PCM_STREAM_CAPTURE:
957 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
959 break;
960 }
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100961 if ((err = snd_register_device(devtype, pcm->card,
962 pcm->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +0100963 &snd_pcm_f_ops[cidx],
964 pcm, str)) < 0)
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100965 {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100966 list_del(&pcm->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100967 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return err;
969 }
Takashi Iwai9d19f482006-09-06 14:27:46 +0200970 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
971 &pcm_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
973 snd_pcm_timer_init(substream);
974 }
975 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100976 struct snd_pcm_notify *notify;
977 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 notify->n_register(pcm);
979 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100980 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
982}
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai877211f2005-11-17 13:59:38 +0100986 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200987 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +0100988 struct snd_pcm_substream *substream;
Takashi Iwaic4614822006-06-23 14:38:23 +0200989 int cidx, devtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100991 mutex_lock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200992 if (list_empty(&pcm->list))
993 goto unlock;
994
Clemens Ladischf87135f2005-11-20 14:06:59 +0100995 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 for (cidx = 0; cidx < 2; cidx++)
997 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
998 if (substream->runtime)
999 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwaic4614822006-06-23 14:38:23 +02001000 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 notify->n_disconnect(pcm);
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 for (cidx = 0; cidx < 2; cidx++) {
1004 devtype = -1;
1005 switch (cidx) {
1006 case SNDRV_PCM_STREAM_PLAYBACK:
1007 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1008 break;
1009 case SNDRV_PCM_STREAM_CAPTURE:
1010 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1011 break;
1012 }
1013 snd_unregister_device(devtype, pcm->card, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001015 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001016 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Takashi Iwai877211f2005-11-17 13:59:38 +01001020int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001022 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Takashi Iwaic4614822006-06-23 14:38:23 +02001024 snd_assert(notify != NULL &&
1025 notify->n_register != NULL &&
1026 notify->n_unregister != NULL &&
1027 notify->n_disconnect, return -EINVAL);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001028 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (nfree) {
1030 list_del(&notify->list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001031 list_for_each(p, &snd_pcm_devices)
1032 notify->n_unregister(list_entry(p,
1033 struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 } else {
1035 list_add_tail(&notify->list, &snd_pcm_notify_list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001036 list_for_each(p, &snd_pcm_devices)
1037 notify->n_register(list_entry(p, struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001039 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 0;
1041}
1042
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001043EXPORT_SYMBOL(snd_pcm_notify);
1044
Takashi Iwaie28563c2005-12-01 10:42:42 +01001045#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
1047 * Info interface
1048 */
1049
Takashi Iwai877211f2005-11-17 13:59:38 +01001050static void snd_pcm_proc_read(struct snd_info_entry *entry,
1051 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001053 struct list_head *p;
Takashi Iwai877211f2005-11-17 13:59:38 +01001054 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001056 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001057 list_for_each(p, &snd_pcm_devices) {
1058 pcm = list_entry(p, struct snd_pcm, list);
1059 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1060 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001062 snd_iprintf(buffer, " : playback %i",
1063 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001065 snd_iprintf(buffer, " : capture %i",
1066 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 snd_iprintf(buffer, "\n");
1068 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001069 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001072static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Takashi Iwaie28563c2005-12-01 10:42:42 +01001074static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Takashi Iwai877211f2005-11-17 13:59:38 +01001076 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001079 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (snd_info_register(entry) < 0) {
1081 snd_info_free_entry(entry);
1082 entry = NULL;
1083 }
1084 }
1085 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001086}
1087
1088static void snd_pcm_proc_done(void)
1089{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001090 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001091}
1092
1093#else /* !CONFIG_PROC_FS */
1094#define snd_pcm_proc_init()
1095#define snd_pcm_proc_done()
1096#endif /* CONFIG_PROC_FS */
1097
1098
1099/*
1100 * ENTRY functions
1101 */
1102
1103static int __init alsa_pcm_init(void)
1104{
1105 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1106 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1107 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109}
1110
1111static void __exit alsa_pcm_exit(void)
1112{
1113 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1114 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001115 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118module_init(alsa_pcm_init)
1119module_exit(alsa_pcm_exit)