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