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