OSDN Git Service

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