blob: 08223783cfa3317912dceb5515772e12acd5f1b7 [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);
45static int snd_pcm_dev_unregister(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Clemens Ladischf87135f2005-11-20 14:06:59 +010047static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
48{
49 struct list_head *p;
50 struct snd_pcm *pcm;
51
52 list_for_each(p, &snd_pcm_devices) {
53 pcm = list_entry(p, struct snd_pcm, list);
54 if (pcm->card == card && pcm->device == device)
55 return pcm;
56 }
57 return NULL;
58}
59
Takashi Iwai877211f2005-11-17 13:59:38 +010060static int snd_pcm_control_ioctl(struct snd_card *card,
61 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned int cmd, unsigned long arg)
63{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 switch (cmd) {
65 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
66 {
67 int device;
68
69 if (get_user(device, (int __user *)arg))
70 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010071 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 device = device < 0 ? 0 : device + 1;
73 while (device < SNDRV_PCM_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010074 if (snd_pcm_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
76 device++;
77 }
78 if (device == SNDRV_PCM_DEVICES)
79 device = -1;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010080 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (put_user(device, (int __user *)arg))
82 return -EFAULT;
83 return 0;
84 }
85 case SNDRV_CTL_IOCTL_PCM_INFO:
86 {
Takashi Iwai877211f2005-11-17 13:59:38 +010087 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010089 int stream;
90 struct snd_pcm *pcm;
91 struct snd_pcm_str *pstr;
92 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +010093 int err;
94
Takashi Iwai877211f2005-11-17 13:59:38 +010095 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (get_user(device, &info->device))
97 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (get_user(stream, &info->stream))
99 return -EFAULT;
100 if (stream < 0 || stream > 1)
101 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (get_user(subdevice, &info->subdevice))
103 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100104 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100105 pcm = snd_pcm_search(card, device);
106 if (pcm == NULL) {
107 err = -ENXIO;
108 goto _error;
109 }
110 pstr = &pcm->streams[stream];
111 if (pstr->substream_count == 0) {
112 err = -ENOENT;
113 goto _error;
114 }
115 if (subdevice >= pstr->substream_count) {
116 err = -ENXIO;
117 goto _error;
118 }
119 for (substream = pstr->substream; substream;
120 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (substream->number == (int)subdevice)
122 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100123 if (substream == NULL) {
124 err = -ENXIO;
125 goto _error;
126 }
127 err = snd_pcm_info_user(substream, info);
128 _error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100129 mutex_unlock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
133 {
134 int val;
135
136 if (get_user(val, (int __user *)arg))
137 return -EFAULT;
138 control->prefer_pcm_subdevice = val;
139 return 0;
140 }
141 }
142 return -ENOIOCTLCMD;
143}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100144
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200145#ifdef CONFIG_SND_VERBOSE_PROCFS
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
148#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
149#define READY(v) [SNDRV_PCM_READY_##v] = #v
150#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
151#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
152#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
153#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
154#define START(v) [SNDRV_PCM_START_##v] = #v
155#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
156#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static char *snd_pcm_format_names[] = {
159 FORMAT(S8),
160 FORMAT(U8),
161 FORMAT(S16_LE),
162 FORMAT(S16_BE),
163 FORMAT(U16_LE),
164 FORMAT(U16_BE),
165 FORMAT(S24_LE),
166 FORMAT(S24_BE),
167 FORMAT(U24_LE),
168 FORMAT(U24_BE),
169 FORMAT(S32_LE),
170 FORMAT(S32_BE),
171 FORMAT(U32_LE),
172 FORMAT(U32_BE),
173 FORMAT(FLOAT_LE),
174 FORMAT(FLOAT_BE),
175 FORMAT(FLOAT64_LE),
176 FORMAT(FLOAT64_BE),
177 FORMAT(IEC958_SUBFRAME_LE),
178 FORMAT(IEC958_SUBFRAME_BE),
179 FORMAT(MU_LAW),
180 FORMAT(A_LAW),
181 FORMAT(IMA_ADPCM),
182 FORMAT(MPEG),
183 FORMAT(GSM),
184 FORMAT(SPECIAL),
185 FORMAT(S24_3LE),
186 FORMAT(S24_3BE),
187 FORMAT(U24_3LE),
188 FORMAT(U24_3BE),
189 FORMAT(S20_3LE),
190 FORMAT(S20_3BE),
191 FORMAT(U20_3LE),
192 FORMAT(U20_3BE),
193 FORMAT(S18_3LE),
194 FORMAT(S18_3BE),
195 FORMAT(U18_3LE),
196 FORMAT(U18_3BE),
197};
198
Adrian Bunk12831c12006-04-11 11:12:46 +0200199static const char *snd_pcm_format_name(snd_pcm_format_t format)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100200{
201 return snd_pcm_format_names[format];
202}
203
Takashi Iwaie28563c2005-12-01 10:42:42 +0100204static char *snd_pcm_stream_names[] = {
205 STREAM(PLAYBACK),
206 STREAM(CAPTURE),
207};
208
209static char *snd_pcm_state_names[] = {
210 STATE(OPEN),
211 STATE(SETUP),
212 STATE(PREPARED),
213 STATE(RUNNING),
214 STATE(XRUN),
215 STATE(DRAINING),
216 STATE(PAUSED),
217 STATE(SUSPENDED),
218};
219
220static char *snd_pcm_access_names[] = {
221 ACCESS(MMAP_INTERLEAVED),
222 ACCESS(MMAP_NONINTERLEAVED),
223 ACCESS(MMAP_COMPLEX),
224 ACCESS(RW_INTERLEAVED),
225 ACCESS(RW_NONINTERLEAVED),
226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static char *snd_pcm_subformat_names[] = {
229 SUBFORMAT(STD),
230};
231
232static char *snd_pcm_tstamp_mode_names[] = {
233 TSTAMP(NONE),
234 TSTAMP(MMAP),
235};
236
Takashi Iwai877211f2005-11-17 13:59:38 +0100237static const char *snd_pcm_stream_name(int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
240 return snd_pcm_stream_names[stream];
241}
242
243static const char *snd_pcm_access_name(snd_pcm_access_t access)
244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return snd_pcm_access_names[access];
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
249{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return snd_pcm_subformat_names[subformat];
251}
252
Takashi Iwai877211f2005-11-17 13:59:38 +0100253static const char *snd_pcm_tstamp_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
256 return snd_pcm_tstamp_mode_names[mode];
257}
258
259static const char *snd_pcm_state_name(snd_pcm_state_t state)
260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return snd_pcm_state_names[state];
262}
263
264#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
265#include <linux/soundcard.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static const char *snd_pcm_oss_format_name(int format)
268{
269 switch (format) {
270 case AFMT_MU_LAW:
271 return "MU_LAW";
272 case AFMT_A_LAW:
273 return "A_LAW";
274 case AFMT_IMA_ADPCM:
275 return "IMA_ADPCM";
276 case AFMT_U8:
277 return "U8";
278 case AFMT_S16_LE:
279 return "S16_LE";
280 case AFMT_S16_BE:
281 return "S16_BE";
282 case AFMT_S8:
283 return "S8";
284 case AFMT_U16_LE:
285 return "U16_LE";
286 case AFMT_U16_BE:
287 return "U16_BE";
288 case AFMT_MPEG:
289 return "MPEG";
290 default:
291 return "unknown";
292 }
293}
294#endif
295
Takashi Iwai877211f2005-11-17 13:59:38 +0100296static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
297 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwai877211f2005-11-17 13:59:38 +0100299 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int err;
301
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200302 if (! substream)
303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 info = kmalloc(sizeof(*info), GFP_KERNEL);
306 if (! info) {
307 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
308 return;
309 }
310
311 err = snd_pcm_info(substream, info);
312 if (err < 0) {
313 snd_iprintf(buffer, "error %d\n", err);
314 kfree(info);
315 return;
316 }
317 snd_iprintf(buffer, "card: %d\n", info->card);
318 snd_iprintf(buffer, "device: %d\n", info->device);
319 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
320 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
321 snd_iprintf(buffer, "id: %s\n", info->id);
322 snd_iprintf(buffer, "name: %s\n", info->name);
323 snd_iprintf(buffer, "subname: %s\n", info->subname);
324 snd_iprintf(buffer, "class: %d\n", info->dev_class);
325 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
326 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
327 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
328 kfree(info);
329}
330
Takashi Iwai877211f2005-11-17 13:59:38 +0100331static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
332 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Takashi Iwai877211f2005-11-17 13:59:38 +0100334 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
335 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Takashi Iwai877211f2005-11-17 13:59:38 +0100338static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
339 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Takashi Iwai877211f2005-11-17 13:59:38 +0100341 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
342 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Takashi Iwai877211f2005-11-17 13:59:38 +0100345static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
346 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Takashi Iwai877211f2005-11-17 13:59:38 +0100348 struct snd_pcm_substream *substream = entry->private_data;
349 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!runtime) {
351 snd_iprintf(buffer, "closed\n");
352 return;
353 }
354 snd_pcm_stream_lock_irq(substream);
355 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
356 snd_iprintf(buffer, "no setup\n");
357 snd_pcm_stream_unlock_irq(substream);
358 return;
359 }
360 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
361 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
362 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
363 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
364 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
365 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
366 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
367 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
368#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
369 if (substream->oss.oss) {
370 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
371 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
372 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
373 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
374 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
375 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
376 }
377#endif
378 snd_pcm_stream_unlock_irq(substream);
379}
380
Takashi Iwai877211f2005-11-17 13:59:38 +0100381static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
382 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Takashi Iwai877211f2005-11-17 13:59:38 +0100384 struct snd_pcm_substream *substream = entry->private_data;
385 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!runtime) {
387 snd_iprintf(buffer, "closed\n");
388 return;
389 }
390 snd_pcm_stream_lock_irq(substream);
391 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
392 snd_iprintf(buffer, "no setup\n");
393 snd_pcm_stream_unlock_irq(substream);
394 return;
395 }
396 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
397 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
398 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
399 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
400 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
401 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
402 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
403 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
404 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
405 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
406 snd_pcm_stream_unlock_irq(substream);
407}
408
Takashi Iwai877211f2005-11-17 13:59:38 +0100409static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
410 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Takashi Iwai877211f2005-11-17 13:59:38 +0100412 struct snd_pcm_substream *substream = entry->private_data;
413 struct snd_pcm_runtime *runtime = substream->runtime;
414 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int err;
416 if (!runtime) {
417 snd_iprintf(buffer, "closed\n");
418 return;
419 }
420 memset(&status, 0, sizeof(status));
421 err = snd_pcm_status(substream, &status);
422 if (err < 0) {
423 snd_iprintf(buffer, "error %d\n", err);
424 return;
425 }
426 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
427 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
428 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
429 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
430 status.tstamp.tv_sec, status.tstamp.tv_nsec);
431 snd_iprintf(buffer, "delay : %ld\n", status.delay);
432 snd_iprintf(buffer, "avail : %ld\n", status.avail);
433 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
434 snd_iprintf(buffer, "-----\n");
435 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
436 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200439#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100440static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
441 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Takashi Iwai877211f2005-11-17 13:59:38 +0100443 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
445}
446
Takashi Iwai877211f2005-11-17 13:59:38 +0100447static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
448 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai877211f2005-11-17 13:59:38 +0100450 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 char line[64];
452 if (!snd_info_get_line(buffer, line, sizeof(line)))
453 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
454}
455#endif
456
Takashi Iwai877211f2005-11-17 13:59:38 +0100457static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai877211f2005-11-17 13:59:38 +0100459 struct snd_pcm *pcm = pstr->pcm;
460 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 char name[16];
462
463 sprintf(name, "pcm%i%c", pcm->device,
464 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
465 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
466 return -ENOMEM;
467 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
468 if (snd_info_register(entry) < 0) {
469 snd_info_free_entry(entry);
470 return -ENOMEM;
471 }
472 pstr->proc_root = entry;
473
474 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200475 snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (snd_info_register(entry) < 0) {
477 snd_info_free_entry(entry);
478 entry = NULL;
479 }
480 }
481 pstr->proc_info_entry = entry;
482
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200483#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100484 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
485 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 entry->c.text.read = snd_pcm_xrun_debug_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200488 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 entry->private_data = pstr;
490 if (snd_info_register(entry) < 0) {
491 snd_info_free_entry(entry);
492 entry = NULL;
493 }
494 }
495 pstr->proc_xrun_debug_entry = entry;
496#endif
497 return 0;
498}
499
Takashi Iwai877211f2005-11-17 13:59:38 +0100500static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jaroslav Kysela61fb63c2006-04-24 21:57:16 +0200502#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (pstr->proc_xrun_debug_entry) {
504 snd_info_unregister(pstr->proc_xrun_debug_entry);
505 pstr->proc_xrun_debug_entry = NULL;
506 }
507#endif
508 if (pstr->proc_info_entry) {
509 snd_info_unregister(pstr->proc_info_entry);
510 pstr->proc_info_entry = NULL;
511 }
512 if (pstr->proc_root) {
513 snd_info_unregister(pstr->proc_root);
514 pstr->proc_root = NULL;
515 }
516 return 0;
517}
518
Takashi Iwai877211f2005-11-17 13:59:38 +0100519static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Takashi Iwai877211f2005-11-17 13:59:38 +0100521 struct snd_info_entry *entry;
522 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char name[16];
524
525 card = substream->pcm->card;
526
527 sprintf(name, "sub%i", substream->number);
528 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
529 return -ENOMEM;
530 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
531 if (snd_info_register(entry) < 0) {
532 snd_info_free_entry(entry);
533 return -ENOMEM;
534 }
535 substream->proc_root = entry;
536
537 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200538 snd_info_set_text_ops(entry, substream,
539 snd_pcm_substream_proc_info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (snd_info_register(entry) < 0) {
541 snd_info_free_entry(entry);
542 entry = NULL;
543 }
544 }
545 substream->proc_info_entry = entry;
546
547 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200548 snd_info_set_text_ops(entry, substream,
549 snd_pcm_substream_proc_hw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (snd_info_register(entry) < 0) {
551 snd_info_free_entry(entry);
552 entry = NULL;
553 }
554 }
555 substream->proc_hw_params_entry = entry;
556
557 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200558 snd_info_set_text_ops(entry, substream,
559 snd_pcm_substream_proc_sw_params_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (snd_info_register(entry) < 0) {
561 snd_info_free_entry(entry);
562 entry = NULL;
563 }
564 }
565 substream->proc_sw_params_entry = entry;
566
567 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +0200568 snd_info_set_text_ops(entry, substream,
569 snd_pcm_substream_proc_status_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (snd_info_register(entry) < 0) {
571 snd_info_free_entry(entry);
572 entry = NULL;
573 }
574 }
575 substream->proc_status_entry = entry;
576
577 return 0;
578}
579
Takashi Iwai877211f2005-11-17 13:59:38 +0100580static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 if (substream->proc_info_entry) {
583 snd_info_unregister(substream->proc_info_entry);
584 substream->proc_info_entry = NULL;
585 }
586 if (substream->proc_hw_params_entry) {
587 snd_info_unregister(substream->proc_hw_params_entry);
588 substream->proc_hw_params_entry = NULL;
589 }
590 if (substream->proc_sw_params_entry) {
591 snd_info_unregister(substream->proc_sw_params_entry);
592 substream->proc_sw_params_entry = NULL;
593 }
594 if (substream->proc_status_entry) {
595 snd_info_unregister(substream->proc_status_entry);
596 substream->proc_status_entry = NULL;
597 }
598 if (substream->proc_root) {
599 snd_info_unregister(substream->proc_root);
600 substream->proc_root = NULL;
601 }
602 return 0;
603}
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200604#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwaie28563c2005-12-01 10:42:42 +0100605static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
606static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
607static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
608static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
Takashi Iwaib7d90a32006-04-25 12:56:04 +0200609#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/**
612 * snd_pcm_new_stream - create a new PCM stream
613 * @pcm: the pcm instance
614 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
615 * @substream_count: the number of substreams
616 *
617 * Creates a new stream for the pcm.
618 * The corresponding stream on the pcm must have been empty before
619 * calling this, i.e. zero must be given to the argument of
620 * snd_pcm_new().
621 *
622 * Returns zero if successful, or a negative error code on failure.
623 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100624int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100627 struct snd_pcm_str *pstr = &pcm->streams[stream];
628 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100631 mutex_init(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#endif
633 pstr->stream = stream;
634 pstr->pcm = pcm;
635 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (substream_count > 0) {
637 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100638 if (err < 0) {
639 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 prev = NULL;
644 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200645 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100646 if (substream == NULL) {
647 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 substream->pcm = pcm;
651 substream->pstr = pstr;
652 substream->number = idx;
653 substream->stream = stream;
654 sprintf(substream->name, "subdevice #%i", idx);
655 substream->buffer_bytes_max = UINT_MAX;
656 if (prev == NULL)
657 pstr->substream = substream;
658 else
659 prev->next = substream;
660 err = snd_pcm_substream_proc_init(substream);
661 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100662 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 kfree(substream);
664 return err;
665 }
666 substream->group = &substream->self_group;
667 spin_lock_init(&substream->self_group.lock);
668 INIT_LIST_HEAD(&substream->self_group.substreams);
669 list_add_tail(&substream->link_list, &substream->self_group.substreams);
670 spin_lock_init(&substream->timer_lock);
671 prev = substream;
672 }
673 return 0;
674}
675
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200676EXPORT_SYMBOL(snd_pcm_new_stream);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/**
679 * snd_pcm_new - create a new PCM instance
680 * @card: the card instance
681 * @id: the id string
682 * @device: the device index (zero based)
683 * @playback_count: the number of substreams for playback
684 * @capture_count: the number of substreams for capture
685 * @rpcm: the pointer to store the new pcm instance
686 *
687 * Creates a new PCM instance.
688 *
689 * The pcm operators have to be set afterwards to the new instance
690 * via snd_pcm_set_ops().
691 *
692 * Returns zero if successful, or a negative error code on failure.
693 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100694int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100696 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Takashi Iwai877211f2005-11-17 13:59:38 +0100698 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100700 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 .dev_free = snd_pcm_dev_free,
702 .dev_register = snd_pcm_dev_register,
703 .dev_disconnect = snd_pcm_dev_disconnect,
704 .dev_unregister = snd_pcm_dev_unregister
705 };
706
707 snd_assert(rpcm != NULL, return -EINVAL);
708 *rpcm = NULL;
709 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200710 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100711 if (pcm == NULL) {
712 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 pcm->card = card;
716 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100717 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
720 snd_pcm_free(pcm);
721 return err;
722 }
723 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
724 snd_pcm_free(pcm);
725 return err;
726 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100727 mutex_init(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 init_waitqueue_head(&pcm->open_wait);
729 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
730 snd_pcm_free(pcm);
731 return err;
732 }
733 *rpcm = pcm;
734 return 0;
735}
736
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200737EXPORT_SYMBOL(snd_pcm_new);
738
Takashi Iwai877211f2005-11-17 13:59:38 +0100739static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Takashi Iwai877211f2005-11-17 13:59:38 +0100741 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100743 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744#endif
745 substream = pstr->substream;
746 while (substream) {
747 substream_next = substream->next;
748 snd_pcm_substream_proc_done(substream);
749 kfree(substream);
750 substream = substream_next;
751 }
752 snd_pcm_stream_proc_done(pstr);
753#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
754 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
755 setupn = setup->next;
756 kfree(setup->task_name);
757 kfree(setup);
758 }
759#endif
760}
761
Takashi Iwai877211f2005-11-17 13:59:38 +0100762static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 snd_assert(pcm != NULL, return -ENXIO);
765 if (pcm->private_free)
766 pcm->private_free(pcm);
767 snd_pcm_lib_preallocate_free_for_all(pcm);
768 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
769 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
770 kfree(pcm);
771 return 0;
772}
773
Takashi Iwai877211f2005-11-17 13:59:38 +0100774static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Takashi Iwai877211f2005-11-17 13:59:38 +0100776 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return snd_pcm_free(pcm);
778}
779
780static void snd_pcm_tick_timer_func(unsigned long data)
781{
Takashi Iwai877211f2005-11-17 13:59:38 +0100782 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 snd_pcm_tick_elapsed(substream);
784}
785
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200786int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
787 struct file *file,
788 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Takashi Iwai877211f2005-11-17 13:59:38 +0100790 struct snd_pcm_str * pstr;
791 struct snd_pcm_substream *substream;
792 struct snd_pcm_runtime *runtime;
793 struct snd_ctl_file *kctl;
794 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct list_head *list;
796 int prefer_subdevice = -1;
797 size_t size;
798
799 snd_assert(rsubstream != NULL, return -EINVAL);
800 *rsubstream = NULL;
801 snd_assert(pcm != NULL, return -ENXIO);
802 pstr = &pcm->streams[stream];
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200803 if (pstr->substream == NULL || pstr->substream_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ENODEV;
805
806 card = pcm->card;
807 down_read(&card->controls_rwsem);
808 list_for_each(list, &card->ctl_files) {
809 kctl = snd_ctl_file(list);
810 if (kctl->pid == current->pid) {
811 prefer_subdevice = kctl->prefer_pcm_subdevice;
812 break;
813 }
814 }
815 up_read(&card->controls_rwsem);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 switch (stream) {
818 case SNDRV_PCM_STREAM_PLAYBACK:
819 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
820 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
821 if (SUBSTREAM_BUSY(substream))
822 return -EAGAIN;
823 }
824 }
825 break;
826 case SNDRV_PCM_STREAM_CAPTURE:
827 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
828 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
829 if (SUBSTREAM_BUSY(substream))
830 return -EAGAIN;
831 }
832 }
833 break;
834 default:
835 return -EINVAL;
836 }
837
838 if (prefer_subdevice >= 0) {
839 for (substream = pstr->substream; substream; substream = substream->next)
840 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
841 goto __ok;
842 }
843 for (substream = pstr->substream; substream; substream = substream->next)
844 if (!SUBSTREAM_BUSY(substream))
845 break;
846 __ok:
847 if (substream == NULL)
848 return -EAGAIN;
849
Takashi Iwaica2c0962005-09-09 14:20:23 +0200850 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (runtime == NULL)
852 return -ENOMEM;
853
Takashi Iwai877211f2005-11-17 13:59:38 +0100854 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
856 if (runtime->status == NULL) {
857 kfree(runtime);
858 return -ENOMEM;
859 }
860 memset((void*)runtime->status, 0, size);
861
Takashi Iwai877211f2005-11-17 13:59:38 +0100862 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
864 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100865 snd_free_pages((void*)runtime->status,
866 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kfree(runtime);
868 return -ENOMEM;
869 }
870 memset((void*)runtime->control, 0, size);
871
872 init_waitqueue_head(&runtime->sleep);
873 atomic_set(&runtime->mmap_count, 0);
874 init_timer(&runtime->tick_timer);
875 runtime->tick_timer.function = snd_pcm_tick_timer_func;
876 runtime->tick_timer.data = (unsigned long) substream;
877
878 runtime->status->state = SNDRV_PCM_STATE_OPEN;
879
880 substream->runtime = runtime;
881 substream->private_data = pcm->private_data;
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200882 substream->ffile = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pstr->substream_opened++;
884 *rsubstream = substream;
885 return 0;
886}
887
Takashi Iwai3bf75f92006-03-27 16:40:49 +0200888void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Takashi Iwai877211f2005-11-17 13:59:38 +0100890 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 substream->file = NULL;
892 runtime = substream->runtime;
893 snd_assert(runtime != NULL, return);
894 if (runtime->private_free != NULL)
895 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100896 snd_free_pages((void*)runtime->status,
897 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
898 snd_free_pages((void*)runtime->control,
899 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 kfree(runtime->hw_constraints.rules);
901 kfree(runtime);
902 substream->runtime = NULL;
903 substream->pstr->substream_opened--;
904}
905
Takashi Iwai877211f2005-11-17 13:59:38 +0100906static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100908 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100909 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct list_head *list;
911 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100912 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100915 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100916 if (snd_pcm_search(pcm->card, pcm->device)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100917 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -EBUSY;
919 }
Clemens Ladischf87135f2005-11-20 14:06:59 +0100920 list_add_tail(&pcm->list, &snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 for (cidx = 0; cidx < 2; cidx++) {
922 int devtype = -1;
923 if (pcm->streams[cidx].substream == NULL)
924 continue;
925 switch (cidx) {
926 case SNDRV_PCM_STREAM_PLAYBACK:
927 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
929 break;
930 case SNDRV_PCM_STREAM_CAPTURE:
931 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
933 break;
934 }
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100935 if ((err = snd_register_device(devtype, pcm->card,
936 pcm->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +0100937 &snd_pcm_f_ops[cidx],
938 pcm, str)) < 0)
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100939 {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100940 list_del(&pcm->list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100941 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return err;
943 }
944 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
945 snd_pcm_timer_init(substream);
946 }
947 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100948 struct snd_pcm_notify *notify;
949 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 notify->n_register(pcm);
951 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100952 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
955
Takashi Iwai877211f2005-11-17 13:59:38 +0100956static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Takashi Iwai877211f2005-11-17 13:59:38 +0100958 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100960 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100961 int cidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100963 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100964 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 for (cidx = 0; cidx < 2; cidx++)
966 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
967 if (substream->runtime)
968 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
969 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100970 struct snd_pcm_notify *notify;
971 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 notify->n_disconnect(pcm);
973 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100974 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
Takashi Iwai877211f2005-11-17 13:59:38 +0100978static int snd_pcm_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100980 int cidx, devtype;
Takashi Iwai877211f2005-11-17 13:59:38 +0100981 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100983 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 snd_assert(pcm != NULL, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100986 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100987 list_del(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 for (cidx = 0; cidx < 2; cidx++) {
989 devtype = -1;
990 switch (cidx) {
991 case SNDRV_PCM_STREAM_PLAYBACK:
992 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
993 break;
994 case SNDRV_PCM_STREAM_CAPTURE:
995 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
996 break;
997 }
998 snd_unregister_device(devtype, pcm->card, pcm->device);
999 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1000 snd_pcm_timer_done(substream);
1001 }
1002 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001003 struct snd_pcm_notify *notify;
1004 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 notify->n_unregister(pcm);
1006 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001007 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return snd_pcm_free(pcm);
1009}
1010
Takashi Iwai877211f2005-11-17 13:59:38 +01001011int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001013 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001016 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (nfree) {
1018 list_del(&notify->list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001019 list_for_each(p, &snd_pcm_devices)
1020 notify->n_unregister(list_entry(p,
1021 struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 list_add_tail(&notify->list, &snd_pcm_notify_list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001024 list_for_each(p, &snd_pcm_devices)
1025 notify->n_register(list_entry(p, struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001027 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029}
1030
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001031EXPORT_SYMBOL(snd_pcm_notify);
1032
Takashi Iwaie28563c2005-12-01 10:42:42 +01001033#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Info interface
1036 */
1037
Takashi Iwai877211f2005-11-17 13:59:38 +01001038static void snd_pcm_proc_read(struct snd_info_entry *entry,
1039 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001041 struct list_head *p;
Takashi Iwai877211f2005-11-17 13:59:38 +01001042 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001044 mutex_lock(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001045 list_for_each(p, &snd_pcm_devices) {
1046 pcm = list_entry(p, struct snd_pcm, list);
1047 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1048 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 snd_iprintf(buffer, " : playback %i",
1051 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001053 snd_iprintf(buffer, " : capture %i",
1054 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 snd_iprintf(buffer, "\n");
1056 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001057 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Takashi Iwai877211f2005-11-17 13:59:38 +01001060static struct snd_info_entry *snd_pcm_proc_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Takashi Iwaie28563c2005-12-01 10:42:42 +01001062static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwai877211f2005-11-17 13:59:38 +01001064 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001067 snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (snd_info_register(entry) < 0) {
1069 snd_info_free_entry(entry);
1070 entry = NULL;
1071 }
1072 }
1073 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001074}
1075
1076static void snd_pcm_proc_done(void)
1077{
1078 if (snd_pcm_proc_entry)
1079 snd_info_unregister(snd_pcm_proc_entry);
1080}
1081
1082#else /* !CONFIG_PROC_FS */
1083#define snd_pcm_proc_init()
1084#define snd_pcm_proc_done()
1085#endif /* CONFIG_PROC_FS */
1086
1087
1088/*
1089 * ENTRY functions
1090 */
1091
1092static int __init alsa_pcm_init(void)
1093{
1094 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1095 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1096 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
1100static void __exit alsa_pcm_exit(void)
1101{
1102 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1103 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001104 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107module_init(alsa_pcm_init)
1108module_exit(alsa_pcm_exit)