blob: d57f4ec3b08bf021f75dddbfe5205bd6d3b9f37a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/time.h>
26#include <sound/core.h>
27#include <sound/minors.h>
28#include <sound/pcm.h>
29#include <sound/control.h>
30#include <sound/info.h>
31
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
33MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
34MODULE_LICENSE("GPL");
35
36snd_pcm_t *snd_pcm_devices[SNDRV_CARDS * SNDRV_PCM_DEVICES];
37static LIST_HEAD(snd_pcm_notify_list);
38static DECLARE_MUTEX(register_mutex);
39
40static int snd_pcm_free(snd_pcm_t *pcm);
41static int snd_pcm_dev_free(snd_device_t *device);
42static int snd_pcm_dev_register(snd_device_t *device);
43static int snd_pcm_dev_disconnect(snd_device_t *device);
44static int snd_pcm_dev_unregister(snd_device_t *device);
45
46static 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
129static char *snd_pcm_stream_names[] = {
130 STREAM(PLAYBACK),
131 STREAM(CAPTURE),
132};
133
134static 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
145static 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
153static 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
194static char *snd_pcm_subformat_names[] = {
195 SUBFORMAT(STD),
196};
197
198static char *snd_pcm_tstamp_mode_names[] = {
199 TSTAMP(NONE),
200 TSTAMP(MMAP),
201};
202
203static 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
209static 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
215const 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
221static 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
227static 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
233static 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>
241static 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
271static 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 snd_runtime_check(substream, return);
277
278 info = kmalloc(sizeof(*info), GFP_KERNEL);
279 if (! info) {
280 printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
281 return;
282 }
283
284 err = snd_pcm_info(substream, info);
285 if (err < 0) {
286 snd_iprintf(buffer, "error %d\n", err);
287 kfree(info);
288 return;
289 }
290 snd_iprintf(buffer, "card: %d\n", info->card);
291 snd_iprintf(buffer, "device: %d\n", info->device);
292 snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
293 snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
294 snd_iprintf(buffer, "id: %s\n", info->id);
295 snd_iprintf(buffer, "name: %s\n", info->name);
296 snd_iprintf(buffer, "subname: %s\n", info->subname);
297 snd_iprintf(buffer, "class: %d\n", info->dev_class);
298 snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
299 snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
300 snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
301 kfree(info);
302}
303
304static void snd_pcm_stream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
305{
306 snd_pcm_proc_info_read(((snd_pcm_str_t *)entry->private_data)->substream, buffer);
307}
308
309static void snd_pcm_substream_proc_info_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
310{
311 snd_pcm_proc_info_read((snd_pcm_substream_t *)entry->private_data, buffer);
312}
313
314static void snd_pcm_substream_proc_hw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
315{
316 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
317 snd_pcm_runtime_t *runtime = substream->runtime;
318 if (!runtime) {
319 snd_iprintf(buffer, "closed\n");
320 return;
321 }
322 snd_pcm_stream_lock_irq(substream);
323 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
324 snd_iprintf(buffer, "no setup\n");
325 snd_pcm_stream_unlock_irq(substream);
326 return;
327 }
328 snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
329 snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
330 snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
331 snd_iprintf(buffer, "channels: %u\n", runtime->channels);
332 snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
333 snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
334 snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
335 snd_iprintf(buffer, "tick_time: %u\n", runtime->tick_time);
336#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
337 if (substream->oss.oss) {
338 snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
339 snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
340 snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
341 snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
342 snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
343 snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
344 }
345#endif
346 snd_pcm_stream_unlock_irq(substream);
347}
348
349static void snd_pcm_substream_proc_sw_params_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
350{
351 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
352 snd_pcm_runtime_t *runtime = substream->runtime;
353 if (!runtime) {
354 snd_iprintf(buffer, "closed\n");
355 return;
356 }
357 snd_pcm_stream_lock_irq(substream);
358 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
359 snd_iprintf(buffer, "no setup\n");
360 snd_pcm_stream_unlock_irq(substream);
361 return;
362 }
363 snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
364 snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
365 snd_iprintf(buffer, "sleep_min: %u\n", runtime->sleep_min);
366 snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
367 snd_iprintf(buffer, "xfer_align: %lu\n", runtime->xfer_align);
368 snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
369 snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
370 snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
371 snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
372 snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
373 snd_pcm_stream_unlock_irq(substream);
374}
375
376static void snd_pcm_substream_proc_status_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
377{
378 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
379 snd_pcm_runtime_t *runtime = substream->runtime;
380 snd_pcm_status_t status;
381 int err;
382 if (!runtime) {
383 snd_iprintf(buffer, "closed\n");
384 return;
385 }
386 memset(&status, 0, sizeof(status));
387 err = snd_pcm_status(substream, &status);
388 if (err < 0) {
389 snd_iprintf(buffer, "error %d\n", err);
390 return;
391 }
392 snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
393 snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
394 status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
395 snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
396 status.tstamp.tv_sec, status.tstamp.tv_nsec);
397 snd_iprintf(buffer, "delay : %ld\n", status.delay);
398 snd_iprintf(buffer, "avail : %ld\n", status.avail);
399 snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);
400 snd_iprintf(buffer, "-----\n");
401 snd_iprintf(buffer, "hw_ptr : %ld\n", runtime->status->hw_ptr);
402 snd_iprintf(buffer, "appl_ptr : %ld\n", runtime->control->appl_ptr);
403}
404#endif
405
406#ifdef CONFIG_SND_DEBUG
407static void snd_pcm_xrun_debug_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
408{
409 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
410 snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
411}
412
413static void snd_pcm_xrun_debug_write(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
414{
415 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
416 char line[64];
417 if (!snd_info_get_line(buffer, line, sizeof(line)))
418 pstr->xrun_debug = simple_strtoul(line, NULL, 10);
419}
420#endif
421
422static int snd_pcm_stream_proc_init(snd_pcm_str_t *pstr)
423{
424 snd_pcm_t *pcm = pstr->pcm;
425 snd_info_entry_t *entry;
426 char name[16];
427
428 sprintf(name, "pcm%i%c", pcm->device,
429 pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
430 if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
431 return -ENOMEM;
432 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
433 if (snd_info_register(entry) < 0) {
434 snd_info_free_entry(entry);
435 return -ENOMEM;
436 }
437 pstr->proc_root = entry;
438
439 if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
440 snd_info_set_text_ops(entry, pstr, 256, snd_pcm_stream_proc_info_read);
441 if (snd_info_register(entry) < 0) {
442 snd_info_free_entry(entry);
443 entry = NULL;
444 }
445 }
446 pstr->proc_info_entry = entry;
447
448#ifdef CONFIG_SND_DEBUG
449 if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug", pstr->proc_root)) != NULL) {
450 entry->c.text.read_size = 64;
451 entry->c.text.read = snd_pcm_xrun_debug_read;
452 entry->c.text.write_size = 64;
453 entry->c.text.write = snd_pcm_xrun_debug_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +0200454 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 entry->private_data = pstr;
456 if (snd_info_register(entry) < 0) {
457 snd_info_free_entry(entry);
458 entry = NULL;
459 }
460 }
461 pstr->proc_xrun_debug_entry = entry;
462#endif
463 return 0;
464}
465
466static int snd_pcm_stream_proc_done(snd_pcm_str_t *pstr)
467{
468#ifdef CONFIG_SND_DEBUG
469 if (pstr->proc_xrun_debug_entry) {
470 snd_info_unregister(pstr->proc_xrun_debug_entry);
471 pstr->proc_xrun_debug_entry = NULL;
472 }
473#endif
474 if (pstr->proc_info_entry) {
475 snd_info_unregister(pstr->proc_info_entry);
476 pstr->proc_info_entry = NULL;
477 }
478 if (pstr->proc_root) {
479 snd_info_unregister(pstr->proc_root);
480 pstr->proc_root = NULL;
481 }
482 return 0;
483}
484
485static int snd_pcm_substream_proc_init(snd_pcm_substream_t *substream)
486{
487 snd_info_entry_t *entry;
488 snd_card_t *card;
489 char name[16];
490
491 card = substream->pcm->card;
492
493 sprintf(name, "sub%i", substream->number);
494 if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
495 return -ENOMEM;
496 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
497 if (snd_info_register(entry) < 0) {
498 snd_info_free_entry(entry);
499 return -ENOMEM;
500 }
501 substream->proc_root = entry;
502
503 if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
504 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_info_read);
505 if (snd_info_register(entry) < 0) {
506 snd_info_free_entry(entry);
507 entry = NULL;
508 }
509 }
510 substream->proc_info_entry = entry;
511
512 if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
513 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_hw_params_read);
514 if (snd_info_register(entry) < 0) {
515 snd_info_free_entry(entry);
516 entry = NULL;
517 }
518 }
519 substream->proc_hw_params_entry = entry;
520
521 if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
522 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_sw_params_read);
523 if (snd_info_register(entry) < 0) {
524 snd_info_free_entry(entry);
525 entry = NULL;
526 }
527 }
528 substream->proc_sw_params_entry = entry;
529
530 if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
531 snd_info_set_text_ops(entry, substream, 256, snd_pcm_substream_proc_status_read);
532 if (snd_info_register(entry) < 0) {
533 snd_info_free_entry(entry);
534 entry = NULL;
535 }
536 }
537 substream->proc_status_entry = entry;
538
539 return 0;
540}
541
542static int snd_pcm_substream_proc_done(snd_pcm_substream_t *substream)
543{
544 if (substream->proc_info_entry) {
545 snd_info_unregister(substream->proc_info_entry);
546 substream->proc_info_entry = NULL;
547 }
548 if (substream->proc_hw_params_entry) {
549 snd_info_unregister(substream->proc_hw_params_entry);
550 substream->proc_hw_params_entry = NULL;
551 }
552 if (substream->proc_sw_params_entry) {
553 snd_info_unregister(substream->proc_sw_params_entry);
554 substream->proc_sw_params_entry = NULL;
555 }
556 if (substream->proc_status_entry) {
557 snd_info_unregister(substream->proc_status_entry);
558 substream->proc_status_entry = NULL;
559 }
560 if (substream->proc_root) {
561 snd_info_unregister(substream->proc_root);
562 substream->proc_root = NULL;
563 }
564 return 0;
565}
566
567/**
568 * snd_pcm_new_stream - create a new PCM stream
569 * @pcm: the pcm instance
570 * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
571 * @substream_count: the number of substreams
572 *
573 * Creates a new stream for the pcm.
574 * The corresponding stream on the pcm must have been empty before
575 * calling this, i.e. zero must be given to the argument of
576 * snd_pcm_new().
577 *
578 * Returns zero if successful, or a negative error code on failure.
579 */
580int snd_pcm_new_stream(snd_pcm_t *pcm, int stream, int substream_count)
581{
582 int idx, err;
583 snd_pcm_str_t *pstr = &pcm->streams[stream];
584 snd_pcm_substream_t *substream, *prev;
585
586#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
587 init_MUTEX(&pstr->oss.setup_mutex);
588#endif
589 pstr->stream = stream;
590 pstr->pcm = pcm;
591 pstr->substream_count = substream_count;
592 pstr->reg = &snd_pcm_reg[stream];
593 if (substream_count > 0) {
594 err = snd_pcm_stream_proc_init(pstr);
595 if (err < 0)
596 return err;
597 }
598 prev = NULL;
599 for (idx = 0, prev = NULL; idx < substream_count; idx++) {
600 substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
601 if (substream == NULL)
602 return -ENOMEM;
603 substream->pcm = pcm;
604 substream->pstr = pstr;
605 substream->number = idx;
606 substream->stream = stream;
607 sprintf(substream->name, "subdevice #%i", idx);
608 substream->buffer_bytes_max = UINT_MAX;
609 if (prev == NULL)
610 pstr->substream = substream;
611 else
612 prev->next = substream;
613 err = snd_pcm_substream_proc_init(substream);
614 if (err < 0) {
615 kfree(substream);
616 return err;
617 }
618 substream->group = &substream->self_group;
619 spin_lock_init(&substream->self_group.lock);
620 INIT_LIST_HEAD(&substream->self_group.substreams);
621 list_add_tail(&substream->link_list, &substream->self_group.substreams);
622 spin_lock_init(&substream->timer_lock);
623 prev = substream;
624 }
625 return 0;
626}
627
628/**
629 * snd_pcm_new - create a new PCM instance
630 * @card: the card instance
631 * @id: the id string
632 * @device: the device index (zero based)
633 * @playback_count: the number of substreams for playback
634 * @capture_count: the number of substreams for capture
635 * @rpcm: the pointer to store the new pcm instance
636 *
637 * Creates a new PCM instance.
638 *
639 * The pcm operators have to be set afterwards to the new instance
640 * via snd_pcm_set_ops().
641 *
642 * Returns zero if successful, or a negative error code on failure.
643 */
644int snd_pcm_new(snd_card_t * card, char *id, int device,
645 int playback_count, int capture_count,
646 snd_pcm_t ** rpcm)
647{
648 snd_pcm_t *pcm;
649 int err;
650 static snd_device_ops_t ops = {
651 .dev_free = snd_pcm_dev_free,
652 .dev_register = snd_pcm_dev_register,
653 .dev_disconnect = snd_pcm_dev_disconnect,
654 .dev_unregister = snd_pcm_dev_unregister
655 };
656
657 snd_assert(rpcm != NULL, return -EINVAL);
658 *rpcm = NULL;
659 snd_assert(card != NULL, return -ENXIO);
660 pcm = kcalloc(1, sizeof(*pcm), GFP_KERNEL);
661 if (pcm == NULL)
662 return -ENOMEM;
663 pcm->card = card;
664 pcm->device = device;
665 if (id) {
666 strlcpy(pcm->id, id, sizeof(pcm->id));
667 }
668 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
669 snd_pcm_free(pcm);
670 return err;
671 }
672 if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
673 snd_pcm_free(pcm);
674 return err;
675 }
676 init_MUTEX(&pcm->open_mutex);
677 init_waitqueue_head(&pcm->open_wait);
678 if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
679 snd_pcm_free(pcm);
680 return err;
681 }
682 *rpcm = pcm;
683 return 0;
684}
685
686static void snd_pcm_free_stream(snd_pcm_str_t * pstr)
687{
688 snd_pcm_substream_t *substream, *substream_next;
689#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
690 snd_pcm_oss_setup_t *setup, *setupn;
691#endif
692 substream = pstr->substream;
693 while (substream) {
694 substream_next = substream->next;
695 snd_pcm_substream_proc_done(substream);
696 kfree(substream);
697 substream = substream_next;
698 }
699 snd_pcm_stream_proc_done(pstr);
700#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
701 for (setup = pstr->oss.setup_list; setup; setup = setupn) {
702 setupn = setup->next;
703 kfree(setup->task_name);
704 kfree(setup);
705 }
706#endif
707}
708
709static int snd_pcm_free(snd_pcm_t *pcm)
710{
711 snd_assert(pcm != NULL, return -ENXIO);
712 if (pcm->private_free)
713 pcm->private_free(pcm);
714 snd_pcm_lib_preallocate_free_for_all(pcm);
715 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
716 snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
717 kfree(pcm);
718 return 0;
719}
720
721static int snd_pcm_dev_free(snd_device_t *device)
722{
723 snd_pcm_t *pcm = device->device_data;
724 return snd_pcm_free(pcm);
725}
726
727static void snd_pcm_tick_timer_func(unsigned long data)
728{
729 snd_pcm_substream_t *substream = (snd_pcm_substream_t*) data;
730 snd_pcm_tick_elapsed(substream);
731}
732
733int snd_pcm_open_substream(snd_pcm_t *pcm, int stream,
734 snd_pcm_substream_t **rsubstream)
735{
736 snd_pcm_str_t * pstr;
737 snd_pcm_substream_t * substream;
738 snd_pcm_runtime_t * runtime;
739 snd_ctl_file_t *kctl;
740 snd_card_t *card;
741 struct list_head *list;
742 int prefer_subdevice = -1;
743 size_t size;
744
745 snd_assert(rsubstream != NULL, return -EINVAL);
746 *rsubstream = NULL;
747 snd_assert(pcm != NULL, return -ENXIO);
748 pstr = &pcm->streams[stream];
749 if (pstr->substream == NULL)
750 return -ENODEV;
751
752 card = pcm->card;
753 down_read(&card->controls_rwsem);
754 list_for_each(list, &card->ctl_files) {
755 kctl = snd_ctl_file(list);
756 if (kctl->pid == current->pid) {
757 prefer_subdevice = kctl->prefer_pcm_subdevice;
758 break;
759 }
760 }
761 up_read(&card->controls_rwsem);
762
763 if (pstr->substream_count == 0)
764 return -ENODEV;
765 switch (stream) {
766 case SNDRV_PCM_STREAM_PLAYBACK:
767 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
768 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
769 if (SUBSTREAM_BUSY(substream))
770 return -EAGAIN;
771 }
772 }
773 break;
774 case SNDRV_PCM_STREAM_CAPTURE:
775 if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
776 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
777 if (SUBSTREAM_BUSY(substream))
778 return -EAGAIN;
779 }
780 }
781 break;
782 default:
783 return -EINVAL;
784 }
785
786 if (prefer_subdevice >= 0) {
787 for (substream = pstr->substream; substream; substream = substream->next)
788 if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
789 goto __ok;
790 }
791 for (substream = pstr->substream; substream; substream = substream->next)
792 if (!SUBSTREAM_BUSY(substream))
793 break;
794 __ok:
795 if (substream == NULL)
796 return -EAGAIN;
797
798 runtime = kcalloc(1, sizeof(*runtime), GFP_KERNEL);
799 if (runtime == NULL)
800 return -ENOMEM;
801
802 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t));
803 runtime->status = snd_malloc_pages(size, GFP_KERNEL);
804 if (runtime->status == NULL) {
805 kfree(runtime);
806 return -ENOMEM;
807 }
808 memset((void*)runtime->status, 0, size);
809
810 size = PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t));
811 runtime->control = snd_malloc_pages(size, GFP_KERNEL);
812 if (runtime->control == NULL) {
813 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
814 kfree(runtime);
815 return -ENOMEM;
816 }
817 memset((void*)runtime->control, 0, size);
818
819 init_waitqueue_head(&runtime->sleep);
820 atomic_set(&runtime->mmap_count, 0);
821 init_timer(&runtime->tick_timer);
822 runtime->tick_timer.function = snd_pcm_tick_timer_func;
823 runtime->tick_timer.data = (unsigned long) substream;
824
825 runtime->status->state = SNDRV_PCM_STATE_OPEN;
826
827 substream->runtime = runtime;
828 substream->private_data = pcm->private_data;
829 pstr->substream_opened++;
830 *rsubstream = substream;
831 return 0;
832}
833
834void snd_pcm_release_substream(snd_pcm_substream_t *substream)
835{
836 snd_pcm_runtime_t * runtime;
837 substream->file = NULL;
838 runtime = substream->runtime;
839 snd_assert(runtime != NULL, return);
840 if (runtime->private_free != NULL)
841 runtime->private_free(runtime);
842 snd_free_pages((void*)runtime->status, PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)));
843 snd_free_pages((void*)runtime->control, PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)));
844 kfree(runtime->hw_constraints.rules);
845 kfree(runtime);
846 substream->runtime = NULL;
847 substream->pstr->substream_opened--;
848}
849
850static int snd_pcm_dev_register(snd_device_t *device)
851{
852 int idx, cidx, err;
853 unsigned short minor;
854 snd_pcm_substream_t *substream;
855 struct list_head *list;
856 char str[16];
857 snd_pcm_t *pcm = device->device_data;
858
859 snd_assert(pcm != NULL && device != NULL, return -ENXIO);
860 down(&register_mutex);
861 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
862 if (snd_pcm_devices[idx]) {
863 up(&register_mutex);
864 return -EBUSY;
865 }
866 snd_pcm_devices[idx] = pcm;
867 for (cidx = 0; cidx < 2; cidx++) {
868 int devtype = -1;
869 if (pcm->streams[cidx].substream == NULL)
870 continue;
871 switch (cidx) {
872 case SNDRV_PCM_STREAM_PLAYBACK:
873 sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
874 minor = SNDRV_MINOR_PCM_PLAYBACK + idx;
875 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
876 break;
877 case SNDRV_PCM_STREAM_CAPTURE:
878 sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
879 minor = SNDRV_MINOR_PCM_CAPTURE + idx;
880 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
881 break;
882 }
883 if ((err = snd_register_device(devtype, pcm->card, pcm->device, pcm->streams[cidx].reg, str)) < 0) {
884 snd_pcm_devices[idx] = NULL;
885 up(&register_mutex);
886 return err;
887 }
888 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
889 snd_pcm_timer_init(substream);
890 }
891 list_for_each(list, &snd_pcm_notify_list) {
892 snd_pcm_notify_t *notify;
893 notify = list_entry(list, snd_pcm_notify_t, list);
894 notify->n_register(pcm);
895 }
896 up(&register_mutex);
897 return 0;
898}
899
900static int snd_pcm_dev_disconnect(snd_device_t *device)
901{
902 snd_pcm_t *pcm = device->device_data;
903 struct list_head *list;
904 snd_pcm_substream_t *substream;
905 int idx, cidx;
906
907 down(&register_mutex);
908 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
909 snd_pcm_devices[idx] = NULL;
910 for (cidx = 0; cidx < 2; cidx++)
911 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
912 if (substream->runtime)
913 substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
914 list_for_each(list, &snd_pcm_notify_list) {
915 snd_pcm_notify_t *notify;
916 notify = list_entry(list, snd_pcm_notify_t, list);
917 notify->n_disconnect(pcm);
918 }
919 up(&register_mutex);
920 return 0;
921}
922
923static int snd_pcm_dev_unregister(snd_device_t *device)
924{
925 int idx, cidx, devtype;
926 snd_pcm_substream_t *substream;
927 struct list_head *list;
928 snd_pcm_t *pcm = device->device_data;
929
930 snd_assert(pcm != NULL, return -ENXIO);
931 down(&register_mutex);
932 idx = (pcm->card->number * SNDRV_PCM_DEVICES) + pcm->device;
933 snd_pcm_devices[idx] = NULL;
934 for (cidx = 0; cidx < 2; cidx++) {
935 devtype = -1;
936 switch (cidx) {
937 case SNDRV_PCM_STREAM_PLAYBACK:
938 devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
939 break;
940 case SNDRV_PCM_STREAM_CAPTURE:
941 devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
942 break;
943 }
944 snd_unregister_device(devtype, pcm->card, pcm->device);
945 for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
946 snd_pcm_timer_done(substream);
947 }
948 list_for_each(list, &snd_pcm_notify_list) {
949 snd_pcm_notify_t *notify;
950 notify = list_entry(list, snd_pcm_notify_t, list);
951 notify->n_unregister(pcm);
952 }
953 up(&register_mutex);
954 return snd_pcm_free(pcm);
955}
956
957int snd_pcm_notify(snd_pcm_notify_t *notify, int nfree)
958{
959 int idx;
960
961 snd_assert(notify != NULL && notify->n_register != NULL && notify->n_unregister != NULL, return -EINVAL);
962 down(&register_mutex);
963 if (nfree) {
964 list_del(&notify->list);
965 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
966 if (snd_pcm_devices[idx] == NULL)
967 continue;
968 notify->n_unregister(snd_pcm_devices[idx]);
969 }
970 } else {
971 list_add_tail(&notify->list, &snd_pcm_notify_list);
972 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
973 if (snd_pcm_devices[idx] == NULL)
974 continue;
975 notify->n_register(snd_pcm_devices[idx]);
976 }
977 }
978 up(&register_mutex);
979 return 0;
980}
981
982/*
983 * Info interface
984 */
985
986static void snd_pcm_proc_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
987{
988 int idx;
989 snd_pcm_t *pcm;
990
991 down(&register_mutex);
992 for (idx = 0; idx < SNDRV_CARDS * SNDRV_PCM_DEVICES; idx++) {
993 pcm = snd_pcm_devices[idx];
994 if (pcm == NULL)
995 continue;
996 snd_iprintf(buffer, "%02i-%02i: %s : %s", idx / SNDRV_PCM_DEVICES,
997 idx % SNDRV_PCM_DEVICES, pcm->id, pcm->name);
998 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
999 snd_iprintf(buffer, " : playback %i", pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1000 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1001 snd_iprintf(buffer, " : capture %i", pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1002 snd_iprintf(buffer, "\n");
1003 }
1004 up(&register_mutex);
1005}
1006
1007/*
1008 * ENTRY functions
1009 */
1010
1011static snd_info_entry_t *snd_pcm_proc_entry = NULL;
1012
1013static int __init alsa_pcm_init(void)
1014{
1015 snd_info_entry_t *entry;
1016
1017 snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1018 snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1019 if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1020 snd_info_set_text_ops(entry, NULL, SNDRV_CARDS * SNDRV_PCM_DEVICES * 128, snd_pcm_proc_read);
1021 if (snd_info_register(entry) < 0) {
1022 snd_info_free_entry(entry);
1023 entry = NULL;
1024 }
1025 }
1026 snd_pcm_proc_entry = entry;
1027 return 0;
1028}
1029
1030static void __exit alsa_pcm_exit(void)
1031{
1032 snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1033 snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1034 if (snd_pcm_proc_entry) {
1035 snd_info_unregister(snd_pcm_proc_entry);
1036 snd_pcm_proc_entry = NULL;
1037 }
1038}
1039
1040module_init(alsa_pcm_init)
1041module_exit(alsa_pcm_exit)
1042
1043EXPORT_SYMBOL(snd_pcm_devices);
1044EXPORT_SYMBOL(snd_pcm_new);
1045EXPORT_SYMBOL(snd_pcm_new_stream);
1046EXPORT_SYMBOL(snd_pcm_notify);
1047EXPORT_SYMBOL(snd_pcm_open_substream);
1048EXPORT_SYMBOL(snd_pcm_release_substream);
1049EXPORT_SYMBOL(snd_pcm_format_name);
1050 /* pcm_native.c */
1051EXPORT_SYMBOL(snd_pcm_link_rwlock);
1052EXPORT_SYMBOL(snd_pcm_start);
1053#ifdef CONFIG_PM
1054EXPORT_SYMBOL(snd_pcm_suspend);
1055EXPORT_SYMBOL(snd_pcm_suspend_all);
1056#endif
1057EXPORT_SYMBOL(snd_pcm_kernel_playback_ioctl);
1058EXPORT_SYMBOL(snd_pcm_kernel_capture_ioctl);
1059EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
1060EXPORT_SYMBOL(snd_pcm_mmap_data);
1061#if SNDRV_PCM_INFO_MMAP_IOMEM
1062EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
1063#endif
1064 /* pcm_misc.c */
1065EXPORT_SYMBOL(snd_pcm_format_signed);
1066EXPORT_SYMBOL(snd_pcm_format_unsigned);
1067EXPORT_SYMBOL(snd_pcm_format_linear);
1068EXPORT_SYMBOL(snd_pcm_format_little_endian);
1069EXPORT_SYMBOL(snd_pcm_format_big_endian);
1070EXPORT_SYMBOL(snd_pcm_format_width);
1071EXPORT_SYMBOL(snd_pcm_format_physical_width);
Takashi Iwai9502dca2005-05-18 16:25:46 +02001072EXPORT_SYMBOL(snd_pcm_format_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073EXPORT_SYMBOL(snd_pcm_format_silence_64);
1074EXPORT_SYMBOL(snd_pcm_format_set_silence);
1075EXPORT_SYMBOL(snd_pcm_build_linear_format);
1076EXPORT_SYMBOL(snd_pcm_limit_hw_rates);