OSDN Git Service

Renamed ERR to SNDERR. Added s16 pseudo meter scope. Fixed plug hw_refine/params
[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_start_t) params->start_mode == pcm->start_mode &&
155             (snd_pcm_xrun_t) params->xrun_mode == pcm->xrun_mode &&
156             (snd_pcm_tstamp_t) params->tstamp_mode == pcm->tstamp_mode &&
157             params->period_step == pcm->period_step &&
158             params->sleep_min == pcm->sleep_min &&
159             params->xfer_align == pcm->xfer_align &&
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 0;
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 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)), PROT_READ, MAP_FILE|MAP_SHARED, 
365                    hw->fd, SND_PCM_MMAP_OFFSET_STATUS);
366         if (ptr == MAP_FAILED || ptr == NULL) {
367                 SYSERR("status mmap failed");
368                 return -errno;
369         }
370         hw->mmap_status = ptr;
371         pcm->hw_ptr = &hw->mmap_status->hw_ptr;
372         return 0;
373 }
374
375 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
376 {
377         snd_pcm_hw_t *hw = pcm->private_data;
378         void *ptr;
379         ptr = mmap(NULL, page_align(sizeof(struct sndrv_pcm_mmap_control)), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, 
380                    hw->fd, SND_PCM_MMAP_OFFSET_CONTROL);
381         if (ptr == MAP_FAILED || ptr == NULL) {
382                 SYSERR("control mmap failed");
383                 return -errno;
384         }
385         hw->mmap_control = ptr;
386         pcm->appl_ptr = &hw->mmap_control->appl_ptr;
387         return 0;
388 }
389
390 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
391 {
392         snd_pcm_hw_t *hw = pcm->private_data;
393         if (munmap((void*)hw->mmap_status, page_align(sizeof(*hw->mmap_status))) < 0) {
394                 SYSERR("status munmap failed");
395                 return -errno;
396         }
397         return 0;
398 }
399
400 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
401 {
402         snd_pcm_hw_t *hw = pcm->private_data;
403         if (munmap(hw->mmap_control, page_align(sizeof(*hw->mmap_control))) < 0) {
404                 SYSERR("control munmap failed");
405                 return -errno;
406         }
407         return 0;
408 }
409
410 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
411 {
412         snd_pcm_hw_t *hw = pcm->private_data;
413         if (!(pcm->info & SND_PCM_INFO_MMAP)) {
414                 snd_pcm_uframes_t size = snd_pcm_frames_to_bytes(pcm, pcm->buffer_size);
415                 int id = shmget(IPC_PRIVATE, size, 0666);
416                 if (id < 0) {
417                         SYSERR("shmget failed");
418                         return -errno;
419                 }
420                 hw->shmid = id;
421         }
422         return 0;
423 }
424
425 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
426 {
427         snd_pcm_hw_t *hw = pcm->private_data;
428         if (!(pcm->info & SND_PCM_INFO_MMAP)) {
429                 if (shmctl(hw->shmid, IPC_RMID, 0) < 0) {
430                         SYSERR("shmctl IPC_RMID failed");
431                         return -errno;
432                 }
433         }
434         return 0;
435 }
436
437 static int snd_pcm_hw_close(snd_pcm_t *pcm)
438 {
439         snd_pcm_hw_t *hw = pcm->private_data;
440         if (close(hw->fd)) {
441                 SYSERR("close failed\n");
442                 return -errno;
443         }
444         snd_pcm_hw_munmap_status(pcm);
445         snd_pcm_hw_munmap_control(pcm);
446         free(hw);
447         return 0;
448 }
449
450 static snd_pcm_sframes_t snd_pcm_hw_mmap_forward(snd_pcm_t *pcm, snd_pcm_uframes_t size)
451 {
452         if (!(pcm->info & SND_PCM_INFO_MMAP) && 
453             pcm->stream == SND_PCM_STREAM_PLAYBACK)
454                 return snd_pcm_write_mmap(pcm, size);
455         snd_pcm_mmap_appl_forward(pcm, size);
456         return size;
457 }
458
459 static snd_pcm_sframes_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
460 {
461         snd_pcm_uframes_t avail;
462         snd_pcm_sframes_t err;
463 #if 0
464         if (pcm->ready_mode == SND_PCM_READY_ASAP ||
465             pcm->xrun_mode == SND_PCM_XRUN_ASAP) {
466                 snd_pcm_sframes_t d;
467                 int err = snd_pcm_hw_delay(pcm, &d);
468                 if (err < 0)
469                         return err;
470         }
471 #endif
472         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
473                 avail = snd_pcm_mmap_playback_avail(pcm);
474         } else {
475                 avail = snd_pcm_mmap_capture_avail(pcm);
476                 if (avail > 0 && 
477                     !(pcm->info & SND_PCM_INFO_MMAP)) {
478                         err = snd_pcm_read_mmap(pcm, avail);
479                         if (err < 0)
480                                 return err;
481                         assert((snd_pcm_uframes_t)err == avail);
482                         return err;
483                 }
484         }
485         if (avail > pcm->buffer_size)
486                 return -EPIPE;
487         return avail;
488 }
489
490 static void snd_pcm_hw_dump(snd_pcm_t *pcm, snd_output_t *out)
491 {
492         snd_pcm_hw_t *hw = pcm->private_data;
493         char *name = "Unknown";
494         int err = snd_card_get_name(hw->card, &name);
495         assert(err >= 0);
496         snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n",
497                           hw->card, name, hw->device, hw->subdevice);
498         free(name);
499         if (pcm->setup) {
500                 snd_output_printf(out, "\nIts setup is:\n");
501                 snd_pcm_dump_setup(pcm, out);
502         }
503 }
504
505 snd_pcm_ops_t snd_pcm_hw_ops = {
506         close: snd_pcm_hw_close,
507         info: snd_pcm_hw_info,
508         hw_refine: snd_pcm_hw_hw_refine,
509         hw_params: snd_pcm_hw_hw_params,
510         hw_free: snd_pcm_hw_hw_free,
511         sw_params: snd_pcm_hw_sw_params,
512         channel_info: snd_pcm_hw_channel_info,
513         dump: snd_pcm_hw_dump,
514         nonblock: snd_pcm_hw_nonblock,
515         async: snd_pcm_hw_async,
516         mmap: snd_pcm_hw_mmap,
517         munmap: snd_pcm_hw_munmap,
518 };
519
520 snd_pcm_fast_ops_t snd_pcm_hw_fast_ops = {
521         status: snd_pcm_hw_status,
522         state: snd_pcm_hw_state,
523         delay: snd_pcm_hw_delay,
524         prepare: snd_pcm_hw_prepare,
525         reset: snd_pcm_hw_reset,
526         start: snd_pcm_hw_start,
527         drop: snd_pcm_hw_drop,
528         drain: snd_pcm_hw_drain,
529         pause: snd_pcm_hw_pause,
530         rewind: snd_pcm_hw_rewind,
531         writei: snd_pcm_hw_writei,
532         writen: snd_pcm_hw_writen,
533         readi: snd_pcm_hw_readi,
534         readn: snd_pcm_hw_readn,
535         avail_update: snd_pcm_hw_avail_update,
536         mmap_forward: snd_pcm_hw_mmap_forward,
537 };
538
539 int snd_pcm_hw_open_subdevice(snd_pcm_t **pcmp, int card, int device, int subdevice, snd_pcm_stream_t stream, int mode)
540 {
541         char filename[32];
542         char *filefmt;
543         int ver;
544         int ret = 0, fd = -1;
545         int attempt = 0;
546         snd_pcm_info_t info;
547         int fmode;
548         snd_ctl_t *ctl;
549         snd_pcm_t *pcm = NULL;
550         snd_pcm_hw_t *hw = NULL;
551
552         assert(pcmp);
553
554         if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
555                 return ret;
556
557         switch (snd_enum_to_int(stream)) {
558         case SND_PCM_STREAM_PLAYBACK:
559                 filefmt = SNDRV_FILE_PCM_STREAM_PLAYBACK;
560                 break;
561         case SND_PCM_STREAM_CAPTURE:
562                 filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
563                 break;
564         default:
565                 assert(0);
566         }
567         sprintf(filename, filefmt, card, device);
568
569       __again:
570         if (attempt++ > 3) {
571                 ret = -EBUSY;
572                 goto _err;
573         }
574         ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
575         if (ret < 0)
576                 goto _err;
577         fmode = O_RDWR;
578         if (mode & SND_PCM_NONBLOCK)
579                 fmode |= O_NONBLOCK;
580         if (mode & SND_PCM_ASYNC)
581                 fmode |= O_ASYNC;
582         if ((fd = open(filename, fmode)) < 0) {
583                 SYSERR("open %s failed", filename);
584                 ret = -errno;
585                 goto _err;
586         }
587         if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
588                 SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
589                 ret = -errno;
590                 goto _err;
591         }
592         if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_PCM_VERSION_MAX)) {
593                 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
594                 goto _err;
595         }
596         if (subdevice >= 0) {
597                 memset(&info, 0, sizeof(info));
598                 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info) < 0) {
599                         SYSERR("SNDRV_PCM_IOCTL_INFO failed");
600                         ret = -errno;
601                         goto _err;
602                 }
603                 if (info.subdevice != (unsigned int) subdevice) {
604                         close(fd);
605                         goto __again;
606                 }
607         }
608         hw = calloc(1, sizeof(snd_pcm_hw_t));
609         if (!hw) {
610                 ret = -ENOMEM;
611                 goto _err;
612         }
613         hw->card = card;
614         hw->device = device;
615         hw->subdevice = subdevice;
616         hw->fd = fd;
617
618         pcm = calloc(1, sizeof(snd_pcm_t));
619         if (!pcm) {
620                 ret = -ENOMEM;
621                 goto _err;
622         }
623         snd_ctl_close(ctl);
624         pcm->type = SND_PCM_TYPE_HW;
625         pcm->stream = stream;
626         pcm->mode = mode;
627         pcm->ops = &snd_pcm_hw_ops;
628         pcm->op_arg = pcm;
629         pcm->fast_ops = &snd_pcm_hw_fast_ops;
630         pcm->fast_op_arg = pcm;
631         pcm->private_data = hw;
632         pcm->poll_fd = fd;
633         *pcmp = pcm;
634         ret = snd_pcm_hw_mmap_status(pcm);
635         if (ret < 0) {
636                 snd_pcm_close(pcm);
637                 return ret;
638         }
639         ret = snd_pcm_hw_mmap_control(pcm);
640         if (ret < 0) {
641                 snd_pcm_close(pcm);
642                 return ret;
643         }
644         return 0;
645         
646  _err:
647         if (hw)
648                 free(hw);
649         if (pcm)
650                 free(pcm);
651         if (fd >= 0)
652                 close(fd);
653         snd_ctl_close(ctl);
654         return ret;
655 }
656
657 int snd_pcm_hw_open_device(snd_pcm_t **pcmp, int card, int device, snd_pcm_stream_t stream, int mode)
658 {
659         return snd_pcm_hw_open_subdevice(pcmp, card, device, -1, stream, mode);
660 }
661
662 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)
663 {
664         int err = snd_pcm_hw_open_subdevice(pcmp, card, device, subdevice, stream, mode);
665         if (err < 0)
666                 return err;
667         if (name)
668                 (*pcmp)->name = strdup(name);
669         return 0;
670 }
671
672 int _snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name, snd_config_t *conf,
673                      snd_pcm_stream_t stream, int mode)
674 {
675         snd_config_iterator_t i, next;
676         long card = -1, device = 0, subdevice = -1;
677         const char *str;
678         int err;
679         snd_config_for_each(i, next, conf) {
680                 snd_config_t *n = snd_config_iterator_entry(i);
681                 const char *id = snd_config_get_id(n);
682                 if (strcmp(id, "comment") == 0)
683                         continue;
684                 if (strcmp(id, "type") == 0)
685                         continue;
686                 if (strcmp(id, "card") == 0) {
687                         err = snd_config_get_integer(n, &card);
688                         if (err < 0) {
689                                 err = snd_config_get_string(n, &str);
690                                 if (err < 0) {
691                                         SNDERR("Invalid type for %s", id);
692                                         return -EINVAL;
693                                 }
694                                 card = snd_card_get_index(str);
695                                 if (card < 0) {
696                                         SNDERR("Invalid value for %s", id);
697                                         return card;
698                                 }
699                         }
700                         continue;
701                 }
702                 if (strcmp(id, "device") == 0) {
703                         err = snd_config_get_integer(n, &device);
704                         if (err < 0) {
705                                 SNDERR("Invalid type for %s", id);
706                                 return err;
707                         }
708                         continue;
709                 }
710                 if (strcmp(id, "subdevice") == 0) {
711                         err = snd_config_get_integer(n, &subdevice);
712                         if (err < 0) {
713                                 SNDERR("Invalid type for %s", id);
714                                 return err;
715                         }
716                         continue;
717                 }
718                 SNDERR("Unknown field %s", id);
719                 return -EINVAL;
720         }
721         if (card < 0) {
722                 SNDERR("card is not defined");
723                 return -EINVAL;
724         }
725         return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode);
726 }
727