Merge branch 'block-dir' of git://brick.kernel.dk/data/git/linux-2.6-block
[linux-2.6] / sound / core / pcm.c
1 /*
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
32 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
33 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34 MODULE_LICENSE("GPL");
35
36 snd_pcm_t *snd_pcm_devices[SNDRV_CARDS * SNDRV_PCM_DEVICES];
37 static LIST_HEAD(snd_pcm_notify_list);
38 static DECLARE_MUTEX(register_mutex);
39
40 static int snd_pcm_free(snd_pcm_t *pcm);
41 static int snd_pcm_dev_free(snd_device_t *device);
42 static int snd_pcm_dev_register(snd_device_t *device);
43 static int snd_pcm_dev_disconnect(snd_device_t *device);
44 static int snd_pcm_dev_unregister(snd_device_t *device);
45
46 static int snd_pcm_control_ioctl(snd_card_t * card,
47                                  snd_ctl_file_t * control,
48                                  unsigned int cmd, unsigned long arg)
49 {
50         unsigned int tmp;
51
52         tmp = card->number * SNDRV_PCM_DEVICES;
53         switch (cmd) {
54         case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
55                 {
56                         int device;
57
58                         if (get_user(device, (int __user *)arg))
59                                 return -EFAULT;
60                         device = device < 0 ? 0 : device + 1;
61                         while (device < SNDRV_PCM_DEVICES) {
62                                 if (snd_pcm_devices[tmp + device])
63                                         break;
64                                 device++;
65                         }
66                         if (device == SNDRV_PCM_DEVICES)
67                                 device = -1;
68                         if (put_user(device, (int __user *)arg))
69                                 return -EFAULT;
70                         return 0;
71                 }
72         case SNDRV_CTL_IOCTL_PCM_INFO:
73                 {
74                         snd_pcm_info_t __user *info;
75                         unsigned int device, subdevice;
76                         snd_pcm_stream_t stream;
77                         snd_pcm_t *pcm;
78                         snd_pcm_str_t *pstr;
79                         snd_pcm_substream_t *substream;
80                         info = (snd_pcm_info_t __user *)arg;
81                         if (get_user(device, &info->device))
82                                 return -EFAULT;
83                         if (device >= SNDRV_PCM_DEVICES)
84                                 return -ENXIO;
85                         pcm = snd_pcm_devices[tmp + device];
86                         if (pcm == NULL)
87                                 return -ENXIO;
88                         if (get_user(stream, &info->stream))
89                                 return -EFAULT;
90                         if (stream < 0 || stream > 1)
91                                 return -EINVAL;
92                         pstr = &pcm->streams[stream];
93                         if (pstr->substream_count == 0)
94                                 return -ENOENT;
95                         if (get_user(subdevice, &info->subdevice))
96                                 return -EFAULT;
97                         if (subdevice >= pstr->substream_count)
98                                 return -ENXIO;
99                         for (substream = pstr->substream; substream; substream = substream->next)
100                                 if (substream->number == (int)subdevice)
101                                         break;
102                         if (substream == NULL)
103                                 return -ENXIO;
104                         return snd_pcm_info_user(substream, info);
105                 }
106         case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
107                 {
108                         int val;
109                         
110                         if (get_user(val, (int __user *)arg))
111                                 return -EFAULT;
112                         control->prefer_pcm_subdevice = val;
113                         return 0;
114                 }
115         }
116         return -ENOIOCTLCMD;
117 }
118 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
119 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
120 #define READY(v) [SNDRV_PCM_READY_##v] = #v
121 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
122 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
123 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
124 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
125 #define START(v) [SNDRV_PCM_START_##v] = #v
126 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
127 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v 
128
129 static char *snd_pcm_stream_names[] = {
130         STREAM(PLAYBACK),
131         STREAM(CAPTURE),
132 };
133
134 static char *snd_pcm_state_names[] = {
135         STATE(OPEN),
136         STATE(SETUP),
137         STATE(PREPARED),
138         STATE(RUNNING),
139         STATE(XRUN),
140         STATE(DRAINING),
141         STATE(PAUSED),
142         STATE(SUSPENDED),
143 };
144
145 static char *snd_pcm_access_names[] = {
146         ACCESS(MMAP_INTERLEAVED), 
147         ACCESS(MMAP_NONINTERLEAVED),
148         ACCESS(MMAP_COMPLEX),
149         ACCESS(RW_INTERLEAVED),
150         ACCESS(RW_NONINTERLEAVED),
151 };
152
153 static char *snd_pcm_format_names[] = {
154         FORMAT(S8),
155         FORMAT(U8),
156         FORMAT(S16_LE),
157         FORMAT(S16_BE),
158         FORMAT(U16_LE),
159         FORMAT(U16_BE),
160         FORMAT(S24_LE),
161         FORMAT(S24_BE),
162         FORMAT(U24_LE),
163         FORMAT(U24_BE),
164         FORMAT(S32_LE),
165         FORMAT(S32_BE),
166         FORMAT(U32_LE),
167         FORMAT(U32_BE),
168         FORMAT(FLOAT_LE),
169         FORMAT(FLOAT_BE),
170         FORMAT(FLOAT64_LE),
171         FORMAT(FLOAT64_BE),
172         FORMAT(IEC958_SUBFRAME_LE),
173         FORMAT(IEC958_SUBFRAME_BE),
174         FORMAT(MU_LAW),
175         FORMAT(A_LAW),
176         FORMAT(IMA_ADPCM),
177         FORMAT(MPEG),
178         FORMAT(GSM),
179         FORMAT(SPECIAL),
180         FORMAT(S24_3LE),
181         FORMAT(S24_3BE),
182         FORMAT(U24_3LE),
183         FORMAT(U24_3BE),
184         FORMAT(S20_3LE),
185         FORMAT(S20_3BE),
186         FORMAT(U20_3LE),
187         FORMAT(U20_3BE),
188         FORMAT(S18_3LE),
189         FORMAT(S18_3BE),
190         FORMAT(U18_3LE),
191         FORMAT(U18_3BE),
192 };
193
194 static char *snd_pcm_subformat_names[] = {
195         SUBFORMAT(STD), 
196 };
197
198 static char *snd_pcm_tstamp_mode_names[] = {
199         TSTAMP(NONE),
200         TSTAMP(MMAP),
201 };
202
203 static const char *snd_pcm_stream_name(snd_pcm_stream_t stream)
204 {
205         snd_assert(stream <= SNDRV_PCM_STREAM_LAST, return NULL);
206         return snd_pcm_stream_names[stream];
207 }
208
209 static const char *snd_pcm_access_name(snd_pcm_access_t access)
210 {
211         snd_assert(access <= SNDRV_PCM_ACCESS_LAST, return NULL);
212         return snd_pcm_access_names[access];
213 }
214
215 const char *snd_pcm_format_name(snd_pcm_format_t format)
216 {
217         snd_assert(format <= SNDRV_PCM_FORMAT_LAST, return NULL);
218         return snd_pcm_format_names[format];
219 }
220
221 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
222 {
223         snd_assert(subformat <= SNDRV_PCM_SUBFORMAT_LAST, return NULL);
224         return snd_pcm_subformat_names[subformat];
225 }
226
227 static const char *snd_pcm_tstamp_mode_name(snd_pcm_tstamp_t mode)
228 {
229         snd_assert(mode <= SNDRV_PCM_TSTAMP_LAST, return NULL);
230         return snd_pcm_tstamp_mode_names[mode];
231 }
232
233 static const char *snd_pcm_state_name(snd_pcm_state_t state)
234 {
235         snd_assert(state <= SNDRV_PCM_STATE_LAST, return NULL);
236         return snd_pcm_state_names[state];
237 }
238
239 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
240 #include <linux/soundcard.h>
241 static const char *snd_pcm_oss_format_name(int format)
242 {
243         switch (format) {
244         case AFMT_MU_LAW:
245                 return "MU_LAW";
246         case AFMT_A_LAW:
247                 return "A_LAW";
248         case AFMT_IMA_ADPCM:
249                 return "IMA_ADPCM";
250         case AFMT_U8:
251                 return "U8";
252         case AFMT_S16_LE:
253                 return "S16_LE";
254         case AFMT_S16_BE:
255                 return "S16_BE";
256         case AFMT_S8:
257                 return "S8";
258         case AFMT_U16_LE:
259                 return "U16_LE";
260         case AFMT_U16_BE:
261                 return "U16_BE";
262         case AFMT_MPEG:
263                 return "MPEG";
264         default:
265                 return "unknown";
266         }
267 }
268 #endif
269
270 #ifdef CONFIG_PROC_FS
271 static void snd_pcm_proc_info_read(snd_pcm_substream_t *substream, snd_info_buffer_t *buffer)
272 {
273         snd_pcm_info_t *info;
274         int err;
275
276         if (! substream)
277                 return;
278
279         info = kmalloc(sizeof(*info), GFP_KERNEL);
280         if (! info) {
281                 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
282                 return;
283         }
284
285         err = snd_pcm_info(substream, info);
286         if (err < 0) {
287                 snd_iprintf(buffer, "error %d\n", err);
288                 kfree(info);
289                 return;
290         }
291         snd_iprintf(buffer, "card: %d\n", info->card);
292         snd_iprintf(buffer, "device: %d\n", info->device);
293         snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
294         snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
295         snd_iprintf(buffer, "id: %s\n", info->id);
296         snd_iprintf(buffer, "name: %s\n", info->name);
297         snd_iprintf(buffer, "subname: %s\n", info->subname);
298         snd_iprintf(buffer, "class: %d\n", info->dev_class);
299         snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
300         snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
301         snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
302         kfree(info);
303 }
304
305 static void snd_pcm_stream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
306 {
307         snd_pcm_proc_info_read(((snd_pcm_str_t *)entry->private_data)->substream, buffer);
308 }
309
310 static void snd_pcm_substream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
311 {
312         snd_pcm_proc_info_read((snd_pcm_substream_t *)entry->private_data, buffer);
313 }
314
315 static void snd_pcm_substream_proc_hw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
316 {
317         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
318         snd_pcm_runtime_t *runtime = substream->runtime;
319         if (!runtime) {
320                 snd_iprintf(buffer, "closed\n");
321                 return;
322         }
323         snd_pcm_stream_lock_irq(substream);
324         if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
325                 snd_iprintf(buffer, "no setup\n");
326                 snd_pcm_stream_unlock_irq(substream);
327                 return;
328         }
329         snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
330         snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
331         snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
332         snd_iprintf(buffer, "channels: %u\n", runtime->channels);       
333         snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den); 
334         snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);        
335         snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);        
336         snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
337 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
338         if (substream->oss.oss) {
339                 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
340                 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);       
341                 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
342                 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
343                 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
344                 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
345         }
346 #endif
347         snd_pcm_stream_unlock_irq(substream);
348 }
349
350 static void snd_pcm_substream_proc_sw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
351 {
352         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
353         snd_pcm_runtime_t *runtime = substream->runtime;
354         if (!runtime) {
355                 snd_iprintf(buffer, "closed\n");
356                 return;
357         }
358         snd_pcm_stream_lock_irq(substream);
359         if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
360                 snd_iprintf(buffer, "no setup\n");
361                 snd_pcm_stream_unlock_irq(substream);
362                 return;
363         }
364         snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
365         snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
366         snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
367         snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
368         snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
369         snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
370         snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
371         snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
372         snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
373         snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
374         snd_pcm_stream_unlock_irq(substream);
375 }
376
377 static void snd_pcm_substream_proc_status_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
378 {
379         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
380         snd_pcm_runtime_t *runtime = substream->runtime;
381         snd_pcm_status_t status;
382         int err;
383         if (!runtime) {
384                 snd_iprintf(buffer, "closed\n");
385                 return;
386         }
387         memset(&status, 0, sizeof(status));
388         err = snd_pcm_status(substream, &status);
389         if (err < 0) {
390                 snd_iprintf(buffer, "error %d\n", err);
391                 return;
392         }
393         snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
394         snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
395                 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
396         snd_iprintf(buffer, "tstamp      : %ld.%09ld\n",
397                 status.tstamp.tv_sec, status.tstamp.tv_nsec);
398         snd_iprintf(buffer, "delay       : %ld\n", status.delay);
399         snd_iprintf(buffer, "avail       : %ld\n", status.avail);
400         snd_iprintf(buffer, "avail_max   : %ld\n", status.avail_max);
401         snd_iprintf(buffer, "-----\n");
402         snd_iprintf(buffer, "hw_ptr      : %ld\n", runtime->status->hw_ptr);
403         snd_iprintf(buffer, "appl_ptr    : %ld\n", runtime->control->appl_ptr);
404 }
405 #endif
406
407 #ifdef CONFIG_SND_DEBUG
408 static void snd_pcm_xrun_debug_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
409 {
410         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
411         snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
412 }
413
414 static void snd_pcm_xrun_debug_write(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
415 {
416         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
417         char line[64];
418         if (!snd_info_get_line(buffer, line, sizeof(line)))
419                 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
420 }
421 #endif
422
423 static int snd_pcm_stream_proc_init(snd_pcm_str_t *pstr)
424 {
425         snd_pcm_t *pcm = pstr->pcm;
426         snd_info_entry_t *entry;
427         char name[16];
428
429         sprintf(name, "pcm%i%c", pcm->device, 
430                 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
431         if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
432                 return -ENOMEM;
433         entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
434         if (snd_info_register(entry) < 0) {
435                 snd_info_free_entry(entry);
436                 return -ENOMEM;
437         }
438         pstr->proc_root = entry;
439
440         if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
441                 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
442                 if (snd_info_register(entry) < 0) {
443                         snd_info_free_entry(entry);
444                         entry = NULL;
445                 }
446         }
447         pstr->proc_info_entry = entry;
448
449 #ifdef CONFIG_SND_DEBUG
450         if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug", pstr->proc_root)) != NULL) {
451                 entry->c.text.read_size = 64;
452                 entry->c.text.read = snd_pcm_xrun_debug_read;
453                 entry->c.text.write_size = 64;
454                 entry->c.text.write = snd_pcm_xrun_debug_write;
455                 entry->mode |= S_IWUSR;
456                 entry->private_data = pstr;
457                 if (snd_info_register(entry) < 0) {
458                         snd_info_free_entry(entry);
459                         entry = NULL;
460                 }
461         }
462         pstr->proc_xrun_debug_entry = entry;
463 #endif
464         return 0;
465 }
466
467 static int snd_pcm_stream_proc_done(snd_pcm_str_t *pstr)
468 {
469 #ifdef CONFIG_SND_DEBUG
470         if (pstr->proc_xrun_debug_entry) {
471                 snd_info_unregister(pstr->proc_xrun_debug_entry);
472                 pstr->proc_xrun_debug_entry = NULL;
473         }
474 #endif
475         if (pstr->proc_info_entry) {
476                 snd_info_unregister(pstr->proc_info_entry);
477                 pstr->proc_info_entry = NULL;
478         }
479         if (pstr->proc_root) {
480                 snd_info_unregister(pstr->proc_root);
481                 pstr->proc_root = NULL;
482         }
483         return 0;
484 }
485
486 static int snd_pcm_substream_proc_init(snd_pcm_substream_t *substream)
487 {
488         snd_info_entry_t *entry;
489         snd_card_t *card;
490         char name[16];
491
492         card = substream->pcm->card;
493
494         sprintf(name, "sub%i", substream->number);
495         if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
496                 return -ENOMEM;
497         entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
498         if (snd_info_register(entry) < 0) {
499                 snd_info_free_entry(entry);
500                 return -ENOMEM;
501         }
502         substream->proc_root = entry;
503
504         if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
505                 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
506                 if (snd_info_register(entry) < 0) {
507                         snd_info_free_entry(entry);
508                         entry = NULL;
509                 }
510         }
511         substream->proc_info_entry = entry;
512
513         if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
514                 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
515                 if (snd_info_register(entry) < 0) {
516                         snd_info_free_entry(entry);
517                         entry = NULL;
518                 }
519         }
520         substream->proc_hw_params_entry = entry;
521
522         if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
523                 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
524                 if (snd_info_register(entry) < 0) {
525                         snd_info_free_entry(entry);
526                         entry = NULL;
527                 }
528         }
529         substream->proc_sw_params_entry = entry;
530
531         if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
532                 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
533                 if (snd_info_register(entry) < 0) {
534                         snd_info_free_entry(entry);
535                         entry = NULL;
536                 }
537         }
538         substream->proc_status_entry = entry;
539
540         return 0;
541 }
542                 
543 static int snd_pcm_substream_proc_done(snd_pcm_substream_t *substream)
544 {
545         if (substream->proc_info_entry) {
546                 snd_info_unregister(substream->proc_info_entry);
547                 substream->proc_info_entry = NULL;
548         }
549         if (substream->proc_hw_params_entry) {
550                 snd_info_unregister(substream->proc_hw_params_entry);
551                 substream->proc_hw_params_entry = NULL;
552         }
553         if (substream->proc_sw_params_entry) {
554                 snd_info_unregister(substream->proc_sw_params_entry);
555                 substream->proc_sw_params_entry = NULL;
556         }
557         if (substream->proc_status_entry) {
558                 snd_info_unregister(substream->proc_status_entry);
559                 substream->proc_status_entry = NULL;
560         }
561         if (substream->proc_root) {
562                 snd_info_unregister(substream->proc_root);
563                 substream->proc_root = NULL;
564         }
565         return 0;
566 }
567
568 /**
569  * snd_pcm_new_stream - create a new PCM stream
570  * @pcm: the pcm instance
571  * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
572  * @substream_count: the number of substreams
573  *
574  * Creates a new stream for the pcm.
575  * The corresponding stream on the pcm must have been empty before
576  * calling this, i.e. zero must be given to the argument of
577  * snd_pcm_new().
578  *
579  * Returns zero if successful, or a negative error code on failure.
580  */
581 int snd_pcm_new_stream(snd_pcm_t *pcm, int stream, int substream_count)
582 {
583         int idx, err;
584         snd_pcm_str_t *pstr = &pcm->streams[stream];
585         snd_pcm_substream_t *substream, *prev;
586
587 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
588         init_MUTEX(&pstr->oss.setup_mutex);
589 #endif
590         pstr->stream = stream;
591         pstr->pcm = pcm;
592         pstr->substream_count = substream_count;
593         pstr->reg = &snd_pcm_reg[stream];
594         if (substream_count > 0) {
595                 err = snd_pcm_stream_proc_init(pstr);
596                 if (err < 0)
597                         return err;
598         }
599         prev = NULL;
600         for (idx = 0, prev = NULL; idx < substream_count; idx++) {
601                 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
602                 if (substream == NULL)
603                         return -ENOMEM;
604                 substream->pcm = pcm;
605                 substream->pstr = pstr;
606                 substream->number = idx;
607                 substream->stream = stream;
608                 sprintf(substream->name, "subdevice #%i", idx);
609                 substream->buffer_bytes_max = UINT_MAX;
610                 if (prev == NULL)
611                         pstr->substream = substream;
612                 else
613                         prev->next = substream;
614                 err = snd_pcm_substream_proc_init(substream);
615                 if (err < 0) {
616                         kfree(substream);
617                         return err;
618                 }
619                 substream->group = &substream->self_group;
620                 spin_lock_init(&substream->self_group.lock);
621                 INIT_LIST_HEAD(&substream->self_group.substreams);
622                 list_add_tail(&substream->link_list, &substream->self_group.substreams);
623                 spin_lock_init(&substream->timer_lock);
624                 prev = substream;
625         }
626         return 0;
627 }                               
628
629 /**
630  * snd_pcm_new - create a new PCM instance
631  * @card: the card instance
632  * @id: the id string
633  * @device: the device index (zero based)
634  * @playback_count: the number of substreams for playback
635  * @capture_count: the number of substreams for capture
636  * @rpcm: the pointer to store the new pcm instance
637  *
638  * Creates a new PCM instance.
639  *
640  * The pcm operators have to be set afterwards to the new instance
641  * via snd_pcm_set_ops().
642  *
643  * Returns zero if successful, or a negative error code on failure.
644  */
645 int snd_pcm_new(snd_card_t * card, char *id, int device,
646                 int playback_count, int capture_count,
647                 snd_pcm_t ** rpcm)
648 {
649         snd_pcm_t *pcm;
650         int err;
651         static snd_device_ops_t ops = {
652                 .dev_free = snd_pcm_dev_free,
653                 .dev_register = snd_pcm_dev_register,
654                 .dev_disconnect = snd_pcm_dev_disconnect,
655                 .dev_unregister = snd_pcm_dev_unregister
656         };
657
658         snd_assert(rpcm != NULL, return -EINVAL);
659         *rpcm = NULL;
660         snd_assert(card != NULL, return -ENXIO);
661         pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
662         if (pcm == NULL)
663                 return -ENOMEM;
664         pcm->card = card;
665         pcm->device = device;
666         if (id) {
667                 strlcpy(pcm->id, id, sizeof(pcm->id));
668         }
669         if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
670                 snd_pcm_free(pcm);
671                 return err;
672         }
673         if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
674                 snd_pcm_free(pcm);
675                 return err;
676         }
677         init_MUTEX(&pcm->open_mutex);
678         init_waitqueue_head(&pcm->open_wait);
679         if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
680                 snd_pcm_free(pcm);
681                 return err;
682         }
683         *rpcm = pcm;
684         return 0;
685 }
686
687 static void snd_pcm_free_stream(snd_pcm_str_t * pstr)
688 {
689         snd_pcm_substream_t *substream, *substream_next;
690 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
691         snd_pcm_oss_setup_t *setup, *setupn;
692 #endif
693         substream = pstr->substream;
694         while (substream) {
695                 substream_next = substream->next;
696                 snd_pcm_substream_proc_done(substream);
697                 kfree(substream);
698                 substream = substream_next;
699         }
700         snd_pcm_stream_proc_done(pstr);
701 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
702         for (setup = pstr->oss.setup_list; setup; setup = setupn) {
703                 setupn = setup->next;
704                 kfree(setup->task_name);
705                 kfree(setup);
706         }
707 #endif
708 }
709
710 static int snd_pcm_free(snd_pcm_t *pcm)
711 {
712         snd_assert(pcm != NULL, return -ENXIO);
713         if (pcm->private_free)
714                 pcm->private_free(pcm);
715         snd_pcm_lib_preallocate_free_for_all(pcm);
716         snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
717         snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
718         kfree(pcm);
719         return 0;
720 }
721
722 static int snd_pcm_dev_free(snd_device_t *device)
723 {
724         snd_pcm_t *pcm = device->device_data;
725         return snd_pcm_free(pcm);
726 }
727
728 static void snd_pcm_tick_timer_func(unsigned long data)
729 {
730         snd_pcm_substream_t *substream = (snd_pcm_substream_t*) data;
731         snd_pcm_tick_elapsed(substream);
732 }
733
734 int snd_pcm_open_substream(snd_pcm_t *pcm, int stream,
735                            snd_pcm_substream_t **rsubstream)
736 {
737         snd_pcm_str_t * pstr;
738         snd_pcm_substream_t * substream;
739         snd_pcm_runtime_t * runtime;
740         snd_ctl_file_t *kctl;
741         snd_card_t *card;
742         struct list_head *list;
743         int prefer_subdevice = -1;
744         size_t size;
745
746         snd_assert(rsubstream != NULL, return -EINVAL);
747         *rsubstream = NULL;
748         snd_assert(pcm != NULL, return -ENXIO);
749         pstr = &pcm->streams[stream];
750         if (pstr->substream == NULL)
751                 return -ENODEV;
752
753         card = pcm->card;
754         down_read(&card->controls_rwsem);
755         list_for_each(list, &card->ctl_files) {
756                 kctl = snd_ctl_file(list);
757                 if (kctl->pid == current->pid) {
758                         prefer_subdevice = kctl->prefer_pcm_subdevice;
759                         break;
760                 }
761         }
762         up_read(&card->controls_rwsem);
763
764         if (pstr->substream_count == 0)
765                 return -ENODEV;
766         switch (stream) {
767         case SNDRV_PCM_STREAM_PLAYBACK:
768                 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
769                         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
770                                 if (SUBSTREAM_BUSY(substream))
771                                         return -EAGAIN;
772                         }
773                 }
774                 break;
775         case SNDRV_PCM_STREAM_CAPTURE:
776                 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
777                         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
778                                 if (SUBSTREAM_BUSY(substream))
779                                         return -EAGAIN;
780                         }
781                 }
782                 break;
783         default:
784                 return -EINVAL;
785         }
786
787         if (prefer_subdevice >= 0) {
788                 for (substream = pstr->substream; substream; substream = substream->next)
789                         if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
790                                 goto __ok;
791         }
792         for (substream = pstr->substream; substream; substream = substream->next)
793                 if (!SUBSTREAM_BUSY(substream))
794                         break;
795       __ok:
796         if (substream == NULL)
797                 return -EAGAIN;
798
799         runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
800         if (runtime == NULL)
801                 return -ENOMEM;
802
803         size = PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t));
804         runtime->status = snd_malloc_pages(size, GFP_KERNEL);
805         if (runtime->status == NULL) {
806                 kfree(runtime);
807                 return -ENOMEM;
808         }
809         memset((void*)runtime->status, 0, size);
810
811         size = PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t));
812         runtime->control = snd_malloc_pages(size, GFP_KERNEL);
813         if (runtime->control == NULL) {
814                 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
815                 kfree(runtime);
816                 return -ENOMEM;
817         }
818         memset((void*)runtime->control, 0, size);
819
820         init_waitqueue_head(&runtime->sleep);
821         atomic_set(&runtime->mmap_count, 0);
822         init_timer(&runtime->tick_timer);
823         runtime->tick_timer.function = snd_pcm_tick_timer_func;
824         runtime->tick_timer.data = (unsigned long) substream;
825
826         runtime->status->state = SNDRV_PCM_STATE_OPEN;
827
828         substream->runtime = runtime;
829         substream->private_data = pcm->private_data;
830         pstr->substream_opened++;
831         *rsubstream = substream;
832         return 0;
833 }
834
835 void snd_pcm_release_substream(snd_pcm_substream_t *substream)
836 {
837         snd_pcm_runtime_t * runtime;
838         substream->file = NULL;
839         runtime = substream->runtime;
840         snd_assert(runtime != NULL, return);
841         if (runtime->private_free != NULL)
842                 runtime->private_free(runtime);
843         snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
844         snd_free_pages((void*)runtime->control, PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)));
845         kfree(runtime->hw_constraints.rules);
846         kfree(runtime);
847         substream->runtime = NULL;
848         substream->pstr->substream_opened--;
849 }
850
851 static int snd_pcm_dev_register(snd_device_t *device)
852 {
853         int idx, cidx, err;
854         unsigned short minor;
855         snd_pcm_substream_t *substream;
856         struct list_head *list;
857         char str[16];
858         snd_pcm_t *pcm = device->device_data;
859
860         snd_assert(pcm != NULL && device != NULL, return -ENXIO);
861         down(&register_mutex);
862         idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
863         if (snd_pcm_devices[idx]) {
864                 up(&register_mutex);
865                 return -EBUSY;
866         }
867         snd_pcm_devices[idx] = pcm;
868         for (cidx = 0; cidx < 2; cidx++) {
869                 int devtype = -1;
870                 if (pcm->streams[cidx].substream == NULL)
871                         continue;
872                 switch (cidx) {
873                 case SNDRV_PCM_STREAM_PLAYBACK:
874                         sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
875                         minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
876                         devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
877                         break;
878                 case SNDRV_PCM_STREAM_CAPTURE:
879                         sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
880                         minor = SNDRV_MINOR_PCM_CAPTURE + idx;
881                         devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
882                         break;
883                 }
884                 if ((err = snd_register_device(devtype, pcm->card, pcm->device, pcm->streams[cidx].reg, str)) < 0) {
885                         snd_pcm_devices[idx] = NULL;
886                         up(&register_mutex);
887                         return err;
888                 }
889                 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
890                         snd_pcm_timer_init(substream);
891         }
892         list_for_each(list, &snd_pcm_notify_list) {
893                 snd_pcm_notify_t *notify;
894                 notify = list_entry(list, snd_pcm_notify_t, list);
895                 notify->n_register(pcm);
896         }
897         up(&register_mutex);
898         return 0;
899 }
900
901 static int snd_pcm_dev_disconnect(snd_device_t *device)
902 {
903         snd_pcm_t *pcm = device->device_data;
904         struct list_head *list;
905         snd_pcm_substream_t *substream;
906         int idx, cidx;
907
908         down(&register_mutex);
909         idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
910         snd_pcm_devices[idx] = NULL;
911         for (cidx = 0; cidx < 2; cidx++)
912                 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
913                         if (substream->runtime)
914                                 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
915         list_for_each(list, &snd_pcm_notify_list) {
916                 snd_pcm_notify_t *notify;
917                 notify = list_entry(list, snd_pcm_notify_t, list);
918                 notify->n_disconnect(pcm);
919         }
920         up(&register_mutex);
921         return 0;
922 }
923
924 static int snd_pcm_dev_unregister(snd_device_t *device)
925 {
926         int idx, cidx, devtype;
927         snd_pcm_substream_t *substream;
928         struct list_head *list;
929         snd_pcm_t *pcm = device->device_data;
930
931         snd_assert(pcm != NULL, return -ENXIO);
932         down(&register_mutex);
933         idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
934         snd_pcm_devices[idx] = NULL;
935         for (cidx = 0; cidx < 2; cidx++) {
936                 devtype = -1;
937                 switch (cidx) {
938                 case SNDRV_PCM_STREAM_PLAYBACK:
939                         devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
940                         break;
941                 case SNDRV_PCM_STREAM_CAPTURE:
942                         devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
943                         break;
944                 }
945                 snd_unregister_device(devtype, pcm->card, pcm->device);
946                 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
947                         snd_pcm_timer_done(substream);
948         }
949         list_for_each(list, &snd_pcm_notify_list) {
950                 snd_pcm_notify_t *notify;
951                 notify = list_entry(list, snd_pcm_notify_t, list);
952                 notify->n_unregister(pcm);
953         }
954         up(&register_mutex);
955         return snd_pcm_free(pcm);
956 }
957
958 int snd_pcm_notify(snd_pcm_notify_t *notify, int nfree)
959 {
960         int idx;
961
962         snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
963         down(&register_mutex);
964         if (nfree) {
965                 list_del(&notify->list);
966                 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
967                         if (snd_pcm_devices[idx] == NULL)
968                                 continue;
969                         notify->n_unregister(snd_pcm_devices[idx]);
970                 }
971         } else {
972                 list_add_tail(&notify->list, &snd_pcm_notify_list);
973                 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
974                         if (snd_pcm_devices[idx] == NULL)
975                                 continue;
976                         notify->n_register(snd_pcm_devices[idx]);
977                 }
978         }
979         up(&register_mutex);
980         return 0;
981 }
982
983 /*
984  *  Info interface
985  */
986
987 static void snd_pcm_proc_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
988 {
989         int idx;
990         snd_pcm_t *pcm;
991
992         down(&register_mutex);
993         for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
994                 pcm = snd_pcm_devices[idx];
995                 if (pcm == NULL)
996                         continue;
997                 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
998                             idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
999                 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1000                         snd_iprintf(buffer, " : playback %i", pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1001                 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1002                         snd_iprintf(buffer, " : capture %i", pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1003                 snd_iprintf(buffer, "\n");
1004         }
1005         up(&register_mutex);
1006 }
1007
1008 /*
1009  *  ENTRY functions
1010  */
1011
1012 static snd_info_entry_t *snd_pcm_proc_entry = NULL;
1013
1014 static int __init alsa_pcm_init(void)
1015 {
1016         snd_info_entry_t *entry;
1017
1018         snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1019         snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1020         if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1021                 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128, snd_pcm_proc_read);
1022                 if (snd_info_register(entry) < 0) {
1023                         snd_info_free_entry(entry);
1024                         entry = NULL;
1025                 }
1026         }
1027         snd_pcm_proc_entry = entry;
1028         return 0;
1029 }
1030
1031 static void __exit alsa_pcm_exit(void)
1032 {
1033         snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1034         snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1035         if (snd_pcm_proc_entry) {
1036                 snd_info_unregister(snd_pcm_proc_entry);
1037                 snd_pcm_proc_entry = NULL;
1038         }
1039 }
1040
1041 module_init(alsa_pcm_init)
1042 module_exit(alsa_pcm_exit)
1043
1044 EXPORT_SYMBOL(snd_pcm_devices);
1045 EXPORT_SYMBOL(snd_pcm_new);
1046 EXPORT_SYMBOL(snd_pcm_new_stream);
1047 EXPORT_SYMBOL(snd_pcm_notify);
1048 EXPORT_SYMBOL(snd_pcm_open_substream);
1049 EXPORT_SYMBOL(snd_pcm_release_substream);
1050 EXPORT_SYMBOL(snd_pcm_format_name);
1051   /* pcm_native.c */
1052 EXPORT_SYMBOL(snd_pcm_link_rwlock);
1053 #ifdef CONFIG_PM
1054 EXPORT_SYMBOL(snd_pcm_suspend);
1055 EXPORT_SYMBOL(snd_pcm_suspend_all);
1056 #endif
1057 EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1058 EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1059 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1060 EXPORT_SYMBOL(snd_pcm_mmap_data);
1061 #if SNDRV_PCM_INFO_MMAP_IOMEM
1062 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1063 #endif
1064  /* pcm_misc.c */
1065 EXPORT_SYMBOL(snd_pcm_format_signed);
1066 EXPORT_SYMBOL(snd_pcm_format_unsigned);
1067 EXPORT_SYMBOL(snd_pcm_format_linear);
1068 EXPORT_SYMBOL(snd_pcm_format_little_endian);
1069 EXPORT_SYMBOL(snd_pcm_format_big_endian);
1070 EXPORT_SYMBOL(snd_pcm_format_width);
1071 EXPORT_SYMBOL(snd_pcm_format_physical_width);
1072 EXPORT_SYMBOL(snd_pcm_format_size);
1073 EXPORT_SYMBOL(snd_pcm_format_silence_64);
1074 EXPORT_SYMBOL(snd_pcm_format_set_silence);
1075 EXPORT_SYMBOL(snd_pcm_build_linear_format);
1076 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);