Debian Codegraph @

oss/pcm_oss.c

raw symbols SCIP
1 /*
2 * ALSA <-> OSS PCM I/O plugin
3 *
4 * Copyright (c) 2005 by Takashi Iwai <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <sys/ioctl.h>
24 #include <alsa/asoundlib.h>
25 #include <alsa/pcm_external.h>
26 #if defined(__linux__)
27 #include <linux/soundcard.h>
28 #elif HAVE_SYS_SOUNDCARD_H
29 #include <sys/soundcard.h>
30 #elif HAVE_SOUNDCARD_H
31 #include <soundcard.h>
32 #endif
33
34 typedef struct snd_pcm_oss {
35 snd_pcm_ioplug_t io;
36 char *device;
37 int fd;
38 int fragment_set;
39 int caps;
40 int format;
41 unsigned int period_shift;
42 unsigned int periods;
43 unsigned int frame_bytes;
44 } snd_pcm_oss_t;
45
46 static snd_pcm_sframes_t oss_write(snd_pcm_ioplug_t *io,
47 const snd_pcm_channel_area_t *areas,
48 snd_pcm_uframes_t offset,
49 snd_pcm_uframes_t size)
50 {
51 snd_pcm_oss_t *oss = io->private_data;
52 const char *buf;
53 ssize_t result;
54
55 /* we handle only an interleaved buffer */
56 buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
57 size *= oss->frame_bytes;
58 result = write(oss->fd, buf, size);
59 if (result <= 0)
60 return result;
61 return result / oss->frame_bytes;
62 }
63
64 static snd_pcm_sframes_t oss_read(snd_pcm_ioplug_t *io,
65 const snd_pcm_channel_area_t *areas,
66 snd_pcm_uframes_t offset,
67 snd_pcm_uframes_t size)
68 {
69 snd_pcm_oss_t *oss = io->private_data;
70 char *buf;
71 ssize_t result;
72
73 /* we handle only an interleaved buffer */
74 buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
75 size *= oss->frame_bytes;
76 result = read(oss->fd, buf, size);
77 if (result <= 0)
78 return result;
79 return result / oss->frame_bytes;
80 }
81
82 static snd_pcm_sframes_t oss_pointer(snd_pcm_ioplug_t *io)
83 {
84 snd_pcm_oss_t *oss = io->private_data;
85 struct count_info info;
86 int ptr;
87
88 if (ioctl(oss->fd, io->stream == SND_PCM_STREAM_PLAYBACK ?
89 SNDCTL_DSP_GETOPTR : SNDCTL_DSP_GETIPTR, &info) < 0) {
90 fprintf(stderr, "*** OSS: oss_pointer error\n");
91 return 0;
92 }
93 ptr = snd_pcm_bytes_to_frames(io->pcm, info.ptr);
94 return ptr;
95 }
96
97 static int oss_start(snd_pcm_ioplug_t *io)
98 {
99 snd_pcm_oss_t *oss = io->private_data;
100 int tmp = io->stream == SND_PCM_STREAM_PLAYBACK ?
101 PCM_ENABLE_OUTPUT : PCM_ENABLE_INPUT;
102
103 if (ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &tmp) < 0) {
104 fprintf(stderr, "*** OSS: trigger failed\n");
105 if (io->stream == SND_PCM_STREAM_CAPTURE)
106 /* fake read to trigger */
107 read(oss->fd, &tmp, 0);
108 }
109 return 0;
110 }
111
112 static int oss_stop(snd_pcm_ioplug_t *io)
113 {
114 snd_pcm_oss_t *oss = io->private_data;
115 int tmp = 0;
116
117 ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &tmp);
118 return 0;
119 }
120
121 static int oss_drain(snd_pcm_ioplug_t *io)
122 {
123 snd_pcm_oss_t *oss = io->private_data;
124
125 if (io->stream == SND_PCM_STREAM_PLAYBACK)
126 ioctl(oss->fd, SNDCTL_DSP_SYNC);
127 return 0;
128 }
129
130 static int oss_prepare(snd_pcm_ioplug_t *io)
131 {
132 snd_pcm_oss_t *oss = io->private_data;
133 int tmp;
134
135 ioctl(oss->fd, SNDCTL_DSP_RESET);
136
137 tmp = io->channels;
138 if (ioctl(oss->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0) {
139 perror("SNDCTL_DSP_CHANNELS");
140 return -EINVAL;
141 }
142 tmp = oss->format;
143 if (ioctl(oss->fd, SNDCTL_DSP_SETFMT, &tmp) < 0) {
144 perror("SNDCTL_DSP_SETFMT");
145 return -EINVAL;
146 }
147 tmp = io->rate;
148 if (ioctl(oss->fd, SNDCTL_DSP_SPEED, &tmp) < 0 ||
149 tmp > io->rate * 1.01 || tmp < io->rate * 0.99) {
150 perror("SNDCTL_DSP_SPEED");
151 return -EINVAL;
152 }
153 return 0;
154 }
155
156 static int oss_hw_params(snd_pcm_ioplug_t *io,
157 snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED)
158 {
159 snd_pcm_oss_t *oss = io->private_data;
160 int i, tmp, err;
161 unsigned int period_bytes;
162 long oflags, flags;
163
164 oss->frame_bytes = (snd_pcm_format_physical_width(io->format) * io->channels) / 8;
165 switch (io->format) {
166 case SND_PCM_FORMAT_U8:
167 oss->format = AFMT_U8;
168 break;
169 case SND_PCM_FORMAT_S16_LE:
170 oss->format = AFMT_S16_LE;
171 break;
172 case SND_PCM_FORMAT_S16_BE:
173 oss->format = AFMT_S16_BE;
174 break;
175 default:
176 fprintf(stderr, "*** OSS: unsupported format %s\n", snd_pcm_format_name(io->format));
177 return -EINVAL;
178 }
179 period_bytes = io->period_size * oss->frame_bytes;
180 oss->period_shift = 0;
181 for (i = 31; i >= 4; i--) {
182 if (period_bytes & (1U << i)) {
183 oss->period_shift = i;
184 break;
185 }
186 }
187 if (! oss->period_shift) {
188 fprintf(stderr, "*** OSS: invalid period size %d\n", (int)io->period_size);
189 return -EINVAL;
190 }
191 oss->periods = io->buffer_size / io->period_size;
192
193 _retry:
194 tmp = oss->period_shift | (oss->periods << 16);
195 if (ioctl(oss->fd, SNDCTL_DSP_SETFRAGMENT, &tmp) < 0) {
196 if (! oss->fragment_set) {
197 perror("SNDCTL_DSP_SETFRAGMENT");
198 fprintf(stderr, "*** period shift = %d, periods = %d\n", oss->period_shift, oss->periods);
199 return -EINVAL;
200 }
201 /* OSS has no proper way to reinitialize the fragments */
202 /* try to reopen the device */
203 close(oss->fd);
204 oss->fd = open(oss->device, io->stream == SND_PCM_STREAM_PLAYBACK ?
205 O_WRONLY : O_RDONLY);
206 if (oss->fd < 0) {
207 err = -errno;
208 SNDERR("Cannot reopen the device %s", oss->device);
209 return err;
210 }
211 io->poll_fd = oss->fd;
212 io->poll_events = io->stream == SND_PCM_STREAM_PLAYBACK ?
213 POLLOUT : POLLIN;
214 snd_pcm_ioplug_reinit_status(io);
215 oss->fragment_set = 0;
216 goto _retry;
217 }
218 oss->fragment_set = 1;
219
220 if ((flags = fcntl(oss->fd, F_GETFL)) < 0) {
221 err = -errno;
222 perror("F_GETFL");
223 } else {
224 oflags = flags;
225 if (io->nonblock)
226 flags |= O_NONBLOCK;
227 else
228 flags &= ~O_NONBLOCK;
229 if (flags != oflags &&
230 fcntl(oss->fd, F_SETFL, flags) < 0) {
231 err = -errno;
232 perror("F_SETFL");
233 }
234 }
235
236 return 0;
237 }
238
239 #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
240
241 static int oss_hw_constraint(snd_pcm_oss_t *oss)
242 {
243 snd_pcm_ioplug_t *io = &oss->io;
244 static const snd_pcm_access_t access_list[] = {
245 SND_PCM_ACCESS_RW_INTERLEAVED,
246 SND_PCM_ACCESS_MMAP_INTERLEAVED
247 };
248 unsigned int nformats;
249 unsigned int format[5];
250 unsigned int nchannels;
251 unsigned int channel[6];
252 /* period and buffer bytes must be power of two */
253 static const unsigned int bytes_list[] = {
254 1U<<8, 1U<<9, 1U<<10, 1U<<11, 1U<<12, 1U<<13, 1U<<14, 1U<<15,
255 1U<<16, 1U<<17, 1U<<18, 1U<<19, 1U<<20, 1U<<21, 1U<<22, 1U<<23
256 };
257 int i, err, tmp;
258
259 /* check trigger */
260 oss->caps = 0;
261 if (ioctl(oss->fd, SNDCTL_DSP_GETCAPS, &oss->caps) >= 0) {
262 if (! (oss->caps & DSP_CAP_TRIGGER))
263 fprintf(stderr, "*** OSS: trigger is not supported!\n");
264 }
265
266 /* access type - interleaved only */
267 if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
268 ARRAY_SIZE(access_list), access_list)) < 0)
269 return err;
270
271 /* supported formats */
272 tmp = 0;
273 ioctl(oss->fd, SNDCTL_DSP_GETFMTS, &tmp);
274 nformats = 0;
275 if (tmp & AFMT_U8)
276 format[nformats++] = SND_PCM_FORMAT_U8;
277 if (tmp & AFMT_S16_LE)
278 format[nformats++] = SND_PCM_FORMAT_S16_LE;
279 if (tmp & AFMT_S16_BE)
280 format[nformats++] = SND_PCM_FORMAT_S16_BE;
281 if (tmp & AFMT_MU_LAW)
282 format[nformats++] = SND_PCM_FORMAT_MU_LAW;
283 if (! nformats)
284 format[nformats++] = SND_PCM_FORMAT_S16;
285 if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
286 nformats, format)) < 0)
287 return err;
288
289 /* supported channels */
290 nchannels = 0;
291 for (i = 0; i < 6; i++) {
292 tmp = i + 1;
293 if (ioctl(oss->fd, SNDCTL_DSP_CHANNELS, &tmp) >= 0)
294 channel[nchannels++] = tmp;
295 }
296 if (! nchannels) /* assume 2ch stereo */
297 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
298 2, 2);
299 else
300 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_CHANNELS,
301 nchannels, channel);
302 if (err < 0)
303 return err;
304
305 /* supported rates */
306 /* FIXME: should query? */
307 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, 8000, 480000);
308 if (err < 0)
309 return err;
310
311 /* period size (in power of two) */
312 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
313 ARRAY_SIZE(bytes_list), bytes_list);
314 if (err < 0)
315 return err;
316 /* periods */
317 err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS, 2, 1024);
318 if (err < 0)
319 return err;
320 /* buffer size (in power of two) */
321 err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_BUFFER_BYTES,
322 ARRAY_SIZE(bytes_list), bytes_list);
323 if (err < 0)
324 return err;
325
326 return 0;
327 }
328
329
330 static int oss_close(snd_pcm_ioplug_t *io)
331 {
332 snd_pcm_oss_t *oss = io->private_data;
333
334 close(oss->fd);
335 free(oss->device);
336 free(oss);
337 return 0;
338 }
339
340 static const snd_pcm_ioplug_callback_t oss_playback_callback = {
341 .start = oss_start,
342 .stop = oss_stop,
343 .transfer = oss_write,
344 .pointer = oss_pointer,
345 .close = oss_close,
346 .hw_params = oss_hw_params,
347 .prepare = oss_prepare,
348 .drain = oss_drain,
349 };
350
351 static const snd_pcm_ioplug_callback_t oss_capture_callback = {
352 .start = oss_start,
353 .stop = oss_stop,
354 .transfer = oss_read,
355 .pointer = oss_pointer,
356 .close = oss_close,
357 .hw_params = oss_hw_params,
358 .prepare = oss_prepare,
359 .drain = oss_drain,
360 };
361
362
363 SND_PCM_PLUGIN_DEFINE_FUNC(oss)
364 {
365 snd_config_iterator_t i, next;
366 const char *device = "/dev/dsp";
367 int err;
368 snd_pcm_oss_t *oss;
369
370 snd_config_for_each(i, next, conf) {
371 snd_config_t *n = snd_config_iterator_entry(i);
372 const char *id;
373 if (snd_config_get_id(n, &id) < 0)
374 continue;
375 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
376 continue;
377 if (strcmp(id, "device") == 0) {
378 if (snd_config_get_string(n, &device) < 0) {
379 SNDERR("Invalid type for %s", id);
380 return -EINVAL;
381 }
382 continue;
383 }
384 SNDERR("Unknown field %s", id);
385 return -EINVAL;
386 }
387
388 oss = calloc(1, sizeof(*oss));
389 if (! oss) {
390 SNDERR("cannot allocate");
391 return -ENOMEM;
392 }
393
394 oss->device = strdup(device);
395 if (oss->device == NULL) {
396 SNDERR("cannot allocate");
397 free(oss);
398 return -ENOMEM;
399 }
400 oss->fd = open(device, stream == SND_PCM_STREAM_PLAYBACK ?
401 O_WRONLY : O_RDONLY);
402 if (oss->fd < 0) {
403 err = -errno;
404 SNDERR("Cannot open device %s", device);
405 goto error;
406 }
407
408 oss->io.version = SND_PCM_IOPLUG_VERSION;
409 oss->io.name = "ALSA <-> OSS PCM I/O Plugin";
410 oss->io.poll_fd = oss->fd;
411 oss->io.poll_events = stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
412 oss->io.mmap_rw = 0;
413 oss->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
414 &oss_playback_callback : &oss_capture_callback;
415 oss->io.private_data = oss;
416
417 err = snd_pcm_ioplug_create(&oss->io, name, stream, mode);
418 if (err < 0)
419 goto error;
420
421 if ((err = oss_hw_constraint(oss)) < 0) {
422 snd_pcm_ioplug_delete(&oss->io);
423 return err;
424 }
425
426 *pcmp = oss->io.pcm;
427 return 0;
428
429 error:
430 if (oss->fd >= 0)
431 close(oss->fd);
432 free(oss->device);
433 free(oss);
434 return err;
435 }
436
437 SND_PCM_PLUGIN_SYMBOL(oss);