3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
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.
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.
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.
28 #include <sys/ioctl.h>
31 #include "pcm_local.h"
32 #include "../control/control_local.h"
40 int card, device, subdevice;
41 volatile struct sndrv_pcm_mmap_status *mmap_status;
42 struct sndrv_pcm_mmap_control *mmap_control;
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)
50 static int snd_pcm_hw_nonblock(snd_pcm_t *pcm, int nonblock)
53 snd_pcm_hw_t *hw = pcm->private_data;
56 if ((flags = fcntl(fd, F_GETFL)) < 0) {
57 SYSERR("F_GETFL failed");
64 if (fcntl(fd, F_SETFL, flags) < 0) {
65 SYSERR("F_SETFL for O_NONBLOCK failed");
71 static int snd_pcm_hw_async(snd_pcm_t *pcm, int sig, pid_t pid)
74 snd_pcm_hw_t *hw = pcm->private_data;
77 if ((flags = fcntl(fd, F_GETFL)) < 0) {
78 SYSERR("F_GETFL failed");
85 if (fcntl(fd, F_SETFL, flags) < 0) {
86 SYSERR("F_SETFL for O_ASYNC failed");
93 if (fcntl(fd, F_SETSIG, sig) < 0) {
94 SYSERR("F_SETSIG failed");
99 if (fcntl(fd, F_SETOWN, pid) < 0) {
100 SYSERR("F_SETOWN failed");
106 static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
108 snd_pcm_hw_t *hw = pcm->private_data;
110 if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, info) < 0) {
111 SYSERR("SNDRV_PCM_IOCTL_INFO failed");
117 static int snd_pcm_hw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
119 snd_pcm_hw_t *hw = pcm->private_data;
121 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params) < 0) {
122 // SYSERR("SNDRV_PCM_IOCTL_HW_REFINE failed");
128 static int snd_pcm_hw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
130 snd_pcm_hw_t *hw = pcm->private_data;
132 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_PARAMS, params) < 0) {
133 SYSERR("SNDRV_PCM_IOCTL_HW_PARAMS failed");
139 static int snd_pcm_hw_hw_free(snd_pcm_t *pcm)
141 snd_pcm_hw_t *hw = pcm->private_data;
143 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_FREE) < 0) {
144 SYSERR("SNDRV_PCM_IOCTL_HW_FREE failed");
150 static int snd_pcm_hw_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t * params)
152 snd_pcm_hw_t *hw = pcm->private_data;
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;
165 if (ioctl(fd, SNDRV_PCM_IOCTL_SW_PARAMS, params) < 0) {
166 SYSERR("SNDRV_PCM_IOCTL_SW_PARAMS failed");
172 static int snd_pcm_hw_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
174 snd_pcm_hw_t *hw = pcm->private_data;
175 struct sndrv_pcm_channel_info i;
177 i.channel = info->channel;
178 if (ioctl(fd, SNDRV_PCM_IOCTL_CHANNEL_INFO, &i) < 0) {
179 SYSERR("SNDRV_PCM_IOCTL_CHANNEL_INFO failed");
182 info->channel = i.channel;
183 if (pcm->info & SND_PCM_INFO_MMAP) {
185 info->first = i.first;
187 info->type = SND_PCM_AREA_MMAP;
188 info->u.mmap.fd = fd;
189 info->u.mmap.offset = i.offset;
192 return snd_pcm_channel_info_shm(pcm, info, hw->shmid);
195 static int snd_pcm_hw_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
197 snd_pcm_hw_t *hw = pcm->private_data;
199 if (ioctl(fd, SNDRV_PCM_IOCTL_STATUS, status) < 0) {
200 SYSERR("SNDRV_PCM_IOCTL_STATUS failed");
206 static snd_pcm_state_t snd_pcm_hw_state(snd_pcm_t *pcm)
208 snd_pcm_hw_t *hw = pcm->private_data;
209 return (snd_pcm_state_t) hw->mmap_status->state;
212 static int snd_pcm_hw_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
214 snd_pcm_hw_t *hw = pcm->private_data;
216 if (ioctl(fd, SNDRV_PCM_IOCTL_DELAY, delayp) < 0) {
217 // SYSERR("SNDRV_PCM_IOCTL_DELAY failed");
223 static int snd_pcm_hw_prepare(snd_pcm_t *pcm)
225 snd_pcm_hw_t *hw = pcm->private_data;
227 if (ioctl(fd, SNDRV_PCM_IOCTL_PREPARE) < 0) {
228 SYSERR("SNDRV_PCM_IOCTL_PREPARE failed");
234 static int snd_pcm_hw_reset(snd_pcm_t *pcm)
236 snd_pcm_hw_t *hw = pcm->private_data;
238 if (ioctl(fd, SNDRV_PCM_IOCTL_RESET) < 0) {
239 SYSERR("SNDRV_PCM_IOCTL_RESET failed");
245 static int snd_pcm_hw_start(snd_pcm_t *pcm)
247 snd_pcm_hw_t *hw = pcm->private_data;
250 assert(pcm->stream != SND_PCM_STREAM_PLAYBACK ||
251 snd_pcm_mmap_playback_hw_avail(pcm) > 0);
253 if (ioctl(fd, SNDRV_PCM_IOCTL_START) < 0) {
254 SYSERR("SNDRV_PCM_IOCTL_START failed");
260 static int snd_pcm_hw_drop(snd_pcm_t *pcm)
262 snd_pcm_hw_t *hw = pcm->private_data;
264 if (ioctl(fd, SNDRV_PCM_IOCTL_DROP) < 0) {
265 SYSERR("SNDRV_PCM_IOCTL_DROP failed");
271 static int snd_pcm_hw_drain(snd_pcm_t *pcm)
273 snd_pcm_hw_t *hw = pcm->private_data;
275 if (ioctl(fd, SNDRV_PCM_IOCTL_DRAIN) < 0) {
276 SYSERR("SNDRV_PCM_IOCTL_DRAIN failed");
282 static int snd_pcm_hw_pause(snd_pcm_t *pcm, int enable)
284 snd_pcm_hw_t *hw = pcm->private_data;
286 if (ioctl(fd, SNDRV_PCM_IOCTL_PAUSE, enable) < 0) {
287 SYSERR("SNDRV_PCM_IOCTL_PAUSE failed");
293 static snd_pcm_sframes_t snd_pcm_hw_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
295 snd_pcm_hw_t *hw = pcm->private_data;
297 if (ioctl(fd, SNDRV_PCM_IOCTL_REWIND, &frames) < 0) {
298 SYSERR("SNDRV_PCM_IOCTL_REWIND failed");
304 static snd_pcm_sframes_t snd_pcm_hw_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
306 snd_pcm_sframes_t result;
307 snd_pcm_hw_t *hw = pcm->private_data;
309 struct sndrv_xferi xferi;
310 xferi.buf = (char*) buffer;
312 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &xferi);
318 static snd_pcm_sframes_t snd_pcm_hw_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
320 snd_pcm_sframes_t result;
321 snd_pcm_hw_t *hw = pcm->private_data;
323 struct sndrv_xfern xfern;
326 result = ioctl(fd, SNDRV_PCM_IOCTL_WRITEN_FRAMES, &xfern);
332 static snd_pcm_sframes_t snd_pcm_hw_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
334 snd_pcm_sframes_t result;
335 snd_pcm_hw_t *hw = pcm->private_data;
337 struct sndrv_xferi xferi;
340 result = ioctl(fd, SNDRV_PCM_IOCTL_READI_FRAMES, &xferi);
346 static snd_pcm_sframes_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
348 snd_pcm_sframes_t result;
349 snd_pcm_hw_t *hw = pcm->private_data;
351 struct sndrv_xfern xfern;
354 result = ioctl(fd, SNDRV_PCM_IOCTL_READN_FRAMES, &xfern);
360 static int snd_pcm_hw_mmap_status(snd_pcm_t *pcm)
362 snd_pcm_hw_t *hw = pcm->private_data;
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");
371 hw->mmap_status = ptr;
372 pcm->hw_ptr = &hw->mmap_status->hw_ptr;
376 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
378 snd_pcm_hw_t *hw = pcm->private_data;
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");
387 hw->mmap_control = ptr;
388 pcm->appl_ptr = &hw->mmap_control->appl_ptr;
392 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
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");
402 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
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");
412 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
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);
419 SYSERR("shmget failed");
427 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
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");
439 static int snd_pcm_hw_close(snd_pcm_t *pcm)
441 snd_pcm_hw_t *hw = pcm->private_data;
443 SYSERR("close failed\n");
446 snd_pcm_hw_munmap_status(pcm);
447 snd_pcm_hw_munmap_control(pcm);
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)
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);
463 static snd_pcm_sframes_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
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);
470 avail = snd_pcm_mmap_capture_avail(pcm);
472 !(pcm->info & SND_PCM_INFO_MMAP)) {
473 err = snd_pcm_read_mmap(pcm, avail);
476 assert((snd_pcm_uframes_t)err == avail);
480 if (avail > pcm->buffer_size)
485 static void snd_pcm_hw_dump(snd_pcm_t *pcm, snd_output_t *out)
487 snd_pcm_hw_t *hw = pcm->private_data;
489 int err = snd_card_get_name(hw->card, &name);
491 snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n",
492 hw->card, name, hw->device, hw->subdevice);
495 snd_output_printf(out, "\nIts setup is:\n");
496 snd_pcm_dump_setup(pcm, out);
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,
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,
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)
539 int ret = 0, fd = -1;
544 snd_pcm_t *pcm = NULL;
545 snd_pcm_hw_t *hw = NULL;
549 if ((ret = snd_ctl_hw_open(&ctl, NULL, card, 0)) < 0)
552 switch (snd_enum_to_int(stream)) {
553 case SND_PCM_STREAM_PLAYBACK:
554 filefmt = SNDRV_FILE_PCM_STREAM_PLAYBACK;
556 case SND_PCM_STREAM_CAPTURE:
557 filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
562 sprintf(filename, filefmt, card, device);
569 ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
573 if (mode & SND_PCM_NONBLOCK)
575 if (mode & SND_PCM_ASYNC)
577 if ((fd = open(filename, fmode)) < 0) {
578 SYSERR("open %s failed", filename);
582 if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
583 SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
587 if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_PCM_VERSION_MAX)) {
588 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
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");
598 if (info.subdevice != (unsigned int) subdevice) {
603 hw = calloc(1, sizeof(snd_pcm_hw_t));
610 hw->subdevice = subdevice;
613 pcm = calloc(1, sizeof(snd_pcm_t));
619 pcm->type = SND_PCM_TYPE_HW;
620 pcm->stream = stream;
622 pcm->ops = &snd_pcm_hw_ops;
624 pcm->fast_ops = &snd_pcm_hw_fast_ops;
625 pcm->fast_op_arg = pcm;
626 pcm->private_data = hw;
629 ret = snd_pcm_hw_mmap_status(pcm);
634 ret = snd_pcm_hw_mmap_control(pcm);
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)
654 int err = snd_pcm_hw_open_subdevice(pcmp, card, device, subdevice, stream, mode);
658 (*pcmp)->name = strdup(name);
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)
665 snd_config_iterator_t i, next;
666 long card = -1, device = 0, subdevice = -1;
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))
674 if (strcmp(id, "card") == 0) {
675 err = snd_config_get_integer(n, &card);
677 err = snd_config_get_string(n, &str);
679 SNDERR("Invalid type for %s", id);
682 card = snd_card_get_index(str);
684 SNDERR("Invalid value for %s", id);
690 if (strcmp(id, "device") == 0) {
691 err = snd_config_get_integer(n, &device);
693 SNDERR("Invalid type for %s", id);
698 if (strcmp(id, "subdevice") == 0) {
699 err = snd_config_get_integer(n, &subdevice);
701 SNDERR("Invalid type for %s", id);
706 SNDERR("Unknown field %s", id);
710 SNDERR("card is not defined");
713 return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode);