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
37 #include <sys/ioctl.h>
40 #include "pcm_local.h"
41 #include "../control/control_local.h"
43 //#define DEBUG_RW /* use to debug readi/writei/readn/writen */
44 //#define DEBUG_MMAP /* debug mmap_commit */
47 /* entry for static linking */
48 const char *_snd_module_pcm_hw = "";
61 struct sndrv_pcm_hw_params_old {
63 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
64 SNDRV_PCM_HW_PARAM_ACCESS + 1];
65 struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
66 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
71 unsigned int rate_num;
72 unsigned int rate_den;
73 sndrv_pcm_uframes_t fifo_size;
74 unsigned char reserved[64];
77 #define SND_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
78 #define SND_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
80 #define SND_PCM_IOCTL_XRUN _IO('A', 0x48)
82 static int use_old_hw_params_ioctl(int fd, unsigned int cmd, snd_pcm_hw_params_t *params);
91 int card, device, subdevice;
93 volatile struct sndrv_pcm_mmap_status * mmap_status;
94 struct sndrv_pcm_mmap_control *mmap_control;
95 int shadow_appl_ptr: 1,
98 snd_pcm_uframes_t appl_ptr;
102 #define SNDRV_FILE_PCM_STREAM_PLAYBACK "/dev/snd/pcmC%iD%ip"
103 #define SNDRV_FILE_PCM_STREAM_CAPTURE "/dev/snd/pcmC%iD%ic"
104 #define SNDRV_PCM_VERSION_MAX SNDRV_PROTOCOL_VERSION(2, 0, 2)
106 /* update appl_ptr with driver */
107 #define UPDATE_SHADOW_PTR(hw) \
108 do { if (hw->shadow_appl_ptr && !hw->avail_update_flag) \
109 hw->appl_ptr = hw->mmap_control->appl_ptr; } while (0)
110 #define FAST_PCM_STATE(hw) \
111 ((enum sndrv_pcm_state) (hw)->mmap_status->state)
113 #endif /* DOC_HIDDEN */
115 static int snd_pcm_hw_nonblock(snd_pcm_t *pcm, int nonblock)
118 snd_pcm_hw_t *hw = pcm->private_data;
121 if ((flags = fcntl(fd, F_GETFL)) < 0) {
122 SYSERR("F_GETFL failed");
128 flags &= ~O_NONBLOCK;
129 if (fcntl(fd, F_SETFL, flags) < 0) {
130 SYSERR("F_SETFL for O_NONBLOCK failed");
136 static int snd_pcm_hw_async(snd_pcm_t *pcm, int sig, pid_t pid)
139 snd_pcm_hw_t *hw = pcm->private_data;
142 if ((flags = fcntl(fd, F_GETFL)) < 0) {
143 SYSERR("F_GETFL failed");
150 if (fcntl(fd, F_SETFL, flags) < 0) {
151 SYSERR("F_SETFL for O_ASYNC failed");
156 if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
157 SYSERR("F_SETSIG failed");
160 if (fcntl(fd, F_SETOWN, (long)pid) < 0) {
161 SYSERR("F_SETOWN failed");
167 static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
169 snd_pcm_hw_t *hw = pcm->private_data;
171 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, info) < 0) {
172 SYSERR("SNDRV_PCM_IOCTL_INFO failed");
178 static inline int hw_refine_call(snd_pcm_hw_t *pcm_hw, snd_pcm_hw_params_t *params)
180 /* check for new hw_params structure; it's available from 2.0.2 version of PCM API */
181 if (SNDRV_PROTOCOL_VERSION(2, 0, 2) <= pcm_hw->version)
182 return ioctl(pcm_hw->fd, SNDRV_PCM_IOCTL_HW_REFINE, params);
183 return use_old_hw_params_ioctl(pcm_hw->fd, SND_PCM_IOCTL_HW_REFINE_OLD, params);
186 static int snd_pcm_hw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
188 snd_pcm_hw_t *hw = pcm->private_data;
189 if (hw->mmap_emulation) {
191 snd_pcm_access_mask_t oldmask = *snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
192 snd_pcm_access_mask_t mask;
193 const snd_mask_t *pmask;
195 snd_mask_empty(&mask);
196 if (hw_refine_call(hw, params) < 0)
199 snd_pcm_hw_params_t new = *params;
201 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_INTERLEAVED) &&
202 !snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_INTERLEAVED))
203 snd_pcm_access_mask_set(&mask, SND_PCM_ACCESS_RW_INTERLEAVED);
204 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) &&
205 !snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
206 snd_pcm_access_mask_set(&mask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
207 if (snd_pcm_access_mask_empty(&mask))
209 pmask = snd_pcm_hw_param_get_mask(&new, SND_PCM_HW_PARAM_ACCESS);
210 *(snd_mask_t *)pmask = mask;
211 if (hw_refine_call(hw, &new) < 0)
215 pmask = snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
216 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED) ||
217 snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) ||
218 snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_MMAP_COMPLEX))
220 if (snd_pcm_access_mask_test(&mask, SND_PCM_ACCESS_RW_INTERLEAVED)) {
221 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_INTERLEAVED))
222 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
223 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_INTERLEAVED);
225 if (snd_pcm_access_mask_test(&mask, SND_PCM_ACCESS_RW_NONINTERLEAVED)) {
226 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
227 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
228 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
230 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_INTERLEAVED)) {
231 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_INTERLEAVED)) {
232 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_INTERLEAVED))
233 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
236 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) {
237 if (snd_pcm_access_mask_test(&oldmask, SND_PCM_ACCESS_RW_NONINTERLEAVED)) {
238 if (snd_pcm_access_mask_test(pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED))
239 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
243 if (hw_refine_call(hw, params) < 0) {
244 // SYSERR("SNDRV_PCM_IOCTL_HW_REFINE failed");
252 static inline int hw_params_call(snd_pcm_hw_t *pcm_hw, snd_pcm_hw_params_t *params)
254 /* check for new hw_params structure; it's available from 2.0.2 version of PCM API */
255 if (SNDRV_PROTOCOL_VERSION(2, 0, 2) <= pcm_hw->version)
256 return ioctl(pcm_hw->fd, SNDRV_PCM_IOCTL_HW_PARAMS, params);
257 return use_old_hw_params_ioctl(pcm_hw->fd, SND_PCM_IOCTL_HW_PARAMS_OLD, params);
260 static int snd_pcm_hw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
262 snd_pcm_hw_t *hw = pcm->private_data;
263 if (hw->mmap_emulation) {
264 snd_pcm_hw_params_t old = *params;
265 if (hw_params_call(hw, params) < 0) {
266 snd_pcm_access_t access;
267 snd_pcm_access_mask_t oldmask;
268 const snd_mask_t *pmask;
271 pmask = snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS);
272 oldmask = *(snd_pcm_access_mask_t *)pmask;
273 if (INTERNAL(snd_pcm_hw_params_get_access)(params, &access) < 0)
276 case SND_PCM_ACCESS_MMAP_INTERLEAVED:
277 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
278 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_INTERLEAVED);
280 case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
281 snd_pcm_access_mask_reset((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
282 snd_pcm_access_mask_set((snd_pcm_access_mask_t *)pmask, SND_PCM_ACCESS_RW_NONINTERLEAVED);
287 if (hw_params_call(hw, params) < 0)
290 *(snd_pcm_access_mask_t *)pmask = oldmask;
293 if (hw_params_call(hw, params) < 0) {
295 SYSERR("SNDRV_PCM_IOCTL_HW_PARAMS failed");
299 if (pcm->stream == SND_PCM_STREAM_CAPTURE) {
301 hw->shadow_appl_ptr = 1;
303 snd_pcm_set_appl_ptr(pcm, &hw->appl_ptr, -1, 0);
305 hw->shadow_appl_ptr = 0;
306 snd_pcm_set_appl_ptr(pcm, &hw->mmap_control->appl_ptr, hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
312 static int snd_pcm_hw_hw_free(snd_pcm_t *pcm)
314 snd_pcm_hw_t *hw = pcm->private_data;
316 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_FREE) < 0) {
317 SYSERR("SNDRV_PCM_IOCTL_HW_FREE failed");
323 static int snd_pcm_hw_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t * params)
325 snd_pcm_hw_t *hw = pcm->private_data;
327 if ((snd_pcm_tstamp_t) params->tstamp_mode == pcm->tstamp_mode &&
328 params->period_step == pcm->period_step &&
329 params->sleep_min == pcm->sleep_min &&
330 params->xfer_align == pcm->xfer_align &&
331 params->start_threshold == pcm->start_threshold &&
332 params->stop_threshold == pcm->stop_threshold &&
333 params->silence_threshold == pcm->silence_threshold &&
334 params->silence_size == pcm->silence_size) {
335 hw->mmap_control->avail_min = params->avail_min;
338 if (ioctl(fd, SNDRV_PCM_IOCTL_SW_PARAMS, params) < 0) {
339 SYSERR("SNDRV_PCM_IOCTL_SW_PARAMS failed");
345 static int snd_pcm_hw_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
347 snd_pcm_hw_t *hw = pcm->private_data;
348 struct sndrv_pcm_channel_info i;
350 i.channel = info->channel;
351 if (ioctl(fd, SNDRV_PCM_IOCTL_CHANNEL_INFO, &i) < 0) {
352 SYSERR("SNDRV_PCM_IOCTL_CHANNEL_INFO failed");
355 info->channel = i.channel;
358 info->first = i.first;
360 info->type = SND_PCM_AREA_MMAP;
361 info->u.mmap.fd = fd;
362 info->u.mmap.offset = i.offset;
365 return snd_pcm_channel_info_shm(pcm, info, hw->shmid);
368 static int snd_pcm_hw_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
370 snd_pcm_hw_t *hw = pcm->private_data;
372 if (ioctl(fd, SNDRV_PCM_IOCTL_STATUS, status) < 0) {
373 SYSERR("SNDRV_PCM_IOCTL_STATUS failed");
379 static snd_pcm_state_t snd_pcm_hw_state(snd_pcm_t *pcm)
381 snd_pcm_hw_t *hw = pcm->private_data;
382 return (snd_pcm_state_t) hw->mmap_status->state;
385 static int snd_pcm_hw_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
387 snd_pcm_hw_t *hw = pcm->private_data;
389 if (ioctl(fd, SNDRV_PCM_IOCTL_DELAY, delayp) < 0) {
390 // SYSERR("SNDRV_PCM_IOCTL_DELAY failed");
396 static int snd_pcm_hw_prepare(snd_pcm_t *pcm)
398 snd_pcm_hw_t *hw = pcm->private_data;
400 if (ioctl(fd, SNDRV_PCM_IOCTL_PREPARE) < 0) {
401 SYSERR("SNDRV_PCM_IOCTL_PREPARE failed");
407 static int snd_pcm_hw_reset(snd_pcm_t *pcm)
409 snd_pcm_hw_t *hw = pcm->private_data;
411 if (ioctl(fd, SNDRV_PCM_IOCTL_RESET) < 0) {
412 SYSERR("SNDRV_PCM_IOCTL_RESET failed");
418 static int snd_pcm_hw_start(snd_pcm_t *pcm)
420 snd_pcm_hw_t *hw = pcm->private_data;
423 assert(pcm->stream != SND_PCM_STREAM_PLAYBACK ||
424 snd_pcm_mmap_playback_hw_avail(pcm) > 0);
426 if (ioctl(fd, SNDRV_PCM_IOCTL_START) < 0) {
427 SYSERR("SNDRV_PCM_IOCTL_START failed");
430 SNDERR("PCM state = %s", snd_pcm_state_name(snd_pcm_hw_state(pcm)));
437 static int snd_pcm_hw_drop(snd_pcm_t *pcm)
439 snd_pcm_hw_t *hw = pcm->private_data;
441 if (ioctl(fd, SNDRV_PCM_IOCTL_DROP) < 0) {
442 SYSERR("SNDRV_PCM_IOCTL_DROP failed");
448 static int snd_pcm_hw_drain(snd_pcm_t *pcm)
450 snd_pcm_hw_t *hw = pcm->private_data;
452 if (ioctl(fd, SNDRV_PCM_IOCTL_DRAIN) < 0) {
454 SYSERR("SNDRV_PCM_IOCTL_DRAIN failed");
460 static int snd_pcm_hw_pause(snd_pcm_t *pcm, int enable)
462 snd_pcm_hw_t *hw = pcm->private_data;
464 if (ioctl(fd, SNDRV_PCM_IOCTL_PAUSE, enable) < 0) {
465 SYSERR("SNDRV_PCM_IOCTL_PAUSE failed");
471 static snd_pcm_sframes_t snd_pcm_hw_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
473 snd_pcm_hw_t *hw = pcm->private_data;
475 if (ioctl(fd, SNDRV_PCM_IOCTL_REWIND, &frames) < 0) {
476 SYSERR("SNDRV_PCM_IOCTL_REWIND failed");
482 static int snd_pcm_hw_resume(snd_pcm_t *pcm)
484 snd_pcm_hw_t *hw = pcm->private_data;
486 if (ioctl(fd, SNDRV_PCM_IOCTL_RESUME) < 0) {
487 if (errno != ENXIO && errno != ENOSYS)
488 SYSERR("SNDRV_PCM_IOCTL_RESUME failed");
494 static snd_pcm_sframes_t snd_pcm_hw_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
497 snd_pcm_hw_t *hw = pcm->private_data;
499 struct sndrv_xferi xferi;
500 xferi.buf = (char*) buffer;
502 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &xferi);
504 fprintf(stderr, "hw_writei: frames = %li, result = %i, result = %li\n", size, result, xferi.result);
511 static snd_pcm_sframes_t snd_pcm_hw_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
514 snd_pcm_hw_t *hw = pcm->private_data;
516 struct sndrv_xfern xfern;
519 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEN_FRAMES, &xfern);
521 fprintf(stderr, "hw_writen: frames = %li, result = %i, result = %li\n", size, result, xfern.result);
528 static snd_pcm_sframes_t snd_pcm_hw_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
531 snd_pcm_hw_t *hw = pcm->private_data;
533 struct sndrv_xferi xferi;
536 result = ioctl(fd, SNDRV_PCM_IOCTL_READI_FRAMES, &xferi);
538 fprintf(stderr, "hw_readi: frames = %li, result = %i, result = %li\n", size, result, xferi.result);
542 UPDATE_SHADOW_PTR(hw);
546 static snd_pcm_sframes_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
549 snd_pcm_hw_t *hw = pcm->private_data;
551 struct sndrv_xfern xfern;
554 result = ioctl(fd, SNDRV_PCM_IOCTL_READN_FRAMES, &xfern);
556 fprintf(stderr, "hw_readn: frames = %li, result = %i, result = %li\n", size, result, xfern.result);
560 UPDATE_SHADOW_PTR(hw);
564 static int snd_pcm_hw_mmap_status(snd_pcm_t *pcm)
566 snd_pcm_hw_t *hw = pcm->private_data;
568 ptr = mmap(NULL, page_align(sizeof(struct sndrv_pcm_mmap_status)),
569 PROT_READ, MAP_FILE|MAP_SHARED,
570 hw->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
571 if (ptr == MAP_FAILED || ptr == NULL) {
572 SYSERR("status mmap failed");
575 hw->mmap_status = ptr;
576 snd_pcm_set_hw_ptr(pcm, &hw->mmap_status->hw_ptr, hw->fd, SNDRV_PCM_MMAP_OFFSET_STATUS + offsetof(struct sndrv_pcm_mmap_status, hw_ptr));
580 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
582 snd_pcm_hw_t *hw = pcm->private_data;
584 ptr = mmap(NULL, page_align(sizeof(struct sndrv_pcm_mmap_control)),
585 PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
586 hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
587 if (ptr == MAP_FAILED || ptr == NULL) {
588 SYSERR("control mmap failed");
591 hw->mmap_control = ptr;
592 snd_pcm_set_appl_ptr(pcm, &hw->mmap_control->appl_ptr, hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
596 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
598 snd_pcm_hw_t *hw = pcm->private_data;
599 if (munmap((void*)hw->mmap_status, page_align(sizeof(*hw->mmap_status))) < 0) {
600 SYSERR("status munmap failed");
606 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
608 snd_pcm_hw_t *hw = pcm->private_data;
609 if (munmap(hw->mmap_control, page_align(sizeof(*hw->mmap_control))) < 0) {
610 SYSERR("control munmap failed");
616 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
618 snd_pcm_hw_t *hw = pcm->private_data;
620 snd_pcm_uframes_t size = snd_pcm_frames_to_bytes(pcm, (snd_pcm_sframes_t) pcm->buffer_size);
621 int id = shmget(IPC_PRIVATE, size, 0666);
624 SYSERR("shmget failed");
632 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
634 snd_pcm_hw_t *hw = pcm->private_data;
636 if (shmctl(hw->shmid, IPC_RMID, 0) < 0) {
637 SYSERR("shmctl IPC_RMID failed");
644 static int snd_pcm_hw_close(snd_pcm_t *pcm)
646 snd_pcm_hw_t *hw = pcm->private_data;
648 SYSERR("close failed\n");
651 snd_pcm_hw_munmap_status(pcm);
652 snd_pcm_hw_munmap_control(pcm);
657 static snd_pcm_sframes_t snd_pcm_hw_mmap_commit(snd_pcm_t *pcm,
658 snd_pcm_uframes_t offset ATTRIBUTE_UNUSED,
659 snd_pcm_uframes_t size)
661 snd_pcm_hw_t *hw = pcm->private_data;
664 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
665 snd_pcm_sframes_t result = 0, res;
668 res = snd_pcm_write_mmap(pcm, size);
670 return result > 0 ? result : res;
676 snd_pcm_hw_t *hw = pcm->private_data;
677 assert(hw->shadow_appl_ptr);
680 snd_pcm_mmap_appl_forward(pcm, size);
682 fprintf(stderr, "appl_forward: hw_ptr = %li, appl_ptr = %li, size = %li\n", *pcm->hw_ptr, *pcm->appl_ptr, size);
687 static snd_pcm_sframes_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
689 snd_pcm_hw_t *hw = pcm->private_data;
690 snd_pcm_uframes_t avail;
691 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
692 avail = snd_pcm_mmap_playback_avail(pcm);
694 avail = snd_pcm_mmap_capture_avail(pcm);
695 if (avail > 0 && hw->mmap_shm) {
696 snd_pcm_sframes_t err;
697 snd_pcm_hw_t *hw = pcm->private_data;
698 hw->avail_update_flag = 1;
699 err = snd_pcm_read_mmap(pcm, avail);
700 hw->avail_update_flag = 0;
703 assert((snd_pcm_uframes_t)err == avail);
707 switch (FAST_PCM_STATE(hw)) {
708 case SNDRV_PCM_STATE_RUNNING:
709 if (avail >= pcm->stop_threshold) {
710 /* SNDRV_PCM_IOCTL_XRUN ioctl has been implemented since PCM kernel API 2.0.1 */
711 if (SNDRV_PROTOCOL_VERSION(2, 0, 1) <= hw->version) {
712 if (ioctl(hw->fd, SND_PCM_IOCTL_XRUN) < 0)
715 /* everything is ok, state == SND_PCM_STATE_XRUN at the moment */
719 case SNDRV_PCM_STATE_XRUN:
727 static void snd_pcm_hw_dump(snd_pcm_t *pcm, snd_output_t *out)
729 snd_pcm_hw_t *hw = pcm->private_data;
731 int err = snd_card_get_name(hw->card, &name);
733 snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n",
734 hw->card, name, hw->device, hw->subdevice);
737 snd_output_printf(out, "\nIts setup is:\n");
738 snd_pcm_dump_setup(pcm, out);
742 static snd_pcm_ops_t snd_pcm_hw_ops = {
743 close: snd_pcm_hw_close,
744 info: snd_pcm_hw_info,
745 hw_refine: snd_pcm_hw_hw_refine,
746 hw_params: snd_pcm_hw_hw_params,
747 hw_free: snd_pcm_hw_hw_free,
748 sw_params: snd_pcm_hw_sw_params,
749 channel_info: snd_pcm_hw_channel_info,
750 dump: snd_pcm_hw_dump,
751 nonblock: snd_pcm_hw_nonblock,
752 async: snd_pcm_hw_async,
753 mmap: snd_pcm_hw_mmap,
754 munmap: snd_pcm_hw_munmap,
757 static snd_pcm_fast_ops_t snd_pcm_hw_fast_ops = {
758 status: snd_pcm_hw_status,
759 state: snd_pcm_hw_state,
760 delay: snd_pcm_hw_delay,
761 prepare: snd_pcm_hw_prepare,
762 reset: snd_pcm_hw_reset,
763 start: snd_pcm_hw_start,
764 drop: snd_pcm_hw_drop,
765 drain: snd_pcm_hw_drain,
766 pause: snd_pcm_hw_pause,
767 rewind: snd_pcm_hw_rewind,
768 resume: snd_pcm_hw_resume,
769 writei: snd_pcm_hw_writei,
770 writen: snd_pcm_hw_writen,
771 readi: snd_pcm_hw_readi,
772 readn: snd_pcm_hw_readn,
773 avail_update: snd_pcm_hw_avail_update,
774 mmap_commit: snd_pcm_hw_mmap_commit,
778 * \brief Creates a new hw PCM
779 * \param pcmp Returns created PCM handle
780 * \param name Name of PCM
781 * \param card Number of card
782 * \param device Number of device
783 * \param subdevice Number of subdevice
784 * \param stream PCM Stream
785 * \param mode PCM Mode
786 * \retval zero on success otherwise a negative error code
787 * \warning Using of this function might be dangerous in the sense
788 * of compatibility reasons. The prototype might be freely
791 int snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name,
792 int card, int device, int subdevice,
793 snd_pcm_stream_t stream, int mode,
799 int err, ret = 0, fd = -1;
804 snd_pcm_t *pcm = NULL;
805 snd_pcm_hw_t *hw = NULL;
809 if ((ret = snd_ctl_hw_open(&ctl, NULL, card, 0)) < 0)
813 case SND_PCM_STREAM_PLAYBACK:
814 filefmt = SNDRV_FILE_PCM_STREAM_PLAYBACK;
816 case SND_PCM_STREAM_CAPTURE:
817 filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
822 sprintf(filename, filefmt, card, device);
829 ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
833 if (mode & SND_PCM_NONBLOCK)
835 if (mode & SND_PCM_ASYNC)
837 if ((fd = open(filename, fmode)) < 0) {
838 SYSERR("open %s failed", filename);
842 if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
843 SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
847 if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_PCM_VERSION_MAX)) {
848 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
851 if (subdevice >= 0) {
852 memset(&info, 0, sizeof(info));
853 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info) < 0) {
854 SYSERR("SNDRV_PCM_IOCTL_INFO failed");
858 if (info.subdevice != (unsigned int) subdevice) {
863 hw = calloc(1, sizeof(snd_pcm_hw_t));
871 hw->subdevice = subdevice;
873 hw->mmap_emulation = mmap_emulation;
875 err = snd_pcm_new(&pcm, SND_PCM_TYPE_HW, name, stream, mode);
881 pcm->ops = &snd_pcm_hw_ops;
882 pcm->fast_ops = &snd_pcm_hw_fast_ops;
883 pcm->private_data = hw;
886 ret = snd_pcm_hw_mmap_status(pcm);
891 ret = snd_pcm_hw_mmap_control(pcm);
909 /*! \page pcm_plugins
911 \section pcm_plugins_hw Plugin: hw
913 This plugin communicates directly with the ALSA kernel driver. It is a raw
914 communication without any conversions. The emulation of mmap access can be
915 optionally enabled, but expect worse latency in the case.
920 card INT/STR # Card name (string) or number (integer)
921 [device INT] # Device number (default 0)
922 [subdevice INT] # Subdevice number (default -1: first available)
923 [mmap_emulation BOOL] # Enable mmap emulation for ro/wo devices
927 \subsection pcm_plugins_hw_funcref Function reference
930 <LI>snd_pcm_hw_open()
931 <LI>_snd_pcm_hw_open()
937 * \brief Creates a new hw PCM
938 * \param pcmp Returns created PCM handle
939 * \param name Name of PCM
940 * \param root Root configuration node
941 * \param conf Configuration node with hw PCM description
942 * \param stream PCM Stream
943 * \param mode PCM Mode
944 * \warning Using of this function might be dangerous in the sense
945 * of compatibility reasons. The prototype might be freely
948 int _snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name,
949 snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *conf,
950 snd_pcm_stream_t stream, int mode)
952 snd_config_iterator_t i, next;
953 long card = -1, device = 0, subdevice = -1;
955 int err, mmap_emulation = 0;
956 snd_config_for_each(i, next, conf) {
957 snd_config_t *n = snd_config_iterator_entry(i);
959 if (snd_config_get_id(n, &id) < 0)
961 if (snd_pcm_conf_generic_id(id))
963 if (strcmp(id, "card") == 0) {
964 err = snd_config_get_integer(n, &card);
966 err = snd_config_get_string(n, &str);
968 SNDERR("Invalid type for %s", id);
971 card = snd_card_get_index(str);
973 SNDERR("Invalid value for %s", id);
979 if (strcmp(id, "device") == 0) {
980 err = snd_config_get_integer(n, &device);
982 SNDERR("Invalid type for %s", id);
987 if (strcmp(id, "subdevice") == 0) {
988 err = snd_config_get_integer(n, &subdevice);
990 SNDERR("Invalid type for %s", id);
995 if (strcmp(id, "mmap_emulation") == 0) {
996 err = snd_config_get_bool(n);
999 mmap_emulation = err;
1002 SNDERR("Unknown field %s", id);
1006 SNDERR("card is not defined");
1009 return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode, mmap_emulation);
1012 SND_DLSYM_BUILD_VERSION(_snd_pcm_hw_open, SND_PCM_DLSYM_VERSION);
1016 * To be removed helpers, but keep binary compatibility at the time
1020 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
1021 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
1024 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params,
1025 struct sndrv_pcm_hw_params_old *oparams)
1029 memset(params, 0, sizeof(*params));
1030 params->flags = oparams->flags;
1031 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
1032 params->masks[i].bits[0] = oparams->masks[i];
1033 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
1034 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
1035 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
1036 params->info = oparams->info;
1037 params->msbits = oparams->msbits;
1038 params->rate_num = oparams->rate_num;
1039 params->rate_den = oparams->rate_den;
1040 params->fifo_size = oparams->fifo_size;
1043 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams,
1044 snd_pcm_hw_params_t *params,
1045 unsigned int *cmask)
1049 memset(oparams, 0, sizeof(*oparams));
1050 oparams->flags = params->flags;
1051 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++) {
1052 oparams->masks[i] = params->masks[i].bits[0];
1053 for (j = 1; j < sizeof(params->masks[i].bits) / sizeof(unsigned int); j++)
1054 if (params->masks[i].bits[j]) {
1059 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
1060 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
1061 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
1062 oparams->info = params->info;
1063 oparams->msbits = params->msbits;
1064 oparams->rate_num = params->rate_num;
1065 oparams->rate_den = params->rate_den;
1066 oparams->fifo_size = params->fifo_size;
1069 static int use_old_hw_params_ioctl(int fd, unsigned int cmd, snd_pcm_hw_params_t *params)
1071 struct sndrv_pcm_hw_params_old oparams;
1072 unsigned int cmask = 0;
1075 snd_pcm_hw_convert_to_old_params(&oparams, params, &cmask);
1076 res = ioctl(fd, cmd, &oparams);
1077 snd_pcm_hw_convert_from_old_params(params, &oparams);
1078 params->cmask |= cmask;