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>
30 #include "pcm_local.h"
35 int card, device, subdevice;
38 #define SND_FILE_PCM_STREAM_PLAYBACK "/dev/snd/pcmC%iD%ip"
39 #define SND_FILE_PCM_STREAM_CAPTURE "/dev/snd/pcmC%iD%ic"
40 #define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
42 static int snd_pcm_hw_close(void *private)
44 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
53 static int snd_pcm_hw_nonblock(void *private, int nonblock)
56 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
59 if ((flags = fcntl(fd, F_GETFL)) < 0)
65 if (fcntl(fd, F_SETFL, flags) < 0)
70 static int snd_pcm_hw_info(void *private, snd_pcm_info_t * info)
72 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
74 if (ioctl(fd, SND_PCM_IOCTL_INFO, info) < 0)
79 static int snd_pcm_hw_params_info(void *private, snd_pcm_params_info_t * info)
81 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
83 if (ioctl(fd, SND_PCM_IOCTL_PARAMS_INFO, info) < 0)
88 static int snd_pcm_hw_params(void *private, snd_pcm_params_t * params)
90 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
92 if (ioctl(fd, SND_PCM_IOCTL_PARAMS, params) < 0)
97 static int snd_pcm_hw_setup(void *private, snd_pcm_setup_t * setup)
99 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
101 if (ioctl(fd, SND_PCM_IOCTL_SETUP, setup) < 0)
106 static int snd_pcm_hw_channel_setup(void *private, snd_pcm_channel_setup_t * setup)
108 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
110 if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0)
115 static int snd_pcm_hw_status(void *private, snd_pcm_status_t * status)
117 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
119 if (ioctl(fd, SND_PCM_IOCTL_STATUS, status) < 0)
124 static int snd_pcm_hw_state(void *private)
126 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
128 snd_pcm_status_t status;
129 if (ioctl(fd, SND_PCM_IOCTL_STATUS, status) < 0)
134 static ssize_t snd_pcm_hw_frame_io(void *private, int update UNUSED)
136 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
138 ssize_t pos = ioctl(fd, SND_PCM_IOCTL_FRAME_IO);
144 static int snd_pcm_hw_prepare(void *private)
146 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
148 if (ioctl(fd, SND_PCM_IOCTL_PREPARE) < 0)
153 static int snd_pcm_hw_go(void *private)
155 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
157 if (ioctl(fd, SND_PCM_IOCTL_GO) < 0)
162 static int snd_pcm_hw_drain(void *private)
164 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
166 if (ioctl(fd, SND_PCM_IOCTL_DRAIN) < 0)
171 static int snd_pcm_hw_flush(void *private)
173 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
175 if (ioctl(fd, SND_PCM_IOCTL_FLUSH) < 0)
180 static int snd_pcm_hw_pause(void *private, int enable)
182 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
184 if (ioctl(fd, SND_PCM_IOCTL_PAUSE, enable) < 0)
189 static ssize_t snd_pcm_hw_frame_data(void *private, off_t offset)
192 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
193 snd_pcm_t *handle = hw->handle;
195 if (handle->mmap_status && handle->mmap_control)
196 return snd_pcm_mmap_frame_data(handle, offset);
197 result = ioctl(fd, SND_PCM_IOCTL_FRAME_DATA, offset);
203 static ssize_t snd_pcm_hw_write(void *private, snd_timestamp_t *tstamp, const void *buffer, size_t size)
206 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
210 xfer.tstamp = *tstamp;
212 xfer.tstamp.tv_sec = xfer.tstamp.tv_usec = 0;
213 xfer.buf = (char*) buffer;
215 result = ioctl(fd, SND_PCM_IOCTL_WRITE_FRAMES, &xfer);
221 static ssize_t snd_pcm_hw_writev(void *private, snd_timestamp_t *tstamp, const struct iovec *vector, unsigned long count)
224 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
228 xferv.tstamp = *tstamp;
230 xferv.tstamp.tv_sec = xferv.tstamp.tv_usec = 0;
231 xferv.vector = vector;
233 result = ioctl(fd, SND_PCM_IOCTL_WRITEV_FRAMES, &xferv);
239 static ssize_t snd_pcm_hw_read(void *private, snd_timestamp_t *tstamp, void *buffer, size_t size)
242 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
246 xfer.tstamp = *tstamp;
248 xfer.tstamp.tv_sec = xfer.tstamp.tv_usec = 0;
251 result = ioctl(fd, SND_PCM_IOCTL_READ_FRAMES, &xfer);
257 ssize_t snd_pcm_hw_readv(void *private, snd_timestamp_t *tstamp, const struct iovec *vector, unsigned long count)
260 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
264 xferv.tstamp = *tstamp;
266 xferv.tstamp.tv_sec = xferv.tstamp.tv_usec = 0;
267 xferv.vector = vector;
269 result = ioctl(fd, SND_PCM_IOCTL_READV_FRAMES, &xferv);
275 static int snd_pcm_hw_mmap_status(void *private, snd_pcm_mmap_status_t **status)
278 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
279 ptr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ, MAP_FILE|MAP_SHARED,
280 hw->fd, SND_PCM_MMAP_OFFSET_STATUS);
281 if (ptr == MAP_FAILED || ptr == NULL)
287 static int snd_pcm_hw_mmap_control(void *private, snd_pcm_mmap_control_t **control)
290 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
291 ptr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
292 hw->fd, SND_PCM_MMAP_OFFSET_CONTROL);
293 if (ptr == MAP_FAILED || ptr == NULL)
299 static int snd_pcm_hw_mmap_data(void *private, void **buffer, size_t bsize)
303 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
304 prot = hw->handle->stream == SND_PCM_STREAM_PLAYBACK ? PROT_WRITE : PROT_READ;
305 daddr = mmap(NULL, bsize, prot, MAP_FILE|MAP_SHARED,
306 hw->fd, SND_PCM_MMAP_OFFSET_DATA);
307 if (daddr == MAP_FAILED || daddr == NULL)
313 static int snd_pcm_hw_munmap_status(void *private UNUSED, snd_pcm_mmap_status_t *status)
315 if (munmap(status, sizeof(*status)) < 0)
320 static int snd_pcm_hw_munmap_control(void *private UNUSED, snd_pcm_mmap_control_t *control)
322 if (munmap(control, sizeof(*control)) < 0)
327 static int snd_pcm_hw_munmap_data(void *private UNUSED, void *buffer, size_t bsize)
329 if (munmap(buffer, bsize) < 0)
334 static int snd_pcm_hw_file_descriptor(void *private)
336 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
340 static int snd_pcm_hw_channels_mask(void *private UNUSED,
341 bitset_t *client_vmask UNUSED)
346 static void snd_pcm_hw_dump(void *private, FILE *fp)
348 snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
349 snd_pcm_t *handle = hw->handle;
350 char *name = "Unknown";
351 snd_card_get_name(hw->card, &name);
352 fprintf(fp, "Hardware PCM card %d '%s' device %d subdevice %d\n",
353 hw->card, name, hw->device, hw->subdevice);
355 if (handle->valid_setup) {
356 fprintf(fp, "\nIts setup is:\n");
357 snd_pcm_dump_setup(handle, fp);
361 struct snd_pcm_ops snd_pcm_hw_ops = {
362 close: snd_pcm_hw_close,
363 info: snd_pcm_hw_info,
364 params_info: snd_pcm_hw_params_info,
365 params: snd_pcm_hw_params,
366 setup: snd_pcm_hw_setup,
367 dump: snd_pcm_hw_dump,
370 struct snd_pcm_fast_ops snd_pcm_hw_fast_ops = {
371 nonblock: snd_pcm_hw_nonblock,
372 channel_setup: snd_pcm_hw_channel_setup,
373 status: snd_pcm_hw_status,
374 frame_io: snd_pcm_hw_frame_io,
375 state: snd_pcm_hw_state,
376 prepare: snd_pcm_hw_prepare,
378 drain: snd_pcm_hw_drain,
379 flush: snd_pcm_hw_flush,
380 pause: snd_pcm_hw_pause,
381 frame_data: snd_pcm_hw_frame_data,
382 write: snd_pcm_hw_write,
383 writev: snd_pcm_hw_writev,
384 read: snd_pcm_hw_read,
385 readv: snd_pcm_hw_readv,
386 mmap_status: snd_pcm_hw_mmap_status,
387 mmap_control: snd_pcm_hw_mmap_control,
388 mmap_data: snd_pcm_hw_mmap_data,
389 munmap_status: snd_pcm_hw_munmap_status,
390 munmap_control: snd_pcm_hw_munmap_control,
391 munmap_data: snd_pcm_hw_munmap_data,
392 file_descriptor: snd_pcm_hw_file_descriptor,
393 channels_mask: snd_pcm_hw_channels_mask,
396 int snd_pcm_hw_open_subdevice(snd_pcm_t **handlep, int card, int device, int subdevice, int stream, int mode)
401 int ret = 0, fd = -1;
411 if ((ret = snd_ctl_open(&ctl, card)) < 0)
415 case SND_PCM_STREAM_PLAYBACK:
416 filefmt = SND_FILE_PCM_STREAM_PLAYBACK;
418 case SND_PCM_STREAM_CAPTURE:
419 filefmt = SND_FILE_PCM_STREAM_CAPTURE;
424 if ((ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice)) < 0)
426 sprintf(filename, filefmt, card, device);
434 if (mode & SND_PCM_NONBLOCK)
436 if ((fd = open(filename, fmode)) < 0) {
440 if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
444 if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
445 ret = -SND_ERROR_INCOMPATIBLE_VERSION;
448 if (subdevice >= 0) {
449 memset(&info, 0, sizeof(info));
450 if (ioctl(fd, SND_PCM_IOCTL_INFO, &info) < 0) {
454 if (info.subdevice != subdevice) {
459 handle = calloc(1, sizeof(snd_pcm_t));
464 hw = calloc(1, sizeof(snd_pcm_hw_t));
473 hw->subdevice = subdevice;
475 handle->type = SND_PCM_TYPE_HW;
476 handle->stream = stream;
477 handle->ops = &snd_pcm_hw_ops;
479 handle->fast_ops = &snd_pcm_hw_fast_ops;
480 handle->fast_op_arg = hw;
482 handle->private = hw;
486 if (ret < 0 && fd >= 0)
492 int snd_pcm_hw_open(snd_pcm_t **handlep, int card, int device, int stream, int mode)
494 return snd_pcm_hw_open_subdevice(handlep, card, device, -1, stream, mode);