// 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) … #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 … #define STRING_SIZE … 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 sound_adapter { … }; static struct list_head adpt_list; #define MOST_PCM_INFO … static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes) { … } static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes) { … } static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes) { … } static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes) { … } static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes) { … } static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes) { … } static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes) { … } static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes) { … } static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes) { … } static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes) { … } static void most_to_alsa_copy32(void *alsa, void *most, unsigned int 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /* * Initialization of struct snd_pcm_ops */ static const struct snd_pcm_ops pcm_ops = …; static int split_arg_list(char *buf, u16 *ch_num, char **sample_res) { … } static const struct sample_resolution_info { … } sinfo[] = …; static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw, u16 ch_num, char *sample_res, struct most_channel_config *cfg) { … } static void release_adapter(struct sound_adapter *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) { … } static int audio_create_sound_card(void) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /* * Initialization of the struct most_component */ static struct most_component comp = …; static int __init audio_init(void) { … } static void __exit audio_exit(void) { … } module_init(…) …; module_exit(audio_exit); MODULE_LICENSE(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …;