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