Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | // SPDX-License-Identifier: GPL-2.0 /* * sound.c - Sound component for Mostcore * * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/module.h> #include <linux/printk.h> #include <linux/kernel.h> #include <linux/slab.h> #include <linux/init.h> #include <sound/core.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include <linux/sched.h> #include <linux/kthread.h> #include <linux/most.h> #define DRIVER_NAME "sound" #define STRING_SIZE 80 static struct most_component comp; /** * struct channel - private structure to keep channel specific data * @substream: stores the substream structure * @pcm_hardware: low-level hardware description * @iface: interface for which the channel belongs to * @cfg: channel configuration * @card: registered sound card * @list: list for private use * @id: channel index * @period_pos: current period position (ring buffer) * @buffer_pos: current buffer position (ring buffer) * @is_stream_running: identifies whether a stream is running or not * @opened: set when the stream is opened * @playback_task: playback thread * @playback_waitq: waitq used by playback thread * @copy_fn: copy function for PCM-specific format and width */ struct channel { struct snd_pcm_substream *substream; struct snd_pcm_hardware pcm_hardware; struct most_interface *iface; struct most_channel_config *cfg; struct snd_card *card; struct list_head list; int id; unsigned int period_pos; unsigned int buffer_pos; bool is_stream_running; struct task_struct *playback_task; wait_queue_head_t playback_waitq; void (*copy_fn)(void *alsa, void *most, unsigned int bytes); }; struct sound_adapter { struct list_head dev_list; struct most_interface *iface; struct snd_card *card; struct list_head list; bool registered; int pcm_dev_idx; }; static struct list_head adpt_list; #define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \ SNDRV_PCM_INFO_MMAP_VALID | \ SNDRV_PCM_INFO_BATCH | \ SNDRV_PCM_INFO_INTERLEAVED | \ SNDRV_PCM_INFO_BLOCK_TRANSFER) static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes) { unsigned int i = 0; while (i < (bytes / 2)) { dest[i] = swab16(source[i]); i++; } } static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes) { unsigned int i = 0; if (bytes < 2) return; while (i < bytes - 2) { dest[i] = source[i + 2]; dest[i + 1] = source[i + 1]; dest[i + 2] = source[i]; i += 3; } } static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes) { unsigned int i = 0; while (i < bytes / 4) { dest[i] = swab32(source[i]); i++; } } static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes) { memcpy(most, alsa, bytes); } static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes) { swap_copy16(most, alsa, bytes); } static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes) { swap_copy24(most, alsa, bytes); } static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes) { swap_copy32(most, alsa, bytes); } static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes) { memcpy(alsa, most, bytes); } static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes) { swap_copy16(alsa, most, bytes); } static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes) { swap_copy24(alsa, most, bytes); } static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes) { swap_copy32(alsa, most, bytes); } /** * get_channel - get pointer to channel * @iface: interface structure * @channel_id: channel ID * * This traverses the channel list and returns the channel matching the * ID and interface. * * Returns pointer to channel on success or NULL otherwise. */ static struct channel *get_channel(struct most_interface *iface, int channel_id) { struct sound_adapter *adpt = iface->priv; struct channel *channel; list_for_each_entry(channel, &adpt->dev_list, list) { if ((channel->iface == iface) && (channel->id == channel_id)) return channel; } return NULL; } /** * copy_data - implements data copying function * @channel: channel * @mbo: MBO from core * * Copy data from/to ring buffer to/from MBO and update the buffer position */ static bool copy_data(struct channel *channel, struct mbo *mbo) { struct snd_pcm_runtime *const runtime = channel->substream->runtime; unsigned int const frame_bytes = channel->cfg->subbuffer_size; unsigned int const buffer_size = runtime->buffer_size; unsigned int frames; unsigned int fr0; if (channel->cfg->direction & MOST_CH_RX) frames = mbo->processed_length / frame_bytes; else frames = mbo->buffer_length / frame_bytes; fr0 = min(buffer_size - channel->buffer_pos, frames); channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes, mbo->virt_address, fr0 * frame_bytes); if (frames > fr0) { /* wrap around at end of ring buffer */ channel->copy_fn(runtime->dma_area, mbo->virt_address + fr0 * frame_bytes, (frames - fr0) * frame_bytes); } channel->buffer_pos += frames; if (channel->buffer_pos >= buffer_size) channel->buffer_pos -= buffer_size; channel->period_pos += frames; if (channel->period_pos >= runtime->period_size) { channel->period_pos -= runtime->period_size; return true; } return false; } /** * playback_thread - function implements the playback thread * @data: private data * * Thread which does the playback functionality in a loop. It waits for a free * MBO from mostcore for a particular channel and copy the data from ring buffer * to MBO. Submit the MBO back to mostcore, after copying the data. * * Returns 0 on success or error code otherwise. */ static int playback_thread(void *data) { struct channel *const channel = data; while (!kthread_should_stop()) { struct mbo *mbo = NULL; bool period_elapsed = false; wait_event_interruptible( channel->playback_waitq, kthread_should_stop() || (channel->is_stream_running && (mbo = most_get_mbo(channel->iface, channel->id, &comp)))); if (!mbo) continue; if (channel->is_stream_running) period_elapsed = copy_data(channel, mbo); else memset(mbo->virt_address, 0, mbo->buffer_length); most_submit_mbo(mbo); if (period_elapsed) snd_pcm_period_elapsed(channel->substream); } return 0; } /** * pcm_open - implements open callback function for PCM middle layer * @substream: pointer to ALSA PCM substream * * This is called when a PCM substream is opened. At least, the function should * initialize the runtime->hw record. * * Returns 0 on success or error code otherwise. */ static int pcm_open(struct snd_pcm_substream *substream) { struct channel *channel = substream->private_data; struct snd_pcm_runtime *runtime = substream->runtime; struct most_channel_config *cfg = channel->cfg; int ret; channel->substream = substream; if (cfg->direction == MOST_CH_TX) { channel->playback_task = kthread_run(playback_thread, channel, "most_audio_playback"); if (IS_ERR(channel->playback_task)) { pr_err("Couldn't start thread\n"); return PTR_ERR(channel->playback_task); } } ret = most_start_channel(channel->iface, channel->id, &comp); if (ret) { pr_err("most_start_channel() failed!\n"); if (cfg->direction == MOST_CH_TX) kthread_stop(channel->playback_task); return ret; } runtime->hw = channel->pcm_hardware; return 0; } /** * pcm_close - implements close callback function for PCM middle layer * @substream: sub-stream pointer * * Obviously, this is called when a PCM substream is closed. Any private * instance for a PCM substream allocated in the open callback will be * released here. * * Returns 0 on success or error code otherwise. */ static int pcm_close(struct snd_pcm_substream *substream) { struct channel *channel = substream->private_data; if (channel->cfg->direction == MOST_CH_TX) kthread_stop(channel->playback_task); most_stop_channel(channel->iface, channel->id, &comp); return 0; } /** * pcm_prepare - implements prepare callback function for PCM middle layer * @substream: substream pointer * * This callback is called when the PCM is "prepared". Format rate, sample rate, * etc., can be set here. This callback can be called many times at each setup. * * Returns 0 on success or error code otherwise. */ static int pcm_prepare(struct snd_pcm_substream *substream) { struct channel *channel = substream->private_data; struct snd_pcm_runtime *runtime = substream->runtime; struct most_channel_config *cfg = channel->cfg; int width = snd_pcm_format_physical_width(runtime->format); channel->copy_fn = NULL; if (cfg->direction == MOST_CH_TX) { if (snd_pcm_format_big_endian(runtime->format) || width == 8) channel->copy_fn = alsa_to_most_memcpy; else if (width == 16) channel->copy_fn = alsa_to_most_copy16; else if (width == 24) channel->copy_fn = alsa_to_most_copy24; else if (width == 32) channel->copy_fn = alsa_to_most_copy32; } else { if (snd_pcm_format_big_endian(runtime->format) || width == 8) channel->copy_fn = most_to_alsa_memcpy; else if (width == 16) channel->copy_fn = most_to_alsa_copy16; else if (width == 24) channel->copy_fn = most_to_alsa_copy24; else if (width == 32) channel->copy_fn = most_to_alsa_copy32; } if (!channel->copy_fn) return -EINVAL; channel->period_pos = 0; channel->buffer_pos = 0; return 0; } /** * pcm_trigger - implements trigger callback function for PCM middle layer * @substream: substream pointer * @cmd: action to perform * * This is called when the PCM is started, stopped or paused. The action will be * specified in the second argument, SNDRV_PCM_TRIGGER_XXX * * Returns 0 on success or error code otherwise. */ static int pcm_trigger(struct snd_pcm_substream *substream, int cmd) { struct channel *channel = substream->private_data; switch (cmd) { case SNDRV_PCM_TRIGGER_START: channel->is_stream_running = true; wake_up_interruptible(&channel->playback_waitq); return 0; case SNDRV_PCM_TRIGGER_STOP: channel->is_stream_running = false; return 0; default: return -EINVAL; } return 0; } /** * pcm_pointer - implements pointer callback function for PCM middle layer * @substream: substream pointer * * This callback is called when the PCM middle layer inquires the current * hardware position on the buffer. The position must be returned in frames, * ranging from 0 to buffer_size-1. */ static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream) { struct channel *channel = substream->private_data; return channel->buffer_pos; } /* * Initialization of struct snd_pcm_ops */ static const struct snd_pcm_ops pcm_ops = { .open = pcm_open, .close = pcm_close, .prepare = pcm_prepare, .trigger = pcm_trigger, .pointer = pcm_pointer, }; static int split_arg_list(char *buf, u16 *ch_num, char **sample_res) { char *num; int ret; num = strsep(&buf, "x"); if (!num) goto err; ret = kstrtou16(num, 0, ch_num); if (ret) goto err; *sample_res = strsep(&buf, ".\n"); if (!*sample_res) goto err; return 0; err: pr_err("Bad PCM format\n"); return -EINVAL; } static const struct sample_resolution_info { const char *sample_res; int bytes; u64 formats; } sinfo[] = { { "8", 1, SNDRV_PCM_FMTBIT_S8 }, { "16", 2, SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE }, { "24", 3, SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE }, { "32", 4, SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE }, }; static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw, u16 ch_num, char *sample_res, struct most_channel_config *cfg) { int i; for (i = 0; i < ARRAY_SIZE(sinfo); i++) { if (!strcmp(sample_res, sinfo[i].sample_res)) goto found; } pr_err("Unsupported PCM format\n"); return -EINVAL; found: if (!ch_num) { pr_err("Bad number of channels\n"); return -EINVAL; } if (cfg->subbuffer_size != ch_num * sinfo[i].bytes) { pr_err("Audio resolution doesn't fit subbuffer size\n"); return -EINVAL; } pcm_hw->info = MOST_PCM_INFO; pcm_hw->rates = SNDRV_PCM_RATE_48000; pcm_hw->rate_min = 48000; pcm_hw->rate_max = 48000; pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size; pcm_hw->period_bytes_min = cfg->buffer_size; pcm_hw->period_bytes_max = cfg->buffer_size; pcm_hw->periods_min = 1; pcm_hw->periods_max = cfg->num_buffers; pcm_hw->channels_min = ch_num; pcm_hw->channels_max = ch_num; pcm_hw->formats = sinfo[i].formats; return 0; } static void release_adapter(struct sound_adapter *adpt) { struct channel *channel, *tmp; list_for_each_entry_safe(channel, tmp, &adpt->dev_list, list) { list_del(&channel->list); kfree(channel); } if (adpt->card) snd_card_free(adpt->card); list_del(&adpt->list); kfree(adpt); } /** * audio_probe_channel - probe function of the driver module * @iface: pointer to interface instance * @channel_id: channel index/ID * @cfg: pointer to actual channel configuration * @device_name: name of the device to be created in /dev * @arg_list: string that provides the desired audio resolution * * Creates sound card, pcm device, sets pcm ops and registers sound card. * * Returns 0 on success or error code otherwise. */ static int audio_probe_channel(struct most_interface *iface, int channel_id, struct most_channel_config *cfg, char *device_name, char *arg_list) { struct channel *channel; struct sound_adapter *adpt; struct snd_pcm *pcm; int playback_count = 0; int capture_count = 0; int ret; int direction; u16 ch_num; char *sample_res; char arg_list_cpy[STRING_SIZE]; if (cfg->data_type != MOST_CH_SYNC) { pr_err("Incompatible channel type\n"); return -EINVAL; } strscpy(arg_list_cpy, arg_list, STRING_SIZE); ret = split_arg_list(arg_list_cpy, &ch_num, &sample_res); if (ret < 0) return ret; list_for_each_entry(adpt, &adpt_list, list) { if (adpt->iface != iface) continue; if (adpt->registered) return -ENOSPC; adpt->pcm_dev_idx++; goto skip_adpt_alloc; } adpt = kzalloc(sizeof(*adpt), GFP_KERNEL); if (!adpt) return -ENOMEM; adpt->iface = iface; INIT_LIST_HEAD(&adpt->dev_list); iface->priv = adpt; list_add_tail(&adpt->list, &adpt_list); ret = snd_card_new(iface->driver_dev, -1, "INIC", THIS_MODULE, sizeof(*channel), &adpt->card); if (ret < 0) goto err_free_adpt; snprintf(adpt->card->driver, sizeof(adpt->card->driver), "%s", DRIVER_NAME); snprintf(adpt->card->shortname, sizeof(adpt->card->shortname), "Microchip INIC"); snprintf(adpt->card->longname, sizeof(adpt->card->longname), "%s at %s", adpt->card->shortname, iface->description); skip_adpt_alloc: if (get_channel(iface, channel_id)) { pr_err("channel (%s:%d) is already linked\n", iface->description, channel_id); return -EEXIST; } if (cfg->direction == MOST_CH_TX) { playback_count = 1; direction = SNDRV_PCM_STREAM_PLAYBACK; } else { capture_count = 1; direction = SNDRV_PCM_STREAM_CAPTURE; } channel = kzalloc(sizeof(*channel), GFP_KERNEL); if (!channel) { ret = -ENOMEM; goto err_free_adpt; } channel->card = adpt->card; channel->cfg = cfg; channel->iface = iface; channel->id = channel_id; init_waitqueue_head(&channel->playback_waitq); list_add_tail(&channel->list, &adpt->dev_list); ret = audio_set_hw_params(&channel->pcm_hardware, ch_num, sample_res, cfg); if (ret) goto err_free_adpt; ret = snd_pcm_new(adpt->card, device_name, adpt->pcm_dev_idx, playback_count, capture_count, &pcm); if (ret < 0) goto err_free_adpt; pcm->private_data = channel; strscpy(pcm->name, device_name, sizeof(pcm->name)); snd_pcm_set_ops(pcm, direction, &pcm_ops); snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); return 0; err_free_adpt: release_adapter(adpt); return ret; } static int audio_create_sound_card(void) { int ret; struct sound_adapter *adpt; list_for_each_entry(adpt, &adpt_list, list) { if (!adpt->registered) goto adpt_alloc; } return -ENODEV; adpt_alloc: ret = snd_card_register(adpt->card); if (ret < 0) { release_adapter(adpt); return ret; } adpt->registered = true; return 0; } /** * audio_disconnect_channel - function to disconnect a channel * @iface: pointer to interface instance * @channel_id: channel index * * This frees allocated memory and removes the sound card from ALSA * * Returns 0 on success or error code otherwise. */ static int audio_disconnect_channel(struct most_interface *iface, int channel_id) { struct channel *channel; struct sound_adapter *adpt = iface->priv; channel = get_channel(iface, channel_id); if (!channel) return -EINVAL; list_del(&channel->list); kfree(channel); if (list_empty(&adpt->dev_list)) release_adapter(adpt); return 0; } /** * audio_rx_completion - completion handler for rx channels * @mbo: pointer to buffer object that has completed * * This searches for the channel this MBO belongs to and copy the data from MBO * to ring buffer * * Returns 0 on success or error code otherwise. */ static int audio_rx_completion(struct mbo *mbo) { struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id); bool period_elapsed = false; if (!channel) return -EINVAL; if (channel->is_stream_running) period_elapsed = copy_data(channel, mbo); most_put_mbo(mbo); if (period_elapsed) snd_pcm_period_elapsed(channel->substream); return 0; } /** * audio_tx_completion - completion handler for tx channels * @iface: pointer to interface instance * @channel_id: channel index/ID * * This searches the channel that belongs to this combination of interface * pointer and channel ID and wakes a process sitting in the wait queue of * this channel. * * Returns 0 on success or error code otherwise. */ static int audio_tx_completion(struct most_interface *iface, int channel_id) { struct channel *channel = get_channel(iface, channel_id); if (!channel) return -EINVAL; wake_up_interruptible(&channel->playback_waitq); return 0; } /* * Initialization of the struct most_component */ static struct most_component comp = { .mod = THIS_MODULE, .name = DRIVER_NAME, .probe_channel = audio_probe_channel, .disconnect_channel = audio_disconnect_channel, .rx_completion = audio_rx_completion, .tx_completion = audio_tx_completion, .cfg_complete = audio_create_sound_card, }; static int __init audio_init(void) { int ret; INIT_LIST_HEAD(&adpt_list); ret = most_register_component(&comp); if (ret) { pr_err("Failed to register %s\n", comp.name); return ret; } ret = most_register_configfs_subsys(&comp); if (ret) { pr_err("Failed to register %s configfs subsys\n", comp.name); most_deregister_component(&comp); } return ret; } static void __exit audio_exit(void) { most_deregister_configfs_subsys(&comp); most_deregister_component(&comp); } module_init(audio_init); module_exit(audio_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); MODULE_DESCRIPTION("Sound Component Module for Mostcore"); |