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