blob: 76fcc5234d83dc85820ccd2a32982002ef3a81b8 [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{
Clemens Ladischf87135f2005-11-20 14:06:59 +010048 struct snd_pcm *pcm;
49
Johannes Berg9244b2c2006-10-05 16:02:22 +020050 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010051 if (pcm->card == card && pcm->device == device)
52 return pcm;
53 }
54 return NULL;
55}
56
Takashi Iwai877211f2005-11-17 13:59:38 +010057static int snd_pcm_control_ioctl(struct snd_card *card,
58 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int cmd, unsigned long arg)
60{
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 switch (cmd) {
62 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
63 {
64 int device;
65
66 if (get_user(device, (int __user *)arg))
67 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010068 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 device = device < 0 ? 0 : device + 1;
70 while (device < SNDRV_PCM_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010071 if (snd_pcm_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 break;
73 device++;
74 }
75 if (device == SNDRV_PCM_DEVICES)
76 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010077 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (put_user(device, (int __user *)arg))
79 return -EFAULT;
80 return 0;
81 }
82 case SNDRV_CTL_IOCTL_PCM_INFO:
83 {
Takashi Iwai877211f2005-11-17 13:59:38 +010084 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010086 int stream;
87 struct snd_pcm *pcm;
88 struct snd_pcm_str *pstr;
89 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +010090 int err;
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (get_user(device, &info->device))
94 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (get_user(stream, &info->stream))
96 return -EFAULT;
97 if (stream < 0 || stream > 1)
98 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (get_user(subdevice, &info->subdevice))
100 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100101 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100102 pcm = snd_pcm_search(card, device);
103 if (pcm == NULL) {
104 err = -ENXIO;
105 goto _error;
106 }
107 pstr = &pcm->streams[stream];
108 if (pstr->substream_count == 0) {
109 err = -ENOENT;
110 goto _error;
111 }
112 if (subdevice >= pstr->substream_count) {
113 err = -ENXIO;
114 goto _error;
115 }
116 for (substream = pstr->substream; substream;
117 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (substream->number == (int)subdevice)
119 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100120 if (substream == NULL) {
121 err = -ENXIO;
122 goto _error;
123 }
124 err = snd_pcm_info_user(substream, info);
125 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100126 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
130 {
131 int val;
132
133 if (get_user(val, (int __user *)arg))
134 return -EFAULT;
135 control->prefer_pcm_subdevice = val;
136 return 0;
137 }
138 }
139 return -ENOIOCTLCMD;
140}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100141
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200142#ifdef CONFIG_SND_VERBOSE_PROCFS
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
145#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
146#define READY(v) [SNDRV_PCM_READY_##v] = #v
147#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
148#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
149#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
150#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
151#define START(v) [SNDRV_PCM_START_##v] = #v
152#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
153#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static char *snd_pcm_format_names[] = {
156 FORMAT(S8),
157 FORMAT(U8),
158 FORMAT(S16_LE),
159 FORMAT(S16_BE),
160 FORMAT(U16_LE),
161 FORMAT(U16_BE),
162 FORMAT(S24_LE),
163 FORMAT(S24_BE),
164 FORMAT(U24_LE),
165 FORMAT(U24_BE),
166 FORMAT(S32_LE),
167 FORMAT(S32_BE),
168 FORMAT(U32_LE),
169 FORMAT(U32_BE),
170 FORMAT(FLOAT_LE),
171 FORMAT(FLOAT_BE),
172 FORMAT(FLOAT64_LE),
173 FORMAT(FLOAT64_BE),
174 FORMAT(IEC958_SUBFRAME_LE),
175 FORMAT(IEC958_SUBFRAME_BE),
176 FORMAT(MU_LAW),
177 FORMAT(A_LAW),
178 FORMAT(IMA_ADPCM),
179 FORMAT(MPEG),
180 FORMAT(GSM),
181 FORMAT(SPECIAL),
182 FORMAT(S24_3LE),
183 FORMAT(S24_3BE),
184 FORMAT(U24_3LE),
185 FORMAT(U24_3BE),
186 FORMAT(S20_3LE),
187 FORMAT(S20_3BE),
188 FORMAT(U20_3LE),
189 FORMAT(U20_3BE),
190 FORMAT(S18_3LE),
191 FORMAT(S18_3BE),
192 FORMAT(U18_3LE),
193 FORMAT(U18_3BE),
194};
195
Adrian Bunk12831c12006-04-11 11:12:46 +0200196static const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100197{
198 return snd_pcm_format_names[format];
199}
200
Takashi Iwaie28563c2005-12-01 10:42:42 +0100201static char *snd_pcm_stream_names[] = {
202 STREAM(PLAYBACK),
203 STREAM(CAPTURE),
204};
205
206static char *snd_pcm_state_names[] = {
207 STATE(OPEN),
208 STATE(SETUP),
209 STATE(PREPARED),
210 STATE(RUNNING),
211 STATE(XRUN),
212 STATE(DRAINING),
213 STATE(PAUSED),
214 STATE(SUSPENDED),
215};
216
217static char *snd_pcm_access_names[] = {
218 ACCESS(MMAP_INTERLEAVED),
219 ACCESS(MMAP_NONINTERLEAVED),
220 ACCESS(MMAP_COMPLEX),
221 ACCESS(RW_INTERLEAVED),
222 ACCESS(RW_NONINTERLEAVED),
223};
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static char *snd_pcm_subformat_names[] = {
226 SUBFORMAT(STD),
227};
228
229static char *snd_pcm_tstamp_mode_names[] = {
230 TSTAMP(NONE),
231 TSTAMP(MMAP),
232};
233
Takashi Iwai877211f2005-11-17 13:59:38 +0100234static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
237 return snd_pcm_stream_names[stream];
238}
239
240static const char *snd_pcm_access_name(snd_pcm_access_t access)
241{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return snd_pcm_access_names[access];
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return snd_pcm_subformat_names[subformat];
248}
249
Takashi Iwai877211f2005-11-17 13:59:38 +0100250static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
253 return snd_pcm_tstamp_mode_names[mode];
254}
255
256static const char *snd_pcm_state_name(snd_pcm_state_t state)
257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return snd_pcm_state_names[state];
259}
260
261#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
262#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static const char *snd_pcm_oss_format_name(int format)
265{
266 switch (format) {
267 case AFMT_MU_LAW:
268 return "MU_LAW";
269 case AFMT_A_LAW:
270 return "A_LAW";
271 case AFMT_IMA_ADPCM:
272 return "IMA_ADPCM";
273 case AFMT_U8:
274 return "U8";
275 case AFMT_S16_LE:
276 return "S16_LE";
277 case AFMT_S16_BE:
278 return "S16_BE";
279 case AFMT_S8:
280 return "S8";
281 case AFMT_U16_LE:
282 return "U16_LE";
283 case AFMT_U16_BE:
284 return "U16_BE";
285 case AFMT_MPEG:
286 return "MPEG";
287 default:
288 return "unknown";
289 }
290}
291#endif
292
Takashi Iwai877211f2005-11-17 13:59:38 +0100293static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
294 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Takashi Iwai877211f2005-11-17 13:59:38 +0100296 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int err;
298
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200299 if (! substream)
300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 info = kmalloc(sizeof(*info), GFP_KERNEL);
303 if (! info) {
304 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
305 return;
306 }
307
308 err = snd_pcm_info(substream, info);
309 if (err < 0) {
310 snd_iprintf(buffer, "error %d\n", err);
311 kfree(info);
312 return;
313 }
314 snd_iprintf(buffer, "card: %d\n", info->card);
315 snd_iprintf(buffer, "device: %d\n", info->device);
316 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
317 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
318 snd_iprintf(buffer, "id: %s\n", info->id);
319 snd_iprintf(buffer, "name: %s\n", info->name);
320 snd_iprintf(buffer, "subname: %s\n", info->subname);
321 snd_iprintf(buffer, "class: %d\n", info->dev_class);
322 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
323 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
324 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
325 kfree(info);
326}
327
Takashi Iwai877211f2005-11-17 13:59:38 +0100328static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
329 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Takashi Iwai877211f2005-11-17 13:59:38 +0100331 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
332 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Takashi Iwai877211f2005-11-17 13:59:38 +0100335static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
336 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Takashi Iwai877211f2005-11-17 13:59:38 +0100338 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
339 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Takashi Iwai877211f2005-11-17 13:59:38 +0100342static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
343 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Takashi Iwai877211f2005-11-17 13:59:38 +0100345 struct snd_pcm_substream *substream = entry->private_data;
346 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (!runtime) {
348 snd_iprintf(buffer, "closed\n");
349 return;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
352 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return;
354 }
355 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
356 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
357 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
358 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
359 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
360 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
361 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
362 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
363#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
364 if (substream->oss.oss) {
365 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
366 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
367 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
368 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
369 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
370 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
371 }
372#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Takashi Iwai877211f2005-11-17 13:59:38 +0100375static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
376 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Takashi Iwai877211f2005-11-17 13:59:38 +0100378 struct snd_pcm_substream *substream = entry->private_data;
379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!runtime) {
381 snd_iprintf(buffer, "closed\n");
382 return;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
385 snd_iprintf(buffer, "no setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return;
387 }
388 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
389 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
390 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
391 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
392 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
393 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
394 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
395 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
396 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
397 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Takashi Iwai877211f2005-11-17 13:59:38 +0100400static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
401 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Takashi Iwai877211f2005-11-17 13:59:38 +0100403 struct snd_pcm_substream *substream = entry->private_data;
404 struct snd_pcm_runtime *runtime = substream->runtime;
405 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int err;
407 if (!runtime) {
408 snd_iprintf(buffer, "closed\n");
409 return;
410 }
411 memset(&status, 0, sizeof(status));
412 err = snd_pcm_status(substream, &status);
413 if (err < 0) {
414 snd_iprintf(buffer, "error %d\n", err);
415 return;
416 }
417 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
418 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
419 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
420 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
421 status.tstamp.tv_sec, status.tstamp.tv_nsec);
422 snd_iprintf(buffer, "delay : %ld\n", status.delay);
423 snd_iprintf(buffer, "avail : %ld\n", status.avail);
424 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
425 snd_iprintf(buffer, "-----\n");
426 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
427 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
428}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200430#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100431static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
432 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwai877211f2005-11-17 13:59:38 +0100434 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
436}
437
Takashi Iwai877211f2005-11-17 13:59:38 +0100438static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
439 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwai877211f2005-11-17 13:59:38 +0100441 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 char line[64];
443 if (!snd_info_get_line(buffer, line, sizeof(line)))
444 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
445}
446#endif
447
Takashi Iwai877211f2005-11-17 13:59:38 +0100448static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai877211f2005-11-17 13:59:38 +0100450 struct snd_pcm *pcm = pstr->pcm;
451 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 char name[16];
453
454 sprintf(name, "pcm%i%c", pcm->device,
455 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
456 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
457 return -ENOMEM;
458 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
459 if (snd_info_register(entry) < 0) {
460 snd_info_free_entry(entry);
461 return -ENOMEM;
462 }
463 pstr->proc_root = entry;
464
465 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200466 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (snd_info_register(entry) < 0) {
468 snd_info_free_entry(entry);
469 entry = NULL;
470 }
471 }
472 pstr->proc_info_entry = entry;
473
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200474#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100475 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
476 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200479 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 entry->private_data = pstr;
481 if (snd_info_register(entry) < 0) {
482 snd_info_free_entry(entry);
483 entry = NULL;
484 }
485 }
486 pstr->proc_xrun_debug_entry = entry;
487#endif
488 return 0;
489}
490
Takashi Iwai877211f2005-11-17 13:59:38 +0100491static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200493#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai746d4a02006-06-23 14:37:59 +0200494 snd_info_free_entry(pstr->proc_xrun_debug_entry);
495 pstr->proc_xrun_debug_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
Takashi Iwai746d4a02006-06-23 14:37:59 +0200497 snd_info_free_entry(pstr->proc_info_entry);
498 pstr->proc_info_entry = NULL;
499 snd_info_free_entry(pstr->proc_root);
500 pstr->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
Takashi Iwai877211f2005-11-17 13:59:38 +0100504static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Takashi Iwai877211f2005-11-17 13:59:38 +0100506 struct snd_info_entry *entry;
507 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char name[16];
509
510 card = substream->pcm->card;
511
512 sprintf(name, "sub%i", substream->number);
513 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
514 return -ENOMEM;
515 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
516 if (snd_info_register(entry) < 0) {
517 snd_info_free_entry(entry);
518 return -ENOMEM;
519 }
520 substream->proc_root = entry;
521
522 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200523 snd_info_set_text_ops(entry, substream,
524 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (snd_info_register(entry) < 0) {
526 snd_info_free_entry(entry);
527 entry = NULL;
528 }
529 }
530 substream->proc_info_entry = entry;
531
532 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200533 snd_info_set_text_ops(entry, substream,
534 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (snd_info_register(entry) < 0) {
536 snd_info_free_entry(entry);
537 entry = NULL;
538 }
539 }
540 substream->proc_hw_params_entry = entry;
541
542 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200543 snd_info_set_text_ops(entry, substream,
544 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (snd_info_register(entry) < 0) {
546 snd_info_free_entry(entry);
547 entry = NULL;
548 }
549 }
550 substream->proc_sw_params_entry = entry;
551
552 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200553 snd_info_set_text_ops(entry, substream,
554 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (snd_info_register(entry) < 0) {
556 snd_info_free_entry(entry);
557 entry = NULL;
558 }
559 }
560 substream->proc_status_entry = entry;
561
562 return 0;
563}
Takashi Iwai746d4a02006-06-23 14:37:59 +0200564
Takashi Iwai877211f2005-11-17 13:59:38 +0100565static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200567 snd_info_free_entry(substream->proc_info_entry);
568 substream->proc_info_entry = NULL;
569 snd_info_free_entry(substream->proc_hw_params_entry);
570 substream->proc_hw_params_entry = NULL;
571 snd_info_free_entry(substream->proc_sw_params_entry);
572 substream->proc_sw_params_entry = NULL;
573 snd_info_free_entry(substream->proc_status_entry);
574 substream->proc_status_entry = NULL;
575 snd_info_free_entry(substream->proc_root);
576 substream->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200579#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100580static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
581static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
582static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
583static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200584#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/**
587 * snd_pcm_new_stream - create a new PCM stream
588 * @pcm: the pcm instance
589 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
590 * @substream_count: the number of substreams
591 *
592 * Creates a new stream for the pcm.
593 * The corresponding stream on the pcm must have been empty before
594 * calling this, i.e. zero must be given to the argument of
595 * snd_pcm_new().
596 *
597 * Returns zero if successful, or a negative error code on failure.
598 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100599int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100602 struct snd_pcm_str *pstr = &pcm->streams[stream];
603 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100606 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
608 pstr->stream = stream;
609 pstr->pcm = pcm;
610 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (substream_count > 0) {
612 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100613 if (err < 0) {
614 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 prev = NULL;
619 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200620 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100621 if (substream == NULL) {
622 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 substream->pcm = pcm;
626 substream->pstr = pstr;
627 substream->number = idx;
628 substream->stream = stream;
629 sprintf(substream->name, "subdevice #%i", idx);
Takashi Iwai9442e692006-09-30 23:27:19 -0700630 snprintf(substream->latency_id, sizeof(substream->latency_id),
631 "ALSA-PCM%d-%d%c%d", pcm->card->number, pcm->device,
632 (stream ? 'c' : 'p'), idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 substream->buffer_bytes_max = UINT_MAX;
634 if (prev == NULL)
635 pstr->substream = substream;
636 else
637 prev->next = substream;
638 err = snd_pcm_substream_proc_init(substream);
639 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100640 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Akinobu Mita4d361282006-11-23 12:03:24 +0100641 if (prev == NULL)
642 pstr->substream = NULL;
643 else
644 prev->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 kfree(substream);
646 return err;
647 }
648 substream->group = &substream->self_group;
649 spin_lock_init(&substream->self_group.lock);
650 INIT_LIST_HEAD(&substream->self_group.substreams);
651 list_add_tail(&substream->link_list, &substream->self_group.substreams);
652 spin_lock_init(&substream->timer_lock);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200653 atomic_set(&substream->mmap_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 prev = substream;
655 }
656 return 0;
657}
658
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200659EXPORT_SYMBOL(snd_pcm_new_stream);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/**
662 * snd_pcm_new - create a new PCM instance
663 * @card: the card instance
664 * @id: the id string
665 * @device: the device index (zero based)
666 * @playback_count: the number of substreams for playback
667 * @capture_count: the number of substreams for capture
668 * @rpcm: the pointer to store the new pcm instance
669 *
670 * Creates a new PCM instance.
671 *
672 * The pcm operators have to be set afterwards to the new instance
673 * via snd_pcm_set_ops().
674 *
675 * Returns zero if successful, or a negative error code on failure.
676 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100677int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100679 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Takashi Iwai877211f2005-11-17 13:59:38 +0100681 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100683 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 .dev_free = snd_pcm_dev_free,
685 .dev_register = snd_pcm_dev_register,
686 .dev_disconnect = snd_pcm_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 };
688
689 snd_assert(rpcm != NULL, return -EINVAL);
690 *rpcm = NULL;
691 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200692 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100693 if (pcm == NULL) {
694 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 pcm->card = card;
698 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100699 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
702 snd_pcm_free(pcm);
703 return err;
704 }
705 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
706 snd_pcm_free(pcm);
707 return err;
708 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100709 mutex_init(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 init_waitqueue_head(&pcm->open_wait);
711 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
712 snd_pcm_free(pcm);
713 return err;
714 }
715 *rpcm = pcm;
716 return 0;
717}
718
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200719EXPORT_SYMBOL(snd_pcm_new);
720
Takashi Iwai877211f2005-11-17 13:59:38 +0100721static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai877211f2005-11-17 13:59:38 +0100723 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100725 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#endif
727 substream = pstr->substream;
728 while (substream) {
729 substream_next = substream->next;
Takashi Iwaic4614822006-06-23 14:38:23 +0200730 snd_pcm_timer_done(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 snd_pcm_substream_proc_done(substream);
732 kfree(substream);
733 substream = substream_next;
734 }
735 snd_pcm_stream_proc_done(pstr);
736#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
737 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
738 setupn = setup->next;
739 kfree(setup->task_name);
740 kfree(setup);
741 }
742#endif
743}
744
Takashi Iwai877211f2005-11-17 13:59:38 +0100745static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwaic4614822006-06-23 14:38:23 +0200747 struct snd_pcm_notify *notify;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 snd_assert(pcm != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200750 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
751 notify->n_unregister(pcm);
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (pcm->private_free)
754 pcm->private_free(pcm);
755 snd_pcm_lib_preallocate_free_for_all(pcm);
756 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
757 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
758 kfree(pcm);
759 return 0;
760}
761
Takashi Iwai877211f2005-11-17 13:59:38 +0100762static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwai877211f2005-11-17 13:59:38 +0100764 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return snd_pcm_free(pcm);
766}
767
768static void snd_pcm_tick_timer_func(unsigned long data)
769{
Takashi Iwai877211f2005-11-17 13:59:38 +0100770 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 snd_pcm_tick_elapsed(substream);
772}
773
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200774int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
775 struct file *file,
776 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai877211f2005-11-17 13:59:38 +0100778 struct snd_pcm_str * pstr;
779 struct snd_pcm_substream *substream;
780 struct snd_pcm_runtime *runtime;
781 struct snd_ctl_file *kctl;
782 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int prefer_subdevice = -1;
784 size_t size;
785
786 snd_assert(rsubstream != NULL, return -EINVAL);
787 *rsubstream = NULL;
788 snd_assert(pcm != NULL, return -ENXIO);
789 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200790 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return -ENODEV;
792
793 card = pcm->card;
794 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200795 list_for_each_entry(kctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (kctl->pid == current->pid) {
797 prefer_subdevice = kctl->prefer_pcm_subdevice;
Takashi Iwai2529bba2006-08-04 12:57:19 +0200798 if (prefer_subdevice != -1)
799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 }
802 up_read(&card->controls_rwsem);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 switch (stream) {
805 case SNDRV_PCM_STREAM_PLAYBACK:
806 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
807 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
808 if (SUBSTREAM_BUSY(substream))
809 return -EAGAIN;
810 }
811 }
812 break;
813 case SNDRV_PCM_STREAM_CAPTURE:
814 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
815 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
816 if (SUBSTREAM_BUSY(substream))
817 return -EAGAIN;
818 }
819 }
820 break;
821 default:
822 return -EINVAL;
823 }
824
Takashi Iwai0df63e42006-04-28 15:13:41 +0200825 if (file->f_flags & O_APPEND) {
826 if (prefer_subdevice < 0) {
827 if (pstr->substream_count > 1)
828 return -EINVAL; /* must be unique */
829 substream = pstr->substream;
830 } else {
831 for (substream = pstr->substream; substream;
832 substream = substream->next)
833 if (substream->number == prefer_subdevice)
834 break;
835 }
836 if (! substream)
837 return -ENODEV;
838 if (! SUBSTREAM_BUSY(substream))
839 return -EBADFD;
840 substream->ref_count++;
841 *rsubstream = substream;
842 return 0;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (prefer_subdevice >= 0) {
846 for (substream = pstr->substream; substream; substream = substream->next)
847 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
848 goto __ok;
849 }
850 for (substream = pstr->substream; substream; substream = substream->next)
851 if (!SUBSTREAM_BUSY(substream))
852 break;
853 __ok:
854 if (substream == NULL)
855 return -EAGAIN;
856
Takashi Iwaica2c0962005-09-09 14:20:23 +0200857 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (runtime == NULL)
859 return -ENOMEM;
860
Takashi Iwai877211f2005-11-17 13:59:38 +0100861 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
863 if (runtime->status == NULL) {
864 kfree(runtime);
865 return -ENOMEM;
866 }
867 memset((void*)runtime->status, 0, size);
868
Takashi Iwai877211f2005-11-17 13:59:38 +0100869 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
871 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 snd_free_pages((void*)runtime->status,
873 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 kfree(runtime);
875 return -ENOMEM;
876 }
877 memset((void*)runtime->control, 0, size);
878
879 init_waitqueue_head(&runtime->sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 init_timer(&runtime->tick_timer);
881 runtime->tick_timer.function = snd_pcm_tick_timer_func;
882 runtime->tick_timer.data = (unsigned long) substream;
883
884 runtime->status->state = SNDRV_PCM_STATE_OPEN;
885
886 substream->runtime = runtime;
887 substream->private_data = pcm->private_data;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200888 substream->ref_count = 1;
889 substream->f_flags = file->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 pstr->substream_opened++;
891 *rsubstream = substream;
892 return 0;
893}
894
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200895void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwai877211f2005-11-17 13:59:38 +0100897 struct snd_pcm_runtime *runtime;
Takashi Iwai0df63e42006-04-28 15:13:41 +0200898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 runtime = substream->runtime;
900 snd_assert(runtime != NULL, return);
901 if (runtime->private_free != NULL)
902 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100903 snd_free_pages((void*)runtime->status,
904 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
905 snd_free_pages((void*)runtime->control,
906 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 kfree(runtime->hw_constraints.rules);
908 kfree(runtime);
909 substream->runtime = NULL;
910 substream->pstr->substream_opened--;
911}
912
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700913static ssize_t show_pcm_class(struct device *dev,
914 struct device_attribute *attr, char *buf)
Takashi Iwai9d19f482006-09-06 14:27:46 +0200915{
916 struct snd_pcm *pcm;
917 const char *str;
918 static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
919 [SNDRV_PCM_CLASS_GENERIC] = "generic",
920 [SNDRV_PCM_CLASS_MULTI] = "multi",
921 [SNDRV_PCM_CLASS_MODEM] = "modem",
922 [SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
923 };
924
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700925 if (! (pcm = dev_get_drvdata(dev)) ||
Takashi Iwai9d19f482006-09-06 14:27:46 +0200926 pcm->dev_class > SNDRV_PCM_CLASS_LAST)
927 str = "none";
928 else
929 str = strs[pcm->dev_class];
930 return snprintf(buf, PAGE_SIZE, "%s\n", str);
931}
932
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700933static struct device_attribute pcm_attrs =
Takashi Iwai9d19f482006-09-06 14:27:46 +0200934 __ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
935
Takashi Iwai877211f2005-11-17 13:59:38 +0100936static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100938 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100939 struct snd_pcm_substream *substream;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200940 struct snd_pcm_notify *notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100942 struct snd_pcm *pcm = device->device_data;
Johannes Bergc78085f2006-10-05 15:06:34 +0200943 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100946 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100947 if (snd_pcm_search(pcm->card, pcm->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100948 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EBUSY;
950 }
Clemens Ladischf87135f2005-11-20 14:06:59 +0100951 list_add_tail(&pcm->list, &snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (cidx = 0; cidx < 2; cidx++) {
953 int devtype = -1;
954 if (pcm->streams[cidx].substream == NULL)
955 continue;
956 switch (cidx) {
957 case SNDRV_PCM_STREAM_PLAYBACK:
958 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
960 break;
961 case SNDRV_PCM_STREAM_CAPTURE:
962 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
964 break;
965 }
Johannes Bergc78085f2006-10-05 15:06:34 +0200966 /* device pointer to use, pcm->dev takes precedence if
967 * it is assigned, otherwise fall back to card's device
968 * if possible */
969 dev = pcm->dev;
970 if (!dev)
971 dev = pcm->card ? pcm->card->dev : NULL;
972 /* register pcm */
973 err = snd_register_device_for_dev(devtype, pcm->card,
974 pcm->device,
975 &snd_pcm_f_ops[cidx],
976 pcm, str, dev);
977 if (err < 0) {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100978 list_del(&pcm->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100979 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return err;
981 }
Takashi Iwai9d19f482006-09-06 14:27:46 +0200982 snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
983 &pcm_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
985 snd_pcm_timer_init(substream);
986 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200987
988 list_for_each_entry(notify, &snd_pcm_notify_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 notify->n_register(pcm);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200990
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100991 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993}
994
Takashi Iwai877211f2005-11-17 13:59:38 +0100995static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai877211f2005-11-17 13:59:38 +0100997 struct snd_pcm *pcm = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200998 struct snd_pcm_notify *notify;
Takashi Iwai877211f2005-11-17 13:59:38 +0100999 struct snd_pcm_substream *substream;
Takashi Iwaic4614822006-06-23 14:38:23 +02001000 int cidx, devtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001002 mutex_lock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001003 if (list_empty(&pcm->list))
1004 goto unlock;
1005
Clemens Ladischf87135f2005-11-20 14:06:59 +01001006 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 for (cidx = 0; cidx < 2; cidx++)
1008 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1009 if (substream->runtime)
1010 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
Takashi Iwaic4614822006-06-23 14:38:23 +02001011 list_for_each_entry(notify, &snd_pcm_notify_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 notify->n_disconnect(pcm);
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 for (cidx = 0; cidx < 2; cidx++) {
1015 devtype = -1;
1016 switch (cidx) {
1017 case SNDRV_PCM_STREAM_PLAYBACK:
1018 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1019 break;
1020 case SNDRV_PCM_STREAM_CAPTURE:
1021 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1022 break;
1023 }
1024 snd_unregister_device(devtype, pcm->card, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001026 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001027 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02001028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Takashi Iwai877211f2005-11-17 13:59:38 +01001031int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Johannes Berg9244b2c2006-10-05 16:02:22 +02001033 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Takashi Iwaic4614822006-06-23 14:38:23 +02001035 snd_assert(notify != NULL &&
1036 notify->n_register != NULL &&
1037 notify->n_unregister != NULL &&
1038 notify->n_disconnect, return -EINVAL);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001039 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (nfree) {
1041 list_del(&notify->list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001042 list_for_each_entry(pcm, &snd_pcm_devices, list)
1043 notify->n_unregister(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 } else {
1045 list_add_tail(&notify->list, &snd_pcm_notify_list);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001046 list_for_each_entry(pcm, &snd_pcm_devices, list)
1047 notify->n_register(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001049 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051}
1052
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001053EXPORT_SYMBOL(snd_pcm_notify);
1054
Takashi Iwaie28563c2005-12-01 10:42:42 +01001055#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * Info interface
1058 */
1059
Takashi Iwai877211f2005-11-17 13:59:38 +01001060static void snd_pcm_proc_read(struct snd_info_entry *entry,
1061 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Takashi Iwai877211f2005-11-17 13:59:38 +01001063 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001065 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001066 list_for_each_entry(pcm, &snd_pcm_devices, list) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01001067 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1068 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001070 snd_iprintf(buffer, " : playback %i",
1071 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001073 snd_iprintf(buffer, " : capture %i",
1074 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 snd_iprintf(buffer, "\n");
1076 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001077 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001080static struct snd_info_entry *snd_pcm_proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Takashi Iwaie28563c2005-12-01 10:42:42 +01001082static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Takashi Iwai877211f2005-11-17 13:59:38 +01001084 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001087 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (snd_info_register(entry) < 0) {
1089 snd_info_free_entry(entry);
1090 entry = NULL;
1091 }
1092 }
1093 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001094}
1095
1096static void snd_pcm_proc_done(void)
1097{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001098 snd_info_free_entry(snd_pcm_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001099}
1100
1101#else /* !CONFIG_PROC_FS */
1102#define snd_pcm_proc_init()
1103#define snd_pcm_proc_done()
1104#endif /* CONFIG_PROC_FS */
1105
1106
1107/*
1108 * ENTRY functions
1109 */
1110
1111static int __init alsa_pcm_init(void)
1112{
1113 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1114 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1115 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117}
1118
1119static void __exit alsa_pcm_exit(void)
1120{
1121 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1122 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001123 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126module_init(alsa_pcm_init)
1127module_exit(alsa_pcm_exit)