blob: ac990bf0b48f7ab11f5684d56b25fae795bd4d5e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#if 0
23#define PLUGIN_DEBUG
24#endif
25#if 0
26#define OSS_DEBUG
27#endif
28
29#include <sound/driver.h>
30#include <linux/init.h>
31#include <linux/smp_lock.h>
32#include <linux/slab.h>
33#include <linux/time.h>
34#include <linux/vmalloc.h>
35#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070036#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/minors.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include "pcm_plugin.h"
42#include <sound/info.h>
43#include <linux/soundcard.h>
44#include <sound/initval.h>
45
46#define OSS_ALSAEMULVER _SIOR ('M', 249, int)
47
48static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
49static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50static int nonblock_open = 1;
51
52MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54MODULE_LICENSE("GPL");
55module_param_array(dsp_map, int, NULL, 0444);
56MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57module_param_array(adsp_map, int, NULL, 0444);
58MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59module_param(nonblock_open, bool, 0644);
60MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010064extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
65static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
66static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
67static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static inline mm_segment_t snd_enter_user(void)
70{
71 mm_segment_t fs = get_fs();
72 set_fs(get_ds());
73 return fs;
74}
75
76static inline void snd_leave_user(mm_segment_t fs)
77{
78 set_fs(fs);
79}
80
Jaroslav Kysela21a34792006-01-13 09:12:11 +010081#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010082static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010084 struct snd_pcm_runtime *runtime = substream->runtime;
85 struct snd_pcm_plugin *plugin, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 plugin = runtime->oss.plugin_first;
88 while (plugin) {
89 next = plugin->next;
90 snd_pcm_plugin_free(plugin);
91 plugin = next;
92 }
93 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
94 return 0;
95}
96
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010097static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010099 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 plugin->next = runtime->oss.plugin_first;
101 plugin->prev = NULL;
102 if (runtime->oss.plugin_first) {
103 runtime->oss.plugin_first->prev = plugin;
104 runtime->oss.plugin_first = plugin;
105 } else {
106 runtime->oss.plugin_last =
107 runtime->oss.plugin_first = plugin;
108 }
109 return 0;
110}
111
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100112int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100114 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 plugin->next = NULL;
116 plugin->prev = runtime->oss.plugin_last;
117 if (runtime->oss.plugin_last) {
118 runtime->oss.plugin_last->next = plugin;
119 runtime->oss.plugin_last = plugin;
120 } else {
121 runtime->oss.plugin_last =
122 runtime->oss.plugin_first = plugin;
123 }
124 return 0;
125}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100126#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100128static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100130 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200131 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200132 long bytes = frames_to_bytes(runtime, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (buffer_size == runtime->oss.buffer_bytes)
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200134 return bytes;
Takashi Iwaicdc5c532005-05-27 12:40:52 +0200135#if BITS_PER_LONG >= 64
136 return runtime->oss.buffer_bytes * bytes / buffer_size;
137#else
138 {
139 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
140 u32 rem;
141 div64_32(&bsize, buffer_size, &rem);
142 return (long)bsize;
143 }
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100147static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100149 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200150 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (buffer_size == runtime->oss.buffer_bytes)
152 return bytes_to_frames(runtime, bytes);
153 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
154}
155
156static int snd_pcm_oss_format_from(int format)
157{
158 switch (format) {
159 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
160 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
161 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
162 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
163 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
164 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
165 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
166 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
167 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
168 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
169 default: return SNDRV_PCM_FORMAT_U8;
170 }
171}
172
173static int snd_pcm_oss_format_to(int format)
174{
175 switch (format) {
176 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
177 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
178 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
179 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
180 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
181 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
182 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
183 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
184 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
185 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
186 default: return -EINVAL;
187 }
188}
189
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100190static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
191 struct snd_pcm_hw_params *oss_params,
192 struct snd_pcm_hw_params *slave_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 size_t s;
195 size_t oss_buffer_size, oss_period_size, oss_periods;
196 size_t min_period_size, max_period_size;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100197 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 size_t oss_frame_size;
199
200 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
201 params_channels(oss_params) / 8;
202
203 oss_buffer_size = snd_pcm_plug_client_size(substream,
204 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
205 oss_buffer_size = 1 << ld2(oss_buffer_size);
206 if (atomic_read(&runtime->mmap_count)) {
207 if (oss_buffer_size > runtime->oss.mmap_bytes)
208 oss_buffer_size = runtime->oss.mmap_bytes;
209 }
210
Takashi Iwai060d77b2006-03-27 16:44:52 +0200211 if (substream->oss.setup.period_size > 16)
212 oss_period_size = substream->oss.setup.period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 else if (runtime->oss.fragshift) {
214 oss_period_size = 1 << runtime->oss.fragshift;
215 if (oss_period_size > oss_buffer_size / 2)
216 oss_period_size = oss_buffer_size / 2;
217 } else {
218 int sd;
219 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
220
221 oss_period_size = oss_buffer_size;
222 do {
223 oss_period_size /= 2;
224 } while (oss_period_size > bytes_per_sec);
225 if (runtime->oss.subdivision == 0) {
226 sd = 4;
227 if (oss_period_size / sd > 4096)
228 sd *= 2;
229 if (oss_period_size / sd < 4096)
230 sd = 1;
231 } else
232 sd = runtime->oss.subdivision;
233 oss_period_size /= sd;
234 if (oss_period_size < 16)
235 oss_period_size = 16;
236 }
237
238 min_period_size = snd_pcm_plug_client_size(substream,
239 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
240 min_period_size *= oss_frame_size;
241 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
242 if (oss_period_size < min_period_size)
243 oss_period_size = min_period_size;
244
245 max_period_size = snd_pcm_plug_client_size(substream,
246 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
247 max_period_size *= oss_frame_size;
248 max_period_size = 1 << ld2(max_period_size);
249 if (oss_period_size > max_period_size)
250 oss_period_size = max_period_size;
251
252 oss_periods = oss_buffer_size / oss_period_size;
253
Takashi Iwai060d77b2006-03-27 16:44:52 +0200254 if (substream->oss.setup.periods > 1)
255 oss_periods = substream->oss.setup.periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
258 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
259 s = runtime->oss.maxfrags;
260 if (oss_periods > s)
261 oss_periods = s;
262
263 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
264 if (s < 2)
265 s = 2;
266 if (oss_periods < s)
267 oss_periods = s;
268
269 while (oss_period_size * oss_periods > oss_buffer_size)
270 oss_period_size /= 2;
271
272 snd_assert(oss_period_size >= 16, return -EINVAL);
273 runtime->oss.period_bytes = oss_period_size;
274 runtime->oss.period_frames = 1;
275 runtime->oss.periods = oss_periods;
276 return 0;
277}
278
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100279static int choose_rate(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params, unsigned int best_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100282 struct snd_interval *it;
283 struct snd_pcm_hw_params *save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned int rate, prev;
285
286 save = kmalloc(sizeof(*save), GFP_KERNEL);
287 if (save == NULL)
288 return -ENOMEM;
289 *save = *params;
290 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
291
292 /* try multiples of the best rate */
293 rate = best_rate;
294 for (;;) {
295 if (it->max < rate || (it->max == rate && it->openmax))
296 break;
297 if (it->min < rate || (it->min == rate && !it->openmin)) {
298 int ret;
299 ret = snd_pcm_hw_param_set(substream, params,
300 SNDRV_PCM_HW_PARAM_RATE,
301 rate, 0);
302 if (ret == (int)rate) {
303 kfree(save);
304 return rate;
305 }
306 *params = *save;
307 }
308 prev = rate;
309 rate += best_rate;
310 if (rate <= prev)
311 break;
312 }
313
314 /* not found, use the nearest rate */
315 kfree(save);
316 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
317}
318
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100319static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100321 struct snd_pcm_runtime *runtime = substream->runtime;
322 struct snd_pcm_hw_params *params, *sparams;
323 struct snd_pcm_sw_params *sw_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ssize_t oss_buffer_size, oss_period_size;
325 size_t oss_frame_size;
326 int err;
327 int direct;
328 int format, sformat, n;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100329 struct snd_mask sformat_mask;
330 struct snd_mask mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
333 params = kmalloc(sizeof(*params), GFP_KERNEL);
334 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
335 if (!sw_params || !params || !sparams) {
336 snd_printd("No memory\n");
337 err = -ENOMEM;
338 goto failure;
339 }
340
Takashi Iwai060d77b2006-03-27 16:44:52 +0200341 if (atomic_read(&runtime->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +0200343 else
344 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 _snd_pcm_hw_params_any(sparams);
347 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
348 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
349 snd_mask_none(&mask);
350 if (atomic_read(&runtime->mmap_count))
351 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
352 else {
353 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
354 if (!direct)
355 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
356 }
357 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
358 if (err < 0) {
359 snd_printd("No usable accesses\n");
360 err = -EINVAL;
361 goto failure;
362 }
363 choose_rate(substream, sparams, runtime->oss.rate);
364 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
365
366 format = snd_pcm_oss_format_from(runtime->oss.format);
367
368 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
369 if (direct)
370 sformat = format;
371 else
372 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
373
374 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
375 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
376 if (snd_mask_test(&sformat_mask, sformat) &&
377 snd_pcm_oss_format_to(sformat) >= 0)
378 break;
379 }
380 if (sformat > SNDRV_PCM_FORMAT_LAST) {
381 snd_printd("Cannot find a format!!!\n");
382 err = -EINVAL;
383 goto failure;
384 }
385 }
386 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
387 snd_assert(err >= 0, goto failure);
388
389 if (direct) {
390 memcpy(params, sparams, sizeof(*params));
391 } else {
392 _snd_pcm_hw_params_any(params);
393 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
394 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
395 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
396 snd_pcm_oss_format_from(runtime->oss.format), 0);
397 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
398 runtime->oss.channels, 0);
399 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
400 runtime->oss.rate, 0);
401 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
402 params_access(params), params_format(params),
403 params_channels(params), params_rate(params));
404 }
405 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
406 params_access(sparams), params_format(sparams),
407 params_channels(sparams), params_rate(sparams));
408
409 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
410 params_channels(params) / 8;
411
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100412#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 snd_pcm_oss_plugin_clear(substream);
414 if (!direct) {
415 /* add necessary plugins */
416 snd_pcm_oss_plugin_clear(substream);
417 if ((err = snd_pcm_plug_format_plugins(substream,
418 params,
419 sparams)) < 0) {
420 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
421 snd_pcm_oss_plugin_clear(substream);
422 goto failure;
423 }
424 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100425 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
427 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
428 snd_pcm_oss_plugin_clear(substream);
429 goto failure;
430 }
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432 err = snd_pcm_plugin_append(plugin);
433 } else {
434 err = snd_pcm_plugin_insert(plugin);
435 }
436 if (err < 0) {
437 snd_pcm_oss_plugin_clear(substream);
438 goto failure;
439 }
440 }
441 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 err = snd_pcm_oss_period_size(substream, params, sparams);
445 if (err < 0)
446 goto failure;
447
448 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
449 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
450 snd_assert(err >= 0, goto failure);
451
452 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
453 runtime->oss.periods, NULL);
454 snd_assert(err >= 0, goto failure);
455
456 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
457
458 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
459 snd_printd("HW_PARAMS failed: %i\n", err);
460 goto failure;
461 }
462
463 memset(sw_params, 0, sizeof(*sw_params));
464 if (runtime->oss.trigger) {
465 sw_params->start_threshold = 1;
466 } else {
467 sw_params->start_threshold = runtime->boundary;
468 }
469 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
470 sw_params->stop_threshold = runtime->boundary;
471 else
472 sw_params->stop_threshold = runtime->buffer_size;
473 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
474 sw_params->period_step = 1;
475 sw_params->sleep_min = 0;
Takashi Iwai004e6532005-04-12 17:33:59 +0200476 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
477 1 : runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sw_params->xfer_align = 1;
479 if (atomic_read(&runtime->mmap_count) ||
Takashi Iwai060d77b2006-03-27 16:44:52 +0200480 substream->oss.setup.nosilence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sw_params->silence_threshold = 0;
482 sw_params->silence_size = 0;
483 } else {
484 snd_pcm_uframes_t frames;
485 frames = runtime->period_size + 16;
486 if (frames > runtime->buffer_size)
487 frames = runtime->buffer_size;
488 sw_params->silence_threshold = frames;
489 sw_params->silence_size = frames;
490 }
491
492 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
493 snd_printd("SW_PARAMS failed: %i\n", err);
494 goto failure;
495 }
496
497 runtime->oss.periods = params_periods(sparams);
498 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
499 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100500#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (runtime->oss.plugin_first) {
502 err = snd_pcm_plug_alloc(substream, oss_period_size);
503 if (err < 0)
504 goto failure;
505 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 oss_period_size *= oss_frame_size;
508
509 oss_buffer_size = oss_period_size * runtime->oss.periods;
510 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
511
512 runtime->oss.period_bytes = oss_period_size;
513 runtime->oss.buffer_bytes = oss_buffer_size;
514
515 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
516 runtime->oss.period_bytes,
517 runtime->oss.buffer_bytes);
518 pdprintf("slave: period_size = %i, buffer_size = %i\n",
519 params_period_size(sparams),
520 params_buffer_size(sparams));
521
522 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
523 runtime->oss.channels = params_channels(params);
524 runtime->oss.rate = params_rate(params);
525
526 runtime->oss.params = 0;
527 runtime->oss.prepare = 1;
528 vfree(runtime->oss.buffer);
529 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
530 runtime->oss.buffer_used = 0;
531 if (runtime->dma_area)
532 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
533
534 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
535
536 err = 0;
537failure:
538 kfree(sw_params);
539 kfree(params);
540 kfree(sparams);
541 return err;
542}
543
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100544static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100547 struct snd_pcm_substream *asubstream = NULL, *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 for (idx = 0; idx < 2; idx++) {
550 substream = pcm_oss_file->streams[idx];
551 if (substream == NULL)
552 continue;
553 if (asubstream == NULL)
554 asubstream = substream;
555 if (substream->runtime->oss.params) {
556 err = snd_pcm_oss_change_params(substream);
557 if (err < 0)
558 return err;
559 }
560 }
561 snd_assert(asubstream != NULL, return -EIO);
562 if (r_substream)
563 *r_substream = asubstream;
564 return 0;
565}
566
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100567static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100570 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
573 if (err < 0) {
574 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
575 return err;
576 }
577 runtime->oss.prepare = 0;
578 runtime->oss.prev_hw_ptr_interrupt = 0;
579 runtime->oss.period_ptr = 0;
580 runtime->oss.buffer_used = 0;
581
582 return 0;
583}
584
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100585static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100587 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int err;
589
590 if (substream == NULL)
591 return 0;
592 runtime = substream->runtime;
593 if (runtime->oss.params) {
594 err = snd_pcm_oss_change_params(substream);
595 if (err < 0)
596 return err;
597 }
598 if (runtime->oss.prepare) {
599 err = snd_pcm_oss_prepare(substream);
600 if (err < 0)
601 return err;
602 }
603 return 0;
604}
605
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100606static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100608 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 snd_pcm_uframes_t frames;
610 int err = 0;
611
612 while (1) {
613 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
614 if (err < 0)
615 break;
616 runtime = substream->runtime;
617 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
618 break;
619 /* in case of overrun, skip whole periods like OSS/Linux driver does */
620 /* until avail(delay) <= buffer_size */
621 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
622 frames /= runtime->period_size;
623 frames *= runtime->period_size;
624 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
625 if (err < 0)
626 break;
627 }
628 return err;
629}
630
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100631snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100633 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int ret;
635 while (1) {
636 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
637 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
638#ifdef OSS_DEBUG
639 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
640 printk("pcm_oss: write: recovering from XRUN\n");
641 else
642 printk("pcm_oss: write: recovering from SUSPEND\n");
643#endif
644 ret = snd_pcm_oss_prepare(substream);
645 if (ret < 0)
646 break;
647 }
648 if (in_kernel) {
649 mm_segment_t fs;
650 fs = snd_enter_user();
651 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
652 snd_leave_user(fs);
653 } else {
654 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
655 }
656 if (ret != -EPIPE && ret != -ESTRPIPE)
657 break;
658 /* test, if we can't store new data, because the stream */
659 /* has not been started */
660 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
661 return -EAGAIN;
662 }
663 return ret;
664}
665
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100666snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100668 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 snd_pcm_sframes_t delay;
670 int ret;
671 while (1) {
672 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
673 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
674#ifdef OSS_DEBUG
675 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
676 printk("pcm_oss: read: recovering from XRUN\n");
677 else
678 printk("pcm_oss: read: recovering from SUSPEND\n");
679#endif
680 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
681 if (ret < 0)
682 break;
683 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
684 ret = snd_pcm_oss_prepare(substream);
685 if (ret < 0)
686 break;
687 }
688 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
689 if (ret < 0)
690 break;
691 if (in_kernel) {
692 mm_segment_t fs;
693 fs = snd_enter_user();
694 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
695 snd_leave_user(fs);
696 } else {
697 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
698 }
699 if (ret == -EPIPE) {
700 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
701 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
702 if (ret < 0)
703 break;
704 }
705 continue;
706 }
707 if (ret != -ESTRPIPE)
708 break;
709 }
710 return ret;
711}
712
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100713snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100715 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int ret;
717 while (1) {
718 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
719 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
720#ifdef OSS_DEBUG
721 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
722 printk("pcm_oss: writev: recovering from XRUN\n");
723 else
724 printk("pcm_oss: writev: recovering from SUSPEND\n");
725#endif
726 ret = snd_pcm_oss_prepare(substream);
727 if (ret < 0)
728 break;
729 }
730 if (in_kernel) {
731 mm_segment_t fs;
732 fs = snd_enter_user();
733 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
734 snd_leave_user(fs);
735 } else {
736 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
737 }
738 if (ret != -EPIPE && ret != -ESTRPIPE)
739 break;
740
741 /* test, if we can't store new data, because the stream */
742 /* has not been started */
743 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
744 return -EAGAIN;
745 }
746 return ret;
747}
748
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100749snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100751 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int ret;
753 while (1) {
754 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
755 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
756#ifdef OSS_DEBUG
757 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
758 printk("pcm_oss: readv: recovering from XRUN\n");
759 else
760 printk("pcm_oss: readv: recovering from SUSPEND\n");
761#endif
762 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
763 if (ret < 0)
764 break;
765 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
766 ret = snd_pcm_oss_prepare(substream);
767 if (ret < 0)
768 break;
769 }
770 if (in_kernel) {
771 mm_segment_t fs;
772 fs = snd_enter_user();
773 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
774 snd_leave_user(fs);
775 } else {
776 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
777 }
778 if (ret != -EPIPE && ret != -ESTRPIPE)
779 break;
780 }
781 return ret;
782}
783
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100784static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100786 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100788#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100790 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
792 if (!in_kernel) {
793 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
794 return -EFAULT;
795 buf = runtime->oss.buffer;
796 }
797 frames = bytes / oss_frame_bytes;
798 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
799 if (frames1 < 0)
800 return frames1;
801 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
802 if (frames1 <= 0)
803 return frames1;
804 bytes = frames1 * oss_frame_bytes;
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100805 } else
806#endif
807 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 frames = bytes_to_frames(runtime, bytes);
809 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
810 if (frames1 <= 0)
811 return frames1;
812 bytes = frames_to_bytes(runtime, frames1);
813 }
814 return bytes;
815}
816
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100817static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 size_t xfer = 0;
820 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100821 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (atomic_read(&runtime->mmap_count))
824 return -ENXIO;
825
826 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
827 return tmp;
828 while (bytes > 0) {
829 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
830 tmp = bytes;
831 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
832 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
833 if (tmp > 0) {
834 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
835 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
836 }
837 runtime->oss.buffer_used += tmp;
838 buf += tmp;
839 bytes -= tmp;
840 xfer += tmp;
Takashi Iwai060d77b2006-03-27 16:44:52 +0200841 if (substream->oss.setup.partialfrag ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 runtime->oss.buffer_used == runtime->oss.period_bytes) {
843 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
844 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
845 if (tmp <= 0)
846 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
847 runtime->oss.bytes += tmp;
848 runtime->oss.period_ptr += tmp;
849 runtime->oss.period_ptr %= runtime->oss.period_bytes;
850 if (runtime->oss.period_ptr == 0 ||
851 runtime->oss.period_ptr == runtime->oss.buffer_used)
852 runtime->oss.buffer_used = 0;
853 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
854 return xfer > 0 ? xfer : -EAGAIN;
855 }
856 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +0200857 tmp = snd_pcm_oss_write2(substream,
858 (const char __force *)buf,
859 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (tmp <= 0)
861 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
862 runtime->oss.bytes += tmp;
863 buf += tmp;
864 bytes -= tmp;
865 xfer += tmp;
866 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
867 tmp != runtime->oss.period_bytes)
868 break;
869 }
870 }
871 return xfer;
872}
873
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100874static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100876 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100878#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 char __user *final_dst = (char __user *)buf;
880 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100881 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
883 if (!in_kernel)
884 buf = runtime->oss.buffer;
885 frames = bytes / oss_frame_bytes;
886 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
887 if (frames1 < 0)
888 return frames1;
889 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
890 if (frames1 <= 0)
891 return frames1;
892 bytes = frames1 * oss_frame_bytes;
893 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
894 return -EFAULT;
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100895 } else
896#endif
897 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 frames = bytes_to_frames(runtime, bytes);
899 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
900 if (frames1 <= 0)
901 return frames1;
902 bytes = frames_to_bytes(runtime, frames1);
903 }
904 return bytes;
905}
906
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100907static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 size_t xfer = 0;
910 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100911 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (atomic_read(&runtime->mmap_count))
914 return -ENXIO;
915
916 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
917 return tmp;
918 while (bytes > 0) {
919 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
920 if (runtime->oss.buffer_used == 0) {
921 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
922 if (tmp <= 0)
923 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
924 runtime->oss.bytes += tmp;
925 runtime->oss.period_ptr = tmp;
926 runtime->oss.buffer_used = tmp;
927 }
928 tmp = bytes;
929 if ((size_t) tmp > runtime->oss.buffer_used)
930 tmp = runtime->oss.buffer_used;
931 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
932 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
933 buf += tmp;
934 bytes -= tmp;
935 xfer += tmp;
936 runtime->oss.buffer_used -= tmp;
937 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +0200938 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
939 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (tmp <= 0)
941 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
942 runtime->oss.bytes += tmp;
943 buf += tmp;
944 bytes -= tmp;
945 xfer += tmp;
946 }
947 }
948 return xfer;
949}
950
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100951static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100953 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
956 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +0200957 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 substream->runtime->oss.prepare = 1;
959 }
960 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
961 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +0200962 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 substream->runtime->oss.prepare = 1;
964 }
965 return 0;
966}
967
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100968static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100970 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int err;
972
973 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
974 if (substream != NULL) {
975 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
976 return err;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +0200977 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 /* note: all errors from the start action are ignored */
980 /* OSS apps do not know, how to handle them */
981 return 0;
982}
983
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100984static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100986 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ssize_t result = 0;
988 long res;
989 wait_queue_t wait;
990
991 runtime = substream->runtime;
992 init_waitqueue_entry(&wait, current);
993 add_wait_queue(&runtime->sleep, &wait);
994#ifdef OSS_DEBUG
995 printk("sync1: size = %li\n", size);
996#endif
997 while (1) {
998 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
999 if (result > 0) {
1000 runtime->oss.buffer_used = 0;
1001 result = 0;
1002 break;
1003 }
1004 if (result != 0 && result != -EAGAIN)
1005 break;
1006 result = 0;
1007 set_current_state(TASK_INTERRUPTIBLE);
1008 snd_pcm_stream_lock_irq(substream);
1009 res = runtime->status->state;
1010 snd_pcm_stream_unlock_irq(substream);
1011 if (res != SNDRV_PCM_STATE_RUNNING) {
1012 set_current_state(TASK_RUNNING);
1013 break;
1014 }
1015 res = schedule_timeout(10 * HZ);
1016 if (signal_pending(current)) {
1017 result = -ERESTARTSYS;
1018 break;
1019 }
1020 if (res == 0) {
1021 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1022 result = -EIO;
1023 break;
1024 }
1025 }
1026 remove_wait_queue(&runtime->sleep, &wait);
1027 return result;
1028}
1029
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001030static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int err = 0;
1033 unsigned int saved_f_flags;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001034 struct snd_pcm_substream *substream;
1035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 snd_pcm_format_t format;
1037 unsigned long width;
1038 size_t size;
1039
1040 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1041 if (substream != NULL) {
1042 runtime = substream->runtime;
1043 if (atomic_read(&runtime->mmap_count))
1044 goto __direct;
1045 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1046 return err;
1047 format = snd_pcm_oss_format_from(runtime->oss.format);
1048 width = snd_pcm_format_physical_width(format);
1049 if (runtime->oss.buffer_used > 0) {
1050#ifdef OSS_DEBUG
1051 printk("sync: buffer_used\n");
1052#endif
1053 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1054 snd_pcm_format_set_silence(format,
1055 runtime->oss.buffer + runtime->oss.buffer_used,
1056 size);
1057 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1058 if (err < 0)
1059 return err;
1060 } else if (runtime->oss.period_ptr > 0) {
1061#ifdef OSS_DEBUG
1062 printk("sync: period_ptr\n");
1063#endif
1064 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1065 snd_pcm_format_set_silence(format,
1066 runtime->oss.buffer,
1067 size * 8 / width);
1068 err = snd_pcm_oss_sync1(substream, size);
1069 if (err < 0)
1070 return err;
1071 }
1072 /*
1073 * The ALSA's period might be a bit large than OSS one.
1074 * Fill the remain portion of ALSA period with zeros.
1075 */
1076 size = runtime->control->appl_ptr % runtime->period_size;
1077 if (size > 0) {
1078 size = runtime->period_size - size;
1079 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1080 size = (runtime->frame_bits * size) / 8;
1081 while (size > 0) {
1082 mm_segment_t fs;
1083 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1084 size -= size1;
1085 size1 *= 8;
1086 size1 /= runtime->sample_bits;
1087 snd_pcm_format_set_silence(runtime->format,
1088 runtime->oss.buffer,
1089 size1);
1090 fs = snd_enter_user();
1091 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1092 snd_leave_user(fs);
1093 }
1094 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1095 void __user *buffers[runtime->channels];
1096 memset(buffers, 0, runtime->channels * sizeof(void *));
1097 snd_pcm_lib_writev(substream, buffers, size);
1098 }
1099 }
1100 /*
1101 * finish sync: drain the buffer
1102 */
1103 __direct:
1104 saved_f_flags = substream->ffile->f_flags;
1105 substream->ffile->f_flags &= ~O_NONBLOCK;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001106 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 substream->ffile->f_flags = saved_f_flags;
1108 if (err < 0)
1109 return err;
1110 runtime->oss.prepare = 1;
1111 }
1112
1113 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1114 if (substream != NULL) {
1115 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1116 return err;
1117 runtime = substream->runtime;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001118 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (err < 0)
1120 return err;
1121 runtime->oss.buffer_used = 0;
1122 runtime->oss.prepare = 1;
1123 }
1124 return 0;
1125}
1126
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001127static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 int idx;
1130
1131 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001132 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1133 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (substream == NULL)
1135 continue;
1136 runtime = substream->runtime;
1137 if (rate < 1000)
1138 rate = 1000;
1139 else if (rate > 192000)
1140 rate = 192000;
1141 if (runtime->oss.rate != rate) {
1142 runtime->oss.params = 1;
1143 runtime->oss.rate = rate;
1144 }
1145 }
1146 return snd_pcm_oss_get_rate(pcm_oss_file);
1147}
1148
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001149static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001151 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int err;
1153
1154 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1155 return err;
1156 return substream->runtime->oss.rate;
1157}
1158
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001159static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 int idx;
1162 if (channels < 1)
1163 channels = 1;
1164 if (channels > 128)
1165 return -EINVAL;
1166 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001167 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1168 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (substream == NULL)
1170 continue;
1171 runtime = substream->runtime;
1172 if (runtime->oss.channels != channels) {
1173 runtime->oss.params = 1;
1174 runtime->oss.channels = channels;
1175 }
1176 }
1177 return snd_pcm_oss_get_channels(pcm_oss_file);
1178}
1179
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001180static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001182 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int err;
1184
1185 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1186 return err;
1187 return substream->runtime->oss.channels;
1188}
1189
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001190static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001192 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int err;
1194
1195 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1196 return err;
1197 return substream->runtime->oss.period_bytes;
1198}
1199
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001200static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001202 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int err;
1204 int direct;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001205 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unsigned int formats = 0;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001207 struct snd_mask format_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int fmt;
1209
1210 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1211 return err;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001212 if (atomic_read(&substream->runtime->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001214 else
1215 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!direct)
1217 return AFMT_MU_LAW | AFMT_U8 |
1218 AFMT_S16_LE | AFMT_S16_BE |
1219 AFMT_S8 | AFMT_U16_LE |
1220 AFMT_U16_BE;
1221 params = kmalloc(sizeof(*params), GFP_KERNEL);
1222 if (!params)
1223 return -ENOMEM;
1224 _snd_pcm_hw_params_any(params);
1225 err = snd_pcm_hw_refine(substream, params);
1226 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1227 kfree(params);
1228 snd_assert(err >= 0, return err);
1229 for (fmt = 0; fmt < 32; ++fmt) {
1230 if (snd_mask_test(&format_mask, fmt)) {
1231 int f = snd_pcm_oss_format_to(fmt);
1232 if (f >= 0)
1233 formats |= f;
1234 }
1235 }
1236 return formats;
1237}
1238
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001239static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 int formats, idx;
1242
1243 if (format != AFMT_QUERY) {
1244 formats = snd_pcm_oss_get_formats(pcm_oss_file);
Steven Finney5c59e092006-04-13 12:49:31 +02001245 if (formats < 0)
1246 return formats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!(formats & format))
1248 format = AFMT_U8;
1249 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001250 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1251 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (substream == NULL)
1253 continue;
1254 runtime = substream->runtime;
1255 if (runtime->oss.format != format) {
1256 runtime->oss.params = 1;
1257 runtime->oss.format = format;
1258 }
1259 }
1260 }
1261 return snd_pcm_oss_get_format(pcm_oss_file);
1262}
1263
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001264static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001266 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int err;
1268
1269 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1270 return err;
1271 return substream->runtime->oss.format;
1272}
1273
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001274static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001276 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (substream == NULL)
1279 return 0;
1280 runtime = substream->runtime;
1281 if (subdivide == 0) {
1282 subdivide = runtime->oss.subdivision;
1283 if (subdivide == 0)
1284 subdivide = 1;
1285 return subdivide;
1286 }
1287 if (runtime->oss.subdivision || runtime->oss.fragshift)
1288 return -EINVAL;
1289 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1290 subdivide != 8 && subdivide != 16)
1291 return -EINVAL;
1292 runtime->oss.subdivision = subdivide;
1293 runtime->oss.params = 1;
1294 return subdivide;
1295}
1296
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001297static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 int err = -EINVAL, idx;
1300
1301 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001302 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (substream == NULL)
1304 continue;
1305 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1306 return err;
1307 }
1308 return err;
1309}
1310
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001311static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001313 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (substream == NULL)
1316 return 0;
1317 runtime = substream->runtime;
1318 if (runtime->oss.subdivision || runtime->oss.fragshift)
1319 return -EINVAL;
1320 runtime->oss.fragshift = val & 0xffff;
1321 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1322 if (runtime->oss.fragshift < 4) /* < 16 */
1323 runtime->oss.fragshift = 4;
1324 if (runtime->oss.maxfrags < 2)
1325 runtime->oss.maxfrags = 2;
1326 runtime->oss.params = 1;
1327 return 0;
1328}
1329
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001330static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 int err = -EINVAL, idx;
1333
1334 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001335 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (substream == NULL)
1337 continue;
1338 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1339 return err;
1340 }
1341 return err;
1342}
1343
1344static int snd_pcm_oss_nonblock(struct file * file)
1345{
1346 file->f_flags |= O_NONBLOCK;
1347 return 0;
1348}
1349
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001350static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352
1353 if (substream == NULL) {
1354 res &= ~DSP_CAP_DUPLEX;
1355 return res;
1356 }
1357#ifdef DSP_CAP_MULTI
1358 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1359 if (substream->pstr->substream_count > 1)
1360 res |= DSP_CAP_MULTI;
1361#endif
1362 /* DSP_CAP_REALTIME is set all times: */
1363 /* all ALSA drivers can return actual pointer in ring buffer */
1364#if defined(DSP_CAP_REALTIME) && 0
1365 {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001366 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1368 res &= ~DSP_CAP_REALTIME;
1369 }
1370#endif
1371 return res;
1372}
1373
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001374static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 int result, idx;
1377
1378 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1379 for (idx = 0; idx < 2; idx++) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001380 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 result = snd_pcm_oss_get_caps1(substream, result);
1382 }
1383 result |= 0x0001; /* revision - same as SB AWE 64 */
1384 return result;
1385}
1386
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001387static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001389 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 snd_pcm_uframes_t appl_ptr;
1391 appl_ptr = hw_ptr + runtime->buffer_size;
1392 appl_ptr %= runtime->boundary;
1393 runtime->control->appl_ptr = appl_ptr;
1394}
1395
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001396static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001398 struct snd_pcm_runtime *runtime;
1399 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int err, cmd;
1401
1402#ifdef OSS_DEBUG
1403 printk("pcm_oss: trigger = 0x%x\n", trigger);
1404#endif
1405
1406 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1407 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1408
1409 if (psubstream) {
1410 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1411 return err;
1412 }
1413 if (csubstream) {
1414 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1415 return err;
1416 }
1417 if (psubstream) {
1418 runtime = psubstream->runtime;
1419 if (trigger & PCM_ENABLE_OUTPUT) {
1420 if (runtime->oss.trigger)
1421 goto _skip1;
1422 if (atomic_read(&psubstream->runtime->mmap_count))
1423 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1424 runtime->oss.trigger = 1;
1425 runtime->start_threshold = 1;
1426 cmd = SNDRV_PCM_IOCTL_START;
1427 } else {
1428 if (!runtime->oss.trigger)
1429 goto _skip1;
1430 runtime->oss.trigger = 0;
1431 runtime->start_threshold = runtime->boundary;
1432 cmd = SNDRV_PCM_IOCTL_DROP;
1433 runtime->oss.prepare = 1;
1434 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001435 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (err < 0)
1437 return err;
1438 }
1439 _skip1:
1440 if (csubstream) {
1441 runtime = csubstream->runtime;
1442 if (trigger & PCM_ENABLE_INPUT) {
1443 if (runtime->oss.trigger)
1444 goto _skip2;
1445 runtime->oss.trigger = 1;
1446 runtime->start_threshold = 1;
1447 cmd = SNDRV_PCM_IOCTL_START;
1448 } else {
1449 if (!runtime->oss.trigger)
1450 goto _skip2;
1451 runtime->oss.trigger = 0;
1452 runtime->start_threshold = runtime->boundary;
1453 cmd = SNDRV_PCM_IOCTL_DROP;
1454 runtime->oss.prepare = 1;
1455 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001456 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (err < 0)
1458 return err;
1459 }
1460 _skip2:
1461 return 0;
1462}
1463
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001464static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001466 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int result = 0;
1468
1469 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1470 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1471 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1472 result |= PCM_ENABLE_OUTPUT;
1473 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1474 result |= PCM_ENABLE_INPUT;
1475 return result;
1476}
1477
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001478static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001480 struct snd_pcm_substream *substream;
1481 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 snd_pcm_sframes_t delay;
1483 int err;
1484
1485 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1486 if (substream == NULL)
1487 return -EINVAL;
1488 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1489 return err;
1490 runtime = substream->runtime;
1491 if (runtime->oss.params || runtime->oss.prepare)
1492 return 0;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001493 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (err == -EPIPE)
1495 delay = 0; /* hack for broken OSS applications */
1496 else if (err < 0)
1497 return err;
1498 return snd_pcm_oss_bytes(substream, delay);
1499}
1500
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001501static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001503 struct snd_pcm_substream *substream;
1504 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 snd_pcm_sframes_t delay;
1506 int fixup;
1507 struct count_info info;
1508 int err;
1509
1510 if (_info == NULL)
1511 return -EFAULT;
1512 substream = pcm_oss_file->streams[stream];
1513 if (substream == NULL)
1514 return -EINVAL;
1515 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1516 return err;
1517 runtime = substream->runtime;
1518 if (runtime->oss.params || runtime->oss.prepare) {
1519 memset(&info, 0, sizeof(info));
1520 if (copy_to_user(_info, &info, sizeof(info)))
1521 return -EFAULT;
1522 return 0;
1523 }
1524 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1525 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1526 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1527 err = 0;
1528 delay = 0;
1529 fixup = 0;
1530 } else {
1531 fixup = runtime->oss.buffer_used;
1532 }
1533 } else {
1534 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1535 fixup = -runtime->oss.buffer_used;
1536 }
1537 if (err < 0)
1538 return err;
1539 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1540 if (atomic_read(&runtime->mmap_count)) {
1541 snd_pcm_sframes_t n;
1542 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1543 if (n < 0)
1544 n += runtime->boundary;
1545 info.blocks = n / runtime->period_size;
1546 runtime->oss.prev_hw_ptr_interrupt = delay;
1547 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1548 snd_pcm_oss_simulate_fill(substream, delay);
1549 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1550 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02001551 delay = snd_pcm_oss_bytes(substream, delay);
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02001552 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02001553 if (substream->oss.setup.buggyptr)
Takashi Iwai10f69f92005-09-08 13:48:34 +02001554 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
1555 else
1556 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02001558 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02001559 delay += fixup;
1560 info.blocks = delay / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 if (copy_to_user(_info, &info, sizeof(info)))
1565 return -EFAULT;
1566 return 0;
1567}
1568
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001569static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001571 struct snd_pcm_substream *substream;
1572 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 snd_pcm_sframes_t avail;
1574 int fixup;
1575 struct audio_buf_info info;
1576 int err;
1577
1578 if (_info == NULL)
1579 return -EFAULT;
1580 substream = pcm_oss_file->streams[stream];
1581 if (substream == NULL)
1582 return -EINVAL;
1583 runtime = substream->runtime;
1584
1585 if (runtime->oss.params &&
1586 (err = snd_pcm_oss_change_params(substream)) < 0)
1587 return err;
1588
1589 info.fragsize = runtime->oss.period_bytes;
1590 info.fragstotal = runtime->periods;
1591 if (runtime->oss.prepare) {
1592 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1593 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1594 info.fragments = runtime->oss.periods;
1595 } else {
1596 info.bytes = 0;
1597 info.fragments = 0;
1598 }
1599 } else {
1600 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1601 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1602 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1603 avail = runtime->buffer_size;
1604 err = 0;
1605 fixup = 0;
1606 } else {
1607 avail = runtime->buffer_size - avail;
1608 fixup = -runtime->oss.buffer_used;
1609 }
1610 } else {
1611 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1612 fixup = runtime->oss.buffer_used;
1613 }
1614 if (err < 0)
1615 return err;
1616 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1617 info.fragments = info.bytes / runtime->oss.period_bytes;
1618 }
1619
1620#ifdef OSS_DEBUG
1621 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1622#endif
1623 if (copy_to_user(_info, &info, sizeof(info)))
1624 return -EFAULT;
1625 return 0;
1626}
1627
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001628static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 // it won't be probably implemented
1631 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1632 return -EINVAL;
1633}
1634
Takashi Iwai060d77b2006-03-27 16:44:52 +02001635static const char *strip_task_path(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Takashi Iwai060d77b2006-03-27 16:44:52 +02001637 const char *ptr, *ptrl = NULL;
1638 for (ptr = path; *ptr; ptr++) {
1639 if (*ptr == '/')
1640 ptrl = ptr + 1;
1641 }
1642 return ptrl;
1643}
1644
1645static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
1646 const char *task_name,
1647 struct snd_pcm_oss_setup *rsetup)
1648{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001649 struct snd_pcm_oss_setup *setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001651 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02001652 do {
1653 for (setup = pcm->streams[stream].oss.setup_list; setup;
1654 setup = setup->next) {
1655 if (!strcmp(setup->task_name, task_name))
1656 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02001658 } while ((task_name = strip_task_path(task_name)) != NULL);
1659 out:
1660 if (setup)
1661 *rsetup = *setup;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001662 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001665static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
1666{
1667 struct snd_pcm_runtime *runtime;
1668 runtime = substream->runtime;
1669 vfree(runtime->oss.buffer);
1670 runtime->oss.buffer = NULL;
1671#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1672 snd_pcm_oss_plugin_clear(substream);
1673#endif
1674 substream->oss.oss = 0;
1675}
1676
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001677static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
1678 struct snd_pcm_oss_setup *setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int minor)
1680{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001681 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 substream->oss.oss = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001684 substream->oss.setup = *setup;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001685 if (setup->nonblock)
1686 substream->ffile->f_flags |= O_NONBLOCK;
Takashi Iwai15762742006-04-06 19:47:42 +02001687 else if (setup->block)
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001688 substream->ffile->f_flags &= ~O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 runtime = substream->runtime;
1690 runtime->oss.params = 1;
1691 runtime->oss.trigger = 1;
1692 runtime->oss.rate = 8000;
1693 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1694 case SNDRV_MINOR_OSS_PCM_8:
1695 runtime->oss.format = AFMT_U8;
1696 break;
1697 case SNDRV_MINOR_OSS_PCM_16:
1698 runtime->oss.format = AFMT_S16_LE;
1699 break;
1700 default:
1701 runtime->oss.format = AFMT_MU_LAW;
1702 }
1703 runtime->oss.channels = 1;
1704 runtime->oss.fragshift = 0;
1705 runtime->oss.maxfrags = 0;
1706 runtime->oss.subdivision = 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001707 substream->pcm_release = snd_pcm_oss_release_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001710static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int cidx;
1713 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1714 for (cidx = 0; cidx < 2; ++cidx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001715 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001716 if (substream)
1717 snd_pcm_release_substream(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 kfree(pcm_oss_file);
1720 return 0;
1721}
1722
1723static int snd_pcm_oss_open_file(struct file *file,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001724 struct snd_pcm *pcm,
1725 struct snd_pcm_oss_file **rpcm_oss_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int minor,
Takashi Iwai060d77b2006-03-27 16:44:52 +02001727 struct snd_pcm_oss_setup *setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001729 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001730 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001731 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 unsigned int f_mode = file->f_mode;
1733
1734 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1735 *rpcm_oss_file = NULL;
1736
Takashi Iwaica2c0962005-09-09 14:20:23 +02001737 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (pcm_oss_file == NULL)
1739 return -ENOMEM;
1740
1741 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1742 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1743 f_mode = FMODE_WRITE;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001744
1745 for (idx = 0; idx < 2; idx++) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02001746 if (setup[idx].disable)
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001747 continue;
1748 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
1749 if (! (f_mode & FMODE_WRITE))
1750 continue;
1751 } else {
1752 if (! (f_mode & FMODE_READ))
1753 continue;
1754 }
1755 err = snd_pcm_open_substream(pcm, idx, file, &substream);
1756 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 snd_pcm_oss_release_file(pcm_oss_file);
1758 return err;
1759 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001760
1761 pcm_oss_file->streams[idx] = substream;
Takashi Iwai15762742006-04-06 19:47:42 +02001762 substream->file = pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001763 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
OGAWA Hirofumibbdc1b72006-04-06 19:42:40 +02001766 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 snd_pcm_oss_release_file(pcm_oss_file);
1768 return -EINVAL;
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 file->private_data = pcm_oss_file;
1772 *rpcm_oss_file = pcm_oss_file;
1773 return 0;
1774}
1775
1776
Takashi Iwai93f2e372005-10-10 11:51:55 +02001777static int snd_task_name(struct task_struct *task, char *name, size_t size)
1778{
1779 unsigned int idx;
1780
1781 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
1782 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
1783 name[idx] = task->comm[idx];
1784 name[idx] = '\0';
1785 return 0;
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int err;
1791 char task_name[32];
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001792 struct snd_pcm *pcm;
1793 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001794 struct snd_pcm_oss_setup setup[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int nonblock;
1796 wait_queue_t wait;
1797
Clemens Ladischf87135f2005-11-20 14:06:59 +01001798 pcm = snd_lookup_oss_minor_data(iminor(inode),
1799 SNDRV_OSS_DEVICE_TYPE_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (pcm == NULL) {
1801 err = -ENODEV;
1802 goto __error1;
1803 }
1804 err = snd_card_file_add(pcm->card, file);
1805 if (err < 0)
1806 goto __error1;
1807 if (!try_module_get(pcm->card->module)) {
1808 err = -EFAULT;
1809 goto __error2;
1810 }
1811 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1812 err = -EFAULT;
1813 goto __error;
1814 }
Takashi Iwai15762742006-04-06 19:47:42 +02001815 memset(setup, 0, sizeof(setup));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (file->f_mode & FMODE_WRITE)
Takashi Iwai060d77b2006-03-27 16:44:52 +02001817 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1818 task_name, &setup[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (file->f_mode & FMODE_READ)
Takashi Iwai060d77b2006-03-27 16:44:52 +02001820 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
1821 task_name, &setup[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 nonblock = !!(file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (!nonblock)
1825 nonblock = nonblock_open;
1826
1827 init_waitqueue_entry(&wait, current);
1828 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001829 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 while (1) {
1831 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001832 iminor(inode), setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (err >= 0)
1834 break;
1835 if (err == -EAGAIN) {
1836 if (nonblock) {
1837 err = -EBUSY;
1838 break;
1839 }
1840 } else
1841 break;
1842 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001843 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001845 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (signal_pending(current)) {
1847 err = -ERESTARTSYS;
1848 break;
1849 }
1850 }
1851 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001852 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (err < 0)
1854 goto __error;
1855 return err;
1856
1857 __error:
1858 module_put(pcm->card->module);
1859 __error2:
1860 snd_card_file_remove(pcm->card, file);
1861 __error1:
1862 return err;
1863}
1864
1865static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1866{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001867 struct snd_pcm *pcm;
1868 struct snd_pcm_substream *substream;
1869 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 pcm_oss_file = file->private_data;
1872 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1873 if (substream == NULL)
1874 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1875 snd_assert(substream != NULL, return -ENXIO);
1876 pcm = substream->pcm;
1877 snd_pcm_oss_sync(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001878 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 snd_pcm_oss_release_file(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001880 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 wake_up(&pcm->open_wait);
1882 module_put(pcm->card->module);
1883 snd_card_file_remove(pcm->card, file);
1884 return 0;
1885}
1886
1887static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1888{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001889 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int __user *p = (int __user *)arg;
1891 int res;
1892
1893 pcm_oss_file = file->private_data;
1894 if (cmd == OSS_GETVERSION)
1895 return put_user(SNDRV_OSS_VERSION, p);
1896 if (cmd == OSS_ALSAEMULVER)
1897 return put_user(1, p);
1898#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1899 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001900 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int idx;
1902 for (idx = 0; idx < 2; ++idx) {
1903 substream = pcm_oss_file->streams[idx];
1904 if (substream != NULL)
1905 break;
1906 }
1907 snd_assert(substream != NULL, return -ENXIO);
1908 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1909 }
1910#endif
1911 if (((cmd >> 8) & 0xff) != 'P')
1912 return -EINVAL;
1913#ifdef OSS_DEBUG
1914 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1915#endif
1916 switch (cmd) {
1917 case SNDCTL_DSP_RESET:
1918 return snd_pcm_oss_reset(pcm_oss_file);
1919 case SNDCTL_DSP_SYNC:
1920 return snd_pcm_oss_sync(pcm_oss_file);
1921 case SNDCTL_DSP_SPEED:
1922 if (get_user(res, p))
1923 return -EFAULT;
1924 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1925 return res;
1926 return put_user(res, p);
1927 case SOUND_PCM_READ_RATE:
1928 res = snd_pcm_oss_get_rate(pcm_oss_file);
1929 if (res < 0)
1930 return res;
1931 return put_user(res, p);
1932 case SNDCTL_DSP_STEREO:
1933 if (get_user(res, p))
1934 return -EFAULT;
1935 res = res > 0 ? 2 : 1;
1936 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1937 return res;
1938 return put_user(--res, p);
1939 case SNDCTL_DSP_GETBLKSIZE:
1940 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1941 if (res < 0)
1942 return res;
1943 return put_user(res, p);
1944 case SNDCTL_DSP_SETFMT:
1945 if (get_user(res, p))
1946 return -EFAULT;
1947 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1948 if (res < 0)
1949 return res;
1950 return put_user(res, p);
1951 case SOUND_PCM_READ_BITS:
1952 res = snd_pcm_oss_get_format(pcm_oss_file);
1953 if (res < 0)
1954 return res;
1955 return put_user(res, p);
1956 case SNDCTL_DSP_CHANNELS:
1957 if (get_user(res, p))
1958 return -EFAULT;
1959 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1960 if (res < 0)
1961 return res;
1962 return put_user(res, p);
1963 case SOUND_PCM_READ_CHANNELS:
1964 res = snd_pcm_oss_get_channels(pcm_oss_file);
1965 if (res < 0)
1966 return res;
1967 return put_user(res, p);
1968 case SOUND_PCM_WRITE_FILTER:
1969 case SOUND_PCM_READ_FILTER:
1970 return -EIO;
1971 case SNDCTL_DSP_POST:
1972 return snd_pcm_oss_post(pcm_oss_file);
1973 case SNDCTL_DSP_SUBDIVIDE:
1974 if (get_user(res, p))
1975 return -EFAULT;
1976 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
1977 if (res < 0)
1978 return res;
1979 return put_user(res, p);
1980 case SNDCTL_DSP_SETFRAGMENT:
1981 if (get_user(res, p))
1982 return -EFAULT;
1983 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
1984 case SNDCTL_DSP_GETFMTS:
1985 res = snd_pcm_oss_get_formats(pcm_oss_file);
1986 if (res < 0)
1987 return res;
1988 return put_user(res, p);
1989 case SNDCTL_DSP_GETOSPACE:
1990 case SNDCTL_DSP_GETISPACE:
1991 return snd_pcm_oss_get_space(pcm_oss_file,
1992 cmd == SNDCTL_DSP_GETISPACE ?
1993 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
1994 (struct audio_buf_info __user *) arg);
1995 case SNDCTL_DSP_NONBLOCK:
1996 return snd_pcm_oss_nonblock(file);
1997 case SNDCTL_DSP_GETCAPS:
1998 res = snd_pcm_oss_get_caps(pcm_oss_file);
1999 if (res < 0)
2000 return res;
2001 return put_user(res, p);
2002 case SNDCTL_DSP_GETTRIGGER:
2003 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2004 if (res < 0)
2005 return res;
2006 return put_user(res, p);
2007 case SNDCTL_DSP_SETTRIGGER:
2008 if (get_user(res, p))
2009 return -EFAULT;
2010 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2011 case SNDCTL_DSP_GETIPTR:
2012 case SNDCTL_DSP_GETOPTR:
2013 return snd_pcm_oss_get_ptr(pcm_oss_file,
2014 cmd == SNDCTL_DSP_GETIPTR ?
2015 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2016 (struct count_info __user *) arg);
2017 case SNDCTL_DSP_MAPINBUF:
2018 case SNDCTL_DSP_MAPOUTBUF:
2019 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2020 cmd == SNDCTL_DSP_MAPINBUF ?
2021 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2022 (struct buffmem_desc __user *) arg);
2023 case SNDCTL_DSP_SETSYNCRO:
2024 /* stop DMA now.. */
2025 return 0;
2026 case SNDCTL_DSP_SETDUPLEX:
2027 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2028 return 0;
2029 return -EIO;
2030 case SNDCTL_DSP_GETODELAY:
2031 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2032 if (res < 0) {
2033 /* it's for sure, some broken apps don't check for error codes */
2034 put_user(0, p);
2035 return res;
2036 }
2037 return put_user(res, p);
2038 case SNDCTL_DSP_PROFILE:
2039 return 0; /* silently ignore */
2040 default:
2041 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2042 }
2043 return -EINVAL;
2044}
2045
2046#ifdef CONFIG_COMPAT
2047/* all compatible */
2048#define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2049#else
2050#define snd_pcm_oss_ioctl_compat NULL
2051#endif
2052
2053static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2054{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002055 struct snd_pcm_oss_file *pcm_oss_file;
2056 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 pcm_oss_file = file->private_data;
2059 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2060 if (substream == NULL)
2061 return -ENXIO;
2062#ifndef OSS_DEBUG
2063 return snd_pcm_oss_read1(substream, buf, count);
2064#else
2065 {
2066 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2067 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2068 return res;
2069 }
2070#endif
2071}
2072
2073static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2074{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002075 struct snd_pcm_oss_file *pcm_oss_file;
2076 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 long result;
2078
2079 pcm_oss_file = file->private_data;
2080 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2081 if (substream == NULL)
2082 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 result = snd_pcm_oss_write1(substream, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#ifdef OSS_DEBUG
2085 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2086#endif
2087 return result;
2088}
2089
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002090static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002092 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (atomic_read(&runtime->mmap_count))
2094 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2095 else
2096 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2097}
2098
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002099static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002101 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (atomic_read(&runtime->mmap_count))
2103 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2104 else
2105 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2106}
2107
2108static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2109{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002110 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 unsigned int mask;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002112 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 pcm_oss_file = file->private_data;
2115
2116 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2117 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2118
2119 mask = 0;
2120 if (psubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002121 struct snd_pcm_runtime *runtime = psubstream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 poll_wait(file, &runtime->sleep, wait);
2123 snd_pcm_stream_lock_irq(psubstream);
2124 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2125 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2126 snd_pcm_oss_playback_ready(psubstream)))
2127 mask |= POLLOUT | POLLWRNORM;
2128 snd_pcm_stream_unlock_irq(psubstream);
2129 }
2130 if (csubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002131 struct snd_pcm_runtime *runtime = csubstream->runtime;
2132 snd_pcm_state_t ostate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 poll_wait(file, &runtime->sleep, wait);
2134 snd_pcm_stream_lock_irq(csubstream);
2135 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2136 snd_pcm_oss_capture_ready(csubstream))
2137 mask |= POLLIN | POLLRDNORM;
2138 snd_pcm_stream_unlock_irq(csubstream);
2139 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002140 struct snd_pcm_oss_file ofile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 memset(&ofile, 0, sizeof(ofile));
2142 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2143 runtime->oss.trigger = 0;
2144 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2145 }
2146 }
2147
2148 return mask;
2149}
2150
2151static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2152{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002153 struct snd_pcm_oss_file *pcm_oss_file;
2154 struct snd_pcm_substream *substream = NULL;
2155 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int err;
2157
2158#ifdef OSS_DEBUG
2159 printk("pcm_oss: mmap begin\n");
2160#endif
2161 pcm_oss_file = file->private_data;
2162 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2163 case VM_READ | VM_WRITE:
2164 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2165 if (substream)
2166 break;
2167 /* Fall through */
2168 case VM_READ:
2169 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2170 break;
2171 case VM_WRITE:
2172 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2173 break;
2174 default:
2175 return -EINVAL;
2176 }
2177 /* set VM_READ access as well to fix memset() routines that do
2178 reads before writes (to improve performance) */
2179 area->vm_flags |= VM_READ;
2180 if (substream == NULL)
2181 return -ENXIO;
2182 runtime = substream->runtime;
2183 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2184 return -EIO;
2185 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2186 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2187 else
2188 return -EIO;
2189
2190 if (runtime->oss.params) {
2191 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2192 return err;
2193 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002194#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (runtime->oss.plugin_first != NULL)
2196 return -EIO;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 if (area->vm_pgoff != 0)
2200 return -EINVAL;
2201
2202 err = snd_pcm_mmap_data(substream, file, area);
2203 if (err < 0)
2204 return err;
2205 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2206 runtime->silence_threshold = 0;
2207 runtime->silence_size = 0;
2208#ifdef OSS_DEBUG
2209 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2210#endif
2211 /* In mmap mode we never stop */
2212 runtime->stop_threshold = runtime->boundary;
2213
2214 return 0;
2215}
2216
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002217#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/*
2219 * /proc interface
2220 */
2221
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002222static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2223 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002225 struct snd_pcm_str *pstr = entry->private_data;
2226 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002227 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 while (setup) {
2229 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2230 setup->task_name,
2231 setup->periods,
2232 setup->period_size,
2233 setup->disable ? " disable" : "",
2234 setup->direct ? " direct" : "",
2235 setup->block ? " block" : "",
2236 setup->nonblock ? " non-block" : "",
2237 setup->partialfrag ? " partial-frag" : "",
2238 setup->nosilence ? " no-silence" : "");
2239 setup = setup->next;
2240 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002241 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002244static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002246 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2249 setup; setup = setupn) {
2250 setupn = setup->next;
2251 kfree(setup->task_name);
2252 kfree(setup);
2253 }
2254 pstr->oss.setup_list = NULL;
2255}
2256
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002257static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2258 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002260 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 char line[128], str[32], task_name[32], *ptr;
2262 int idx1;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002263 struct snd_pcm_oss_setup *setup, *setup1, template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 while (!snd_info_get_line(buffer, line, sizeof(line))) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002266 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 memset(&template, 0, sizeof(template));
2268 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2269 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2270 snd_pcm_oss_proc_free_setup_list(pstr);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002271 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 continue;
2273 }
2274 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2275 if (!strcmp(setup->task_name, task_name)) {
2276 template = *setup;
2277 break;
2278 }
2279 }
2280 ptr = snd_info_get_str(str, ptr, sizeof(str));
2281 template.periods = simple_strtoul(str, NULL, 10);
2282 ptr = snd_info_get_str(str, ptr, sizeof(str));
2283 template.period_size = simple_strtoul(str, NULL, 10);
2284 for (idx1 = 31; idx1 >= 0; idx1--)
2285 if (template.period_size & (1 << idx1))
2286 break;
2287 for (idx1--; idx1 >= 0; idx1--)
2288 template.period_size &= ~(1 << idx1);
2289 do {
2290 ptr = snd_info_get_str(str, ptr, sizeof(str));
2291 if (!strcmp(str, "disable")) {
2292 template.disable = 1;
2293 } else if (!strcmp(str, "direct")) {
2294 template.direct = 1;
2295 } else if (!strcmp(str, "block")) {
2296 template.block = 1;
2297 } else if (!strcmp(str, "non-block")) {
2298 template.nonblock = 1;
2299 } else if (!strcmp(str, "partial-frag")) {
2300 template.partialfrag = 1;
2301 } else if (!strcmp(str, "no-silence")) {
2302 template.nosilence = 1;
Takashi Iwai10f69f92005-09-08 13:48:34 +02002303 } else if (!strcmp(str, "buggy-ptr")) {
2304 template.buggyptr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 } while (*str);
2307 if (setup == NULL) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002308 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2309 if (! setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 buffer->error = -ENOMEM;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002311 mutex_lock(&pstr->oss.setup_mutex);
2312 return;
2313 }
2314 if (pstr->oss.setup_list == NULL)
2315 pstr->oss.setup_list = setup;
2316 else {
2317 for (setup1 = pstr->oss.setup_list;
2318 setup1->next; setup1 = setup1->next);
2319 setup1->next = setup;
2320 }
2321 template.task_name = kstrdup(task_name, GFP_KERNEL);
2322 if (! template.task_name) {
2323 kfree(setup);
2324 buffer->error = -ENOMEM;
2325 mutex_lock(&pstr->oss.setup_mutex);
2326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002329 *setup = template;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002330 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332}
2333
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002334static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 int stream;
2337 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002338 struct snd_info_entry *entry;
2339 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (pstr->substream_count == 0)
2341 continue;
2342 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2343 entry->content = SNDRV_INFO_CONTENT_TEXT;
2344 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2345 entry->c.text.read_size = 8192;
2346 entry->c.text.read = snd_pcm_oss_proc_read;
2347 entry->c.text.write_size = 8192;
2348 entry->c.text.write = snd_pcm_oss_proc_write;
2349 entry->private_data = pstr;
2350 if (snd_info_register(entry) < 0) {
2351 snd_info_free_entry(entry);
2352 entry = NULL;
2353 }
2354 }
2355 pstr->oss.proc_entry = entry;
2356 }
2357}
2358
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002359static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 int stream;
2362 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002363 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (pstr->oss.proc_entry) {
2365 snd_info_unregister(pstr->oss.proc_entry);
2366 pstr->oss.proc_entry = NULL;
2367 snd_pcm_oss_proc_free_setup_list(pstr);
2368 }
2369 }
2370}
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002371#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwai04f141a2005-12-01 10:43:51 +01002372#define snd_pcm_oss_proc_init(pcm)
2373#define snd_pcm_oss_proc_done(pcm)
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002374#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376/*
2377 * ENTRY functions
2378 */
2379
2380static struct file_operations snd_pcm_oss_f_reg =
2381{
2382 .owner = THIS_MODULE,
2383 .read = snd_pcm_oss_read,
2384 .write = snd_pcm_oss_write,
2385 .open = snd_pcm_oss_open,
2386 .release = snd_pcm_oss_release,
2387 .poll = snd_pcm_oss_poll,
2388 .unlocked_ioctl = snd_pcm_oss_ioctl,
2389 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2390 .mmap = snd_pcm_oss_mmap,
2391};
2392
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002393static void register_oss_dsp(struct snd_pcm *pcm, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 char name[128];
2396 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2397 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002398 pcm->card, index, &snd_pcm_oss_f_reg,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002399 pcm, name) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002400 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2401 pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403}
2404
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002405static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 pcm->oss.reg = 0;
2408 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2409 char name[128];
2410 int duplex;
2411 register_oss_dsp(pcm, 0);
2412 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2413 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2414 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2415 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2416#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2417 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2418 pcm->card->number,
2419 name);
2420#endif
2421 pcm->oss.reg++;
2422 pcm->oss.reg_mask |= 1;
2423 }
2424 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2425 register_oss_dsp(pcm, 1);
2426 pcm->oss.reg++;
2427 pcm->oss.reg_mask |= 2;
2428 }
2429
2430 if (pcm->oss.reg)
2431 snd_pcm_oss_proc_init(pcm);
2432
2433 return 0;
2434}
2435
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002436static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 if (pcm->oss.reg) {
2439 if (pcm->oss.reg_mask & 1) {
2440 pcm->oss.reg_mask &= ~1;
2441 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2442 pcm->card, 0);
2443 }
2444 if (pcm->oss.reg_mask & 2) {
2445 pcm->oss.reg_mask &= ~2;
2446 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2447 pcm->card, 1);
2448 }
2449 }
2450 return 0;
2451}
2452
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002453static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 snd_pcm_oss_disconnect_minor(pcm);
2456 if (pcm->oss.reg) {
2457 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2458#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2459 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2460#endif
2461 }
2462 pcm->oss.reg = 0;
2463 snd_pcm_oss_proc_done(pcm);
2464 }
2465 return 0;
2466}
2467
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002468static struct snd_pcm_notify snd_pcm_oss_notify =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 .n_register = snd_pcm_oss_register_minor,
2471 .n_disconnect = snd_pcm_oss_disconnect_minor,
2472 .n_unregister = snd_pcm_oss_unregister_minor,
2473};
2474
2475static int __init alsa_pcm_oss_init(void)
2476{
2477 int i;
2478 int err;
2479
2480 /* check device map table */
2481 for (i = 0; i < SNDRV_CARDS; i++) {
2482 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002483 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2484 i, dsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 dsp_map[i] = 0;
2486 }
2487 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002488 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
2489 i, adsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 adsp_map[i] = 1;
2491 }
2492 }
2493 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2494 return err;
2495 return 0;
2496}
2497
2498static void __exit alsa_pcm_oss_exit(void)
2499{
2500 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2501}
2502
2503module_init(alsa_pcm_oss_init)
2504module_exit(alsa_pcm_oss_exit)