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_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;
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 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)), 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");
370 hw->mmap_status = ptr;
371 pcm->hw_ptr = &hw->mmap_status->hw_ptr;
375 static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
377 snd_pcm_hw_t *hw = pcm->private_data;
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");
385 hw->mmap_control = ptr;
386 pcm->appl_ptr = &hw->mmap_control->appl_ptr;
390 static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
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");
400 static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
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");
410 static int snd_pcm_hw_mmap(snd_pcm_t *pcm)
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);
417 SYSERR("shmget failed");
425 static int snd_pcm_hw_munmap(snd_pcm_t *pcm)
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");
437 static int snd_pcm_hw_close(snd_pcm_t *pcm)
439 snd_pcm_hw_t *hw = pcm->private_data;
441 SYSERR("close failed\n");
444 snd_pcm_hw_munmap_status(pcm);
445 snd_pcm_hw_munmap_control(pcm);
450 static snd_pcm_sframes_t snd_pcm_hw_mmap_forward(snd_pcm_t *pcm, snd_pcm_uframes_t size)
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);
459 static snd_pcm_sframes_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
461 snd_pcm_uframes_t avail;
462 snd_pcm_sframes_t err;
464 if (pcm->ready_mode == SND_PCM_READY_ASAP ||
465 pcm->xrun_mode == SND_PCM_XRUN_ASAP) {
467 int err = snd_pcm_hw_delay(pcm, &d);
472 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
473 avail = snd_pcm_mmap_playback_avail(pcm);
475 avail = snd_pcm_mmap_capture_avail(pcm);
477 !(pcm->info & SND_PCM_INFO_MMAP)) {
478 err = snd_pcm_read_mmap(pcm, avail);
481 assert((snd_pcm_uframes_t)err == avail);
485 if (avail > pcm->buffer_size)
490 static void snd_pcm_hw_dump(snd_pcm_t *pcm, snd_output_t *out)
492 snd_pcm_hw_t *hw = pcm->private_data;
493 char *name = "Unknown";
494 int err = snd_card_get_name(hw->card, &name);
496 snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n",
497 hw->card, name, hw->device, hw->subdevice);
500 snd_output_printf(out, "\nIts setup is:\n");
501 snd_pcm_dump_setup(pcm, out);
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,
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,
539 int snd_pcm_hw_open_subdevice(snd_pcm_t **pcmp, int card, int device, int subdevice, snd_pcm_stream_t stream, int mode)
544 int ret = 0, fd = -1;
549 snd_pcm_t *pcm = NULL;
550 snd_pcm_hw_t *hw = NULL;
554 if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
557 switch (snd_enum_to_int(stream)) {
558 case SND_PCM_STREAM_PLAYBACK:
559 filefmt = SNDRV_FILE_PCM_STREAM_PLAYBACK;
561 case SND_PCM_STREAM_CAPTURE:
562 filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
567 sprintf(filename, filefmt, card, device);
574 ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice);
578 if (mode & SND_PCM_NONBLOCK)
580 if (mode & SND_PCM_ASYNC)
582 if ((fd = open(filename, fmode)) < 0) {
583 SYSERR("open %s failed", filename);
587 if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
588 SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
592 if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_PCM_VERSION_MAX)) {
593 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
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");
603 if (info.subdevice != (unsigned int) subdevice) {
608 hw = calloc(1, sizeof(snd_pcm_hw_t));
615 hw->subdevice = subdevice;
618 pcm = calloc(1, sizeof(snd_pcm_t));
624 pcm->type = SND_PCM_TYPE_HW;
625 pcm->stream = stream;
627 pcm->ops = &snd_pcm_hw_ops;
629 pcm->fast_ops = &snd_pcm_hw_fast_ops;
630 pcm->fast_op_arg = pcm;
631 pcm->private_data = hw;
634 ret = snd_pcm_hw_mmap_status(pcm);
639 ret = snd_pcm_hw_mmap_control(pcm);
657 int snd_pcm_hw_open_device(snd_pcm_t **pcmp, int card, int device, snd_pcm_stream_t stream, int mode)
659 return snd_pcm_hw_open_subdevice(pcmp, card, device, -1, stream, mode);
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)
664 int err = snd_pcm_hw_open_subdevice(pcmp, card, device, subdevice, stream, mode);
668 (*pcmp)->name = strdup(name);
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)
675 snd_config_iterator_t i, next;
676 long card = -1, device = 0, subdevice = -1;
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)
684 if (strcmp(id, "type") == 0)
686 if (strcmp(id, "card") == 0) {
687 err = snd_config_get_integer(n, &card);
689 err = snd_config_get_string(n, &str);
691 SNDERR("Invalid type for %s", id);
694 card = snd_card_get_index(str);
696 SNDERR("Invalid value for %s", id);
702 if (strcmp(id, "device") == 0) {
703 err = snd_config_get_integer(n, &device);
705 SNDERR("Invalid type for %s", id);
710 if (strcmp(id, "subdevice") == 0) {
711 err = snd_config_get_integer(n, &subdevice);
713 SNDERR("Invalid type for %s", id);
718 SNDERR("Unknown field %s", id);
722 SNDERR("card is not defined");
725 return snd_pcm_hw_open(pcmp, name, card, device, subdevice, stream, mode);