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