blob: ece25c718e95e8f78c5a71b01df6e01e80519908 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020032MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34MODULE_LICENSE("GPL");
35
Clemens Ladischf87135f2005-11-20 14:06:59 +010036static LIST_HEAD(snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static LIST_HEAD(snd_pcm_notify_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010038static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Clemens Ladischf87135f2005-11-20 14:06:59 +010045static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
46{
Clemens Ladischf87135f2005-11-20 14:06:59 +010047 struct snd_pcm *pcm;
48
Johannes Berg9244b2c2006-10-05 16:02:22 +020049 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010050 if (pcm->card == card && pcm->device == device)
51 return pcm;
52 }
53 return NULL;
54}
55
Takashi Iwai877211f2005-11-17 13:59:38 +010056static int snd_pcm_control_ioctl(struct snd_card *card,
57 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned int cmd, unsigned long arg)
59{
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 switch (cmd) {
61 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
62 {
63 int device;
64
65 if (get_user(device, (int __user *)arg))
66 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010067 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 device = device < 0 ? 0 : device + 1;
69 while (device < SNDRV_PCM_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010070 if (snd_pcm_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
72 device++;
73 }
74 if (device == SNDRV_PCM_DEVICES)
75 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010076 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (put_user(device, (int __user *)arg))
78 return -EFAULT;
79 return 0;
80 }
81 case SNDRV_CTL_IOCTL_PCM_INFO:
82 {
Takashi Iwai877211f2005-11-17 13:59:38 +010083 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010085 int stream;
86 struct snd_pcm *pcm;
87 struct snd_pcm_str *pstr;
88 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +010089 int err;
90
Takashi Iwai877211f2005-11-17 13:59:38 +010091 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (get_user(device, &info->device))
93 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (get_user(stream, &info->stream))
95 return -EFAULT;
96 if (stream < 0 || stream > 1)
97 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (get_user(subdevice, &info->subdevice))
99 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100100 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100101 pcm = snd_pcm_search(card, device);
102 if (pcm == NULL) {
103 err = -ENXIO;
104 goto _error;
105 }
106 pstr = &pcm->streams[stream];
107 if (pstr->substream_count == 0) {
108 err = -ENOENT;
109 goto _error;
110 }
111 if (subdevice >= pstr->substream_count) {
112 err = -ENXIO;
113 goto _error;
114 }
115 for (substream = pstr->substream; substream;
116 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (substream->number == (int)subdevice)
118 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100119 if (substream == NULL) {
120 err = -ENXIO;
121 goto _error;
122 }
123 err = snd_pcm_info_user(substream, info);
124 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100125 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
129 {
130 int val;
131
132 if (get_user(val, (int __user *)arg))
133 return -EFAULT;
134 control->prefer_pcm_subdevice = val;
135 return 0;
136 }
137 }
138 return -ENOIOCTLCMD;
139}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100140
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200141#ifdef CONFIG_SND_VERBOSE_PROCFS
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
144#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
145#define READY(v) [SNDRV_PCM_READY_##v] = #v
146#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
147#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
148#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
149#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
150#define START(v) [SNDRV_PCM_START_##v] = #v
151#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
152#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static char *snd_pcm_format_names[] = {
155 FORMAT(S8),
156 FORMAT(U8),
157 FORMAT(S16_LE),
158 FORMAT(S16_BE),
159 FORMAT(U16_LE),
160 FORMAT(U16_BE),
161 FORMAT(S24_LE),
162 FORMAT(S24_BE),
163 FORMAT(U24_LE),
164 FORMAT(U24_BE),
165 FORMAT(S32_LE),
166 FORMAT(S32_BE),
167 FORMAT(U32_LE),
168 FORMAT(U32_BE),
169 FORMAT(FLOAT_LE),
170 FORMAT(FLOAT_BE),
171 FORMAT(FLOAT64_LE),
172 FORMAT(FLOAT64_BE),
173 FORMAT(IEC958_SUBFRAME_LE),
174 FORMAT(IEC958_SUBFRAME_BE),
175 FORMAT(MU_LAW),
176 FORMAT(A_LAW),
177 FORMAT(IMA_ADPCM),
178 FORMAT(MPEG),
179 FORMAT(GSM),
180 FORMAT(SPECIAL),
181 FORMAT(S24_3LE),
182 FORMAT(S24_3BE),
183 FORMAT(U24_3LE),
184 FORMAT(U24_3BE),
185 FORMAT(S20_3LE),
186 FORMAT(S20_3BE),
187 FORMAT(U20_3LE),
188 FORMAT(U20_3BE),
189 FORMAT(S18_3LE),
190 FORMAT(S18_3BE),
191 FORMAT(U18_3LE),
192 FORMAT(U18_3BE),
193};
194
Adrian Bunk12831c12006-04-11 11:12:46 +0200195static const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100196{
197 return snd_pcm_format_names[format];
198}
199
Takashi Iwaie28563c2005-12-01 10:42:42 +0100200static char *snd_pcm_stream_names[] = {
201 STREAM(PLAYBACK),
202 STREAM(CAPTURE),
203};
204
205static char *snd_pcm_state_names[] = {
206 STATE(OPEN),
207 STATE(SETUP),
208 STATE(PREPARED),
209 STATE(RUNNING),
210 STATE(XRUN),
211 STATE(DRAINING),
212 STATE(PAUSED),
213 STATE(SUSPENDED),
214};
215
216static char *snd_pcm_access_names[] = {
217 ACCESS(MMAP_INTERLEAVED),
218 ACCESS(MMAP_NONINTERLEAVED),
219 ACCESS(MMAP_COMPLEX),
220 ACCESS(RW_INTERLEAVED),
221 ACCESS(RW_NONINTERLEAVED),
222};
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static char *snd_pcm_subformat_names[] = {
225 SUBFORMAT(STD),
226};
227
228static char *snd_pcm_tstamp_mode_names[] = {
229 TSTAMP(NONE),
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100230 TSTAMP(ENABLE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Takashi Iwai877211f2005-11-17 13:59:38 +0100233static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
236 return snd_pcm_stream_names[stream];
237}
238
239static const char *snd_pcm_access_name(snd_pcm_access_t access)
240{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return snd_pcm_access_names[access];
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return snd_pcm_subformat_names[subformat];
247}
248
Takashi Iwai877211f2005-11-17 13:59:38 +0100249static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
252 return snd_pcm_tstamp_mode_names[mode];
253}
254
255static const char *snd_pcm_state_name(snd_pcm_state_t state)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return snd_pcm_state_names[state];
258}
259
260#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
261#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static const char *snd_pcm_oss_format_name(int format)
264{
265 switch (format) {
266 case AFMT_MU_LAW:
267 return "MU_LAW";
268 case AFMT_A_LAW:
269 return "A_LAW";
270 case AFMT_IMA_ADPCM:
271 return "IMA_ADPCM";
272 case AFMT_U8:
273 return "U8";
274 case AFMT_S16_LE:
275 return "S16_LE";
276 case AFMT_S16_BE:
277 return "S16_BE";
278 case AFMT_S8:
279 return "S8";
280 case AFMT_U16_LE:
281 return "U16_LE";
282 case AFMT_U16_BE:
283 return "U16_BE";
284 case AFMT_MPEG:
285 return "MPEG";
286 default:
287 return "unknown";
288 }
289}
290#endif
291
Takashi Iwai877211f2005-11-17 13:59:38 +0100292static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
293 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Takashi Iwai877211f2005-11-17 13:59:38 +0100295 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int err;
297
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200298 if (! substream)
299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 info = kmalloc(sizeof(*info), GFP_KERNEL);
302 if (! info) {
303 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
304 return;
305 }
306
307 err = snd_pcm_info(substream, info);
308 if (err < 0) {
309 snd_iprintf(buffer, "error %d\n", err);
310 kfree(info);
311 return;
312 }
313 snd_iprintf(buffer, "card: %d\n", info->card);
314 snd_iprintf(buffer, "device: %d\n", info->device);
315 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
316 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
317 snd_iprintf(buffer, "id: %s\n", info->id);
318 snd_iprintf(buffer, "name: %s\n", info->name);
319 snd_iprintf(buffer, "subname: %s\n", info->subname);
320 snd_iprintf(buffer, "class: %d\n", info->dev_class);
321 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
322 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
323 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
324 kfree(info);
325}
326
Takashi Iwai877211f2005-11-17 13:59:38 +0100327static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
328 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Takashi Iwai877211f2005-11-17 13:59:38 +0100330 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
331 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Takashi Iwai877211f2005-11-17 13:59:38 +0100334static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
335 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Takashi Iwai877211f2005-11-17 13:59:38 +0100337 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
338 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Takashi Iwai877211f2005-11-17 13:59:38 +0100341static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
342 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai877211f2005-11-17 13:59:38 +0100344 struct snd_pcm_substream *substream = entry->private_data;
345 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!runtime) {
347 snd_iprintf(buffer, "closed\n");
348 return;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
351 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
354 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
355 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
356 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
357 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
358 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
359 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
360 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
362 if (substream->oss.oss) {
363 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
364 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
365 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
366 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
367 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
368 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
369 }
370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Takashi Iwai877211f2005-11-17 13:59:38 +0100373static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
374 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Takashi Iwai877211f2005-11-17 13:59:38 +0100376 struct snd_pcm_substream *substream = entry->private_data;
377 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!runtime) {
379 snd_iprintf(buffer, "closed\n");
380 return;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
383 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return;
385 }
386 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
387 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
390 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
391 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
392 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
393 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Takashi Iwai877211f2005-11-17 13:59:38 +0100396static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
397 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Takashi Iwai877211f2005-11-17 13:59:38 +0100399 struct snd_pcm_substream *substream = entry->private_data;
400 struct snd_pcm_runtime *runtime = substream->runtime;
401 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int err;
403 if (!runtime) {
404 snd_iprintf(buffer, "closed\n");
405 return;
406 }
407 memset(&status, 0, sizeof(status));
408 err = snd_pcm_status(substream, &status);
409 if (err < 0) {
410 snd_iprintf(buffer, "error %d\n", err);
411 return;
412 }
413 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
414 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
415 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
416 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
417 status.tstamp.tv_sec, status.tstamp.tv_nsec);
418 snd_iprintf(buffer, "delay : %ld\n", status.delay);
419 snd_iprintf(buffer, "avail : %ld\n", status.avail);
420 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
421 snd_iprintf(buffer, "-----\n");
422 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
423 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
424}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200426#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100427static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
428 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Takashi Iwai877211f2005-11-17 13:59:38 +0100430 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
432}
433
Takashi Iwai877211f2005-11-17 13:59:38 +0100434static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
435 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Takashi Iwai877211f2005-11-17 13:59:38 +0100437 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 char line[64];
439 if (!snd_info_get_line(buffer, line, sizeof(line)))
440 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
441}
442#endif
443
Takashi Iwai877211f2005-11-17 13:59:38 +0100444static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwai877211f2005-11-17 13:59:38 +0100446 struct snd_pcm *pcm = pstr->pcm;
447 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 char name[16];
449
450 sprintf(name, "pcm%i%c", pcm->device,
451 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
452 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
453 return -ENOMEM;
454 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
455 if (snd_info_register(entry) < 0) {
456 snd_info_free_entry(entry);
457 return -ENOMEM;
458 }
459 pstr->proc_root = entry;
460
461 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200462 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (snd_info_register(entry) < 0) {
464 snd_info_free_entry(entry);
465 entry = NULL;
466 }
467 }
468 pstr->proc_info_entry = entry;
469
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200470#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100471 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
472 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200475 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 entry->private_data = pstr;
477 if (snd_info_register(entry) < 0) {
478 snd_info_free_entry(entry);
479 entry = NULL;
480 }
481 }
482 pstr->proc_xrun_debug_entry = entry;
483#endif
484 return 0;
485}
486
Takashi Iwai877211f2005-11-17 13:59:38 +0100487static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200489#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai746d4a02006-06-23 14:37:59 +0200490 snd_info_free_entry(pstr->proc_xrun_debug_entry);
491 pstr->proc_xrun_debug_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200493 snd_info_free_entry(pstr->proc_info_entry);
494 pstr->proc_info_entry = NULL;
495 snd_info_free_entry(pstr->proc_root);
496 pstr->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
498}
499
Takashi Iwai877211f2005-11-17 13:59:38 +0100500static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Takashi Iwai877211f2005-11-17 13:59:38 +0100502 struct snd_info_entry *entry;
503 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 char name[16];
505
506 card = substream->pcm->card;
507
508 sprintf(name, "sub%i", substream->number);
509 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
510 return -ENOMEM;
511 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
512 if (snd_info_register(entry) < 0) {
513 snd_info_free_entry(entry);
514 return -ENOMEM;
515 }
516 substream->proc_root = entry;
517
518 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200519 snd_info_set_text_ops(entry, substream,
520 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (snd_info_register(entry) < 0) {
522 snd_info_free_entry(entry);
523 entry = NULL;
524 }
525 }
526 substream->proc_info_entry = entry;
527
528 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200529 snd_info_set_text_ops(entry, substream,
530 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
533 entry = NULL;
534 }
535 }
536 substream->proc_hw_params_entry = entry;
537
538 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200539 snd_info_set_text_ops(entry, substream,
540 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (snd_info_register(entry) < 0) {
542 snd_info_free_entry(entry);
543 entry = NULL;
544 }
545 }
546 substream->proc_sw_params_entry = entry;
547
548 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200549 snd_info_set_text_ops(entry, substream,
550 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (snd_info_register(entry) < 0) {
552 snd_info_free_entry(entry);
553 entry = NULL;
554 }
555 }
556 substream->proc_status_entry = entry;
557
558 return 0;
559}
Takashi Iwai746d4a02006-06-23 14:37:59 +0200560
Takashi Iwai877211f2005-11-17 13:59:38 +0100561static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200563 snd_info_free_entry(substream->proc_info_entry);
564 substream->proc_info_entry = NULL;
565 snd_info_free_entry(substream->proc_hw_params_entry);
566 substream->proc_hw_params_entry = NULL;
567 snd_info_free_entry(substream->proc_sw_params_entry);
568 substream->proc_sw_params_entry = NULL;
569 snd_info_free_entry(substream->proc_status_entry);
570 substream->proc_status_entry = NULL;
571 snd_info_free_entry(substream->proc_root);
572 substream->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 0;
574}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200575#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100576static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
577static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
578static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
579static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200580#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582/**
583 * snd_pcm_new_stream - create a new PCM stream
584 * @pcm: the pcm instance
585 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
586 * @substream_count: the number of substreams
587 *
588 * Creates a new stream for the pcm.
589 * The corresponding stream on the pcm must have been empty before
590 * calling this, i.e. zero must be given to the argument of
591 * snd_pcm_new().
592 *
593 * Returns zero if successful, or a negative error code on failure.
594 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100595int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100598 struct snd_pcm_str *pstr = &pcm->streams[stream];
599 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100602 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#endif
604 pstr->stream = stream;
605 pstr->pcm = pcm;
606 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (substream_count > 0) {
608 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100609 if (err < 0) {
610 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 prev = NULL;
615 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200616 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100617 if (substream == NULL) {
618 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 substream->pcm = pcm;
622 substream->pstr = pstr;
623 substream->number = idx;
624 substream->stream = stream;
625 sprintf(substream->name, "subdevice #%i", idx);
Takashi Iwai9442e692006-09-30 23:27:19 -0700626 snprintf(substream->latency_id, sizeof(substream->latency_id),
627 "ALSA-PCM%d-%d%c%d", pcm->card->number, pcm->device,
628 (stream ? 'c' : 'p'), idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 substream->buffer_bytes_max = UINT_MAX;
630 if (prev == NULL)
631 pstr->substream = substream;
632 else
633 prev->next = substream;
634 err = snd_pcm_substream_proc_init(substream);
635 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100636 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Akinobu Mita4d361282006-11-23 12:03:24 +0100637 if (prev == NULL)
638 pstr->substream = NULL;
639 else
640 prev->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree(substream);
642 return err;
643 }
644 substream->group = &substream->self_group;
645 spin_lock_init(&substream->self_group.lock);
646 INIT_LIST_HEAD(&substream->self_group.substreams);
647 list_add_tail(&substream->link_list, &substream->self_group.substreams);
648 spin_lock_init(&substream->timer_lock);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200649 atomic_set(&substream->mmap_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 prev = substream;
651 }
652 return 0;
653}
654
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200655EXPORT_SYMBOL(snd_pcm_new_stream);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/**
658 * snd_pcm_new - create a new PCM instance
659 * @card: the card instance
660 * @id: the id string
661 * @device: the device index (zero based)
662 * @playback_count: the number of substreams for playback
663 * @capture_count: the number of substreams for capture
664 * @rpcm: the pointer to store the new pcm instance
665 *
666 * Creates a new PCM instance.
667 *
668 * The pcm operators have to be set afterwards to the new instance
669 * via snd_pcm_set_ops().
670 *
671 * Returns zero if successful, or a negative error code on failure.
672 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100673int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100675 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100679 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 .dev_free = snd_pcm_dev_free,
681 .dev_register = snd_pcm_dev_register,
682 .dev_disconnect = snd_pcm_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 };
684
685 snd_assert(rpcm != NULL, return -EINVAL);
686 *rpcm = NULL;
687 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200688 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100689 if (pcm == NULL) {
690 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pcm->card = card;
694 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100695 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
698 snd_pcm_free(pcm);
699 return err;
700 }
701 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
702 snd_pcm_free(pcm);
703 return err;
704 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100705 mutex_init(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 init_waitqueue_head(&pcm->open_wait);
707 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
708 snd_pcm_free(pcm);
709 return err;
710 }
711 *rpcm = pcm;
712 return 0;
713}
714
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200715EXPORT_SYMBOL(snd_pcm_new);
716
Takashi Iwai877211f2005-11-17 13:59:38 +0100717static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Takashi Iwai877211f2005-11-17 13:59:38 +0100719 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100721 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722#endif
723 substream = pstr->substream;
724 while (substream) {
725 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200726 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 snd_pcm_substream_proc_done(substream);
728 kfree(substream);
729 substream = substream_next;
730 }
731 snd_pcm_stream_proc_done(pstr);
732#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
733 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
734 setupn = setup->next;
735 kfree(setup->task_name);
736 kfree(setup);
737 }
738#endif
739}
740
Takashi Iwai877211f2005-11-17 13:59:38 +0100741static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Takashi Iwaic4614822006-06-23 14:38:23 +0200743 struct snd_pcm_notify *notify;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 snd_assert(pcm != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200746 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
747 notify->n_unregister(pcm);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (pcm->private_free)
750 pcm->private_free(pcm);
751 snd_pcm_lib_preallocate_free_for_all(pcm);
752 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
753 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
754 kfree(pcm);
755 return 0;
756}
757
Takashi Iwai877211f2005-11-17 13:59:38 +0100758static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Takashi Iwai877211f2005-11-17 13:59:38 +0100760 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return snd_pcm_free(pcm);
762}
763
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200764int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
765 struct file *file,
766 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai877211f2005-11-17 13:59:38 +0100768 struct snd_pcm_str * pstr;
769 struct snd_pcm_substream *substream;
770 struct snd_pcm_runtime *runtime;
771 struct snd_ctl_file *kctl;
772 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int prefer_subdevice = -1;
774 size_t size;
775
776 snd_assert(rsubstream != NULL, return -EINVAL);
777 *rsubstream = NULL;
778 snd_assert(pcm != NULL, return -ENXIO);
779 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200780 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENODEV;
782
783 card = pcm->card;
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200784 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200785 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (kctl->pid == current->pid) {
787 prefer_subdevice = kctl->prefer_pcm_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200788 if (prefer_subdevice != -1)
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791 }
Takashi Iwai399ccdc2008-09-25 14:51:03 +0200792 read_unlock(&card->ctl_files_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 switch (stream) {
795 case SNDRV_PCM_STREAM_PLAYBACK:
796 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
797 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
798 if (SUBSTREAM_BUSY(substream))
799 return -EAGAIN;
800 }
801 }
802 break;
803 case SNDRV_PCM_STREAM_CAPTURE:
804 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
805 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
806 if (SUBSTREAM_BUSY(substream))
807 return -EAGAIN;
808 }
809 }
810 break;
811 default:
812 return -EINVAL;
813 }
814
Takashi Iwai0df63e42006-04-28 15:13:41 +0200815 if (file->f_flags & O_APPEND) {
816 if (prefer_subdevice < 0) {
817 if (pstr->substream_count > 1)
818 return -EINVAL; /* must be unique */
819 substream = pstr->substream;
820 } else {
821 for (substream = pstr->substream; substream;
822 substream = substream->next)
823 if (substream->number == prefer_subdevice)
824 break;
825 }
826 if (! substream)
827 return -ENODEV;
828 if (! SUBSTREAM_BUSY(substream))
829 return -EBADFD;
830 substream->ref_count++;
831 *rsubstream = substream;
832 return 0;
833 }
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (prefer_subdevice >= 0) {
836 for (substream = pstr->substream; substream; substream = substream->next)
837 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
838 goto __ok;
839 }
840 for (substream = pstr->substream; substream; substream = substream->next)
841 if (!SUBSTREAM_BUSY(substream))
842 break;
843 __ok:
844 if (substream == NULL)
845 return -EAGAIN;
846
Takashi Iwaica2c0962005-09-09 14:20:23 +0200847 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (runtime == NULL)
849 return -ENOMEM;
850
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
853 if (runtime->status == NULL) {
854 kfree(runtime);
855 return -ENOMEM;
856 }
857 memset((void*)runtime->status, 0, size);
858
Takashi Iwai877211f2005-11-17 13:59:38 +0100859 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
861 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100862 snd_free_pages((void*)runtime->status,
863 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 kfree(runtime);
865 return -ENOMEM;
866 }
867 memset((void*)runtime->control, 0, size);
868
869 init_waitqueue_head(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 runtime->status->state = SNDRV_PCM_STATE_OPEN;
872
873 substream->runtime = runtime;
874 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200875 substream->ref_count = 1;
876 substream->f_flags = file->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pstr->substream_opened++;
878 *rsubstream = substream;
879 return 0;
880}
881
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200882void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwai877211f2005-11-17 13:59:38 +0100884 struct snd_pcm_runtime *runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 runtime = substream->runtime;
887 snd_assert(runtime != NULL, return);
888 if (runtime->private_free != NULL)
889 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100890 snd_free_pages((void*)runtime->status,
891 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
892 snd_free_pages((void*)runtime->control,
893 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 kfree(runtime->hw_constraints.rules);
895 kfree(runtime);
896 substream->runtime = NULL;
897 substream->pstr->substream_opened--;
898}
899
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700900static ssize_t show_pcm_class(struct device *dev,
901 struct device_attribute *attr, char *buf)
Takashi Iwai9d19f482006-09-06 14:27:46 +0200902{
903 struct snd_pcm *pcm;
904 const char *str;
905 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
906 [SNDRV_PCM_CLASS_GENERIC] = "generic",
907 [SNDRV_PCM_CLASS_MULTI] = "multi",
908 [SNDRV_PCM_CLASS_MODEM] = "modem",
909 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
910 };
911
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700912 if (! (pcm = dev_get_drvdata(dev)) ||
Takashi Iwai9d19f482006-09-06 14:27:46 +0200913 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
914 str = "none";
915 else
916 str = strs[pcm->dev_class];
917 return snprintf(buf, PAGE_SIZE, "%s\n", str);
918}
919
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700920static struct device_attribute pcm_attrs =
Takashi Iwai9d19f482006-09-06 14:27:46 +0200921 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
922
Takashi Iwai877211f2005-11-17 13:59:38 +0100923static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100925 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100926 struct snd_pcm_substream *substream;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200927 struct snd_pcm_notify *notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100929 struct snd_pcm *pcm = device->device_data;
Johannes Bergc78085f2006-10-05 15:06:34 +0200930 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100933 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100934 if (snd_pcm_search(pcm->card, pcm->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100935 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -EBUSY;
937 }
Clemens Ladischf87135f2005-11-20 14:06:59 +0100938 list_add_tail(&pcm->list, &snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (cidx = 0; cidx < 2; cidx++) {
940 int devtype = -1;
941 if (pcm->streams[cidx].substream == NULL)
942 continue;
943 switch (cidx) {
944 case SNDRV_PCM_STREAM_PLAYBACK:
945 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
947 break;
948 case SNDRV_PCM_STREAM_CAPTURE:
949 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
951 break;
952 }
Johannes Bergc78085f2006-10-05 15:06:34 +0200953 /* device pointer to use, pcm->dev takes precedence if
954 * it is assigned, otherwise fall back to card's device
955 * if possible */
956 dev = pcm->dev;
957 if (!dev)
Takashi Iwaic2902c82007-02-09 16:25:48 +0100958 dev = snd_card_get_device_link(pcm->card);
Johannes Bergc78085f2006-10-05 15:06:34 +0200959 /* register pcm */
960 err = snd_register_device_for_dev(devtype, pcm->card,
961 pcm->device,
962 &snd_pcm_f_ops[cidx],
963 pcm, str, dev);
964 if (err < 0) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100965 list_del(&pcm->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100966 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return err;
968 }
Takashi Iwai9d19f482006-09-06 14:27:46 +0200969 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
970 &pcm_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
972 snd_pcm_timer_init(substream);
973 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200974
975 list_for_each_entry(notify, &snd_pcm_notify_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 notify->n_register(pcm);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200977
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100978 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Takashi Iwai877211f2005-11-17 13:59:38 +0100982static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Takashi Iwai877211f2005-11-17 13:59:38 +0100984 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200985 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +0100986 struct snd_pcm_substream *substream;
Takashi Iwaic4614822006-06-23 14:38:23 +0200987 int cidx, devtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100989 mutex_lock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +0200990 if (list_empty(&pcm->list))
991 goto unlock;
992
Clemens Ladischf87135f2005-11-20 14:06:59 +0100993 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 for (cidx = 0; cidx < 2; cidx++)
995 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
996 if (substream->runtime)
997 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwaic4614822006-06-23 14:38:23 +0200998 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 notify->n_disconnect(pcm);
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 for (cidx = 0; cidx < 2; cidx++) {
1002 devtype = -1;
1003 switch (cidx) {
1004 case SNDRV_PCM_STREAM_PLAYBACK:
1005 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1006 break;
1007 case SNDRV_PCM_STREAM_CAPTURE:
1008 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1009 break;
1010 }
1011 snd_unregister_device(devtype, pcm->card, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001013 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001014 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Takashi Iwai877211f2005-11-17 13:59:38 +01001018int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Johannes Berg9244b2c2006-10-05 16:02:22 +02001020 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Takashi Iwaic4614822006-06-23 14:38:23 +02001022 snd_assert(notify != NULL &&
1023 notify->n_register != NULL &&
1024 notify->n_unregister != NULL &&
1025 notify->n_disconnect, return -EINVAL);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001026 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (nfree) {
1028 list_del(&notify->list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001029 list_for_each_entry(pcm, &snd_pcm_devices, list)
1030 notify->n_unregister(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 } else {
1032 list_add_tail(&notify->list, &snd_pcm_notify_list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001033 list_for_each_entry(pcm, &snd_pcm_devices, list)
1034 notify->n_register(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001036 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 0;
1038}
1039
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001040EXPORT_SYMBOL(snd_pcm_notify);
1041
Takashi Iwaie28563c2005-12-01 10:42:42 +01001042#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * Info interface
1045 */
1046
Takashi Iwai877211f2005-11-17 13:59:38 +01001047static void snd_pcm_proc_read(struct snd_info_entry *entry,
1048 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001052 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001053 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01001054 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1055 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001057 snd_iprintf(buffer, " : playback %i",
1058 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001060 snd_iprintf(buffer, " : capture %i",
1061 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 snd_iprintf(buffer, "\n");
1063 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001064 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001067static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Takashi Iwaie28563c2005-12-01 10:42:42 +01001069static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001074 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (snd_info_register(entry) < 0) {
1076 snd_info_free_entry(entry);
1077 entry = NULL;
1078 }
1079 }
1080 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001081}
1082
1083static void snd_pcm_proc_done(void)
1084{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001085 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001086}
1087
1088#else /* !CONFIG_PROC_FS */
1089#define snd_pcm_proc_init()
1090#define snd_pcm_proc_done()
1091#endif /* CONFIG_PROC_FS */
1092
1093
1094/*
1095 * ENTRY functions
1096 */
1097
1098static int __init alsa_pcm_init(void)
1099{
1100 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1101 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1102 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 0;
1104}
1105
1106static void __exit alsa_pcm_exit(void)
1107{
1108 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1109 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001110 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113module_init(alsa_pcm_init)
1114module_exit(alsa_pcm_exit)