4 * \brief PCM HW Plugin Interface
5 * \author Abramo Bagnara <abramo@alsa-project.org>
6 * \author Jaroslav Kysela <perex@suse.cz>
11 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
14 * This library is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as
16 * published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/ioctl.h>
39 #include "pcm_local.h"
40 #include "../control/control_local.h"
43 /* entry for static linking */
44 const char *_snd_module_pcm_hw = "";
53 #define SND_PCM_IOCTL_XRUN _IO('A', 0x48)
58 int card, device, subdevice;
60 volatile struct sndrv_pcm_mmap_status *mmap_status;
61 struct sndrv_pcm_mmap_control *mmap_control;
62 int shadow_appl_ptr: 1,
65 snd_pcm_uframes_t appl_ptr;
69 #define SNDRV_FILE_PCM_STREAM_PLAYBACK "/dev/snd/pcmC%iD%ip"
70 #define SNDRV_FILE_PCM_STREAM_CAPTURE "/dev/snd/pcmC%iD%ic"
71 #define SNDRV_PCM_VERSION_MAX SNDRV_PROTOCOL_VERSION(2, 0, 1)
73 /* update appl_ptr with driver */
74 #define UPDATE_SHADOW_PTR(hw) \
75 do { if (hw->shadow_appl_ptr && !hw->avail_update_flag) \
76 hw->appl_ptr = hw->mmap_control->appl_ptr; } while (0)
77 #define FAST_PCM_STATE(hw) \
78 ((enum sndrv_pcm_state) (hw)->mmap_status->state)
80 #endif /* DOC_HIDDEN */
82 static int snd_pcm_hw_nonblock(snd_pcm_t *pcm, int nonblock)
85 snd_pcm_hw_t *hw = pcm->private_data;
88 if ((flags = fcntl(fd, F_GETFL)) < 0) {
89 SYSERR("F_GETFL failed");
96 if (fcntl(fd, F_SETFL, flags) < 0) {
97 SYSERR("F_SETFL for O_NONBLOCK failed");
103 static int snd_pcm_hw_async(snd_pcm_t *pcm, int sig, pid_t pid)
106 snd_pcm_hw_t *hw = pcm->private_data;
109 if ((flags = fcntl(fd, F_GETFL)) < 0) {
110 SYSERR("F_GETFL failed");
117 if (fcntl(fd, F_SETFL, flags) < 0) {
118 SYSERR("F_SETFL for O_ASYNC failed");
123 if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
124 SYSERR("F_SETSIG failed");
127 if (fcntl(fd, F_SETOWN, (long)pid) < 0) {
128 SYSERR("F_SETOWN failed");
134 static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
136 snd_pcm_hw_t *hw = pcm->private_data;
138 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, info) < 0) {
139 SYSERR("SNDRV_PCM_IOCTL_INFO failed");
145 static int snd_pcm_hw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
147 snd_pcm_hw_t *hw = pcm->private_data;
149 if (hw->mmap_emulation) {
151 snd_pcm_access_mask_t oldmask = *snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
152 snd_pcm_access_mask_t mask = { 0 };
153 const snd_mask_t *pmask;
155 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params) < 0)
158 snd_pcm_hw_params_t new = *params;
160 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_INTERLEAVED) &&
161 !snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_INTERLEAVED))
162 snd_pcm_access_mask_set(&mask, SND_PCM_ACCESS_RW_INTERLEAVED);
163 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) &&
164 !snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
165 snd_pcm_access_mask_set(&mask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
166 if (snd_pcm_access_mask_empty(&mask))
168 pmask = snd_pcm_hw_param_get_mask(&new, SND_PCM_HW_PARAM_ACCESS);
169 ((snd_mask_t *)pmask)->bits = mask.bits;
170 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, &new) < 0)
174 pmask = snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
175 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED) ||
176 snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) ||
177 snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_COMPLEX))
179 if (snd_pcm_access_mask_test(&mask, SND_PCM_ACCESS_RW_INTERLEAVED)) {
180 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_INTERLEAVED))
181 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
182 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_INTERLEAVED);
184 if (snd_pcm_access_mask_test(&mask, SND_PCM_ACCESS_RW_NONINTERLEAVED)) {
185 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
186 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
187 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
189 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_INTERLEAVED)) {
190 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_INTERLEAVED)) {
191 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_INTERLEAVED))
192 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
194 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
197 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) {
198 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_NONINTERLEAVED)) {
199 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
200 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
202 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
206 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params) < 0) {
207 // SYSERR("SNDRV_PCM_IOCTL_HW_REFINE failed");
215 static int snd_pcm_hw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
217 snd_pcm_hw_t *hw = pcm->private_data;
219 if (hw->mmap_emulation) {
220 snd_pcm_hw_params_t old = *params;
221 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_PARAMS, params) < 0) {
222 snd_pcm_access_mask_t oldmask;
223 const snd_mask_t *pmask;
226 pmask = snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
227 oldmask = *(snd_pcm_access_mask_t *)pmask;
228 switch (snd_pcm_hw_params_get_access(params)) {
229 case SND_PCM_ACCESS_MMAP_INTERLEAVED:
230 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
231 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_INTERLEAVED);
233 case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
234 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
235 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
240 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_PARAMS, params) < 0)
243 *(snd_pcm_access_mask_t *)pmask = oldmask;
246 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_PARAMS, params) < 0) {
248 SYSERR("SNDRV_PCM_IOCTL_HW_PARAMS failed");
252 if (pcm->stream == SND_PCM_STREAM_CAPTURE) {
254 hw->shadow_appl_ptr = 1;
256 pcm->appl_ptr = &hw->appl_ptr;
258 hw->shadow_appl_ptr = 0;
259 pcm->appl_ptr = &hw->mmap_control->appl_ptr;
265 static int snd_pcm_hw_hw_free(snd_pcm_t *pcm)
267 snd_pcm_hw_t *hw = pcm->private_data;
269 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_FREE) < 0) {
270 SYSERR("SNDRV_PCM_IOCTL_HW_FREE failed");
276 static int snd_pcm_hw_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t * params)
278 snd_pcm_hw_t *hw = pcm->private_data;
280 if ((snd_pcm_tstamp_t) params->tstamp_mode == pcm->tstamp_mode &&
281 params->period_step == pcm->period_step &&
282 params->sleep_min == pcm->sleep_min &&
283 params->xfer_align == pcm->xfer_align &&
284 params->start_threshold == pcm->start_threshold &&
285 params->stop_threshold == pcm->stop_threshold &&
286 params->silence_threshold == pcm->silence_threshold &&
287 params->silence_size == pcm->silence_size) {
288 hw->mmap_control->avail_min = params->avail_min;
291 if (ioctl(fd, SNDRV_PCM_IOCTL_SW_PARAMS, params) < 0) {
292 SYSERR("SNDRV_PCM_IOCTL_SW_PARAMS failed");
298 static int snd_pcm_hw_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
300 snd_pcm_hw_t *hw = pcm->private_data;
301 struct sndrv_pcm_channel_info i;
303 i.channel = info->channel;
304 if (ioctl(fd, SNDRV_PCM_IOCTL_CHANNEL_INFO, &i) < 0) {
305 SYSERR("SNDRV_PCM_IOCTL_CHANNEL_INFO failed");
308 info->channel = i.channel;
311 info->first = i.first;
313 info->type = SND_PCM_AREA_MMAP;
314 info->u.mmap.fd = fd;
315 info->u.mmap.offset = i.offset;
318 return snd_pcm_channel_info_shm(pcm, info, hw->shmid);
321 static int snd_pcm_hw_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
323 snd_pcm_hw_t *hw = pcm->private_data;
325 if (ioctl(fd, SNDRV_PCM_IOCTL_STATUS, status) < 0) {
326 SYSERR("SNDRV_PCM_IOCTL_STATUS failed");
332 static snd_pcm_state_t snd_pcm_hw_state(snd_pcm_t *pcm)
334 snd_pcm_hw_t *hw = pcm->private_data;
335 return (snd_pcm_state_t) hw->mmap_status->state;
338 static int snd_pcm_hw_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
340 snd_pcm_hw_t *hw = pcm->private_data;
342 if (ioctl(fd, SNDRV_PCM_IOCTL_DELAY, delayp) < 0) {
343 // SYSERR("SNDRV_PCM_IOCTL_DELAY failed");
349 static int snd_pcm_hw_prepare(snd_pcm_t *pcm)
351 snd_pcm_hw_t *hw = pcm->private_data;
353 if (ioctl(fd, SNDRV_PCM_IOCTL_PREPARE) < 0) {
354 SYSERR("SNDRV_PCM_IOCTL_PREPARE failed");
360 static int snd_pcm_hw_reset(snd_pcm_t *pcm)
362 snd_pcm_hw_t *hw = pcm->private_data;
364 if (ioctl(fd, SNDRV_PCM_IOCTL_RESET) < 0) {
365 SYSERR("SNDRV_PCM_IOCTL_RESET failed");
371 static int snd_pcm_hw_start(snd_pcm_t *pcm)
373 snd_pcm_hw_t *hw = pcm->private_data;
376 assert(pcm->stream != SND_PCM_STREAM_PLAYBACK ||
377 snd_pcm_mmap_playback_hw_avail(pcm) > 0);
379 if (ioctl(fd, SNDRV_PCM_IOCTL_START) < 0) {
380 SYSERR("SNDRV_PCM_IOCTL_START failed");
383 SNDERR("PCM state = %s", snd_pcm_state_name(snd_pcm_hw_state(pcm)));
390 static int snd_pcm_hw_drop(snd_pcm_t *pcm)
392 snd_pcm_hw_t *hw = pcm->private_data;
394 if (ioctl(fd, SNDRV_PCM_IOCTL_DROP) < 0) {
395 SYSERR("SNDRV_PCM_IOCTL_DROP failed");
401 static int snd_pcm_hw_drain(snd_pcm_t *pcm)
403 snd_pcm_hw_t *hw = pcm->private_data;
405 if (ioctl(fd, SNDRV_PCM_IOCTL_DRAIN) < 0) {
407 SYSERR("SNDRV_PCM_IOCTL_DRAIN failed");
413 static int snd_pcm_hw_pause(snd_pcm_t *pcm, int enable)
415 snd_pcm_hw_t *hw = pcm->private_data;
417 if (ioctl(fd, SNDRV_PCM_IOCTL_PAUSE, enable) < 0) {
418 SYSERR("SNDRV_PCM_IOCTL_PAUSE failed");
424 static snd_pcm_sframes_t snd_pcm_hw_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
426 snd_pcm_hw_t *hw = pcm->private_data;
428 if (ioctl(fd, SNDRV_PCM_IOCTL_REWIND, &frames) < 0) {
429 SYSERR("SNDRV_PCM_IOCTL_REWIND failed");
435 static int snd_pcm_hw_resume(snd_pcm_t *pcm)
437 snd_pcm_hw_t *hw = pcm->private_data;
439 if (ioctl(fd, SNDRV_PCM_IOCTL_RESUME) < 0) {
440 if (errno != ENXIO && errno != ENOSYS)
441 SYSERR("SNDRV_PCM_IOCTL_RESUME failed");
447 static snd_pcm_sframes_t snd_pcm_hw_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
449 snd_pcm_sframes_t result;
450 snd_pcm_hw_t *hw = pcm->private_data;
452 struct sndrv_xferi xferi;
453 xferi.buf = (char*) buffer;
455 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &xferi);
461 static snd_pcm_sframes_t snd_pcm_hw_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
463 snd_pcm_sframes_t result;
464 snd_pcm_hw_t *hw = pcm->private_data;
466 struct sndrv_xfern xfern;
469 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEN_FRAMES, &xfern);
475 static snd_pcm_sframes_t snd_pcm_hw_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
477 snd_pcm_sframes_t result;
478 snd_pcm_hw_t *hw = pcm->private_data;
480 struct sndrv_xferi xferi;
483 result = ioctl(fd, SNDRV_PCM_IOCTL_READI_FRAMES, &xferi);
486 UPDATE_SHADOW_PTR(hw);
490 static snd_pcm_sframes_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
492 snd_pcm_sframes_t result;
493 snd_pcm_hw_t *hw = pcm->private_data;
495 struct sndrv_xfern xfern;
498 result = ioctl(fd, SNDRV_PCM_IOCTL_READN_FRAMES, &xfern);
501 UPDATE_SHADOW_PTR(hw);
505 static int snd_pcm_hw_mmap_status(snd_pcm_t *pcm)
507 snd_pcm_hw_t *hw = pcm->private_data;
509 ptr = mmap(NULL, page_align(sizeof(struct sndrv_pcm_mmap_status)),
510 PROT_READ, MAP_FILE|MAP_SHARED,
511 hw->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
512 if (ptr == MAP_FAILED || ptr == NULL) {
513 SYSERR("status mmap failed");
516 hw->mmap_status = ptr;
517 pcm->hw_ptr = &hw->mmap_status->hw_ptr;
521 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
523 snd_pcm_hw_t *hw = pcm->private_data;
525 ptr = mmap(NULL, page_align(sizeof(struct sndrv_pcm_mmap_control)),
526 PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
527 hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
528 if (ptr == MAP_FAILED || ptr == NULL) {
529 SYSERR("control mmap failed");
532 hw->mmap_control = ptr;
533 pcm->appl_ptr = &hw->mmap_control->appl_ptr;
537 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
539 snd_pcm_hw_t *hw = pcm->private_data;
540 if (munmap((void*)hw->mmap_status, page_align(sizeof(*hw->mmap_status))) < 0) {
541 SYSERR("status munmap failed");
547 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
549 snd_pcm_hw_t *hw = pcm->private_data;
550 if (munmap(hw->mmap_control, page_align(sizeof(*hw->mmap_control))) < 0) {
551 SYSERR("control munmap failed");
557 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
559 snd_pcm_hw_t *hw = pcm->private_data;
561 snd_pcm_uframes_t size = snd_pcm_frames_to_bytes(pcm, (snd_pcm_sframes_t) pcm->buffer_size);
562 int id = shmget(IPC_PRIVATE, size, 0666);
565 SYSERR("shmget failed");
573 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
575 snd_pcm_hw_t *hw = pcm->private_data;
577 if (shmctl(hw->shmid, IPC_RMID, 0) < 0) {
578 SYSERR("shmctl IPC_RMID failed");
585 static int snd_pcm_hw_close(snd_pcm_t *pcm)
587 snd_pcm_hw_t *hw = pcm->private_data;
589 SYSERR("close failed\n");
592 snd_pcm_hw_munmap_status(pcm);
593 snd_pcm_hw_munmap_control(pcm);
598 static snd_pcm_sframes_t snd_pcm_hw_mmap_commit(snd_pcm_t *pcm,
599 snd_pcm_uframes_t offset ATTRIBUTE_UNUSED,
600 snd_pcm_uframes_t size)
602 snd_pcm_hw_t *hw = pcm->private_data;
605 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
606 snd_pcm_sframes_t result = 0, res;
609 res = snd_pcm_write_mmap(pcm, size);
611 return result > 0 ? result : res;
617 snd_pcm_hw_t *hw = pcm->private_data;
618 assert(hw->shadow_appl_ptr);
621 snd_pcm_mmap_appl_forward(pcm, size);
625 static snd_pcm_sframes_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
627 snd_pcm_hw_t *hw = pcm->private_data;
628 snd_pcm_uframes_t avail;
629 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
630 avail = snd_pcm_mmap_playback_avail(pcm);
632 avail = snd_pcm_mmap_capture_avail(pcm);
633 if (avail > 0 && hw->mmap_shm) {
634 snd_pcm_sframes_t err;
635 snd_pcm_hw_t *hw = pcm->private_data;
636 hw->avail_update_flag = 1;
637 err = snd_pcm_read_mmap(pcm, avail);
638 hw->avail_update_flag = 0;
641 assert((snd_pcm_uframes_t)err == avail);
645 switch (FAST_PCM_STATE(hw)) {
646 case SNDRV_PCM_STATE_RUNNING:
647 if (avail >= pcm->stop_threshold) {
648 /* SNDRV_PCM_IOCTL_XRUN ioctl has been implemented since PCM kernel API 2.0.1 */
649 if (SNDRV_PROTOCOL_VERSION(2, 0, 1) <= hw->version) {
650 if (ioctl(hw->fd, SND_PCM_IOCTL_XRUN) < 0)
653 /* everything is ok, state == SND_PCM_STATE_XRUN at the moment */
657 case SNDRV_PCM_STATE_XRUN:
665 static void snd_pcm_hw_dump(snd_pcm_t *pcm, snd_output_t *out)
667 snd_pcm_hw_t *hw = pcm->private_data;
669 int err = snd_card_get_name(hw->card, &name);
671 snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n",
672 hw->card, name, hw->device, hw->subdevice);
675 snd_output_printf(out, "\nIts setup is:\n");
676 snd_pcm_dump_setup(pcm, out);
680 static snd_pcm_ops_t snd_pcm_hw_ops = {
681 close: snd_pcm_hw_close,
682 info: snd_pcm_hw_info,
683 hw_refine: snd_pcm_hw_hw_refine,
684 hw_params: snd_pcm_hw_hw_params,
685 hw_free: snd_pcm_hw_hw_free,
686 sw_params: snd_pcm_hw_sw_params,
687 channel_info: snd_pcm_hw_channel_info,
688 dump: snd_pcm_hw_dump,
689 nonblock: snd_pcm_hw_nonblock,
690 async: snd_pcm_hw_async,
691 mmap: snd_pcm_hw_mmap,
692 munmap: snd_pcm_hw_munmap,
695 static snd_pcm_fast_ops_t snd_pcm_hw_fast_ops = {
696 status: snd_pcm_hw_status,
697 state: snd_pcm_hw_state,
698 delay: snd_pcm_hw_delay,
699 prepare: snd_pcm_hw_prepare,
700 reset: snd_pcm_hw_reset,
701 start: snd_pcm_hw_start,
702 drop: snd_pcm_hw_drop,
703 drain: snd_pcm_hw_drain,
704 pause: snd_pcm_hw_pause,
705 rewind: snd_pcm_hw_rewind,
706 resume: snd_pcm_hw_resume,
707 writei: snd_pcm_hw_writei,
708 writen: snd_pcm_hw_writen,
709 readi: snd_pcm_hw_readi,
710 readn: snd_pcm_hw_readn,
711 avail_update: snd_pcm_hw_avail_update,
712 mmap_commit: snd_pcm_hw_mmap_commit,
716 * \brief Creates a new hw PCM
717 * \param pcmp Returns created PCM handle
718 * \param name Name of PCM
719 * \param card Number of card
720 * \param device Number of device
721 * \param subdevice Number of subdevice
722 * \param stream PCM Stream
723 * \param mode PCM Mode
724 * \retval zero on success otherwise a negative error code
725 * \warning Using of this function might be dangerous in the sense
726 * of compatibility reasons. The prototype might be freely
729 int snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name,
730 int card, int device, int subdevice,
731 snd_pcm_stream_t stream, int mode,
737 int err, ret = 0, fd = -1;
742 snd_pcm_t *pcm = NULL;
743 snd_pcm_hw_t *hw = NULL;
747 if ((ret = snd_ctl_hw_open(&ctl, NULL, card, 0)) < 0)
751 case SND_PCM_STREAM_PLAYBACK:
752 filefmt = SNDRV_FILE_PCM_STREAM_PLAYBACK;
754 case SND_PCM_STREAM_CAPTURE:
755 filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
760 sprintf(filename, filefmt, card, device);
767 ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
771 if (mode & SND_PCM_NONBLOCK)
773 if (mode & SND_PCM_ASYNC)
775 if ((fd = open(filename, fmode)) < 0) {
776 SYSERR("open %s failed", filename);
780 if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
781 SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
785 if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_PCM_VERSION_MAX)) {
786 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
789 if (subdevice >= 0) {
790 memset(&info, 0, sizeof(info));
791 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info) < 0) {
792 SYSERR("SNDRV_PCM_IOCTL_INFO failed");
796 if (info.subdevice != (unsigned int) subdevice) {
801 hw = calloc(1, sizeof(snd_pcm_hw_t));
809 hw->subdevice = subdevice;
811 hw->mmap_emulation = mmap_emulation;
813 err = snd_pcm_new(&pcm, SND_PCM_TYPE_HW, name, stream, mode);
819 pcm->ops = &snd_pcm_hw_ops;
820 pcm->fast_ops = &snd_pcm_hw_fast_ops;
821 pcm->private_data = hw;
824 ret = snd_pcm_hw_mmap_status(pcm);
829 ret = snd_pcm_hw_mmap_control(pcm);
847 /*! \page pcm_plugins
849 \section pcm_plugins_hw Plugin: hw
851 This plugin communicates directly with the ALSA kernel driver. It is a raw
852 communication without any conversions. The emulation of mmap access can be
853 optionally enabled, but expect worse latency in the case.
858 card INT/STR # Card name (string) or number (integer)
859 [device INT] # Device number (default 0)
860 [subdevice INT] # Subdevice number (default -1: first available)
861 [mmap_emulation BOOL] # Enable mmap emulation for ro/wo devices
865 \subsection pcm_plugins_hw_funcref Function reference
868 <LI>snd_pcm_hw_open()
869 <LI>_snd_pcm_hw_open()
875 * \brief Creates a new hw PCM
876 * \param pcmp Returns created PCM handle
877 * \param name Name of PCM
878 * \param root Root configuration node
879 * \param conf Configuration node with hw PCM description
880 * \param stream PCM Stream
881 * \param mode PCM Mode
882 * \warning Using of this function might be dangerous in the sense
883 * of compatibility reasons. The prototype might be freely
886 int _snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name,
887 snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *conf,
888 snd_pcm_stream_t stream, int mode)
890 snd_config_iterator_t i, next;
891 long card = -1, device = 0, subdevice = -1;
893 int err, mmap_emulation = 0;
894 snd_config_for_each(i, next, conf) {
895 snd_config_t *n = snd_config_iterator_entry(i);
897 if (snd_config_get_id(n, &id) < 0)
899 if (snd_pcm_conf_generic_id(id))
901 if (strcmp(id, "card") == 0) {
902 err = snd_config_get_integer(n, &card);
904 err = snd_config_get_string(n, &str);
906 SNDERR("Invalid type for %s", id);
909 card = snd_card_get_index(str);
911 SNDERR("Invalid value for %s", id);
917 if (strcmp(id, "device") == 0) {
918 err = snd_config_get_integer(n, &device);
920 SNDERR("Invalid type for %s", id);
925 if (strcmp(id, "subdevice") == 0) {
926 err = snd_config_get_integer(n, &subdevice);
928 SNDERR("Invalid type for %s", id);
933 if (strcmp(id, "mmap_emulation") == 0) {
934 err = snd_config_get_bool(n);
937 mmap_emulation = err;
940 SNDERR("Unknown field %s", id);
944 SNDERR("card is not defined");
947 return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode, mmap_emulation);
950 SND_DLSYM_BUILD_VERSION(_snd_pcm_hw_open, SND_PCM_DLSYM_VERSION);