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