OSDN Git Service

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