[PATCH] ieee80211: Fix debug comments ipw->ieee80211
[deliverable/linux.git] / sound / core / oss / pcm_oss.c
1 /*
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>
36 #include <linux/string.h>
37 #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
48 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
49 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50 static int nonblock_open = 1;
51
52 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54 MODULE_LICENSE("GPL");
55 module_param_array(dsp_map, int, NULL, 0444);
56 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map, int, NULL, 0444);
58 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59 module_param(nonblock_open, bool, 0644);
60 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63
64 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
65 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
66 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
67 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
68
69 static 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
76 static inline void snd_leave_user(mm_segment_t fs)
77 {
78 set_fs(fs);
79 }
80
81 static int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
82 {
83 snd_pcm_runtime_t *runtime = substream->runtime;
84 snd_pcm_plugin_t *plugin, *next;
85
86 plugin = runtime->oss.plugin_first;
87 while (plugin) {
88 next = plugin->next;
89 snd_pcm_plugin_free(plugin);
90 plugin = next;
91 }
92 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
93 return 0;
94 }
95
96 static int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
97 {
98 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
99 plugin->next = runtime->oss.plugin_first;
100 plugin->prev = NULL;
101 if (runtime->oss.plugin_first) {
102 runtime->oss.plugin_first->prev = plugin;
103 runtime->oss.plugin_first = plugin;
104 } else {
105 runtime->oss.plugin_last =
106 runtime->oss.plugin_first = plugin;
107 }
108 return 0;
109 }
110
111 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
112 {
113 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
114 plugin->next = NULL;
115 plugin->prev = runtime->oss.plugin_last;
116 if (runtime->oss.plugin_last) {
117 runtime->oss.plugin_last->next = plugin;
118 runtime->oss.plugin_last = plugin;
119 } else {
120 runtime->oss.plugin_last =
121 runtime->oss.plugin_first = plugin;
122 }
123 return 0;
124 }
125
126 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
127 {
128 snd_pcm_runtime_t *runtime = substream->runtime;
129 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
130 long bytes = frames_to_bytes(runtime, frames);
131 if (buffer_size == runtime->oss.buffer_bytes)
132 return bytes;
133 #if BITS_PER_LONG >= 64
134 return runtime->oss.buffer_bytes * bytes / buffer_size;
135 #else
136 {
137 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
138 u32 rem;
139 div64_32(&bsize, buffer_size, &rem);
140 return (long)bsize;
141 }
142 #endif
143 }
144
145 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
146 {
147 snd_pcm_runtime_t *runtime = substream->runtime;
148 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
149 if (buffer_size == runtime->oss.buffer_bytes)
150 return bytes_to_frames(runtime, bytes);
151 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
152 }
153
154 static int snd_pcm_oss_format_from(int format)
155 {
156 switch (format) {
157 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
158 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
159 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
160 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
161 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
162 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
163 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
164 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
165 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
166 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
167 default: return SNDRV_PCM_FORMAT_U8;
168 }
169 }
170
171 static int snd_pcm_oss_format_to(int format)
172 {
173 switch (format) {
174 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
175 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
176 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
177 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
178 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
179 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
180 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
181 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
182 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
183 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
184 default: return -EINVAL;
185 }
186 }
187
188 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream,
189 snd_pcm_hw_params_t *oss_params,
190 snd_pcm_hw_params_t *slave_params)
191 {
192 size_t s;
193 size_t oss_buffer_size, oss_period_size, oss_periods;
194 size_t min_period_size, max_period_size;
195 snd_pcm_runtime_t *runtime = substream->runtime;
196 size_t oss_frame_size;
197
198 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
199 params_channels(oss_params) / 8;
200
201 oss_buffer_size = snd_pcm_plug_client_size(substream,
202 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
203 oss_buffer_size = 1 << ld2(oss_buffer_size);
204 if (atomic_read(&runtime->mmap_count)) {
205 if (oss_buffer_size > runtime->oss.mmap_bytes)
206 oss_buffer_size = runtime->oss.mmap_bytes;
207 }
208
209 if (substream->oss.setup &&
210 substream->oss.setup->period_size > 16)
211 oss_period_size = substream->oss.setup->period_size;
212 else if (runtime->oss.fragshift) {
213 oss_period_size = 1 << runtime->oss.fragshift;
214 if (oss_period_size > oss_buffer_size / 2)
215 oss_period_size = oss_buffer_size / 2;
216 } else {
217 int sd;
218 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
219
220 oss_period_size = oss_buffer_size;
221 do {
222 oss_period_size /= 2;
223 } while (oss_period_size > bytes_per_sec);
224 if (runtime->oss.subdivision == 0) {
225 sd = 4;
226 if (oss_period_size / sd > 4096)
227 sd *= 2;
228 if (oss_period_size / sd < 4096)
229 sd = 1;
230 } else
231 sd = runtime->oss.subdivision;
232 oss_period_size /= sd;
233 if (oss_period_size < 16)
234 oss_period_size = 16;
235 }
236
237 min_period_size = snd_pcm_plug_client_size(substream,
238 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
239 min_period_size *= oss_frame_size;
240 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
241 if (oss_period_size < min_period_size)
242 oss_period_size = min_period_size;
243
244 max_period_size = snd_pcm_plug_client_size(substream,
245 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
246 max_period_size *= oss_frame_size;
247 max_period_size = 1 << ld2(max_period_size);
248 if (oss_period_size > max_period_size)
249 oss_period_size = max_period_size;
250
251 oss_periods = oss_buffer_size / oss_period_size;
252
253 if (substream->oss.setup) {
254 if (substream->oss.setup->periods > 1)
255 oss_periods = substream->oss.setup->periods;
256 }
257
258 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
259 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
260 s = runtime->oss.maxfrags;
261 if (oss_periods > s)
262 oss_periods = s;
263
264 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
265 if (s < 2)
266 s = 2;
267 if (oss_periods < s)
268 oss_periods = s;
269
270 while (oss_period_size * oss_periods > oss_buffer_size)
271 oss_period_size /= 2;
272
273 snd_assert(oss_period_size >= 16, return -EINVAL);
274 runtime->oss.period_bytes = oss_period_size;
275 runtime->oss.period_frames = 1;
276 runtime->oss.periods = oss_periods;
277 return 0;
278 }
279
280 static int choose_rate(snd_pcm_substream_t *substream,
281 snd_pcm_hw_params_t *params, unsigned int best_rate)
282 {
283 snd_interval_t *it;
284 snd_pcm_hw_params_t *save;
285 unsigned int rate, prev;
286
287 save = kmalloc(sizeof(*save), GFP_KERNEL);
288 if (save == NULL)
289 return -ENOMEM;
290 *save = *params;
291 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
292
293 /* try multiples of the best rate */
294 rate = best_rate;
295 for (;;) {
296 if (it->max < rate || (it->max == rate && it->openmax))
297 break;
298 if (it->min < rate || (it->min == rate && !it->openmin)) {
299 int ret;
300 ret = snd_pcm_hw_param_set(substream, params,
301 SNDRV_PCM_HW_PARAM_RATE,
302 rate, 0);
303 if (ret == (int)rate) {
304 kfree(save);
305 return rate;
306 }
307 *params = *save;
308 }
309 prev = rate;
310 rate += best_rate;
311 if (rate <= prev)
312 break;
313 }
314
315 /* not found, use the nearest rate */
316 kfree(save);
317 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
318 }
319
320 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
321 {
322 snd_pcm_runtime_t *runtime = substream->runtime;
323 snd_pcm_hw_params_t *params, *sparams;
324 snd_pcm_sw_params_t *sw_params;
325 ssize_t oss_buffer_size, oss_period_size;
326 size_t oss_frame_size;
327 int err;
328 int direct;
329 int format, sformat, n;
330 snd_mask_t sformat_mask;
331 snd_mask_t mask;
332
333 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
334 params = kmalloc(sizeof(*params), GFP_KERNEL);
335 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
336 if (!sw_params || !params || !sparams) {
337 snd_printd("No memory\n");
338 err = -ENOMEM;
339 goto failure;
340 }
341
342 if (atomic_read(&runtime->mmap_count)) {
343 direct = 1;
344 } else {
345 snd_pcm_oss_setup_t *setup = substream->oss.setup;
346 direct = (setup != NULL && setup->direct);
347 }
348
349 _snd_pcm_hw_params_any(sparams);
350 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
351 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
352 snd_mask_none(&mask);
353 if (atomic_read(&runtime->mmap_count))
354 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
355 else {
356 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
357 if (!direct)
358 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
359 }
360 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
361 if (err < 0) {
362 snd_printd("No usable accesses\n");
363 err = -EINVAL;
364 goto failure;
365 }
366 choose_rate(substream, sparams, runtime->oss.rate);
367 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
368
369 format = snd_pcm_oss_format_from(runtime->oss.format);
370
371 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
372 if (direct)
373 sformat = format;
374 else
375 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
376
377 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
378 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
379 if (snd_mask_test(&sformat_mask, sformat) &&
380 snd_pcm_oss_format_to(sformat) >= 0)
381 break;
382 }
383 if (sformat > SNDRV_PCM_FORMAT_LAST) {
384 snd_printd("Cannot find a format!!!\n");
385 err = -EINVAL;
386 goto failure;
387 }
388 }
389 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
390 snd_assert(err >= 0, goto failure);
391
392 if (direct) {
393 memcpy(params, sparams, sizeof(*params));
394 } else {
395 _snd_pcm_hw_params_any(params);
396 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
397 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
398 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
399 snd_pcm_oss_format_from(runtime->oss.format), 0);
400 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
401 runtime->oss.channels, 0);
402 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
403 runtime->oss.rate, 0);
404 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
405 params_access(params), params_format(params),
406 params_channels(params), params_rate(params));
407 }
408 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
409 params_access(sparams), params_format(sparams),
410 params_channels(sparams), params_rate(sparams));
411
412 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
413 params_channels(params) / 8;
414
415 snd_pcm_oss_plugin_clear(substream);
416 if (!direct) {
417 /* add necessary plugins */
418 snd_pcm_oss_plugin_clear(substream);
419 if ((err = snd_pcm_plug_format_plugins(substream,
420 params,
421 sparams)) < 0) {
422 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
423 snd_pcm_oss_plugin_clear(substream);
424 goto failure;
425 }
426 if (runtime->oss.plugin_first) {
427 snd_pcm_plugin_t *plugin;
428 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
429 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
430 snd_pcm_oss_plugin_clear(substream);
431 goto failure;
432 }
433 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
434 err = snd_pcm_plugin_append(plugin);
435 } else {
436 err = snd_pcm_plugin_insert(plugin);
437 }
438 if (err < 0) {
439 snd_pcm_oss_plugin_clear(substream);
440 goto failure;
441 }
442 }
443 }
444
445 err = snd_pcm_oss_period_size(substream, params, sparams);
446 if (err < 0)
447 goto failure;
448
449 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
450 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
451 snd_assert(err >= 0, goto failure);
452
453 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
454 runtime->oss.periods, NULL);
455 snd_assert(err >= 0, goto failure);
456
457 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
458
459 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
460 snd_printd("HW_PARAMS failed: %i\n", err);
461 goto failure;
462 }
463
464 memset(sw_params, 0, sizeof(*sw_params));
465 if (runtime->oss.trigger) {
466 sw_params->start_threshold = 1;
467 } else {
468 sw_params->start_threshold = runtime->boundary;
469 }
470 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
471 sw_params->stop_threshold = runtime->boundary;
472 else
473 sw_params->stop_threshold = runtime->buffer_size;
474 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
475 sw_params->period_step = 1;
476 sw_params->sleep_min = 0;
477 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
478 1 : runtime->period_size;
479 sw_params->xfer_align = 1;
480 if (atomic_read(&runtime->mmap_count) ||
481 (substream->oss.setup && substream->oss.setup->nosilence)) {
482 sw_params->silence_threshold = 0;
483 sw_params->silence_size = 0;
484 } else {
485 snd_pcm_uframes_t frames;
486 frames = runtime->period_size + 16;
487 if (frames > runtime->buffer_size)
488 frames = runtime->buffer_size;
489 sw_params->silence_threshold = frames;
490 sw_params->silence_size = frames;
491 }
492
493 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
494 snd_printd("SW_PARAMS failed: %i\n", err);
495 goto failure;
496 }
497
498 runtime->oss.periods = params_periods(sparams);
499 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
500 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
501 if (runtime->oss.plugin_first) {
502 err = snd_pcm_plug_alloc(substream, oss_period_size);
503 if (err < 0)
504 goto failure;
505 }
506 oss_period_size *= oss_frame_size;
507
508 oss_buffer_size = oss_period_size * runtime->oss.periods;
509 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
510
511 runtime->oss.period_bytes = oss_period_size;
512 runtime->oss.buffer_bytes = oss_buffer_size;
513
514 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
515 runtime->oss.period_bytes,
516 runtime->oss.buffer_bytes);
517 pdprintf("slave: period_size = %i, buffer_size = %i\n",
518 params_period_size(sparams),
519 params_buffer_size(sparams));
520
521 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
522 runtime->oss.channels = params_channels(params);
523 runtime->oss.rate = params_rate(params);
524
525 runtime->oss.params = 0;
526 runtime->oss.prepare = 1;
527 vfree(runtime->oss.buffer);
528 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
529 runtime->oss.buffer_used = 0;
530 if (runtime->dma_area)
531 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
532
533 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
534
535 err = 0;
536 failure:
537 kfree(sw_params);
538 kfree(params);
539 kfree(sparams);
540 return err;
541 }
542
543 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
544 {
545 int idx, err;
546 snd_pcm_substream_t *asubstream = NULL, *substream;
547
548 for (idx = 0; idx < 2; idx++) {
549 substream = pcm_oss_file->streams[idx];
550 if (substream == NULL)
551 continue;
552 if (asubstream == NULL)
553 asubstream = substream;
554 if (substream->runtime->oss.params) {
555 err = snd_pcm_oss_change_params(substream);
556 if (err < 0)
557 return err;
558 }
559 }
560 snd_assert(asubstream != NULL, return -EIO);
561 if (r_substream)
562 *r_substream = asubstream;
563 return 0;
564 }
565
566 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
567 {
568 int err;
569 snd_pcm_runtime_t *runtime = substream->runtime;
570
571 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
572 if (err < 0) {
573 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
574 return err;
575 }
576 runtime->oss.prepare = 0;
577 runtime->oss.prev_hw_ptr_interrupt = 0;
578 runtime->oss.period_ptr = 0;
579 runtime->oss.buffer_used = 0;
580
581 return 0;
582 }
583
584 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
585 {
586 snd_pcm_runtime_t *runtime;
587 int err;
588
589 if (substream == NULL)
590 return 0;
591 runtime = substream->runtime;
592 if (runtime->oss.params) {
593 err = snd_pcm_oss_change_params(substream);
594 if (err < 0)
595 return err;
596 }
597 if (runtime->oss.prepare) {
598 err = snd_pcm_oss_prepare(substream);
599 if (err < 0)
600 return err;
601 }
602 return 0;
603 }
604
605 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
606 {
607 snd_pcm_runtime_t *runtime;
608 snd_pcm_uframes_t frames;
609 int err = 0;
610
611 while (1) {
612 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
613 if (err < 0)
614 break;
615 runtime = substream->runtime;
616 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
617 break;
618 /* in case of overrun, skip whole periods like OSS/Linux driver does */
619 /* until avail(delay) <= buffer_size */
620 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
621 frames /= runtime->period_size;
622 frames *= runtime->period_size;
623 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
624 if (err < 0)
625 break;
626 }
627 return err;
628 }
629
630 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
631 {
632 snd_pcm_runtime_t *runtime = substream->runtime;
633 int ret;
634 while (1) {
635 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
636 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
637 #ifdef OSS_DEBUG
638 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
639 printk("pcm_oss: write: recovering from XRUN\n");
640 else
641 printk("pcm_oss: write: recovering from SUSPEND\n");
642 #endif
643 ret = snd_pcm_oss_prepare(substream);
644 if (ret < 0)
645 break;
646 }
647 if (in_kernel) {
648 mm_segment_t fs;
649 fs = snd_enter_user();
650 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
651 snd_leave_user(fs);
652 } else {
653 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
654 }
655 if (ret != -EPIPE && ret != -ESTRPIPE)
656 break;
657 /* test, if we can't store new data, because the stream */
658 /* has not been started */
659 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
660 return -EAGAIN;
661 }
662 return ret;
663 }
664
665 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
666 {
667 snd_pcm_runtime_t *runtime = substream->runtime;
668 snd_pcm_sframes_t delay;
669 int ret;
670 while (1) {
671 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
672 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
673 #ifdef OSS_DEBUG
674 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
675 printk("pcm_oss: read: recovering from XRUN\n");
676 else
677 printk("pcm_oss: read: recovering from SUSPEND\n");
678 #endif
679 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
680 if (ret < 0)
681 break;
682 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
683 ret = snd_pcm_oss_prepare(substream);
684 if (ret < 0)
685 break;
686 }
687 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
688 if (ret < 0)
689 break;
690 if (in_kernel) {
691 mm_segment_t fs;
692 fs = snd_enter_user();
693 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
694 snd_leave_user(fs);
695 } else {
696 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
697 }
698 if (ret == -EPIPE) {
699 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
700 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
701 if (ret < 0)
702 break;
703 }
704 continue;
705 }
706 if (ret != -ESTRPIPE)
707 break;
708 }
709 return ret;
710 }
711
712 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
713 {
714 snd_pcm_runtime_t *runtime = substream->runtime;
715 int ret;
716 while (1) {
717 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
718 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
719 #ifdef OSS_DEBUG
720 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
721 printk("pcm_oss: writev: recovering from XRUN\n");
722 else
723 printk("pcm_oss: writev: recovering from SUSPEND\n");
724 #endif
725 ret = snd_pcm_oss_prepare(substream);
726 if (ret < 0)
727 break;
728 }
729 if (in_kernel) {
730 mm_segment_t fs;
731 fs = snd_enter_user();
732 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
733 snd_leave_user(fs);
734 } else {
735 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
736 }
737 if (ret != -EPIPE && ret != -ESTRPIPE)
738 break;
739
740 /* test, if we can't store new data, because the stream */
741 /* has not been started */
742 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
743 return -EAGAIN;
744 }
745 return ret;
746 }
747
748 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
749 {
750 snd_pcm_runtime_t *runtime = substream->runtime;
751 int ret;
752 while (1) {
753 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
754 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
755 #ifdef OSS_DEBUG
756 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
757 printk("pcm_oss: readv: recovering from XRUN\n");
758 else
759 printk("pcm_oss: readv: recovering from SUSPEND\n");
760 #endif
761 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
762 if (ret < 0)
763 break;
764 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
765 ret = snd_pcm_oss_prepare(substream);
766 if (ret < 0)
767 break;
768 }
769 if (in_kernel) {
770 mm_segment_t fs;
771 fs = snd_enter_user();
772 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
773 snd_leave_user(fs);
774 } else {
775 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
776 }
777 if (ret != -EPIPE && ret != -ESTRPIPE)
778 break;
779 }
780 return ret;
781 }
782
783 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
784 {
785 snd_pcm_runtime_t *runtime = substream->runtime;
786 snd_pcm_sframes_t frames, frames1;
787 if (runtime->oss.plugin_first) {
788 snd_pcm_plugin_channel_t *channels;
789 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
790 if (!in_kernel) {
791 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
792 return -EFAULT;
793 buf = runtime->oss.buffer;
794 }
795 frames = bytes / oss_frame_bytes;
796 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
797 if (frames1 < 0)
798 return frames1;
799 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
800 if (frames1 <= 0)
801 return frames1;
802 bytes = frames1 * oss_frame_bytes;
803 } else {
804 frames = bytes_to_frames(runtime, bytes);
805 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
806 if (frames1 <= 0)
807 return frames1;
808 bytes = frames_to_bytes(runtime, frames1);
809 }
810 return bytes;
811 }
812
813 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char __user *buf, size_t bytes)
814 {
815 size_t xfer = 0;
816 ssize_t tmp;
817 snd_pcm_runtime_t *runtime = substream->runtime;
818
819 if (atomic_read(&runtime->mmap_count))
820 return -ENXIO;
821
822 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
823 return tmp;
824 while (bytes > 0) {
825 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
826 tmp = bytes;
827 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
828 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
829 if (tmp > 0) {
830 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
831 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
832 }
833 runtime->oss.buffer_used += tmp;
834 buf += tmp;
835 bytes -= tmp;
836 xfer += tmp;
837 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
838 runtime->oss.buffer_used == runtime->oss.period_bytes) {
839 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
840 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
841 if (tmp <= 0)
842 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
843 runtime->oss.bytes += tmp;
844 runtime->oss.period_ptr += tmp;
845 runtime->oss.period_ptr %= runtime->oss.period_bytes;
846 if (runtime->oss.period_ptr == 0 ||
847 runtime->oss.period_ptr == runtime->oss.buffer_used)
848 runtime->oss.buffer_used = 0;
849 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
850 return xfer > 0 ? xfer : -EAGAIN;
851 }
852 } else {
853 tmp = snd_pcm_oss_write2(substream, (const char *)buf, runtime->oss.period_bytes, 0);
854 if (tmp <= 0)
855 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
856 runtime->oss.bytes += tmp;
857 buf += tmp;
858 bytes -= tmp;
859 xfer += tmp;
860 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
861 tmp != runtime->oss.period_bytes)
862 break;
863 }
864 }
865 return xfer;
866 }
867
868 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
869 {
870 snd_pcm_runtime_t *runtime = substream->runtime;
871 snd_pcm_sframes_t frames, frames1;
872 char __user *final_dst = (char __user *)buf;
873 if (runtime->oss.plugin_first) {
874 snd_pcm_plugin_channel_t *channels;
875 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
876 if (!in_kernel)
877 buf = runtime->oss.buffer;
878 frames = bytes / oss_frame_bytes;
879 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
880 if (frames1 < 0)
881 return frames1;
882 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
883 if (frames1 <= 0)
884 return frames1;
885 bytes = frames1 * oss_frame_bytes;
886 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
887 return -EFAULT;
888 } else {
889 frames = bytes_to_frames(runtime, bytes);
890 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
891 if (frames1 <= 0)
892 return frames1;
893 bytes = frames_to_bytes(runtime, frames1);
894 }
895 return bytes;
896 }
897
898 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char __user *buf, size_t bytes)
899 {
900 size_t xfer = 0;
901 ssize_t tmp;
902 snd_pcm_runtime_t *runtime = substream->runtime;
903
904 if (atomic_read(&runtime->mmap_count))
905 return -ENXIO;
906
907 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
908 return tmp;
909 while (bytes > 0) {
910 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
911 if (runtime->oss.buffer_used == 0) {
912 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
913 if (tmp <= 0)
914 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
915 runtime->oss.bytes += tmp;
916 runtime->oss.period_ptr = tmp;
917 runtime->oss.buffer_used = tmp;
918 }
919 tmp = bytes;
920 if ((size_t) tmp > runtime->oss.buffer_used)
921 tmp = runtime->oss.buffer_used;
922 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
923 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
924 buf += tmp;
925 bytes -= tmp;
926 xfer += tmp;
927 runtime->oss.buffer_used -= tmp;
928 } else {
929 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
930 if (tmp <= 0)
931 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
932 runtime->oss.bytes += tmp;
933 buf += tmp;
934 bytes -= tmp;
935 xfer += tmp;
936 }
937 }
938 return xfer;
939 }
940
941 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
942 {
943 snd_pcm_substream_t *substream;
944
945 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
946 if (substream != NULL) {
947 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
948 substream->runtime->oss.prepare = 1;
949 }
950 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
951 if (substream != NULL) {
952 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
953 substream->runtime->oss.prepare = 1;
954 }
955 return 0;
956 }
957
958 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
959 {
960 snd_pcm_substream_t *substream;
961 int err;
962
963 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
964 if (substream != NULL) {
965 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
966 return err;
967 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
968 }
969 /* note: all errors from the start action are ignored */
970 /* OSS apps do not know, how to handle them */
971 return 0;
972 }
973
974 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
975 {
976 snd_pcm_runtime_t *runtime;
977 ssize_t result = 0;
978 long res;
979 wait_queue_t wait;
980
981 runtime = substream->runtime;
982 init_waitqueue_entry(&wait, current);
983 add_wait_queue(&runtime->sleep, &wait);
984 #ifdef OSS_DEBUG
985 printk("sync1: size = %li\n", size);
986 #endif
987 while (1) {
988 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
989 if (result > 0) {
990 runtime->oss.buffer_used = 0;
991 result = 0;
992 break;
993 }
994 if (result != 0 && result != -EAGAIN)
995 break;
996 result = 0;
997 set_current_state(TASK_INTERRUPTIBLE);
998 snd_pcm_stream_lock_irq(substream);
999 res = runtime->status->state;
1000 snd_pcm_stream_unlock_irq(substream);
1001 if (res != SNDRV_PCM_STATE_RUNNING) {
1002 set_current_state(TASK_RUNNING);
1003 break;
1004 }
1005 res = schedule_timeout(10 * HZ);
1006 if (signal_pending(current)) {
1007 result = -ERESTARTSYS;
1008 break;
1009 }
1010 if (res == 0) {
1011 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1012 result = -EIO;
1013 break;
1014 }
1015 }
1016 remove_wait_queue(&runtime->sleep, &wait);
1017 return result;
1018 }
1019
1020 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1021 {
1022 int err = 0;
1023 unsigned int saved_f_flags;
1024 snd_pcm_substream_t *substream;
1025 snd_pcm_runtime_t *runtime;
1026 snd_pcm_format_t format;
1027 unsigned long width;
1028 size_t size;
1029
1030 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1031 if (substream != NULL) {
1032 runtime = substream->runtime;
1033 if (atomic_read(&runtime->mmap_count))
1034 goto __direct;
1035 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1036 return err;
1037 format = snd_pcm_oss_format_from(runtime->oss.format);
1038 width = snd_pcm_format_physical_width(format);
1039 if (runtime->oss.buffer_used > 0) {
1040 #ifdef OSS_DEBUG
1041 printk("sync: buffer_used\n");
1042 #endif
1043 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1044 snd_pcm_format_set_silence(format,
1045 runtime->oss.buffer + runtime->oss.buffer_used,
1046 size);
1047 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1048 if (err < 0)
1049 return err;
1050 } else if (runtime->oss.period_ptr > 0) {
1051 #ifdef OSS_DEBUG
1052 printk("sync: period_ptr\n");
1053 #endif
1054 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1055 snd_pcm_format_set_silence(format,
1056 runtime->oss.buffer,
1057 size * 8 / width);
1058 err = snd_pcm_oss_sync1(substream, size);
1059 if (err < 0)
1060 return err;
1061 }
1062 /*
1063 * The ALSA's period might be a bit large than OSS one.
1064 * Fill the remain portion of ALSA period with zeros.
1065 */
1066 size = runtime->control->appl_ptr % runtime->period_size;
1067 if (size > 0) {
1068 size = runtime->period_size - size;
1069 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1070 size = (runtime->frame_bits * size) / 8;
1071 while (size > 0) {
1072 mm_segment_t fs;
1073 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1074 size -= size1;
1075 size1 *= 8;
1076 size1 /= runtime->sample_bits;
1077 snd_pcm_format_set_silence(runtime->format,
1078 runtime->oss.buffer,
1079 size1);
1080 fs = snd_enter_user();
1081 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1082 snd_leave_user(fs);
1083 }
1084 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1085 void __user *buffers[runtime->channels];
1086 memset(buffers, 0, runtime->channels * sizeof(void *));
1087 snd_pcm_lib_writev(substream, buffers, size);
1088 }
1089 }
1090 /*
1091 * finish sync: drain the buffer
1092 */
1093 __direct:
1094 saved_f_flags = substream->ffile->f_flags;
1095 substream->ffile->f_flags &= ~O_NONBLOCK;
1096 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1097 substream->ffile->f_flags = saved_f_flags;
1098 if (err < 0)
1099 return err;
1100 runtime->oss.prepare = 1;
1101 }
1102
1103 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1104 if (substream != NULL) {
1105 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1106 return err;
1107 runtime = substream->runtime;
1108 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1109 if (err < 0)
1110 return err;
1111 runtime->oss.buffer_used = 0;
1112 runtime->oss.prepare = 1;
1113 }
1114 return 0;
1115 }
1116
1117 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1118 {
1119 int idx;
1120
1121 for (idx = 1; idx >= 0; --idx) {
1122 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1123 snd_pcm_runtime_t *runtime;
1124 if (substream == NULL)
1125 continue;
1126 runtime = substream->runtime;
1127 if (rate < 1000)
1128 rate = 1000;
1129 else if (rate > 192000)
1130 rate = 192000;
1131 if (runtime->oss.rate != rate) {
1132 runtime->oss.params = 1;
1133 runtime->oss.rate = rate;
1134 }
1135 }
1136 return snd_pcm_oss_get_rate(pcm_oss_file);
1137 }
1138
1139 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1140 {
1141 snd_pcm_substream_t *substream;
1142 int err;
1143
1144 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1145 return err;
1146 return substream->runtime->oss.rate;
1147 }
1148
1149 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1150 {
1151 int idx;
1152 if (channels < 1)
1153 channels = 1;
1154 if (channels > 128)
1155 return -EINVAL;
1156 for (idx = 1; idx >= 0; --idx) {
1157 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1158 snd_pcm_runtime_t *runtime;
1159 if (substream == NULL)
1160 continue;
1161 runtime = substream->runtime;
1162 if (runtime->oss.channels != channels) {
1163 runtime->oss.params = 1;
1164 runtime->oss.channels = channels;
1165 }
1166 }
1167 return snd_pcm_oss_get_channels(pcm_oss_file);
1168 }
1169
1170 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1171 {
1172 snd_pcm_substream_t *substream;
1173 int err;
1174
1175 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1176 return err;
1177 return substream->runtime->oss.channels;
1178 }
1179
1180 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1181 {
1182 snd_pcm_substream_t *substream;
1183 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.period_bytes;
1188 }
1189
1190 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1191 {
1192 snd_pcm_substream_t *substream;
1193 int err;
1194 int direct;
1195 snd_pcm_hw_params_t *params;
1196 unsigned int formats = 0;
1197 snd_mask_t format_mask;
1198 int fmt;
1199
1200 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1201 return err;
1202 if (atomic_read(&substream->runtime->mmap_count)) {
1203 direct = 1;
1204 } else {
1205 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1206 direct = (setup != NULL && setup->direct);
1207 }
1208 if (!direct)
1209 return AFMT_MU_LAW | AFMT_U8 |
1210 AFMT_S16_LE | AFMT_S16_BE |
1211 AFMT_S8 | AFMT_U16_LE |
1212 AFMT_U16_BE;
1213 params = kmalloc(sizeof(*params), GFP_KERNEL);
1214 if (!params)
1215 return -ENOMEM;
1216 _snd_pcm_hw_params_any(params);
1217 err = snd_pcm_hw_refine(substream, params);
1218 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1219 kfree(params);
1220 snd_assert(err >= 0, return err);
1221 for (fmt = 0; fmt < 32; ++fmt) {
1222 if (snd_mask_test(&format_mask, fmt)) {
1223 int f = snd_pcm_oss_format_to(fmt);
1224 if (f >= 0)
1225 formats |= f;
1226 }
1227 }
1228 return formats;
1229 }
1230
1231 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1232 {
1233 int formats, idx;
1234
1235 if (format != AFMT_QUERY) {
1236 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1237 if (!(formats & format))
1238 format = AFMT_U8;
1239 for (idx = 1; idx >= 0; --idx) {
1240 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1241 snd_pcm_runtime_t *runtime;
1242 if (substream == NULL)
1243 continue;
1244 runtime = substream->runtime;
1245 if (runtime->oss.format != format) {
1246 runtime->oss.params = 1;
1247 runtime->oss.format = format;
1248 }
1249 }
1250 }
1251 return snd_pcm_oss_get_format(pcm_oss_file);
1252 }
1253
1254 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1255 {
1256 snd_pcm_substream_t *substream;
1257 int err;
1258
1259 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1260 return err;
1261 return substream->runtime->oss.format;
1262 }
1263
1264 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1265 {
1266 snd_pcm_runtime_t *runtime;
1267
1268 if (substream == NULL)
1269 return 0;
1270 runtime = substream->runtime;
1271 if (subdivide == 0) {
1272 subdivide = runtime->oss.subdivision;
1273 if (subdivide == 0)
1274 subdivide = 1;
1275 return subdivide;
1276 }
1277 if (runtime->oss.subdivision || runtime->oss.fragshift)
1278 return -EINVAL;
1279 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1280 subdivide != 8 && subdivide != 16)
1281 return -EINVAL;
1282 runtime->oss.subdivision = subdivide;
1283 runtime->oss.params = 1;
1284 return subdivide;
1285 }
1286
1287 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1288 {
1289 int err = -EINVAL, idx;
1290
1291 for (idx = 1; idx >= 0; --idx) {
1292 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1293 if (substream == NULL)
1294 continue;
1295 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1296 return err;
1297 }
1298 return err;
1299 }
1300
1301 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1302 {
1303 snd_pcm_runtime_t *runtime;
1304
1305 if (substream == NULL)
1306 return 0;
1307 runtime = substream->runtime;
1308 if (runtime->oss.subdivision || runtime->oss.fragshift)
1309 return -EINVAL;
1310 runtime->oss.fragshift = val & 0xffff;
1311 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1312 if (runtime->oss.fragshift < 4) /* < 16 */
1313 runtime->oss.fragshift = 4;
1314 if (runtime->oss.maxfrags < 2)
1315 runtime->oss.maxfrags = 2;
1316 runtime->oss.params = 1;
1317 return 0;
1318 }
1319
1320 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1321 {
1322 int err = -EINVAL, idx;
1323
1324 for (idx = 1; idx >= 0; --idx) {
1325 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1326 if (substream == NULL)
1327 continue;
1328 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1329 return err;
1330 }
1331 return err;
1332 }
1333
1334 static int snd_pcm_oss_nonblock(struct file * file)
1335 {
1336 file->f_flags |= O_NONBLOCK;
1337 return 0;
1338 }
1339
1340 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1341 {
1342
1343 if (substream == NULL) {
1344 res &= ~DSP_CAP_DUPLEX;
1345 return res;
1346 }
1347 #ifdef DSP_CAP_MULTI
1348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1349 if (substream->pstr->substream_count > 1)
1350 res |= DSP_CAP_MULTI;
1351 #endif
1352 /* DSP_CAP_REALTIME is set all times: */
1353 /* all ALSA drivers can return actual pointer in ring buffer */
1354 #if defined(DSP_CAP_REALTIME) && 0
1355 {
1356 snd_pcm_runtime_t *runtime = substream->runtime;
1357 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1358 res &= ~DSP_CAP_REALTIME;
1359 }
1360 #endif
1361 return res;
1362 }
1363
1364 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1365 {
1366 int result, idx;
1367
1368 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1369 for (idx = 0; idx < 2; idx++) {
1370 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1371 result = snd_pcm_oss_get_caps1(substream, result);
1372 }
1373 result |= 0x0001; /* revision - same as SB AWE 64 */
1374 return result;
1375 }
1376
1377 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1378 {
1379 snd_pcm_runtime_t *runtime = substream->runtime;
1380 snd_pcm_uframes_t appl_ptr;
1381 appl_ptr = hw_ptr + runtime->buffer_size;
1382 appl_ptr %= runtime->boundary;
1383 runtime->control->appl_ptr = appl_ptr;
1384 }
1385
1386 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1387 {
1388 snd_pcm_runtime_t *runtime;
1389 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1390 int err, cmd;
1391
1392 #ifdef OSS_DEBUG
1393 printk("pcm_oss: trigger = 0x%x\n", trigger);
1394 #endif
1395
1396 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1397 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1398
1399 if (psubstream) {
1400 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1401 return err;
1402 }
1403 if (csubstream) {
1404 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1405 return err;
1406 }
1407 if (psubstream) {
1408 runtime = psubstream->runtime;
1409 if (trigger & PCM_ENABLE_OUTPUT) {
1410 if (runtime->oss.trigger)
1411 goto _skip1;
1412 if (atomic_read(&psubstream->runtime->mmap_count))
1413 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1414 runtime->oss.trigger = 1;
1415 runtime->start_threshold = 1;
1416 cmd = SNDRV_PCM_IOCTL_START;
1417 } else {
1418 if (!runtime->oss.trigger)
1419 goto _skip1;
1420 runtime->oss.trigger = 0;
1421 runtime->start_threshold = runtime->boundary;
1422 cmd = SNDRV_PCM_IOCTL_DROP;
1423 runtime->oss.prepare = 1;
1424 }
1425 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, NULL);
1426 if (err < 0)
1427 return err;
1428 }
1429 _skip1:
1430 if (csubstream) {
1431 runtime = csubstream->runtime;
1432 if (trigger & PCM_ENABLE_INPUT) {
1433 if (runtime->oss.trigger)
1434 goto _skip2;
1435 runtime->oss.trigger = 1;
1436 runtime->start_threshold = 1;
1437 cmd = SNDRV_PCM_IOCTL_START;
1438 } else {
1439 if (!runtime->oss.trigger)
1440 goto _skip2;
1441 runtime->oss.trigger = 0;
1442 runtime->start_threshold = runtime->boundary;
1443 cmd = SNDRV_PCM_IOCTL_DROP;
1444 runtime->oss.prepare = 1;
1445 }
1446 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1447 if (err < 0)
1448 return err;
1449 }
1450 _skip2:
1451 return 0;
1452 }
1453
1454 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1455 {
1456 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1457 int result = 0;
1458
1459 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1460 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1461 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1462 result |= PCM_ENABLE_OUTPUT;
1463 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1464 result |= PCM_ENABLE_INPUT;
1465 return result;
1466 }
1467
1468 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1469 {
1470 snd_pcm_substream_t *substream;
1471 snd_pcm_runtime_t *runtime;
1472 snd_pcm_sframes_t delay;
1473 int err;
1474
1475 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1476 if (substream == NULL)
1477 return -EINVAL;
1478 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1479 return err;
1480 runtime = substream->runtime;
1481 if (runtime->oss.params || runtime->oss.prepare)
1482 return 0;
1483 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1484 if (err == -EPIPE)
1485 delay = 0; /* hack for broken OSS applications */
1486 else if (err < 0)
1487 return err;
1488 return snd_pcm_oss_bytes(substream, delay);
1489 }
1490
1491 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info __user * _info)
1492 {
1493 snd_pcm_substream_t *substream;
1494 snd_pcm_runtime_t *runtime;
1495 snd_pcm_sframes_t delay;
1496 int fixup;
1497 struct count_info info;
1498 int err;
1499
1500 if (_info == NULL)
1501 return -EFAULT;
1502 substream = pcm_oss_file->streams[stream];
1503 if (substream == NULL)
1504 return -EINVAL;
1505 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1506 return err;
1507 runtime = substream->runtime;
1508 if (runtime->oss.params || runtime->oss.prepare) {
1509 memset(&info, 0, sizeof(info));
1510 if (copy_to_user(_info, &info, sizeof(info)))
1511 return -EFAULT;
1512 return 0;
1513 }
1514 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1515 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1516 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1517 err = 0;
1518 delay = 0;
1519 fixup = 0;
1520 } else {
1521 fixup = runtime->oss.buffer_used;
1522 }
1523 } else {
1524 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1525 fixup = -runtime->oss.buffer_used;
1526 }
1527 if (err < 0)
1528 return err;
1529 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1530 if (atomic_read(&runtime->mmap_count)) {
1531 snd_pcm_sframes_t n;
1532 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1533 if (n < 0)
1534 n += runtime->boundary;
1535 info.blocks = n / runtime->period_size;
1536 runtime->oss.prev_hw_ptr_interrupt = delay;
1537 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1538 snd_pcm_oss_simulate_fill(substream, delay);
1539 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1540 } else {
1541 delay = snd_pcm_oss_bytes(substream, delay);
1542 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1543 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
1544 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1545 } else {
1546 delay += fixup;
1547 info.blocks = delay / runtime->oss.period_bytes;
1548 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1549 }
1550 }
1551 if (copy_to_user(_info, &info, sizeof(info)))
1552 return -EFAULT;
1553 return 0;
1554 }
1555
1556 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1557 {
1558 snd_pcm_substream_t *substream;
1559 snd_pcm_runtime_t *runtime;
1560 snd_pcm_sframes_t avail;
1561 int fixup;
1562 struct audio_buf_info info;
1563 int err;
1564
1565 if (_info == NULL)
1566 return -EFAULT;
1567 substream = pcm_oss_file->streams[stream];
1568 if (substream == NULL)
1569 return -EINVAL;
1570 runtime = substream->runtime;
1571
1572 if (runtime->oss.params &&
1573 (err = snd_pcm_oss_change_params(substream)) < 0)
1574 return err;
1575
1576 info.fragsize = runtime->oss.period_bytes;
1577 info.fragstotal = runtime->periods;
1578 if (runtime->oss.prepare) {
1579 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1580 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1581 info.fragments = runtime->oss.periods;
1582 } else {
1583 info.bytes = 0;
1584 info.fragments = 0;
1585 }
1586 } else {
1587 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1588 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1589 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1590 avail = runtime->buffer_size;
1591 err = 0;
1592 fixup = 0;
1593 } else {
1594 avail = runtime->buffer_size - avail;
1595 fixup = -runtime->oss.buffer_used;
1596 }
1597 } else {
1598 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1599 fixup = runtime->oss.buffer_used;
1600 }
1601 if (err < 0)
1602 return err;
1603 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1604 info.fragments = info.bytes / runtime->oss.period_bytes;
1605 }
1606
1607 #ifdef OSS_DEBUG
1608 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1609 #endif
1610 if (copy_to_user(_info, &info, sizeof(info)))
1611 return -EFAULT;
1612 return 0;
1613 }
1614
1615 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1616 {
1617 // it won't be probably implemented
1618 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1619 return -EINVAL;
1620 }
1621
1622 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1623 {
1624 const char *ptr, *ptrl;
1625 snd_pcm_oss_setup_t *setup;
1626
1627 down(&pcm->streams[stream].oss.setup_mutex);
1628 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1629 if (!strcmp(setup->task_name, task_name)) {
1630 up(&pcm->streams[stream].oss.setup_mutex);
1631 return setup;
1632 }
1633 }
1634 ptr = ptrl = task_name;
1635 while (*ptr) {
1636 if (*ptr == '/')
1637 ptrl = ptr + 1;
1638 ptr++;
1639 }
1640 if (ptrl == task_name) {
1641 goto __not_found;
1642 return NULL;
1643 }
1644 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1645 if (!strcmp(setup->task_name, ptrl)) {
1646 up(&pcm->streams[stream].oss.setup_mutex);
1647 return setup;
1648 }
1649 }
1650 __not_found:
1651 up(&pcm->streams[stream].oss.setup_mutex);
1652 return NULL;
1653 }
1654
1655 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1656 snd_pcm_oss_setup_t *setup,
1657 int minor)
1658 {
1659 snd_pcm_runtime_t *runtime;
1660
1661 substream->oss.oss = 1;
1662 substream->oss.setup = setup;
1663 runtime = substream->runtime;
1664 runtime->oss.params = 1;
1665 runtime->oss.trigger = 1;
1666 runtime->oss.rate = 8000;
1667 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1668 case SNDRV_MINOR_OSS_PCM_8:
1669 runtime->oss.format = AFMT_U8;
1670 break;
1671 case SNDRV_MINOR_OSS_PCM_16:
1672 runtime->oss.format = AFMT_S16_LE;
1673 break;
1674 default:
1675 runtime->oss.format = AFMT_MU_LAW;
1676 }
1677 runtime->oss.channels = 1;
1678 runtime->oss.fragshift = 0;
1679 runtime->oss.maxfrags = 0;
1680 runtime->oss.subdivision = 0;
1681 }
1682
1683 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1684 {
1685 snd_pcm_runtime_t *runtime;
1686 runtime = substream->runtime;
1687 vfree(runtime->oss.buffer);
1688 snd_pcm_oss_plugin_clear(substream);
1689 substream->oss.file = NULL;
1690 substream->oss.oss = 0;
1691 }
1692
1693 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1694 {
1695 int cidx;
1696 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1697 for (cidx = 0; cidx < 2; ++cidx) {
1698 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1699 snd_pcm_runtime_t *runtime;
1700 if (substream == NULL)
1701 continue;
1702 runtime = substream->runtime;
1703
1704 snd_pcm_stream_lock_irq(substream);
1705 if (snd_pcm_running(substream))
1706 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1707 snd_pcm_stream_unlock_irq(substream);
1708 if (substream->open_flag) {
1709 if (substream->ops->hw_free != NULL)
1710 substream->ops->hw_free(substream);
1711 substream->ops->close(substream);
1712 substream->open_flag = 0;
1713 }
1714 substream->ffile = NULL;
1715 snd_pcm_oss_release_substream(substream);
1716 snd_pcm_release_substream(substream);
1717 }
1718 kfree(pcm_oss_file);
1719 return 0;
1720 }
1721
1722 static int snd_pcm_oss_open_file(struct file *file,
1723 snd_pcm_t *pcm,
1724 snd_pcm_oss_file_t **rpcm_oss_file,
1725 int minor,
1726 snd_pcm_oss_setup_t *psetup,
1727 snd_pcm_oss_setup_t *csetup)
1728 {
1729 int err = 0;
1730 snd_pcm_oss_file_t *pcm_oss_file;
1731 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1732 unsigned int f_mode = file->f_mode;
1733
1734 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1735 *rpcm_oss_file = NULL;
1736
1737 pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1738 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;
1744 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1745 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1746 &psubstream)) < 0) {
1747 snd_pcm_oss_release_file(pcm_oss_file);
1748 return err;
1749 }
1750 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1751 }
1752 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1753 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1754 &csubstream)) < 0) {
1755 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1756 snd_pcm_oss_release_file(pcm_oss_file);
1757 return err;
1758 } else {
1759 csubstream = NULL;
1760 }
1761 }
1762 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1763 }
1764
1765 if (psubstream == NULL && csubstream == NULL) {
1766 snd_pcm_oss_release_file(pcm_oss_file);
1767 return -EINVAL;
1768 }
1769 if (psubstream != NULL) {
1770 psubstream->oss.file = pcm_oss_file;
1771 err = snd_pcm_hw_constraints_init(psubstream);
1772 if (err < 0) {
1773 snd_printd("snd_pcm_hw_constraint_init failed\n");
1774 snd_pcm_oss_release_file(pcm_oss_file);
1775 return err;
1776 }
1777 if ((err = psubstream->ops->open(psubstream)) < 0) {
1778 snd_pcm_oss_release_file(pcm_oss_file);
1779 return err;
1780 }
1781 psubstream->open_flag = 1;
1782 err = snd_pcm_hw_constraints_complete(psubstream);
1783 if (err < 0) {
1784 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1785 snd_pcm_oss_release_file(pcm_oss_file);
1786 return err;
1787 }
1788 psubstream->ffile = file;
1789 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1790 }
1791 if (csubstream != NULL) {
1792 csubstream->oss.file = pcm_oss_file;
1793 err = snd_pcm_hw_constraints_init(csubstream);
1794 if (err < 0) {
1795 snd_printd("snd_pcm_hw_constraint_init failed\n");
1796 snd_pcm_oss_release_file(pcm_oss_file);
1797 return err;
1798 }
1799 if ((err = csubstream->ops->open(csubstream)) < 0) {
1800 snd_pcm_oss_release_file(pcm_oss_file);
1801 return err;
1802 }
1803 csubstream->open_flag = 1;
1804 err = snd_pcm_hw_constraints_complete(csubstream);
1805 if (err < 0) {
1806 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1807 snd_pcm_oss_release_file(pcm_oss_file);
1808 return err;
1809 }
1810 csubstream->ffile = file;
1811 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1812 }
1813
1814 file->private_data = pcm_oss_file;
1815 *rpcm_oss_file = pcm_oss_file;
1816 return 0;
1817 }
1818
1819
1820 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1821 {
1822 int minor = iminor(inode);
1823 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1824 int device;
1825 int err;
1826 char task_name[32];
1827 snd_pcm_t *pcm;
1828 snd_pcm_oss_file_t *pcm_oss_file;
1829 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1830 int nonblock;
1831 wait_queue_t wait;
1832
1833 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1834 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1835 adsp_map[cardnum] : dsp_map[cardnum];
1836
1837 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1838 if (pcm == NULL) {
1839 err = -ENODEV;
1840 goto __error1;
1841 }
1842 err = snd_card_file_add(pcm->card, file);
1843 if (err < 0)
1844 goto __error1;
1845 if (!try_module_get(pcm->card->module)) {
1846 err = -EFAULT;
1847 goto __error2;
1848 }
1849 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1850 err = -EFAULT;
1851 goto __error;
1852 }
1853 if (file->f_mode & FMODE_WRITE)
1854 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1855 if (file->f_mode & FMODE_READ)
1856 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1857
1858 nonblock = !!(file->f_flags & O_NONBLOCK);
1859 if (psetup && !psetup->disable) {
1860 if (psetup->nonblock)
1861 nonblock = 1;
1862 else if (psetup->block)
1863 nonblock = 0;
1864 } else if (csetup && !csetup->disable) {
1865 if (csetup->nonblock)
1866 nonblock = 1;
1867 else if (csetup->block)
1868 nonblock = 0;
1869 }
1870 if (!nonblock)
1871 nonblock = nonblock_open;
1872
1873 init_waitqueue_entry(&wait, current);
1874 add_wait_queue(&pcm->open_wait, &wait);
1875 down(&pcm->open_mutex);
1876 while (1) {
1877 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1878 minor, psetup, csetup);
1879 if (err >= 0)
1880 break;
1881 if (err == -EAGAIN) {
1882 if (nonblock) {
1883 err = -EBUSY;
1884 break;
1885 }
1886 } else
1887 break;
1888 set_current_state(TASK_INTERRUPTIBLE);
1889 up(&pcm->open_mutex);
1890 schedule();
1891 down(&pcm->open_mutex);
1892 if (signal_pending(current)) {
1893 err = -ERESTARTSYS;
1894 break;
1895 }
1896 }
1897 remove_wait_queue(&pcm->open_wait, &wait);
1898 up(&pcm->open_mutex);
1899 if (err < 0)
1900 goto __error;
1901 return err;
1902
1903 __error:
1904 module_put(pcm->card->module);
1905 __error2:
1906 snd_card_file_remove(pcm->card, file);
1907 __error1:
1908 return err;
1909 }
1910
1911 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1912 {
1913 snd_pcm_t *pcm;
1914 snd_pcm_substream_t *substream;
1915 snd_pcm_oss_file_t *pcm_oss_file;
1916
1917 pcm_oss_file = file->private_data;
1918 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1919 if (substream == NULL)
1920 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1921 snd_assert(substream != NULL, return -ENXIO);
1922 pcm = substream->pcm;
1923 snd_pcm_oss_sync(pcm_oss_file);
1924 down(&pcm->open_mutex);
1925 snd_pcm_oss_release_file(pcm_oss_file);
1926 up(&pcm->open_mutex);
1927 wake_up(&pcm->open_wait);
1928 module_put(pcm->card->module);
1929 snd_card_file_remove(pcm->card, file);
1930 return 0;
1931 }
1932
1933 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1934 {
1935 snd_pcm_oss_file_t *pcm_oss_file;
1936 int __user *p = (int __user *)arg;
1937 int res;
1938
1939 pcm_oss_file = file->private_data;
1940 if (cmd == OSS_GETVERSION)
1941 return put_user(SNDRV_OSS_VERSION, p);
1942 if (cmd == OSS_ALSAEMULVER)
1943 return put_user(1, p);
1944 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1945 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1946 snd_pcm_substream_t *substream;
1947 int idx;
1948 for (idx = 0; idx < 2; ++idx) {
1949 substream = pcm_oss_file->streams[idx];
1950 if (substream != NULL)
1951 break;
1952 }
1953 snd_assert(substream != NULL, return -ENXIO);
1954 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1955 }
1956 #endif
1957 if (((cmd >> 8) & 0xff) != 'P')
1958 return -EINVAL;
1959 #ifdef OSS_DEBUG
1960 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1961 #endif
1962 switch (cmd) {
1963 case SNDCTL_DSP_RESET:
1964 return snd_pcm_oss_reset(pcm_oss_file);
1965 case SNDCTL_DSP_SYNC:
1966 return snd_pcm_oss_sync(pcm_oss_file);
1967 case SNDCTL_DSP_SPEED:
1968 if (get_user(res, p))
1969 return -EFAULT;
1970 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1971 return res;
1972 return put_user(res, p);
1973 case SOUND_PCM_READ_RATE:
1974 res = snd_pcm_oss_get_rate(pcm_oss_file);
1975 if (res < 0)
1976 return res;
1977 return put_user(res, p);
1978 case SNDCTL_DSP_STEREO:
1979 if (get_user(res, p))
1980 return -EFAULT;
1981 res = res > 0 ? 2 : 1;
1982 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1983 return res;
1984 return put_user(--res, p);
1985 case SNDCTL_DSP_GETBLKSIZE:
1986 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1987 if (res < 0)
1988 return res;
1989 return put_user(res, p);
1990 case SNDCTL_DSP_SETFMT:
1991 if (get_user(res, p))
1992 return -EFAULT;
1993 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1994 if (res < 0)
1995 return res;
1996 return put_user(res, p);
1997 case SOUND_PCM_READ_BITS:
1998 res = snd_pcm_oss_get_format(pcm_oss_file);
1999 if (res < 0)
2000 return res;
2001 return put_user(res, p);
2002 case SNDCTL_DSP_CHANNELS:
2003 if (get_user(res, p))
2004 return -EFAULT;
2005 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2006 if (res < 0)
2007 return res;
2008 return put_user(res, p);
2009 case SOUND_PCM_READ_CHANNELS:
2010 res = snd_pcm_oss_get_channels(pcm_oss_file);
2011 if (res < 0)
2012 return res;
2013 return put_user(res, p);
2014 case SOUND_PCM_WRITE_FILTER:
2015 case SOUND_PCM_READ_FILTER:
2016 return -EIO;
2017 case SNDCTL_DSP_POST:
2018 return snd_pcm_oss_post(pcm_oss_file);
2019 case SNDCTL_DSP_SUBDIVIDE:
2020 if (get_user(res, p))
2021 return -EFAULT;
2022 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2023 if (res < 0)
2024 return res;
2025 return put_user(res, p);
2026 case SNDCTL_DSP_SETFRAGMENT:
2027 if (get_user(res, p))
2028 return -EFAULT;
2029 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2030 case SNDCTL_DSP_GETFMTS:
2031 res = snd_pcm_oss_get_formats(pcm_oss_file);
2032 if (res < 0)
2033 return res;
2034 return put_user(res, p);
2035 case SNDCTL_DSP_GETOSPACE:
2036 case SNDCTL_DSP_GETISPACE:
2037 return snd_pcm_oss_get_space(pcm_oss_file,
2038 cmd == SNDCTL_DSP_GETISPACE ?
2039 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2040 (struct audio_buf_info __user *) arg);
2041 case SNDCTL_DSP_NONBLOCK:
2042 return snd_pcm_oss_nonblock(file);
2043 case SNDCTL_DSP_GETCAPS:
2044 res = snd_pcm_oss_get_caps(pcm_oss_file);
2045 if (res < 0)
2046 return res;
2047 return put_user(res, p);
2048 case SNDCTL_DSP_GETTRIGGER:
2049 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2050 if (res < 0)
2051 return res;
2052 return put_user(res, p);
2053 case SNDCTL_DSP_SETTRIGGER:
2054 if (get_user(res, p))
2055 return -EFAULT;
2056 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2057 case SNDCTL_DSP_GETIPTR:
2058 case SNDCTL_DSP_GETOPTR:
2059 return snd_pcm_oss_get_ptr(pcm_oss_file,
2060 cmd == SNDCTL_DSP_GETIPTR ?
2061 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2062 (struct count_info __user *) arg);
2063 case SNDCTL_DSP_MAPINBUF:
2064 case SNDCTL_DSP_MAPOUTBUF:
2065 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2066 cmd == SNDCTL_DSP_MAPINBUF ?
2067 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2068 (struct buffmem_desc __user *) arg);
2069 case SNDCTL_DSP_SETSYNCRO:
2070 /* stop DMA now.. */
2071 return 0;
2072 case SNDCTL_DSP_SETDUPLEX:
2073 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2074 return 0;
2075 return -EIO;
2076 case SNDCTL_DSP_GETODELAY:
2077 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2078 if (res < 0) {
2079 /* it's for sure, some broken apps don't check for error codes */
2080 put_user(0, p);
2081 return res;
2082 }
2083 return put_user(res, p);
2084 case SNDCTL_DSP_PROFILE:
2085 return 0; /* silently ignore */
2086 default:
2087 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2088 }
2089 return -EINVAL;
2090 }
2091
2092 #ifdef CONFIG_COMPAT
2093 /* all compatible */
2094 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2095 #else
2096 #define snd_pcm_oss_ioctl_compat NULL
2097 #endif
2098
2099 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2100 {
2101 snd_pcm_oss_file_t *pcm_oss_file;
2102 snd_pcm_substream_t *substream;
2103
2104 pcm_oss_file = file->private_data;
2105 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2106 if (substream == NULL)
2107 return -ENXIO;
2108 #ifndef OSS_DEBUG
2109 return snd_pcm_oss_read1(substream, buf, count);
2110 #else
2111 {
2112 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2113 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2114 return res;
2115 }
2116 #endif
2117 }
2118
2119 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2120 {
2121 snd_pcm_oss_file_t *pcm_oss_file;
2122 snd_pcm_substream_t *substream;
2123 long result;
2124
2125 pcm_oss_file = file->private_data;
2126 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2127 if (substream == NULL)
2128 return -ENXIO;
2129 up(&file->f_dentry->d_inode->i_sem);
2130 result = snd_pcm_oss_write1(substream, buf, count);
2131 down(&file->f_dentry->d_inode->i_sem);
2132 #ifdef OSS_DEBUG
2133 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2134 #endif
2135 return result;
2136 }
2137
2138 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2139 {
2140 snd_pcm_runtime_t *runtime = substream->runtime;
2141 if (atomic_read(&runtime->mmap_count))
2142 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2143 else
2144 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2145 }
2146
2147 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2148 {
2149 snd_pcm_runtime_t *runtime = substream->runtime;
2150 if (atomic_read(&runtime->mmap_count))
2151 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2152 else
2153 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2154 }
2155
2156 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2157 {
2158 snd_pcm_oss_file_t *pcm_oss_file;
2159 unsigned int mask;
2160 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2161
2162 pcm_oss_file = file->private_data;
2163
2164 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2165 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2166
2167 mask = 0;
2168 if (psubstream != NULL) {
2169 snd_pcm_runtime_t *runtime = psubstream->runtime;
2170 poll_wait(file, &runtime->sleep, wait);
2171 snd_pcm_stream_lock_irq(psubstream);
2172 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2173 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2174 snd_pcm_oss_playback_ready(psubstream)))
2175 mask |= POLLOUT | POLLWRNORM;
2176 snd_pcm_stream_unlock_irq(psubstream);
2177 }
2178 if (csubstream != NULL) {
2179 snd_pcm_runtime_t *runtime = csubstream->runtime;
2180 enum sndrv_pcm_state ostate;
2181 poll_wait(file, &runtime->sleep, wait);
2182 snd_pcm_stream_lock_irq(csubstream);
2183 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2184 snd_pcm_oss_capture_ready(csubstream))
2185 mask |= POLLIN | POLLRDNORM;
2186 snd_pcm_stream_unlock_irq(csubstream);
2187 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2188 snd_pcm_oss_file_t ofile;
2189 memset(&ofile, 0, sizeof(ofile));
2190 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2191 runtime->oss.trigger = 0;
2192 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2193 }
2194 }
2195
2196 return mask;
2197 }
2198
2199 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2200 {
2201 snd_pcm_oss_file_t *pcm_oss_file;
2202 snd_pcm_substream_t *substream = NULL;
2203 snd_pcm_runtime_t *runtime;
2204 int err;
2205
2206 #ifdef OSS_DEBUG
2207 printk("pcm_oss: mmap begin\n");
2208 #endif
2209 pcm_oss_file = file->private_data;
2210 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2211 case VM_READ | VM_WRITE:
2212 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2213 if (substream)
2214 break;
2215 /* Fall through */
2216 case VM_READ:
2217 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2218 break;
2219 case VM_WRITE:
2220 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2221 break;
2222 default:
2223 return -EINVAL;
2224 }
2225 /* set VM_READ access as well to fix memset() routines that do
2226 reads before writes (to improve performance) */
2227 area->vm_flags |= VM_READ;
2228 if (substream == NULL)
2229 return -ENXIO;
2230 runtime = substream->runtime;
2231 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2232 return -EIO;
2233 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2234 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2235 else
2236 return -EIO;
2237
2238 if (runtime->oss.params) {
2239 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2240 return err;
2241 }
2242 if (runtime->oss.plugin_first != NULL)
2243 return -EIO;
2244
2245 if (area->vm_pgoff != 0)
2246 return -EINVAL;
2247
2248 err = snd_pcm_mmap_data(substream, file, area);
2249 if (err < 0)
2250 return err;
2251 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2252 runtime->silence_threshold = 0;
2253 runtime->silence_size = 0;
2254 #ifdef OSS_DEBUG
2255 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2256 #endif
2257 /* In mmap mode we never stop */
2258 runtime->stop_threshold = runtime->boundary;
2259
2260 return 0;
2261 }
2262
2263 /*
2264 * /proc interface
2265 */
2266
2267 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2268 snd_info_buffer_t * buffer)
2269 {
2270 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2271 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2272 down(&pstr->oss.setup_mutex);
2273 while (setup) {
2274 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2275 setup->task_name,
2276 setup->periods,
2277 setup->period_size,
2278 setup->disable ? " disable" : "",
2279 setup->direct ? " direct" : "",
2280 setup->block ? " block" : "",
2281 setup->nonblock ? " non-block" : "",
2282 setup->partialfrag ? " partial-frag" : "",
2283 setup->nosilence ? " no-silence" : "");
2284 setup = setup->next;
2285 }
2286 up(&pstr->oss.setup_mutex);
2287 }
2288
2289 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2290 {
2291 unsigned int idx;
2292 snd_pcm_substream_t *substream;
2293 snd_pcm_oss_setup_t *setup, *setupn;
2294
2295 for (idx = 0, substream = pstr->substream;
2296 idx < pstr->substream_count; idx++, substream = substream->next)
2297 substream->oss.setup = NULL;
2298 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2299 setup; setup = setupn) {
2300 setupn = setup->next;
2301 kfree(setup->task_name);
2302 kfree(setup);
2303 }
2304 pstr->oss.setup_list = NULL;
2305 }
2306
2307 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2308 snd_info_buffer_t * buffer)
2309 {
2310 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2311 char line[128], str[32], task_name[32], *ptr;
2312 int idx1;
2313 snd_pcm_oss_setup_t *setup, *setup1, template;
2314
2315 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2316 down(&pstr->oss.setup_mutex);
2317 memset(&template, 0, sizeof(template));
2318 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2319 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2320 snd_pcm_oss_proc_free_setup_list(pstr);
2321 up(&pstr->oss.setup_mutex);
2322 continue;
2323 }
2324 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2325 if (!strcmp(setup->task_name, task_name)) {
2326 template = *setup;
2327 break;
2328 }
2329 }
2330 ptr = snd_info_get_str(str, ptr, sizeof(str));
2331 template.periods = simple_strtoul(str, NULL, 10);
2332 ptr = snd_info_get_str(str, ptr, sizeof(str));
2333 template.period_size = simple_strtoul(str, NULL, 10);
2334 for (idx1 = 31; idx1 >= 0; idx1--)
2335 if (template.period_size & (1 << idx1))
2336 break;
2337 for (idx1--; idx1 >= 0; idx1--)
2338 template.period_size &= ~(1 << idx1);
2339 do {
2340 ptr = snd_info_get_str(str, ptr, sizeof(str));
2341 if (!strcmp(str, "disable")) {
2342 template.disable = 1;
2343 } else if (!strcmp(str, "direct")) {
2344 template.direct = 1;
2345 } else if (!strcmp(str, "block")) {
2346 template.block = 1;
2347 } else if (!strcmp(str, "non-block")) {
2348 template.nonblock = 1;
2349 } else if (!strcmp(str, "partial-frag")) {
2350 template.partialfrag = 1;
2351 } else if (!strcmp(str, "no-silence")) {
2352 template.nosilence = 1;
2353 }
2354 } while (*str);
2355 if (setup == NULL) {
2356 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2357 if (setup) {
2358 if (pstr->oss.setup_list == NULL) {
2359 pstr->oss.setup_list = setup;
2360 } else {
2361 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2362 setup1->next = setup;
2363 }
2364 template.task_name = kstrdup(task_name, GFP_KERNEL);
2365 } else {
2366 buffer->error = -ENOMEM;
2367 }
2368 }
2369 if (setup)
2370 *setup = template;
2371 up(&pstr->oss.setup_mutex);
2372 }
2373 }
2374
2375 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2376 {
2377 int stream;
2378 for (stream = 0; stream < 2; ++stream) {
2379 snd_info_entry_t *entry;
2380 snd_pcm_str_t *pstr = &pcm->streams[stream];
2381 if (pstr->substream_count == 0)
2382 continue;
2383 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2384 entry->content = SNDRV_INFO_CONTENT_TEXT;
2385 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2386 entry->c.text.read_size = 8192;
2387 entry->c.text.read = snd_pcm_oss_proc_read;
2388 entry->c.text.write_size = 8192;
2389 entry->c.text.write = snd_pcm_oss_proc_write;
2390 entry->private_data = pstr;
2391 if (snd_info_register(entry) < 0) {
2392 snd_info_free_entry(entry);
2393 entry = NULL;
2394 }
2395 }
2396 pstr->oss.proc_entry = entry;
2397 }
2398 }
2399
2400 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2401 {
2402 int stream;
2403 for (stream = 0; stream < 2; ++stream) {
2404 snd_pcm_str_t *pstr = &pcm->streams[stream];
2405 if (pstr->oss.proc_entry) {
2406 snd_info_unregister(pstr->oss.proc_entry);
2407 pstr->oss.proc_entry = NULL;
2408 snd_pcm_oss_proc_free_setup_list(pstr);
2409 }
2410 }
2411 }
2412
2413 /*
2414 * ENTRY functions
2415 */
2416
2417 static struct file_operations snd_pcm_oss_f_reg =
2418 {
2419 .owner = THIS_MODULE,
2420 .read = snd_pcm_oss_read,
2421 .write = snd_pcm_oss_write,
2422 .open = snd_pcm_oss_open,
2423 .release = snd_pcm_oss_release,
2424 .poll = snd_pcm_oss_poll,
2425 .unlocked_ioctl = snd_pcm_oss_ioctl,
2426 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2427 .mmap = snd_pcm_oss_mmap,
2428 };
2429
2430 static snd_minor_t snd_pcm_oss_reg =
2431 {
2432 .comment = "digital audio",
2433 .f_ops = &snd_pcm_oss_f_reg,
2434 };
2435
2436 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2437 {
2438 char name[128];
2439 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2440 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2441 pcm->card, index, &snd_pcm_oss_reg,
2442 name) < 0) {
2443 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2444 }
2445 }
2446
2447 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2448 {
2449 pcm->oss.reg = 0;
2450 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2451 char name[128];
2452 int duplex;
2453 register_oss_dsp(pcm, 0);
2454 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2455 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2456 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2457 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2458 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2459 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2460 pcm->card->number,
2461 name);
2462 #endif
2463 pcm->oss.reg++;
2464 pcm->oss.reg_mask |= 1;
2465 }
2466 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2467 register_oss_dsp(pcm, 1);
2468 pcm->oss.reg++;
2469 pcm->oss.reg_mask |= 2;
2470 }
2471
2472 if (pcm->oss.reg)
2473 snd_pcm_oss_proc_init(pcm);
2474
2475 return 0;
2476 }
2477
2478 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2479 {
2480 if (pcm->oss.reg) {
2481 if (pcm->oss.reg_mask & 1) {
2482 pcm->oss.reg_mask &= ~1;
2483 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2484 pcm->card, 0);
2485 }
2486 if (pcm->oss.reg_mask & 2) {
2487 pcm->oss.reg_mask &= ~2;
2488 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2489 pcm->card, 1);
2490 }
2491 }
2492 return 0;
2493 }
2494
2495 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2496 {
2497 snd_pcm_oss_disconnect_minor(pcm);
2498 if (pcm->oss.reg) {
2499 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2500 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2501 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2502 #endif
2503 }
2504 pcm->oss.reg = 0;
2505 snd_pcm_oss_proc_done(pcm);
2506 }
2507 return 0;
2508 }
2509
2510 static snd_pcm_notify_t snd_pcm_oss_notify =
2511 {
2512 .n_register = snd_pcm_oss_register_minor,
2513 .n_disconnect = snd_pcm_oss_disconnect_minor,
2514 .n_unregister = snd_pcm_oss_unregister_minor,
2515 };
2516
2517 static int __init alsa_pcm_oss_init(void)
2518 {
2519 int i;
2520 int err;
2521
2522 /* check device map table */
2523 for (i = 0; i < SNDRV_CARDS; i++) {
2524 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2525 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2526 dsp_map[i] = 0;
2527 }
2528 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2529 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2530 adsp_map[i] = 1;
2531 }
2532 }
2533 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2534 return err;
2535 return 0;
2536 }
2537
2538 static void __exit alsa_pcm_oss_exit(void)
2539 {
2540 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2541 }
2542
2543 module_init(alsa_pcm_oss_init)
2544 module_exit(alsa_pcm_oss_exit)
This page took 0.116007 seconds and 5 git commands to generate.