blob: 28ca61eb0b0d9948515985a687e71e5543e9866e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/time.h>
26#include <sound/core.h>
27#include <sound/minors.h>
28#include <sound/pcm.h>
29#include <sound/control.h>
30#include <sound/info.h>
31
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
33MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34MODULE_LICENSE("GPL");
35
Clemens Ladischf87135f2005-11-20 14:06:59 +010036static LIST_HEAD(snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static LIST_HEAD(snd_pcm_notify_list);
38static DECLARE_MUTEX(register_mutex);
39
Takashi Iwai877211f2005-11-17 13:59:38 +010040static int snd_pcm_free(struct snd_pcm *pcm);
41static int snd_pcm_dev_free(struct snd_device *device);
42static int snd_pcm_dev_register(struct snd_device *device);
43static int snd_pcm_dev_disconnect(struct snd_device *device);
44static int snd_pcm_dev_unregister(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Clemens Ladischf87135f2005-11-20 14:06:59 +010046static struct snd_pcm *snd_pcm_search(struct snd_card *card, int device)
47{
48 struct list_head *p;
49 struct snd_pcm *pcm;
50
51 list_for_each(p, &snd_pcm_devices) {
52 pcm = list_entry(p, struct snd_pcm, list);
53 if (pcm->card == card && pcm->device == device)
54 return pcm;
55 }
56 return NULL;
57}
58
Takashi Iwai877211f2005-11-17 13:59:38 +010059static int snd_pcm_control_ioctl(struct snd_card *card,
60 struct snd_ctl_file *control,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned int cmd, unsigned long arg)
62{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 switch (cmd) {
64 case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
65 {
66 int device;
67
68 if (get_user(device, (int __user *)arg))
69 return -EFAULT;
Clemens Ladischf87135f2005-11-20 14:06:59 +010070 down(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 device = device < 0 ? 0 : device + 1;
72 while (device < SNDRV_PCM_DEVICES) {
Clemens Ladischf87135f2005-11-20 14:06:59 +010073 if (snd_pcm_search(card, device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 device++;
76 }
77 if (device == SNDRV_PCM_DEVICES)
78 device = -1;
Clemens Ladischf87135f2005-11-20 14:06:59 +010079 up(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (put_user(device, (int __user *)arg))
81 return -EFAULT;
82 return 0;
83 }
84 case SNDRV_CTL_IOCTL_PCM_INFO:
85 {
Takashi Iwai877211f2005-11-17 13:59:38 +010086 struct snd_pcm_info __user *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned int device, subdevice;
Takashi Iwai877211f2005-11-17 13:59:38 +010088 int stream;
89 struct snd_pcm *pcm;
90 struct snd_pcm_str *pstr;
91 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +010092 int err;
93
Takashi Iwai877211f2005-11-17 13:59:38 +010094 info = (struct snd_pcm_info __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (get_user(device, &info->device))
96 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (get_user(stream, &info->stream))
98 return -EFAULT;
99 if (stream < 0 || stream > 1)
100 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (get_user(subdevice, &info->subdevice))
102 return -EFAULT;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100103 down(&register_mutex);
104 pcm = snd_pcm_search(card, device);
105 if (pcm == NULL) {
106 err = -ENXIO;
107 goto _error;
108 }
109 pstr = &pcm->streams[stream];
110 if (pstr->substream_count == 0) {
111 err = -ENOENT;
112 goto _error;
113 }
114 if (subdevice >= pstr->substream_count) {
115 err = -ENXIO;
116 goto _error;
117 }
118 for (substream = pstr->substream; substream;
119 substream = substream->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (substream->number == (int)subdevice)
121 break;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100122 if (substream == NULL) {
123 err = -ENXIO;
124 goto _error;
125 }
126 err = snd_pcm_info_user(substream, info);
127 _error:
128 up(&register_mutex);
129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
132 {
133 int val;
134
135 if (get_user(val, (int __user *)arg))
136 return -EFAULT;
137 control->prefer_pcm_subdevice = val;
138 return 0;
139 }
140 }
141 return -ENOIOCTLCMD;
142}
143#define STATE(v) [SNDRV_PCM_STATE_##v] = #v
144#define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
145#define READY(v) [SNDRV_PCM_READY_##v] = #v
146#define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
147#define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
148#define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
149#define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
150#define START(v) [SNDRV_PCM_START_##v] = #v
151#define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
152#define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static char *snd_pcm_format_names[] = {
155 FORMAT(S8),
156 FORMAT(U8),
157 FORMAT(S16_LE),
158 FORMAT(S16_BE),
159 FORMAT(U16_LE),
160 FORMAT(U16_BE),
161 FORMAT(S24_LE),
162 FORMAT(S24_BE),
163 FORMAT(U24_LE),
164 FORMAT(U24_BE),
165 FORMAT(S32_LE),
166 FORMAT(S32_BE),
167 FORMAT(U32_LE),
168 FORMAT(U32_BE),
169 FORMAT(FLOAT_LE),
170 FORMAT(FLOAT_BE),
171 FORMAT(FLOAT64_LE),
172 FORMAT(FLOAT64_BE),
173 FORMAT(IEC958_SUBFRAME_LE),
174 FORMAT(IEC958_SUBFRAME_BE),
175 FORMAT(MU_LAW),
176 FORMAT(A_LAW),
177 FORMAT(IMA_ADPCM),
178 FORMAT(MPEG),
179 FORMAT(GSM),
180 FORMAT(SPECIAL),
181 FORMAT(S24_3LE),
182 FORMAT(S24_3BE),
183 FORMAT(U24_3LE),
184 FORMAT(U24_3BE),
185 FORMAT(S20_3LE),
186 FORMAT(S20_3BE),
187 FORMAT(U20_3LE),
188 FORMAT(U20_3BE),
189 FORMAT(S18_3LE),
190 FORMAT(S18_3BE),
191 FORMAT(U18_3LE),
192 FORMAT(U18_3BE),
193};
194
Takashi Iwaie28563c2005-12-01 10:42:42 +0100195const char *snd_pcm_format_name(snd_pcm_format_t format)
196{
197 return snd_pcm_format_names[format];
198}
199
200#ifdef CONFIG_PROC_FS
201static 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>
263static const char *snd_pcm_oss_format_name(int format)
264{
265 switch (format) {
266 case AFMT_MU_LAW:
267 return "MU_LAW";
268 case AFMT_A_LAW:
269 return "A_LAW";
270 case AFMT_IMA_ADPCM:
271 return "IMA_ADPCM";
272 case AFMT_U8:
273 return "U8";
274 case AFMT_S16_LE:
275 return "S16_LE";
276 case AFMT_S16_BE:
277 return "S16_BE";
278 case AFMT_S8:
279 return "S8";
280 case AFMT_U16_LE:
281 return "U16_LE";
282 case AFMT_U16_BE:
283 return "U16_BE";
284 case AFMT_MPEG:
285 return "MPEG";
286 default:
287 return "unknown";
288 }
289}
290#endif
291
Takashi Iwai877211f2005-11-17 13:59:38 +0100292static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
293 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Takashi Iwai877211f2005-11-17 13:59:38 +0100295 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int err;
297
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200298 if (! substream)
299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 info = kmalloc(sizeof(*info), GFP_KERNEL);
302 if (! info) {
303 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
304 return;
305 }
306
307 err = snd_pcm_info(substream, info);
308 if (err < 0) {
309 snd_iprintf(buffer, "error %d\n", err);
310 kfree(info);
311 return;
312 }
313 snd_iprintf(buffer, "card: %d\n", info->card);
314 snd_iprintf(buffer, "device: %d\n", info->device);
315 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
316 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
317 snd_iprintf(buffer, "id: %s\n", info->id);
318 snd_iprintf(buffer, "name: %s\n", info->name);
319 snd_iprintf(buffer, "subname: %s\n", info->subname);
320 snd_iprintf(buffer, "class: %d\n", info->dev_class);
321 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
322 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
323 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
324 kfree(info);
325}
326
Takashi Iwai877211f2005-11-17 13:59:38 +0100327static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
328 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Takashi Iwai877211f2005-11-17 13:59:38 +0100330 snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
331 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Takashi Iwai877211f2005-11-17 13:59:38 +0100334static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
335 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Takashi Iwai877211f2005-11-17 13:59:38 +0100337 snd_pcm_proc_info_read((struct snd_pcm_substream *)entry->private_data,
338 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Takashi Iwai877211f2005-11-17 13:59:38 +0100341static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
342 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai877211f2005-11-17 13:59:38 +0100344 struct snd_pcm_substream *substream = entry->private_data;
345 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!runtime) {
347 snd_iprintf(buffer, "closed\n");
348 return;
349 }
350 snd_pcm_stream_lock_irq(substream);
351 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
352 snd_iprintf(buffer, "no setup\n");
353 snd_pcm_stream_unlock_irq(substream);
354 return;
355 }
356 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
357 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
358 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
359 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
360 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
361 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
362 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
363 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
364#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
365 if (substream->oss.oss) {
366 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
367 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
368 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
369 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
370 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
371 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
372 }
373#endif
374 snd_pcm_stream_unlock_irq(substream);
375}
376
Takashi Iwai877211f2005-11-17 13:59:38 +0100377static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
378 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Takashi Iwai877211f2005-11-17 13:59:38 +0100380 struct snd_pcm_substream *substream = entry->private_data;
381 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!runtime) {
383 snd_iprintf(buffer, "closed\n");
384 return;
385 }
386 snd_pcm_stream_lock_irq(substream);
387 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
388 snd_iprintf(buffer, "no setup\n");
389 snd_pcm_stream_unlock_irq(substream);
390 return;
391 }
392 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
393 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
394 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
395 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
396 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
397 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
398 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
399 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
400 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
401 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
402 snd_pcm_stream_unlock_irq(substream);
403}
404
Takashi Iwai877211f2005-11-17 13:59:38 +0100405static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
406 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Takashi Iwai877211f2005-11-17 13:59:38 +0100408 struct snd_pcm_substream *substream = entry->private_data;
409 struct snd_pcm_runtime *runtime = substream->runtime;
410 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int err;
412 if (!runtime) {
413 snd_iprintf(buffer, "closed\n");
414 return;
415 }
416 memset(&status, 0, sizeof(status));
417 err = snd_pcm_status(substream, &status);
418 if (err < 0) {
419 snd_iprintf(buffer, "error %d\n", err);
420 return;
421 }
422 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
423 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
424 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
425 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
426 status.tstamp.tv_sec, status.tstamp.tv_nsec);
427 snd_iprintf(buffer, "delay : %ld\n", status.delay);
428 snd_iprintf(buffer, "avail : %ld\n", status.avail);
429 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
430 snd_iprintf(buffer, "-----\n");
431 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
432 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435#ifdef CONFIG_SND_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100436static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
437 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai877211f2005-11-17 13:59:38 +0100439 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
441}
442
Takashi Iwai877211f2005-11-17 13:59:38 +0100443static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
444 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwai877211f2005-11-17 13:59:38 +0100446 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 char line[64];
448 if (!snd_info_get_line(buffer, line, sizeof(line)))
449 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
450}
451#endif
452
Takashi Iwai877211f2005-11-17 13:59:38 +0100453static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Takashi Iwai877211f2005-11-17 13:59:38 +0100455 struct snd_pcm *pcm = pstr->pcm;
456 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 char name[16];
458
459 sprintf(name, "pcm%i%c", pcm->device,
460 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
461 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
462 return -ENOMEM;
463 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
464 if (snd_info_register(entry) < 0) {
465 snd_info_free_entry(entry);
466 return -ENOMEM;
467 }
468 pstr->proc_root = entry;
469
470 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
471 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
472 if (snd_info_register(entry) < 0) {
473 snd_info_free_entry(entry);
474 entry = NULL;
475 }
476 }
477 pstr->proc_info_entry = entry;
478
479#ifdef CONFIG_SND_DEBUG
Takashi Iwai877211f2005-11-17 13:59:38 +0100480 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
481 pstr->proc_root)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 entry->c.text.read_size = 64;
483 entry->c.text.read = snd_pcm_xrun_debug_read;
484 entry->c.text.write_size = 64;
485 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200486 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 entry->private_data = pstr;
488 if (snd_info_register(entry) < 0) {
489 snd_info_free_entry(entry);
490 entry = NULL;
491 }
492 }
493 pstr->proc_xrun_debug_entry = entry;
494#endif
495 return 0;
496}
497
Takashi Iwai877211f2005-11-17 13:59:38 +0100498static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500#ifdef CONFIG_SND_DEBUG
501 if (pstr->proc_xrun_debug_entry) {
502 snd_info_unregister(pstr->proc_xrun_debug_entry);
503 pstr->proc_xrun_debug_entry = NULL;
504 }
505#endif
506 if (pstr->proc_info_entry) {
507 snd_info_unregister(pstr->proc_info_entry);
508 pstr->proc_info_entry = NULL;
509 }
510 if (pstr->proc_root) {
511 snd_info_unregister(pstr->proc_root);
512 pstr->proc_root = NULL;
513 }
514 return 0;
515}
516
Takashi Iwai877211f2005-11-17 13:59:38 +0100517static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai877211f2005-11-17 13:59:38 +0100519 struct snd_info_entry *entry;
520 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char name[16];
522
523 card = substream->pcm->card;
524
525 sprintf(name, "sub%i", substream->number);
526 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
527 return -ENOMEM;
528 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
529 if (snd_info_register(entry) < 0) {
530 snd_info_free_entry(entry);
531 return -ENOMEM;
532 }
533 substream->proc_root = entry;
534
535 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
536 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
537 if (snd_info_register(entry) < 0) {
538 snd_info_free_entry(entry);
539 entry = NULL;
540 }
541 }
542 substream->proc_info_entry = entry;
543
544 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
545 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
546 if (snd_info_register(entry) < 0) {
547 snd_info_free_entry(entry);
548 entry = NULL;
549 }
550 }
551 substream->proc_hw_params_entry = entry;
552
553 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
554 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
555 if (snd_info_register(entry) < 0) {
556 snd_info_free_entry(entry);
557 entry = NULL;
558 }
559 }
560 substream->proc_sw_params_entry = entry;
561
562 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
563 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
564 if (snd_info_register(entry) < 0) {
565 snd_info_free_entry(entry);
566 entry = NULL;
567 }
568 }
569 substream->proc_status_entry = entry;
570
571 return 0;
572}
573
Takashi Iwai877211f2005-11-17 13:59:38 +0100574static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 if (substream->proc_info_entry) {
577 snd_info_unregister(substream->proc_info_entry);
578 substream->proc_info_entry = NULL;
579 }
580 if (substream->proc_hw_params_entry) {
581 snd_info_unregister(substream->proc_hw_params_entry);
582 substream->proc_hw_params_entry = NULL;
583 }
584 if (substream->proc_sw_params_entry) {
585 snd_info_unregister(substream->proc_sw_params_entry);
586 substream->proc_sw_params_entry = NULL;
587 }
588 if (substream->proc_status_entry) {
589 snd_info_unregister(substream->proc_status_entry);
590 substream->proc_status_entry = NULL;
591 }
592 if (substream->proc_root) {
593 snd_info_unregister(substream->proc_root);
594 substream->proc_root = NULL;
595 }
596 return 0;
597}
Takashi Iwaie28563c2005-12-01 10:42:42 +0100598#else /* !CONFIG_PROC_FS */
599static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
600static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
601static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
602static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
603#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605/**
606 * snd_pcm_new_stream - create a new PCM stream
607 * @pcm: the pcm instance
608 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
609 * @substream_count: the number of substreams
610 *
611 * Creates a new stream for the pcm.
612 * The corresponding stream on the pcm must have been empty before
613 * calling this, i.e. zero must be given to the argument of
614 * snd_pcm_new().
615 *
616 * Returns zero if successful, or a negative error code on failure.
617 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100618int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 int idx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100621 struct snd_pcm_str *pstr = &pcm->streams[stream];
622 struct snd_pcm_substream *substream, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
625 init_MUTEX(&pstr->oss.setup_mutex);
626#endif
627 pstr->stream = stream;
628 pstr->pcm = pcm;
629 pstr->substream_count = substream_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (substream_count > 0) {
631 err = snd_pcm_stream_proc_init(pstr);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100632 if (err < 0) {
633 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return err;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 prev = NULL;
638 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200639 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100640 if (substream == NULL) {
641 snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 substream->pcm = pcm;
645 substream->pstr = pstr;
646 substream->number = idx;
647 substream->stream = stream;
648 sprintf(substream->name, "subdevice #%i", idx);
649 substream->buffer_bytes_max = UINT_MAX;
650 if (prev == NULL)
651 pstr->substream = substream;
652 else
653 prev->next = substream;
654 err = snd_pcm_substream_proc_init(substream);
655 if (err < 0) {
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100656 snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 kfree(substream);
658 return err;
659 }
660 substream->group = &substream->self_group;
661 spin_lock_init(&substream->self_group.lock);
662 INIT_LIST_HEAD(&substream->self_group.substreams);
663 list_add_tail(&substream->link_list, &substream->self_group.substreams);
664 spin_lock_init(&substream->timer_lock);
665 prev = substream;
666 }
667 return 0;
668}
669
670/**
671 * snd_pcm_new - create a new PCM instance
672 * @card: the card instance
673 * @id: the id string
674 * @device: the device index (zero based)
675 * @playback_count: the number of substreams for playback
676 * @capture_count: the number of substreams for capture
677 * @rpcm: the pointer to store the new pcm instance
678 *
679 * Creates a new PCM instance.
680 *
681 * The pcm operators have to be set afterwards to the new instance
682 * via snd_pcm_set_ops().
683 *
684 * Returns zero if successful, or a negative error code on failure.
685 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100686int snd_pcm_new(struct snd_card *card, char *id, int device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int playback_count, int capture_count,
Takashi Iwai877211f2005-11-17 13:59:38 +0100688 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Takashi Iwai877211f2005-11-17 13:59:38 +0100690 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100692 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .dev_free = snd_pcm_dev_free,
694 .dev_register = snd_pcm_dev_register,
695 .dev_disconnect = snd_pcm_dev_disconnect,
696 .dev_unregister = snd_pcm_dev_unregister
697 };
698
699 snd_assert(rpcm != NULL, return -EINVAL);
700 *rpcm = NULL;
701 snd_assert(card != NULL, return -ENXIO);
Takashi Iwaica2c0962005-09-09 14:20:23 +0200702 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100703 if (pcm == NULL) {
704 snd_printk(KERN_ERR "Cannot allocate PCM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 pcm->card = card;
708 pcm->device = device;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100709 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 strlcpy(pcm->id, id, sizeof(pcm->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
712 snd_pcm_free(pcm);
713 return err;
714 }
715 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
716 snd_pcm_free(pcm);
717 return err;
718 }
719 init_MUTEX(&pcm->open_mutex);
720 init_waitqueue_head(&pcm->open_wait);
721 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
722 snd_pcm_free(pcm);
723 return err;
724 }
725 *rpcm = pcm;
726 return 0;
727}
728
Takashi Iwai877211f2005-11-17 13:59:38 +0100729static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai877211f2005-11-17 13:59:38 +0100731 struct snd_pcm_substream *substream, *substream_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
Takashi Iwai877211f2005-11-17 13:59:38 +0100733 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#endif
735 substream = pstr->substream;
736 while (substream) {
737 substream_next = substream->next;
738 snd_pcm_substream_proc_done(substream);
739 kfree(substream);
740 substream = substream_next;
741 }
742 snd_pcm_stream_proc_done(pstr);
743#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
744 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
745 setupn = setup->next;
746 kfree(setup->task_name);
747 kfree(setup);
748 }
749#endif
750}
751
Takashi Iwai877211f2005-11-17 13:59:38 +0100752static int snd_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 snd_assert(pcm != NULL, return -ENXIO);
755 if (pcm->private_free)
756 pcm->private_free(pcm);
757 snd_pcm_lib_preallocate_free_for_all(pcm);
758 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
759 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
760 kfree(pcm);
761 return 0;
762}
763
Takashi Iwai877211f2005-11-17 13:59:38 +0100764static int snd_pcm_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Takashi Iwai877211f2005-11-17 13:59:38 +0100766 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return snd_pcm_free(pcm);
768}
769
770static void snd_pcm_tick_timer_func(unsigned long data)
771{
Takashi Iwai877211f2005-11-17 13:59:38 +0100772 struct snd_pcm_substream *substream = (struct snd_pcm_substream *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 snd_pcm_tick_elapsed(substream);
774}
775
Takashi Iwai877211f2005-11-17 13:59:38 +0100776int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
777 struct snd_pcm_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Takashi Iwai877211f2005-11-17 13:59:38 +0100779 struct snd_pcm_str * pstr;
780 struct snd_pcm_substream *substream;
781 struct snd_pcm_runtime *runtime;
782 struct snd_ctl_file *kctl;
783 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct list_head *list;
785 int prefer_subdevice = -1;
786 size_t size;
787
788 snd_assert(rsubstream != NULL, return -EINVAL);
789 *rsubstream = NULL;
790 snd_assert(pcm != NULL, return -ENXIO);
791 pstr = &pcm->streams[stream];
792 if (pstr->substream == NULL)
793 return -ENODEV;
794
795 card = pcm->card;
796 down_read(&card->controls_rwsem);
797 list_for_each(list, &card->ctl_files) {
798 kctl = snd_ctl_file(list);
799 if (kctl->pid == current->pid) {
800 prefer_subdevice = kctl->prefer_pcm_subdevice;
801 break;
802 }
803 }
804 up_read(&card->controls_rwsem);
805
806 if (pstr->substream_count == 0)
807 return -ENODEV;
808 switch (stream) {
809 case SNDRV_PCM_STREAM_PLAYBACK:
810 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
811 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
812 if (SUBSTREAM_BUSY(substream))
813 return -EAGAIN;
814 }
815 }
816 break;
817 case SNDRV_PCM_STREAM_CAPTURE:
818 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
819 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
820 if (SUBSTREAM_BUSY(substream))
821 return -EAGAIN;
822 }
823 }
824 break;
825 default:
826 return -EINVAL;
827 }
828
829 if (prefer_subdevice >= 0) {
830 for (substream = pstr->substream; substream; substream = substream->next)
831 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
832 goto __ok;
833 }
834 for (substream = pstr->substream; substream; substream = substream->next)
835 if (!SUBSTREAM_BUSY(substream))
836 break;
837 __ok:
838 if (substream == NULL)
839 return -EAGAIN;
840
Takashi Iwaica2c0962005-09-09 14:20:23 +0200841 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (runtime == NULL)
843 return -ENOMEM;
844
Takashi Iwai877211f2005-11-17 13:59:38 +0100845 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
847 if (runtime->status == NULL) {
848 kfree(runtime);
849 return -ENOMEM;
850 }
851 memset((void*)runtime->status, 0, size);
852
Takashi Iwai877211f2005-11-17 13:59:38 +0100853 size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
855 if (runtime->control == NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100856 snd_free_pages((void*)runtime->status,
857 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 kfree(runtime);
859 return -ENOMEM;
860 }
861 memset((void*)runtime->control, 0, size);
862
863 init_waitqueue_head(&runtime->sleep);
864 atomic_set(&runtime->mmap_count, 0);
865 init_timer(&runtime->tick_timer);
866 runtime->tick_timer.function = snd_pcm_tick_timer_func;
867 runtime->tick_timer.data = (unsigned long) substream;
868
869 runtime->status->state = SNDRV_PCM_STATE_OPEN;
870
871 substream->runtime = runtime;
872 substream->private_data = pcm->private_data;
873 pstr->substream_opened++;
874 *rsubstream = substream;
875 return 0;
876}
877
Takashi Iwai877211f2005-11-17 13:59:38 +0100878void snd_pcm_release_substream(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Takashi Iwai877211f2005-11-17 13:59:38 +0100880 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 substream->file = NULL;
882 runtime = substream->runtime;
883 snd_assert(runtime != NULL, return);
884 if (runtime->private_free != NULL)
885 runtime->private_free(runtime);
Takashi Iwai877211f2005-11-17 13:59:38 +0100886 snd_free_pages((void*)runtime->status,
887 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
888 snd_free_pages((void*)runtime->control,
889 PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 kfree(runtime->hw_constraints.rules);
891 kfree(runtime);
892 substream->runtime = NULL;
893 substream->pstr->substream_opened--;
894}
895
Takashi Iwai877211f2005-11-17 13:59:38 +0100896static int snd_pcm_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100898 int cidx, err;
Takashi Iwai877211f2005-11-17 13:59:38 +0100899 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct list_head *list;
901 char str[16];
Takashi Iwai877211f2005-11-17 13:59:38 +0100902 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
905 down(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100906 if (snd_pcm_search(pcm->card, pcm->device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 up(&register_mutex);
908 return -EBUSY;
909 }
Clemens Ladischf87135f2005-11-20 14:06:59 +0100910 list_add_tail(&pcm->list, &snd_pcm_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 for (cidx = 0; cidx < 2; cidx++) {
912 int devtype = -1;
913 if (pcm->streams[cidx].substream == NULL)
914 continue;
915 switch (cidx) {
916 case SNDRV_PCM_STREAM_PLAYBACK:
917 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
919 break;
920 case SNDRV_PCM_STREAM_CAPTURE:
921 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
923 break;
924 }
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100925 if ((err = snd_register_device(devtype, pcm->card,
926 pcm->device,
Clemens Ladischf87135f2005-11-20 14:06:59 +0100927 &snd_pcm_f_ops[cidx],
928 pcm, str)) < 0)
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100929 {
Clemens Ladischf87135f2005-11-20 14:06:59 +0100930 list_del(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 up(&register_mutex);
932 return err;
933 }
934 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
935 snd_pcm_timer_init(substream);
936 }
937 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100938 struct snd_pcm_notify *notify;
939 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 notify->n_register(pcm);
941 }
942 up(&register_mutex);
943 return 0;
944}
945
Takashi Iwai877211f2005-11-17 13:59:38 +0100946static int snd_pcm_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Takashi Iwai877211f2005-11-17 13:59:38 +0100948 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100950 struct snd_pcm_substream *substream;
Clemens Ladischf87135f2005-11-20 14:06:59 +0100951 int cidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 down(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100954 list_del_init(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 for (cidx = 0; cidx < 2; cidx++)
956 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
957 if (substream->runtime)
958 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
959 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100960 struct snd_pcm_notify *notify;
961 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 notify->n_disconnect(pcm);
963 }
964 up(&register_mutex);
965 return 0;
966}
967
Takashi Iwai877211f2005-11-17 13:59:38 +0100968static int snd_pcm_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Clemens Ladischf87135f2005-11-20 14:06:59 +0100970 int cidx, devtype;
Takashi Iwai877211f2005-11-17 13:59:38 +0100971 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct list_head *list;
Takashi Iwai877211f2005-11-17 13:59:38 +0100973 struct snd_pcm *pcm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 snd_assert(pcm != NULL, return -ENXIO);
976 down(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +0100977 list_del(&pcm->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 for (cidx = 0; cidx < 2; cidx++) {
979 devtype = -1;
980 switch (cidx) {
981 case SNDRV_PCM_STREAM_PLAYBACK:
982 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
983 break;
984 case SNDRV_PCM_STREAM_CAPTURE:
985 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
986 break;
987 }
988 snd_unregister_device(devtype, pcm->card, pcm->device);
989 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
990 snd_pcm_timer_done(substream);
991 }
992 list_for_each(list, &snd_pcm_notify_list) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100993 struct snd_pcm_notify *notify;
994 notify = list_entry(list, struct snd_pcm_notify, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 notify->n_unregister(pcm);
996 }
997 up(&register_mutex);
998 return snd_pcm_free(pcm);
999}
1000
Takashi Iwai877211f2005-11-17 13:59:38 +01001001int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001003 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
1006 down(&register_mutex);
1007 if (nfree) {
1008 list_del(&notify->list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001009 list_for_each(p, &snd_pcm_devices)
1010 notify->n_unregister(list_entry(p,
1011 struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 } else {
1013 list_add_tail(&notify->list, &snd_pcm_notify_list);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001014 list_for_each(p, &snd_pcm_devices)
1015 notify->n_register(list_entry(p, struct snd_pcm, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 up(&register_mutex);
1018 return 0;
1019}
1020
Takashi Iwaie28563c2005-12-01 10:42:42 +01001021#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * Info interface
1024 */
1025
Takashi Iwai877211f2005-11-17 13:59:38 +01001026static void snd_pcm_proc_read(struct snd_info_entry *entry,
1027 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Clemens Ladischf87135f2005-11-20 14:06:59 +01001029 struct list_head *p;
Takashi Iwai877211f2005-11-17 13:59:38 +01001030 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 down(&register_mutex);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001033 list_for_each(p, &snd_pcm_devices) {
1034 pcm = list_entry(p, struct snd_pcm, list);
1035 snd_iprintf(buffer, "%02i-%02i: %s : %s",
1036 pcm->card->number, pcm->device, pcm->id, pcm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001038 snd_iprintf(buffer, " : playback %i",
1039 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
Takashi Iwai877211f2005-11-17 13:59:38 +01001041 snd_iprintf(buffer, " : capture %i",
1042 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 snd_iprintf(buffer, "\n");
1044 }
1045 up(&register_mutex);
1046}
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static struct snd_info_entry *snd_pcm_proc_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Takashi Iwaie28563c2005-12-01 10:42:42 +01001050static void snd_pcm_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Takashi Iwai877211f2005-11-17 13:59:38 +01001052 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001055 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128,
1056 snd_pcm_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (snd_info_register(entry) < 0) {
1058 snd_info_free_entry(entry);
1059 entry = NULL;
1060 }
1061 }
1062 snd_pcm_proc_entry = entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001063}
1064
1065static void snd_pcm_proc_done(void)
1066{
1067 if (snd_pcm_proc_entry)
1068 snd_info_unregister(snd_pcm_proc_entry);
1069}
1070
1071#else /* !CONFIG_PROC_FS */
1072#define snd_pcm_proc_init()
1073#define snd_pcm_proc_done()
1074#endif /* CONFIG_PROC_FS */
1075
1076
1077/*
1078 * ENTRY functions
1079 */
1080
1081static int __init alsa_pcm_init(void)
1082{
1083 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1084 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1085 snd_pcm_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 0;
1087}
1088
1089static void __exit alsa_pcm_exit(void)
1090{
1091 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1092 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001093 snd_pcm_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096module_init(alsa_pcm_init)
1097module_exit(alsa_pcm_exit)
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099EXPORT_SYMBOL(snd_pcm_new);
1100EXPORT_SYMBOL(snd_pcm_new_stream);
1101EXPORT_SYMBOL(snd_pcm_notify);
1102EXPORT_SYMBOL(snd_pcm_open_substream);
1103EXPORT_SYMBOL(snd_pcm_release_substream);
1104EXPORT_SYMBOL(snd_pcm_format_name);
1105 /* pcm_native.c */
1106EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#ifdef CONFIG_PM
1108EXPORT_SYMBOL(snd_pcm_suspend);
1109EXPORT_SYMBOL(snd_pcm_suspend_all);
1110#endif
1111EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1112EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1113EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1114EXPORT_SYMBOL(snd_pcm_mmap_data);
1115#if SNDRV_PCM_INFO_MMAP_IOMEM
1116EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1117#endif
1118 /* pcm_misc.c */
1119EXPORT_SYMBOL(snd_pcm_format_signed);
1120EXPORT_SYMBOL(snd_pcm_format_unsigned);
1121EXPORT_SYMBOL(snd_pcm_format_linear);
1122EXPORT_SYMBOL(snd_pcm_format_little_endian);
1123EXPORT_SYMBOL(snd_pcm_format_big_endian);
1124EXPORT_SYMBOL(snd_pcm_format_width);
1125EXPORT_SYMBOL(snd_pcm_format_physical_width);
Takashi Iwai9502dca2005-05-18 16:25:46 +02001126EXPORT_SYMBOL(snd_pcm_format_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127EXPORT_SYMBOL(snd_pcm_format_silence_64);
1128EXPORT_SYMBOL(snd_pcm_format_set_silence);
1129EXPORT_SYMBOL(snd_pcm_build_linear_format);
1130EXPORT_SYMBOL(snd_pcm_limit_hw_rates);