OSDN Git Service

Remove channels_mask. Implemented pcm_null
[android-x86/external-alsa-lib.git] / src / pcm / pcm_hw.c
1 /*
2  *  PCM - Hardware
3  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 of
9  *   the License, or (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 Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21   
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/mman.h>
31 #include <sys/shm.h>
32 #include "pcm_local.h"
33
34 #ifndef F_SETSIG
35 #define F_SETSIG 10
36 #endif
37
38 typedef struct {
39         int fd;
40         int card, device, subdevice;
41         volatile snd_pcm_mmap_status_t *mmap_status;
42         snd_pcm_mmap_control_t *mmap_control;
43 } snd_pcm_hw_t;
44
45 #define SND_FILE_PCM_STREAM_PLAYBACK            "/dev/snd/pcmC%iD%ip"
46 #define SND_FILE_PCM_STREAM_CAPTURE             "/dev/snd/pcmC%iD%ic"
47 #define SND_PCM_VERSION_MAX     SND_PROTOCOL_VERSION(2, 0, 0)
48
49 static int snd_pcm_hw_nonblock(snd_pcm_t *pcm, int nonblock)
50 {
51         long flags;
52         snd_pcm_hw_t *hw = pcm->private;
53         int fd = hw->fd;
54
55         if ((flags = fcntl(fd, F_GETFL)) < 0) {
56                 SYSERR("F_GETFL failed");
57                 return -errno;
58         }
59         if (nonblock)
60                 flags |= O_NONBLOCK;
61         else
62                 flags &= ~O_NONBLOCK;
63         if (fcntl(fd, F_SETFL, flags) < 0) {
64                 SYSERR("F_SETFL for O_NONBLOCK failed");
65                 return -errno;
66         }
67         return 0;
68 }
69
70 static int snd_pcm_hw_async(snd_pcm_t *pcm, int sig, pid_t pid)
71 {
72         long flags;
73         snd_pcm_hw_t *hw = pcm->private;
74         int fd = hw->fd;
75
76         if ((flags = fcntl(fd, F_GETFL)) < 0) {
77                 SYSERR("F_GETFL failed");
78                 return -errno;
79         }
80         if (sig >= 0)
81                 flags |= O_ASYNC;
82         else
83                 flags &= ~O_ASYNC;
84         if (fcntl(fd, F_SETFL, flags) < 0) {
85                 SYSERR("F_SETFL for O_ASYNC failed");
86                 return -errno;
87         }
88         if (sig < 0)
89                 return 0;
90         if (sig == 0)
91                 sig = SIGIO;
92         if (fcntl(fd, F_SETSIG, sig) < 0) {
93                 SYSERR("F_SETSIG failed");
94                 return -errno;
95         }
96         if (pid == 0)
97                 pid = getpid();
98         if (fcntl(fd, F_SETOWN, pid) < 0) {
99                 SYSERR("F_SETOWN failed");
100                 return -errno;
101         }
102         return 0;
103 }
104
105 static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
106 {
107         snd_pcm_hw_t *hw = pcm->private;
108         int fd = hw->fd;
109         if (ioctl(fd, SND_PCM_IOCTL_INFO, info) < 0) {
110                 SYSERR("SND_PCM_IOCTL_INFO failed");
111                 return -errno;
112         }
113         return 0;
114 }
115
116 static int snd_pcm_hw_params_info(snd_pcm_t *pcm, snd_pcm_params_info_t * info)
117 {
118         snd_pcm_hw_t *hw = pcm->private;
119         int fd = hw->fd;
120         if (ioctl(fd, SND_PCM_IOCTL_PARAMS_INFO, info) < 0) {
121                 SYSERR("SND_PCM_IOCTL_PARAMS_INFO failed");
122                 return -errno;
123         }
124         return 0;
125 }
126
127 static int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_params_t * params)
128 {
129         snd_pcm_hw_t *hw = pcm->private;
130         int fd = hw->fd;
131         if (ioctl(fd, SND_PCM_IOCTL_PARAMS, params) < 0) {
132                 SYSERR("SND_PCM_IOCTL_PARAMS failed");
133                 return -errno;
134         }
135         return 0;
136 }
137
138 static int snd_pcm_hw_setup(snd_pcm_t *pcm, snd_pcm_setup_t * setup)
139 {
140         snd_pcm_hw_t *hw = pcm->private;
141         int fd = hw->fd;
142         if (ioctl(fd, SND_PCM_IOCTL_SETUP, setup) < 0) {
143                 SYSERR("SND_PCM_IOCTL_SETUP failed");
144                 return -errno;
145         }
146         if (setup->mmap_shape == SND_PCM_MMAP_UNSPECIFIED) {
147                 if (setup->xfer_mode == SND_PCM_XFER_INTERLEAVED)
148                         setup->mmap_shape = SND_PCM_MMAP_INTERLEAVED;
149                 else
150                         setup->mmap_shape = SND_PCM_MMAP_NONINTERLEAVED;
151         }
152         return 0;
153 }
154
155 static int snd_pcm_hw_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
156 {
157         snd_pcm_hw_t *hw = pcm->private;
158         int fd = hw->fd;
159         if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_INFO, info) < 0) {
160                 SYSERR("SND_PCM_IOCTL_CHANNEL_INFO failed");
161                 return -errno;
162         }
163         return 0;
164 }
165
166 static int snd_pcm_hw_channel_params(snd_pcm_t *pcm, snd_pcm_channel_params_t * params)
167 {
168         snd_pcm_hw_t *hw = pcm->private;
169         int fd = hw->fd;
170         if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_PARAMS, params) < 0) {
171                 SYSERR("SND_PCM_IOCTL_CHANNEL_PARAMS failed");
172                 return -errno;
173         }
174         return 0;
175 }
176
177 static int snd_pcm_hw_channel_setup(snd_pcm_t *pcm, snd_pcm_channel_setup_t * setup)
178 {
179         snd_pcm_hw_t *hw = pcm->private;
180         int fd = hw->fd;
181         if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0) {
182                 SYSERR("SND_PCM_IOCTL_CHANNEL_SETUP failed");
183                 return -errno;
184         }
185         if (!pcm->mmap_info)
186                 return 0;
187         if (pcm->setup.mmap_shape == SND_PCM_MMAP_UNSPECIFIED) {
188                 if (pcm->setup.xfer_mode == SND_PCM_XFER_INTERLEAVED) {
189                         setup->running_area.addr = pcm->mmap_info->addr;
190                         setup->running_area.first = setup->channel * pcm->bits_per_sample;
191                         setup->running_area.step = pcm->bits_per_frame;
192                 } else {
193                         setup->running_area.addr = pcm->mmap_info->addr + setup->channel * pcm->setup.buffer_size * pcm->bits_per_sample / 8;
194                         setup->running_area.first = 0;
195                         setup->running_area.step = pcm->bits_per_sample;
196                 }
197                 setup->stopped_area = setup->running_area;
198         } else {
199                 setup->running_area.addr = pcm->mmap_info->addr + (long)setup->running_area.addr;
200                 setup->stopped_area.addr = setup->running_area.addr;
201         }
202         return 0;
203 }
204
205 static int snd_pcm_hw_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
206 {
207         snd_pcm_hw_t *hw = pcm->private;
208         int fd = hw->fd;
209         if (ioctl(fd, SND_PCM_IOCTL_STATUS, status) < 0) {
210                 SYSERR("SND_PCM_IOCTL_STATUS failed");
211                 return -errno;
212         }
213         return 0;
214 }
215
216 static int snd_pcm_hw_state(snd_pcm_t *pcm)
217 {
218         snd_pcm_hw_t *hw = pcm->private;
219         return hw->mmap_status->state;
220 }
221
222 static int snd_pcm_hw_delay(snd_pcm_t *pcm, ssize_t *delayp)
223 {
224         snd_pcm_hw_t *hw = pcm->private;
225         int fd = hw->fd;
226         if (ioctl(fd, SND_PCM_IOCTL_DELAY, delayp) < 0) {
227                 SYSERR("SND_PCM_IOCTL_DELAY failed");
228                 return -errno;
229         }
230         return 0;
231 }
232
233 static int snd_pcm_hw_prepare(snd_pcm_t *pcm)
234 {
235         snd_pcm_hw_t *hw = pcm->private;
236         int fd = hw->fd;
237         if (ioctl(fd, SND_PCM_IOCTL_PREPARE) < 0) {
238                 SYSERR("SND_PCM_IOCTL_PREPARE failed");
239                 return -errno;
240         }
241         return 0;
242 }
243
244 static int snd_pcm_hw_start(snd_pcm_t *pcm)
245 {
246         snd_pcm_hw_t *hw = pcm->private;
247         int fd = hw->fd;
248         if (ioctl(fd, SND_PCM_IOCTL_START) < 0) {
249                 SYSERR("SND_PCM_IOCTL_START failed");
250                 return -errno;
251         }
252         return 0;
253 }
254
255 static int snd_pcm_hw_drop(snd_pcm_t *pcm)
256 {
257         snd_pcm_hw_t *hw = pcm->private;
258         int fd = hw->fd;
259         if (ioctl(fd, SND_PCM_IOCTL_DROP) < 0) {
260                 SYSERR("SND_PCM_IOCTL_DROP failed");
261                 return -errno;
262         }
263         return 0;
264 }
265
266 static int snd_pcm_hw_drain(snd_pcm_t *pcm)
267 {
268         snd_pcm_hw_t *hw = pcm->private;
269         int fd = hw->fd;
270         if (ioctl(fd, SND_PCM_IOCTL_DRAIN) < 0) {
271                 SYSERR("SND_PCM_IOCTL_DRAIN failed");
272                 return -errno;
273         }
274         return 0;
275 }
276
277 static int snd_pcm_hw_pause(snd_pcm_t *pcm, int enable)
278 {
279         snd_pcm_hw_t *hw = pcm->private;
280         int fd = hw->fd;
281         if (ioctl(fd, SND_PCM_IOCTL_PAUSE, enable) < 0) {
282                 SYSERR("SND_PCM_IOCTL_PAUSE failed");
283                 return -errno;
284         }
285         return 0;
286 }
287
288 static ssize_t snd_pcm_hw_rewind(snd_pcm_t *pcm, size_t frames)
289 {
290         ssize_t hw_avail;
291         if (pcm->setup.xrun_mode == SND_PCM_XRUN_ASAP) {
292                 ssize_t d;
293                 int err = snd_pcm_hw_delay(pcm, &d);
294                 if (err < 0)
295                         return 0;
296         }
297         hw_avail = snd_pcm_mmap_hw_avail(pcm);
298         if (hw_avail <= 0)
299                 return 0;
300         if (frames > (size_t)hw_avail)
301                 frames = hw_avail;
302         snd_pcm_mmap_appl_backward(pcm, frames);
303         return frames;
304 }
305
306 static ssize_t snd_pcm_hw_writei(snd_pcm_t *pcm, const void *buffer, size_t size)
307 {
308         ssize_t result;
309         snd_pcm_hw_t *hw = pcm->private;
310         int fd = hw->fd;
311         snd_xferi_t xferi;
312         xferi.buf = (char*) buffer;
313         xferi.frames = size;
314         result = ioctl(fd, SND_PCM_IOCTL_WRITEI_FRAMES, &xferi);
315         if (result < 0)
316                 return -errno;
317         return xferi.result;
318 }
319
320 static ssize_t snd_pcm_hw_writen(snd_pcm_t *pcm, void **bufs, size_t size)
321 {
322         ssize_t result;
323         snd_pcm_hw_t *hw = pcm->private;
324         int fd = hw->fd;
325         snd_xfern_t xfern;
326         xfern.bufs = bufs;
327         xfern.frames = size;
328         result = ioctl(fd, SND_PCM_IOCTL_WRITEN_FRAMES, &xfern);
329         if (result < 0)
330                 return -errno;
331         return xfern.result;
332 }
333
334 static ssize_t snd_pcm_hw_readi(snd_pcm_t *pcm, void *buffer, size_t size)
335 {
336         ssize_t result;
337         snd_pcm_hw_t *hw = pcm->private;
338         int fd = hw->fd;
339         snd_xferi_t xferi;
340         xferi.buf = buffer;
341         xferi.frames = size;
342         result = ioctl(fd, SND_PCM_IOCTL_READI_FRAMES, &xferi);
343         if (result < 0)
344                 return -errno;
345         return xferi.result;
346 }
347
348 ssize_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, size_t size)
349 {
350         ssize_t result;
351         snd_pcm_hw_t *hw = pcm->private;
352         int fd = hw->fd;
353         snd_xfern_t xfern;
354         xfern.bufs = bufs;
355         xfern.frames = size;
356         result = ioctl(fd, SND_PCM_IOCTL_READN_FRAMES, &xfern);
357         if (result < 0)
358                 return -errno;
359         return xfern.result;
360 }
361
362 static int snd_pcm_hw_mmap_status(snd_pcm_t *pcm)
363 {
364         snd_pcm_hw_t *hw = pcm->private;
365         void *ptr;
366         ptr = mmap(NULL, sizeof(snd_pcm_mmap_status_t), PROT_READ, MAP_FILE|MAP_SHARED, 
367                    hw->fd, SND_PCM_MMAP_OFFSET_STATUS);
368         if (ptr == MAP_FAILED || ptr == NULL) {
369                 SYSERR("status mmap failed");
370                 return -errno;
371         }
372         hw->mmap_status = ptr;
373         pcm->hw_ptr = &hw->mmap_status->hw_ptr;
374         return 0;
375 }
376
377 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
378 {
379         snd_pcm_hw_t *hw = pcm->private;
380         void *ptr;
381         ptr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, 
382                    hw->fd, SND_PCM_MMAP_OFFSET_CONTROL);
383         if (ptr == MAP_FAILED || ptr == NULL) {
384                 SYSERR("control mmap failed");
385                 return -errno;
386         }
387         hw->mmap_control = ptr;
388         pcm->appl_ptr = &hw->mmap_control->appl_ptr;
389         return 0;
390 }
391
392 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
393 {
394         snd_pcm_hw_t *hw = pcm->private;
395         snd_pcm_mmap_info_t *i = calloc(1, sizeof(*i));
396         int err;
397         if (!i)
398                 return -ENOMEM;
399         if (pcm->setup.mmap_shape == SND_PCM_MMAP_UNSPECIFIED) {
400                 err = snd_pcm_alloc_user_mmap(pcm, i);
401                 if (err < 0) {
402                         free(i);
403                         return err;
404                 }
405         } else {
406                 err = snd_pcm_alloc_kernel_mmap(pcm, i, hw->fd);
407                 if (err < 0) {
408                         free(i);
409                         return err;
410                 }
411         }
412         pcm->mmap_info = i;
413         pcm->mmap_info_count = 1;
414         return 0;
415 }
416
417 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
418 {
419         snd_pcm_hw_t *hw = pcm->private;
420         if (munmap((void*)hw->mmap_status, sizeof(*hw->mmap_status)) < 0) {
421                 SYSERR("status munmap failed");
422                 return -errno;
423         }
424         return 0;
425 }
426
427 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
428 {
429         snd_pcm_hw_t *hw = pcm->private;
430         if (munmap(hw->mmap_control, sizeof(*hw->mmap_control)) < 0) {
431                 SYSERR("control munmap failed");
432                 return -errno;
433         }
434         return 0;
435 }
436
437 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
438 {
439         int err = snd_pcm_free_mmap(pcm, pcm->mmap_info);
440         if (err < 0)
441                 return err;
442         pcm->mmap_info_count = 0;
443         free(pcm->mmap_info);
444         pcm->mmap_info = 0;
445         return 0;
446 }
447
448 static int snd_pcm_hw_close(snd_pcm_t *pcm)
449 {
450         snd_pcm_hw_t *hw = pcm->private;
451         int fd = hw->fd;
452         free(hw);
453         if (close(fd)) {
454                 SYSERR("close failed\n");
455                 return -errno;
456         }
457         snd_pcm_hw_munmap_status(pcm);
458         snd_pcm_hw_munmap_control(pcm);
459         return 0;
460 }
461
462 static ssize_t snd_pcm_hw_mmap_forward(snd_pcm_t *pcm, size_t size)
463 {
464         if (pcm->setup.mmap_shape == SND_PCM_MMAP_UNSPECIFIED && pcm->stream == SND_PCM_STREAM_PLAYBACK)
465                 return snd_pcm_write_mmap(pcm, size);
466         snd_pcm_mmap_appl_forward(pcm, size);
467         return size;
468 }
469
470 static ssize_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
471 {
472         size_t avail;
473         ssize_t err;
474         if (pcm->setup.ready_mode == SND_PCM_READY_ASAP ||
475             pcm->setup.xrun_mode == SND_PCM_XRUN_ASAP) {
476                 ssize_t d;
477                 int err = snd_pcm_hw_delay(pcm, &d);
478                 if (err < 0)
479                         return err;
480         }
481         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
482                 avail = snd_pcm_mmap_playback_avail(pcm);
483         } else {
484                 avail = snd_pcm_mmap_capture_avail(pcm);
485                 if (avail > 0 && pcm->setup.mmap_shape == SND_PCM_MMAP_UNSPECIFIED) {
486                         err = snd_pcm_read_mmap(pcm, avail);
487                         if (err < 0)
488                                 return err;
489                         assert((size_t)err == avail);
490                         return err;
491                 }
492         }
493         if (avail > pcm->setup.buffer_size)
494                 return -EPIPE;
495         return avail;
496 }
497
498 static int snd_pcm_hw_set_avail_min(snd_pcm_t *pcm, size_t frames)
499 {
500         snd_pcm_hw_t *hw = pcm->private;
501         hw->mmap_control->avail_min = frames;
502         return 0;
503 }
504
505 static void snd_pcm_hw_dump(snd_pcm_t *pcm, FILE *fp)
506 {
507         snd_pcm_hw_t *hw = pcm->private;
508         char *name = "Unknown";
509         snd_card_get_name(hw->card, &name);
510         fprintf(fp, "Hardware PCM card %d '%s' device %d subdevice %d\n",
511                 hw->card, name, hw->device, hw->subdevice);
512         free(name);
513         if (pcm->valid_setup) {
514                 fprintf(fp, "\nIts setup is:\n");
515                 snd_pcm_dump_setup(pcm, fp);
516         }
517 }
518
519 snd_pcm_ops_t snd_pcm_hw_ops = {
520         close: snd_pcm_hw_close,
521         info: snd_pcm_hw_info,
522         params_info: snd_pcm_hw_params_info,
523         params: snd_pcm_hw_params,
524         setup: snd_pcm_hw_setup,
525         channel_info: snd_pcm_hw_channel_info,
526         channel_params: snd_pcm_hw_channel_params,
527         channel_setup: snd_pcm_hw_channel_setup,
528         dump: snd_pcm_hw_dump,
529         nonblock: snd_pcm_hw_nonblock,
530         async: snd_pcm_hw_async,
531         mmap: snd_pcm_hw_mmap,
532         munmap: snd_pcm_hw_munmap,
533 };
534
535 snd_pcm_fast_ops_t snd_pcm_hw_fast_ops = {
536         status: snd_pcm_hw_status,
537         state: snd_pcm_hw_state,
538         delay: snd_pcm_hw_delay,
539         prepare: snd_pcm_hw_prepare,
540         start: snd_pcm_hw_start,
541         drop: snd_pcm_hw_drop,
542         drain: snd_pcm_hw_drain,
543         pause: snd_pcm_hw_pause,
544         rewind: snd_pcm_hw_rewind,
545         writei: snd_pcm_hw_writei,
546         writen: snd_pcm_hw_writen,
547         readi: snd_pcm_hw_readi,
548         readn: snd_pcm_hw_readn,
549         avail_update: snd_pcm_hw_avail_update,
550         mmap_forward: snd_pcm_hw_mmap_forward,
551         set_avail_min: snd_pcm_hw_set_avail_min,
552 };
553
554 int snd_pcm_hw_open_subdevice(snd_pcm_t **pcmp, int card, int device, int subdevice, int stream, int mode)
555 {
556         char filename[32];
557         char *filefmt;
558         int ver;
559         int ret = 0, fd = -1;
560         int attempt = 0;
561         snd_pcm_info_t info;
562         int fmode;
563         snd_ctl_t *ctl;
564         snd_pcm_t *pcm = NULL;
565         snd_pcm_hw_t *hw = NULL;
566
567         assert(pcmp);
568
569         if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
570                 return ret;
571
572         switch (stream) {
573         case SND_PCM_STREAM_PLAYBACK:
574                 filefmt = SND_FILE_PCM_STREAM_PLAYBACK;
575                 break;
576         case SND_PCM_STREAM_CAPTURE:
577                 filefmt = SND_FILE_PCM_STREAM_CAPTURE;
578                 break;
579         default:
580                 assert(0);
581         }
582         ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
583         snd_ctl_close(ctl);
584         if (ret < 0)
585                 return ret;
586         sprintf(filename, filefmt, card, device);
587
588       __again:
589         if (attempt++ > 3)
590                 return -EBUSY;
591         fmode = O_RDWR;
592         if (mode & SND_PCM_NONBLOCK)
593                 fmode |= O_NONBLOCK;
594         if (mode & SND_PCM_ASYNC)
595                 fmode |= O_ASYNC;
596         if ((fd = open(filename, fmode)) < 0) {
597                 SYSERR("open %s failed", filename);
598                 return -errno;
599         }
600         if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
601                 SYSERR("SND_PCM_IOCTL_PVERSION failed");
602                 ret = -errno;
603                 goto _err;
604         }
605         if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
606                 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
607                 goto _err;
608         }
609         if (subdevice >= 0) {
610                 memset(&info, 0, sizeof(info));
611                 if (ioctl(fd, SND_PCM_IOCTL_INFO, &info) < 0) {
612                         SYSERR("SND_PCM_IOCTL_INFO failed");
613                         ret = -errno;
614                         goto _err;
615                 }
616                 if (info.subdevice != subdevice) {
617                         close(fd);
618                         goto __again;
619                 }
620         }
621         hw = calloc(1, sizeof(snd_pcm_hw_t));
622         if (!hw) {
623                 ret = -ENOMEM;
624                 goto _err;
625         }
626         hw->card = card;
627         hw->device = device;
628         hw->subdevice = subdevice;
629         hw->fd = fd;
630
631         pcm = calloc(1, sizeof(snd_pcm_t));
632         if (!pcm) {
633                 ret = -ENOMEM;
634                 goto _err;
635         }
636         pcm->type = SND_PCM_TYPE_HW;
637         pcm->stream = stream;
638         pcm->mode = mode;
639         pcm->ops = &snd_pcm_hw_ops;
640         pcm->op_arg = pcm;
641         pcm->fast_ops = &snd_pcm_hw_fast_ops;
642         pcm->fast_op_arg = pcm;
643         pcm->private = hw;
644         pcm->poll_fd = fd;
645         *pcmp = pcm;
646         ret = snd_pcm_hw_mmap_status(pcm);
647         if (ret < 0) {
648                 snd_pcm_close(pcm);
649                 return ret;
650         }
651         ret = snd_pcm_hw_mmap_control(pcm);
652         if (ret < 0) {
653                 snd_pcm_close(pcm);
654                 return ret;
655         }
656         return 0;
657         
658  _err:
659         if (hw)
660                 free(hw);
661         if (pcm)
662                 free(pcm);
663         close(fd);
664         return ret;
665 }
666
667 int snd_pcm_hw_open_device(snd_pcm_t **pcmp, int card, int device, int stream, int mode)
668 {
669         return snd_pcm_hw_open_subdevice(pcmp, card, device, -1, stream, mode);
670 }
671
672 int snd_pcm_hw_open(snd_pcm_t **pcmp, char *name, int card, int device, int subdevice, int stream, int mode)
673 {
674         int err = snd_pcm_hw_open_subdevice(pcmp, card, device, subdevice, stream, mode);
675         if (err < 0)
676                 return err;
677         if (name)
678                 (*pcmp)->name = strdup(name);
679         return 0;
680 }
681
682 int _snd_pcm_hw_open(snd_pcm_t **pcmp, char *name, snd_config_t *conf,
683                      int stream, int mode)
684 {
685         snd_config_iterator_t i;
686         long card = -1, device = 0, subdevice = -1;
687         char *str;
688         int err;
689         snd_config_foreach(i, conf) {
690                 snd_config_t *n = snd_config_entry(i);
691                 if (strcmp(n->id, "comment") == 0)
692                         continue;
693                 if (strcmp(n->id, "type") == 0)
694                         continue;
695                 if (strcmp(n->id, "stream") == 0)
696                         continue;
697                 if (strcmp(n->id, "card") == 0) {
698                         err = snd_config_integer_get(n, &card);
699                         if (err < 0) {
700                                 err = snd_config_string_get(n, &str);
701                                 if (err < 0)
702                                         return -EINVAL;
703                                 card = snd_card_get_index(str);
704                                 if (card < 0)
705                                         return card;
706                         }
707                         continue;
708                 }
709                 if (strcmp(n->id, "device") == 0) {
710                         err = snd_config_integer_get(n, &device);
711                         if (err < 0)
712                                 return err;
713                         continue;
714                 }
715                 if (strcmp(n->id, "subdevice") == 0) {
716                         err = snd_config_integer_get(n, &subdevice);
717                         if (err < 0)
718                                 return err;
719                         continue;
720                 }
721                 return -EINVAL;
722         }
723         if (card < 0)
724                 return -EINVAL;
725         return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode);
726 }
727