Loading drivers/usb/gadget/function/f_uac1_legacy.c +925 −309 File changed.Preview size limit exceeded, changes collapsed. Show changes drivers/usb/gadget/function/u_uac1_legacy.c +475 −23 Original line number Diff line number Diff line /* * u_uac1.c -- ALSA audio utilities for Gadget stack * u_uac1_legacy.c -- ALSA audio utilities for Gadget stack * * Copyright (c) 2012-2016,2020, The Linux Foundation. All rights reserved. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> * Copyright (C) 2008 Analog Devices, Inc * Loading @@ -17,9 +18,15 @@ #include <linux/ctype.h> #include <linux/random.h> #include <linux/syscalls.h> #include <linux/configfs.h> #include <sound/pcm.h> #include "u_uac1_legacy.h" #ifdef pr_fmt #undef pr_fmt #endif #define pr_fmt(fmt) "%s: " fmt, __func__ /* * This component encapsulates the ALSA devices for USB audio gadget */ Loading Loading @@ -89,25 +96,54 @@ static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params, } /*-------------------------------------------------------------------------*/ static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n) { return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); } static int pcm_buffer_size(struct snd_pcm_hw_params *params) { struct snd_interval *i = param_to_interval(params, SNDRV_PCM_HW_PARAM_BUFFER_BYTES); pr_debug("buffer_bytes = (%d,%d) omin=%d omax=%d int=%d empty=%d\n", i->min, i->max, i->openmin, i->openmax, i->integer, i->empty); return i->min; } static int pcm_period_size(struct snd_pcm_hw_params *params) { struct snd_interval *i = param_to_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES); return i->min; } /** * Set default hardware params */ static int playback_default_hw_params(struct gaudio_snd_dev *snd) static int playback_prepare_params(struct gaudio_snd_dev *snd) { struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_hw_params *params; struct snd_pcm_sw_params *swparams; unsigned long period_size; unsigned long buffer_size; snd_pcm_sframes_t result; struct gaudio *card = container_of(snd, struct gaudio, playback); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 2 * RATE: 48000 * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 2; snd->rate = 48000; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) Loading @@ -123,17 +159,200 @@ static int playback_default_hw_params(struct gaudio_snd_dev *snd) _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_DROP failed: %d\n", (int)result); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); if (result < 0) { ERROR(snd->card, "SNDRV_PCM_IOCTL_HW_PARAMS failed: %d\n", (int)result); kfree(params); return result; } result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) ERROR(snd->card, "Preparing sound card failed: %d\n", (int)result); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); /* Set SW params */ swparams = kzalloc(sizeof(*swparams), GFP_KERNEL); if (!swparams) { kfree(params); return -ENOMEM; } buffer_size = pcm_buffer_size(params); period_size = pcm_period_size(params); swparams->avail_min = period_size/2; swparams->xfer_align = period_size/2; swparams->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; swparams->period_step = 1; swparams->start_threshold = 1; swparams->stop_threshold = INT_MAX; swparams->silence_size = 0; swparams->silence_threshold = 0; result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, swparams); if (result < 0) pr_err("SNDRV_PCM_IOCTL_SW_PARAMS failed: %d\n", (int)result); kfree(swparams); kfree(params); pr_debug("playback params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return 0; } static int capture_prepare_params(struct gaudio_snd_dev *snd) { struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; struct snd_pcm_hw_params *params; struct snd_pcm_sw_params *swparams; unsigned long period_size; unsigned long buffer_size; snd_pcm_sframes_t result = 0; struct gaudio *card = container_of(snd, struct gaudio, capture); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_DROP failed: %d\n", (int)result); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); if (result < 0) { ERROR(snd->card, "SNDRV_PCM_IOCTL_HW_PARAMS failed: %d\n", (int)result); kfree(params); return result; } result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) ERROR(snd->card, "Preparing capture failed: %d\n", (int)result); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); runtime->frame_bits = snd_pcm_format_physical_width(runtime->format); swparams = kzalloc(sizeof(*swparams), GFP_KERNEL); if (!swparams) { kfree(params); return -ENOMEM; } buffer_size = pcm_buffer_size(params); period_size = pcm_period_size(params); swparams->avail_min = period_size/2; swparams->xfer_align = period_size/2; swparams->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; swparams->period_step = 1; swparams->start_threshold = 1; swparams->stop_threshold = INT_MAX; swparams->silence_size = 0; swparams->silence_threshold = 0; result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, swparams); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_SW_PARAMS failed: %d\n", (int)result); kfree(swparams); kfree(params); INFO(snd->card, "capture params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return result; } /** * Set default hardware params */ static int playback_default_hw_params(struct gaudio_snd_dev *snd) { struct snd_pcm_hw_params *params; struct gaudio *card = container_of(snd, struct gaudio, playback); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); Loading @@ -149,6 +368,110 @@ static int playback_default_hw_params(struct gaudio_snd_dev *snd) return 0; } static int capture_default_hw_params(struct gaudio_snd_dev *snd) { struct snd_pcm_hw_params *params; struct gaudio *card = container_of(snd, struct gaudio, capture); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); kfree(params); INFO(snd->card, "capture params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return 0; } static int gaudio_open_playback_streams(struct gaudio *the_card) { struct gaudio_snd_dev *snd; int res = 0; if (!the_card) { pr_err("%s: Card is NULL", __func__); return -ENODEV; } pr_debug("Initialize hw params"); /* Open PCM playback device and setup substream */ snd = &the_card->playback; res = playback_prepare_params(snd); if (res) { pr_err("Setting playback params failed: err %d", res); return res; } pr_debug("Initialized playback params"); return 0; } static int gaudio_open_capture_streams(struct gaudio *the_card) { struct gaudio_snd_dev *snd; int res = 0; if (!the_card) { pr_err("%s: Card is NULL", __func__); return -ENODEV; } pr_debug("Initialize hw params"); /* Open PCM capture device and setup substream */ snd = &the_card->capture; res = capture_prepare_params(snd); if (res) { pr_err("Setting capture params failed: err %d", res); return res; } pr_info("Initialized capture params"); return 0; } void u_audio_clear(struct gaudio *card) { card->audio_reinit_playback = false; card->audio_reinit_capture = false; } /** * Playback audio buffer data by ALSA PCM device */ Loading @@ -157,12 +480,29 @@ size_t u_audio_playback(struct gaudio *card, void *buf, size_t count) struct gaudio_snd_dev *snd = &card->playback; struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; mm_segment_t old_fs; ssize_t result; snd_pcm_sframes_t frames; int err = 0; if (!count) { pr_err("Buffer is empty, no data to play"); return 0; } if (!card->audio_reinit_playback) { err = gaudio_open_playback_streams(card); if (err) { pr_err("Failed to init audio streams"); return 0; } card->audio_reinit_playback = 1; } try_again: if (runtime->status->state == SNDRV_PCM_STATE_XRUN || runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { runtime->status->state == SNDRV_PCM_STATE_SUSPENDED || runtime->status->state == SNDRV_PCM_STATE_SETUP) { result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) { Loading @@ -172,26 +512,107 @@ size_t u_audio_playback(struct gaudio *card, void *buf, size_t count) } } if (!runtime->frame_bits) { ERROR(card, "SND failure - runtime->frame_bits == 0"); return 0; } frames = bytes_to_frames(runtime, count); result = snd_pcm_kernel_write(snd->substream, buf, frames); pr_debug("runtime->frame_bits = %d, count = %d, frames = %d", runtime->frame_bits, (int)count, (int)frames); old_fs = get_fs(); set_fs(KERNEL_DS); result = snd_pcm_lib_write(snd->substream, (void __user *)buf, frames); if (result != frames) { ERROR(card, "Playback error: %d\n", (int)result); set_fs(old_fs); goto try_again; } set_fs(old_fs); pr_debug("Done. Sent %d frames", (int)frames); return 0; } size_t u_audio_capture(struct gaudio *card, void *buf, size_t count) { ssize_t result; mm_segment_t old_fs; snd_pcm_sframes_t frames; int err = 0; struct gaudio_snd_dev *snd = &card->capture; struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; if (!card->audio_reinit_capture) { err = gaudio_open_capture_streams(card); if (err) { pr_err("Failed to init audio streams: err %d", err); return 0; } card->audio_reinit_capture = 1; } try_again: if (runtime->status->state == SNDRV_PCM_STATE_XRUN || runtime->status->state == SNDRV_PCM_STATE_SUSPENDED || runtime->status->state == SNDRV_PCM_STATE_SETUP) { result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) { pr_err("Preparing capture failed: %d\n", (int)result); return result; } } frames = bytes_to_frames(runtime, count); old_fs = get_fs(); set_fs(KERNEL_DS); pr_debug("frames = %d, count = %zd", (int)frames, count); result = snd_pcm_lib_read(substream, buf, frames); if (result != frames) { pr_err("Capture error: %d\n", (int)result); set_fs(old_fs); goto try_again; } set_fs(old_fs); return 0; } int u_audio_get_playback_channels(struct gaudio *card) { pr_debug("Return %d", card->playback.channels); return card->playback.channels; } int u_audio_get_playback_rate(struct gaudio *card) { pr_debug("Return %d", card->playback.rate); return card->playback.rate; } int u_audio_get_capture_channels(struct gaudio *card) { pr_debug("Return %d", card->capture.channels); return card->capture.channels; } int u_audio_get_capture_rate(struct gaudio *card) { pr_debug("Return %d", card->capture.rate); return card->capture.rate; } /** * Open ALSA PCM and control device files * Initial the PCM or control device Loading @@ -202,9 +623,10 @@ static int gaudio_open_snd_dev(struct gaudio *card) struct gaudio_snd_dev *snd; struct f_uac1_legacy_opts *opts; char *fn_play, *fn_cap, *fn_cntl; int res = 0; opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); fn_play = opts->fn_play; fn_cap = opts->fn_cap; fn_cntl = opts->fn_cntl; Loading @@ -231,10 +653,16 @@ static int gaudio_open_snd_dev(struct gaudio *card) snd->filp = NULL; return ret; } pr_debug("Initialized PCM playback device: %s\n", fn_play); pcm_file = snd->filp->private_data; snd->substream = pcm_file->substream; snd->card = card; playback_default_hw_params(snd); res = playback_default_hw_params(snd); if (res) { ERROR(card, "Setting playback HW params failed: err %d", res); return res; } /* Open PCM capture device and setup substream */ snd = &card->capture; Loading @@ -244,13 +672,19 @@ static int gaudio_open_snd_dev(struct gaudio *card) snd->substream = NULL; snd->card = NULL; snd->filp = NULL; } else { return PTR_ERR(snd->filp); } pcm_file = snd->filp->private_data; snd->substream = pcm_file->substream; snd->card = card; } return 0; res = capture_default_hw_params(snd); if (res) ERROR(card, "Setting capture HW params failed: err %d", res); return res; } /** Loading @@ -260,20 +694,30 @@ static int gaudio_close_snd_dev(struct gaudio *gau) { struct gaudio_snd_dev *snd; pr_debug("Enter"); /* Close control device */ snd = &gau->control; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; /* Close PCM playback device and setup substream */ snd = &gau->playback; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; /* Close PCM capture device and setup substream */ snd = &gau->capture; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; return 0; } Loading @@ -287,8 +731,16 @@ static int gaudio_close_snd_dev(struct gaudio *gau) */ int gaudio_setup(struct gaudio *card) { struct gaudio_snd_dev *snd; int ret; snd = &card->control; if (snd->card) { pr_debug("snd devices already opened\n"); return 0; } pr_debug("trying to open snd devices\n"); ret = gaudio_open_snd_dev(card); if (ret) ERROR(card, "we need at least one control device\n"); Loading drivers/usb/gadget/function/u_uac1_legacy.h +26 −11 Original line number Diff line number Diff line /* * u_uac1.h -- interface to USB gadget "ALSA AUDIO" utilities * u_uac1_legacy.h -- interface to USB gadget "ALSA AUDIO" utilities * * Copyright (c) 2020, The Linux Foundation. All rights reserved. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> * Copyright (C) 2008 Analog Devices, Inc * Loading @@ -8,7 +9,6 @@ * * Licensed under the GPL-2 or later. */ #ifndef __U_UAC1_LEGACY_H #define __U_UAC1_LEGACY_H Loading @@ -21,13 +21,17 @@ #include <sound/pcm.h> #include <sound/pcm_params.h> #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p" #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c" #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D5p" #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D6c" #define FILE_CONTROL "/dev/snd/controlC0" #define UAC1_OUT_EP_MAX_PACKET_SIZE 200 #define UAC1_REQ_COUNT 256 #define UAC1_AUDIO_BUF_SIZE 48000 #define UAC1_IN_EP_MAX_PACKET_SIZE 32 #define UAC1_OUT_EP_MAX_PACKET_SIZE 32 #define UAC1_OUT_REQ_COUNT 48 #define UAC1_IN_REQ_COUNT 4 #define UAC1_AUDIO_PLAYBACK_BUF_SIZE 256 /* Matches with Audio driver */ #define UAC1_AUDIO_CAPTURE_BUF_SIZE 256 /* Matches with Audio driver */ #define UAC1_SAMPLE_RATE 16000 /* * This represents the USB side of an audio card device, managed by a USB Loading @@ -53,14 +57,20 @@ struct gaudio { struct gaudio_snd_dev playback; struct gaudio_snd_dev capture; /* TODO */ bool audio_reinit_capture; bool audio_reinit_playback; }; struct f_uac1_legacy_opts { struct usb_function_instance func_inst; int req_buf_size; int req_count; int audio_buf_size; int req_playback_buf_size; int req_capture_buf_size; int req_playback_count; int req_capture_count; int audio_playback_buf_size; int audio_capture_buf_size; int audio_playback_realtime; int sample_rate; char *fn_play; char *fn_cap; char *fn_cntl; Loading @@ -68,6 +78,7 @@ struct f_uac1_legacy_opts { unsigned fn_play_alloc:1; unsigned fn_cap_alloc:1; unsigned fn_cntl_alloc:1; struct gaudio *card; struct mutex lock; int refcnt; }; Loading @@ -75,8 +86,12 @@ struct f_uac1_legacy_opts { int gaudio_setup(struct gaudio *card); void gaudio_cleanup(struct gaudio *the_card); void u_audio_clear(struct gaudio *card); size_t u_audio_playback(struct gaudio *card, void *buf, size_t count); size_t u_audio_capture(struct gaudio *card, void *buf, size_t count); int u_audio_get_playback_channels(struct gaudio *card); int u_audio_get_playback_rate(struct gaudio *card); int u_audio_get_capture_channels(struct gaudio *card); int u_audio_get_capture_rate(struct gaudio *card); #endif /* __U_UAC1_LEGACY_H */ Loading
drivers/usb/gadget/function/f_uac1_legacy.c +925 −309 File changed.Preview size limit exceeded, changes collapsed. Show changes
drivers/usb/gadget/function/u_uac1_legacy.c +475 −23 Original line number Diff line number Diff line /* * u_uac1.c -- ALSA audio utilities for Gadget stack * u_uac1_legacy.c -- ALSA audio utilities for Gadget stack * * Copyright (c) 2012-2016,2020, The Linux Foundation. All rights reserved. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> * Copyright (C) 2008 Analog Devices, Inc * Loading @@ -17,9 +18,15 @@ #include <linux/ctype.h> #include <linux/random.h> #include <linux/syscalls.h> #include <linux/configfs.h> #include <sound/pcm.h> #include "u_uac1_legacy.h" #ifdef pr_fmt #undef pr_fmt #endif #define pr_fmt(fmt) "%s: " fmt, __func__ /* * This component encapsulates the ALSA devices for USB audio gadget */ Loading Loading @@ -89,25 +96,54 @@ static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params, } /*-------------------------------------------------------------------------*/ static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n) { return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); } static int pcm_buffer_size(struct snd_pcm_hw_params *params) { struct snd_interval *i = param_to_interval(params, SNDRV_PCM_HW_PARAM_BUFFER_BYTES); pr_debug("buffer_bytes = (%d,%d) omin=%d omax=%d int=%d empty=%d\n", i->min, i->max, i->openmin, i->openmax, i->integer, i->empty); return i->min; } static int pcm_period_size(struct snd_pcm_hw_params *params) { struct snd_interval *i = param_to_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES); return i->min; } /** * Set default hardware params */ static int playback_default_hw_params(struct gaudio_snd_dev *snd) static int playback_prepare_params(struct gaudio_snd_dev *snd) { struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_hw_params *params; struct snd_pcm_sw_params *swparams; unsigned long period_size; unsigned long buffer_size; snd_pcm_sframes_t result; struct gaudio *card = container_of(snd, struct gaudio, playback); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 2 * RATE: 48000 * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 2; snd->rate = 48000; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) Loading @@ -123,17 +159,200 @@ static int playback_default_hw_params(struct gaudio_snd_dev *snd) _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_DROP failed: %d\n", (int)result); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); if (result < 0) { ERROR(snd->card, "SNDRV_PCM_IOCTL_HW_PARAMS failed: %d\n", (int)result); kfree(params); return result; } result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) ERROR(snd->card, "Preparing sound card failed: %d\n", (int)result); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); /* Set SW params */ swparams = kzalloc(sizeof(*swparams), GFP_KERNEL); if (!swparams) { kfree(params); return -ENOMEM; } buffer_size = pcm_buffer_size(params); period_size = pcm_period_size(params); swparams->avail_min = period_size/2; swparams->xfer_align = period_size/2; swparams->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; swparams->period_step = 1; swparams->start_threshold = 1; swparams->stop_threshold = INT_MAX; swparams->silence_size = 0; swparams->silence_threshold = 0; result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, swparams); if (result < 0) pr_err("SNDRV_PCM_IOCTL_SW_PARAMS failed: %d\n", (int)result); kfree(swparams); kfree(params); pr_debug("playback params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return 0; } static int capture_prepare_params(struct gaudio_snd_dev *snd) { struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; struct snd_pcm_hw_params *params; struct snd_pcm_sw_params *swparams; unsigned long period_size; unsigned long buffer_size; snd_pcm_sframes_t result = 0; struct gaudio *card = container_of(snd, struct gaudio, capture); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_DROP failed: %d\n", (int)result); result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); if (result < 0) { ERROR(snd->card, "SNDRV_PCM_IOCTL_HW_PARAMS failed: %d\n", (int)result); kfree(params); return result; } result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) ERROR(snd->card, "Preparing capture failed: %d\n", (int)result); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); runtime->frame_bits = snd_pcm_format_physical_width(runtime->format); swparams = kzalloc(sizeof(*swparams), GFP_KERNEL); if (!swparams) { kfree(params); return -ENOMEM; } buffer_size = pcm_buffer_size(params); period_size = pcm_period_size(params); swparams->avail_min = period_size/2; swparams->xfer_align = period_size/2; swparams->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; swparams->period_step = 1; swparams->start_threshold = 1; swparams->stop_threshold = INT_MAX; swparams->silence_size = 0; swparams->silence_threshold = 0; result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, swparams); if (result < 0) ERROR(snd->card, "SNDRV_PCM_IOCTL_SW_PARAMS failed: %d\n", (int)result); kfree(swparams); kfree(params); INFO(snd->card, "capture params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return result; } /** * Set default hardware params */ static int playback_default_hw_params(struct gaudio_snd_dev *snd) { struct snd_pcm_hw_params *params; struct gaudio *card = container_of(snd, struct gaudio, playback); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); Loading @@ -149,6 +368,110 @@ static int playback_default_hw_params(struct gaudio_snd_dev *snd) return 0; } static int capture_default_hw_params(struct gaudio_snd_dev *snd) { struct snd_pcm_hw_params *params; struct gaudio *card = container_of(snd, struct gaudio, capture); struct f_uac1_legacy_opts *opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); /* * SNDRV_PCM_ACCESS_RW_INTERLEAVED, * SNDRV_PCM_FORMAT_S16_LE * CHANNELS: 1 * RATE: 16K default, user configurable */ snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; snd->format = SNDRV_PCM_FORMAT_S16_LE; snd->channels = 1; snd->rate = opts->sample_rate; params = kzalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, snd->access, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, snd->format, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, snd->channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, snd->rate, 0); /* Store the hardware parameters */ snd->access = params_access(params); snd->format = params_format(params); snd->channels = params_channels(params); snd->rate = params_rate(params); kfree(params); INFO(snd->card, "capture params: access %x, format %x, channels %d, rate %d\n", snd->access, snd->format, snd->channels, snd->rate); return 0; } static int gaudio_open_playback_streams(struct gaudio *the_card) { struct gaudio_snd_dev *snd; int res = 0; if (!the_card) { pr_err("%s: Card is NULL", __func__); return -ENODEV; } pr_debug("Initialize hw params"); /* Open PCM playback device and setup substream */ snd = &the_card->playback; res = playback_prepare_params(snd); if (res) { pr_err("Setting playback params failed: err %d", res); return res; } pr_debug("Initialized playback params"); return 0; } static int gaudio_open_capture_streams(struct gaudio *the_card) { struct gaudio_snd_dev *snd; int res = 0; if (!the_card) { pr_err("%s: Card is NULL", __func__); return -ENODEV; } pr_debug("Initialize hw params"); /* Open PCM capture device and setup substream */ snd = &the_card->capture; res = capture_prepare_params(snd); if (res) { pr_err("Setting capture params failed: err %d", res); return res; } pr_info("Initialized capture params"); return 0; } void u_audio_clear(struct gaudio *card) { card->audio_reinit_playback = false; card->audio_reinit_capture = false; } /** * Playback audio buffer data by ALSA PCM device */ Loading @@ -157,12 +480,29 @@ size_t u_audio_playback(struct gaudio *card, void *buf, size_t count) struct gaudio_snd_dev *snd = &card->playback; struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; mm_segment_t old_fs; ssize_t result; snd_pcm_sframes_t frames; int err = 0; if (!count) { pr_err("Buffer is empty, no data to play"); return 0; } if (!card->audio_reinit_playback) { err = gaudio_open_playback_streams(card); if (err) { pr_err("Failed to init audio streams"); return 0; } card->audio_reinit_playback = 1; } try_again: if (runtime->status->state == SNDRV_PCM_STATE_XRUN || runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { runtime->status->state == SNDRV_PCM_STATE_SUSPENDED || runtime->status->state == SNDRV_PCM_STATE_SETUP) { result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) { Loading @@ -172,26 +512,107 @@ size_t u_audio_playback(struct gaudio *card, void *buf, size_t count) } } if (!runtime->frame_bits) { ERROR(card, "SND failure - runtime->frame_bits == 0"); return 0; } frames = bytes_to_frames(runtime, count); result = snd_pcm_kernel_write(snd->substream, buf, frames); pr_debug("runtime->frame_bits = %d, count = %d, frames = %d", runtime->frame_bits, (int)count, (int)frames); old_fs = get_fs(); set_fs(KERNEL_DS); result = snd_pcm_lib_write(snd->substream, (void __user *)buf, frames); if (result != frames) { ERROR(card, "Playback error: %d\n", (int)result); set_fs(old_fs); goto try_again; } set_fs(old_fs); pr_debug("Done. Sent %d frames", (int)frames); return 0; } size_t u_audio_capture(struct gaudio *card, void *buf, size_t count) { ssize_t result; mm_segment_t old_fs; snd_pcm_sframes_t frames; int err = 0; struct gaudio_snd_dev *snd = &card->capture; struct snd_pcm_substream *substream = snd->substream; struct snd_pcm_runtime *runtime = substream->runtime; if (!card->audio_reinit_capture) { err = gaudio_open_capture_streams(card); if (err) { pr_err("Failed to init audio streams: err %d", err); return 0; } card->audio_reinit_capture = 1; } try_again: if (runtime->status->state == SNDRV_PCM_STATE_XRUN || runtime->status->state == SNDRV_PCM_STATE_SUSPENDED || runtime->status->state == SNDRV_PCM_STATE_SETUP) { result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (result < 0) { pr_err("Preparing capture failed: %d\n", (int)result); return result; } } frames = bytes_to_frames(runtime, count); old_fs = get_fs(); set_fs(KERNEL_DS); pr_debug("frames = %d, count = %zd", (int)frames, count); result = snd_pcm_lib_read(substream, buf, frames); if (result != frames) { pr_err("Capture error: %d\n", (int)result); set_fs(old_fs); goto try_again; } set_fs(old_fs); return 0; } int u_audio_get_playback_channels(struct gaudio *card) { pr_debug("Return %d", card->playback.channels); return card->playback.channels; } int u_audio_get_playback_rate(struct gaudio *card) { pr_debug("Return %d", card->playback.rate); return card->playback.rate; } int u_audio_get_capture_channels(struct gaudio *card) { pr_debug("Return %d", card->capture.channels); return card->capture.channels; } int u_audio_get_capture_rate(struct gaudio *card) { pr_debug("Return %d", card->capture.rate); return card->capture.rate; } /** * Open ALSA PCM and control device files * Initial the PCM or control device Loading @@ -202,9 +623,10 @@ static int gaudio_open_snd_dev(struct gaudio *card) struct gaudio_snd_dev *snd; struct f_uac1_legacy_opts *opts; char *fn_play, *fn_cap, *fn_cntl; int res = 0; opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); opts = container_of(card->func.fi, struct f_uac1_legacy_opts, func_inst); fn_play = opts->fn_play; fn_cap = opts->fn_cap; fn_cntl = opts->fn_cntl; Loading @@ -231,10 +653,16 @@ static int gaudio_open_snd_dev(struct gaudio *card) snd->filp = NULL; return ret; } pr_debug("Initialized PCM playback device: %s\n", fn_play); pcm_file = snd->filp->private_data; snd->substream = pcm_file->substream; snd->card = card; playback_default_hw_params(snd); res = playback_default_hw_params(snd); if (res) { ERROR(card, "Setting playback HW params failed: err %d", res); return res; } /* Open PCM capture device and setup substream */ snd = &card->capture; Loading @@ -244,13 +672,19 @@ static int gaudio_open_snd_dev(struct gaudio *card) snd->substream = NULL; snd->card = NULL; snd->filp = NULL; } else { return PTR_ERR(snd->filp); } pcm_file = snd->filp->private_data; snd->substream = pcm_file->substream; snd->card = card; } return 0; res = capture_default_hw_params(snd); if (res) ERROR(card, "Setting capture HW params failed: err %d", res); return res; } /** Loading @@ -260,20 +694,30 @@ static int gaudio_close_snd_dev(struct gaudio *gau) { struct gaudio_snd_dev *snd; pr_debug("Enter"); /* Close control device */ snd = &gau->control; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; /* Close PCM playback device and setup substream */ snd = &gau->playback; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; /* Close PCM capture device and setup substream */ snd = &gau->capture; if (snd->filp) if (snd->filp) { filp_close(snd->filp, NULL); snd->filp = NULL; } snd->card = NULL; return 0; } Loading @@ -287,8 +731,16 @@ static int gaudio_close_snd_dev(struct gaudio *gau) */ int gaudio_setup(struct gaudio *card) { struct gaudio_snd_dev *snd; int ret; snd = &card->control; if (snd->card) { pr_debug("snd devices already opened\n"); return 0; } pr_debug("trying to open snd devices\n"); ret = gaudio_open_snd_dev(card); if (ret) ERROR(card, "we need at least one control device\n"); Loading
drivers/usb/gadget/function/u_uac1_legacy.h +26 −11 Original line number Diff line number Diff line /* * u_uac1.h -- interface to USB gadget "ALSA AUDIO" utilities * u_uac1_legacy.h -- interface to USB gadget "ALSA AUDIO" utilities * * Copyright (c) 2020, The Linux Foundation. All rights reserved. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> * Copyright (C) 2008 Analog Devices, Inc * Loading @@ -8,7 +9,6 @@ * * Licensed under the GPL-2 or later. */ #ifndef __U_UAC1_LEGACY_H #define __U_UAC1_LEGACY_H Loading @@ -21,13 +21,17 @@ #include <sound/pcm.h> #include <sound/pcm_params.h> #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p" #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c" #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D5p" #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D6c" #define FILE_CONTROL "/dev/snd/controlC0" #define UAC1_OUT_EP_MAX_PACKET_SIZE 200 #define UAC1_REQ_COUNT 256 #define UAC1_AUDIO_BUF_SIZE 48000 #define UAC1_IN_EP_MAX_PACKET_SIZE 32 #define UAC1_OUT_EP_MAX_PACKET_SIZE 32 #define UAC1_OUT_REQ_COUNT 48 #define UAC1_IN_REQ_COUNT 4 #define UAC1_AUDIO_PLAYBACK_BUF_SIZE 256 /* Matches with Audio driver */ #define UAC1_AUDIO_CAPTURE_BUF_SIZE 256 /* Matches with Audio driver */ #define UAC1_SAMPLE_RATE 16000 /* * This represents the USB side of an audio card device, managed by a USB Loading @@ -53,14 +57,20 @@ struct gaudio { struct gaudio_snd_dev playback; struct gaudio_snd_dev capture; /* TODO */ bool audio_reinit_capture; bool audio_reinit_playback; }; struct f_uac1_legacy_opts { struct usb_function_instance func_inst; int req_buf_size; int req_count; int audio_buf_size; int req_playback_buf_size; int req_capture_buf_size; int req_playback_count; int req_capture_count; int audio_playback_buf_size; int audio_capture_buf_size; int audio_playback_realtime; int sample_rate; char *fn_play; char *fn_cap; char *fn_cntl; Loading @@ -68,6 +78,7 @@ struct f_uac1_legacy_opts { unsigned fn_play_alloc:1; unsigned fn_cap_alloc:1; unsigned fn_cntl_alloc:1; struct gaudio *card; struct mutex lock; int refcnt; }; Loading @@ -75,8 +86,12 @@ struct f_uac1_legacy_opts { int gaudio_setup(struct gaudio *card); void gaudio_cleanup(struct gaudio *the_card); void u_audio_clear(struct gaudio *card); size_t u_audio_playback(struct gaudio *card, void *buf, size_t count); size_t u_audio_capture(struct gaudio *card, void *buf, size_t count); int u_audio_get_playback_channels(struct gaudio *card); int u_audio_get_playback_rate(struct gaudio *card); int u_audio_get_capture_channels(struct gaudio *card); int u_audio_get_capture_rate(struct gaudio *card); #endif /* __U_UAC1_LEGACY_H */